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>
21 #include <glade/glade.h>
24 #include <libpspp/message.h>
27 #include "psppire-syntax-window.h"
29 #include "psppire-data-window.h"
30 #include "psppire-window-register.h"
32 #include "psppire-syntax-window.h"
33 #include "syntax-editor-source.h"
34 #include <language/lexer/lexer.h>
37 #define _(msgid) gettext (msgid)
38 #define N_(msgid) msgid
40 static void psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *, gpointer);
41 static void psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class);
42 static void psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class);
43 static void psppire_syntax_window_init (PsppireSyntaxWindow *syntax_editor);
46 psppire_syntax_window_get_type (void)
48 static GType psppire_syntax_window_type = 0;
50 if (!psppire_syntax_window_type)
52 static const GTypeInfo psppire_syntax_window_info =
54 sizeof (PsppireSyntaxWindowClass),
55 (GBaseInitFunc) psppire_syntax_window_base_init,
56 (GBaseFinalizeFunc) psppire_syntax_window_base_finalize,
57 (GClassInitFunc)psppire_syntax_window_class_init,
58 (GClassFinalizeFunc) NULL,
60 sizeof (PsppireSyntaxWindow),
62 (GInstanceInitFunc) psppire_syntax_window_init,
65 psppire_syntax_window_type =
66 g_type_register_static (PSPPIRE_WINDOW_TYPE, "PsppireSyntaxWindow",
67 &psppire_syntax_window_info, 0);
70 return psppire_syntax_window_type;
74 psppire_syntax_window_finalize (GObject *object)
76 GObjectClass *class = G_OBJECT_GET_CLASS (object);
78 PsppireSyntaxWindow *window = PSPPIRE_SYNTAX_WINDOW (object);
80 GObjectClass *parent_class = g_type_class_peek_parent (class);
82 if (G_OBJECT_CLASS (parent_class)->finalize)
83 (*G_OBJECT_CLASS (parent_class)->finalize) (object);
88 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
94 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
96 GObjectClass *object_class = G_OBJECT_CLASS (class);
98 object_class->finalize = psppire_syntax_window_finalize;
104 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
111 editor_execute_syntax (const PsppireSyntaxWindow *se, GtkTextIter start,
114 execute_syntax (create_syntax_editor_source (se->buffer, start, stop));
118 /* Parse and execute all the text in the buffer */
120 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
122 GtkTextIter begin, end;
123 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
125 gtk_text_buffer_get_iter_at_offset (se->buffer, &begin, 0);
126 gtk_text_buffer_get_iter_at_offset (se->buffer, &end, -1);
128 editor_execute_syntax (se, begin, end);
131 /* Parse and execute the currently selected text */
133 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
135 GtkTextIter begin, end;
136 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
138 if ( gtk_text_buffer_get_selection_bounds (se->buffer, &begin, &end) )
139 editor_execute_syntax (se, begin, end);
143 /* Parse and execute the from the current line, to the end of the
146 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
148 GtkTextIter begin, end;
152 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
154 /* Get the current line */
155 gtk_text_buffer_get_iter_at_mark (se->buffer,
157 gtk_text_buffer_get_insert (se->buffer)
160 line = gtk_text_iter_get_line (&here) ;
162 /* Now set begin and end to the start of this line, and end of buffer
164 gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
165 gtk_text_buffer_get_iter_at_line (se->buffer, &end, -1);
167 editor_execute_syntax (se, begin, end);
172 /* Parse and execute the current line */
174 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
176 GtkTextIter begin, end;
180 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
182 /* Get the current line */
183 gtk_text_buffer_get_iter_at_mark (se->buffer,
185 gtk_text_buffer_get_insert (se->buffer)
188 line = gtk_text_iter_get_line (&here) ;
190 /* Now set begin and end to the start of this line, and start of
191 following line respectively */
192 gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
193 gtk_text_buffer_get_iter_at_line (se->buffer, &end, line + 1);
195 editor_execute_syntax (se, begin, end);
200 /* Append ".sps" to FILENAME if necessary.
201 The returned result must be freed when no longer required.
204 append_suffix (const gchar *filename)
206 if ( ! g_str_has_suffix (filename, ".sps" ) &&
207 ! g_str_has_suffix (filename, ".SPS" ) )
209 return g_strdup_printf ("%s.sps", filename);
212 return strdup (filename);
216 Save BUFFER to the file called FILENAME.
217 If successful, clears the buffer's modified flag
220 save_editor_to_file (PsppireSyntaxWindow *se,
221 const gchar *filename,
224 GtkTextBuffer *buffer = se->buffer;
226 GtkTextIter start, stop;
233 suffixedname = append_suffix (filename);
235 glibfilename = g_filename_from_utf8 (suffixedname, -1, 0, 0, err);
237 g_free ( suffixedname);
239 if ( ! glibfilename )
242 gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
243 gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
245 text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
247 result = g_file_set_contents (glibfilename, text, -1, err);
251 psppire_window_set_filename (PSPPIRE_WINDOW (se), filename);
252 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 (PsppireSyntaxWindow *se)
265 if ( TRUE == gtk_text_buffer_get_modified (se->buffer))
270 const gchar *filename = psppire_window_get_filename (PSPPIRE_WINDOW (se));
272 g_return_if_fail (filename != NULL);
275 gtk_message_dialog_new (GTK_WINDOW (se),
277 GTK_MESSAGE_QUESTION,
279 _("Save contents of syntax editor to %s?"),
282 gtk_dialog_add_button (GTK_DIALOG (dialog),
284 GTK_RESPONSE_ACCEPT);
285 gtk_dialog_add_button (GTK_DIALOG (dialog),
287 GTK_RESPONSE_REJECT);
288 gtk_dialog_add_button (GTK_DIALOG (dialog),
290 GTK_RESPONSE_CANCEL);
293 response = gtk_dialog_run (GTK_DIALOG (dialog));
295 gtk_widget_destroy (dialog);
297 if ( response == GTK_RESPONSE_ACCEPT )
301 if ( ! save_editor_to_file (se, filename, &err) )
303 msg (ME, err->message);
308 if ( response == GTK_RESPONSE_CANCEL )
312 gtk_widget_destroy (GTK_WIDGET (se));
315 /* Callback for the File->SaveAs menuitem */
317 on_syntax_save_as (GtkMenuItem *menuitem, gpointer user_data)
319 GtkFileFilter *filter;
322 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
325 gtk_file_chooser_dialog_new (_("Save Syntax"),
327 GTK_FILE_CHOOSER_ACTION_SAVE,
328 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
329 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
332 filter = gtk_file_filter_new ();
333 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
334 gtk_file_filter_add_pattern (filter, "*.sps");
335 gtk_file_filter_add_pattern (filter, "*.SPS");
336 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
338 filter = gtk_file_filter_new ();
339 gtk_file_filter_set_name (filter, _("All Files"));
340 gtk_file_filter_add_pattern (filter, "*");
341 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
343 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
345 response = gtk_dialog_run (GTK_DIALOG (dialog));
347 if ( response == GTK_RESPONSE_ACCEPT )
351 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
353 if ( ! save_editor_to_file (se, filename, &err) )
355 msg ( ME, err->message );
362 gtk_widget_destroy (dialog);
366 /* Callback for the File->Save menuitem */
368 on_syntax_save (GtkMenuItem *menuitem, gpointer user_data)
370 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
371 const gchar *filename = psppire_window_get_filename (PSPPIRE_WINDOW (se));
374 if ( filename == NULL )
375 on_syntax_save_as (menuitem, se);
379 save_editor_to_file (se, filename, &err);
382 msg (ME, err->message);
389 /* Callback for the File->Quit menuitem */
391 on_quit (GtkMenuItem *menuitem, gpointer user_data)
393 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
394 save_if_modified (se);
399 /* Callback for the "delete" action (clicking the x on the top right
400 hand corner of the window) */
402 on_delete (GtkWidget *w, GdkEvent *event, gpointer user_data)
404 PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
406 save_if_modified (se);
412 create_syntax_window (void)
414 GtkWidget *w = psppire_syntax_window_new ();
418 /* Callback for the File->Open->Syntax menuitem */
420 open_syntax_window (GtkMenuItem *menuitem, gpointer parent)
422 GtkFileFilter *filter;
426 gtk_file_chooser_dialog_new (_("Open Syntax"),
428 GTK_FILE_CHOOSER_ACTION_OPEN,
429 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
430 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
433 filter = gtk_file_filter_new ();
434 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
435 gtk_file_filter_add_pattern (filter, "*.sps");
436 gtk_file_filter_add_pattern (filter, "*.SPS");
437 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
439 filter = gtk_file_filter_new ();
440 gtk_file_filter_set_name (filter, _("All Files"));
441 gtk_file_filter_add_pattern (filter, "*");
442 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
444 response = gtk_dialog_run (GTK_DIALOG (dialog));
446 if (response == GTK_RESPONSE_ACCEPT)
448 const char *file_name = gtk_file_chooser_get_filename
449 (GTK_FILE_CHOOSER (dialog));
451 GtkWidget *se = psppire_syntax_window_new ();
453 if ( psppire_syntax_window_load_from_file (PSPPIRE_SYNTAX_WINDOW (se), file_name, NULL) )
454 #if RECENT_LISTS_AVAILABLE
456 GtkRecentManager *manager = gtk_recent_manager_get_default();
457 gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
459 gtk_recent_manager_remove_item (manager, uri, NULL);
460 if ( ! gtk_recent_manager_add_item (manager, uri))
461 g_warning ("Could not add item %s to recent list\n",uri);
468 gtk_widget_show (se);
471 gtk_widget_destroy (dialog);
476 extern struct source_stream *the_source_stream ;
479 psppire_syntax_window_init (PsppireSyntaxWindow *window)
481 GladeXML *xml = XML_NEW ("syntax-editor.glade");
482 GtkWidget *box = gtk_vbox_new (FALSE, 0);
484 GtkWidget *menubar = get_widget_assert (xml, "menubar2");
485 GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
486 GtkWidget *sb = get_widget_assert (xml, "statusbar2");
488 GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
489 window->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
490 window->lexer = lex_create (the_source_stream);
494 gtk_container_add (GTK_CONTAINER (window), box);
496 g_object_ref (menubar);
497 gtk_widget_unparent (menubar);
500 gtk_widget_unparent (sw);
503 gtk_widget_unparent (sb);
506 gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
507 gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
508 gtk_box_pack_start (GTK_BOX (box), sb, FALSE, TRUE, 0);
510 gtk_widget_show_all (box);
512 g_signal_connect (get_widget_assert (xml,"file_new_syntax"),
514 G_CALLBACK (create_syntax_window),
517 g_signal_connect (get_widget_assert (xml,"file_open_syntax"),
519 G_CALLBACK (open_syntax_window),
523 g_signal_connect (get_widget_assert (xml,"file_new_data"),
525 G_CALLBACK (create_data_window),
528 g_signal_connect (get_widget_assert (xml,"help_about"),
530 G_CALLBACK (about_new),
533 g_signal_connect (get_widget_assert (xml,"help_reference"),
535 G_CALLBACK (reference_manual),
538 g_signal_connect (get_widget_assert (xml, "file_save"),
540 G_CALLBACK (on_syntax_save),
543 g_signal_connect (get_widget_assert (xml, "file_save_as"),
545 G_CALLBACK (on_syntax_save_as),
548 g_signal_connect (get_widget_assert (xml,"file_quit"),
550 G_CALLBACK (on_quit),
553 g_signal_connect (get_widget_assert (xml,"run_all"),
555 G_CALLBACK (on_run_all),
559 g_signal_connect (get_widget_assert (xml,"run_selection"),
561 G_CALLBACK (on_run_selection),
564 g_signal_connect (get_widget_assert (xml,"run_current_line"),
566 G_CALLBACK (on_run_current_line),
569 g_signal_connect (get_widget_assert (xml,"run_to_end"),
571 G_CALLBACK (on_run_to_end),
574 g_signal_connect (get_widget_assert (xml,"windows_minimise_all"),
576 G_CALLBACK (psppire_window_minimise_all), NULL);
578 PSPPIRE_WINDOW (window)->menu = GTK_MENU (get_widget_assert (xml,"windows_menu"));
579 g_object_ref (PSPPIRE_WINDOW (window)->menu);
581 g_object_unref (xml);
583 g_signal_connect (window, "delete-event",
584 G_CALLBACK (on_delete), window);
589 psppire_syntax_window_new (void)
591 return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
592 "usage", PSPPIRE_WINDOW_USAGE_SYNTAX,
598 Loads the buffer from the file called FILENAME
601 psppire_syntax_window_load_from_file (PsppireSyntaxWindow *se,
602 const gchar *filename,
608 gchar *glibfilename = g_filename_from_utf8 (filename, -1, 0, 0, err);
610 if ( ! glibfilename )
613 /* FIXME: What if it's a very big file ? */
614 if ( ! g_file_get_contents (glibfilename, &text, NULL, err) )
616 g_free (glibfilename);
619 g_free (glibfilename);
621 gtk_text_buffer_get_iter_at_line (se->buffer, &iter, 0);
623 gtk_text_buffer_insert (se->buffer, &iter, text, -1);
625 psppire_window_set_filename (PSPPIRE_WINDOW (se), filename);
627 gtk_text_buffer_set_modified (se->buffer, FALSE);