Merge commit 'origin/master' into sso
[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 "relocatable.h"
20
21 #include <gtk/gtksignal.h>
22 #include <gtk/gtkbox.h>
23 #include "executor.h"
24 #include "helper.h"
25
26 #include <gtksourceview/gtksourcebuffer.h>
27 #include <gtksourceview/gtksourcelanguage.h>
28 #include <gtksourceview/gtksourcelanguagemanager.h>
29 #include <gtksourceview/gtksourceprintcompositor.h>
30
31 #include <libpspp/message.h>
32 #include <stdlib.h>
33
34 #include "psppire.h"
35
36 #include "psppire-data-window.h"
37 #include "psppire-window-register.h"
38 #include "psppire.h"
39 #include "help-menu.h"
40 #include "psppire-syntax-window.h"
41 #include "syntax-editor-source.h"
42 #include <language/lexer/lexer.h>
43
44 #include "xalloc.h"
45
46 #include <gettext.h>
47 #define _(msgid) gettext (msgid)
48 #define N_(msgid) msgid
49
50 static void psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *, gpointer);
51 static void psppire_syntax_window_base_init     (PsppireSyntaxWindowClass *class);
52 static void psppire_syntax_window_class_init    (PsppireSyntaxWindowClass *class);
53 static void psppire_syntax_window_init          (PsppireSyntaxWindow      *syntax_editor);
54
55
56 static void psppire_syntax_window_iface_init (PsppireWindowIface *iface);
57
58
59 GType
60 psppire_syntax_window_get_type (void)
61 {
62   static GType psppire_syntax_window_type = 0;
63
64   if (!psppire_syntax_window_type)
65     {
66       static const GTypeInfo psppire_syntax_window_info =
67       {
68         sizeof (PsppireSyntaxWindowClass),
69         (GBaseInitFunc) psppire_syntax_window_base_init,
70         (GBaseFinalizeFunc) psppire_syntax_window_base_finalize,
71         (GClassInitFunc)psppire_syntax_window_class_init,
72         (GClassFinalizeFunc) NULL,
73         NULL,
74         sizeof (PsppireSyntaxWindow),
75         0,
76         (GInstanceInitFunc) psppire_syntax_window_init,
77       };
78
79       static const GInterfaceInfo window_interface_info =
80         {
81           (GInterfaceInitFunc) psppire_syntax_window_iface_init,
82           NULL,
83           NULL
84         };
85
86       psppire_syntax_window_type =
87         g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireSyntaxWindow",
88                                 &psppire_syntax_window_info, 0);
89
90       g_type_add_interface_static (psppire_syntax_window_type,
91                                    PSPPIRE_TYPE_WINDOW_MODEL,
92                                    &window_interface_info);
93     }
94
95   return psppire_syntax_window_type;
96 }
97
98 static GObjectClass *parent_class ;
99
100 static void
101 psppire_syntax_window_finalize (GObject *object)
102 {
103   if (G_OBJECT_CLASS (parent_class)->finalize)
104     (*G_OBJECT_CLASS (parent_class)->finalize) (object);
105 }
106
107
108 static void
109 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
110 {
111   GtkSourceLanguageManager *lm = gtk_source_language_manager_get_default ();
112
113   const gchar * const *existing_paths =  gtk_source_language_manager_get_search_path (lm);
114   gchar **new_paths = g_strdupv ((gchar **)existing_paths);
115   int n = g_strv_length ((gchar **) existing_paths);
116
117   new_paths = g_realloc (new_paths, (n + 2) * sizeof (*new_paths));
118   new_paths[n] = g_strdup (relocate (PKGDATADIR));
119   new_paths[n+1] = NULL;
120
121   lm = gtk_source_language_manager_new ();
122   gtk_source_language_manager_set_search_path (lm, new_paths);
123
124   class->lan = gtk_source_language_manager_get_language (lm, "pspp");
125
126   if (class->lan == NULL)
127     g_warning ("pspp.lang file not found.  Syntax highlighting will not be available.");
128
129   parent_class = g_type_class_peek_parent (class);
130
131   g_strfreev (new_paths);
132 }
133
134
135 static void
136 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
137 {
138   GObjectClass *object_class = G_OBJECT_CLASS (class);
139   object_class->finalize = psppire_syntax_window_finalize;
140 }
141
142
143
144 static void
145 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
146                                      gpointer class_data)
147 {
148 }
149
150
151 static void
152 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
153                        GtkTextIter stop)
154 {
155   PsppireWindow *win = PSPPIRE_WINDOW (sw);
156   const gchar *name = psppire_window_get_filename (win);
157   execute_syntax (create_syntax_editor_source (GTK_TEXT_BUFFER (sw->buffer), start, stop, name));
158 }
159
160
161 \f
162
163 /* Delete the currently selected text */
164 static void
165 on_edit_delete (PsppireSyntaxWindow *sw)
166 {
167   GtkTextIter begin, end;
168   GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
169   
170   if ( gtk_text_buffer_get_selection_bounds (buffer, &begin, &end) )
171     gtk_text_buffer_delete (buffer, &begin, &end);
172 }
173
174
175 /* The syntax editor's clipboard deals only with text */
176 enum {
177   SELECT_FMT_NULL,
178   SELECT_FMT_TEXT,
179 };
180
181
182 static void
183 selection_changed (PsppireSyntaxWindow *sw)
184 {
185   gboolean sel = gtk_text_buffer_get_has_selection (GTK_TEXT_BUFFER (sw->buffer));
186
187   gtk_action_set_sensitive (sw->edit_copy, sel);
188   gtk_action_set_sensitive (sw->edit_cut, sel);
189   gtk_action_set_sensitive (sw->edit_delete, sel);
190 }
191
192 /* The callback which runs when something request clipboard data */
193 static void
194 clipboard_get_cb (GtkClipboard     *clipboard,
195                   GtkSelectionData *selection_data,
196                   guint             info,
197                   gpointer          data)
198 {
199   PsppireSyntaxWindow *sw = data;
200   g_assert (info == SELECT_FMT_TEXT);
201
202   gtk_selection_data_set (selection_data, selection_data->target,
203                           8,
204                           (const guchar *) sw->cliptext, strlen (sw->cliptext));
205
206 }
207
208 static void
209 clipboard_clear_cb (GtkClipboard *clipboard,
210                     gpointer data)
211 {
212   PsppireSyntaxWindow *sw = data;
213   g_free (sw->cliptext);
214   sw->cliptext = NULL;
215 }
216
217
218 static const GtkTargetEntry targets[] = {
219   { "UTF8_STRING",   0, SELECT_FMT_TEXT },
220   { "STRING",        0, SELECT_FMT_TEXT },
221   { "TEXT",          0, SELECT_FMT_TEXT },
222   { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
223   { "text/plain;charset=utf-8", 0, SELECT_FMT_TEXT },
224   { "text/plain",    0, SELECT_FMT_TEXT },
225 };
226
227
228 /*
229   Store a clip containing the currently selected text.
230   Returns true iff something was set.
231   As a side effect, begin and end will be set to indicate
232   the limits of the selected text.
233 */
234 static gboolean
235 set_clip (PsppireSyntaxWindow *sw, GtkTextIter *begin, GtkTextIter *end)
236 {
237   GtkClipboard *clipboard ;
238   GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
239
240   if ( ! gtk_text_buffer_get_selection_bounds (buffer, begin, end) )
241     return FALSE;
242
243   g_free (sw->cliptext);
244   sw->cliptext = gtk_text_buffer_get_text  (buffer, begin, end, FALSE);
245
246   clipboard =
247     gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
248
249   if (!gtk_clipboard_set_with_owner (clipboard, targets,
250                                      G_N_ELEMENTS (targets),
251                                      clipboard_get_cb, clipboard_clear_cb,
252                                      G_OBJECT (sw)))
253     clipboard_clear_cb (clipboard, sw);
254
255   return TRUE;
256 }
257
258 static void
259 on_edit_cut (PsppireSyntaxWindow *sw)
260 {
261   GtkTextIter begin, end;
262   
263   if ( set_clip (sw, &begin, &end))
264     gtk_text_buffer_delete (GTK_TEXT_BUFFER (sw->buffer), &begin, &end);
265 }
266
267 static void
268 on_edit_copy (PsppireSyntaxWindow *sw)
269 {
270   GtkTextIter begin, end;
271
272   set_clip (sw, &begin, &end);
273 }
274
275
276 /* A callback for when the clipboard contents have been received */
277 static void
278 contents_received_callback (GtkClipboard *clipboard,
279                             GtkSelectionData *sd,
280                             gpointer data)
281 {
282   gchar *c;
283   PsppireSyntaxWindow *syntax_window = data;
284
285   if ( sd->length < 0 )
286     return;
287
288   if ( sd->type != gdk_atom_intern ("UTF8_STRING", FALSE))
289     return;
290
291   c = (gchar *) sd->data;
292
293   gtk_text_buffer_insert_at_cursor (GTK_TEXT_BUFFER (syntax_window->buffer),
294                                     (gchar *) sd->data,
295                                     sd->length);
296
297 }
298
299 static void
300 on_edit_paste (PsppireSyntaxWindow *sw)
301 {
302   GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (sw));
303   GtkClipboard *clipboard =
304     gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
305
306   gtk_clipboard_request_contents (clipboard,
307                                   gdk_atom_intern ("UTF8_STRING", TRUE),
308                                   contents_received_callback,
309                                   sw);
310 }
311
312 static void
313 on_owner_change (GtkClipboard *clip, GdkEventOwnerChange *event, gpointer data)
314 {
315   gint i;
316   gboolean compatible_target = FALSE;
317   PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (data);
318
319   for (i = 0 ; i < sizeof (targets) / sizeof (targets[0]) ; ++i)
320     {
321       GdkAtom atom = gdk_atom_intern (targets[i].target, TRUE);
322       if ( gtk_clipboard_wait_is_target_available (clip, atom))
323         {
324           compatible_target = TRUE;
325           break;
326         }
327     }
328
329   gtk_action_set_sensitive (sw->edit_paste, compatible_target);
330 }
331
332
333 \f
334
335 /* Parse and execute all the text in the buffer */
336 static void
337 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
338 {
339   GtkTextIter begin, end;
340   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
341
342   gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &begin, 0);
343   gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &end, -1);
344
345   editor_execute_syntax (se, begin, end);
346 }
347
348 /* Parse and execute the currently selected text */
349 static void
350 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
351 {
352   GtkTextIter begin, end;
353   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
354
355   if ( gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (se->buffer), &begin, &end) )
356     editor_execute_syntax (se, begin, end);
357 }
358
359
360 /* Parse and execute the from the current line, to the end of the
361    buffer */
362 static void
363 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
364 {
365   GtkTextIter begin, end;
366   GtkTextIter here;
367   gint line;
368
369   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
370
371   /* Get the current line */
372   gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
373                                     &here,
374                                     gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
375                                     );
376
377   line = gtk_text_iter_get_line (&here) ;
378
379   /* Now set begin and end to the start of this line, and end of buffer
380      respectively */
381   gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
382   gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, -1);
383
384   editor_execute_syntax (se, begin, end);
385 }
386
387
388
389 /* Parse and execute the current line */
390 static void
391 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
392 {
393   GtkTextIter begin, end;
394   GtkTextIter here;
395   gint line;
396
397   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
398
399   /* Get the current line */
400   gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
401                                     &here,
402                                     gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
403                                     );
404
405   line = gtk_text_iter_get_line (&here) ;
406
407   /* Now set begin and end to the start of this line, and start of
408      following line respectively */
409   gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
410   gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, line + 1);
411
412   editor_execute_syntax (se, begin, end);
413 }
414
415
416
417 /* Append ".sps" to FILENAME if necessary.
418    The returned result must be freed when no longer required.
419  */
420 static gchar *
421 append_suffix (const gchar *filename)
422 {
423   if ( ! g_str_has_suffix (filename, ".sps" ) &&
424        ! g_str_has_suffix (filename, ".SPS" ) )
425     {
426       return g_strdup_printf ("%s.sps", filename);
427     }
428
429   return xstrdup (filename);
430 }
431
432 /*
433   Save BUFFER to the file called FILENAME.
434   FILENAME must be encoded in Glib filename encoding.
435   If successful, clears the buffer's modified flag.
436 */
437 static gboolean
438 save_editor_to_file (PsppireSyntaxWindow *se,
439                      const gchar *filename,
440                      GError **err)
441 {
442   GtkTextBuffer *buffer = GTK_TEXT_BUFFER (se->buffer);
443   gboolean result ;
444   GtkTextIter start, stop;
445   gchar *text;
446
447   gchar *suffixedname;
448   g_assert (filename);
449
450   suffixedname = append_suffix (filename);
451
452   gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
453   gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
454
455   text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
456
457   result =  g_file_set_contents (suffixedname, text, -1, err);
458
459   g_free (suffixedname);
460
461   if ( result )
462     {
463       char *fn = g_filename_display_name (filename);
464       gchar *msg = g_strdup_printf (_("Saved file \"%s\""), fn);
465       g_free (fn);
466       gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
467       gtk_text_buffer_set_modified (buffer, FALSE);
468       g_free (msg);
469     }
470
471   return result;
472 }
473
474
475 /* Callback for the File->SaveAs menuitem */
476 static void
477 syntax_save_as (PsppireWindow *se)
478 {
479   GtkFileFilter *filter;
480   gint response;
481
482   GtkWidget *dialog =
483     gtk_file_chooser_dialog_new (_("Save Syntax"),
484                                  GTK_WINDOW (se),
485                                  GTK_FILE_CHOOSER_ACTION_SAVE,
486                                  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
487                                  GTK_STOCK_SAVE,   GTK_RESPONSE_ACCEPT,
488                                  NULL);
489
490   filter = gtk_file_filter_new ();
491   gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
492   gtk_file_filter_add_pattern (filter, "*.sps");
493   gtk_file_filter_add_pattern (filter, "*.SPS");
494   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
495
496   filter = gtk_file_filter_new ();
497   gtk_file_filter_set_name (filter, _("All Files"));
498   gtk_file_filter_add_pattern (filter, "*");
499   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
500
501   gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
502                                                   TRUE);
503   response = gtk_dialog_run (GTK_DIALOG (dialog));
504
505   if ( response == GTK_RESPONSE_ACCEPT )
506     {
507       GError *err = NULL;
508       char *filename =
509         gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
510
511       if ( ! save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err) )
512         {
513           msg ( ME, "%s", err->message );
514           g_error_free (err);
515         }
516
517       free (filename);
518     }
519
520   gtk_widget_destroy (dialog);
521 }
522
523
524 /* Callback for the File->Save menuitem */
525 static void
526 syntax_save (PsppireWindow *se)
527 {
528   const gchar *filename = psppire_window_get_filename (se);
529
530   if ( filename == NULL )
531     syntax_save_as (se);
532   else
533     {
534       GError *err = NULL;
535       save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
536       if ( err )
537         {
538           msg (ME, "%s", err->message);
539           g_error_free (err);
540         }
541     }
542 }
543
544
545 /* Callback for the File->Quit menuitem */
546 static gboolean
547 on_quit (GtkMenuItem *menuitem, gpointer    user_data)
548 {
549   psppire_quit ();
550
551   return FALSE;
552 }
553
554
555 static void
556 load_and_show_syntax_window (GtkWidget *se, const gchar *filename)
557 {
558   gboolean ok;
559
560   gtk_source_buffer_begin_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
561   ok = psppire_window_load (PSPPIRE_WINDOW (se), filename);
562   gtk_source_buffer_end_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
563
564   if (ok )
565     gtk_widget_show (se);
566   else
567     gtk_widget_destroy (se);
568 }
569
570 void
571 create_syntax_window (void)
572 {
573   GtkWidget *w = psppire_syntax_window_new ();
574   gtk_widget_show (w);
575 }
576
577 void
578 open_new_syntax_window (const char *file_name)
579 {
580   GtkWidget *se = psppire_syntax_window_new ();
581
582   if ( file_name)
583     load_and_show_syntax_window (se, file_name);
584 }
585
586
587
588 static void psppire_syntax_window_print (PsppireSyntaxWindow *window);
589
590 static void
591 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
592 {
593   if (gtk_text_buffer_get_modified (buffer))
594     psppire_window_set_unsaved (window);
595 }
596
597 extern struct source_stream *the_source_stream ;
598
599 static void undo_redo_update (PsppireSyntaxWindow *window);
600 static void undo_last_edit (PsppireSyntaxWindow *window);
601 static void redo_last_edit (PsppireSyntaxWindow *window);
602
603 static void
604 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
605 {
606   gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
607   undo_redo_update (window);
608 }
609
610 static void
611 psppire_syntax_window_init (PsppireSyntaxWindow *window)
612 {
613   GtkBuilder *xml = builder_new ("syntax-editor.ui");
614   GtkWidget *box = gtk_vbox_new (FALSE, 0);
615
616   GtkWidget *menubar = get_widget_assert (xml, "menubar");
617   GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
618
619   GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
620
621   PsppireSyntaxWindowClass *class
622     = PSPPIRE_SYNTAX_WINDOW_CLASS (G_OBJECT_GET_CLASS (window));
623
624   GtkClipboard *clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
625   GtkClipboard *clip_primary =   gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
626
627   window->print_settings = NULL;
628   window->undo_menuitem = get_action_assert (xml, "edit_undo");
629   window->redo_menuitem = get_action_assert (xml, "edit_redo");
630
631   if (class->lan)
632     window->buffer = gtk_source_buffer_new_with_language (class->lan);
633   else
634     window->buffer = gtk_source_buffer_new (NULL);
635
636   gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), GTK_TEXT_BUFFER (window->buffer));
637
638   g_object_set (window->buffer,
639                 "highlight-matching-brackets", TRUE,
640                 NULL);
641
642   g_object_set (text_view,
643                 "show-line-numbers", TRUE,
644                 "show-line-marks", TRUE,
645                 "auto-indent", TRUE,
646                 "indent-width", 4,
647                 "highlight-current-line", TRUE,
648                 NULL);
649
650   g_signal_connect_swapped (clip_primary, "owner-change", G_CALLBACK (selection_changed), window);
651
652   g_signal_connect (clip_selection, "owner-change", G_CALLBACK (on_owner_change), window);
653
654   window->cliptext = NULL;
655
656   window->edit_delete = get_action_assert (xml, "edit_delete");
657   window->edit_copy = get_action_assert (xml, "edit_copy");
658   window->edit_cut = get_action_assert (xml, "edit_cut");
659   window->edit_paste = get_action_assert (xml, "edit_paste");
660
661   window->lexer = lex_create (the_source_stream);
662
663   window->sb = get_widget_assert (xml, "statusbar2");
664   window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
665
666   g_signal_connect (window->buffer, "changed", G_CALLBACK (on_text_changed), window);
667
668   g_signal_connect (window->buffer, "modified-changed",
669                     G_CALLBACK (on_modified_changed), window);
670
671   g_signal_connect_swapped (get_action_assert (xml, "file_print"), "activate",
672                             G_CALLBACK (psppire_syntax_window_print), window);
673
674
675   g_signal_connect_swapped (window->undo_menuitem,
676                             "activate",
677                             G_CALLBACK (undo_last_edit),
678                             window);
679
680   g_signal_connect_swapped (window->redo_menuitem,
681                             "activate",
682                             G_CALLBACK (redo_last_edit),
683                             window);
684
685   undo_redo_update (window);
686
687   connect_help (xml);
688
689   gtk_container_add (GTK_CONTAINER (window), box);
690
691   g_object_ref (menubar);
692
693   g_object_ref (sw);
694
695   g_object_ref (window->sb);
696
697
698   gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
699   gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
700   gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
701
702   gtk_widget_show_all (box);
703
704   g_signal_connect_swapped (get_action_assert (xml,"file_new_syntax"), "activate", G_CALLBACK (create_syntax_window), NULL);
705
706 #if 0
707   g_signal_connect (get_action_assert (xml,"file_new_data"),
708                     "activate",
709                     G_CALLBACK (create_data_window),
710                     window);
711 #endif
712
713   g_signal_connect_swapped (get_action_assert (xml, "file_save"),
714                     "activate",
715                     G_CALLBACK (syntax_save),
716                     window);
717
718   g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
719                     "activate",
720                     G_CALLBACK (syntax_save_as),
721                     window);
722
723   g_signal_connect (get_action_assert (xml,"file_quit"),
724                     "activate",
725                     G_CALLBACK (on_quit),
726                     window);
727
728   g_signal_connect_swapped (window->edit_delete,
729                     "activate",
730                     G_CALLBACK (on_edit_delete),
731                     window);
732
733   g_signal_connect_swapped (window->edit_copy,
734                     "activate",
735                     G_CALLBACK (on_edit_copy),
736                     window);
737
738   g_signal_connect_swapped (window->edit_cut,
739                     "activate",
740                     G_CALLBACK (on_edit_cut),
741                     window);
742
743   g_signal_connect_swapped (window->edit_paste,
744                     "activate",
745                     G_CALLBACK (on_edit_paste),
746                     window);
747
748   g_signal_connect (get_action_assert (xml,"run_all"),
749                     "activate",
750                     G_CALLBACK (on_run_all),
751                     window);
752
753   g_signal_connect (get_action_assert (xml,"run_selection"),
754                     "activate",
755                     G_CALLBACK (on_run_selection),
756                     window);
757
758   g_signal_connect (get_action_assert (xml,"run_current_line"),
759                     "activate",
760                     G_CALLBACK (on_run_current_line),
761                     window);
762
763   g_signal_connect (get_action_assert (xml,"run_to_end"),
764                     "activate",
765                     G_CALLBACK (on_run_to_end),
766                     window);
767
768   g_signal_connect (get_action_assert (xml,"windows_minimise_all"),
769                     "activate",
770                     G_CALLBACK (psppire_window_minimise_all), NULL);
771
772
773
774
775
776   {
777   GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
778
779   merge_help_menu (uim);
780
781   PSPPIRE_WINDOW (window)->menu =
782     GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
783   }
784
785   g_object_unref (xml);
786 }
787
788
789
790
791
792 GtkWidget*
793 psppire_syntax_window_new (void)
794 {
795   return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
796                                    "filename", "Syntax",
797                                    "description", _("Syntax Editor"),
798                                    NULL));
799 }
800
801 static void
802 error_dialog (GtkWindow *w, const gchar *filename,  GError *err)
803 {
804   gchar *fn = g_filename_display_basename (filename);
805
806   GtkWidget *dialog =
807     gtk_message_dialog_new (w,
808                             GTK_DIALOG_DESTROY_WITH_PARENT,
809                             GTK_MESSAGE_ERROR,
810                             GTK_BUTTONS_CLOSE,
811                             _("Cannot load syntax file '%s'"),
812                             fn);
813
814   g_free (fn);
815
816   g_object_set (dialog, "icon-name", "psppicon", NULL);
817
818   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
819                                             "%s", err->message);
820
821   gtk_dialog_run (GTK_DIALOG (dialog));
822
823   gtk_widget_destroy (dialog);
824 }
825
826 /*
827   Loads the buffer from the file called FILENAME
828 */
829 gboolean
830 syntax_load (PsppireWindow *window, const gchar *filename)
831 {
832   GError *err = NULL;
833   gchar *text_locale = NULL;
834   gchar *text_utf8 = NULL;
835   gsize len_locale = -1;
836   gsize len_utf8 = -1;
837   GtkTextIter iter;
838   PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
839   GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
840   /* FIXME: What if it's a very big file ? */
841   if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
842     {
843       error_dialog (GTK_WINDOW (window), filename, err);
844       g_clear_error (&err);
845       return FALSE;
846     }
847
848   text_utf8 = g_locale_to_utf8 (text_locale, len_locale, NULL, &len_utf8, &err);
849
850   free (text_locale);
851
852   if ( text_utf8 == NULL )
853     {
854       error_dialog (GTK_WINDOW (window), filename, err);
855       g_clear_error (&err);
856       return FALSE;
857     }
858
859   gtk_text_buffer_get_iter_at_line (buffer, &iter, 0);
860
861   gtk_text_buffer_insert (buffer, &iter, text_utf8, len_utf8);
862
863   gtk_text_buffer_set_modified (buffer, FALSE);
864
865   free (text_utf8);
866
867   return TRUE;
868 }
869
870 \f
871
872 static void
873 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
874 {
875   iface->save = syntax_save;
876   iface->load = syntax_load;
877 }
878
879 \f
880
881 static void
882 undo_redo_update (PsppireSyntaxWindow *window)
883 {
884   gtk_action_set_sensitive (window->undo_menuitem,
885                             gtk_source_buffer_can_undo (window->buffer));
886
887   gtk_action_set_sensitive (window->redo_menuitem,
888                             gtk_source_buffer_can_redo (window->buffer));
889 }
890
891 static void
892 undo_last_edit (PsppireSyntaxWindow *window)
893 {
894   gtk_source_buffer_undo (window->buffer);
895   undo_redo_update (window);
896 }
897
898 static void
899 redo_last_edit (PsppireSyntaxWindow *window)
900 {
901   gtk_source_buffer_redo (window->buffer);
902   undo_redo_update (window);
903 }
904
905
906 \f
907 /* Printing related stuff */
908
909
910 static void
911 begin_print (GtkPrintOperation *operation,
912           GtkPrintContext   *context,
913           PsppireSyntaxWindow *window)
914 {
915   window->compositor =
916     gtk_source_print_compositor_new (window->buffer);
917 }
918
919
920 static void
921 end_print (GtkPrintOperation *operation,
922           GtkPrintContext   *context,
923           PsppireSyntaxWindow *window)
924 {
925   g_object_unref (window->compositor);
926   window->compositor = NULL;
927 }
928
929
930
931 static gboolean
932 paginate (GtkPrintOperation *operation,
933           GtkPrintContext   *context,
934           PsppireSyntaxWindow *window)
935 {
936   if (gtk_source_print_compositor_paginate (window->compositor, context))
937     {
938       gint n_pages = gtk_source_print_compositor_get_n_pages (window->compositor);
939       gtk_print_operation_set_n_pages (operation, n_pages);
940         
941       return TRUE;
942     }
943
944   return FALSE;
945 }
946
947 static void
948 draw_page (GtkPrintOperation *operation,
949            GtkPrintContext   *context,
950            gint               page_nr,
951           PsppireSyntaxWindow *window)
952 {
953   gtk_source_print_compositor_draw_page (window->compositor, 
954                                          context,
955                                          page_nr);
956 }
957
958
959
960 static void
961 psppire_syntax_window_print (PsppireSyntaxWindow *window)
962 {
963   GtkPrintOperationResult res;
964
965   GtkPrintOperation *print = gtk_print_operation_new ();
966
967   if (window->print_settings != NULL) 
968     gtk_print_operation_set_print_settings (print, window->print_settings);
969
970
971   g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
972   g_signal_connect (print, "end_print", G_CALLBACK (end_print),     window);
973   g_signal_connect (print, "draw_page", G_CALLBACK (draw_page),     window);
974   g_signal_connect (print, "paginate", G_CALLBACK (paginate),       window);
975
976   res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
977                                  GTK_WINDOW (window), NULL);
978
979   if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
980     {
981       if (window->print_settings != NULL)
982         g_object_unref (window->print_settings);
983       window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
984     }
985
986   g_object_unref (print);
987 }