1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008-2015, 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-view.h"
24 #include "libpspp/assertion.h"
25 #include "libpspp/string-map.h"
26 #include "output/cairo.h"
27 #include "output/driver-provider.h"
28 #include "output/driver.h"
29 #include "output/chart-item.h"
30 #include "output/group-item.h"
31 #include "output/message-item.h"
32 #include "output/output-item.h"
33 #include "output/output-item-provider.h"
34 #include "output/table-item.h"
35 #include "output/text-item.h"
37 #include "gl/c-xvasprintf.h"
38 #include "gl/minmax.h"
39 #include "gl/tmpdir.h"
40 #include "gl/xalloc.h"
43 #define _(msgid) gettext (msgid)
45 struct output_view_item
47 struct output_item *item;
48 GtkWidget *drawing_area;
51 struct psppire_output_view
61 struct string_map render_opts;
62 GtkTreeView *overview;
63 GtkTreePath *cur_group;
67 struct output_view_item *items;
68 size_t n_items, allocated_items;
70 /* Variables pertaining to printing */
71 GtkPrintSettings *print_settings;
72 struct xr_driver *print_xrd;
80 COL_NAME, /* Table name. */
81 COL_ADDR, /* Pointer to the table */
82 COL_Y, /* Y position of top of name. */
86 /* Draws a white background on the GtkLayout to match the white background of
87 each of the output items. */
89 layout_draw_callback (GtkWidget *widget, cairo_t *cr, gpointer data)
93 int width = gtk_widget_get_allocated_width (widget);
94 int height = gtk_widget_get_allocated_height (widget);
95 cairo_rectangle (cr, 0, 0, width, height);
96 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
101 return FALSE; /* Continue drawing the GtkDrawingAreas. */
105 draw_callback (GtkWidget *widget, cairo_t *cr, gpointer data)
108 if (!gdk_cairo_get_clip_rectangle (cr, &clip))
111 struct xr_rendering *r = g_object_get_data (G_OBJECT (widget), "rendering");
112 xr_rendering_draw (r, cr, clip.x, clip.y,
113 clip.x + clip.width, clip.y + clip.height);
118 free_rendering (gpointer rendering_)
120 struct xr_rendering *rendering = rendering_;
121 xr_rendering_destroy (rendering);
125 get_xr_options (struct psppire_output_view *view, struct string_map *options)
127 string_map_clear (options);
129 GtkStyleContext *context
130 = gtk_widget_get_style_context (GTK_WIDGET (view->output));
131 GtkStateFlags state = gtk_widget_get_state_flags (GTK_WIDGET (view->output));
133 /* Use GTK+ default font as proportional font. */
134 PangoFontDescription *font_desc;
135 gtk_style_context_get (context, state, "font", &font_desc, NULL);
136 char *font_name = pango_font_description_to_string (font_desc);
137 string_map_insert (options, "prop-font", font_name);
140 /* Derived emphasized font from proportional font. */
141 pango_font_description_set_style (font_desc, PANGO_STYLE_ITALIC);
142 font_name = pango_font_description_to_string (font_desc);
143 string_map_insert (options, "emph-font", font_name);
145 pango_font_description_free (font_desc);
147 /* Pretend that the "page" has a reasonable width and a very big length,
148 so that most tables can be conveniently viewed on-screen with vertical
149 scrolling only. (The length should not be increased very much because
150 it is already close enough to INT_MAX when expressed as thousands of a
152 string_map_insert_nocopy (options, xstrdup ("paper-size"),
153 xasprintf ("%dx1000000pt", view->render_width));
154 string_map_insert (options, "left-margin", "0");
155 string_map_insert (options, "right-margin", "0");
156 string_map_insert (options, "top-margin", "0");
157 string_map_insert (options, "bottom-margin", "0");
161 create_xr (struct psppire_output_view *view)
163 get_xr_options (view, &view->render_opts);
165 struct string_map options;
166 string_map_clone (&options, &view->render_opts);
168 GdkWindow *win = gtk_layout_get_bin_window (view->output);
169 cairo_region_t *region = gdk_window_get_visible_region (win);
170 GdkDrawingContext *ctx = gdk_window_begin_draw_frame (win, region);
171 cairo_t *cr = gdk_drawing_context_get_cairo_context (ctx);
173 view->xr = xr_driver_create (cr, &options);
174 string_map_destroy (&options);
176 struct text_item *text_item = text_item_create (TEXT_ITEM_LOG, "X");
177 struct xr_rendering *r
178 = xr_rendering_create (view->xr, text_item_super (text_item), cr);
179 xr_rendering_measure (r, NULL, &view->font_height);
180 xr_rendering_destroy (r);
181 text_item_unref (text_item);
183 gdk_window_end_draw_frame (win, ctx);
184 cairo_region_destroy (region);
187 /* Return the horizontal position to place a widget whose
188 width is CHILD_WIDTH */
190 get_xpos (const struct psppire_output_view *view, gint child_width)
192 GdkWindow *gdkw = gtk_widget_get_window (GTK_WIDGET (view->output));
193 guint w = gdk_window_get_width (gdkw);
195 g_object_get (view->output, "border-width", &gutter, NULL);
196 return (gtk_widget_get_direction (GTK_WIDGET (view->output)) == GTK_TEXT_DIR_RTL) ? w - child_width - gutter: gutter;
200 create_drawing_area (struct psppire_output_view *view,
201 GtkWidget *drawing_area, struct xr_rendering *r,
204 g_object_set_data_full (G_OBJECT (drawing_area),
205 "rendering", r, free_rendering);
207 g_signal_connect (drawing_area, "draw",
208 G_CALLBACK (draw_callback), view);
210 gtk_widget_set_size_request (drawing_area, tw, th);
211 gint xpos = get_xpos (view, tw);
213 gtk_layout_put (view->output, drawing_area, xpos, view->y);
215 gtk_widget_show (drawing_area);
219 rerender (struct psppire_output_view *view)
221 struct output_view_item *item;
222 GdkWindow *gdkw = gtk_widget_get_window (GTK_WIDGET (view->output));
224 if (!view->n_items || ! gdkw)
227 if (view->xr == NULL)
230 GdkWindow *win = gtk_layout_get_bin_window (view->output);
231 cairo_region_t *region = gdk_window_get_visible_region (win);
232 GdkDrawingContext *ctx = gdk_window_begin_draw_frame (win, region);
233 cairo_t *cr = gdk_drawing_context_get_cairo_context (ctx);
237 for (item = view->items; item < &view->items[view->n_items]; item++)
239 struct xr_rendering *r;
244 view->y += view->font_height / 2;
246 if (is_group_open_item (item->item))
249 r = xr_rendering_create (view->xr, item->item, cr);
252 g_warn_if_reached ();
256 xr_rendering_measure (r, &tw, &th);
258 gint xpos = get_xpos (view, tw);
260 if (!item->drawing_area)
262 item->drawing_area = gtk_drawing_area_new ();
263 create_drawing_area (view, item->drawing_area, r, tw, th);
267 g_object_set_data_full (G_OBJECT (item->drawing_area),
268 "rendering", r, free_rendering);
269 gtk_widget_set_size_request (item->drawing_area, tw, th);
270 gtk_layout_move (view->output, item->drawing_area, xpos, view->y);
276 /* This code probably doesn't bring us anthing, but Gtk
277 shows warnings if get_preferred_width/height is not
278 called before the size_allocate below is called. */
279 gtk_widget_get_preferred_width (item->drawing_area, &minw, NULL);
280 gtk_widget_get_preferred_height (item->drawing_area, &minh, NULL);
281 if (th > minh) th = minh;
282 if (tw > minw) tw = minw;
289 gtk_widget_size_allocate (item->drawing_area, &alloc);
291 if (view->max_width < tw)
292 view->max_width = tw;
296 gtk_layout_set_size (view->output,
297 view->max_width + view->font_height,
298 view->y + view->font_height);
300 gdk_window_end_draw_frame (win, ctx);
301 cairo_region_destroy (region);
305 psppire_output_view_put (struct psppire_output_view *view,
306 const struct output_item *item)
308 struct output_view_item *view_item;
309 GtkWidget *drawing_area;
313 if (is_group_close_item (item))
317 if (!gtk_tree_path_up (view->cur_group))
319 gtk_tree_path_free (view->cur_group);
320 view->cur_group = NULL;
325 else if (is_text_item (item))
327 const struct text_item *text_item = to_text_item (item);
328 const char *text = text_item_get_text (text_item);
333 if (view->n_items >= view->allocated_items)
334 view->items = x2nrealloc (view->items, &view->allocated_items,
335 sizeof *view->items);
336 view_item = &view->items[view->n_items++];
337 view_item->item = output_item_ref (item);
338 view_item->drawing_area = NULL;
340 GdkWindow *win = gtk_widget_get_window (GTK_WIDGET (view->output));
341 if (is_group_open_item (item))
345 view_item->drawing_area = drawing_area = gtk_drawing_area_new ();
347 if (view->xr == NULL)
350 cairo_region_t *region = gdk_window_get_visible_region (win);
351 GdkDrawingContext *ctx = gdk_window_begin_draw_frame (win, region);
352 cairo_t *cr = gdk_drawing_context_get_cairo_context (ctx);
355 view->y += view->font_height / 2;
357 struct xr_rendering *r = xr_rendering_create (view->xr, item, cr);
360 gdk_window_end_draw_frame (win, ctx);
361 cairo_region_destroy (region);
365 xr_rendering_measure (r, &tw, &th);
367 create_drawing_area (view, drawing_area, r, tw, th);
368 gdk_window_end_draw_frame (win, ctx);
369 cairo_region_destroy (region);
376 GtkTreeStore *store = GTK_TREE_STORE (
377 gtk_tree_view_get_model (view->overview));
379 ds_init_empty (&name);
381 /* Create a new node in the tree and puts a reference to it in 'iter'. */
385 && gtk_tree_path_get_depth (view->cur_group) > 0
386 && gtk_tree_model_get_iter (GTK_TREE_MODEL (store),
387 &parent, view->cur_group))
388 gtk_tree_store_append (store, &iter, &parent);
390 gtk_tree_store_append (store, &iter, NULL);
392 if (is_group_open_item (item))
394 gtk_tree_path_free (view->cur_group);
395 view->cur_group = gtk_tree_model_get_path (GTK_TREE_MODEL (store),
400 if (is_text_item (item))
402 const struct text_item *text_item = to_text_item (item);
403 ds_put_cstr (&name, text_item_type_to_string (
404 text_item_get_type (text_item)));
406 else if (is_message_item (item))
408 const struct message_item *msg_item = to_message_item (item);
409 const struct msg *msg = message_item_get_msg (msg_item);
410 ds_put_format (&name, "%s: %s", _("Message"),
411 msg_severity_to_string (msg->severity));
413 else if (is_table_item (item))
415 const struct table_item_text *title
416 = table_item_get_title (to_table_item (item));
418 ds_put_format (&name, "Table: %s", title->content);
420 ds_put_cstr (&name, "Table");
422 else if (is_chart_item (item))
424 const char *s = chart_item_get_title (to_chart_item (item));
426 ds_put_format (&name, "Chart: %s", s);
428 ds_put_cstr (&name, "Chart");
430 else if (is_group_open_item (item))
431 ds_put_cstr (&name, to_group_open_item (item)->command_name);
432 gtk_tree_store_set (store, &iter,
433 COL_NAME, ds_cstr (&name),
439 GtkTreePath *path = gtk_tree_model_get_path (
440 GTK_TREE_MODEL (store), &iter);
441 gtk_tree_view_expand_row (view->overview, path, TRUE);
442 gtk_tree_path_free (path);
445 if (view->max_width < tw)
446 view->max_width = tw;
449 gtk_layout_set_size (view->output, view->max_width, view->y);
453 on_row_activate (GtkTreeView *overview,
455 GtkTreeViewColumn *column,
456 struct psppire_output_view *view)
464 model = gtk_tree_view_get_model (overview);
465 if (!gtk_tree_model_get_iter (model, &iter, path))
468 gtk_tree_model_get_value (model, &iter, COL_Y, &value);
469 y = g_value_get_long (&value);
470 g_value_unset (&value);
472 vadj = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (view->output));
473 min = gtk_adjustment_get_lower (vadj);
474 max = gtk_adjustment_get_upper (vadj) - gtk_adjustment_get_page_size (vadj);
479 gtk_adjustment_set_value (vadj, y);
483 on_style_updated (GtkWidget *toplevel, struct psppire_output_view *view)
485 if (!view->n_items || !gtk_widget_get_window (GTK_WIDGET (view->output)))
488 /* GTK+ fires this signal for trivial changes like the mouse moving in or out
489 of the window. Check whether the actual rendering options changed and
490 re-render only if they did. */
491 struct string_map options = STRING_MAP_INITIALIZER (options);
492 get_xr_options (view, &options);
493 if (!string_map_equals (&options, &view->render_opts))
495 xr_driver_destroy (view->xr);
500 string_map_destroy (&options);
511 /* GNU Hurd doesn't have PATH_MAX. Use a fallback.
512 Temporary directory names are usually not that long. */
514 # define PATH_MAX 1024
518 clipboard_get_cb (GtkClipboard *clipboard,
519 GtkSelectionData *selection_data,
523 struct psppire_output_view *view = data;
527 struct output_driver *driver = NULL;
528 char dirname[PATH_MAX], *filename;
529 struct string_map options;
531 GtkTreeSelection *sel = gtk_tree_view_get_selection (view->overview);
532 GtkTreeModel *model = gtk_tree_view_get_model (view->overview);
534 GList *rows = gtk_tree_selection_get_selected_rows (sel, &model);
540 if (path_search (dirname, sizeof dirname, NULL, NULL, true)
541 || mkdtemp (dirname) == NULL)
543 msg_error (errno, _("failed to create temporary directory during clipboard operation"));
546 filename = xasprintf ("%s/clip.tmp", dirname);
548 string_map_init (&options);
549 string_map_insert (&options, "output-file", filename);
553 case SELECT_FMT_UTF8:
554 string_map_insert (&options, "box", "unicode");
557 case SELECT_FMT_TEXT:
558 string_map_insert (&options, "format", "txt");
561 case SELECT_FMT_HTML:
562 string_map_insert (&options, "format", "html");
563 string_map_insert (&options, "borders", "false");
564 string_map_insert (&options, "css", "false");
568 string_map_insert (&options, "format", "odt");
572 g_warning ("unsupported clip target\n");
577 driver = output_driver_create (&options);
583 GtkTreePath *path = n->data ;
585 struct output_item *item ;
587 gtk_tree_model_get_iter (model, &iter, path);
588 gtk_tree_model_get (model, &iter, COL_ADDR, &item, -1);
590 driver->class->submit (driver, item);
595 if (driver->class->flush)
596 driver->class->flush (driver);
599 /* Some drivers (eg: the odt one) don't write anything until they
601 output_driver_destroy (driver);
604 if (g_file_get_contents (filename, &text, &length, NULL))
606 gtk_selection_data_set (selection_data, gtk_selection_data_get_target (selection_data),
608 (const guchar *) text, length);
614 output_driver_destroy (driver);
626 clipboard_clear_cb (GtkClipboard *clipboard,
631 static const GtkTargetEntry targets[] = {
633 { "STRING", 0, SELECT_FMT_TEXT },
634 { "TEXT", 0, SELECT_FMT_TEXT },
635 { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
636 { "text/plain", 0, SELECT_FMT_TEXT },
638 { "UTF8_STRING", 0, SELECT_FMT_UTF8 },
639 { "text/plain;charset=utf-8", 0, SELECT_FMT_UTF8 },
641 { "text/html", 0, SELECT_FMT_HTML },
643 { "application/vnd.oasis.opendocument.text", 0, SELECT_FMT_ODT }
647 on_copy (struct psppire_output_view *view)
649 GtkWidget *widget = GTK_WIDGET (view->overview);
650 GtkClipboard *cb = gtk_widget_get_clipboard (widget, GDK_SELECTION_CLIPBOARD);
652 if (!gtk_clipboard_set_with_data (cb, targets, G_N_ELEMENTS (targets),
653 clipboard_get_cb, clipboard_clear_cb,
655 clipboard_clear_cb (cb, view);
659 on_selection_change (GtkTreeSelection *sel, GAction *copy_action)
661 /* The Copy action is available only if there is something selected */
662 g_object_set (copy_action,
663 "enabled", gtk_tree_selection_count_selected_rows (sel) > 0,
668 on_select_all (struct psppire_output_view *view)
670 GtkTreeSelection *sel = gtk_tree_view_get_selection (view->overview);
671 gtk_tree_view_expand_all (view->overview);
672 gtk_tree_selection_select_all (sel);
676 on_size_allocate (GtkWidget *widget,
677 GdkRectangle *allocation,
678 struct psppire_output_view *view)
680 view->render_width = MAX (300, allocation->width);
685 on_realize (GtkWidget *overview, GObject *view)
687 GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (overview));
688 gtk_tree_selection_set_mode (sel, GTK_SELECTION_MULTIPLE);
690 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (overview));
692 GAction *copy_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
695 GAction *select_all_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
698 g_object_set (copy_action, "enabled", FALSE, NULL);
700 g_signal_connect_swapped (select_all_action, "activate",
701 G_CALLBACK (on_select_all), view);
703 g_signal_connect_swapped (copy_action, "activate",
704 G_CALLBACK (on_copy), view);
706 g_signal_connect (sel, "changed", G_CALLBACK (on_selection_change),
710 struct psppire_output_view *
711 psppire_output_view_new (GtkLayout *output, GtkTreeView *overview)
713 struct psppire_output_view *view;
714 GtkTreeViewColumn *column;
715 GtkCellRenderer *renderer;
719 view = xmalloc (sizeof *view);
721 view->font_height = 0;
722 view->output = output;
723 view->render_width = 0;
726 string_map_init (&view->render_opts);
727 view->overview = overview;
728 view->cur_group = NULL;
729 view->toplevel = gtk_widget_get_toplevel (GTK_WIDGET (output));
731 view->n_items = view->allocated_items = 0;
732 view->print_settings = NULL;
733 view->print_xrd = NULL;
734 view->print_item = 0;
735 view->print_n_pages = 0;
736 view->paginated = FALSE;
738 g_signal_connect (output, "draw", G_CALLBACK (layout_draw_callback), NULL);
740 g_signal_connect (output, "style-updated", G_CALLBACK (on_style_updated), view);
742 g_signal_connect (output, "size-allocate", G_CALLBACK (on_size_allocate), view);
744 gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (output)),
745 GTK_STYLE_CLASS_VIEW);
749 g_signal_connect (overview, "realize", G_CALLBACK (on_realize), view);
751 model = GTK_TREE_MODEL (gtk_tree_store_new (
753 G_TYPE_STRING, /* COL_NAME */
754 G_TYPE_POINTER, /* COL_ADDR */
755 G_TYPE_LONG)); /* COL_Y */
756 gtk_tree_view_set_model (overview, model);
757 g_object_unref (model);
759 column = gtk_tree_view_column_new ();
760 gtk_tree_view_append_column (GTK_TREE_VIEW (overview), column);
761 renderer = gtk_cell_renderer_text_new ();
762 gtk_tree_view_column_pack_start (column, renderer, TRUE);
763 gtk_tree_view_column_add_attribute (column, renderer, "text", COL_NAME);
765 g_signal_connect (GTK_TREE_VIEW (overview),
766 "row-activated", G_CALLBACK (on_row_activate), view);
773 psppire_output_view_destroy (struct psppire_output_view *view)
780 g_signal_handlers_disconnect_by_func (view->output,
781 G_CALLBACK (on_style_updated), view);
783 string_map_destroy (&view->render_opts);
785 for (i = 0; i < view->n_items; i++)
786 output_item_unref (view->items[i].item);
789 view->n_items = view->allocated_items = 0;
791 if (view->print_settings != NULL)
792 g_object_unref (view->print_settings);
794 xr_driver_destroy (view->xr);
800 psppire_output_view_clear (struct psppire_output_view *view)
807 for (i = 0; i < view->n_items; i++)
809 gtk_container_remove (GTK_CONTAINER (view->output),
810 view->items[i].drawing_area);
811 output_item_unref (view->items[i].item);
815 view->n_items = view->allocated_items = 0;
821 psppire_output_view_export (struct psppire_output_view *view,
822 struct string_map *options)
824 struct output_driver *driver;
826 driver = output_driver_create (options);
831 for (i = 0; i < view->n_items; i++)
832 driver->class->submit (driver, view->items[i].item);
833 output_driver_destroy (driver);
840 get_cairo_context_from_print_context (GtkPrintContext *context)
842 cairo_t *cr = gtk_print_context_get_cairo_context (context);
845 For all platforms except windows, gtk_print_context_get_dpi_[xy] returns 72.
848 double xres = gtk_print_context_get_dpi_x (context);
849 double yres = gtk_print_context_get_dpi_y (context);
851 /* This means that the cairo context now has its dimensions in Points */
852 cairo_scale (cr, xres / 72.0, yres / 72.0);
859 create_xr_print_driver (GtkPrintContext *context, struct psppire_output_view *view)
861 struct string_map options;
862 GtkPageSetup *page_setup;
863 double width, height;
867 double bottom_margin;
869 page_setup = gtk_print_context_get_page_setup (context);
870 width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM);
871 height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_MM);
872 left_margin = gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM);
873 right_margin = gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_MM);
874 top_margin = gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM);
875 bottom_margin = gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_MM);
877 string_map_init (&options);
878 string_map_insert_nocopy (&options, xstrdup ("paper-size"),
879 c_xasprintf("%.2fx%.2fmm", width, height));
880 string_map_insert_nocopy (&options, xstrdup ("left-margin"),
881 c_xasprintf ("%.2fmm", left_margin));
882 string_map_insert_nocopy (&options, xstrdup ("right-margin"),
883 c_xasprintf ("%.2fmm", right_margin));
884 string_map_insert_nocopy (&options, xstrdup ("top-margin"),
885 c_xasprintf ("%.2fmm", top_margin));
886 string_map_insert_nocopy (&options, xstrdup ("bottom-margin"),
887 c_xasprintf ("%.2fmm", bottom_margin));
889 view->print_xrd = xr_driver_create (get_cairo_context_from_print_context (context), &options);
891 string_map_destroy (&options);
895 paginate (GtkPrintOperation *operation,
896 GtkPrintContext *context,
897 struct psppire_output_view *view)
901 /* Sometimes GTK+ emits this signal again even after pagination is
902 complete. Don't let that screw up printing. */
905 else if (view->print_item < view->n_items)
907 xr_driver_output_item (view->print_xrd,
908 view->items[view->print_item++].item);
909 while (xr_driver_need_new_page (view->print_xrd))
911 xr_driver_next_page (view->print_xrd, get_cairo_context_from_print_context (context));
912 view->print_n_pages ++;
918 gtk_print_operation_set_n_pages (operation, view->print_n_pages);
920 /* Re-create the driver to do the real printing. */
921 xr_driver_destroy (view->print_xrd);
922 create_xr_print_driver (context, view);
923 view->print_item = 0;
924 view->paginated = TRUE;
931 begin_print (GtkPrintOperation *operation,
932 GtkPrintContext *context,
933 struct psppire_output_view *view)
935 create_xr_print_driver (context, view);
937 view->print_item = 0;
938 view->print_n_pages = 1;
939 view->paginated = FALSE;
943 end_print (GtkPrintOperation *operation,
944 GtkPrintContext *context,
945 struct psppire_output_view *view)
947 xr_driver_destroy (view->print_xrd);
952 draw_page (GtkPrintOperation *operation,
953 GtkPrintContext *context,
955 struct psppire_output_view *view)
957 xr_driver_next_page (view->print_xrd, get_cairo_context_from_print_context (context));
958 while (!xr_driver_need_new_page (view->print_xrd)
959 && view->print_item < view->n_items)
960 xr_driver_output_item (view->print_xrd, view->items [view->print_item++].item);
965 psppire_output_view_print (struct psppire_output_view *view,
966 GtkWindow *parent_window)
968 GtkPrintOperationResult res;
970 GtkPrintOperation *print = gtk_print_operation_new ();
972 if (view->print_settings != NULL)
973 gtk_print_operation_set_print_settings (print, view->print_settings);
975 g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), view);
976 g_signal_connect (print, "end_print", G_CALLBACK (end_print), view);
977 g_signal_connect (print, "paginate", G_CALLBACK (paginate), view);
978 g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), view);
980 res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
981 parent_window, NULL);
983 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
985 if (view->print_settings != NULL)
986 g_object_unref (view->print_settings);
987 view->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
990 g_object_unref (print);
993 struct psppire_output_view_driver
995 struct output_driver driver;
996 struct psppire_output_view *view;
999 static struct psppire_output_view_driver *
1000 psppire_output_view_driver_cast (struct output_driver *driver)
1002 return UP_CAST (driver, struct psppire_output_view_driver, driver);
1006 psppire_output_view_submit (struct output_driver *this,
1007 const struct output_item *item)
1009 struct psppire_output_view_driver *povd = psppire_output_view_driver_cast (this);
1011 if (is_table_item (item))
1012 psppire_output_view_put (povd->view, item);
1015 static struct output_driver_class psppire_output_view_driver_class =
1017 "PSPPIRE Output View", /* name */
1019 psppire_output_view_submit, /* submit */
1024 psppire_output_view_register_driver (struct psppire_output_view *view)
1026 struct psppire_output_view_driver *povd;
1027 struct output_driver *d;
1029 povd = xzalloc (sizeof *povd);
1032 output_driver_init (d, &psppire_output_view_driver_class, "PSPPIRE Output View",
1033 SETTINGS_DEVICE_UNFILTERED);
1034 output_driver_register (d);