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