gui: Include only <gtk/gtk.h> to use GTK+.
[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/gtk.h>
20 #include "executor.h"
21 #include "helper.h"
22
23 #include <language/lexer/lexer.h>
24 #include <libpspp/message.h>
25 #include <stdlib.h>
26
27 #include "help-menu.h"
28 #include "psppire.h"
29 #include "psppire-data-window.h"
30 #include "psppire-window-register.h"
31 #include "psppire-syntax-window.h"
32
33 #include "xalloc.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
45 static void psppire_syntax_window_iface_init (PsppireWindowIface *iface);
46
47
48 GType
49 psppire_syntax_window_get_type (void)
50 {
51   static GType psppire_syntax_window_type = 0;
52
53   if (!psppire_syntax_window_type)
54     {
55       static const GTypeInfo psppire_syntax_window_info =
56       {
57         sizeof (PsppireSyntaxWindowClass),
58         (GBaseInitFunc) psppire_syntax_window_base_init,
59         (GBaseFinalizeFunc) psppire_syntax_window_base_finalize,
60         (GClassInitFunc)psppire_syntax_window_class_init,
61         (GClassFinalizeFunc) NULL,
62         NULL,
63         sizeof (PsppireSyntaxWindow),
64         0,
65         (GInstanceInitFunc) psppire_syntax_window_init,
66       };
67
68       static const GInterfaceInfo window_interface_info =
69         {
70           (GInterfaceInitFunc) psppire_syntax_window_iface_init,
71           NULL,
72           NULL
73         };
74
75       psppire_syntax_window_type =
76         g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireSyntaxWindow",
77                                 &psppire_syntax_window_info, 0);
78
79       g_type_add_interface_static (psppire_syntax_window_type,
80                                    PSPPIRE_TYPE_WINDOW_MODEL,
81                                    &window_interface_info);
82     }
83
84   return psppire_syntax_window_type;
85 }
86
87 static GObjectClass *parent_class ;
88
89 static void
90 psppire_syntax_window_finalize (GObject *object)
91 {
92   if (G_OBJECT_CLASS (parent_class)->finalize)
93     (*G_OBJECT_CLASS (parent_class)->finalize) (object);
94 }
95
96
97 static void
98 psppire_syntax_window_dispose (GObject *obj)
99 {
100   PsppireSyntaxWindow *sw = (PsppireSyntaxWindow *)obj;
101
102   GtkClipboard *clip_selection;
103   GtkClipboard *clip_primary;
104
105   if (sw->dispose_has_run)
106     return;
107
108   clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
109   clip_primary =   gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_PRIMARY);
110
111   g_signal_handler_disconnect (clip_primary, sw->sel_handler);
112
113   g_signal_handler_disconnect (clip_selection, sw->ps_handler);
114
115   /* Make sure dispose does not run twice. */
116   sw->dispose_has_run = TRUE;
117
118   /* Chain up to the parent class */
119   G_OBJECT_CLASS (parent_class)->dispose (obj);
120 }
121
122
123
124 static void
125 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
126 {
127   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
128
129   parent_class = g_type_class_peek_parent (class);
130
131   gobject_class->dispose = psppire_syntax_window_dispose;
132 }
133
134
135 static void
136 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
137 {
138   GObjectClass *object_class = G_OBJECT_CLASS (class);
139
140   object_class->finalize = psppire_syntax_window_finalize;
141 }
142
143
144
145 static void
146 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
147                                      gpointer class_data)
148 {
149 }
150
151
152 static void
153 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
154                        GtkTextIter stop)
155 {
156   PsppireWindow *win = PSPPIRE_WINDOW (sw);
157   struct lex_reader *reader;
158   gchar *text;
159
160   text = gtk_text_buffer_get_text (sw->buffer, &start, &stop, FALSE);
161   reader = lex_reader_for_string (text);
162   g_free (text);
163
164   lex_reader_set_file_name (reader, psppire_window_get_filename (win));
165
166   execute_syntax (reader);
167 }
168
169
170 \f
171
172 /* Delete the currently selected text */
173 static void
174 on_edit_delete (PsppireSyntaxWindow *sw)
175 {
176   GtkTextIter begin, end;
177   
178   if ( gtk_text_buffer_get_selection_bounds (sw->buffer, &begin, &end) )
179     gtk_text_buffer_delete (sw->buffer, &begin, &end);
180 }
181
182
183
184
185 /* The syntax editor's clipboard deals only with text */
186 enum {
187   SELECT_FMT_NULL,
188   SELECT_FMT_TEXT,
189 };
190
191
192 static void
193 selection_changed (PsppireSyntaxWindow *sw)
194 {
195   gboolean sel = gtk_text_buffer_get_has_selection (sw->buffer);
196
197   gtk_action_set_sensitive (sw->edit_copy, sel);
198   gtk_action_set_sensitive (sw->edit_cut, sel);
199   gtk_action_set_sensitive (sw->edit_delete, sel);
200 }
201
202 /* The callback which runs when something request clipboard data */
203 static void
204 clipboard_get_cb (GtkClipboard     *clipboard,
205                   GtkSelectionData *selection_data,
206                   guint             info,
207                   gpointer          data)
208 {
209   PsppireSyntaxWindow *sw = data;
210   g_assert (info == SELECT_FMT_TEXT);
211
212   gtk_selection_data_set (selection_data, selection_data->target,
213                           8,
214                           (const guchar *) sw->cliptext, strlen (sw->cliptext));
215
216 }
217
218 static void
219 clipboard_clear_cb (GtkClipboard *clipboard,
220                     gpointer data)
221 {
222   PsppireSyntaxWindow *sw = data;
223   g_free (sw->cliptext);
224   sw->cliptext = NULL;
225 }
226
227
228 static const GtkTargetEntry targets[] = {
229   { "UTF8_STRING",   0, SELECT_FMT_TEXT },
230   { "STRING",        0, SELECT_FMT_TEXT },
231   { "TEXT",          0, SELECT_FMT_TEXT },
232   { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
233   { "text/plain;charset=utf-8", 0, SELECT_FMT_TEXT },
234   { "text/plain",    0, SELECT_FMT_TEXT },
235 };
236
237
238 /*
239   Store a clip containing the currently selected text.
240   Returns true iff something was set.
241   As a side effect, begin and end will be set to indicate
242   the limits of the selected text.
243 */
244 static gboolean
245 set_clip (PsppireSyntaxWindow *sw, GtkTextIter *begin, GtkTextIter *end)
246 {
247   GtkClipboard *clipboard ;
248
249   if ( ! gtk_text_buffer_get_selection_bounds (sw->buffer, begin, end) )
250     return FALSE;
251
252   g_free (sw->cliptext);
253   sw->cliptext = gtk_text_buffer_get_text  (sw->buffer, begin, end, FALSE);
254
255   clipboard =
256     gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
257
258   if (!gtk_clipboard_set_with_owner (clipboard, targets,
259                                      G_N_ELEMENTS (targets),
260                                      clipboard_get_cb, clipboard_clear_cb,
261                                      G_OBJECT (sw)))
262     clipboard_clear_cb (clipboard, sw);
263
264   return TRUE;
265 }
266
267 static void
268 on_edit_cut (PsppireSyntaxWindow *sw)
269 {
270   GtkTextIter begin, end;
271   
272   if ( set_clip (sw, &begin, &end))
273     gtk_text_buffer_delete (sw->buffer, &begin, &end);
274 }
275
276 static void
277 on_edit_copy (PsppireSyntaxWindow *sw)
278 {
279   GtkTextIter begin, end;
280
281   set_clip (sw, &begin, &end);
282 }
283
284
285 /* A callback for when the clipboard contents have been received */
286 static void
287 contents_received_callback (GtkClipboard *clipboard,
288                             GtkSelectionData *sd,
289                             gpointer data)
290 {
291   gchar *c;
292   PsppireSyntaxWindow *syntax_window = data;
293
294   if ( sd->length < 0 )
295     return;
296
297   if ( sd->type != gdk_atom_intern ("UTF8_STRING", FALSE))
298     return;
299
300   c = (gchar *) sd->data;
301
302   gtk_text_buffer_insert_at_cursor (syntax_window->buffer,
303                                     (gchar *) sd->data,
304                                     sd->length);
305
306 }
307
308 static void
309 on_edit_paste (PsppireSyntaxWindow *sw)
310 {
311   GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (sw));
312   GtkClipboard *clipboard =
313     gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
314
315   gtk_clipboard_request_contents (clipboard,
316                                   gdk_atom_intern ("UTF8_STRING", TRUE),
317                                   contents_received_callback,
318                                   sw);
319 }
320
321
322 /* Check to see if CLIP holds a target which we know how to paste,
323    and set the sensitivity of the Paste action accordingly.
324  */
325 static void
326 set_paste_sensitivity (GtkClipboard *clip, GdkEventOwnerChange *event, gpointer data)
327 {
328   gint i;
329   gboolean compatible_target = FALSE;
330   PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (data);
331
332   for (i = 0 ; i < sizeof (targets) / sizeof (targets[0]) ; ++i)
333     {
334       GdkAtom atom = gdk_atom_intern (targets[i].target, TRUE);
335       if ( gtk_clipboard_wait_is_target_available (clip, atom))
336         {
337           compatible_target = TRUE;
338           break;
339         }
340     }
341
342   gtk_action_set_sensitive (sw->edit_paste, compatible_target);
343 }
344
345
346 \f
347
348 /* Parse and execute all the text in the buffer */
349 static void
350 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
351 {
352   GtkTextIter begin, end;
353   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
354
355   gtk_text_buffer_get_iter_at_offset (se->buffer, &begin, 0);
356   gtk_text_buffer_get_iter_at_offset (se->buffer, &end, -1);
357
358   editor_execute_syntax (se, begin, end);
359 }
360
361 /* Parse and execute the currently selected text */
362 static void
363 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
364 {
365   GtkTextIter begin, end;
366   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
367
368   if ( gtk_text_buffer_get_selection_bounds (se->buffer, &begin, &end) )
369     editor_execute_syntax (se, begin, end);
370 }
371
372
373 /* Parse and execute the from the current line, to the end of the
374    buffer */
375 static void
376 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
377 {
378   GtkTextIter begin, end;
379   GtkTextIter here;
380   gint line;
381
382   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
383
384   /* Get the current line */
385   gtk_text_buffer_get_iter_at_mark (se->buffer,
386                                     &here,
387                                     gtk_text_buffer_get_insert (se->buffer)
388                                     );
389
390   line = gtk_text_iter_get_line (&here) ;
391
392   /* Now set begin and end to the start of this line, and end of buffer
393      respectively */
394   gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
395   gtk_text_buffer_get_iter_at_line (se->buffer, &end, -1);
396
397   editor_execute_syntax (se, begin, end);
398 }
399
400
401
402 /* Parse and execute the current line */
403 static void
404 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
405 {
406   GtkTextIter begin, end;
407   GtkTextIter here;
408   gint line;
409
410   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
411
412   /* Get the current line */
413   gtk_text_buffer_get_iter_at_mark (se->buffer,
414                                     &here,
415                                     gtk_text_buffer_get_insert (se->buffer)
416                                     );
417
418   line = gtk_text_iter_get_line (&here) ;
419
420   /* Now set begin and end to the start of this line, and start of
421      following line respectively */
422   gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
423   gtk_text_buffer_get_iter_at_line (se->buffer, &end, line + 1);
424
425   editor_execute_syntax (se, begin, end);
426 }
427
428
429
430 /* Append ".sps" to FILENAME if necessary.
431    The returned result must be freed when no longer required.
432  */
433 static gchar *
434 append_suffix (const gchar *filename)
435 {
436   if ( ! g_str_has_suffix (filename, ".sps" ) &&
437        ! g_str_has_suffix (filename, ".SPS" ) )
438     {
439       return g_strdup_printf ("%s.sps", filename);
440     }
441
442   return xstrdup (filename);
443 }
444
445 /*
446   Save BUFFER to the file called FILENAME.
447   FILENAME must be encoded in Glib filename encoding.
448   If successful, clears the buffer's modified flag.
449 */
450 static gboolean
451 save_editor_to_file (PsppireSyntaxWindow *se,
452                      const gchar *filename,
453                      GError **err)
454 {
455   GtkTextBuffer *buffer = se->buffer;
456   gboolean result ;
457   GtkTextIter start, stop;
458   gchar *text;
459
460   gchar *suffixedname;
461   g_assert (filename);
462
463   suffixedname = append_suffix (filename);
464
465   gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
466   gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
467
468   text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
469
470   result =  g_file_set_contents (suffixedname, text, -1, err);
471
472   g_free (suffixedname);
473
474   if ( result )
475     {
476       char *fn = g_filename_display_name (filename);
477       gchar *msg = g_strdup_printf (_("Saved file `%s'"), fn);
478       g_free (fn);
479       gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
480       gtk_text_buffer_set_modified (buffer, FALSE);
481       g_free (msg);
482     }
483
484   return result;
485 }
486
487
488 /* Callback for the File->SaveAs menuitem */
489 static void
490 syntax_save_as (PsppireWindow *se)
491 {
492   GtkFileFilter *filter;
493   gint response;
494
495   GtkWidget *dialog =
496     gtk_file_chooser_dialog_new (_("Save Syntax"),
497                                  GTK_WINDOW (se),
498                                  GTK_FILE_CHOOSER_ACTION_SAVE,
499                                  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
500                                  GTK_STOCK_SAVE,   GTK_RESPONSE_ACCEPT,
501                                  NULL);
502
503   filter = gtk_file_filter_new ();
504   gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
505   gtk_file_filter_add_pattern (filter, "*.sps");
506   gtk_file_filter_add_pattern (filter, "*.SPS");
507   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
508
509   filter = gtk_file_filter_new ();
510   gtk_file_filter_set_name (filter, _("All Files"));
511   gtk_file_filter_add_pattern (filter, "*");
512   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
513
514   gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
515                                                   TRUE);
516   response = gtk_dialog_run (GTK_DIALOG (dialog));
517
518   if ( response == GTK_RESPONSE_ACCEPT )
519     {
520       GError *err = NULL;
521       char *filename =
522         gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
523
524       if ( ! save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err) )
525         {
526           msg ( ME, "%s", err->message );
527           g_error_free (err);
528         }
529
530       free (filename);
531     }
532
533   gtk_widget_destroy (dialog);
534 }
535
536
537 /* Callback for the File->Save menuitem */
538 static void
539 syntax_save (PsppireWindow *se)
540 {
541   const gchar *filename = psppire_window_get_filename (se);
542
543   if ( filename == NULL )
544     syntax_save_as (se);
545   else
546     {
547       GError *err = NULL;
548       save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
549       if ( err )
550         {
551           msg (ME, "%s", err->message);
552           g_error_free (err);
553         }
554     }
555 }
556
557
558 /* Callback for the File->Quit menuitem */
559 static gboolean
560 on_quit (GtkMenuItem *menuitem, gpointer    user_data)
561 {
562   psppire_quit ();
563
564   return FALSE;
565 }
566
567
568 void
569 create_syntax_window (void)
570 {
571   GtkWidget *w = psppire_syntax_window_new ();
572   gtk_widget_show (w);
573 }
574
575 void
576 open_syntax_window (const char *file_name)
577 {
578   GtkWidget *se = psppire_syntax_window_new ();
579
580   if ( psppire_window_load (PSPPIRE_WINDOW (se), file_name) )
581     gtk_widget_show (se);
582   else
583     gtk_widget_destroy (se);
584 }
585
586 static void
587 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
588 {
589   gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
590 }
591
592 static void
593 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
594 {
595   if (gtk_text_buffer_get_modified (buffer))
596     psppire_window_set_unsaved (window);
597 }
598
599 static void
600 psppire_syntax_window_init (PsppireSyntaxWindow *window)
601 {
602   GtkBuilder *xml = builder_new ("syntax-editor.ui");
603   GtkWidget *box = gtk_vbox_new (FALSE, 0);
604
605   GtkWidget *menubar = get_widget_assert (xml, "menubar");
606   GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
607
608
609   GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
610
611   GtkClipboard *clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
612   GtkClipboard *clip_primary =   gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
613
614   window->cliptext = NULL;
615   window->dispose_has_run = FALSE;
616
617   window->edit_delete = get_action_assert (xml, "edit_delete");
618   window->edit_copy = get_action_assert (xml, "edit_copy");
619   window->edit_cut = get_action_assert (xml, "edit_cut");
620   window->edit_paste = get_action_assert (xml, "edit_paste");
621
622   window->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
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