Added stock icon to About menuitem
[pspp-builds.git] / 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.h"
27 #include "psppire-syntax-window.h"
28
29 #include "psppire-data-window.h"
30 #include "psppire-window-register.h"
31 #include "psppire.h"
32 #include "about.h"
33 #include "psppire-syntax-window.h"
34 #include "syntax-editor-source.h"
35 #include <language/lexer/lexer.h>
36
37 #include <gettext.h>
38 #define _(msgid) gettext (msgid)
39 #define N_(msgid) msgid
40
41 static void psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *, gpointer);
42 static void psppire_syntax_window_base_init     (PsppireSyntaxWindowClass *class);
43 static void psppire_syntax_window_class_init    (PsppireSyntaxWindowClass *class);
44 static void psppire_syntax_window_init          (PsppireSyntaxWindow      *syntax_editor);
45
46
47 static void psppire_syntax_window_iface_init (PsppireWindowIface *iface);
48
49
50 GType
51 psppire_syntax_window_get_type (void)
52 {
53   static GType psppire_syntax_window_type = 0;
54
55   if (!psppire_syntax_window_type)
56     {
57       static const GTypeInfo psppire_syntax_window_info =
58       {
59         sizeof (PsppireSyntaxWindowClass),
60         (GBaseInitFunc) psppire_syntax_window_base_init,
61         (GBaseFinalizeFunc) psppire_syntax_window_base_finalize,
62         (GClassInitFunc)psppire_syntax_window_class_init,
63         (GClassFinalizeFunc) NULL,
64         NULL,
65         sizeof (PsppireSyntaxWindow),
66         0,
67         (GInstanceInitFunc) psppire_syntax_window_init,
68       };
69
70       static const GInterfaceInfo window_interface_info =
71         {
72           (GInterfaceInitFunc) psppire_syntax_window_iface_init,
73           NULL,
74           NULL
75         };
76
77       psppire_syntax_window_type =
78         g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireSyntaxWindow",
79                                 &psppire_syntax_window_info, 0);
80
81       g_type_add_interface_static (psppire_syntax_window_type,
82                                    PSPPIRE_TYPE_WINDOW_MODEL,
83                                    &window_interface_info);
84     }
85
86   return psppire_syntax_window_type;
87 }
88
89 static GObjectClass *parent_class ;
90
91 static void
92 psppire_syntax_window_finalize (GObject *object)
93 {
94   if (G_OBJECT_CLASS (parent_class)->finalize)
95     (*G_OBJECT_CLASS (parent_class)->finalize) (object);
96 }
97
98
99 static void
100 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
101 {
102   parent_class = g_type_class_peek_parent (class);
103 }
104
105
106 static void
107 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
108 {
109   GObjectClass *object_class = G_OBJECT_CLASS (class);
110
111   object_class->finalize = psppire_syntax_window_finalize;
112 }
113
114
115
116 static void
117 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
118                                      gpointer class_data)
119 {
120 }
121
122
123 static void
124 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
125                        GtkTextIter stop)
126 {
127   PsppireWindow *win = PSPPIRE_WINDOW (sw);
128   const gchar *name = psppire_window_get_filename (win);
129   execute_syntax (create_syntax_editor_source (sw->buffer, start, stop, name));
130 }
131
132
133 /* Parse and execute all the text in the buffer */
134 static void
135 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
136 {
137   GtkTextIter begin, end;
138   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
139
140   gtk_text_buffer_get_iter_at_offset (se->buffer, &begin, 0);
141   gtk_text_buffer_get_iter_at_offset (se->buffer, &end, -1);
142
143   editor_execute_syntax (se, begin, end);
144 }
145
146 /* Parse and execute the currently selected text */
147 static void
148 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
149 {
150   GtkTextIter begin, end;
151   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
152
153   if ( gtk_text_buffer_get_selection_bounds (se->buffer, &begin, &end) )
154     editor_execute_syntax (se, begin, end);
155 }
156
157
158 /* Parse and execute the from the current line, to the end of the
159    buffer */
160 static void
161 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
162 {
163   GtkTextIter begin, end;
164   GtkTextIter here;
165   gint line;
166
167   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
168
169   /* Get the current line */
170   gtk_text_buffer_get_iter_at_mark (se->buffer,
171                                     &here,
172                                     gtk_text_buffer_get_insert (se->buffer)
173                                     );
174
175   line = gtk_text_iter_get_line (&here) ;
176
177   /* Now set begin and end to the start of this line, and end of buffer
178      respectively */
179   gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
180   gtk_text_buffer_get_iter_at_line (se->buffer, &end, -1);
181
182   editor_execute_syntax (se, begin, end);
183 }
184
185
186
187 /* Parse and execute the current line */
188 static void
189 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
190 {
191   GtkTextIter begin, end;
192   GtkTextIter here;
193   gint line;
194
195   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
196
197   /* Get the current line */
198   gtk_text_buffer_get_iter_at_mark (se->buffer,
199                                     &here,
200                                     gtk_text_buffer_get_insert (se->buffer)
201                                     );
202
203   line = gtk_text_iter_get_line (&here) ;
204
205   /* Now set begin and end to the start of this line, and start of
206      following line respectively */
207   gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
208   gtk_text_buffer_get_iter_at_line (se->buffer, &end, line + 1);
209
210   editor_execute_syntax (se, begin, end);
211 }
212
213
214
215 /* Append ".sps" to FILENAME if necessary.
216    The returned result must be freed when no longer required.
217  */
218 static gchar *
219 append_suffix (const gchar *filename)
220 {
221   if ( ! g_str_has_suffix (filename, ".sps" ) &&
222        ! g_str_has_suffix (filename, ".SPS" ) )
223     {
224       return g_strdup_printf ("%s.sps", filename);
225     }
226
227   return strdup (filename);
228 }
229
230 /*
231   Save BUFFER to the file called FILENAME.
232   If successful, clears the buffer's modified flag
233 */
234 static gboolean
235 save_editor_to_file (PsppireSyntaxWindow *se,
236                      const gchar *filename,
237                      GError **err)
238 {
239   GtkTextBuffer *buffer = se->buffer;
240   gboolean result ;
241   GtkTextIter start, stop;
242   gchar *text;
243
244   gchar *suffixedname;
245   gchar *glibfilename;
246   g_assert (filename);
247
248   suffixedname = append_suffix (filename);
249
250   glibfilename = g_filename_from_utf8 (suffixedname, -1, 0, 0, err);
251
252   g_free ( suffixedname);
253
254   if ( ! glibfilename )
255     return FALSE;
256
257   gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
258   gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
259
260   text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
261
262   result =  g_file_set_contents (glibfilename, text, -1, err);
263
264   if ( result )
265     {
266       gchar *msg = g_strdup_printf (_("Saved file \"%s\""), filename);
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, 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, 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 /* Callback for the File->Open->Syntax menuitem */
364 void
365 open_syntax_window (GtkMenuItem *menuitem, gpointer parent)
366 {
367   GtkFileFilter *filter;
368   gint response;
369
370   GtkWidget *dialog =
371     gtk_file_chooser_dialog_new (_("Open Syntax"),
372                                  GTK_WINDOW (parent),
373                                  GTK_FILE_CHOOSER_ACTION_OPEN,
374                                  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
375                                  GTK_STOCK_OPEN,   GTK_RESPONSE_ACCEPT,
376                                  NULL);
377
378   filter = gtk_file_filter_new ();
379   gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
380   gtk_file_filter_add_pattern (filter, "*.sps");
381   gtk_file_filter_add_pattern (filter, "*.SPS");
382   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
383
384   filter = gtk_file_filter_new ();
385   gtk_file_filter_set_name (filter, _("All Files"));
386   gtk_file_filter_add_pattern (filter, "*");
387   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
388
389   response = gtk_dialog_run (GTK_DIALOG (dialog));
390
391   if (response == GTK_RESPONSE_ACCEPT)
392     {
393       const char *file_name = gtk_file_chooser_get_filename
394         (GTK_FILE_CHOOSER (dialog));
395
396       GtkWidget *se = psppire_syntax_window_new ();
397
398       if ( psppire_syntax_window_load_from_file (PSPPIRE_SYNTAX_WINDOW (se), file_name, NULL) )
399         {
400           add_most_recent (file_name, the_recent_mgr);
401         }
402
403       gtk_widget_show (se);
404     }
405
406   gtk_widget_destroy (dialog);
407 }
408
409
410 static void
411 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
412 {
413   gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
414 }
415
416 static void
417 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
418 {
419   psppire_window_set_unsaved (window, gtk_text_buffer_get_modified (buffer));
420 }
421
422
423 extern struct source_stream *the_source_stream ;
424
425 static void
426 psppire_syntax_window_init (PsppireSyntaxWindow *window)
427 {
428   GtkBuilder *xml = builder_new ("syntax-editor.ui");
429   GtkWidget *box = gtk_vbox_new (FALSE, 0);
430
431   GtkWidget *menubar = get_widget_assert (xml, "menubar2");
432   GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
433
434
435   GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
436   window->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
437   window->lexer = lex_create (the_source_stream);
438
439   window->sb = get_widget_assert (xml, "statusbar2");
440   window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
441
442   g_signal_connect (window->buffer, "changed", G_CALLBACK (on_text_changed), window);
443
444   g_signal_connect (window->buffer, "modified-changed",
445                     G_CALLBACK (on_modified_changed), window);
446
447   connect_help (xml);
448
449   gtk_container_add (GTK_CONTAINER (window), box);
450
451   g_object_ref (menubar);
452
453   g_object_ref (sw);
454
455   g_object_ref (window->sb);
456
457
458   gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
459   gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
460   gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
461
462   gtk_widget_show_all (box);
463
464   g_signal_connect (get_action_assert (xml,"file_new_syntax"),
465                     "activate",
466                     G_CALLBACK (create_syntax_window),
467                     NULL);
468
469   g_signal_connect (get_action_assert (xml,"file_open_syntax"),
470                     "activate",
471                     G_CALLBACK (open_syntax_window),
472                     window);
473
474 #if 0
475   g_signal_connect (get_action_assert (xml,"file_new_data"),
476                     "activate",
477                     G_CALLBACK (create_data_window),
478                     window);
479 #endif
480
481   {
482     GtkAction *abt = get_action_assert (xml, "help_about");
483     g_object_set (abt, "stock-id", "gtk-about", NULL);
484
485     g_signal_connect (abt,
486                       "activate",
487                       G_CALLBACK (about_new),
488                       window);
489   }
490
491   g_signal_connect (get_action_assert (xml,"help_reference"),
492                     "activate",
493                     G_CALLBACK (reference_manual),
494                     NULL);
495
496   g_signal_connect_swapped (get_action_assert (xml, "file_save"),
497                     "activate",
498                     G_CALLBACK (syntax_save),
499                     window);
500
501   g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
502                     "activate",
503                     G_CALLBACK (syntax_save_as),
504                     window);
505
506   g_signal_connect (get_action_assert (xml,"file_quit"),
507                     "activate",
508                     G_CALLBACK (on_quit),
509                     window);
510
511   g_signal_connect (get_action_assert (xml,"run_all"),
512                     "activate",
513                     G_CALLBACK (on_run_all),
514                     window);
515
516
517   g_signal_connect (get_action_assert (xml,"run_selection"),
518                     "activate",
519                     G_CALLBACK (on_run_selection),
520                     window);
521
522   g_signal_connect (get_action_assert (xml,"run_current_line"),
523                     "activate",
524                     G_CALLBACK (on_run_current_line),
525                     window);
526
527   g_signal_connect (get_action_assert (xml,"run_to_end"),
528                     "activate",
529                     G_CALLBACK (on_run_to_end),
530                     window);
531
532   g_signal_connect (get_action_assert (xml,"windows_minimise_all"),
533                     "activate",
534                     G_CALLBACK (psppire_window_minimise_all), NULL);
535
536
537   {
538   GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
539
540   PSPPIRE_WINDOW (window)->menu =
541     GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar2/windows/windows_minimise_all")->parent);
542   }
543
544   g_object_unref (xml);
545 }
546
547
548 GtkWidget*
549 psppire_syntax_window_new (void)
550 {
551   return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
552                                    "filename", "Syntax",
553                                    "description", _("Syntax Editor"),
554                                    NULL));
555 }
556
557
558 /*
559   Loads the buffer from the file called FILENAME
560 */
561 gboolean
562 psppire_syntax_window_load_from_file (PsppireSyntaxWindow *se,
563                                       const gchar *filename,
564                                       GError **err)
565 {
566   gchar *text;
567   GtkTextIter iter;
568
569   gchar *glibfilename = g_filename_from_utf8 (filename, -1, 0, 0, err);
570
571   if ( ! glibfilename )
572     return FALSE;
573
574   /* FIXME: What if it's a very big file ? */
575   if ( ! g_file_get_contents (glibfilename, &text, NULL, err) )
576     {
577       g_free (glibfilename);
578       return FALSE;
579     }
580   g_free (glibfilename);
581
582   gtk_text_buffer_get_iter_at_line (se->buffer, &iter, 0);
583
584   gtk_text_buffer_insert (se->buffer, &iter, text, -1);
585
586   psppire_window_set_filename (PSPPIRE_WINDOW (se), filename);
587
588   gtk_text_buffer_set_modified (se->buffer, FALSE);
589
590   return TRUE;
591 }
592
593 \f
594
595 static void
596 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
597 {
598   iface->save = syntax_save;
599 }