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