1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2009, 2010, 2011 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/>. */
23 #include <sys/types.h>
26 #include "libpspp/cast.h"
27 #include "libpspp/message.h"
28 #include "libpspp/string-map.h"
29 #include "output/cairo.h"
30 #include "output/chart-item.h"
31 #include "output/driver-provider.h"
32 #include "output/message-item.h"
33 #include "output/output-item.h"
34 #include "output/tab.h"
35 #include "output/table-item.h"
36 #include "output/text-item.h"
37 #include "ui/gui/help-menu.h"
38 #include "ui/gui/helper.h"
39 #include "ui/gui/psppire-output-window.h"
42 #include "gl/tmpdir.h"
43 #include "gl/xalloc.h"
46 #define _(msgid) gettext (msgid)
47 #define N_(msgid) msgid
51 COL_TITLE, /* Table title. */
52 COL_ADDR, /* Pointer to the table */
53 COL_Y, /* Y position of top of title. */
57 static void psppire_output_window_base_finalize (PsppireOutputWindowClass *, gpointer);
58 static void psppire_output_window_base_init (PsppireOutputWindowClass *class);
59 static void psppire_output_window_class_init (PsppireOutputWindowClass *class);
60 static void psppire_output_window_init (PsppireOutputWindow *window);
64 psppire_output_window_get_type (void)
66 static GType psppire_output_window_type = 0;
68 if (!psppire_output_window_type)
70 static const GTypeInfo psppire_output_window_info =
72 sizeof (PsppireOutputWindowClass),
73 (GBaseInitFunc) psppire_output_window_base_init,
74 (GBaseFinalizeFunc) psppire_output_window_base_finalize,
75 (GClassInitFunc)psppire_output_window_class_init,
76 (GClassFinalizeFunc) NULL,
78 sizeof (PsppireOutputWindow),
80 (GInstanceInitFunc) psppire_output_window_init,
83 psppire_output_window_type =
84 g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireOutputWindow",
85 &psppire_output_window_info, 0);
88 return psppire_output_window_type;
91 static GObjectClass *parent_class;
94 psppire_output_window_finalize (GObject *object)
96 if (G_OBJECT_CLASS (parent_class)->finalize)
97 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
102 psppire_output_window_dispose (GObject *obj)
104 PsppireOutputWindow *viewer = PSPPIRE_OUTPUT_WINDOW (obj);
107 for (i = 0; i < viewer->n_items; i++)
108 output_item_unref (viewer->items[i]);
109 free (viewer->items);
110 viewer->items = NULL;
111 viewer->n_items = viewer->allocated_items = 0;
113 if (viewer->print_settings != NULL)
114 g_object_unref (viewer->print_settings);
116 /* Chain up to the parent class */
117 G_OBJECT_CLASS (parent_class)->dispose (obj);
121 psppire_output_window_class_init (PsppireOutputWindowClass *class)
123 GObjectClass *object_class = G_OBJECT_CLASS (class);
125 parent_class = g_type_class_peek_parent (class);
126 object_class->dispose = psppire_output_window_dispose;
131 psppire_output_window_base_init (PsppireOutputWindowClass *class)
133 GObjectClass *object_class = G_OBJECT_CLASS (class);
135 object_class->finalize = psppire_output_window_finalize;
141 psppire_output_window_base_finalize (PsppireOutputWindowClass *class,
146 /* Output driver class. */
148 struct psppire_output_driver
150 struct output_driver driver;
151 PsppireOutputWindow *viewer;
152 struct xr_driver *xr;
156 static struct output_driver_class psppire_output_class;
158 static struct psppire_output_driver *
159 psppire_output_cast (struct output_driver *driver)
161 assert (driver->class == &psppire_output_class);
162 return UP_CAST (driver, struct psppire_output_driver, driver);
166 expose_event_callback (GtkWidget *widget, GdkEventExpose *event, gpointer data)
168 struct xr_rendering *r = g_object_get_data (G_OBJECT (widget), "rendering");
171 cr = gdk_cairo_create (widget->window);
172 xr_rendering_draw (r, cr, event->area.x, event->area.y,
173 event->area.width, event->area.height);
180 psppire_output_submit (struct output_driver *this,
181 const struct output_item *item)
183 struct psppire_output_driver *pod = psppire_output_cast (this);
184 PsppireOutputWindow *viewer;
185 GtkWidget *drawing_area;
186 struct xr_rendering *r;
194 if (pod->viewer == NULL)
196 pod->viewer = PSPPIRE_OUTPUT_WINDOW (psppire_output_window_new ());
197 gtk_widget_show_all (GTK_WIDGET (pod->viewer));
198 pod->viewer->driver = pod;
200 viewer = pod->viewer;
202 if (viewer->n_items >= viewer->allocated_items)
203 viewer->items = x2nrealloc (viewer->items, &viewer->allocated_items,
204 sizeof *viewer->items);
205 viewer->items[viewer->n_items++] = output_item_ref (item);
207 if (is_text_item (item))
209 const struct text_item *text_item = to_text_item (item);
210 enum text_item_type type = text_item_get_type (text_item);
211 const char *text = text_item_get_text (text_item);
213 if (type == TEXT_ITEM_COMMAND_CLOSE)
215 viewer->in_command = false;
218 else if (text[0] == '\0')
222 cr = gdk_cairo_create (GTK_WIDGET (pod->viewer)->window);
225 const GtkStyle *style = gtk_widget_get_style (GTK_WIDGET (viewer));
226 struct string_map options = STRING_MAP_INITIALIZER (options);
227 struct text_item *text_item;
228 PangoFontDescription *font_desc;
232 /* Use GTK+ default font as proportional font. */
233 font_name = pango_font_description_to_string (style->font_desc);
234 string_map_insert (&options, "prop-font", font_name);
237 /* Derived emphasized font from proportional font. */
238 font_desc = pango_font_description_copy (style->font_desc);
239 pango_font_description_set_style (font_desc, PANGO_STYLE_ITALIC);
240 font_name = pango_font_description_to_string (font_desc);
241 string_map_insert (&options, "emph-font", font_name);
243 pango_font_description_free (font_desc);
245 /* Pretend that the "page" has a reasonable width and a very big length,
246 so that most tables can be conveniently viewed on-screen with vertical
247 scrolling only. (The length should not be increased very much because
248 it is already close enough to INT_MAX when expressed as thousands of a
250 string_map_insert (&options, "paper-size", "300x200000mm");
251 string_map_insert (&options, "left-margin", "0");
252 string_map_insert (&options, "right-margin", "0");
253 string_map_insert (&options, "top-margin", "0");
254 string_map_insert (&options, "bottom-margin", "0");
256 pod->xr = xr_driver_create (cr, &options);
258 string_map_destroy (&options);
260 text_item = text_item_create (TEXT_ITEM_PARAGRAPH, "X");
261 r = xr_rendering_create (pod->xr, text_item_super (text_item), cr);
262 xr_rendering_measure (r, &font_width, &pod->font_height);
263 /* xr_rendering_destroy (r); */
264 text_item_unref (text_item);
267 pod->viewer->y += pod->font_height / 2;
269 r = xr_rendering_create (pod->xr, item, cr);
273 xr_rendering_measure (r, &tw, &th);
275 drawing_area = gtk_drawing_area_new ();
276 gtk_widget_modify_bg (
277 GTK_WIDGET (drawing_area), GTK_STATE_NORMAL,
278 >k_widget_get_style (drawing_area)->base[GTK_STATE_NORMAL]);
279 g_object_set_data (G_OBJECT (drawing_area), "rendering", r);
280 gtk_widget_set_size_request (drawing_area, tw, th);
281 gtk_layout_put (pod->viewer->output, drawing_area, 0, pod->viewer->y);
282 gtk_widget_show (drawing_area);
283 g_signal_connect (G_OBJECT (drawing_area), "expose_event",
284 G_CALLBACK (expose_event_callback), NULL);
286 if (!is_text_item (item)
287 || text_item_get_type (to_text_item (item)) != TEXT_ITEM_SYNTAX
288 || !viewer->in_command)
290 store = GTK_TREE_STORE (gtk_tree_view_get_model (viewer->overview));
292 ds_init_empty (&title);
293 if (is_text_item (item)
294 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_COMMAND_OPEN)
296 gtk_tree_store_append (store, &iter, NULL);
297 viewer->cur_command = iter; /* XXX shouldn't save a GtkTreeIter */
298 viewer->in_command = true;
302 GtkTreeIter *p = viewer->in_command ? &viewer->cur_command : NULL;
303 gtk_tree_store_append (store, &iter, p);
307 if (is_text_item (item))
308 ds_put_cstr (&title, text_item_get_text (to_text_item (item)));
309 else if (is_message_item (item))
311 const struct message_item *msg_item = to_message_item (item);
312 const struct msg *msg = message_item_get_msg (msg_item);
313 ds_put_format (&title, "%s: %s", _("Message"),
314 msg_severity_to_string (msg->severity));
316 else if (is_table_item (item))
318 const char *caption = table_item_get_caption (to_table_item (item));
320 ds_put_format (&title, "Table: %s", caption);
322 ds_put_cstr (&title, "Table");
324 else if (is_chart_item (item))
326 const char *s = chart_item_get_title (to_chart_item (item));
328 ds_put_format (&title, "Chart: %s", s);
330 ds_put_cstr (&title, "Chart");
332 gtk_tree_store_set (store, &iter,
333 COL_TITLE, ds_cstr (&title),
339 path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
340 gtk_tree_view_expand_row (viewer->overview, path, TRUE);
341 gtk_tree_path_free (path);
344 if (pod->viewer->max_width < tw)
345 pod->viewer->max_width = tw;
346 pod->viewer->y += th;
348 gtk_layout_set_size (pod->viewer->output,
349 pod->viewer->max_width, pod->viewer->y);
351 gtk_window_set_urgency_hint (GTK_WINDOW (pod->viewer), TRUE);
357 static struct output_driver_class psppire_output_class =
359 "PSPPIRE", /* name */
361 psppire_output_submit, /* submit */
366 psppire_output_window_setup (void)
368 struct psppire_output_driver *pod;
369 struct output_driver *d;
371 pod = xzalloc (sizeof *pod);
373 output_driver_init (d, &psppire_output_class, "PSPPIRE",
374 SETTINGS_DEVICE_UNFILTERED);
375 output_driver_register (d);
380 /* Callback for the "delete" action (clicking the x on the top right
381 hand corner of the window) */
383 on_delete (GtkWidget *w, GdkEvent *event, gpointer user_data)
385 PsppireOutputWindow *ow = PSPPIRE_OUTPUT_WINDOW (user_data);
387 gtk_widget_destroy (GTK_WIDGET (ow));
389 ow->driver->viewer = NULL;
397 cancel_urgency (GtkWindow *window, gpointer data)
399 gtk_window_set_urgency_hint (window, FALSE);
403 on_row_activate (GtkTreeView *overview,
405 GtkTreeViewColumn *column,
406 PsppireOutputWindow *window)
414 model = gtk_tree_view_get_model (overview);
415 if (!gtk_tree_model_get_iter (model, &iter, path))
418 gtk_tree_model_get_value (model, &iter, COL_Y, &value);
419 y = g_value_get_long (&value);
420 g_value_unset (&value);
422 vadj = gtk_layout_get_vadjustment (window->output);
424 max = vadj->upper - vadj->page_size;
429 gtk_adjustment_set_value (vadj, y);
432 static void psppire_output_window_print (PsppireOutputWindow *window);
436 export_output (PsppireOutputWindow *window, struct string_map *options,
439 struct output_driver *driver;
442 string_map_insert (options, "format", format);
443 driver = output_driver_create (options);
447 for (i = 0; i < window->n_items; i++)
448 driver->class->submit (driver, window->items[i]);
449 output_driver_destroy (driver);
471 #define N_EXTENSIONS (n_FT - 1)
473 struct file_types ft[n_FT] = {
474 {N_("Infer file type from extension"), NULL},
475 {N_("PDF (*.pdf)"), ".pdf"},
476 {N_("HTML (*.html)"), ".html"},
477 {N_("OpenDocument (*.odt)"), ".odt"},
478 {N_("Text (*.txt)"), ".txt"},
479 {N_("PostScript (*.ps)"), ".ps"},
480 {N_("Comma-Separated Values (*.csv)"), ".csv"}
485 on_combo_change (GtkFileChooser *chooser)
487 gboolean sensitive = FALSE;
488 GtkWidget *combo = gtk_file_chooser_get_extra_widget (chooser);
491 gchar *fn = gtk_file_chooser_get_filename (chooser);
493 if (combo && gtk_widget_get_realized (combo))
494 x = gtk_combo_box_get_active (GTK_COMBO_BOX (combo));
506 for (i = 1 ; i < N_EXTENSIONS ; ++i)
508 if ( g_str_has_suffix (fn, ft[i].ext))
518 gtk_dialog_set_response_sensitive (GTK_DIALOG (chooser), GTK_RESPONSE_ACCEPT, sensitive);
523 on_file_chooser_change (GObject *w, GParamSpec *pspec, gpointer data)
526 GtkFileChooser *chooser = data;
527 const gchar *name = g_param_spec_get_name (pspec);
529 if ( ! gtk_widget_get_realized (GTK_WIDGET (chooser)))
532 /* Ignore this one. It causes recursion. */
533 if ( 0 == strcmp ("tooltip-text", name))
536 on_combo_change (chooser);
540 /* Recursively descend all the children of W, connecting
541 to their "notify" signal */
543 iterate_widgets (GtkWidget *w, gpointer data)
545 if ( GTK_IS_CONTAINER (w))
546 gtk_container_forall (GTK_CONTAINER (w), iterate_widgets, data);
548 g_signal_connect (w, "notify", G_CALLBACK (on_file_chooser_change), data);
553 static GtkListStore *
554 create_file_type_list (void)
558 GtkListStore *list = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
560 for (i = 0 ; i < n_FT ; ++i)
562 gtk_list_store_append (list, &iter);
563 gtk_list_store_set (list, &iter,
564 0, gettext (ft[i].label),
573 psppire_output_window_export (PsppireOutputWindow *window)
579 GtkFileChooser *chooser;
581 GtkWidget *dialog = gtk_file_chooser_dialog_new (_("Export Output"),
583 GTK_FILE_CHOOSER_ACTION_SAVE,
584 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
585 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
588 g_object_set (dialog, "local-only", FALSE, NULL);
590 chooser = GTK_FILE_CHOOSER (dialog);
592 list = create_file_type_list ();
594 combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (list));
598 /* Create text cell renderer */
599 GtkCellRenderer *cell = gtk_cell_renderer_text_new();
600 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE );
602 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo), cell, "text", 0);
605 g_signal_connect_swapped (combo, "changed", G_CALLBACK (on_combo_change), chooser);
607 gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
609 gtk_file_chooser_set_extra_widget (chooser, combo);
611 /* This kludge is necessary because there is no signal to tell us
612 when the candidate filename of a GtkFileChooser has changed */
613 gtk_container_forall (GTK_CONTAINER (dialog), iterate_widgets, dialog);
616 gtk_file_chooser_set_do_overwrite_confirmation (chooser, TRUE);
618 response = gtk_dialog_run (GTK_DIALOG (dialog));
620 if ( response == GTK_RESPONSE_ACCEPT )
622 int file_type = gtk_combo_box_get_active (GTK_COMBO_BOX (combo));
623 char *filename = gtk_file_chooser_get_filename (chooser);
624 struct string_map options;
626 g_return_if_fail (filename);
628 if (file_type == FT_AUTO)
631 for (i = 1 ; i < N_EXTENSIONS ; ++i)
633 if ( g_str_has_suffix (filename, ft[i].ext))
642 string_map_init (&options);
643 string_map_insert (&options, "output-file", filename);
648 export_output (window, &options, "pdf");
651 export_output (window, &options, "html");
654 export_output (window, &options, "odt");
657 export_output (window, &options, "ps");
660 export_output (window, &options, "csv");
664 string_map_insert (&options, "headers", "false");
665 string_map_insert (&options, "paginate", "false");
666 string_map_insert (&options, "squeeze", "true");
667 string_map_insert (&options, "emphasis", "none");
668 string_map_insert (&options, "charts", "none");
669 string_map_insert (&options, "top-margin", "0");
670 string_map_insert (&options, "bottom-margin", "0");
671 export_output (window, &options, "txt");
674 g_assert_not_reached ();
677 string_map_destroy (&options);
682 gtk_widget_destroy (dialog);
695 clipboard_get_cb (GtkClipboard *clipboard,
696 GtkSelectionData *selection_data,
700 PsppireOutputWindow *window = data;
704 struct output_driver *driver = NULL;
705 char dirname[PATH_MAX], *filename;
706 struct string_map options;
708 GtkTreeSelection *sel = gtk_tree_view_get_selection (window->overview);
709 GtkTreeModel *model = gtk_tree_view_get_model (window->overview);
711 GList *rows = gtk_tree_selection_get_selected_rows (sel, &model);
717 if (path_search (dirname, sizeof dirname, NULL, NULL, true)
718 || mkdtemp (dirname) == NULL)
720 error (0, errno, _("failed to create temporary directory"));
723 filename = xasprintf ("%s/clip.tmp", dirname);
725 string_map_init (&options);
726 string_map_insert (&options, "output-file", filename);
730 case SELECT_FMT_UTF8:
731 string_map_insert (&options, "box", "unicode");
734 case SELECT_FMT_TEXT:
735 string_map_insert (&options, "format", "txt");
738 case SELECT_FMT_HTML:
739 string_map_insert (&options, "format", "html");
740 string_map_insert (&options, "borders", "false");
741 string_map_insert (&options, "css", "false");
745 string_map_insert (&options, "format", "odt");
749 g_warning ("unsupported clip target\n");
754 driver = output_driver_create (&options);
760 GtkTreePath *path = n->data ;
762 struct output_item *item ;
764 gtk_tree_model_get_iter (model, &iter, path);
765 gtk_tree_model_get (model, &iter, COL_ADDR, &item, -1);
767 driver->class->submit (driver, item);
772 if ( driver->class->flush)
773 driver->class->flush (driver);
776 /* Some drivers (eg: the odt one) don't write anything until they
778 output_driver_destroy (driver);
781 if ( g_file_get_contents (filename, &text, &length, NULL) )
783 gtk_selection_data_set (selection_data, selection_data->target,
785 (const guchar *) text, length);
791 output_driver_destroy (driver);
803 clipboard_clear_cb (GtkClipboard *clipboard,
808 static const GtkTargetEntry targets[] = {
810 { "STRING", 0, SELECT_FMT_TEXT },
811 { "TEXT", 0, SELECT_FMT_TEXT },
812 { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
813 { "text/plain", 0, SELECT_FMT_TEXT },
815 { "UTF8_STRING", 0, SELECT_FMT_UTF8 },
816 { "text/plain;charset=utf-8", 0, SELECT_FMT_UTF8 },
818 { "text/html", 0, SELECT_FMT_HTML },
820 { "application/vnd.oasis.opendocument.text", 0, SELECT_FMT_ODT }
824 on_copy (PsppireOutputWindow *window)
827 GtkClipboard *clipboard =
828 gtk_widget_get_clipboard (GTK_WIDGET (window),
829 GDK_SELECTION_CLIPBOARD);
831 if (!gtk_clipboard_set_with_data (clipboard, targets,
832 G_N_ELEMENTS (targets),
833 clipboard_get_cb, clipboard_clear_cb,
836 clipboard_clear_cb (clipboard, window);
841 on_selection_change (GtkTreeSelection *sel, GtkAction *copy_action)
843 /* The Copy action is available only if there is something selected */
844 gtk_action_set_sensitive (copy_action, gtk_tree_selection_count_selected_rows (sel) > 0);
848 on_select_all (PsppireOutputWindow *window)
850 GtkTreeSelection *sel = gtk_tree_view_get_selection (window->overview);
851 gtk_tree_view_expand_all (window->overview);
852 gtk_tree_selection_select_all (sel);
857 psppire_output_window_init (PsppireOutputWindow *window)
859 GtkTreeViewColumn *column;
860 GtkCellRenderer *renderer;
862 GtkAction *copy_action;
863 GtkAction *select_all_action;
864 GtkTreeSelection *sel;
866 xml = builder_new ("output-viewer.ui");
868 copy_action = get_action_assert (xml, "edit_copy");
869 select_all_action = get_action_assert (xml, "edit_select-all");
871 gtk_action_set_sensitive (copy_action, FALSE);
873 g_signal_connect_swapped (copy_action, "activate", G_CALLBACK (on_copy), window);
875 g_signal_connect_swapped (select_all_action, "activate", G_CALLBACK (on_select_all), window);
877 gtk_widget_reparent (get_widget_assert (xml, "vbox1"), GTK_WIDGET (window));
879 window->output = GTK_LAYOUT (get_widget_assert (xml, "output"));
882 window->overview = GTK_TREE_VIEW (get_widget_assert (xml, "overview"));
884 sel = gtk_tree_view_get_selection (window->overview);
886 gtk_tree_selection_set_mode (sel, GTK_SELECTION_MULTIPLE);
888 g_signal_connect (sel, "changed", G_CALLBACK (on_selection_change), copy_action);
890 gtk_tree_view_set_model (window->overview,
891 GTK_TREE_MODEL (gtk_tree_store_new (
893 G_TYPE_STRING, /* COL_TITLE */
894 G_TYPE_POINTER, /* COL_ADDR */
895 G_TYPE_LONG))); /* COL_Y */
897 window->in_command = false;
899 window->items = NULL;
900 window->n_items = window->allocated_items = 0;
902 column = gtk_tree_view_column_new ();
903 gtk_tree_view_append_column (GTK_TREE_VIEW (window->overview), column);
904 renderer = gtk_cell_renderer_text_new ();
905 gtk_tree_view_column_pack_start (column, renderer, TRUE);
906 gtk_tree_view_column_add_attribute (column, renderer, "text", COL_TITLE);
908 g_signal_connect (GTK_TREE_VIEW (window->overview),
909 "row-activated", G_CALLBACK (on_row_activate), window);
911 gtk_widget_modify_bg (GTK_WIDGET (window->output), GTK_STATE_NORMAL,
912 >k_widget_get_style (GTK_WIDGET (window->output))->base[GTK_STATE_NORMAL]);
916 g_signal_connect (window,
918 G_CALLBACK (cancel_urgency),
921 g_signal_connect (get_action_assert (xml,"windows_minimise-all"),
923 G_CALLBACK (psppire_window_minimise_all),
927 GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
928 merge_help_menu (uim);
930 PSPPIRE_WINDOW (window)->menu =
931 GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows_menuitem/windows_minimise-all")->parent);
934 g_signal_connect_swapped (get_action_assert (xml, "file_export"), "activate",
935 G_CALLBACK (psppire_output_window_export), window);
938 g_signal_connect_swapped (get_action_assert (xml, "file_print"), "activate",
939 G_CALLBACK (psppire_output_window_print), window);
941 g_object_unref (xml);
943 g_signal_connect (window, "delete-event",
944 G_CALLBACK (on_delete), window);
949 psppire_output_window_new (void)
951 return GTK_WIDGET (g_object_new (psppire_output_window_get_type (),
952 /* TRANSLATORS: This will form a filename. Please avoid whitespace. */
953 "filename", _("Output"),
954 "description", _("Output Viewer"),
961 create_xr_print_driver (GtkPrintContext *context, PsppireOutputWindow *window)
963 struct string_map options;
964 GtkPageSetup *page_setup;
965 double width, height;
969 double bottom_margin;
971 page_setup = gtk_print_context_get_page_setup (context);
972 width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM);
973 height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_MM);
974 left_margin = gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM);
975 right_margin = gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_MM);
976 top_margin = gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM);
977 bottom_margin = gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_MM);
979 string_map_init (&options);
980 string_map_insert_nocopy (&options, xstrdup ("paper-size"),
981 xasprintf("%.2fx%.2fmm", width, height));
982 string_map_insert_nocopy (&options, xstrdup ("left-margin"),
983 xasprintf ("%.2fmm", left_margin));
984 string_map_insert_nocopy (&options, xstrdup ("right-margin"),
985 xasprintf ("%.2fmm", right_margin));
986 string_map_insert_nocopy (&options, xstrdup ("top-margin"),
987 xasprintf ("%.2fmm", top_margin));
988 string_map_insert_nocopy (&options, xstrdup ("bottom-margin"),
989 xasprintf ("%.2fmm", bottom_margin));
992 xr_driver_create (gtk_print_context_get_cairo_context (context), &options);
994 string_map_destroy (&options);
998 paginate (GtkPrintOperation *operation,
999 GtkPrintContext *context,
1000 PsppireOutputWindow *window)
1002 if ( window->print_item < window->n_items )
1004 xr_driver_output_item (window->print_xrd, window->items[window->print_item++]);
1005 while (xr_driver_need_new_page (window->print_xrd))
1007 xr_driver_next_page (window->print_xrd, NULL);
1008 window->print_n_pages ++;
1014 gtk_print_operation_set_n_pages (operation, window->print_n_pages);
1015 window->print_item = 0;
1016 create_xr_print_driver (context, window);
1022 begin_print (GtkPrintOperation *operation,
1023 GtkPrintContext *context,
1024 PsppireOutputWindow *window)
1026 create_xr_print_driver (context, window);
1028 window->print_item = 0;
1029 window->print_n_pages = 1;
1033 end_print (GtkPrintOperation *operation,
1034 GtkPrintContext *context,
1035 PsppireOutputWindow *window)
1037 xr_driver_destroy (window->print_xrd);
1042 draw_page (GtkPrintOperation *operation,
1043 GtkPrintContext *context,
1045 PsppireOutputWindow *window)
1047 xr_driver_next_page (window->print_xrd, gtk_print_context_get_cairo_context (context));
1048 while (!xr_driver_need_new_page (window->print_xrd)
1049 && window->print_item < window->n_items)
1050 xr_driver_output_item (window->print_xrd, window->items [window->print_item++]);
1055 psppire_output_window_print (PsppireOutputWindow *window)
1057 GtkPrintOperationResult res;
1059 GtkPrintOperation *print = gtk_print_operation_new ();
1061 if (window->print_settings != NULL)
1062 gtk_print_operation_set_print_settings (print, window->print_settings);
1064 g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
1065 g_signal_connect (print, "end_print", G_CALLBACK (end_print), window);
1066 g_signal_connect (print, "paginate", G_CALLBACK (paginate), window);
1067 g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), window);
1069 res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1070 GTK_WINDOW (window), NULL);
1072 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
1074 if (window->print_settings != NULL)
1075 g_object_unref (window->print_settings);
1076 window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
1080 g_object_unref (print);