[gtksourceview/wip/undo-redo: 2/3] UndoManager: update_can_undo_can_redo() utility function



commit 076256d7c33d61b936c41aa377a4ecd6c15c06cf
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Aug 22 18:42:29 2014 +0200

    UndoManager: update_can_undo_can_redo() utility function

 gtksourceview/gtksourceundomanagerdefault.c |   83 ++++++++-------------------
 1 files changed, 25 insertions(+), 58 deletions(-)
---
diff --git a/gtksourceview/gtksourceundomanagerdefault.c b/gtksourceview/gtksourceundomanagerdefault.c
index 673a310..90d3f31 100644
--- a/gtksourceview/gtksourceundomanagerdefault.c
+++ b/gtksourceview/gtksourceundomanagerdefault.c
@@ -158,6 +158,26 @@ G_DEFINE_TYPE_WITH_CODE (GtkSourceUndoManagerDefault, gtk_source_undo_manager_de
                                                 gtk_source_undo_manager_iface_init))
 
 static void
+update_can_undo_can_redo (GtkSourceUndoManagerDefault *manager)
+{
+       guint len = manager->priv->actions->len;
+       gboolean can_undo = len > 0 && manager->priv->next_redo < len - 1;
+       gboolean can_redo = manager->priv->next_redo >= 0;
+
+       if (manager->priv->can_undo != can_undo)
+       {
+               manager->priv->can_undo = can_undo;
+               gtk_source_undo_manager_can_undo_changed (GTK_SOURCE_UNDO_MANAGER (manager));
+       }
+
+       if (manager->priv->can_redo != can_redo)
+       {
+               manager->priv->can_redo = can_redo;
+               gtk_source_undo_manager_can_redo_changed (GTK_SOURCE_UNDO_MANAGER (manager));
+       }
+}
+
+static void
 gtk_source_undo_action_free (GtkSourceUndoAction *action)
 {
        if (action == NULL)
@@ -476,18 +496,7 @@ add_action (GtkSourceUndoManagerDefault *um,
        }
 
        check_list_size (um);
-
-       if (!um->priv->can_undo)
-       {
-               um->priv->can_undo = TRUE;
-               gtk_source_undo_manager_can_undo_changed (GTK_SOURCE_UNDO_MANAGER (um));
-       }
-
-       if (um->priv->can_redo)
-       {
-               um->priv->can_redo = FALSE;
-               gtk_source_undo_manager_can_redo_changed (GTK_SOURCE_UNDO_MANAGER (um));
-       }
+       update_can_undo_can_redo (um);
 }
 
 static void
@@ -497,17 +506,7 @@ clear_undo (GtkSourceUndoManagerDefault *manager)
 
        manager->priv->next_redo = -1;
 
-       if (manager->priv->can_undo)
-       {
-               manager->priv->can_undo = FALSE;
-               gtk_source_undo_manager_can_undo_changed (GTK_SOURCE_UNDO_MANAGER (manager));
-       }
-
-       if (manager->priv->can_redo)
-       {
-               manager->priv->can_redo = FALSE;
-               gtk_source_undo_manager_can_redo_changed (GTK_SOURCE_UNDO_MANAGER (manager));
-       }
+       update_can_undo_can_redo (manager);
 }
 
 static void
@@ -535,19 +534,7 @@ set_max_undo_levels (GtkSourceUndoManagerDefault *manager,
                /* now remove undo actions if necessary */
                check_list_size (manager);
 
-               /* emit "can_undo" and/or "can_redo" if appropiate */
-               if (manager->priv->next_redo < 0 && manager->priv->can_redo)
-               {
-                       manager->priv->can_redo = FALSE;
-                       gtk_source_undo_manager_can_redo_changed (GTK_SOURCE_UNDO_MANAGER (manager));
-               }
-
-               if (manager->priv->can_undo &&
-                   manager->priv->next_redo >= (gint)manager->priv->actions->len - 1)
-               {
-                       manager->priv->can_undo = FALSE;
-                       gtk_source_undo_manager_can_undo_changed (GTK_SOURCE_UNDO_MANAGER (manager));
-               }
+               update_can_undo_can_redo (manager);
        }
 }
 
@@ -806,17 +793,7 @@ gtk_source_undo_manager_undo_impl (GtkSourceUndoManager *manager)
 
        manager_default->priv->modified_undoing_group = FALSE;
 
-       if (!manager_default->priv->can_redo)
-       {
-               manager_default->priv->can_redo = TRUE;
-               gtk_source_undo_manager_can_redo_changed (manager);
-       }
-
-       if (manager_default->priv->next_redo >= (gint)manager_default->priv->actions->len - 1)
-       {
-               manager_default->priv->can_undo = FALSE;
-               gtk_source_undo_manager_can_undo_changed (manager);
-       }
+       update_can_undo_can_redo (manager_default);
 }
 
 static void
@@ -908,17 +885,7 @@ gtk_source_undo_manager_redo_impl (GtkSourceUndoManager *manager)
        /* FIXME: why is this only internal ?*/
        end_not_undoable_action_internal (manager_default);
 
-       if (manager_default->priv->next_redo < 0)
-       {
-               manager_default->priv->can_redo = FALSE;
-               gtk_source_undo_manager_can_redo_changed (manager);
-       }
-
-       if (!manager_default->priv->can_undo)
-       {
-               manager_default->priv->can_undo = TRUE;
-               gtk_source_undo_manager_can_undo_changed (manager);
-       }
+       update_can_undo_can_redo (manager_default);
 }
 
 static void


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