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/>. */
23 #include <language/lexer/lexer.h>
24 #include <libpspp/message.h>
27 #include "help-menu.h"
29 #include "psppire-data-window.h"
30 #include "psppire-window-register.h"
31 #include "psppire-syntax-window.h"
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) msgid
39 static void psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *, gpointer);
40 static void psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class);
41 static void psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class);
42 static void psppire_syntax_window_init (PsppireSyntaxWindow *syntax_editor);
45 static void psppire_syntax_window_iface_init (PsppireWindowIface *iface);
49 psppire_syntax_window_get_type (void)
51 static GType psppire_syntax_window_type = 0;
53 if (!psppire_syntax_window_type)
55 static const GTypeInfo psppire_syntax_window_info =
57 sizeof (PsppireSyntaxWindowClass),
58 (GBaseInitFunc) psppire_syntax_window_base_init,
59 (GBaseFinalizeFunc) psppire_syntax_window_base_finalize,
60 (GClassInitFunc)psppire_syntax_window_class_init,
61 (GClassFinalizeFunc) NULL,
63 sizeof (PsppireSyntaxWindow),
65 (GInstanceInitFunc) psppire_syntax_window_init,
68 static const GInterfaceInfo window_interface_info =
70 (GInterfaceInitFunc) psppire_syntax_window_iface_init,
75 psppire_syntax_window_type =
76 g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireSyntaxWindow",
77 &psppire_syntax_window_info, 0);
79 g_type_add_interface_static (psppire_syntax_window_type,
80 PSPPIRE_TYPE_WINDOW_MODEL,
81 &window_interface_info);
84 return psppire_syntax_window_type;
87 static GObjectClass *parent_class ;
90 psppire_syntax_window_finalize (GObject *object)
92 if (G_OBJECT_CLASS (parent_class)->finalize)
93 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
98 psppire_syntax_window_dispose (GObject *obj)
100 PsppireSyntaxWindow *sw = (PsppireSyntaxWindow *)obj;
102 GtkClipboard *clip_selection;
103 GtkClipboard *clip_primary;
105 if (sw->dispose_has_run)
108 clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
109 clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_PRIMARY);
111 g_signal_handler_disconnect (clip_primary, sw->sel_handler);
113 g_signal_handler_disconnect (clip_selection, sw->ps_handler);
115 /* Make sure dispose does not run twice. */
116 sw->dispose_has_run = TRUE;
118 /* Chain up to the parent class */
119 G_OBJECT_CLASS (parent_class)->dispose (obj);
125 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
127 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
129 parent_class = g_type_class_peek_parent (class);
131 gobject_class->dispose = psppire_syntax_window_dispose;
136 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
138 GObjectClass *object_class = G_OBJECT_CLASS (class);
140 object_class->finalize = psppire_syntax_window_finalize;
146 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
153 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
156 PsppireWindow *win = PSPPIRE_WINDOW (sw);
157 struct lex_reader *reader;
160 text = gtk_text_buffer_get_text (sw->buffer, &start, &stop, FALSE);
161 reader = lex_reader_for_string (text);
164 lex_reader_set_file_name (reader, psppire_window_get_filename (win));
166 execute_syntax (psppire_default_data_window (), reader);
172 /* Delete the currently selected text */
174 on_edit_delete (PsppireSyntaxWindow *sw)
176 GtkTextIter begin, end;
178 if ( gtk_text_buffer_get_selection_bounds (sw->buffer, &begin, &end) )
179 gtk_text_buffer_delete (sw->buffer, &begin, &end);
185 /* The syntax editor's clipboard deals only with text */
193 selection_changed (PsppireSyntaxWindow *sw)
195 gboolean sel = gtk_text_buffer_get_has_selection (sw->buffer);
197 gtk_action_set_sensitive (sw->edit_copy, sel);
198 gtk_action_set_sensitive (sw->edit_cut, sel);
199 gtk_action_set_sensitive (sw->edit_delete, sel);
202 /* The callback which runs when something request clipboard data */
204 clipboard_get_cb (GtkClipboard *clipboard,
205 GtkSelectionData *selection_data,
209 PsppireSyntaxWindow *sw = data;
210 g_assert (info == SELECT_FMT_TEXT);
212 gtk_selection_data_set (selection_data, selection_data->target,
214 (const guchar *) sw->cliptext, strlen (sw->cliptext));
219 clipboard_clear_cb (GtkClipboard *clipboard,
222 PsppireSyntaxWindow *sw = data;
223 g_free (sw->cliptext);
228 static const GtkTargetEntry targets[] = {
229 { "UTF8_STRING", 0, SELECT_FMT_TEXT },
230 { "STRING", 0, SELECT_FMT_TEXT },
231 { "TEXT", 0, SELECT_FMT_TEXT },
232 { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
233 { "text/plain;charset=utf-8", 0, SELECT_FMT_TEXT },
234 { "text/plain", 0, SELECT_FMT_TEXT },
239 Store a clip containing the currently selected text.
240 Returns true iff something was set.
241 As a side effect, begin and end will be set to indicate
242 the limits of the selected text.
245 set_clip (PsppireSyntaxWindow *sw, GtkTextIter *begin, GtkTextIter *end)
247 GtkClipboard *clipboard ;
249 if ( ! gtk_text_buffer_get_selection_bounds (sw->buffer, begin, end) )
252 g_free (sw->cliptext);
253 sw->cliptext = gtk_text_buffer_get_text (sw->buffer, begin, end, FALSE);
256 gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
258 if (!gtk_clipboard_set_with_owner (clipboard, targets,
259 G_N_ELEMENTS (targets),
260 clipboard_get_cb, clipboard_clear_cb,
262 clipboard_clear_cb (clipboard, sw);
268 on_edit_cut (PsppireSyntaxWindow *sw)
270 GtkTextIter begin, end;
272 if ( set_clip (sw, &begin, &end))
273 gtk_text_buffer_delete (sw->buffer, &begin, &end);
277 on_edit_copy (PsppireSyntaxWindow *sw)
279 GtkTextIter begin, end;
281 set_clip (sw, &begin, &end);
285 /* A callback for when the clipboard contents have been received */
287 contents_received_callback (GtkClipboard *clipboard,
288 GtkSelectionData *sd,
292 PsppireSyntaxWindow *syntax_window = data;
294 if ( sd->length < 0 )
297 if ( sd->type != gdk_atom_intern ("UTF8_STRING", FALSE))
300 c = (gchar *) sd->data;
302 gtk_text_buffer_insert_at_cursor (syntax_window->buffer,
309 on_edit_paste (PsppireSyntaxWindow *sw)
311 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (sw));
312 GtkClipboard *clipboard =
313 gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
315 gtk_clipboard_request_contents (clipboard,
316 gdk_atom_intern ("UTF8_STRING", TRUE),
317 contents_received_callback,
322 /* Check to see if CLIP holds a target which we know how to paste,
323 and set the sensitivity of the Paste action accordingly.
326 set_paste_sensitivity (GtkClipboard *clip, GdkEventOwnerChange *event, gpointer data)
329 gboolean compatible_target = FALSE;
330 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (data);
332 for (i = 0 ; i < sizeof (targets) / sizeof (targets[0]) ; ++i)
334 GdkAtom atom = gdk_atom_intern (targets[i].target, TRUE);
335 if ( gtk_clipboard_wait_is_target_available (clip, atom))
337 compatible_target = TRUE;
342 gtk_action_set_sensitive (sw->edit_paste, compatible_target);
348 /* Parse and execute all the text in the buffer */
350 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
352 GtkTextIter begin, end;
353 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
355 gtk_text_buffer_get_iter_at_offset (se->buffer, &begin, 0);
356 gtk_text_buffer_get_iter_at_offset (se->buffer, &end, -1);
358 editor_execute_syntax (se, begin, end);
361 /* Parse and execute the currently selected text */
363 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
365 GtkTextIter begin, end;
366 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
368 if ( gtk_text_buffer_get_selection_bounds (se->buffer, &begin, &end) )
369 editor_execute_syntax (se, begin, end);
373 /* Parse and execute the from the current line, to the end of the
376 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
378 GtkTextIter begin, end;
382 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
384 /* Get the current line */
385 gtk_text_buffer_get_iter_at_mark (se->buffer,
387 gtk_text_buffer_get_insert (se->buffer)
390 line = gtk_text_iter_get_line (&here) ;
392 /* Now set begin and end to the start of this line, and end of buffer
394 gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
395 gtk_text_buffer_get_iter_at_line (se->buffer, &end, -1);
397 editor_execute_syntax (se, begin, end);
402 /* Parse and execute the current line */
404 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
406 GtkTextIter begin, end;
410 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
412 /* Get the current line */
413 gtk_text_buffer_get_iter_at_mark (se->buffer,
415 gtk_text_buffer_get_insert (se->buffer)
418 line = gtk_text_iter_get_line (&here) ;
420 /* Now set begin and end to the start of this line, and start of
421 following line respectively */
422 gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
423 gtk_text_buffer_get_iter_at_line (se->buffer, &end, line + 1);
425 editor_execute_syntax (se, begin, end);
430 /* Append ".sps" to FILENAME if necessary.
431 The returned result must be freed when no longer required.
434 append_suffix (const gchar *filename)
436 if ( ! g_str_has_suffix (filename, ".sps" ) &&
437 ! g_str_has_suffix (filename, ".SPS" ) )
439 return g_strdup_printf ("%s.sps", filename);
442 return xstrdup (filename);
446 Save BUFFER to the file called FILENAME.
447 FILENAME must be encoded in Glib filename encoding.
448 If successful, clears the buffer's modified flag.
451 save_editor_to_file (PsppireSyntaxWindow *se,
452 const gchar *filename,
455 GtkTextBuffer *buffer = se->buffer;
457 GtkTextIter start, stop;
463 suffixedname = append_suffix (filename);
465 gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
466 gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
468 text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
470 result = g_file_set_contents (suffixedname, text, -1, err);
472 g_free (suffixedname);
476 char *fn = g_filename_display_name (filename);
477 gchar *msg = g_strdup_printf (_("Saved file `%s'"), fn);
479 gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
480 gtk_text_buffer_set_modified (buffer, FALSE);
488 /* Callback for the File->SaveAs menuitem */
490 syntax_save_as (PsppireWindow *se)
492 GtkFileFilter *filter;
496 gtk_file_chooser_dialog_new (_("Save Syntax"),
498 GTK_FILE_CHOOSER_ACTION_SAVE,
499 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
500 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
503 filter = gtk_file_filter_new ();
504 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
505 gtk_file_filter_add_pattern (filter, "*.sps");
506 gtk_file_filter_add_pattern (filter, "*.SPS");
507 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
509 filter = gtk_file_filter_new ();
510 gtk_file_filter_set_name (filter, _("All Files"));
511 gtk_file_filter_add_pattern (filter, "*");
512 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
514 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
516 response = gtk_dialog_run (GTK_DIALOG (dialog));
518 if ( response == GTK_RESPONSE_ACCEPT )
522 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
524 if ( ! save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err) )
526 msg ( ME, "%s", err->message );
533 gtk_widget_destroy (dialog);
537 /* Callback for the File->Save menuitem */
539 syntax_save (PsppireWindow *se)
541 const gchar *filename = psppire_window_get_filename (se);
543 if ( filename == NULL )
548 save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
551 msg (ME, "%s", err->message);
558 /* Callback for the File->Quit menuitem */
560 on_quit (GtkMenuItem *menuitem, gpointer user_data)
569 create_syntax_window (void)
571 GtkWidget *w = psppire_syntax_window_new ();
576 open_syntax_window (const char *file_name)
578 GtkWidget *se = psppire_syntax_window_new ();
580 if ( psppire_window_load (PSPPIRE_WINDOW (se), file_name) )
581 gtk_widget_show (se);
583 gtk_widget_destroy (se);
587 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
589 gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
593 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
595 if (gtk_text_buffer_get_modified (buffer))
596 psppire_window_set_unsaved (window);
600 psppire_syntax_window_init (PsppireSyntaxWindow *window)
602 GtkBuilder *xml = builder_new ("syntax-editor.ui");
603 GtkWidget *box = gtk_vbox_new (FALSE, 0);
605 GtkWidget *menubar = get_widget_assert (xml, "menubar");
606 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
609 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
611 GtkClipboard *clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
612 GtkClipboard *clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
614 window->cliptext = NULL;
615 window->dispose_has_run = FALSE;
617 window->edit_delete = get_action_assert (xml, "edit_delete");
618 window->edit_copy = get_action_assert (xml, "edit_copy");
619 window->edit_cut = get_action_assert (xml, "edit_cut");
620 window->edit_paste = get_action_assert (xml, "edit_paste");
622 window->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
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;