1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2009, 2010, 2011 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/psppire-data-window.h"
35 #include "ui/gui/psppire-encoding-selector.h"
36 #include "ui/gui/psppire-lex-reader.h"
37 #include "ui/gui/psppire-syntax-window.h"
38 #include "ui/gui/psppire-window-register.h"
39 #include "ui/gui/psppire.h"
41 #include "gl/localcharset.h"
42 #include "gl/xalloc.h"
43 #include "gl/xvasprintf.h"
46 #define _(msgid) gettext (msgid)
47 #define N_(msgid) msgid
49 static void psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *, gpointer);
50 static void psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class);
51 static void psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class);
52 static void psppire_syntax_window_init (PsppireSyntaxWindow *syntax_editor);
55 static void psppire_syntax_window_iface_init (PsppireWindowIface *iface);
66 psppire_syntax_window_set_property (GObject *object,
71 PsppireSyntaxWindow *window = PSPPIRE_SYNTAX_WINDOW (object);
76 g_free (window->encoding);
77 window->encoding = g_value_dup_string (value);
80 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
87 psppire_syntax_window_get_property (GObject *object,
92 PsppireSyntaxWindow *window = PSPPIRE_SYNTAX_WINDOW (object);
97 g_value_set_string (value, window->encoding);
100 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
106 psppire_syntax_window_get_type (void)
108 static GType psppire_syntax_window_type = 0;
110 if (!psppire_syntax_window_type)
112 static const GTypeInfo psppire_syntax_window_info =
114 sizeof (PsppireSyntaxWindowClass),
115 (GBaseInitFunc) psppire_syntax_window_base_init,
116 (GBaseFinalizeFunc) psppire_syntax_window_base_finalize,
117 (GClassInitFunc)psppire_syntax_window_class_init,
118 (GClassFinalizeFunc) NULL,
120 sizeof (PsppireSyntaxWindow),
122 (GInstanceInitFunc) psppire_syntax_window_init,
125 static const GInterfaceInfo window_interface_info =
127 (GInterfaceInitFunc) psppire_syntax_window_iface_init,
132 psppire_syntax_window_type =
133 g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireSyntaxWindow",
134 &psppire_syntax_window_info, 0);
136 g_type_add_interface_static (psppire_syntax_window_type,
137 PSPPIRE_TYPE_WINDOW_MODEL,
138 &window_interface_info);
141 return psppire_syntax_window_type;
144 static GObjectClass *parent_class ;
147 psppire_syntax_window_finalize (GObject *object)
149 if (G_OBJECT_CLASS (parent_class)->finalize)
150 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
155 psppire_syntax_window_dispose (GObject *obj)
157 PsppireSyntaxWindow *sw = (PsppireSyntaxWindow *)obj;
159 GtkClipboard *clip_selection;
160 GtkClipboard *clip_primary;
162 if (sw->dispose_has_run)
165 g_free (sw->encoding);
168 clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
169 clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_PRIMARY);
171 g_signal_handler_disconnect (clip_primary, sw->sel_handler);
173 g_signal_handler_disconnect (clip_selection, sw->ps_handler);
175 /* Make sure dispose does not run twice. */
176 sw->dispose_has_run = TRUE;
178 /* Chain up to the parent class */
179 G_OBJECT_CLASS (parent_class)->dispose (obj);
185 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
187 GParamSpec *encoding_spec;
188 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
190 GtkSourceLanguageManager *lm = gtk_source_language_manager_get_default ();
192 const gchar * const *existing_paths = gtk_source_language_manager_get_search_path (lm);
193 gchar **new_paths = g_strdupv ((gchar **)existing_paths);
194 int n = g_strv_length ((gchar **) existing_paths);
196 new_paths = g_realloc (new_paths, (n + 2) * sizeof (*new_paths));
197 new_paths[n] = g_strdup (relocate (PKGDATADIR));
198 new_paths[n+1] = NULL;
200 lm = gtk_source_language_manager_new ();
201 gtk_source_language_manager_set_search_path (lm, new_paths);
203 class->lan = gtk_source_language_manager_get_language (lm, "pspp");
205 if (class->lan == NULL)
206 g_warning ("pspp.lang file not found. Syntax highlighting will not be available.");
208 parent_class = g_type_class_peek_parent (class);
210 g_strfreev (new_paths);
213 null_if_empty_param ("encoding",
214 "Character encoding",
215 "IANA character encoding in this syntax file",
217 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
219 parent_class = g_type_class_peek_parent (class);
221 gobject_class->set_property = psppire_syntax_window_set_property;
222 gobject_class->get_property = psppire_syntax_window_get_property;
223 gobject_class->dispose = psppire_syntax_window_dispose;
225 g_object_class_install_property (gobject_class,
232 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
234 GObjectClass *object_class = G_OBJECT_CLASS (class);
235 object_class->finalize = psppire_syntax_window_finalize;
241 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
248 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
251 PsppireWindow *win = PSPPIRE_WINDOW (sw);
252 struct lex_reader *reader = lex_reader_for_gtk_text_buffer (GTK_TEXT_BUFFER (sw->buffer), start, stop);
254 lex_reader_set_file_name (reader, psppire_window_get_filename (win));
256 execute_syntax (psppire_default_data_window (), reader);
259 /* Delete the currently selected text */
261 on_edit_delete (PsppireSyntaxWindow *sw)
263 GtkTextIter begin, end;
264 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
266 if ( gtk_text_buffer_get_selection_bounds (buffer, &begin, &end) )
267 gtk_text_buffer_delete (buffer, &begin, &end);
271 /* The syntax editor's clipboard deals only with text */
279 selection_changed (PsppireSyntaxWindow *sw)
281 gboolean sel = gtk_text_buffer_get_has_selection (GTK_TEXT_BUFFER (sw->buffer));
283 gtk_action_set_sensitive (sw->edit_copy, sel);
284 gtk_action_set_sensitive (sw->edit_cut, sel);
285 gtk_action_set_sensitive (sw->edit_delete, sel);
288 /* The callback which runs when something request clipboard data */
290 clipboard_get_cb (GtkClipboard *clipboard,
291 GtkSelectionData *selection_data,
295 PsppireSyntaxWindow *sw = data;
296 g_assert (info == SELECT_FMT_TEXT);
298 gtk_selection_data_set (selection_data, selection_data->target,
300 (const guchar *) sw->cliptext, strlen (sw->cliptext));
305 clipboard_clear_cb (GtkClipboard *clipboard,
308 PsppireSyntaxWindow *sw = data;
309 g_free (sw->cliptext);
314 static const GtkTargetEntry targets[] = {
315 { "UTF8_STRING", 0, SELECT_FMT_TEXT },
316 { "STRING", 0, SELECT_FMT_TEXT },
317 { "TEXT", 0, SELECT_FMT_TEXT },
318 { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
319 { "text/plain;charset=utf-8", 0, SELECT_FMT_TEXT },
320 { "text/plain", 0, SELECT_FMT_TEXT },
325 Store a clip containing the currently selected text.
326 Returns true iff something was set.
327 As a side effect, begin and end will be set to indicate
328 the limits of the selected text.
331 set_clip (PsppireSyntaxWindow *sw, GtkTextIter *begin, GtkTextIter *end)
333 GtkClipboard *clipboard ;
334 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
336 if ( ! gtk_text_buffer_get_selection_bounds (buffer, begin, end) )
339 g_free (sw->cliptext);
340 sw->cliptext = gtk_text_buffer_get_text (buffer, begin, end, FALSE);
343 gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
345 if (!gtk_clipboard_set_with_owner (clipboard, targets,
346 G_N_ELEMENTS (targets),
347 clipboard_get_cb, clipboard_clear_cb,
349 clipboard_clear_cb (clipboard, sw);
355 on_edit_cut (PsppireSyntaxWindow *sw)
357 GtkTextIter begin, end;
359 if ( set_clip (sw, &begin, &end))
360 gtk_text_buffer_delete (GTK_TEXT_BUFFER (sw->buffer), &begin, &end);
364 on_edit_copy (PsppireSyntaxWindow *sw)
366 GtkTextIter begin, end;
368 set_clip (sw, &begin, &end);
372 /* A callback for when the clipboard contents have been received */
374 contents_received_callback (GtkClipboard *clipboard,
375 GtkSelectionData *sd,
379 PsppireSyntaxWindow *syntax_window = data;
381 if ( sd->length < 0 )
384 if ( sd->type != gdk_atom_intern ("UTF8_STRING", FALSE))
387 c = (gchar *) sd->data;
389 gtk_text_buffer_insert_at_cursor (GTK_TEXT_BUFFER (syntax_window->buffer),
396 on_edit_paste (PsppireSyntaxWindow *sw)
398 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (sw));
399 GtkClipboard *clipboard =
400 gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
402 gtk_clipboard_request_contents (clipboard,
403 gdk_atom_intern ("UTF8_STRING", TRUE),
404 contents_received_callback,
409 /* Check to see if CLIP holds a target which we know how to paste,
410 and set the sensitivity of the Paste action accordingly.
413 set_paste_sensitivity (GtkClipboard *clip, GdkEventOwnerChange *event, gpointer data)
416 gboolean compatible_target = FALSE;
417 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (data);
419 for (i = 0 ; i < sizeof (targets) / sizeof (targets[0]) ; ++i)
421 GdkAtom atom = gdk_atom_intern (targets[i].target, TRUE);
422 if ( gtk_clipboard_wait_is_target_available (clip, atom))
424 compatible_target = TRUE;
429 gtk_action_set_sensitive (sw->edit_paste, compatible_target);
435 /* Parse and execute all the text in the buffer */
437 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
439 GtkTextIter begin, end;
440 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
442 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &begin, 0);
443 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &end, -1);
445 editor_execute_syntax (se, begin, end);
448 /* Parse and execute the currently selected text */
450 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
452 GtkTextIter begin, end;
453 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
455 if ( gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (se->buffer), &begin, &end) )
456 editor_execute_syntax (se, begin, end);
460 /* Parse and execute the from the current line, to the end of the
463 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
465 GtkTextIter begin, end;
469 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
471 /* Get the current line */
472 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
474 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
477 line = gtk_text_iter_get_line (&here) ;
479 /* Now set begin and end to the start of this line, and end of buffer
481 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
482 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, -1);
484 editor_execute_syntax (se, begin, end);
489 /* Parse and execute the current line */
491 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
493 GtkTextIter begin, end;
497 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
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 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
595 GTK_STOCK_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);
658 /* Callback for the File->Quit menuitem */
660 on_quit (GtkMenuItem *menuitem, gpointer user_data)
669 load_and_show_syntax_window (GtkWidget *se, const gchar *filename)
673 gtk_source_buffer_begin_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
674 ok = psppire_window_load (PSPPIRE_WINDOW (se), filename);
675 gtk_source_buffer_end_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
678 gtk_widget_show (se);
680 gtk_widget_destroy (se);
684 create_syntax_window (void)
686 GtkWidget *w = psppire_syntax_window_new (NULL);
691 open_syntax_window (const char *file_name, const gchar *encoding)
693 GtkWidget *se = psppire_syntax_window_new (encoding);
696 load_and_show_syntax_window (se, file_name);
701 static void psppire_syntax_window_print (PsppireSyntaxWindow *window);
704 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
706 if (gtk_text_buffer_get_modified (buffer))
707 psppire_window_set_unsaved (window);
710 static void undo_redo_update (PsppireSyntaxWindow *window);
711 static void undo_last_edit (PsppireSyntaxWindow *window);
712 static void redo_last_edit (PsppireSyntaxWindow *window);
715 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
717 gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
718 undo_redo_update (window);
722 psppire_syntax_window_init (PsppireSyntaxWindow *window)
724 GtkBuilder *xml = builder_new ("syntax-editor.ui");
725 GtkWidget *box = gtk_vbox_new (FALSE, 0);
727 GtkWidget *menubar = get_widget_assert (xml, "menubar");
728 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
730 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
732 PsppireSyntaxWindowClass *class
733 = PSPPIRE_SYNTAX_WINDOW_CLASS (G_OBJECT_GET_CLASS (window));
735 GtkClipboard *clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
736 GtkClipboard *clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
738 window->print_settings = NULL;
739 window->undo_menuitem = get_action_assert (xml, "edit_undo");
740 window->redo_menuitem = get_action_assert (xml, "edit_redo");
743 window->buffer = gtk_source_buffer_new_with_language (class->lan);
745 window->buffer = gtk_source_buffer_new (NULL);
747 gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), GTK_TEXT_BUFFER (window->buffer));
749 g_object_set (window->buffer,
750 "highlight-matching-brackets", TRUE,
753 g_object_set (text_view,
754 "show-line-numbers", TRUE,
755 "show-line-marks", TRUE,
758 "highlight-current-line", TRUE,
761 window->encoding = NULL;
763 window->cliptext = NULL;
764 window->dispose_has_run = FALSE;
766 window->edit_delete = get_action_assert (xml, "edit_delete");
767 window->edit_copy = get_action_assert (xml, "edit_copy");
768 window->edit_cut = get_action_assert (xml, "edit_cut");
769 window->edit_paste = get_action_assert (xml, "edit_paste");
771 window->buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view)));
773 window->sb = get_widget_assert (xml, "statusbar2");
774 window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
776 g_signal_connect (window->buffer, "changed",
777 G_CALLBACK (on_text_changed), window);
779 g_signal_connect (window->buffer, "modified-changed",
780 G_CALLBACK (on_modified_changed), window);
782 g_signal_connect_swapped (get_action_assert (xml, "file_print"), "activate",
783 G_CALLBACK (psppire_syntax_window_print), window);
786 g_signal_connect_swapped (window->undo_menuitem,
788 G_CALLBACK (undo_last_edit),
791 g_signal_connect_swapped (window->redo_menuitem,
793 G_CALLBACK (redo_last_edit),
796 undo_redo_update (window);
798 window->sel_handler = g_signal_connect_swapped (clip_primary, "owner-change",
799 G_CALLBACK (selection_changed), window);
801 window->ps_handler = g_signal_connect (clip_selection, "owner-change",
802 G_CALLBACK (set_paste_sensitivity), window);
806 gtk_container_add (GTK_CONTAINER (window), box);
808 g_object_ref (menubar);
812 g_object_ref (window->sb);
814 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
815 gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
816 gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
818 gtk_widget_show_all (box);
820 g_signal_connect_swapped (get_action_assert (xml,"file_new_syntax"), "activate", G_CALLBACK (create_syntax_window), NULL);
822 g_signal_connect (get_action_assert (xml,"file_new_data"),
824 G_CALLBACK (create_data_window),
827 g_signal_connect_swapped (get_action_assert (xml, "file_open"),
829 G_CALLBACK (psppire_window_open),
832 g_signal_connect_swapped (get_action_assert (xml, "file_save"),
834 G_CALLBACK (psppire_window_save),
837 g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
839 G_CALLBACK (psppire_window_save_as),
842 g_signal_connect (get_action_assert (xml,"file_quit"),
844 G_CALLBACK (on_quit),
847 g_signal_connect_swapped (window->edit_delete,
849 G_CALLBACK (on_edit_delete),
852 g_signal_connect_swapped (window->edit_copy,
854 G_CALLBACK (on_edit_copy),
857 g_signal_connect_swapped (window->edit_cut,
859 G_CALLBACK (on_edit_cut),
862 g_signal_connect_swapped (window->edit_paste,
864 G_CALLBACK (on_edit_paste),
867 g_signal_connect (get_action_assert (xml,"run_all"),
869 G_CALLBACK (on_run_all),
872 g_signal_connect (get_action_assert (xml,"run_selection"),
874 G_CALLBACK (on_run_selection),
877 g_signal_connect (get_action_assert (xml,"run_current_line"),
879 G_CALLBACK (on_run_current_line),
882 g_signal_connect (get_action_assert (xml,"run_to_end"),
884 G_CALLBACK (on_run_to_end),
887 g_signal_connect (get_action_assert (xml,"windows_minimise_all"),
889 G_CALLBACK (psppire_window_minimise_all), NULL);
896 GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
898 merge_help_menu (uim);
900 PSPPIRE_WINDOW (window)->menu =
901 GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
904 g_object_unref (xml);
912 psppire_syntax_window_new (const char *encoding)
914 return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
915 "description", _("Syntax Editor"),
916 "encoding", encoding,
921 error_dialog (GtkWindow *w, const gchar *filename, GError *err)
923 gchar *fn = g_filename_display_basename (filename);
926 gtk_message_dialog_new (w,
927 GTK_DIALOG_DESTROY_WITH_PARENT,
930 _("Cannot load syntax file `%s'"),
935 g_object_set (dialog, "icon-name", "pspp", NULL);
937 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
940 gtk_dialog_run (GTK_DIALOG (dialog));
942 gtk_widget_destroy (dialog);
946 Loads the buffer from the file called FILENAME
949 syntax_load (PsppireWindow *window, const gchar *filename)
952 gchar *text_locale = NULL;
953 gchar *text_utf8 = NULL;
954 gsize len_locale = -1;
957 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
958 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
962 /* FIXME: What if it's a very big file ? */
963 if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
965 error_dialog (GTK_WINDOW (window), filename, err);
966 g_clear_error (&err);
970 /* Determine the file's encoding and update sw->encoding. (The ordering is
971 important here because encoding_guess_whole_file() often returns its
972 argument instead of a copy of it.) */
973 encoding = g_strdup (encoding_guess_whole_file (sw->encoding, text_locale,
975 g_free (sw->encoding);
976 sw->encoding = encoding;
978 text_utf8 = recode_substring_pool ("UTF-8", encoding,
979 ss_buffer (text_locale, len_locale),
983 if ( text_utf8 == NULL )
985 error_dialog (GTK_WINDOW (window), filename, err);
986 g_clear_error (&err);
990 gtk_text_buffer_get_iter_at_line (buffer, &iter, 0);
992 gtk_text_buffer_insert (buffer, &iter, text_utf8, len_utf8);
994 gtk_text_buffer_set_modified (buffer, FALSE);
998 mime_type = xasprintf ("text/x-spss-syntax; charset=%s", sw->encoding);
999 add_most_recent (filename, mime_type);
1008 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
1010 iface->save = syntax_save;
1011 iface->pick_filename = syntax_pick_filename;
1012 iface->load = syntax_load;
1019 undo_redo_update (PsppireSyntaxWindow *window)
1021 gtk_action_set_sensitive (window->undo_menuitem,
1022 gtk_source_buffer_can_undo (window->buffer));
1024 gtk_action_set_sensitive (window->redo_menuitem,
1025 gtk_source_buffer_can_redo (window->buffer));
1029 undo_last_edit (PsppireSyntaxWindow *window)
1031 gtk_source_buffer_undo (window->buffer);
1032 undo_redo_update (window);
1036 redo_last_edit (PsppireSyntaxWindow *window)
1038 gtk_source_buffer_redo (window->buffer);
1039 undo_redo_update (window);
1044 /* Printing related stuff */
1048 begin_print (GtkPrintOperation *operation,
1049 GtkPrintContext *context,
1050 PsppireSyntaxWindow *window)
1052 window->compositor =
1053 gtk_source_print_compositor_new (window->buffer);
1058 end_print (GtkPrintOperation *operation,
1059 GtkPrintContext *context,
1060 PsppireSyntaxWindow *window)
1062 g_object_unref (window->compositor);
1063 window->compositor = NULL;
1069 paginate (GtkPrintOperation *operation,
1070 GtkPrintContext *context,
1071 PsppireSyntaxWindow *window)
1073 if (gtk_source_print_compositor_paginate (window->compositor, context))
1075 gint n_pages = gtk_source_print_compositor_get_n_pages (window->compositor);
1076 gtk_print_operation_set_n_pages (operation, n_pages);
1085 draw_page (GtkPrintOperation *operation,
1086 GtkPrintContext *context,
1088 PsppireSyntaxWindow *window)
1090 gtk_source_print_compositor_draw_page (window->compositor,
1098 psppire_syntax_window_print (PsppireSyntaxWindow *window)
1100 GtkPrintOperationResult res;
1102 GtkPrintOperation *print = gtk_print_operation_new ();
1104 if (window->print_settings != NULL)
1105 gtk_print_operation_set_print_settings (print, window->print_settings);
1108 g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
1109 g_signal_connect (print, "end_print", G_CALLBACK (end_print), window);
1110 g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), window);
1111 g_signal_connect (print, "paginate", G_CALLBACK (paginate), window);
1113 res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1114 GTK_WINDOW (window), NULL);
1116 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
1118 if (window->print_settings != NULL)
1119 g_object_unref (window->print_settings);
1120 window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
1123 g_object_unref (print);