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