Output Window: Fix sensitivity of Save button. Also add option for png and svg files.
[pspp] / src / ui / gui / psppire-output-window.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2016,
3    2021  Free Software Foundation
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
20 #include "ui/gui/psppire-output-window.h"
21
22 #include <errno.h>
23 #include <gtk/gtk.h>
24 #include <stdlib.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include "libpspp/cast.h"
30 #include "libpspp/message.h"
31 #include "libpspp/string-map.h"
32 #include "output/driver-provider.h"
33 #include "output/output-item.h"
34 #include "ui/gui/help-menu.h"
35 #include "ui/gui/builder-wrapper.h"
36 #include "ui/gui/psppire-output-view.h"
37 #include "ui/gui/psppire-conf.h"
38 #include "ui/gui/windows-menu.h"
39
40 #include "gl/xalloc.h"
41
42 #include "helper.h"
43
44 #include <gettext.h>
45 #define _(msgid) gettext (msgid)
46 #define N_(msgid) msgid
47
48 G_DEFINE_TYPE (PsppireOutputWindow, psppire_output_window, PSPPIRE_TYPE_WINDOW)
49
50 static GObjectClass *parent_class;
51
52 static void
53 psppire_output_window_finalize (GObject *object)
54 {
55   if (G_OBJECT_CLASS (parent_class)->finalize)
56     (*G_OBJECT_CLASS (parent_class)->finalize) (object);
57 }
58
59
60 static void
61 psppire_output_window_dispose (GObject *obj)
62 {
63   PsppireOutputWindow *window = PSPPIRE_OUTPUT_WINDOW (obj);
64
65   if (window->dispose_has_run)
66     return;
67
68   window->dispose_has_run = TRUE;
69   psppire_output_view_destroy (window->view);
70   window->view = NULL;
71
72   /* Chain up to the parent class */
73   G_OBJECT_CLASS (parent_class)->dispose (obj);
74 }
75
76 static void
77 psppire_output_window_class_init (PsppireOutputWindowClass *class)
78 {
79   GObjectClass *object_class = G_OBJECT_CLASS (class);
80
81   parent_class = g_type_class_peek_parent (class);
82   object_class->dispose = psppire_output_window_dispose;
83
84   object_class->finalize = psppire_output_window_finalize;
85 }
86 \f
87 /* Output driver class. */
88
89 struct psppire_output_driver
90   {
91     struct output_driver driver;
92     PsppireOutputWindow *window;
93   };
94
95 static struct output_driver_class psppire_output_class;
96
97 static struct psppire_output_driver *
98 psppire_output_cast (struct output_driver *driver)
99 {
100   assert (driver->class == &psppire_output_class);
101   return UP_CAST (driver, struct psppire_output_driver, driver);
102 }
103
104 static void
105 psppire_output_submit (struct output_driver *this,
106                        const struct output_item *item)
107 {
108   struct psppire_output_driver *pod = psppire_output_cast (this);
109   PsppireOutputWindow *window;
110   bool new;
111
112   new = pod->window == NULL;
113   if (new)
114     {
115       pod->window = PSPPIRE_OUTPUT_WINDOW (psppire_output_window_new ());
116       GApplication *app = g_application_get_default ();
117       gtk_application_add_window (GTK_APPLICATION (app),
118                                   GTK_WINDOW (pod->window));
119
120       pod->window->driver = pod;
121     }
122   window = pod->window;
123
124   psppire_output_view_put (window->view, item);
125
126   if (new)
127     {
128       /* We could have called this earlier in the previous "if (new)" block,
129          but doing it here finds, in a plain GTK+ environment, a bug that
130          otherwise only showed up on an Ubuntu Unity desktop.  See bug
131          #43362. */
132       gtk_widget_show_all (GTK_WIDGET (pod->window));
133     }
134
135   PsppireConf *conf = psppire_conf_new ();
136   {
137     gboolean status = true;
138     psppire_conf_get_boolean (conf, "OutputWindowAction", "alert",
139                               &status);
140     gtk_window_set_urgency_hint (GTK_WINDOW (pod->window), status);
141   }
142
143   {
144     gboolean status ;
145     if (psppire_conf_get_boolean (conf, "OutputWindowAction", "maximize",
146                                   &status) && status)
147       gtk_window_maximize (GTK_WINDOW (pod->window));
148   }
149
150   {
151     gboolean status ;
152     if (psppire_conf_get_boolean (conf, "OutputWindowAction", "raise",
153                                   &status) && status)
154       gtk_window_present (GTK_WINDOW (pod->window));
155   }
156 }
157
158 static struct output_driver_class psppire_output_class =
159   {
160     .name = "PSPPIRE",
161     .submit = psppire_output_submit,
162     .handles_groups = true,
163     .handles_show = true,
164   };
165
166 void
167 psppire_output_window_setup (void)
168 {
169   struct psppire_output_driver *pod;
170   struct output_driver *d;
171
172   pod = xzalloc (sizeof *pod);
173   d = &pod->driver;
174   output_driver_init (d, &psppire_output_class, "PSPPIRE",
175                       SETTINGS_DEVICE_UNFILTERED);
176   output_driver_register (d);
177 }
178
179 \f
180
181 /* Callback for the "delete" action (clicking the x on the top right
182    hand corner of the window) */
183 static gboolean
184 on_delete (GtkWidget *w, GdkEvent *event, gpointer user_data)
185 {
186   PsppireOutputWindow *ow = PSPPIRE_OUTPUT_WINDOW (user_data);
187
188   gtk_widget_destroy (GTK_WIDGET (ow));
189
190   ow->driver->window = NULL;
191
192   return FALSE;
193 }
194
195
196
197 static void
198 cancel_urgency (GtkWindow *window,  gpointer data)
199 {
200   gtk_window_set_urgency_hint (window, FALSE);
201 }
202
203 static void psppire_output_window_print (PsppireOutputWindow *window);
204
205
206 static void
207 export_output (PsppireOutputWindow *window, struct string_map *options,
208                const char *format)
209 {
210   string_map_insert (options, "format", format);
211   psppire_output_view_export (window->view, options);
212 }
213
214
215 struct file_types
216 {
217   const gchar *label;
218   const gchar *ext;
219 };
220
221 enum
222   {
223     FT_AUTO = 0,
224     FT_SPV,
225     FT_PDF,
226     FT_HTML,
227     FT_ODT,
228     FT_TXT,
229     FT_ASCII,
230     FT_PS,
231     FT_CSV,
232     FT_PNG,
233     FT_SVG,
234     n_FT
235   };
236
237 static const struct file_types ft[n_FT] = {
238   {N_("Infer file type from extension"),  NULL},
239   {N_("SPSS Viewer (*.spv)"),             ".spv"},
240   {N_("PDF (*.pdf)"),                     ".pdf"},
241   {N_("HTML (*.html)"),                   ".html"},
242   {N_("OpenDocument (*.odt)"),            ".odt"},
243   {N_("Text (*.txt)"),                    ".txt"},
244   {N_("Text [plain] (*.txt)"),            ".txt"},
245   {N_("PostScript (*.ps)"),               ".ps"},
246   {N_("Comma-Separated Values (*.csv)"),  ".csv"},
247   {N_("Portable Network Graphics (*.png)"),  ".png"},
248   {N_("Scalable Vector Graphics (*.svg)"),   ".svg"}
249 };
250
251
252 static void
253 on_combo_change (GtkFileChooser *chooser)
254 {
255   gboolean sensitive = FALSE;
256   GtkWidget *combo = gtk_file_chooser_get_extra_widget (chooser);
257
258   int file_type = FT_AUTO;
259   gchar *fn = gtk_file_chooser_get_filename (chooser);
260
261   if (combo &&  gtk_widget_get_realized (combo))
262     file_type = gtk_combo_box_get_active (GTK_COMBO_BOX (combo));
263
264   if (fn != NULL && file_type == FT_AUTO)
265     {
266       for (gint i = 1 ; i < n_FT ; ++i)
267         {
268           if (g_str_has_suffix (fn, ft[i].ext))
269             {
270               sensitive = TRUE;
271               break;
272             }
273         }
274     }
275   else
276     sensitive = (fn != NULL);
277
278   g_free (fn);
279
280   gtk_dialog_set_response_sensitive (GTK_DIALOG (chooser), GTK_RESPONSE_ACCEPT, sensitive);
281 }
282
283
284 static GtkListStore *
285 create_file_type_list (void)
286 {
287   int i;
288   GtkTreeIter iter;
289   GtkListStore *list = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
290
291   for (i = 0 ; i < n_FT ; ++i)
292     {
293       gtk_list_store_append (list, &iter);
294       gtk_list_store_set (list, &iter,
295                           0,  gettext (ft[i].label),
296                           1,  ft[i].ext,
297                           -1);
298     }
299
300   return list;
301 }
302
303 static void
304 psppire_output_window_export (PsppireOutputWindow *window)
305 {
306   gint response;
307   GtkWidget *combo;
308   GtkListStore *list;
309
310   GtkFileChooser *chooser;
311
312   GtkWidget *dialog = gtk_file_chooser_dialog_new (_("Export Output"),
313                                         GTK_WINDOW (window),
314                                         GTK_FILE_CHOOSER_ACTION_SAVE,
315                                         _("Cancel"), GTK_RESPONSE_CANCEL,
316                                         _("Save"),   GTK_RESPONSE_ACCEPT,
317                                         NULL);
318
319   g_object_set (dialog, "local-only", FALSE, NULL);
320
321   chooser = GTK_FILE_CHOOSER (dialog);
322
323   list = create_file_type_list ();
324
325   combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (list));
326
327
328   {
329     /* Create text cell renderer */
330     GtkCellRenderer *cell = gtk_cell_renderer_text_new();
331     gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE);
332
333     gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo), cell,  "text", 0);
334   }
335
336   g_signal_connect_swapped (combo, "changed", G_CALLBACK (on_combo_change), chooser);
337
338   gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
339
340   gtk_file_chooser_set_extra_widget (chooser, combo);
341
342   g_signal_connect (chooser, "selection-changed", G_CALLBACK (on_combo_change), NULL);
343   gtk_file_chooser_set_do_overwrite_confirmation (chooser, TRUE);
344
345   response = gtk_dialog_run (GTK_DIALOG (dialog));
346
347   if (response == GTK_RESPONSE_ACCEPT)
348     {
349       gint file_type = gtk_combo_box_get_active (GTK_COMBO_BOX (combo));
350       gchar *filename = gtk_file_chooser_get_filename (chooser);
351       struct string_map options;
352
353       g_return_if_fail (filename);
354
355       if (file_type == FT_AUTO)
356         {
357           /* If the "Infer file type from extension" option was chosen,
358              search for the respective type in the list.
359              (It's a O(n) search, but fortunately n is small). */
360           gint i;
361           for (i = 1 ; i < n_FT ; ++i)
362             {
363               if (g_str_has_suffix (filename, ft[i].ext))
364                 {
365                   file_type = i;
366                   break;
367                 }
368             }
369         }
370       else if (! g_str_has_suffix (filename, ft[file_type].ext))
371         {
372           /* If an explicit document format was chosen, and if the chosen
373              filename does not already have that particular "extension",
374              then append it.
375            */
376
377           gchar *of = filename;
378           filename = g_strconcat (filename, ft[file_type].ext, NULL);
379           g_free (of);
380         }
381
382       string_map_init (&options);
383       string_map_insert (&options, "output-file", filename);
384
385       switch (file_type)
386         {
387         case FT_SPV:
388           export_output (window, &options, "spv");
389           break;
390         case FT_PDF:
391           export_output (window, &options, "pdf");
392           break;
393         case FT_HTML:
394           export_output (window, &options, "html");
395           break;
396         case FT_ODT:
397           export_output (window, &options, "odt");
398           break;
399         case FT_PS:
400           export_output (window, &options, "ps");
401           break;
402         case FT_CSV:
403           export_output (window, &options, "csv");
404           break;
405         case FT_PNG:
406           export_output (window, &options, "png");
407           break;
408         case FT_SVG:
409           export_output (window, &options, "svg");
410           break;
411
412         case FT_TXT:
413           string_map_insert (&options, "box", "unicode");
414           /* Fall through */
415
416         case FT_ASCII:
417           string_map_insert (&options, "charts", "none");
418           export_output (window, &options, "txt");
419           break;
420         default:
421           g_assert_not_reached ();
422         }
423
424       string_map_destroy (&options);
425
426       free (filename);
427     }
428
429   gtk_widget_destroy (dialog);
430 }
431
432 static void
433 psppire_output_window_init (PsppireOutputWindow *window)
434 {
435   GtkBuilder *xml = builder_new ("output-window.ui");
436   GtkApplication *app = GTK_APPLICATION (g_application_get_default());
437   GtkWidget *box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
438   gtk_container_add (GTK_CONTAINER (window), box);
439
440   GtkWidget *paned = get_widget_assert (xml, "paned1");
441
442   window->dispose_has_run = FALSE;
443
444   window->view = psppire_output_view_new (
445     GTK_LAYOUT (get_widget_assert (xml, "output")),
446     GTK_TREE_VIEW (get_widget_assert (xml, "overview")));
447
448   g_signal_connect (window,
449                     "focus-in-event",
450                     G_CALLBACK (cancel_urgency),
451                     NULL);
452
453   GObject *menu = get_object_assert (xml, "output-window-menu", G_TYPE_MENU);
454   GtkWidget *menubar = gtk_menu_bar_new_from_model (G_MENU_MODEL (menu));
455   gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, FALSE, 0);
456   gtk_box_pack_start (GTK_BOX (box), paned, TRUE, TRUE, 0);
457
458   gtk_menu_shell_append (GTK_MENU_SHELL (menubar),
459                          create_windows_menu (GTK_WINDOW (window)));
460
461   gtk_menu_shell_append (GTK_MENU_SHELL (menubar),
462                          create_help_menu (GTK_WINDOW (window)));
463
464   {
465     GSimpleAction *print = g_simple_action_new ("print", NULL);
466     g_signal_connect_swapped (print, "activate", G_CALLBACK (psppire_output_window_print), window);
467     g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (print));
468
469
470     const gchar *accels[2] = { "<Primary>P", NULL};
471     gtk_application_set_accels_for_action (app,
472                                            "win.print",
473                                            accels);
474   }
475
476
477   {
478     GSimpleAction *export = g_simple_action_new ("export", NULL);
479     g_signal_connect_swapped (export, "activate", G_CALLBACK (psppire_output_window_export), window);
480     g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (export));
481   }
482
483   {
484     GSimpleAction *select_all = g_simple_action_new ("select-all", NULL);
485     g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (select_all));
486   }
487
488   {
489     GSimpleAction *copy = g_simple_action_new ("copy", NULL);
490     g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (copy));
491
492     const gchar *accels[2] = { "<Primary>C", NULL};
493     gtk_application_set_accels_for_action (app,
494                                            "win.copy",
495                                            accels);
496   }
497
498
499   g_object_unref (xml);
500
501   g_signal_connect (window, "delete-event",
502                     G_CALLBACK (on_delete), window);
503 }
504
505
506 GtkWidget*
507 psppire_output_window_new (void)
508 {
509   return GTK_WIDGET (g_object_new (psppire_output_window_get_type (),
510                                    /* TRANSLATORS: This will be part of a filename.  Please avoid whitespace. */
511                                    "filename", _("Output"),
512                                    "description", _("Output Viewer"),
513                                    NULL));
514 }
515
516 static void
517 psppire_output_window_print (PsppireOutputWindow *window)
518 {
519   psppire_output_view_print (window->view, GTK_WINDOW (window));
520 }