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 <gtksourceview/gtksourcebuffer.h>
25 #include <gtksourceview/gtksourcelanguage.h>
26 #include <gtksourceview/gtksourcelanguagemanager.h>
27 #include <gtksourceview/gtksourceprintcompositor.h>
29 #include <libpspp/message.h>
34 #include "psppire-data-window.h"
35 #include "psppire-window-register.h"
37 #include "help-menu.h"
38 #include "psppire-syntax-window.h"
39 #include "syntax-editor-source.h"
40 #include <language/lexer/lexer.h>
45 #define _(msgid) gettext (msgid)
46 #define N_(msgid) msgid
48 static void psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *, gpointer);
49 static void psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class);
50 static void psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class);
51 static void psppire_syntax_window_init (PsppireSyntaxWindow *syntax_editor);
54 static void psppire_syntax_window_iface_init (PsppireWindowIface *iface);
58 psppire_syntax_window_get_type (void)
60 static GType psppire_syntax_window_type = 0;
62 if (!psppire_syntax_window_type)
64 static const GTypeInfo psppire_syntax_window_info =
66 sizeof (PsppireSyntaxWindowClass),
67 (GBaseInitFunc) psppire_syntax_window_base_init,
68 (GBaseFinalizeFunc) psppire_syntax_window_base_finalize,
69 (GClassInitFunc)psppire_syntax_window_class_init,
70 (GClassFinalizeFunc) NULL,
72 sizeof (PsppireSyntaxWindow),
74 (GInstanceInitFunc) psppire_syntax_window_init,
77 static const GInterfaceInfo window_interface_info =
79 (GInterfaceInitFunc) psppire_syntax_window_iface_init,
84 psppire_syntax_window_type =
85 g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireSyntaxWindow",
86 &psppire_syntax_window_info, 0);
88 g_type_add_interface_static (psppire_syntax_window_type,
89 PSPPIRE_TYPE_WINDOW_MODEL,
90 &window_interface_info);
93 return psppire_syntax_window_type;
96 static GObjectClass *parent_class ;
99 psppire_syntax_window_finalize (GObject *object)
101 if (G_OBJECT_CLASS (parent_class)->finalize)
102 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
107 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
109 GtkSourceLanguageManager *lm = gtk_source_language_manager_get_default ();
111 const gchar * const *existing_paths = gtk_source_language_manager_get_search_path (lm);
113 const gchar **new_paths = g_strdupv (existing_paths);
115 int n = g_strv_length (existing_paths);
117 new_paths = g_realloc (new_paths, (n+1) * sizeof (*new_paths));
118 new_paths[n] = g_strdup (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);
134 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
136 GObjectClass *object_class = G_OBJECT_CLASS (class);
137 object_class->finalize = psppire_syntax_window_finalize;
143 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
150 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
153 PsppireWindow *win = PSPPIRE_WINDOW (sw);
154 const gchar *name = psppire_window_get_filename (win);
155 execute_syntax (create_syntax_editor_source (GTK_TEXT_BUFFER (sw->buffer), start, stop, name));
159 /* Parse and execute all the text in the buffer */
161 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
163 GtkTextIter begin, end;
164 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
166 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &begin, 0);
167 gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &end, -1);
169 editor_execute_syntax (se, begin, end);
172 /* Parse and execute the currently selected text */
174 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
176 GtkTextIter begin, end;
177 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
179 if ( gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (se->buffer), &begin, &end) )
180 editor_execute_syntax (se, begin, end);
184 /* Parse and execute the from the current line, to the end of the
187 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
189 GtkTextIter begin, end;
193 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
195 /* Get the current line */
196 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
198 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
201 line = gtk_text_iter_get_line (&here) ;
203 /* Now set begin and end to the start of this line, and end of buffer
205 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
206 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, -1);
208 editor_execute_syntax (se, begin, end);
213 /* Parse and execute the current line */
215 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
217 GtkTextIter begin, end;
221 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
223 /* Get the current line */
224 gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
226 gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
229 line = gtk_text_iter_get_line (&here) ;
231 /* Now set begin and end to the start of this line, and start of
232 following line respectively */
233 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
234 gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, line + 1);
236 editor_execute_syntax (se, begin, end);
241 /* Append ".sps" to FILENAME if necessary.
242 The returned result must be freed when no longer required.
245 append_suffix (const gchar *filename)
247 if ( ! g_str_has_suffix (filename, ".sps" ) &&
248 ! g_str_has_suffix (filename, ".SPS" ) )
250 return g_strdup_printf ("%s.sps", filename);
253 return xstrdup (filename);
257 Save BUFFER to the file called FILENAME.
258 FILENAME must be encoded in Glib filename encoding.
259 If successful, clears the buffer's modified flag.
262 save_editor_to_file (PsppireSyntaxWindow *se,
263 const gchar *filename,
266 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (se->buffer);
268 GtkTextIter start, stop;
274 suffixedname = append_suffix (filename);
276 gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
277 gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
279 text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
281 result = g_file_set_contents (suffixedname, text, -1, err);
283 g_free (suffixedname);
287 char *fn = g_filename_display_name (filename);
288 gchar *msg = g_strdup_printf (_("Saved file \"%s\""), fn);
290 gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
291 gtk_text_buffer_set_modified (buffer, FALSE);
299 /* Callback for the File->SaveAs menuitem */
301 syntax_save_as (PsppireWindow *se)
303 GtkFileFilter *filter;
307 gtk_file_chooser_dialog_new (_("Save Syntax"),
309 GTK_FILE_CHOOSER_ACTION_SAVE,
310 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
311 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
314 filter = gtk_file_filter_new ();
315 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
316 gtk_file_filter_add_pattern (filter, "*.sps");
317 gtk_file_filter_add_pattern (filter, "*.SPS");
318 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
320 filter = gtk_file_filter_new ();
321 gtk_file_filter_set_name (filter, _("All Files"));
322 gtk_file_filter_add_pattern (filter, "*");
323 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
325 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
327 response = gtk_dialog_run (GTK_DIALOG (dialog));
329 if ( response == GTK_RESPONSE_ACCEPT )
333 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
335 if ( ! save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err) )
337 msg ( ME, "%s", err->message );
344 gtk_widget_destroy (dialog);
348 /* Callback for the File->Save menuitem */
350 syntax_save (PsppireWindow *se)
352 const gchar *filename = psppire_window_get_filename (se);
354 if ( filename == NULL )
359 save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
362 msg (ME, "%s", err->message);
369 /* Callback for the File->Quit menuitem */
371 on_quit (GtkMenuItem *menuitem, gpointer user_data)
380 create_syntax_window (void)
382 GtkWidget *w = psppire_syntax_window_new ();
387 open_syntax_window (const char *file_name)
389 GtkWidget *se = psppire_syntax_window_new ();
391 if ( psppire_window_load (PSPPIRE_WINDOW (se), file_name) )
392 gtk_widget_show (se);
394 gtk_widget_destroy (se);
398 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
400 gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
403 static void psppire_syntax_window_print (PsppireSyntaxWindow *window);
406 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
408 if (gtk_text_buffer_get_modified (buffer))
409 psppire_window_set_unsaved (window);
412 extern struct source_stream *the_source_stream ;
415 psppire_syntax_window_init (PsppireSyntaxWindow *window)
417 GtkBuilder *xml = builder_new ("syntax-editor.ui");
418 GtkWidget *box = gtk_vbox_new (FALSE, 0);
420 GtkWidget *menubar = get_widget_assert (xml, "menubar");
421 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
423 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
425 PsppireSyntaxWindowClass *class
426 = PSPPIRE_SYNTAX_WINDOW_CLASS (G_OBJECT_GET_CLASS (window));
428 window->print_settings = NULL;
431 window->buffer = gtk_source_buffer_new_with_language (class->lan);
433 window->buffer = gtk_source_buffer_new (NULL);
435 gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), GTK_TEXT_BUFFER (window->buffer));
437 g_object_set (window->buffer,
438 "highlight-matching-brackets", TRUE,
442 g_object_set (text_view,
443 "show-line-numbers", TRUE,
444 "show-line-marks", TRUE,
447 "highlight-current-line", TRUE,
449 window->lexer = lex_create (the_source_stream);
451 window->sb = get_widget_assert (xml, "statusbar2");
452 window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
454 g_signal_connect (window->buffer, "changed", G_CALLBACK (on_text_changed), window);
456 g_signal_connect (window->buffer, "modified-changed",
457 G_CALLBACK (on_modified_changed), window);
459 g_signal_connect_swapped (get_action_assert (xml, "file_print"), "activate",
460 G_CALLBACK (psppire_syntax_window_print), window);
464 gtk_container_add (GTK_CONTAINER (window), box);
466 g_object_ref (menubar);
470 g_object_ref (window->sb);
473 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
474 gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
475 gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
477 gtk_widget_show_all (box);
479 g_signal_connect_swapped (get_action_assert (xml,"file_new_syntax"), "activate", G_CALLBACK (create_syntax_window), NULL);
482 g_signal_connect (get_action_assert (xml,"file_new_data"),
484 G_CALLBACK (create_data_window),
488 g_signal_connect_swapped (get_action_assert (xml, "file_save"),
490 G_CALLBACK (syntax_save),
493 g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
495 G_CALLBACK (syntax_save_as),
498 g_signal_connect (get_action_assert (xml,"file_quit"),
500 G_CALLBACK (on_quit),
503 g_signal_connect (get_action_assert (xml,"run_all"),
505 G_CALLBACK (on_run_all),
509 g_signal_connect (get_action_assert (xml,"run_selection"),
511 G_CALLBACK (on_run_selection),
514 g_signal_connect (get_action_assert (xml,"run_current_line"),
516 G_CALLBACK (on_run_current_line),
519 g_signal_connect (get_action_assert (xml,"run_to_end"),
521 G_CALLBACK (on_run_to_end),
524 g_signal_connect (get_action_assert (xml,"windows_minimise_all"),
526 G_CALLBACK (psppire_window_minimise_all), NULL);
533 GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
535 merge_help_menu (uim);
537 PSPPIRE_WINDOW (window)->menu =
538 GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
541 g_object_unref (xml);
546 psppire_syntax_window_new (void)
548 return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
549 "filename", "Syntax",
550 "description", _("Syntax Editor"),
555 error_dialog (GtkWindow *w, const gchar *filename, GError *err)
557 gchar *fn = g_filename_display_basename (filename);
560 gtk_message_dialog_new (w,
561 GTK_DIALOG_DESTROY_WITH_PARENT,
564 _("Cannot load syntax file '%s'"),
569 g_object_set (dialog, "icon-name", "psppicon", NULL);
571 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
574 gtk_dialog_run (GTK_DIALOG (dialog));
576 gtk_widget_destroy (dialog);
580 Loads the buffer from the file called FILENAME
583 syntax_load (PsppireWindow *window, const gchar *filename)
586 gchar *text_locale = NULL;
587 gchar *text_utf8 = NULL;
588 gsize len_locale = -1;
591 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
592 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
593 /* FIXME: What if it's a very big file ? */
594 if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
596 error_dialog (GTK_WINDOW (window), filename, err);
597 g_clear_error (&err);
601 text_utf8 = g_locale_to_utf8 (text_locale, len_locale, NULL, &len_utf8, &err);
605 if ( text_utf8 == NULL )
607 error_dialog (GTK_WINDOW (window), filename, err);
608 g_clear_error (&err);
612 gtk_text_buffer_get_iter_at_line (buffer, &iter, 0);
614 gtk_text_buffer_insert (buffer, &iter, text_utf8, len_utf8);
616 gtk_text_buffer_set_modified (buffer, FALSE);
626 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
628 iface->save = syntax_save;
629 iface->load = syntax_load;
634 /* Printing related stuff */
638 begin_print (GtkPrintOperation *operation,
639 GtkPrintContext *context,
640 PsppireSyntaxWindow *window)
643 gtk_source_print_compositor_new (window->buffer);
648 end_print (GtkPrintOperation *operation,
649 GtkPrintContext *context,
650 PsppireSyntaxWindow *window)
652 g_object_unref (window->compositor);
653 window->compositor = NULL;
659 paginate (GtkPrintOperation *operation,
660 GtkPrintContext *context,
661 PsppireSyntaxWindow *window)
663 if (gtk_source_print_compositor_paginate (window->compositor, context))
665 gint n_pages = gtk_source_print_compositor_get_n_pages (window->compositor);
666 gtk_print_operation_set_n_pages (operation, n_pages);
675 draw_page (GtkPrintOperation *operation,
676 GtkPrintContext *context,
678 PsppireSyntaxWindow *window)
680 gtk_source_print_compositor_draw_page (window->compositor,
688 psppire_syntax_window_print (PsppireSyntaxWindow *window)
690 GtkPrintOperationResult res;
692 GtkPrintOperation *print = gtk_print_operation_new ();
694 if (window->print_settings != NULL)
695 gtk_print_operation_set_print_settings (print, window->print_settings);
698 g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
699 g_signal_connect (print, "end_print", G_CALLBACK (end_print), window);
700 g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), window);
701 g_signal_connect (print, "paginate", G_CALLBACK (paginate), window);
703 res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
704 GTK_WINDOW (window), NULL);
706 if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
708 if (window->print_settings != NULL)
709 g_object_unref (window->print_settings);
710 window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
713 g_object_unref (print);