Syntax Window: Edit->* item sensitivity follows selection
[pspp] / src / ui / gui / psppire-syntax-window.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2008, 2009, 2010  Free Software Foundation
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <gtk/gtksignal.h>
20 #include <gtk/gtkbox.h>
21 #include "executor.h"
22 #include "helper.h"
23
24 #include <libpspp/message.h>
25 #include <stdlib.h>
26
27 #include "psppire.h"
28 #include "psppire-syntax-window.h"
29
30 #include "psppire-data-window.h"
31 #include "psppire-window-register.h"
32 #include "psppire.h"
33 #include "help-menu.h"
34 #include "psppire-syntax-window.h"
35 #include "syntax-editor-source.h"
36 #include <language/lexer/lexer.h>
37
38 #include "xalloc.h"
39
40 #include <gettext.h>
41 #define _(msgid) gettext (msgid)
42 #define N_(msgid) msgid
43
44 static void psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *, gpointer);
45 static void psppire_syntax_window_base_init     (PsppireSyntaxWindowClass *class);
46 static void psppire_syntax_window_class_init    (PsppireSyntaxWindowClass *class);
47 static void psppire_syntax_window_init          (PsppireSyntaxWindow      *syntax_editor);
48
49
50 static void psppire_syntax_window_iface_init (PsppireWindowIface *iface);
51
52
53 GType
54 psppire_syntax_window_get_type (void)
55 {
56   static GType psppire_syntax_window_type = 0;
57
58   if (!psppire_syntax_window_type)
59     {
60       static const GTypeInfo psppire_syntax_window_info =
61       {
62         sizeof (PsppireSyntaxWindowClass),
63         (GBaseInitFunc) psppire_syntax_window_base_init,
64         (GBaseFinalizeFunc) psppire_syntax_window_base_finalize,
65         (GClassInitFunc)psppire_syntax_window_class_init,
66         (GClassFinalizeFunc) NULL,
67         NULL,
68         sizeof (PsppireSyntaxWindow),
69         0,
70         (GInstanceInitFunc) psppire_syntax_window_init,
71       };
72
73       static const GInterfaceInfo window_interface_info =
74         {
75           (GInterfaceInitFunc) psppire_syntax_window_iface_init,
76           NULL,
77           NULL
78         };
79
80       psppire_syntax_window_type =
81         g_type_register_static (PSPPIRE_TYPE_WINDOW, "PsppireSyntaxWindow",
82                                 &psppire_syntax_window_info, 0);
83
84       g_type_add_interface_static (psppire_syntax_window_type,
85                                    PSPPIRE_TYPE_WINDOW_MODEL,
86                                    &window_interface_info);
87     }
88
89   return psppire_syntax_window_type;
90 }
91
92 static GObjectClass *parent_class ;
93
94 static void
95 psppire_syntax_window_finalize (GObject *object)
96 {
97   if (G_OBJECT_CLASS (parent_class)->finalize)
98     (*G_OBJECT_CLASS (parent_class)->finalize) (object);
99 }
100
101
102 static void
103 psppire_syntax_window_class_init (PsppireSyntaxWindowClass *class)
104 {
105   parent_class = g_type_class_peek_parent (class);
106 }
107
108
109 static void
110 psppire_syntax_window_base_init (PsppireSyntaxWindowClass *class)
111 {
112   GObjectClass *object_class = G_OBJECT_CLASS (class);
113
114   object_class->finalize = psppire_syntax_window_finalize;
115 }
116
117
118
119 static void
120 psppire_syntax_window_base_finalize (PsppireSyntaxWindowClass *class,
121                                      gpointer class_data)
122 {
123 }
124
125
126 static void
127 editor_execute_syntax (const PsppireSyntaxWindow *sw, GtkTextIter start,
128                        GtkTextIter stop)
129 {
130   PsppireWindow *win = PSPPIRE_WINDOW (sw);
131   const gchar *name = psppire_window_get_filename (win);
132   execute_syntax (create_syntax_editor_source (sw->buffer, start, stop, name));
133 }
134
135
136 \f
137
138 /* Delete the currently selected text */
139 static void
140 on_edit_delete (PsppireSyntaxWindow *sw)
141 {
142   GtkTextIter begin, end;
143   
144   if ( gtk_text_buffer_get_selection_bounds (sw->buffer, &begin, &end) )
145     gtk_text_buffer_delete (sw->buffer, &begin, &end);
146 }
147
148 /* The syntax editor's clipboard deals only with text */
149 enum {
150   SELECT_FMT_NULL,
151   SELECT_FMT_TEXT,
152 };
153
154
155 static void
156 selection_changed (PsppireSyntaxWindow *sw)
157 {
158   gboolean sel = gtk_text_buffer_get_has_selection (sw->buffer);
159
160   gtk_action_set_sensitive (sw->edit_copy, sel);
161   gtk_action_set_sensitive (sw->edit_cut, sel);
162   gtk_action_set_sensitive (sw->edit_delete, sel);
163 }
164
165 /* The callback which runs when something request clipboard data */
166 static void
167 clipboard_get_cb (GtkClipboard     *clipboard,
168                   GtkSelectionData *selection_data,
169                   guint             info,
170                   gpointer          data)
171 {
172   PsppireSyntaxWindow *sw = data;
173   g_assert (info == SELECT_FMT_TEXT);
174
175   gtk_selection_data_set (selection_data, selection_data->target,
176                           8,
177                           (const guchar *) sw->cliptext, strlen (sw->cliptext));
178
179 }
180
181 static void
182 clipboard_clear_cb (GtkClipboard *clipboard,
183                     gpointer data)
184 {
185   PsppireSyntaxWindow *sw = data;
186   g_free (sw->cliptext);
187   sw->cliptext = NULL;
188 }
189
190
191 static const GtkTargetEntry targets[] = {
192   { "UTF8_STRING",   0, SELECT_FMT_TEXT },
193   { "STRING",        0, SELECT_FMT_TEXT },
194   { "TEXT",          0, SELECT_FMT_TEXT },
195   { "COMPOUND_TEXT", 0, SELECT_FMT_TEXT },
196   { "text/plain;charset=utf-8", 0, SELECT_FMT_TEXT },
197   { "text/plain",    0, SELECT_FMT_TEXT },
198 };
199
200
201 /*
202   Store a clip containing the currently selected text.
203   Returns true iff something was set.
204   As a side effect, begin and end will be set to indicate
205   the limits of the selected text.
206 */
207 static gboolean
208 set_clip (PsppireSyntaxWindow *sw, GtkTextIter *begin, GtkTextIter *end)
209 {
210   GtkClipboard *clipboard ;
211
212   if ( ! gtk_text_buffer_get_selection_bounds (sw->buffer, begin, end) )
213     return FALSE;
214
215   g_free (sw->cliptext);
216   sw->cliptext = gtk_text_buffer_get_text  (sw->buffer, begin, end, FALSE);
217
218   clipboard =
219     gtk_widget_get_clipboard (GTK_WIDGET (sw), GDK_SELECTION_CLIPBOARD);
220
221   if (!gtk_clipboard_set_with_owner (clipboard, targets,
222                                      G_N_ELEMENTS (targets),
223                                      clipboard_get_cb, clipboard_clear_cb,
224                                      G_OBJECT (sw)))
225     clipboard_clear_cb (clipboard, sw);
226
227   return TRUE;
228 }
229
230 static void
231 on_edit_cut (PsppireSyntaxWindow *sw)
232 {
233   GtkTextIter begin, end;
234   
235   if ( set_clip (sw, &begin, &end))
236     gtk_text_buffer_delete (sw->buffer, &begin, &end);
237 }
238
239 static void
240 on_edit_copy (PsppireSyntaxWindow *sw)
241 {
242   GtkTextIter begin, end;
243
244   set_clip (sw, &begin, &end);
245 }
246
247 \f
248
249 /* Parse and execute all the text in the buffer */
250 static void
251 on_run_all (GtkMenuItem *menuitem, gpointer user_data)
252 {
253   GtkTextIter begin, end;
254   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
255
256   gtk_text_buffer_get_iter_at_offset (se->buffer, &begin, 0);
257   gtk_text_buffer_get_iter_at_offset (se->buffer, &end, -1);
258
259   editor_execute_syntax (se, begin, end);
260 }
261
262 /* Parse and execute the currently selected text */
263 static void
264 on_run_selection (GtkMenuItem *menuitem, gpointer user_data)
265 {
266   GtkTextIter begin, end;
267   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
268
269   if ( gtk_text_buffer_get_selection_bounds (se->buffer, &begin, &end) )
270     editor_execute_syntax (se, begin, end);
271 }
272
273
274 /* Parse and execute the from the current line, to the end of the
275    buffer */
276 static void
277 on_run_to_end (GtkMenuItem *menuitem, gpointer user_data)
278 {
279   GtkTextIter begin, end;
280   GtkTextIter here;
281   gint line;
282
283   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
284
285   /* Get the current line */
286   gtk_text_buffer_get_iter_at_mark (se->buffer,
287                                     &here,
288                                     gtk_text_buffer_get_insert (se->buffer)
289                                     );
290
291   line = gtk_text_iter_get_line (&here) ;
292
293   /* Now set begin and end to the start of this line, and end of buffer
294      respectively */
295   gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
296   gtk_text_buffer_get_iter_at_line (se->buffer, &end, -1);
297
298   editor_execute_syntax (se, begin, end);
299 }
300
301
302
303 /* Parse and execute the current line */
304 static void
305 on_run_current_line (GtkMenuItem *menuitem, gpointer user_data)
306 {
307   GtkTextIter begin, end;
308   GtkTextIter here;
309   gint line;
310
311   PsppireSyntaxWindow *se = PSPPIRE_SYNTAX_WINDOW (user_data);
312
313   /* Get the current line */
314   gtk_text_buffer_get_iter_at_mark (se->buffer,
315                                     &here,
316                                     gtk_text_buffer_get_insert (se->buffer)
317                                     );
318
319   line = gtk_text_iter_get_line (&here) ;
320
321   /* Now set begin and end to the start of this line, and start of
322      following line respectively */
323   gtk_text_buffer_get_iter_at_line (se->buffer, &begin, line);
324   gtk_text_buffer_get_iter_at_line (se->buffer, &end, line + 1);
325
326   editor_execute_syntax (se, begin, end);
327 }
328
329
330
331 /* Append ".sps" to FILENAME if necessary.
332    The returned result must be freed when no longer required.
333  */
334 static gchar *
335 append_suffix (const gchar *filename)
336 {
337   if ( ! g_str_has_suffix (filename, ".sps" ) &&
338        ! g_str_has_suffix (filename, ".SPS" ) )
339     {
340       return g_strdup_printf ("%s.sps", filename);
341     }
342
343   return xstrdup (filename);
344 }
345
346 /*
347   Save BUFFER to the file called FILENAME.
348   FILENAME must be encoded in Glib filename encoding.
349   If successful, clears the buffer's modified flag.
350 */
351 static gboolean
352 save_editor_to_file (PsppireSyntaxWindow *se,
353                      const gchar *filename,
354                      GError **err)
355 {
356   GtkTextBuffer *buffer = se->buffer;
357   gboolean result ;
358   GtkTextIter start, stop;
359   gchar *text;
360
361   gchar *suffixedname;
362   g_assert (filename);
363
364   suffixedname = append_suffix (filename);
365
366   gtk_text_buffer_get_iter_at_line (buffer, &start, 0);
367   gtk_text_buffer_get_iter_at_offset (buffer, &stop, -1);
368
369   text = gtk_text_buffer_get_text (buffer, &start, &stop, FALSE);
370
371   result =  g_file_set_contents (suffixedname, text, -1, err);
372
373   g_free (suffixedname);
374
375   if ( result )
376     {
377       char *fn = g_filename_display_name (filename);
378       gchar *msg = g_strdup_printf (_("Saved file \"%s\""), fn);
379       g_free (fn);
380       gtk_statusbar_push (GTK_STATUSBAR (se->sb), se->text_context, msg);
381       gtk_text_buffer_set_modified (buffer, FALSE);
382       g_free (msg);
383     }
384
385   return result;
386 }
387
388
389 /* Callback for the File->SaveAs menuitem */
390 static void
391 syntax_save_as (PsppireWindow *se)
392 {
393   GtkFileFilter *filter;
394   gint response;
395
396   GtkWidget *dialog =
397     gtk_file_chooser_dialog_new (_("Save Syntax"),
398                                  GTK_WINDOW (se),
399                                  GTK_FILE_CHOOSER_ACTION_SAVE,
400                                  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
401                                  GTK_STOCK_SAVE,   GTK_RESPONSE_ACCEPT,
402                                  NULL);
403
404   filter = gtk_file_filter_new ();
405   gtk_file_filter_set_name (filter, _("Syntax Files (*.sps) "));
406   gtk_file_filter_add_pattern (filter, "*.sps");
407   gtk_file_filter_add_pattern (filter, "*.SPS");
408   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
409
410   filter = gtk_file_filter_new ();
411   gtk_file_filter_set_name (filter, _("All Files"));
412   gtk_file_filter_add_pattern (filter, "*");
413   gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
414
415   gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog),
416                                                   TRUE);
417   response = gtk_dialog_run (GTK_DIALOG (dialog));
418
419   if ( response == GTK_RESPONSE_ACCEPT )
420     {
421       GError *err = NULL;
422       char *filename =
423         gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog) );
424
425       if ( ! save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err) )
426         {
427           msg ( ME, "%s", err->message );
428           g_error_free (err);
429         }
430
431       free (filename);
432     }
433
434   gtk_widget_destroy (dialog);
435 }
436
437
438 /* Callback for the File->Save menuitem */
439 static void
440 syntax_save (PsppireWindow *se)
441 {
442   const gchar *filename = psppire_window_get_filename (se);
443
444   if ( filename == NULL )
445     syntax_save_as (se);
446   else
447     {
448       GError *err = NULL;
449       save_editor_to_file (PSPPIRE_SYNTAX_WINDOW (se), filename, &err);
450       if ( err )
451         {
452           msg (ME, "%s", err->message);
453           g_error_free (err);
454         }
455     }
456 }
457
458
459 /* Callback for the File->Quit menuitem */
460 static gboolean
461 on_quit (GtkMenuItem *menuitem, gpointer    user_data)
462 {
463   psppire_quit ();
464
465   return FALSE;
466 }
467
468
469 void
470 create_syntax_window (void)
471 {
472   GtkWidget *w = psppire_syntax_window_new ();
473   gtk_widget_show (w);
474 }
475
476 void
477 open_syntax_window (const char *file_name)
478 {
479   GtkWidget *se = psppire_syntax_window_new ();
480
481   if ( psppire_window_load (PSPPIRE_WINDOW (se), file_name) )
482     gtk_widget_show (se);
483   else
484     gtk_widget_destroy (se);
485 }
486
487 static void
488 on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
489 {
490   gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
491 }
492
493 static void
494 on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
495 {
496   if (gtk_text_buffer_get_modified (buffer))
497     psppire_window_set_unsaved (window);
498 }
499
500 extern struct source_stream *the_source_stream ;
501
502 static void
503 psppire_syntax_window_init (PsppireSyntaxWindow *window)
504 {
505   GtkBuilder *xml = builder_new ("syntax-editor.ui");
506   GtkWidget *box = gtk_vbox_new (FALSE, 0);
507
508   GtkWidget *menubar = get_widget_assert (xml, "menubar");
509   GtkWidget *sw = get_widget_assert (xml, "scrolledwindow8");
510
511
512   GtkWidget *text_view = get_widget_assert (xml, "syntax_text_view");
513
514
515   GtkClipboard *clipboard =
516     gtk_widget_get_clipboard (GTK_WIDGET (window), GDK_SELECTION_PRIMARY);
517
518   g_signal_connect_swapped (clipboard, "owner-change", G_CALLBACK (selection_changed), window);
519
520   window->cliptext = NULL;
521
522   window->edit_delete = get_action_assert (xml, "edit_delete");
523   window->edit_copy = get_action_assert (xml, "edit_copy");
524   window->edit_cut = get_action_assert (xml, "edit_cut");
525
526   window->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
527   window->lexer = lex_create (the_source_stream);
528
529   window->sb = get_widget_assert (xml, "statusbar2");
530   window->text_context = gtk_statusbar_get_context_id (GTK_STATUSBAR (window->sb), "Text Context");
531
532   g_signal_connect (window->buffer, "changed", G_CALLBACK (on_text_changed), window);
533
534   g_signal_connect (window->buffer, "modified-changed",
535                     G_CALLBACK (on_modified_changed), window);
536
537   connect_help (xml);
538
539   gtk_container_add (GTK_CONTAINER (window), box);
540
541   g_object_ref (menubar);
542
543   g_object_ref (sw);
544
545   g_object_ref (window->sb);
546
547
548   gtk_box_pack_start (GTK_BOX (box), menubar, FALSE, TRUE, 0);
549   gtk_box_pack_start (GTK_BOX (box), sw, TRUE, TRUE, 0);
550   gtk_box_pack_start (GTK_BOX (box), window->sb, FALSE, TRUE, 0);
551
552   gtk_widget_show_all (box);
553
554   g_signal_connect_swapped (get_action_assert (xml,"file_new_syntax"), "activate", G_CALLBACK (create_syntax_window), NULL);
555
556 #if 0
557   g_signal_connect (get_action_assert (xml,"file_new_data"),
558                     "activate",
559                     G_CALLBACK (create_data_window),
560                     window);
561 #endif
562
563   g_signal_connect_swapped (get_action_assert (xml, "file_save"),
564                     "activate",
565                     G_CALLBACK (syntax_save),
566                     window);
567
568   g_signal_connect_swapped (get_action_assert (xml, "file_save_as"),
569                     "activate",
570                     G_CALLBACK (syntax_save_as),
571                     window);
572
573   g_signal_connect (get_action_assert (xml,"file_quit"),
574                     "activate",
575                     G_CALLBACK (on_quit),
576                     window);
577
578   g_signal_connect_swapped (window->edit_delete,
579                     "activate",
580                     G_CALLBACK (on_edit_delete),
581                     window);
582
583   g_signal_connect_swapped (window->edit_copy,
584                     "activate",
585                     G_CALLBACK (on_edit_copy),
586                     window);
587
588   g_signal_connect_swapped (window->edit_cut,
589                     "activate",
590                     G_CALLBACK (on_edit_cut),
591                     window);
592
593   g_signal_connect (get_action_assert (xml,"run_all"),
594                     "activate",
595                     G_CALLBACK (on_run_all),
596                     window);
597
598   g_signal_connect (get_action_assert (xml,"run_selection"),
599                     "activate",
600                     G_CALLBACK (on_run_selection),
601                     window);
602
603   g_signal_connect (get_action_assert (xml,"run_current_line"),
604                     "activate",
605                     G_CALLBACK (on_run_current_line),
606                     window);
607
608   g_signal_connect (get_action_assert (xml,"run_to_end"),
609                     "activate",
610                     G_CALLBACK (on_run_to_end),
611                     window);
612
613   g_signal_connect (get_action_assert (xml,"windows_minimise_all"),
614                     "activate",
615                     G_CALLBACK (psppire_window_minimise_all), NULL);
616
617
618   {
619   GtkUIManager *uim = GTK_UI_MANAGER (get_object_assert (xml, "uimanager1", GTK_TYPE_UI_MANAGER));
620
621   merge_help_menu (uim);
622
623   PSPPIRE_WINDOW (window)->menu =
624     GTK_MENU_SHELL (gtk_ui_manager_get_widget (uim,"/ui/menubar/windows/windows_minimise_all")->parent);
625   }
626
627   g_object_unref (xml);
628 }
629
630
631
632
633
634 GtkWidget*
635 psppire_syntax_window_new (void)
636 {
637   return GTK_WIDGET (g_object_new (psppire_syntax_window_get_type (),
638                                    "filename", "Syntax",
639                                    "description", _("Syntax Editor"),
640                                    NULL));
641 }
642
643 static void
644 error_dialog (GtkWindow *w, const gchar *filename,  GError *err)
645 {
646   gchar *fn = g_filename_display_basename (filename);
647
648   GtkWidget *dialog =
649     gtk_message_dialog_new (w,
650                             GTK_DIALOG_DESTROY_WITH_PARENT,
651                             GTK_MESSAGE_ERROR,
652                             GTK_BUTTONS_CLOSE,
653                             _("Cannot load syntax file '%s'"),
654                             fn);
655
656   g_free (fn);
657
658   g_object_set (dialog, "icon-name", "psppicon", NULL);
659
660   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
661                                             "%s", err->message);
662
663   gtk_dialog_run (GTK_DIALOG (dialog));
664
665   gtk_widget_destroy (dialog);
666 }
667
668 /*
669   Loads the buffer from the file called FILENAME
670 */
671 gboolean
672 syntax_load (PsppireWindow *window, const gchar *filename)
673 {
674   GError *err = NULL;
675   gchar *text_locale = NULL;
676   gchar *text_utf8 = NULL;
677   gsize len_locale = -1;
678   gsize len_utf8 = -1;
679   GtkTextIter iter;
680   PsppireSyntaxWindow *sw = PSPPIRE_SYNTAX_WINDOW (window);
681
682   /* FIXME: What if it's a very big file ? */
683   if ( ! g_file_get_contents (filename, &text_locale, &len_locale, &err) )
684     {
685       error_dialog (GTK_WINDOW (window), filename, err);
686       g_clear_error (&err);
687       return FALSE;
688     }
689
690   text_utf8 = g_locale_to_utf8 (text_locale, len_locale, NULL, &len_utf8, &err);
691
692   free (text_locale);
693
694   if ( text_utf8 == NULL )
695     {
696       error_dialog (GTK_WINDOW (window), filename, err);
697       g_clear_error (&err);
698       return FALSE;
699     }
700
701   gtk_text_buffer_get_iter_at_line (sw->buffer, &iter, 0);
702
703   gtk_text_buffer_insert (sw->buffer, &iter, text_utf8, len_utf8);
704
705   gtk_text_buffer_set_modified (sw->buffer, FALSE);
706
707   free (text_utf8);
708
709   return TRUE;
710 }
711
712 \f
713
714 static void
715 psppire_syntax_window_iface_init (PsppireWindowIface *iface)
716 {
717   iface->save = syntax_save;
718   iface->load = syntax_load;
719 }
720
721
722