gui: Speed up rendering of large tables that are only partly visible.
[pspp] / src / ui / gui / psppire-output-window.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 2009, 2010  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 <gtk/gtksignal.h>
20 #include <gtk/gtkbox.h>
21 #include "helper.h"
22
23 #include <libpspp/cast.h>
24 #include <libpspp/message.h>
25 #include <libpspp/string-map.h>
26 #include <output/cairo.h>
27 #include <output/chart-item.h>
28 #include <output/driver-provider.h>
29 #include <output/output-item.h>
30 #include <output/table-item.h>
31 #include <output/text-item.h>
32 #include <output/tab.h>
33 #include <stdlib.h>
34
35 #include "help-menu.h"
36
37 #include "psppire-output-window.h"
38
39
40 #include "xalloc.h"
41
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <unistd.h>
45
46 #include <gettext.h>
47 #define _(msgid) gettext (msgid)
48 #define N_(msgid) msgid
49
50 enum
51   {
52     COL_TITLE,                  /* Table title. */
53     COL_ADDR,                   /* Pointer to the table */
54     COL_Y,                      /* Y position of top of title. */
55     N_COLS
56   };
57
58 static void psppire_output_window_base_finalize (PsppireOutputWindowClass *, gpointer);
59 static void psppire_output_window_base_init     (PsppireOutputWindowClass *class);
60 static void psppire_output_window_class_init    (PsppireOutputWindowClass *class);
61 static void psppire_output_window_init          (PsppireOutputWindow      *window);
62
63
64 GType
65 psppire_output_window_get_type (void)
66 {
67   static GType psppire_output_window_type = 0;
68
69   if (!psppire_output_window_type)
70     {
71       static const GTypeInfo psppire_output_window_info =
72       {
73         sizeof (PsppireOutputWindowClass),
74         (GBaseInitFunc) psppire_output_window_base_init,
75         (GBaseFinalizeFunc) psppire_output_window_base_finalize,
76         (GClassInitFunc)psppire_output_window_class_init,
77         (GClassFinalizeFunc) NULL,
78         NULL,
79         sizeof (PsppireOutputWindow),
80         0,
81         (GInstanceInitFunc) psppire_output_window_init,
82       };
83
84       psppire_output_window_type =
85         g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireOutputWindow",
86                                 &psppire_output_window_info, 0);
87     }
88
89   return psppire_output_window_type;
90 }
91
92 static GObjectClass *parent_class;
93
94 static void
95 psppire_output_window_finalize (GObject *object)
96 {
97   if (G_OBJECT_CLASS (parent_class)->finalize)
98     (*G_OBJECT_CLASS (parent_class)->finalize) (object);
99 }
100
101
102 static void
103 psppire_output_window_dispose (GObject *obj)
104 {
105   PsppireOutputWindow *viewer = PSPPIRE_OUTPUT_WINDOW (obj);
106   size_t i;
107
108   for (i = 0; i < viewer->n_items; i++)
109     output_item_unref (viewer->items[i]);
110   free (viewer->items);
111   viewer->items = NULL;
112   viewer->n_items = viewer->allocated_items = 0;
113
114   g_object_unref (viewer->print_settings);
115
116   /* Chain up to the parent class */
117   G_OBJECT_CLASS (parent_class)->dispose (obj);
118 }
119
120 static void
121 psppire_output_window_class_init (PsppireOutputWindowClass *class)
122 {
123   GObjectClass *object_class = G_OBJECT_CLASS (class);
124
125   parent_class = g_type_class_peek_parent (class);
126   object_class->dispose = psppire_output_window_dispose;
127 }
128
129
130 static void
131 psppire_output_window_base_init (PsppireOutputWindowClass *class)
132 {
133   GObjectClass *object_class = G_OBJECT_CLASS (class);
134
135   object_class->finalize = psppire_output_window_finalize;
136 }
137
138
139
140 static void
141 psppire_output_window_base_finalize (PsppireOutputWindowClass *class,
142                                      gpointer class_data)
143 {
144 }
145 \f
146 /* Output driver class. */
147
148 struct psppire_output_driver
149   {
150     struct output_driver driver;
151     PsppireOutputWindow *viewer;
152     struct xr_driver *xr;
153   };
154
155 static struct output_driver_class psppire_output_class;
156
157 static struct psppire_output_driver *
158 psppire_output_cast (struct output_driver *driver)
159 {
160   assert (driver->class == &psppire_output_class);
161   return UP_CAST (driver, struct psppire_output_driver, driver);
162 }
163
164 static gboolean
165 expose_event_callback (GtkWidget *widget, GdkEventExpose *event, gpointer data)
166 {
167   struct xr_rendering *r = g_object_get_data (G_OBJECT (widget), "rendering");
168   cairo_t *cr;
169
170   cr = gdk_cairo_create (widget->window);
171   xr_rendering_draw (r, cr, event->area.x, event->area.y,
172                      event->area.width, event->area.height);
173   cairo_destroy (cr);
174
175   return TRUE;
176 }
177
178 static void
179 psppire_output_submit (struct output_driver *this,
180                        const struct output_item *item)
181 {
182   struct psppire_output_driver *pod = psppire_output_cast (this);
183   PsppireOutputWindow *viewer;
184   GtkWidget *drawing_area;
185   struct xr_rendering *r;
186   struct string title;
187   GtkTreeStore *store;
188   GtkTreePath *path;
189   GtkTreeIter iter;
190   cairo_t *cr;
191   int tw, th;
192
193   if (pod->viewer == NULL)
194     {
195       pod->viewer = PSPPIRE_OUTPUT_WINDOW (psppire_output_window_new ());
196       gtk_widget_show_all (GTK_WIDGET (pod->viewer));
197       pod->viewer->driver = pod;
198     }
199   viewer = pod->viewer;
200
201   if (viewer->n_items >= viewer->allocated_items)
202     viewer->items = x2nrealloc (viewer->items, &viewer->allocated_items,
203                                 sizeof *viewer->items);
204   viewer->items[viewer->n_items++] = output_item_ref (item);
205
206   if (is_text_item (item))
207     {
208       const struct text_item *text_item = to_text_item (item);
209       enum text_item_type type = text_item_get_type (text_item);
210       const char *text = text_item_get_text (text_item);
211
212       if (type == TEXT_ITEM_COMMAND_CLOSE)
213         {
214           viewer->in_command = false;
215           return;
216         }
217       else if (text[0] == '\0')
218         return;
219     }
220
221   cr = gdk_cairo_create (GTK_WIDGET (pod->viewer)->window);
222   if (pod->xr == NULL)
223     {
224       const GtkStyle *style = gtk_widget_get_style (GTK_WIDGET (viewer));
225       struct string_map options = STRING_MAP_INITIALIZER (options);
226       PangoFontDescription *font_desc;
227       char *font_name;
228
229       /* Use GTK+ default font as proportional font. */
230       font_name = pango_font_description_to_string (style->font_desc);
231       string_map_insert (&options, "prop-font", font_name);
232       g_free (font_name);
233
234       /* Derived emphasized font from proportional font. */
235       font_desc = pango_font_description_copy (style->font_desc);
236       pango_font_description_set_style (font_desc, PANGO_STYLE_ITALIC);
237       font_name = pango_font_description_to_string (font_desc);
238       string_map_insert (&options, "emph-font", font_name);
239       g_free (font_name);
240       pango_font_description_free (font_desc);
241
242       /* Pretend that the "page" has a reasonable width and a very big length,
243          so that most tables can be conveniently viewed on-screen with vertical
244          scrolling only.  (The length should not be increased very much because
245          it is already close enough to INT_MAX when expressed as thousands of a
246          point.) */
247       string_map_insert (&options, "paper-size", "300x200000mm");
248       string_map_insert (&options, "headers", "off");
249       string_map_insert (&options, "left-margin", "0");
250       string_map_insert (&options, "right-margin", "0");
251       string_map_insert (&options, "top-margin", "0");
252       string_map_insert (&options, "bottom-margin", "0");
253
254       pod->xr = xr_driver_create (cr, &options);
255
256       string_map_destroy (&options);
257     }
258
259   r = xr_rendering_create (pod->xr, item, cr);
260   if (r == NULL)
261     goto done;
262
263   xr_rendering_measure (r, &tw, &th);
264
265   drawing_area = gtk_drawing_area_new ();
266   gtk_widget_modify_bg (
267     GTK_WIDGET (drawing_area), GTK_STATE_NORMAL,
268     &gtk_widget_get_style (drawing_area)->base[GTK_STATE_NORMAL]);
269   g_object_set_data (G_OBJECT (drawing_area), "rendering", r);
270   gtk_widget_set_size_request (drawing_area, tw, th);
271   gtk_layout_put (pod->viewer->output, drawing_area, 0, pod->viewer->y);
272   gtk_widget_show (drawing_area);
273   g_signal_connect (G_OBJECT (drawing_area), "expose_event",
274                      G_CALLBACK (expose_event_callback), NULL);
275
276   if (!is_text_item (item)
277       || text_item_get_type (to_text_item (item)) != TEXT_ITEM_SYNTAX
278       || !viewer->in_command)
279     {
280       store = GTK_TREE_STORE (gtk_tree_view_get_model (viewer->overview));
281
282       ds_init_empty (&title);
283       if (is_text_item (item)
284           && text_item_get_type (to_text_item (item)) == TEXT_ITEM_COMMAND_OPEN)
285         {
286           gtk_tree_store_append (store, &iter, NULL);
287           viewer->cur_command = iter; /* XXX shouldn't save a GtkTreeIter */
288           viewer->in_command = true;
289         }
290       else
291         {
292           GtkTreeIter *p = viewer->in_command ? &viewer->cur_command : NULL;
293           gtk_tree_store_append (store, &iter, p);
294         }
295
296       ds_clear (&title);
297       if (is_text_item (item))
298         ds_put_cstr (&title, text_item_get_text (to_text_item (item)));
299       else if (is_table_item (item))
300         {
301           const char *caption = table_item_get_caption (to_table_item (item));
302           if (caption != NULL)
303             ds_put_format (&title, "Table: %s", caption);
304           else
305             ds_put_cstr (&title, "Table");
306         }
307       else if (is_chart_item (item))
308         {
309           const char *s = chart_item_get_title (to_chart_item (item));
310           if (s != NULL)
311             ds_put_format (&title, "Chart: %s", s);
312           else
313             ds_put_cstr (&title, "Chart");
314         }
315       gtk_tree_store_set (store, &iter,
316                           COL_TITLE, ds_cstr (&title),
317                           COL_ADDR, item, 
318                           COL_Y, viewer->y,
319                           -1);
320       ds_destroy (&title);
321
322       path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
323       gtk_tree_view_expand_row (viewer->overview, path, TRUE);
324       gtk_tree_path_free (path);
325     }
326
327   if (pod->viewer->max_width < tw)
328     pod->viewer->max_width = tw;
329   pod->viewer->y += th;
330
331   gtk_layout_set_size (pod->viewer->output,
332                        pod->viewer->max_width, pod->viewer->y);
333
334   gtk_window_set_urgency_hint (GTK_WINDOW (pod->viewer), TRUE);
335
336 done:
337   cairo_destroy (cr);
338 }
339
340 static struct output_driver_class psppire_output_class =
341   {
342     "PSPPIRE",                  /* name */
343     NULL,                       /* destroy */
344     psppire_output_submit,      /* submit */
345     NULL,                       /* flush */
346   };
347
348 void
349 psppire_output_window_setup (void)
350 {
351   struct psppire_output_driver *pod;
352   struct output_driver *d;
353
354   pod = xzalloc (sizeof *pod);
355   d = &pod->driver;
356   output_driver_init (d, &psppire_output_class, "PSPPIRE",
357                       SETTINGS_DEVICE_UNFILTERED);
358   output_driver_register (d);
359 }
360 \f
361 int viewer_length = 16;
362 int viewer_width = 59;
363
364 /* Callback for the "delete" action (clicking the x on the top right
365    hand corner of the window) */
366 static gboolean
367 on_delete (GtkWidget *w, GdkEvent *event, gpointer user_data)
368 {
369   PsppireOutputWindow *ow = PSPPIRE_OUTPUT_WINDOW (user_data);
370
371   gtk_widget_destroy (GTK_WIDGET (ow));
372
373   ow->driver->viewer = NULL;
374
375   return FALSE;
376 }
377
378
379
380 static void
381 cancel_urgency (GtkWindow *window,  gpointer data)
382 {
383   gtk_window_set_urgency_hint (window, FALSE);
384 }
385
386 static void
387 on_row_activate (GtkTreeView *overview,
388                  GtkTreePath *path,
389                  GtkTreeViewColumn *column,
390                  PsppireOutputWindow *window)
391 {
392   GtkTreeModel *model;
393   GtkTreeIter iter;
394   GtkAdjustment *vadj;
395   GValue value = {0};
396   double y, min, max;
397
398   model = gtk_tree_view_get_model (overview);
399   if (!gtk_tree_model_get_iter (model, &iter, path))
400     return;
401
402   gtk_tree_model_get_value (model, &iter, COL_Y, &value);
403   y = g_value_get_long (&value);
404   g_value_unset (&value);
405
406   vadj = gtk_layout_get_vadjustment (window->output);
407   min = vadj->lower;
408   max = vadj->upper - vadj->page_size;
409   if (y < min)
410     y = min;
411   else if (y > max)
412     y = max;
413   gtk_adjustment_set_value (vadj, y);
414 }
415
416 static void psppire_output_window_print (PsppireOutputWindow *window);
417
418
419 static void
420 export_output (PsppireOutputWindow *window, struct string_map *options,
421                const char *format)
422 {
423   struct output_driver *driver;
424   size_t i;
425
426   string_map_insert (options, "format", format);
427   driver = output_driver_create (options);
428   if (driver == NULL)
429     return;
430
431   for (i = 0; i < window->n_items; i++)
432     driver->class->submit (driver, window->items[i]);
433   output_driver_destroy (driver);
434 }
435
436
437 struct file_types
438 {
439   const gchar *label;
440   const gchar *ext;
441 };
442
443 enum 
444   {
445     FT_AUTO = 0,
446     FT_PDF,
447     FT_HTML,
448     FT_ODT,
449     FT_TXT,
450     FT_PS,
451     FT_CSV,
452     n_FT
453   };
454
455 #define N_EXTENTIONS (n_FT - 1)
456
457 struct file_types ft[n_FT] = {
458   {N_("Infer file type from extention"),  NULL},
459   {N_("PDF (*.pdf)"),                     ".pdf"},
460   {N_("HTML (*.html)"),                   ".html"},
461   {N_("OpenDocument (*.odt)"),            ".odt"},
462   {N_("Text (*.txt)"),                    ".txt"},
463   {N_("PostScript (*.ps)"),               ".ps"},
464   {N_("Comma-Separated Values (*.csv)"),  ".csv"}
465 };
466
467
468 static void
469 on_combo_change (GtkFileChooser *chooser)
470 {
471   gboolean sensitive = FALSE;
472   GtkWidget *combo = gtk_file_chooser_get_extra_widget (chooser);
473
474   int x = 0; 
475   gchar *fn = gtk_file_chooser_get_filename (chooser);
476
477   if (combo &&  GTK_WIDGET_REALIZED (combo))
478     x = gtk_combo_box_get_active (GTK_COMBO_BOX (combo));
479
480   if (fn == NULL)
481     {
482       sensitive = FALSE;
483     }
484   else
485     {
486       gint i;
487       if ( x != 0 )
488         sensitive = TRUE;
489
490       for (i = 1 ; i < N_EXTENTIONS ; ++i)
491         {
492           if ( g_str_has_suffix (fn, ft[i].ext))
493             {
494               sensitive = TRUE;
495               break;
496             }
497         }
498     }
499
500   g_free (fn);
501
502   gtk_dialog_set_response_sensitive (GTK_DIALOG (chooser), GTK_RESPONSE_ACCEPT, sensitive);
503 }
504
505
506 static void
507 on_file_chooser_change (GObject *w, GParamSpec *pspec, gpointer data)
508 {
509
510   GtkFileChooser *chooser = data;
511   const gchar *name = g_param_spec_get_name (pspec);
512
513   if ( ! GTK_WIDGET_REALIZED (chooser))
514     return;
515
516   /* Ignore this one.  It causes recursion. */
517   if ( 0 == strcmp ("tooltip-text", name))
518     return;
519
520   on_combo_change (chooser);
521 }
522
523
524 /* Recursively descend all the children of W, connecting
525    to their "notify" signal */
526 static void
527 iterate_widgets (GtkWidget *w, gpointer data)
528 {
529   if ( GTK_IS_CONTAINER (w))
530     gtk_container_forall (GTK_CONTAINER (w), iterate_widgets, data);
531   else
532     g_signal_connect (w, "notify",  G_CALLBACK (on_file_chooser_change), data);
533 }
534
535
536
537 static GtkListStore *
538 create_file_type_list (void)
539 {
540   int i;
541   GtkTreeIter iter;
542   GtkListStore *list = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
543   
544   for (i = 0 ; i < 6 ; ++i)
545     {
546       gtk_list_store_append (list, &iter);
547       gtk_list_store_set (list, &iter,
548                           0,  gettext (ft[i].label),
549                           1,  ft[i].ext,
550                           -1);
551     }
552   
553   return list;
554 }
555
556 static void
557 psppire_output_window_export (PsppireOutputWindow *window)
558 {
559   gint response;
560   GtkWidget *combo;
561   GtkListStore *list;
562
563   GtkFileChooser *chooser;
564   
565   GtkWidget *dialog = gtk_file_chooser_dialog_new (_("Export Output"),
566                                         GTK_WINDOW (window),
567                                         GTK_FILE_CHOOSER_ACTION_SAVE,
568                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
569                                         GTK_STOCK_SAVE,   GTK_RESPONSE_ACCEPT,
570                                         NULL);
571
572   chooser = GTK_FILE_CHOOSER (dialog);
573
574   list = create_file_type_list ();
575
576   combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (list));
577
578
579   {
580     /* Create text cell renderer */
581     GtkCellRenderer *cell = gtk_cell_renderer_text_new();
582     gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, FALSE );
583
584     gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo), cell,  "text", 0);
585   }
586
587   g_signal_connect_swapped (combo, "changed", G_CALLBACK (on_combo_change), chooser);
588
589   gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
590
591   gtk_file_chooser_set_extra_widget (chooser, combo);
592
593   /* This kludge is necessary because there is no signal to tell us
594      when the candidate filename of a GtkFileChooser has changed */
595   gtk_container_forall (GTK_CONTAINER (dialog), iterate_widgets, dialog);
596
597
598   gtk_file_chooser_set_do_overwrite_confirmation (chooser, TRUE);
599
600   response = gtk_dialog_run (GTK_DIALOG (dialog));
601
602   if ( response == GTK_RESPONSE_ACCEPT )
603     {
604       int file_type = gtk_combo_box_get_active (GTK_COMBO_BOX (combo));
605       char *filename = gtk_file_chooser_get_filename (chooser);
606       struct string_map options;
607
608       g_return_if_fail (filename);
609
610       if (file_type == FT_AUTO)
611         {
612           gint i;
613           for (i = 1 ; i < N_EXTENTIONS ; ++i)
614             {
615               if ( g_str_has_suffix (filename, ft[i].ext))
616                 {
617                   file_type = i;
618                   break;
619                 }
620             }
621         }
622
623       
624       string_map_init (&options);
625       string_map_insert (&options, "output-file", filename);
626
627       switch (file_type)
628         {
629         case FT_PDF:
630           export_output (window, &options, "pdf");
631           break;
632         case FT_HTML:
633           export_output (window, &options, "html");
634           break;
635         case FT_ODT:
636           export_output (window, &options, "odt");
637           break;
638         case FT_PS:
639           export_output (window, &options, "ps");
640           break;
641         case FT_CSV:
642           export_output (window, &options, "csv");
643           break;
644
645         case FT_TXT:
646           string_map_insert (&options, "headers", "false");
647           string_map_insert (&options, "paginate", "false");
648           string_map_insert (&options, "squeeze", "true");
649           string_map_insert (&options, "emphasis", "none");
650           string_map_insert (&options, "charts", "none");
651           string_map_insert (&options, "top-margin", "0");
652           string_map_insert (&options, "bottom-margin", "0");
653           export_output (window, &options, "txt");
654           break;
655         default:
656           g_assert_not_reached ();
657         }
658
659       string_map_destroy (&options);
660
661       free (filename);
662     }
663
664   gtk_widget_destroy (dialog);
665 }
666
667
668 enum {
669   SELECT_FMT_NULL,
670   SELECT_FMT_TEXT,
671   SELECT_FMT_UTF8,
672   SELECT_FMT_HTML,
673   SELECT_FMT_ODT
674 };
675
676
677 static void
678 insert_glyph (struct string_map *map, const char *opt, gunichar glyph)
679 {
680   char s[6] = {0,0,0,0,0,0};
681
682   g_unichar_to_utf8 (glyph, s);
683   string_map_insert (map, opt, s);
684 }
685
686 struct glyph_pair
687 {
688   gunichar glyph;
689   char opt[10];
690 };
691
692 /* See the table at 
693    http://en.wikipedia.org/wiki/Box-drawing_characters */
694 struct glyph_pair table[] = {
695   {0x2500, "box[1010]"},
696   {0x2501, "box[2020]"},
697   {0x2502, "box[0101]"},
698   {0x2503, "box[0202]"},
699
700   {0x250C, "box[1100]"},
701   {0x250D, "box[2100]"},
702   {0x250E, "box[1200]"},
703   {0x250F, "box[2200]"},
704   {0x2510, "box[0110]"},
705   {0x2511, "box[0110]"},
706   {0x2512, "box[0210]"},
707   {0x2513, "box[0220]"},
708   {0x2514, "box[1001]"},
709   {0x2515, "box[2001]"},
710   {0x2516, "box[1002]"},
711   {0x2517, "box[2002]"},
712   {0x2518, "box[0011]"},
713   {0x2519, "box[0021]"},
714   {0x251A, "box[0012]"},
715   {0x251B, "box[0022]"},
716   {0x251C, "box[1101]"},
717   {0x251D, "box[2101]"},
718   {0x251E, "box[1102]"},
719   {0x251F, "box[1201]"},
720   {0x2520, "box[1202]"},
721   {0x2521, "box[2102]"},
722   {0x2522, "box[2201]"},
723   {0x2523, "box[2202]"},
724   {0x2524, "box[0111]"},
725   {0x2525, "box[0121]"},
726   {0x2526, "box[0112]"},
727   {0x2527, "box[0211]"},
728   {0x2528, "box[0212]"},
729   {0x2529, "box[0122]"},
730   {0x252A, "box[0221]"},
731   {0x252B, "box[0222]"},
732   {0x252C, "box[1110]"},
733   {0x252D, "box[1120]"},
734   {0x252E, "box[2110]"},
735   {0x252F, "box[2120]"},
736   {0x2530, "box[1210]"},
737   {0x2531, "box[1220]"},
738   {0x2532, "box[2210]"},
739   {0x2533, "box[2220]"},
740   {0x2534, "box[1011]"},
741   {0x2535, "box[1021]"},
742   {0x2536, "box[2011]"},
743   {0x2537, "box[2021]"},
744   {0x2538, "box[1012]"},
745   {0x2539, "box[1022]"},
746   {0x253A, "box[2012]"},
747   {0x253B, "box[2022]"},
748   {0x253C, "box[1111]"},
749   {0x253D, "box[1121]"},
750   {0x253E, "box[2111]"},
751   {0x253F, "box[2121]"},
752   {0x2540, "box[1112]"},
753   {0x2541, "box[1211]"},
754   {0x2542, "box[1212]"},
755   {0x2543, "box[1122]"},
756   {0x2544, "box[2112]"},
757   {0x2545, "box[1221]"},
758   {0x2546, "box[2211]"},
759   {0x2547, "box[2122]"},
760   {0x2548, "box[2221]"},
761   {0x2549, "box[1222]"},
762   {0x254A, "box[2212]"},
763   {0x254B, "box[2222]"},
764
765   {0x2574, "box[0010]"},
766   {0x2575, "box[0001]"},
767   {0x2576, "box[1000]"},
768   {0x2577, "box[0100]"},
769   {0x2578, "box[0020]"},
770   {0x2579, "box[0002]"},
771   {0x257A, "box[2000]"},
772   {0x257B, "box[0200]"},
773   {0x257C, "box[2010]"},
774   {0x257D, "box[0201]"},
775   {0x257E, "box[1020]"},
776   {0x257F, "box[0102]"},
777 };
778
779
780 static void
781 utf8_box_chars (struct string_map *map)
782 {
783   int i;
784   for (i = 0; i < sizeof (table) / sizeof (table[0]); ++i)
785     {
786       const struct glyph_pair *p = &table[i];
787       insert_glyph (map, p->opt, p->glyph);
788     }
789 }
790
791
792
793 static void
794 clipboard_get_cb (GtkClipboard     *clipboard,
795                   GtkSelectionData *selection_data,
796                   guint             info,
797                   gpointer          data)
798 {
799   PsppireOutputWindow *window = data;
800
801   gsize length;
802   gchar *text = NULL;
803   struct output_driver *driver = NULL;
804   char *filename = NULL;
805   struct string_map options;
806
807   GtkTreeSelection *sel = gtk_tree_view_get_selection (window->overview);
808   GtkTreeModel *model = gtk_tree_view_get_model (window->overview);
809
810   GList *rows = gtk_tree_selection_get_selected_rows (sel, &model);
811   GList *n = rows;
812
813   if ( n == NULL)
814     return;
815
816   string_map_init (&options);
817   filename = tempnam (NULL, NULL);
818   string_map_insert (&options, "output-file", filename);
819
820   switch (info)
821     {
822     case SELECT_FMT_UTF8:
823       utf8_box_chars (&options);
824       /* fall-through */
825
826     case SELECT_FMT_TEXT:
827       string_map_insert (&options, "format", "txt");
828       break;
829
830     case SELECT_FMT_HTML:
831       string_map_insert (&options, "format", "html");
832       break;
833
834     case SELECT_FMT_ODT:
835       string_map_insert (&options, "format", "odt");
836       break;
837
838     default:
839       g_warning ("unsupported clip target\n");
840       goto finish;
841       break;
842     }
843
844   driver = output_driver_create (&options);
845   if (driver == NULL)
846     goto finish;
847
848   while (n)
849     {
850       GtkTreePath *path = n->data ; 
851       GtkTreeIter iter;
852       struct output_item *item ;
853
854       gtk_tree_model_get_iter (model, &iter, path);
855       gtk_tree_model_get (model, &iter, COL_ADDR, &item, -1);
856
857       driver->class->submit (driver, item);
858
859       n = n->next;
860     }
861
862   if ( driver->class->flush)
863     driver->class->flush (driver);
864
865
866   /* Some drivers (eg: the odt one) don't write anything until they
867      are closed */
868   output_driver_destroy (driver);
869   driver = NULL;
870
871   if ( g_file_get_contents (filename, &text, &length, NULL) )
872     {
873       gtk_selection_data_set (selection_data, selection_data->target,
874                               8,
875                               (const guchar *) text, length);
876     }
877
878  finish:
879
880   if (driver != NULL)
881     output_driver_destroy (driver);
882
883   g_free (text);
884
885   unlink (filename);
886   free (filename);
887
888   g_list_free (rows);
889 }
890
891 static void
892 clipboard_clear_cb (GtkClipboard *clipboard,
893                     gpointer data)
894 {
895 }
896
897 static const GtkTargetEntry targets[] = {
898
899   { "STRING",        0, SELECT_FMT_TEXT },
900   { "TEXT",          0, SELECT_FMT_TEXT },
901   { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
902   { "text/plain",    0, SELECT_FMT_TEXT },
903
904   { "UTF8_STRING",   0, SELECT_FMT_UTF8 },
905   { "text/plain;charset=utf-8", 0, SELECT_FMT_UTF8 },
906
907   { "text/html",     0, SELECT_FMT_HTML },
908
909   { "application/vnd.oasis.opendocument.text", 0, SELECT_FMT_ODT }
910 };
911
912 static void
913 on_copy (PsppireOutputWindow *window)
914 {
915   {
916     GtkClipboard *clipboard =
917       gtk_widget_get_clipboard (GTK_WIDGET (window),
918                                 GDK_SELECTION_CLIPBOARD);
919
920     if (!gtk_clipboard_set_with_data (clipboard, targets,
921                                        G_N_ELEMENTS (targets),
922                                        clipboard_get_cb, clipboard_clear_cb,
923                                       window))
924
925       clipboard_clear_cb (clipboard, window);
926   }
927 }
928
929 static void
930 on_selection_change (GtkTreeSelection *sel, GtkAction *copy_action)
931 {
932   /* The Copy action is available only if there is something selected */
933   gtk_action_set_sensitive (copy_action, gtk_tree_selection_count_selected_rows (sel) > 0);
934 }
935
936 static void
937 on_select_all (PsppireOutputWindow *window)
938 {
939   GtkTreeSelection *sel = gtk_tree_view_get_selection (window->overview);
940   gtk_tree_view_expand_all (window->overview);
941   gtk_tree_selection_select_all (sel);
942 }
943
944
945 static void
946 psppire_output_window_init (PsppireOutputWindow *window)
947 {
948   GtkTreeViewColumn *column;
949   GtkCellRenderer *renderer;
950   GtkBuilder *xml;
951   GtkAction *copy_action;
952   GtkAction *select_all_action;
953   GtkTreeSelection *sel;
954
955   xml = builder_new ("output-viewer.ui");
956
957   copy_action = get_action_assert (xml, "edit_copy");
958   select_all_action = get_action_assert (xml, "edit_select-all");
959
960   gtk_action_set_sensitive (copy_action, FALSE);
961
962   g_signal_connect_swapped (copy_action, "activate", G_CALLBACK (on_copy), window);
963
964   g_signal_connect_swapped (select_all_action, "activate", G_CALLBACK (on_select_all), window);
965
966   gtk_widget_reparent (get_widget_assert (xml, "vbox1"), GTK_WIDGET (window));
967
968   window->output = GTK_LAYOUT (get_widget_assert (xml, "output"));
969   window->y = 0;
970
971   window->overview = GTK_TREE_VIEW (get_widget_assert (xml, "overview"));
972
973   sel = gtk_tree_view_get_selection (window->overview);
974
975   gtk_tree_selection_set_mode (sel, GTK_SELECTION_MULTIPLE);
976
977   g_signal_connect (sel, "changed", G_CALLBACK (on_selection_change), copy_action);
978
979   gtk_tree_view_set_model (window->overview,
980                            GTK_TREE_MODEL (gtk_tree_store_new (
981                                              N_COLS,
982                                              G_TYPE_STRING,  /* COL_TITLE */
983                                              G_TYPE_POINTER, /* COL_ADDR */
984                                              G_TYPE_LONG))); /* COL_Y */
985
986   window->in_command = false;
987
988   window->items = NULL;
989   window->n_items = window->allocated_items = 0;
990
991   column = gtk_tree_view_column_new ();
992   gtk_tree_view_append_column (GTK_TREE_VIEW (window->overview), column);
993   renderer = gtk_cell_renderer_text_new ();
994   gtk_tree_view_column_pack_start (column, renderer, TRUE);
995   gtk_tree_view_column_add_attribute (column, renderer, "text", COL_TITLE);
996
997   g_signal_connect (GTK_TREE_VIEW (window->overview),
998                     "row-activated", G_CALLBACK (on_row_activate), window);
999
1000   gtk_widget_modify_bg (GTK_WIDGET (window->output), GTK_STATE_NORMAL,
1001                         &gtk_widget_get_style (GTK_WIDGET (window->output))->base[GTK_STATE_NORMAL]);
1002
1003   connect_help (xml);
1004
1005   g_signal_connect (window,
1006                     "focus-in-event",
1007                     G_CALLBACK (cancel_urgency),
1008                     NULL);
1009
1010   g_signal_connect (get_action_assert (xml,"windows_minimise-all"),
1011                     "activate",
1012                     G_CALLBACK (psppire_window_minimise_all),
1013                     NULL);
1014
1015   {
1016     GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
1017     merge_help_menu (uim);
1018
1019     PSPPIRE_WINDOW (window)->menu =
1020       GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows_menuitem/windows_minimise-all")->parent);
1021   }
1022
1023   g_signal_connect_swapped (get_action_assert (xml, "file_export"), "activate",
1024                             G_CALLBACK (psppire_output_window_export), window);
1025
1026
1027   g_signal_connect_swapped (get_action_assert (xml, "file_print"), "activate",
1028                             G_CALLBACK (psppire_output_window_print), window);
1029
1030   g_object_unref (xml);
1031
1032   g_signal_connect (window, "delete-event",
1033                     G_CALLBACK (on_delete), window);
1034 }
1035
1036
1037 GtkWidget*
1038 psppire_output_window_new (void)
1039 {
1040   return GTK_WIDGET (g_object_new (psppire_output_window_get_type (),
1041                                    "filename", "Output",
1042                                    "description", _("Output Viewer"),
1043                                    NULL));
1044 }
1045
1046
1047 \f
1048 static void
1049 create_xr_print_driver (GtkPrintContext *context, PsppireOutputWindow *window)
1050 {
1051   struct string_map options;
1052   GtkPageSetup *page_setup;
1053   double width, height;
1054   double left_margin;
1055   double right_margin;
1056   double top_margin;
1057   double bottom_margin;
1058
1059   page_setup = gtk_print_context_get_page_setup (context);
1060   width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM);
1061   height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_MM);
1062   left_margin = gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM);
1063   right_margin = gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_MM);
1064   top_margin = gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM);
1065   bottom_margin = gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_MM);
1066
1067   string_map_init (&options);
1068   string_map_insert_nocopy (&options, xstrdup ("paper-size"),
1069                             xasprintf("%.2fx%.2fmm", width, height));
1070   string_map_insert_nocopy (&options, xstrdup ("left-margin"),
1071                             xasprintf ("%.2fmm", left_margin));
1072   string_map_insert_nocopy (&options, xstrdup ("right-margin"),
1073                             xasprintf ("%.2fmm", right_margin));
1074   string_map_insert_nocopy (&options, xstrdup ("top-margin"),
1075                             xasprintf ("%.2fmm", top_margin));
1076   string_map_insert_nocopy (&options, xstrdup ("bottom-margin"),
1077                             xasprintf ("%.2fmm", bottom_margin));
1078
1079   window->print_xrd =
1080     xr_driver_create (gtk_print_context_get_cairo_context (context), &options);
1081
1082   string_map_destroy (&options);
1083 }
1084
1085 static gboolean
1086 paginate (GtkPrintOperation *operation,
1087           GtkPrintContext   *context,
1088           PsppireOutputWindow *window)
1089 {
1090   if ( window->print_item < window->n_items )
1091     {
1092       xr_driver_output_item (window->print_xrd, window->items[window->print_item++]);
1093       if (xr_driver_need_new_page (window->print_xrd))
1094         {
1095           xr_driver_next_page (window->print_xrd, NULL);
1096           window->print_n_pages ++;
1097         }
1098       return FALSE;
1099     }
1100   else
1101     {
1102       gtk_print_operation_set_n_pages (operation, window->print_n_pages);
1103       window->print_item = 0;
1104       create_xr_print_driver (context, window);
1105       return TRUE;
1106     }
1107 }
1108
1109 static void
1110 begin_print (GtkPrintOperation *operation,
1111              GtkPrintContext   *context,
1112              PsppireOutputWindow *window)
1113 {
1114   create_xr_print_driver (context, window);
1115
1116   window->print_item = 0;
1117   window->print_n_pages = 1;
1118 }
1119
1120 static void
1121 end_print (GtkPrintOperation *operation,
1122            GtkPrintContext   *context,
1123            PsppireOutputWindow *window)
1124 {
1125   xr_driver_destroy (window->print_xrd);
1126 }
1127
1128
1129 static void
1130 draw_page (GtkPrintOperation *operation,
1131            GtkPrintContext   *context,
1132            gint               page_number,
1133            PsppireOutputWindow *window)
1134 {
1135   xr_driver_next_page (window->print_xrd, gtk_print_context_get_cairo_context (context));
1136   while ( window->print_item < window->n_items)
1137     {
1138       xr_driver_output_item (window->print_xrd, window->items [window->print_item++]);
1139       if ( xr_driver_need_new_page (window->print_xrd) )
1140           break;          
1141     }
1142 }
1143
1144
1145 static void
1146 psppire_output_window_print (PsppireOutputWindow *window)
1147 {
1148   GtkPrintOperationResult res;
1149
1150   GtkPrintOperation *print = gtk_print_operation_new ();
1151
1152   if (window->print_settings != NULL) 
1153     gtk_print_operation_set_print_settings (print, window->print_settings);
1154
1155   g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
1156   g_signal_connect (print, "end_print", G_CALLBACK (end_print),     window);
1157   g_signal_connect (print, "paginate", G_CALLBACK (paginate),       window);
1158   g_signal_connect (print, "draw_page", G_CALLBACK (draw_page),     window);
1159
1160   res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1161                                  GTK_WINDOW (window), NULL);
1162
1163   if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
1164     {
1165       if (window->print_settings != NULL)
1166         g_object_unref (window->print_settings);
1167       window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
1168       
1169     }
1170
1171   g_object_unref (print);
1172 }