1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2009, 2010, 2011, 2012, 2014 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-window-register.h"
40 #include "ui/gui/psppire.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 gtk_action_set_sensitive (sw->edit_copy, sel);
285 gtk_action_set_sensitive (sw->edit_cut, sel);
286 gtk_action_set_sensitive (sw->edit_delete, sel);
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, selection_data->target,
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);
374 on_edit_paste (PsppireSyntaxWindow *sw)
376 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (sw));
377 GtkClipboard *clipboard =
378 gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
380 gtk_text_buffer_paste_clipboard (GTK_TEXT_BUFFER (sw->buffer), clipboard, NULL, TRUE);
384 /* Check to see if CLIP holds a target which we know how to paste,
385 and set the sensitivity of the Paste action accordingly.
388 set_paste_sensitivity (GtkClipboard *clip, GdkEventOwnerChange *event, gpointer data)
391 gboolean compatible_target = FALSE;
392 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (data);
394 for (i = 0 ; i < sizeof (targets) / sizeof (targets[0]) ; ++i)
396 GdkAtom atom = gdk_atom_intern (targets[i].target, TRUE);
397 if ( gtk_clipboard_wait_is_target_available (clip, atom))
399 compatible_target = TRUE;
404 gtk_action_set_sensitive (sw->edit_paste, compatible_target);
410 /* Parse and execute all the text in the buffer */
412 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
414 GtkTextIter begin, end;
415 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
417 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &begin, 0);
418 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &end, -1);
420 editor_execute_syntax (se, begin, end);
423 /* Parse and execute the currently selected text */
425 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
427 GtkTextIter begin, end;
428 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
430 if ( gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (se->buffer), &begin, &end) )
431 editor_execute_syntax (se, begin, end);
435 /* Parse and execute the from the current line, to the end of the
438 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
440 GtkTextIter begin, end;
444 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
446 /* Get the current line */
447 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
449 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
452 line = gtk_text_iter_get_line (&here) ;
454 /* Now set begin and end to the start of this line, and end of buffer
456 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
457 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, -1);
459 editor_execute_syntax (se, begin, end);
464 /* Parse and execute the current line */
466 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
468 GtkTextIter begin, end;
472 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
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 start of
483 following line respectively */
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, line + 1);
487 editor_execute_syntax (se, begin, end);
492 /* Append ".sps" to FILENAME if necessary.
493 The returned result must be freed when no longer required.
496 append_suffix (const gchar *filename)
498 if ( ! g_str_has_suffix (filename, ".sps" ) &&
499 ! g_str_has_suffix (filename, ".SPS" ) )
501 return g_strdup_printf ("%s.sps", filename);
504 return xstrdup (filename);
508 Save BUFFER to the file called FILENAME.
509 FILENAME must be encoded in Glib filename encoding.
510 If successful, clears the buffer's modified flag.
513 save_editor_to_file (PsppireSyntaxWindow *se,
514 const gchar *filename,
517 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (se->buffer);
518 struct substring text_locale;
520 GtkTextIter start, stop;
526 suffixedname = append_suffix (filename);
528 gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
529 gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
531 text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
533 text_locale = recode_substring_pool (se->encoding, "UTF-8", ss_cstr (text),
536 result = g_file_set_contents (suffixedname, ss_data (text_locale),
537 ss_length (text_locale), err);
539 ss_dealloc (&text_locale);
540 g_free (suffixedname);
544 char *fn = g_filename_display_name (filename);
545 gchar *msg = g_strdup_printf (_("Saved file `%s'"), fn);
547 gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
548 gtk_text_buffer_set_modified (buffer, FALSE);
556 /* PsppireWindow 'pick_Filename' callback. */
558 syntax_pick_filename (PsppireWindow *window)
560 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (window);
561 const char *default_encoding;
562 GtkFileFilter *filter;
566 gtk_file_chooser_dialog_new (_("Save Syntax"),
568 GTK_FILE_CHOOSER_ACTION_SAVE,
569 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
570 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
573 g_object_set (dialog, "local-only", FALSE, NULL);
575 filter = gtk_file_filter_new ();
576 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
577 gtk_file_filter_add_pattern (filter, "*.sps");
578 gtk_file_filter_add_pattern (filter, "*.SPS");
579 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
581 filter = gtk_file_filter_new ();
582 gtk_file_filter_set_name (filter, _("All Files"));
583 gtk_file_filter_add_pattern (filter, "*");
584 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
586 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
589 default_encoding = se->encoding != NULL ? se->encoding : locale_charset ();
590 gtk_file_chooser_set_extra_widget (
591 GTK_FILE_CHOOSER (dialog),
592 psppire_encoding_selector_new (default_encoding, false));
594 response = gtk_dialog_run (GTK_DIALOG (dialog));
596 if ( response == GTK_RESPONSE_ACCEPT )
601 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
602 psppire_window_set_filename (window, filename);
605 encoding = psppire_encoding_selector_get_encoding (
606 gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
607 if (encoding != NULL)
609 g_free (se->encoding);
610 se->encoding = encoding;
614 gtk_widget_destroy (dialog);
618 /* PsppireWindow 'save' callback. */
620 syntax_save (PsppireWindow *se)
622 const gchar *filename = psppire_window_get_filename (se);
624 save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
627 msg (ME, "%s", err->message);
633 /* Callback for the File->Quit menuitem */
635 on_quit (GtkMenuItem *menuitem, gpointer user_data)
644 load_and_show_syntax_window (GtkWidget *se, const gchar *filename,
645 const gchar *encoding)
649 gtk_source_buffer_begin_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
650 ok = psppire_window_load (PSPPIRE_WINDOW (se), filename, encoding, NULL);
651 gtk_source_buffer_end_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
654 gtk_widget_show (se);
656 gtk_widget_destroy (se);
660 create_syntax_window (void)
662 GtkWidget *w = psppire_syntax_window_new (NULL);
667 open_syntax_window (const char *file_name, const gchar *encoding)
669 GtkWidget *se = psppire_syntax_window_new (NULL);
672 load_and_show_syntax_window (se, file_name, encoding);
677 static void psppire_syntax_window_print (PsppireSyntaxWindow *window);
680 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
682 if (gtk_text_buffer_get_modified (buffer))
683 psppire_window_set_unsaved (window);
686 static void undo_redo_update (PsppireSyntaxWindow *window);
687 static void undo_last_edit (PsppireSyntaxWindow *window);
688 static void redo_last_edit (PsppireSyntaxWindow *window);
691 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
693 gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
694 undo_redo_update (window);
698 psppire_syntax_window_init (PsppireSyntaxWindow *window)
700 GtkBuilder *xml = builder_new ("syntax-editor.ui");
701 GtkWidget *box = gtk_vbox_new (FALSE, 0);
703 GtkWidget *menubar = get_widget_assert (xml, "menubar");
704 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
706 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
708 PsppireSyntaxWindowClass *class
709 = PSPPIRE_SYNTAX_WINDOW_CLASS (G_OBJECT_GET_CLASS (window));
711 GtkClipboard *clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
712 GtkClipboard *clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
714 window->print_settings = NULL;
715 window->undo_menuitem = get_action_assert (xml, "edit_undo");
716 window->redo_menuitem = get_action_assert (xml, "edit_redo");
719 window->buffer = gtk_source_buffer_new_with_language (class->lan);
721 window->buffer = gtk_source_buffer_new (NULL);
723 gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), GTK_TEXT_BUFFER (window->buffer));
725 g_object_set (window->buffer,
726 "highlight-matching-brackets", TRUE,
729 g_object_set (text_view,
730 "show-line-numbers", TRUE,
731 "show-line-marks", TRUE,
734 "highlight-current-line", TRUE,
737 window->encoding = NULL;
739 window->cliptext = NULL;
740 window->dispose_has_run = FALSE;
742 window->edit_delete = get_action_assert (xml, "edit_delete");
743 window->edit_copy = get_action_assert (xml, "edit_copy");
744 window->edit_cut = get_action_assert (xml, "edit_cut");
745 window->edit_paste = get_action_assert (xml, "edit_paste");
747 window->buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view)));
749 window->sb = get_widget_assert (xml, "statusbar2");
750 window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
752 g_signal_connect (window->buffer, "changed",
753 G_CALLBACK (on_text_changed), window);
755 g_signal_connect (window->buffer, "modified-changed",
756 G_CALLBACK (on_modified_changed), window);
758 g_signal_connect_swapped (get_action_assert (xml, "file_print"), "activate",
759 G_CALLBACK (psppire_syntax_window_print), window);
762 g_signal_connect_swapped (window->undo_menuitem,
764 G_CALLBACK (undo_last_edit),
767 g_signal_connect_swapped (window->redo_menuitem,
769 G_CALLBACK (redo_last_edit),
772 undo_redo_update (window);
774 window->sel_handler = g_signal_connect_swapped (clip_primary, "owner-change",
775 G_CALLBACK (selection_changed), window);
777 window->ps_handler = g_signal_connect (clip_selection, "owner-change",
778 G_CALLBACK (set_paste_sensitivity), window);
782 gtk_container_add (GTK_CONTAINER (window), box);
784 g_object_ref (menubar);
788 g_object_ref (window->sb);
790 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
791 gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
792 gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
794 gtk_widget_show_all (box);
796 g_signal_connect_swapped (get_action_assert (xml,"file_new_syntax"), "activate", G_CALLBACK (create_syntax_window), NULL);
798 g_signal_connect (get_action_assert (xml,"file_new_data"),
800 G_CALLBACK (create_data_window),
803 g_signal_connect_swapped (get_action_assert (xml, "file_open"),
805 G_CALLBACK (psppire_window_open),
808 g_signal_connect_swapped (get_action_assert (xml, "file_save"),
810 G_CALLBACK (psppire_window_save),
813 g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
815 G_CALLBACK (psppire_window_save_as),
818 g_signal_connect (get_action_assert (xml,"file_quit"),
820 G_CALLBACK (on_quit),
823 g_signal_connect_swapped (window->edit_delete,
825 G_CALLBACK (on_edit_delete),
828 g_signal_connect_swapped (window->edit_copy,
830 G_CALLBACK (on_edit_copy),
833 g_signal_connect_swapped (window->edit_cut,
835 G_CALLBACK (on_edit_cut),
838 g_signal_connect_swapped (window->edit_paste,
840 G_CALLBACK (on_edit_paste),
843 g_signal_connect (get_action_assert (xml,"run_all"),
845 G_CALLBACK (on_run_all),
848 g_signal_connect (get_action_assert (xml,"run_selection"),
850 G_CALLBACK (on_run_selection),
853 g_signal_connect (get_action_assert (xml,"run_current_line"),
855 G_CALLBACK (on_run_current_line),
858 g_signal_connect (get_action_assert (xml,"run_to_end"),
860 G_CALLBACK (on_run_to_end),
863 g_signal_connect (get_action_assert (xml,"windows_minimise_all"),
865 G_CALLBACK (psppire_window_minimise_all), NULL);
872 GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
874 merge_help_menu (uim);
876 PSPPIRE_WINDOW (window)->menu =
877 GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
880 g_object_unref (xml);
888 psppire_syntax_window_new (const char *encoding)
890 return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
891 "description", _("Syntax Editor"),
892 "encoding", encoding,
897 error_dialog (GtkWindow *w, const gchar *filename, GError *err)
899 gchar *fn = g_filename_display_basename (filename);
902 gtk_message_dialog_new (w,
903 GTK_DIALOG_DESTROY_WITH_PARENT,
906 _("Cannot load syntax file `%s'"),
911 g_object_set (dialog, "icon-name", "pspp", NULL);
913 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
916 gtk_dialog_run (GTK_DIALOG (dialog));
918 gtk_widget_destroy (dialog);
922 Loads the buffer from the file called FILENAME
925 syntax_load (PsppireWindow *window, const gchar *filename,
926 const gchar *encoding, gpointer not_used)
929 gchar *text_locale = NULL;
930 gchar *text_utf8 = NULL;
931 gsize len_locale = -1;
934 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
935 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
937 /* FIXME: What if it's a very big file ? */
938 if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
940 error_dialog (GTK_WINDOW (window), filename, err);
941 g_clear_error (&err);
945 if (!encoding || !encoding[0])
947 /* Determine the file's encoding and update sw->encoding. (The ordering
948 is important here because encoding_guess_whole_file() often returns
949 its argument instead of a copy of it.) */
950 char *guessed_encoding;
952 guessed_encoding = g_strdup (encoding_guess_whole_file (sw->encoding,
955 g_free (sw->encoding);
956 sw->encoding = guessed_encoding;
960 g_free (sw->encoding);
961 sw->encoding = g_strdup (encoding);
964 text_utf8 = recode_substring_pool ("UTF-8", sw->encoding,
965 ss_buffer (text_locale, len_locale),
969 if ( text_utf8 == NULL )
971 error_dialog (GTK_WINDOW (window), filename, err);
972 g_clear_error (&err);
976 gtk_text_buffer_get_iter_at_line (buffer, &iter, 0);
978 gtk_text_buffer_insert (buffer, &iter, text_utf8, len_utf8);
980 gtk_text_buffer_set_modified (buffer, FALSE);
984 add_most_recent (filename, "text/x-spss-syntax", sw->encoding);
992 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
994 iface->save = syntax_save;
995 iface->pick_filename = syntax_pick_filename;
996 iface->load = syntax_load;
1003 undo_redo_update (PsppireSyntaxWindow *window)
1005 gtk_action_set_sensitive (window->undo_menuitem,
1006 gtk_source_buffer_can_undo (window->buffer));
1008 gtk_action_set_sensitive (window->redo_menuitem,
1009 gtk_source_buffer_can_redo (window->buffer));
1013 undo_last_edit (PsppireSyntaxWindow *window)
1015 gtk_source_buffer_undo (window->buffer);
1016 undo_redo_update (window);
1020 redo_last_edit (PsppireSyntaxWindow *window)
1022 gtk_source_buffer_redo (window->buffer);
1023 undo_redo_update (window);
1028 /* Printing related stuff */
1032 begin_print (GtkPrintOperation *operation,
1033 GtkPrintContext *context,
1034 PsppireSyntaxWindow *window)
1036 window->compositor =
1037 gtk_source_print_compositor_new (window->buffer);
1042 end_print (GtkPrintOperation *operation,
1043 GtkPrintContext *context,
1044 PsppireSyntaxWindow *window)
1046 g_object_unref (window->compositor);
1047 window->compositor = NULL;
1053 paginate (GtkPrintOperation *operation,
1054 GtkPrintContext *context,
1055 PsppireSyntaxWindow *window)
1057 if (gtk_source_print_compositor_paginate (window->compositor, context))
1059 gint n_pages = gtk_source_print_compositor_get_n_pages (window->compositor);
1060 gtk_print_operation_set_n_pages (operation, n_pages);
1069 draw_page (GtkPrintOperation *operation,
1070 GtkPrintContext *context,
1072 PsppireSyntaxWindow *window)
1074 gtk_source_print_compositor_draw_page (window->compositor,
1082 psppire_syntax_window_print (PsppireSyntaxWindow *window)
1084 GtkPrintOperationResult res;
1086 GtkPrintOperation *print = gtk_print_operation_new ();
1088 if (window->print_settings != NULL)
1089 gtk_print_operation_set_print_settings (print, window->print_settings);
1092 g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
1093 g_signal_connect (print, "end_print", G_CALLBACK (end_print), window);
1094 g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), window);
1095 g_signal_connect (print, "paginate", G_CALLBACK (paginate), window);
1097 res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1098 GTK_WINDOW (window), NULL);
1100 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
1102 if (window->print_settings != NULL)
1103 g_object_unref (window->print_settings);
1104 window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
1107 g_object_unref (print);