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/gtksource.h>
24 #include "language/lexer/lexer.h"
25 #include "libpspp/encoding-guesser.h"
26 #include "libpspp/i18n.h"
27 #include "libpspp/message.h"
28 #include "ui/gui/executor.h"
29 #include "ui/gui/help-menu.h"
30 #include "ui/gui/helper.h"
31 #include "ui/gui/builder-wrapper.h"
32 #include "ui/gui/psppire-data-window.h"
33 #include "ui/gui/psppire-encoding-selector.h"
34 #include "ui/gui/psppire-lex-reader.h"
35 #include "ui/gui/psppire-syntax-window.h"
36 #include "ui/gui/psppire.h"
37 #include "ui/gui/windows-menu.h"
39 #include "gl/localcharset.h"
40 #include "gl/xalloc.h"
41 #include "gl/xvasprintf.h"
44 #define _(msgid) gettext (msgid)
45 #define N_(msgid) msgid
47 static void psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *, gpointer);
48 static void psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class);
49 static void psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class);
50 static void psppire_syntax_window_init (PsppireSyntaxWindow *syntax_editor);
53 static void psppire_syntax_window_iface_init (PsppireWindowIface *iface);
64 psppire_syntax_window_set_property (GObject *object,
69 PsppireSyntaxWindow *window = PSPPIRE_SYNTAX_WINDOW (object);
74 g_free (window->encoding);
75 window->encoding = g_value_dup_string (value);
78 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
85 psppire_syntax_window_get_property (GObject *object,
90 PsppireSyntaxWindow *window = PSPPIRE_SYNTAX_WINDOW (object);
95 g_value_set_string (value, window->encoding);
98 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
104 psppire_syntax_window_get_type (void)
106 static GType psppire_syntax_window_type = 0;
108 if (!psppire_syntax_window_type)
110 static const GTypeInfo psppire_syntax_window_info =
112 sizeof (PsppireSyntaxWindowClass),
113 (GBaseInitFunc) psppire_syntax_window_base_init,
114 (GBaseFinalizeFunc) psppire_syntax_window_base_finalize,
115 (GClassInitFunc)psppire_syntax_window_class_init,
116 (GClassFinalizeFunc) NULL,
118 sizeof (PsppireSyntaxWindow),
120 (GInstanceInitFunc) psppire_syntax_window_init,
123 static const GInterfaceInfo window_interface_info =
125 (GInterfaceInitFunc) psppire_syntax_window_iface_init,
130 psppire_syntax_window_type =
131 g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireSyntaxWindow",
132 &psppire_syntax_window_info, 0);
134 g_type_add_interface_static (psppire_syntax_window_type,
135 PSPPIRE_TYPE_WINDOW_MODEL,
136 &window_interface_info);
139 return psppire_syntax_window_type;
142 static GObjectClass *parent_class ;
145 psppire_syntax_window_finalize (GObject *object)
147 if (G_OBJECT_CLASS (parent_class)->finalize)
148 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
153 psppire_syntax_window_dispose (GObject *obj)
155 PsppireSyntaxWindow *sw = (PsppireSyntaxWindow *)obj;
157 GtkClipboard *clip_selection;
158 GtkClipboard *clip_primary;
160 if (sw->dispose_has_run)
163 g_free (sw->encoding);
166 clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
167 clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_PRIMARY);
169 g_signal_handler_disconnect (clip_primary, sw->sel_handler);
171 g_signal_handler_disconnect (clip_selection, sw->ps_handler);
173 /* Make sure dispose does not run twice. */
174 sw->dispose_has_run = TRUE;
176 /* Chain up to the parent class */
177 G_OBJECT_CLASS (parent_class)->dispose (obj);
183 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
185 GParamSpec *encoding_spec;
186 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
188 GtkSourceLanguageManager *lm = gtk_source_language_manager_get_default ();
190 const gchar * const *existing_paths = gtk_source_language_manager_get_search_path (lm);
191 gchar **new_paths = g_strdupv ((gchar **)existing_paths);
192 int n = g_strv_length ((gchar **) existing_paths);
194 new_paths = g_realloc (new_paths, (n + 2) * sizeof (*new_paths));
195 new_paths[n] = g_strdup (relocate (PKGDATADIR));
196 new_paths[n+1] = NULL;
198 lm = gtk_source_language_manager_new ();
199 gtk_source_language_manager_set_search_path (lm, new_paths);
201 class->lan = gtk_source_language_manager_get_language (lm, "pspp");
203 if (class->lan == NULL)
204 g_warning ("pspp.lang file not found. Syntax highlighting will not be available.");
206 parent_class = g_type_class_peek_parent (class);
208 g_strfreev (new_paths);
211 null_if_empty_param ("encoding",
212 "Character encoding",
213 "IANA character encoding in this syntax file",
215 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
217 parent_class = g_type_class_peek_parent (class);
219 gobject_class->set_property = psppire_syntax_window_set_property;
220 gobject_class->get_property = psppire_syntax_window_get_property;
221 gobject_class->dispose = psppire_syntax_window_dispose;
223 g_object_class_install_property (gobject_class,
230 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
232 GObjectClass *object_class = G_OBJECT_CLASS (class);
233 object_class->finalize = psppire_syntax_window_finalize;
239 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
246 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
249 PsppireWindow *win = PSPPIRE_WINDOW (sw);
250 struct lex_reader *reader = lex_reader_for_gtk_text_buffer (GTK_TEXT_BUFFER (sw->buffer), start, stop);
252 lex_reader_set_file_name (reader, psppire_window_get_filename (win));
254 execute_syntax (psppire_default_data_window (), reader);
257 /* Delete the currently selected text */
259 on_edit_delete (PsppireSyntaxWindow *sw)
261 GtkTextIter begin, end;
262 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
264 if ( gtk_text_buffer_get_selection_bounds (buffer, &begin, &end) )
265 gtk_text_buffer_delete (buffer, &begin, &end);
269 /* The syntax editor's clipboard deals only with text */
277 selection_changed (PsppireSyntaxWindow *sw)
279 gboolean sel = gtk_text_buffer_get_has_selection (GTK_TEXT_BUFFER (sw->buffer));
281 g_object_set (sw->edit_copy, "enabled", sel, NULL);
282 g_object_set (sw->edit_cut, "enabled", sel, NULL);
283 g_object_set (sw->edit_delete, "enabled", sel, NULL);
286 /* The callback which runs when something request clipboard data */
288 clipboard_get_cb (GtkClipboard *clipboard,
289 GtkSelectionData *selection_data,
293 PsppireSyntaxWindow *sw = data;
294 g_assert (info == SELECT_FMT_TEXT);
296 gtk_selection_data_set (selection_data, gtk_selection_data_get_target (selection_data),
298 (const guchar *) sw->cliptext, strlen (sw->cliptext));
303 clipboard_clear_cb (GtkClipboard *clipboard,
306 PsppireSyntaxWindow *sw = data;
307 g_free (sw->cliptext);
312 static const GtkTargetEntry targets[] = {
313 { "UTF8_STRING", 0, SELECT_FMT_TEXT },
314 { "STRING", 0, SELECT_FMT_TEXT },
315 { "TEXT", 0, SELECT_FMT_TEXT },
316 { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
317 { "text/plain;charset=utf-8", 0, SELECT_FMT_TEXT },
318 { "text/plain", 0, SELECT_FMT_TEXT },
323 Store a clip containing the currently selected text.
324 Returns true iff something was set.
325 As a side effect, begin and end will be set to indicate
326 the limits of the selected text.
329 set_clip (PsppireSyntaxWindow *sw, GtkTextIter *begin, GtkTextIter *end)
331 GtkClipboard *clipboard ;
332 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
334 if ( ! gtk_text_buffer_get_selection_bounds (buffer, begin, end) )
337 g_free (sw->cliptext);
338 sw->cliptext = gtk_text_buffer_get_text (buffer, begin, end, FALSE);
341 gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
343 if (!gtk_clipboard_set_with_owner (clipboard, targets,
344 G_N_ELEMENTS (targets),
345 clipboard_get_cb, clipboard_clear_cb,
347 clipboard_clear_cb (clipboard, sw);
353 on_edit_cut (PsppireSyntaxWindow *sw)
355 GtkTextIter begin, end;
357 if ( set_clip (sw, &begin, &end))
358 gtk_text_buffer_delete (GTK_TEXT_BUFFER (sw->buffer), &begin, &end);
362 on_edit_copy (PsppireSyntaxWindow *sw)
364 GtkTextIter begin, end;
366 set_clip (sw, &begin, &end);
370 on_edit_paste (PsppireSyntaxWindow *sw)
372 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (sw));
373 GtkClipboard *clipboard =
374 gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
376 gtk_text_buffer_paste_clipboard (GTK_TEXT_BUFFER (sw->buffer), clipboard, NULL, TRUE);
380 /* Check to see if CLIP holds a target which we know how to paste,
381 and set the sensitivity of the Paste action accordingly.
384 set_paste_sensitivity (GtkClipboard *clip, GdkEventOwnerChange *event, gpointer data)
387 gboolean compatible_target = FALSE;
388 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (data);
390 for (i = 0 ; i < sizeof (targets) / sizeof (targets[0]) ; ++i)
392 GdkAtom atom = gdk_atom_intern (targets[i].target, TRUE);
393 if ( gtk_clipboard_wait_is_target_available (clip, atom))
395 compatible_target = TRUE;
400 g_object_set (sw->edit_paste, "enabled", compatible_target, NULL);
406 /* Parse and execute all the text in the buffer */
408 on_run_all (PsppireSyntaxWindow *se)
410 GtkTextIter begin, end;
412 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &begin, 0);
413 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &end, -1);
415 editor_execute_syntax (se, begin, end);
418 /* Parse and execute the currently selected text */
420 on_run_selection (PsppireSyntaxWindow *se)
422 GtkTextIter begin, end;
424 if ( gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (se->buffer), &begin, &end) )
425 editor_execute_syntax (se, begin, end);
429 /* Parse and execute the from the current line, to the end of the
432 on_run_to_end (PsppireSyntaxWindow *se)
434 GtkTextIter begin, end;
438 /* Get the current line */
439 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
441 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
444 line = gtk_text_iter_get_line (&here) ;
446 /* Now set begin and end to the start of this line, and end of buffer
448 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
449 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, -1);
451 editor_execute_syntax (se, begin, end);
456 /* Parse and execute the current line */
458 on_run_current_line (PsppireSyntaxWindow *se)
460 GtkTextIter begin, end;
464 /* Get the current line */
465 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
467 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
470 line = gtk_text_iter_get_line (&here) ;
472 /* Now set begin and end to the start of this line, and start of
473 following line respectively */
474 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
475 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, line + 1);
477 editor_execute_syntax (se, begin, end);
482 /* Append ".sps" to FILENAME if necessary.
483 The returned result must be freed when no longer required.
486 append_suffix (const gchar *filename)
488 if ( ! g_str_has_suffix (filename, ".sps" ) &&
489 ! g_str_has_suffix (filename, ".SPS" ) )
491 return g_strdup_printf ("%s.sps", filename);
494 return xstrdup (filename);
498 Save BUFFER to the file called FILENAME.
499 FILENAME must be encoded in Glib filename encoding.
500 If successful, clears the buffer's modified flag.
503 save_editor_to_file (PsppireSyntaxWindow *se,
504 const gchar *filename,
507 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (se->buffer);
508 struct substring text_locale;
510 GtkTextIter start, stop;
516 suffixedname = append_suffix (filename);
518 gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
519 gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
521 text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
523 text_locale = recode_substring_pool (se->encoding, "UTF-8", ss_cstr (text),
526 result = g_file_set_contents (suffixedname, ss_data (text_locale),
527 ss_length (text_locale), err);
529 ss_dealloc (&text_locale);
530 g_free (suffixedname);
534 char *fn = g_filename_display_name (filename);
535 gchar *msg = g_strdup_printf (_("Saved file `%s'"), fn);
537 gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
538 gtk_text_buffer_set_modified (buffer, FALSE);
546 /* PsppireWindow 'pick_Filename' callback. */
548 syntax_pick_filename (PsppireWindow *window)
550 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (window);
551 const char *default_encoding;
552 GtkFileFilter *filter;
556 gtk_file_chooser_dialog_new (_("Save Syntax"),
558 GTK_FILE_CHOOSER_ACTION_SAVE,
559 _("Cancel"), GTK_RESPONSE_CANCEL,
560 _("Save"), GTK_RESPONSE_ACCEPT,
563 g_object_set (dialog, "local-only", FALSE, NULL);
565 filter = gtk_file_filter_new ();
566 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
567 gtk_file_filter_add_pattern (filter, "*.sps");
568 gtk_file_filter_add_pattern (filter, "*.SPS");
569 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
571 filter = gtk_file_filter_new ();
572 gtk_file_filter_set_name (filter, _("All Files"));
573 gtk_file_filter_add_pattern (filter, "*");
574 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
576 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
579 default_encoding = se->encoding != NULL ? se->encoding : locale_charset ();
580 gtk_file_chooser_set_extra_widget (
581 GTK_FILE_CHOOSER (dialog),
582 psppire_encoding_selector_new (default_encoding, false));
584 response = gtk_dialog_run (GTK_DIALOG (dialog));
586 if ( response == GTK_RESPONSE_ACCEPT )
591 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
592 psppire_window_set_filename (window, filename);
595 encoding = psppire_encoding_selector_get_encoding (
596 gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
597 if (encoding != NULL)
599 g_free (se->encoding);
600 se->encoding = encoding;
604 gtk_widget_destroy (dialog);
608 /* PsppireWindow 'save' callback. */
610 syntax_save (PsppireWindow *se)
612 const gchar *filename = psppire_window_get_filename (se);
614 save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
617 msg (ME, "%s", err->message);
624 load_and_show_syntax_window (GtkWidget *se, const gchar *filename,
625 const gchar *encoding)
629 gtk_source_buffer_begin_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
630 ok = psppire_window_load (PSPPIRE_WINDOW (se), filename, encoding, NULL);
631 gtk_source_buffer_end_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
634 gtk_widget_show (se);
636 gtk_widget_destroy (se);
640 create_syntax_window (void)
642 GtkWidget *w = psppire_syntax_window_new (NULL);
648 open_syntax_window (const char *file_name, const gchar *encoding)
650 GtkWidget *se = psppire_syntax_window_new (NULL);
653 load_and_show_syntax_window (se, file_name, encoding);
655 return GTK_WINDOW (se);
660 static void psppire_syntax_window_print (PsppireSyntaxWindow *window);
663 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
665 if (gtk_text_buffer_get_modified (buffer))
666 psppire_window_set_unsaved (window);
669 static void undo_redo_update (PsppireSyntaxWindow *window);
670 static void undo_last_edit (PsppireSyntaxWindow *window);
671 static void redo_last_edit (PsppireSyntaxWindow *window);
674 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
676 gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
677 undo_redo_update (window);
681 psppire_syntax_window_init (PsppireSyntaxWindow *window)
683 GtkBuilder *xml = builder_new ("syntax-editor.ui");
684 GtkWidget *box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
686 GObject *menu = get_object_assert (xml, "syntax-window-menu", G_TYPE_MENU);
687 GtkWidget *menubar = gtk_menu_bar_new_from_model (G_MENU_MODEL (menu));
689 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
691 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
693 PsppireSyntaxWindowClass *class
694 = PSPPIRE_SYNTAX_WINDOW_CLASS (G_OBJECT_GET_CLASS (window));
696 GtkClipboard *clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
697 GtkClipboard *clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
699 window->print_settings = NULL;
701 window->undo_menuitem = g_simple_action_new ("undo", NULL);
702 window->redo_menuitem = g_simple_action_new ("redo", NULL);
704 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->undo_menuitem));
705 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->redo_menuitem));
709 window->buffer = gtk_source_buffer_new_with_language (class->lan);
711 window->buffer = gtk_source_buffer_new (NULL);
713 gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), GTK_TEXT_BUFFER (window->buffer));
715 g_object_set (window->buffer,
716 "highlight-matching-brackets", TRUE,
719 g_object_set (text_view,
720 "show-line-numbers", TRUE,
721 "show-line-marks", TRUE,
724 "highlight-current-line", TRUE,
727 window->encoding = NULL;
729 window->cliptext = NULL;
730 window->dispose_has_run = FALSE;
733 window->edit_delete = g_simple_action_new ("delete", NULL);
734 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_delete));
736 window->edit_copy = g_simple_action_new ("copy", NULL);
737 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_copy));
739 window->edit_cut = g_simple_action_new ("cut", NULL);
740 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_cut));
742 window->edit_paste = g_simple_action_new ("paste", NULL);
743 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_paste));
746 window->buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view)));
748 window->sb = get_widget_assert (xml, "statusbar2");
749 window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
751 g_signal_connect (window->buffer, "changed",
752 G_CALLBACK (on_text_changed), window);
754 g_signal_connect (window->buffer, "modified-changed",
755 G_CALLBACK (on_modified_changed), window);
759 GSimpleAction *print = g_simple_action_new ("print", NULL);
761 g_signal_connect_swapped (print, "activate",
762 G_CALLBACK (psppire_syntax_window_print), window);
764 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (print));
767 g_signal_connect_swapped (window->undo_menuitem,
769 G_CALLBACK (undo_last_edit),
772 g_signal_connect_swapped (window->redo_menuitem,
774 G_CALLBACK (redo_last_edit),
777 undo_redo_update (window);
780 window->sel_handler = g_signal_connect_swapped (clip_primary, "owner-change",
781 G_CALLBACK (selection_changed), window);
783 window->ps_handler = g_signal_connect (clip_selection, "owner-change",
784 G_CALLBACK (set_paste_sensitivity), window);
788 gtk_container_add (GTK_CONTAINER (window), box);
792 g_object_ref (window->sb);
794 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
795 gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
796 gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
798 gtk_widget_show_all (box);
800 GtkApplication *app = GTK_APPLICATION (g_application_get_default ());
803 GSimpleAction *open = g_simple_action_new ("open", NULL);
805 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (open));
807 g_signal_connect_swapped (open,
809 G_CALLBACK (psppire_window_open),
814 GSimpleAction *save = g_simple_action_new ("save", NULL);
816 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (save));
818 g_signal_connect_swapped (save,
820 G_CALLBACK (psppire_window_save),
823 const gchar *accels[2] = { "<Ctrl>S", NULL};
824 gtk_application_set_accels_for_action (app,
831 GSimpleAction *save_as = g_simple_action_new ("save_as", NULL);
833 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (save_as));
835 g_signal_connect_swapped (save_as,
837 G_CALLBACK (psppire_window_save_as),
841 const gchar *accels[2] = { "<Shift><Ctrl>S", NULL};
842 gtk_application_set_accels_for_action (app,
850 g_signal_connect_swapped (window->edit_delete,
852 G_CALLBACK (on_edit_delete),
855 g_signal_connect_swapped (window->edit_copy,
857 G_CALLBACK (on_edit_copy),
860 g_signal_connect_swapped (window->edit_cut,
862 G_CALLBACK (on_edit_cut),
865 g_signal_connect_swapped (window->edit_paste,
867 G_CALLBACK (on_edit_paste),
871 GSimpleAction *run_all = g_simple_action_new ("run-all", NULL);
873 g_signal_connect_swapped (run_all, "activate",
874 G_CALLBACK (on_run_all), window);
876 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (run_all));
880 GSimpleAction *run_current_line = g_simple_action_new ("run-current-line", NULL);
882 g_signal_connect_swapped (run_current_line, "activate",
883 G_CALLBACK (on_run_current_line), window);
885 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (run_current_line));
887 GtkApplication *app = GTK_APPLICATION (g_application_get_default ());
888 const gchar *accels[2] = { "<Ctrl>R", NULL};
889 gtk_application_set_accels_for_action (app,
890 "win.run-current-line",
895 GSimpleAction *run_selection = g_simple_action_new ("run-selection", NULL);
897 g_signal_connect_swapped (run_selection, "activate",
898 G_CALLBACK (on_run_selection), window);
900 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (run_selection));
904 GSimpleAction *run_to_end = g_simple_action_new ("run-to-end", NULL);
906 g_signal_connect_swapped (run_to_end, "activate",
907 G_CALLBACK (on_run_to_end), window);
909 g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (run_to_end));
912 gtk_menu_shell_append (GTK_MENU_SHELL (menubar),
913 create_windows_menu (GTK_WINDOW (window)));
915 gtk_menu_shell_append (GTK_MENU_SHELL (menubar),
916 create_help_menu (GTK_WINDOW (window)));
918 g_object_unref (xml);
926 psppire_syntax_window_new (const char *encoding)
928 GObject *sw = g_object_new (psppire_syntax_window_get_type (),
929 "description", _("Syntax Editor"),
930 "encoding", encoding,
933 GApplication *app = g_application_get_default ();
934 gtk_application_add_window (GTK_APPLICATION (app), GTK_WINDOW (sw));
936 return GTK_WIDGET (sw);
940 error_dialog (GtkWindow *w, const gchar *filename, GError *err)
942 gchar *fn = g_filename_display_basename (filename);
945 gtk_message_dialog_new (w,
946 GTK_DIALOG_DESTROY_WITH_PARENT,
949 _("Cannot load syntax file `%s'"),
954 g_object_set (dialog, "icon-name", "pspp", NULL);
956 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
959 gtk_dialog_run (GTK_DIALOG (dialog));
961 gtk_widget_destroy (dialog);
965 Loads the buffer from the file called FILENAME
968 syntax_load (PsppireWindow *window, const gchar *filename,
969 const gchar *encoding, gpointer not_used)
972 gchar *text_locale = NULL;
973 gchar *text_utf8 = NULL;
974 gsize len_locale = -1;
977 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
978 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
980 /* FIXME: What if it's a very big file ? */
981 if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
983 error_dialog (GTK_WINDOW (window), filename, err);
984 g_clear_error (&err);
988 if (!encoding || !encoding[0])
990 /* Determine the file's encoding and update sw->encoding. (The ordering
991 is important here because encoding_guess_whole_file() often returns
992 its argument instead of a copy of it.) */
993 char *guessed_encoding;
995 guessed_encoding = g_strdup (encoding_guess_whole_file (sw->encoding,
998 g_free (sw->encoding);
999 sw->encoding = guessed_encoding;
1003 g_free (sw->encoding);
1004 sw->encoding = g_strdup (encoding);
1007 text_utf8 = recode_substring_pool ("UTF-8", sw->encoding,
1008 ss_buffer (text_locale, len_locale),
1012 if ( text_utf8 == NULL )
1014 error_dialog (GTK_WINDOW (window), filename, err);
1015 g_clear_error (&err);
1019 gtk_text_buffer_get_iter_at_line (buffer, &iter, 0);
1021 gtk_text_buffer_insert (buffer, &iter, text_utf8, len_utf8);
1023 gtk_text_buffer_set_modified (buffer, FALSE);
1027 add_most_recent (filename, "text/x-spss-syntax", sw->encoding);
1035 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
1037 iface->save = syntax_save;
1038 iface->pick_filename = syntax_pick_filename;
1039 iface->load = syntax_load;
1046 undo_redo_update (PsppireSyntaxWindow *window)
1048 g_object_set (window->undo_menuitem, "enabled",
1049 gtk_source_buffer_can_undo (window->buffer), NULL);
1051 g_object_set (window->redo_menuitem, "enabled",
1052 gtk_source_buffer_can_redo (window->buffer), NULL);
1056 undo_last_edit (PsppireSyntaxWindow *window)
1058 gtk_source_buffer_undo (window->buffer);
1059 undo_redo_update (window);
1063 redo_last_edit (PsppireSyntaxWindow *window)
1065 gtk_source_buffer_redo (window->buffer);
1066 undo_redo_update (window);
1071 /* Printing related stuff */
1075 begin_print (GtkPrintOperation *operation,
1076 GtkPrintContext *context,
1077 PsppireSyntaxWindow *window)
1079 window->compositor =
1080 gtk_source_print_compositor_new (window->buffer);
1085 end_print (GtkPrintOperation *operation,
1086 GtkPrintContext *context,
1087 PsppireSyntaxWindow *window)
1089 g_object_unref (window->compositor);
1090 window->compositor = NULL;
1096 paginate (GtkPrintOperation *operation,
1097 GtkPrintContext *context,
1098 PsppireSyntaxWindow *window)
1100 if (gtk_source_print_compositor_paginate (window->compositor, context))
1102 gint n_pages = gtk_source_print_compositor_get_n_pages (window->compositor);
1103 gtk_print_operation_set_n_pages (operation, n_pages);
1112 draw_page (GtkPrintOperation *operation,
1113 GtkPrintContext *context,
1115 PsppireSyntaxWindow *window)
1117 gtk_source_print_compositor_draw_page (window->compositor,
1125 psppire_syntax_window_print (PsppireSyntaxWindow *window)
1127 GtkPrintOperationResult res;
1129 GtkPrintOperation *print = gtk_print_operation_new ();
1131 if (window->print_settings != NULL)
1132 gtk_print_operation_set_print_settings (print, window->print_settings);
1135 g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
1136 g_signal_connect (print, "end_print", G_CALLBACK (end_print), window);
1137 g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), window);
1138 g_signal_connect (print, "paginate", G_CALLBACK (paginate), window);
1140 res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1141 GTK_WINDOW (window), NULL);
1143 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
1145 if (window->print_settings != NULL)
1146 g_object_unref (window->print_settings);
1147 window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
1150 g_object_unref (print);