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/chart-item.h"
32 #include "output/group-item.h"
33 #include "output/message-item.h"
34 #include "output/output-item.h"
35 #include "output/output-item-provider.h"
36 #include "output/table-item.h"
37 #include "output/text-item.h"
39 #include "gl/c-xvasprintf.h"
40 #include "gl/minmax.h"
41 #include "gl/clean-temp.h"
42 #include "gl/xalloc.h"
45 #define _(msgid) gettext (msgid)
47 struct output_view_item
49 struct output_item *item;
50 GtkWidget *drawing_area;
53 struct psppire_output_view
55 struct xr_fsm_style *style;
63 GtkTreeView *overview;
64 GtkTreePath *cur_group;
68 guint buttontime; /* Time of the button event */
70 struct output_view_item *items;
71 size_t n_items, allocated_items;
72 struct output_view_item *selected_item;
74 /* Variables pertaining to printing */
75 GtkPrintSettings *print_settings;
77 struct xr_fsm_style *fsm_style;
78 struct xr_page_style *page_style;
79 struct xr_pager *pager;
87 COL_NAME, /* Table name. */
88 COL_ADDR, /* Pointer to the table */
89 COL_Y, /* Y position of top of name. */
93 static GtkTargetList *build_target_list (const struct output_item *item);
94 static void clipboard_get_cb (GtkClipboard *clipboard,
95 GtkSelectionData *selection_data,
99 /* Draws a white background on the GtkLayout to match the white background of
100 each of the output items. */
102 layout_draw_callback (GtkWidget *widget, cairo_t *cr, gpointer data)
104 int width = gtk_widget_get_allocated_width (widget);
105 int height = gtk_widget_get_allocated_height (widget);
106 GtkStyleContext *context = gtk_widget_get_style_context (widget);
107 gtk_render_background (context, cr, 0, 0, width, height);
108 return FALSE; /* Continue drawing the GtkDrawingAreas. */
112 draw_callback (GtkWidget *widget, cairo_t *cr, gpointer data)
115 if (!gdk_cairo_get_clip_rectangle (cr, &clip))
118 struct xr_fsm *fsm = g_object_get_data (G_OBJECT (widget), "fsm");
120 /* Draw the background based on the state of the widget
121 which can be selected or not selected */
122 GtkStyleContext *context = gtk_widget_get_style_context (widget);
123 gtk_render_background (context, cr, clip.x, clip.y,
124 clip.x + clip.width, clip.y + clip.height);
125 /* Select the default foreground color based on current style
126 and state of the widget */
127 GtkStateFlags state = gtk_widget_get_state_flags (widget);
129 gtk_style_context_get_color (context, state, &color);
130 cairo_set_source_rgba (cr, color.red, color.green, color.blue, color.alpha);
131 xr_fsm_draw_region (fsm, cr, clip.x, clip.y, clip.width, clip.height);
137 free_fsm (gpointer fsm_)
139 struct xr_fsm *fsm = fsm_;
140 xr_fsm_destroy (fsm);
143 static struct xr_fsm_style *
144 get_xr_fsm_style (struct psppire_output_view *view)
146 GtkStyleContext *context
147 = gtk_widget_get_style_context (GTK_WIDGET (view->output));
148 GtkStateFlags state = gtk_widget_get_state_flags (GTK_WIDGET (view->output));
150 int xr_width = view->render_width * 1000;
152 PangoFontDescription *pf;
153 gtk_style_context_get (context, state, "font", &pf, NULL);
154 PangoFontDescription *ff = pango_font_description_from_string ("Monospace");
155 pango_font_description_set_size (ff, pango_font_description_get_size (pf));
157 struct xr_fsm_style *style = xmalloc (sizeof *style);
158 *style = (struct xr_fsm_style) {
160 .size = { [TABLE_HORZ] = xr_width, [TABLE_VERT] = INT_MAX },
161 .min_break = { [TABLE_HORZ] = xr_width / 2, [TABLE_VERT] = 0 },
163 [XR_FONT_PROPORTIONAL] = pf,
164 [XR_FONT_FIXED] = ff,
166 .use_system_colors = true,
167 .font_resolution = 96.0,
173 /* Return the horizontal position to place a widget whose
174 width is CHILD_WIDTH */
176 get_xpos (const struct psppire_output_view *view, gint child_width)
178 GdkWindow *gdkw = gtk_widget_get_window (GTK_WIDGET (view->output));
179 guint w = gdk_window_get_width (gdkw);
181 g_object_get (view->output, "border-width", &gutter, NULL);
182 return (gtk_widget_get_direction (GTK_WIDGET (view->output)) == GTK_TEXT_DIR_RTL) ? w - child_width - gutter: gutter;
185 static struct output_view_item *
186 find_selected_item (struct psppire_output_view *view)
188 struct output_view_item *item = NULL;
191 if (view->items == NULL)
194 for (item = view->items; item < &view->items[view->n_items]; item++)
196 GtkWidget *widget = GTK_WIDGET (item->drawing_area);
197 if (GTK_IS_WIDGET (widget))
199 GtkStateFlags state = gtk_widget_get_state_flags (widget);
200 if (state & GTK_STATE_FLAG_SELECTED)
209 set_copy_action (struct psppire_output_view *view,
212 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (view->output));
213 GAction *copy_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
215 g_object_set (copy_action,
221 clear_selection (struct psppire_output_view *view)
225 struct output_view_item *item = find_selected_item (view);
228 set_copy_action (view, FALSE);
229 GtkWidget *widget = GTK_WIDGET (item->drawing_area);
230 if (GTK_IS_WIDGET (widget))
232 gtk_widget_unset_state_flags (widget, GTK_STATE_FLAG_SELECTED);
233 gtk_widget_queue_draw (widget);
238 off_item_button_press_event_cb (GtkWidget *widget,
239 GdkEventButton *event,
240 struct psppire_output_view *view)
242 /* buttontime is set by button_press_event_cb
243 If our event->time is equal to the time from the
244 button_press_event_cb, then we handle the same event.
245 In that case we must not clear the selection because
246 it was just set by button_press_event_cb from the item */
247 if (event->time != view->buttontime)
248 clear_selection (view);
249 return FALSE; /* Forward the event -> DragNDrop */
253 button_press_event_cb (GtkWidget *widget,
254 GdkEventButton *event,
255 struct psppire_output_view *view)
257 view->buttontime = event->time;
258 clear_selection (view);
259 set_copy_action (view, TRUE);
260 gtk_widget_set_state_flags (widget, GTK_STATE_FLAG_SELECTED, FALSE);
261 gtk_widget_queue_draw (widget);
262 return FALSE; /* Forward Event -> off_item will trigger */
266 drag_data_get_cb (GtkWidget *widget, GdkDragContext *context,
267 GtkSelectionData *selection_data,
268 guint target_type, guint time,
269 struct psppire_output_view *view)
271 view->selected_item = find_selected_item (view);
272 clipboard_get_cb (NULL, selection_data, target_type, view);
276 create_drawing_area (struct psppire_output_view *view,
277 GtkWidget *drawing_area, struct xr_fsm *r,
278 int tw, int th, const struct output_item *item)
280 g_object_set_data_full (G_OBJECT (drawing_area),
282 g_signal_connect (drawing_area, "button-press-event",
283 G_CALLBACK (button_press_event_cb), view);
284 gtk_widget_add_events (drawing_area, GDK_BUTTON_PRESS_MASK);
286 { /* Drag and Drop */
287 GtkTargetList *tl = build_target_list (item);
289 gtk_drag_source_set (drawing_area, GDK_BUTTON1_MASK, NULL, 0, GDK_ACTION_COPY);
290 gtk_drag_source_set_target_list (drawing_area, tl);
291 gtk_target_list_unref (tl);
292 g_signal_connect (drawing_area, "drag-data-get",
293 G_CALLBACK (drag_data_get_cb), view);
295 GtkStyleContext *context = gtk_widget_get_style_context (drawing_area);
296 gtk_style_context_add_class (context,
297 GTK_STYLE_CLASS_VIEW);
298 g_signal_connect (drawing_area, "draw",
299 G_CALLBACK (draw_callback), view);
301 gtk_widget_set_size_request (drawing_area, tw, th);
302 gint xpos = get_xpos (view, tw);
304 gtk_layout_put (view->output, drawing_area, xpos, view->y);
306 gtk_widget_show (drawing_area);
310 rerender (struct psppire_output_view *view)
312 struct output_view_item *item;
313 GdkWindow *gdkw = gtk_widget_get_window (GTK_WIDGET (view->output));
315 if (!view->n_items || ! gdkw)
319 view->style = get_xr_fsm_style (view);
321 GdkWindow *win = gtk_layout_get_bin_window (view->output);
322 cairo_region_t *region = gdk_window_get_visible_region (win);
323 GdkDrawingContext *ctx = gdk_window_begin_draw_frame (win, region);
324 cairo_t *cr = gdk_drawing_context_get_cairo_context (ctx);
328 for (item = view->items; item < &view->items[view->n_items]; item++)
335 view->y += view->object_spacing;
337 if (is_group_open_item (item->item))
340 r = xr_fsm_create (item->item, view->style, cr);
343 g_warn_if_reached ();
347 xr_fsm_measure (r, cr, &tw, &th);
349 gint xpos = get_xpos (view, tw);
351 if (!item->drawing_area)
353 item->drawing_area = gtk_drawing_area_new ();
354 create_drawing_area (view, item->drawing_area, r, tw, th, item->item);
358 g_object_set_data_full (G_OBJECT (item->drawing_area),
360 gtk_widget_set_size_request (item->drawing_area, tw, th);
361 gtk_layout_move (view->output, item->drawing_area, xpos, view->y);
364 if (is_table_item (item->item))
366 const struct table_item *ti = to_table_item (item->item);
367 gtk_widget_set_tooltip_text (item->drawing_area, ti->notes);
373 /* This code probably doesn't bring us anthing, but Gtk
374 shows warnings if get_preferred_width/height is not
375 called before the size_allocate below is called. */
376 gtk_widget_get_preferred_width (item->drawing_area, &minw, NULL);
377 gtk_widget_get_preferred_height (item->drawing_area, &minh, NULL);
378 if (th > minh) th = minh;
379 if (tw > minw) tw = minw;
386 gtk_widget_size_allocate (item->drawing_area, &alloc);
388 if (view->max_width < tw)
389 view->max_width = tw;
393 gtk_layout_set_size (view->output,
394 view->max_width + view->object_spacing,
395 view->y + view->object_spacing);
397 gdk_window_end_draw_frame (win, ctx);
398 cairo_region_destroy (region);
403 psppire_output_view_put (struct psppire_output_view *view,
404 const struct output_item *item)
406 struct output_view_item *view_item;
407 GtkWidget *drawing_area;
411 if (is_group_close_item (item))
415 if (!gtk_tree_path_up (view->cur_group))
417 gtk_tree_path_free (view->cur_group);
418 view->cur_group = NULL;
423 else if (is_text_item (item))
425 const struct text_item *text_item = to_text_item (item);
426 const char *text = text_item_get_text (text_item);
431 if (view->n_items >= view->allocated_items)
432 view->items = x2nrealloc (view->items, &view->allocated_items,
433 sizeof *view->items);
434 view_item = &view->items[view->n_items++];
435 view_item->item = output_item_ref (item);
436 view_item->drawing_area = NULL;
438 GdkWindow *win = gtk_widget_get_window (GTK_WIDGET (view->output));
439 if (is_group_open_item (item))
443 view_item->drawing_area = drawing_area = gtk_drawing_area_new ();
446 view->style = get_xr_fsm_style (view);
448 cairo_region_t *region = gdk_window_get_visible_region (win);
449 GdkDrawingContext *ctx = gdk_window_begin_draw_frame (win, region);
450 cairo_t *cr = gdk_drawing_context_get_cairo_context (ctx);
453 view->y += view->object_spacing;
455 struct xr_fsm *r = xr_fsm_create (item, view->style, cr);
458 gdk_window_end_draw_frame (win, ctx);
459 cairo_region_destroy (region);
463 xr_fsm_measure (r, cr, &tw, &th);
464 create_drawing_area (view, drawing_area, r, tw, th, item);
465 gdk_window_end_draw_frame (win, ctx);
466 cairo_region_destroy (region);
473 GtkTreeStore *store = GTK_TREE_STORE (
474 gtk_tree_view_get_model (view->overview));
476 ds_init_empty (&name);
478 /* Create a new node in the tree and puts a reference to it in 'iter'. */
482 && gtk_tree_path_get_depth (view->cur_group) > 0
483 && gtk_tree_model_get_iter (GTK_TREE_MODEL (store),
484 &parent, view->cur_group))
485 gtk_tree_store_append (store, &iter, &parent);
487 gtk_tree_store_append (store, &iter, NULL);
489 if (is_group_open_item (item))
491 gtk_tree_path_free (view->cur_group);
492 view->cur_group = gtk_tree_model_get_path (GTK_TREE_MODEL (store),
497 if (is_text_item (item))
499 const struct text_item *text_item = to_text_item (item);
500 ds_put_cstr (&name, text_item_type_to_string (
501 text_item_get_type (text_item)));
503 else if (is_message_item (item))
505 const struct message_item *msg_item = to_message_item (item);
506 const struct msg *msg = message_item_get_msg (msg_item);
507 ds_put_format (&name, "%s: %s", _("Message"),
508 msg_severity_to_string (msg->severity));
510 else if (is_table_item (item))
512 const struct table_item_text *title
513 = table_item_get_title (to_table_item (item));
515 ds_put_format (&name, "Table: %s", title->content);
517 ds_put_cstr (&name, "Table");
519 else if (is_chart_item (item))
521 const char *s = chart_item_get_title (to_chart_item (item));
523 ds_put_format (&name, "Chart: %s", s);
525 ds_put_cstr (&name, "Chart");
527 else if (is_group_open_item (item))
528 ds_put_cstr (&name, to_group_open_item (item)->command_name);
529 gtk_tree_store_set (store, &iter,
530 COL_NAME, ds_cstr (&name),
536 GtkTreePath *path = gtk_tree_model_get_path (
537 GTK_TREE_MODEL (store), &iter);
538 gtk_tree_view_expand_row (view->overview, path, TRUE);
539 gtk_tree_path_free (path);
542 if (view->max_width < tw)
543 view->max_width = tw;
546 gtk_layout_set_size (view->output, view->max_width, view->y);
550 on_row_activate (GtkTreeView *overview,
552 GtkTreeViewColumn *column,
553 struct psppire_output_view *view)
561 model = gtk_tree_view_get_model (overview);
562 if (!gtk_tree_model_get_iter (model, &iter, path))
565 gtk_tree_model_get_value (model, &iter, COL_Y, &value);
566 y = g_value_get_long (&value);
567 g_value_unset (&value);
569 vadj = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (view->output));
570 min = gtk_adjustment_get_lower (vadj);
571 max = gtk_adjustment_get_upper (vadj) - gtk_adjustment_get_page_size (vadj);
576 gtk_adjustment_set_value (vadj, y);
580 on_style_updated (GtkWidget *toplevel, struct psppire_output_view *view)
582 if (!view->n_items || !gtk_widget_get_window (GTK_WIDGET (view->output)))
585 /* GTK+ fires this signal for trivial changes like the mouse moving in or out
586 of the window. Check whether the actual fsm options changed and
587 re-render only if they did. */
588 struct xr_fsm_style *style = get_xr_fsm_style (view);
589 if (!view->style || !xr_fsm_style_equals (style, view->style))
591 xr_fsm_style_unref (view->style);
592 view->style = xr_fsm_style_ref (style);
595 xr_fsm_style_unref (style);
609 clear_rectangle (cairo_surface_t *surface,
610 double x0, double y0, double x1, double y1)
612 cairo_t *cr = cairo_create (surface);
613 cairo_set_source_rgb (cr, 1, 1, 1);
615 cairo_rectangle (cr, x0, y0, x1 - x0, y1 - y0);
621 clipboard_get_cb (GtkClipboard *clipboard,
622 GtkSelectionData *selection_data,
626 struct psppire_output_view *view = data;
630 struct output_driver *driver = NULL;
632 struct string_map options;
633 struct temp_dir *td = NULL;
635 if (view->selected_item == NULL)
638 td = create_temp_dir ("pspp", NULL, false);
641 msg_error (errno, _("failed to create temporary directory during clipboard operation"));
644 filename = xasprintf ("%s/clip.tmp", td->dir_name);
646 string_map_init (&options);
647 string_map_insert (&options, "output-file", filename);
651 case SELECT_FMT_UTF8:
652 string_map_insert (&options, "box", "unicode");
655 case SELECT_FMT_TEXT:
656 string_map_insert (&options, "format", "txt");
657 string_map_insert (&options, "width", "1000");
660 case SELECT_FMT_HTML:
661 string_map_insert (&options, "format", "html");
662 string_map_insert (&options, "borders", "false");
663 string_map_insert (&options, "css", "false");
672 string_map_insert (&options, "format", "odt");
676 g_warning ("unsupported clip target\n");
681 if ((info == SELECT_FMT_IMG) ||
682 (info == SELECT_FMT_SVG) )
684 GtkWidget *widget = view->selected_item->drawing_area;
685 struct xr_fsm *fsm = g_object_get_data (G_OBJECT (widget), "fsm");
687 GdkWindow *win = gtk_layout_get_bin_window (view->output);
688 cairo_region_t *region = gdk_window_get_visible_region (win);
689 GdkDrawingContext *ctx = gdk_window_begin_draw_frame (win, region);
690 cairo_t *cr = gdk_drawing_context_get_cairo_context (ctx);
693 xr_fsm_measure (fsm, cr, &w, &h);
695 gdk_window_end_draw_frame (win, ctx);
696 cairo_region_destroy (region);
698 cairo_surface_t *surface
699 = (info == SELECT_FMT_SVG
700 ? cairo_svg_surface_create (filename, w, h)
701 : cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h));
702 clear_rectangle (surface, 0, 0, w, h);
703 cairo_t *cr2 = cairo_create (surface);
704 xr_fsm_draw_all (fsm, cr2);
706 if (info == SELECT_FMT_IMG)
708 GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface (surface,
712 gtk_selection_data_set_pixbuf (selection_data, pixbuf);
713 g_object_unref (pixbuf);
716 cairo_surface_destroy (surface);
720 driver = output_driver_create (&options);
724 driver->class->submit (driver, view->selected_item->item);
726 if (driver->class->flush)
727 driver->class->flush (driver);
729 /* Some drivers (eg: the odt one) don't write anything until they
731 output_driver_destroy (driver);
735 if (info != SELECT_FMT_IMG
736 && g_file_get_contents (filename, &text, &length, NULL))
737 gtk_selection_data_set (selection_data,
738 gtk_selection_data_get_target (selection_data),
739 8, (const guchar *) text, length);
744 output_driver_destroy (driver);
750 cleanup_temp_dir (td);
754 clipboard_clear_cb (GtkClipboard *clipboard,
760 CT ( ctn1, "STRING", 0, SELECT_FMT_TEXT ) \
761 CT ( ctn2, "TEXT", 0, SELECT_FMT_TEXT ) \
762 CT ( ctn3, "COMPOUND_TEXT", 0, SELECT_FMT_TEXT ) \
763 CT ( ctn4, "text/plain", 0, SELECT_FMT_TEXT ) \
764 CT ( ctn5, "UTF8_STRING", 0, SELECT_FMT_UTF8 ) \
765 CT ( ctn6, "text/plain;charset=utf-8", 0, SELECT_FMT_UTF8 ) \
766 CT ( ctn7, "text/html", 0, SELECT_FMT_HTML ) \
767 CT ( ctn8, "image/svg+xml", 0, SELECT_FMT_SVG )
769 #define CT(ID, TARGET, FLAGS, INFO) static gchar ID[] = TARGET;
772 static gchar ctnlast[] = "application/vnd.oasis.opendocument.text";
774 static const GtkTargetEntry targets[] = {
775 #define CT(ID, TARGET, FLAGS, INFO) { ID, FLAGS, INFO },
778 { ctnlast, 0, SELECT_FMT_ODT }
781 static GtkTargetList *
782 build_target_list (const struct output_item *item)
784 GtkTargetList *tl = gtk_target_list_new (targets, G_N_ELEMENTS (targets));
785 g_return_val_if_fail (tl, NULL);
786 if (is_table_item (item) ||
787 is_chart_item (item))
788 gtk_target_list_add_image_targets (tl, SELECT_FMT_IMG, TRUE);
793 on_copy (struct psppire_output_view *view)
795 GtkWidget *widget = GTK_WIDGET (view->overview);
796 GtkClipboard *cb = gtk_widget_get_clipboard (widget, GDK_SELECTION_CLIPBOARD);
798 struct output_view_item *ov_item = find_selected_item (view);
801 view->selected_item = ov_item;
802 GtkTargetList *tl = build_target_list (ov_item->item);
803 g_return_if_fail (tl);
804 gint no_of_targets = 0;
805 GtkTargetEntry *ta = gtk_target_table_new_from_list (tl, &no_of_targets);
806 g_return_if_fail (ta);
807 if (!gtk_clipboard_set_with_data (cb, ta, no_of_targets,
808 clipboard_get_cb, clipboard_clear_cb,
810 clipboard_clear_cb (cb, view);
812 gtk_target_list_unref (tl);
813 gtk_target_table_free (ta,no_of_targets);
817 on_size_allocate (GtkWidget *widget,
818 GdkRectangle *allocation,
819 struct psppire_output_view *view)
821 view->render_width = MAX (300, allocation->width);
826 on_realize (GtkWidget *overview, GObject *view)
828 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (overview));
830 GAction *copy_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
833 GAction *select_all_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
836 g_object_set (copy_action, "enabled", FALSE, NULL);
837 g_object_set (select_all_action, "enabled", FALSE, NULL);
839 g_signal_connect_swapped (copy_action, "activate",
840 G_CALLBACK (on_copy), view);
844 struct psppire_output_view *
845 psppire_output_view_new (GtkLayout *output, GtkTreeView *overview)
847 struct psppire_output_view *view;
848 GtkTreeViewColumn *column;
849 GtkCellRenderer *renderer;
853 view = xmalloc (sizeof *view);
854 *view = (struct psppire_output_view) {
855 .object_spacing = 10,
857 .overview = overview,
858 .toplevel = gtk_widget_get_toplevel (GTK_WIDGET (output)),
861 g_signal_connect (output, "draw", G_CALLBACK (layout_draw_callback), NULL);
863 g_signal_connect (output, "style-updated", G_CALLBACK (on_style_updated), view);
865 g_signal_connect (output, "size-allocate", G_CALLBACK (on_size_allocate), view);
867 gtk_widget_add_events (GTK_WIDGET (output), GDK_BUTTON_PRESS_MASK);
868 g_signal_connect (output, "button-press-event",
869 G_CALLBACK (off_item_button_press_event_cb), view);
871 gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (output)),
872 GTK_STYLE_CLASS_VIEW);
876 g_signal_connect (overview, "realize", G_CALLBACK (on_realize), view);
878 model = GTK_TREE_MODEL (gtk_tree_store_new (
880 G_TYPE_STRING, /* COL_NAME */
881 G_TYPE_POINTER, /* COL_ADDR */
882 G_TYPE_LONG)); /* COL_Y */
883 gtk_tree_view_set_model (overview, model);
884 g_object_unref (model);
886 column = gtk_tree_view_column_new ();
887 gtk_tree_view_append_column (GTK_TREE_VIEW (overview), column);
888 renderer = gtk_cell_renderer_text_new ();
889 gtk_tree_view_column_pack_start (column, renderer, TRUE);
890 gtk_tree_view_column_add_attribute (column, renderer, "text", COL_NAME);
892 g_signal_connect (GTK_TREE_VIEW (overview),
893 "row-activated", G_CALLBACK (on_row_activate), view);
900 psppire_output_view_destroy (struct psppire_output_view *view)
907 g_signal_handlers_disconnect_by_func (view->output,
908 G_CALLBACK (on_style_updated), view);
910 xr_fsm_style_unref (view->style);
912 for (i = 0; i < view->n_items; i++)
913 output_item_unref (view->items[i].item);
916 view->n_items = view->allocated_items = 0;
918 if (view->print_settings != NULL)
919 g_object_unref (view->print_settings);
922 gtk_tree_path_free (view->cur_group);
928 psppire_output_view_clear (struct psppire_output_view *view)
935 for (i = 0; i < view->n_items; i++)
937 gtk_container_remove (GTK_CONTAINER (view->output),
938 view->items[i].drawing_area);
939 output_item_unref (view->items[i].item);
943 view->n_items = view->allocated_items = 0;
949 psppire_output_view_export (struct psppire_output_view *view,
950 struct string_map *options)
952 struct output_driver *driver;
954 driver = output_driver_create (options);
959 for (i = 0; i < view->n_items; i++)
960 driver->class->submit (driver, view->items[i].item);
961 output_driver_destroy (driver);
968 get_cairo_context_from_print_context (GtkPrintContext *context)
970 cairo_t *cr = gtk_print_context_get_cairo_context (context);
971 return cairo_reference (cr);
975 create_xr_print_driver (GtkPrintContext *context, struct psppire_output_view *view)
977 GtkPageSetup *ps = gtk_print_context_get_page_setup (context);
979 enum { H = TABLE_HORZ, V = TABLE_VERT };
980 int paper[TABLE_N_AXES] = {
981 [H] = gtk_page_setup_get_paper_width (ps, GTK_UNIT_POINTS) * XR_POINT,
982 [V] = gtk_page_setup_get_paper_height (ps, GTK_UNIT_POINTS) * XR_POINT,
985 /* These are all 1/2 inch. The "margins" that GTK+ gives us are useless:
986 they are the printer's imagable area. */
987 int margins[TABLE_N_AXES][2] = {
988 [H][0] = XR_POINT * 36,
989 [H][1] = XR_POINT * 36,
990 [V][0] = XR_POINT * 36,
991 [V][1] = XR_POINT * 36,
994 double size[TABLE_N_AXES];
995 for (int a = 0; a < TABLE_N_AXES; a++)
996 size[a] = paper[a] - margins[a][0] - margins[a][1];
998 PangoFontDescription *proportional_font
999 = pango_font_description_from_string ("Sans Serif 10");
1000 PangoFontDescription *fixed_font
1001 = pango_font_description_from_string ("Monospace 10");
1003 view->page_style = xmalloc (sizeof *view->page_style);
1004 *view->page_style = (struct xr_page_style) {
1008 [H] = { margins[H][0], margins[H][1] },
1009 [V] = { margins[V][0], margins[V][1] },
1011 .initial_page_number = 1,
1012 .object_spacing = 12 * XR_POINT,
1015 view->fsm_style = xmalloc (sizeof *view->fsm_style);
1016 *view->fsm_style = (struct xr_fsm_style) {
1019 .size = { [H] = size[H], [V] = size[V] },
1020 .min_break = { [H] = size[H] / 2, [V] = size[V] / 2 },
1022 [XR_FONT_PROPORTIONAL] = proportional_font,
1023 [XR_FONT_FIXED] = fixed_font,
1025 .fg = CELL_COLOR_BLACK,
1026 .use_system_colors = false,
1027 .font_resolution = 72.0
1030 view->pager = xr_pager_create (view->page_style, view->fsm_style);
1034 paginate (GtkPrintOperation *operation,
1035 GtkPrintContext *context,
1036 struct psppire_output_view *view)
1038 if (view->paginated)
1040 /* Sometimes GTK+ emits this signal again even after pagination is
1041 complete. Don't let that screw up printing. */
1044 else if (view->print_item < view->n_items)
1046 xr_pager_add_item (view->pager, view->items[view->print_item++].item);
1047 while (xr_pager_needs_new_page (view->pager))
1049 xr_pager_add_page (view->pager,
1050 get_cairo_context_from_print_context (context));
1051 view->print_n_pages ++;
1057 gtk_print_operation_set_n_pages (operation, MAX (1, view->print_n_pages));
1059 /* Re-create the driver to do the real printing. */
1060 xr_pager_destroy (view->pager);
1061 view->pager = xr_pager_create (view->page_style, view->fsm_style);
1062 view->print_item = 0;
1063 view->paginated = TRUE;
1070 begin_print (GtkPrintOperation *operation,
1071 GtkPrintContext *context,
1072 struct psppire_output_view *view)
1074 create_xr_print_driver (context, view);
1076 view->print_item = 0;
1077 view->print_n_pages = 0;
1078 view->paginated = FALSE;
1082 end_print (GtkPrintOperation *operation,
1083 GtkPrintContext *context,
1084 struct psppire_output_view *view)
1086 xr_pager_destroy (view->pager);
1092 draw_page (GtkPrintOperation *operation,
1093 GtkPrintContext *context,
1095 struct psppire_output_view *view)
1097 xr_pager_add_page (view->pager,
1098 get_cairo_context_from_print_context (context));
1099 while (!xr_pager_needs_new_page (view->pager)
1100 && view->print_item < view->n_items)
1101 xr_pager_add_item (view->pager, view->items [view->print_item++].item);
1106 psppire_output_view_print (struct psppire_output_view *view,
1107 GtkWindow *parent_window)
1109 GtkPrintOperationResult res;
1111 GtkPrintOperation *print = gtk_print_operation_new ();
1113 if (view->print_settings != NULL)
1114 gtk_print_operation_set_print_settings (print, view->print_settings);
1116 gtk_print_operation_set_use_full_page (print, TRUE);
1117 gtk_print_operation_set_unit (print, GTK_UNIT_POINTS);
1119 g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), view);
1120 g_signal_connect (print, "end_print", G_CALLBACK (end_print), view);
1121 g_signal_connect (print, "paginate", G_CALLBACK (paginate), view);
1122 g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), view);
1124 res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1125 parent_window, NULL);
1127 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
1129 if (view->print_settings != NULL)
1130 g_object_unref (view->print_settings);
1131 view->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
1134 g_object_unref (print);
1137 struct psppire_output_view_driver
1139 struct output_driver driver;
1140 struct psppire_output_view *view;
1143 static struct psppire_output_view_driver *
1144 psppire_output_view_driver_cast (struct output_driver *driver)
1146 return UP_CAST (driver, struct psppire_output_view_driver, driver);
1150 psppire_output_view_submit (struct output_driver *this,
1151 const struct output_item *item)
1153 struct psppire_output_view_driver *povd = psppire_output_view_driver_cast (this);
1155 if (is_table_item (item))
1156 psppire_output_view_put (povd->view, item);
1159 static struct output_driver_class psppire_output_view_driver_class =
1161 "PSPPIRE Output View", /* name */
1163 psppire_output_view_submit, /* submit */
1168 psppire_output_view_register_driver (struct psppire_output_view *view)
1170 struct psppire_output_view_driver *povd;
1171 struct output_driver *d;
1173 povd = xzalloc (sizeof *povd);
1176 output_driver_init (d, &psppire_output_view_driver_class, "PSPPIRE Output View",
1177 SETTINGS_DEVICE_UNFILTERED);
1178 output_driver_register (d);