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_dispose (GObject *obj)
111 PsppireSyntaxWindow *sw = (PsppireSyntaxWindow *)obj;
113 GtkClipboard *clip_selection;
114 GtkClipboard *clip_primary;
116 if (sw->dispose_has_run)
119 clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
120 clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_PRIMARY);
122 g_signal_handler_disconnect (clip_primary, sw->sel_handler);
124 g_signal_handler_disconnect (clip_selection, sw->ps_handler);
126 /* Make sure dispose does not run twice. */
127 sw->dispose_has_run = TRUE;
129 /* Chain up to the parent class */
130 G_OBJECT_CLASS (parent_class)->dispose (obj);
136 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
138 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
140 GtkSourceLanguageManager *lm = gtk_source_language_manager_get_default ();
142 const gchar * const *existing_paths = gtk_source_language_manager_get_search_path (lm);
143 gchar **new_paths = g_strdupv ((gchar **)existing_paths);
144 int n = g_strv_length ((gchar **) existing_paths);
146 new_paths = g_realloc (new_paths, (n + 2) * sizeof (*new_paths));
147 new_paths[n] = g_strdup (relocate (PKGDATADIR));
148 new_paths[n+1] = NULL;
150 lm = gtk_source_language_manager_new ();
151 gtk_source_language_manager_set_search_path (lm, new_paths);
153 class->lan = gtk_source_language_manager_get_language (lm, "pspp");
155 if (class->lan == NULL)
156 g_warning ("pspp.lang file not found. Syntax highlighting will not be available.");
158 parent_class = g_type_class_peek_parent (class);
160 g_strfreev (new_paths);
162 parent_class = g_type_class_peek_parent (class);
164 gobject_class->dispose = psppire_syntax_window_dispose;
169 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
171 GObjectClass *object_class = G_OBJECT_CLASS (class);
172 object_class->finalize = psppire_syntax_window_finalize;
178 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
185 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
188 PsppireWindow *win = PSPPIRE_WINDOW (sw);
189 const gchar *name = psppire_window_get_filename (win);
190 execute_syntax (create_syntax_editor_source (GTK_TEXT_BUFFER (sw->buffer), start, stop, name));
196 /* Delete the currently selected text */
198 on_edit_delete (PsppireSyntaxWindow *sw)
200 GtkTextIter begin, end;
201 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
203 if ( gtk_text_buffer_get_selection_bounds (buffer, &begin, &end) )
204 gtk_text_buffer_delete (buffer, &begin, &end);
208 /* The syntax editor's clipboard deals only with text */
216 selection_changed (PsppireSyntaxWindow *sw)
218 gboolean sel = gtk_text_buffer_get_has_selection (GTK_TEXT_BUFFER (sw->buffer));
220 gtk_action_set_sensitive (sw->edit_copy, sel);
221 gtk_action_set_sensitive (sw->edit_cut, sel);
222 gtk_action_set_sensitive (sw->edit_delete, sel);
225 /* The callback which runs when something request clipboard data */
227 clipboard_get_cb (GtkClipboard *clipboard,
228 GtkSelectionData *selection_data,
232 PsppireSyntaxWindow *sw = data;
233 g_assert (info == SELECT_FMT_TEXT);
235 gtk_selection_data_set (selection_data, selection_data->target,
237 (const guchar *) sw->cliptext, strlen (sw->cliptext));
242 clipboard_clear_cb (GtkClipboard *clipboard,
245 PsppireSyntaxWindow *sw = data;
246 g_free (sw->cliptext);
251 static const GtkTargetEntry targets[] = {
252 { "UTF8_STRING", 0, SELECT_FMT_TEXT },
253 { "STRING", 0, SELECT_FMT_TEXT },
254 { "TEXT", 0, SELECT_FMT_TEXT },
255 { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
256 { "text/plain;charset=utf-8", 0, SELECT_FMT_TEXT },
257 { "text/plain", 0, SELECT_FMT_TEXT },
262 Store a clip containing the currently selected text.
263 Returns true iff something was set.
264 As a side effect, begin and end will be set to indicate
265 the limits of the selected text.
268 set_clip (PsppireSyntaxWindow *sw, GtkTextIter *begin, GtkTextIter *end)
270 GtkClipboard *clipboard ;
271 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
273 if ( ! gtk_text_buffer_get_selection_bounds (buffer, begin, end) )
276 g_free (sw->cliptext);
277 sw->cliptext = gtk_text_buffer_get_text (buffer, begin, end, FALSE);
280 gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
282 if (!gtk_clipboard_set_with_owner (clipboard, targets,
283 G_N_ELEMENTS (targets),
284 clipboard_get_cb, clipboard_clear_cb,
286 clipboard_clear_cb (clipboard, sw);
292 on_edit_cut (PsppireSyntaxWindow *sw)
294 GtkTextIter begin, end;
296 if ( set_clip (sw, &begin, &end))
297 gtk_text_buffer_delete (GTK_TEXT_BUFFER (sw->buffer), &begin, &end);
301 on_edit_copy (PsppireSyntaxWindow *sw)
303 GtkTextIter begin, end;
305 set_clip (sw, &begin, &end);
309 /* A callback for when the clipboard contents have been received */
311 contents_received_callback (GtkClipboard *clipboard,
312 GtkSelectionData *sd,
316 PsppireSyntaxWindow *syntax_window = data;
318 if ( sd->length < 0 )
321 if ( sd->type != gdk_atom_intern ("UTF8_STRING", FALSE))
324 c = (gchar *) sd->data;
326 gtk_text_buffer_insert_at_cursor (GTK_TEXT_BUFFER (syntax_window->buffer),
333 on_edit_paste (PsppireSyntaxWindow *sw)
335 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (sw));
336 GtkClipboard *clipboard =
337 gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
339 gtk_clipboard_request_contents (clipboard,
340 gdk_atom_intern ("UTF8_STRING", TRUE),
341 contents_received_callback,
346 /* Check to see if CLIP holds a target which we know how to paste,
347 and set the sensitivity of the Paste action accordingly.
350 set_paste_sensitivity (GtkClipboard *clip, GdkEventOwnerChange *event, gpointer data)
353 gboolean compatible_target = FALSE;
354 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (data);
356 for (i = 0 ; i < sizeof (targets) / sizeof (targets[0]) ; ++i)
358 GdkAtom atom = gdk_atom_intern (targets[i].target, TRUE);
359 if ( gtk_clipboard_wait_is_target_available (clip, atom))
361 compatible_target = TRUE;
366 gtk_action_set_sensitive (sw->edit_paste, compatible_target);
372 /* Parse and execute all the text in the buffer */
374 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
376 GtkTextIter begin, end;
377 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
379 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &begin, 0);
380 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &end, -1);
382 editor_execute_syntax (se, begin, end);
385 /* Parse and execute the currently selected text */
387 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
389 GtkTextIter begin, end;
390 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
392 if ( gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (se->buffer), &begin, &end) )
393 editor_execute_syntax (se, begin, end);
397 /* Parse and execute the from the current line, to the end of the
400 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
402 GtkTextIter begin, end;
406 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
408 /* Get the current line */
409 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
411 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
414 line = gtk_text_iter_get_line (&here) ;
416 /* Now set begin and end to the start of this line, and end of buffer
418 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
419 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, -1);
421 editor_execute_syntax (se, begin, end);
426 /* Parse and execute the current line */
428 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
430 GtkTextIter begin, end;
434 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
436 /* Get the current line */
437 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
439 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
442 line = gtk_text_iter_get_line (&here) ;
444 /* Now set begin and end to the start of this line, and start of
445 following line respectively */
446 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
447 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, line + 1);
449 editor_execute_syntax (se, begin, end);
454 /* Append ".sps" to FILENAME if necessary.
455 The returned result must be freed when no longer required.
458 append_suffix (const gchar *filename)
460 if ( ! g_str_has_suffix (filename, ".sps" ) &&
461 ! g_str_has_suffix (filename, ".SPS" ) )
463 return g_strdup_printf ("%s.sps", filename);
466 return xstrdup (filename);
470 Save BUFFER to the file called FILENAME.
471 FILENAME must be encoded in Glib filename encoding.
472 If successful, clears the buffer's modified flag.
475 save_editor_to_file (PsppireSyntaxWindow *se,
476 const gchar *filename,
479 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (se->buffer);
481 GtkTextIter start, stop;
487 suffixedname = append_suffix (filename);
489 gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
490 gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
492 text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
494 result = g_file_set_contents (suffixedname, text, -1, err);
496 g_free (suffixedname);
500 char *fn = g_filename_display_name (filename);
501 gchar *msg = g_strdup_printf (_("Saved file `%s'"), fn);
503 gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
504 gtk_text_buffer_set_modified (buffer, FALSE);
512 /* Callback for the File->SaveAs menuitem */
514 syntax_save_as (PsppireWindow *se)
516 GtkFileFilter *filter;
520 gtk_file_chooser_dialog_new (_("Save Syntax"),
522 GTK_FILE_CHOOSER_ACTION_SAVE,
523 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
524 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
527 filter = gtk_file_filter_new ();
528 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
529 gtk_file_filter_add_pattern (filter, "*.sps");
530 gtk_file_filter_add_pattern (filter, "*.SPS");
531 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
533 filter = gtk_file_filter_new ();
534 gtk_file_filter_set_name (filter, _("All Files"));
535 gtk_file_filter_add_pattern (filter, "*");
536 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
538 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
540 response = gtk_dialog_run (GTK_DIALOG (dialog));
542 if ( response == GTK_RESPONSE_ACCEPT )
546 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
548 if ( ! save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err) )
550 msg ( ME, "%s", err->message );
557 gtk_widget_destroy (dialog);
561 /* Callback for the File->Save menuitem */
563 syntax_save (PsppireWindow *se)
565 const gchar *filename = psppire_window_get_filename (se);
567 if ( filename == NULL )
572 save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
575 msg (ME, "%s", err->message);
582 /* Callback for the File->Quit menuitem */
584 on_quit (GtkMenuItem *menuitem, gpointer user_data)
593 load_and_show_syntax_window (GtkWidget *se, const gchar *filename)
597 gtk_source_buffer_begin_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
598 ok = psppire_window_load (PSPPIRE_WINDOW (se), filename);
599 gtk_source_buffer_end_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
602 gtk_widget_show (se);
604 gtk_widget_destroy (se);
608 create_syntax_window (void)
610 GtkWidget *w = psppire_syntax_window_new ();
615 open_new_syntax_window (const char *file_name)
617 GtkWidget *se = psppire_syntax_window_new ();
620 load_and_show_syntax_window (se, file_name);
625 static void psppire_syntax_window_print (PsppireSyntaxWindow *window);
628 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
630 if (gtk_text_buffer_get_modified (buffer))
631 psppire_window_set_unsaved (window);
634 extern struct source_stream *the_source_stream ;
636 static void undo_redo_update (PsppireSyntaxWindow *window);
637 static void undo_last_edit (PsppireSyntaxWindow *window);
638 static void redo_last_edit (PsppireSyntaxWindow *window);
641 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
643 gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
644 undo_redo_update (window);
648 psppire_syntax_window_init (PsppireSyntaxWindow *window)
650 GtkBuilder *xml = builder_new ("syntax-editor.ui");
651 GtkWidget *box = gtk_vbox_new (FALSE, 0);
653 GtkWidget *menubar = get_widget_assert (xml, "menubar");
654 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
656 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
658 PsppireSyntaxWindowClass *class
659 = PSPPIRE_SYNTAX_WINDOW_CLASS (G_OBJECT_GET_CLASS (window));
661 GtkClipboard *clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
662 GtkClipboard *clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
665 window->print_settings = NULL;
666 window->undo_menuitem = get_action_assert (xml, "edit_undo");
667 window->redo_menuitem = get_action_assert (xml, "edit_redo");
670 window->buffer = gtk_source_buffer_new_with_language (class->lan);
672 window->buffer = gtk_source_buffer_new (NULL);
674 gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), GTK_TEXT_BUFFER (window->buffer));
676 g_object_set (window->buffer,
677 "highlight-matching-brackets", TRUE,
680 g_object_set (text_view,
681 "show-line-numbers", TRUE,
682 "show-line-marks", TRUE,
685 "highlight-current-line", TRUE,
688 window->cliptext = NULL;
689 window->dispose_has_run = FALSE;
691 window->edit_delete = get_action_assert (xml, "edit_delete");
692 window->edit_copy = get_action_assert (xml, "edit_copy");
693 window->edit_cut = get_action_assert (xml, "edit_cut");
694 window->edit_paste = get_action_assert (xml, "edit_paste");
696 window->lexer = lex_create (the_source_stream);
698 window->sb = get_widget_assert (xml, "statusbar2");
699 window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
701 g_signal_connect (window->buffer, "changed",
702 G_CALLBACK (on_text_changed), window);
704 g_signal_connect (window->buffer, "modified-changed",
705 G_CALLBACK (on_modified_changed), window);
707 g_signal_connect_swapped (get_action_assert (xml, "file_print"), "activate",
708 G_CALLBACK (psppire_syntax_window_print), window);
711 g_signal_connect_swapped (window->undo_menuitem,
713 G_CALLBACK (undo_last_edit),
716 g_signal_connect_swapped (window->redo_menuitem,
718 G_CALLBACK (redo_last_edit),
721 undo_redo_update (window);
723 window->sel_handler = g_signal_connect_swapped (clip_primary, "owner-change",
724 G_CALLBACK (selection_changed), window);
726 window->ps_handler = g_signal_connect (clip_selection, "owner-change",
727 G_CALLBACK (set_paste_sensitivity), window);
731 gtk_container_add (GTK_CONTAINER (window), box);
733 g_object_ref (menubar);
737 g_object_ref (window->sb);
739 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
740 gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
741 gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
743 gtk_widget_show_all (box);
745 g_signal_connect_swapped (get_action_assert (xml,"file_new_syntax"), "activate", G_CALLBACK (create_syntax_window), NULL);
748 g_signal_connect (get_action_assert (xml,"file_new_data"),
750 G_CALLBACK (create_data_window),
754 g_signal_connect_swapped (get_action_assert (xml, "file_save"),
756 G_CALLBACK (syntax_save),
759 g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
761 G_CALLBACK (syntax_save_as),
764 g_signal_connect (get_action_assert (xml,"file_quit"),
766 G_CALLBACK (on_quit),
769 g_signal_connect_swapped (window->edit_delete,
771 G_CALLBACK (on_edit_delete),
774 g_signal_connect_swapped (window->edit_copy,
776 G_CALLBACK (on_edit_copy),
779 g_signal_connect_swapped (window->edit_cut,
781 G_CALLBACK (on_edit_cut),
784 g_signal_connect_swapped (window->edit_paste,
786 G_CALLBACK (on_edit_paste),
789 g_signal_connect (get_action_assert (xml,"run_all"),
791 G_CALLBACK (on_run_all),
794 g_signal_connect (get_action_assert (xml,"run_selection"),
796 G_CALLBACK (on_run_selection),
799 g_signal_connect (get_action_assert (xml,"run_current_line"),
801 G_CALLBACK (on_run_current_line),
804 g_signal_connect (get_action_assert (xml,"run_to_end"),
806 G_CALLBACK (on_run_to_end),
809 g_signal_connect (get_action_assert (xml,"windows_minimise_all"),
811 G_CALLBACK (psppire_window_minimise_all), NULL);
818 GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
820 merge_help_menu (uim);
822 PSPPIRE_WINDOW (window)->menu =
823 GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
826 g_object_unref (xml);
834 psppire_syntax_window_new (void)
836 return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
837 /* TRANSLATORS: This will form a filename. Please avoid whitespace. */
838 "filename", _("Syntax"),
839 "description", _("Syntax Editor"),
844 error_dialog (GtkWindow *w, const gchar *filename, GError *err)
846 gchar *fn = g_filename_display_basename (filename);
849 gtk_message_dialog_new (w,
850 GTK_DIALOG_DESTROY_WITH_PARENT,
853 _("Cannot load syntax file `%s'"),
858 g_object_set (dialog, "icon-name", "psppicon", NULL);
860 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
863 gtk_dialog_run (GTK_DIALOG (dialog));
865 gtk_widget_destroy (dialog);
869 Loads the buffer from the file called FILENAME
872 syntax_load (PsppireWindow *window, const gchar *filename)
875 gchar *text_locale = NULL;
876 gchar *text_utf8 = NULL;
877 gsize len_locale = -1;
880 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
881 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
882 /* FIXME: What if it's a very big file ? */
883 if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
885 error_dialog (GTK_WINDOW (window), filename, err);
886 g_clear_error (&err);
890 text_utf8 = g_locale_to_utf8 (text_locale, len_locale, NULL, &len_utf8, &err);
894 if ( text_utf8 == NULL )
896 error_dialog (GTK_WINDOW (window), filename, err);
897 g_clear_error (&err);
901 gtk_text_buffer_get_iter_at_line (buffer, &iter, 0);
903 gtk_text_buffer_insert (buffer, &iter, text_utf8, len_utf8);
905 gtk_text_buffer_set_modified (buffer, FALSE);
915 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
917 iface->save = syntax_save;
918 iface->load = syntax_load;
925 undo_redo_update (PsppireSyntaxWindow *window)
927 gtk_action_set_sensitive (window->undo_menuitem,
928 gtk_source_buffer_can_undo (window->buffer));
930 gtk_action_set_sensitive (window->redo_menuitem,
931 gtk_source_buffer_can_redo (window->buffer));
935 undo_last_edit (PsppireSyntaxWindow *window)
937 gtk_source_buffer_undo (window->buffer);
938 undo_redo_update (window);
942 redo_last_edit (PsppireSyntaxWindow *window)
944 gtk_source_buffer_redo (window->buffer);
945 undo_redo_update (window);
950 /* Printing related stuff */
954 begin_print (GtkPrintOperation *operation,
955 GtkPrintContext *context,
956 PsppireSyntaxWindow *window)
959 gtk_source_print_compositor_new (window->buffer);
964 end_print (GtkPrintOperation *operation,
965 GtkPrintContext *context,
966 PsppireSyntaxWindow *window)
968 g_object_unref (window->compositor);
969 window->compositor = NULL;
975 paginate (GtkPrintOperation *operation,
976 GtkPrintContext *context,
977 PsppireSyntaxWindow *window)
979 if (gtk_source_print_compositor_paginate (window->compositor, context))
981 gint n_pages = gtk_source_print_compositor_get_n_pages (window->compositor);
982 gtk_print_operation_set_n_pages (operation, n_pages);
991 draw_page (GtkPrintOperation *operation,
992 GtkPrintContext *context,
994 PsppireSyntaxWindow *window)
996 gtk_source_print_compositor_draw_page (window->compositor,
1004 psppire_syntax_window_print (PsppireSyntaxWindow *window)
1006 GtkPrintOperationResult res;
1008 GtkPrintOperation *print = gtk_print_operation_new ();
1010 if (window->print_settings != NULL)
1011 gtk_print_operation_set_print_settings (print, window->print_settings);
1014 g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
1015 g_signal_connect (print, "end_print", G_CALLBACK (end_print), window);
1016 g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), window);
1017 g_signal_connect (print, "paginate", G_CALLBACK (paginate), window);
1019 res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1020 GTK_WINDOW (window), NULL);
1022 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
1024 if (window->print_settings != NULL)
1025 g_object_unref (window->print_settings);
1026 window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
1029 g_object_unref (print);