[gtk/wip/otte/undo: 3/17] entry: Add an undo action



commit 19a9c6ab60800e8d5f4330b6cabeae220c8bbaef
Author: Benjamin Otte <otte redhat com>
Date:   Thu Aug 6 02:08:49 2015 +0200

    entry: Add an undo action
    
    Bind it to Ctrl-z for undo and Ctrl-Shift for redo, mirroring what gedit
    does.

 gtk/gtkentry.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gtk/gtkentry.h |  3 ++-
 2 files changed, 58 insertions(+), 1 deletion(-)
---
diff --git a/gtk/gtkentry.c b/gtk/gtkentry.c
index 8c43b8ae07..25fda154c9 100644
--- a/gtk/gtkentry.c
+++ b/gtk/gtkentry.c
@@ -279,6 +279,7 @@ enum {
   COPY_CLIPBOARD,
   PASTE_CLIPBOARD,
   TOGGLE_OVERWRITE,
+  UNDO,
   ICON_PRESS,
   ICON_RELEASE,
   PREEDIT_CHANGED,
@@ -508,6 +509,8 @@ static void gtk_entry_cut_clipboard      (GtkEntry        *entry);
 static void gtk_entry_copy_clipboard     (GtkEntry        *entry);
 static void gtk_entry_paste_clipboard    (GtkEntry        *entry);
 static void gtk_entry_toggle_overwrite   (GtkEntry        *entry);
+static void gtk_entry_undo               (GtkEntry        *entry,
+                                          gint             count);
 static void gtk_entry_select_all         (GtkEntry        *entry);
 static void gtk_entry_real_activate      (GtkEntry        *entry);
 static gboolean gtk_entry_popup_menu     (GtkWidget       *widget);
@@ -758,6 +761,7 @@ gtk_entry_class_init (GtkEntryClass *class)
   class->activate = gtk_entry_real_activate;
   class->get_text_area_size = gtk_entry_get_text_area_size;
   class->get_frame_size = gtk_entry_get_frame_size;
+  class->undo = gtk_entry_undo;
   
   quark_inner_border = g_quark_from_static_string ("gtk-entry-inner-border");
   quark_password_hint = g_quark_from_static_string ("gtk-entry-password-hint");
@@ -1806,6 +1810,27 @@ gtk_entry_class_init (GtkEntryClass *class)
                  _gtk_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
 
+  /**
+   * GtkEntry::undo:
+   * @entry: the object which received the signal
+   * @count: the number of steps to undo (or redo if @count is negative)
+   *
+   * The ::undo signal is a [keybinding signal][GtkBindingSignal]
+   * which gets emitted to undo or redo user input.
+   *
+   * The default bindings for this signal are Ctrl-z for undo and
+   * Ctrl-Shift-z for redo.
+   */
+  signals[UNDO] =
+    g_signal_new (I_("undo"),
+                 G_OBJECT_CLASS_TYPE (gobject_class),
+                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
+                 G_STRUCT_OFFSET (GtkEntryClass, undo),
+                 NULL, NULL,
+                 _gtk_marshal_VOID__INT,
+                 G_TYPE_NONE, 1,
+                 G_TYPE_INT);
+
   /**
    * GtkEntry::icon-press:
    * @entry: The entry on which the signal is emitted
@@ -2043,6 +2068,15 @@ gtk_entry_class_init (GtkEntryClass *class)
   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Insert, 0,
                                "toggle-overwrite", 0);
 
+  /* Undo */
+  gtk_binding_entry_add_signal (binding_set, GDK_KEY_z, GDK_CONTROL_MASK,
+                               "undo", 1,
+                               G_TYPE_INT, 1);
+  gtk_binding_entry_add_signal (binding_set, GDK_KEY_z, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
+                               "undo", 1,
+                               G_TYPE_INT, -1);
+
+
   /**
    * GtkEntry:inner-border:
    *
@@ -5996,6 +6030,28 @@ gtk_entry_toggle_overwrite (GtkEntry *entry)
   gtk_widget_queue_draw (GTK_WIDGET (entry));
 }
 
+static void
+gtk_entry_undo (GtkEntry *entry,
+                gint      count)
+{
+  GtkEntryPrivate *priv = entry->priv;
+  int i;
+
+  if (priv->editable)
+    {
+      for (i = 0; i < count; i++)
+        {
+          if (!gtk_undo_stack_undo (priv->undo_stack))
+            break;
+        }
+      for (i = 0; i > count; i--)
+        {
+          if (!gtk_undo_stack_redo (priv->undo_stack))
+            break;
+        }
+    }
+}
+
 static void
 gtk_entry_select_all (GtkEntry *entry)
 {
diff --git a/gtk/gtkentry.h b/gtk/gtkentry.h
index 98c5a67e7f..bd0f1b1bff 100644
--- a/gtk/gtkentry.h
+++ b/gtk/gtkentry.h
@@ -159,10 +159,11 @@ struct _GtkEntryClass
                               gint           *width,
                               gint           *height);
 
+  void (* undo)               (GtkEntry             *entry,
+                              gint                  count);
   /*< private >*/
 
   /* Padding for future expansion */
-  void (*_gtk_reserved1)      (void);
   void (*_gtk_reserved2)      (void);
   void (*_gtk_reserved3)      (void);
   void (*_gtk_reserved4)      (void);


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