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 "language/lexer/lexer.h"
23 #include "libpspp/message.h"
24 #include "ui/gui/executor.h"
25 #include "ui/gui/help-menu.h"
26 #include "ui/gui/helper.h"
27 #include "ui/gui/psppire-data-window.h"
28 #include "ui/gui/psppire-syntax-window.h"
29 #include "ui/gui/psppire-syntax-window.h"
30 #include "ui/gui/psppire-window-register.h"
31 #include "ui/gui/psppire.h"
32 #include "ui/gui/psppire.h"
34 #include "gl/xalloc.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 (psppire_default_data_window (), reader);
170 /* Delete the currently selected text */
172 on_edit_delete (PsppireSyntaxWindow *sw)
174 GtkTextIter begin, end;
176 if ( gtk_text_buffer_get_selection_bounds (sw->buffer, &begin, &end) )
177 gtk_text_buffer_delete (sw->buffer, &begin, &end);
183 /* The syntax editor's clipboard deals only with text */
191 selection_changed (PsppireSyntaxWindow *sw)
193 gboolean sel = gtk_text_buffer_get_has_selection (sw->buffer);
195 gtk_action_set_sensitive (sw->edit_copy, sel);
196 gtk_action_set_sensitive (sw->edit_cut, sel);
197 gtk_action_set_sensitive (sw->edit_delete, sel);
200 /* The callback which runs when something request clipboard data */
202 clipboard_get_cb (GtkClipboard *clipboard,
203 GtkSelectionData *selection_data,
207 PsppireSyntaxWindow *sw = data;
208 g_assert (info == SELECT_FMT_TEXT);
210 gtk_selection_data_set (selection_data, selection_data->target,
212 (const guchar *) sw->cliptext, strlen (sw->cliptext));
217 clipboard_clear_cb (GtkClipboard *clipboard,
220 PsppireSyntaxWindow *sw = data;
221 g_free (sw->cliptext);
226 static const GtkTargetEntry targets[] = {
227 { "UTF8_STRING", 0, SELECT_FMT_TEXT },
228 { "STRING", 0, SELECT_FMT_TEXT },
229 { "TEXT", 0, SELECT_FMT_TEXT },
230 { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
231 { "text/plain;charset=utf-8", 0, SELECT_FMT_TEXT },
232 { "text/plain", 0, SELECT_FMT_TEXT },
237 Store a clip containing the currently selected text.
238 Returns true iff something was set.
239 As a side effect, begin and end will be set to indicate
240 the limits of the selected text.
243 set_clip (PsppireSyntaxWindow *sw, GtkTextIter *begin, GtkTextIter *end)
245 GtkClipboard *clipboard ;
247 if ( ! gtk_text_buffer_get_selection_bounds (sw->buffer, begin, end) )
250 g_free (sw->cliptext);
251 sw->cliptext = gtk_text_buffer_get_text (sw->buffer, begin, end, FALSE);
254 gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
256 if (!gtk_clipboard_set_with_owner (clipboard, targets,
257 G_N_ELEMENTS (targets),
258 clipboard_get_cb, clipboard_clear_cb,
260 clipboard_clear_cb (clipboard, sw);
266 on_edit_cut (PsppireSyntaxWindow *sw)
268 GtkTextIter begin, end;
270 if ( set_clip (sw, &begin, &end))
271 gtk_text_buffer_delete (sw->buffer, &begin, &end);
275 on_edit_copy (PsppireSyntaxWindow *sw)
277 GtkTextIter begin, end;
279 set_clip (sw, &begin, &end);
283 /* A callback for when the clipboard contents have been received */
285 contents_received_callback (GtkClipboard *clipboard,
286 GtkSelectionData *sd,
290 PsppireSyntaxWindow *syntax_window = data;
292 if ( sd->length < 0 )
295 if ( sd->type != gdk_atom_intern ("UTF8_STRING", FALSE))
298 c = (gchar *) sd->data;
300 gtk_text_buffer_insert_at_cursor (syntax_window->buffer,
307 on_edit_paste (PsppireSyntaxWindow *sw)
309 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (sw));
310 GtkClipboard *clipboard =
311 gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
313 gtk_clipboard_request_contents (clipboard,
314 gdk_atom_intern ("UTF8_STRING", TRUE),
315 contents_received_callback,
320 /* Check to see if CLIP holds a target which we know how to paste,
321 and set the sensitivity of the Paste action accordingly.
324 set_paste_sensitivity (GtkClipboard *clip, GdkEventOwnerChange *event, gpointer data)
327 gboolean compatible_target = FALSE;
328 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (data);
330 for (i = 0 ; i < sizeof (targets) / sizeof (targets[0]) ; ++i)
332 GdkAtom atom = gdk_atom_intern (targets[i].target, TRUE);
333 if ( gtk_clipboard_wait_is_target_available (clip, atom))
335 compatible_target = TRUE;
340 gtk_action_set_sensitive (sw->edit_paste, compatible_target);
346 /* Parse and execute all the text in the buffer */
348 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
350 GtkTextIter begin, end;
351 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
353 gtk_text_buffer_get_iter_at_offset (se->buffer, &begin, 0);
354 gtk_text_buffer_get_iter_at_offset (se->buffer, &end, -1);
356 editor_execute_syntax (se, begin, end);
359 /* Parse and execute the currently selected text */
361 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
363 GtkTextIter begin, end;
364 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
366 if ( gtk_text_buffer_get_selection_bounds (se->buffer, &begin, &end) )
367 editor_execute_syntax (se, begin, end);
371 /* Parse and execute the from the current line, to the end of the
374 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
376 GtkTextIter begin, end;
380 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
382 /* Get the current line */
383 gtk_text_buffer_get_iter_at_mark (se->buffer,
385 gtk_text_buffer_get_insert (se->buffer)
388 line = gtk_text_iter_get_line (&here) ;
390 /* Now set begin and end to the start of this line, and end of buffer
392 gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
393 gtk_text_buffer_get_iter_at_line (se->buffer, &end, -1);
395 editor_execute_syntax (se, begin, end);
400 /* Parse and execute the current line */
402 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
404 GtkTextIter begin, end;
408 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
410 /* Get the current line */
411 gtk_text_buffer_get_iter_at_mark (se->buffer,
413 gtk_text_buffer_get_insert (se->buffer)
416 line = gtk_text_iter_get_line (&here) ;
418 /* Now set begin and end to the start of this line, and start of
419 following line respectively */
420 gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
421 gtk_text_buffer_get_iter_at_line (se->buffer, &end, line + 1);
423 editor_execute_syntax (se, begin, end);
428 /* Append ".sps" to FILENAME if necessary.
429 The returned result must be freed when no longer required.
432 append_suffix (const gchar *filename)
434 if ( ! g_str_has_suffix (filename, ".sps" ) &&
435 ! g_str_has_suffix (filename, ".SPS" ) )
437 return g_strdup_printf ("%s.sps", filename);
440 return xstrdup (filename);
444 Save BUFFER to the file called FILENAME.
445 FILENAME must be encoded in Glib filename encoding.
446 If successful, clears the buffer's modified flag.
449 save_editor_to_file (PsppireSyntaxWindow *se,
450 const gchar *filename,
453 GtkTextBuffer *buffer = se->buffer;
455 GtkTextIter start, stop;
461 suffixedname = append_suffix (filename);
463 gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
464 gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
466 text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
468 result = g_file_set_contents (suffixedname, text, -1, err);
470 g_free (suffixedname);
474 char *fn = g_filename_display_name (filename);
475 gchar *msg = g_strdup_printf (_("Saved file `%s'"), fn);
477 gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
478 gtk_text_buffer_set_modified (buffer, FALSE);
486 /* PsppireWindow 'pick_Filename' callback. */
488 syntax_pick_filename (PsppireWindow *se)
490 GtkFileFilter *filter;
494 gtk_file_chooser_dialog_new (_("Save Syntax"),
496 GTK_FILE_CHOOSER_ACTION_SAVE,
497 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
498 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
501 filter = gtk_file_filter_new ();
502 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
503 gtk_file_filter_add_pattern (filter, "*.sps");
504 gtk_file_filter_add_pattern (filter, "*.SPS");
505 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
507 filter = gtk_file_filter_new ();
508 gtk_file_filter_set_name (filter, _("All Files"));
509 gtk_file_filter_add_pattern (filter, "*");
510 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
512 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
514 response = gtk_dialog_run (GTK_DIALOG (dialog));
516 if ( response == GTK_RESPONSE_ACCEPT )
519 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
520 psppire_window_set_filename (se, filename);
524 gtk_widget_destroy (dialog);
528 /* PsppireWindow 'save' callback. */
530 syntax_save (PsppireWindow *se)
532 const gchar *filename = psppire_window_get_filename (se);
534 save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
537 msg (ME, "%s", err->message);
543 /* Callback for the File->Quit menuitem */
545 on_quit (GtkMenuItem *menuitem, gpointer user_data)
554 create_syntax_window (void)
556 GtkWidget *w = psppire_syntax_window_new ();
561 open_syntax_window (const char *file_name)
563 GtkWidget *se = psppire_syntax_window_new ();
565 if ( psppire_window_load (PSPPIRE_WINDOW (se), file_name) )
566 gtk_widget_show (se);
568 gtk_widget_destroy (se);
572 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
574 gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
578 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
580 if (gtk_text_buffer_get_modified (buffer))
581 psppire_window_set_unsaved (window);
585 psppire_syntax_window_init (PsppireSyntaxWindow *window)
587 GtkBuilder *xml = builder_new ("syntax-editor.ui");
588 GtkWidget *box = gtk_vbox_new (FALSE, 0);
590 GtkWidget *menubar = get_widget_assert (xml, "menubar");
591 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
594 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
596 GtkClipboard *clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
597 GtkClipboard *clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
599 window->cliptext = NULL;
600 window->dispose_has_run = FALSE;
602 window->edit_delete = get_action_assert (xml, "edit_delete");
603 window->edit_copy = get_action_assert (xml, "edit_copy");
604 window->edit_cut = get_action_assert (xml, "edit_cut");
605 window->edit_paste = get_action_assert (xml, "edit_paste");
607 window->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
609 window->sb = get_widget_assert (xml, "statusbar2");
610 window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
612 g_signal_connect (window->buffer, "changed",
613 G_CALLBACK (on_text_changed), window);
615 g_signal_connect (window->buffer, "modified-changed",
616 G_CALLBACK (on_modified_changed), window);
618 window->sel_handler = g_signal_connect_swapped (clip_primary, "owner-change",
619 G_CALLBACK (selection_changed), window);
621 window->ps_handler = g_signal_connect (clip_selection, "owner-change",
622 G_CALLBACK (set_paste_sensitivity), window);
626 gtk_container_add (GTK_CONTAINER (window), box);
628 g_object_ref (menubar);
632 g_object_ref (window->sb);
634 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
635 gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
636 gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
638 gtk_widget_show_all (box);
640 g_signal_connect_swapped (get_action_assert (xml,"file_new_syntax"), "activate", G_CALLBACK (create_syntax_window), NULL);
642 g_signal_connect (get_action_assert (xml,"file_new_data"),
644 G_CALLBACK (create_data_window),
647 g_signal_connect_swapped (get_action_assert (xml, "file_open"),
649 G_CALLBACK (psppire_window_open),
652 g_signal_connect_swapped (get_action_assert (xml, "file_save"),
654 G_CALLBACK (psppire_window_save),
657 g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
659 G_CALLBACK (psppire_window_save_as),
662 g_signal_connect (get_action_assert (xml,"file_quit"),
664 G_CALLBACK (on_quit),
667 g_signal_connect_swapped (window->edit_delete,
669 G_CALLBACK (on_edit_delete),
672 g_signal_connect_swapped (window->edit_copy,
674 G_CALLBACK (on_edit_copy),
677 g_signal_connect_swapped (window->edit_cut,
679 G_CALLBACK (on_edit_cut),
682 g_signal_connect_swapped (window->edit_paste,
684 G_CALLBACK (on_edit_paste),
687 g_signal_connect (get_action_assert (xml,"run_all"),
689 G_CALLBACK (on_run_all),
692 g_signal_connect (get_action_assert (xml,"run_selection"),
694 G_CALLBACK (on_run_selection),
697 g_signal_connect (get_action_assert (xml,"run_current_line"),
699 G_CALLBACK (on_run_current_line),
702 g_signal_connect (get_action_assert (xml,"run_to_end"),
704 G_CALLBACK (on_run_to_end),
707 g_signal_connect (get_action_assert (xml,"windows_minimise_all"),
709 G_CALLBACK (psppire_window_minimise_all), NULL);
713 GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
715 merge_help_menu (uim);
717 PSPPIRE_WINDOW (window)->menu =
718 GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
721 g_object_unref (xml);
729 psppire_syntax_window_new (void)
731 return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
732 /* TRANSLATORS: This will form a filename. Please avoid whitespace. */
733 "description", _("Syntax Editor"),
738 error_dialog (GtkWindow *w, const gchar *filename, GError *err)
740 gchar *fn = g_filename_display_basename (filename);
743 gtk_message_dialog_new (w,
744 GTK_DIALOG_DESTROY_WITH_PARENT,
747 _("Cannot load syntax file `%s'"),
752 g_object_set (dialog, "icon-name", "psppicon", NULL);
754 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
757 gtk_dialog_run (GTK_DIALOG (dialog));
759 gtk_widget_destroy (dialog);
763 Loads the buffer from the file called FILENAME
766 syntax_load (PsppireWindow *window, const gchar *filename)
769 gchar *text_locale = NULL;
770 gchar *text_utf8 = NULL;
771 gsize len_locale = -1;
774 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
776 /* FIXME: What if it's a very big file ? */
777 if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
779 error_dialog (GTK_WINDOW (window), filename, err);
780 g_clear_error (&err);
784 text_utf8 = g_locale_to_utf8 (text_locale, len_locale, NULL, &len_utf8, &err);
788 if ( text_utf8 == NULL )
790 error_dialog (GTK_WINDOW (window), filename, err);
791 g_clear_error (&err);
795 gtk_text_buffer_get_iter_at_line (sw->buffer, &iter, 0);
797 gtk_text_buffer_insert (sw->buffer, &iter, text_utf8, len_utf8);
799 gtk_text_buffer_set_modified (sw->buffer, FALSE);
809 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
811 iface->save = syntax_save;
812 iface->pick_filename = syntax_pick_filename;
813 iface->load = syntax_load;