1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2009, 2010, 2011, 2012, 2014, 2016,
3 2020 Free Software Foundation
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include <gtksourceview/gtksource.h>
25 #include "language/lexer/lexer.h"
26 #include "libpspp/encoding-guesser.h"
27 #include "libpspp/i18n.h"
28 #include "libpspp/message.h"
29 #include "ui/gui/executor.h"
30 #include "ui/gui/help-menu.h"
31 #include "ui/gui/helper.h"
32 #include "ui/gui/builder-wrapper.h"
33 #include "ui/gui/psppire-data-window.h"
34 #include "ui/gui/psppire-encoding-selector.h"
35 #include "ui/gui/psppire-lex-reader.h"
36 #include "ui/gui/psppire-syntax-window.h"
37 #include "ui/gui/psppire.h"
38 #include "ui/gui/windows-menu.h"
40 #include "gl/localcharset.h"
41 #include "gl/xalloc.h"
42 #include "gl/xvasprintf.h"
45 #define _(msgid) gettext (msgid)
46 #define N_(msgid) msgid
48 static void psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class);
49 static void psppire_syntax_window_init (PsppireSyntaxWindow *syntax_editor);
52 static void psppire_syntax_window_iface_init (PsppireWindowIface *iface);
63 psppire_syntax_window_set_property (GObject *object,
68 PsppireSyntaxWindow *window = PSPPIRE_SYNTAX_WINDOW (object);
73 g_free (window->encoding);
74 window->encoding = g_value_dup_string (value);
77 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
84 psppire_syntax_window_get_property (GObject *object,
89 PsppireSyntaxWindow *window = PSPPIRE_SYNTAX_WINDOW (object);
94 g_value_set_string (value, window->encoding);
97 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
102 G_DEFINE_TYPE_WITH_CODE (PsppireSyntaxWindow, psppire_syntax_window, PSPPIRE_TYPE_WINDOW,
103 G_IMPLEMENT_INTERFACE (PSPPIRE_TYPE_WINDOW_MODEL, psppire_syntax_window_iface_init))
105 static GObjectClass *parent_class ;
108 psppire_syntax_window_finalize (GObject *object)
110 if (G_OBJECT_CLASS (parent_class)->finalize)
111 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
116 psppire_syntax_window_dispose (GObject *obj)
118 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (obj);
120 GtkClipboard *clip_selection;
121 GtkClipboard *clip_primary;
123 if (sw->dispose_has_run)
126 g_object_unref (sw->search_text_buffer);
128 g_free (sw->encoding);
131 clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
132 clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_PRIMARY);
134 g_signal_handler_disconnect (clip_primary, sw->sel_handler);
136 g_signal_handler_disconnect (clip_selection, sw->ps_handler);
138 /* Make sure dispose does not run twice. */
139 sw->dispose_has_run = TRUE;
141 /* Chain up to the parent class */
142 G_OBJECT_CLASS (parent_class)->dispose (obj);
148 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
150 GParamSpec *encoding_spec;
151 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
152 gobject_class->finalize = psppire_syntax_window_finalize;
154 GtkSourceLanguageManager *lm = gtk_source_language_manager_get_default ();
156 const gchar * const *existing_paths = gtk_source_language_manager_get_search_path (lm);
157 gchar **new_paths = g_strdupv ((gchar **)existing_paths);
158 int n = g_strv_length ((gchar **) existing_paths);
160 new_paths = g_realloc (new_paths, (n + 2) * sizeof (*new_paths));
161 new_paths[n] = g_strdup (relocate (PKGDATADIR));
162 new_paths[n+1] = NULL;
164 lm = gtk_source_language_manager_new ();
165 gtk_source_language_manager_set_search_path (lm, new_paths);
167 class->lan = gtk_source_language_manager_get_language (lm, "pspp");
169 if (class->lan == NULL)
170 g_warning ("pspp.lang file not found. Syntax highlighting will not be available.");
172 parent_class = g_type_class_peek_parent (class);
174 g_strfreev (new_paths);
177 null_if_empty_param ("encoding",
178 "Character encoding",
179 "IANA character encoding in this syntax file",
181 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
183 parent_class = g_type_class_peek_parent (class);
185 gobject_class->set_property = psppire_syntax_window_set_property;
186 gobject_class->get_property = psppire_syntax_window_get_property;
187 gobject_class->dispose = psppire_syntax_window_dispose;
189 g_object_class_install_property (gobject_class,
196 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
199 PsppireWindow *win = PSPPIRE_WINDOW (sw);
200 struct lex_reader *reader = lex_reader_for_gtk_text_buffer (GTK_TEXT_BUFFER (sw->buffer), start, stop);
202 lex_reader_set_file_name (reader, psppire_window_get_filename (win));
204 execute_syntax (psppire_default_data_window (), reader);
207 /* Delete the currently selected text */
209 on_edit_delete (PsppireSyntaxWindow *sw)
211 GtkTextIter begin, end;
212 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
214 if (gtk_text_buffer_get_selection_bounds (buffer, &begin, &end))
215 gtk_text_buffer_delete (buffer, &begin, &end);
218 /* Create and run a dialog to collect the search string.
219 In future this might be expanded to include options, for example
220 backward searching, case sensitivity etc. */
222 get_search_text (PsppireSyntaxWindow *parent)
224 const char *search_text = NULL;
225 GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL;
226 GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Text Search"),
235 GtkWidget *content_area =
236 gtk_dialog_get_content_area (GTK_DIALOG (dialog));
238 GtkWidget *box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
239 GtkWidget *label = gtk_label_new (_("Text to search for:"));
240 GtkWidget *entry = gtk_entry_new_with_buffer (parent->search_text_buffer);
242 /* Add the label, and show everything we have added. */
243 gtk_container_add (GTK_CONTAINER (content_area), box);
244 gtk_container_add (GTK_CONTAINER (box), label);
245 gtk_container_add (GTK_CONTAINER (box), entry);
246 gtk_widget_show_all (content_area);
248 int result = gtk_dialog_run (GTK_DIALOG (dialog));
251 case GTK_RESPONSE_OK:
252 search_text = gtk_entry_get_text (GTK_ENTRY (entry));
258 gtk_widget_destroy (dialog);
263 /* What to do when the Find menuitem is called. */
265 on_edit_find (PsppireSyntaxWindow *sw)
267 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
270 const char *target = get_search_text (sw);
275 /* This is a wrap-around search. So start searching one
276 character after the current char. */
277 GtkTextMark *mark = gtk_text_buffer_get_insert (buffer);
278 gtk_text_buffer_get_iter_at_mark (buffer, &begin, mark);
279 gtk_text_iter_forward_char (&begin);
280 if (gtk_text_iter_forward_search (&begin, target, 0,
283 gtk_text_buffer_place_cursor (buffer, &loc);
287 /* If not found, then continue the search from the top
289 gtk_text_buffer_get_start_iter (buffer, &begin);
290 if (gtk_text_iter_forward_search (&begin, target, 0,
293 gtk_text_buffer_place_cursor (buffer, &loc);
299 /* The syntax editor's clipboard deals only with text */
307 selection_changed (PsppireSyntaxWindow *sw)
309 gboolean sel = gtk_text_buffer_get_has_selection (GTK_TEXT_BUFFER (sw->buffer));
311 g_object_set (sw->edit_copy, "enabled", sel, NULL);
312 g_object_set (sw->edit_cut, "enabled", sel, NULL);
313 g_object_set (sw->edit_delete, "enabled", sel, NULL);
316 /* The callback which runs when something request clipboard data */
318 clipboard_get_cb (GtkClipboard *clipboard,
319 GtkSelectionData *selection_data,
323 PsppireSyntaxWindow *sw = data;
324 g_assert (info == SELECT_FMT_TEXT);
326 gtk_selection_data_set (selection_data, gtk_selection_data_get_target (selection_data),
328 (const guchar *) sw->cliptext, strlen (sw->cliptext));
333 clipboard_clear_cb (GtkClipboard *clipboard,
336 PsppireSyntaxWindow *sw = data;
337 g_free (sw->cliptext);
341 static gchar tn1[] = "UTF8_STRING";
342 static gchar tn2[] = "STRING";
343 static gchar tn3[] = "TEXT";
344 static gchar tn4[] = "COMPOUND_TEXT";
345 static gchar tn5[] = "text/plain;charset=utf-8";
346 static gchar tn6[] = "text/plain";
348 static const GtkTargetEntry targets[] = {
349 { tn1, 0, SELECT_FMT_TEXT },
350 { tn2, 0, SELECT_FMT_TEXT },
351 { tn3, 0, SELECT_FMT_TEXT },
352 { tn4, 0, SELECT_FMT_TEXT },
353 { tn5, 0, SELECT_FMT_TEXT },
354 { tn6, 0, SELECT_FMT_TEXT }
358 Store a clip containing the currently selected text.
359 Returns true iff something was set.
360 As a side effect, begin and end will be set to indicate
361 the limits of the selected text.
364 set_clip (PsppireSyntaxWindow *sw, GtkTextIter *begin, GtkTextIter *end)
366 GtkClipboard *clipboard ;
367 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
369 if (! gtk_text_buffer_get_selection_bounds (buffer, begin, end))
372 g_free (sw->cliptext);
373 sw->cliptext = gtk_text_buffer_get_text (buffer, begin, end, FALSE);
376 gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
378 if (!gtk_clipboard_set_with_owner (clipboard, targets,
379 G_N_ELEMENTS (targets),
380 clipboard_get_cb, clipboard_clear_cb,
382 clipboard_clear_cb (clipboard, sw);
388 on_edit_cut (PsppireSyntaxWindow *sw)
390 GtkTextIter begin, end;
392 if (set_clip (sw, &begin, &end))
393 gtk_text_buffer_delete (GTK_TEXT_BUFFER (sw->buffer), &begin, &end);
397 on_edit_copy (PsppireSyntaxWindow *sw)
399 GtkTextIter begin, end;
401 set_clip (sw, &begin, &end);
405 on_edit_paste (PsppireSyntaxWindow *sw)
407 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (sw));
408 GtkClipboard *clipboard =
409 gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
411 gtk_text_buffer_paste_clipboard (GTK_TEXT_BUFFER (sw->buffer), clipboard, NULL, TRUE);
415 /* Check to see if CLIP holds a target which we know how to paste,
416 and set the sensitivity of the Paste action accordingly.
419 set_paste_sensitivity (GtkClipboard *clip, GdkEventOwnerChange *event, gpointer data)
422 gboolean compatible_target = FALSE;
423 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (data);
425 for (i = 0 ; i < sizeof (targets) / sizeof (targets[0]) ; ++i)
427 GdkAtom atom = gdk_atom_intern (targets[i].target, TRUE);
428 if (gtk_clipboard_wait_is_target_available (clip, atom))
430 compatible_target = TRUE;
435 g_object_set (sw->edit_paste, "enabled", compatible_target, NULL);
441 /* Parse and execute all the text in the buffer */
443 on_run_all (PsppireSyntaxWindow *se)
445 GtkTextIter begin, end;
447 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &begin, 0);
448 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &end, -1);
450 editor_execute_syntax (se, begin, end);
453 /* Parse and execute the currently selected text */
455 on_run_selection (PsppireSyntaxWindow *se)
457 GtkTextIter begin, end;
459 if (gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (se->buffer), &begin, &end))
460 editor_execute_syntax (se, begin, end);
464 /* Parse and execute the from the current line, to the end of the
467 on_run_to_end (PsppireSyntaxWindow *se)
469 GtkTextIter begin, end;
473 /* Get the current line */
474 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
476 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
479 line = gtk_text_iter_get_line (&here) ;
481 /* Now set begin and end to the start of this line, and end of buffer
483 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
484 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, -1);
486 editor_execute_syntax (se, begin, end);
491 /* Parse and execute the current line */
493 on_run_current_line (PsppireSyntaxWindow *se)
495 GtkTextIter begin, end;
499 /* Get the current line */
500 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
502 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
505 line = gtk_text_iter_get_line (&here) ;
507 /* Now set begin and end to the start of this line, and start of
508 following line respectively */
509 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
510 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, line + 1);
512 editor_execute_syntax (se, begin, end);
517 /* Append ".sps" to FILENAME if necessary.
518 The returned result must be freed when no longer required.
521 append_suffix (const gchar *filename)
523 if (! g_str_has_suffix (filename, ".sps") &&
524 ! g_str_has_suffix (filename, ".SPS"))
526 return g_strdup_printf ("%s.sps", filename);
529 return xstrdup (filename);
533 Save BUFFER to the file called FILENAME.
534 FILENAME must be encoded in Glib filename encoding.
535 If successful, clears the buffer's modified flag.
538 save_editor_to_file (PsppireSyntaxWindow *se,
539 const gchar *filename,
542 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (se->buffer);
543 struct substring text_locale;
545 GtkTextIter start, stop;
551 suffixedname = append_suffix (filename);
553 gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
554 gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
556 text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
558 text_locale = recode_substring_pool (se->encoding, "UTF-8", ss_cstr (text),
561 result = g_file_set_contents (suffixedname, ss_data (text_locale),
562 ss_length (text_locale), err);
564 ss_dealloc (&text_locale);
565 g_free (suffixedname);
569 char *fn = g_filename_display_name (filename);
570 gchar *msg = g_strdup_printf (_("Saved file `%s'"), fn);
572 gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
573 gtk_text_buffer_set_modified (buffer, FALSE);
581 /* PsppireWindow 'pick_Filename' callback. */
583 syntax_pick_filename (PsppireWindow *window)
585 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (window);
586 const char *default_encoding;
587 GtkFileFilter *filter;
591 gtk_file_chooser_dialog_new (_("Save Syntax"),
593 GTK_FILE_CHOOSER_ACTION_SAVE,
594 _("Cancel"), GTK_RESPONSE_CANCEL,
595 _("Save"), GTK_RESPONSE_ACCEPT,
598 g_object_set (dialog, "local-only", FALSE, NULL);
600 filter = gtk_file_filter_new ();
601 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
602 gtk_file_filter_add_pattern (filter, "*.sps");
603 gtk_file_filter_add_pattern (filter, "*.SPS");
604 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
606 filter = gtk_file_filter_new ();
607 gtk_file_filter_set_name (filter, _("All Files"));
608 gtk_file_filter_add_pattern (filter, "*");
609 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
611 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
614 default_encoding = se->encoding != NULL ? se->encoding : locale_charset ();
615 gtk_file_chooser_set_extra_widget (
616 GTK_FILE_CHOOSER (dialog),
617 psppire_encoding_selector_new (default_encoding, false));
619 response = gtk_dialog_run (GTK_DIALOG (dialog));
621 if (response == GTK_RESPONSE_ACCEPT)
626 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
627 psppire_window_set_filename (window, filename);
630 encoding = psppire_encoding_selector_get_encoding (
631 gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
632 if (encoding != NULL)
634 g_free (se->encoding);
635 se->encoding = encoding;
639 gtk_widget_destroy (dialog);
643 /* PsppireWindow 'save' callback. */
645 syntax_save (PsppireWindow *se)
647 const gchar *filename = psppire_window_get_filename (se);
649 save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
652 msg (ME, "%s", err->message);
659 load_and_show_syntax_window (GtkWidget *se, const gchar *filename,
660 const gchar *encoding)
664 gtk_source_buffer_begin_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
665 ok = psppire_window_load (PSPPIRE_WINDOW (se), filename, encoding, NULL);
666 gtk_source_buffer_end_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
669 gtk_widget_show (se);
671 gtk_widget_destroy (se);
675 create_syntax_window (void)
677 GtkWidget *w = psppire_syntax_window_new (NULL);
683 open_syntax_window (const char *file_name, const gchar *encoding)
685 GtkWidget *se = psppire_syntax_window_new (NULL);
688 load_and_show_syntax_window (se, file_name, encoding);
690 return GTK_WINDOW (se);
695 static void psppire_syntax_window_print (PsppireSyntaxWindow *window);
698 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
700 if (gtk_text_buffer_get_modified (buffer))
701 psppire_window_set_unsaved (window);
704 static void undo_redo_update (PsppireSyntaxWindow *window);
705 static void undo_last_edit (PsppireSyntaxWindow *window);
706 static void redo_last_edit (PsppireSyntaxWindow *window);
709 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
711 gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
712 undo_redo_update (window);
716 psppire_syntax_window_init (PsppireSyntaxWindow *window)
718 GtkBuilder *xml = builder_new ("syntax-editor.ui");
719 GtkWidget *box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
721 GObject *menu = get_object_assert (xml, "syntax-window-menu", G_TYPE_MENU);
722 GtkWidget *menubar = gtk_menu_bar_new_from_model (G_MENU_MODEL (menu));
724 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
726 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
728 PsppireSyntaxWindowClass *class
729 = PSPPIRE_SYNTAX_WINDOW_CLASS (G_OBJECT_GET_CLASS (window));
731 GtkClipboard *clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
732 GtkClipboard *clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
734 window->print_settings = NULL;
736 window->undo_menuitem = g_simple_action_new ("undo", NULL);
737 window->redo_menuitem = g_simple_action_new ("redo", NULL);
739 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->undo_menuitem));
740 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->redo_menuitem));
744 window->buffer = gtk_source_buffer_new_with_language (class->lan);
746 window->buffer = gtk_source_buffer_new (NULL);
748 gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), GTK_TEXT_BUFFER (window->buffer));
750 g_object_set (window->buffer,
751 "highlight-matching-brackets", TRUE,
754 g_object_set (text_view,
755 "show-line-numbers", TRUE,
756 "show-line-marks", TRUE,
759 "highlight-current-line", TRUE,
762 window->encoding = NULL;
764 window->cliptext = NULL;
765 window->dispose_has_run = FALSE;
767 window->search_text_buffer = gtk_entry_buffer_new (NULL, -1);
769 window->edit_delete = g_simple_action_new ("delete", NULL);
770 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_delete));
772 window->edit_copy = g_simple_action_new ("copy", NULL);
773 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_copy));
775 window->edit_cut = g_simple_action_new ("cut", NULL);
776 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_cut));
778 window->edit_paste = g_simple_action_new ("paste", NULL);
779 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_paste));
781 window->edit_find = g_simple_action_new ("find", NULL);
782 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_find));
784 window->buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view)));
786 window->sb = get_widget_assert (xml, "statusbar2");
787 window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
789 g_signal_connect (window->buffer, "changed",
790 G_CALLBACK (on_text_changed), window);
792 g_signal_connect (window->buffer, "modified-changed",
793 G_CALLBACK (on_modified_changed), window);
797 GSimpleAction *print = g_simple_action_new ("print", NULL);
799 g_signal_connect_swapped (print, "activate",
800 G_CALLBACK (psppire_syntax_window_print), window);
802 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (print));
805 g_signal_connect_swapped (window->undo_menuitem,
807 G_CALLBACK (undo_last_edit),
810 g_signal_connect_swapped (window->redo_menuitem,
812 G_CALLBACK (redo_last_edit),
815 undo_redo_update (window);
818 window->sel_handler = g_signal_connect_swapped (clip_primary, "owner-change",
819 G_CALLBACK (selection_changed), window);
821 window->ps_handler = g_signal_connect (clip_selection, "owner-change",
822 G_CALLBACK (set_paste_sensitivity), window);
824 gtk_container_add (GTK_CONTAINER (window), box);
828 g_object_ref (window->sb);
830 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
831 gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
832 gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
834 gtk_widget_show_all (box);
836 GtkApplication *app = GTK_APPLICATION (g_application_get_default ());
839 GSimpleAction *open = g_simple_action_new ("open", NULL);
841 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (open));
843 g_signal_connect_swapped (open,
845 G_CALLBACK (psppire_window_open),
850 GSimpleAction *save = g_simple_action_new ("save", NULL);
852 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (save));
854 g_signal_connect_swapped (save,
856 G_CALLBACK (psppire_window_save),
859 const gchar *accels[2] = { "<Primary>S", NULL};
860 gtk_application_set_accels_for_action (app,
867 GSimpleAction *save_as = g_simple_action_new ("save_as", NULL);
869 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (save_as));
871 g_signal_connect_swapped (save_as,
873 G_CALLBACK (psppire_window_save_as),
877 const gchar *accels[2] = { "<Shift><Primary>S", NULL};
878 gtk_application_set_accels_for_action (app,
886 g_signal_connect_swapped (window->edit_delete,
888 G_CALLBACK (on_edit_delete),
891 g_signal_connect_swapped (window->edit_copy,
893 G_CALLBACK (on_edit_copy),
896 g_signal_connect_swapped (window->edit_cut,
898 G_CALLBACK (on_edit_cut),
901 g_signal_connect_swapped (window->edit_paste,
903 G_CALLBACK (on_edit_paste),
906 g_signal_connect_swapped (window->edit_find,
908 G_CALLBACK (on_edit_find),
912 GSimpleAction *run_all = g_simple_action_new ("run-all", NULL);
914 g_signal_connect_swapped (run_all, "activate",
915 G_CALLBACK (on_run_all), window);
917 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (run_all));
921 GSimpleAction *run_current_line = g_simple_action_new ("run-current-line", NULL);
923 g_signal_connect_swapped (run_current_line, "activate",
924 G_CALLBACK (on_run_current_line), window);
926 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (run_current_line));
928 GtkApplication *app = GTK_APPLICATION (g_application_get_default ());
929 const gchar *accels[2] = { "<Primary>R", NULL};
930 gtk_application_set_accels_for_action (app,
931 "win.run-current-line",
936 GSimpleAction *run_selection = g_simple_action_new ("run-selection", NULL);
938 g_signal_connect_swapped (run_selection, "activate",
939 G_CALLBACK (on_run_selection), window);
941 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (run_selection));
945 GSimpleAction *run_to_end = g_simple_action_new ("run-to-end", NULL);
947 g_signal_connect_swapped (run_to_end, "activate",
948 G_CALLBACK (on_run_to_end), window);
950 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (run_to_end));
953 gtk_menu_shell_append (GTK_MENU_SHELL (menubar),
954 create_windows_menu (GTK_WINDOW (window)));
956 gtk_menu_shell_append (GTK_MENU_SHELL (menubar),
957 create_help_menu (GTK_WINDOW (window)));
959 g_object_unref (xml);
967 psppire_syntax_window_new (const char *encoding)
969 GObject *sw = g_object_new (psppire_syntax_window_get_type (),
970 "description", _("Syntax Editor"),
971 "encoding", encoding,
974 GApplication *app = g_application_get_default ();
975 gtk_application_add_window (GTK_APPLICATION (app), GTK_WINDOW (sw));
977 return GTK_WIDGET (sw);
981 error_dialog (GtkWindow *w, const gchar *filename, GError *err)
983 gchar *fn = g_filename_display_basename (filename);
986 gtk_message_dialog_new (w,
987 GTK_DIALOG_DESTROY_WITH_PARENT,
990 _("Cannot load syntax file `%s'"),
995 g_object_set (dialog, "icon-name", "pspp", NULL);
997 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1000 gtk_dialog_run (GTK_DIALOG (dialog));
1002 gtk_widget_destroy (dialog);
1006 Loads the buffer from the file called FILENAME
1009 syntax_load (PsppireWindow *window, const gchar *filename,
1010 const gchar *encoding, gpointer not_used)
1013 gchar *text_locale = NULL;
1014 gchar *text_utf8 = NULL;
1015 gsize len_locale = -1;
1016 gsize len_utf8 = -1;
1018 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
1019 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
1021 /* FIXME: What if it's a very big file ? */
1022 if (! g_file_get_contents (filename, &text_locale, &len_locale, &err))
1024 error_dialog (GTK_WINDOW (window), filename, err);
1025 g_clear_error (&err);
1029 if (!encoding || !encoding[0])
1031 /* Determine the file's encoding and update sw->encoding. (The ordering
1032 is important here because encoding_guess_whole_file() often returns
1033 its argument instead of a copy of it.) */
1034 char *guessed_encoding;
1036 guessed_encoding = g_strdup (encoding_guess_whole_file (sw->encoding,
1039 g_free (sw->encoding);
1040 sw->encoding = guessed_encoding;
1044 g_free (sw->encoding);
1045 sw->encoding = g_strdup (encoding);
1048 text_utf8 = recode_substring_pool ("UTF-8", sw->encoding,
1049 ss_buffer (text_locale, len_locale),
1053 if (text_utf8 == NULL)
1055 error_dialog (GTK_WINDOW (window), filename, err);
1056 g_clear_error (&err);
1060 gtk_text_buffer_get_iter_at_line (buffer, &iter, 0);
1062 gtk_text_buffer_insert (buffer, &iter, text_utf8, len_utf8);
1064 gtk_text_buffer_set_modified (buffer, FALSE);
1068 add_most_recent (filename, "text/x-spss-syntax", sw->encoding);
1076 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
1078 iface->save = syntax_save;
1079 iface->pick_filename = syntax_pick_filename;
1080 iface->load = syntax_load;
1087 undo_redo_update (PsppireSyntaxWindow *window)
1089 g_object_set (window->undo_menuitem, "enabled",
1090 gtk_source_buffer_can_undo (window->buffer), NULL);
1092 g_object_set (window->redo_menuitem, "enabled",
1093 gtk_source_buffer_can_redo (window->buffer), NULL);
1097 undo_last_edit (PsppireSyntaxWindow *window)
1099 gtk_source_buffer_undo (window->buffer);
1100 undo_redo_update (window);
1104 redo_last_edit (PsppireSyntaxWindow *window)
1106 gtk_source_buffer_redo (window->buffer);
1107 undo_redo_update (window);
1112 /* Printing related stuff */
1116 begin_print (GtkPrintOperation *operation,
1117 GtkPrintContext *context,
1118 PsppireSyntaxWindow *window)
1120 window->compositor =
1121 gtk_source_print_compositor_new (window->buffer);
1126 end_print (GtkPrintOperation *operation,
1127 GtkPrintContext *context,
1128 PsppireSyntaxWindow *window)
1130 g_object_unref (window->compositor);
1131 window->compositor = NULL;
1137 paginate (GtkPrintOperation *operation,
1138 GtkPrintContext *context,
1139 PsppireSyntaxWindow *window)
1141 if (gtk_source_print_compositor_paginate (window->compositor, context))
1143 gint n_pages = gtk_source_print_compositor_get_n_pages (window->compositor);
1144 gtk_print_operation_set_n_pages (operation, n_pages);
1153 draw_page (GtkPrintOperation *operation,
1154 GtkPrintContext *context,
1156 PsppireSyntaxWindow *window)
1158 gtk_source_print_compositor_draw_page (window->compositor,
1166 psppire_syntax_window_print (PsppireSyntaxWindow *window)
1168 GtkPrintOperationResult res;
1170 GtkPrintOperation *print = gtk_print_operation_new ();
1172 if (window->print_settings != NULL)
1173 gtk_print_operation_set_print_settings (print, window->print_settings);
1176 g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
1177 g_signal_connect (print, "end_print", G_CALLBACK (end_print), window);
1178 g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), window);
1179 g_signal_connect (print, "paginate", G_CALLBACK (paginate), window);
1181 res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1182 GTK_WINDOW (window), NULL);
1184 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
1186 if (window->print_settings != NULL)
1187 g_object_unref (window->print_settings);
1188 window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
1191 g_object_unref (print);