output: Make groups contain their subitems, and get rid of spv_item.
[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 <cairo/cairo-svg.h>
22 #include <errno.h>
23 #include <stdbool.h>
24
25 #include "libpspp/assertion.h"
26 #include "libpspp/string-map.h"
27 #include "output/cairo-fsm.h"
28 #include "output/cairo-pager.h"
29 #include "output/driver-provider.h"
30 #include "output/driver.h"
31 #include "output/output-item.h"
32 #include "output/pivot-table.h"
33
34 #include "gl/c-xvasprintf.h"
35 #include "gl/minmax.h"
36 #include "gl/clean-temp.h"
37 #include "gl/xalloc.h"
38
39 #include <gettext.h>
40 #define _(msgid) gettext (msgid)
41
42 struct output_view_item
43   {
44     struct output_item *item;
45     GtkWidget *drawing_area;
46   };
47
48 struct psppire_output_view
49   {
50     struct xr_fsm_style *style;
51     int object_spacing;
52
53     GtkLayout *output;
54     int render_width;
55     int max_width;
56     glong y;
57
58     GtkTreeView *overview;
59     GtkTreePath *cur_group;
60
61     GtkWidget *toplevel;
62
63     guint buttontime; /* Time of the button event */
64
65     struct output_view_item *items;
66     size_t n_items, allocated_items;
67     struct output_view_item *selected_item;
68
69     /* Variables pertaining to printing */
70     GtkPrintSettings *print_settings;
71
72     struct xr_fsm_style *fsm_style;
73     struct xr_page_style *page_style;
74     struct xr_pager *pager;
75     int print_item;
76     int print_n_pages;
77     gboolean paginated;
78   };
79
80 enum
81   {
82     COL_LABEL,                  /* Output item label. */
83     COL_ADDR,                   /* Pointer to the table */
84     COL_Y,                      /* Y position of top of object. */
85     N_COLS
86   };
87
88 static GtkTargetList *build_target_list (const struct output_item *item);
89 static void clipboard_get_cb (GtkClipboard     *clipboard,
90                               GtkSelectionData *selection_data,
91                               guint             info,
92                               gpointer          data);
93
94 /* Draws a white background on the GtkLayout to match the white background of
95    each of the output items. */
96 static gboolean
97 layout_draw_callback (GtkWidget *widget, cairo_t *cr, gpointer data)
98 {
99   int width = gtk_widget_get_allocated_width (widget);
100   int height = gtk_widget_get_allocated_height (widget);
101   GtkStyleContext *context = gtk_widget_get_style_context (widget);
102   gtk_render_background (context, cr, 0, 0, width, height);
103   return FALSE;                 /* Continue drawing the GtkDrawingAreas. */
104 }
105
106 static gboolean
107 draw_callback (GtkWidget *widget, cairo_t *cr, gpointer data)
108 {
109   GdkRectangle clip;
110   if (!gdk_cairo_get_clip_rectangle (cr, &clip))
111     return TRUE;
112
113   struct xr_fsm *fsm = g_object_get_data (G_OBJECT (widget), "fsm");
114
115   /* Draw the background based on the state of the widget
116      which can be selected or not selected */
117   GtkStyleContext *context = gtk_widget_get_style_context (widget);
118   gtk_render_background (context, cr, clip.x, clip.y,
119                          clip.x + clip.width, clip.y + clip.height);
120   /* Select the default foreground color based on current style
121      and state of the widget */
122   GtkStateFlags state = gtk_widget_get_state_flags (widget);
123   GdkRGBA color;
124   gtk_style_context_get_color (context, state, &color);
125   cairo_set_source_rgba (cr, color.red, color.green, color.blue, color.alpha);
126   xr_fsm_draw_region (fsm, cr, clip.x, clip.y, clip.width, clip.height);
127
128   return TRUE;
129 }
130
131 static void
132 free_fsm (gpointer fsm_)
133 {
134   struct xr_fsm *fsm = fsm_;
135   xr_fsm_destroy (fsm);
136 }
137
138 static struct xr_fsm_style *
139 get_xr_fsm_style (struct psppire_output_view *view)
140 {
141   GtkStyleContext *context
142     = gtk_widget_get_style_context (GTK_WIDGET (view->output));
143   GtkStateFlags state = gtk_widget_get_state_flags (GTK_WIDGET (view->output));
144
145   int xr_width = view->render_width * 1000;
146
147   PangoFontDescription *pf;
148   gtk_style_context_get (context, state, "font", &pf, NULL);
149
150   struct xr_fsm_style *style = xmalloc (sizeof *style);
151   *style = (struct xr_fsm_style) {
152     .ref_cnt = 1,
153     .size = { [TABLE_HORZ] = xr_width, [TABLE_VERT] = INT_MAX },
154     .min_break = { [TABLE_HORZ] = xr_width / 2, [TABLE_VERT] = 0 },
155     .font = pf,
156     .use_system_colors = true,
157     .object_spacing = XR_POINT * 12,
158     .font_resolution = 96.0,
159   };
160
161   return style;
162 }
163
164 /* Return the horizontal position to place a widget whose
165    width is CHILD_WIDTH */
166 static gint
167 get_xpos (const struct psppire_output_view *view, gint child_width)
168 {
169   GdkWindow *gdkw = gtk_widget_get_window (GTK_WIDGET (view->output));
170   guint w = gdk_window_get_width (gdkw);
171   int gutter = 0;
172   g_object_get (view->output, "border-width", &gutter, NULL);
173   return (gtk_widget_get_direction (GTK_WIDGET (view->output)) ==  GTK_TEXT_DIR_RTL) ? w - child_width - gutter: gutter;
174 }
175
176 static struct output_view_item *
177 find_selected_item (struct psppire_output_view *view)
178 {
179   struct output_view_item *item = NULL;
180   if (view == NULL)
181     return NULL;
182   if (view->items == NULL)
183     return NULL;
184
185   for (item = view->items; item < &view->items[view->n_items]; item++)
186     {
187       GtkWidget *widget = GTK_WIDGET (item->drawing_area);
188       if (GTK_IS_WIDGET (widget))
189         {
190           GtkStateFlags state = gtk_widget_get_state_flags (widget);
191           if (state & GTK_STATE_FLAG_SELECTED)
192             return item;
193         }
194     }
195   return NULL;
196 }
197
198
199 static void
200 set_copy_action (struct psppire_output_view *view,
201                  gboolean state)
202 {
203   GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (view->output));
204   GAction *copy_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
205                                                      "copy");
206   g_object_set (copy_action,
207                 "enabled", state,
208                 NULL);
209 }
210
211 static void
212 clear_selection (struct psppire_output_view *view)
213 {
214   if (view == NULL)
215     return;
216   struct output_view_item *item = find_selected_item (view);
217   if (item == NULL)
218     return;
219   set_copy_action (view, FALSE);
220   GtkWidget *widget = GTK_WIDGET (item->drawing_area);
221   if (GTK_IS_WIDGET (widget))
222     {
223       gtk_widget_unset_state_flags (widget, GTK_STATE_FLAG_SELECTED);
224       gtk_widget_queue_draw (widget);
225     }
226 }
227
228 static gboolean
229 off_item_button_press_event_cb (GtkWidget      *widget,
230                                 GdkEventButton *event,
231                                 struct psppire_output_view *view)
232 {
233   /* buttontime is set by button_press_event_cb
234      If our event->time is equal to the time from the
235      button_press_event_cb, then we handle the same event.
236      In that case we must not clear the selection because
237      it was just set by button_press_event_cb from the item */
238   if (event->time != view->buttontime)
239     clear_selection (view);
240   return FALSE; /* Forward the event -> DragNDrop */
241 }
242
243 static gboolean
244 button_press_event_cb (GtkWidget      *widget,
245                        GdkEventButton *event,
246                        struct psppire_output_view *view)
247 {
248   view->buttontime = event->time;
249   clear_selection (view);
250   set_copy_action (view, TRUE);
251   gtk_widget_set_state_flags (widget, GTK_STATE_FLAG_SELECTED, FALSE);
252   gtk_widget_queue_draw (widget);
253   return FALSE; /* Forward Event -> off_item will trigger */
254 }
255
256 static void
257 drag_data_get_cb (GtkWidget *widget, GdkDragContext *context,
258                   GtkSelectionData *selection_data,
259                   guint target_type, guint time,
260                   struct psppire_output_view *view)
261 {
262   view->selected_item = find_selected_item (view);
263   clipboard_get_cb (NULL, selection_data, target_type, view);
264 }
265
266 static void
267 create_drawing_area (struct psppire_output_view *view,
268                      GtkWidget *drawing_area, struct xr_fsm *r,
269                      int tw, int th, const struct output_item *item)
270 {
271   g_object_set_data_full (G_OBJECT (drawing_area),
272                           "fsm", r, free_fsm);
273   g_signal_connect (drawing_area, "button-press-event",
274                     G_CALLBACK (button_press_event_cb), view);
275   gtk_widget_add_events (drawing_area, GDK_BUTTON_PRESS_MASK);
276
277   { /* Drag and Drop */
278     GtkTargetList *tl = build_target_list (item);
279     g_assert (tl);
280     gtk_drag_source_set (drawing_area, GDK_BUTTON1_MASK, NULL, 0, GDK_ACTION_COPY);
281     gtk_drag_source_set_target_list (drawing_area, tl);
282     gtk_target_list_unref (tl);
283     g_signal_connect (drawing_area, "drag-data-get",
284                       G_CALLBACK (drag_data_get_cb), view);
285   }
286   GtkStyleContext *context = gtk_widget_get_style_context (drawing_area);
287   gtk_style_context_add_class (context,
288                                GTK_STYLE_CLASS_VIEW);
289   g_signal_connect (drawing_area, "draw",
290                     G_CALLBACK (draw_callback), view);
291
292   gtk_widget_set_size_request (drawing_area, tw, th);
293   gint xpos = get_xpos (view, tw);
294
295   gtk_layout_put (view->output, drawing_area, xpos, view->y);
296
297   gtk_widget_show (drawing_area);
298 }
299
300 static void
301 rerender (struct psppire_output_view *view)
302 {
303   struct output_view_item *item;
304   GdkWindow *gdkw = gtk_widget_get_window (GTK_WIDGET (view->output));
305
306   if (!view->n_items || ! gdkw)
307     return;
308
309   if (!view->style)
310     view->style = get_xr_fsm_style (view);
311
312   GdkWindow *win = gtk_layout_get_bin_window (view->output);
313   cairo_region_t *region = gdk_window_get_visible_region (win);
314   GdkDrawingContext *ctx =  gdk_window_begin_draw_frame (win, region);
315   cairo_t *cr = gdk_drawing_context_get_cairo_context (ctx);
316
317   view->y = 0;
318   view->max_width = 0;
319   for (item = view->items; item < &view->items[view->n_items]; item++)
320     {
321       struct xr_fsm *r;
322       GtkAllocation alloc;
323       int tw, th;
324
325       if (view->y > 0)
326         view->y += view->object_spacing;
327
328       if (item->item->type == OUTPUT_ITEM_GROUP)
329         continue;
330
331       r = xr_fsm_create_for_scrolling (item->item, view->style, cr);
332       if (r == NULL)
333         {
334           g_warn_if_reached ();
335           continue;
336         }
337
338       xr_fsm_measure (r, cr, &tw, &th);
339
340       gint xpos = get_xpos (view, tw);
341
342       if (!item->drawing_area)
343         {
344           item->drawing_area = gtk_drawing_area_new ();
345           create_drawing_area (view, item->drawing_area, r, tw, th, item->item);
346         }
347       else
348         {
349           g_object_set_data_full (G_OBJECT (item->drawing_area),
350                                   "fsm", r, free_fsm);
351           gtk_widget_set_size_request (item->drawing_area, tw, th);
352           gtk_layout_move (view->output, item->drawing_area, xpos, view->y);
353         }
354
355       if (item->item->type == OUTPUT_ITEM_TABLE)
356         gtk_widget_set_tooltip_text (item->drawing_area,
357                                      item->item->table->notes);
358
359       {
360         gint minw;
361         gint minh;
362         /* This code probably doesn't bring us anthing, but Gtk
363            shows warnings if get_preferred_width/height is not
364            called before the size_allocate below is called. */
365         gtk_widget_get_preferred_width (item->drawing_area, &minw, NULL);
366         gtk_widget_get_preferred_height (item->drawing_area, &minh, NULL);
367         if (th > minh) th = minh;
368         if (tw > minw) tw = minw;
369       }
370       alloc.x = xpos;
371       alloc.y = view->y;
372       alloc.width = tw;
373       alloc.height = th;
374
375       gtk_widget_size_allocate (item->drawing_area, &alloc);
376
377       if (view->max_width < tw)
378         view->max_width = tw;
379       view->y += th;
380     }
381
382   gtk_layout_set_size (view->output,
383                        view->max_width + view->object_spacing,
384                        view->y + view->object_spacing);
385
386   gdk_window_end_draw_frame (win, ctx);
387   cairo_region_destroy (region);
388 }
389
390
391 void
392 psppire_output_view_put (struct psppire_output_view *view,
393                          const struct output_item *item)
394 {
395   struct output_view_item *view_item;
396   GtkWidget *drawing_area;
397   int tw, th;
398
399   if (item->type == OUTPUT_ITEM_GROUP)
400     return;
401
402   if (item->type == OUTPUT_ITEM_TEXT)
403     {
404       char *text = text_item_get_plain_text (item);
405       bool text_is_empty = text[0] == '\0';
406       free (text);
407       if (text_is_empty)
408         return;
409     }
410
411   if (view->n_items >= view->allocated_items)
412     view->items = x2nrealloc (view->items, &view->allocated_items,
413                                 sizeof *view->items);
414   view_item = &view->items[view->n_items++];
415   view_item->item = output_item_ref (item);
416   view_item->drawing_area = NULL;
417
418   GdkWindow *win = gtk_widget_get_window (GTK_WIDGET (view->output));
419   if (win)
420     {
421       view_item->drawing_area = drawing_area = gtk_drawing_area_new ();
422
423       if (!view->style)
424         view->style = get_xr_fsm_style (view);
425
426       cairo_region_t *region = gdk_window_get_visible_region (win);
427       GdkDrawingContext *ctx = gdk_window_begin_draw_frame (win, region);
428       cairo_t *cr = gdk_drawing_context_get_cairo_context (ctx);
429
430       if (view->y > 0)
431         view->y += view->object_spacing;
432
433       struct xr_fsm *r = xr_fsm_create_for_scrolling (item, view->style, cr);
434       if (r == NULL)
435         {
436           gdk_window_end_draw_frame (win, ctx);
437           cairo_region_destroy (region);
438           return;
439         }
440
441       xr_fsm_measure (r, cr, &tw, &th);
442       create_drawing_area (view, drawing_area, r, tw, th, item);
443       gdk_window_end_draw_frame (win, ctx);
444       cairo_region_destroy (region);
445     }
446   else
447     tw = th = 0;
448
449   if (view->overview)
450     {
451       GtkTreeStore *store = GTK_TREE_STORE (
452         gtk_tree_view_get_model (view->overview));
453
454       /* Create a new node in the tree and puts a reference to it in 'iter'. */
455       GtkTreeIter iter;
456       GtkTreeIter parent;
457       if (view->cur_group
458           && gtk_tree_path_get_depth (view->cur_group) > 0
459           && gtk_tree_model_get_iter (GTK_TREE_MODEL (store),
460                                       &parent, view->cur_group))
461         gtk_tree_store_append (store, &iter, &parent);
462       else
463         gtk_tree_store_append (store, &iter, NULL);
464
465       /* XXX group? */
466       gtk_tree_store_set (store, &iter,
467                           COL_LABEL, output_item_get_label (item),
468                           COL_ADDR, item,
469                           COL_Y, view->y,
470                           -1);
471
472       GtkTreePath *path = gtk_tree_model_get_path (
473         GTK_TREE_MODEL (store), &iter);
474       gtk_tree_view_expand_row (view->overview, path, TRUE);
475       gtk_tree_path_free (path);
476     }
477
478   if (view->max_width < tw)
479     view->max_width = tw;
480   view->y += th;
481
482   gtk_layout_set_size (view->output, view->max_width, view->y);
483 }
484
485 static void
486 on_row_activate (GtkTreeView *overview,
487                  GtkTreePath *path,
488                  GtkTreeViewColumn *column,
489                  struct psppire_output_view *view)
490 {
491   GtkTreeModel *model;
492   GtkTreeIter iter;
493   GtkAdjustment *vadj;
494   GValue value = {0};
495   double y, min, max;
496
497   model = gtk_tree_view_get_model (overview);
498   if (!gtk_tree_model_get_iter (model, &iter, path))
499     return;
500
501   gtk_tree_model_get_value (model, &iter, COL_Y, &value);
502   y = g_value_get_long (&value);
503   g_value_unset (&value);
504
505   vadj = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (view->output));
506   min = gtk_adjustment_get_lower (vadj);
507   max = gtk_adjustment_get_upper (vadj) - gtk_adjustment_get_page_size (vadj);
508   if (y < min)
509     y = min;
510   else if (y > max)
511     y = max;
512   gtk_adjustment_set_value (vadj, y);
513 }
514
515 static void
516 on_style_updated (GtkWidget *toplevel, struct psppire_output_view *view)
517 {
518   if (!view->n_items || !gtk_widget_get_window (GTK_WIDGET (view->output)))
519     return;
520
521   /* GTK+ fires this signal for trivial changes like the mouse moving in or out
522      of the window.  Check whether the actual fsm options changed and
523      re-render only if they did. */
524   struct xr_fsm_style *style = get_xr_fsm_style (view);
525   if (!view->style || !xr_fsm_style_equals (style, view->style))
526     {
527       xr_fsm_style_unref (view->style);
528       view->style = xr_fsm_style_ref (style);
529       rerender (view);
530     }
531   xr_fsm_style_unref (style);
532 }
533
534 enum {
535   SELECT_FMT_NULL,
536   SELECT_FMT_TEXT,
537   SELECT_FMT_UTF8,
538   SELECT_FMT_HTML,
539   SELECT_FMT_SVG,
540   SELECT_FMT_IMG,
541   SELECT_FMT_ODT
542 };
543
544 static void
545 clear_rectangle (cairo_surface_t *surface,
546                  double x0, double y0, double x1, double y1)
547 {
548   cairo_t *cr = cairo_create (surface);
549   cairo_set_source_rgb (cr, 1, 1, 1);
550   cairo_new_path (cr);
551   cairo_rectangle (cr, x0, y0, x1 - x0, y1 - y0);
552   cairo_fill (cr);
553   cairo_destroy (cr);
554 }
555
556 static void
557 clipboard_get_cb (GtkClipboard     *clipboard,
558                   GtkSelectionData *selection_data,
559                   guint             info,
560                   gpointer          data)
561 {
562   struct psppire_output_view *view = data;
563
564   gsize length;
565   gchar *text = NULL;
566   struct output_driver *driver = NULL;
567   char *filename;
568   struct string_map options;
569   struct temp_dir *td = NULL;
570
571   if (view->selected_item == NULL)
572     return;
573
574   td = create_temp_dir ("pspp", NULL, false);
575   if (td == NULL)
576     {
577       msg_error (errno, _("failed to create temporary directory during clipboard operation"));
578       return;
579     }
580   filename = xasprintf ("%s/clip.tmp", td->dir_name);
581
582   string_map_init (&options);
583   string_map_insert (&options, "output-file", filename);
584
585   switch (info)
586     {
587     case SELECT_FMT_UTF8:
588       string_map_insert (&options, "box", "unicode");
589       /* fall-through */
590
591     case SELECT_FMT_TEXT:
592       string_map_insert (&options, "format", "txt");
593       string_map_insert (&options, "width", "1000");
594       break;
595
596     case SELECT_FMT_HTML:
597       string_map_insert (&options, "format", "html");
598       string_map_insert (&options, "borders", "false");
599       string_map_insert (&options, "css", "false");
600       break;
601
602     case SELECT_FMT_SVG:
603     case SELECT_FMT_IMG:
604       /* see below */
605       break;
606
607     case SELECT_FMT_ODT:
608       string_map_insert (&options, "format", "odt");
609       break;
610
611     default:
612       g_warning ("unsupported clip target\n");
613       goto finish;
614       break;
615     }
616
617   if ((info == SELECT_FMT_IMG) ||
618       (info == SELECT_FMT_SVG) )
619     {
620       GtkWidget *widget = view->selected_item->drawing_area;
621       struct xr_fsm *fsm = g_object_get_data (G_OBJECT (widget), "fsm");
622
623       GdkWindow *win = gtk_layout_get_bin_window (view->output);
624       cairo_region_t *region = gdk_window_get_visible_region (win);
625       GdkDrawingContext *ctx =  gdk_window_begin_draw_frame (win, region);
626       cairo_t *cr = gdk_drawing_context_get_cairo_context (ctx);
627
628       int w, h;
629       xr_fsm_measure (fsm, cr, &w, &h);
630
631       gdk_window_end_draw_frame (win, ctx);
632       cairo_region_destroy (region);
633
634       cairo_surface_t *surface
635         = (info == SELECT_FMT_SVG
636            ? cairo_svg_surface_create (filename, w, h)
637            : cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w, h));
638       clear_rectangle (surface, 0, 0, w, h);
639       cairo_t *cr2 = cairo_create (surface);
640       xr_fsm_draw_all (fsm, cr2);
641       cairo_destroy (cr2);
642       if (info == SELECT_FMT_IMG)
643         {
644           GdkPixbuf *pixbuf = gdk_pixbuf_get_from_surface (surface,
645                                                            0, 0, w, h);
646           if (pixbuf)
647             {
648               gtk_selection_data_set_pixbuf (selection_data, pixbuf);
649               g_object_unref (pixbuf);
650             }
651         }
652       cairo_surface_destroy (surface);
653     }
654   else
655     {
656       driver = output_driver_create (&options);
657       if (driver == NULL)
658         goto finish;
659
660       driver->class->submit (driver, view->selected_item->item);
661
662       if (driver->class->flush)
663         driver->class->flush (driver);
664
665       /* Some drivers (eg: the odt one) don't write anything until they
666          are closed */
667       output_driver_destroy (driver);
668       driver = NULL;
669     }
670
671   if (info != SELECT_FMT_IMG
672       && g_file_get_contents (filename, &text, &length, NULL))
673     gtk_selection_data_set (selection_data,
674                             gtk_selection_data_get_target (selection_data),
675                             8, (const guchar *) text, length);
676
677  finish:
678
679   if (driver != NULL)
680     output_driver_destroy (driver);
681
682   g_free (text);
683
684   unlink (filename);
685   free (filename);
686   cleanup_temp_dir (td);
687 }
688
689 static void
690 clipboard_clear_cb (GtkClipboard *clipboard,
691                     gpointer data)
692 {
693 }
694
695 #define CBTARGETS                                           \
696 CT ( ctn1, "STRING",        0, SELECT_FMT_TEXT )            \
697 CT ( ctn2, "TEXT",          0, SELECT_FMT_TEXT )            \
698 CT ( ctn3, "COMPOUND_TEXT", 0, SELECT_FMT_TEXT )            \
699 CT ( ctn4, "text/plain",    0, SELECT_FMT_TEXT )            \
700 CT ( ctn5, "UTF8_STRING",   0, SELECT_FMT_UTF8 )            \
701 CT ( ctn6, "text/plain;charset=utf-8", 0, SELECT_FMT_UTF8 ) \
702 CT ( ctn7, "text/html",     0, SELECT_FMT_HTML )            \
703 CT ( ctn8, "image/svg+xml", 0, SELECT_FMT_SVG )
704
705 #define CT(ID, TARGET, FLAGS, INFO) static gchar ID[] = TARGET;
706 CBTARGETS
707 #undef CT
708 static gchar ctnlast[] = "application/vnd.oasis.opendocument.text";
709
710 static const GtkTargetEntry targets[] = {
711 #define CT(ID, TARGET, FLAGS, INFO) { ID, FLAGS, INFO },
712   CBTARGETS
713 #undef CT
714   { ctnlast, 0, SELECT_FMT_ODT }
715 };
716
717 static GtkTargetList *
718 build_target_list (const struct output_item *item)
719 {
720   GtkTargetList *tl = gtk_target_list_new (targets, G_N_ELEMENTS (targets));
721   g_return_val_if_fail (tl, NULL);
722   if (item->type == OUTPUT_ITEM_TABLE || item->type == OUTPUT_ITEM_CHART)
723     gtk_target_list_add_image_targets (tl, SELECT_FMT_IMG, TRUE);
724   return tl;
725 }
726
727 static void
728 on_copy (struct psppire_output_view *view)
729 {
730   GtkWidget *widget = GTK_WIDGET (view->overview);
731   GtkClipboard *cb = gtk_widget_get_clipboard (widget, GDK_SELECTION_CLIPBOARD);
732
733   struct output_view_item *ov_item = find_selected_item (view);
734   if (ov_item == NULL)
735     return;
736   view->selected_item = ov_item;
737   GtkTargetList *tl = build_target_list (ov_item->item);
738   g_return_if_fail (tl);
739   gint no_of_targets = 0;
740   GtkTargetEntry *ta = gtk_target_table_new_from_list (tl, &no_of_targets);
741   g_return_if_fail (ta);
742   if (!gtk_clipboard_set_with_data (cb, ta, no_of_targets,
743                                     clipboard_get_cb, clipboard_clear_cb,
744                                     view))
745     clipboard_clear_cb (cb, view);
746
747   gtk_target_list_unref (tl);
748   gtk_target_table_free (ta,no_of_targets);
749 }
750
751 static void
752 on_size_allocate (GtkWidget    *widget,
753                   GdkRectangle *allocation,
754                   struct psppire_output_view *view)
755 {
756   view->render_width = MAX (300, allocation->width);
757   rerender (view);
758 }
759
760 static void
761 on_realize (GtkWidget *overview, GObject *view)
762 {
763   GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (overview));
764
765   GAction *copy_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
766                                                      "copy");
767
768   GAction *select_all_action = g_action_map_lookup_action (G_ACTION_MAP (toplevel),
769                                                            "select-all");
770
771   g_object_set (copy_action, "enabled", FALSE, NULL);
772   g_object_set (select_all_action, "enabled", FALSE, NULL);
773
774   g_signal_connect_swapped (copy_action, "activate",
775                             G_CALLBACK (on_copy), view);
776
777 }
778
779 struct psppire_output_view *
780 psppire_output_view_new (GtkLayout *output, GtkTreeView *overview)
781 {
782   struct psppire_output_view *view;
783   GtkTreeViewColumn *column;
784   GtkCellRenderer *renderer;
785
786   GtkTreeModel *model;
787
788   view = xmalloc (sizeof *view);
789   *view = (struct psppire_output_view) {
790     .object_spacing = 10,
791     .output = output,
792     .overview = overview,
793     .toplevel = gtk_widget_get_toplevel (GTK_WIDGET (output)),
794   };
795
796   g_signal_connect (output, "draw", G_CALLBACK (layout_draw_callback), NULL);
797
798   g_signal_connect (output, "style-updated", G_CALLBACK (on_style_updated), view);
799
800   g_signal_connect (output, "size-allocate", G_CALLBACK (on_size_allocate), view);
801
802   gtk_widget_add_events (GTK_WIDGET (output), GDK_BUTTON_PRESS_MASK);
803   g_signal_connect (output, "button-press-event",
804                     G_CALLBACK (off_item_button_press_event_cb), view);
805
806   gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (output)),
807                                GTK_STYLE_CLASS_VIEW);
808
809   if (overview)
810     {
811       g_signal_connect (overview, "realize", G_CALLBACK (on_realize), view);
812
813       model = GTK_TREE_MODEL (gtk_tree_store_new (
814                                 N_COLS,
815                                 G_TYPE_STRING,  /* COL_LABEL */
816                                 G_TYPE_POINTER, /* COL_ADDR */
817                                 G_TYPE_LONG));  /* COL_Y */
818       gtk_tree_view_set_model (overview, model);
819       g_object_unref (model);
820
821       column = gtk_tree_view_column_new ();
822       gtk_tree_view_append_column (GTK_TREE_VIEW (overview), column);
823       renderer = gtk_cell_renderer_text_new ();
824       gtk_tree_view_column_pack_start (column, renderer, TRUE);
825       gtk_tree_view_column_add_attribute (column, renderer, "text", COL_LABEL);
826
827       g_signal_connect (GTK_TREE_VIEW (overview),
828                         "row-activated", G_CALLBACK (on_row_activate), view);
829     }
830
831   return view;
832 }
833
834 void
835 psppire_output_view_destroy (struct psppire_output_view *view)
836 {
837   size_t i;
838
839   if (!view)
840     return;
841
842   g_signal_handlers_disconnect_by_func (view->output,
843                                         G_CALLBACK (on_style_updated), view);
844
845   xr_fsm_style_unref (view->style);
846
847   for (i = 0; i < view->n_items; i++)
848     output_item_unref (view->items[i].item);
849   free (view->items);
850   view->items = NULL;
851   view->n_items = view->allocated_items = 0;
852
853   if (view->print_settings != NULL)
854     g_object_unref (view->print_settings);
855
856   if (view->cur_group)
857     gtk_tree_path_free (view->cur_group);
858
859   free (view);
860 }
861
862 void
863 psppire_output_view_clear (struct psppire_output_view *view)
864 {
865   size_t i;
866
867   view->max_width = 0;
868   view->y = 0;
869
870   for (i = 0; i < view->n_items; i++)
871     {
872       gtk_container_remove (GTK_CONTAINER (view->output),
873                             view->items[i].drawing_area);
874       output_item_unref (view->items[i].item);
875     }
876   free (view->items);
877   view->items = NULL;
878   view->n_items = view->allocated_items = 0;
879 }
880
881 /* Export. */
882
883 void
884 psppire_output_view_export (struct psppire_output_view *view,
885                             struct string_map *options)
886 {
887   struct output_driver *driver;
888
889   driver = output_driver_create (options);
890   if (driver)
891     {
892       size_t i;
893
894       for (i = 0; i < view->n_items; i++)
895         driver->class->submit (driver, view->items[i].item);
896       output_driver_destroy (driver);
897     }
898 }
899 \f
900 /* Print. */
901
902 static cairo_t *
903 get_cairo_context_from_print_context (GtkPrintContext *context)
904 {
905   cairo_t *cr = gtk_print_context_get_cairo_context (context);
906   return cairo_reference (cr);
907 }
908
909 static void
910 create_xr_print_driver (GtkPrintContext *context, struct psppire_output_view *view)
911 {
912   GtkPageSetup *ps = gtk_print_context_get_page_setup (context);
913
914   enum { H = TABLE_HORZ, V = TABLE_VERT };
915   int paper[TABLE_N_AXES] = {
916     [H] = gtk_page_setup_get_paper_width (ps, GTK_UNIT_POINTS) * XR_POINT,
917     [V] = gtk_page_setup_get_paper_height (ps, GTK_UNIT_POINTS) * XR_POINT,
918   };
919
920   /* These are all 1/2 inch.  The "margins" that GTK+ gives us are useless:
921      they are the printer's imagable area. */
922   int margins[TABLE_N_AXES][2] = {
923     [H][0] = XR_POINT * 36,
924     [H][1] = XR_POINT * 36,
925     [V][0] = XR_POINT * 36,
926     [V][1] = XR_POINT * 36,
927   };
928
929   double size[TABLE_N_AXES];
930   for (int a = 0; a < TABLE_N_AXES; a++)
931     size[a] = paper[a] - margins[a][0] - margins[a][1];
932
933   view->page_style = xmalloc (sizeof *view->page_style);
934   *view->page_style = (struct xr_page_style) {
935     .ref_cnt = 1,
936
937     .margins = {
938       [H] = { margins[H][0], margins[H][1] },
939       [V] = { margins[V][0], margins[V][1] },
940     },
941     .initial_page_number = 1,
942   };
943
944   view->fsm_style = xmalloc (sizeof *view->fsm_style);
945   *view->fsm_style = (struct xr_fsm_style) {
946     .ref_cnt = 1,
947
948     .size = { [H] = size[H], [V] = size[V] },
949     .min_break = { [H] = size[H] / 2, [V] = size[V] / 2 },
950     .font = pango_font_description_from_string ("Sans Serif 10"),
951     .fg = CELL_COLOR_BLACK,
952     .use_system_colors = false,
953     .object_spacing = 12 * XR_POINT,
954     .font_resolution = 72.0
955   };
956
957   view->pager = xr_pager_create (view->page_style, view->fsm_style);
958 }
959
960 static gboolean
961 paginate (GtkPrintOperation *operation,
962           GtkPrintContext   *context,
963           struct psppire_output_view *view)
964 {
965   if (view->paginated)
966     {
967       /* Sometimes GTK+ emits this signal again even after pagination is
968          complete.  Don't let that screw up printing. */
969       return TRUE;
970     }
971   else if (view->print_item < view->n_items)
972     {
973       xr_pager_add_item (view->pager, view->items[view->print_item++].item);
974       while (xr_pager_needs_new_page (view->pager))
975         {
976           xr_pager_add_page (view->pager,
977                              get_cairo_context_from_print_context (context));
978           view->print_n_pages ++;
979         }
980       return FALSE;
981     }
982   else
983     {
984       gtk_print_operation_set_n_pages (operation, MAX (1, view->print_n_pages));
985
986       /* Re-create the driver to do the real printing. */
987       xr_pager_destroy (view->pager);
988       view->pager = xr_pager_create (view->page_style, view->fsm_style);
989       view->print_item = 0;
990       view->paginated = TRUE;
991
992       return TRUE;
993     }
994 }
995
996 static void
997 begin_print (GtkPrintOperation *operation,
998              GtkPrintContext   *context,
999              struct psppire_output_view *view)
1000 {
1001   create_xr_print_driver (context, view);
1002
1003   view->print_item = 0;
1004   view->print_n_pages = 0;
1005   view->paginated = FALSE;
1006 }
1007
1008 static void
1009 end_print (GtkPrintOperation *operation,
1010            GtkPrintContext   *context,
1011            struct psppire_output_view *view)
1012 {
1013   xr_pager_destroy (view->pager);
1014   view->pager = NULL;
1015 }
1016
1017
1018 static void
1019 draw_page (GtkPrintOperation *operation,
1020            GtkPrintContext   *context,
1021            gint               page_number,
1022            struct psppire_output_view *view)
1023 {
1024   xr_pager_add_page (view->pager,
1025                      get_cairo_context_from_print_context (context));
1026   while (!xr_pager_needs_new_page (view->pager)
1027          && view->print_item < view->n_items)
1028     xr_pager_add_item (view->pager, view->items [view->print_item++].item);
1029 }
1030
1031
1032 void
1033 psppire_output_view_print (struct psppire_output_view *view,
1034                            GtkWindow *parent_window)
1035 {
1036   GtkPrintOperationResult res;
1037
1038   GtkPrintOperation *print = gtk_print_operation_new ();
1039
1040   if (view->print_settings != NULL)
1041     gtk_print_operation_set_print_settings (print, view->print_settings);
1042
1043   gtk_print_operation_set_use_full_page (print, TRUE);
1044   gtk_print_operation_set_unit (print, GTK_UNIT_POINTS);
1045
1046   g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), view);
1047   g_signal_connect (print, "end_print",   G_CALLBACK (end_print),   view);
1048   g_signal_connect (print, "paginate",    G_CALLBACK (paginate),    view);
1049   g_signal_connect (print, "draw_page",   G_CALLBACK (draw_page),   view);
1050
1051   res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1052                                  parent_window, NULL);
1053
1054   if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
1055     {
1056       if (view->print_settings != NULL)
1057         g_object_unref (view->print_settings);
1058       view->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
1059     }
1060
1061   g_object_unref (print);
1062 }
1063 \f
1064 struct psppire_output_view_driver
1065   {
1066     struct output_driver driver;
1067     struct psppire_output_view *view;
1068   };
1069
1070 static struct psppire_output_view_driver *
1071 psppire_output_view_driver_cast (struct output_driver *driver)
1072 {
1073   return UP_CAST (driver, struct psppire_output_view_driver, driver);
1074 }
1075
1076 static void
1077 psppire_output_view_submit (struct output_driver *this,
1078                             const struct output_item *item)
1079 {
1080   struct psppire_output_view_driver *povd = psppire_output_view_driver_cast (this);
1081
1082   if (item->type == OUTPUT_ITEM_TABLE)
1083     psppire_output_view_put (povd->view, item);
1084 }
1085
1086 static struct output_driver_class psppire_output_view_driver_class =
1087   {
1088     .name = "PSPPIRE Output View",
1089     .submit = psppire_output_view_submit,
1090     .handles_groups = true,
1091     .handles_show = true,
1092   };
1093
1094 void
1095 psppire_output_view_register_driver (struct psppire_output_view *view)
1096 {
1097   struct psppire_output_view_driver *povd;
1098   struct output_driver *d;
1099
1100   povd = xzalloc (sizeof *povd);
1101   povd->view = view;
1102   d = &povd->driver;
1103   output_driver_init (d, &psppire_output_view_driver_class, "PSPPIRE Output View",
1104                       SETTINGS_DEVICE_UNFILTERED);
1105   output_driver_register (d);
1106 }