Merge commit 'window-manager/master'
[pspp] / src / ui / gui / psppire-syntax-window.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008  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 "helper.h"
22
23 #include <libpspp/message.h>
24 #include <stdlib.h>
25
26 #include "psppire-syntax-window.h"
27
28 #include "psppire-data-window.h"
29 #include "psppire-window-register.h"
30 #include "about.h"
31 #include "psppire-syntax-window.h"
32 #include "syntax-editor-source.h"
33 #include <language/lexer/lexer.h>
34
35 #include <gettext.h>
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) msgid
38
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);
43
44 GType
45 psppire_syntax_window_get_type (void)
46 {
47   static GType psppire_syntax_window_type = 0;
48
49   if (!psppire_syntax_window_type)
50     {
51       static const GTypeInfo psppire_syntax_window_info =
52       {
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,
58         NULL,
59         sizeof (PsppireSyntaxWindow),
60         0,
61         (GInstanceInitFunc) psppire_syntax_window_init,
62       };
63
64       psppire_syntax_window_type =
65         g_type_register_static (PSPPIRE_WINDOW_TYPE, "PsppireSyntaxWindow",
66                                 &psppire_syntax_window_info, 0);
67     }
68
69   return psppire_syntax_window_type;
70 }
71
72 static GObjectClass *parent_class ;
73
74 static void
75 psppire_syntax_window_finalize (GObject *object)
76 {
77   if (G_OBJECT_CLASS (parent_class)->finalize)
78     (*G_OBJECT_CLASS (parent_class)->finalize) (object);
79 }
80
81
82 static void
83 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
84 {
85   parent_class = g_type_class_peek_parent (class);
86 }
87
88
89 static void
90 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
91 {
92   GObjectClass *object_class = G_OBJECT_CLASS (class);
93
94   object_class->finalize = psppire_syntax_window_finalize;
95 }
96
97
98
99 static void
100 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
101                                      gpointer class_data)
102 {
103 }
104
105
106 static void
107 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
108                        GtkTextIter stop)
109 {
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));
113 }
114
115
116 /* Parse and execute all the text in the buffer */
117 static void
118 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
119 {
120   GtkTextIter begin, end;
121   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
122
123   gtk_text_buffer_get_iter_at_offset (se->buffer, &begin, 0);
124   gtk_text_buffer_get_iter_at_offset (se->buffer, &end, -1);
125
126   editor_execute_syntax (se, begin, end);
127 }
128
129 /* Parse and execute the currently selected text */
130 static void
131 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
132 {
133   GtkTextIter begin, end;
134   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
135
136   if ( gtk_text_buffer_get_selection_bounds (se->buffer, &begin, &end) )
137     editor_execute_syntax (se, begin, end);
138 }
139
140
141 /* Parse and execute the from the current line, to the end of the
142    buffer */
143 static void
144 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
145 {
146   GtkTextIter begin, end;
147   GtkTextIter here;
148   gint line;
149
150   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
151
152   /* Get the current line */
153   gtk_text_buffer_get_iter_at_mark (se->buffer,
154                                     &here,
155                                     gtk_text_buffer_get_insert (se->buffer)
156                                     );
157
158   line = gtk_text_iter_get_line (&here) ;
159
160   /* Now set begin and end to the start of this line, and end of buffer
161      respectively */
162   gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
163   gtk_text_buffer_get_iter_at_line (se->buffer, &end, -1);
164
165   editor_execute_syntax (se, begin, end);
166 }
167
168
169
170 /* Parse and execute the current line */
171 static void
172 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
173 {
174   GtkTextIter begin, end;
175   GtkTextIter here;
176   gint line;
177
178   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
179
180   /* Get the current line */
181   gtk_text_buffer_get_iter_at_mark (se->buffer,
182                                     &here,
183                                     gtk_text_buffer_get_insert (se->buffer)
184                                     );
185
186   line = gtk_text_iter_get_line (&here) ;
187
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);
192
193   editor_execute_syntax (se, begin, end);
194 }
195
196
197
198 /* Append ".sps" to FILENAME if necessary.
199    The returned result must be freed when no longer required.
200  */
201 static gchar *
202 append_suffix (const gchar *filename)
203 {
204   if ( ! g_str_has_suffix (filename, ".sps" ) &&
205        ! g_str_has_suffix (filename, ".SPS" ) )
206     {
207       return g_strdup_printf ("%s.sps", filename);
208     }
209
210   return strdup (filename);
211 }
212
213 /*
214   Save BUFFER to the file called FILENAME.
215   If successful, clears the buffer's modified flag
216 */
217 static gboolean
218 save_editor_to_file (PsppireSyntaxWindow *se,
219                      const gchar *filename,
220                      GError **err)
221 {
222   GtkTextBuffer *buffer = se->buffer;
223   gboolean result ;
224   GtkTextIter start, stop;
225   gchar *text;
226
227   gchar *suffixedname;
228   gchar *glibfilename;
229   g_assert (filename);
230
231   suffixedname = append_suffix (filename);
232
233   glibfilename = g_filename_from_utf8 (suffixedname, -1, 0, 0, err);
234
235   g_free ( suffixedname);
236
237   if ( ! glibfilename )
238     return FALSE;
239
240   gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
241   gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
242
243   text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
244
245   result =  g_file_set_contents (glibfilename, text, -1, err);
246
247   if ( result )
248     {
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);
252       g_free (msg);
253     }
254
255   return result;
256 }
257
258 /* If the buffer's modified flag is set, then save it, and close the window.
259    Otherwise just close the window.
260 */
261 static void
262 save_if_modified (PsppireSyntaxWindow *se)
263 {
264   if ( TRUE == gtk_text_buffer_get_modified (se->buffer))
265     {
266       gint response;
267       GtkWidget *dialog;
268
269       const gchar *filename = psppire_window_get_filename (PSPPIRE_WINDOW (se));
270
271       g_return_if_fail (filename != NULL);
272
273       dialog =
274         gtk_message_dialog_new (GTK_WINDOW (se),
275                                 GTK_DIALOG_MODAL,
276                                 GTK_MESSAGE_QUESTION,
277                                 GTK_BUTTONS_NONE,
278                                 _("Save contents of syntax editor to %s?"),
279                                 filename);
280
281       gtk_dialog_add_button  (GTK_DIALOG (dialog),
282                               GTK_STOCK_YES,
283                               GTK_RESPONSE_ACCEPT);
284
285       gtk_dialog_add_button  (GTK_DIALOG (dialog),
286                               GTK_STOCK_NO,
287                               GTK_RESPONSE_REJECT);
288
289       gtk_dialog_add_button  (GTK_DIALOG (dialog),
290                               GTK_STOCK_CANCEL,
291                               GTK_RESPONSE_CANCEL);
292
293       response = gtk_dialog_run (GTK_DIALOG (dialog));
294
295       gtk_widget_destroy (dialog);
296
297       if ( response == GTK_RESPONSE_ACCEPT )
298         {
299           GError *err = NULL;
300
301           if ( ! save_editor_to_file (se, filename, &err) )
302             {
303               msg (ME, err->message);
304               g_error_free (err);
305             }
306           psppire_window_set_filename (PSPPIRE_WINDOW (se), filename);
307         }
308
309       if ( response == GTK_RESPONSE_CANCEL )
310         return ;
311     }
312
313   gtk_widget_destroy (GTK_WIDGET (se));
314 }
315
316 /* Callback for the File->SaveAs menuitem */
317 static void
318 on_syntax_save_as (GtkMenuItem *menuitem, gpointer user_data)
319 {
320   GtkFileFilter *filter;
321   gint response;
322
323   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
324
325   GtkWidget *dialog =
326     gtk_file_chooser_dialog_new (_("Save Syntax"),
327                                  GTK_WINDOW (se),
328                                  GTK_FILE_CHOOSER_ACTION_SAVE,
329                                  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
330                                  GTK_STOCK_SAVE,   GTK_RESPONSE_ACCEPT,
331                                  NULL);
332
333   filter = gtk_file_filter_new ();
334   gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
335   gtk_file_filter_add_pattern (filter, "*.sps");
336   gtk_file_filter_add_pattern (filter, "*.SPS");
337   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
338
339   filter = gtk_file_filter_new ();
340   gtk_file_filter_set_name (filter, _("All Files"));
341   gtk_file_filter_add_pattern (filter, "*");
342   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
343
344   gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
345                                                   TRUE);
346   response = gtk_dialog_run (GTK_DIALOG (dialog));
347
348   if ( response == GTK_RESPONSE_ACCEPT )
349     {
350       GError *err = NULL;
351       char *filename =
352         gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
353
354       if ( ! save_editor_to_file (se, filename, &err) )
355         {
356           msg ( ME, err->message );
357           g_error_free (err);
358         }
359
360       free (filename);
361     }
362
363   gtk_widget_destroy (dialog);
364 }
365
366
367 /* Callback for the File->Save menuitem */
368 static void
369 on_syntax_save (GtkMenuItem *menuitem, gpointer user_data)
370 {
371   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
372   const gchar *filename = psppire_window_get_filename (PSPPIRE_WINDOW (se));
373
374
375   if ( filename == NULL )
376     on_syntax_save_as (menuitem, se);
377   else
378     {
379       GError *err = NULL;
380       save_editor_to_file (se, filename, &err);
381       if ( err )
382         {
383           msg (ME, err->message);
384           g_error_free (err);
385         }
386     }
387 }
388
389
390 /* Callback for the File->Quit menuitem */
391 static gboolean
392 on_quit (GtkMenuItem *menuitem, gpointer    user_data)
393 {
394   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
395   save_if_modified (se);
396   return FALSE;
397 }
398
399
400 /* Callback for the "delete" action (clicking the x on the top right
401    hand corner of the window) */
402 static gboolean
403 on_delete (GtkWidget *w, GdkEvent *event, gpointer user_data)
404 {
405   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
406
407   save_if_modified (se);
408   return TRUE;
409 }
410
411
412 void
413 create_syntax_window (void)
414 {
415   GtkWidget *w = psppire_syntax_window_new ();
416   gtk_widget_show (w);
417 }
418
419 /* Callback for the File->Open->Syntax menuitem */
420 void
421 open_syntax_window (GtkMenuItem *menuitem, gpointer parent)
422 {
423   GtkFileFilter *filter;
424   gint response;
425
426   GtkWidget *dialog =
427     gtk_file_chooser_dialog_new (_("Open Syntax"),
428                                  GTK_WINDOW (parent),
429                                  GTK_FILE_CHOOSER_ACTION_OPEN,
430                                  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
431                                  GTK_STOCK_OPEN,   GTK_RESPONSE_ACCEPT,
432                                  NULL);
433
434   filter = gtk_file_filter_new ();
435   gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
436   gtk_file_filter_add_pattern (filter, "*.sps");
437   gtk_file_filter_add_pattern (filter, "*.SPS");
438   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
439
440   filter = gtk_file_filter_new ();
441   gtk_file_filter_set_name (filter, _("All Files"));
442   gtk_file_filter_add_pattern (filter, "*");
443   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
444
445   response = gtk_dialog_run (GTK_DIALOG (dialog));
446
447   if (response == GTK_RESPONSE_ACCEPT)
448     {
449       const char *file_name = gtk_file_chooser_get_filename
450         (GTK_FILE_CHOOSER (dialog));
451
452       GtkWidget *se = psppire_syntax_window_new ();
453
454       if ( psppire_syntax_window_load_from_file (PSPPIRE_SYNTAX_WINDOW (se), file_name, NULL) )
455 #if RECENT_LISTS_AVAILABLE
456       {
457         GtkRecentManager *manager = gtk_recent_manager_get_default();
458         gchar *uri = g_filename_to_uri (file_name, NULL, NULL);
459
460         gtk_recent_manager_remove_item (manager, uri, NULL);
461         if ( ! gtk_recent_manager_add_item (manager, uri))
462           g_warning ("Could not add item %s to recent list\n",uri);
463
464         g_free (uri);
465       }
466 #else
467       ;
468 #endif
469       gtk_widget_show (se);
470     }
471
472   gtk_widget_destroy (dialog);
473 }
474
475
476 static void
477 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
478 {
479   gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
480 }
481
482 extern struct source_stream *the_source_stream ;
483
484 static void
485 psppire_syntax_window_init (PsppireSyntaxWindow *window)
486 {
487   GtkBuilder *xml = builder_new ("syntax-editor.ui");
488   GtkWidget *box = gtk_vbox_new (FALSE, 0);
489
490   GtkWidget *menubar = get_widget_assert (xml, "menubar2");
491   GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
492
493
494   GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
495   window->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
496   window->lexer = lex_create (the_source_stream);
497
498   window->sb = get_widget_assert (xml, "statusbar2");
499   window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
500
501   g_signal_connect (window->buffer, "changed", G_CALLBACK (on_text_changed), window);
502
503   connect_help (xml);
504
505   gtk_container_add (GTK_CONTAINER (window), box);
506
507   g_object_ref (menubar);
508
509   g_object_ref (sw);
510
511   g_object_ref (window->sb);
512
513
514   gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
515   gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
516   gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
517
518   gtk_widget_show_all (box);
519
520   g_signal_connect (get_object_assert (xml,"file_new_syntax"),
521                     "activate",
522                     G_CALLBACK (create_syntax_window),
523                     NULL);
524
525   g_signal_connect (get_object_assert (xml,"file_open_syntax"),
526                     "activate",
527                     G_CALLBACK (open_syntax_window),
528                     window);
529
530 #if 0
531   g_signal_connect (get_object_assert (xml,"file_new_data"),
532                     "activate",
533                     G_CALLBACK (create_data_window),
534                     window);
535 #endif
536
537   g_signal_connect (get_object_assert (xml,"help_about"),
538                     "activate",
539                     G_CALLBACK (about_new),
540                     window);
541
542   g_signal_connect (get_object_assert (xml,"help_reference"),
543                     "activate",
544                     G_CALLBACK (reference_manual),
545                     NULL);
546
547   g_signal_connect (get_object_assert (xml, "file_save"),
548                     "activate",
549                     G_CALLBACK (on_syntax_save),
550                     window);
551
552   g_signal_connect (get_object_assert (xml, "file_save_as"),
553                     "activate",
554                     G_CALLBACK (on_syntax_save_as),
555                     window);
556
557   g_signal_connect (get_object_assert (xml,"file_quit"),
558                     "activate",
559                     G_CALLBACK (on_quit),
560                     window);
561
562   g_signal_connect (get_object_assert (xml,"run_all"),
563                     "activate",
564                     G_CALLBACK (on_run_all),
565                     window);
566
567
568   g_signal_connect (get_object_assert (xml,"run_selection"),
569                     "activate",
570                     G_CALLBACK (on_run_selection),
571                     window);
572
573   g_signal_connect (get_object_assert (xml,"run_current_line"),
574                     "activate",
575                     G_CALLBACK (on_run_current_line),
576                     window);
577
578   g_signal_connect (get_object_assert (xml,"run_to_end"),
579                     "activate",
580                     G_CALLBACK (on_run_to_end),
581                     window);
582
583   g_signal_connect (get_object_assert (xml,"windows_minimise_all"),
584                     "activate",
585                     G_CALLBACK (psppire_window_minimise_all), NULL);
586
587   GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1"));
588
589   PSPPIRE_WINDOW (window)->menu =
590     GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar2/windows/windows_minimise_all")->parent);
591
592
593   g_object_unref (xml);
594
595   g_signal_connect (window, "delete-event",
596                     G_CALLBACK (on_delete), window);
597 }
598
599
600 GtkWidget*
601 psppire_syntax_window_new (void)
602 {
603   return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
604                                    "filename", "Syntax",
605                                    "description", _("Syntax Editor"),
606                                    NULL));
607 }
608
609
610 /*
611   Loads the buffer from the file called FILENAME
612 */
613 gboolean
614 psppire_syntax_window_load_from_file (PsppireSyntaxWindow *se,
615                                       const gchar *filename,
616                                       GError **err)
617 {
618   gchar *text;
619   GtkTextIter iter;
620
621   gchar *glibfilename = g_filename_from_utf8 (filename, -1, 0, 0, err);
622
623   if ( ! glibfilename )
624     return FALSE;
625
626   /* FIXME: What if it's a very big file ? */
627   if ( ! g_file_get_contents (glibfilename, &text, NULL, err) )
628     {
629       g_free (glibfilename);
630       return FALSE;
631     }
632   g_free (glibfilename);
633
634   gtk_text_buffer_get_iter_at_line (se->buffer, &iter, 0);
635
636   gtk_text_buffer_insert (se->buffer, &iter, text, -1);
637
638   psppire_window_set_filename (PSPPIRE_WINDOW (se), filename);
639
640   gtk_text_buffer_set_modified (se->buffer, FALSE);
641
642   return TRUE;
643 }
644