1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2009, 2010 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 <gtk/gtksignal.h>
20 #include <gtk/gtkbox.h>
23 #include <libpspp/cast.h>
24 #include <libpspp/message.h>
25 #include <libpspp/string-map.h>
26 #include <output/cairo.h>
27 #include <output/chart-item.h>
28 #include <output/driver-provider.h>
29 #include <output/output-item.h>
30 #include <output/table-item.h>
31 #include <output/text-item.h>
32 #include <output/tab.h>
35 #include "help-menu.h"
37 #include "psppire-output-window.h"
42 #include <sys/types.h>
47 #define _(msgid) gettext (msgid)
48 #define N_(msgid) msgid
52 COL_TITLE, /* Table title. */
53 COL_ADDR, /* Pointer to the table */
54 COL_Y, /* Y position of top of title. */
58 static void psppire_output_window_base_finalize (PsppireOutputWindowClass *, gpointer);
59 static void psppire_output_window_base_init (PsppireOutputWindowClass *class);
60 static void psppire_output_window_class_init (PsppireOutputWindowClass *class);
61 static void psppire_output_window_init (PsppireOutputWindow *window);
65 psppire_output_window_get_type (void)
67 static GType psppire_output_window_type = 0;
69 if (!psppire_output_window_type)
71 static const GTypeInfo psppire_output_window_info =
73 sizeof (PsppireOutputWindowClass),
74 (GBaseInitFunc) psppire_output_window_base_init,
75 (GBaseFinalizeFunc) psppire_output_window_base_finalize,
76 (GClassInitFunc)psppire_output_window_class_init,
77 (GClassFinalizeFunc) NULL,
79 sizeof (PsppireOutputWindow),
81 (GInstanceInitFunc) psppire_output_window_init,
84 psppire_output_window_type =
85 g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireOutputWindow",
86 &psppire_output_window_info, 0);
89 return psppire_output_window_type;
92 static GObjectClass *parent_class;
95 psppire_output_window_finalize (GObject *object)
97 if (G_OBJECT_CLASS (parent_class)->finalize)
98 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
103 psppire_output_window_dispose (GObject *obj)
105 PsppireOutputWindow *viewer = PSPPIRE_OUTPUT_WINDOW (obj);
108 for (i = 0; i < viewer->n_items; i++)
109 output_item_unref (viewer->items[i]);
110 free (viewer->items);
111 viewer->items = NULL;
112 viewer->n_items = viewer->allocated_items = 0;
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;
155 static struct output_driver_class psppire_output_class;
157 static struct psppire_output_driver *
158 psppire_output_cast (struct output_driver *driver)
160 assert (driver->class == &psppire_output_class);
161 return UP_CAST (driver, struct psppire_output_driver, driver);
165 expose_event_callback (GtkWidget *widget, GdkEventExpose *event, gpointer data)
167 struct xr_rendering *r = g_object_get_data (G_OBJECT (widget), "rendering");
170 cr = gdk_cairo_create (widget->window);
171 xr_rendering_draw (r, cr);
178 psppire_output_submit (struct output_driver *this,
179 const struct output_item *item)
181 struct psppire_output_driver *pod = psppire_output_cast (this);
182 PsppireOutputWindow *viewer;
183 GtkWidget *drawing_area;
184 struct xr_rendering *r;
192 if (pod->viewer == NULL)
194 pod->viewer = PSPPIRE_OUTPUT_WINDOW (psppire_output_window_new ());
195 gtk_widget_show_all (GTK_WIDGET (pod->viewer));
196 pod->viewer->driver = pod;
198 viewer = pod->viewer;
200 if (viewer->n_items >= viewer->allocated_items)
201 viewer->items = x2nrealloc (viewer->items, &viewer->allocated_items,
202 sizeof *viewer->items);
203 viewer->items[viewer->n_items++] = output_item_ref (item);
205 if (is_text_item (item))
207 const struct text_item *text_item = to_text_item (item);
208 enum text_item_type type = text_item_get_type (text_item);
209 const char *text = text_item_get_text (text_item);
211 if (type == TEXT_ITEM_COMMAND_CLOSE)
213 viewer->in_command = false;
216 else if (text[0] == '\0')
220 cr = gdk_cairo_create (GTK_WIDGET (pod->viewer)->window);
223 const GtkStyle *style = gtk_widget_get_style (GTK_WIDGET (viewer));
224 struct string_map options = STRING_MAP_INITIALIZER (options);
225 PangoFontDescription *font_desc;
228 /* Use GTK+ default font as proportional font. */
229 font_name = pango_font_description_to_string (style->font_desc);
230 string_map_insert (&options, "prop-font", font_name);
233 /* Derived emphasized font from proportional font. */
234 font_desc = pango_font_description_copy (style->font_desc);
235 pango_font_description_set_style (font_desc, PANGO_STYLE_ITALIC);
236 font_name = pango_font_description_to_string (font_desc);
237 string_map_insert (&options, "emph-font", font_name);
239 pango_font_description_free (font_desc);
241 /* Pretend that the "page" has a reasonable width and a very big length,
242 so that most tables can be conveniently viewed on-screen with vertical
243 scrolling only. (The length should not be increased very much because
244 it is already close enough to INT_MAX when expressed as thousands of a
246 string_map_insert (&options, "paper-size", "300x200000mm");
247 string_map_insert (&options, "headers", "off");
248 string_map_insert (&options, "left-margin", "0");
249 string_map_insert (&options, "right-margin", "0");
250 string_map_insert (&options, "top-margin", "0");
251 string_map_insert (&options, "bottom-margin", "0");
253 pod->xr = xr_driver_create (cr, &options);
255 string_map_destroy (&options);
258 r = xr_rendering_create (pod->xr, item, cr);
262 xr_rendering_measure (r, &tw, &th);
264 drawing_area = gtk_drawing_area_new ();
265 gtk_widget_modify_bg (
266 GTK_WIDGET (drawing_area), GTK_STATE_NORMAL,
267 >k_widget_get_style (drawing_area)->base[GTK_STATE_NORMAL]);
268 g_object_set_data (G_OBJECT (drawing_area), "rendering", r);
269 gtk_widget_set_size_request (drawing_area, tw, th);
270 gtk_layout_put (pod->viewer->output, drawing_area, 0, pod->viewer->y);
271 gtk_widget_show (drawing_area);
272 g_signal_connect (G_OBJECT (drawing_area), "expose_event",
273 G_CALLBACK (expose_event_callback), NULL);
275 if (!is_text_item (item)
276 || text_item_get_type (to_text_item (item)) != TEXT_ITEM_SYNTAX
277 || !viewer->in_command)
279 store = GTK_TREE_STORE (gtk_tree_view_get_model (viewer->overview));
281 ds_init_empty (&title);
282 if (is_text_item (item)
283 && text_item_get_type (to_text_item (item)) == TEXT_ITEM_COMMAND_OPEN)
285 gtk_tree_store_append (store, &iter, NULL);
286 viewer->cur_command = iter; /* XXX shouldn't save a GtkTreeIter */
287 viewer->in_command = true;
291 GtkTreeIter *p = viewer->in_command ? &viewer->cur_command : NULL;
292 gtk_tree_store_append (store, &iter, p);
296 if (is_text_item (item))
297 ds_put_cstr (&title, text_item_get_text (to_text_item (item)));
298 else if (is_table_item (item))
300 const char *caption = table_item_get_caption (to_table_item (item));
302 ds_put_format (&title, "Table: %s", caption);
304 ds_put_cstr (&title, "Table");
306 else if (is_chart_item (item))
308 const char *s = chart_item_get_title (to_chart_item (item));
310 ds_put_format (&title, "Chart: %s", s);
312 ds_put_cstr (&title, "Chart");
314 gtk_tree_store_set (store, &iter,
315 COL_TITLE, ds_cstr (&title),
321 path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
322 gtk_tree_view_expand_row (viewer->overview, path, TRUE);
323 gtk_tree_path_free (path);
326 if (pod->viewer->max_width < tw)
327 pod->viewer->max_width = tw;
328 pod->viewer->y += th;
330 gtk_layout_set_size (pod->viewer->output,
331 pod->viewer->max_width, pod->viewer->y);
333 gtk_window_set_urgency_hint (GTK_WINDOW (pod->viewer), TRUE);
339 static struct output_driver_class psppire_output_class =
341 "PSPPIRE", /* name */
343 psppire_output_submit, /* submit */
348 psppire_output_window_setup (void)
350 struct psppire_output_driver *pod;
351 struct output_driver *d;
353 pod = xzalloc (sizeof *pod);
355 output_driver_init (d, &psppire_output_class, "PSPPIRE",
356 SETTINGS_DEVICE_UNFILTERED);
357 output_driver_register (d);
360 int viewer_length = 16;
361 int viewer_width = 59;
363 /* Callback for the "delete" action (clicking the x on the top right
364 hand corner of the window) */
366 on_delete (GtkWidget *w, GdkEvent *event, gpointer user_data)
368 PsppireOutputWindow *ow = PSPPIRE_OUTPUT_WINDOW (user_data);
370 gtk_widget_destroy (GTK_WIDGET (ow));
372 ow->driver->viewer = NULL;
380 cancel_urgency (GtkWindow *window, gpointer data)
382 gtk_window_set_urgency_hint (window, FALSE);
386 on_row_activate (GtkTreeView *overview,
388 GtkTreeViewColumn *column,
389 PsppireOutputWindow *window)
397 model = gtk_tree_view_get_model (overview);
398 if (!gtk_tree_model_get_iter (model, &iter, path))
401 gtk_tree_model_get_value (model, &iter, COL_Y, &value);
402 y = g_value_get_long (&value);
403 g_value_unset (&value);
405 vadj = gtk_layout_get_vadjustment (window->output);
407 max = vadj->upper - vadj->page_size;
412 gtk_adjustment_set_value (vadj, y);
415 static void psppire_output_window_print (PsppireOutputWindow *window);
418 static GtkFileFilter *
419 add_filter (GtkFileChooser *chooser, const char *name, const char *pattern)
421 GtkFileFilter *filter = gtk_file_filter_new ();
422 g_object_ref_sink (G_OBJECT (filter));
423 gtk_file_filter_set_name (filter, name);
424 gtk_file_filter_add_pattern (filter, pattern);
425 gtk_file_chooser_add_filter (chooser, filter);
430 export_output (PsppireOutputWindow *window, struct string_map *options,
433 struct output_driver *driver;
436 string_map_insert (options, "format", format);
437 driver = output_driver_create (options);
441 for (i = 0; i < window->n_items; i++)
442 driver->class->submit (driver, window->items[i]);
443 output_driver_destroy (driver);
447 psppire_output_window_export (PsppireOutputWindow *window)
451 GtkFileFilter *pdf_filter;
452 GtkFileFilter *html_filter;
453 GtkFileFilter *odt_filter;
454 GtkFileFilter *txt_filter;
455 GtkFileFilter *ps_filter;
456 GtkFileFilter *csv_filter;
457 GtkFileChooser *chooser;
460 dialog = gtk_file_chooser_dialog_new (_("Export Output"),
462 GTK_FILE_CHOOSER_ACTION_SAVE,
463 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
464 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
466 chooser = GTK_FILE_CHOOSER (dialog);
468 pdf_filter = add_filter (chooser, _("PDF Files (*.pdf)"), "*.pdf");
469 html_filter = add_filter (chooser, _("HTML Files (*.html)"), "*.html");
470 odt_filter = add_filter (chooser, _("OpenDocument Files (*.odt)"), "*.odt");
471 txt_filter = add_filter (chooser, _("Text Files (*.txt)"), "*.txt");
472 ps_filter = add_filter (chooser, _("PostScript Files (*.ps)"), "*.ps");
473 csv_filter = add_filter (chooser, _("Comma-Separated Value Files (*.csv)"),
476 gtk_file_chooser_set_do_overwrite_confirmation (chooser, TRUE);
477 gtk_file_chooser_set_filter (chooser, pdf_filter);
479 response = gtk_dialog_run (GTK_DIALOG (dialog));
481 if ( response == GTK_RESPONSE_ACCEPT )
483 char *filename = gtk_file_chooser_get_filename (chooser);
484 GtkFileFilter *filter = gtk_file_chooser_get_filter (chooser);
485 struct string_map options;
487 g_return_if_fail (filename);
488 g_return_if_fail (filter);
490 string_map_init (&options);
491 string_map_insert (&options, "output-file", filename);
492 if (filter == pdf_filter)
494 export_output (window, &options, "pdf");
496 else if (filter == html_filter)
497 export_output (window, &options, "html");
498 else if (filter == odt_filter)
499 export_output (window, &options, "odt");
500 else if (filter == txt_filter)
502 string_map_insert (&options, "headers", "false");
503 string_map_insert (&options, "paginate", "false");
504 string_map_insert (&options, "squeeze", "true");
505 string_map_insert (&options, "emphasis", "none");
506 string_map_insert (&options, "charts", "none");
507 string_map_insert (&options, "top-margin", "0");
508 string_map_insert (&options, "bottom-margin", "0");
509 export_output (window, &options, "txt");
511 else if (filter == ps_filter)
512 export_output (window, &options, "ps");
513 else if (filter == csv_filter)
514 export_output (window, &options, "csv");
516 g_return_if_reached ();
521 g_object_unref (G_OBJECT (pdf_filter));
522 g_object_unref (G_OBJECT (html_filter));
523 g_object_unref (G_OBJECT (txt_filter));
524 g_object_unref (G_OBJECT (ps_filter));
525 g_object_unref (G_OBJECT (csv_filter));
527 gtk_widget_destroy (dialog);
541 insert_glyph (struct string_map *map, const char *opt, gunichar glyph)
543 char s[6] = {0,0,0,0,0,0};
545 g_unichar_to_utf8 (glyph, s);
546 string_map_insert (map, opt, s);
555 struct glyph_pair table[] = {
556 {0x250C, "box[1100]"},
557 {0x2518, "box[0011]"},
558 {0x2510, "box[0110]"},
559 {0x2514, "box[1001]"},
560 {0x2502, "box[0101]"},
561 {0x2500, "box[1010]"},
562 {0x2534, "box[1011]"},
563 {0x252C, "box[1110]"},
564 {0x2524, "box[0111]"},
565 {0x251C, "box[1101]"},
566 {0x253C, "box[1111]"},
568 {0x2501, "box[2020]"},
569 {0x2503, "box[0202]"},
570 {0x253F, "box[2121]"},
571 {0x2542, "box[1212]"},
572 {0x254B, "box[2222]"},
573 {0x2525, "box[0121]"},
574 {0x2530, "box[1210]"},
575 {0x2538, "box[1012]"},
576 {0x251D, "box[2101]"},
577 {0x2537, "box[2021]"},
578 {0x252F, "box[2120]"},
580 {0x250F, "box[2200]"},
581 {0x251B, "box[0022]"},
582 {0x2517, "box[2002]"},
583 {0x2513, "box[0220]"},
585 {0x2528, "box[0212]"},
586 {0x2520, "box[1202]"},
587 {0x2533, "box[2220]"},
588 {0x253B, "box[2022]"},
589 {0x2519, "box[0021]"},
591 {0x2578, "box[0020]"},
592 {0x257A, "box[2000]"},
594 {0x2523, "box[2202]"},
595 {0x252B, "box[0222]"},
597 {0x2515, "box[2001]"},
602 utf8_box_chars (struct string_map *map)
605 for (i = 0; i < sizeof (table) / sizeof (table[0]); ++i)
607 const struct glyph_pair *p = &table[i];
608 insert_glyph (map, p->opt, p->glyph);
615 clipboard_get_cb (GtkClipboard *clipboard,
616 GtkSelectionData *selection_data,
620 PsppireOutputWindow *window = data;
624 struct output_driver *driver = NULL;
625 char *filename = NULL;
626 struct string_map options;
628 GtkTreeSelection *sel = gtk_tree_view_get_selection (window->overview);
629 GtkTreeModel *model = gtk_tree_view_get_model (window->overview);
631 GList *rows = gtk_tree_selection_get_selected_rows (sel, &model);
637 string_map_init (&options);
638 filename = tempnam (NULL, NULL);
639 string_map_insert (&options, "output-file", filename);
643 case SELECT_FMT_UTF8:
644 utf8_box_chars (&options);
647 case SELECT_FMT_TEXT:
648 string_map_insert (&options, "format", "txt");
651 case SELECT_FMT_HTML:
652 string_map_insert (&options, "format", "html");
656 string_map_insert (&options, "format", "odt");
660 g_print ("unsupportted clip target\n");
665 driver = output_driver_create (&options);
671 GtkTreePath *path = n->data ;
673 struct output_item *item ;
675 gtk_tree_model_get_iter (model, &iter, path);
676 gtk_tree_model_get (model, &iter, COL_ADDR, &item, -1);
678 driver->class->submit (driver, item);
683 if ( driver->class->flush)
684 driver->class->flush (driver);
687 /* Some drivers (eg: the odt one) don't write anything until they
689 output_driver_destroy (driver);
692 if ( g_file_get_contents (filename, &text, &length, NULL) )
694 gtk_selection_data_set (selection_data, selection_data->target,
696 (const guchar *) text, length);
702 output_driver_destroy (driver);
713 clipboard_clear_cb (GtkClipboard *clipboard,
718 static const GtkTargetEntry targets[] = {
720 { "STRING", 0, SELECT_FMT_TEXT },
721 { "TEXT", 0, SELECT_FMT_TEXT },
722 { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
723 { "text/plain", 0, SELECT_FMT_TEXT },
725 { "UTF8_STRING", 0, SELECT_FMT_UTF8 },
726 { "text/plain;charset=utf-8", 0, SELECT_FMT_UTF8 },
728 { "text/html", 0, SELECT_FMT_HTML },
730 { "application/vnd.oasis.opendocument.text", 0, SELECT_FMT_ODT }
734 on_copy (PsppireOutputWindow *window)
737 GtkClipboard *clipboard =
738 gtk_widget_get_clipboard (GTK_WIDGET (window),
739 GDK_SELECTION_CLIPBOARD);
741 if (!gtk_clipboard_set_with_data (clipboard, targets,
742 G_N_ELEMENTS (targets),
743 clipboard_get_cb, clipboard_clear_cb,
746 clipboard_clear_cb (clipboard, window);
751 on_selection_change (GtkTreeSelection *sel, GtkAction *copy_action)
753 /* The Copy action is available only if there is something selected */
754 gtk_action_set_sensitive (copy_action, gtk_tree_selection_count_selected_rows (sel) > 0);
758 on_select_all (PsppireOutputWindow *window)
760 GtkTreeSelection *sel = gtk_tree_view_get_selection (window->overview);
761 gtk_tree_view_expand_all (window->overview);
762 gtk_tree_selection_select_all (sel);
767 psppire_output_window_init (PsppireOutputWindow *window)
769 GtkTreeViewColumn *column;
770 GtkCellRenderer *renderer;
772 GtkAction *copy_action;
773 GtkAction *select_all_action;
774 GtkTreeSelection *sel;
776 xml = builder_new ("output-viewer.ui");
778 copy_action = get_action_assert (xml, "edit_copy");
779 select_all_action = get_action_assert (xml, "edit_select-all");
781 gtk_action_set_sensitive (copy_action, FALSE);
783 g_signal_connect_swapped (copy_action, "activate", G_CALLBACK (on_copy), window);
785 g_signal_connect_swapped (select_all_action, "activate", G_CALLBACK (on_select_all), window);
787 gtk_widget_reparent (get_widget_assert (xml, "vbox1"), GTK_WIDGET (window));
789 window->output = GTK_LAYOUT (get_widget_assert (xml, "output"));
792 window->overview = GTK_TREE_VIEW (get_widget_assert (xml, "overview"));
794 sel = gtk_tree_view_get_selection (window->overview);
796 gtk_tree_selection_set_mode (sel, GTK_SELECTION_MULTIPLE);
798 g_signal_connect (sel, "changed", G_CALLBACK (on_selection_change), copy_action);
800 gtk_tree_view_set_model (window->overview,
801 GTK_TREE_MODEL (gtk_tree_store_new (
803 G_TYPE_STRING, /* COL_TITLE */
804 G_TYPE_POINTER, /* COL_ADDR */
805 G_TYPE_LONG))); /* COL_Y */
807 window->in_command = false;
809 window->items = NULL;
810 window->n_items = window->allocated_items = 0;
812 column = gtk_tree_view_column_new ();
813 gtk_tree_view_append_column (GTK_TREE_VIEW (window->overview), column);
814 renderer = gtk_cell_renderer_text_new ();
815 gtk_tree_view_column_pack_start (column, renderer, TRUE);
816 gtk_tree_view_column_add_attribute (column, renderer, "text", COL_TITLE);
818 g_signal_connect (GTK_TREE_VIEW (window->overview),
819 "row-activated", G_CALLBACK (on_row_activate), window);
821 gtk_widget_modify_bg (GTK_WIDGET (window->output), GTK_STATE_NORMAL,
822 >k_widget_get_style (GTK_WIDGET (window->output))->base[GTK_STATE_NORMAL]);
826 g_signal_connect (window,
828 G_CALLBACK (cancel_urgency),
831 g_signal_connect (get_action_assert (xml,"windows_minimise-all"),
833 G_CALLBACK (psppire_window_minimise_all),
837 GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
838 merge_help_menu (uim);
840 PSPPIRE_WINDOW (window)->menu =
841 GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows_menuitem/windows_minimise-all")->parent);
844 g_signal_connect_swapped (get_action_assert (xml, "file_export"), "activate",
845 G_CALLBACK (psppire_output_window_export), window);
848 g_signal_connect_swapped (get_action_assert (xml, "file_print"), "activate",
849 G_CALLBACK (psppire_output_window_print), window);
851 g_object_unref (xml);
853 g_signal_connect (window, "delete-event",
854 G_CALLBACK (on_delete), window);
859 psppire_output_window_new (void)
861 return GTK_WIDGET (g_object_new (psppire_output_window_get_type (),
862 "filename", "Output",
863 "description", _("Output Viewer"),
870 create_xr_print_driver (GtkPrintContext *context, PsppireOutputWindow *window)
872 struct string_map options;
873 GtkPageSetup *page_setup;
874 double width, height;
878 double bottom_margin;
880 page_setup = gtk_print_context_get_page_setup (context);
881 width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM);
882 height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_MM);
883 left_margin = gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM);
884 right_margin = gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_MM);
885 top_margin = gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM);
886 bottom_margin = gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_MM);
888 string_map_init (&options);
889 string_map_insert_nocopy (&options, xstrdup ("paper-size"),
890 xasprintf("%.2fx%.2fmm", width, height));
891 string_map_insert_nocopy (&options, xstrdup ("left-margin"),
892 xasprintf ("%.2fmm", left_margin));
893 string_map_insert_nocopy (&options, xstrdup ("right-margin"),
894 xasprintf ("%.2fmm", right_margin));
895 string_map_insert_nocopy (&options, xstrdup ("top-margin"),
896 xasprintf ("%.2fmm", top_margin));
897 string_map_insert_nocopy (&options, xstrdup ("bottom-margin"),
898 xasprintf ("%.2fmm", bottom_margin));
901 xr_driver_create (gtk_print_context_get_cairo_context (context), &options);
903 string_map_destroy (&options);
907 paginate (GtkPrintOperation *operation,
908 GtkPrintContext *context,
909 PsppireOutputWindow *window)
911 if ( window->print_item < window->n_items )
913 xr_driver_output_item (window->print_xrd, window->items[window->print_item++]);
914 if (xr_driver_need_new_page (window->print_xrd))
916 xr_driver_next_page (window->print_xrd, NULL);
917 window->print_n_pages ++;
923 gtk_print_operation_set_n_pages (operation, window->print_n_pages);
924 window->print_item = 0;
925 create_xr_print_driver (context, window);
931 begin_print (GtkPrintOperation *operation,
932 GtkPrintContext *context,
933 PsppireOutputWindow *window)
935 create_xr_print_driver (context, window);
937 window->print_item = 0;
938 window->print_n_pages = 1;
942 end_print (GtkPrintOperation *operation,
943 GtkPrintContext *context,
944 PsppireOutputWindow *window)
946 xr_driver_destroy (window->print_xrd);
951 draw_page (GtkPrintOperation *operation,
952 GtkPrintContext *context,
954 PsppireOutputWindow *window)
956 xr_driver_next_page (window->print_xrd, gtk_print_context_get_cairo_context (context));
957 while ( window->print_item < window->n_items)
959 xr_driver_output_item (window->print_xrd, window->items [window->print_item++]);
960 if ( xr_driver_need_new_page (window->print_xrd) )
967 psppire_output_window_print (PsppireOutputWindow *window)
969 GtkPrintOperationResult res;
971 GtkPrintOperation *print = gtk_print_operation_new ();
973 if (window->print_settings != NULL)
974 gtk_print_operation_set_print_settings (print, window->print_settings);
976 g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
977 g_signal_connect (print, "end_print", G_CALLBACK (end_print), window);
978 g_signal_connect (print, "paginate", G_CALLBACK (paginate), window);
979 g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), window);
981 res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
982 GTK_WINDOW (window), NULL);
984 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
986 if (window->print_settings != NULL)
987 g_object_unref (window->print_settings);
988 window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
992 g_object_unref (print);