de12be49f21d7d96d6a1e9145d599f84bd1ef2bf
[pspp] / src / ui / gui / psppire-syntax-window.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 2009, 2010  Free Software Foundation
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <gtk/gtksignal.h>
20 #include <gtk/gtkbox.h>
21 #include "executor.h"
22 #include "helper.h"
23
24 #include <libpspp/message.h>
25 #include <stdlib.h>
26
27 #include "psppire.h"
28 #include "psppire-syntax-window.h"
29
30 #include "psppire-data-window.h"
31 #include "psppire-window-register.h"
32 #include "psppire.h"
33 #include "help-menu.h"
34 #include "psppire-syntax-window.h"
35 #include "syntax-editor-source.h"
36 #include <language/lexer/lexer.h>
37
38 #include "xalloc.h"
39
40 #include <gettext.h>
41 #define _(msgid) gettext (msgid)
42 #define N_(msgid) msgid
43
44 static void psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *, gpointer);
45 static void psppire_syntax_window_base_init     (PsppireSyntaxWindowClass *class);
46 static void psppire_syntax_window_class_init    (PsppireSyntaxWindowClass *class);
47 static void psppire_syntax_window_init          (PsppireSyntaxWindow      *syntax_editor);
48
49
50 static void psppire_syntax_window_iface_init (PsppireWindowIface *iface);
51
52
53 GType
54 psppire_syntax_window_get_type (void)
55 {
56   static GType psppire_syntax_window_type = 0;
57
58   if (!psppire_syntax_window_type)
59     {
60       static const GTypeInfo psppire_syntax_window_info =
61       {
62         sizeof (PsppireSyntaxWindowClass),
63         (GBaseInitFunc) psppire_syntax_window_base_init,
64         (GBaseFinalizeFunc) psppire_syntax_window_base_finalize,
65         (GClassInitFunc)psppire_syntax_window_class_init,
66         (GClassFinalizeFunc) NULL,
67         NULL,
68         sizeof (PsppireSyntaxWindow),
69         0,
70         (GInstanceInitFunc) psppire_syntax_window_init,
71       };
72
73       static const GInterfaceInfo window_interface_info =
74         {
75           (GInterfaceInitFunc) psppire_syntax_window_iface_init,
76           NULL,
77           NULL
78         };
79
80       psppire_syntax_window_type =
81         g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireSyntaxWindow",
82                                 &psppire_syntax_window_info, 0);
83
84       g_type_add_interface_static (psppire_syntax_window_type,
85                                    PSPPIRE_TYPE_WINDOW_MODEL,
86                                    &window_interface_info);
87     }
88
89   return psppire_syntax_window_type;
90 }
91
92 static GObjectClass *parent_class ;
93
94 static void
95 psppire_syntax_window_finalize (GObject *object)
96 {
97   if (G_OBJECT_CLASS (parent_class)->finalize)
98     (*G_OBJECT_CLASS (parent_class)->finalize) (object);
99 }
100
101
102 static void
103 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
104 {
105   parent_class = g_type_class_peek_parent (class);
106 }
107
108
109 static void
110 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
111 {
112   GObjectClass *object_class = G_OBJECT_CLASS (class);
113
114   object_class->finalize = psppire_syntax_window_finalize;
115 }
116
117
118
119 static void
120 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
121                                      gpointer class_data)
122 {
123 }
124
125
126 static void
127 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
128                        GtkTextIter stop)
129 {
130   PsppireWindow *win = PSPPIRE_WINDOW (sw);
131   const gchar *name = psppire_window_get_filename (win);
132   execute_syntax (create_syntax_editor_source (sw->buffer, start, stop, name));
133 }
134
135
136 /* Parse and execute all the text in the buffer */
137 static void
138 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
139 {
140   GtkTextIter begin, end;
141   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
142
143   gtk_text_buffer_get_iter_at_offset (se->buffer, &begin, 0);
144   gtk_text_buffer_get_iter_at_offset (se->buffer, &end, -1);
145
146   editor_execute_syntax (se, begin, end);
147 }
148
149 /* Parse and execute the currently selected text */
150 static void
151 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
152 {
153   GtkTextIter begin, end;
154   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
155
156   if ( gtk_text_buffer_get_selection_bounds (se->buffer, &begin, &end) )
157     editor_execute_syntax (se, begin, end);
158 }
159
160
161 /* Parse and execute the from the current line, to the end of the
162    buffer */
163 static void
164 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
165 {
166   GtkTextIter begin, end;
167   GtkTextIter here;
168   gint line;
169
170   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
171
172   /* Get the current line */
173   gtk_text_buffer_get_iter_at_mark (se->buffer,
174                                     &here,
175                                     gtk_text_buffer_get_insert (se->buffer)
176                                     );
177
178   line = gtk_text_iter_get_line (&here) ;
179
180   /* Now set begin and end to the start of this line, and end of buffer
181      respectively */
182   gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
183   gtk_text_buffer_get_iter_at_line (se->buffer, &end, -1);
184
185   editor_execute_syntax (se, begin, end);
186 }
187
188
189
190 /* Parse and execute the current line */
191 static void
192 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
193 {
194   GtkTextIter begin, end;
195   GtkTextIter here;
196   gint line;
197
198   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
199
200   /* Get the current line */
201   gtk_text_buffer_get_iter_at_mark (se->buffer,
202                                     &here,
203                                     gtk_text_buffer_get_insert (se->buffer)
204                                     );
205
206   line = gtk_text_iter_get_line (&here) ;
207
208   /* Now set begin and end to the start of this line, and start of
209      following line respectively */
210   gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
211   gtk_text_buffer_get_iter_at_line (se->buffer, &end, line + 1);
212
213   editor_execute_syntax (se, begin, end);
214 }
215
216
217
218 /* Append ".sps" to FILENAME if necessary.
219    The returned result must be freed when no longer required.
220  */
221 static gchar *
222 append_suffix (const gchar *filename)
223 {
224   if ( ! g_str_has_suffix (filename, ".sps" ) &&
225        ! g_str_has_suffix (filename, ".SPS" ) )
226     {
227       return g_strdup_printf ("%s.sps", filename);
228     }
229
230   return xstrdup (filename);
231 }
232
233 /*
234   Save BUFFER to the file called FILENAME.
235   FILENAME must be encoded in Glib filename encoding.
236   If successful, clears the buffer's modified flag.
237 */
238 static gboolean
239 save_editor_to_file (PsppireSyntaxWindow *se,
240                      const gchar *filename,
241                      GError **err)
242 {
243   GtkTextBuffer *buffer = se->buffer;
244   gboolean result ;
245   GtkTextIter start, stop;
246   gchar *text;
247
248   gchar *suffixedname;
249   g_assert (filename);
250
251   suffixedname = append_suffix (filename);
252
253   gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
254   gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
255
256   text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
257
258   result =  g_file_set_contents (suffixedname, text, -1, err);
259
260   g_free (suffixedname);
261
262   if ( result )
263     {
264       char *fn = g_filename_display_name (filename);
265       gchar *msg = g_strdup_printf (_("Saved file \"%s\""), fn);
266       g_free (fn);
267       gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
268       gtk_text_buffer_set_modified (buffer, FALSE);
269       g_free (msg);
270     }
271
272   return result;
273 }
274
275
276 /* Callback for the File->SaveAs menuitem */
277 static void
278 syntax_save_as (PsppireWindow *se)
279 {
280   GtkFileFilter *filter;
281   gint response;
282
283   GtkWidget *dialog =
284     gtk_file_chooser_dialog_new (_("Save Syntax"),
285                                  GTK_WINDOW (se),
286                                  GTK_FILE_CHOOSER_ACTION_SAVE,
287                                  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
288                                  GTK_STOCK_SAVE,   GTK_RESPONSE_ACCEPT,
289                                  NULL);
290
291   filter = gtk_file_filter_new ();
292   gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
293   gtk_file_filter_add_pattern (filter, "*.sps");
294   gtk_file_filter_add_pattern (filter, "*.SPS");
295   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
296
297   filter = gtk_file_filter_new ();
298   gtk_file_filter_set_name (filter, _("All Files"));
299   gtk_file_filter_add_pattern (filter, "*");
300   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
301
302   gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
303                                                   TRUE);
304   response = gtk_dialog_run (GTK_DIALOG (dialog));
305
306   if ( response == GTK_RESPONSE_ACCEPT )
307     {
308       GError *err = NULL;
309       char *filename =
310         gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
311
312       if ( ! save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err) )
313         {
314           msg ( ME, "%s", err->message );
315           g_error_free (err);
316         }
317
318       free (filename);
319     }
320
321   gtk_widget_destroy (dialog);
322 }
323
324
325 /* Callback for the File->Save menuitem */
326 static void
327 syntax_save (PsppireWindow *se)
328 {
329   const gchar *filename = psppire_window_get_filename (se);
330
331   if ( filename == NULL )
332     syntax_save_as (se);
333   else
334     {
335       GError *err = NULL;
336       save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
337       if ( err )
338         {
339           msg (ME, "%s", err->message);
340           g_error_free (err);
341         }
342     }
343 }
344
345
346 /* Callback for the File->Quit menuitem */
347 static gboolean
348 on_quit (GtkMenuItem *menuitem, gpointer    user_data)
349 {
350   psppire_quit ();
351
352   return FALSE;
353 }
354
355
356 void
357 create_syntax_window (void)
358 {
359   GtkWidget *w = psppire_syntax_window_new ();
360   gtk_widget_show (w);
361 }
362
363 void
364 open_syntax_window (const char *file_name)
365 {
366   GtkWidget *se = psppire_syntax_window_new ();
367
368   if ( psppire_window_load (PSPPIRE_WINDOW (se), file_name) )
369     gtk_widget_show (se);
370   else
371     gtk_widget_destroy (se);
372 }
373
374 static void
375 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
376 {
377   gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
378 }
379
380 static void
381 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
382 {
383   if (gtk_text_buffer_get_modified (buffer))
384     psppire_window_set_unsaved (window);
385 }
386
387 extern struct source_stream *the_source_stream ;
388
389 static void
390 psppire_syntax_window_init (PsppireSyntaxWindow *window)
391 {
392   GtkBuilder *xml = builder_new ("syntax-editor.ui");
393   GtkWidget *box = gtk_vbox_new (FALSE, 0);
394
395   GtkWidget *menubar = get_widget_assert (xml, "menubar");
396   GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
397
398
399   GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
400   window->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
401   window->lexer = lex_create (the_source_stream);
402
403   window->sb = get_widget_assert (xml, "statusbar2");
404   window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
405
406   g_signal_connect (window->buffer, "changed", G_CALLBACK (on_text_changed), window);
407
408   g_signal_connect (window->buffer, "modified-changed",
409                     G_CALLBACK (on_modified_changed), window);
410
411   connect_help (xml);
412
413   gtk_container_add (GTK_CONTAINER (window), box);
414
415   g_object_ref (menubar);
416
417   g_object_ref (sw);
418
419   g_object_ref (window->sb);
420
421
422   gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
423   gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
424   gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
425
426   gtk_widget_show_all (box);
427
428   g_signal_connect_swapped (get_action_assert (xml,"file_new_syntax"), "activate", G_CALLBACK (create_syntax_window), NULL);
429
430 #if 0
431   g_signal_connect (get_action_assert (xml,"file_new_data"),
432                     "activate",
433                     G_CALLBACK (create_data_window),
434                     window);
435 #endif
436
437   g_signal_connect_swapped (get_action_assert (xml, "file_save"),
438                     "activate",
439                     G_CALLBACK (syntax_save),
440                     window);
441
442   g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
443                     "activate",
444                     G_CALLBACK (syntax_save_as),
445                     window);
446
447   g_signal_connect (get_action_assert (xml,"file_quit"),
448                     "activate",
449                     G_CALLBACK (on_quit),
450                     window);
451
452   g_signal_connect (get_action_assert (xml,"run_all"),
453                     "activate",
454                     G_CALLBACK (on_run_all),
455                     window);
456
457
458   g_signal_connect (get_action_assert (xml,"run_selection"),
459                     "activate",
460                     G_CALLBACK (on_run_selection),
461                     window);
462
463   g_signal_connect (get_action_assert (xml,"run_current_line"),
464                     "activate",
465                     G_CALLBACK (on_run_current_line),
466                     window);
467
468   g_signal_connect (get_action_assert (xml,"run_to_end"),
469                     "activate",
470                     G_CALLBACK (on_run_to_end),
471                     window);
472
473   g_signal_connect (get_action_assert (xml,"windows_minimise_all"),
474                     "activate",
475                     G_CALLBACK (psppire_window_minimise_all), NULL);
476
477
478   {
479   GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
480
481   merge_help_menu (uim);
482
483   PSPPIRE_WINDOW (window)->menu =
484     GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
485   }
486
487   g_object_unref (xml);
488 }
489
490
491 GtkWidget*
492 psppire_syntax_window_new (void)
493 {
494   return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
495                                    "filename", "Syntax",
496                                    "description", _("Syntax Editor"),
497                                    NULL));
498 }
499
500 static void
501 error_dialog (GtkWindow *w, const gchar *filename,  GError *err)
502 {
503   gchar *fn = g_filename_display_basename (filename);
504
505   GtkWidget *dialog =
506     gtk_message_dialog_new (w,
507                             GTK_DIALOG_DESTROY_WITH_PARENT,
508                             GTK_MESSAGE_ERROR,
509                             GTK_BUTTONS_CLOSE,
510                             _("Cannot load syntax file '%s'"),
511                             fn);
512
513   g_free (fn);
514
515   g_object_set (dialog, "icon-name", "psppicon", NULL);
516
517   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
518                                             "%s", err->message);
519
520   gtk_dialog_run (GTK_DIALOG (dialog));
521
522   gtk_widget_destroy (dialog);
523 }
524
525 /*
526   Loads the buffer from the file called FILENAME
527 */
528 gboolean
529 syntax_load (PsppireWindow *window, const gchar *filename)
530 {
531   GError *err = NULL;
532   gchar *text_locale = NULL;
533   gchar *text_utf8 = NULL;
534   gsize len_locale = -1;
535   gsize len_utf8 = -1;
536   GtkTextIter iter;
537   PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
538
539   /* FIXME: What if it's a very big file ? */
540   if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
541     {
542       error_dialog (GTK_WINDOW (window), filename, err);
543       g_clear_error (&err);
544       return FALSE;
545     }
546
547   text_utf8 = g_locale_to_utf8 (text_locale, len_locale, NULL, &len_utf8, &err);
548
549   free (text_locale);
550
551   if ( text_utf8 == NULL )
552     {
553       error_dialog (GTK_WINDOW (window), filename, err);
554       g_clear_error (&err);
555       return FALSE;
556     }
557
558   gtk_text_buffer_get_iter_at_line (sw->buffer, &iter, 0);
559
560   gtk_text_buffer_insert (sw->buffer, &iter, text_utf8, len_utf8);
561
562   gtk_text_buffer_set_modified (sw->buffer, FALSE);
563
564   free (text_utf8);
565
566   return TRUE;
567 }
568
569 \f
570
571 static void
572 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
573 {
574   iface->save = syntax_save;
575   iface->load = syntax_load;
576 }