From: John Darrington <john@darrington.wattle.id.au>
Date: Sun, 8 Aug 2010 15:23:25 +0000 (+0200)
Subject: Added basic undo/redo to syntax window
X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c87318f12b3024c7abe25067b1a0aefbfe2e94b0;p=pspp

Added basic undo/redo to syntax window
---

diff --git a/src/ui/gui/psppire-data-window.c b/src/ui/gui/psppire-data-window.c
index e6d905e0b5..a5cd15c0d5 100644
--- a/src/ui/gui/psppire-data-window.c
+++ b/src/ui/gui/psppire-data-window.c
@@ -465,7 +465,7 @@ open_window (PsppireWindow *de)
 	if (any_reader_may_open (sysname))
 	  psppire_window_load (de, name);
 	else
-	  open_syntax_window (name);
+	  open_new_syntax_window (name);
 
 	g_free (sysname);
 	g_free (name);
@@ -853,8 +853,6 @@ on_recent_files_select (GtkMenuShell *menushell,   gpointer user_data)
 {
   gchar *file;
 
-  GtkWidget *se ;
-
   gchar *uri =
     gtk_recent_chooser_get_current_uri (GTK_RECENT_CHOOSER (menushell));
 
@@ -862,17 +860,13 @@ on_recent_files_select (GtkMenuShell *menushell,   gpointer user_data)
 
   g_free (uri);
 
-  se = psppire_syntax_window_new ();
-
-  if ( psppire_window_load (PSPPIRE_WINDOW (se), file) ) 
-    gtk_widget_show (se);
-  else
-    gtk_widget_destroy (se);
+  open_new_syntax_window (file);
 
   g_free (file);
 }
 
 
+
 static void
 enable_delete_cases (GtkWidget *w, gint case_num, gpointer data)
 {
diff --git a/src/ui/gui/psppire-syntax-window.c b/src/ui/gui/psppire-syntax-window.c
index 2bdebadd52..f0a33dbf85 100644
--- a/src/ui/gui/psppire-syntax-window.c
+++ b/src/ui/gui/psppire-syntax-window.c
@@ -376,6 +376,21 @@ on_quit (GtkMenuItem *menuitem, gpointer    user_data)
 }
 
 
+static void
+load_and_show_syntax_window (GtkWidget *se, const gchar *filename)
+{
+  gboolean ok;
+
+  gtk_source_buffer_begin_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
+  ok = psppire_window_load (PSPPIRE_WINDOW (se), filename);
+  gtk_source_buffer_end_not_undoable_action (PSPPIRE_SYNTAX_WINDOW (se)->buffer);
+
+  if (ok )
+    gtk_widget_show (se);
+  else
+    gtk_widget_destroy (se);
+}
+
 void
 create_syntax_window (void)
 {
@@ -384,21 +399,15 @@ create_syntax_window (void)
 }
 
 void
-open_syntax_window (const char *file_name)
+open_new_syntax_window (const char *file_name)
 {
   GtkWidget *se = psppire_syntax_window_new ();
 
-  if ( psppire_window_load (PSPPIRE_WINDOW (se), file_name) )
-    gtk_widget_show (se);
-  else
-    gtk_widget_destroy (se);
+  if ( file_name)
+    load_and_show_syntax_window (se, file_name);
 }
 
-static void
-on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
-{
-  gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
-}
+
 
 static void psppire_syntax_window_print (PsppireSyntaxWindow *window);
 
@@ -411,6 +420,17 @@ on_modified_changed (GtkTextBuffer *buffer, PsppireWindow *window)
 
 extern struct source_stream *the_source_stream ;
 
+static void undo_redo_update (PsppireSyntaxWindow *window);
+static void undo_last_edit (PsppireSyntaxWindow *window);
+static void redo_last_edit (PsppireSyntaxWindow *window);
+
+static void
+on_text_changed (GtkTextBuffer *buffer, PsppireSyntaxWindow *window)
+{
+  gtk_statusbar_pop (GTK_STATUSBAR (window->sb), window->text_context);
+  undo_redo_update (window);
+}
+
 static void
 psppire_syntax_window_init (PsppireSyntaxWindow *window)
 {
@@ -426,6 +446,8 @@ psppire_syntax_window_init (PsppireSyntaxWindow *window)
     = PSPPIRE_SYNTAX_WINDOW_CLASS (G_OBJECT_GET_CLASS (window));
 
   window->print_settings = NULL;
+  window->undo_menuitem = get_action_assert (xml, "edit_undo");
+  window->redo_menuitem = get_action_assert (xml, "edit_redo");
 
   if (class->lan)
     window->buffer = gtk_source_buffer_new_with_language (class->lan);
@@ -459,6 +481,19 @@ psppire_syntax_window_init (PsppireSyntaxWindow *window)
   g_signal_connect_swapped (get_action_assert (xml, "file_print"), "activate",
                             G_CALLBACK (psppire_syntax_window_print), window);
 
+
+  g_signal_connect_swapped (window->undo_menuitem,
+			    "activate",
+                            G_CALLBACK (undo_last_edit),
+			    window);
+
+  g_signal_connect_swapped (window->redo_menuitem,
+			    "activate",
+                            G_CALLBACK (redo_last_edit),
+			    window);
+
+  undo_redo_update (window);
+
   connect_help (xml);
 
   gtk_container_add (GTK_CONTAINER (window), box);
@@ -629,6 +664,32 @@ psppire_syntax_window_iface_init (PsppireWindowIface *iface)
   iface->load = syntax_load;
 }
 
