ccf94e7bf2f0298b7f38c9ca95b07905f744e07c
[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_dispose (GObject *obj)
104 {
105   PsppireSyntaxWindow *sw = (PsppireSyntaxWindow *)obj;
106
107   GtkClipboard *clip_selection;
108   GtkClipboard *clip_primary;
109
110   if (sw->dispose_has_run)
111     return;
112
113   clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
114   clip_primary =   gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_PRIMARY);
115
116   g_signal_handler_disconnect (clip_primary, sw->sel_handler);
117
118   g_signal_handler_disconnect (clip_selection, sw->ps_handler);
119
120   /* Make sure dispose does not run twice. */
121   sw->dispose_has_run = TRUE;
122
123   /* Chain up to the parent class */
124   G_OBJECT_CLASS (parent_class)->dispose (obj);
125 }
126
127
128
129 static void
130 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
131 {
132   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
133
134   parent_class = g_type_class_peek_parent (class);
135
136   gobject_class->dispose = psppire_syntax_window_dispose;
137 }
138
139
140 static void
141 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
142 {
143   GObjectClass *object_class = G_OBJECT_CLASS (class);
144
145   object_class->finalize = psppire_syntax_window_finalize;
146 }
147
148
149
150 static void
151 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
152                                      gpointer class_data)
153 {
154 }
155
156
157 static void
158 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
159                        GtkTextIter stop)
160 {
161   PsppireWindow *win = PSPPIRE_WINDOW (sw);
162   const gchar *name = psppire_window_get_filename (win);
163   execute_syntax (create_syntax_editor_source (sw->buffer, start, stop, name));
164 }
165
166
167 \f
168
169 /* Delete the currently selected text */
170 static void
171 on_edit_delete (PsppireSyntaxWindow *sw)
172 {
173   GtkTextIter begin, end;
174   
175   if ( gtk_text_buffer_get_selection_bounds (sw->buffer, &begin, &end) )
176     gtk_text_buffer_delete (sw->buffer, &begin, &end);
177 }
178
179
180
181
182 /* The syntax editor's clipboard deals only with text */
183 enum {
184   SELECT_FMT_NULL,
185   SELECT_FMT_TEXT,
186 };
187
188
189 static void
190 selection_changed (PsppireSyntaxWindow *sw)
191 {
192   gboolean sel = gtk_text_buffer_get_has_selection (sw->buffer);
193
194   gtk_action_set_sensitive (sw->edit_copy, sel);
195   gtk_action_set_sensitive (sw->edit_cut, sel);
196   gtk_action_set_sensitive (sw->edit_delete, sel);
197 }
198
199 /* The callback which runs when something request clipboard data */
200 static void
201 clipboard_get_cb (GtkClipboard     *clipboard,
202                   GtkSelectionData *selection_data,
203                   guint             info,
204                   gpointer          data)
205 {
206   PsppireSyntaxWindow *sw = data;
207   g_assert (info == SELECT_FMT_TEXT);
208
209   gtk_selection_data_set (selection_data, selection_data->target,
210                           8,
211                           (const guchar *) sw->cliptext, strlen (sw->cliptext));
212
213 }
214
215 static void
216 clipboard_clear_cb (GtkClipboard *clipboard,
217                     gpointer data)
218 {
219   PsppireSyntaxWindow *sw = data;
220   g_free (sw->cliptext);
221   sw->cliptext = NULL;
222 }
223
224
225 static const GtkTargetEntry targets[] = {
226   { "UTF8_STRING",   0, SELECT_FMT_TEXT },
227   { "STRING",        0, SELECT_FMT_TEXT },
228   { "TEXT",          0, SELECT_FMT_TEXT },
229   { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
230   { "text/plain;charset=utf-8", 0, SELECT_FMT_TEXT },
231   { "text/plain",    0, SELECT_FMT_TEXT },
232 };
233
234
235 /*
236   Store a clip containing the currently selected text.
237   Returns true iff something was set.
238   As a side effect, begin and end will be set to indicate
239   the limits of the selected text.
240 */
241 static gboolean
242 set_clip (PsppireSyntaxWindow *sw, GtkTextIter *begin, GtkTextIter *end)
243 {
244   GtkClipboard *clipboard ;
245
246   if ( ! gtk_text_buffer_get_selection_bounds (sw->buffer, begin, end) )
247     return FALSE;
248
249   g_free (sw->cliptext);
250   sw->cliptext = gtk_text_buffer_get_text  (sw->buffer, begin, end, FALSE);
251
252   clipboard =
253     gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
254
255   if (!gtk_clipboard_set_with_owner (clipboard, targets,
256                                      G_N_ELEMENTS (targets),
257                                      clipboard_get_cb, clipboard_clear_cb,
258                                      G_OBJECT (sw)))
259     clipboard_clear_cb (clipboard, sw);
260
261   return TRUE;
262 }
263
264 static void
265 on_edit_cut (PsppireSyntaxWindow *sw)
266 {
267   GtkTextIter begin, end;
268   
269   if ( set_clip (sw, &begin, &end))
270     gtk_text_buffer_delete (sw->buffer, &begin, &end);
271 }
272
273 static void
274 on_edit_copy (PsppireSyntaxWindow *sw)
275 {
276   GtkTextIter begin, end;
277
278   set_clip (sw, &begin, &end);
279 }
280
281
282 /* A callback for when the clipboard contents have been received */
283 static void
284 contents_received_callback (GtkClipboard *clipboard,
285                             GtkSelectionData *sd,
286                             gpointer data)
287 {
288   gchar *c;
289   PsppireSyntaxWindow *syntax_window = data;
290
291   if ( sd->length < 0 )
292     return;
293
294   if ( sd->type != gdk_atom_intern ("UTF8_STRING", FALSE))
295     return;
296
297   c = (gchar *) sd->data;
298
299   gtk_text_buffer_insert_at_cursor (syntax_window->buffer,
300                                     (gchar *) sd->data,
301                                     sd->length);
302
303 }
304
305 static void
306 on_edit_paste (PsppireSyntaxWindow *sw)
307 {
308   GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (sw));
309   GtkClipboard *clipboard =
310     gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
311
312   gtk_clipboard_request_contents (clipboard,
313                                   gdk_atom_intern ("UTF8_STRING", TRUE),
314                                   contents_received_callback,
315                                   sw);
316 }
317
318
319 /* Check to see if CLIP holds a target which we know how to paste,
320    and set the sensitivity of the Paste action accordingly.
321  */
322 static void
323 set_paste_sensitivity (GtkClipboard *clip, GdkEventOwnerChange *event, gpointer data)
324 {
325   gint i;
326   gboolean compatible_target = FALSE;
327   PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (data);
328
329   for (i = 0 ; i < sizeof (targets) / sizeof (targets[0]) ; ++i)
330     {
331       GdkAtom atom = gdk_atom_intern (targets[i].target, TRUE);
332       if ( gtk_clipboard_wait_is_target_available (clip, atom))
333         {
334           compatible_target = TRUE;
335           break;
336         }
337     }
338
339   gtk_action_set_sensitive (sw->edit_paste, compatible_target);
340 }
341
342
343 \f
344
345 /* Parse and execute all the text in the buffer */
346 static void
347 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
348 {
349   GtkTextIter begin, end;
350   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
351
352   gtk_text_buffer_get_iter_at_offset (se->buffer, &begin, 0);
353   gtk_text_buffer_get_iter_at_offset (se->buffer, &end, -1);
354
355   editor_execute_syntax (se, begin, end);
356 }
357
358 /* Parse and execute the currently selected text */
359 static void
360 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
361 {
362   GtkTextIter begin, end;
363   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
364
365   if ( gtk_text_buffer_get_selection_bounds (se->buffer, &begin, &end) )
366     editor_execute_syntax (se, begin, end);
367 }
368
369
370 /* Parse and execute the from the current line, to the end of the
371    buffer */
372 static void
373 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
374 {
375   GtkTextIter begin, end;
376   GtkTextIter here;
377   gint line;
378
379   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
380
381   /* Get the current line */
382   gtk_text_buffer_get_iter_at_mark (se->buffer,
383                                     &here,
384                                     gtk_text_buffer_get_insert (se->buffer)
385                                     );
386
387   line = gtk_text_iter_get_line (&here) ;
388
389   /* Now set begin and end to the start of this line, and end of buffer
390      respectively */
391   gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
392   gtk_text_buffer_get_iter_at_line (se->buffer, &end, -1);
393
394   editor_execute_syntax (se, begin, end);
395 }
396
397
398
399 /* Parse and execute the current line */
400 static void
401 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
402 {
403   GtkTextIter begin, end;
404   GtkTextIter here;
405   gint line;
406
407   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
408
409   /* Get the current line */
410   gtk_text_buffer_get_iter_at_mark (se->buffer,
411                                     &here,
412                                     gtk_text_buffer_get_insert (se->buffer)
413                                     );
414
415   line = gtk_text_iter_get_line (&here) ;
416
417   /* Now set begin and end to the start of this line, and start of
418      following line respectively */
419   gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
420   gtk_text_buffer_get_iter_at_line (se->buffer, &end, line + 1);
421
422   editor_execute_syntax (se, begin, end);
423 }
424
425
426
427 /* Append ".sps" to FILENAME if necessary.
428    The returned result must be freed when no longer required.
429  */
430 static gchar *
431 append_suffix (const gchar *filename)
432 {
433   if ( ! g_str_has_suffix (filename, ".sps" ) &&
434        ! g_str_has_suffix (filename, ".SPS" ) )
435     {
436       return g_strdup_printf ("%s.sps", filename);
437     }
438
439   return xstrdup (filename);
440 }
441
442 /*
443   Save BUFFER to the file called FILENAME.
444   FILENAME must be encoded in Glib filename encoding.
445   If successful, clears the buffer's modified flag.
446 */
447 static gboolean
448 save_editor_to_file (PsppireSyntaxWindow *se,
449                      const gchar *filename,
450                      GError **err)
451 {
452   GtkTextBuffer *buffer = se->buffer;
453   gboolean result ;
454   GtkTextIter start, stop;
455   gchar *text;
456
457   gchar *suffixedname;
458   g_assert (filename);
459
460   suffixedname = append_suffix (filename);
461
462   gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
463   gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
464
465   text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
466
467   result =  g_file_set_contents (suffixedname, text, -1, err);
468
469   g_free (suffixedname);
470
471   if ( result )
472     {
473       char *fn = g_filename_display_name (filename);
474       gchar *msg = g_strdup_printf (_("Saved file `%s'"), fn);
475       g_free (fn);
476       gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
477       gtk_text_buffer_set_modified (buffer, FALSE);
478       g_free (msg);
479     }
480
481   return result;
482 }
483
484
485 /* Callback for the File->SaveAs menuitem */
486 static void
487 syntax_save_as (PsppireWindow *se)
488 {
489   GtkFileFilter *filter;
490   gint response;
491
492   GtkWidget *dialog =
493     gtk_file_chooser_dialog_new (_("Save Syntax"),
494                                  GTK_WINDOW (se),
495                                  GTK_FILE_CHOOSER_ACTION_SAVE,
496                                  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
497                                  GTK_STOCK_SAVE,   GTK_RESPONSE_ACCEPT,
498                                  NULL);
499
500   filter = gtk_file_filter_new ();
501   gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
502   gtk_file_filter_add_pattern (filter, "*.sps");
503   gtk_file_filter_add_pattern (filter, "*.SPS");
504   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
505
506   filter = gtk_file_filter_new ();
507   gtk_file_filter_set_name (filter, _("All Files"));
508   gtk_file_filter_add_pattern (filter, "*");
509   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
510
511   gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
512                                                   TRUE);
513   response = gtk_dialog_run (GTK_DIALOG (dialog));
514
515   if ( response == GTK_RESPONSE_ACCEPT )
516     {
517       GError *err = NULL;
518       char *filename =
519         gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
520
521       if ( ! save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err) )
522         {
523           msg ( ME, "%s", err->message );
524           g_error_free (err);
525         }
526
527       free (filename);
528     }
529
530   gtk_widget_destroy (dialog);
531 }
532
533
534 /* Callback for the File->Save menuitem */
535 static void
536 syntax_save (PsppireWindow *se)
537 {
538   const gchar *filename = psppire_window_get_filename (se);
539
540   if ( filename == NULL )
541     syntax_save_as (se);
542   else
543     {
544       GError *err = NULL;
545       save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
546       if ( err )
547         {
548           msg (ME, "%s", err->message);
549           g_error_free (err);
550         }
551     }
552 }
553
554
555 /* Callback for the File->Quit menuitem */
556 static gboolean
557 on_quit (GtkMenuItem *menuitem, gpointer    user_data)
558 {
559   psppire_quit ();
560
561   return FALSE;
562 }
563
564
565 void
566 create_syntax_window (void)
567 {
568   GtkWidget *w = psppire_syntax_window_new ();
569   gtk_widget_show (w);
570 }
571
572 void
573 open_syntax_window (const char *file_name)
574 {
575   GtkWidget *se = psppire_syntax_window_new ();
576
577   if ( psppire_window_load (PSPPIRE_WINDOW (se), file_name) )
578     gtk_widget_show (se);
579   else
580     gtk_widget_destroy (se);
581 }
582
583 static void
584 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
585 {
586   gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
587 }
588
589 static void
590 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
591 {
592   if (gtk_text_buffer_get_modified (buffer))
593     psppire_window_set_unsaved (window);
594 }
595
596 extern struct source_stream *the_source_stream ;
597
598 static void
599 psppire_syntax_window_init (PsppireSyntaxWindow *window)
600 {
601   GtkBuilder *xml = builder_new ("syntax-editor.ui");
602   GtkWidget *box = gtk_vbox_new (FALSE, 0);
603
604   GtkWidget *menubar = get_widget_assert (xml, "menubar");
605   GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
606
607
608   GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
609
610   GtkClipboard *clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
611   GtkClipboard *clip_primary =   gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
612
613   window->cliptext = NULL;
614   window->dispose_has_run = FALSE;
615
616   window->edit_delete = get_action_assert (xml, "edit_delete");
617   window->edit_copy = get_action_assert (xml, "edit_copy");
618   window->edit_cut = get_action_assert (xml, "edit_cut");
619   window->edit_paste = get_action_assert (xml, "edit_paste");
620
621   window->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
622   window->lexer = lex_create (the_source_stream);
623
624   window->sb = get_widget_assert (xml, "statusbar2");
625   window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
626
627   g_signal_connect (window->buffer, "changed", 
628                     G_CALLBACK (on_text_changed), window);
629
630   g_signal_connect (window->buffer, "modified-changed", 
631                     G_CALLBACK (on_modified_changed), window);
632
633   window->sel_handler = g_signal_connect_swapped (clip_primary, "owner-change", 
634                                                    G_CALLBACK (selection_changed), window);
635
636   window->ps_handler = g_signal_connect (clip_selection, "owner-change", 
637                                           G_CALLBACK (set_paste_sensitivity), window);
638
639   connect_help (xml);
640
641   gtk_container_add (GTK_CONTAINER (window), box);
642
643   g_object_ref (menubar);
644
645   g_object_ref (sw);
646
647   g_object_ref (window->sb);
648
649   gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
650   gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
651   gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
652
653   gtk_widget_show_all (box);
654
655   g_signal_connect_swapped (get_action_assert (xml,"file_new_syntax"), "activate", G_CALLBACK (create_syntax_window), NULL);
656
657 #if 0
658   g_signal_connect (get_action_assert (xml,"file_new_data"),
659                     "activate",
660                     G_CALLBACK (create_data_window),
661                     window);
662 #endif
663
664   g_signal_connect_swapped (get_action_assert (xml, "file_save"),
665                     "activate",
666                     G_CALLBACK (syntax_save),
667                     window);
668
669   g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
670                     "activate",
671                     G_CALLBACK (syntax_save_as),
672                     window);
673
674   g_signal_connect (get_action_assert (xml,"file_quit"),
675                     "activate",
676                     G_CALLBACK (on_quit),
677                     window);
678
679   g_signal_connect_swapped (window->edit_delete,
680                     "activate",
681                     G_CALLBACK (on_edit_delete),
682                     window);
683
684   g_signal_connect_swapped (window->edit_copy,
685                     "activate",
686                     G_CALLBACK (on_edit_copy),
687                     window);
688
689   g_signal_connect_swapped (window->edit_cut,
690                     "activate",
691                     G_CALLBACK (on_edit_cut),
692                     window);
693
694   g_signal_connect_swapped (window->edit_paste,
695                     "activate",
696                     G_CALLBACK (on_edit_paste),
697                     window);
698
699   g_signal_connect (get_action_assert (xml,"run_all"),
700                     "activate",
701                     G_CALLBACK (on_run_all),
702                     window);
703
704   g_signal_connect (get_action_assert (xml,"run_selection"),
705                     "activate",
706                     G_CALLBACK (on_run_selection),
707                     window);
708
709   g_signal_connect (get_action_assert (xml,"run_current_line"),
710                     "activate",
711                     G_CALLBACK (on_run_current_line),
712                     window);
713
714   g_signal_connect (get_action_assert (xml,"run_to_end"),
715                     "activate",
716                     G_CALLBACK (on_run_to_end),
717                     window);
718
719   g_signal_connect (get_action_assert (xml,"windows_minimise_all"),
720                     "activate",
721                     G_CALLBACK (psppire_window_minimise_all), NULL);
722
723
724   {
725   GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
726
727   merge_help_menu (uim);
728
729   PSPPIRE_WINDOW (window)->menu =
730     GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
731   }
732
733   g_object_unref (xml);
734 }
735
736
737
738
739
740 GtkWidget*
741 psppire_syntax_window_new (void)
742 {
743   return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
744                                    /* TRANSLATORS: This will form a filename.  Please avoid whitespace. */
745                                    "filename", _("Syntax"),
746                                    "description", _("Syntax Editor"),
747                                    NULL));
748 }
749
750 static void
751 error_dialog (GtkWindow *w, const gchar *filename,  GError *err)
752 {
753   gchar *fn = g_filename_display_basename (filename);
754
755   GtkWidget *dialog =
756     gtk_message_dialog_new (w,
757                             GTK_DIALOG_DESTROY_WITH_PARENT,
758                             GTK_MESSAGE_ERROR,
759                             GTK_BUTTONS_CLOSE,
760                             _("Cannot load syntax file `%s'"),
761                             fn);
762
763   g_free (fn);
764
765   g_object_set (dialog, "icon-name", "psppicon", NULL);
766
767   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
768                                             "%s", err->message);
769
770   gtk_dialog_run (GTK_DIALOG (dialog));
771
772   gtk_widget_destroy (dialog);
773 }
774
775 /*
776   Loads the buffer from the file called FILENAME
777 */
778 gboolean
779 syntax_load (PsppireWindow *window, const gchar *filename)
780 {
781   GError *err = NULL;
782   gchar *text_locale = NULL;
783   gchar *text_utf8 = NULL;
784   gsize len_locale = -1;
785   gsize len_utf8 = -1;
786   GtkTextIter iter;
787   PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
788
789   /* FIXME: What if it's a very big file ? */
790   if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
791     {
792       error_dialog (GTK_WINDOW (window), filename, err);
793       g_clear_error (&err);
794       return FALSE;
795     }
796
797   text_utf8 = g_locale_to_utf8 (text_locale, len_locale, NULL, &len_utf8, &err);
798
799   free (text_locale);
800
801   if ( text_utf8 == NULL )
802     {
803       error_dialog (GTK_WINDOW (window), filename, err);
804       g_clear_error (&err);
805       return FALSE;
806     }
807
808   gtk_text_buffer_get_iter_at_line (sw->buffer, &iter, 0);
809
810   gtk_text_buffer_insert (sw->buffer, &iter, text_utf8, len_utf8);
811
812   gtk_text_buffer_set_modified (sw->buffer, FALSE);
813
814   free (text_utf8);
815
816   return TRUE;
817 }
818
819 \f
820
821 static void
822 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
823 {
824   iface->save = syntax_save;
825   iface->load = syntax_load;
826 }
827