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_class_init (PsppireSyntaxWindowClass *class)
105 parent_class = g_type_class_peek_parent (class);
110 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
112 GObjectClass *object_class = G_OBJECT_CLASS (class);
114 object_class->finalize = psppire_syntax_window_finalize;
120 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
127 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
130 PsppireWindow *win = PSPPIRE_WINDOW (sw);
131 const gchar *name = psppire_window_get_filename (win);
132 execute_syntax (create_syntax_editor_source (sw->buffer, start, stop, name));
138 /* Delete the currently selected text */
140 on_edit_delete (PsppireSyntaxWindow *sw)
142 GtkTextIter begin, end;
144 if ( gtk_text_buffer_get_selection_bounds (sw->buffer, &begin, &end) )
145 gtk_text_buffer_delete (sw->buffer, &begin, &end);
151 /* The syntax editor's clipboard deals only with text */
159 selection_changed (PsppireSyntaxWindow *sw)
161 gboolean sel = gtk_text_buffer_get_has_selection (sw->buffer);
163 gtk_action_set_sensitive (sw->edit_copy, sel);
164 gtk_action_set_sensitive (sw->edit_cut, sel);
165 gtk_action_set_sensitive (sw->edit_delete, sel);
168 /* The callback which runs when something request clipboard data */
170 clipboard_get_cb (GtkClipboard *clipboard,
171 GtkSelectionData *selection_data,
175 PsppireSyntaxWindow *sw = data;
176 g_assert (info == SELECT_FMT_TEXT);
178 gtk_selection_data_set (selection_data, selection_data->target,
180 (const guchar *) sw->cliptext, strlen (sw->cliptext));
185 clipboard_clear_cb (GtkClipboard *clipboard,
188 PsppireSyntaxWindow *sw = data;
189 g_free (sw->cliptext);
194 static const GtkTargetEntry targets[] = {
195 { "UTF8_STRING", 0, SELECT_FMT_TEXT },
196 { "STRING", 0, SELECT_FMT_TEXT },
197 { "TEXT", 0, SELECT_FMT_TEXT },
198 { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
199 { "text/plain;charset=utf-8", 0, SELECT_FMT_TEXT },
200 { "text/plain", 0, SELECT_FMT_TEXT },
205 Store a clip containing the currently selected text.
206 Returns true iff something was set.
207 As a side effect, begin and end will be set to indicate
208 the limits of the selected text.
211 set_clip (PsppireSyntaxWindow *sw, GtkTextIter *begin, GtkTextIter *end)
213 GtkClipboard *clipboard ;
215 if ( ! gtk_text_buffer_get_selection_bounds (sw->buffer, begin, end) )
218 g_free (sw->cliptext);
219 sw->cliptext = gtk_text_buffer_get_text (sw->buffer, begin, end, FALSE);
222 gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
224 if (!gtk_clipboard_set_with_owner (clipboard, targets,
225 G_N_ELEMENTS (targets),
226 clipboard_get_cb, clipboard_clear_cb,
228 clipboard_clear_cb (clipboard, sw);
234 on_edit_cut (PsppireSyntaxWindow *sw)
236 GtkTextIter begin, end;
238 if ( set_clip (sw, &begin, &end))
239 gtk_text_buffer_delete (sw->buffer, &begin, &end);
243 on_edit_copy (PsppireSyntaxWindow *sw)
245 GtkTextIter begin, end;
247 set_clip (sw, &begin, &end);
251 /* A callback for when the clipboard contents have been received */
253 contents_received_callback (GtkClipboard *clipboard,
254 GtkSelectionData *sd,
258 PsppireSyntaxWindow *syntax_window = data;
260 if ( sd->length < 0 )
263 if ( sd->type != gdk_atom_intern ("UTF8_STRING", FALSE))
266 c = (gchar *) sd->data;
268 gtk_text_buffer_insert_at_cursor (syntax_window->buffer,
275 on_edit_paste (PsppireSyntaxWindow *sw)
277 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (sw));
278 GtkClipboard *clipboard =
279 gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
281 gtk_clipboard_request_contents (clipboard,
282 gdk_atom_intern ("UTF8_STRING", TRUE),
283 contents_received_callback,
288 on_owner_change (GtkClipboard *clip, GdkEventOwnerChange *event, gpointer data)
291 gboolean compatible_target = FALSE;
292 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (data);
294 for (i = 0 ; i < sizeof (targets) / sizeof (targets[0]) ; ++i)
296 GdkAtom atom = gdk_atom_intern (targets[i].target, TRUE);
297 if ( gtk_clipboard_wait_is_target_available (clip, atom))
299 compatible_target = TRUE;
304 gtk_action_set_sensitive (sw->edit_paste, compatible_target);
310 /* Parse and execute all the text in the buffer */
312 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
314 GtkTextIter begin, end;
315 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
317 gtk_text_buffer_get_iter_at_offset (se->buffer, &begin, 0);
318 gtk_text_buffer_get_iter_at_offset (se->buffer, &end, -1);
320 editor_execute_syntax (se, begin, end);
323 /* Parse and execute the currently selected text */
325 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
327 GtkTextIter begin, end;
328 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
330 if ( gtk_text_buffer_get_selection_bounds (se->buffer, &begin, &end) )
331 editor_execute_syntax (se, begin, end);
335 /* Parse and execute the from the current line, to the end of the
338 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
340 GtkTextIter begin, end;
344 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
346 /* Get the current line */
347 gtk_text_buffer_get_iter_at_mark (se->buffer,
349 gtk_text_buffer_get_insert (se->buffer)
352 line = gtk_text_iter_get_line (&here) ;
354 /* Now set begin and end to the start of this line, and end of buffer
356 gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
357 gtk_text_buffer_get_iter_at_line (se->buffer, &end, -1);
359 editor_execute_syntax (se, begin, end);
364 /* Parse and execute the current line */
366 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
368 GtkTextIter begin, end;
372 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
374 /* Get the current line */
375 gtk_text_buffer_get_iter_at_mark (se->buffer,
377 gtk_text_buffer_get_insert (se->buffer)
380 line = gtk_text_iter_get_line (&here) ;
382 /* Now set begin and end to the start of this line, and start of
383 following line respectively */
384 gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
385 gtk_text_buffer_get_iter_at_line (se->buffer, &end, line + 1);
387 editor_execute_syntax (se, begin, end);
392 /* Append ".sps" to FILENAME if necessary.
393 The returned result must be freed when no longer required.
396 append_suffix (const gchar *filename)
398 if ( ! g_str_has_suffix (filename, ".sps" ) &&
399 ! g_str_has_suffix (filename, ".SPS" ) )
401 return g_strdup_printf ("%s.sps", filename);
404 return xstrdup (filename);
408 Save BUFFER to the file called FILENAME.
409 FILENAME must be encoded in Glib filename encoding.
410 If successful, clears the buffer's modified flag.
413 save_editor_to_file (PsppireSyntaxWindow *se,
414 const gchar *filename,
417 GtkTextBuffer *buffer = se->buffer;
419 GtkTextIter start, stop;
425 suffixedname = append_suffix (filename);
427 gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
428 gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
430 text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
432 result = g_file_set_contents (suffixedname, text, -1, err);
434 g_free (suffixedname);
438 char *fn = g_filename_display_name (filename);
439 gchar *msg = g_strdup_printf (_("Saved file `%s'"), fn);
441 gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
442 gtk_text_buffer_set_modified (buffer, FALSE);
450 /* Callback for the File->SaveAs menuitem */
452 syntax_save_as (PsppireWindow *se)
454 GtkFileFilter *filter;
458 gtk_file_chooser_dialog_new (_("Save Syntax"),
460 GTK_FILE_CHOOSER_ACTION_SAVE,
461 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
462 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
465 filter = gtk_file_filter_new ();
466 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
467 gtk_file_filter_add_pattern (filter, "*.sps");
468 gtk_file_filter_add_pattern (filter, "*.SPS");
469 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
471 filter = gtk_file_filter_new ();
472 gtk_file_filter_set_name (filter, _("All Files"));
473 gtk_file_filter_add_pattern (filter, "*");
474 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
476 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
478 response = gtk_dialog_run (GTK_DIALOG (dialog));
480 if ( response == GTK_RESPONSE_ACCEPT )
484 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
486 if ( ! save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err) )
488 msg ( ME, "%s", err->message );
495 gtk_widget_destroy (dialog);
499 /* Callback for the File->Save menuitem */
501 syntax_save (PsppireWindow *se)
503 const gchar *filename = psppire_window_get_filename (se);
505 if ( filename == NULL )
510 save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
513 msg (ME, "%s", err->message);
520 /* Callback for the File->Quit menuitem */
522 on_quit (GtkMenuItem *menuitem, gpointer user_data)
531 create_syntax_window (void)
533 GtkWidget *w = psppire_syntax_window_new ();
538 open_syntax_window (const char *file_name)
540 GtkWidget *se = psppire_syntax_window_new ();
542 if ( psppire_window_load (PSPPIRE_WINDOW (se), file_name) )
543 gtk_widget_show (se);
545 gtk_widget_destroy (se);
549 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
551 gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
555 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
557 if (gtk_text_buffer_get_modified (buffer))
558 psppire_window_set_unsaved (window);
561 extern struct source_stream *the_source_stream ;
564 psppire_syntax_window_init (PsppireSyntaxWindow *window)
566 GtkBuilder *xml = builder_new ("syntax-editor.ui");
567 GtkWidget *box = gtk_vbox_new (FALSE, 0);
569 GtkWidget *menubar = get_widget_assert (xml, "menubar");
570 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
573 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
575 GtkClipboard *clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
576 GtkClipboard *clip_primary = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
578 window->cliptext = NULL;
580 window->edit_delete = get_action_assert (xml, "edit_delete");
581 window->edit_copy = get_action_assert (xml, "edit_copy");
582 window->edit_cut = get_action_assert (xml, "edit_cut");
583 window->edit_paste = get_action_assert (xml, "edit_paste");
585 window->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
586 window->lexer = lex_create (the_source_stream);
588 window->sb = get_widget_assert (xml, "statusbar2");
589 window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
591 g_signal_connect (window->buffer, "changed",
592 G_CALLBACK (on_text_changed), window);
594 g_signal_connect (window->buffer, "modified-changed",
595 G_CALLBACK (on_modified_changed), window);
597 g_signal_connect_swapped (clip_primary, "owner-change",
598 G_CALLBACK (selection_changed), window);
600 g_signal_connect (clip_selection, "owner-change",
601 G_CALLBACK (on_owner_change), window);
605 gtk_container_add (GTK_CONTAINER (window), box);
607 g_object_ref (menubar);
611 g_object_ref (window->sb);
613 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
614 gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
615 gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
617 gtk_widget_show_all (box);
619 g_signal_connect_swapped (get_action_assert (xml,"file_new_syntax"), "activate", G_CALLBACK (create_syntax_window), NULL);
622 g_signal_connect (get_action_assert (xml,"file_new_data"),
624 G_CALLBACK (create_data_window),
628 g_signal_connect_swapped (get_action_assert (xml, "file_save"),
630 G_CALLBACK (syntax_save),
633 g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
635 G_CALLBACK (syntax_save_as),
638 g_signal_connect (get_action_assert (xml,"file_quit"),
640 G_CALLBACK (on_quit),
643 g_signal_connect_swapped (window->edit_delete,
645 G_CALLBACK (on_edit_delete),
648 g_signal_connect_swapped (window->edit_copy,
650 G_CALLBACK (on_edit_copy),
653 g_signal_connect_swapped (window->edit_cut,
655 G_CALLBACK (on_edit_cut),
658 g_signal_connect_swapped (window->edit_paste,
660 G_CALLBACK (on_edit_paste),
663 g_signal_connect (get_action_assert (xml,"run_all"),
665 G_CALLBACK (on_run_all),
668 g_signal_connect (get_action_assert (xml,"run_selection"),
670 G_CALLBACK (on_run_selection),
673 g_signal_connect (get_action_assert (xml,"run_current_line"),
675 G_CALLBACK (on_run_current_line),
678 g_signal_connect (get_action_assert (xml,"run_to_end"),
680 G_CALLBACK (on_run_to_end),
683 g_signal_connect (get_action_assert (xml,"windows_minimise_all"),
685 G_CALLBACK (psppire_window_minimise_all), NULL);
689 GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
691 merge_help_menu (uim);
693 PSPPIRE_WINDOW (window)->menu =
694 GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
697 g_object_unref (xml);
705 psppire_syntax_window_new (void)
707 return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
708 /* TRANSLATORS: This will form a filename. Please avoid whitespace. */
709 "filename", _("Syntax"),
710 "description", _("Syntax Editor"),
715 error_dialog (GtkWindow *w, const gchar *filename, GError *err)
717 gchar *fn = g_filename_display_basename (filename);
720 gtk_message_dialog_new (w,
721 GTK_DIALOG_DESTROY_WITH_PARENT,
724 _("Cannot load syntax file '%s'"),
729 g_object_set (dialog, "icon-name", "psppicon", NULL);
731 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
734 gtk_dialog_run (GTK_DIALOG (dialog));
736 gtk_widget_destroy (dialog);
740 Loads the buffer from the file called FILENAME
743 syntax_load (PsppireWindow *window, const gchar *filename)
746 gchar *text_locale = NULL;
747 gchar *text_utf8 = NULL;
748 gsize len_locale = -1;
751 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
753 /* FIXME: What if it's a very big file ? */
754 if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
756 error_dialog (GTK_WINDOW (window), filename, err);
757 g_clear_error (&err);
761 text_utf8 = g_locale_to_utf8 (text_locale, len_locale, NULL, &len_utf8, &err);
765 if ( text_utf8 == NULL )
767 error_dialog (GTK_WINDOW (window), filename, err);
768 g_clear_error (&err);
772 gtk_text_buffer_get_iter_at_line (sw->buffer, &iter, 0);
774 gtk_text_buffer_insert (sw->buffer, &iter, text_utf8, len_utf8);
776 gtk_text_buffer_set_modified (sw->buffer, FALSE);
786 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
788 iface->save = syntax_save;
789 iface->load = syntax_load;