Added remaining relevant unicode box characters
[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);
172   cairo_destroy (cr);
173
174   return TRUE;
175 }
176
177 static void
178 psppire_output_submit (struct output_driver *this,
179                        const struct output_item *item)
180 {
181   struct psppire_output_driver *pod = psppire_output_cast (this);
182   PsppireOutputWindow *viewer;
183   GtkWidget *drawing_area;
184   struct xr_rendering *r;
185   struct string title;
186   GtkTreeStore *store;
187   GtkTreePath *path;
188   GtkTreeIter iter;
189   cairo_t *cr;
190   int tw, th;
191
192   if (pod->viewer == NULL)
193     {
194       pod->viewer = PSPPIRE_OUTPUT_WINDOW (psppire_output_window_new ());
195       gtk_widget_show_all (GTK_WIDGET (pod->viewer));
196       pod->viewer->driver = pod;
197     }
198   viewer = pod->viewer;
199
200   if (viewer->n_items >= viewer->allocated_items)
201     viewer->items = x2nrealloc (viewer->items, &viewer->allocated_items,
202                                 sizeof *viewer->items);
203   viewer->items[viewer->n_items++] = output_item_ref (item);
204
205   if (is_text_item (item))
206     {
207       const struct text_item *text_item = to_text_item (item);
208       enum text_item_type type = text_item_get_type (text_item);
209       const char *text = text_item_get_text (text_item);
210
211       if (type == TEXT_ITEM_COMMAND_CLOSE)
212         {
213           viewer->in_command = false;
214           return;
215         }
216       else if (text[0] == '\0')
217         return;
218     }
219
220   cr = gdk_cairo_create (GTK_WIDGET (pod->viewer)->window);
221   if (pod->xr == NULL)
222     {
223       const GtkStyle *style = gtk_widget_get_style (GTK_WIDGET (viewer));
224       struct string_map options = STRING_MAP_INITIALIZER (options);
225       PangoFontDescription *font_desc;
226       char *font_name;
227
228       /* Use GTK+ default font as proportional font. */
229       font_name = pango_font_description_to_string (style->font_desc);
230       string_map_insert (&options, "prop-font", font_name);
231       g_free (font_name);
232
233       /* Derived emphasized font from proportional font. */
234       font_desc = pango_font_description_copy (style->font_desc);
235       pango_font_description_set_style (font_desc, PANGO_STYLE_ITALIC);
236       font_name = pango_font_description_to_string (font_desc);
237       string_map_insert (&options, "emph-font", font_name);
238       g_free (font_name);
239       pango_font_description_free (font_desc);
240
241       /* Pretend that the "page" has a reasonable width and a very big length,
242          so that most tables can be conveniently viewed on-screen with vertical
243          scrolling only.  (The length should not be increased very much because
244          it is already close enough to INT_MAX when expressed as thousands of a
245          point.) */
246       string_map_insert (&options, "paper-size", "300x200000mm");
247       string_map_insert (&options, "headers", "off");
248       string_map_insert (&options, "left-margin", "0");
249       string_map_insert (&options, "right-margin", "0");
250       string_map_insert (&options, "top-margin", "0");
251       string_map_insert (&options, "bottom-margin", "0");
252
253       pod->xr = xr_driver_create (cr, &options);
254
255       string_map_destroy (&options);
256     }
257
258   r = xr_rendering_create (pod->xr, item, cr);
259   if (r == NULL)
260     goto done;
261
262   xr_rendering_measure (r, &tw, &th);
263
264   drawing_area = gtk_drawing_area_new ();
265   gtk_widget_modify_bg (
266     GTK_WIDGET (drawing_area), GTK_STATE_NORMAL,
267     &gtk_widget_get_style (drawing_area)->base[GTK_STATE_NORMAL]);
268   g_object_set_data (G_OBJECT (drawing_area), "rendering", r);
269   gtk_widget_set_size_request (drawing_area, tw, th);
270   gtk_layout_put (pod->viewer->output, drawing_area, 0, pod->viewer->y);
271   gtk_widget_show (drawing_area);
272   g_signal_connect (G_OBJECT (drawing_area), "expose_event",
273                      G_CALLBACK (expose_event_callback), NULL);
274
275   if (!is_text_item (item)
276       || text_item_get_type (to_text_item (item)) != TEXT_ITEM_SYNTAX
277       || !viewer->in_command)
278     {
279       store = GTK_TREE_STORE (gtk_tree_view_get_model (viewer->overview));
280
281       ds_init_empty (&title);
282       if (is_text_item (item)
283           && text_item_get_type (to_text_item (item)) == TEXT_ITEM_COMMAND_OPEN)
284         {
285           gtk_tree_store_append (store, &iter, NULL);
286           viewer->cur_command = iter; /* XXX shouldn't save a GtkTreeIter */
287           viewer->in_command = true;
288         }
289       else
290         {
291           GtkTreeIter *p = viewer->in_command ? &viewer->cur_command : NULL;
292           gtk_tree_store_append (store, &iter, p);
293         }
294
295       ds_clear (&title);
296       if (is_text_item (item))
297         ds_put_cstr (&title, text_item_get_text (to_text_item (item)));
298       else if (is_table_item (item))
299         {
300           const char *caption = table_item_get_caption (to_table_item (item));
301           if (caption != NULL)
302             ds_put_format (&title, "Table: %s", caption);
303           else
304             ds_put_cstr (&title, "Table");
305         }
306       else if (is_chart_item (item))
307         {
308           const char *s = chart_item_get_title (to_chart_item (item));
309           if (s != NULL)
310             ds_put_format (&title, "Chart: %s", s);
311           else
312             ds_put_cstr (&title, "Chart");
313         }
314       gtk_tree_store_set (store, &iter,
315                           COL_TITLE, ds_cstr (&title),
316                           COL_ADDR, item, 
317                           COL_Y, viewer->y,
318                           -1);
319       ds_destroy (&title);
320
321       path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
322       gtk_tree_view_expand_row (viewer->overview, path, TRUE);
323       gtk_tree_path_free (path);
324     }
325
326   if (pod->viewer->max_width < tw)
327     pod->viewer->max_width = tw;
328   pod->viewer->y += th;
329
330   gtk_layout_set_size (pod->viewer->output,
331                        pod->viewer->max_width, pod->viewer->y);
332
333   gtk_window_set_urgency_hint (GTK_WINDOW (pod->viewer), TRUE);
334
335 done:
336   cairo_destroy (cr);
337 }
338
339 static struct output_driver_class psppire_output_class =
340   {
341     "PSPPIRE",                  /* name */
342     NULL,                       /* destroy */
343     psppire_output_submit,      /* submit */
344     NULL,                       /* flush */
345   };
346
347 void
348 psppire_output_window_setup (void)
349 {
350   struct psppire_output_driver *pod;
351   struct output_driver *d;
352
353   pod = xzalloc (sizeof *pod);
354   d = &pod->driver;
355   output_driver_init (d, &psppire_output_class, "PSPPIRE",
356                       SETTINGS_DEVICE_UNFILTERED);
357   output_driver_register (d);
358 }
359 \f
360 int viewer_length = 16;
361 int viewer_width = 59;
362
363 /* Callback for the "delete" action (clicking the x on the top right
364    hand corner of the window) */
365 static gboolean
366 on_delete (GtkWidget *w, GdkEvent *event, gpointer user_data)
367 {
368   PsppireOutputWindow *ow = PSPPIRE_OUTPUT_WINDOW (user_data);
369
370   gtk_widget_destroy (GTK_WIDGET (ow));
371
372   ow->driver->viewer = NULL;
373
374   return FALSE;
375 }
376
377
378
379 static void
380 cancel_urgency (GtkWindow *window,  gpointer data)
381 {
382   gtk_window_set_urgency_hint (window, FALSE);
383 }
384
385 static void
386 on_row_activate (GtkTreeView *overview,
387                  GtkTreePath *path,
388                  GtkTreeViewColumn *column,
389                  PsppireOutputWindow *window)
390 {
391   GtkTreeModel *model;
392   GtkTreeIter iter;
393   GtkAdjustment *vadj;
394   GValue value = {0};
395   double y, min, max;
396
397   model = gtk_tree_view_get_model (overview);
398   if (!gtk_tree_model_get_iter (model, &iter, path))
399     return;
400
401   gtk_tree_model_get_value (model, &iter, COL_Y, &value);
402   y = g_value_get_long (&value);
403   g_value_unset (&value);
404
405   vadj = gtk_layout_get_vadjustment (window->output);
406   min = vadj->lower;
407   max = vadj->upper - vadj->page_size;
408   if (y < min)
409     y = min;
410   else if (y > max)
411     y = max;
412   gtk_adjustment_set_value (vadj, y);
413 }
414
415 static void psppire_output_window_print (PsppireOutputWindow *window);
416
417
418 static GtkFileFilter *
419 add_filter (GtkFileChooser *chooser, const char *name, const char *pattern)
420 {
421   GtkFileFilter *filter = gtk_file_filter_new ();
422   g_object_ref_sink (G_OBJECT (filter));
423   gtk_file_filter_set_name (filter, name);
424   gtk_file_filter_add_pattern (filter, pattern);
425   gtk_file_chooser_add_filter (chooser, filter);
426   return filter;
427 }
428
429 static void
430 export_output (PsppireOutputWindow *window, struct string_map *options,
431                const char *format)
432 {
433   struct output_driver *driver;
434   size_t i;
435
436   string_map_insert (options, "format", format);
437   driver = output_driver_create (options);
438   if (driver == NULL)
439     return;
440
441   for (i = 0; i < window->n_items; i++)
442     driver->class->submit (driver, window->items[i]);
443   output_driver_destroy (driver);
444 }
445
446 static void
447 psppire_output_window_export (PsppireOutputWindow *window)
448 {
449   gint response;
450
451   GtkFileFilter *pdf_filter;
452   GtkFileFilter *html_filter;
453   GtkFileFilter *odt_filter;
454   GtkFileFilter *txt_filter;
455   GtkFileFilter *ps_filter;
456   GtkFileFilter *csv_filter;
457   GtkFileChooser *chooser;
458   GtkWidget *dialog;
459
460   dialog = gtk_file_chooser_dialog_new (_("Export Output"),
461                                         GTK_WINDOW (window),
462                                         GTK_FILE_CHOOSER_ACTION_SAVE,
463                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
464                                         GTK_STOCK_SAVE,   GTK_RESPONSE_ACCEPT,
465                                         NULL);
466   chooser = GTK_FILE_CHOOSER (dialog);
467
468   pdf_filter = add_filter (chooser, _("PDF Files (*.pdf)"), "*.pdf");
469   html_filter = add_filter (chooser, _("HTML Files (*.html)"), "*.html");
470   odt_filter = add_filter (chooser, _("OpenDocument Files (*.odt)"), "*.odt");
471   txt_filter = add_filter (chooser, _("Text Files (*.txt)"), "*.txt");
472   ps_filter = add_filter (chooser, _("PostScript Files (*.ps)"), "*.ps");
473   csv_filter = add_filter (chooser, _("Comma-Separated Value Files (*.csv)"),
474                            "*.csv");
475
476   gtk_file_chooser_set_do_overwrite_confirmation (chooser, TRUE);
477   gtk_file_chooser_set_filter (chooser, pdf_filter);
478
479   response = gtk_dialog_run (GTK_DIALOG (dialog));
480
481   if ( response == GTK_RESPONSE_ACCEPT )
482     {
483       char *filename = gtk_file_chooser_get_filename (chooser);
484       GtkFileFilter *filter = gtk_file_chooser_get_filter (chooser);
485       struct string_map options;
486
487       g_return_if_fail (filename);
488       g_return_if_fail (filter);
489
490       string_map_init (&options);
491       string_map_insert (&options, "output-file", filename);
492       if (filter == pdf_filter)
493         {
494           export_output (window, &options, "pdf");
495         }
496       else if (filter == html_filter)
497         export_output (window, &options, "html");
498       else if (filter == odt_filter)
499         export_output (window, &options, "odt");
500       else if (filter == txt_filter)
501         {
502           string_map_insert (&options, "headers", "false");
503           string_map_insert (&options, "paginate", "false");
504           string_map_insert (&options, "squeeze", "true");
505           string_map_insert (&options, "emphasis", "none");
506           string_map_insert (&options, "charts", "none");
507           string_map_insert (&options, "top-margin", "0");
508           string_map_insert (&options, "bottom-margin", "0");
509           export_output (window, &options, "txt");
510         }
511       else if (filter == ps_filter)
512         export_output (window, &options, "ps");
513       else if (filter == csv_filter)
514         export_output (window, &options, "csv");
515       else
516         g_return_if_reached ();
517
518       free (filename);
519     }
520
521   g_object_unref (G_OBJECT (pdf_filter));
522   g_object_unref (G_OBJECT (html_filter));
523   g_object_unref (G_OBJECT (txt_filter));
524   g_object_unref (G_OBJECT (ps_filter));
525   g_object_unref (G_OBJECT (csv_filter));
526
527   gtk_widget_destroy (dialog);
528 }
529
530
531 enum {
532   SELECT_FMT_NULL,
533   SELECT_FMT_TEXT,
534   SELECT_FMT_UTF8,
535   SELECT_FMT_HTML,
536   SELECT_FMT_ODT
537 };
538
539
540 static void
541 insert_glyph (struct string_map *map, const char *opt, gunichar glyph)
542 {
543   char s[6] = {0,0,0,0,0,0};
544
545   g_unichar_to_utf8 (glyph, s);
546   string_map_insert (map, opt, s);
547 }
548
549 struct glyph_pair
550 {
551   gunichar glyph;
552   char opt[10];
553 };
554
555 /* See the table at 
556    http://en.wikipedia.org/wiki/Box-drawing_characters */
557 struct glyph_pair table[] = {
558   {0x2500, "box[1010]"},
559   {0x2501, "box[2020]"},
560   {0x2502, "box[0101]"},
561   {0x2503, "box[0202]"},
562
563   {0x250C, "box[1100]"},
564   {0x250D, "box[2100]"},
565   {0x250E, "box[1200]"},
566   {0x250F, "box[2200]"},
567   {0x2510, "box[0110]"},
568   {0x2511, "box[0110]"},
569   {0x2512, "box[0210]"},
570   {0x2513, "box[0220]"},
571   {0x2514, "box[1001]"},
572   {0x2515, "box[2001]"},
573   {0x2516, "box[1002]"},
574   {0x2517, "box[2002]"},
575   {0x2518, "box[0011]"},
576   {0x2519, "box[0021]"},
577   {0x251A, "box[0012]"},
578   {0x251B, "box[0022]"},
579   {0x251C, "box[1101]"},
580   {0x251D, "box[2101]"},
581   {0x251E, "box[1102]"},
582   {0x251F, "box[1201]"},
583   {0x2520, "box[1202]"},
584   {0x2521, "box[2102]"},
585   {0x2522, "box[2201]"},
586   {0x2523, "box[2202]"},
587   {0x2524, "box[0111]"},
588   {0x2525, "box[0121]"},
589   {0x2526, "box[0112]"},
590   {0x2527, "box[0211]"},
591   {0x2528, "box[0212]"},
592   {0x2529, "box[0122]"},
593   {0x252A, "box[0221]"},
594   {0x252B, "box[0222]"},
595   {0x252C, "box[1110]"},
596   {0x252D, "box[1120]"},
597   {0x252E, "box[2110]"},
598   {0x252F, "box[2120]"},
599   {0x2530, "box[1210]"},
600   {0x2531, "box[1220]"},
601   {0x2532, "box[2210]"},
602   {0x2533, "box[2220]"},
603   {0x2534, "box[1011]"},
604   {0x2535, "box[1021]"},
605   {0x2536, "box[2011]"},
606   {0x2537, "box[2021]"},
607   {0x2538, "box[1012]"},
608   {0x2539, "box[1022]"},
609   {0x253A, "box[2012]"},
610   {0x253B, "box[2022]"},
611   {0x253C, "box[1111]"},
612   {0x253D, "box[1121]"},
613   {0x253E, "box[2111]"},
614   {0x253F, "box[2121]"},
615   {0x2540, "box[1112]"},
616   {0x2541, "box[1211]"},
617   {0x2542, "box[1212]"},
618   {0x2543, "box[1122]"},
619   {0x2544, "box[2112]"},
620   {0x2545, "box[1221]"},
621   {0x2546, "box[2211]"},
622   {0x2547, "box[2122]"},
623   {0x2548, "box[2221]"},
624   {0x2549, "box[1222]"},
625   {0x254A, "box[2212]"},
626   {0x254B, "box[2222]"},
627
628   {0x2574, "box[0010]"},
629   {0x2575, "box[0001]"},
630   {0x2576, "box[1000]"},
631   {0x2577, "box[0100]"},
632   {0x2578, "box[0020]"},
633   {0x2579, "box[0002]"},
634   {0x257A, "box[2000]"},
635   {0x257B, "box[0200]"},
636   {0x257C, "box[2010]"},
637   {0x257D, "box[0201]"},
638   {0x257E, "box[1020]"},
639   {0x257F, "box[0102]"},
640 };
641
642
643 static void
644 utf8_box_chars (struct string_map *map)
645 {
646   int i;
647   for (i = 0; i < sizeof (table) / sizeof (table[0]); ++i)
648     {
649       const struct glyph_pair *p = &table[i];
650       insert_glyph (map, p->opt, p->glyph);
651     }
652 }
653
654
655
656 static void
657 clipboard_get_cb (GtkClipboard     *clipboard,
658                   GtkSelectionData *selection_data,
659                   guint             info,
660                   gpointer          data)
661 {
662   PsppireOutputWindow *window = data;
663
664   gsize length;
665   gchar *text = NULL;
666   struct output_driver *driver = NULL;
667   char *filename = NULL;
668   struct string_map options;
669
670   GtkTreeSelection *sel = gtk_tree_view_get_selection (window->overview);
671   GtkTreeModel *model = gtk_tree_view_get_model (window->overview);
672
673   GList *rows = gtk_tree_selection_get_selected_rows (sel, &model);
674   GList *n = rows;
675
676   if ( n == NULL)
677     return;
678
679   string_map_init (&options);
680   filename = tempnam (NULL, NULL);
681   string_map_insert (&options, "output-file", filename);
682
683   switch (info)
684     {
685     case SELECT_FMT_UTF8:
686       utf8_box_chars (&options);
687       /* fall-through */
688
689     case SELECT_FMT_TEXT:
690       string_map_insert (&options, "format", "txt");
691       break;
692
693     case SELECT_FMT_HTML:
694       string_map_insert (&options, "format", "html");
695       break;
696
697     case SELECT_FMT_ODT:
698       string_map_insert (&options, "format", "odt");
699       break;
700
701     default:
702       g_print ("unsupportted clip target\n");
703       goto finish;
704       break;
705     }
706
707   driver = output_driver_create (&options);
708   if (driver == NULL)
709     goto finish;
710
711   while (n)
712     {
713       GtkTreePath *path = n->data ; 
714       GtkTreeIter iter;
715       struct output_item *item ;
716
717       gtk_tree_model_get_iter (model, &iter, path);
718       gtk_tree_model_get (model, &iter, COL_ADDR, &item, -1);
719
720       driver->class->submit (driver, item);
721
722       n = n->next;
723     }
724
725   if ( driver->class->flush)
726     driver->class->flush (driver);
727
728
729   /* Some drivers (eg: the odt one) don't write anything until they
730      are closed */
731   output_driver_destroy (driver);
732   driver = NULL;
733
734   if ( g_file_get_contents (filename, &text, &length, NULL) )
735     {
736       gtk_selection_data_set (selection_data, selection_data->target,
737                               8,
738                               (const guchar *) text, length);
739     }
740
741  finish:
742
743   if (driver != NULL)
744     output_driver_destroy (driver);
745
746   g_free (text);
747
748   unlink (filename);
749   free (filename);
750
751   g_list_free (rows);
752 }
753
754 static void
755 clipboard_clear_cb (GtkClipboard *clipboard,
756                     gpointer data)
757 {
758 }
759
760 static const GtkTargetEntry targets[] = {
761
762   { "STRING",        0, SELECT_FMT_TEXT },
763   { "TEXT",          0, SELECT_FMT_TEXT },
764   { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
765   { "text/plain",    0, SELECT_FMT_TEXT },
766
767   { "UTF8_STRING",   0, SELECT_FMT_UTF8 },
768   { "text/plain;charset=utf-8", 0, SELECT_FMT_UTF8 },
769
770   { "text/html",     0, SELECT_FMT_HTML },
771
772   { "application/vnd.oasis.opendocument.text", 0, SELECT_FMT_ODT }
773 };
774
775 static void
776 on_copy (PsppireOutputWindow *window)
777 {
778   {
779     GtkClipboard *clipboard =
780       gtk_widget_get_clipboard (GTK_WIDGET (window),
781                                 GDK_SELECTION_CLIPBOARD);
782
783     if (!gtk_clipboard_set_with_data (clipboard, targets,
784                                        G_N_ELEMENTS (targets),
785                                        clipboard_get_cb, clipboard_clear_cb,
786                                       window))
787
788       clipboard_clear_cb (clipboard, window);
789   }
790 }
791
792 static void
793 on_selection_change (GtkTreeSelection *sel, GtkAction *copy_action)
794 {
795   /* The Copy action is available only if there is something selected */
796   gtk_action_set_sensitive (copy_action, gtk_tree_selection_count_selected_rows (sel) > 0);
797 }
798
799 static void
800 on_select_all (PsppireOutputWindow *window)
801 {
802   GtkTreeSelection *sel = gtk_tree_view_get_selection (window->overview);
803   gtk_tree_view_expand_all (window->overview);
804   gtk_tree_selection_select_all (sel);
805 }
806
807
808 static void
809 psppire_output_window_init (PsppireOutputWindow *window)
810 {
811   GtkTreeViewColumn *column;
812   GtkCellRenderer *renderer;
813   GtkBuilder *xml;
814   GtkAction *copy_action;
815   GtkAction *select_all_action;
816   GtkTreeSelection *sel;
817
818   xml = builder_new ("output-viewer.ui");
819
820   copy_action = get_action_assert (xml, "edit_copy");
821   select_all_action = get_action_assert (xml, "edit_select-all");
822
823   gtk_action_set_sensitive (copy_action, FALSE);
824
825   g_signal_connect_swapped (copy_action, "activate", G_CALLBACK (on_copy), window);
826
827   g_signal_connect_swapped (select_all_action, "activate", G_CALLBACK (on_select_all), window);
828
829   gtk_widget_reparent (get_widget_assert (xml, "vbox1"), GTK_WIDGET (window));
830
831   window->output = GTK_LAYOUT (get_widget_assert (xml, "output"));
832   window->y = 0;
833
834   window->overview = GTK_TREE_VIEW (get_widget_assert (xml, "overview"));
835
836   sel = gtk_tree_view_get_selection (window->overview);
837
838   gtk_tree_selection_set_mode (sel, GTK_SELECTION_MULTIPLE);
839
840   g_signal_connect (sel, "changed", G_CALLBACK (on_selection_change), copy_action);
841
842   gtk_tree_view_set_model (window->overview,
843                            GTK_TREE_MODEL (gtk_tree_store_new (
844                                              N_COLS,
845                                              G_TYPE_STRING,  /* COL_TITLE */
846                                              G_TYPE_POINTER, /* COL_ADDR */
847                                              G_TYPE_LONG))); /* COL_Y */
848
849   window->in_command = false;
850
851   window->items = NULL;
852   window->n_items = window->allocated_items = 0;
853
854   column = gtk_tree_view_column_new ();
855   gtk_tree_view_append_column (GTK_TREE_VIEW (window->overview), column);
856   renderer = gtk_cell_renderer_text_new ();
857   gtk_tree_view_column_pack_start (column, renderer, TRUE);
858   gtk_tree_view_column_add_attribute (column, renderer, "text", COL_TITLE);
859
860   g_signal_connect (GTK_TREE_VIEW (window->overview),
861                     "row-activated", G_CALLBACK (on_row_activate), window);
862
863   gtk_widget_modify_bg (GTK_WIDGET (window->output), GTK_STATE_NORMAL,
864                         &gtk_widget_get_style (GTK_WIDGET (window->output))->base[GTK_STATE_NORMAL]);
865
866   connect_help (xml);
867
868   g_signal_connect (window,
869                     "focus-in-event",
870                     G_CALLBACK (cancel_urgency),
871                     NULL);
872
873   g_signal_connect (get_action_assert (xml,"windows_minimise-all"),
874                     "activate",
875                     G_CALLBACK (psppire_window_minimise_all),
876                     NULL);
877
878   {
879     GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
880     merge_help_menu (uim);
881
882     PSPPIRE_WINDOW (window)->menu =
883       GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows_menuitem/windows_minimise-all")->parent);
884   }
885
886   g_signal_connect_swapped (get_action_assert (xml, "file_export"), "activate",
887                             G_CALLBACK (psppire_output_window_export), window);
888
889
890   g_signal_connect_swapped (get_action_assert (xml, "file_print"), "activate",
891                             G_CALLBACK (psppire_output_window_print), window);
892
893   g_object_unref (xml);
894
895   g_signal_connect (window, "delete-event",
896                     G_CALLBACK (on_delete), window);
897 }
898
899
900 GtkWidget*
901 psppire_output_window_new (void)
902 {
903   return GTK_WIDGET (g_object_new (psppire_output_window_get_type (),
904                                    "filename", "Output",
905                                    "description", _("Output Viewer"),
906                                    NULL));
907 }
908
909
910 \f
911 static void
912 create_xr_print_driver (GtkPrintContext *context, PsppireOutputWindow *window)
913 {
914   struct string_map options;
915   GtkPageSetup *page_setup;
916   double width, height;
917   double left_margin;
918   double right_margin;
919   double top_margin;
920   double bottom_margin;
921
922   page_setup = gtk_print_context_get_page_setup (context);
923   width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM);
924   height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_MM);
925   left_margin = gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM);
926   right_margin = gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_MM);
927   top_margin = gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM);
928   bottom_margin = gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_MM);
929
930   string_map_init (&options);
931   string_map_insert_nocopy (&options, xstrdup ("paper-size"),
932                             xasprintf("%.2fx%.2fmm", width, height));
933   string_map_insert_nocopy (&options, xstrdup ("left-margin"),
934                             xasprintf ("%.2fmm", left_margin));
935   string_map_insert_nocopy (&options, xstrdup ("right-margin"),
936                             xasprintf ("%.2fmm", right_margin));
937   string_map_insert_nocopy (&options, xstrdup ("top-margin"),
938                             xasprintf ("%.2fmm", top_margin));
939   string_map_insert_nocopy (&options, xstrdup ("bottom-margin"),
940                             xasprintf ("%.2fmm", bottom_margin));
941
942   window->print_xrd =
943     xr_driver_create (gtk_print_context_get_cairo_context (context), &options);
944
945   string_map_destroy (&options);
946 }
947
948 static gboolean
949 paginate (GtkPrintOperation *operation,
950           GtkPrintContext   *context,
951           PsppireOutputWindow *window)
952 {
953   if ( window->print_item < window->n_items )
954     {
955       xr_driver_output_item (window->print_xrd, window->items[window->print_item++]);
956       if (xr_driver_need_new_page (window->print_xrd))
957         {
958           xr_driver_next_page (window->print_xrd, NULL);
959           window->print_n_pages ++;
960         }
961       return FALSE;
962     }
963   else
964     {
965       gtk_print_operation_set_n_pages (operation, window->print_n_pages);
966       window->print_item = 0;
967       create_xr_print_driver (context, window);
968       return TRUE;
969     }
970 }
971
972 static void
973 begin_print (GtkPrintOperation *operation,
974              GtkPrintContext   *context,
975              PsppireOutputWindow *window)
976 {
977   create_xr_print_driver (context, window);
978
979   window->print_item = 0;
980   window->print_n_pages = 1;
981 }
982
983 static void
984 end_print (GtkPrintOperation *operation,
985            GtkPrintContext   *context,
986            PsppireOutputWindow *window)
987 {
988   xr_driver_destroy (window->print_xrd);
989 }
990
991
992 static void
993 draw_page (GtkPrintOperation *operation,
994            GtkPrintContext   *context,
995            gint               page_number,
996            PsppireOutputWindow *window)
997 {
998   xr_driver_next_page (window->print_xrd, gtk_print_context_get_cairo_context (context));
999   while ( window->print_item < window->n_items)
1000     {
1001       xr_driver_output_item (window->print_xrd, window->items [window->print_item++]);
1002       if ( xr_driver_need_new_page (window->print_xrd) )
1003           break;          
1004     }
1005 }
1006
1007
1008 static void
1009 psppire_output_window_print (PsppireOutputWindow *window)
1010 {
1011   GtkPrintOperationResult res;
1012
1013   GtkPrintOperation *print = gtk_print_operation_new ();
1014
1015   if (window->print_settings != NULL) 
1016     gtk_print_operation_set_print_settings (print, window->print_settings);
1017
1018   g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
1019   g_signal_connect (print, "end_print", G_CALLBACK (end_print),     window);
1020   g_signal_connect (print, "paginate", G_CALLBACK (paginate),       window);
1021   g_signal_connect (print, "draw_page", G_CALLBACK (draw_page),     window);
1022
1023   res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1024                                  GTK_WINDOW (window), NULL);
1025
1026   if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
1027     {
1028       if (window->print_settings != NULL)
1029         g_object_unref (window->print_settings);
1030       window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
1031       
1032     }
1033
1034   g_object_unref (print);
1035 }