3a7ce4d357bb70b8e2a9e5a6f5e812fa33826b44
[pspp] / src / ui / gui / psppire-output-view.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008-2015, 2016 Free Software Foundation.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "ui/gui/psppire-output-view.h"
20
21 #include <errno.h>
22 #include <stdbool.h>
23
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/table-item.h"
34 #include "output/text-item.h"
35
36 #include "gl/c-xvasprintf.h"
37 #include "gl/minmax.h"
38 #include "gl/tmpdir.h"
39 #include "gl/xalloc.h"
40
41 #include <gettext.h>
42 #define _(msgid) gettext (msgid)
43
44 struct output_view_item
45   {
46     struct output_item *item;
47     GtkWidget *drawing_area;
48   };
49
50 struct psppire_output_view
51   {
52     struct xr_driver *xr;
53     int font_height;
54
55     GtkLayout *output;
56     int render_width;
57     int max_width;
58     glong y;
59
60     struct string_map render_opts;
61     GtkTreeView *overview;
62     GtkTreeIter cur_command;
63     bool in_command;
64
65     GtkWidget *toplevel;
66
67     struct output_view_item *items;
68     size_t n_items, allocated_items;
69
70     /* Variables pertaining to printing */
71     GtkPrintSettings *print_settings;
72     struct xr_driver *print_xrd;
73     int print_item;
74     int print_n_pages;
75     gboolean paginated;
76   };
77
78 enum
79   {
80     COL_NAME,                   /* Table name. */
81     COL_ADDR,                   /* Pointer to the table */
82     COL_Y,                      /* Y position of top of name. */
83     N_COLS
84   };
85
86 /* Draws a white background on the GtkLayout to match the white background of
87    each of the output items. */
88 static gboolean
89 layout_draw_callback (GtkWidget *widget, cairo_t *cr, gpointer data)
90 {
91   cairo_save (cr);
92
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);
97   cairo_fill (cr);
98
99   cairo_restore (cr);
100
101   return FALSE;                 /* Continue drawing the GtkDrawingAreas. */
102 }
103
104 static gboolean
105 draw_callback (GtkWidget *widget, cairo_t *cr, gpointer data)
106 {
107   GdkRectangle clip;
108   if (!gdk_cairo_get_clip_rectangle (cr, &clip))
109     return TRUE;
110
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);
114   return TRUE;
115 }
116
117 static void
118 free_rendering (gpointer rendering_)
119 {
120   struct xr_rendering *rendering = rendering_;
121   xr_rendering_destroy (rendering);
122 }
123
124 static void
125 get_xr_options (struct psppire_output_view *view, struct string_map *options)
126 {
127   string_map_clear (options);
128
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));
132
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);
138   g_free (font_name);
139
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);
144   g_free (font_name);
145   pango_font_description_free (font_desc);
146
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
151      point.) */
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");
158 }
159
160 static void
161 create_xr (struct psppire_output_view *view)
162 {
163   get_xr_options (view, &view->render_opts);
164
165   struct string_map options;
166   string_map_clone (&options, &view->render_opts);
167   cairo_t *cr = gdk_cairo_create (
168     gtk_widget_get_window (GTK_WIDGET (view->output)));
169   view->xr = xr_driver_create (cr, &options);
170   string_map_destroy (&options);
171
172   struct text_item *text_item = text_item_create (TEXT_ITEM_LOG, "X");
173   struct xr_rendering *r = xr_rendering_create (
174     view->xr, text_item_super (text_item), cr);
175   xr_rendering_measure (r, NULL, &view->font_height);
176   text_item_unref (text_item);
177
178   cairo_destroy (cr);
179 }
180
181 /* Return the horizontal position to place a widget whose
182    width is CHILD_WIDTH */
183 static gint
184 get_xpos (const struct psppire_output_view *view, gint child_width)
185 {
186   GdkWindow *gdkw = gtk_widget_get_window (GTK_WIDGET (view->output));
187   guint w = gdk_window_get_width (gdkw);
188   int gutter = 0;
189   g_object_get (view->output, "border-width", &gutter, NULL);
190   return (gtk_widget_get_direction (GTK_WIDGET (view->output)) ==  GTK_TEXT_DIR_RTL) ? w - child_width - gutter: gutter;
191 }
192
193 static void
194 create_drawing_area (struct psppire_output_view *view,
195                      GtkWidget *drawing_area, struct xr_rendering *r,
196                      int tw, int th)
197 {
198   /* Enable this to help with debugging.  It shows you which widgets are being
199      put where. */
200   if (0)
201     {
202       GdkRGBA green = {0, 1, 0, 1};
203       gtk_widget_override_background_color (GTK_WIDGET (view->output),
204                                             GTK_STATE_FLAG_NORMAL, &green);
205       GdkRGBA red = {1, 0, 0, 1};
206       gtk_widget_override_background_color (drawing_area,
207                                             GTK_STATE_FLAG_NORMAL, &red);
208     }
209
210   g_object_set_data_full (G_OBJECT (drawing_area),
211                           "rendering", r, free_rendering);
212
213   g_signal_connect (drawing_area, "draw",
214                     G_CALLBACK (draw_callback), view);
215
216   gtk_widget_set_size_request (drawing_area, tw, th);
217   gint xpos = get_xpos (view, tw);
218
219   gtk_layout_put (view->output, drawing_area, xpos, view->y);
220
221   gtk_widget_show (drawing_area);
222 }
223
224 static void
225 rerender (struct psppire_output_view *view)
226 {
227   struct output_view_item *item;
228   GdkWindow *gdkw = gtk_widget_get_window (GTK_WIDGET (view->output));
229   cairo_t *cr;
230
231   if (!view->n_items || ! gdkw)
232     return;
233
234   cr = gdk_cairo_create (gdkw);
235   if (view->xr == NULL)
236     create_xr (view);
237   view->y = 0;
238   view->max_width = 0;
239   for (item = view->items; item < &view->items[view->n_items]; item++)
240     {
241       struct xr_rendering *r;
242       GtkAllocation alloc;
243       int tw, th;
244
245       if (view->y > 0)
246         view->y += view->font_height / 2;
247
248       r = xr_rendering_create (view->xr, item->item, cr);
249       if (r == NULL)
250         {
251           g_warn_if_reached ();
252           continue;
253         }
254
255       xr_rendering_measure (r, &tw, &th);
256
257       gint xpos = get_xpos (view, tw);
258
259       if (!item->drawing_area)
260         {
261           item->drawing_area = gtk_drawing_area_new ();
262           create_drawing_area (view, item->drawing_area, r, tw, th);
263         }
264       else
265         {
266           g_object_set_data_full (G_OBJECT (item->drawing_area),
267                                   "rendering", r, free_rendering);
268           gtk_widget_set_size_request (item->drawing_area, tw, th);
269           gtk_layout_move (view->output, item->drawing_area, xpos, view->y);
270         }
271
272       {
273         gint minw;
274         gint minh;
275         /* This code probably doesn't bring us anthing, but Gtk
276            shows warnings if get_preferred_width/height is not
277            called before the size_allocate below is called. */
278         gtk_widget_get_preferred_width (item->drawing_area, &minw, NULL);
279         gtk_widget_get_preferred_height (item->drawing_area, &minh, NULL);
280         if (th > minh) th = minh;
281         if (tw > minw) tw = minw;
282       }
283       alloc.x = xpos;
284       alloc.y = view->y;
285       alloc.width = tw;
286       alloc.height = th;
287
288       gtk_widget_size_allocate (item->drawing_area, &alloc);
289
290       if (view->max_width < tw)
291         view->max_width = tw;
292       view->y += th;
293     }
294
295   gtk_layout_set_size (view->output,
296                        view->max_width + view->font_height,
297                        view->y + view->font_height);
298   cairo_destroy (cr);
299 }
300
301 void
302 psppire_output_view_put (struct psppire_output_view *view,
303                          const struct output_item *item)
304 {
305   struct output_view_item *view_item;
306   GtkWidget *drawing_area;
307   struct xr_rendering *r;
308   struct string name;
309   GtkTreeStore *store;
310   cairo_t *cr = NULL;
311   GtkTreePath *path;
312   GtkTreeIter iter;
313   int tw, th;
314
315   if (is_group_close_item (item))
316     {
317       if (output_get_group_level () == 0)
318         {
319           view->in_command = false;
320           return;
321         }
322     }
323   else if (is_text_item (item))
324     {
325       const struct text_item *text_item = to_text_item (item);
326       const char *text = text_item_get_text (text_item);
327       if (text[0] == '\0')
328         return;
329     }
330
331   if (view->n_items >= view->allocated_items)
332     view->items = x2nrealloc (view->items, &view->allocated_items,
333                                 sizeof *view->items);
334   view_item = &view->items[view->n_items++];
335   view_item->item = output_item_ref (item);
336   view_item->drawing_area = NULL;
337
338   if (gtk_widget_get_window (GTK_WIDGET (view->output)))
339     {
340       view_item->drawing_area = drawing_area = gtk_drawing_area_new ();
341
342       cr = gdk_cairo_create (gtk_widget_get_window (GTK_WIDGET (view->output)));
343       if (view->xr == NULL)
344         create_xr (view);
345
346       if (view->y > 0)
347         view->y += view->font_height / 2;
348
349       r = xr_rendering_create (view->xr, item, cr);
350       if (r == NULL)
351         goto done;
352
353       xr_rendering_measure (r, &tw, &th);
354
355       create_drawing_area (view, drawing_area, r, tw, th);
356     }
357   else
358     tw = th = 0;
359
360   if (view->overview
361       && (!is_text_item (item)
362           || text_item_get_type (to_text_item (item)) != TEXT_ITEM_SYNTAX
363           || !view->in_command))
364     {
365       store = GTK_TREE_STORE (gtk_tree_view_get_model (view->overview));
366
367       ds_init_empty (&name);
368       if (is_group_open_item (item) && output_get_group_level () == 1)
369         {
370           gtk_tree_store_append (store, &iter, NULL);
371           view->cur_command = iter; /* XXX shouldn't save a GtkTreeIter */
372           view->in_command = true;
373         }
374       else
375         {
376           GtkTreeIter *p = view->in_command ? &view->cur_command : NULL;
377           gtk_tree_store_append (store, &iter, p);
378         }
379
380       ds_clear (&name);
381       if (is_text_item (item))
382         ds_put_cstr (&name, text_item_get_text (to_text_item (item)));
383       else if (is_message_item (item))
384         {
385           const struct message_item *msg_item = to_message_item (item);
386           const struct msg *msg = message_item_get_msg (msg_item);
387           ds_put_format (&name, "%s: %s", _("Message"),
388                          msg_severity_to_string (msg->severity));
389         }
390       else if (is_table_item (item))
391         {
392           const struct table_item_text *title
393             = table_item_get_title (to_table_item (item));
394           if (title != NULL)
395             ds_put_format (&name, "Table: %s", title->content);
396           else
397             ds_put_cstr (&name, "Table");
398         }
399       else if (is_chart_item (item))
400         {
401           const char *s = chart_item_get_title (to_chart_item (item));
402           if (s != NULL)
403             ds_put_format (&name, "Chart: %s", s);
404           else
405             ds_put_cstr (&name, "Chart");
406         }
407       else if (is_group_open_item (item))
408         ds_put_cstr (&name, to_group_open_item (item)->command_name);
409       gtk_tree_store_set (store, &iter,
410                           COL_NAME, ds_cstr (&name),
411                           COL_ADDR, item,
412                           COL_Y, (view->y),
413                           -1);
414       ds_destroy (&name);
415
416       path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
417       gtk_tree_view_expand_row (view->overview, path, TRUE);
418       gtk_tree_path_free (path);
419     }
420
421   if (view->max_width < tw)
422     view->max_width = tw;
423   view->y += th;
424
425   gtk_layout_set_size (view->output, view->max_width, view->y);
426
427 done:
428   cairo_destroy (cr);
429 }
430
431 static void
432 on_row_activate (GtkTreeView *overview,
433                  GtkTreePath *path,
434                  GtkTreeViewColumn *column,
435                  struct psppire_output_view *view)
436 {
437   GtkTreeModel *model;
438   GtkTreeIter iter;
439   GtkAdjustment *vadj;
440   GValue value = {0};
441   double y, min, max;
442
443   model = gtk_tree_view_get_model (overview);
444   if (!gtk_tree_model_get_iter (model, &iter, path))
445     return;
446
447   gtk_tree_model_get_value (model, &iter, COL_Y, &value);
448   y = g_value_get_long (&value);
449   g_value_unset (&value);
450
451   vadj = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (view->output));
452   min = gtk_adjustment_get_lower (vadj);
453   max = gtk_adjustment_get_upper (vadj) - gtk_adjustment_get_page_size (vadj);
454   if (y < min)
455     y = min;
456   else if (y > max)
457     y = max;
458   gtk_adjustment_set_value (vadj, y);
459 }
460
461 static void
462 on_style_updated (GtkWidget *toplevel, struct psppire_output_view *view)
463 {
464   if (!view->n_items || !gtk_widget_get_window (GTK_WIDGET (view->output)))
465     return;
466
467   /* GTK+ fires this signal for trivial changes like the mouse moving in or out
468      of the window.  Check whether the actual rendering options changed and
469      re-render only if they did. */
470   struct string_map options = STRING_MAP_INITIALIZER (options);
471   get_xr_options (view, &options);
472   if (!string_map_equals (&options, &view->render_opts))
473     {
474       xr_driver_destroy (view->xr);
475       view->xr = NULL;
476
477       rerender (view);
478     }
479   string_map_destroy (&options);
480 }
481
482 enum {
483   SELECT_FMT_NULL,
484   SELECT_FMT_TEXT,
485   SELECT_FMT_UTF8,
486   SELECT_FMT_HTML,
487   SELECT_FMT_ODT
488 };
489
490 /* GNU Hurd doesn't have PATH_MAX.  Use a fallback.
491    Temporary directory names are usually not that long.  */
492 #ifndef PATH_MAX
493 # define PATH_MAX 1024
494 #endif
495
496 static void
497 clipboard_get_cb (GtkClipboard     *clipboard,
498                   GtkSelectionData *selection_data,
499                   guint             info,
500                   gpointer          data)
501 {
502   struct psppire_output_view *view = data;
503
504   gsize length;
505   gchar *text = NULL;
506   struct output_driver *driver = NULL;
507   char dirname[PATH_MAX], *filename;
508   struct string_map options;
509
510   GtkTreeSelection *sel = gtk_tree_view_get_selection (view->overview);
511   GtkTreeModel *model = gtk_tree_view_get_model (view->overview);
512
513   GList *rows = gtk_tree_selection_get_selected_rows (sel, &model);
514   GList *n = rows;
515
516   if ( n == NULL)
517     return;
518
519   if (path_search (dirname, sizeof dirname, NULL, NULL, true)
520       || mkdtemp (dirname) == NULL)
521     {
522       msg_error (errno, _("failed to create temporary directory during clipboard operation"));
523       return;
524     }
525   filename = xasprintf ("%s/clip.tmp", dirname);
526
527   string_map_init (&options);
528   string_map_insert (&options, "output-file", filename);
529
530   switch (info)
531     {
532     case SELECT_FMT_UTF8:
533       string_map_insert (&options, "box", "unicode");
534       /* fall-through */
535
536     case SELECT_FMT_TEXT:
537       string_map_insert (&options, "format", "txt");
538       break;
539
540     case SELECT_FMT_HTML:
541       string_map_insert (&options, "format", "html");
542       string_map_insert (&options, "borders", "false");
543       string_map_insert (&options, "css", "false");
544       break;
545
546     case SELECT_FMT_ODT:
547       string_map_insert (&options, "format", "odt");
548       break;
549
550     default:
551       g_warning ("unsupported clip target\n");
552       goto finish;
553       break;
554     }
555
556   driver = output_driver_create (&options);
557   if (driver == NULL)
558     goto finish;
559
560   while (n)
561     {
562       GtkTreePath *path = n->data ;
563       GtkTreeIter iter;
564       struct output_item *item ;
565
566       gtk_tree_model_get_iter (model, &iter, path);
567       gtk_tree_model_get (model, &iter, COL_ADDR, &item, -1);
568
569       driver->class->submit (driver, item);
570
571       n = n->next;
572     }
573
574   if ( driver->class->flush)
575     driver->class->flush (driver);
576
577
578   /* Some drivers (eg: the odt one) don't write anything until they
579      are closed */
580   output_driver_destroy (driver);
581   driver = NULL;
582
583   if ( g_file_get_contents (filename, &text, &length, NULL) )
584     {
585       gtk_selection_data_set (selection_data, gtk_selection_data_get_target (selection_data),
586                               8,
587                               (const guchar *) text, length);
588     }
589
590  finish:
591
592   if (driver != NULL)
593     output_driver_destroy (driver);
594
595   g_free (text);
596
597   unlink (filename);
598   free (filename);
599   rmdir (dirname);
600
601   g_list_free (rows);
602 }
603
604 static void
605 clipboard_clear_cb (GtkClipboard *clipboard,
606                     gpointer data)
607 {
608 }
609
610 static const GtkTargetEntry targets[] = {
611
612   { "STRING",        0, SELECT_FMT_TEXT },
613   { "TEXT",          0, SELECT_FMT_TEXT },
614   { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
615   { "text/plain",    0, SELECT_FMT_TEXT },
616
617   { "UTF8_STRING",   0, SELECT_FMT_UTF8 },
618   { "text/plain;charset=utf-8", 0, SELECT_FMT_UTF8 },
619
620   { "text/html",     0, SELECT_FMT_HTML },
621
622   { "application/vnd.oasis.opendocument.text", 0, SELECT_FMT_ODT }
623 };
624
625 static void
626 on_copy (struct psppire_output_view *view)
627 {
628   GtkWidget *widget = GTK_WIDGET (view->overview);
629   GtkClipboard *cb = gtk_widget_get_clipboard (widget, GDK_SELECTION_CLIPBOARD);
630
631   if (!gtk_clipboard_set_with_data (cb, targets, G_N_ELEMENTS (targets),
632                                     clipboard_get_cb, clipboard_clear_cb,
633                                     view))
634     clipboard_clear_cb (cb, view);
635 }
636
637 static void
638 on_selection_change (GtkTreeSelection *sel, GAction *copy_action)
639 {
640   /* The Copy action is available only if there is something selected */
641   g_object_set (copy_action,
642                 "enabled", gtk_tree_selection_count_selected_rows (sel) > 0,
643                 NULL);
644 }
645
646 static void
647 on_select_all (struct psppire_output_view *view)
648 {
649   GtkTreeSelection *sel = gtk_tree_view_get_selection (view->overview);
650   gtk_tree_view_expand_all (view->overview);
651   gtk_tree_selection_select_all (sel);
652 }
653
654 static void
655 on_size_allocate (GtkWidget    *widget,
656                   GdkRectangle *allocation,
657                   struct psppire_output_view *view)
658 {
659   view->render_width = MAX (300, allocation->width);
660   rerender (view);
661 }
662
663 static void
664 on_realize (GtkWidget *overview, GObject *view)
665 {
666   GtkTreeSelection *sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (overview));
667   gtk_tree_selection_set_mode (sel, GTK_SELECTION_MULTIPLE);
668
669   GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (overview));
670
671   GAction *copy_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
672                                                      "copy");
673
674   GAction *select_all_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
675                                                            "select-all");
676
677   g_object_set (copy_action, "enabled", FALSE, NULL);
678
679   g_signal_connect_swapped (select_all_action, "activate",
680                             G_CALLBACK (on_select_all), view);
681
682   g_signal_connect_swapped (copy_action, "activate",
683                             G_CALLBACK (on_copy), view);
684
685   g_signal_connect (sel, "changed", G_CALLBACK (on_selection_change),
686                     copy_action);
687 }
688
689 struct psppire_output_view *
690 psppire_output_view_new (GtkLayout *output, GtkTreeView *overview)
691 {
692   struct psppire_output_view *view;
693   GtkTreeViewColumn *column;
694   GtkCellRenderer *renderer;
695
696   GtkTreeModel *model;
697
698   view = xmalloc (sizeof *view);
699   view->xr = NULL;
700   view->font_height = 0;
701   view->output = output;
702   view->render_width = 0;
703   view->max_width = 0;
704   view->y = 0;
705   string_map_init (&view->render_opts);
706   view->overview = overview;
707   memset (&view->cur_command, 0, sizeof view->cur_command);
708   view->in_command = false;
709   view->toplevel = gtk_widget_get_toplevel (GTK_WIDGET (output));
710   view->items = NULL;
711   view->n_items = view->allocated_items = 0;
712   view->print_settings = NULL;
713   view->print_xrd = NULL;
714   view->print_item = 0;
715   view->print_n_pages = 0;
716   view->paginated = FALSE;
717
718   g_signal_connect (output, "draw", G_CALLBACK (layout_draw_callback), NULL);
719
720   g_signal_connect (output, "style-updated", G_CALLBACK (on_style_updated), view);
721
722   g_signal_connect (output, "size-allocate", G_CALLBACK (on_size_allocate), view);
723
724   gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (output)),
725                                GTK_STYLE_CLASS_VIEW);
726
727   if (overview)
728     {
729       g_signal_connect (overview, "realize", G_CALLBACK (on_realize), view);
730
731       model = GTK_TREE_MODEL (gtk_tree_store_new (
732                                 N_COLS,
733                                 G_TYPE_STRING,  /* COL_NAME */
734                                 G_TYPE_POINTER, /* COL_ADDR */
735                                 G_TYPE_LONG));  /* COL_Y */
736       gtk_tree_view_set_model (overview, model);
737       g_object_unref (model);
738
739       column = gtk_tree_view_column_new ();
740       gtk_tree_view_append_column (GTK_TREE_VIEW (overview), column);
741       renderer = gtk_cell_renderer_text_new ();
742       gtk_tree_view_column_pack_start (column, renderer, TRUE);
743       gtk_tree_view_column_add_attribute (column, renderer, "text", COL_NAME);
744
745       g_signal_connect (GTK_TREE_VIEW (overview),
746                         "row-activated", G_CALLBACK (on_row_activate), view);
747     }
748
749   return view;
750 }
751
752 void
753 psppire_output_view_destroy (struct psppire_output_view *view)
754 {
755   size_t i;
756
757   if (!view)
758     return;
759
760   g_signal_handlers_disconnect_by_func (view->output,
761                                         G_CALLBACK (on_style_updated), view);
762
763   string_map_destroy (&view->render_opts);
764
765   for (i = 0; i < view->n_items; i++)
766     output_item_unref (view->items[i].item);
767   free (view->items);
768   view->items = NULL;
769   view->n_items = view->allocated_items = 0;
770
771   if (view->print_settings != NULL)
772     g_object_unref (view->print_settings);
773
774   xr_driver_destroy (view->xr);
775
776   free (view);
777 }
778
779 void
780 psppire_output_view_clear (struct psppire_output_view *view)
781 {
782   size_t i;
783
784   view->max_width = 0;
785   view->y = 0;
786
787   for (i = 0; i < view->n_items; i++)
788     {
789       gtk_container_remove (GTK_CONTAINER (view->output),
790                             view->items[i].drawing_area);
791       output_item_unref (view->items[i].item);
792     }
793   free (view->items);
794   view->items = NULL;
795   view->n_items = view->allocated_items = 0;
796 }
797
798 /* Export. */
799
800 void
801 psppire_output_view_export (struct psppire_output_view *view,
802                             struct string_map *options)
803 {
804   struct output_driver *driver;
805
806   driver = output_driver_create (options);
807   if (driver)
808     {
809       size_t i;
810
811       for (i = 0; i < view->n_items; i++)
812         driver->class->submit (driver, view->items[i].item);
813       output_driver_destroy (driver);
814     }
815 }
816 \f
817 /* Print. */
818
819 static cairo_t *
820 get_cairo_context_from_print_context (GtkPrintContext *context)
821 {
822   cairo_t *cr = gtk_print_context_get_cairo_context (context);
823
824   /*
825     For all platforms except windows, gtk_print_context_get_dpi_[xy] returns 72.
826     Windows returns 600.
827   */
828   double xres = gtk_print_context_get_dpi_x (context);
829   double yres = gtk_print_context_get_dpi_y (context);
830
831   /* This means that the cairo context now has its dimensions in Points */
832   cairo_scale (cr, xres / 72.0, yres / 72.0);
833
834   return cr;
835 }
836
837
838 static void
839 create_xr_print_driver (GtkPrintContext *context, struct psppire_output_view *view)
840 {
841   struct string_map options;
842   GtkPageSetup *page_setup;
843   double width, height;
844   double left_margin;
845   double right_margin;
846   double top_margin;
847   double bottom_margin;
848
849   page_setup = gtk_print_context_get_page_setup (context);
850   width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM);
851   height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_MM);
852   left_margin = gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM);
853   right_margin = gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_MM);
854   top_margin = gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM);
855   bottom_margin = gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_MM);
856
857   string_map_init (&options);
858   string_map_insert_nocopy (&options, xstrdup ("paper-size"),
859                             c_xasprintf("%.2fx%.2fmm", width, height));
860   string_map_insert_nocopy (&options, xstrdup ("left-margin"),
861                             c_xasprintf ("%.2fmm", left_margin));
862   string_map_insert_nocopy (&options, xstrdup ("right-margin"),
863                             c_xasprintf ("%.2fmm", right_margin));
864   string_map_insert_nocopy (&options, xstrdup ("top-margin"),
865                             c_xasprintf ("%.2fmm", top_margin));
866   string_map_insert_nocopy (&options, xstrdup ("bottom-margin"),
867                             c_xasprintf ("%.2fmm", bottom_margin));
868
869   view->print_xrd = xr_driver_create (get_cairo_context_from_print_context (context), &options);
870
871   string_map_destroy (&options);
872 }
873
874 static gboolean
875 paginate (GtkPrintOperation *operation,
876           GtkPrintContext   *context,
877           struct psppire_output_view *view)
878 {
879   if (view->paginated)
880     {
881       /* Sometimes GTK+ emits this signal again even after pagination is
882          complete.  Don't let that screw up printing. */
883       return TRUE;
884     }
885   else if ( view->print_item < view->n_items )
886     {
887       xr_driver_output_item (view->print_xrd,
888                              view->items[view->print_item++].item);
889       while (xr_driver_need_new_page (view->print_xrd))
890         {
891           xr_driver_next_page (view->print_xrd, get_cairo_context_from_print_context (context));
892           view->print_n_pages ++;
893         }
894       return FALSE;
895     }
896   else
897     {
898       gtk_print_operation_set_n_pages (operation, view->print_n_pages);
899
900       /* Re-create the driver to do the real printing. */
901       xr_driver_destroy (view->print_xrd);
902       create_xr_print_driver (context, view);
903       view->print_item = 0;
904       view->paginated = TRUE;
905
906       return TRUE;
907     }
908 }
909
910 static void
911 begin_print (GtkPrintOperation *operation,
912              GtkPrintContext   *context,
913              struct psppire_output_view *view)
914 {
915   create_xr_print_driver (context, view);
916
917   view->print_item = 0;
918   view->print_n_pages = 1;
919   view->paginated = FALSE;
920 }
921
922 static void
923 end_print (GtkPrintOperation *operation,
924            GtkPrintContext   *context,
925            struct psppire_output_view *view)
926 {
927   xr_driver_destroy (view->print_xrd);
928 }
929
930
931 static void
932 draw_page (GtkPrintOperation *operation,
933            GtkPrintContext   *context,
934            gint               page_number,
935            struct psppire_output_view *view)
936 {
937   xr_driver_next_page (view->print_xrd, get_cairo_context_from_print_context (context));
938   while (!xr_driver_need_new_page (view->print_xrd)
939          && view->print_item < view->n_items)
940     xr_driver_output_item (view->print_xrd, view->items [view->print_item++].item);
941 }
942
943
944 void
945 psppire_output_view_print (struct psppire_output_view *view,
946                            GtkWindow *parent_window)
947 {
948   GtkPrintOperationResult res;
949
950   GtkPrintOperation *print = gtk_print_operation_new ();
951
952   if (view->print_settings != NULL)
953     gtk_print_operation_set_print_settings (print, view->print_settings);
954
955   g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), view);
956   g_signal_connect (print, "end_print",   G_CALLBACK (end_print),   view);
957   g_signal_connect (print, "paginate",    G_CALLBACK (paginate),    view);
958   g_signal_connect (print, "draw_page",   G_CALLBACK (draw_page),   view);
959
960   res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
961                                  parent_window, NULL);
962
963   if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
964     {
965       if (view->print_settings != NULL)
966         g_object_unref (view->print_settings);
967       view->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
968     }
969
970   g_object_unref (print);
971 }
972 \f
973 struct psppire_output_view_driver
974   {
975     struct output_driver driver;
976     struct psppire_output_view *view;
977   };
978
979 static struct psppire_output_view_driver *
980 psppire_output_view_driver_cast (struct output_driver *driver)
981 {
982   return UP_CAST (driver, struct psppire_output_view_driver, driver);
983 }
984
985 static void
986 psppire_output_view_submit (struct output_driver *this,
987                             const struct output_item *item)
988 {
989   struct psppire_output_view_driver *povd = psppire_output_view_driver_cast (this);
990
991   if (is_table_item (item))
992     psppire_output_view_put (povd->view, item);
993 }
994
995 static struct output_driver_class psppire_output_view_driver_class =
996   {
997     "PSPPIRE Output View",      /* name */
998     NULL,                       /* destroy */
999     psppire_output_view_submit, /* submit */
1000     NULL,                       /* flush */
1001   };
1002
1003 void
1004 psppire_output_view_register_driver (struct psppire_output_view *view)
1005 {
1006   struct psppire_output_view_driver *povd;
1007   struct output_driver *d;
1008
1009   povd = xzalloc (sizeof *povd);
1010   povd->view = view;
1011   d = &povd->driver;
1012   output_driver_init (d, &psppire_output_view_driver_class, "PSPPIRE Output View",
1013                       SETTINGS_DEVICE_UNFILTERED);
1014   output_driver_register (d);
1015 }