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