1 /* PSPPIRE - a graphical user interface for PSPP.
2 Copyright (C) 2008 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>
26 #include "psppire-syntax-window.h"
28 #include "psppire-data-window.h"
29 #include "psppire-window-register.h"
31 #include "psppire-syntax-window.h"
32 #include "syntax-editor-source.h"
33 #include <language/lexer/lexer.h>
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) msgid
39 static void psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *, gpointer);
40 static void psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class);
41 static void psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class);
42 static void psppire_syntax_window_init (PsppireSyntaxWindow *syntax_editor);
45 psppire_syntax_window_get_type (void)
47 static GType psppire_syntax_window_type = 0;
49 if (!psppire_syntax_window_type)
51 static const GTypeInfo psppire_syntax_window_info =
53 sizeof (PsppireSyntaxWindowClass),
54 (GBaseInitFunc) psppire_syntax_window_base_init,
55 (GBaseFinalizeFunc) psppire_syntax_window_base_finalize,
56 (GClassInitFunc)psppire_syntax_window_class_init,
57 (GClassFinalizeFunc) NULL,
59 sizeof (PsppireSyntaxWindow),
61 (GInstanceInitFunc) psppire_syntax_window_init,
64 psppire_syntax_window_type =
65 g_type_register_static (PSPPIRE_WINDOW_TYPE, "PsppireSyntaxWindow",
66 &psppire_syntax_window_info, 0);
69 return psppire_syntax_window_type;
72 static GObjectClass *parent_class ;
75 psppire_syntax_window_finalize (GObject *object)
77 if (G_OBJECT_CLASS (parent_class)->finalize)
78 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
83 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
85 parent_class = g_type_class_peek_parent (class);
90 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
92 GObjectClass *object_class = G_OBJECT_CLASS (class);
94 object_class->finalize = psppire_syntax_window_finalize;
100 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
107 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
110 PsppireWindow *win = PSPPIRE_WINDOW (sw);
111 const gchar *name = psppire_window_get_filename (win);
112 execute_syntax (create_syntax_editor_source (sw->buffer, start, stop, name));
116 /* Parse and execute all the text in the buffer */
118 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
120 GtkTextIter begin, end;
121 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
123 gtk_text_buffer_get_iter_at_offset (se->buffer, &begin, 0);
124 gtk_text_buffer_get_iter_at_offset (se->buffer, &end, -1);
126 editor_execute_syntax (se, begin, end);
129 /* Parse and execute the currently selected text */
131 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
133 GtkTextIter begin, end;
134 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
136 if ( gtk_text_buffer_get_selection_bounds (se->buffer, &begin, &end) )
137 editor_execute_syntax (se, begin, end);
141 /* Parse and execute the from the current line, to the end of the
144 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
146 GtkTextIter begin, end;
150 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
152 /* Get the current line */
153 gtk_text_buffer_get_iter_at_mark (se->buffer,
155 gtk_text_buffer_get_insert (se->buffer)
158 line = gtk_text_iter_get_line (&here) ;
160 /* Now set begin and end to the start of this line, and end of buffer
162 gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
163 gtk_text_buffer_get_iter_at_line (se->buffer, &end, -1);
165 editor_execute_syntax (se, begin, end);
170 /* Parse and execute the current line */
172 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
174 GtkTextIter begin, end;
178 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
180 /* Get the current line */
181 gtk_text_buffer_get_iter_at_mark (se->buffer,
183 gtk_text_buffer_get_insert (se->buffer)
186 line = gtk_text_iter_get_line (&here) ;
188 /* Now set begin and end to the start of this line, and start of
189 following line respectively */
190 gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
191 gtk_text_buffer_get_iter_at_line (se->buffer, &end, line + 1);
193 editor_execute_syntax (se, begin, end);
198 /* Append ".sps" to FILENAME if necessary.
199 The returned result must be freed when no longer required.
202 append_suffix (const gchar *filename)
204 if ( ! g_str_has_suffix (filename, ".sps" ) &&
205 ! g_str_has_suffix (filename, ".SPS" ) )
207 return g_strdup_printf ("%s.sps", filename);
210 return strdup (filename);
214 Save BUFFER to the file called FILENAME.
215 If successful, clears the buffer's modified flag
218 save_editor_to_file (PsppireSyntaxWindow *se,
219 const gchar *filename,
222 GtkTextBuffer *buffer = se->buffer;
224 GtkTextIter start, stop;
231 suffixedname = append_suffix (filename);
233 glibfilename = g_filename_from_utf8 (suffixedname, -1, 0, 0, err);
235 g_free ( suffixedname);
237 if ( ! glibfilename )
240 gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
241 gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
243 text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
245 result = g_file_set_contents (glibfilename, text, -1, err);
249 gchar *msg = g_strdup_printf (_("Saved file \"%s\""), filename);
250 gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
251 gtk_text_buffer_set_modified (buffer, FALSE);
258 /* If the buffer's modified flag is set, then save it, and close the window.
259 Otherwise just close the window.
262 save_if_modified (PsppireWindow *se)
264 if ( TRUE == psppire_window_get_unsaved (se))
269 const gchar *description;
270 const gchar *filename = psppire_window_get_filename (se);
272 g_object_get (se, "description", &description, NULL);
274 g_return_if_fail (filename != NULL);
277 gtk_message_dialog_new (GTK_WINDOW (se),
279 GTK_MESSAGE_QUESTION,
281 _("Save contents of %s to \"%s\"?"),
285 gtk_dialog_add_button (GTK_DIALOG (dialog),
287 GTK_RESPONSE_ACCEPT);
289 gtk_dialog_add_button (GTK_DIALOG (dialog),
291 GTK_RESPONSE_REJECT);
293 gtk_dialog_add_button (GTK_DIALOG (dialog),
295 GTK_RESPONSE_CANCEL);
297 response = gtk_dialog_run (GTK_DIALOG (dialog));
299 gtk_widget_destroy (dialog);
301 if ( response == GTK_RESPONSE_ACCEPT )
305 if ( ! save_editor_to_file (se, filename, &err) )
307 msg (ME, err->message);
310 psppire_window_set_filename (se, filename);
313 if ( response == GTK_RESPONSE_CANCEL )
317 gtk_widget_destroy (GTK_WIDGET (se));
320 /* Callback for the File->SaveAs menuitem */
322 on_syntax_save_as (GtkMenuItem *menuitem, gpointer user_data)
324 GtkFileFilter *filter;
327 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
330 gtk_file_chooser_dialog_new (_("Save Syntax"),
332 GTK_FILE_CHOOSER_ACTION_SAVE,
333 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
334 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
337 filter = gtk_file_filter_new ();
338 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
339 gtk_file_filter_add_pattern (filter, "*.sps");
340 gtk_file_filter_add_pattern (filter, "*.SPS");
341 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
343 filter = gtk_file_filter_new ();
344 gtk_file_filter_set_name (filter, _("All Files"));
345 gtk_file_filter_add_pattern (filter, "*");
346 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
348 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
350 response = gtk_dialog_run (GTK_DIALOG (dialog));
352 if ( response == GTK_RESPONSE_ACCEPT )
356 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
358 if ( ! save_editor_to_file (se, filename, &err) )
360 msg ( ME, err->message );
367 gtk_widget_destroy (dialog);
371 /* Callback for the File->Save menuitem */
373 on_syntax_save (GtkMenuItem *menuitem, gpointer user_data)
375 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
376 const gchar *filename = psppire_window_get_filename (PSPPIRE_WINDOW (se));
379 if ( filename == NULL )
380 on_syntax_save_as (menuitem, se);
384 save_editor_to_file (se, filename, &err);
387 msg (ME, err->message);
394 /* Callback for the File->Quit menuitem */
396 on_quit (GtkMenuItem *menuitem, gpointer user_data)
398 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
399 save_if_modified (se);
404 /* Callback for the "delete" action (clicking the x on the top right
405 hand corner of the window) */
407 on_delete (GtkWidget *w, GdkEvent *event, gpointer user_data)
409 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
411 save_if_modified (se);
417 create_syntax_window (void)
419 GtkWidget *w = psppire_syntax_window_new ();
423 /* Callback for the File->Open->Syntax menuitem */
425 open_syntax_window (GtkMenuItem *menuitem, gpointer parent)
427 GtkFileFilter *filter;
431 gtk_file_chooser_dialog_new (_("Open Syntax"),
433 GTK_FILE_CHOOSER_ACTION_OPEN,
434 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
435 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
438 filter = gtk_file_filter_new ();
439 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
440 gtk_file_filter_add_pattern (filter, "*.sps");
441 gtk_file_filter_add_pattern (filter, "*.SPS");
442 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
444 filter = gtk_file_filter_new ();
445 gtk_file_filter_set_name (filter, _("All Files"));
446 gtk_file_filter_add_pattern (filter, "*");
447 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
449 response = gtk_dialog_run (GTK_DIALOG (dialog));
451 if (response == GTK_RESPONSE_ACCEPT)
453 const char *file_name = gtk_file_chooser_get_filename
454 (GTK_FILE_CHOOSER (dialog));
456 GtkWidget *se = psppire_syntax_window_new ();
458 if ( psppire_syntax_window_load_from_file (PSPPIRE_SYNTAX_WINDOW (se), file_name, NULL) )
459 #if RECENT_LISTS_AVAILABLE
461 GtkRecentManager *manager = gtk_recent_manager_get_default();
462 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
464 gtk_recent_manager_remove_item (manager, uri, NULL);
465 if ( ! gtk_recent_manager_add_item (manager, uri))
466 g_warning ("Could not add item %s to recent list\n",uri);
473 gtk_widget_show (se);
476 gtk_widget_destroy (dialog);
481 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
483 gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
487 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
489 psppire_window_set_unsaved (window, gtk_text_buffer_get_modified (buffer));
493 extern struct source_stream *the_source_stream ;
496 psppire_syntax_window_init (PsppireSyntaxWindow *window)
498 GtkBuilder *xml = builder_new ("syntax-editor.ui");
499 GtkWidget *box = gtk_vbox_new (FALSE, 0);
501 GtkWidget *menubar = get_widget_assert (xml, "menubar2");
502 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
505 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
506 window->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
507 window->lexer = lex_create (the_source_stream);
509 window->sb = get_widget_assert (xml, "statusbar2");
510 window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
512 g_signal_connect (window->buffer, "changed", G_CALLBACK (on_text_changed), window);
514 g_signal_connect (window->buffer, "modified-changed",
515 G_CALLBACK (on_modified_changed), window);
519 gtk_container_add (GTK_CONTAINER (window), box);
521 g_object_ref (menubar);
525 g_object_ref (window->sb);
528 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
529 gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
530 gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
532 gtk_widget_show_all (box);
534 g_signal_connect (get_object_assert (xml,"file_new_syntax"),
536 G_CALLBACK (create_syntax_window),
539 g_signal_connect (get_object_assert (xml,"file_open_syntax"),
541 G_CALLBACK (open_syntax_window),
545 g_signal_connect (get_object_assert (xml,"file_new_data"),
547 G_CALLBACK (create_data_window),
551 g_signal_connect (get_object_assert (xml,"help_about"),
553 G_CALLBACK (about_new),
556 g_signal_connect (get_object_assert (xml,"help_reference"),
558 G_CALLBACK (reference_manual),
561 g_signal_connect (get_object_assert (xml, "file_save"),
563 G_CALLBACK (on_syntax_save),
566 g_signal_connect (get_object_assert (xml, "file_save_as"),
568 G_CALLBACK (on_syntax_save_as),
571 g_signal_connect (get_object_assert (xml,"file_quit"),
573 G_CALLBACK (on_quit),
576 g_signal_connect (get_object_assert (xml,"run_all"),
578 G_CALLBACK (on_run_all),
582 g_signal_connect (get_object_assert (xml,"run_selection"),
584 G_CALLBACK (on_run_selection),
587 g_signal_connect (get_object_assert (xml,"run_current_line"),
589 G_CALLBACK (on_run_current_line),
592 g_signal_connect (get_object_assert (xml,"run_to_end"),
594 G_CALLBACK (on_run_to_end),
597 g_signal_connect (get_object_assert (xml,"windows_minimise_all"),
599 G_CALLBACK (psppire_window_minimise_all), NULL);
601 GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1"));
603 PSPPIRE_WINDOW (window)->menu =
604 GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar2/windows/windows_minimise_all")->parent);
607 g_object_unref (xml);
609 g_signal_connect (window, "delete-event",
610 G_CALLBACK (on_delete), window);
615 psppire_syntax_window_new (void)
617 return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
618 "filename", "Syntax",
619 "description", _("Syntax Editor"),
625 Loads the buffer from the file called FILENAME
628 psppire_syntax_window_load_from_file (PsppireSyntaxWindow *se,
629 const gchar *filename,
635 gchar *glibfilename = g_filename_from_utf8 (filename, -1, 0, 0, err);
637 if ( ! glibfilename )
640 /* FIXME: What if it's a very big file ? */
641 if ( ! g_file_get_contents (glibfilename, &text, NULL, err) )
643 g_free (glibfilename);
646 g_free (glibfilename);
648 gtk_text_buffer_get_iter_at_line (se->buffer, &iter, 0);
650 gtk_text_buffer_insert (se->buffer, &iter, text, -1);
652 psppire_window_set_filename (PSPPIRE_WINDOW (se), filename);
654 gtk_text_buffer_set_modified (se->buffer, FALSE);