1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2009, 2010 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/>. */
19 #include <gtk/gtksignal.h>
20 #include <gtk/gtkbox.h>
24 #include <language/lexer/lexer.h>
25 #include <libpspp/message.h>
28 #include "help-menu.h"
30 #include "psppire-data-window.h"
31 #include "psppire-window-register.h"
32 #include "psppire-syntax-window.h"
33 #include "syntax-editor-source.h"
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) msgid
41 static void psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *, gpointer);
42 static void psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class);
43 static void psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class);
44 static void psppire_syntax_window_init (PsppireSyntaxWindow *syntax_editor);
47 static void psppire_syntax_window_iface_init (PsppireWindowIface *iface);
51 psppire_syntax_window_get_type (void)
53 static GType psppire_syntax_window_type = 0;
55 if (!psppire_syntax_window_type)
57 static const GTypeInfo psppire_syntax_window_info =
59 sizeof (PsppireSyntaxWindowClass),
60 (GBaseInitFunc) psppire_syntax_window_base_init,
61 (GBaseFinalizeFunc) psppire_syntax_window_base_finalize,
62 (GClassInitFunc)psppire_syntax_window_class_init,
63 (GClassFinalizeFunc) NULL,
65 sizeof (PsppireSyntaxWindow),
67 (GInstanceInitFunc) psppire_syntax_window_init,
70 static const GInterfaceInfo window_interface_info =
72 (GInterfaceInitFunc) psppire_syntax_window_iface_init,
77 psppire_syntax_window_type =
78 g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireSyntaxWindow",
79 &psppire_syntax_window_info, 0);
81 g_type_add_interface_static (psppire_syntax_window_type,
82 PSPPIRE_TYPE_WINDOW_MODEL,
83 &window_interface_info);
86 return psppire_syntax_window_type;
89 static GObjectClass *parent_class ;
92 psppire_syntax_window_finalize (GObject *object)
94 if (G_OBJECT_CLASS (parent_class)->finalize)
95 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
100 psppire_syntax_window_dispose (GObject *obj)
102 PsppireSyntaxWindow *sw = (PsppireSyntaxWindow *)obj;
104 GtkClipboard *clip_selection;
105 GtkClipboard *clip_primary;
107 if (sw->dispose_has_run)
110 clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
111 clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_PRIMARY);
113 g_signal_handler_disconnect (clip_primary, sw->sel_handler);
115 g_signal_handler_disconnect (clip_selection, sw->ps_handler);
117 /* Make sure dispose does not run twice. */
118 sw->dispose_has_run = TRUE;
120 /* Chain up to the parent class */
121 G_OBJECT_CLASS (parent_class)->dispose (obj);
127 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
129 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
131 parent_class = g_type_class_peek_parent (class);
133 gobject_class->dispose = psppire_syntax_window_dispose;
138 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
140 GObjectClass *object_class = G_OBJECT_CLASS (class);
142 object_class->finalize = psppire_syntax_window_finalize;
148 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
155 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
158 PsppireWindow *win = PSPPIRE_WINDOW (sw);
159 const gchar *name = psppire_window_get_filename (win);
160 execute_syntax (create_syntax_editor_source (sw->buffer, start, stop, name));
166 /* Delete the currently selected text */
168 on_edit_delete (PsppireSyntaxWindow *sw)
170 GtkTextIter begin, end;
172 if ( gtk_text_buffer_get_selection_bounds (sw->buffer, &begin, &end) )
173 gtk_text_buffer_delete (sw->buffer, &begin, &end);
179 /* The syntax editor's clipboard deals only with text */
187 selection_changed (PsppireSyntaxWindow *sw)
189 gboolean sel = gtk_text_buffer_get_has_selection (sw->buffer);
191 gtk_action_set_sensitive (sw->edit_copy, sel);
192 gtk_action_set_sensitive (sw->edit_cut, sel);
193 gtk_action_set_sensitive (sw->edit_delete, sel);
196 /* The callback which runs when something request clipboard data */
198 clipboard_get_cb (GtkClipboard *clipboard,
199 GtkSelectionData *selection_data,
203 PsppireSyntaxWindow *sw = data;
204 g_assert (info == SELECT_FMT_TEXT);
206 gtk_selection_data_set (selection_data, selection_data->target,
208 (const guchar *) sw->cliptext, strlen (sw->cliptext));
213 clipboard_clear_cb (GtkClipboard *clipboard,
216 PsppireSyntaxWindow *sw = data;
217 g_free (sw->cliptext);
222 static const GtkTargetEntry targets[] = {
223 { "UTF8_STRING", 0, SELECT_FMT_TEXT },
224 { "STRING", 0, SELECT_FMT_TEXT },
225 { "TEXT", 0, SELECT_FMT_TEXT },
226 { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
227 { "text/plain;charset=utf-8", 0, SELECT_FMT_TEXT },
228 { "text/plain", 0, SELECT_FMT_TEXT },
233 Store a clip containing the currently selected text.
234 Returns true iff something was set.
235 As a side effect, begin and end will be set to indicate
236 the limits of the selected text.
239 set_clip (PsppireSyntaxWindow *sw, GtkTextIter *begin, GtkTextIter *end)
241 GtkClipboard *clipboard ;
243 if ( ! gtk_text_buffer_get_selection_bounds (sw->buffer, begin, end) )
246 g_free (sw->cliptext);
247 sw->cliptext = gtk_text_buffer_get_text (sw->buffer, begin, end, FALSE);
250 gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
252 if (!gtk_clipboard_set_with_owner (clipboard, targets,
253 G_N_ELEMENTS (targets),
254 clipboard_get_cb, clipboard_clear_cb,
256 clipboard_clear_cb (clipboard, sw);
262 on_edit_cut (PsppireSyntaxWindow *sw)
264 GtkTextIter begin, end;
266 if ( set_clip (sw, &begin, &end))
267 gtk_text_buffer_delete (sw->buffer, &begin, &end);
271 on_edit_copy (PsppireSyntaxWindow *sw)
273 GtkTextIter begin, end;
275 set_clip (sw, &begin, &end);
279 /* A callback for when the clipboard contents have been received */
281 contents_received_callback (GtkClipboard *clipboard,
282 GtkSelectionData *sd,
286 PsppireSyntaxWindow *syntax_window = data;
288 if ( sd->length < 0 )
291 if ( sd->type != gdk_atom_intern ("UTF8_STRING", FALSE))
294 c = (gchar *) sd->data;
296 gtk_text_buffer_insert_at_cursor (syntax_window->buffer,
303 on_edit_paste (PsppireSyntaxWindow *sw)
305 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (sw));
306 GtkClipboard *clipboard =
307 gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
309 gtk_clipboard_request_contents (clipboard,
310 gdk_atom_intern ("UTF8_STRING", TRUE),
311 contents_received_callback,
316 /* Check to see if CLIP holds a target which we know how to paste,
317 and set the sensitivity of the Paste action accordingly.
320 set_paste_sensitivity (GtkClipboard *clip, GdkEventOwnerChange *event, gpointer data)
323 gboolean compatible_target = FALSE;
324 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (data);
326 for (i = 0 ; i < sizeof (targets) / sizeof (targets[0]) ; ++i)
328 GdkAtom atom = gdk_atom_intern (targets[i].target, TRUE);
329 if ( gtk_clipboard_wait_is_target_available (clip, atom))
331 compatible_target = TRUE;
336 gtk_action_set_sensitive (sw->edit_paste, compatible_target);
342 /* Parse and execute all the text in the buffer */
344 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
346 GtkTextIter begin, end;
347 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
349 gtk_text_buffer_get_iter_at_offset (se->buffer, &begin, 0);
350 gtk_text_buffer_get_iter_at_offset (se->buffer, &end, -1);
352 editor_execute_syntax (se, begin, end);
355 /* Parse and execute the currently selected text */
357 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
359 GtkTextIter begin, end;
360 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
362 if ( gtk_text_buffer_get_selection_bounds (se->buffer, &begin, &end) )
363 editor_execute_syntax (se, begin, end);
367 /* Parse and execute the from the current line, to the end of the
370 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
372 GtkTextIter begin, end;
376 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
378 /* Get the current line */
379 gtk_text_buffer_get_iter_at_mark (se->buffer,
381 gtk_text_buffer_get_insert (se->buffer)
384 line = gtk_text_iter_get_line (&here) ;
386 /* Now set begin and end to the start of this line, and end of buffer
388 gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
389 gtk_text_buffer_get_iter_at_line (se->buffer, &end, -1);
391 editor_execute_syntax (se, begin, end);
396 /* Parse and execute the current line */
398 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
400 GtkTextIter begin, end;
404 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
406 /* Get the current line */
407 gtk_text_buffer_get_iter_at_mark (se->buffer,
409 gtk_text_buffer_get_insert (se->buffer)
412 line = gtk_text_iter_get_line (&here) ;
414 /* Now set begin and end to the start of this line, and start of
415 following line respectively */
416 gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
417 gtk_text_buffer_get_iter_at_line (se->buffer, &end, line + 1);
419 editor_execute_syntax (se, begin, end);
424 /* Append ".sps" to FILENAME if necessary.
425 The returned result must be freed when no longer required.
428 append_suffix (const gchar *filename)
430 if ( ! g_str_has_suffix (filename, ".sps" ) &&
431 ! g_str_has_suffix (filename, ".SPS" ) )
433 return g_strdup_printf ("%s.sps", filename);
436 return xstrdup (filename);
440 Save BUFFER to the file called FILENAME.
441 FILENAME must be encoded in Glib filename encoding.
442 If successful, clears the buffer's modified flag.
445 save_editor_to_file (PsppireSyntaxWindow *se,
446 const gchar *filename,
449 GtkTextBuffer *buffer = se->buffer;
451 GtkTextIter start, stop;
457 suffixedname = append_suffix (filename);
459 gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
460 gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
462 text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
464 result = g_file_set_contents (suffixedname, text, -1, err);
466 g_free (suffixedname);
470 char *fn = g_filename_display_name (filename);
471 gchar *msg = g_strdup_printf (_("Saved file `%s'"), fn);
473 gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
474 gtk_text_buffer_set_modified (buffer, FALSE);
482 /* Callback for the File->SaveAs menuitem */
484 syntax_save_as (PsppireWindow *se)
486 GtkFileFilter *filter;
490 gtk_file_chooser_dialog_new (_("Save Syntax"),
492 GTK_FILE_CHOOSER_ACTION_SAVE,
493 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
494 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
497 filter = gtk_file_filter_new ();
498 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
499 gtk_file_filter_add_pattern (filter, "*.sps");
500 gtk_file_filter_add_pattern (filter, "*.SPS");
501 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
503 filter = gtk_file_filter_new ();
504 gtk_file_filter_set_name (filter, _("All Files"));
505 gtk_file_filter_add_pattern (filter, "*");
506 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
508 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
510 response = gtk_dialog_run (GTK_DIALOG (dialog));
512 if ( response == GTK_RESPONSE_ACCEPT )
516 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
518 if ( ! save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err) )
520 msg ( ME, "%s", err->message );
527 gtk_widget_destroy (dialog);
531 /* Callback for the File->Save menuitem */
533 syntax_save (PsppireWindow *se)
535 const gchar *filename = psppire_window_get_filename (se);
537 if ( filename == NULL )
542 save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
545 msg (ME, "%s", err->message);
552 /* Callback for the File->Quit menuitem */
554 on_quit (GtkMenuItem *menuitem, gpointer user_data)
563 create_syntax_window (void)
565 GtkWidget *w = psppire_syntax_window_new ();
570 open_syntax_window (const char *file_name)
572 GtkWidget *se = psppire_syntax_window_new ();
574 if ( psppire_window_load (PSPPIRE_WINDOW (se), file_name) )
575 gtk_widget_show (se);
577 gtk_widget_destroy (se);
581 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
583 gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
587 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
589 if (gtk_text_buffer_get_modified (buffer))
590 psppire_window_set_unsaved (window);
593 extern struct source_stream *the_source_stream ;
596 psppire_syntax_window_init (PsppireSyntaxWindow *window)
598 GtkBuilder *xml = builder_new ("syntax-editor.ui");
599 GtkWidget *box = gtk_vbox_new (FALSE, 0);
601 GtkWidget *menubar = get_widget_assert (xml, "menubar");
602 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
605 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
607 GtkClipboard *clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
608 GtkClipboard *clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
610 window->cliptext = NULL;
611 window->dispose_has_run = FALSE;
613 window->edit_delete = get_action_assert (xml, "edit_delete");
614 window->edit_copy = get_action_assert (xml, "edit_copy");
615 window->edit_cut = get_action_assert (xml, "edit_cut");
616 window->edit_paste = get_action_assert (xml, "edit_paste");
618 window->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
619 window->lexer = lex_create (the_source_stream);
621 window->sb = get_widget_assert (xml, "statusbar2");
622 window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
624 g_signal_connect (window->buffer, "changed",
625 G_CALLBACK (on_text_changed), window);
627 g_signal_connect (window->buffer, "modified-changed",
628 G_CALLBACK (on_modified_changed), window);
630 window->sel_handler = g_signal_connect_swapped (clip_primary, "owner-change",
631 G_CALLBACK (selection_changed), window);
633 window->ps_handler = g_signal_connect (clip_selection, "owner-change",
634 G_CALLBACK (set_paste_sensitivity), window);
638 gtk_container_add (GTK_CONTAINER (window), box);
640 g_object_ref (menubar);
644 g_object_ref (window->sb);
646 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
647 gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
648 gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
650 gtk_widget_show_all (box);
652 g_signal_connect_swapped (get_action_assert (xml,"file_new_syntax"), "activate", G_CALLBACK (create_syntax_window), NULL);
655 g_signal_connect (get_action_assert (xml,"file_new_data"),
657 G_CALLBACK (create_data_window),
661 g_signal_connect_swapped (get_action_assert (xml, "file_save"),
663 G_CALLBACK (syntax_save),
666 g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
668 G_CALLBACK (syntax_save_as),
671 g_signal_connect (get_action_assert (xml,"file_quit"),
673 G_CALLBACK (on_quit),
676 g_signal_connect_swapped (window->edit_delete,
678 G_CALLBACK (on_edit_delete),
681 g_signal_connect_swapped (window->edit_copy,
683 G_CALLBACK (on_edit_copy),
686 g_signal_connect_swapped (window->edit_cut,
688 G_CALLBACK (on_edit_cut),
691 g_signal_connect_swapped (window->edit_paste,
693 G_CALLBACK (on_edit_paste),
696 g_signal_connect (get_action_assert (xml,"run_all"),
698 G_CALLBACK (on_run_all),
701 g_signal_connect (get_action_assert (xml,"run_selection"),
703 G_CALLBACK (on_run_selection),
706 g_signal_connect (get_action_assert (xml,"run_current_line"),
708 G_CALLBACK (on_run_current_line),
711 g_signal_connect (get_action_assert (xml,"run_to_end"),
713 G_CALLBACK (on_run_to_end),
716 g_signal_connect (get_action_assert (xml,"windows_minimise_all"),
718 G_CALLBACK (psppire_window_minimise_all), NULL);
722 GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
724 merge_help_menu (uim);
726 PSPPIRE_WINDOW (window)->menu =
727 GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
730 g_object_unref (xml);
738 psppire_syntax_window_new (void)
740 return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
741 /* TRANSLATORS: This will form a filename. Please avoid whitespace. */
742 "filename", _("Syntax"),
743 "description", _("Syntax Editor"),
748 error_dialog (GtkWindow *w, const gchar *filename, GError *err)
750 gchar *fn = g_filename_display_basename (filename);
753 gtk_message_dialog_new (w,
754 GTK_DIALOG_DESTROY_WITH_PARENT,
757 _("Cannot load syntax file `%s'"),
762 g_object_set (dialog, "icon-name", "psppicon", NULL);
764 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
767 gtk_dialog_run (GTK_DIALOG (dialog));
769 gtk_widget_destroy (dialog);
773 Loads the buffer from the file called FILENAME
776 syntax_load (PsppireWindow *window, const gchar *filename)
779 gchar *text_locale = NULL;
780 gchar *text_utf8 = NULL;
781 gsize len_locale = -1;
784 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
786 /* FIXME: What if it's a very big file ? */
787 if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
789 error_dialog (GTK_WINDOW (window), filename, err);
790 g_clear_error (&err);
794 text_utf8 = g_locale_to_utf8 (text_locale, len_locale, NULL, &len_utf8, &err);
798 if ( text_utf8 == NULL )
800 error_dialog (GTK_WINDOW (window), filename, err);
801 g_clear_error (&err);
805 gtk_text_buffer_get_iter_at_line (sw->buffer, &iter, 0);
807 gtk_text_buffer_insert (sw->buffer, &iter, text_utf8, len_utf8);
809 gtk_text_buffer_set_modified (sw->buffer, FALSE);
819 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
821 iface->save = syntax_save;
822 iface->load = syntax_load;