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"
21 #include <cairo/cairo-svg.h>
25 #include "libpspp/assertion.h"
26 #include "libpspp/string-map.h"
27 #include "output/cairo-fsm.h"
28 #include "output/cairo-pager.h"
29 #include "output/driver-provider.h"
30 #include "output/driver.h"
31 #include "output/output-item.h"
32 #include "output/pivot-table.h"
34 #include "gl/c-xvasprintf.h"
35 #include "gl/minmax.h"
36 #include "gl/clean-temp.h"
37 #include "gl/xalloc.h"
40 #define _(msgid) gettext (msgid)
42 struct output_view_item
44 struct output_item *item;
45 GtkWidget *drawing_area;
48 struct psppire_output_view
50 struct xr_fsm_style *style;
58 GtkTreeView *overview;
59 GtkTreePath *cur_group;
63 guint buttontime; /* Time of the button event */
65 struct output_view_item *items;
66 size_t n_items, allocated_items;
67 struct output_view_item *selected_item;
69 /* Variables pertaining to printing */
70 GtkPrintSettings *print_settings;
72 struct xr_fsm_style *fsm_style;
73 struct xr_page_style *page_style;
74 struct xr_pager *pager;
82 COL_LABEL, /* Output item label. */
83 COL_ADDR, /* Pointer to the table */
84 COL_Y, /* Y position of top of object. */
88 static GtkTargetList *build_target_list (const struct output_item *item);
89 static void clipboard_get_cb (GtkClipboard *clipboard,
90 GtkSelectionData *selection_data,
94 /* Draws a white background on the GtkLayout to match the white background of
95 each of the output items. */
97 layout_draw_callback (GtkWidget *widget, cairo_t *cr, gpointer data)
99 int width = gtk_widget_get_allocated_width (widget);
100 int height = gtk_widget_get_allocated_height (widget);
101 GtkStyleContext *context = gtk_widget_get_style_context (widget);
102 gtk_render_background (context, cr, 0, 0, width, height);
103 return FALSE; /* Continue drawing the GtkDrawingAreas. */
107 draw_callback (GtkWidget *widget, cairo_t *cr, gpointer data)
110 if (!gdk_cairo_get_clip_rectangle (cr, &clip))
113 struct xr_fsm *fsm = g_object_get_data (G_OBJECT (widget), "fsm");
115 /* Draw the background based on the state of the widget
116 which can be selected or not selected */
117 GtkStyleContext *context = gtk_widget_get_style_context (widget);
118 gtk_render_background (context, cr, clip.x, clip.y,
119 clip.x + clip.width, clip.y + clip.height);
120 /* Select the default foreground color based on current style
121 and state of the widget */
122 GtkStateFlags state = gtk_widget_get_state_flags (widget);
124 gtk_style_context_get_color (context, state, &color);
125 cairo_set_source_rgba (cr, color.red, color.green, color.blue, color.alpha);
126 xr_fsm_draw_region (fsm, cr, clip.x, clip.y, clip.width, clip.height);
132 free_fsm (gpointer fsm_)
134 struct xr_fsm *fsm = fsm_;
135 xr_fsm_destroy (fsm);
138 static struct xr_fsm_style *
139 get_xr_fsm_style (struct psppire_output_view *view)
141 GtkStyleContext *context
142 = gtk_widget_get_style_context (GTK_WIDGET (view->output));
143 GtkStateFlags state = gtk_widget_get_state_flags (GTK_WIDGET (view->output));
145 int xr_width = view->render_width * 1000;
147 PangoFontDescription *pf;
148 gtk_style_context_get (context, state, "font", &pf, NULL);
150 struct xr_fsm_style *style = xmalloc (sizeof *style);
151 *style = (struct xr_fsm_style) {
153 .size = { [TABLE_HORZ] = xr_width, [TABLE_VERT] = INT_MAX },
154 .min_break = { [TABLE_HORZ] = xr_width / 2, [TABLE_VERT] = 0 },
156 .use_system_colors = true,
157 .object_spacing = XR_POINT * 12,
158 .font_resolution = 96.0,
164 /* Return the horizontal position to place a widget whose
165 width is CHILD_WIDTH */
167 get_xpos (const struct psppire_output_view *view, gint child_width)
169 GdkWindow *gdkw = gtk_widget_get_window (GTK_WIDGET (view->output));
170 guint w = gdk_window_get_width (gdkw);
172 g_object_get (view->output, "border-width", &gutter, NULL);
173 return (gtk_widget_get_direction (GTK_WIDGET (view->output)) == GTK_TEXT_DIR_RTL) ? w - child_width - gutter: gutter;
176 static struct output_view_item *
177 find_selected_item (struct psppire_output_view *view)
179 struct output_view_item *item = NULL;
182 if (view->items == NULL)
185 for (item = view->items; item < &view->items[view->n_items]; item++)
187 GtkWidget *widget = GTK_WIDGET (item->drawing_area);
188 if (GTK_IS_WIDGET (widget))
190 GtkStateFlags state = gtk_widget_get_state_flags (widget);
191 if (state & GTK_STATE_FLAG_SELECTED)
200 set_copy_action (struct psppire_output_view *view,
203 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (view->output));
204 GAction *copy_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
206 g_object_set (copy_action,
212 clear_selection (struct psppire_output_view *view)
216 struct output_view_item *item = find_selected_item (view);
219 set_copy_action (view, FALSE);
220 GtkWidget *widget = GTK_WIDGET (item->drawing_area);
221 if (GTK_IS_WIDGET (widget))
223 gtk_widget_unset_state_flags (widget, GTK_STATE_FLAG_SELECTED);
224 gtk_widget_queue_draw (widget);
229 off_item_button_press_event_cb (GtkWidget *widget,
230 GdkEventButton *event,
231 struct psppire_output_view *view)
233 /* buttontime is set by button_press_event_cb
234 If our event->time is equal to the time from the
235 button_press_event_cb, then we handle the same event.
236 In that case we must not clear the selection because
237 it was just set by button_press_event_cb from the item */
238 if (event->time != view->buttontime)
239 clear_selection (view);
240 return FALSE; /* Forward the event -> DragNDrop */
244 button_press_event_cb (GtkWidget *widget,
245 GdkEventButton *event,
246 struct psppire_output_view *view)
248 view->buttontime = event->time;
249 clear_selection (view);
250 set_copy_action (view, TRUE);
251 gtk_widget_set_state_flags (widget, GTK_STATE_FLAG_SELECTED, FALSE);
252 gtk_widget_queue_draw (widget);
253 return FALSE; /* Forward Event -> off_item will trigger */
257 drag_data_get_cb (GtkWidget *widget, GdkDragContext *context,
258 GtkSelectionData *selection_data,
259 guint target_type, guint time,
260 struct psppire_output_view *view)
262 view->selected_item = find_selected_item (view);
263 clipboard_get_cb (NULL, selection_data, target_type, view);
267 create_drawing_area (struct psppire_output_view *view,
268 GtkWidget *drawing_area, struct xr_fsm *r,
269 int tw, int th, const struct output_item *item)
271 g_object_set_data_full (G_OBJECT (drawing_area),
273 g_signal_connect (drawing_area, "button-press-event",
274 G_CALLBACK (button_press_event_cb), view);
275 gtk_widget_add_events (drawing_area, GDK_BUTTON_PRESS_MASK);
277 { /* Drag and Drop */
278 GtkTargetList *tl = build_target_list (item);
280 gtk_drag_source_set (drawing_area, GDK_BUTTON1_MASK, NULL, 0, GDK_ACTION_COPY);
281 gtk_drag_source_set_target_list (drawing_area, tl);
282 gtk_target_list_unref (tl);
283 g_signal_connect (drawing_area, "drag-data-get",
284 G_CALLBACK (drag_data_get_cb), view);
286 GtkStyleContext *context = gtk_widget_get_style_context (drawing_area);
287 gtk_style_context_add_class (context,
288 GTK_STYLE_CLASS_VIEW);
289 g_signal_connect (drawing_area, "draw",
290 G_CALLBACK (draw_callback), view);
292 gtk_widget_set_size_request (drawing_area, tw, th);
293 gint xpos = get_xpos (view, tw);
295 gtk_layout_put (view->output, drawing_area, xpos, view->y);
297 gtk_widget_show (drawing_area);
301 rerender (struct psppire_output_view *view)
303 struct output_view_item *item;
304 GdkWindow *gdkw = gtk_widget_get_window (GTK_WIDGET (view->output));
306 if (!view->n_items || ! gdkw)
310 view->style = get_xr_fsm_style (view);
312 GdkWindow *win = gtk_layout_get_bin_window (view->output);
313 cairo_region_t *region = gdk_window_get_visible_region (win);
314 GdkDrawingContext *ctx = gdk_window_begin_draw_frame (win, region);
315 cairo_t *cr = gdk_drawing_context_get_cairo_context (ctx);
319 for (item = view->items; item < &view->items[view->n_items]; item++)
326 view->y += view->object_spacing;
328 if (item->item->type == OUTPUT_ITEM_GROUP)
331 r = xr_fsm_create_for_scrolling (item->item, view->style, cr);
334 g_warn_if_reached ();
338 xr_fsm_measure (r, cr, &tw, &th);
340 gint xpos = get_xpos (view, tw);
342 if (!item->drawing_area)
344 item->drawing_area = gtk_drawing_area_new ();
345 create_drawing_area (view, item->drawing_area, r, tw, th, item->item);
349 g_object_set_data_full (G_OBJECT (item->drawing_area),
351 gtk_widget_set_size_request (item->drawing_area, tw, th);
352 gtk_layout_move (view->output, item->drawing_area, xpos, view->y);
355 if (item->item->type == OUTPUT_ITEM_TABLE)
356 gtk_widget_set_tooltip_text (item->drawing_area,
357 item->item->table->notes);
362 /* This code probably doesn't bring us anthing, but Gtk
363 shows warnings if get_preferred_width/height is not
364 called before the size_allocate below is called. */
365 gtk_widget_get_preferred_width (item->drawing_area, &minw, NULL);
366 gtk_widget_get_preferred_height (item->drawing_area, &minh, NULL);
367 if (th > minh) th = minh;
368 if (tw > minw) tw = minw;
375 gtk_widget_size_allocate (item->drawing_area, &alloc);
377 if (view->max_width < tw)
378 view->max_width = tw;
382 gtk_layout_set_size (view->output,
383 view->max_width + view->object_spacing,
384 view->y + view->object_spacing);
386 gdk_window_end_draw_frame (win, ctx);
387 cairo_region_destroy (region);
392 psppire_output_view_put (struct psppire_output_view *view,
393 const struct output_item *item)
395 struct output_view_item *view_item;
396 GtkWidget *drawing_area;
399 if (item->type == OUTPUT_ITEM_GROUP)
402 if (item->type == OUTPUT_ITEM_TEXT)
404 char *text = text_item_get_plain_text (item);
405 bool text_is_empty = text[0] == '\0';
411 if (view->n_items >= view->allocated_items)
412 view->items = x2nrealloc (view->items, &view->allocated_items,
413 sizeof *view->items);
414 view_item = &view->items[view->n_items++];
415 view_item->item = output_item_ref (item);
416 view_item->drawing_area = NULL;
418 GdkWindow *win = gtk_widget_get_window (GTK_WIDGET (view->output));
421 view_item->drawing_area = drawing_area = gtk_drawing_area_new ();
424 view->style = get_xr_fsm_style (view);
426 cairo_region_t *region = gdk_window_get_visible_region (win);
427 GdkDrawingContext *ctx = gdk_window_begin_draw_frame (win, region);
428 cairo_t *cr = gdk_drawing_context_get_cairo_context (ctx);
431 view->y += view->object_spacing;
433 struct xr_fsm *r = xr_fsm_create_for_scrolling (item, view->style, cr);
436 gdk_window_end_draw_frame (win, ctx);
437 cairo_region_destroy (region);
441 xr_fsm_measure (r, cr, &tw, &th);
442 create_drawing_area (view, drawing_area, r, tw, th, item);
443 gdk_window_end_draw_frame (win, ctx);
444 cairo_region_destroy (region);
451 GtkTreeStore *store = GTK_TREE_STORE (
452 gtk_tree_view_get_model (view->overview));
454 /* Create a new node in the tree and puts a reference to it in 'iter'. */
458 && gtk_tree_path_get_depth (view->cur_group) > 0
459 && gtk_tree_model_get_iter (GTK_TREE_MODEL (store),
460 &parent, view->cur_group))
461 gtk_tree_store_append (store, &iter, &parent);
463 gtk_tree_store_append (store, &iter, NULL);
466 gtk_tree_store_set (store, &iter,
467 COL_LABEL, output_item_get_label (item),
472 GtkTreePath *path = gtk_tree_model_get_path (
473 GTK_TREE_MODEL (store), &iter);
474 gtk_tree_view_expand_row (view->overview, path, TRUE);
475 gtk_tree_path_free (path);
478 if (view->max_width < tw)
479 view->max_width = tw;
482 gtk_layout_set_size (view->output, view->max_width, view->y);
486 on_row_activate (GtkTreeView *overview,
488 GtkTreeViewColumn *column,
489 struct psppire_output_view *view)
497 model = gtk_tree_view_get_model (overview);
498 if (!gtk_tree_model_get_iter (model, &iter, path))
501 gtk_tree_model_get_value (model, &iter, COL_Y, &value);
502 y = g_value_get_long (&value);
503 g_value_unset (&value);
505 vadj = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (view->output));
506 min = gtk_adjustment_get_lower (vadj);
507 max = gtk_adjustment_get_upper (vadj) - gtk_adjustment_get_page_size (vadj);
512 gtk_adjustment_set_value (vadj, y);
516 on_style_updated (GtkWidget *toplevel, struct psppire_output_view *view)
518 if (!view->n_items || !gtk_widget_get_window (GTK_WIDGET (view->output)))
521 /* GTK+ fires this signal for trivial changes like the mouse moving in or out
522 of the window. Check whether the actual fsm options changed and
523 re-render only if they did. */
524 struct xr_fsm_style *style = get_xr_fsm_style (view);
525 if (!view->style || !xr_fsm_style_equals (style, view->style))
527 xr_fsm_style_unref (view->style);
528 view->style = xr_fsm_style_ref (style);
531 xr_fsm_style_unref (style);
545 clear_rectangle (cairo_surface_t *surface,
546 double x0, double y0, double x1, double y1)
548 cairo_t *cr = cairo_create (surface);
549 cairo_set_source_rgb (cr, 1, 1, 1);
551 cairo_rectangle (cr, x0, y0, x1 - x0, y1 - y0);
557 clipboard_get_cb (GtkClipboard *clipboard,
558 GtkSelectionData *selection_data,
562 struct psppire_output_view *view = data;
566 struct output_driver *driver = NULL;
568 struct string_map options;
569 struct temp_dir *td = NULL;
571 if (view->selected_item == NULL)
574 td = create_temp_dir ("pspp", NULL, false);
577 msg_error (errno, _("failed to create temporary directory during clipboard operation"));
580 filename = xasprintf ("%s/clip.tmp", td->dir_name);
582 string_map_init (&options);
583 string_map_insert (&options, "output-file", filename);
587 case SELECT_FMT_UTF8:
588 string_map_insert (&options, "box", "unicode");
591 case SELECT_FMT_TEXT:
592 string_map_insert (&options, "format", "txt");
593 string_map_insert (&options, "width", "1000");
596 case SELECT_FMT_HTML:
597 string_map_insert (&options, "format", "html");
598 string_map_insert (&options, "borders", "false");
599 string_map_insert (&options, "css", "false");
608 string_map_insert (&options, "format", "odt");
612 g_warning ("unsupported clip target\n");
617 if ((info == SELECT_FMT_IMG) ||
618 (info == SELECT_FMT_SVG) )
620 GtkWidget *widget = view->selected_item->drawing_area;
621 struct xr_fsm *fsm = g_object_get_data (G_OBJECT (widget), "fsm");
623 GdkWindow *win = gtk_layout_get_bin_window (view->output);
624 cairo_region_t *region = gdk_window_get_visible_region (win);
625 GdkDrawingContext *ctx = gdk_window_begin_draw_frame (win, region);
626 cairo_t *cr = gdk_drawing_context_get_cairo_context (ctx);
629 xr_fsm_measure (fsm, cr, &w, &h);
631 gdk_window_end_draw_frame (win, ctx);
632 cairo_region_destroy (region);
634 cairo_surface_t *surface
635 = (info == SELECT_FMT_SVG
636 ? cairo_svg_surface_create (filename, w, h)
637 : cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h));
638 clear_rectangle (surface, 0, 0, w, h);
639 cairo_t *cr2 = cairo_create (surface);
640 xr_fsm_draw_all (fsm, cr2);
642 if (info == SELECT_FMT_IMG)
644 GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface (surface,
648 gtk_selection_data_set_pixbuf (selection_data, pixbuf);
649 g_object_unref (pixbuf);
652 cairo_surface_destroy (surface);
656 driver = output_driver_create (&options);
660 driver->class->submit (driver, view->selected_item->item);
662 if (driver->class->flush)
663 driver->class->flush (driver);
665 /* Some drivers (eg: the odt one) don't write anything until they
667 output_driver_destroy (driver);
671 if (info != SELECT_FMT_IMG
672 && g_file_get_contents (filename, &text, &length, NULL))
673 gtk_selection_data_set (selection_data,
674 gtk_selection_data_get_target (selection_data),
675 8, (const guchar *) text, length);
680 output_driver_destroy (driver);
686 cleanup_temp_dir (td);
690 clipboard_clear_cb (GtkClipboard *clipboard,
696 CT ( ctn1, "STRING", 0, SELECT_FMT_TEXT ) \
697 CT ( ctn2, "TEXT", 0, SELECT_FMT_TEXT ) \
698 CT ( ctn3, "COMPOUND_TEXT", 0, SELECT_FMT_TEXT ) \
699 CT ( ctn4, "text/plain", 0, SELECT_FMT_TEXT ) \
700 CT ( ctn5, "UTF8_STRING", 0, SELECT_FMT_UTF8 ) \
701 CT ( ctn6, "text/plain;charset=utf-8", 0, SELECT_FMT_UTF8 ) \
702 CT ( ctn7, "text/html", 0, SELECT_FMT_HTML ) \
703 CT ( ctn8, "image/svg+xml", 0, SELECT_FMT_SVG )
705 #define CT(ID, TARGET, FLAGS, INFO) static gchar ID[] = TARGET;
708 static gchar ctnlast[] = "application/vnd.oasis.opendocument.text";
710 static const GtkTargetEntry targets[] = {
711 #define CT(ID, TARGET, FLAGS, INFO) { ID, FLAGS, INFO },
714 { ctnlast, 0, SELECT_FMT_ODT }
717 static GtkTargetList *
718 build_target_list (const struct output_item *item)
720 GtkTargetList *tl = gtk_target_list_new (targets, G_N_ELEMENTS (targets));
721 g_return_val_if_fail (tl, NULL);
722 if (item->type == OUTPUT_ITEM_TABLE || item->type == OUTPUT_ITEM_CHART)
723 gtk_target_list_add_image_targets (tl, SELECT_FMT_IMG, TRUE);
728 on_copy (struct psppire_output_view *view)
730 GtkWidget *widget = GTK_WIDGET (view->overview);
731 GtkClipboard *cb = gtk_widget_get_clipboard (widget, GDK_SELECTION_CLIPBOARD);
733 struct output_view_item *ov_item = find_selected_item (view);
736 view->selected_item = ov_item;
737 GtkTargetList *tl = build_target_list (ov_item->item);
738 g_return_if_fail (tl);
739 gint no_of_targets = 0;
740 GtkTargetEntry *ta = gtk_target_table_new_from_list (tl, &no_of_targets);
741 g_return_if_fail (ta);
742 if (!gtk_clipboard_set_with_data (cb, ta, no_of_targets,
743 clipboard_get_cb, clipboard_clear_cb,
745 clipboard_clear_cb (cb, view);
747 gtk_target_list_unref (tl);
748 gtk_target_table_free (ta,no_of_targets);
752 on_size_allocate (GtkWidget *widget,
753 GdkRectangle *allocation,
754 struct psppire_output_view *view)
756 view->render_width = MAX (300, allocation->width);
761 on_realize (GtkWidget *overview, GObject *view)
763 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (overview));
765 GAction *copy_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
768 GAction *select_all_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
771 g_object_set (copy_action, "enabled", FALSE, NULL);
772 g_object_set (select_all_action, "enabled", FALSE, NULL);
774 g_signal_connect_swapped (copy_action, "activate",
775 G_CALLBACK (on_copy), view);
779 struct psppire_output_view *
780 psppire_output_view_new (GtkLayout *output, GtkTreeView *overview)
782 struct psppire_output_view *view;
783 GtkTreeViewColumn *column;
784 GtkCellRenderer *renderer;
788 view = xmalloc (sizeof *view);
789 *view = (struct psppire_output_view) {
790 .object_spacing = 10,
792 .overview = overview,
793 .toplevel = gtk_widget_get_toplevel (GTK_WIDGET (output)),
796 g_signal_connect (output, "draw", G_CALLBACK (layout_draw_callback), NULL);
798 g_signal_connect (output, "style-updated", G_CALLBACK (on_style_updated), view);
800 g_signal_connect (output, "size-allocate", G_CALLBACK (on_size_allocate), view);
802 gtk_widget_add_events (GTK_WIDGET (output), GDK_BUTTON_PRESS_MASK);
803 g_signal_connect (output, "button-press-event",
804 G_CALLBACK (off_item_button_press_event_cb), view);
806 gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (output)),
807 GTK_STYLE_CLASS_VIEW);
811 g_signal_connect (overview, "realize", G_CALLBACK (on_realize), view);
813 model = GTK_TREE_MODEL (gtk_tree_store_new (
815 G_TYPE_STRING, /* COL_LABEL */
816 G_TYPE_POINTER, /* COL_ADDR */
817 G_TYPE_LONG)); /* COL_Y */
818 gtk_tree_view_set_model (overview, model);
819 g_object_unref (model);
821 column = gtk_tree_view_column_new ();
822 gtk_tree_view_append_column (GTK_TREE_VIEW (overview), column);
823 renderer = gtk_cell_renderer_text_new ();
824 gtk_tree_view_column_pack_start (column, renderer, TRUE);
825 gtk_tree_view_column_add_attribute (column, renderer, "text", COL_LABEL);
827 g_signal_connect (GTK_TREE_VIEW (overview),
828 "row-activated", G_CALLBACK (on_row_activate), view);
835 psppire_output_view_destroy (struct psppire_output_view *view)
842 g_signal_handlers_disconnect_by_func (view->output,
843 G_CALLBACK (on_style_updated), view);
845 xr_fsm_style_unref (view->style);
847 for (i = 0; i < view->n_items; i++)
848 output_item_unref (view->items[i].item);
851 view->n_items = view->allocated_items = 0;
853 if (view->print_settings != NULL)
854 g_object_unref (view->print_settings);
857 gtk_tree_path_free (view->cur_group);
863 psppire_output_view_clear (struct psppire_output_view *view)
870 for (i = 0; i < view->n_items; i++)
872 gtk_container_remove (GTK_CONTAINER (view->output),
873 view->items[i].drawing_area);
874 output_item_unref (view->items[i].item);
878 view->n_items = view->allocated_items = 0;
884 psppire_output_view_export (struct psppire_output_view *view,
885 struct string_map *options)
887 struct output_driver *driver;
889 driver = output_driver_create (options);
894 for (i = 0; i < view->n_items; i++)
895 driver->class->submit (driver, view->items[i].item);
896 output_driver_destroy (driver);
903 get_cairo_context_from_print_context (GtkPrintContext *context)
905 cairo_t *cr = gtk_print_context_get_cairo_context (context);
906 return cairo_reference (cr);
910 create_xr_print_driver (GtkPrintContext *context, struct psppire_output_view *view)
912 GtkPageSetup *ps = gtk_print_context_get_page_setup (context);
914 enum { H = TABLE_HORZ, V = TABLE_VERT };
915 int paper[TABLE_N_AXES] = {
916 [H] = gtk_page_setup_get_paper_width (ps, GTK_UNIT_POINTS) * XR_POINT,
917 [V] = gtk_page_setup_get_paper_height (ps, GTK_UNIT_POINTS) * XR_POINT,
920 /* These are all 1/2 inch. The "margins" that GTK+ gives us are useless:
921 they are the printer's imagable area. */
922 int margins[TABLE_N_AXES][2] = {
923 [H][0] = XR_POINT * 36,
924 [H][1] = XR_POINT * 36,
925 [V][0] = XR_POINT * 36,
926 [V][1] = XR_POINT * 36,
929 double size[TABLE_N_AXES];
930 for (int a = 0; a < TABLE_N_AXES; a++)
931 size[a] = paper[a] - margins[a][0] - margins[a][1];
933 view->page_style = xmalloc (sizeof *view->page_style);
934 *view->page_style = (struct xr_page_style) {
938 [H] = { margins[H][0], margins[H][1] },
939 [V] = { margins[V][0], margins[V][1] },
941 .initial_page_number = 1,
944 view->fsm_style = xmalloc (sizeof *view->fsm_style);
945 *view->fsm_style = (struct xr_fsm_style) {
948 .size = { [H] = size[H], [V] = size[V] },
949 .min_break = { [H] = size[H] / 2, [V] = size[V] / 2 },
950 .font = pango_font_description_from_string ("Sans Serif 10"),
951 .fg = CELL_COLOR_BLACK,
952 .use_system_colors = false,
953 .object_spacing = 12 * XR_POINT,
954 .font_resolution = 72.0
957 view->pager = xr_pager_create (view->page_style, view->fsm_style);
961 paginate (GtkPrintOperation *operation,
962 GtkPrintContext *context,
963 struct psppire_output_view *view)
967 /* Sometimes GTK+ emits this signal again even after pagination is
968 complete. Don't let that screw up printing. */
971 else if (view->print_item < view->n_items)
973 xr_pager_add_item (view->pager, view->items[view->print_item++].item);
974 while (xr_pager_needs_new_page (view->pager))
976 xr_pager_add_page (view->pager,
977 get_cairo_context_from_print_context (context));
978 view->print_n_pages ++;
984 gtk_print_operation_set_n_pages (operation, MAX (1, view->print_n_pages));
986 /* Re-create the driver to do the real printing. */
987 xr_pager_destroy (view->pager);
988 view->pager = xr_pager_create (view->page_style, view->fsm_style);
989 view->print_item = 0;
990 view->paginated = TRUE;
997 begin_print (GtkPrintOperation *operation,
998 GtkPrintContext *context,
999 struct psppire_output_view *view)
1001 create_xr_print_driver (context, view);
1003 view->print_item = 0;
1004 view->print_n_pages = 0;
1005 view->paginated = FALSE;
1009 end_print (GtkPrintOperation *operation,
1010 GtkPrintContext *context,
1011 struct psppire_output_view *view)
1013 xr_pager_destroy (view->pager);
1019 draw_page (GtkPrintOperation *operation,
1020 GtkPrintContext *context,
1022 struct psppire_output_view *view)
1024 xr_pager_add_page (view->pager,
1025 get_cairo_context_from_print_context (context));
1026 while (!xr_pager_needs_new_page (view->pager)
1027 && view->print_item < view->n_items)
1028 xr_pager_add_item (view->pager, view->items [view->print_item++].item);
1033 psppire_output_view_print (struct psppire_output_view *view,
1034 GtkWindow *parent_window)
1036 GtkPrintOperationResult res;
1038 GtkPrintOperation *print = gtk_print_operation_new ();
1040 if (view->print_settings != NULL)
1041 gtk_print_operation_set_print_settings (print, view->print_settings);
1043 gtk_print_operation_set_use_full_page (print, TRUE);
1044 gtk_print_operation_set_unit (print, GTK_UNIT_POINTS);
1046 g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), view);
1047 g_signal_connect (print, "end_print", G_CALLBACK (end_print), view);
1048 g_signal_connect (print, "paginate", G_CALLBACK (paginate), view);
1049 g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), view);
1051 res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1052 parent_window, NULL);
1054 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
1056 if (view->print_settings != NULL)
1057 g_object_unref (view->print_settings);
1058 view->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
1061 g_object_unref (print);
1064 struct psppire_output_view_driver
1066 struct output_driver driver;
1067 struct psppire_output_view *view;
1070 static struct psppire_output_view_driver *
1071 psppire_output_view_driver_cast (struct output_driver *driver)
1073 return UP_CAST (driver, struct psppire_output_view_driver, driver);
1077 psppire_output_view_submit (struct output_driver *this,
1078 const struct output_item *item)
1080 struct psppire_output_view_driver *povd = psppire_output_view_driver_cast (this);
1082 if (item->type == OUTPUT_ITEM_TABLE)
1083 psppire_output_view_put (povd->view, item);
1086 static struct output_driver_class psppire_output_view_driver_class =
1088 .name = "PSPPIRE Output View",
1089 .submit = psppire_output_view_submit,
1090 .handles_groups = true,
1091 .handles_show = true,
1095 psppire_output_view_register_driver (struct psppire_output_view *view)
1097 struct psppire_output_view_driver *povd;
1098 struct output_driver *d;
1100 povd = xzalloc (sizeof *povd);
1103 output_driver_init (d, &psppire_output_view_driver_class, "PSPPIRE Output View",
1104 SETTINGS_DEVICE_UNFILTERED);
1105 output_driver_register (d);