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