1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2009, 2010 Free Software Foundation
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.
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.
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/>. */
19 #include "relocatable.h"
21 #include <gtk/gtksignal.h>
22 #include <gtk/gtkbox.h>
26 #include <gtksourceview/gtksourcebuffer.h>
27 #include <gtksourceview/gtksourcelanguage.h>
28 #include <gtksourceview/gtksourcelanguagemanager.h>
29 #include <gtksourceview/gtksourceprintcompositor.h>
31 #include <libpspp/message.h>
36 #include "psppire-data-window.h"
37 #include "psppire-window-register.h"
39 #include "help-menu.h"
40 #include "psppire-syntax-window.h"
41 #include "syntax-editor-source.h"
42 #include <language/lexer/lexer.h>
47 #define _(msgid) gettext (msgid)
48 #define N_(msgid) msgid
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);
56 static void psppire_syntax_window_iface_init (PsppireWindowIface *iface);
60 psppire_syntax_window_get_type (void)
62 static GType psppire_syntax_window_type = 0;
64 if (!psppire_syntax_window_type)
66 static const GTypeInfo psppire_syntax_window_info =
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,
74 sizeof (PsppireSyntaxWindow),
76 (GInstanceInitFunc) psppire_syntax_window_init,
79 static const GInterfaceInfo window_interface_info =
81 (GInterfaceInitFunc) psppire_syntax_window_iface_init,
86 psppire_syntax_window_type =
87 g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireSyntaxWindow",
88 &psppire_syntax_window_info, 0);
90 g_type_add_interface_static (psppire_syntax_window_type,
91 PSPPIRE_TYPE_WINDOW_MODEL,
92 &window_interface_info);
95 return psppire_syntax_window_type;
98 static GObjectClass *parent_class ;
101 psppire_syntax_window_finalize (GObject *object)
103 if (G_OBJECT_CLASS (parent_class)->finalize)
104 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
109 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
111 GtkSourceLanguageManager *lm = gtk_source_language_manager_get_default ();
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);
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;
121 lm = gtk_source_language_manager_new ();
122 gtk_source_language_manager_set_search_path (lm, new_paths);
124 class->lan = gtk_source_language_manager_get_language (lm, "pspp");
126 if (class->lan == NULL)
127 g_warning ("pspp.lang file not found. Syntax highlighting will not be available.");
129 parent_class = g_type_class_peek_parent (class);
131 g_strfreev (new_paths);
136 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
138 GObjectClass *object_class = G_OBJECT_CLASS (class);
139 object_class->finalize = psppire_syntax_window_finalize;
145 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
152 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
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));
163 /* Delete the currently selected text */
165 on_edit_delete (PsppireSyntaxWindow *sw)
167 GtkTextIter begin, end;
168 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
170 if ( gtk_text_buffer_get_selection_bounds (buffer, &begin, &end) )
171 gtk_text_buffer_delete (buffer, &begin, &end);
175 /* The syntax editor's clipboard deals only with text */
183 selection_changed (PsppireSyntaxWindow *sw)
185 gboolean sel = gtk_text_buffer_get_has_selection (GTK_TEXT_BUFFER (sw->buffer));
187 gtk_action_set_sensitive (sw->edit_copy, sel);
188 gtk_action_set_sensitive (sw->edit_cut, sel);
189 gtk_action_set_sensitive (sw->edit_delete, sel);
192 /* The callback which runs when something request clipboard data */
194 clipboard_get_cb (GtkClipboard *clipboard,
195 GtkSelectionData *selection_data,
199 PsppireSyntaxWindow *sw = data;
200 g_assert (info == SELECT_FMT_TEXT);
202 gtk_selection_data_set (selection_data, selection_data->target,
204 (const guchar *) sw->cliptext, strlen (sw->cliptext));
209 clipboard_clear_cb (GtkClipboard *clipboard,
212 PsppireSyntaxWindow *sw = data;
213 g_free (sw->cliptext);
218 static const GtkTargetEntry targets[] = {
219 { "UTF8_STRING", 0, SELECT_FMT_TEXT },
220 { "STRING", 0, SELECT_FMT_TEXT },
221 { "TEXT", 0, SELECT_FMT_TEXT },
222 { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
223 { "text/plain;charset=utf-8", 0, SELECT_FMT_TEXT },
224 { "text/plain", 0, SELECT_FMT_TEXT },
229 Store a clip containing the currently selected text.
230 Returns true iff something was set.
231 As a side effect, begin and end will be set to indicate
232 the limits of the selected text.
235 set_clip (PsppireSyntaxWindow *sw, GtkTextIter *begin, GtkTextIter *end)
237 GtkClipboard *clipboard ;
238 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
240 if ( ! gtk_text_buffer_get_selection_bounds (buffer, begin, end) )
243 g_free (sw->cliptext);
244 sw->cliptext = gtk_text_buffer_get_text (buffer, begin, end, FALSE);
247 gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
249 if (!gtk_clipboard_set_with_owner (clipboard, targets,
250 G_N_ELEMENTS (targets),
251 clipboard_get_cb, clipboard_clear_cb,
253 clipboard_clear_cb (clipboard, sw);
259 on_edit_cut (PsppireSyntaxWindow *sw)
261 GtkTextIter begin, end;
263 if ( set_clip (sw, &begin, &end))
264 gtk_text_buffer_delete (GTK_TEXT_BUFFER (sw->buffer), &begin, &end);
268 on_edit_copy (PsppireSyntaxWindow *sw)
270 GtkTextIter begin, end;
272 set_clip (sw, &begin, &end);
276 /* A callback for when the clipboard contents have been received */
278 contents_received_callback (GtkClipboard *clipboard,
279 GtkSelectionData *sd,
283 PsppireSyntaxWindow *syntax_window = data;
285 if ( sd->length < 0 )
288 if ( sd->type != gdk_atom_intern ("UTF8_STRING", FALSE))
291 c = (gchar *) sd->data;
293 gtk_text_buffer_insert_at_cursor (GTK_TEXT_BUFFER (syntax_window->buffer),
300 on_edit_paste (PsppireSyntaxWindow *sw)
302 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (sw));
303 GtkClipboard *clipboard =
304 gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
306 gtk_clipboard_request_contents (clipboard,
307 gdk_atom_intern ("UTF8_STRING", TRUE),
308 contents_received_callback,
313 on_owner_change (GtkClipboard *clip, GdkEventOwnerChange *event, gpointer data)
316 gboolean compatible_target = FALSE;
317 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (data);
319 for (i = 0 ; i < sizeof (targets) / sizeof (targets[0]) ; ++i)
321 GdkAtom atom = gdk_atom_intern (targets[i].target, TRUE);
322 if ( gtk_clipboard_wait_is_target_available (clip, atom))
324 compatible_target = TRUE;
329 gtk_action_set_sensitive (sw->edit_paste, compatible_target);
335 /* Parse and execute all the text in the buffer */
337 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
339 GtkTextIter begin, end;
340 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
342 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &begin, 0);
343 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &end, -1);
345 editor_execute_syntax (se, begin, end);
348 /* Parse and execute the currently selected text */
350 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
352 GtkTextIter begin, end;
353 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
355 if ( gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (se->buffer), &begin, &end) )
356 editor_execute_syntax (se, begin, end);
360 /* Parse and execute the from the current line, to the end of the
363 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
365 GtkTextIter begin, end;
369 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
371 /* Get the current line */
372 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
374 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
377 line = gtk_text_iter_get_line (&here) ;
379 /* Now set begin and end to the start of this line, and end of buffer
381 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
382 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, -1);
384 editor_execute_syntax (se, begin, end);
389 /* Parse and execute the current line */
391 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
393 GtkTextIter begin, end;
397 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
399 /* Get the current line */
400 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
402 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
405 line = gtk_text_iter_get_line (&here) ;
407 /* Now set begin and end to the start of this line, and start of
408 following line respectively */
409 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
410 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, line + 1);
412 editor_execute_syntax (se, begin, end);
417 /* Append ".sps" to FILENAME if necessary.
418 The returned result must be freed when no longer required.
421 append_suffix (const gchar *filename)
423 if ( ! g_str_has_suffix (filename, ".sps" ) &&
424 ! g_str_has_suffix (filename, ".SPS" ) )
426 return g_strdup_printf ("%s.sps", filename);
429 return xstrdup (filename);
433 Save BUFFER to the file called FILENAME.
434 FILENAME must be encoded in Glib filename encoding.
435 If successful, clears the buffer's modified flag.
438 save_editor_to_file (PsppireSyntaxWindow *se,
439 const gchar *filename,
442 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (se->buffer);
444 GtkTextIter start, stop;
450 suffixedname = append_suffix (filename);
452 gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
453 gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
455 text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
457 result = g_file_set_contents (suffixedname, text, -1, err);
459 g_free (suffixedname);
463 char *fn = g_filename_display_name (filename);
464 gchar *msg = g_strdup_printf (_("Saved file \"%s\""), fn);
466 gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
467 gtk_text_buffer_set_modified (buffer, FALSE);
475 /* Callback for the File->SaveAs menuitem */
477 syntax_save_as (PsppireWindow *se)
479 GtkFileFilter *filter;
483 gtk_file_chooser_dialog_new (_("Save Syntax"),
485 GTK_FILE_CHOOSER_ACTION_SAVE,
486 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
487 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
490 filter = gtk_file_filter_new ();
491 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
492 gtk_file_filter_add_pattern (filter, "*.sps");
493 gtk_file_filter_add_pattern (filter, "*.SPS");
494 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
496 filter = gtk_file_filter_new ();
497 gtk_file_filter_set_name (filter, _("All Files"));
498 gtk_file_filter_add_pattern (filter, "*");
499 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
501 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
503 response = gtk_dialog_run (GTK_DIALOG (dialog));
505 if ( response == GTK_RESPONSE_ACCEPT )
509 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
511 if ( ! save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err) )
513 msg ( ME, "%s", err->message );
520 gtk_widget_destroy (dialog);
524 /* Callback for the File->Save menuitem */
526 syntax_save (PsppireWindow *se)
528 const gchar *filename = psppire_window_get_filename (se);
530 if ( filename == NULL )
535 save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
538 msg (ME, "%s", err->message);
545 /* Callback for the File->Quit menuitem */
547 on_quit (GtkMenuItem *menuitem, gpointer user_data)
556 load_and_show_syntax_window (GtkWidget *se, const gchar *filename)
560 gtk_source_buffer_begin_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
561 ok = psppire_window_load (PSPPIRE_WINDOW (se), filename);
562 gtk_source_buffer_end_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
565 gtk_widget_show (se);
567 gtk_widget_destroy (se);
571 create_syntax_window (void)
573 GtkWidget *w = psppire_syntax_window_new ();
578 open_new_syntax_window (const char *file_name)
580 GtkWidget *se = psppire_syntax_window_new ();
583 load_and_show_syntax_window (se, file_name);
588 static void psppire_syntax_window_print (PsppireSyntaxWindow *window);
591 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
593 if (gtk_text_buffer_get_modified (buffer))
594 psppire_window_set_unsaved (window);
597 extern struct source_stream *the_source_stream ;
599 static void undo_redo_update (PsppireSyntaxWindow *window);
600 static void undo_last_edit (PsppireSyntaxWindow *window);
601 static void redo_last_edit (PsppireSyntaxWindow *window);
604 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
606 gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
607 undo_redo_update (window);
611 psppire_syntax_window_init (PsppireSyntaxWindow *window)
613 GtkBuilder *xml = builder_new ("syntax-editor.ui");
614 GtkWidget *box = gtk_vbox_new (FALSE, 0);
616 GtkWidget *menubar = get_widget_assert (xml, "menubar");
617 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
619 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
621 PsppireSyntaxWindowClass *class
622 = PSPPIRE_SYNTAX_WINDOW_CLASS (G_OBJECT_GET_CLASS (window));
624 GtkClipboard *clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
625 GtkClipboard *clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
627 window->print_settings = NULL;
628 window->undo_menuitem = get_action_assert (xml, "edit_undo");
629 window->redo_menuitem = get_action_assert (xml, "edit_redo");
632 window->buffer = gtk_source_buffer_new_with_language (class->lan);
634 window->buffer = gtk_source_buffer_new (NULL);
636 gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), GTK_TEXT_BUFFER (window->buffer));
638 g_object_set (window->buffer,
639 "highlight-matching-brackets", TRUE,
642 g_object_set (text_view,
643 "show-line-numbers", TRUE,
644 "show-line-marks", TRUE,
647 "highlight-current-line", TRUE,
650 g_signal_connect_swapped (clip_primary, "owner-change", G_CALLBACK (selection_changed), window);
652 g_signal_connect (clip_selection, "owner-change", G_CALLBACK (on_owner_change), window);
654 window->cliptext = NULL;
656 window->edit_delete = get_action_assert (xml, "edit_delete");
657 window->edit_copy = get_action_assert (xml, "edit_copy");
658 window->edit_cut = get_action_assert (xml, "edit_cut");
659 window->edit_paste = get_action_assert (xml, "edit_paste");
661 window->lexer = lex_create (the_source_stream);
663 window->sb = get_widget_assert (xml, "statusbar2");
664 window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
666 g_signal_connect (window->buffer, "changed", G_CALLBACK (on_text_changed), window);
668 g_signal_connect (window->buffer, "modified-changed",
669 G_CALLBACK (on_modified_changed), window);
671 g_signal_connect_swapped (get_action_assert (xml, "file_print"), "activate",
672 G_CALLBACK (psppire_syntax_window_print), window);
675 g_signal_connect_swapped (window->undo_menuitem,
677 G_CALLBACK (undo_last_edit),
680 g_signal_connect_swapped (window->redo_menuitem,
682 G_CALLBACK (redo_last_edit),
685 undo_redo_update (window);
689 gtk_container_add (GTK_CONTAINER (window), box);
691 g_object_ref (menubar);
695 g_object_ref (window->sb);
698 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
699 gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
700 gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
702 gtk_widget_show_all (box);
704 g_signal_connect_swapped (get_action_assert (xml,"file_new_syntax"), "activate", G_CALLBACK (create_syntax_window), NULL);
707 g_signal_connect (get_action_assert (xml,"file_new_data"),
709 G_CALLBACK (create_data_window),
713 g_signal_connect_swapped (get_action_assert (xml, "file_save"),
715 G_CALLBACK (syntax_save),
718 g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
720 G_CALLBACK (syntax_save_as),
723 g_signal_connect (get_action_assert (xml,"file_quit"),
725 G_CALLBACK (on_quit),
728 g_signal_connect_swapped (window->edit_delete,
730 G_CALLBACK (on_edit_delete),
733 g_signal_connect_swapped (window->edit_copy,
735 G_CALLBACK (on_edit_copy),
738 g_signal_connect_swapped (window->edit_cut,
740 G_CALLBACK (on_edit_cut),
743 g_signal_connect_swapped (window->edit_paste,
745 G_CALLBACK (on_edit_paste),
748 g_signal_connect (get_action_assert (xml,"run_all"),
750 G_CALLBACK (on_run_all),
753 g_signal_connect (get_action_assert (xml,"run_selection"),
755 G_CALLBACK (on_run_selection),
758 g_signal_connect (get_action_assert (xml,"run_current_line"),
760 G_CALLBACK (on_run_current_line),
763 g_signal_connect (get_action_assert (xml,"run_to_end"),
765 G_CALLBACK (on_run_to_end),
768 g_signal_connect (get_action_assert (xml,"windows_minimise_all"),
770 G_CALLBACK (psppire_window_minimise_all), NULL);
777 GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
779 merge_help_menu (uim);
781 PSPPIRE_WINDOW (window)->menu =
782 GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
785 g_object_unref (xml);
793 psppire_syntax_window_new (void)
795 return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
796 "filename", "Syntax",
797 "description", _("Syntax Editor"),
802 error_dialog (GtkWindow *w, const gchar *filename, GError *err)
804 gchar *fn = g_filename_display_basename (filename);
807 gtk_message_dialog_new (w,
808 GTK_DIALOG_DESTROY_WITH_PARENT,
811 _("Cannot load syntax file '%s'"),
816 g_object_set (dialog, "icon-name", "psppicon", NULL);
818 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
821 gtk_dialog_run (GTK_DIALOG (dialog));
823 gtk_widget_destroy (dialog);
827 Loads the buffer from the file called FILENAME
830 syntax_load (PsppireWindow *window, const gchar *filename)
833 gchar *text_locale = NULL;
834 gchar *text_utf8 = NULL;
835 gsize len_locale = -1;
838 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
839 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
840 /* FIXME: What if it's a very big file ? */
841 if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
843 error_dialog (GTK_WINDOW (window), filename, err);
844 g_clear_error (&err);
848 text_utf8 = g_locale_to_utf8 (text_locale, len_locale, NULL, &len_utf8, &err);
852 if ( text_utf8 == NULL )
854 error_dialog (GTK_WINDOW (window), filename, err);
855 g_clear_error (&err);
859 gtk_text_buffer_get_iter_at_line (buffer, &iter, 0);
861 gtk_text_buffer_insert (buffer, &iter, text_utf8, len_utf8);
863 gtk_text_buffer_set_modified (buffer, FALSE);
873 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
875 iface->save = syntax_save;
876 iface->load = syntax_load;
882 undo_redo_update (PsppireSyntaxWindow *window)
884 gtk_action_set_sensitive (window->undo_menuitem,
885 gtk_source_buffer_can_undo (window->buffer));
887 gtk_action_set_sensitive (window->redo_menuitem,
888 gtk_source_buffer_can_redo (window->buffer));
892 undo_last_edit (PsppireSyntaxWindow *window)
894 gtk_source_buffer_undo (window->buffer);
895 undo_redo_update (window);
899 redo_last_edit (PsppireSyntaxWindow *window)
901 gtk_source_buffer_redo (window->buffer);
902 undo_redo_update (window);
907 /* Printing related stuff */
911 begin_print (GtkPrintOperation *operation,
912 GtkPrintContext *context,
913 PsppireSyntaxWindow *window)
916 gtk_source_print_compositor_new (window->buffer);
921 end_print (GtkPrintOperation *operation,
922 GtkPrintContext *context,
923 PsppireSyntaxWindow *window)
925 g_object_unref (window->compositor);
926 window->compositor = NULL;
932 paginate (GtkPrintOperation *operation,
933 GtkPrintContext *context,
934 PsppireSyntaxWindow *window)
936 if (gtk_source_print_compositor_paginate (window->compositor, context))
938 gint n_pages = gtk_source_print_compositor_get_n_pages (window->compositor);
939 gtk_print_operation_set_n_pages (operation, n_pages);
948 draw_page (GtkPrintOperation *operation,
949 GtkPrintContext *context,
951 PsppireSyntaxWindow *window)
953 gtk_source_print_compositor_draw_page (window->compositor,
961 psppire_syntax_window_print (PsppireSyntaxWindow *window)
963 GtkPrintOperationResult res;
965 GtkPrintOperation *print = gtk_print_operation_new ();
967 if (window->print_settings != NULL)
968 gtk_print_operation_set_print_settings (print, window->print_settings);
971 g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
972 g_signal_connect (print, "end_print", G_CALLBACK (end_print), window);
973 g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), window);
974 g_signal_connect (print, "paginate", G_CALLBACK (paginate), window);
976 res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
977 GTK_WINDOW (window), NULL);
979 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
981 if (window->print_settings != NULL)
982 g_object_unref (window->print_settings);
983 window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
986 g_object_unref (print);