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