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);
115 const gchar **new_paths = g_strdupv (existing_paths);
117 int n = g_strv_length (existing_paths);
119 new_paths = g_realloc (new_paths, (n+1) * sizeof (*new_paths));
121 new_paths[n] = g_strdup (relocate (PKGDATADIR));
122 new_paths[n+1] = NULL;
124 lm = gtk_source_language_manager_new ();
125 gtk_source_language_manager_set_search_path (lm, new_paths);
127 class->lan = gtk_source_language_manager_get_language (lm, "pspp");
129 if (class->lan == NULL)
130 g_warning ("pspp.lang file not found. Syntax highlighting will not be available.");
132 parent_class = g_type_class_peek_parent (class);
137 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
139 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 const gchar *name = psppire_window_get_filename (win);
158 execute_syntax (create_syntax_editor_source (GTK_TEXT_BUFFER (sw->buffer), start, stop, name));
162 /* Parse and execute all the text in the buffer */
164 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
166 GtkTextIter begin, end;
167 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
169 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &begin, 0);
170 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &end, -1);
172 editor_execute_syntax (se, begin, end);
175 /* Parse and execute the currently selected text */
177 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
179 GtkTextIter begin, end;
180 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
182 if ( gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (se->buffer), &begin, &end) )
183 editor_execute_syntax (se, begin, end);
187 /* Parse and execute the from the current line, to the end of the
190 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
192 GtkTextIter begin, end;
196 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
198 /* Get the current line */
199 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
201 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
204 line = gtk_text_iter_get_line (&here) ;
206 /* Now set begin and end to the start of this line, and end of buffer
208 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
209 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, -1);
211 editor_execute_syntax (se, begin, end);
216 /* Parse and execute the current line */
218 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
220 GtkTextIter begin, end;
224 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
226 /* Get the current line */
227 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
229 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
232 line = gtk_text_iter_get_line (&here) ;
234 /* Now set begin and end to the start of this line, and start of
235 following line respectively */
236 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
237 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, line + 1);
239 editor_execute_syntax (se, begin, end);
244 /* Append ".sps" to FILENAME if necessary.
245 The returned result must be freed when no longer required.
248 append_suffix (const gchar *filename)
250 if ( ! g_str_has_suffix (filename, ".sps" ) &&
251 ! g_str_has_suffix (filename, ".SPS" ) )
253 return g_strdup_printf ("%s.sps", filename);
256 return xstrdup (filename);
260 Save BUFFER to the file called FILENAME.
261 FILENAME must be encoded in Glib filename encoding.
262 If successful, clears the buffer's modified flag.
265 save_editor_to_file (PsppireSyntaxWindow *se,
266 const gchar *filename,
269 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (se->buffer);
271 GtkTextIter start, stop;
277 suffixedname = append_suffix (filename);
279 gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
280 gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
282 text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
284 result = g_file_set_contents (suffixedname, text, -1, err);
286 g_free (suffixedname);
290 char *fn = g_filename_display_name (filename);
291 gchar *msg = g_strdup_printf (_("Saved file \"%s\""), fn);
293 gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
294 gtk_text_buffer_set_modified (buffer, FALSE);
302 /* Callback for the File->SaveAs menuitem */
304 syntax_save_as (PsppireWindow *se)
306 GtkFileFilter *filter;
310 gtk_file_chooser_dialog_new (_("Save Syntax"),
312 GTK_FILE_CHOOSER_ACTION_SAVE,
313 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
314 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
317 filter = gtk_file_filter_new ();
318 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
319 gtk_file_filter_add_pattern (filter, "*.sps");
320 gtk_file_filter_add_pattern (filter, "*.SPS");
321 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
323 filter = gtk_file_filter_new ();
324 gtk_file_filter_set_name (filter, _("All Files"));
325 gtk_file_filter_add_pattern (filter, "*");
326 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
328 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
330 response = gtk_dialog_run (GTK_DIALOG (dialog));
332 if ( response == GTK_RESPONSE_ACCEPT )
336 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
338 if ( ! save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err) )
340 msg ( ME, "%s", err->message );
347 gtk_widget_destroy (dialog);
351 /* Callback for the File->Save menuitem */
353 syntax_save (PsppireWindow *se)
355 const gchar *filename = psppire_window_get_filename (se);
357 if ( filename == NULL )
362 save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
365 msg (ME, "%s", err->message);
372 /* Callback for the File->Quit menuitem */
374 on_quit (GtkMenuItem *menuitem, gpointer user_data)
383 load_and_show_syntax_window (GtkWidget *se, const gchar *filename)
387 gtk_source_buffer_begin_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
388 ok = psppire_window_load (PSPPIRE_WINDOW (se), filename);
389 gtk_source_buffer_end_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
392 gtk_widget_show (se);
394 gtk_widget_destroy (se);
398 create_syntax_window (void)
400 GtkWidget *w = psppire_syntax_window_new ();
405 open_new_syntax_window (const char *file_name)
407 GtkWidget *se = psppire_syntax_window_new ();
410 load_and_show_syntax_window (se, file_name);
415 static void psppire_syntax_window_print (PsppireSyntaxWindow *window);
418 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
420 if (gtk_text_buffer_get_modified (buffer))
421 psppire_window_set_unsaved (window);
424 extern struct source_stream *the_source_stream ;
426 static void undo_redo_update (PsppireSyntaxWindow *window);
427 static void undo_last_edit (PsppireSyntaxWindow *window);
428 static void redo_last_edit (PsppireSyntaxWindow *window);
431 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
433 gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
434 undo_redo_update (window);
438 psppire_syntax_window_init (PsppireSyntaxWindow *window)
440 GtkBuilder *xml = builder_new ("syntax-editor.ui");
441 GtkWidget *box = gtk_vbox_new (FALSE, 0);
443 GtkWidget *menubar = get_widget_assert (xml, "menubar");
444 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
446 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
448 PsppireSyntaxWindowClass *class
449 = PSPPIRE_SYNTAX_WINDOW_CLASS (G_OBJECT_GET_CLASS (window));
451 window->print_settings = NULL;
452 window->undo_menuitem = get_action_assert (xml, "edit_undo");
453 window->redo_menuitem = get_action_assert (xml, "edit_redo");
456 window->buffer = gtk_source_buffer_new_with_language (class->lan);
458 window->buffer = gtk_source_buffer_new (NULL);
460 gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), GTK_TEXT_BUFFER (window->buffer));
462 g_object_set (window->buffer,
463 "highlight-matching-brackets", TRUE,
467 g_object_set (text_view,
468 "show-line-numbers", TRUE,
469 "show-line-marks", TRUE,
472 "highlight-current-line", TRUE,
474 window->lexer = lex_create (the_source_stream);
476 window->sb = get_widget_assert (xml, "statusbar2");
477 window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
479 g_signal_connect (window->buffer, "changed", G_CALLBACK (on_text_changed), window);
481 g_signal_connect (window->buffer, "modified-changed",
482 G_CALLBACK (on_modified_changed), window);
484 g_signal_connect_swapped (get_action_assert (xml, "file_print"), "activate",
485 G_CALLBACK (psppire_syntax_window_print), window);
488 g_signal_connect_swapped (window->undo_menuitem,
490 G_CALLBACK (undo_last_edit),
493 g_signal_connect_swapped (window->redo_menuitem,
495 G_CALLBACK (redo_last_edit),
498 undo_redo_update (window);
502 gtk_container_add (GTK_CONTAINER (window), box);
504 g_object_ref (menubar);
508 g_object_ref (window->sb);
511 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
512 gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
513 gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
515 gtk_widget_show_all (box);
517 g_signal_connect_swapped (get_action_assert (xml,"file_new_syntax"), "activate", G_CALLBACK (create_syntax_window), NULL);
520 g_signal_connect (get_action_assert (xml,"file_new_data"),
522 G_CALLBACK (create_data_window),
526 g_signal_connect_swapped (get_action_assert (xml, "file_save"),
528 G_CALLBACK (syntax_save),
531 g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
533 G_CALLBACK (syntax_save_as),
536 g_signal_connect (get_action_assert (xml,"file_quit"),
538 G_CALLBACK (on_quit),
541 g_signal_connect (get_action_assert (xml,"run_all"),
543 G_CALLBACK (on_run_all),
547 g_signal_connect (get_action_assert (xml,"run_selection"),
549 G_CALLBACK (on_run_selection),
552 g_signal_connect (get_action_assert (xml,"run_current_line"),
554 G_CALLBACK (on_run_current_line),
557 g_signal_connect (get_action_assert (xml,"run_to_end"),
559 G_CALLBACK (on_run_to_end),
562 g_signal_connect (get_action_assert (xml,"windows_minimise_all"),
564 G_CALLBACK (psppire_window_minimise_all), NULL);
571 GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
573 merge_help_menu (uim);
575 PSPPIRE_WINDOW (window)->menu =
576 GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
579 g_object_unref (xml);
584 psppire_syntax_window_new (void)
586 return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
587 "filename", "Syntax",
588 "description", _("Syntax Editor"),
593 error_dialog (GtkWindow *w, const gchar *filename, GError *err)
595 gchar *fn = g_filename_display_basename (filename);
598 gtk_message_dialog_new (w,
599 GTK_DIALOG_DESTROY_WITH_PARENT,
602 _("Cannot load syntax file '%s'"),
607 g_object_set (dialog, "icon-name", "psppicon", NULL);
609 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
612 gtk_dialog_run (GTK_DIALOG (dialog));
614 gtk_widget_destroy (dialog);
618 Loads the buffer from the file called FILENAME
621 syntax_load (PsppireWindow *window, const gchar *filename)
624 gchar *text_locale = NULL;
625 gchar *text_utf8 = NULL;
626 gsize len_locale = -1;
629 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
630 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
631 /* FIXME: What if it's a very big file ? */
632 if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
634 error_dialog (GTK_WINDOW (window), filename, err);
635 g_clear_error (&err);
639 text_utf8 = g_locale_to_utf8 (text_locale, len_locale, NULL, &len_utf8, &err);
643 if ( text_utf8 == NULL )
645 error_dialog (GTK_WINDOW (window), filename, err);
646 g_clear_error (&err);
650 gtk_text_buffer_get_iter_at_line (buffer, &iter, 0);
652 gtk_text_buffer_insert (buffer, &iter, text_utf8, len_utf8);
654 gtk_text_buffer_set_modified (buffer, FALSE);
664 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
666 iface->save = syntax_save;
667 iface->load = syntax_load;
673 undo_redo_update (PsppireSyntaxWindow *window)
675 gtk_action_set_sensitive (window->undo_menuitem,
676 gtk_source_buffer_can_undo (window->buffer));
678 gtk_action_set_sensitive (window->redo_menuitem,
679 gtk_source_buffer_can_redo (window->buffer));
683 undo_last_edit (PsppireSyntaxWindow *window)
685 gtk_source_buffer_undo (window->buffer);
686 undo_redo_update (window);
690 redo_last_edit (PsppireSyntaxWindow *window)
692 gtk_source_buffer_redo (window->buffer);
693 undo_redo_update (window);
698 /* Printing related stuff */
702 begin_print (GtkPrintOperation *operation,
703 GtkPrintContext *context,
704 PsppireSyntaxWindow *window)
707 gtk_source_print_compositor_new (window->buffer);
712 end_print (GtkPrintOperation *operation,
713 GtkPrintContext *context,
714 PsppireSyntaxWindow *window)
716 g_object_unref (window->compositor);
717 window->compositor = NULL;
723 paginate (GtkPrintOperation *operation,
724 GtkPrintContext *context,
725 PsppireSyntaxWindow *window)
727 if (gtk_source_print_compositor_paginate (window->compositor, context))
729 gint n_pages = gtk_source_print_compositor_get_n_pages (window->compositor);
730 gtk_print_operation_set_n_pages (operation, n_pages);
739 draw_page (GtkPrintOperation *operation,
740 GtkPrintContext *context,
742 PsppireSyntaxWindow *window)
744 gtk_source_print_compositor_draw_page (window->compositor,
752 psppire_syntax_window_print (PsppireSyntaxWindow *window)
754 GtkPrintOperationResult res;
756 GtkPrintOperation *print = gtk_print_operation_new ();
758 if (window->print_settings != NULL)
759 gtk_print_operation_set_print_settings (print, window->print_settings);
762 g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
763 g_signal_connect (print, "end_print", G_CALLBACK (end_print), window);
764 g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), window);
765 g_signal_connect (print, "paginate", G_CALLBACK (paginate), window);
767 res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
768 GTK_WINDOW (window), NULL);
770 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
772 if (window->print_settings != NULL)
773 g_object_unref (window->print_settings);
774 window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
777 g_object_unref (print);