1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008, 2009 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>
23 #include <libpspp/message.h>
27 #include "psppire-syntax-window.h"
29 #include "psppire-data-window.h"
30 #include "psppire-window-register.h"
33 #include "psppire-syntax-window.h"
34 #include "syntax-editor-source.h"
35 #include <language/lexer/lexer.h>
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) msgid
41 static void psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *, gpointer);
42 static void psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class);
43 static void psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class);
44 static void psppire_syntax_window_init (PsppireSyntaxWindow *syntax_editor);
47 static void psppire_syntax_window_iface_init (PsppireWindowIface *iface);
51 psppire_syntax_window_get_type (void)
53 static GType psppire_syntax_window_type = 0;
55 if (!psppire_syntax_window_type)
57 static const GTypeInfo psppire_syntax_window_info =
59 sizeof (PsppireSyntaxWindowClass),
60 (GBaseInitFunc) psppire_syntax_window_base_init,
61 (GBaseFinalizeFunc) psppire_syntax_window_base_finalize,
62 (GClassInitFunc)psppire_syntax_window_class_init,
63 (GClassFinalizeFunc) NULL,
65 sizeof (PsppireSyntaxWindow),
67 (GInstanceInitFunc) psppire_syntax_window_init,
70 static const GInterfaceInfo window_interface_info =
72 (GInterfaceInitFunc) psppire_syntax_window_iface_init,
77 psppire_syntax_window_type =
78 g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireSyntaxWindow",
79 &psppire_syntax_window_info, 0);
81 g_type_add_interface_static (psppire_syntax_window_type,
82 PSPPIRE_TYPE_WINDOW_MODEL,
83 &window_interface_info);
86 return psppire_syntax_window_type;
89 static GObjectClass *parent_class ;
92 psppire_syntax_window_finalize (GObject *object)
94 if (G_OBJECT_CLASS (parent_class)->finalize)
95 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
100 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
102 parent_class = g_type_class_peek_parent (class);
107 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
109 GObjectClass *object_class = G_OBJECT_CLASS (class);
111 object_class->finalize = psppire_syntax_window_finalize;
117 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
124 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
127 PsppireWindow *win = PSPPIRE_WINDOW (sw);
128 const gchar *name = psppire_window_get_filename (win);
129 execute_syntax (create_syntax_editor_source (sw->buffer, start, stop, name));
133 /* Parse and execute all the text in the buffer */
135 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
137 GtkTextIter begin, end;
138 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
140 gtk_text_buffer_get_iter_at_offset (se->buffer, &begin, 0);
141 gtk_text_buffer_get_iter_at_offset (se->buffer, &end, -1);
143 editor_execute_syntax (se, begin, end);
146 /* Parse and execute the currently selected text */
148 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
150 GtkTextIter begin, end;
151 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
153 if ( gtk_text_buffer_get_selection_bounds (se->buffer, &begin, &end) )
154 editor_execute_syntax (se, begin, end);
158 /* Parse and execute the from the current line, to the end of the
161 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
163 GtkTextIter begin, end;
167 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
169 /* Get the current line */
170 gtk_text_buffer_get_iter_at_mark (se->buffer,
172 gtk_text_buffer_get_insert (se->buffer)
175 line = gtk_text_iter_get_line (&here) ;
177 /* Now set begin and end to the start of this line, and end of buffer
179 gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
180 gtk_text_buffer_get_iter_at_line (se->buffer, &end, -1);
182 editor_execute_syntax (se, begin, end);
187 /* Parse and execute the current line */
189 on_run_current_line (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 (se->buffer,
200 gtk_text_buffer_get_insert (se->buffer)
203 line = gtk_text_iter_get_line (&here) ;
205 /* Now set begin and end to the start of this line, and start of
206 following line respectively */
207 gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
208 gtk_text_buffer_get_iter_at_line (se->buffer, &end, line + 1);
210 editor_execute_syntax (se, begin, end);
215 /* Append ".sps" to FILENAME if necessary.
216 The returned result must be freed when no longer required.
219 append_suffix (const gchar *filename)
221 if ( ! g_str_has_suffix (filename, ".sps" ) &&
222 ! g_str_has_suffix (filename, ".SPS" ) )
224 return g_strdup_printf ("%s.sps", filename);
227 return strdup (filename);
231 Save BUFFER to the file called FILENAME.
232 FILENAME must be encoded in Glib filename encoding.
233 If successful, clears the buffer's modified flag.
236 save_editor_to_file (PsppireSyntaxWindow *se,
237 const gchar *filename,
240 GtkTextBuffer *buffer = se->buffer;
242 GtkTextIter start, stop;
248 suffixedname = append_suffix (filename);
250 gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
251 gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
253 text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
255 result = g_file_set_contents (suffixedname, text, -1, err);
257 g_free (suffixedname);
261 char *fn = g_filename_display_name (filename);
262 gchar *msg = g_strdup_printf (_("Saved file \"%s\""), fn);
264 gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
265 gtk_text_buffer_set_modified (buffer, FALSE);
273 /* Callback for the File->SaveAs menuitem */
275 syntax_save_as (PsppireWindow *se)
277 GtkFileFilter *filter;
281 gtk_file_chooser_dialog_new (_("Save Syntax"),
283 GTK_FILE_CHOOSER_ACTION_SAVE,
284 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
285 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
288 filter = gtk_file_filter_new ();
289 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
290 gtk_file_filter_add_pattern (filter, "*.sps");
291 gtk_file_filter_add_pattern (filter, "*.SPS");
292 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
294 filter = gtk_file_filter_new ();
295 gtk_file_filter_set_name (filter, _("All Files"));
296 gtk_file_filter_add_pattern (filter, "*");
297 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
299 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
301 response = gtk_dialog_run (GTK_DIALOG (dialog));
303 if ( response == GTK_RESPONSE_ACCEPT )
307 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
309 if ( ! save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err) )
311 msg ( ME, err->message );
318 gtk_widget_destroy (dialog);
322 /* Callback for the File->Save menuitem */
324 syntax_save (PsppireWindow *se)
326 const gchar *filename = psppire_window_get_filename (se);
328 if ( filename == NULL )
333 save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
336 msg (ME, err->message);
343 /* Callback for the File->Quit menuitem */
345 on_quit (GtkMenuItem *menuitem, gpointer user_data)
354 create_syntax_window (void)
356 GtkWidget *w = psppire_syntax_window_new ();
360 /* Callback for the File->Open->Syntax menuitem */
362 open_syntax_window (GtkMenuItem *menuitem, gpointer parent)
364 GtkFileFilter *filter;
368 gtk_file_chooser_dialog_new (_("Open Syntax"),
370 GTK_FILE_CHOOSER_ACTION_OPEN,
371 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
372 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
375 filter = gtk_file_filter_new ();
376 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
377 gtk_file_filter_add_pattern (filter, "*.sps");
378 gtk_file_filter_add_pattern (filter, "*.SPS");
379 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
381 filter = gtk_file_filter_new ();
382 gtk_file_filter_set_name (filter, _("All Files"));
383 gtk_file_filter_add_pattern (filter, "*");
384 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
386 response = gtk_dialog_run (GTK_DIALOG (dialog));
388 if (response == GTK_RESPONSE_ACCEPT)
390 const char *file_name = gtk_file_chooser_get_filename
391 (GTK_FILE_CHOOSER (dialog));
393 GtkWidget *se = psppire_syntax_window_new ();
395 if ( psppire_window_load (PSPPIRE_WINDOW (se), file_name) )
396 gtk_widget_show (se);
398 gtk_widget_destroy (se);
401 gtk_widget_destroy (dialog);
406 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
408 gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
412 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
414 if (gtk_text_buffer_get_modified (buffer))
415 psppire_window_set_unsaved (window);
418 extern struct source_stream *the_source_stream ;
421 psppire_syntax_window_init (PsppireSyntaxWindow *window)
423 GtkBuilder *xml = builder_new ("syntax-editor.ui");
424 GtkWidget *box = gtk_vbox_new (FALSE, 0);
426 GtkWidget *menubar = get_widget_assert (xml, "menubar2");
427 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
430 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
431 window->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
432 window->lexer = lex_create (the_source_stream);
434 window->sb = get_widget_assert (xml, "statusbar2");
435 window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
437 g_signal_connect (window->buffer, "changed", G_CALLBACK (on_text_changed), window);
439 g_signal_connect (window->buffer, "modified-changed",
440 G_CALLBACK (on_modified_changed), window);
444 gtk_container_add (GTK_CONTAINER (window), box);
446 g_object_ref (menubar);
450 g_object_ref (window->sb);
453 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
454 gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
455 gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
457 gtk_widget_show_all (box);
459 g_signal_connect (get_action_assert (xml,"file_new_syntax"),
461 G_CALLBACK (create_syntax_window),
464 g_signal_connect (get_action_assert (xml,"file_open_syntax"),
466 G_CALLBACK (open_syntax_window),
470 g_signal_connect (get_action_assert (xml,"file_new_data"),
472 G_CALLBACK (create_data_window),
477 GtkAction *abt = get_action_assert (xml, "help_about");
478 g_object_set (abt, "stock-id", "gtk-about", NULL);
480 g_signal_connect (abt,
482 G_CALLBACK (about_new),
486 g_signal_connect (get_action_assert (xml,"help_reference"),
488 G_CALLBACK (reference_manual),
491 g_signal_connect_swapped (get_action_assert (xml, "file_save"),
493 G_CALLBACK (syntax_save),
496 g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
498 G_CALLBACK (syntax_save_as),
501 g_signal_connect (get_action_assert (xml,"file_quit"),
503 G_CALLBACK (on_quit),
506 g_signal_connect (get_action_assert (xml,"run_all"),
508 G_CALLBACK (on_run_all),
512 g_signal_connect (get_action_assert (xml,"run_selection"),
514 G_CALLBACK (on_run_selection),
517 g_signal_connect (get_action_assert (xml,"run_current_line"),
519 G_CALLBACK (on_run_current_line),
522 g_signal_connect (get_action_assert (xml,"run_to_end"),
524 G_CALLBACK (on_run_to_end),
527 g_signal_connect (get_action_assert (xml,"windows_minimise_all"),
529 G_CALLBACK (psppire_window_minimise_all), NULL);
533 GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
535 PSPPIRE_WINDOW (window)->menu =
536 GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar2/windows/windows_minimise_all")->parent);
539 g_object_unref (xml);
544 psppire_syntax_window_new (void)
546 return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
547 "filename", "Syntax",
548 "description", _("Syntax Editor"),
553 error_dialog (GtkWindow *w, const gchar *filename, GError *err)
555 gchar *fn = g_filename_display_basename (filename);
558 gtk_message_dialog_new (w,
559 GTK_DIALOG_DESTROY_WITH_PARENT,
562 _("Cannot load syntax file '%s'"),
567 g_object_set (dialog, "icon-name", "psppicon", NULL);
569 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
572 gtk_dialog_run (GTK_DIALOG (dialog));
574 gtk_widget_destroy (dialog);
578 Loads the buffer from the file called FILENAME
581 syntax_load (PsppireWindow *window, const gchar *filename)
586 PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
588 /* FIXME: What if it's a very big file ? */
589 if ( ! g_file_get_contents (filename, &text, NULL, &err) )
591 error_dialog (GTK_WINDOW (window), filename, err);
592 g_clear_error (&err);
596 gtk_text_buffer_get_iter_at_line (sw->buffer, &iter, 0);
598 gtk_text_buffer_insert (sw->buffer, &iter, text, -1);
600 gtk_text_buffer_set_modified (sw->buffer, FALSE);
608 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
610 iface->save = syntax_save;
611 iface->load = syntax_load;