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>
26 #include "librsvg/rsvg.h"
28 #include "libpspp/assertion.h"
29 #include "libpspp/string-map.h"
30 #include "output/cairo-fsm.h"
31 #include "output/cairo.h"
32 #include "output/driver-provider.h"
33 #include "output/driver.h"
34 #include "output/chart-item.h"
35 #include "output/group-item.h"
36 #include "output/message-item.h"
37 #include "output/output-item.h"
38 #include "output/output-item-provider.h"
39 #include "output/table-item.h"
40 #include "output/text-item.h"
42 #include "gl/c-xvasprintf.h"
43 #include "gl/minmax.h"
44 #include "gl/tmpdir.h"
45 #include "gl/xalloc.h"
48 #define _(msgid) gettext (msgid)
50 struct output_view_item
52 struct output_item *item;
53 GtkWidget *drawing_area;
56 struct psppire_output_view
58 struct xr_fsm_style *style;
66 GtkTreeView *overview;
67 GtkTreePath *cur_group;
71 guint buttontime; /* Time of the button event */
73 struct output_view_item *items;
74 size_t n_items, allocated_items;
75 struct output_view_item *selected_item;
77 /* Variables pertaining to printing */
78 GtkPrintSettings *print_settings;
79 struct xr_driver *print_xrd;
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,
174 /* Return the horizontal position to place a widget whose
175 width is CHILD_WIDTH */
177 get_xpos (const struct psppire_output_view *view, gint child_width)
179 GdkWindow *gdkw = gtk_widget_get_window (GTK_WIDGET (view->output));
180 guint w = gdk_window_get_width (gdkw);
182 g_object_get (view->output, "border-width", &gutter, NULL);
183 return (gtk_widget_get_direction (GTK_WIDGET (view->output)) == GTK_TEXT_DIR_RTL) ? w - child_width - gutter: gutter;
186 static struct output_view_item *
187 find_selected_item (struct psppire_output_view *view)
189 struct output_view_item *item = NULL;
192 if (view->items == NULL)
195 for (item = view->items; item < &view->items[view->n_items]; item++)
197 GtkWidget *widget = GTK_WIDGET (item->drawing_area);
198 if GTK_IS_WIDGET (widget)
200 GtkStateFlags state = gtk_widget_get_state_flags (widget);
201 if (state & GTK_STATE_FLAG_SELECTED)
210 set_copy_action (struct psppire_output_view *view,
213 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (view->output));
214 GAction *copy_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
216 g_object_set (copy_action,
222 clear_selection (struct psppire_output_view *view)
226 struct output_view_item *item = find_selected_item (view);
229 set_copy_action (view, FALSE);
230 GtkWidget *widget = GTK_WIDGET (item->drawing_area);
231 if (GTK_IS_WIDGET (widget))
233 gtk_widget_unset_state_flags (widget, GTK_STATE_FLAG_SELECTED);
234 gtk_widget_queue_draw (widget);
239 off_item_button_press_event_cb (GtkWidget *widget,
240 GdkEventButton *event,
241 struct psppire_output_view *view)
243 /* buttontime is set by button_press_event_cb
244 If our event->time is equal to the time from the
245 button_press_event_cb, then we handle the same event.
246 In that case we must not clear the selection because
247 it was just set by button_press_event_cb from the item */
248 if (event->time != view->buttontime)
249 clear_selection (view);
250 return FALSE; /* Forward the event -> DragNDrop */
254 button_press_event_cb (GtkWidget *widget,
255 GdkEventButton *event,
256 struct psppire_output_view *view)
258 view->buttontime = event->time;
259 clear_selection (view);
260 set_copy_action (view, TRUE);
261 gtk_widget_set_state_flags (widget, GTK_STATE_FLAG_SELECTED, FALSE);
262 gtk_widget_queue_draw (widget);
263 return FALSE; /* Forward Event -> off_item will trigger */
267 drag_data_get_cb (GtkWidget *widget, GdkDragContext *context,
268 GtkSelectionData *selection_data,
269 guint target_type, guint time,
270 struct psppire_output_view *view)
272 view->selected_item = find_selected_item (view);
273 clipboard_get_cb (NULL, selection_data, target_type, view);
277 create_drawing_area (struct psppire_output_view *view,
278 GtkWidget *drawing_area, struct xr_fsm *r,
279 int tw, int th, const struct output_item *item)
281 g_object_set_data_full (G_OBJECT (drawing_area),
283 g_signal_connect (drawing_area, "button-press-event",
284 G_CALLBACK (button_press_event_cb), view);
285 gtk_widget_add_events (drawing_area, GDK_BUTTON_PRESS_MASK);
287 { /* Drag and Drop */
288 GtkTargetList *tl = build_target_list (item);
290 gtk_drag_source_set (drawing_area, GDK_BUTTON1_MASK, NULL, 0, GDK_ACTION_COPY);
291 gtk_drag_source_set_target_list (drawing_area, tl);
292 gtk_target_list_unref (tl);
293 g_signal_connect (drawing_area, "drag-data-get",
294 G_CALLBACK (drag_data_get_cb), view);
296 GtkStyleContext *context = gtk_widget_get_style_context (drawing_area);
297 gtk_style_context_add_class (context,
298 GTK_STYLE_CLASS_VIEW);
299 g_signal_connect (drawing_area, "draw",
300 G_CALLBACK (draw_callback), view);
302 gtk_widget_set_size_request (drawing_area, tw, th);
303 gint xpos = get_xpos (view, tw);
305 gtk_layout_put (view->output, drawing_area, xpos, view->y);
307 gtk_widget_show (drawing_area);
311 rerender (struct psppire_output_view *view)
313 struct output_view_item *item;
314 GdkWindow *gdkw = gtk_widget_get_window (GTK_WIDGET (view->output));
316 if (!view->n_items || ! gdkw)
320 view->style = get_xr_fsm_style (view);
322 GdkWindow *win = gtk_layout_get_bin_window (view->output);
323 cairo_region_t *region = gdk_window_get_visible_region (win);
324 GdkDrawingContext *ctx = gdk_window_begin_draw_frame (win, region);
325 cairo_t *cr = gdk_drawing_context_get_cairo_context (ctx);
329 for (item = view->items; item < &view->items[view->n_items]; item++)
336 view->y += view->object_spacing;
338 if (is_group_open_item (item->item))
341 r = xr_fsm_create (item->item, view->style, cr);
344 g_warn_if_reached ();
348 xr_fsm_measure (r, cr, &tw, &th);
350 gint xpos = get_xpos (view, tw);
352 if (!item->drawing_area)
354 item->drawing_area = gtk_drawing_area_new ();
355 create_drawing_area (view, item->drawing_area, r, tw, th, item->item);
359 g_object_set_data_full (G_OBJECT (item->drawing_area),
361 gtk_widget_set_size_request (item->drawing_area, tw, th);
362 gtk_layout_move (view->output, item->drawing_area, xpos, view->y);
368 /* This code probably doesn't bring us anthing, but Gtk
369 shows warnings if get_preferred_width/height is not
370 called before the size_allocate below is called. */
371 gtk_widget_get_preferred_width (item->drawing_area, &minw, NULL);
372 gtk_widget_get_preferred_height (item->drawing_area, &minh, NULL);
373 if (th > minh) th = minh;
374 if (tw > minw) tw = minw;
381 gtk_widget_size_allocate (item->drawing_area, &alloc);
383 if (view->max_width < tw)
384 view->max_width = tw;
388 gtk_layout_set_size (view->output,
389 view->max_width + view->object_spacing,
390 view->y + view->object_spacing);
392 gdk_window_end_draw_frame (win, ctx);
393 cairo_region_destroy (region);
398 psppire_output_view_put (struct psppire_output_view *view,
399 const struct output_item *item)
401 struct output_view_item *view_item;
402 GtkWidget *drawing_area;
406 if (is_group_close_item (item))
410 if (!gtk_tree_path_up (view->cur_group))
412 gtk_tree_path_free (view->cur_group);
413 view->cur_group = NULL;
418 else if (is_text_item (item))
420 const struct text_item *text_item = to_text_item (item);
421 const char *text = text_item_get_text (text_item);
426 if (view->n_items >= view->allocated_items)
427 view->items = x2nrealloc (view->items, &view->allocated_items,
428 sizeof *view->items);
429 view_item = &view->items[view->n_items++];
430 view_item->item = output_item_ref (item);
431 view_item->drawing_area = NULL;
433 GdkWindow *win = gtk_widget_get_window (GTK_WIDGET (view->output));
434 if (is_group_open_item (item))
438 view_item->drawing_area = drawing_area = gtk_drawing_area_new ();
441 view->style = get_xr_fsm_style (view);
443 cairo_region_t *region = gdk_window_get_visible_region (win);
444 GdkDrawingContext *ctx = gdk_window_begin_draw_frame (win, region);
445 cairo_t *cr = gdk_drawing_context_get_cairo_context (ctx);
448 view->y += view->object_spacing;
450 struct xr_fsm *r = xr_fsm_create (item, view->style, cr);
453 gdk_window_end_draw_frame (win, ctx);
454 cairo_region_destroy (region);
458 xr_fsm_measure (r, cr, &tw, &th);
459 create_drawing_area (view, drawing_area, r, tw, th, item);
460 gdk_window_end_draw_frame (win, ctx);
461 cairo_region_destroy (region);
468 GtkTreeStore *store = GTK_TREE_STORE (
469 gtk_tree_view_get_model (view->overview));
471 ds_init_empty (&name);
473 /* Create a new node in the tree and puts a reference to it in 'iter'. */
477 && gtk_tree_path_get_depth (view->cur_group) > 0
478 && gtk_tree_model_get_iter (GTK_TREE_MODEL (store),
479 &parent, view->cur_group))
480 gtk_tree_store_append (store, &iter, &parent);
482 gtk_tree_store_append (store, &iter, NULL);
484 if (is_group_open_item (item))
486 gtk_tree_path_free (view->cur_group);
487 view->cur_group = gtk_tree_model_get_path (GTK_TREE_MODEL (store),
492 if (is_text_item (item))
494 const struct text_item *text_item = to_text_item (item);
495 ds_put_cstr (&name, text_item_type_to_string (
496 text_item_get_type (text_item)));
498 else if (is_message_item (item))
500 const struct message_item *msg_item = to_message_item (item);
501 const struct msg *msg = message_item_get_msg (msg_item);
502 ds_put_format (&name, "%s: %s", _("Message"),
503 msg_severity_to_string (msg->severity));
505 else if (is_table_item (item))
507 const struct table_item_text *title
508 = table_item_get_title (to_table_item (item));
510 ds_put_format (&name, "Table: %s", title->content);
512 ds_put_cstr (&name, "Table");
514 else if (is_chart_item (item))
516 const char *s = chart_item_get_title (to_chart_item (item));
518 ds_put_format (&name, "Chart: %s", s);
520 ds_put_cstr (&name, "Chart");
522 else if (is_group_open_item (item))
523 ds_put_cstr (&name, to_group_open_item (item)->command_name);
524 gtk_tree_store_set (store, &iter,
525 COL_NAME, ds_cstr (&name),
531 GtkTreePath *path = gtk_tree_model_get_path (
532 GTK_TREE_MODEL (store), &iter);
533 gtk_tree_view_expand_row (view->overview, path, TRUE);
534 gtk_tree_path_free (path);
537 if (view->max_width < tw)
538 view->max_width = tw;
541 gtk_layout_set_size (view->output, view->max_width, view->y);
545 on_row_activate (GtkTreeView *overview,
547 GtkTreeViewColumn *column,
548 struct psppire_output_view *view)
556 model = gtk_tree_view_get_model (overview);
557 if (!gtk_tree_model_get_iter (model, &iter, path))
560 gtk_tree_model_get_value (model, &iter, COL_Y, &value);
561 y = g_value_get_long (&value);
562 g_value_unset (&value);
564 vadj = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (view->output));
565 min = gtk_adjustment_get_lower (vadj);
566 max = gtk_adjustment_get_upper (vadj) - gtk_adjustment_get_page_size (vadj);
571 gtk_adjustment_set_value (vadj, y);
575 on_style_updated (GtkWidget *toplevel, struct psppire_output_view *view)
577 if (!view->n_items || !gtk_widget_get_window (GTK_WIDGET (view->output)))
580 /* GTK+ fires this signal for trivial changes like the mouse moving in or out
581 of the window. Check whether the actual fsm options changed and
582 re-render only if they did. */
583 struct xr_fsm_style *style = get_xr_fsm_style (view);
584 if (!view->style || !xr_fsm_style_equals (style, view->style))
586 xr_fsm_style_unref (view->style);
587 view->style = xr_fsm_style_ref (style);
590 xr_fsm_style_unref (style);
603 /* GNU Hurd doesn't have PATH_MAX. Use a fallback.
604 Temporary directory names are usually not that long. */
606 # define PATH_MAX 1024
609 /* Returns a pixbuf from a svg file */
610 /* You must unref the pixbuf after usage */
612 derive_pixbuf_from_svg (const char *filename)
615 GdkPixbuf *pixbuf = NULL;
617 RsvgHandle *handle = rsvg_handle_new_from_file (filename, &err);
620 rsvg_handle_set_dpi (handle, 300.0);
621 pixbuf = rsvg_handle_get_pixbuf (handle);
622 g_object_unref (handle);
625 pixbuf = gdk_pixbuf_new_from_file (filename, &err);
629 msg (ME, _("Could not open file %s during copy operation: %s"),
630 filename, err->message);
638 clipboard_get_cb (GtkClipboard *clipboard,
639 GtkSelectionData *selection_data,
643 struct psppire_output_view *view = data;
647 struct output_driver *driver = NULL;
648 char dirname[PATH_MAX], *filename;
649 struct string_map options;
651 if (view->selected_item == NULL)
654 if (path_search (dirname, sizeof dirname, NULL, NULL, true)
655 || mkdtemp (dirname) == NULL)
657 msg_error (errno, _("failed to create temporary directory during clipboard operation"));
660 filename = xasprintf ("%s/clip.tmp", dirname);
662 string_map_init (&options);
663 string_map_insert (&options, "output-file", filename);
667 case SELECT_FMT_UTF8:
668 string_map_insert (&options, "box", "unicode");
671 case SELECT_FMT_TEXT:
672 string_map_insert (&options, "format", "txt");
675 case SELECT_FMT_HTML:
676 string_map_insert (&options, "format", "html");
677 string_map_insert (&options, "borders", "false");
678 string_map_insert (&options, "css", "false");
687 string_map_insert (&options, "format", "odt");
691 g_warning ("unsupported clip target\n");
696 if ((info == SELECT_FMT_IMG) ||
697 (info == SELECT_FMT_SVG) )
699 GtkWidget *widget = view->selected_item->drawing_area;
700 struct xr_fsm *fsm = g_object_get_data (G_OBJECT (widget), "fsm");
702 GdkWindow *win = gtk_layout_get_bin_window (view->output);
703 cairo_region_t *region = gdk_window_get_visible_region (win);
704 GdkDrawingContext *ctx = gdk_window_begin_draw_frame (win, region);
705 cairo_t *cr = gdk_drawing_context_get_cairo_context (ctx);
708 xr_fsm_measure (fsm, cr, &w, &h);
710 gdk_window_end_draw_frame (win, ctx);
711 cairo_region_destroy (region);
713 cairo_surface_t *surface = cairo_svg_surface_create (filename, w, h);
716 cairo_t *cr = cairo_create (surface);
717 xr_fsm_draw_all (fsm, cr);
719 cairo_surface_destroy (surface);
722 g_error ("Could not create cairo svg surface with file %s", filename);
726 driver = output_driver_create (&options);
730 driver->class->submit (driver, view->selected_item->item);
732 if (driver->class->flush)
733 driver->class->flush (driver);
735 /* Some drivers (eg: the odt one) don't write anything until they
737 output_driver_destroy (driver);
741 if (info == SELECT_FMT_IMG)
743 GdkPixbuf *pixbuf = derive_pixbuf_from_svg (filename);
746 gtk_selection_data_set_pixbuf (selection_data, pixbuf);
747 g_object_unref (pixbuf);
750 else if (g_file_get_contents (filename, &text, &length, NULL))
751 gtk_selection_data_set (selection_data, gtk_selection_data_get_target (selection_data),
753 (const guchar *) text, length);
758 output_driver_destroy (driver);
768 clipboard_clear_cb (GtkClipboard *clipboard,
774 CT ( ctn1, "STRING", 0, SELECT_FMT_TEXT ) \
775 CT ( ctn2, "TEXT", 0, SELECT_FMT_TEXT ) \
776 CT ( ctn3, "COMPOUND_TEXT", 0, SELECT_FMT_TEXT ) \
777 CT ( ctn4, "text/plain", 0, SELECT_FMT_TEXT ) \
778 CT ( ctn5, "UTF8_STRING", 0, SELECT_FMT_UTF8 ) \
779 CT ( ctn6, "text/plain;charset=utf-8", 0, SELECT_FMT_UTF8 ) \
780 CT ( ctn7, "text/html", 0, SELECT_FMT_HTML ) \
781 CT ( ctn8, "image/svg+xml", 0, SELECT_FMT_SVG )
783 #define CT(ID, TARGET, FLAGS, INFO) static gchar ID[] = TARGET;
786 static gchar ctnlast[] = "application/vnd.oasis.opendocument.text";
788 static const GtkTargetEntry targets[] = {
789 #define CT(ID, TARGET, FLAGS, INFO) { ID, FLAGS, INFO },
792 { ctnlast, 0, SELECT_FMT_ODT }
795 static GtkTargetList *
796 build_target_list (const struct output_item *item)
798 GtkTargetList *tl = gtk_target_list_new (targets, G_N_ELEMENTS (targets));
799 g_return_val_if_fail (tl, NULL);
800 if (is_table_item (item) ||
801 is_chart_item (item))
802 gtk_target_list_add_image_targets (tl, SELECT_FMT_IMG, TRUE);
807 on_copy (struct psppire_output_view *view)
809 GtkWidget *widget = GTK_WIDGET (view->overview);
810 GtkClipboard *cb = gtk_widget_get_clipboard (widget, GDK_SELECTION_CLIPBOARD);
812 struct output_view_item *ov_item = find_selected_item (view);
815 view->selected_item = ov_item;
816 GtkTargetList *tl = build_target_list (ov_item->item);
817 g_return_if_fail (tl);
818 gint no_of_targets = 0;
819 GtkTargetEntry *ta = gtk_target_table_new_from_list (tl, &no_of_targets);
820 g_return_if_fail (ta);
821 if (!gtk_clipboard_set_with_data (cb, ta, no_of_targets,
822 clipboard_get_cb, clipboard_clear_cb,
824 clipboard_clear_cb (cb, view);
826 gtk_target_list_unref (tl);
827 gtk_target_table_free (ta,no_of_targets);
831 on_size_allocate (GtkWidget *widget,
832 GdkRectangle *allocation,
833 struct psppire_output_view *view)
835 view->render_width = MAX (300, allocation->width);
840 on_realize (GtkWidget *overview, GObject *view)
842 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (overview));
844 GAction *copy_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
847 GAction *select_all_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
850 g_object_set (copy_action, "enabled", FALSE, NULL);
851 g_object_set (select_all_action, "enabled", FALSE, NULL);
853 g_signal_connect_swapped (copy_action, "activate",
854 G_CALLBACK (on_copy), view);
858 struct psppire_output_view *
859 psppire_output_view_new (GtkLayout *output, GtkTreeView *overview)
861 struct psppire_output_view *view;
862 GtkTreeViewColumn *column;
863 GtkCellRenderer *renderer;
867 view = xmalloc (sizeof *view);
869 view->object_spacing = 10;
870 view->output = output;
871 view->render_width = 0;
874 view->overview = overview;
875 view->cur_group = NULL;
876 view->toplevel = gtk_widget_get_toplevel (GTK_WIDGET (output));
877 view->buttontime = 0;
879 view->n_items = view->allocated_items = 0;
880 view->selected_item = NULL;
881 view->print_settings = NULL;
882 view->print_xrd = NULL;
883 view->print_item = 0;
884 view->print_n_pages = 0;
885 view->paginated = FALSE;
887 g_signal_connect (output, "draw", G_CALLBACK (layout_draw_callback), NULL);
889 g_signal_connect (output, "style-updated", G_CALLBACK (on_style_updated), view);
891 g_signal_connect (output, "size-allocate", G_CALLBACK (on_size_allocate), view);
893 gtk_widget_add_events (GTK_WIDGET (output), GDK_BUTTON_PRESS_MASK);
894 g_signal_connect (output, "button-press-event",
895 G_CALLBACK (off_item_button_press_event_cb), view);
897 gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (output)),
898 GTK_STYLE_CLASS_VIEW);
902 g_signal_connect (overview, "realize", G_CALLBACK (on_realize), view);
904 model = GTK_TREE_MODEL (gtk_tree_store_new (
906 G_TYPE_STRING, /* COL_NAME */
907 G_TYPE_POINTER, /* COL_ADDR */
908 G_TYPE_LONG)); /* COL_Y */
909 gtk_tree_view_set_model (overview, model);
910 g_object_unref (model);
912 column = gtk_tree_view_column_new ();
913 gtk_tree_view_append_column (GTK_TREE_VIEW (overview), column);
914 renderer = gtk_cell_renderer_text_new ();
915 gtk_tree_view_column_pack_start (column, renderer, TRUE);
916 gtk_tree_view_column_add_attribute (column, renderer, "text", COL_NAME);
918 g_signal_connect (GTK_TREE_VIEW (overview),
919 "row-activated", G_CALLBACK (on_row_activate), view);
926 psppire_output_view_destroy (struct psppire_output_view *view)
933 g_signal_handlers_disconnect_by_func (view->output,
934 G_CALLBACK (on_style_updated), view);
936 xr_fsm_style_unref (view->style);
938 for (i = 0; i < view->n_items; i++)
939 output_item_unref (view->items[i].item);
942 view->n_items = view->allocated_items = 0;
944 if (view->print_settings != NULL)
945 g_object_unref (view->print_settings);
948 gtk_tree_path_free (view->cur_group);
954 psppire_output_view_clear (struct psppire_output_view *view)
961 for (i = 0; i < view->n_items; i++)
963 gtk_container_remove (GTK_CONTAINER (view->output),
964 view->items[i].drawing_area);
965 output_item_unref (view->items[i].item);
969 view->n_items = view->allocated_items = 0;
975 psppire_output_view_export (struct psppire_output_view *view,
976 struct string_map *options)
978 struct output_driver *driver;
980 driver = output_driver_create (options);
985 for (i = 0; i < view->n_items; i++)
986 driver->class->submit (driver, view->items[i].item);
987 output_driver_destroy (driver);
994 get_cairo_context_from_print_context (GtkPrintContext *context)
996 cairo_t *cr = gtk_print_context_get_cairo_context (context);
999 For all platforms except windows, gtk_print_context_get_dpi_[xy] returns 72.
1000 Windows returns 600.
1002 double xres = gtk_print_context_get_dpi_x (context);
1003 double yres = gtk_print_context_get_dpi_y (context);
1005 /* This means that the cairo context now has its dimensions in Points */
1006 cairo_scale (cr, xres / 72.0, yres / 72.0);
1013 create_xr_print_driver (GtkPrintContext *context, struct psppire_output_view *view)
1015 struct string_map options;
1016 GtkPageSetup *page_setup;
1017 double width, height;
1019 double right_margin;
1021 double bottom_margin;
1023 page_setup = gtk_print_context_get_page_setup (context);
1024 width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM);
1025 height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_MM);
1026 left_margin = gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM);
1027 right_margin = gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_MM);
1028 top_margin = gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM);
1029 bottom_margin = gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_MM);
1031 string_map_init (&options);
1032 string_map_insert_nocopy (&options, xstrdup ("paper-size"),
1033 c_xasprintf("%.2fx%.2fmm", width, height));
1034 string_map_insert_nocopy (&options, xstrdup ("left-margin"),
1035 c_xasprintf ("%.2fmm", left_margin));
1036 string_map_insert_nocopy (&options, xstrdup ("right-margin"),
1037 c_xasprintf ("%.2fmm", right_margin));
1038 string_map_insert_nocopy (&options, xstrdup ("top-margin"),
1039 c_xasprintf ("%.2fmm", top_margin));
1040 string_map_insert_nocopy (&options, xstrdup ("bottom-margin"),
1041 c_xasprintf ("%.2fmm", bottom_margin));
1043 view->print_xrd = xr_driver_create (get_cairo_context_from_print_context (context), &options);
1045 string_map_destroy (&options);
1049 paginate (GtkPrintOperation *operation,
1050 GtkPrintContext *context,
1051 struct psppire_output_view *view)
1053 if (view->paginated)
1055 /* Sometimes GTK+ emits this signal again even after pagination is
1056 complete. Don't let that screw up printing. */
1059 else if (view->print_item < view->n_items)
1061 xr_driver_output_item (view->print_xrd,
1062 view->items[view->print_item++].item);
1063 while (xr_driver_need_new_page (view->print_xrd))
1065 xr_driver_next_page (view->print_xrd, get_cairo_context_from_print_context (context));
1066 view->print_n_pages ++;
1072 gtk_print_operation_set_n_pages (operation, view->print_n_pages);
1074 /* Re-create the driver to do the real printing. */
1075 xr_driver_destroy (view->print_xrd);
1076 create_xr_print_driver (context, view);
1077 view->print_item = 0;
1078 view->paginated = TRUE;
1085 begin_print (GtkPrintOperation *operation,
1086 GtkPrintContext *context,
1087 struct psppire_output_view *view)
1089 create_xr_print_driver (context, view);
1091 view->print_item = 0;
1092 view->print_n_pages = 1;
1093 view->paginated = FALSE;
1097 end_print (GtkPrintOperation *operation,
1098 GtkPrintContext *context,
1099 struct psppire_output_view *view)
1101 xr_driver_destroy (view->print_xrd);
1106 draw_page (GtkPrintOperation *operation,
1107 GtkPrintContext *context,
1109 struct psppire_output_view *view)
1111 xr_driver_next_page (view->print_xrd, get_cairo_context_from_print_context (context));
1112 while (!xr_driver_need_new_page (view->print_xrd)
1113 && view->print_item < view->n_items)
1114 xr_driver_output_item (view->print_xrd, view->items [view->print_item++].item);
1119 psppire_output_view_print (struct psppire_output_view *view,
1120 GtkWindow *parent_window)
1122 GtkPrintOperationResult res;
1124 GtkPrintOperation *print = gtk_print_operation_new ();
1126 if (view->print_settings != NULL)
1127 gtk_print_operation_set_print_settings (print, view->print_settings);
1129 g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), view);
1130 g_signal_connect (print, "end_print", G_CALLBACK (end_print), view);
1131 g_signal_connect (print, "paginate", G_CALLBACK (paginate), view);
1132 g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), view);
1134 res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1135 parent_window, NULL);
1137 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
1139 if (view->print_settings != NULL)
1140 g_object_unref (view->print_settings);
1141 view->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
1144 g_object_unref (print);
1147 struct psppire_output_view_driver
1149 struct output_driver driver;
1150 struct psppire_output_view *view;
1153 static struct psppire_output_view_driver *
1154 psppire_output_view_driver_cast (struct output_driver *driver)
1156 return UP_CAST (driver, struct psppire_output_view_driver, driver);
1160 psppire_output_view_submit (struct output_driver *this,
1161 const struct output_item *item)
1163 struct psppire_output_view_driver *povd = psppire_output_view_driver_cast (this);
1165 if (is_table_item (item))
1166 psppire_output_view_put (povd->view, item);
1169 static struct output_driver_class psppire_output_view_driver_class =
1171 "PSPPIRE Output View", /* name */
1173 psppire_output_view_submit, /* submit */
1178 psppire_output_view_register_driver (struct psppire_output_view *view)
1180 struct psppire_output_view_driver *povd;
1181 struct output_driver *d;
1183 povd = xzalloc (sizeof *povd);
1186 output_driver_init (d, &psppire_output_view_driver_class, "PSPPIRE Output View",
1187 SETTINGS_DEVICE_UNFILTERED);
1188 output_driver_register (d);