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 "relocatable.h"
21 #include <gtk/gtksignal.h>
22 #include <gtk/gtkbox.h>
26 #include <gtksourceview/gtksourcebuffer.h>
27 #include <gtksourceview/gtksourcelanguage.h>
28 #include <gtksourceview/gtksourcelanguagemanager.h>
29 #include <gtksourceview/gtksourceprintcompositor.h>
31 #include <libpspp/message.h>
36 #include "psppire-data-window.h"
37 #include "psppire-window-register.h"
39 #include "help-menu.h"
40 #include "psppire-syntax-window.h"
41 #include "syntax-editor-source.h"
42 #include <language/lexer/lexer.h>
47 #define _(msgid) gettext (msgid)
48 #define N_(msgid) msgid
50 static void psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *, gpointer);
51 static void psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class);
52 static void psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class);
53 static void psppire_syntax_window_init (PsppireSyntaxWindow *syntax_editor);
56 static void psppire_syntax_window_iface_init (PsppireWindowIface *iface);
60 psppire_syntax_window_get_type (void)
62 static GType psppire_syntax_window_type = 0;
64 if (!psppire_syntax_window_type)
66 static const GTypeInfo psppire_syntax_window_info =
68 sizeof (PsppireSyntaxWindowClass),
69 (GBaseInitFunc) psppire_syntax_window_base_init,
70 (GBaseFinalizeFunc) psppire_syntax_window_base_finalize,
71 (GClassInitFunc)psppire_syntax_window_class_init,
72 (GClassFinalizeFunc) NULL,
74 sizeof (PsppireSyntaxWindow),
76 (GInstanceInitFunc) psppire_syntax_window_init,
79 static const GInterfaceInfo window_interface_info =
81 (GInterfaceInitFunc) psppire_syntax_window_iface_init,
86 psppire_syntax_window_type =
87 g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireSyntaxWindow",
88 &psppire_syntax_window_info, 0);
90 g_type_add_interface_static (psppire_syntax_window_type,
91 PSPPIRE_TYPE_WINDOW_MODEL,
92 &window_interface_info);
95 return psppire_syntax_window_type;
98 static GObjectClass *parent_class ;
101 psppire_syntax_window_finalize (GObject *object)
103 if (G_OBJECT_CLASS (parent_class)->finalize)
104 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
109 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
111 GtkSourceLanguageManager *lm = gtk_source_language_manager_get_default ();
113 const gchar * const *existing_paths = gtk_source_language_manager_get_search_path (lm);
114 gchar **new_paths = g_strdupv ((gchar **)existing_paths);
115 int n = g_strv_length ((gchar **) existing_paths);
117 new_paths = g_realloc (new_paths, (n + 2) * sizeof (*new_paths));
118 new_paths[n] = g_strdup (relocate (PKGDATADIR));
119 new_paths[n+1] = NULL;
121 lm = gtk_source_language_manager_new ();
122 gtk_source_language_manager_set_search_path (lm, new_paths);
124 class->lan = gtk_source_language_manager_get_language (lm, "pspp");
126 if (class->lan == NULL)
127 g_warning ("pspp.lang file not found. Syntax highlighting will not be available.");
129 parent_class = g_type_class_peek_parent (class);
131 g_strfreev (new_paths);
136 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
138 GObjectClass *object_class = G_OBJECT_CLASS (class);
139 object_class->finalize = psppire_syntax_window_finalize;
145 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
152 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
155 PsppireWindow *win = PSPPIRE_WINDOW (sw);
156 const gchar *name = psppire_window_get_filename (win);
157 execute_syntax (create_syntax_editor_source (GTK_TEXT_BUFFER (sw->buffer), start, stop, name));
161 /* Parse and execute all the text in the buffer */
163 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
165 GtkTextIter begin, end;
166 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
168 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &begin, 0);
169 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &end, -1);
171 editor_execute_syntax (se, begin, end);
174 /* Parse and execute the currently selected text */
176 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
178 GtkTextIter begin, end;
179 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
181 if ( gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (se->buffer), &begin, &end) )
182 editor_execute_syntax (se, begin, end);
186 /* Parse and execute the from the current line, to the end of the
189 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
191 GtkTextIter begin, end;
195 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
197 /* Get the current line */
198 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
200 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
203 line = gtk_text_iter_get_line (&here) ;
205 /* Now set begin and end to the start of this line, and end of buffer
207 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
208 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, -1);
210 editor_execute_syntax (se, begin, end);
215 /* Parse and execute the current line */
217 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
219 GtkTextIter begin, end;
223 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
225 /* Get the current line */
226 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
228 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
231 line = gtk_text_iter_get_line (&here) ;
233 /* Now set begin and end to the start of this line, and start of
234 following line respectively */
235 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
236 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, line + 1);
238 editor_execute_syntax (se, begin, end);
243 /* Append ".sps" to FILENAME if necessary.
244 The returned result must be freed when no longer required.
247 append_suffix (const gchar *filename)
249 if ( ! g_str_has_suffix (filename, ".sps" ) &&
250 ! g_str_has_suffix (filename, ".SPS" ) )
252 return g_strdup_printf ("%s.sps", filename);
255 return xstrdup (filename);
259 Save BUFFER to the file called FILENAME.
260 FILENAME must be encoded in Glib filename encoding.
261 If successful, clears the buffer's modified flag.
264 save_editor_to_file (PsppireSyntaxWindow *se,
265 const gchar *filename,
268 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (se->buffer);
270 GtkTextIter start, stop;
276 suffixedname = append_suffix (filename);
278 gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
279 gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
281 text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
283 result = g_file_set_contents (suffixedname, text, -1, err);
285 g_free (suffixedname);
289 char *fn = g_filename_display_name (filename);
290 gchar *msg = g_strdup_printf (_("Saved file \"%s\""), fn);
292 gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
293 gtk_text_buffer_set_modified (buffer, FALSE);
301 /* Callback for the File->SaveAs menuitem */
303 syntax_save_as (PsppireWindow *se)
305 GtkFileFilter *filter;
309 gtk_file_chooser_dialog_new (_("Save Syntax"),
311 GTK_FILE_CHOOSER_ACTION_SAVE,
312 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
313 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
316 filter = gtk_file_filter_new ();
317 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
318 gtk_file_filter_add_pattern (filter, "*.sps");
319 gtk_file_filter_add_pattern (filter, "*.SPS");
320 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
322 filter = gtk_file_filter_new ();
323 gtk_file_filter_set_name (filter, _("All Files"));
324 gtk_file_filter_add_pattern (filter, "*");
325 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
327 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
329 response = gtk_dialog_run (GTK_DIALOG (dialog));
331 if ( response == GTK_RESPONSE_ACCEPT )
335 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
337 if ( ! save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err) )
339 msg ( ME, "%s", err->message );
346 gtk_widget_destroy (dialog);
350 /* Callback for the File->Save menuitem */
352 syntax_save (PsppireWindow *se)
354 const gchar *filename = psppire_window_get_filename (se);
356 if ( filename == NULL )
361 save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
364 msg (ME, "%s", err->message);
371 /* Callback for the File->Quit menuitem */
373 on_quit (GtkMenuItem *menuitem, gpointer user_data)
382 load_and_show_syntax_window (GtkWidget *se, const gchar *filename)
386 gtk_source_buffer_begin_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
387 ok = psppire_window_load (PSPPIRE_WINDOW (se), filename);
388 gtk_source_buffer_end_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
391 gtk_widget_show (se);
393 gtk_widget_destroy (se);
397 create_syntax_window (void)
399 GtkWidget *w = psppire_syntax_window_new ();
404 open_new_syntax_window (const char *file_name)
406 GtkWidget *se = psppire_syntax_window_new ();
409 load_and_show_syntax_window (se, file_name);
414 static void psppire_syntax_window_print (PsppireSyntaxWindow *window);
417 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
419 if (gtk_text_buffer_get_modified (buffer))
420 psppire_window_set_unsaved (window);
423 extern struct source_stream *the_source_stream ;
425 static void undo_redo_update (PsppireSyntaxWindow *window);
426 static void undo_last_edit (PsppireSyntaxWindow *window);
427 static void redo_last_edit (PsppireSyntaxWindow *window);
430 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
432 gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
433 undo_redo_update (window);
437 psppire_syntax_window_init (PsppireSyntaxWindow *window)
439 GtkBuilder *xml = builder_new ("syntax-editor.ui");
440 GtkWidget *box = gtk_vbox_new (FALSE, 0);
442 GtkWidget *menubar = get_widget_assert (xml, "menubar");
443 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
445 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
447 PsppireSyntaxWindowClass *class
448 = PSPPIRE_SYNTAX_WINDOW_CLASS (G_OBJECT_GET_CLASS (window));
450 window->print_settings = NULL;
451 window->undo_menuitem = get_action_assert (xml, "edit_undo");
452 window->redo_menuitem = get_action_assert (xml, "edit_redo");
455 window->buffer = gtk_source_buffer_new_with_language (class->lan);
457 window->buffer = gtk_source_buffer_new (NULL);
459 gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), GTK_TEXT_BUFFER (window->buffer));
461 g_object_set (window->buffer,
462 "highlight-matching-brackets", TRUE,
466 g_object_set (text_view,
467 "show-line-numbers", TRUE,
468 "show-line-marks", TRUE,
471 "highlight-current-line", TRUE,
473 window->lexer = lex_create (the_source_stream);
475 window->sb = get_widget_assert (xml, "statusbar2");
476 window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
478 g_signal_connect (window->buffer, "changed", G_CALLBACK (on_text_changed), window);
480 g_signal_connect (window->buffer, "modified-changed",
481 G_CALLBACK (on_modified_changed), window);
483 g_signal_connect_swapped (get_action_assert (xml, "file_print"), "activate",
484 G_CALLBACK (psppire_syntax_window_print), window);
487 g_signal_connect_swapped (window->undo_menuitem,
489 G_CALLBACK (undo_last_edit),
492 g_signal_connect_swapped (window->redo_menuitem,
494 G_CALLBACK (redo_last_edit),
497 undo_redo_update (window);
501 gtk_container_add (GTK_CONTAINER (window), box);
503 g_object_ref (menubar);
507 g_object_ref (window->sb);
510 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
511 gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
512 gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
514 gtk_widget_show_all (box);
516 g_signal_connect_swapped (get_action_assert (xml,"file_new_syntax"), "activate", G_CALLBACK (create_syntax_window), NULL);
519 g_signal_connect (get_action_assert (xml,"file_new_data"),
521 G_CALLBACK (create_data_window),
525 g_signal_connect_swapped (get_action_assert (xml, "file_save"),
527 G_CALLBACK (syntax_save),
530 g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
532 G_CALLBACK (syntax_save_as),
535 g_signal_connect (get_action_assert (xml,"file_quit"),
537 G_CALLBACK (on_quit),
540 g_signal_connect (get_action_assert (xml,"run_all"),
542 G_CALLBACK (on_run_all),
546 g_signal_connect (get_action_assert (xml,"run_selection"),
548 G_CALLBACK (on_run_selection),
551 g_signal_connect (get_action_assert (xml,"run_current_line"),
553 G_CALLBACK (on_run_current_line),
556 g_signal_connect (get_action_assert (xml,"run_to_end"),
558 G_CALLBACK (on_run_to_end),
561 g_signal_connect (get_action_assert (xml,"windows_minimise_all"),
563 G_CALLBACK (psppire_window_minimise_all), NULL);
570 GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
572 merge_help_menu (uim);
574 PSPPIRE_WINDOW (window)->menu =
575 GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
578 g_object_unref (xml);
583 psppire_syntax_window_new (void)
585 return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
586 "filename", "Syntax",
587 "description", _("Syntax Editor"),
592 error_dialog (GtkWindow *w, const gchar *filename, GError *err)
594 gchar *fn = g_filename_display_basename (filename);
597 gtk_message_dialog_new (w,
598 GTK_DIALOG_DESTROY_WITH_PARENT,
601 _("Cannot load syntax file '%s'"),
606 g_object_set (dialog, "icon-name", "psppicon", NULL);
608 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
611 gtk_dialog_run (GTK_DIALOG (dialog));
613 gtk_widget_destroy (dialog);
617 Loads the buffer from the file called FILENAME
620 syntax_load (PsppireWindow *window, const gchar *filename)
623 gchar *text_locale = NULL;
624 gchar *text_utf8 = NULL;
625 gsize len_locale = -1;
628 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
629 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
630 /* FIXME: What if it's a very big file ? */
631 if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
633 error_dialog (GTK_WINDOW (window), filename, err);
634 g_clear_error (&err);
638 text_utf8 = g_locale_to_utf8 (text_locale, len_locale, NULL, &len_utf8, &err);
642 if ( text_utf8 == NULL )
644 error_dialog (GTK_WINDOW (window), filename, err);
645 g_clear_error (&err);
649 gtk_text_buffer_get_iter_at_line (buffer, &iter, 0);
651 gtk_text_buffer_insert (buffer, &iter, text_utf8, len_utf8);
653 gtk_text_buffer_set_modified (buffer, FALSE);
663 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
665 iface->save = syntax_save;
666 iface->load = syntax_load;
672 undo_redo_update (PsppireSyntaxWindow *window)
674 gtk_action_set_sensitive (window->undo_menuitem,
675 gtk_source_buffer_can_undo (window->buffer));
677 gtk_action_set_sensitive (window->redo_menuitem,
678 gtk_source_buffer_can_redo (window->buffer));
682 undo_last_edit (PsppireSyntaxWindow *window)
684 gtk_source_buffer_undo (window->buffer);
685 undo_redo_update (window);
689 redo_last_edit (PsppireSyntaxWindow *window)
691 gtk_source_buffer_redo (window->buffer);
692 undo_redo_update (window);
697 /* Printing related stuff */
701 begin_print (GtkPrintOperation *operation,
702 GtkPrintContext *context,
703 PsppireSyntaxWindow *window)
706 gtk_source_print_compositor_new (window->buffer);
711 end_print (GtkPrintOperation *operation,
712 GtkPrintContext *context,
713 PsppireSyntaxWindow *window)
715 g_object_unref (window->compositor);
716 window->compositor = NULL;
722 paginate (GtkPrintOperation *operation,
723 GtkPrintContext *context,
724 PsppireSyntaxWindow *window)
726 if (gtk_source_print_compositor_paginate (window->compositor, context))
728 gint n_pages = gtk_source_print_compositor_get_n_pages (window->compositor);
729 gtk_print_operation_set_n_pages (operation, n_pages);
738 draw_page (GtkPrintOperation *operation,
739 GtkPrintContext *context,
741 PsppireSyntaxWindow *window)
743 gtk_source_print_compositor_draw_page (window->compositor,
751 psppire_syntax_window_print (PsppireSyntaxWindow *window)
753 GtkPrintOperationResult res;
755 GtkPrintOperation *print = gtk_print_operation_new ();
757 if (window->print_settings != NULL)
758 gtk_print_operation_set_print_settings (print, window->print_settings);
761 g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
762 g_signal_connect (print, "end_print", G_CALLBACK (end_print), window);
763 g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), window);
764 g_signal_connect (print, "paginate", G_CALLBACK (paginate), window);
766 res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
767 GTK_WINDOW (window), NULL);
769 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
771 if (window->print_settings != NULL)
772 g_object_unref (window->print_settings);
773 window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
776 g_object_unref (print);