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>
28 #include "language/lexer/lexer.h"
29 #include "libpspp/encoding-guesser.h"
30 #include "libpspp/i18n.h"
31 #include "libpspp/message.h"
32 #include "ui/gui/executor.h"
33 #include "ui/gui/help-menu.h"
34 #include "ui/gui/helper.h"
35 #include "ui/gui/psppire-data-window.h"
36 #include "ui/gui/psppire-encoding-selector.h"
37 #include "ui/gui/psppire-syntax-window.h"
38 #include "ui/gui/psppire-syntax-window.h"
39 #include "ui/gui/psppire-window-register.h"
40 #include "ui/gui/psppire.h"
41 #include "ui/gui/psppire.h"
43 #include "gl/localcharset.h"
44 #include "gl/xalloc.h"
45 #include "gl/xvasprintf.h"
48 #define _(msgid) gettext (msgid)
49 #define N_(msgid) msgid
51 static void psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *, gpointer);
52 static void psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class);
53 static void psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class);
54 static void psppire_syntax_window_init (PsppireSyntaxWindow *syntax_editor);
57 static void psppire_syntax_window_iface_init (PsppireWindowIface *iface);
68 psppire_syntax_window_set_property (GObject *object,
73 PsppireSyntaxWindow *window = PSPPIRE_SYNTAX_WINDOW (object);
78 g_free (window->encoding);
79 window->encoding = g_value_dup_string (value);
82 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
89 psppire_syntax_window_get_property (GObject *object,
94 PsppireSyntaxWindow *window = PSPPIRE_SYNTAX_WINDOW (object);
99 g_value_set_string (value, window->encoding);
102 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
108 psppire_syntax_window_get_type (void)
110 static GType psppire_syntax_window_type = 0;
112 if (!psppire_syntax_window_type)
114 static const GTypeInfo psppire_syntax_window_info =
116 sizeof (PsppireSyntaxWindowClass),
117 (GBaseInitFunc) psppire_syntax_window_base_init,
118 (GBaseFinalizeFunc) psppire_syntax_window_base_finalize,
119 (GClassInitFunc)psppire_syntax_window_class_init,
120 (GClassFinalizeFunc) NULL,
122 sizeof (PsppireSyntaxWindow),
124 (GInstanceInitFunc) psppire_syntax_window_init,
127 static const GInterfaceInfo window_interface_info =
129 (GInterfaceInitFunc) psppire_syntax_window_iface_init,
134 psppire_syntax_window_type =
135 g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireSyntaxWindow",
136 &psppire_syntax_window_info, 0);
138 g_type_add_interface_static (psppire_syntax_window_type,
139 PSPPIRE_TYPE_WINDOW_MODEL,
140 &window_interface_info);
143 return psppire_syntax_window_type;
146 static GObjectClass *parent_class ;
149 psppire_syntax_window_finalize (GObject *object)
151 if (G_OBJECT_CLASS (parent_class)->finalize)
152 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
157 psppire_syntax_window_dispose (GObject *obj)
159 PsppireSyntaxWindow *sw = (PsppireSyntaxWindow *)obj;
161 GtkClipboard *clip_selection;
162 GtkClipboard *clip_primary;
164 if (sw->dispose_has_run)
167 g_free (sw->encoding);
170 clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
171 clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_PRIMARY);
173 g_signal_handler_disconnect (clip_primary, sw->sel_handler);
175 g_signal_handler_disconnect (clip_selection, sw->ps_handler);
177 /* Make sure dispose does not run twice. */
178 sw->dispose_has_run = TRUE;
180 /* Chain up to the parent class */
181 G_OBJECT_CLASS (parent_class)->dispose (obj);
187 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
189 GParamSpec *encoding_spec;
190 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
192 GtkSourceLanguageManager *lm = gtk_source_language_manager_get_default ();
194 const gchar * const *existing_paths = gtk_source_language_manager_get_search_path (lm);
195 gchar **new_paths = g_strdupv ((gchar **)existing_paths);
196 int n = g_strv_length ((gchar **) existing_paths);
198 new_paths = g_realloc (new_paths, (n + 2) * sizeof (*new_paths));
199 new_paths[n] = g_strdup (relocate (PKGDATADIR));
200 new_paths[n+1] = NULL;
202 lm = gtk_source_language_manager_new ();
203 gtk_source_language_manager_set_search_path (lm, new_paths);
205 class->lan = gtk_source_language_manager_get_language (lm, "pspp");
207 if (class->lan == NULL)
208 g_warning ("pspp.lang file not found. Syntax highlighting will not be available.");
210 parent_class = g_type_class_peek_parent (class);
212 g_strfreev (new_paths);
215 null_if_empty_param ("encoding",
216 "Character encoding",
217 "IANA character encoding in this syntax file",
219 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
221 parent_class = g_type_class_peek_parent (class);
223 gobject_class->set_property = psppire_syntax_window_set_property;
224 gobject_class->get_property = psppire_syntax_window_get_property;
225 gobject_class->dispose = psppire_syntax_window_dispose;
227 g_object_class_install_property (gobject_class,
234 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
236 GObjectClass *object_class = G_OBJECT_CLASS (class);
237 object_class->finalize = psppire_syntax_window_finalize;
243 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
250 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
253 PsppireWindow *win = PSPPIRE_WINDOW (sw);
254 struct lex_reader *reader;
255 gchar *text = gtk_text_buffer_get_text (GTK_TEXT_BUFFER (sw->buffer), &start, &stop, FALSE);
256 reader = lex_reader_for_string (text);
259 lex_reader_set_file_name (reader, psppire_window_get_filename (win));
261 execute_syntax (psppire_default_data_window (), reader);
264 /* Delete the currently selected text */
266 on_edit_delete (PsppireSyntaxWindow *sw)
268 GtkTextIter begin, end;
269 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
271 if ( gtk_text_buffer_get_selection_bounds (buffer, &begin, &end) )
272 gtk_text_buffer_delete (buffer, &begin, &end);
276 /* The syntax editor's clipboard deals only with text */
284 selection_changed (PsppireSyntaxWindow *sw)
286 gboolean sel = gtk_text_buffer_get_has_selection (GTK_TEXT_BUFFER (sw->buffer));
288 gtk_action_set_sensitive (sw->edit_copy, sel);
289 gtk_action_set_sensitive (sw->edit_cut, sel);
290 gtk_action_set_sensitive (sw->edit_delete, sel);
293 /* The callback which runs when something request clipboard data */
295 clipboard_get_cb (GtkClipboard *clipboard,
296 GtkSelectionData *selection_data,
300 PsppireSyntaxWindow *sw = data;
301 g_assert (info == SELECT_FMT_TEXT);
303 gtk_selection_data_set (selection_data, selection_data->target,
305 (const guchar *) sw->cliptext, strlen (sw->cliptext));
310 clipboard_clear_cb (GtkClipboard *clipboard,
313 PsppireSyntaxWindow *sw = data;
314 g_free (sw->cliptext);
319 static const GtkTargetEntry targets[] = {
320 { "UTF8_STRING", 0, SELECT_FMT_TEXT },
321 { "STRING", 0, SELECT_FMT_TEXT },
322 { "TEXT", 0, SELECT_FMT_TEXT },
323 { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
324 { "text/plain;charset=utf-8", 0, SELECT_FMT_TEXT },
325 { "text/plain", 0, SELECT_FMT_TEXT },
330 Store a clip containing the currently selected text.
331 Returns true iff something was set.
332 As a side effect, begin and end will be set to indicate
333 the limits of the selected text.
336 set_clip (PsppireSyntaxWindow *sw, GtkTextIter *begin, GtkTextIter *end)
338 GtkClipboard *clipboard ;
339 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
341 if ( ! gtk_text_buffer_get_selection_bounds (buffer, begin, end) )
344 g_free (sw->cliptext);
345 sw->cliptext = gtk_text_buffer_get_text (buffer, begin, end, FALSE);
348 gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
350 if (!gtk_clipboard_set_with_owner (clipboard, targets,
351 G_N_ELEMENTS (targets),
352 clipboard_get_cb, clipboard_clear_cb,
354 clipboard_clear_cb (clipboard, sw);
360 on_edit_cut (PsppireSyntaxWindow *sw)
362 GtkTextIter begin, end;
364 if ( set_clip (sw, &begin, &end))
365 gtk_text_buffer_delete (GTK_TEXT_BUFFER (sw->buffer), &begin, &end);
369 on_edit_copy (PsppireSyntaxWindow *sw)
371 GtkTextIter begin, end;
373 set_clip (sw, &begin, &end);
377 /* A callback for when the clipboard contents have been received */
379 contents_received_callback (GtkClipboard *clipboard,
380 GtkSelectionData *sd,
384 PsppireSyntaxWindow *syntax_window = data;
386 if ( sd->length < 0 )
389 if ( sd->type != gdk_atom_intern ("UTF8_STRING", FALSE))
392 c = (gchar *) sd->data;
394 gtk_text_buffer_insert_at_cursor (GTK_TEXT_BUFFER (syntax_window->buffer),
401 on_edit_paste (PsppireSyntaxWindow *sw)
403 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (sw));
404 GtkClipboard *clipboard =
405 gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
407 gtk_clipboard_request_contents (clipboard,
408 gdk_atom_intern ("UTF8_STRING", TRUE),
409 contents_received_callback,
414 /* Check to see if CLIP holds a target which we know how to paste,
415 and set the sensitivity of the Paste action accordingly.
418 set_paste_sensitivity (GtkClipboard *clip, GdkEventOwnerChange *event, gpointer data)
421 gboolean compatible_target = FALSE;
422 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (data);
424 for (i = 0 ; i < sizeof (targets) / sizeof (targets[0]) ; ++i)
426 GdkAtom atom = gdk_atom_intern (targets[i].target, TRUE);
427 if ( gtk_clipboard_wait_is_target_available (clip, atom))
429 compatible_target = TRUE;
434 gtk_action_set_sensitive (sw->edit_paste, compatible_target);
440 /* Parse and execute all the text in the buffer */
442 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
444 GtkTextIter begin, end;
445 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
447 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &begin, 0);
448 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &end, -1);
450 editor_execute_syntax (se, begin, end);
453 /* Parse and execute the currently selected text */
455 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
457 GtkTextIter begin, end;
458 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
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 (GtkMenuItem *menuitem, gpointer user_data)
470 GtkTextIter begin, end;
474 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
476 /* Get the current line */
477 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
479 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
482 line = gtk_text_iter_get_line (&here) ;
484 /* Now set begin and end to the start of this line, and end of buffer
486 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
487 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, -1);
489 editor_execute_syntax (se, begin, end);
494 /* Parse and execute the current line */
496 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
498 GtkTextIter begin, end;
502 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
504 /* Get the current line */
505 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
507 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
510 line = gtk_text_iter_get_line (&here) ;
512 /* Now set begin and end to the start of this line, and start of
513 following line respectively */
514 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
515 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, line + 1);
517 editor_execute_syntax (se, begin, end);
522 /* Append ".sps" to FILENAME if necessary.
523 The returned result must be freed when no longer required.
526 append_suffix (const gchar *filename)
528 if ( ! g_str_has_suffix (filename, ".sps" ) &&
529 ! g_str_has_suffix (filename, ".SPS" ) )
531 return g_strdup_printf ("%s.sps", filename);
534 return xstrdup (filename);
538 Save BUFFER to the file called FILENAME.
539 FILENAME must be encoded in Glib filename encoding.
540 If successful, clears the buffer's modified flag.
543 save_editor_to_file (PsppireSyntaxWindow *se,
544 const gchar *filename,
547 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (se->buffer);
548 struct substring text_locale;
550 GtkTextIter start, stop;
556 suffixedname = append_suffix (filename);
558 gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
559 gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
561 text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
563 text_locale = recode_substring_pool (se->encoding, "UTF-8", ss_cstr (text),
566 result = g_file_set_contents (suffixedname, ss_data (text_locale),
567 ss_length (text_locale), err);
569 ss_dealloc (&text_locale);
570 g_free (suffixedname);
574 char *fn = g_filename_display_name (filename);
575 gchar *msg = g_strdup_printf (_("Saved file `%s'"), fn);
577 gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
578 gtk_text_buffer_set_modified (buffer, FALSE);
586 /* PsppireWindow 'pick_Filename' callback. */
588 syntax_pick_filename (PsppireWindow *window)
590 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (window);
591 const char *default_encoding;
592 GtkFileFilter *filter;
596 gtk_file_chooser_dialog_new (_("Save Syntax"),
598 GTK_FILE_CHOOSER_ACTION_SAVE,
599 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
600 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
603 filter = gtk_file_filter_new ();
604 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
605 gtk_file_filter_add_pattern (filter, "*.sps");
606 gtk_file_filter_add_pattern (filter, "*.SPS");
607 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
609 filter = gtk_file_filter_new ();
610 gtk_file_filter_set_name (filter, _("All Files"));
611 gtk_file_filter_add_pattern (filter, "*");
612 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
614 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
617 default_encoding = se->encoding != NULL ? se->encoding : locale_charset ();
618 gtk_file_chooser_set_extra_widget (
619 GTK_FILE_CHOOSER (dialog),
620 psppire_encoding_selector_new (default_encoding, false));
622 response = gtk_dialog_run (GTK_DIALOG (dialog));
624 if ( response == GTK_RESPONSE_ACCEPT )
629 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
630 psppire_window_set_filename (window, filename);
633 encoding = psppire_encoding_selector_get_encoding (
634 gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
635 if (encoding != NULL)
637 g_free (se->encoding);
638 se->encoding = encoding;
642 gtk_widget_destroy (dialog);
646 /* PsppireWindow 'save' callback. */
648 syntax_save (PsppireWindow *se)
650 const gchar *filename = psppire_window_get_filename (se);
652 save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
655 msg (ME, "%s", err->message);
661 /* Callback for the File->Quit menuitem */
663 on_quit (GtkMenuItem *menuitem, gpointer user_data)
672 load_and_show_syntax_window (GtkWidget *se, const gchar *filename)
676 gtk_source_buffer_begin_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
677 ok = psppire_window_load (PSPPIRE_WINDOW (se), filename);
678 gtk_source_buffer_end_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
681 gtk_widget_show (se);
683 gtk_widget_destroy (se);
687 create_syntax_window (void)
689 GtkWidget *w = psppire_syntax_window_new (NULL);
694 open_syntax_window (const char *file_name, const gchar *encoding)
696 GtkWidget *se = psppire_syntax_window_new (encoding);
699 load_and_show_syntax_window (se, file_name);
704 static void psppire_syntax_window_print (PsppireSyntaxWindow *window);
707 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
709 if (gtk_text_buffer_get_modified (buffer))
710 psppire_window_set_unsaved (window);
713 static void undo_redo_update (PsppireSyntaxWindow *window);
714 static void undo_last_edit (PsppireSyntaxWindow *window);
715 static void redo_last_edit (PsppireSyntaxWindow *window);
718 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
720 gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
721 undo_redo_update (window);
725 psppire_syntax_window_init (PsppireSyntaxWindow *window)
727 GtkBuilder *xml = builder_new ("syntax-editor.ui");
728 GtkWidget *box = gtk_vbox_new (FALSE, 0);
730 GtkWidget *menubar = get_widget_assert (xml, "menubar");
731 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
733 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
735 PsppireSyntaxWindowClass *class
736 = PSPPIRE_SYNTAX_WINDOW_CLASS (G_OBJECT_GET_CLASS (window));
738 GtkClipboard *clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
739 GtkClipboard *clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
741 window->print_settings = NULL;
742 window->undo_menuitem = get_action_assert (xml, "edit_undo");
743 window->redo_menuitem = get_action_assert (xml, "edit_redo");
746 window->buffer = gtk_source_buffer_new_with_language (class->lan);
748 window->buffer = gtk_source_buffer_new (NULL);
750 gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), GTK_TEXT_BUFFER (window->buffer));
752 g_object_set (window->buffer,
753 "highlight-matching-brackets", TRUE,
756 g_object_set (text_view,
757 "show-line-numbers", TRUE,
758 "show-line-marks", TRUE,
761 "highlight-current-line", TRUE,
764 window->encoding = NULL;
766 window->cliptext = NULL;
767 window->dispose_has_run = FALSE;
769 window->edit_delete = get_action_assert (xml, "edit_delete");
770 window->edit_copy = get_action_assert (xml, "edit_copy");
771 window->edit_cut = get_action_assert (xml, "edit_cut");
772 window->edit_paste = get_action_assert (xml, "edit_paste");
774 window->buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view)));
776 window->sb = get_widget_assert (xml, "statusbar2");
777 window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
779 g_signal_connect (window->buffer, "changed",
780 G_CALLBACK (on_text_changed), window);
782 g_signal_connect (window->buffer, "modified-changed",
783 G_CALLBACK (on_modified_changed), window);
785 g_signal_connect_swapped (get_action_assert (xml, "file_print"), "activate",
786 G_CALLBACK (psppire_syntax_window_print), window);
789 g_signal_connect_swapped (window->undo_menuitem,
791 G_CALLBACK (undo_last_edit),
794 g_signal_connect_swapped (window->redo_menuitem,
796 G_CALLBACK (redo_last_edit),
799 undo_redo_update (window);
801 window->sel_handler = g_signal_connect_swapped (clip_primary, "owner-change",
802 G_CALLBACK (selection_changed), window);
804 window->ps_handler = g_signal_connect (clip_selection, "owner-change",
805 G_CALLBACK (set_paste_sensitivity), window);
809 gtk_container_add (GTK_CONTAINER (window), box);
811 g_object_ref (menubar);
815 g_object_ref (window->sb);
817 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
818 gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
819 gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
821 gtk_widget_show_all (box);
823 g_signal_connect_swapped (get_action_assert (xml,"file_new_syntax"), "activate", G_CALLBACK (create_syntax_window), NULL);
825 g_signal_connect (get_action_assert (xml,"file_new_data"),
827 G_CALLBACK (create_data_window),
830 g_signal_connect_swapped (get_action_assert (xml, "file_open"),
832 G_CALLBACK (psppire_window_open),
835 g_signal_connect_swapped (get_action_assert (xml, "file_save"),
837 G_CALLBACK (psppire_window_save),
840 g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
842 G_CALLBACK (psppire_window_save_as),
845 g_signal_connect (get_action_assert (xml,"file_quit"),
847 G_CALLBACK (on_quit),
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),
870 g_signal_connect (get_action_assert (xml,"run_all"),
872 G_CALLBACK (on_run_all),
875 g_signal_connect (get_action_assert (xml,"run_selection"),
877 G_CALLBACK (on_run_selection),
880 g_signal_connect (get_action_assert (xml,"run_current_line"),
882 G_CALLBACK (on_run_current_line),
885 g_signal_connect (get_action_assert (xml,"run_to_end"),
887 G_CALLBACK (on_run_to_end),
890 g_signal_connect (get_action_assert (xml,"windows_minimise_all"),
892 G_CALLBACK (psppire_window_minimise_all), NULL);
899 GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
901 merge_help_menu (uim);
903 PSPPIRE_WINDOW (window)->menu =
904 GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
907 g_object_unref (xml);
915 psppire_syntax_window_new (const char *encoding)
917 return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
918 "description", _("Syntax Editor"),
919 "encoding", encoding,
924 error_dialog (GtkWindow *w, const gchar *filename, GError *err)
926 gchar *fn = g_filename_display_basename (filename);
929 gtk_message_dialog_new (w,
930 GTK_DIALOG_DESTROY_WITH_PARENT,
933 _("Cannot load syntax file `%s'"),
938 g_object_set (dialog, "icon-name", "psppicon", NULL);
940 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
943 gtk_dialog_run (GTK_DIALOG (dialog));
945 gtk_widget_destroy (dialog);
949 Loads the buffer from the file called FILENAME
952 syntax_load (PsppireWindow *window, const gchar *filename)
955 gchar *text_locale = NULL;
956 gchar *text_utf8 = NULL;
957 gsize len_locale = -1;
960 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
961 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
965 /* FIXME: What if it's a very big file ? */
966 if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
968 error_dialog (GTK_WINDOW (window), filename, err);
969 g_clear_error (&err);
973 /* Determine the file's encoding and update sw->encoding. (The ordering is
974 important here because encoding_guess_whole_file() often returns its
975 argument instead of a copy of it.) */
976 encoding = g_strdup (encoding_guess_whole_file (sw->encoding, text_locale,
978 g_free (sw->encoding);
979 sw->encoding = encoding;
981 text_utf8 = recode_substring_pool ("UTF-8", encoding,
982 ss_buffer (text_locale, len_locale),
986 if ( text_utf8 == NULL )
988 error_dialog (GTK_WINDOW (window), filename, err);
989 g_clear_error (&err);
993 gtk_text_buffer_get_iter_at_line (buffer, &iter, 0);
995 gtk_text_buffer_insert (buffer, &iter, text_utf8, len_utf8);
997 gtk_text_buffer_set_modified (buffer, FALSE);
1001 mime_type = xasprintf ("text/x-spss-syntax; charset=%s", sw->encoding);
1002 add_most_recent (filename, mime_type);
1011 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
1013 iface->save = syntax_save;
1014 iface->pick_filename = syntax_pick_filename;
1015 iface->load = syntax_load;
1022 undo_redo_update (PsppireSyntaxWindow *window)
1024 gtk_action_set_sensitive (window->undo_menuitem,
1025 gtk_source_buffer_can_undo (window->buffer));
1027 gtk_action_set_sensitive (window->redo_menuitem,
1028 gtk_source_buffer_can_redo (window->buffer));
1032 undo_last_edit (PsppireSyntaxWindow *window)
1034 gtk_source_buffer_undo (window->buffer);
1035 undo_redo_update (window);
1039 redo_last_edit (PsppireSyntaxWindow *window)
1041 gtk_source_buffer_redo (window->buffer);
1042 undo_redo_update (window);
1047 /* Printing related stuff */
1051 begin_print (GtkPrintOperation *operation,
1052 GtkPrintContext *context,
1053 PsppireSyntaxWindow *window)
1055 window->compositor =
1056 gtk_source_print_compositor_new (window->buffer);
1061 end_print (GtkPrintOperation *operation,
1062 GtkPrintContext *context,
1063 PsppireSyntaxWindow *window)
1065 g_object_unref (window->compositor);
1066 window->compositor = NULL;
1072 paginate (GtkPrintOperation *operation,
1073 GtkPrintContext *context,
1074 PsppireSyntaxWindow *window)
1076 if (gtk_source_print_compositor_paginate (window->compositor, context))
1078 gint n_pages = gtk_source_print_compositor_get_n_pages (window->compositor);
1079 gtk_print_operation_set_n_pages (operation, n_pages);
1088 draw_page (GtkPrintOperation *operation,
1089 GtkPrintContext *context,
1091 PsppireSyntaxWindow *window)
1093 gtk_source_print_compositor_draw_page (window->compositor,
1101 psppire_syntax_window_print (PsppireSyntaxWindow *window)
1103 GtkPrintOperationResult res;
1105 GtkPrintOperation *print = gtk_print_operation_new ();
1107 if (window->print_settings != NULL)
1108 gtk_print_operation_set_print_settings (print, window->print_settings);
1111 g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
1112 g_signal_connect (print, "end_print", G_CALLBACK (end_print), window);
1113 g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), window);
1114 g_signal_connect (print, "paginate", G_CALLBACK (paginate), window);
1116 res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1117 GTK_WINDOW (window), NULL);
1119 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
1121 if (window->print_settings != NULL)
1122 g_object_unref (window->print_settings);
1123 window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
1126 g_object_unref (print);