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 (
201 GTK_TEXT_BUFFER (sw->buffer), start, stop, sw->syntax_mode);
203 lex_reader_set_file_name (reader, psppire_window_get_filename (win));
205 execute_syntax (psppire_default_data_window (), reader);
208 /* Delete the currently selected text */
210 on_edit_delete (PsppireSyntaxWindow *sw)
212 GtkTextIter begin, end;
213 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
215 if (gtk_text_buffer_get_selection_bounds (buffer, &begin, &end))
216 gtk_text_buffer_delete (buffer, &begin, &end);
219 /* Create and run a dialog to collect the search string.
220 In future this might be expanded to include options, for example
221 backward searching, case sensitivity etc. */
223 get_search_text (PsppireSyntaxWindow *parent)
225 const char *search_text = NULL;
226 GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_MODAL;
227 GtkWidget *dialog = gtk_dialog_new_with_buttons (_("Text Search"),
236 GtkWidget *content_area =
237 gtk_dialog_get_content_area (GTK_DIALOG (dialog));
239 GtkWidget *box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
240 GtkWidget *label = gtk_label_new (_("Text to search for:"));
241 GtkWidget *entry = gtk_entry_new_with_buffer (parent->search_text_buffer);
243 /* Add the label, and show everything we have added. */
244 gtk_container_add (GTK_CONTAINER (content_area), box);
245 gtk_container_add (GTK_CONTAINER (box), label);
246 gtk_container_add (GTK_CONTAINER (box), entry);
247 gtk_widget_show_all (content_area);
249 int result = gtk_dialog_run (GTK_DIALOG (dialog));
252 case GTK_RESPONSE_OK:
253 search_text = gtk_entry_get_text (GTK_ENTRY (entry));
259 gtk_widget_destroy (dialog);
264 /* What to do when the Find menuitem is called. */
266 on_edit_find (PsppireSyntaxWindow *sw)
268 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
271 const char *target = get_search_text (sw);
276 /* This is a wrap-around search. So start searching one
277 character after the current char. */
278 GtkTextMark *mark = gtk_text_buffer_get_insert (buffer);
279 gtk_text_buffer_get_iter_at_mark (buffer, &begin, mark);
280 gtk_text_iter_forward_char (&begin);
281 if (gtk_text_iter_forward_search (&begin, target, 0,
284 gtk_text_buffer_place_cursor (buffer, &loc);
288 /* If not found, then continue the search from the top
290 gtk_text_buffer_get_start_iter (buffer, &begin);
291 if (gtk_text_iter_forward_search (&begin, target, 0,
294 gtk_text_buffer_place_cursor (buffer, &loc);
300 /* The syntax editor's clipboard deals only with text */
308 selection_changed (PsppireSyntaxWindow *sw)
310 gboolean sel = gtk_text_buffer_get_has_selection (GTK_TEXT_BUFFER (sw->buffer));
312 g_object_set (sw->edit_copy, "enabled", sel, NULL);
313 g_object_set (sw->edit_cut, "enabled", sel, NULL);
314 g_object_set (sw->edit_delete, "enabled", sel, NULL);
317 /* The callback which runs when something request clipboard data */
319 clipboard_get_cb (GtkClipboard *clipboard,
320 GtkSelectionData *selection_data,
324 PsppireSyntaxWindow *sw = data;
325 g_assert (info == SELECT_FMT_TEXT);
327 gtk_selection_data_set (selection_data, gtk_selection_data_get_target (selection_data),
329 (const guchar *) sw->cliptext, strlen (sw->cliptext));
334 clipboard_clear_cb (GtkClipboard *clipboard,
337 PsppireSyntaxWindow *sw = data;
338 g_free (sw->cliptext);
342 static gchar tn1[] = "UTF8_STRING";
343 static gchar tn2[] = "STRING";
344 static gchar tn3[] = "TEXT";
345 static gchar tn4[] = "COMPOUND_TEXT";
346 static gchar tn5[] = "text/plain;charset=utf-8";
347 static gchar tn6[] = "text/plain";
349 static const GtkTargetEntry targets[] = {
350 { tn1, 0, SELECT_FMT_TEXT },
351 { tn2, 0, SELECT_FMT_TEXT },
352 { tn3, 0, SELECT_FMT_TEXT },
353 { tn4, 0, SELECT_FMT_TEXT },
354 { tn5, 0, SELECT_FMT_TEXT },
355 { tn6, 0, SELECT_FMT_TEXT }
359 Store a clip containing the currently selected text.
360 Returns true iff something was set.
361 As a side effect, begin and end will be set to indicate
362 the limits of the selected text.
365 set_clip (PsppireSyntaxWindow *sw, GtkTextIter *begin, GtkTextIter *end)
367 GtkClipboard *clipboard ;
368 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
370 if (! gtk_text_buffer_get_selection_bounds (buffer, begin, end))
373 g_free (sw->cliptext);
374 sw->cliptext = gtk_text_buffer_get_text (buffer, begin, end, FALSE);
377 gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
379 if (!gtk_clipboard_set_with_owner (clipboard, targets,
380 G_N_ELEMENTS (targets),
381 clipboard_get_cb, clipboard_clear_cb,
383 clipboard_clear_cb (clipboard, sw);
389 on_edit_cut (PsppireSyntaxWindow *sw)
391 GtkTextIter begin, end;
393 if (set_clip (sw, &begin, &end))
394 gtk_text_buffer_delete (GTK_TEXT_BUFFER (sw->buffer), &begin, &end);
398 on_edit_copy (PsppireSyntaxWindow *sw)
400 GtkTextIter begin, end;
402 set_clip (sw, &begin, &end);
406 on_edit_paste (PsppireSyntaxWindow *sw)
408 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (sw));
409 GtkClipboard *clipboard =
410 gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
412 gtk_text_buffer_paste_clipboard (GTK_TEXT_BUFFER (sw->buffer), clipboard, NULL, TRUE);
416 /* Check to see if CLIP holds a target which we know how to paste,
417 and set the sensitivity of the Paste action accordingly.
420 set_paste_sensitivity (GtkClipboard *clip, GdkEventOwnerChange *event, gpointer data)
423 gboolean compatible_target = FALSE;
424 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (data);
426 for (i = 0 ; i < sizeof (targets) / sizeof (targets[0]) ; ++i)
428 GdkAtom atom = gdk_atom_intern (targets[i].target, TRUE);
429 if (gtk_clipboard_wait_is_target_available (clip, atom))
431 compatible_target = TRUE;
436 g_object_set (sw->edit_paste, "enabled", compatible_target, NULL);
442 /* Parse and execute all the text in the buffer */
444 on_run_all (PsppireSyntaxWindow *se)
446 GtkTextIter begin, end;
448 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &begin, 0);
449 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &end, -1);
451 editor_execute_syntax (se, begin, end);
454 /* Parse and execute the currently selected text */
456 on_run_selection (PsppireSyntaxWindow *se)
458 GtkTextIter begin, end;
460 if (gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (se->buffer), &begin, &end))
461 editor_execute_syntax (se, begin, end);
465 /* Parse and execute the from the current line, to the end of the
468 on_run_to_end (PsppireSyntaxWindow *se)
470 GtkTextIter begin, end;
474 /* Get the current line */
475 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
477 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
480 line = gtk_text_iter_get_line (&here) ;
482 /* Now set begin and end to the start of this line, and end of buffer
484 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
485 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, -1);
487 editor_execute_syntax (se, begin, end);
492 /* Parse and execute the current line */
494 on_run_current_line (PsppireSyntaxWindow *se)
496 GtkTextIter begin, end;
500 /* Get the current line */
501 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
503 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
506 line = gtk_text_iter_get_line (&here) ;
508 /* Now set begin and end to the start of this line, and start of
509 following line respectively */
510 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
511 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, line + 1);
513 editor_execute_syntax (se, begin, end);
519 on_syntax (GAction *action, GVariant *param, PsppireSyntaxWindow *sw)
521 g_action_change_state (action, param);
522 GVariant *new_state = g_action_get_state (action);
524 const gchar *what = g_variant_get_string (new_state, NULL);
525 if (0 == g_strcmp0 (what, "auto"))
526 sw->syntax_mode = LEX_SYNTAX_AUTO;
527 else if (0 == g_strcmp0 (what, "interactive"))
528 sw->syntax_mode = LEX_SYNTAX_INTERACTIVE;
529 else if (0 == g_strcmp0 (what, "batch"))
530 sw->syntax_mode = LEX_SYNTAX_BATCH;
532 g_warn_if_reached ();
536 /* Append ".sps" to FILENAME if necessary.
537 The returned result must be freed when no longer required.
540 append_suffix (const gchar *filename)
542 if (! g_str_has_suffix (filename, ".sps") &&
543 ! g_str_has_suffix (filename, ".SPS"))
545 return g_strdup_printf ("%s.sps", filename);
548 return xstrdup (filename);
552 Save BUFFER to the file called FILENAME.
553 FILENAME must be encoded in Glib filename encoding.
554 If successful, clears the buffer's modified flag.
557 save_editor_to_file (PsppireSyntaxWindow *se,
558 const gchar *filename,
561 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (se->buffer);
562 struct substring text_locale;
564 GtkTextIter start, stop;
570 suffixedname = append_suffix (filename);
572 gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
573 gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
575 text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
577 text_locale = recode_substring_pool (se->encoding, "UTF-8", ss_cstr (text),
580 result = g_file_set_contents (suffixedname, ss_data (text_locale),
581 ss_length (text_locale), err);
583 ss_dealloc (&text_locale);
584 g_free (suffixedname);
588 char *fn = g_filename_display_name (filename);
589 gchar *msg = g_strdup_printf (_("Saved file `%s'"), fn);
591 gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
592 gtk_text_buffer_set_modified (buffer, FALSE);
600 /* PsppireWindow 'pick_Filename' callback. */
602 syntax_pick_filename (PsppireWindow *window)
604 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (window);
605 const char *default_encoding;
606 GtkFileFilter *filter;
610 gtk_file_chooser_dialog_new (_("Save Syntax"),
612 GTK_FILE_CHOOSER_ACTION_SAVE,
613 _("Cancel"), GTK_RESPONSE_CANCEL,
614 _("Save"), GTK_RESPONSE_ACCEPT,
617 g_object_set (dialog, "local-only", FALSE, NULL);
619 filter = gtk_file_filter_new ();
620 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
621 gtk_file_filter_add_pattern (filter, "*.sps");
622 gtk_file_filter_add_pattern (filter, "*.SPS");
623 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
625 filter = gtk_file_filter_new ();
626 gtk_file_filter_set_name (filter, _("All Files"));
627 gtk_file_filter_add_pattern (filter, "*");
628 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
630 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
633 default_encoding = se->encoding != NULL ? se->encoding : locale_charset ();
634 gtk_file_chooser_set_extra_widget (
635 GTK_FILE_CHOOSER (dialog),
636 psppire_encoding_selector_new (default_encoding, false));
638 response = gtk_dialog_run (GTK_DIALOG (dialog));
640 if (response == GTK_RESPONSE_ACCEPT)
645 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
646 psppire_window_set_filename (window, filename);
649 encoding = psppire_encoding_selector_get_encoding (
650 gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
651 if (encoding != NULL)
653 g_free (se->encoding);
654 se->encoding = encoding;
658 gtk_widget_destroy (dialog);
662 /* PsppireWindow 'save' callback. */
664 syntax_save (PsppireWindow *se)
666 const gchar *filename = psppire_window_get_filename (se);
668 save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
671 msg (ME, "%s", err->message);
678 load_and_show_syntax_window (GtkWidget *se, const gchar *filename,
679 const gchar *encoding)
683 gtk_source_buffer_begin_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
684 ok = psppire_window_load (PSPPIRE_WINDOW (se), filename, encoding, NULL);
685 gtk_source_buffer_end_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
688 gtk_widget_show (se);
690 gtk_widget_destroy (se);
694 create_syntax_window (void)
696 GtkWidget *w = psppire_syntax_window_new (NULL);
702 open_syntax_window (const char *file_name, const gchar *encoding)
704 GtkWidget *se = psppire_syntax_window_new (NULL);
707 load_and_show_syntax_window (se, file_name, encoding);
709 return GTK_WINDOW (se);
714 static void psppire_syntax_window_print (PsppireSyntaxWindow *window);
717 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
719 if (gtk_text_buffer_get_modified (buffer))
720 psppire_window_set_unsaved (window);
723 static void undo_redo_update (PsppireSyntaxWindow *window);
724 static void undo_last_edit (PsppireSyntaxWindow *window);
725 static void redo_last_edit (PsppireSyntaxWindow *window);
728 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
730 gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
731 undo_redo_update (window);
735 psppire_syntax_window_init (PsppireSyntaxWindow *window)
737 GtkBuilder *xml = builder_new ("syntax-editor.ui");
738 GtkWidget *box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
740 GObject *menu = get_object_assert (xml, "syntax-window-menu", G_TYPE_MENU);
741 GtkWidget *menubar = gtk_menu_bar_new_from_model (G_MENU_MODEL (menu));
743 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
745 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
747 PsppireSyntaxWindowClass *class
748 = PSPPIRE_SYNTAX_WINDOW_CLASS (G_OBJECT_GET_CLASS (window));
750 GtkClipboard *clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
751 GtkClipboard *clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
753 window->print_settings = NULL;
755 window->undo_menuitem = g_simple_action_new ("undo", NULL);
756 window->redo_menuitem = g_simple_action_new ("redo", NULL);
758 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->undo_menuitem));
759 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->redo_menuitem));
763 window->buffer = gtk_source_buffer_new_with_language (class->lan);
765 window->buffer = gtk_source_buffer_new (NULL);
767 gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), GTK_TEXT_BUFFER (window->buffer));
769 g_object_set (window->buffer,
770 "highlight-matching-brackets", TRUE,
773 g_object_set (text_view,
774 "show-line-numbers", TRUE,
775 "show-line-marks", TRUE,
778 "highlight-current-line", TRUE,
781 window->encoding = NULL;
782 window->syntax_mode = LEX_SYNTAX_AUTO;
784 window->cliptext = NULL;
785 window->dispose_has_run = FALSE;
787 window->search_text_buffer = gtk_entry_buffer_new (NULL, -1);
789 window->edit_delete = g_simple_action_new ("delete", NULL);
790 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_delete));
792 window->edit_copy = g_simple_action_new ("copy", NULL);
793 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_copy));
795 window->edit_cut = g_simple_action_new ("cut", NULL);
796 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_cut));
798 window->edit_paste = g_simple_action_new ("paste", NULL);
799 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_paste));
801 window->edit_find = g_simple_action_new ("find", NULL);
802 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_find));
804 window->buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view)));
806 window->sb = get_widget_assert (xml, "statusbar2");
807 window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
809 g_signal_connect (window->buffer, "changed",
810 G_CALLBACK (on_text_changed), window);
812 g_signal_connect (window->buffer, "modified-changed",
813 G_CALLBACK (on_modified_changed), window);
817 GSimpleAction *print = g_simple_action_new ("print", NULL);
819 g_signal_connect_swapped (print, "activate",
820 G_CALLBACK (psppire_syntax_window_print), window);
822 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (print));
825 g_signal_connect_swapped (window->undo_menuitem,
827 G_CALLBACK (undo_last_edit),
830 g_signal_connect_swapped (window->redo_menuitem,
832 G_CALLBACK (redo_last_edit),
835 undo_redo_update (window);
838 window->sel_handler = g_signal_connect_swapped (clip_primary, "owner-change",
839 G_CALLBACK (selection_changed), window);
841 window->ps_handler = g_signal_connect (clip_selection, "owner-change",
842 G_CALLBACK (set_paste_sensitivity), window);
844 gtk_container_add (GTK_CONTAINER (window), box);
848 g_object_ref (window->sb);
850 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
851 gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
852 gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
854 gtk_widget_show_all (box);
856 GtkApplication *app = GTK_APPLICATION (g_application_get_default ());
859 GSimpleAction *open = g_simple_action_new ("open", NULL);
861 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (open));
863 g_signal_connect_swapped (open,
865 G_CALLBACK (psppire_window_open),
870 GSimpleAction *save = g_simple_action_new ("save", NULL);
872 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (save));
874 g_signal_connect_swapped (save,
876 G_CALLBACK (psppire_window_save),
879 const gchar *accels[2] = { "<Primary>S", NULL};
880 gtk_application_set_accels_for_action (app,
887 GSimpleAction *save_as = g_simple_action_new ("save_as", NULL);
889 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (save_as));
891 g_signal_connect_swapped (save_as,
893 G_CALLBACK (psppire_window_save_as),
897 const gchar *accels[2] = { "<Shift><Primary>S", NULL};
898 gtk_application_set_accels_for_action (app,
906 g_signal_connect_swapped (window->edit_delete,
908 G_CALLBACK (on_edit_delete),
911 g_signal_connect_swapped (window->edit_copy,
913 G_CALLBACK (on_edit_copy),
916 g_signal_connect_swapped (window->edit_cut,
918 G_CALLBACK (on_edit_cut),
921 g_signal_connect_swapped (window->edit_paste,
923 G_CALLBACK (on_edit_paste),
926 g_signal_connect_swapped (window->edit_find,
928 G_CALLBACK (on_edit_find),
932 GSimpleAction *run_all = g_simple_action_new ("run-all", NULL);
934 g_signal_connect_swapped (run_all, "activate",
935 G_CALLBACK (on_run_all), window);
937 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (run_all));
941 GSimpleAction *run_current_line = g_simple_action_new ("run-current-line", NULL);
943 g_signal_connect_swapped (run_current_line, "activate",
944 G_CALLBACK (on_run_current_line), window);
946 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (run_current_line));
948 GtkApplication *app = GTK_APPLICATION (g_application_get_default ());
949 const gchar *accels[2] = { "<Primary>R", NULL};
950 gtk_application_set_accels_for_action (app,
951 "win.run-current-line",
956 GSimpleAction *run_selection = g_simple_action_new ("run-selection", NULL);
958 g_signal_connect_swapped (run_selection, "activate",
959 G_CALLBACK (on_run_selection), window);
961 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (run_selection));
965 GSimpleAction *run_to_end = g_simple_action_new ("run-to-end", NULL);
967 g_signal_connect_swapped (run_to_end, "activate",
968 G_CALLBACK (on_run_to_end), window);
970 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (run_to_end));
974 GSimpleAction *act_syntax = g_simple_action_new_stateful ("syntax", G_VARIANT_TYPE_STRING,
975 g_variant_new_string ("auto"));
976 g_signal_connect (act_syntax, "activate", G_CALLBACK (on_syntax), window);
977 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (act_syntax));
980 gtk_menu_shell_append (GTK_MENU_SHELL (menubar),
981 create_windows_menu (GTK_WINDOW (window)));
983 gtk_menu_shell_append (GTK_MENU_SHELL (menubar),
984 create_help_menu (GTK_WINDOW (window)));
986 g_object_unref (xml);
994 psppire_syntax_window_new (const char *encoding)
996 GObject *sw = g_object_new (psppire_syntax_window_get_type (),
997 "description", _("Syntax Editor"),
998 "encoding", encoding,
1001 GApplication *app = g_application_get_default ();
1002 gtk_application_add_window (GTK_APPLICATION (app), GTK_WINDOW (sw));
1004 return GTK_WIDGET (sw);
1008 error_dialog (GtkWindow *w, const gchar *filename, GError *err)
1010 gchar *fn = g_filename_display_basename (filename);
1013 gtk_message_dialog_new (w,
1014 GTK_DIALOG_DESTROY_WITH_PARENT,
1017 _("Cannot load syntax file `%s'"),
1022 g_object_set (dialog, "icon-name", "pspp", NULL);
1024 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1025 "%s", err->message);
1027 gtk_dialog_run (GTK_DIALOG (dialog));
1029 gtk_widget_destroy (dialog);
1033 Loads the buffer from the file called FILENAME
1036 syntax_load (PsppireWindow *window, const gchar *filename,
1037 const gchar *encoding, gpointer not_used)
1040 gchar *text_locale = NULL;
1041 gchar *text_utf8 = NULL;
1042 gsize len_locale = -1;
1043 gsize len_utf8 = -1;
1045 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
1046 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
1048 /* FIXME: What if it's a very big file ? */
1049 if (! g_file_get_contents (filename, &text_locale, &len_locale, &err))
1051 error_dialog (GTK_WINDOW (window), filename, err);
1052 g_clear_error (&err);
1056 if (!encoding || !encoding[0])
1058 /* Determine the file's encoding and update sw->encoding. (The ordering
1059 is important here because encoding_guess_whole_file() often returns
1060 its argument instead of a copy of it.) */
1061 char *guessed_encoding;
1063 guessed_encoding = g_strdup (encoding_guess_whole_file (sw->encoding,
1066 g_free (sw->encoding);
1067 sw->encoding = guessed_encoding;
1071 g_free (sw->encoding);
1072 sw->encoding = g_strdup (encoding);
1075 text_utf8 = recode_substring_pool ("UTF-8", sw->encoding,
1076 ss_buffer (text_locale, len_locale),
1080 if (text_utf8 == NULL)
1082 error_dialog (GTK_WINDOW (window), filename, err);
1083 g_clear_error (&err);
1087 gtk_text_buffer_get_iter_at_line (buffer, &iter, 0);
1089 gtk_text_buffer_insert (buffer, &iter, text_utf8, len_utf8);
1091 gtk_text_buffer_set_modified (buffer, FALSE);
1095 add_most_recent (filename, "text/x-spss-syntax", sw->encoding);
1103 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
1105 iface->save = syntax_save;
1106 iface->pick_filename = syntax_pick_filename;
1107 iface->load = syntax_load;
1114 undo_redo_update (PsppireSyntaxWindow *window)
1116 g_object_set (window->undo_menuitem, "enabled",
1117 gtk_source_buffer_can_undo (window->buffer), NULL);
1119 g_object_set (window->redo_menuitem, "enabled",
1120 gtk_source_buffer_can_redo (window->buffer), NULL);
1124 undo_last_edit (PsppireSyntaxWindow *window)
1126 gtk_source_buffer_undo (window->buffer);
1127 undo_redo_update (window);
1131 redo_last_edit (PsppireSyntaxWindow *window)
1133 gtk_source_buffer_redo (window->buffer);
1134 undo_redo_update (window);
1139 /* Printing related stuff */
1143 begin_print (GtkPrintOperation *operation,
1144 GtkPrintContext *context,
1145 PsppireSyntaxWindow *window)
1147 window->compositor =
1148 gtk_source_print_compositor_new (window->buffer);
1153 end_print (GtkPrintOperation *operation,
1154 GtkPrintContext *context,
1155 PsppireSyntaxWindow *window)
1157 g_object_unref (window->compositor);
1158 window->compositor = NULL;
1164 paginate (GtkPrintOperation *operation,
1165 GtkPrintContext *context,
1166 PsppireSyntaxWindow *window)
1168 if (gtk_source_print_compositor_paginate (window->compositor, context))
1170 gint n_pages = gtk_source_print_compositor_get_n_pages (window->compositor);
1171 gtk_print_operation_set_n_pages (operation, n_pages);
1180 draw_page (GtkPrintOperation *operation,
1181 GtkPrintContext *context,
1183 PsppireSyntaxWindow *window)
1185 gtk_source_print_compositor_draw_page (window->compositor,
1193 psppire_syntax_window_print (PsppireSyntaxWindow *window)
1195 GtkPrintOperationResult res;
1197 GtkPrintOperation *print = gtk_print_operation_new ();
1199 if (window->print_settings != NULL)
1200 gtk_print_operation_set_print_settings (print, window->print_settings);
1203 g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
1204 g_signal_connect (print, "end_print", G_CALLBACK (end_print), window);
1205 g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), window);
1206 g_signal_connect (print, "paginate", G_CALLBACK (paginate), window);
1208 res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1209 GTK_WINDOW (window), NULL);
1211 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
1213 if (window->print_settings != NULL)
1214 g_object_unref (window->print_settings);
1215 window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
1218 g_object_unref (print);