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