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