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 <libpspp/message.h>
28 #include "psppire-syntax-window.h"
30 #include "psppire-data-window.h"
31 #include "psppire-window-register.h"
33 #include "help-menu.h"
34 #include "psppire-syntax-window.h"
35 #include "syntax-editor-source.h"
36 #include <language/lexer/lexer.h>
41 #define _(msgid) gettext (msgid)
42 #define N_(msgid) msgid
44 static void psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *, gpointer);
45 static void psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class);
46 static void psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class);
47 static void psppire_syntax_window_init (PsppireSyntaxWindow *syntax_editor);
50 static void psppire_syntax_window_iface_init (PsppireWindowIface *iface);
54 psppire_syntax_window_get_type (void)
56 static GType psppire_syntax_window_type = 0;
58 if (!psppire_syntax_window_type)
60 static const GTypeInfo psppire_syntax_window_info =
62 sizeof (PsppireSyntaxWindowClass),
63 (GBaseInitFunc) psppire_syntax_window_base_init,
64 (GBaseFinalizeFunc) psppire_syntax_window_base_finalize,
65 (GClassInitFunc)psppire_syntax_window_class_init,
66 (GClassFinalizeFunc) NULL,
68 sizeof (PsppireSyntaxWindow),
70 (GInstanceInitFunc) psppire_syntax_window_init,
73 static const GInterfaceInfo window_interface_info =
75 (GInterfaceInitFunc) psppire_syntax_window_iface_init,
80 psppire_syntax_window_type =
81 g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireSyntaxWindow",
82 &psppire_syntax_window_info, 0);
84 g_type_add_interface_static (psppire_syntax_window_type,
85 PSPPIRE_TYPE_WINDOW_MODEL,
86 &window_interface_info);
89 return psppire_syntax_window_type;
92 static GObjectClass *parent_class ;
95 psppire_syntax_window_finalize (GObject *object)
97 if (G_OBJECT_CLASS (parent_class)->finalize)
98 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
103 psppire_syntax_window_dispose (GObject *obj)
105 PsppireSyntaxWindow *sw = (PsppireSyntaxWindow *)obj;
107 GtkClipboard *clip_selection;
108 GtkClipboard *clip_primary;
110 if (sw->dispose_has_run)
113 clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
114 clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_PRIMARY);
116 g_signal_handler_disconnect (clip_primary, sw->sel_handler);
118 g_signal_handler_disconnect (clip_selection, sw->ps_handler);
120 /* Make sure dispose does not run twice. */
121 sw->dispose_has_run = TRUE;
123 /* Chain up to the parent class */
124 G_OBJECT_CLASS (parent_class)->dispose (obj);
130 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
132 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
134 parent_class = g_type_class_peek_parent (class);
136 gobject_class->dispose = psppire_syntax_window_dispose;
141 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
143 GObjectClass *object_class = G_OBJECT_CLASS (class);
145 object_class->finalize = psppire_syntax_window_finalize;
151 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
158 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
161 PsppireWindow *win = PSPPIRE_WINDOW (sw);
162 const gchar *name = psppire_window_get_filename (win);
163 execute_syntax (create_syntax_editor_source (sw->buffer, start, stop, name));
169 /* Delete the currently selected text */
171 on_edit_delete (PsppireSyntaxWindow *sw)
173 GtkTextIter begin, end;
175 if ( gtk_text_buffer_get_selection_bounds (sw->buffer, &begin, &end) )
176 gtk_text_buffer_delete (sw->buffer, &begin, &end);
182 /* The syntax editor's clipboard deals only with text */
190 selection_changed (PsppireSyntaxWindow *sw)
192 gboolean sel = gtk_text_buffer_get_has_selection (sw->buffer);
194 gtk_action_set_sensitive (sw->edit_copy, sel);
195 gtk_action_set_sensitive (sw->edit_cut, sel);
196 gtk_action_set_sensitive (sw->edit_delete, sel);
199 /* The callback which runs when something request clipboard data */
201 clipboard_get_cb (GtkClipboard *clipboard,
202 GtkSelectionData *selection_data,
206 PsppireSyntaxWindow *sw = data;
207 g_assert (info == SELECT_FMT_TEXT);
209 gtk_selection_data_set (selection_data, selection_data->target,
211 (const guchar *) sw->cliptext, strlen (sw->cliptext));
216 clipboard_clear_cb (GtkClipboard *clipboard,
219 PsppireSyntaxWindow *sw = data;
220 g_free (sw->cliptext);
225 static const GtkTargetEntry targets[] = {
226 { "UTF8_STRING", 0, SELECT_FMT_TEXT },
227 { "STRING", 0, SELECT_FMT_TEXT },
228 { "TEXT", 0, SELECT_FMT_TEXT },
229 { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
230 { "text/plain;charset=utf-8", 0, SELECT_FMT_TEXT },
231 { "text/plain", 0, SELECT_FMT_TEXT },
236 Store a clip containing the currently selected text.
237 Returns true iff something was set.
238 As a side effect, begin and end will be set to indicate
239 the limits of the selected text.
242 set_clip (PsppireSyntaxWindow *sw, GtkTextIter *begin, GtkTextIter *end)
244 GtkClipboard *clipboard ;
246 if ( ! gtk_text_buffer_get_selection_bounds (sw->buffer, begin, end) )
249 g_free (sw->cliptext);
250 sw->cliptext = gtk_text_buffer_get_text (sw->buffer, begin, end, FALSE);
253 gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
255 if (!gtk_clipboard_set_with_owner (clipboard, targets,
256 G_N_ELEMENTS (targets),
257 clipboard_get_cb, clipboard_clear_cb,
259 clipboard_clear_cb (clipboard, sw);
265 on_edit_cut (PsppireSyntaxWindow *sw)
267 GtkTextIter begin, end;
269 if ( set_clip (sw, &begin, &end))
270 gtk_text_buffer_delete (sw->buffer, &begin, &end);
274 on_edit_copy (PsppireSyntaxWindow *sw)
276 GtkTextIter begin, end;
278 set_clip (sw, &begin, &end);
282 /* A callback for when the clipboard contents have been received */
284 contents_received_callback (GtkClipboard *clipboard,
285 GtkSelectionData *sd,
289 PsppireSyntaxWindow *syntax_window = data;
291 if ( sd->length < 0 )
294 if ( sd->type != gdk_atom_intern ("UTF8_STRING", FALSE))
297 c = (gchar *) sd->data;
299 gtk_text_buffer_insert_at_cursor (syntax_window->buffer,
306 on_edit_paste (PsppireSyntaxWindow *sw)
308 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (sw));
309 GtkClipboard *clipboard =
310 gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
312 gtk_clipboard_request_contents (clipboard,
313 gdk_atom_intern ("UTF8_STRING", TRUE),
314 contents_received_callback,
319 /* Check to see if CLIP holds a target which we know how to paste,
320 and set the sensitivity of the Paste action accordingly.
323 set_paste_sensitivity (GtkClipboard *clip, GdkEventOwnerChange *event, gpointer data)
326 gboolean compatible_target = FALSE;
327 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (data);
329 for (i = 0 ; i < sizeof (targets) / sizeof (targets[0]) ; ++i)
331 GdkAtom atom = gdk_atom_intern (targets[i].target, TRUE);
332 if ( gtk_clipboard_wait_is_target_available (clip, atom))
334 compatible_target = TRUE;
339 gtk_action_set_sensitive (sw->edit_paste, compatible_target);
345 /* Parse and execute all the text in the buffer */
347 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
349 GtkTextIter begin, end;
350 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
352 gtk_text_buffer_get_iter_at_offset (se->buffer, &begin, 0);
353 gtk_text_buffer_get_iter_at_offset (se->buffer, &end, -1);
355 editor_execute_syntax (se, begin, end);
358 /* Parse and execute the currently selected text */
360 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
362 GtkTextIter begin, end;
363 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
365 if ( gtk_text_buffer_get_selection_bounds (se->buffer, &begin, &end) )
366 editor_execute_syntax (se, begin, end);
370 /* Parse and execute the from the current line, to the end of the
373 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
375 GtkTextIter begin, end;
379 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
381 /* Get the current line */
382 gtk_text_buffer_get_iter_at_mark (se->buffer,
384 gtk_text_buffer_get_insert (se->buffer)
387 line = gtk_text_iter_get_line (&here) ;
389 /* Now set begin and end to the start of this line, and end of buffer
391 gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
392 gtk_text_buffer_get_iter_at_line (se->buffer, &end, -1);
394 editor_execute_syntax (se, begin, end);
399 /* Parse and execute the current line */
401 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
403 GtkTextIter begin, end;
407 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
409 /* Get the current line */
410 gtk_text_buffer_get_iter_at_mark (se->buffer,
412 gtk_text_buffer_get_insert (se->buffer)
415 line = gtk_text_iter_get_line (&here) ;
417 /* Now set begin and end to the start of this line, and start of
418 following line respectively */
419 gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
420 gtk_text_buffer_get_iter_at_line (se->buffer, &end, line + 1);
422 editor_execute_syntax (se, begin, end);
427 /* Append ".sps" to FILENAME if necessary.
428 The returned result must be freed when no longer required.
431 append_suffix (const gchar *filename)
433 if ( ! g_str_has_suffix (filename, ".sps" ) &&
434 ! g_str_has_suffix (filename, ".SPS" ) )
436 return g_strdup_printf ("%s.sps", filename);
439 return xstrdup (filename);
443 Save BUFFER to the file called FILENAME.
444 FILENAME must be encoded in Glib filename encoding.
445 If successful, clears the buffer's modified flag.
448 save_editor_to_file (PsppireSyntaxWindow *se,
449 const gchar *filename,
452 GtkTextBuffer *buffer = se->buffer;
454 GtkTextIter start, stop;
460 suffixedname = append_suffix (filename);
462 gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
463 gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
465 text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
467 result = g_file_set_contents (suffixedname, text, -1, err);
469 g_free (suffixedname);
473 char *fn = g_filename_display_name (filename);
474 gchar *msg = g_strdup_printf (_("Saved file `%s'"), fn);
476 gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
477 gtk_text_buffer_set_modified (buffer, FALSE);
485 /* Callback for the File->SaveAs menuitem */
487 syntax_save_as (PsppireWindow *se)
489 GtkFileFilter *filter;
493 gtk_file_chooser_dialog_new (_("Save Syntax"),
495 GTK_FILE_CHOOSER_ACTION_SAVE,
496 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
497 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
500 filter = gtk_file_filter_new ();
501 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
502 gtk_file_filter_add_pattern (filter, "*.sps");
503 gtk_file_filter_add_pattern (filter, "*.SPS");
504 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
506 filter = gtk_file_filter_new ();
507 gtk_file_filter_set_name (filter, _("All Files"));
508 gtk_file_filter_add_pattern (filter, "*");
509 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
511 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
513 response = gtk_dialog_run (GTK_DIALOG (dialog));
515 if ( response == GTK_RESPONSE_ACCEPT )
519 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
521 if ( ! save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err) )
523 msg ( ME, "%s", err->message );
530 gtk_widget_destroy (dialog);
534 /* Callback for the File->Save menuitem */
536 syntax_save (PsppireWindow *se)
538 const gchar *filename = psppire_window_get_filename (se);
540 if ( filename == NULL )
545 save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
548 msg (ME, "%s", err->message);
555 /* Callback for the File->Quit menuitem */
557 on_quit (GtkMenuItem *menuitem, gpointer user_data)
566 create_syntax_window (void)
568 GtkWidget *w = psppire_syntax_window_new ();
573 open_syntax_window (const char *file_name)
575 GtkWidget *se = psppire_syntax_window_new ();
577 if ( psppire_window_load (PSPPIRE_WINDOW (se), file_name) )
578 gtk_widget_show (se);
580 gtk_widget_destroy (se);
584 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
586 gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
590 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
592 if (gtk_text_buffer_get_modified (buffer))
593 psppire_window_set_unsaved (window);
596 extern struct source_stream *the_source_stream ;
599 psppire_syntax_window_init (PsppireSyntaxWindow *window)
601 GtkBuilder *xml = builder_new ("syntax-editor.ui");
602 GtkWidget *box = gtk_vbox_new (FALSE, 0);
604 GtkWidget *menubar = get_widget_assert (xml, "menubar");
605 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
608 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
610 GtkClipboard *clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
611 GtkClipboard *clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
613 window->cliptext = NULL;
614 window->dispose_has_run = FALSE;
616 window->edit_delete = get_action_assert (xml, "edit_delete");
617 window->edit_copy = get_action_assert (xml, "edit_copy");
618 window->edit_cut = get_action_assert (xml, "edit_cut");
619 window->edit_paste = get_action_assert (xml, "edit_paste");
621 window->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
622 window->lexer = lex_create (the_source_stream);
624 window->sb = get_widget_assert (xml, "statusbar2");
625 window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
627 g_signal_connect (window->buffer, "changed",
628 G_CALLBACK (on_text_changed), window);
630 g_signal_connect (window->buffer, "modified-changed",
631 G_CALLBACK (on_modified_changed), window);
633 window->sel_handler = g_signal_connect_swapped (clip_primary, "owner-change",
634 G_CALLBACK (selection_changed), window);
636 window->ps_handler = g_signal_connect (clip_selection, "owner-change",
637 G_CALLBACK (set_paste_sensitivity), window);
641 gtk_container_add (GTK_CONTAINER (window), box);
643 g_object_ref (menubar);
647 g_object_ref (window->sb);
649 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
650 gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
651 gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
653 gtk_widget_show_all (box);
655 g_signal_connect_swapped (get_action_assert (xml,"file_new_syntax"), "activate", G_CALLBACK (create_syntax_window), NULL);
658 g_signal_connect (get_action_assert (xml,"file_new_data"),
660 G_CALLBACK (create_data_window),
664 g_signal_connect_swapped (get_action_assert (xml, "file_save"),
666 G_CALLBACK (syntax_save),
669 g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
671 G_CALLBACK (syntax_save_as),
674 g_signal_connect (get_action_assert (xml,"file_quit"),
676 G_CALLBACK (on_quit),
679 g_signal_connect_swapped (window->edit_delete,
681 G_CALLBACK (on_edit_delete),
684 g_signal_connect_swapped (window->edit_copy,
686 G_CALLBACK (on_edit_copy),
689 g_signal_connect_swapped (window->edit_cut,
691 G_CALLBACK (on_edit_cut),
694 g_signal_connect_swapped (window->edit_paste,
696 G_CALLBACK (on_edit_paste),
699 g_signal_connect (get_action_assert (xml,"run_all"),
701 G_CALLBACK (on_run_all),
704 g_signal_connect (get_action_assert (xml,"run_selection"),
706 G_CALLBACK (on_run_selection),
709 g_signal_connect (get_action_assert (xml,"run_current_line"),
711 G_CALLBACK (on_run_current_line),
714 g_signal_connect (get_action_assert (xml,"run_to_end"),
716 G_CALLBACK (on_run_to_end),
719 g_signal_connect (get_action_assert (xml,"windows_minimise_all"),
721 G_CALLBACK (psppire_window_minimise_all), NULL);
725 GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
727 merge_help_menu (uim);
729 PSPPIRE_WINDOW (window)->menu =
730 GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
733 g_object_unref (xml);
741 psppire_syntax_window_new (void)
743 return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
744 /* TRANSLATORS: This will form a filename. Please avoid whitespace. */
745 "filename", _("Syntax"),
746 "description", _("Syntax Editor"),
751 error_dialog (GtkWindow *w, const gchar *filename, GError *err)
753 gchar *fn = g_filename_display_basename (filename);
756 gtk_message_dialog_new (w,
757 GTK_DIALOG_DESTROY_WITH_PARENT,
760 _("Cannot load syntax file `%s'"),
765 g_object_set (dialog, "icon-name", "psppicon", NULL);
767 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
770 gtk_dialog_run (GTK_DIALOG (dialog));
772 gtk_widget_destroy (dialog);
776 Loads the buffer from the file called FILENAME
779 syntax_load (PsppireWindow *window, const gchar *filename)
782 gchar *text_locale = NULL;
783 gchar *text_utf8 = NULL;
784 gsize len_locale = -1;
787 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
789 /* FIXME: What if it's a very big file ? */
790 if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
792 error_dialog (GTK_WINDOW (window), filename, err);
793 g_clear_error (&err);
797 text_utf8 = g_locale_to_utf8 (text_locale, len_locale, NULL, &len_utf8, &err);
801 if ( text_utf8 == NULL )
803 error_dialog (GTK_WINDOW (window), filename, err);
804 g_clear_error (&err);
808 gtk_text_buffer_get_iter_at_line (sw->buffer, &iter, 0);
810 gtk_text_buffer_insert (sw->buffer, &iter, text_utf8, len_utf8);
812 gtk_text_buffer_set_modified (sw->buffer, FALSE);
822 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
824 iface->save = syntax_save;
825 iface->load = syntax_load;