Whitespace changes only.
[pspp] / src / ui / gui / psppire-syntax-window.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 2009, 2010, 2011, 2012, 2014, 2016  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 <stdlib.h>
21
22 #include <gtksourceview/gtksource.h>
23
24 #include "language/lexer/lexer.h"
25 #include "libpspp/encoding-guesser.h"
26 #include "libpspp/i18n.h"
27 #include "libpspp/message.h"
28 #include "ui/gui/executor.h"
29 #include "ui/gui/help-menu.h"
30 #include "ui/gui/helper.h"
31 #include "ui/gui/builder-wrapper.h"
32 #include "ui/gui/psppire-data-window.h"
33 #include "ui/gui/psppire-encoding-selector.h"
34 #include "ui/gui/psppire-lex-reader.h"
35 #include "ui/gui/psppire-syntax-window.h"
36 #include "ui/gui/psppire.h"
37 #include "ui/gui/windows-menu.h"
38
39 #include "gl/localcharset.h"
40 #include "gl/xalloc.h"
41 #include "gl/xvasprintf.h"
42
43 #include <gettext.h>
44 #define _(msgid) gettext (msgid)
45 #define N_(msgid) msgid
46
47 static void psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *, gpointer);
48 static void psppire_syntax_window_base_init     (PsppireSyntaxWindowClass *class);
49 static void psppire_syntax_window_class_init    (PsppireSyntaxWindowClass *class);
50 static void psppire_syntax_window_init          (PsppireSyntaxWindow      *syntax_editor);
51
52
53 static void psppire_syntax_window_iface_init (PsppireWindowIface *iface);
54
55
56 /* Properties */
57 enum
58 {
59   PROP_0,
60   PROP_ENCODING
61 };
62
63 static void
64 psppire_syntax_window_set_property (GObject         *object,
65                                     guint            prop_id,
66                                     const GValue    *value,
67                                     GParamSpec      *pspec)
68 {
69   PsppireSyntaxWindow *window = PSPPIRE_SYNTAX_WINDOW (object);
70
71   switch (prop_id)
72     {
73     case PROP_ENCODING:
74       g_free (window->encoding);
75       window->encoding = g_value_dup_string (value);
76       break;
77     default:
78       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
79       break;
80     };
81 }
82
83
84 static void
85 psppire_syntax_window_get_property (GObject         *object,
86                                     guint            prop_id,
87                                     GValue          *value,
88                                     GParamSpec      *pspec)
89 {
90   PsppireSyntaxWindow *window = PSPPIRE_SYNTAX_WINDOW (object);
91
92   switch (prop_id)
93     {
94     case PROP_ENCODING:
95       g_value_set_string (value, window->encoding);
96       break;
97     default:
98       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
99       break;
100     };
101 }
102
103 GType
104 psppire_syntax_window_get_type (void)
105 {
106   static GType psppire_syntax_window_type = 0;
107
108   if (!psppire_syntax_window_type)
109     {
110       static const GTypeInfo psppire_syntax_window_info =
111       {
112         sizeof (PsppireSyntaxWindowClass),
113         (GBaseInitFunc) psppire_syntax_window_base_init,
114         (GBaseFinalizeFunc) psppire_syntax_window_base_finalize,
115         (GClassInitFunc)psppire_syntax_window_class_init,
116         (GClassFinalizeFunc) NULL,
117         NULL,
118         sizeof (PsppireSyntaxWindow),
119         0,
120         (GInstanceInitFunc) psppire_syntax_window_init,
121       };
122
123       static const GInterfaceInfo window_interface_info =
124         {
125           (GInterfaceInitFunc) psppire_syntax_window_iface_init,
126           NULL,
127           NULL
128         };
129
130       psppire_syntax_window_type =
131         g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireSyntaxWindow",
132                                 &psppire_syntax_window_info, 0);
133
134       g_type_add_interface_static (psppire_syntax_window_type,
135                                    PSPPIRE_TYPE_WINDOW_MODEL,
136                                    &window_interface_info);
137     }
138
139   return psppire_syntax_window_type;
140 }
141
142 static GObjectClass *parent_class ;
143
144 static void
145 psppire_syntax_window_finalize (GObject *object)
146 {
147   if (G_OBJECT_CLASS (parent_class)->finalize)
148     (*G_OBJECT_CLASS (parent_class)->finalize) (object);
149 }
150
151
152 static void
153 psppire_syntax_window_dispose (GObject *obj)
154 {
155   PsppireSyntaxWindow *sw = (PsppireSyntaxWindow *)obj;
156
157   GtkClipboard *clip_selection;
158   GtkClipboard *clip_primary;
159
160   if (sw->dispose_has_run)
161     return;
162
163   g_free (sw->encoding);
164   sw->encoding = NULL;
165
166   clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
167   clip_primary =   gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_PRIMARY);
168
169   g_signal_handler_disconnect (clip_primary, sw->sel_handler);
170
171   g_signal_handler_disconnect (clip_selection, sw->ps_handler);
172
173   /* Make sure dispose does not run twice. */
174   sw->dispose_has_run = TRUE;
175
176   /* Chain up to the parent class */
177   G_OBJECT_CLASS (parent_class)->dispose (obj);
178 }
179
180
181
182 static void
183 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
184 {
185   GParamSpec *encoding_spec;
186   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
187
188   GtkSourceLanguageManager *lm = gtk_source_language_manager_get_default ();
189
190   const gchar * const *existing_paths =  gtk_source_language_manager_get_search_path (lm);
191   gchar **new_paths = g_strdupv ((gchar **)existing_paths);
192   int n = g_strv_length ((gchar **) existing_paths);
193
194   new_paths = g_realloc (new_paths, (n + 2) * sizeof (*new_paths));
195   new_paths[n] = g_strdup (relocate (PKGDATADIR));
196   new_paths[n+1] = NULL;
197
198   lm = gtk_source_language_manager_new ();
199   gtk_source_language_manager_set_search_path (lm, new_paths);
200
201   class->lan = gtk_source_language_manager_get_language (lm, "pspp");
202
203   if (class->lan == NULL)
204     g_warning ("pspp.lang file not found.  Syntax highlighting will not be available.");
205
206   parent_class = g_type_class_peek_parent (class);
207
208   g_strfreev (new_paths);
209
210   encoding_spec =
211     null_if_empty_param ("encoding",
212                          "Character encoding",
213                          "IANA character encoding in this syntax file",
214                          NULL,
215                          G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
216
217   parent_class = g_type_class_peek_parent (class);
218
219   gobject_class->set_property = psppire_syntax_window_set_property;
220   gobject_class->get_property = psppire_syntax_window_get_property;
221   gobject_class->dispose = psppire_syntax_window_dispose;
222
223   g_object_class_install_property (gobject_class,
224                                    PROP_ENCODING,
225                                    encoding_spec);
226 }
227
228
229 static void
230 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
231 {
232   GObjectClass *object_class = G_OBJECT_CLASS (class);
233   object_class->finalize = psppire_syntax_window_finalize;
234 }
235
236
237
238 static void
239 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
240                                      gpointer class_data)
241 {
242 }
243
244
245 static void
246 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
247                        GtkTextIter stop)
248 {
249   PsppireWindow *win = PSPPIRE_WINDOW (sw);
250   struct lex_reader *reader = lex_reader_for_gtk_text_buffer (GTK_TEXT_BUFFER (sw->buffer), start, stop);
251
252   lex_reader_set_file_name (reader, psppire_window_get_filename (win));
253
254   execute_syntax (psppire_default_data_window (), reader);
255 }
256 \f
257 /* Delete the currently selected text */
258 static void
259 on_edit_delete (PsppireSyntaxWindow *sw)
260 {
261   GtkTextIter begin, end;
262   GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
263
264   if (gtk_text_buffer_get_selection_bounds (buffer, &begin, &end))
265     gtk_text_buffer_delete (buffer, &begin, &end);
266 }
267
268
269 /* The syntax editor's clipboard deals only with text */
270 enum {
271   SELECT_FMT_NULL,
272   SELECT_FMT_TEXT,
273 };
274
275
276 static void
277 selection_changed (PsppireSyntaxWindow *sw)
278 {
279   gboolean sel = gtk_text_buffer_get_has_selection (GTK_TEXT_BUFFER (sw->buffer));
280
281   g_object_set (sw->edit_copy,    "enabled", sel, NULL);
282   g_object_set (sw->edit_cut,     "enabled", sel, NULL);
283   g_object_set (sw->edit_delete,  "enabled", sel, NULL);
284 }
285
286 /* The callback which runs when something request clipboard data */
287 static void
288 clipboard_get_cb (GtkClipboard     *clipboard,
289                   GtkSelectionData *selection_data,
290                   guint             info,
291                   gpointer          data)
292 {
293   PsppireSyntaxWindow *sw = data;
294   g_assert (info == SELECT_FMT_TEXT);
295
296   gtk_selection_data_set (selection_data, gtk_selection_data_get_target (selection_data),
297                           8,
298                           (const guchar *) sw->cliptext, strlen (sw->cliptext));
299
300 }
301
302 static void
303 clipboard_clear_cb (GtkClipboard *clipboard,
304                     gpointer data)
305 {
306   PsppireSyntaxWindow *sw = data;
307   g_free (sw->cliptext);
308   sw->cliptext = NULL;
309 }
310
311
312 static const GtkTargetEntry targets[] = {
313   { "UTF8_STRING",   0, SELECT_FMT_TEXT },
314   { "STRING",        0, SELECT_FMT_TEXT },
315   { "TEXT",          0, SELECT_FMT_TEXT },
316   { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
317   { "text/plain;charset=utf-8", 0, SELECT_FMT_TEXT },
318   { "text/plain",    0, SELECT_FMT_TEXT },
319 };
320
321
322 /*
323   Store a clip containing the currently selected text.
324   Returns true iff something was set.
325   As a side effect, begin and end will be set to indicate
326   the limits of the selected text.
327 */
328 static gboolean
329 set_clip (PsppireSyntaxWindow *sw, GtkTextIter *begin, GtkTextIter *end)
330 {
331   GtkClipboard *clipboard ;
332   GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
333
334   if (! gtk_text_buffer_get_selection_bounds (buffer, begin, end))
335     return FALSE;
336
337   g_free (sw->cliptext);
338   sw->cliptext = gtk_text_buffer_get_text  (buffer, begin, end, FALSE);
339
340   clipboard =
341     gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
342
343   if (!gtk_clipboard_set_with_owner (clipboard, targets,
344                                      G_N_ELEMENTS (targets),
345                                      clipboard_get_cb, clipboard_clear_cb,
346                                      G_OBJECT (sw)))
347     clipboard_clear_cb (clipboard, sw);
348
349   return TRUE;
350 }
351
352 static void
353 on_edit_cut (PsppireSyntaxWindow *sw)
354 {
355   GtkTextIter begin, end;
356
357   if (set_clip (sw, &begin, &end))
358     gtk_text_buffer_delete (GTK_TEXT_BUFFER (sw->buffer), &begin, &end);
359 }
360
361 static void
362 on_edit_copy (PsppireSyntaxWindow *sw)
363 {
364   GtkTextIter begin, end;
365
366   set_clip (sw, &begin, &end);
367 }
368
369 static void
370 on_edit_paste (PsppireSyntaxWindow *sw)
371 {
372   GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (sw));
373   GtkClipboard *clipboard =
374     gtk_clipboard_get_for_display (display, GDK_SELECTION_CLIPBOARD);
375
376   gtk_text_buffer_paste_clipboard (GTK_TEXT_BUFFER (sw->buffer), clipboard, NULL, TRUE);
377 }
378
379
380 /* Check to see if CLIP holds a target which we know how to paste,
381    and set the sensitivity of the Paste action accordingly.
382  */
383 static void
384 set_paste_sensitivity (GtkClipboard *clip, GdkEventOwnerChange *event, gpointer data)
385 {
386   gint i;
387   gboolean compatible_target = FALSE;
388   PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (data);
389
390   for (i = 0 ; i < sizeof (targets) / sizeof (targets[0]) ; ++i)
391     {
392       GdkAtom atom = gdk_atom_intern (targets[i].target, TRUE);
393       if (gtk_clipboard_wait_is_target_available (clip, atom))
394         {
395           compatible_target = TRUE;
396           break;
397         }
398     }
399
400   g_object_set (sw->edit_paste, "enabled", compatible_target, NULL);
401 }
402
403
404 \f
405
406 /* Parse and execute all the text in the buffer */
407 static void
408 on_run_all (PsppireSyntaxWindow *se)
409 {
410   GtkTextIter begin, end;
411
412   gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &begin, 0);
413   gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (se->buffer), &end, -1);
414
415   editor_execute_syntax (se, begin, end);
416 }
417
418 /* Parse and execute the currently selected text */
419 static void
420 on_run_selection (PsppireSyntaxWindow *se)
421 {
422   GtkTextIter begin, end;
423
424   if (gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (se->buffer), &begin, &end))
425     editor_execute_syntax (se, begin, end);
426 }
427
428
429 /* Parse and execute the from the current line, to the end of the
430    buffer */
431 static void
432 on_run_to_end (PsppireSyntaxWindow *se)
433 {
434   GtkTextIter begin, end;
435   GtkTextIter here;
436   gint line;
437
438   /* Get the current line */
439   gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
440                                     &here,
441                                     gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
442                                 );
443
444   line = gtk_text_iter_get_line (&here) ;
445
446   /* Now set begin and end to the start of this line, and end of buffer
447      respectively */
448   gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
449   gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, -1);
450
451   editor_execute_syntax (se, begin, end);
452 }
453
454
455
456 /* Parse and execute the current line */
457 static void
458 on_run_current_line (PsppireSyntaxWindow *se)
459 {
460   GtkTextIter begin, end;
461   GtkTextIter here;
462   gint line;
463
464   /* Get the current line */
465   gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (se->buffer),
466                                     &here,
467                                     gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (se->buffer))
468                                 );
469
470   line = gtk_text_iter_get_line (&here) ;
471
472   /* Now set begin and end to the start of this line, and start of
473      following line respectively */
474   gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &begin, line);
475   gtk_text_buffer_get_iter_at_line (GTK_TEXT_BUFFER (se->buffer), &end, line + 1);
476
477   editor_execute_syntax (se, begin, end);
478 }
479
480
481
482 /* Append ".sps" to FILENAME if necessary.
483    The returned result must be freed when no longer required.
484  */
485 static gchar *
486 append_suffix (const gchar *filename)
487 {
488   if (! g_str_has_suffix (filename, ".sps") &&
489        ! g_str_has_suffix (filename, ".SPS"))
490     {
491       return g_strdup_printf ("%s.sps", filename);
492     }
493
494   return xstrdup (filename);
495 }
496
497 /*
498   Save BUFFER to the file called FILENAME.
499   FILENAME must be encoded in Glib filename encoding.
500   If successful, clears the buffer's modified flag.
501 */
502 static gboolean
503 save_editor_to_file (PsppireSyntaxWindow *se,
504                      const gchar *filename,
505                      GError **err)
506 {
507   GtkTextBuffer *buffer = GTK_TEXT_BUFFER (se->buffer);
508   struct substring text_locale;
509   gboolean result ;
510   GtkTextIter start, stop;
511   gchar *text;
512
513   gchar *suffixedname;
514   g_assert (filename);
515
516   suffixedname = append_suffix (filename);
517
518   gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
519   gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
520
521   text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
522
523   text_locale = recode_substring_pool (se->encoding, "UTF-8", ss_cstr (text),
524                                        NULL);
525
526   result =  g_file_set_contents (suffixedname, ss_data (text_locale),
527                                  ss_length (text_locale), err);
528
529   ss_dealloc (&text_locale);
530   g_free (suffixedname);
531
532   if (result)
533     {
534       char *fn = g_filename_display_name (filename);
535       gchar *msg = g_strdup_printf (_("Saved file `%s'"), fn);
536       g_free (fn);
537       gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
538       gtk_text_buffer_set_modified (buffer, FALSE);
539       g_free (msg);
540     }
541
542   return result;
543 }
544
545
546 /* PsppireWindow 'pick_Filename' callback. */
547 static void
548 syntax_pick_filename (PsppireWindow *window)
549 {
550   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (window);
551   const char *default_encoding;
552   GtkFileFilter *filter;
553   gint response;
554
555   GtkWidget *dialog =
556     gtk_file_chooser_dialog_new (_("Save Syntax"),
557                                  GTK_WINDOW (se),
558                                  GTK_FILE_CHOOSER_ACTION_SAVE,
559                                  _("Cancel"), GTK_RESPONSE_CANCEL,
560                                  _("Save"),   GTK_RESPONSE_ACCEPT,
561                                  NULL);
562
563   g_object_set (dialog, "local-only", FALSE, NULL);
564
565   filter = gtk_file_filter_new ();
566   gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
567   gtk_file_filter_add_pattern (filter, "*.sps");
568   gtk_file_filter_add_pattern (filter, "*.SPS");
569   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
570
571   filter = gtk_file_filter_new ();
572   gtk_file_filter_set_name (filter, _("All Files"));
573   gtk_file_filter_add_pattern (filter, "*");
574   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
575
576   gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
577                                                   TRUE);
578
579   default_encoding = se->encoding != NULL ? se->encoding : locale_charset ();
580   gtk_file_chooser_set_extra_widget (
581     GTK_FILE_CHOOSER (dialog),
582     psppire_encoding_selector_new (default_encoding, false));
583
584   response = gtk_dialog_run (GTK_DIALOG (dialog));
585
586   if (response == GTK_RESPONSE_ACCEPT)
587     {
588       gchar *encoding;
589       char *filename;
590
591       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
592       psppire_window_set_filename (window, filename);
593       free (filename);
594
595       encoding = psppire_encoding_selector_get_encoding (
596         gtk_file_chooser_get_extra_widget (GTK_FILE_CHOOSER (dialog)));
597       if (encoding != NULL)
598         {
599           g_free (se->encoding);
600           se->encoding = encoding;
601         }
602     }
603
604   gtk_widget_destroy (dialog);
605 }
606
607
608 /* PsppireWindow 'save' callback. */
609 static void
610 syntax_save (PsppireWindow *se)
611 {
612   const gchar *filename = psppire_window_get_filename (se);
613   GError *err = NULL;
614   save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
615   if (err)
616     {
617       msg (ME, "%s", err->message);
618       g_error_free (err);
619     }
620 }
621
622
623 static void
624 load_and_show_syntax_window (GtkWidget *se, const gchar *filename,
625                              const gchar *encoding)
626 {
627   gboolean ok;
628
629   gtk_source_buffer_begin_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
630   ok = psppire_window_load (PSPPIRE_WINDOW (se), filename, encoding, NULL);
631   gtk_source_buffer_end_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
632
633   if (ok)
634     gtk_widget_show (se);
635   else
636     gtk_widget_destroy (se);
637 }
638
639 void
640 create_syntax_window (void)
641 {
642   GtkWidget *w = psppire_syntax_window_new (NULL);
643
644   gtk_widget_show (w);
645 }
646
647 GtkWindow *
648 open_syntax_window (const char *file_name, const gchar *encoding)
649 {
650   GtkWidget *se = psppire_syntax_window_new (NULL);
651
652   if (file_name)
653     load_and_show_syntax_window (se, file_name, encoding);
654
655   return GTK_WINDOW (se);
656 }
657
658
659
660 static void psppire_syntax_window_print (PsppireSyntaxWindow *window);
661
662 static void
663 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
664 {
665   if (gtk_text_buffer_get_modified (buffer))
666     psppire_window_set_unsaved (window);
667 }
668
669 static void undo_redo_update (PsppireSyntaxWindow *window);
670 static void undo_last_edit (PsppireSyntaxWindow *window);
671 static void redo_last_edit (PsppireSyntaxWindow *window);
672
673 static void
674 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
675 {
676   gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
677   undo_redo_update (window);
678 }
679
680 static void
681 psppire_syntax_window_init (PsppireSyntaxWindow *window)
682 {
683   GtkBuilder *xml = builder_new ("syntax-editor.ui");
684   GtkWidget *box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
685
686   GObject *menu = get_object_assert (xml, "syntax-window-menu", G_TYPE_MENU);
687   GtkWidget *menubar = gtk_menu_bar_new_from_model (G_MENU_MODEL (menu));
688
689   GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
690
691   GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
692
693   PsppireSyntaxWindowClass *class
694     = PSPPIRE_SYNTAX_WINDOW_CLASS (G_OBJECT_GET_CLASS (window));
695
696   GtkClipboard *clip_selection = gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_CLIPBOARD);
697   GtkClipboard *clip_primary =   gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
698
699   window->print_settings = NULL;
700
701   window->undo_menuitem = g_simple_action_new ("undo", NULL);
702   window->redo_menuitem = g_simple_action_new ("redo", NULL);
703
704   g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->undo_menuitem));
705   g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->redo_menuitem));
706
707
708   if (class->lan)
709     window->buffer = gtk_source_buffer_new_with_language (class->lan);
710   else
711     window->buffer = gtk_source_buffer_new (NULL);
712
713   gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), GTK_TEXT_BUFFER (window->buffer));
714
715   g_object_set (window->buffer,
716                 "highlight-matching-brackets", TRUE,
717                 NULL);
718
719   g_object_set (text_view,
720                 "show-line-numbers", TRUE,
721                 "show-line-marks", TRUE,
722                 "auto-indent", TRUE,
723                 "indent-width", 4,
724                 "highlight-current-line", TRUE,
725                 NULL);
726
727   window->encoding = NULL;
728
729   window->cliptext = NULL;
730   window->dispose_has_run = FALSE;
731
732
733   window->edit_delete = g_simple_action_new ("delete", NULL);
734   g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_delete));
735
736   window->edit_copy = g_simple_action_new ("copy", NULL);
737   g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_copy));
738
739   window->edit_cut = g_simple_action_new ("cut", NULL);
740   g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_cut));
741
742   window->edit_paste = g_simple_action_new ("paste", NULL);
743   g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (window->edit_paste));
744
745
746   window->buffer = GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view)));
747
748   window->sb = get_widget_assert (xml, "statusbar2");
749   window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
750
751   g_signal_connect (window->buffer, "changed",
752                     G_CALLBACK (on_text_changed), window);
753
754   g_signal_connect (window->buffer, "modified-changed",
755                     G_CALLBACK (on_modified_changed), window);
756
757
758   {
759     GSimpleAction *print = g_simple_action_new ("print", NULL);
760
761     g_signal_connect_swapped (print, "activate",
762                               G_CALLBACK (psppire_syntax_window_print), window);
763
764     g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (print));
765   }
766
767   g_signal_connect_swapped (window->undo_menuitem,
768                             "activate",
769                             G_CALLBACK (undo_last_edit),
770                             window);
771
772   g_signal_connect_swapped (window->redo_menuitem,
773                             "activate",
774                             G_CALLBACK (redo_last_edit),
775                             window);
776
777   undo_redo_update (window);
778
779
780   window->sel_handler = g_signal_connect_swapped (clip_primary, "owner-change",
781                                                    G_CALLBACK (selection_changed), window);
782
783   window->ps_handler = g_signal_connect (clip_selection, "owner-change",
784                                           G_CALLBACK (set_paste_sensitivity), window);
785
786   connect_help (xml);
787
788   gtk_container_add (GTK_CONTAINER (window), box);
789
790   g_object_ref (sw);
791
792   g_object_ref (window->sb);
793
794   gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
795   gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
796   gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
797
798   gtk_widget_show_all (box);
799
800   GtkApplication *app = GTK_APPLICATION (g_application_get_default ());
801
802   {
803     GSimpleAction *open = g_simple_action_new ("open", NULL);
804
805     g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (open));
806
807     g_signal_connect_swapped (open,
808                               "activate",
809                               G_CALLBACK (psppire_window_open),
810                               window);
811   }
812
813   {
814     GSimpleAction *save = g_simple_action_new ("save", NULL);
815
816     g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (save));
817
818     g_signal_connect_swapped (save,
819                               "activate",
820                               G_CALLBACK (psppire_window_save),
821                               window);
822
823     const gchar *accels[2] = { "<Ctrl>S", NULL};
824     gtk_application_set_accels_for_action (app,
825                                            "win.save",
826                                            accels);
827
828   }
829
830   {
831     GSimpleAction *save_as = g_simple_action_new ("save_as", NULL);
832
833     g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (save_as));
834
835     g_signal_connect_swapped (save_as,
836                               "activate",
837                               G_CALLBACK (psppire_window_save_as),
838                               window);
839
840
841     const gchar *accels[2] = { "<Shift><Ctrl>S", NULL};
842     gtk_application_set_accels_for_action (app,
843                                            "win.save_as",
844                                            accels);
845
846
847   }
848
849
850   g_signal_connect_swapped (window->edit_delete,
851                     "activate",
852                     G_CALLBACK (on_edit_delete),
853                     window);
854
855   g_signal_connect_swapped (window->edit_copy,
856                     "activate",
857                     G_CALLBACK (on_edit_copy),
858                     window);
859
860   g_signal_connect_swapped (window->edit_cut,
861                     "activate",
862                     G_CALLBACK (on_edit_cut),
863                     window);
864
865   g_signal_connect_swapped (window->edit_paste,
866                     "activate",
867                     G_CALLBACK (on_edit_paste),
868                     window);
869
870   {
871     GSimpleAction *run_all = g_simple_action_new ("run-all", NULL);
872
873     g_signal_connect_swapped (run_all, "activate",
874                               G_CALLBACK (on_run_all), window);
875
876     g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (run_all));
877   }
878
879   {
880     GSimpleAction *run_current_line = g_simple_action_new ("run-current-line", NULL);
881
882     g_signal_connect_swapped (run_current_line, "activate",
883                               G_CALLBACK (on_run_current_line), window);
884
885     g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (run_current_line));
886
887     GtkApplication *app = GTK_APPLICATION (g_application_get_default ());
888     const gchar *accels[2] = { "<Ctrl>R", NULL};
889     gtk_application_set_accels_for_action (app,
890                                            "win.run-current-line",
891                                            accels);
892   }
893
894   {
895     GSimpleAction *run_selection = g_simple_action_new ("run-selection", NULL);
896
897     g_signal_connect_swapped (run_selection, "activate",
898                               G_CALLBACK (on_run_selection), window);
899
900     g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (run_selection));
901   }
902
903   {
904     GSimpleAction *run_to_end = g_simple_action_new ("run-to-end", NULL);
905
906     g_signal_connect_swapped (run_to_end, "activate",
907                               G_CALLBACK (on_run_to_end), window);
908
909     g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (run_to_end));
910   }
911
912   gtk_menu_shell_append (GTK_MENU_SHELL (menubar),
913                          create_windows_menu (GTK_WINDOW (window)));
914
915   gtk_menu_shell_append (GTK_MENU_SHELL (menubar),
916                          create_help_menu (GTK_WINDOW (window)));
917
918   g_object_unref (xml);
919 }
920
921
922
923
924
925 GtkWidget *
926 psppire_syntax_window_new (const char *encoding)
927 {
928   GObject *sw = g_object_new (psppire_syntax_window_get_type (),
929                               "description", _("Syntax Editor"),
930                               "encoding", encoding,
931                               NULL);
932
933   GApplication *app = g_application_get_default ();
934   gtk_application_add_window (GTK_APPLICATION (app), GTK_WINDOW (sw));
935
936   return GTK_WIDGET (sw);
937 }
938
939 static void
940 error_dialog (GtkWindow *w, const gchar *filename,  GError *err)
941 {
942   gchar *fn = g_filename_display_basename (filename);
943
944   GtkWidget *dialog =
945     gtk_message_dialog_new (w,
946                             GTK_DIALOG_DESTROY_WITH_PARENT,
947                             GTK_MESSAGE_ERROR,
948                             GTK_BUTTONS_CLOSE,
949                             _("Cannot load syntax file `%s'"),
950                             fn);
951
952   g_free (fn);
953
954   g_object_set (dialog, "icon-name", "pspp", NULL);
955
956   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
957                                             "%s", err->message);
958
959   gtk_dialog_run (GTK_DIALOG (dialog));
960
961   gtk_widget_destroy (dialog);
962 }
963
964 /*
965   Loads the buffer from the file called FILENAME
966 */
967 gboolean
968 syntax_load (PsppireWindow *window, const gchar *filename,
969              const gchar *encoding, gpointer not_used)
970 {
971   GError *err = NULL;
972   gchar *text_locale = NULL;
973   gchar *text_utf8 = NULL;
974   gsize len_locale = -1;
975   gsize len_utf8 = -1;
976   GtkTextIter iter;
977   PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
978   GtkTextBuffer *buffer = GTK_TEXT_BUFFER (sw->buffer);
979
980   /* FIXME: What if it's a very big file ? */
981   if (! g_file_get_contents (filename, &text_locale, &len_locale, &err))
982     {
983       error_dialog (GTK_WINDOW (window), filename, err);
984       g_clear_error (&err);
985       return FALSE;
986     }
987
988   if (!encoding || !encoding[0])
989     {
990       /* Determine the file's encoding and update sw->encoding.  (The ordering
991          is important here because encoding_guess_whole_file() often returns
992          its argument instead of a copy of it.) */
993       char *guessed_encoding;
994
995       guessed_encoding = g_strdup (encoding_guess_whole_file (sw->encoding,
996                                                               text_locale,
997                                                               len_locale));
998       g_free (sw->encoding);
999       sw->encoding = guessed_encoding;
1000     }
1001   else
1002     {
1003       g_free (sw->encoding);
1004       sw->encoding = g_strdup (encoding);
1005     }
1006
1007   text_utf8 = recode_substring_pool ("UTF-8", sw->encoding,
1008                                      ss_buffer (text_locale, len_locale),
1009                                      NULL).string;
1010   free (text_locale);
1011
1012   if (text_utf8 == NULL)
1013     {
1014       error_dialog (GTK_WINDOW (window), filename, err);
1015       g_clear_error (&err);
1016       return FALSE;
1017     }
1018
1019   gtk_text_buffer_get_iter_at_line (buffer, &iter, 0);
1020
1021   gtk_text_buffer_insert (buffer, &iter, text_utf8, len_utf8);
1022
1023   gtk_text_buffer_set_modified (buffer, FALSE);
1024
1025   free (text_utf8);
1026
1027   add_most_recent (filename, "text/x-spss-syntax", sw->encoding);
1028
1029   return TRUE;
1030 }
1031
1032 \f
1033
1034 static void
1035 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
1036 {
1037   iface->save = syntax_save;
1038   iface->pick_filename = syntax_pick_filename;
1039   iface->load = syntax_load;
1040 }
1041
1042
1043 \f
1044
1045 static void
1046 undo_redo_update (PsppireSyntaxWindow *window)
1047 {
1048   g_object_set (window->undo_menuitem, "enabled",
1049                 gtk_source_buffer_can_undo (window->buffer), NULL);
1050
1051   g_object_set  (window->redo_menuitem, "enabled",
1052                  gtk_source_buffer_can_redo (window->buffer), NULL);
1053 }
1054
1055 static void
1056 undo_last_edit (PsppireSyntaxWindow *window)
1057 {
1058   gtk_source_buffer_undo (window->buffer);
1059   undo_redo_update (window);
1060 }
1061
1062 static void
1063 redo_last_edit (PsppireSyntaxWindow *window)
1064 {
1065   gtk_source_buffer_redo (window->buffer);
1066   undo_redo_update (window);
1067 }
1068
1069
1070 \f
1071 /* Printing related stuff */
1072
1073
1074 static void
1075 begin_print (GtkPrintOperation *operation,
1076           GtkPrintContext   *context,
1077           PsppireSyntaxWindow *window)
1078 {
1079   window->compositor =
1080     gtk_source_print_compositor_new (window->buffer);
1081 }
1082
1083
1084 static void
1085 end_print (GtkPrintOperation *operation,
1086           GtkPrintContext   *context,
1087           PsppireSyntaxWindow *window)
1088 {
1089   g_object_unref (window->compositor);
1090   window->compositor = NULL;
1091 }
1092
1093
1094
1095 static gboolean
1096 paginate (GtkPrintOperation *operation,
1097           GtkPrintContext   *context,
1098           PsppireSyntaxWindow *window)
1099 {
1100   if (gtk_source_print_compositor_paginate (window->compositor, context))
1101     {
1102       gint n_pages = gtk_source_print_compositor_get_n_pages (window->compositor);
1103       gtk_print_operation_set_n_pages (operation, n_pages);
1104
1105       return TRUE;
1106     }
1107
1108   return FALSE;
1109 }
1110
1111 static void
1112 draw_page (GtkPrintOperation *operation,
1113            GtkPrintContext   *context,
1114            gint               page_nr,
1115           PsppireSyntaxWindow *window)
1116 {
1117   gtk_source_print_compositor_draw_page (window->compositor,
1118                                          context,
1119                                          page_nr);
1120 }
1121
1122
1123
1124 static void
1125 psppire_syntax_window_print (PsppireSyntaxWindow *window)
1126 {
1127   GtkPrintOperationResult res;
1128
1129   GtkPrintOperation *print = gtk_print_operation_new ();
1130
1131   if (window->print_settings != NULL)
1132     gtk_print_operation_set_print_settings (print, window->print_settings);
1133
1134
1135   g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), window);
1136   g_signal_connect (print, "end_print", G_CALLBACK (end_print),     window);
1137   g_signal_connect (print, "draw_page", G_CALLBACK (draw_page),     window);
1138   g_signal_connect (print, "paginate", G_CALLBACK (paginate),       window);
1139
1140   res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
1141                                  GTK_WINDOW (window), NULL);
1142
1143   if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
1144     {
1145       if (window->print_settings != NULL)
1146         g_object_unref (window->print_settings);
1147       window->print_settings = g_object_ref (gtk_print_operation_get_print_settings (print));
1148     }
1149
1150   g_object_unref (print);
1151 }