+
+
+static void
+undo_redo_update (PsppireSyntaxWindow *window)
+{
+  gtk_action_set_sensitive (window->undo_menuitem,
+			    gtk_source_buffer_can_undo (window->buffer));
+
+  gtk_action_set_sensitive (window->redo_menuitem,
+			    gtk_source_buffer_can_redo (window->buffer));
+}
+
+static void
+undo_last_edit (PsppireSyntaxWindow *window)
+{
+  gtk_source_buffer_undo (window->buffer);
+  undo_redo_update (window);
+}
+
+static void
+redo_last_edit (PsppireSyntaxWindow *window)
+{
+  gtk_source_buffer_redo (window->buffer);
+  undo_redo_update (window);
+}
+
 
 
 /* Printing related stuff */
diff --git a/src/ui/gui/psppire-syntax-window.h b/src/ui/gui/psppire-syntax-window.h
index 2f36fd2c26..14710df2d5 100644
--- a/src/ui/gui/psppire-syntax-window.h
+++ b/src/ui/gui/psppire-syntax-window.h
@@ -59,6 +59,8 @@ struct _PsppireSyntaxWindow
 
   GtkPrintSettings *print_settings;
   GtkSourcePrintCompositor *compositor;
+  GtkAction *undo_menuitem;
+  GtkAction *redo_menuitem;
 };
 
 struct _PsppireSyntaxWindowClass
@@ -73,7 +75,8 @@ GType      psppire_syntax_window_get_type        (void);
 GtkWidget* psppire_syntax_window_new             (void);
 
 void create_syntax_window (void);
-void open_syntax_window (const char *file_name);
+void open_new_syntax_window (const char *file_name);
+
 
 G_END_DECLS
 
diff --git a/src/ui/gui/syntax-editor.ui b/src/ui/gui/syntax-editor.ui
index bffa173170..a345097376 100644
--- a/src/ui/gui/syntax-editor.ui
+++ b/src/ui/gui/syntax-editor.ui
@@ -77,27 +77,39 @@
           </object>
         </child>
         <child>
-          <object class="GtkAction" id="cut2">
+          <object class="GtkAction" id="edit_cut">
             <property name="stock-id">gtk-cut</property>
-            <property name="name">cut2</property>
+            <property name="name">cut</property>
           </object>
         </child>
         <child>
-          <object class="GtkAction" id="copy2">
+          <object class="GtkAction" id="edit_copy">
             <property name="stock-id">gtk-copy</property>
-            <property name="name">copy2</property>
+            <property name="name">copy</property>
           </object>
         </child>
         <child>
-          <object class="GtkAction" id="paste2">
+          <object class="GtkAction" id="edit_paste">
             <property name="stock-id">gtk-paste</property>
-            <property name="name">paste2</property>
+            <property name="name">paste</property>
           </object>
         </child>
         <child>
-          <object class="GtkAction" id="delete1">
+          <object class="GtkAction" id="edit_delete">
             <property name="stock-id">gtk-delete</property>
-            <property name="name">delete1</property>
+            <property name="name">delete</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkAction" id="edit_undo">
+            <property name="stock-id">gtk-undo</property>
+            <property name="name">edit_undo</property>
+          </object>
+        </child>
+        <child>
+          <object class="GtkAction" id="edit_redo">
+            <property name="stock-id">gtk-redo</property>
+            <property name="name">edit_redo</property>
           </object>
         </child>
         <child>
@@ -164,10 +176,13 @@
           <menuitem action="file_quit"/>
         </menu>
         <menu action="menuitem7">
-          <menuitem action="cut2"/>
-          <menuitem action="copy2"/>
-          <menuitem action="paste2"/>
-          <menuitem action="delete1"/>
+          <menuitem action="edit_cut"/>
+          <menuitem action="edit_copy"/>
+          <menuitem action="edit_paste"/>
+          <menuitem action="edit_delete"/>
+          <separator/>
+          <menuitem action="edit_undo"/>
+          <menuitem action="edit_redo"/>
         </menu>
         <menu action="run1">
           <menuitem action="run_all"/>