Added basic undo/redo to syntax window
[pspp] / src / ui / gui / psppire-syntax-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 "executor.h"
22 #include "helper.h"
23
24 #include <gtksourceview/gtksourcebuffer.h>
25 #include <gtksourceview/gtksourcelanguage.h>
26 #include <gtksourceview/gtksourcelanguagemanager.h>
27 #include <gtksourceview/gtksourceprintcompositor.h>
28
29 #include <libpspp/message.h>
30 #include <stdlib.h>
31
32 #include "psppire.h"
33
34 #include "psppire-data-window.h"
35 #include "psppire-window-register.h"
36 #include "psppire.h"
37 #include "help-menu.h"
38 #include "psppire-syntax-window.h"
39 #include "syntax-editor-source.h"
40 #include <language/lexer/lexer.h>
41
42 #include "xalloc.h"
43
44 #include <gettext.h>
45 #define _(msgid) gettext (msgid)
46 #define N_(msgid) msgid
47
48 static void psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *, gpointer);
49 static void psppire_syntax_window_base_init     (PsppireSyntaxWindowClass *class);
50 static void psppire_syntax_window_class_init    (PsppireSyntaxWindowClass *class);
51 static void psppire_syntax_window_init          (PsppireSyntaxWindow      *syntax_editor);
52
53
54 static void psppire_syntax_window_iface_init (PsppireWindowIface *iface);
55
56
57 GType
58 psppire_syntax_window_get_type (void)
59 {
60   static GType psppire_syntax_window_type = 0;
61
62   if (!psppire_syntax_window_type)
63     {
64       static const GTypeInfo psppire_syntax_window_info =
65       {
66         sizeof (PsppireSyntaxWindowClass),
67         (GBaseInitFunc) psppire_syntax_window_base_init,
68         (GBaseFinalizeFunc) psppire_syntax_window_base_finalize,
69         (GClassInitFunc)psppire_syntax_window_class_init,
70         (GClassFinalizeFunc) NULL,
71         NULL,
72         sizeof (PsppireSyntaxWindow),
73         0,
74         (GInstanceInitFunc) psppire_syntax_window_init,
75       };
76
77       static const GInterfaceInfo window_interface_info =
78         {
79           (GInterfaceInitFunc) psppire_syntax_window_iface_init,
80           NULL,
81           NULL
82         };
83
84       psppire_syntax_window_type =
85         g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireSyntaxWindow",
86                                 &psppire_syntax_window_info, 0);
87
88       g_type_add_interface_static (psppire_syntax_window_type,
89                                    PSPPIRE_TYPE_WINDOW_MODEL,
90                                    &window_interface_info);
91     }
92
93   return psppire_syntax_window_type;
94 }
95
96 static GObjectClass *parent_class ;
97
98 static void
99 psppire_syntax_window_finalize (GObject *object)
100 {
101   if (G_OBJECT_CLASS (parent_class)->finalize)
102     (*G_OBJECT_CLASS (parent_class)->finalize) (object);
103 }
104
105
106 static void
107 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
108 {
109   GtkSourceLanguageManager *lm = gtk_source_language_manager_get_default ();
110
111   const gchar * const *existing_paths =  gtk_source_language_manager_get_search_path (lm);
112
113   const gchar **new_paths = g_strdupv (existing_paths);
114
115   int n = g_strv_length (existing_paths);
116
117   new_paths = g_realloc (new_paths, (n+1) * sizeof (*new_paths));
118   new_paths[n] = g_strdup (PKGDATADIR);
119   new_paths[n+1] = NULL;
120
121   lm = gtk_source_language_manager_new ();
122   gtk_source_language_manager_set_search_path (lm, new_paths);
123
124   class->lan = gtk_source_language_manager_get_language (lm, "pspp");
125
126   if (class->lan == NULL)
127     g_warning ("pspp.lang file not found.  Syntax highlighting will not be available.");
128
129   parent_class = g_type_class_peek_parent (class);
130 }
131
132
133 static void
134 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
135 {
136   GObjectClass *object_class = G_OBJECT_CLASS (class);
137   object_class->finalize = psppire_syntax_window_finalize;
138 }
139
140
141
142 static void
143 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
144                                      gpointer class_data)
145 {
146 }
147
148
149 static void
150 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
151                        GtkTextIter stop)
152 {
153   PsppireWindow *win = PSPPIRE_WINDOW (sw);
154   const gchar *name = psppire_window_get_filename (win);
155   execute_syntax (create_syntax_editor_source (GTK_TEXT_BUFFER (sw->buffer), start, stop, name));
156 }
157
158
159 /* Parse and execute all the text in the buffer */
160 static void
161 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
162 {
163   GtkTextIter begin, end;
164   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
165
166   gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &begin, 0);
167   gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &end, -1);
168
169   editor_execute_syntax (se, begin, end);
170 }
171
172 /* Parse and execute the currently selected text */
173 static void
174 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
175 {
176   GtkTextIter begin, end;
177   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
178
179   if ( gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (se->buffer), &begin, &end) )
180     editor_execute_syntax (se, begin, end);
181 }
182
183
184 /* Parse and execute the from the current line, to the end of the
185    buffer */
186 static void
187 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
188 {
189   GtkTextIter begin, end;
190   GtkTextIter here;
191   gint line;
192
193   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
194
195   /* Get the current line */
196   gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
197                                     &here,
198                                     gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
199                                     );
200
201   line = gtk_text_iter_get_line (&here) ;
202
203   /* Now set begin and end to the start of this line, and end of buffer
204      respectively */
205   gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
206   gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, -1);
207
208   editor_execute_syntax (se, begin, end);
209 }
210
211
212
213 /* Parse and execute the current line */
214 static void
215 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
216 {
217   GtkTextIter begin, end;
218   GtkTextIter here;
219   gint line;
220
221   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
222
223   /* Get the current line */
224   gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
225                                     &here,
226                                     gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
227                                     );
228
229   line = gtk_text_iter_get_line (&here) ;
230
231   /* Now set begin and end to the start of this line, and start of
232      following line respectively */
233   gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
234   gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, line + 1);
235
236   editor_execute_syntax (se, begin, end);
237 }
238
239
240
241 /* Append ".sps" to FILENAME if necessary.
242    The returned result must be freed when no longer required.
243  */
244 static gchar *
245 append_suffix (const gchar *filename)
246 {
247   if ( ! g_str_has_suffix (filename, ".sps" ) &&
248        ! g_str_has_suffix (filename, ".SPS" ) )
249     {
250       return g_strdup_printf ("%s.sps", filename);
251     }
252
253   return xstrdup (filename);
254 }
255
256 /*
257   Save BUFFER to the file called FILENAME.
258   FILENAME must be encoded in Glib filename encoding.
259   If successful, clears the buffer's modified flag.
260 */
261 static gboolean
262 save_editor_to_file (PsppireSyntaxWindow *se,
263                      const gchar *filename,
264                      GError **err)
265 {
266   GtkTextBuffer *buffer = GTK_TEXT_BUFFER (se->buffer);
267   gboolean result ;
268   GtkTextIter start, stop;
269   gchar *text;
270
271   gchar *suffixedname;
272   g_assert (filename);
273
274   suffixedname = append_suffix (filename);
275
276   gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
277   gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
278
279   text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
280
281   result =  g_file_set_contents (suffixedname, text, -1, err);
282
283   g_free (suffixedname);
284
285   if ( result )
286     {
287       char *fn = g_filename_display_name (filename);
288       gchar *msg = g_strdup_printf (_("Saved file \"%s\""), fn);
289       g_free (fn);
290       gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
291       gtk_text_buffer_set_modified (buffer, FALSE);
292       g_free (msg);
293     }
294
295   return result;
296 }
297
298
299 /* Callback for the File->SaveAs menuitem */
300 static void
301 syntax_save_as (PsppireWindow *se)
302 {
303   GtkFileFilter *filter;
304   gint response;
305
306   GtkWidget *dialog =
307     gtk_file_chooser_dialog_new (_("Save Syntax"),
308                                  GTK_WINDOW (se),
309                                  GTK_FILE_CHOOSER_ACTION_SAVE,
310                                  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
311                                  GTK_STOCK_SAVE,   GTK_RESPONSE_ACCEPT,
312                                  NULL);
313
314   filter = gtk_file_filter_new ();
315   gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
316   gtk_file_filter_add_pattern (filter, "*.sps");
317   gtk_file_filter_add_pattern (filter, "*.SPS");
318   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
319
320   filter = gtk_file_filter_new ();
321   gtk_file_filter_set_name (filter, _("All Files"));
322   gtk_file_filter_add_pattern (filter, "*");
323   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
324
325   gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
326                                                   TRUE);
327   response = gtk_dialog_run (GTK_DIALOG (dialog));
328
329   if ( response == GTK_RESPONSE_ACCEPT )
330     {
331       GError *err = NULL;
332       char *filename =
333         gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
334
335       if ( ! save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err) )
336         {
337           msg ( ME, "%s", err->message );
338           g_error_free (err);
339         }
340
341       free (filename);
342     }
343
344   gtk_widget_destroy (dialog);
345 }
346
347
348 /* Callback for the File->Save menuitem */
349 static void
350 syntax_save (PsppireWindow *se)
351 {
352   const gchar *filename = psppire_window_get_filename (se);
353
354   if ( filename == NULL )
355     syntax_save_as (se);
356   else
357     {
358       GError *err = NULL;
359       save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
360       if ( err )
361         {
362           msg (ME, "%s", err->message);
363           g_error_free (err);
364         }
365     }
366 }
367
368
369 /* Callback for the File->Quit menuitem */
370 static gboolean
371 on_quit (GtkMenuItem *menuitem, gpointer    user_data)
372 {
373   psppire_quit ();
374
375   return FALSE;
376 }
377
378
379 static void
380 load_and_show_syntax_window (GtkWidget *se, const gchar *filename)
381 {
382   gboolean ok;
383
384   gtk_source_buffer_begin_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
385   ok = psppire_window_load (PSPPIRE_WINDOW (se), filename);
386   gtk_source_buffer_end_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
387
388   if (ok )
389     gtk_widget_show (se);
390   else
391     gtk_widget_destroy (se);
392 }
393
394 void
395 create_syntax_window (void)
396 {
397   GtkWidget *w = psppire_syntax_window_new ();
398   gtk_widget_show (w);
399 }
400
401 void
402 open_new_syntax_window (const char *file_name)
403 {
404   GtkWidget *se = psppire_syntax_window_new ();
405
406   if ( file_name)
407     load_and_show_syntax_window (se, file_name);
408 }
409
410
411
412 static void psppire_syntax_window_print (PsppireSyntaxWindow *window);
413
414 static void
415 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
416 {
417   if (gtk_text_buffer_get_modified (buffer))
418     psppire_window_set_unsaved (window);
419 }
420
421 extern struct source_stream *the_source_stream ;
422
423 static void undo_redo_update (PsppireSyntaxWindow *window);
424 static void undo_last_edit (PsppireSyntaxWindow *window);
425 static void redo_last_edit (PsppireSyntaxWindow *window);
426
427 static void
428 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
429 {
430   gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
431   undo_redo_update (window);
432 }
433
434 static void
435 psppire_syntax_window_init (PsppireSyntaxWindow *window)
436 {
437   GtkBuilder *xml = builder_new ("syntax-editor.ui");
438   GtkWidget *box = gtk_vbox_new (FALSE, 0);
439
440   GtkWidget *menubar = get_widget_assert (xml, "menubar");
441   GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
442
443   GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
444
445   PsppireSyntaxWindowClass *class
446     = PSPPIRE_SYNTAX_WINDOW_CLASS (G_OBJECT_GET_CLASS (window));
447
448   window->print_settings = NULL;
449   window->undo_menuitem = get_action_assert (xml, "edit_undo");
450   window->redo_menuitem = get_action_assert (xml, "edit_redo");
451
452   if (class->lan)
453     window->buffer = gtk_source_buffer_new_with_language (class->lan);
454   else
455     window->buffer = gtk_source_buffer_new (NULL);
456
457   gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), GTK_TEXT_BUFFER (window->buffer));
458
459   g_object_set (window->buffer,
460                 "highlight-matching-brackets", TRUE,
461                 NULL);
462
463
464   g_object_set (text_view,
465                 "show-line-numbers", TRUE,
466                 "show-line-marks", TRUE,
467                 "auto-indent", TRUE,
468                 "indent-width", 4,
469                 "highlight-current-line", TRUE,
470                 NULL);
471   window->lexer = lex_create (the_source_stream);
472
473   window->sb = get_widget_assert (xml, "statusbar2");
474   window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
475
476   g_signal_connect (window->buffer, "changed", G_CALLBACK (on_text_changed), window);
477
478   g_signal_connect (window->buffer, "modified-changed",
479                     G_CALLBACK (on_modified_changed), window);
480
481   g_signal_connect_swapped (get_action_assert (xml, "file_print"), "activate",
482                             G_CALLBACK (psppire_syntax_window_print), window);
483
484
485   g_signal_connect_swapped (window->undo_menuitem,
486                             "activate",
487                             G_CALLBACK (undo_last_edit),
488                             window);
489
490   g_signal_connect_swapped (window->redo_menuitem,
491                             "activate",
492                             G_CALLBACK (redo_last_edit),
493                             window);
494
495   undo_redo_update (window);
496
497   connect_help (xml);
498
499   gtk_container_add (GTK_CONTAINER (window), box);
500
501   g_object_ref (menubar);
502
503   g_object_ref (sw);
504
505   g_object_ref (window->sb);
506
507
508   gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
509   gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
510   gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
511
512   gtk_widget_show_all (box);
513
514   g_signal_connect_swapped (get_action_assert (xml,"file_new_syntax"), "activate", G_CALLBACK (create_syntax_window), NULL);
515
516 #if 0
517   g_signal_connect (get_action_assert (xml,"file_new_data"),
518                     "activate",
519                     G_CALLBACK (create_data_window),
520                     window);
521 #endif
522
523   g_signal_connect_swapped (get_action_assert (xml, "file_save"),
524                     "activate",
525                     G_CALLBACK (syntax_save),
526                     window);
527
528   g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
529                     "activate",
530                     G_CALLBACK (syntax_save_as),
531                     window);
532
533   g_signal_connect (get_action_assert (xml,"file_quit"),
534                     "activate",
535                     G_CALLBACK (on_quit),
536                     window);
537
538   g_signal_connect (get_action_assert (xml,"run_all"),
539                     "activate",
540                     G_CALLBACK (on_run_all),
541                     window);
542
543
544   g_signal_connect (get_action_assert (xml,"run_selection"),
545                     "activate",
546                     G_CALLBACK (on_run_selection),
547                     window);
548
549   g_signal_connect (get_action_assert (xml,"run_current_line"),
550                     "activate",
551                     G_CALLBACK (on_run_current_line),
552                     window);
553
554   g_signal_connect (get_action_assert (xml,"run_to_end"),
555                     "activate",
556                     G_CALLBACK (on_run_to_end),
557                     window);
558
559   g_signal_connect (get_action_assert (xml,"windows_minimise_all"),
560                     "activate",
561                     G_CALLBACK (psppire_window_minimise_all), NULL);
562
563
564
565
566
567   {
568   GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
569
570   merge_help_menu (uim);
571
572   PSPPIRE_WINDOW (window)->menu =
573     GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
574   }
575
576   g_object_unref (xml);
577 }
578
579
580 GtkWidget*
581 psppire_syntax_window_new (void)
582 {
583   return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
584                                    "filename", "Syntax",
585                                    "description", _("Syntax Editor"),
586                                    NULL));
587 }
588
589 static void
590 error_dialog (GtkWindow *w, const gchar *filename,  GError *err)
591 {
592   gchar *fn = g_filename_display_basename (filename);
593
594   GtkWidget *dialog =
595     gtk_message_dialog_new (w,
596                             GTK_DIALOG_DESTROY_WITH_PARENT,
597                             GTK_MESSAGE_ERROR,
598                             GTK_BUTTONS_CLOSE,
599                             _("Cannot load syntax file '%s'"),
600                             fn);
601
602   g_free (fn);
603
604   g_object_set (dialog, "icon-name", "psppicon", NULL);
605
606   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
607                                             "%s", err->message);
608
609   gtk_dialog_run (GTK_DIALOG (dialog));
610
611   gtk_widget_destroy (dialog);
612 }
613
614 /*
615   Loads the buffer from the file called FILENAME
616 */
617 gboolean
618 syntax_load (PsppireWindow *window, const gchar *filename)
619 {
620   GError *err = NULL;
621   gchar *text_locale = NULL;
622   gchar *text_utf8 = NULL;
623   gsize len_locale = -1;
624   gsize len_utf8 = -1;
625   GtkTextIter iter;
626   PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
627   GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
628   /* FIXME: What if it's a very big file ? */
629   if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
630     {
631       error_dialog (GTK_WINDOW (window), filename, err);
632       g_clear_error (&err);
633       return FALSE;
634     }
635
636   text_utf8 = g_locale_to_utf8 (text_locale, len_locale, NULL, &len_utf8, &err);
637
638   free (text_locale);
639
640   if ( text_utf8 == NULL )
641     {
642       error_dialog (GTK_WINDOW (window), filename, err);
643       g_clear_error (&err);
644       return FALSE;
645     }
646
647   gtk_text_buffer_get_iter_at_line (buffer, &iter, 0);
648
649   gtk_text_buffer_insert (buffer, &iter, text_utf8, len_utf8);
650
651   gtk_text_buffer_set_modified (buffer, FALSE);
652
653   free (text_utf8);
654
655   return TRUE;
656 }
657
658 \f
659
660 static void
661 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
662 {
663   iface->save = syntax_save;
664   iface->load = syntax_load;
665 }
666
667 \f
668
669 static void
670 undo_redo_update (PsppireSyntaxWindow *window)
671 {
672   gtk_action_set_sensitive (window->undo_menuitem,
673                             gtk_source_buffer_can_undo (window->buffer));
674
675   gtk_action_set_sensitive (window->redo_menuitem,
676                             gtk_source_buffer_can_redo (window->buffer));
677 }
678
679 static void
680 undo_last_edit (PsppireSyntaxWindow *window)
681 {
682   gtk_source_buffer_undo (window->buffer);
683   undo_redo_update (window);
684 }
685
686 static void
687 redo_last_edit (PsppireSyntaxWindow *window)
688 {
689   gtk_source_buffer_redo (window->buffer);
690   undo_redo_update (window);
691 }
692
693
694 \f
695 /* Printing related stuff */
696
697
698 static void
699 begin_print (GtkPrintOperation *operation,
700           GtkPrintContext   *context,
701           PsppireSyntaxWindow *window)
702 {
703   window->compositor =
704     gtk_source_print_compositor_new (window->buffer);
705 }
706
707
708 static void
709 end_print (GtkPrintOperation *operation,
710           GtkPrintContext   *context,
711           PsppireSyntaxWindow *window)
712 {
713   g_object_unref (window->compositor);
714   window->compositor = NULL;
715 }
716
717
718
719 static gboolean
720 paginate (GtkPrintOperation *operation,
721           GtkPrintContext   *context,
722           PsppireSyntaxWindow *window)
723 {
724   if (gtk_source_print_compositor_paginate (window->compositor, context))
725     {
726       gint n_pages = gtk_source_print_compositor_get_n_pages (window->compositor);
727       gtk_print_operation_set_n_pages (operation, n_pages);
728         
729       return TRUE;
730     }
731
732   return FALSE;
733 }
734
735 static void
736 draw_page (GtkPrintOperation *operation,
737            GtkPrintContext   *context,
738            gint               page_nr,
739           PsppireSyntaxWindow *window)
740 {
741   gtk_source_print_compositor_draw_page (window->compositor, 
742                                          context,
743                                          page_nr);
744 }
745
746
747
748 static void
749 psppire_syntax_window_print (PsppireSyntaxWindow *window)
750 {
751   GtkPrintOperationResult res;
752
753   GtkPrintOperation *print = gtk_print_operation_new ();
754
755   if (window->print_settings != NULL) 
756     gtk_print_operation_set_print_settings (print, window->print_settings);
757
758
759   g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
760   g_signal_connect (print, "end_print", G_CALLBACK (end_print),     window);
761   g_signal_connect (print, "draw_page", G_CALLBACK (draw_page),     window);
762   g_signal_connect (print, "paginate", G_CALLBACK (paginate),       window);
763
764   res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
765                                  GTK_WINDOW (window), NULL);
766
767   if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
768     {
769       if (window->print_settings != NULL)
770         g_object_unref (window->print_settings);
771       window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
772     }
773
774   g_object_unref (print);
775 }