b78ef8d3344290064af9c1ba267b1903904b6c8d
[pspp-builds.git] / 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 "about.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_Y,                      /* Y position of top of title. */
54     N_COLS
55   };
56
57 static void psppire_output_window_base_finalize (PsppireOutputWindowClass *, gpointer);
58 static void psppire_output_window_base_init     (PsppireOutputWindowClass *class);
59 static void psppire_output_window_class_init    (PsppireOutputWindowClass *class);
60 static void psppire_output_window_init          (PsppireOutputWindow      *window);
61
62
63 GType
64 psppire_output_window_get_type (void)
65 {
66   static GType psppire_output_window_type = 0;
67
68   if (!psppire_output_window_type)
69     {
70       static const GTypeInfo psppire_output_window_info =
71       {
72         sizeof (PsppireOutputWindowClass),
73         (GBaseInitFunc) psppire_output_window_base_init,
74         (GBaseFinalizeFunc) psppire_output_window_base_finalize,
75         (GClassInitFunc)psppire_output_window_class_init,
76         (GClassFinalizeFunc) NULL,
77         NULL,
78         sizeof (PsppireOutputWindow),
79         0,
80         (GInstanceInitFunc) psppire_output_window_init,
81       };
82
83       psppire_output_window_type =
84         g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireOutputWindow",
85                                 &psppire_output_window_info, 0);
86     }
87
88   return psppire_output_window_type;
89 }
90
91 static GObjectClass *parent_class;
92
93 static void
94 psppire_output_window_finalize (GObject *object)
95 {
96   if (G_OBJECT_CLASS (parent_class)->finalize)
97     (*G_OBJECT_CLASS (parent_class)->finalize) (object);
98 }
99
100
101 static void
102 psppire_output_window_dispose (GObject *obj)
103 {
104   PsppireOutputWindow *viewer = PSPPIRE_OUTPUT_WINDOW (obj);
105   size_t i;
106
107   for (i = 0; i < viewer->n_items; i++)
108     output_item_unref (viewer->items[i]);
109   free (viewer->items);
110   viewer->items = NULL;
111   viewer->n_items = viewer->allocated_items = 0;
112
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   };
153
154 static struct output_driver_class psppire_output_class;
155
156 static struct psppire_output_driver *
157 psppire_output_cast (struct output_driver *driver)
158 {
159   assert (driver->class == &psppire_output_class);
160   return UP_CAST (driver, struct psppire_output_driver, driver);
161 }
162
163 static gboolean
164 expose_event_callback (GtkWidget *widget, GdkEventExpose *event, gpointer data)
165 {
166   struct xr_rendering *r = g_object_get_data (G_OBJECT (widget), "rendering");
167   cairo_t *cr;
168
169   cr = gdk_cairo_create (widget->window);
170   xr_rendering_draw (r, cr);
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, "headers", "off");
247       string_map_insert (&options, "left-margin", "0");
248       string_map_insert (&options, "right-margin", "0");
249       string_map_insert (&options, "top-margin", "0");
250       string_map_insert (&options, "bottom-margin", "0");
251
252       pod->xr = xr_driver_create (cr, &options);
253
254       string_map_destroy (&options);
255     }
256
257   r = xr_rendering_create (pod->xr, item, cr);
258   if (r == NULL)
259     goto done;
260
261   xr_rendering_measure (r, &tw, &th);
262
263   drawing_area = gtk_drawing_area_new ();
264   gtk_widget_modify_bg (
265     GTK_WIDGET (drawing_area), GTK_STATE_NORMAL,
266     &gtk_widget_get_style (drawing_area)->base[GTK_STATE_NORMAL]);
267   g_object_set_data (G_OBJECT (drawing_area), "rendering", r);
268   gtk_widget_set_size_request (drawing_area, tw, th);
269   gtk_layout_put (pod->viewer->output, drawing_area, 0, pod->viewer->y);
270   gtk_widget_show (drawing_area);
271   g_signal_connect (G_OBJECT (drawing_area), "expose_event",
272                      G_CALLBACK (expose_event_callback), NULL);
273
274   if (!is_text_item (item)
275       || text_item_get_type (to_text_item (item)) != TEXT_ITEM_SYNTAX
276       || !viewer->in_command)
277     {
278       store = GTK_TREE_STORE (gtk_tree_view_get_model (viewer->overview));
279
280       ds_init_empty (&title);
281       if (is_text_item (item)
282           && text_item_get_type (to_text_item (item)) == TEXT_ITEM_COMMAND_OPEN)
283         {
284           gtk_tree_store_append (store, &iter, NULL);
285           viewer->cur_command = iter; /* XXX shouldn't save a GtkTreeIter */
286           viewer->in_command = true;
287         }
288       else
289         {
290           GtkTreeIter *p = viewer->in_command ? &viewer->cur_command : NULL;
291           gtk_tree_store_append (store, &iter, p);
292         }
293
294       ds_clear (&title);
295       if (is_text_item (item))
296         ds_put_cstr (&title, text_item_get_text (to_text_item (item)));
297       else if (is_table_item (item))
298         {
299           const char *caption = table_item_get_caption (to_table_item (item));
300           if (caption != NULL)
301             ds_put_format (&title, "Table: %s", caption);
302           else
303             ds_put_cstr (&title, "Table");
304         }
305       else if (is_chart_item (item))
306         {
307           const char *s = chart_item_get_title (to_chart_item (item));
308           if (s != NULL)
309             ds_put_format (&title, "Chart: %s", s);
310           else
311             ds_put_cstr (&title, "Chart");
312         }
313       gtk_tree_store_set (store, &iter,
314                           COL_TITLE, ds_cstr (&title),
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 GtkFileFilter *
417 add_filter (GtkFileChooser *chooser, const char *name, const char *pattern)
418 {
419   GtkFileFilter *filter = gtk_file_filter_new ();
420   g_object_ref_sink (G_OBJECT (filter));
421   gtk_file_filter_set_name (filter, name);
422   gtk_file_filter_add_pattern (filter, pattern);
423   gtk_file_chooser_add_filter (chooser, filter);
424   return filter;
425 }
426
427 static void
428 export_output (PsppireOutputWindow *window, struct string_map *options,
429                const char *format)
430 {
431   struct output_driver *driver;
432   size_t i;
433
434   string_map_insert (options, "format", format);
435   driver = output_driver_create (options);
436   if (driver == NULL)
437     return;
438
439   for (i = 0; i < window->n_items; i++)
440     driver->class->submit (driver, window->items[i]);
441   output_driver_destroy (driver);
442 }
443
444 static void
445 psppire_output_window_export (PsppireOutputWindow *window)
446 {
447   gint response;
448
449   GtkFileFilter *pdf_filter;
450   GtkFileFilter *html_filter;
451   GtkFileFilter *odt_filter;
452   GtkFileFilter *txt_filter;
453   GtkFileFilter *ps_filter;
454   GtkFileFilter *csv_filter;
455   GtkFileChooser *chooser;
456   GtkWidget *dialog;
457
458   dialog = gtk_file_chooser_dialog_new (_("Export Output"),
459                                         GTK_WINDOW (window),
460                                         GTK_FILE_CHOOSER_ACTION_SAVE,
461                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
462                                         GTK_STOCK_SAVE,   GTK_RESPONSE_ACCEPT,
463                                         NULL);
464   chooser = GTK_FILE_CHOOSER (dialog);
465
466   pdf_filter = add_filter (chooser, _("PDF Files (*.pdf)"), "*.pdf");
467   html_filter = add_filter (chooser, _("HTML Files (*.html)"), "*.html");
468   odt_filter = add_filter (chooser, _("OpenDocument Files (*.odt)"), "*.odt");
469   txt_filter = add_filter (chooser, _("Text Files (*.txt)"), "*.txt");
470   ps_filter = add_filter (chooser, _("PostScript Files (*.ps)"), "*.ps");
471   csv_filter = add_filter (chooser, _("Comma-Separated Value Files (*.csv)"),
472                            "*.csv");
473
474   gtk_file_chooser_set_do_overwrite_confirmation (chooser, TRUE);
475   gtk_file_chooser_set_filter (chooser, pdf_filter);
476
477   response = gtk_dialog_run (GTK_DIALOG (dialog));
478
479   if ( response == GTK_RESPONSE_ACCEPT )
480     {
481       char *filename = gtk_file_chooser_get_filename (chooser);
482       GtkFileFilter *filter = gtk_file_chooser_get_filter (chooser);
483       struct string_map options;
484
485       g_return_if_fail (filename);
486       g_return_if_fail (filter);
487
488       string_map_init (&options);
489       string_map_insert (&options, "output-file", filename);
490       if (filter == pdf_filter)
491         {
492           export_output (window, &options, "pdf");
493         }
494       else if (filter == html_filter)
495         export_output (window, &options, "html");
496       else if (filter == odt_filter)
497         export_output (window, &options, "odt");
498       else if (filter == txt_filter)
499         {
500           string_map_insert (&options, "headers", "false");
501           string_map_insert (&options, "paginate", "false");
502           string_map_insert (&options, "squeeze", "true");
503           string_map_insert (&options, "emphasis", "none");
504           string_map_insert (&options, "charts", "none");
505           string_map_insert (&options, "top-margin", "0");
506           string_map_insert (&options, "bottom-margin", "0");
507           export_output (window, &options, "txt");
508         }
509       else if (filter == ps_filter)
510         export_output (window, &options, "ps");
511       else if (filter == csv_filter)
512         export_output (window, &options, "csv");
513       else
514         g_return_if_reached ();
515
516       free (filename);
517     }
518
519   g_object_unref (G_OBJECT (pdf_filter));
520   g_object_unref (G_OBJECT (html_filter));
521   g_object_unref (G_OBJECT (txt_filter));
522   g_object_unref (G_OBJECT (ps_filter));
523   g_object_unref (G_OBJECT (csv_filter));
524
525   gtk_widget_destroy (dialog);
526 }
527
528 static void
529 psppire_output_window_init (PsppireOutputWindow *window)
530 {
531   GtkTreeViewColumn *column;
532   GtkCellRenderer *renderer;
533   GtkBuilder *xml;
534
535   xml = builder_new ("output-viewer.ui");
536
537   gtk_widget_reparent (get_widget_assert (xml, "vbox1"), GTK_WIDGET (window));
538
539   window->output = GTK_LAYOUT (get_widget_assert (xml, "output"));
540   window->y = 0;
541
542   window->overview = GTK_TREE_VIEW (get_widget_assert (xml, "overview"));
543   gtk_tree_view_set_model (window->overview,
544                            GTK_TREE_MODEL (gtk_tree_store_new (
545                                              N_COLS,
546                                              G_TYPE_STRING, /* COL_TITLE */
547                                              G_TYPE_LONG))); /* COL_Y */
548
549   window->in_command = false;
550
551   window->items = NULL;
552   window->n_items = window->allocated_items = 0;
553
554   column = gtk_tree_view_column_new ();
555   gtk_tree_view_append_column (GTK_TREE_VIEW (window->overview), column);
556   renderer = gtk_cell_renderer_text_new ();
557   gtk_tree_view_column_pack_start (column, renderer, TRUE);
558   gtk_tree_view_column_add_attribute (column, renderer, "text", COL_TITLE);
559
560   g_signal_connect (GTK_TREE_VIEW (window->overview),
561                     "row-activated", G_CALLBACK (on_row_activate), window);
562
563   gtk_widget_modify_bg (GTK_WIDGET (window->output), GTK_STATE_NORMAL,
564                         &gtk_widget_get_style (GTK_WIDGET (window->output))->base[GTK_STATE_NORMAL]);
565
566   connect_help (xml);
567
568   g_signal_connect (window,
569                     "focus-in-event",
570                     G_CALLBACK (cancel_urgency),
571                     NULL);
572
573   g_signal_connect (get_action_assert (xml,"help_about"),
574                     "activate",
575                     G_CALLBACK (about_new),
576                     window);
577
578   g_signal_connect (get_action_assert (xml,"help_reference"),
579                     "activate",
580                     G_CALLBACK (reference_manual),
581                     NULL);
582
583   g_signal_connect (get_action_assert (xml,"windows_minimise-all"),
584                     "activate",
585                     G_CALLBACK (psppire_window_minimise_all),
586                     NULL);
587
588   {
589     GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
590
591     PSPPIRE_WINDOW (window)->menu =
592       GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar1/windows_menuitem/windows_minimise-all")->parent);
593   }
594
595   g_signal_connect_swapped (get_action_assert (xml, "file_export"), "activate",
596                             G_CALLBACK (psppire_output_window_export), window);
597
598
599   g_signal_connect_swapped (get_action_assert (xml, "file_print"), "activate",
600                             G_CALLBACK (psppire_output_window_print), window);
601
602   g_object_unref (xml);
603
604   g_signal_connect (window, "delete-event",
605                     G_CALLBACK (on_delete), window);
606 }
607
608
609 GtkWidget*
610 psppire_output_window_new (void)
611 {
612   return GTK_WIDGET (g_object_new (psppire_output_window_get_type (),
613                                    "filename", "Output",
614                                    "description", _("Output Viewer"),
615                                    NULL));
616 }
617
618
619 \f
620 static void
621 create_xr_print_driver (GtkPrintContext *context, PsppireOutputWindow *window)
622 {
623   struct string_map options;
624
625   string_map_init (&options);
626   window->print_xrd =
627     xr_driver_create (gtk_print_context_get_cairo_context (context), &options);
628
629   string_map_destroy (&options);
630 }
631
632 static gboolean
633 paginate (GtkPrintOperation *operation,
634           GtkPrintContext   *context,
635           PsppireOutputWindow *window)
636 {
637   g_print ("%s\n", __FUNCTION__);
638
639   if ( window->print_item < window->n_items )
640     {
641       g_print ("Passing item %d\n", window->print_item);
642       xr_driver_output_item (window->print_xrd, window->items[window->print_item++]);
643       bool x = xr_driver_need_new_page (window->print_xrd);
644       if ( x )
645         {
646           g_print ("Need new page: %d\n", x);
647           xr_driver_next_page (window->print_xrd, NULL);
648           window->print_n_pages ++;
649         }
650       return FALSE;
651     }
652   else
653     {
654       g_print ("Number of pages is %d\n", window->print_n_pages);
655       gtk_print_operation_set_n_pages (operation, window->print_n_pages);
656       window->print_item = 0;
657
658       create_xr_print_driver (context, window);
659
660       return TRUE;
661     }
662 }
663
664 static void
665 begin_print (GtkPrintOperation *operation,
666              GtkPrintContext   *context,
667              PsppireOutputWindow *window)
668 {
669   g_print ("%s\n", __FUNCTION__);
670
671   create_xr_print_driver (context, window);
672
673   window->print_item = 0;
674   window->print_n_pages = 1;
675 }
676
677 static void
678 end_print (GtkPrintOperation *operation,
679            GtkPrintContext   *context,
680            PsppireOutputWindow *window)
681 {
682   g_print ("%s\n", __FUNCTION__);
683   //  xr_driver_destroy (window->print_xrd);
684 }
685
686 static void
687 done (GtkPrintOperation *operation,
688       GtkPrintOperationResult   result,
689       gpointer           user_data)    
690 {
691   g_print ("%s %d\n", __FUNCTION__, result);
692 }
693
694
695 static void
696 draw_page (GtkPrintOperation *operation,
697            GtkPrintContext   *context,
698            gint               page_number,
699            PsppireOutputWindow *window)
700 {
701   g_print ("%s: %d\n", __FUNCTION__, page_number);
702
703   xr_driver_next_page (window->print_xrd, gtk_print_context_get_cairo_context (context));
704   while ( window->print_item < window->n_items)
705     {
706       xr_driver_output_item (window->print_xrd, window->items [window->print_item++]);
707       if ( xr_driver_need_new_page (window->print_xrd) )
708           break;          
709     }
710 }
711
712
713 static void
714 psppire_output_window_print (PsppireOutputWindow *window)
715 {
716   GtkPrintOperationResult res;
717
718   GtkPrintOperation *print = gtk_print_operation_new ();
719
720   if (window->print_settings != NULL) 
721     gtk_print_operation_set_print_settings (print, window->print_settings);
722
723   g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
724   g_signal_connect (print, "end_print", G_CALLBACK (end_print),     window);
725   g_signal_connect (print, "paginate", G_CALLBACK (paginate),       window);
726   g_signal_connect (print, "draw_page", G_CALLBACK (draw_page),     window);
727   g_signal_connect (print, "done", G_CALLBACK (done),               window);
728
729   res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
730                                  GTK_WINDOW (window), NULL);
731
732   if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
733     {
734       if (window->print_settings != NULL)
735         g_object_unref (window->print_settings);
736       window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
737       
738     }
739
740   g_object_unref (print);
741 }