[gnome-latex: 16/205] undo/redo



commit 14caf78809909eab9673b98f23b32d5c71328f51
Author: Sébastien Wilmet <sebastien wilmet gmail com>
Date:   Wed Aug 5 20:46:08 2009 +0200

    undo/redo

 TODO            |  4 ++--
 src/callbacks.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 src/callbacks.h |  2 ++
 src/main.c      |  8 ++++++--
 src/main.h      |  2 ++
 src/ui.xml      |  6 +++---
 6 files changed, 58 insertions(+), 7 deletions(-)
---
diff --git a/TODO b/TODO
index cd4db6e..fedeb8f 100644
--- a/TODO
+++ b/TODO
@@ -14,8 +14,8 @@ Jul 31, 2009 to Aug 7, 2009
        x change title of tabs
        - tabs with close buttons
 
-[-] GtkSourceView
+[x] GtkSourceView
        x syntaxic color
        x show/hide line numbers
-       - undo/redo
+       x undo/redo
 
diff --git a/src/callbacks.c b/src/callbacks.c
index ee9689d..5936038 100644
--- a/src/callbacks.c
+++ b/src/callbacks.c
@@ -16,6 +16,7 @@ static void save_as_dialog (void);
 static void file_save (void);
 static gboolean close_all (void);
 static void set_title (void);
+static void set_undo_redo_sensitivity (void);
 
 void
 cb_new (void)
@@ -157,6 +158,8 @@ cb_close (void)
                        docs.active = g_list_nth_data (docs.all, gtk_notebook_get_current_page 
(docs.notebook));
                else
                        docs.active = NULL;
+
+               set_undo_redo_sensitivity ();
        }
 
        else
@@ -173,6 +176,24 @@ cb_quit (void)
        }
 }
 
+void
+cb_undo (void)
+{
+       if (gtk_source_buffer_can_undo (docs.active->source_buffer))
+               gtk_source_buffer_undo (docs.active->source_buffer);
+
+       set_undo_redo_sensitivity ();
+}
+
+void
+cb_redo (void)
+{
+       if (gtk_source_buffer_can_redo (docs.active->source_buffer))
+               gtk_source_buffer_redo (docs.active->source_buffer);
+
+       set_undo_redo_sensitivity ();
+}
+
 gboolean
 cb_delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
 {
@@ -238,6 +259,7 @@ cb_text_changed (GtkWidget *widget, gpointer user_data)
        {
                docs.active->saved = FALSE;
                set_title ();
+               set_undo_redo_sensitivity ();
        }
 }
 
@@ -245,6 +267,7 @@ void
 cb_page_change (GtkNotebook *notebook, GtkNotebookPage *page, guint page_num, gpointer user_data)
 {
        docs.active = g_list_nth_data (docs.all, page_num);
+       set_undo_redo_sensitivity ();
 }
 
 /*****************************
@@ -290,7 +313,9 @@ create_document_in_new_tab (gchar *path, gchar *text, GtkWidget *label)
        gtk_source_view_set_auto_indent (GTK_SOURCE_VIEW (new_doc->source_view), TRUE);
 
        // put the text into the buffer
+       gtk_source_buffer_begin_not_undoable_action (new_doc->source_buffer);
        gtk_text_buffer_set_text (GTK_TEXT_BUFFER (new_doc->source_buffer), text, -1);
+       gtk_source_buffer_end_not_undoable_action (new_doc->source_buffer);
 
        // when the text is modified
        g_signal_connect (G_OBJECT (new_doc->source_buffer), "changed",
@@ -306,6 +331,8 @@ create_document_in_new_tab (gchar *path, gchar *text, GtkWidget *label)
        // add the new document in a new tab
        gint index = gtk_notebook_append_page (docs.notebook, sw, label);
        gtk_notebook_set_current_page (docs.notebook, index);
+
+       set_undo_redo_sensitivity ();
 }
 
 static void
@@ -405,3 +432,19 @@ set_title (void)
                g_free (title);
        }
 }
+
+static void
+set_undo_redo_sensitivity (void)
+{
+       gboolean can_undo = FALSE;
+       gboolean can_redo = FALSE;
+
+       if (docs.active != NULL)
+       {
+               can_undo = gtk_source_buffer_can_undo (docs.active->source_buffer);
+               can_redo = gtk_source_buffer_can_redo (docs.active->source_buffer);
+       }
+
+       gtk_action_set_sensitive (docs.undo, can_undo);
+       gtk_action_set_sensitive (docs.redo, can_redo);
+}
diff --git a/src/callbacks.h b/src/callbacks.h
index b2850a8..600c8ea 100644
--- a/src/callbacks.h
+++ b/src/callbacks.h
@@ -7,6 +7,8 @@ void cb_save (void);
 void cb_save_as (void);
 void cb_close (void);
 void cb_quit (void);
+void cb_undo (void);
+void cb_redo (void);
 void cb_about_dialog (void);
 void cb_text_changed (GtkWidget *widget, gpointer user_data);
 void cb_page_change (GtkNotebook *notebook, GtkNotebookPage *page, guint page_num, gpointer user_data);
diff --git a/src/main.c b/src/main.c
index 8293dbb..4f67124 100644
--- a/src/main.c
+++ b/src/main.c
@@ -63,9 +63,9 @@ main (int argc, char *argv[])
                
                {"Edit", NULL, _("Edit"), NULL, NULL, NULL},
                {"EditUndo", GTK_STOCK_UNDO, _("Undo"), "<Control>Z",
-                       _("Undo the last action"), NULL},
+                       _("Undo the last action"), G_CALLBACK (cb_undo)},
                {"EditRedo", GTK_STOCK_REDO, _("Redo"), "<Shift><Control>Z",
-                       _("Redo the last undone action"), NULL},
+                       _("Redo the last undone action"), G_CALLBACK (cb_redo)},
 
                {"View", NULL, _("View"), NULL, NULL, NULL},
                
@@ -121,6 +121,10 @@ main (int argc, char *argv[])
        gtk_window_add_accel_group (GTK_WINDOW (window), 
                        gtk_ui_manager_get_accel_group (ui_manager));
 
+       // get actions
+       docs.undo = gtk_ui_manager_get_action (ui_manager, "/MainMenu/Edit/Undo");
+       docs.redo = gtk_ui_manager_get_action (ui_manager, "/MainMenu/Edit/Redo");
+
 
        /* vertical pane for the source view and the log zone */
        GtkWidget *vpaned = gtk_vpaned_new ();
diff --git a/src/main.h b/src/main.h
index beb5a41..7f18009 100644
--- a/src/main.h
+++ b/src/main.h
@@ -22,6 +22,8 @@ typedef struct
        GtkWindow *main_window;
        GtkNotebook *notebook;
        GtkSourceLanguageManager *lm;
+       GtkAction *undo;
+       GtkAction *redo;
 } docs_t;
 
 // all the documents are accessible by the docs variable
diff --git a/src/ui.xml b/src/ui.xml
index 37242a4..71047dd 100644
--- a/src/ui.xml
+++ b/src/ui.xml
@@ -16,9 +16,9 @@ In the code, GtkUIManager is used to construct them.
       <menuitem action="FileQuit" />
     </menu>
     
-    <menu action="Edit">
-      <menuitem action="EditUndo" />
-      <menuitem action="EditRedo" />
+    <menu name="Edit" action="Edit">
+      <menuitem name="Undo" action="EditUndo" />
+      <menuitem name="Redo" action="EditRedo" />
     </menu>
 
     <menu action="View">


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]