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 g_object_set (dialog, "local-only", FALSE, NULL);
605 filter = gtk_file_filter_new ();
606 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
607 gtk_file_filter_add_pattern (filter, "*.sps");
608 gtk_file_filter_add_pattern (filter, "*.SPS");
609 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
611 filter = gtk_file_filter_new ();
612 gtk_file_filter_set_name (filter, _("All Files"));
613 gtk_file_filter_add_pattern (filter, "*");
614 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
616 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
619 default_encoding = se->encoding != NULL ? se->encoding : locale_charset ();
620 gtk_file_chooser_set_extra_widget (
621 GTK_FILE_CHOOSER (dialog),
622 psppire_encoding_selector_new (default_encoding, false));
624 response = gtk_dialog_run (GTK_DIALOG (dialog));
626 if ( response == GTK_RESPONSE_ACCEPT )
631 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
632 psppire_window_set_filename (window, filename);
635 encoding = psppire_encoding_selector_get_encoding (
636 gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
637 if (encoding != NULL)
639 g_free (se->encoding);
640 se->encoding = encoding;
644 gtk_widget_destroy (dialog);
648 /* PsppireWindow 'save' callback. */
650 syntax_save (PsppireWindow *se)
652 const gchar *filename = psppire_window_get_filename (se);
654 save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
657 msg (ME, "%s", err->message);
663 /* Callback for the File->Quit menuitem */
665 on_quit (GtkMenuItem *menuitem, gpointer user_data)
674 load_and_show_syntax_window (GtkWidget *se, const gchar *filename)
678 gtk_source_buffer_begin_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
679 ok = psppire_window_load (PSPPIRE_WINDOW (se), filename);
680 gtk_source_buffer_end_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
683 gtk_widget_show (se);
685 gtk_widget_destroy (se);
689 create_syntax_window (void)
691 GtkWidget *w = psppire_syntax_window_new (NULL);
696 open_syntax_window (const char *file_name, const gchar *encoding)
698 GtkWidget *se = psppire_syntax_window_new (encoding);
701 load_and_show_syntax_window (se, file_name);
706 static void psppire_syntax_window_print (PsppireSyntaxWindow *window);
709 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
711 if (gtk_text_buffer_get_modified (buffer))
712 psppire_window_set_unsaved (window);
715 static void undo_redo_update (PsppireSyntaxWindow *window);
716 static void undo_last_edit (PsppireSyntaxWindow *window);
717 static void redo_last_edit (PsppireSyntaxWindow *window);
720 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
722 gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
723 undo_redo_update (window);
727 psppire_syntax_window_init (PsppireSyntaxWindow *window)
729 GtkBuilder *xml = builder_new ("syntax-editor.ui");
730 GtkWidget *box = gtk_vbox_new (FALSE, 0);
732 GtkWidget *menubar = get_widget_assert (xml, "menubar");
733 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
735 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
737 PsppireSyntaxWindowClass *class
738 = PSPPIRE_SYNTAX_WINDOW_CLASS (G_OBJECT_GET_CLASS (window));
740 GtkClipboard *clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
741 GtkClipboard *clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
743 window->print_settings = NULL;
744 window->undo_menuitem = get_action_assert (xml, "edit_undo");
745 window->redo_menuitem = get_action_assert (xml, "edit_redo");
748 window->buffer = gtk_source_buffer_new_with_language (class->lan);
750 window->buffer = gtk_source_buffer_new (NULL);
752 gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), GTK_TEXT_BUFFER (window->buffer));
754 g_object_set (window->buffer,
755 "highlight-matching-brackets", TRUE,
758 g_object_set (text_view,
759 "show-line-numbers", TRUE,
760 "show-line-marks", TRUE,
763 "highlight-current-line", TRUE,
766 window->encoding = NULL;
768 window->cliptext = NULL;
769 window->dispose_has_run = FALSE;
771 window->edit_delete = get_action_assert (xml, "edit_delete");
772 window->edit_copy = get_action_assert (xml, "edit_copy");
773 window->edit_cut = get_action_assert (xml, "edit_cut");
774 window->edit_paste = get_action_assert (xml, "edit_paste");
776 window->buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view)));
778 window->sb = get_widget_assert (xml, "statusbar2");
779 window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
781 g_signal_connect (window->buffer, "changed",
782 G_CALLBACK (on_text_changed), window);
784 g_signal_connect (window->buffer, "modified-changed",
785 G_CALLBACK (on_modified_changed), window);
787 g_signal_connect_swapped (get_action_assert (xml, "file_print"), "activate",
788 G_CALLBACK (psppire_syntax_window_print), window);
791 g_signal_connect_swapped (window->undo_menuitem,
793 G_CALLBACK (undo_last_edit),
796 g_signal_connect_swapped (window->redo_menuitem,
798 G_CALLBACK (redo_last_edit),
801 undo_redo_update (window);
803 window->sel_handler = g_signal_connect_swapped (clip_primary, "owner-change",
804 G_CALLBACK (selection_changed), window);
806 window->ps_handler = g_signal_connect (clip_selection, "owner-change",
807 G_CALLBACK (set_paste_sensitivity), window);
811 gtk_container_add (GTK_CONTAINER (window), box);
813 g_object_ref (menubar);
817 g_object_ref (window->sb);
819 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
820 gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
821 gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
823 gtk_widget_show_all (box);
825 g_signal_connect_swapped (get_action_assert (xml,"file_new_syntax"), "activate", G_CALLBACK (create_syntax_window), NULL);
827 g_signal_connect (get_action_assert (xml,"file_new_data"),
829 G_CALLBACK (create_data_window),
832 g_signal_connect_swapped (get_action_assert (xml, "file_open"),
834 G_CALLBACK (psppire_window_open),
837 g_signal_connect_swapped (get_action_assert (xml, "file_save"),
839 G_CALLBACK (psppire_window_save),
842 g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
844 G_CALLBACK (psppire_window_save_as),
847 g_signal_connect (get_action_assert (xml,"file_quit"),
849 G_CALLBACK (on_quit),
852 g_signal_connect_swapped (window->edit_delete,
854 G_CALLBACK (on_edit_delete),
857 g_signal_connect_swapped (window->edit_copy,
859 G_CALLBACK (on_edit_copy),
862 g_signal_connect_swapped (window->edit_cut,
864 G_CALLBACK (on_edit_cut),
867 g_signal_connect_swapped (window->edit_paste,
869 G_CALLBACK (on_edit_paste),
872 g_signal_connect (get_action_assert (xml,"run_all"),
874 G_CALLBACK (on_run_all),
877 g_signal_connect (get_action_assert (xml,"run_selection"),
879 G_CALLBACK (on_run_selection),
882 g_signal_connect (get_action_assert (xml,"run_current_line"),
884 G_CALLBACK (on_run_current_line),
887 g_signal_connect (get_action_assert (xml,"run_to_end"),
889 G_CALLBACK (on_run_to_end),
892 g_signal_connect (get_action_assert (xml,"windows_minimise_all"),
894 G_CALLBACK (psppire_window_minimise_all), NULL);
901 GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
903 merge_help_menu (uim);
905 PSPPIRE_WINDOW (window)->menu =
906 GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
909 g_object_unref (xml);
917 psppire_syntax_window_new (const char *encoding)
919 return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
920 "description", _("Syntax Editor"),
921 "encoding", encoding,
926 error_dialog (GtkWindow *w, const gchar *filename, GError *err)
928 gchar *fn = g_filename_display_basename (filename);
931 gtk_message_dialog_new (w,
932 GTK_DIALOG_DESTROY_WITH_PARENT,
935 _("Cannot load syntax file `%s'"),
940 g_object_set (dialog, "icon-name", "pspp", NULL);
942 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
945 gtk_dialog_run (GTK_DIALOG (dialog));
947 gtk_widget_destroy (dialog);
951 Loads the buffer from the file called FILENAME
954 syntax_load (PsppireWindow *window, const gchar *filename)
957 gchar *text_locale = NULL;
958 gchar *text_utf8 = NULL;
959 gsize len_locale = -1;
962 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
963 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
967 /* FIXME: What if it's a very big file ? */
968 if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
970 error_dialog (GTK_WINDOW (window), filename, err);
971 g_clear_error (&err);
975 /* Determine the file's encoding and update sw->encoding. (The ordering is
976 important here because encoding_guess_whole_file() often returns its
977 argument instead of a copy of it.) */
978 encoding = g_strdup (encoding_guess_whole_file (sw->encoding, text_locale,
980 g_free (sw->encoding);
981 sw->encoding = encoding;
983 text_utf8 = recode_substring_pool ("UTF-8", encoding,
984 ss_buffer (text_locale, len_locale),
988 if ( text_utf8 == NULL )
990 error_dialog (GTK_WINDOW (window), filename, err);
991 g_clear_error (&err);
995 gtk_text_buffer_get_iter_at_line (buffer, &iter, 0);
997 gtk_text_buffer_insert (buffer, &iter, text_utf8, len_utf8);
999 gtk_text_buffer_set_modified (buffer, FALSE);
1003 mime_type = xasprintf ("text/x-spss-syntax; charset=%s", sw->encoding);
1004 add_most_recent (filename, mime_type);
1013 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
1015 iface->save = syntax_save;
1016 iface->pick_filename = syntax_pick_filename;
1017 iface->load = syntax_load;
1024 undo_redo_update (PsppireSyntaxWindow *window)
1026 gtk_action_set_sensitive (window->undo_menuitem,
1027 gtk_source_buffer_can_undo (window->buffer));
1029 gtk_action_set_sensitive (window->redo_menuitem,
1030 gtk_source_buffer_can_redo (window->buffer));
1034 undo_last_edit (PsppireSyntaxWindow *window)
1036 gtk_source_buffer_undo (window->buffer);
1037 undo_redo_update (window);
1041 redo_last_edit (PsppireSyntaxWindow *window)
1043 gtk_source_buffer_redo (window->buffer);
1044 undo_redo_update (window);
1049 /* Printing related stuff */
1053 begin_print (GtkPrintOperation *operation,
1054 GtkPrintContext *context,
1055 PsppireSyntaxWindow *window)
1057 window->compositor =
1058 gtk_source_print_compositor_new (window->buffer);
1063 end_print (GtkPrintOperation *operation,
1064 GtkPrintContext *context,
1065 PsppireSyntaxWindow *window)
1067 g_object_unref (window->compositor);
1068 window->compositor = NULL;
1074 paginate (GtkPrintOperation *operation,
1075 GtkPrintContext *context,
1076 PsppireSyntaxWindow *window)
1078 if (gtk_source_print_compositor_paginate (window->compositor, context))
1080 gint n_pages = gtk_source_print_compositor_get_n_pages (window->compositor);
1081 gtk_print_operation_set_n_pages (operation, n_pages);
1090 draw_page (GtkPrintOperation *operation,
1091 GtkPrintContext *context,
1093 PsppireSyntaxWindow *window)
1095 gtk_source_print_compositor_draw_page (window->compositor,
1103 psppire_syntax_window_print (PsppireSyntaxWindow *window)
1105 GtkPrintOperationResult res;
1107 GtkPrintOperation *print = gtk_print_operation_new ();
1109 if (window->print_settings != NULL)
1110 gtk_print_operation_set_print_settings (print, window->print_settings);
1113 g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
1114 g_signal_connect (print, "end_print", G_CALLBACK (end_print), window);
1115 g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), window);
1116 g_signal_connect (print, "paginate", G_CALLBACK (paginate), window);
1118 res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1119 GTK_WINDOW (window), NULL);
1121 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
1123 if (window->print_settings != NULL)
1124 g_object_unref (window->print_settings);
1125 window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
1128 g_object_unref (print);