[gtksourceview/wip/undo-redo-restore-selection] UndoManager: restore selection (not finished)



commit df2273ec19d2aab0c77e4cdee56659bb2c1f4103
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Oct 11 15:10:46 2014 +0200

    UndoManager: restore selection (not finished)

 gtksourceview/gtksourceundomanagerdefault.c |   64 ++++++++++++++++++++++++++-
 1 files changed, 63 insertions(+), 1 deletions(-)
---
diff --git a/gtksourceview/gtksourceundomanagerdefault.c b/gtksourceview/gtksourceundomanagerdefault.c
index 6d61fbf..0710f44 100644
--- a/gtksourceview/gtksourceundomanagerdefault.c
+++ b/gtksourceview/gtksourceundomanagerdefault.c
@@ -58,6 +58,16 @@ struct _Action
         */
        gchar *text;
 
+       /* Character offsets of the insert and selection bound marks.
+        * -1 if it doesn't match @start or @end. If the text cursor or the
+        * selected text is not related to the action, the selection is not
+        * stored.
+        * If not -1, when undoing or redoing an action, the insert and
+        * selection bound marks are restored to where they were.
+        */
+       gint selection_insert;
+       gint selection_bound;
+
        /* Used only for a deletion. If forward is TRUE, the Delete key was
         * probably used. If forward is FALSE, the Backspace key was probably
         * used.
@@ -162,7 +172,14 @@ G_DEFINE_TYPE_WITH_CODE (GtkSourceUndoManagerDefault,
 static Action *
 action_new (void)
 {
-       return g_slice_new0 (Action);
+       Action *action;
+
+       action = g_slice_new0 (Action);
+
+       action->selection_insert = -1;
+       action->selection_bound = -1;
+
+       return action;
 }
 
 static void
@@ -923,6 +940,25 @@ action_set_cursor_position (GtkTextBuffer *buffer,
 /* Buffer signal handlers */
 
 static void
+set_selection_bounds (GtkTextBuffer *buffer,
+                     Action        *action)
+{
+       GtkTextMark *insert_mark;
+       GtkTextMark *selection_mark;
+       GtkTextIter insert_iter;
+       GtkTextIter selection_iter;
+
+       insert_mark = gtk_text_buffer_get_insert (buffer);
+       selection_mark = gtk_text_buffer_get_selection_bound (buffer);
+
+       gtk_text_buffer_get_iter_at_mark (buffer, &insert_iter, insert_mark);
+       gtk_text_buffer_get_iter_at_mark (buffer, &selection_iter, selection_mark);
+
+       action->selection_insert = gtk_text_iter_get_offset (&insert_iter);
+       action->selection_bound = gtk_text_iter_get_offset (&selection_iter);
+}
+
+static void
 insert_text_cb (GtkTextBuffer               *buffer,
                GtkTextIter                 *location,
                const gchar                 *text,
@@ -936,6 +972,21 @@ insert_text_cb (GtkTextBuffer               *buffer,
        action->text = g_strndup (text, length);
        action->end = action->start + g_utf8_strlen (action->text, -1);
 
+       set_selection_bounds (buffer, action);
+
+       if (action->selection_insert != action->selection_bound ||
+           action->selection_insert != action->start)
+       {
+               action->selection_insert = -1;
+               action->selection_bound = -1;
+       }
+       else
+       {
+               /* The insertion occurred at the cursor. */
+               g_assert_cmpint (action->selection_insert, ==, action->start);
+               g_assert_cmpint (action->selection_bound, ==, action->start);
+       }
+
        insert_action (manager, action);
 }
 
@@ -960,6 +1011,17 @@ delete_range_cb (GtkTextBuffer               *buffer,
 
        action->forward = gtk_text_iter_equal (&cursor_pos, start);
 
+       set_selection_bounds (buffer, action);
+
+       if ((action->selection_insert != action->start &&
+            action->selection_insert != action->end) ||
+           (action->selection_bound != action->start &&
+            action->selection_bound != action->end))
+       {
+               action->selection_insert = -1;
+               action->selection_bound = -1;
+       }
+
        insert_action (manager, action);
 }
 


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