1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2016 Free Software Foundation
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.
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.
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/>. */
19 #include "ui/gui/psppire-output-window.h"
25 #include <sys/types.h>
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"
43 #include "gl/xalloc.h"
48 #define _(msgid) gettext (msgid)
49 #define N_(msgid) msgid
51 static void psppire_output_window_class_init (PsppireOutputWindowClass *class);
52 static void psppire_output_window_init (PsppireOutputWindow *window);
55 psppire_output_window_get_type (void)
57 static GType psppire_output_window_type = 0;
59 if (!psppire_output_window_type)
61 static const GTypeInfo psppire_output_window_info =
63 sizeof (PsppireOutputWindowClass),
65 (GBaseFinalizeFunc) NULL,
66 (GClassInitFunc)psppire_output_window_class_init,
67 (GClassFinalizeFunc) NULL,
69 sizeof (PsppireOutputWindow),
71 (GInstanceInitFunc) psppire_output_window_init,
74 psppire_output_window_type =
75 g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireOutputWindow",
76 &psppire_output_window_info, 0);
79 return psppire_output_window_type;
82 static GObjectClass *parent_class;
85 psppire_output_window_finalize (GObject *object)
87 if (G_OBJECT_CLASS (parent_class)->finalize)
88 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
93 psppire_output_window_dispose (GObject *obj)
95 PsppireOutputWindow *window = PSPPIRE_OUTPUT_WINDOW (obj);
97 if (window->dispose_has_run)
100 window->dispose_has_run = TRUE;
101 psppire_output_view_destroy (window->view);
104 /* Chain up to the parent class */
105 G_OBJECT_CLASS (parent_class)->dispose (obj);
109 psppire_output_window_class_init (PsppireOutputWindowClass *class)
111 GObjectClass *object_class = G_OBJECT_CLASS (class);
113 parent_class = g_type_class_peek_parent (class);
114 object_class->dispose = psppire_output_window_dispose;
116 object_class->finalize = psppire_output_window_finalize;
119 /* Output driver class. */
121 struct psppire_output_driver
123 struct output_driver driver;
124 PsppireOutputWindow *window;
127 static struct output_driver_class psppire_output_class;
129 static struct psppire_output_driver *
130 psppire_output_cast (struct output_driver *driver)
132 assert (driver->class == &psppire_output_class);
133 return UP_CAST (driver, struct psppire_output_driver, driver);
137 psppire_output_submit (struct output_driver *this,
138 const struct output_item *item)
140 struct psppire_output_driver *pod = psppire_output_cast (this);
141 PsppireOutputWindow *window;
144 new = pod->window == NULL;
147 pod->window = PSPPIRE_OUTPUT_WINDOW (psppire_output_window_new ());
148 GApplication *app = g_application_get_default ();
149 gtk_application_add_window (GTK_APPLICATION (app),
150 GTK_WINDOW (pod->window));
152 pod->window->driver = pod;
154 window = pod->window;
156 psppire_output_view_put (window->view, item);
160 /* We could have called this earlier in the previous "if (new)" block,
161 but doing it here finds, in a plain GTK+ environment, a bug that
162 otherwise only showed up on an Ubuntu Unity desktop. See bug
164 gtk_widget_show_all (GTK_WIDGET (pod->window));
167 gtk_window_set_urgency_hint (GTK_WINDOW (pod->window), TRUE);
170 static struct output_driver_class psppire_output_class =
172 "PSPPIRE", /* name */
174 psppire_output_submit, /* submit */
179 psppire_output_window_setup (void)
181 struct psppire_output_driver *pod;
182 struct output_driver *d;
184 pod = xzalloc (sizeof *pod);
186 output_driver_init (d, &psppire_output_class, "PSPPIRE",
187 SETTINGS_DEVICE_UNFILTERED);
188 output_driver_register (d);
193 /* Callback for the "delete" action (clicking the x on the top right
194 hand corner of the window) */
196 on_delete (GtkWidget *w, GdkEvent *event, gpointer user_data)
198 PsppireOutputWindow *ow = PSPPIRE_OUTPUT_WINDOW (user_data);
200 gtk_widget_destroy (GTK_WIDGET (ow));
202 ow->driver->window = NULL;
210 cancel_urgency (GtkWindow *window, gpointer data)
212 gtk_window_set_urgency_hint (window, FALSE);
215 static void psppire_output_window_print (PsppireOutputWindow *window);
219 export_output (PsppireOutputWindow *window, struct string_map *options,
222 string_map_insert (options, "format", format);
223 psppire_output_view_export (window->view, options);
246 #define N_EXTENSIONS (n_FT - 1)
248 struct file_types ft[n_FT] = {
249 {N_("Infer file type from extension"), NULL},
250 {N_("PDF (*.pdf)"), ".pdf"},
251 {N_("HTML (*.html)"), ".html"},
252 {N_("OpenDocument (*.odt)"), ".odt"},
253 {N_("Text (*.txt)"), ".txt"},
254 {N_("Text [plain] (*.txt)"), ".txt"},
255 {N_("PostScript (*.ps)"), ".ps"},
256 {N_("Comma-Separated Values (*.csv)"), ".csv"}
261 on_combo_change (GtkFileChooser *chooser)
263 gboolean sensitive = FALSE;
264 GtkWidget *combo = gtk_file_chooser_get_extra_widget (chooser);
267 gchar *fn = gtk_file_chooser_get_filename (chooser);
269 if (combo && gtk_widget_get_realized (combo))
270 x = gtk_combo_box_get_active (GTK_COMBO_BOX (combo));
282 for (i = 1 ; i < N_EXTENSIONS ; ++i)
284 if ( g_str_has_suffix (fn, ft[i].ext))
294 gtk_dialog_set_response_sensitive (GTK_DIALOG (chooser), GTK_RESPONSE_ACCEPT, sensitive);
299 on_file_chooser_change (GObject *w, GParamSpec *pspec, gpointer data)
302 GtkFileChooser *chooser = data;
303 const gchar *name = g_param_spec_get_name (pspec);
305 if ( ! gtk_widget_get_realized (GTK_WIDGET (chooser)))
308 /* Ignore this one. It causes recursion. */
309 if ( 0 == strcmp ("tooltip-text", name))
312 on_combo_change (chooser);
316 /* Recursively descend all the children of W, connecting
317 to their "notify" signal */
319 iterate_widgets (GtkWidget *w, gpointer data)
321 if ( GTK_IS_CONTAINER (w))
322 gtk_container_forall (GTK_CONTAINER (w), iterate_widgets, data);
324 g_signal_connect (w, "notify", G_CALLBACK (on_file_chooser_change), data);
329 static GtkListStore *
330 create_file_type_list (void)
334 GtkListStore *list = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
336 for (i = 0 ; i < n_FT ; ++i)
338 gtk_list_store_append (list, &iter);
339 gtk_list_store_set (list, &iter,
340 0, gettext (ft[i].label),
349 psppire_output_window_export (PsppireOutputWindow *window)
355 GtkFileChooser *chooser;
357 GtkWidget *dialog = gtk_file_chooser_dialog_new (_("Export Output"),
359 GTK_FILE_CHOOSER_ACTION_SAVE,
360 _("Cancel"), GTK_RESPONSE_CANCEL,
361 _("Save"), GTK_RESPONSE_ACCEPT,
364 g_object_set (dialog, "local-only", FALSE, NULL);
366 chooser = GTK_FILE_CHOOSER (dialog);
368 list = create_file_type_list ();
370 combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (list));
374 /* Create text cell renderer */
375 GtkCellRenderer *cell = gtk_cell_renderer_text_new();
376 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE );
378 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo), cell, "text", 0);
381 g_signal_connect_swapped (combo, "changed", G_CALLBACK (on_combo_change), chooser);
383 gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
385 gtk_file_chooser_set_extra_widget (chooser, combo);
387 /* This kludge is necessary because there is no signal to tell us
388 when the candidate filename of a GtkFileChooser has changed */
389 gtk_container_forall (GTK_CONTAINER (dialog), iterate_widgets, dialog);
392 gtk_file_chooser_set_do_overwrite_confirmation (chooser, TRUE);
394 response = gtk_dialog_run (GTK_DIALOG (dialog));
396 if ( response == GTK_RESPONSE_ACCEPT )
398 gint file_type = gtk_combo_box_get_active (GTK_COMBO_BOX (combo));
399 gchar *filename = gtk_file_chooser_get_filename (chooser);
400 struct string_map options;
402 g_return_if_fail (filename);
404 if (file_type == FT_AUTO)
406 /* If the "Infer file type from extension" option was chosen,
407 search for the respective type in the list.
408 (It's a O(n) search, but fortunately n is small). */
410 for (i = 1 ; i < N_EXTENSIONS ; ++i)
412 if ( g_str_has_suffix (filename, ft[i].ext))
419 else if (! g_str_has_suffix (filename, ft[file_type].ext))
421 /* If an explicit document format was chosen, and if the chosen
422 filename does not already have that particular "extension",
426 gchar *of = filename;
427 filename = g_strconcat (filename, ft[file_type].ext, NULL);
431 string_map_init (&options);
432 string_map_insert (&options, "output-file", filename);
437 export_output (window, &options, "pdf");
440 export_output (window, &options, "html");
443 export_output (window, &options, "odt");
446 export_output (window, &options, "ps");
449 export_output (window, &options, "csv");
453 string_map_insert (&options, "box", "unicode");
457 string_map_insert (&options, "headers", "false");
458 string_map_insert (&options, "paginate", "false");
459 string_map_insert (&options, "squeeze", "true");
460 string_map_insert (&options, "emphasis", "none");
461 string_map_insert (&options, "charts", "none");
462 string_map_insert (&options, "top-margin", "0");
463 string_map_insert (&options, "bottom-margin", "0");
464 export_output (window, &options, "txt");
467 g_assert_not_reached ();
470 string_map_destroy (&options);
475 gtk_widget_destroy (dialog);
479 psppire_output_window_init (PsppireOutputWindow *window)
481 GtkBuilder *xml = builder_new ("output-window.ui");
482 GtkApplication *app = GTK_APPLICATION (g_application_get_default());
483 GtkWidget *box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
484 gtk_container_add (GTK_CONTAINER (window), box);
486 GtkWidget *paned = get_widget_assert (xml, "paned1");
488 window->dispose_has_run = FALSE;
490 window->view = psppire_output_view_new (
491 GTK_LAYOUT (get_widget_assert (xml, "output")),
492 GTK_TREE_VIEW (get_widget_assert (xml, "overview")));
497 g_signal_connect (window,
499 G_CALLBACK (cancel_urgency),
502 GObject *menu = get_object_assert (xml, "output-window-menu", G_TYPE_MENU);
503 GtkWidget *menubar = gtk_menu_bar_new_from_model (G_MENU_MODEL (menu));
504 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, FALSE, 0);
505 gtk_box_pack_start (GTK_BOX (box), paned, TRUE, TRUE, 0);
507 gtk_menu_shell_append (GTK_MENU_SHELL (menubar),
508 create_windows_menu (GTK_WINDOW (window)));
510 gtk_menu_shell_append (GTK_MENU_SHELL (menubar),
511 create_help_menu (GTK_WINDOW (window)));
514 GSimpleAction *print = g_simple_action_new ("print", NULL);
515 g_signal_connect_swapped (print, "activate", G_CALLBACK (psppire_output_window_print), window);
516 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (print));
519 const gchar *accels[2] = { "<Ctrl>P", NULL};
520 gtk_application_set_accels_for_action (app,
527 GSimpleAction *export = g_simple_action_new ("export", NULL);
528 g_signal_connect_swapped (export, "activate", G_CALLBACK (psppire_output_window_export), window);
529 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (export));
533 GSimpleAction *select_all = g_simple_action_new ("select-all", NULL);
534 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (select_all));
538 GSimpleAction *copy = g_simple_action_new ("copy", NULL);
539 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (copy));
541 const gchar *accels[2] = { "<Ctrl>C", NULL};
542 gtk_application_set_accels_for_action (app,
548 g_object_unref (xml);
550 g_signal_connect (window, "delete-event",
551 G_CALLBACK (on_delete), window);
556 psppire_output_window_new (void)
558 return GTK_WIDGET (g_object_new (psppire_output_window_get_type (),
559 /* TRANSLATORS: This will be part of a filename. Please avoid whitespace. */
560 "filename", _("Output"),
561 "description", _("Output Viewer"),
566 psppire_output_window_print (PsppireOutputWindow *window)
568 psppire_output_view_print (window->view, GTK_WINDOW (window));