2 PSPPIRE --- A Graphical User Interface for PSPP
3 Copyright (C) 2006 Free Software Foundation
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #define _(msgid) gettext (msgid)
24 #define N_(msgid) msgid
26 #include <glade/glade.h>
28 #include <libpspp/message.h>
29 #include <libpspp/getl.h>
34 #include <language/command.h>
35 #include <data/procedure.h>
36 #include "syntax-editor.h"
37 #include "syntax-editor-source.h"
39 extern struct source_stream *the_source_stream ;
40 extern struct lexer *the_lexer;
41 extern struct dataset *the_dataset;
45 static gboolean save_editor_to_file (struct syntax_editor *se,
46 const gchar *filename,
49 /* If the buffer's modified flag is set, then save it, and close the window.
50 Otherwise just close the window.
53 save_if_modified (struct syntax_editor *se)
55 if ( TRUE == gtk_text_buffer_get_modified (se->buffer))
59 gtk_message_dialog_new (GTK_WINDOW(se->window),
63 _("Save contents of syntax editor to %s?"),
64 se->name ? se->name : _("Untitled")
67 gtk_dialog_add_button (GTK_DIALOG(dialog),
70 gtk_dialog_add_button (GTK_DIALOG(dialog),
73 gtk_dialog_add_button (GTK_DIALOG(dialog),
78 response = gtk_dialog_run (GTK_DIALOG(dialog));
80 gtk_widget_destroy (dialog);
82 if ( response == GTK_RESPONSE_ACCEPT )
86 if ( ! save_editor_to_file (se, se->name ? se->name : _("Untitled"),
89 msg (ME, err->message);
94 if ( response == GTK_RESPONSE_CANCEL )
98 gtk_widget_destroy (se->window);
101 /* Callback for the File->SaveAs menuitem */
103 on_syntax_save_as (GtkMenuItem *menuitem,
106 GtkFileFilter *filter;
108 struct syntax_editor *se = user_data;
111 gtk_file_chooser_dialog_new (_("Save Syntax"),
112 GTK_WINDOW(se->window),
113 GTK_FILE_CHOOSER_ACTION_SAVE,
114 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
115 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
118 filter = gtk_file_filter_new ();
119 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
120 gtk_file_filter_add_pattern (filter, "*.sps");
121 gtk_file_filter_add_pattern (filter, "*.SPS");
122 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER(dialog), filter);
124 filter = gtk_file_filter_new ();
125 gtk_file_filter_set_name (filter, _("All Files"));
126 gtk_file_filter_add_pattern (filter, "*");
127 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
129 gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER(dialog),
131 response = gtk_dialog_run (GTK_DIALOG (dialog));
133 if ( response == GTK_RESPONSE_ACCEPT )
137 gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
139 if ( save_editor_to_file (se, filename, &err) )
142 se->name = g_strdup (filename);
146 msg ( ME, err->message );
153 gtk_widget_destroy ( dialog );
156 /* Callback for the File->Save menuitem */
158 on_syntax_save (GtkMenuItem *menuitem,
161 struct syntax_editor *se = user_data;
163 if ( se->name == NULL )
164 on_syntax_save_as (menuitem, user_data);
168 save_editor_to_file (se, se->name, &err);
169 msg (ME, err->message);
175 /* Callback for the "delete" action (clicking the x on the top right
176 hand corner of the window) */
178 on_delete (GtkWidget *w, GdkEvent *event, gpointer user_data)
180 struct syntax_editor *se = user_data;
181 save_if_modified (se);
186 /* Callback for the File->Quit menuitem */
188 on_quit (GtkMenuItem *menuitem, gpointer user_data)
190 struct syntax_editor *se = user_data;
191 save_if_modified (se);
196 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
198 GtkTextIter begin, end;
199 struct syntax_editor *se = user_data;
201 gtk_text_buffer_get_iter_at_line (se->buffer, &begin, 0);
202 gtk_text_buffer_get_iter_at_line (se->buffer, &end, -1);
204 getl_append_source (the_source_stream,
205 create_syntax_editor_source (se, begin, end));
208 int result = cmd_parse (the_lexer, the_dataset,
209 proc_has_source (the_dataset)
210 ? CMD_STATE_DATA : CMD_STATE_INITIAL);
212 if (result == CMD_EOF || result == CMD_FINISH)
219 new_syntax_window (GtkMenuItem *menuitem,
224 static void open_syntax_window (GtkMenuItem *menuitem,
229 Create a new syntax editor with NAME.
230 If NAME is NULL, a name will be automatically assigned
232 static struct syntax_editor *
233 new_syntax_editor (const gchar *name)
236 GtkWidget *text_view;
237 struct syntax_editor *se ;
239 new_xml = glade_xml_new (xml->filename, "syntax_editor", NULL);
241 se = g_malloc (sizeof (*se));
243 se->window = get_widget_assert (new_xml, "syntax_editor");
244 text_view = get_widget_assert (new_xml, "syntax_text_view");
245 se->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW(text_view));
247 se->name = g_strdup (name);
251 g_signal_connect (get_widget_assert (new_xml,"file_new_syntax"),
253 G_CALLBACK(new_syntax_window),
256 g_signal_connect (get_widget_assert (new_xml,"file_open_syntax"),
258 G_CALLBACK(open_syntax_window),
261 g_signal_connect (get_widget_assert (new_xml,"file_quit"),
266 g_signal_connect (get_widget_assert (new_xml,"file_save"),
268 G_CALLBACK(on_syntax_save),
271 g_signal_connect (get_widget_assert (new_xml,"file_save_as"),
273 G_CALLBACK(on_syntax_save_as),
277 g_signal_connect (get_widget_assert (new_xml,"run_all"),
279 G_CALLBACK(on_run_all),
282 g_object_unref (new_xml);
284 g_signal_connect (se->window, "delete-event",
285 G_CALLBACK(on_delete), se);
291 Callback for the File->New->Syntax menuitem
294 new_syntax_window (GtkMenuItem *menuitem,
297 struct syntax_editor *se = new_syntax_editor (NULL);
298 gtk_widget_show (se->window);
303 set_window_title_from_filename (struct syntax_editor *se,
304 const gchar *filename)
309 se->name = strdup (filename);
310 basename = g_path_get_basename (filename);
312 g_strdup_printf (_("%s --- PSPP Syntax Editor"), basename);
314 gtk_window_set_title (GTK_WINDOW(se->window), title);
320 Save BUFFER to the file called FILENAME.
321 If successful, clears the buffer's modified flag
324 save_editor_to_file (struct syntax_editor *se,
325 const gchar *filename,
328 GtkTextBuffer *buffer = se->buffer;
330 GtkTextIter start, stop;
336 glibfilename = g_filename_from_utf8 (filename, -1, 0, 0, err);
338 if ( ! glibfilename )
341 gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
342 gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
344 text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
346 result = g_file_set_contents (glibfilename, text, -1, err);
350 set_window_title_from_filename (se, filename);
351 gtk_text_buffer_set_modified (buffer, FALSE);
359 Loads the buffer from the file called FILENAME
362 load_editor_from_file (struct syntax_editor *se,
363 const gchar *filename,
366 GtkTextBuffer *buffer = se->buffer;
370 gchar *glibfilename = g_filename_from_utf8 (filename, -1, 0, 0, err);
372 if ( ! glibfilename )
375 /* FIXME: What if it's a very big file ? */
376 if ( ! g_file_get_contents (glibfilename, &text, NULL, err) )
378 g_free (glibfilename);
381 g_free (glibfilename);
383 gtk_text_buffer_get_iter_at_line (buffer, &iter, 0);
385 gtk_text_buffer_insert (buffer, &iter, text, -1);
387 set_window_title_from_filename (se, filename);
388 gtk_text_buffer_set_modified (buffer, FALSE);
394 /* Callback for the File->Open->Syntax menuitem */
396 open_syntax_window (GtkMenuItem *menuitem, gpointer parent)
398 GtkFileFilter *filter;
402 gtk_file_chooser_dialog_new (_("Open Syntax"),
404 GTK_FILE_CHOOSER_ACTION_OPEN,
405 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
406 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
409 filter = gtk_file_filter_new ();
410 gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
411 gtk_file_filter_add_pattern (filter, "*.sps");
412 gtk_file_filter_add_pattern (filter, "*.SPS");
413 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER(dialog), filter);
415 filter = gtk_file_filter_new ();
416 gtk_file_filter_set_name (filter, _("All Files"));
417 gtk_file_filter_add_pattern (filter, "*");
418 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
420 response = gtk_dialog_run (GTK_DIALOG (dialog));
422 if (response == GTK_RESPONSE_ACCEPT)
424 const char *file_name = gtk_file_chooser_get_filename
425 (GTK_FILE_CHOOSER (dialog));
427 struct syntax_editor *se = new_syntax_editor (file_name);
429 load_editor_from_file (se, file_name, NULL);
431 gtk_widget_show (se->window);
434 gtk_widget_destroy (dialog);
439 /* FIXME: get rid of these functions */
441 on_syntax4_activate (GtkMenuItem *menuitem,
444 g_print ("%s\n", __FUNCTION__);
450 on_syntax2_activate (GtkMenuItem *menuitem,
453 g_print ("%s\n", __FUNCTION__);
457 on_syntax1_activate (GtkMenuItem *menuitem,
460 g_print ("%s\n", __FUNCTION__);
461 new_syntax_window (menuitem, user_data);