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"
37 #define _(msgid) gettext (msgid)
38 #define N_(msgid) msgid
40 static void psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *, gpointer);
41 static void psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class);
42 static void psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class);
43 static void psppire_syntax_window_init (PsppireSyntaxWindow *syntax_editor);
46 static void psppire_syntax_window_iface_init (PsppireWindowIface *iface);
50 psppire_syntax_window_get_type (void)
52 static GType psppire_syntax_window_type = 0;
54 if (!psppire_syntax_window_type)
56 static const GTypeInfo psppire_syntax_window_info =
58 sizeof (PsppireSyntaxWindowClass),
59 (GBaseInitFunc) psppire_syntax_window_base_init,
60 (GBaseFinalizeFunc) psppire_syntax_window_base_finalize,
61 (GClassInitFunc)psppire_syntax_window_class_init,
62 (GClassFinalizeFunc) NULL,
64 sizeof (PsppireSyntaxWindow),
66 (GInstanceInitFunc) psppire_syntax_window_init,
69 static const GInterfaceInfo window_interface_info =
71 (GInterfaceInitFunc) psppire_syntax_window_iface_init,
76 psppire_syntax_window_type =
77 g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireSyntaxWindow",
78 &psppire_syntax_window_info, 0);
80 g_type_add_interface_static (psppire_syntax_window_type,
81 PSPPIRE_TYPE_WINDOW_MODEL,
82 &window_interface_info);
85 return psppire_syntax_window_type;
88 static GObjectClass *parent_class ;
91 psppire_syntax_window_finalize (GObject *object)
93 if (G_OBJECT_CLASS (parent_class)->finalize)
94 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
99 psppire_syntax_window_dispose (GObject *obj)
101 PsppireSyntaxWindow *sw = (PsppireSyntaxWindow *)obj;
103 GtkClipboard *clip_selection;
104 GtkClipboard *clip_primary;
106 if (sw->dispose_has_run)
109 clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
110 clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_PRIMARY);
112 g_signal_handler_disconnect (clip_primary, sw->sel_handler);
114 g_signal_handler_disconnect (clip_selection, sw->ps_handler);
116 /* Make sure dispose does not run twice. */
117 sw->dispose_has_run = TRUE;
119 /* Chain up to the parent class */
120 G_OBJECT_CLASS (parent_class)->dispose (obj);
126 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
128 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
130 parent_class = g_type_class_peek_parent (class);
132 gobject_class->dispose = psppire_syntax_window_dispose;
137 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
139 GObjectClass *object_class = G_OBJECT_CLASS (class);
141 object_class->finalize = psppire_syntax_window_finalize;
147 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
154 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
157 PsppireWindow *win = PSPPIRE_WINDOW (sw);
158 struct lex_reader *reader;
161 text = gtk_text_buffer_get_text (sw->buffer, &start, &stop, FALSE);
162 reader = lex_reader_for_string (text);
165 lex_reader_set_file_name (reader, psppire_window_get_filename (win));
167 execute_syntax (reader);
173 /* Delete the currently selected text */
175 on_edit_delete (PsppireSyntaxWindow *sw)
177 GtkTextIter begin, end;
179 if ( gtk_text_buffer_get_selection_bounds (sw->buffer, &begin, &end) )
180 gtk_text_buffer_delete (sw->buffer, &begin, &end);
186 /* The syntax editor's clipboard deals only with text */
194 selection_changed (PsppireSyntaxWindow *sw)
196 gboolean sel = gtk_text_buffer_get_has_selection (sw->buffer);
198 gtk_action_set_sensitive (sw->edit_copy, sel);
199 gtk_action_set_sensitive (sw->edit_cut, sel);
200 gtk_action_set_sensitive (sw->edit_delete, sel);
203 /* The callback which runs when something request clipboard data */
205 clipboard_get_cb (GtkClipboard *clipboard,
206 GtkSelectionData *selection_data,
210 PsppireSyntaxWindow *sw = data;
211 g_assert (info == SELECT_FMT_TEXT);
213 gtk_selection_data_set (selection_data, selection_data->target,
215 (const guchar *) sw->cliptext, strlen (sw->cliptext));
220 clipboard_clear_cb (GtkClipboard *clipboard,
223 PsppireSyntaxWindow *sw = data;
224 g_free (sw->cliptext);
229 static const GtkTargetEntry targets[] = {
230 { "UTF8_STRING", 0, SELECT_FMT_TEXT },
231 { "STRING", 0, SELECT_FMT_TEXT },
232 { "TEXT", 0, SELECT_FMT_TEXT },
233 { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
234 { "text/plain;charset=utf-8", 0, SELECT_FMT_TEXT },
235 { "text/plain", 0, SELECT_FMT_TEXT },
240 Store a clip containing the currently selected text.
241 Returns true iff something was set.
242 As a side effect, begin and end will be set to indicate
243 the limits of the selected text.
246 set_clip (PsppireSyntaxWindow *sw, GtkTextIter *begin, GtkTextIter *end)
248 GtkClipboard *clipboard ;
250 if ( ! gtk_text_buffer_get_selection_bounds (sw->buffer, begin, end) )
253 g_free (sw->cliptext);
254 sw->cliptext = gtk_text_buffer_get_text (sw->buffer, begin, end, FALSE);
257 gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
259 if (!gtk_clipboard_set_with_owner (clipboard, targets,
260 G_N_ELEMENTS (targets),
261 clipboard_get_cb, clipboard_clear_cb,
263 clipboard_clear_cb (clipboard, sw);
269 on_edit_cut (PsppireSyntaxWindow *sw)
271 GtkTextIter begin, end;
273 if ( set_clip (sw, &begin, &end))
274 gtk_text_buffer_delete (sw->buffer, &begin, &end);
278 on_edit_copy (PsppireSyntaxWindow *sw)
280 GtkTextIter begin, end;
282 set_clip (sw, &begin, &end);
286 /* A callback for when the clipboard contents have been received */
288 contents_received_callback (GtkClipboard *clipboard,
289 GtkSelectionData *sd,
293 PsppireSyntaxWindow *syntax_window = data;
295 if ( sd->length < 0 )
298 if ( sd->type != gdk_atom_intern ("UTF8_STRING", FALSE))
301 c = (gchar *) sd->data;
303 gtk_text_buffer_insert_at_cursor (syntax_window->buffer,
310 on_edit_paste (PsppireSyntaxWindow *sw)
312 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (sw));
313 GtkClipboard *clipboard =
314 gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
316 gtk_clipboard_request_contents (clipboard,
317 gdk_atom_intern ("UTF8_STRING", TRUE),
318 contents_received_callback,
323 /* Check to see if CLIP holds a target which we know how to paste,
324 and set the sensitivity of the Paste action accordingly.
327 set_paste_sensitivity (GtkClipboard *clip, GdkEventOwnerChange *event, gpointer data)
330 gboolean compatible_target = FALSE;
331 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (data);
333 for (i = 0 ; i < sizeof (targets) / sizeof (targets[0]) ; ++i)
335 GdkAtom atom = gdk_atom_intern (targets[i].target, TRUE);
336 if ( gtk_clipboard_wait_is_target_available (clip, atom))
338 compatible_target = TRUE;
343 gtk_action_set_sensitive (sw->edit_paste, compatible_target);
349 /* Parse and execute all the text in the buffer */
351 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
353 GtkTextIter begin, end;
354 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
356 gtk_text_buffer_get_iter_at_offset (se->buffer, &begin, 0);
357 gtk_text_buffer_get_iter_at_offset (se->buffer, &end, -1);
359 editor_execute_syntax (se, begin, end);
362 /* Parse and execute the currently selected text */
364 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
366 GtkTextIter begin, end;
367 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
369 if ( gtk_text_buffer_get_selection_bounds (se->buffer, &begin, &end) )
370 editor_execute_syntax (se, begin, end);
374 /* Parse and execute the from the current line, to the end of the
377 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
379 GtkTextIter begin, end;
383 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
385 /* Get the current line */
386 gtk_text_buffer_get_iter_at_mark (se->buffer,
388 gtk_text_buffer_get_insert (se->buffer)
391 line = gtk_text_iter_get_line (&here) ;
393 /* Now set begin and end to the start of this line, and end of buffer
395 gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
396 gtk_text_buffer_get_iter_at_line (se->buffer, &end, -1);
398 editor_execute_syntax (se, begin, end);
403 /* Parse and execute the current line */
405 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
407 GtkTextIter begin, end;
411 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
413 /* Get the current line */
414 gtk_text_buffer_get_iter_at_mark (se->buffer,
416 gtk_text_buffer_get_insert (se->buffer)
419 line = gtk_text_iter_get_line (&here) ;
421 /* Now set begin and end to the start of this line, and start of
422 following line respectively */
423 gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
424 gtk_text_buffer_get_iter_at_line (se->buffer, &end, line + 1);
426 editor_execute_syntax (se, begin, end);
431 /* Append ".sps" to FILENAME if necessary.
432 The returned result must be freed when no longer required.
435 append_suffix (const gchar *filename)
437 if ( ! g_str_has_suffix (filename, ".sps" ) &&
438 ! g_str_has_suffix (filename, ".SPS" ) )
440 return g_strdup_printf ("%s.sps", filename);
443 return xstrdup (filename);
447 Save BUFFER to the file called FILENAME.
448 FILENAME must be encoded in Glib filename encoding.
449 If successful, clears the buffer's modified flag.
452 save_editor_to_file (PsppireSyntaxWindow *se,
453 const gchar *filename,
456 GtkTextBuffer *buffer = se->buffer;
458 GtkTextIter start, stop;
464 suffixedname = append_suffix (filename);
466 gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
467 gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
469 text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
471 result = g_file_set_contents (suffixedname, text, -1, err);
473 g_free (suffixedname);
477 char *fn = g_filename_display_name (filename);
478 gchar *msg = g_strdup_printf (_("Saved file `%s'"), fn);
480 gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
481 gtk_text_buffer_set_modified (buffer, FALSE);
489 /* Callback for the File->SaveAs menuitem */
491 syntax_save_as (PsppireWindow *se)
493 GtkFileFilter *filter;
497 gtk_file_chooser_dialog_new (_("Save Syntax"),
499 GTK_FILE_CHOOSER_ACTION_SAVE,
500 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
501 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
504 filter = gtk_file_filter_new ();
505 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
506 gtk_file_filter_add_pattern (filter, "*.sps");
507 gtk_file_filter_add_pattern (filter, "*.SPS");
508 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
510 filter = gtk_file_filter_new ();
511 gtk_file_filter_set_name (filter, _("All Files"));
512 gtk_file_filter_add_pattern (filter, "*");
513 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
515 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
517 response = gtk_dialog_run (GTK_DIALOG (dialog));
519 if ( response == GTK_RESPONSE_ACCEPT )
523 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
525 if ( ! save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err) )
527 msg ( ME, "%s", err->message );
534 gtk_widget_destroy (dialog);
538 /* Callback for the File->Save menuitem */
540 syntax_save (PsppireWindow *se)
542 const gchar *filename = psppire_window_get_filename (se);
544 if ( filename == NULL )
549 save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
552 msg (ME, "%s", err->message);
559 /* Callback for the File->Quit menuitem */
561 on_quit (GtkMenuItem *menuitem, gpointer user_data)
570 create_syntax_window (void)
572 GtkWidget *w = psppire_syntax_window_new ();
577 open_syntax_window (const char *file_name)
579 GtkWidget *se = psppire_syntax_window_new ();
581 if ( psppire_window_load (PSPPIRE_WINDOW (se), file_name) )
582 gtk_widget_show (se);
584 gtk_widget_destroy (se);
588 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
590 gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
594 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
596 if (gtk_text_buffer_get_modified (buffer))
597 psppire_window_set_unsaved (window);
601 psppire_syntax_window_init (PsppireSyntaxWindow *window)
603 GtkBuilder *xml = builder_new ("syntax-editor.ui");
604 GtkWidget *box = gtk_vbox_new (FALSE, 0);
606 GtkWidget *menubar = get_widget_assert (xml, "menubar");
607 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
610 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
612 GtkClipboard *clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
613 GtkClipboard *clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
615 window->cliptext = NULL;
616 window->dispose_has_run = FALSE;
618 window->edit_delete = get_action_assert (xml, "edit_delete");
619 window->edit_copy = get_action_assert (xml, "edit_copy");
620 window->edit_cut = get_action_assert (xml, "edit_cut");
621 window->edit_paste = get_action_assert (xml, "edit_paste");
623 window->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
625 window->sb = get_widget_assert (xml, "statusbar2");
626 window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
628 g_signal_connect (window->buffer, "changed",
629 G_CALLBACK (on_text_changed), window);
631 g_signal_connect (window->buffer, "modified-changed",
632 G_CALLBACK (on_modified_changed), window);
634 window->sel_handler = g_signal_connect_swapped (clip_primary, "owner-change",
635 G_CALLBACK (selection_changed), window);
637 window->ps_handler = g_signal_connect (clip_selection, "owner-change",
638 G_CALLBACK (set_paste_sensitivity), window);
642 gtk_container_add (GTK_CONTAINER (window), box);
644 g_object_ref (menubar);
648 g_object_ref (window->sb);
650 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
651 gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
652 gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
654 gtk_widget_show_all (box);
656 g_signal_connect_swapped (get_action_assert (xml,"file_new_syntax"), "activate", G_CALLBACK (create_syntax_window), NULL);
659 g_signal_connect (get_action_assert (xml,"file_new_data"),
661 G_CALLBACK (create_data_window),
665 g_signal_connect_swapped (get_action_assert (xml, "file_save"),
667 G_CALLBACK (syntax_save),
670 g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
672 G_CALLBACK (syntax_save_as),
675 g_signal_connect (get_action_assert (xml,"file_quit"),
677 G_CALLBACK (on_quit),
680 g_signal_connect_swapped (window->edit_delete,
682 G_CALLBACK (on_edit_delete),
685 g_signal_connect_swapped (window->edit_copy,
687 G_CALLBACK (on_edit_copy),
690 g_signal_connect_swapped (window->edit_cut,
692 G_CALLBACK (on_edit_cut),
695 g_signal_connect_swapped (window->edit_paste,
697 G_CALLBACK (on_edit_paste),
700 g_signal_connect (get_action_assert (xml,"run_all"),
702 G_CALLBACK (on_run_all),
705 g_signal_connect (get_action_assert (xml,"run_selection"),
707 G_CALLBACK (on_run_selection),
710 g_signal_connect (get_action_assert (xml,"run_current_line"),
712 G_CALLBACK (on_run_current_line),
715 g_signal_connect (get_action_assert (xml,"run_to_end"),
717 G_CALLBACK (on_run_to_end),
720 g_signal_connect (get_action_assert (xml,"windows_minimise_all"),
722 G_CALLBACK (psppire_window_minimise_all), NULL);
726 GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
728 merge_help_menu (uim);
730 PSPPIRE_WINDOW (window)->menu =
731 GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
734 g_object_unref (xml);
742 psppire_syntax_window_new (void)
744 return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
745 /* TRANSLATORS: This will form a filename. Please avoid whitespace. */
746 "filename", _("Syntax"),
747 "description", _("Syntax Editor"),
752 error_dialog (GtkWindow *w, const gchar *filename, GError *err)
754 gchar *fn = g_filename_display_basename (filename);
757 gtk_message_dialog_new (w,
758 GTK_DIALOG_DESTROY_WITH_PARENT,
761 _("Cannot load syntax file `%s'"),
766 g_object_set (dialog, "icon-name", "psppicon", NULL);
768 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
771 gtk_dialog_run (GTK_DIALOG (dialog));
773 gtk_widget_destroy (dialog);
777 Loads the buffer from the file called FILENAME
780 syntax_load (PsppireWindow *window, const gchar *filename)
783 gchar *text_locale = NULL;
784 gchar *text_utf8 = NULL;
785 gsize len_locale = -1;
788 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
790 /* FIXME: What if it's a very big file ? */
791 if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
793 error_dialog (GTK_WINDOW (window), filename, err);
794 g_clear_error (&err);
798 text_utf8 = g_locale_to_utf8 (text_locale, len_locale, NULL, &len_utf8, &err);
802 if ( text_utf8 == NULL )
804 error_dialog (GTK_WINDOW (window), filename, err);
805 g_clear_error (&err);
809 gtk_text_buffer_get_iter_at_line (sw->buffer, &iter, 0);
811 gtk_text_buffer_insert (sw->buffer, &iter, text_utf8, len_utf8);
813 gtk_text_buffer_set_modified (sw->buffer, FALSE);
823 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
825 iface->save = syntax_save;
826 iface->load = syntax_load;