1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2009, 2010, 2011, 2012, 2014, 2016 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/>. */
22 #include <gtksourceview/gtksourcebuffer.h>
23 #include <gtksourceview/gtksourcelanguage.h>
24 #include <gtksourceview/gtksourcelanguagemanager.h>
25 #include <gtksourceview/gtksourceprintcompositor.h>
27 #include "language/lexer/lexer.h"
28 #include "libpspp/encoding-guesser.h"
29 #include "libpspp/i18n.h"
30 #include "libpspp/message.h"
31 #include "ui/gui/executor.h"
32 #include "ui/gui/help-menu.h"
33 #include "ui/gui/helper.h"
34 #include "ui/gui/builder-wrapper.h"
35 #include "ui/gui/psppire-data-window.h"
36 #include "ui/gui/psppire-encoding-selector.h"
37 #include "ui/gui/psppire-lex-reader.h"
38 #include "ui/gui/psppire-syntax-window.h"
39 #include "ui/gui/psppire.h"
40 #include "ui/gui/windows-menu.h"
42 #include "gl/localcharset.h"
43 #include "gl/xalloc.h"
44 #include "gl/xvasprintf.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);
67 psppire_syntax_window_set_property (GObject *object,
72 PsppireSyntaxWindow *window = PSPPIRE_SYNTAX_WINDOW (object);
77 g_free (window->encoding);
78 window->encoding = g_value_dup_string (value);
81 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
88 psppire_syntax_window_get_property (GObject *object,
93 PsppireSyntaxWindow *window = PSPPIRE_SYNTAX_WINDOW (object);
98 g_value_set_string (value, window->encoding);
101 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
107 psppire_syntax_window_get_type (void)
109 static GType psppire_syntax_window_type = 0;
111 if (!psppire_syntax_window_type)
113 static const GTypeInfo psppire_syntax_window_info =
115 sizeof (PsppireSyntaxWindowClass),
116 (GBaseInitFunc) psppire_syntax_window_base_init,
117 (GBaseFinalizeFunc) psppire_syntax_window_base_finalize,
118 (GClassInitFunc)psppire_syntax_window_class_init,
119 (GClassFinalizeFunc) NULL,
121 sizeof (PsppireSyntaxWindow),
123 (GInstanceInitFunc) psppire_syntax_window_init,
126 static const GInterfaceInfo window_interface_info =
128 (GInterfaceInitFunc) psppire_syntax_window_iface_init,
133 psppire_syntax_window_type =
134 g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireSyntaxWindow",
135 &psppire_syntax_window_info, 0);
137 g_type_add_interface_static (psppire_syntax_window_type,
138 PSPPIRE_TYPE_WINDOW_MODEL,
139 &window_interface_info);
142 return psppire_syntax_window_type;
145 static GObjectClass *parent_class ;
148 psppire_syntax_window_finalize (GObject *object)
150 if (G_OBJECT_CLASS (parent_class)->finalize)
151 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
156 psppire_syntax_window_dispose (GObject *obj)
158 PsppireSyntaxWindow *sw = (PsppireSyntaxWindow *)obj;
160 GtkClipboard *clip_selection;
161 GtkClipboard *clip_primary;
163 if (sw->dispose_has_run)
166 g_free (sw->encoding);
169 clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
170 clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_PRIMARY);
172 g_signal_handler_disconnect (clip_primary, sw->sel_handler);
174 g_signal_handler_disconnect (clip_selection, sw->ps_handler);
176 /* Make sure dispose does not run twice. */
177 sw->dispose_has_run = TRUE;
179 /* Chain up to the parent class */
180 G_OBJECT_CLASS (parent_class)->dispose (obj);
186 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
188 GParamSpec *encoding_spec;
189 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
191 GtkSourceLanguageManager *lm = gtk_source_language_manager_get_default ();
193 const gchar * const *existing_paths = gtk_source_language_manager_get_search_path (lm);
194 gchar **new_paths = g_strdupv ((gchar **)existing_paths);
195 int n = g_strv_length ((gchar **) existing_paths);
197 new_paths = g_realloc (new_paths, (n + 2) * sizeof (*new_paths));
198 new_paths[n] = g_strdup (relocate (PKGDATADIR));
199 new_paths[n+1] = NULL;
201 lm = gtk_source_language_manager_new ();
202 gtk_source_language_manager_set_search_path (lm, new_paths);
204 class->lan = gtk_source_language_manager_get_language (lm, "pspp");
206 if (class->lan == NULL)
207 g_warning ("pspp.lang file not found. Syntax highlighting will not be available.");
209 parent_class = g_type_class_peek_parent (class);
211 g_strfreev (new_paths);
214 null_if_empty_param ("encoding",
215 "Character encoding",
216 "IANA character encoding in this syntax file",
218 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
220 parent_class = g_type_class_peek_parent (class);
222 gobject_class->set_property = psppire_syntax_window_set_property;
223 gobject_class->get_property = psppire_syntax_window_get_property;
224 gobject_class->dispose = psppire_syntax_window_dispose;
226 g_object_class_install_property (gobject_class,
233 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
235 GObjectClass *object_class = G_OBJECT_CLASS (class);
236 object_class->finalize = psppire_syntax_window_finalize;
242 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
249 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
252 PsppireWindow *win = PSPPIRE_WINDOW (sw);
253 struct lex_reader *reader = lex_reader_for_gtk_text_buffer (GTK_TEXT_BUFFER (sw->buffer), start, stop);
255 lex_reader_set_file_name (reader, psppire_window_get_filename (win));
257 execute_syntax (psppire_default_data_window (), reader);
260 /* Delete the currently selected text */
262 on_edit_delete (PsppireSyntaxWindow *sw)
264 GtkTextIter begin, end;
265 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
267 if ( gtk_text_buffer_get_selection_bounds (buffer, &begin, &end) )
268 gtk_text_buffer_delete (buffer, &begin, &end);
272 /* The syntax editor's clipboard deals only with text */
280 selection_changed (PsppireSyntaxWindow *sw)
282 gboolean sel = gtk_text_buffer_get_has_selection (GTK_TEXT_BUFFER (sw->buffer));
284 g_object_set (sw->edit_copy, "enabled", sel, NULL);
285 g_object_set (sw->edit_cut, "enabled", sel, NULL);
286 g_object_set (sw->edit_delete, "enabled", sel, NULL);
289 /* The callback which runs when something request clipboard data */
291 clipboard_get_cb (GtkClipboard *clipboard,
292 GtkSelectionData *selection_data,
296 PsppireSyntaxWindow *sw = data;
297 g_assert (info == SELECT_FMT_TEXT);
299 gtk_selection_data_set (selection_data, gtk_selection_data_get_target (selection_data),
301 (const guchar *) sw->cliptext, strlen (sw->cliptext));
306 clipboard_clear_cb (GtkClipboard *clipboard,
309 PsppireSyntaxWindow *sw = data;
310 g_free (sw->cliptext);
315 static const GtkTargetEntry targets[] = {
316 { "UTF8_STRING", 0, SELECT_FMT_TEXT },
317 { "STRING", 0, SELECT_FMT_TEXT },
318 { "TEXT", 0, SELECT_FMT_TEXT },
319 { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
320 { "text/plain;charset=utf-8", 0, SELECT_FMT_TEXT },
321 { "text/plain", 0, SELECT_FMT_TEXT },
326 Store a clip containing the currently selected text.
327 Returns true iff something was set.
328 As a side effect, begin and end will be set to indicate
329 the limits of the selected text.
332 set_clip (PsppireSyntaxWindow *sw, GtkTextIter *begin, GtkTextIter *end)
334 GtkClipboard *clipboard ;
335 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
337 if ( ! gtk_text_buffer_get_selection_bounds (buffer, begin, end) )
340 g_free (sw->cliptext);
341 sw->cliptext = gtk_text_buffer_get_text (buffer, begin, end, FALSE);
344 gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
346 if (!gtk_clipboard_set_with_owner (clipboard, targets,
347 G_N_ELEMENTS (targets),
348 clipboard_get_cb, clipboard_clear_cb,
350 clipboard_clear_cb (clipboard, sw);
356 on_edit_cut (PsppireSyntaxWindow *sw)
358 GtkTextIter begin, end;
360 if ( set_clip (sw, &begin, &end))
361 gtk_text_buffer_delete (GTK_TEXT_BUFFER (sw->buffer), &begin, &end);
365 on_edit_copy (PsppireSyntaxWindow *sw)
367 GtkTextIter begin, end;
369 set_clip (sw, &begin, &end);
373 on_edit_paste (PsppireSyntaxWindow *sw)
375 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (sw));
376 GtkClipboard *clipboard =
377 gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
379 gtk_text_buffer_paste_clipboard (GTK_TEXT_BUFFER (sw->buffer), clipboard, NULL, TRUE);
383 /* Check to see if CLIP holds a target which we know how to paste,
384 and set the sensitivity of the Paste action accordingly.
387 set_paste_sensitivity (GtkClipboard *clip, GdkEventOwnerChange *event, gpointer data)
390 gboolean compatible_target = FALSE;
391 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (data);
393 for (i = 0 ; i < sizeof (targets) / sizeof (targets[0]) ; ++i)
395 GdkAtom atom = gdk_atom_intern (targets[i].target, TRUE);
396 if ( gtk_clipboard_wait_is_target_available (clip, atom))
398 compatible_target = TRUE;
403 g_object_set (sw->edit_paste, "enabled", compatible_target, NULL);
409 /* Parse and execute all the text in the buffer */
411 on_run_all (PsppireSyntaxWindow *se)
413 GtkTextIter begin, end;
415 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &begin, 0);
416 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &end, -1);
418 editor_execute_syntax (se, begin, end);
421 /* Parse and execute the currently selected text */
423 on_run_selection (PsppireSyntaxWindow *se)
425 GtkTextIter begin, end;
427 if ( gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (se->buffer), &begin, &end) )
428 editor_execute_syntax (se, begin, end);
432 /* Parse and execute the from the current line, to the end of the
435 on_run_to_end (PsppireSyntaxWindow *se)
437 GtkTextIter begin, end;
441 /* Get the current line */
442 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
444 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
447 line = gtk_text_iter_get_line (&here) ;
449 /* Now set begin and end to the start of this line, and end of buffer
451 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
452 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, -1);
454 editor_execute_syntax (se, begin, end);
459 /* Parse and execute the current line */
461 on_run_current_line (PsppireSyntaxWindow *se)
463 GtkTextIter begin, end;
467 /* Get the current line */
468 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
470 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
473 line = gtk_text_iter_get_line (&here) ;
475 /* Now set begin and end to the start of this line, and start of
476 following line respectively */
477 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
478 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, line + 1);
480 editor_execute_syntax (se, begin, end);
485 /* Append ".sps" to FILENAME if necessary.
486 The returned result must be freed when no longer required.
489 append_suffix (const gchar *filename)
491 if ( ! g_str_has_suffix (filename, ".sps" ) &&
492 ! g_str_has_suffix (filename, ".SPS" ) )
494 return g_strdup_printf ("%s.sps", filename);
497 return xstrdup (filename);
501 Save BUFFER to the file called FILENAME.
502 FILENAME must be encoded in Glib filename encoding.
503 If successful, clears the buffer's modified flag.
506 save_editor_to_file (PsppireSyntaxWindow *se,
507 const gchar *filename,
510 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (se->buffer);
511 struct substring text_locale;
513 GtkTextIter start, stop;
519 suffixedname = append_suffix (filename);
521 gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
522 gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
524 text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
526 text_locale = recode_substring_pool (se->encoding, "UTF-8", ss_cstr (text),
529 result = g_file_set_contents (suffixedname, ss_data (text_locale),
530 ss_length (text_locale), err);
532 ss_dealloc (&text_locale);
533 g_free (suffixedname);
537 char *fn = g_filename_display_name (filename);
538 gchar *msg = g_strdup_printf (_("Saved file `%s'"), fn);
540 gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
541 gtk_text_buffer_set_modified (buffer, FALSE);
549 /* PsppireWindow 'pick_Filename' callback. */
551 syntax_pick_filename (PsppireWindow *window)
553 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (window);
554 const char *default_encoding;
555 GtkFileFilter *filter;
559 gtk_file_chooser_dialog_new (_("Save Syntax"),
561 GTK_FILE_CHOOSER_ACTION_SAVE,
562 _("Cancel"), GTK_RESPONSE_CANCEL,
563 _("Save"), GTK_RESPONSE_ACCEPT,
566 g_object_set (dialog, "local-only", FALSE, NULL);
568 filter = gtk_file_filter_new ();
569 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
570 gtk_file_filter_add_pattern (filter, "*.sps");
571 gtk_file_filter_add_pattern (filter, "*.SPS");
572 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
574 filter = gtk_file_filter_new ();
575 gtk_file_filter_set_name (filter, _("All Files"));
576 gtk_file_filter_add_pattern (filter, "*");
577 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
579 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
582 default_encoding = se->encoding != NULL ? se->encoding : locale_charset ();
583 gtk_file_chooser_set_extra_widget (
584 GTK_FILE_CHOOSER (dialog),
585 psppire_encoding_selector_new (default_encoding, false));
587 response = gtk_dialog_run (GTK_DIALOG (dialog));
589 if ( response == GTK_RESPONSE_ACCEPT )
594 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
595 psppire_window_set_filename (window, filename);
598 encoding = psppire_encoding_selector_get_encoding (
599 gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
600 if (encoding != NULL)
602 g_free (se->encoding);
603 se->encoding = encoding;
607 gtk_widget_destroy (dialog);
611 /* PsppireWindow 'save' callback. */
613 syntax_save (PsppireWindow *se)
615 const gchar *filename = psppire_window_get_filename (se);
617 save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
620 msg (ME, "%s", err->message);
627 load_and_show_syntax_window (GtkWidget *se, const gchar *filename,
628 const gchar *encoding)
632 gtk_source_buffer_begin_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
633 ok = psppire_window_load (PSPPIRE_WINDOW (se), filename, encoding, NULL);
634 gtk_source_buffer_end_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
637 gtk_widget_show (se);
639 gtk_widget_destroy (se);
643 create_syntax_window (void)
645 GtkWidget *w = psppire_syntax_window_new (NULL);
651 open_syntax_window (const char *file_name, const gchar *encoding)
653 GtkWidget *se = psppire_syntax_window_new (NULL);
656 load_and_show_syntax_window (se, file_name, encoding);
658 return GTK_WINDOW (se);
663 static void psppire_syntax_window_print (PsppireSyntaxWindow *window);
666 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
668 if (gtk_text_buffer_get_modified (buffer))
669 psppire_window_set_unsaved (window);
672 static void undo_redo_update (PsppireSyntaxWindow *window);
673 static void undo_last_edit (PsppireSyntaxWindow *window);
674 static void redo_last_edit (PsppireSyntaxWindow *window);
677 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
679 gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
680 undo_redo_update (window);
684 psppire_syntax_window_init (PsppireSyntaxWindow *window)
686 GtkBuilder *xml = builder_new ("syntax-editor.ui");
687 GtkWidget *box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
689 GObject *menu = get_object_assert (xml, "syntax-window-menu", G_TYPE_MENU);
690 GtkWidget *menubar = gtk_menu_bar_new_from_model (G_MENU_MODEL (menu));
692 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
694 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
696 PsppireSyntaxWindowClass *class
697 = PSPPIRE_SYNTAX_WINDOW_CLASS (G_OBJECT_GET_CLASS (window));
699 GtkClipboard *clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
700 GtkClipboard *clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
702 window->print_settings = NULL;
704 window->undo_menuitem = g_simple_action_new ("undo", NULL);
705 window->redo_menuitem = g_simple_action_new ("redo", NULL);
707 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->undo_menuitem));
708 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->redo_menuitem));
712 window->buffer = gtk_source_buffer_new_with_language (class->lan);
714 window->buffer = gtk_source_buffer_new (NULL);
716 gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), GTK_TEXT_BUFFER (window->buffer));
718 g_object_set (window->buffer,
719 "highlight-matching-brackets", TRUE,
722 g_object_set (text_view,
723 "show-line-numbers", TRUE,
724 "show-line-marks", TRUE,
727 "highlight-current-line", TRUE,
730 window->encoding = NULL;
732 window->cliptext = NULL;
733 window->dispose_has_run = FALSE;
736 window->edit_delete = g_simple_action_new ("delete", NULL);
737 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_delete));
739 window->edit_copy = g_simple_action_new ("copy", NULL);
740 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_copy));
742 window->edit_cut = g_simple_action_new ("cut", NULL);
743 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_cut));
745 window->edit_paste = g_simple_action_new ("paste", NULL);
746 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_paste));
749 window->buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view)));
751 window->sb = get_widget_assert (xml, "statusbar2");
752 window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
754 g_signal_connect (window->buffer, "changed",
755 G_CALLBACK (on_text_changed), window);
757 g_signal_connect (window->buffer, "modified-changed",
758 G_CALLBACK (on_modified_changed), window);
762 GSimpleAction *print = g_simple_action_new ("print", NULL);
764 g_signal_connect_swapped (print, "activate",
765 G_CALLBACK (psppire_syntax_window_print), window);
767 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (print));
770 g_signal_connect_swapped (window->undo_menuitem,
772 G_CALLBACK (undo_last_edit),
775 g_signal_connect_swapped (window->redo_menuitem,
777 G_CALLBACK (redo_last_edit),
780 undo_redo_update (window);
783 window->sel_handler = g_signal_connect_swapped (clip_primary, "owner-change",
784 G_CALLBACK (selection_changed), window);
786 window->ps_handler = g_signal_connect (clip_selection, "owner-change",
787 G_CALLBACK (set_paste_sensitivity), window);
791 gtk_container_add (GTK_CONTAINER (window), box);
795 g_object_ref (window->sb);
797 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
798 gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
799 gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
801 gtk_widget_show_all (box);
803 GtkApplication *app = GTK_APPLICATION (g_application_get_default ());
806 GSimpleAction *open = g_simple_action_new ("open", NULL);
808 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (open));
810 g_signal_connect_swapped (open,
812 G_CALLBACK (psppire_window_open),
817 GSimpleAction *save = g_simple_action_new ("save", NULL);
819 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (save));
821 g_signal_connect_swapped (save,
823 G_CALLBACK (psppire_window_save),
826 const gchar *accels[2] = { "<Ctrl>S", NULL};
827 gtk_application_set_accels_for_action (app,
834 GSimpleAction *save_as = g_simple_action_new ("save_as", NULL);
836 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (save_as));
838 g_signal_connect_swapped (save_as,
840 G_CALLBACK (psppire_window_save_as),
844 const gchar *accels[2] = { "<Shift><Ctrl>S", NULL};
845 gtk_application_set_accels_for_action (app,
853 g_signal_connect_swapped (window->edit_delete,
855 G_CALLBACK (on_edit_delete),
858 g_signal_connect_swapped (window->edit_copy,
860 G_CALLBACK (on_edit_copy),
863 g_signal_connect_swapped (window->edit_cut,
865 G_CALLBACK (on_edit_cut),
868 g_signal_connect_swapped (window->edit_paste,
870 G_CALLBACK (on_edit_paste),
874 GSimpleAction *run_all = g_simple_action_new ("run-all", NULL);
876 g_signal_connect_swapped (run_all, "activate",
877 G_CALLBACK (on_run_all), window);
879 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (run_all));
883 GSimpleAction *run_current_line = g_simple_action_new ("run-current-line", NULL);
885 g_signal_connect_swapped (run_current_line, "activate",
886 G_CALLBACK (on_run_current_line), window);
888 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (run_current_line));
890 GtkApplication *app = GTK_APPLICATION (g_application_get_default ());
891 const gchar *accels[2] = { "<Ctrl>R", NULL};
892 gtk_application_set_accels_for_action (app,
893 "win.run-current-line",
898 GSimpleAction *run_selection = g_simple_action_new ("run-selection", NULL);
900 g_signal_connect_swapped (run_selection, "activate",
901 G_CALLBACK (on_run_selection), window);
903 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (run_selection));
907 GSimpleAction *run_to_end = g_simple_action_new ("run-to-end", NULL);
909 g_signal_connect_swapped (run_to_end, "activate",
910 G_CALLBACK (on_run_to_end), window);
912 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (run_to_end));
915 gtk_menu_shell_append (GTK_MENU_SHELL (menubar),
916 create_windows_menu (GTK_WINDOW (window)));
918 gtk_menu_shell_append (GTK_MENU_SHELL (menubar),
919 create_help_menu (GTK_WINDOW (window)));
921 g_object_unref (xml);
929 psppire_syntax_window_new (const char *encoding)
931 GObject *sw = g_object_new (psppire_syntax_window_get_type (),
932 "description", _("Syntax Editor"),
933 "encoding", encoding,
936 GApplication *app = g_application_get_default ();
937 gtk_application_add_window (GTK_APPLICATION (app), GTK_WINDOW (sw));
939 return GTK_WIDGET (sw);
943 error_dialog (GtkWindow *w, const gchar *filename, GError *err)
945 gchar *fn = g_filename_display_basename (filename);
948 gtk_message_dialog_new (w,
949 GTK_DIALOG_DESTROY_WITH_PARENT,
952 _("Cannot load syntax file `%s'"),
957 g_object_set (dialog, "icon-name", "pspp", NULL);
959 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
962 gtk_dialog_run (GTK_DIALOG (dialog));
964 gtk_widget_destroy (dialog);
968 Loads the buffer from the file called FILENAME
971 syntax_load (PsppireWindow *window, const gchar *filename,
972 const gchar *encoding, gpointer not_used)
975 gchar *text_locale = NULL;
976 gchar *text_utf8 = NULL;
977 gsize len_locale = -1;
980 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
981 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
983 /* FIXME: What if it's a very big file ? */
984 if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
986 error_dialog (GTK_WINDOW (window), filename, err);
987 g_clear_error (&err);
991 if (!encoding || !encoding[0])
993 /* Determine the file's encoding and update sw->encoding. (The ordering
994 is important here because encoding_guess_whole_file() often returns
995 its argument instead of a copy of it.) */
996 char *guessed_encoding;
998 guessed_encoding = g_strdup (encoding_guess_whole_file (sw->encoding,
1001 g_free (sw->encoding);
1002 sw->encoding = guessed_encoding;
1006 g_free (sw->encoding);
1007 sw->encoding = g_strdup (encoding);
1010 text_utf8 = recode_substring_pool ("UTF-8", sw->encoding,
1011 ss_buffer (text_locale, len_locale),
1015 if ( text_utf8 == NULL )
1017 error_dialog (GTK_WINDOW (window), filename, err);
1018 g_clear_error (&err);
1022 gtk_text_buffer_get_iter_at_line (buffer, &iter, 0);
1024 gtk_text_buffer_insert (buffer, &iter, text_utf8, len_utf8);
1026 gtk_text_buffer_set_modified (buffer, FALSE);
1030 add_most_recent (filename, "text/x-spss-syntax", sw->encoding);
1038 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
1040 iface->save = syntax_save;
1041 iface->pick_filename = syntax_pick_filename;
1042 iface->load = syntax_load;
1049 undo_redo_update (PsppireSyntaxWindow *window)
1051 g_object_set (window->undo_menuitem, "enabled",
1052 gtk_source_buffer_can_undo (window->buffer), NULL);
1054 g_object_set (window->redo_menuitem, "enabled",
1055 gtk_source_buffer_can_redo (window->buffer), NULL);
1059 undo_last_edit (PsppireSyntaxWindow *window)
1061 gtk_source_buffer_undo (window->buffer);
1062 undo_redo_update (window);
1066 redo_last_edit (PsppireSyntaxWindow *window)
1068 gtk_source_buffer_redo (window->buffer);
1069 undo_redo_update (window);
1074 /* Printing related stuff */
1078 begin_print (GtkPrintOperation *operation,
1079 GtkPrintContext *context,
1080 PsppireSyntaxWindow *window)
1082 window->compositor =
1083 gtk_source_print_compositor_new (window->buffer);
1088 end_print (GtkPrintOperation *operation,
1089 GtkPrintContext *context,
1090 PsppireSyntaxWindow *window)
1092 g_object_unref (window->compositor);
1093 window->compositor = NULL;
1099 paginate (GtkPrintOperation *operation,
1100 GtkPrintContext *context,
1101 PsppireSyntaxWindow *window)
1103 if (gtk_source_print_compositor_paginate (window->compositor, context))
1105 gint n_pages = gtk_source_print_compositor_get_n_pages (window->compositor);
1106 gtk_print_operation_set_n_pages (operation, n_pages);
1115 draw_page (GtkPrintOperation *operation,
1116 GtkPrintContext *context,
1118 PsppireSyntaxWindow *window)
1120 gtk_source_print_compositor_draw_page (window->compositor,
1128 psppire_syntax_window_print (PsppireSyntaxWindow *window)
1130 GtkPrintOperationResult res;
1132 GtkPrintOperation *print = gtk_print_operation_new ();
1134 if (window->print_settings != NULL)
1135 gtk_print_operation_set_print_settings (print, window->print_settings);
1138 g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
1139 g_signal_connect (print, "end_print", G_CALLBACK (end_print), window);
1140 g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), window);
1141 g_signal_connect (print, "paginate", G_CALLBACK (paginate), window);
1143 res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1144 GTK_WINDOW (window), NULL);
1146 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
1148 if (window->print_settings != NULL)
1149 g_object_unref (window->print_settings);
1150 window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
1153 g_object_unref (print);