[Xfce4-commits] <mousepad:master> Remove undo manager code and use GtkSourceView's undo manager.

Matthew Brush noreply at xfce.org
Sat May 5 21:31:37 CEST 2012


Updating branch refs/heads/master
         to 728cb754e087cf0bbc2efa7167a8008b2c66752a (commit)
       from 6be5f6c5f9de3242f234f08506da31e55396d9b5 (commit)

commit 728cb754e087cf0bbc2efa7167a8008b2c66752a
Author: Matthew Brush <mbrush at codebrainz.ca>
Date:   Mon Oct 3 06:54:20 2011 -0700

    Remove undo manager code and use GtkSourceView's undo manager.
    
    There was a call to mousepad_undo_lock() in mousepad-encoding-dialog.c
    that needs to be checked, since I wasn't sure what that call was for.

 mousepad/Makefile.am                |    2 -
 mousepad/mousepad-document.c        |    9 +-
 mousepad/mousepad-document.h        |    4 -
 mousepad/mousepad-encoding-dialog.c |    3 -
 mousepad/mousepad-undo.c            |  835 -----------------------------------
 mousepad/mousepad-undo.h            |   54 ---
 mousepad/mousepad-window.c          |   58 ++--
 7 files changed, 31 insertions(+), 934 deletions(-)

diff --git a/mousepad/Makefile.am b/mousepad/Makefile.am
index 41b7b1e..3dfb8ac 100644
--- a/mousepad/Makefile.am
+++ b/mousepad/Makefile.am
@@ -46,8 +46,6 @@ mousepad_SOURCES = \
 	mousepad-statusbar.h \
 	mousepad-view.c \
 	mousepad-view.h \
-	mousepad-undo.c \
-	mousepad-undo.h \
 	mousepad-util.c \
 	mousepad-util.h \
 	mousepad-window.c \
diff --git a/mousepad/mousepad-document.c b/mousepad/mousepad-document.c
index 2e39044..638f639 100644
--- a/mousepad/mousepad-document.c
+++ b/mousepad/mousepad-document.c
@@ -33,7 +33,6 @@
 #include <mousepad/mousepad-document.h>
 #include <mousepad/mousepad-marshal.h>
 #include <mousepad/mousepad-view.h>
-#include <mousepad/mousepad-undo.h>
 #include <mousepad/mousepad-preferences.h>
 #include <mousepad/mousepad-window.h>
 
@@ -185,7 +184,7 @@ mousepad_document_init (MousepadDocument *document)
   gtk_scrolled_window_set_vadjustment (GTK_SCROLLED_WINDOW (document), NULL);
 
   /* create a textbuffer */
-  document->buffer = gtk_text_buffer_new (NULL);
+  document->buffer = GTK_TEXT_BUFFER (gtk_source_buffer_new (NULL));
 
   /* initialize the file */
   document->file = mousepad_file_new (document->buffer);
@@ -193,9 +192,6 @@ mousepad_document_init (MousepadDocument *document)
   /* connect signals to the file */
   g_signal_connect_swapped (G_OBJECT (document->file), "filename-changed", G_CALLBACK (mousepad_document_filename_changed), document);
 
-  /* initialize the undo manager */
-  document->undo = mousepad_undo_new (document->buffer);
-
   /* create the highlight tag */
   document->tag = gtk_text_buffer_create_tag (document->buffer, NULL, "background", "#ffff78", NULL);
 
@@ -255,9 +251,6 @@ mousepad_document_finalize (GObject *object)
   g_free (document->priv->utf8_filename);
   g_free (document->priv->utf8_basename);
 
-  /* release the undo manager */
-  g_object_unref (G_OBJECT (document->undo));
-
   /* release the file */
   g_object_unref (G_OBJECT (document->file));
 
diff --git a/mousepad/mousepad-document.h b/mousepad/mousepad-document.h
index 2daff7f..f2176c1 100644
--- a/mousepad/mousepad-document.h
+++ b/mousepad/mousepad-document.h
@@ -21,7 +21,6 @@ G_BEGIN_DECLS
 
 #include <mousepad/mousepad-util.h>
 #include <mousepad/mousepad-file.h>
-#include <mousepad/mousepad-undo.h>
 #include <mousepad/mousepad-view.h>
 
 typedef struct _MousepadDocumentPrivate MousepadDocumentPrivate;
@@ -47,9 +46,6 @@ struct _MousepadDocument
   /* file */
   MousepadFile            *file;
 
-  /* undo manager */
-  MousepadUndo            *undo;
-
   /* text buffer */
   GtkTextBuffer           *buffer;
 
diff --git a/mousepad/mousepad-encoding-dialog.c b/mousepad/mousepad-encoding-dialog.c
index 953bfbd..7d4ce76 100644
--- a/mousepad/mousepad-encoding-dialog.c
+++ b/mousepad/mousepad-encoding-dialog.c
@@ -213,9 +213,6 @@ mousepad_encoding_dialog_init (MousepadEncodingDialog *dialog)
   mousepad_view_set_line_numbers (dialog->document->textview, FALSE);
   mousepad_document_set_word_wrap (dialog->document, FALSE);
   gtk_widget_show (GTK_WIDGET (dialog->document));
-
-  /* lock undo manager forever */
-  mousepad_undo_lock (dialog->document->undo);
 }
 
 
diff --git a/mousepad/mousepad-undo.c b/mousepad/mousepad-undo.c
deleted file mode 100644
index ecea5cd..0000000
--- a/mousepad/mousepad-undo.c
+++ /dev/null
@@ -1,835 +0,0 @@
-/*
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 2 of the License, or (at your option)
- * any later version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include <mousepad/mousepad-private.h>
-#include <mousepad/mousepad-undo.h>
-
-
-/* global */
-#define MOUSEPAD_UNDO_MAX_STEPS         (100) /* maximum number of undo steps */
-#define MOUSEPAD_UNDO_BUFFER_SIZE       (40)  /* buffer size */
-
-/* points system */
-#define MOUSEPAD_UNDO_POINTS            (30)  /* maximum points in a step */
-#define MOUSEPAD_UNDO_POINTS_CHAR       (1)   /* points for a character */
-#define MOUSEPAD_UNDO_POINTS_WORD_BREAK (10)  /* points for a word break */
-#define MOUSEPAD_UNDO_POINTS_NEW_LINE   (25)  /* points for a new line */
-
-
-
-typedef struct _MousepadUndoCache  MousepadUndoCache;
-typedef struct _MousepadUndoStep   MousepadUndoStep;
-typedef enum   _MousepadUndoAction MousepadUndoAction;
-
-
-
-static void mousepad_undo_finalize                 (GObject            *object);
-static void mousepad_undo_emit_signals             (MousepadUndo       *undo);
-static void mousepad_undo_step_free                (MousepadUndoStep   *step);
-static void mousepad_undo_step                     (MousepadUndo       *undo,
-                                                    gboolean            redo);
-static void mousepad_undo_cache_reset              (MousepadUndo       *undo);
-static void mousepad_undo_clear_oldest_step        (MousepadUndo       *undo);
-static void mousepad_undo_cache_to_step            (MousepadUndo       *undo);
-static void mousepad_undo_needle_reset             (MousepadUndo       *undo);
-static void mousepad_undo_buffer_changed           (MousepadUndo       *undo,
-                                                    MousepadUndoAction  action,
-                                                    const gchar        *text,
-                                                    gint                length,
-                                                    gint                start_offset,
-                                                    gint                end_offset);
-static void mousepad_undo_buffer_insert            (GtkTextBuffer      *buffer,
-                                                    GtkTextIter        *pos,
-                                                    const gchar        *text,
-                                                    gint                length,
-                                                    MousepadUndo       *undo);
-static void mousepad_undo_buffer_delete            (GtkTextBuffer      *buffer,
-                                                    GtkTextIter        *start_iter,
-                                                    GtkTextIter        *end_iter,
-                                                    MousepadUndo       *undo);
-static void mousepad_undo_buffer_begin_user_action (GtkTextBuffer      *buffer,
-                                                    MousepadUndo       *undo);
-
-
-enum
-{
-  CAN_UNDO,
-  CAN_REDO,
-  LAST_SIGNAL
-};
-
-struct _MousepadUndoClass
-{
-  GObjectClass __parent__;
-};
-
-enum _MousepadUndoAction
-{
-  INSERT, /* insert action */
-  DELETE, /* delete action */
-};
-
-struct _MousepadUndo
-{
-  GObject __parent__;
-
-  /* the text buffer we're monitoring */
-  GtkTextBuffer      *buffer;
-
-  /* whether the undo manager is locked */
-  gint                locked;
-
-  /* whether multiple changes are merged */
-  gint                grouping;
-
-  /* whether we can undo or redo */
-  guint               can_undo : 1;
-  guint               can_redo : 1;
-
-  /* list containing the steps */
-  GList              *steps;
-
-  /* number of steps */
-  gint                n_steps;
-
-  /* steps list pointer when undoing */
-  GList              *needle;
-
-  /* element in the last when saving */
-  GList              *saved;
-
-  /* string holding the deleted characters */
-  GString            *cache;
-
-  /* start and end positions of the cache */
-  gint                cache_start;
-  gint                cache_end;
-
-  /* if the last character in the cache is a space */
-  guint               cache_is_space : 1;
-
-  /* if the changes in the cache are part of a group */
-  guint               cache_in_group : 1;
-
-  /* current action in the cache */
-  MousepadUndoAction  cache_action;
-
-  /* number of points assigned to the cache */
-  gint                cache_points;
-};
-
-struct _MousepadUndoStep
-{
-  /* step action */
-  MousepadUndoAction  action;
-
-  /* deleted string */
-  gchar              *data;
-
-  /* start and end positions */
-  gint                start;
-  gint                end;
-
-  /* whether this step is part of a group */
-  guint               in_group : 1;
-};
-
-
-
-static guint undo_signals[LAST_SIGNAL];
-
-
-
-G_DEFINE_TYPE (MousepadUndo, mousepad_undo, G_TYPE_OBJECT);
-
-
-static void
-mousepad_undo_class_init (MousepadUndoClass *klass)
-{
-  GObjectClass *gobject_class;
-
-  gobject_class = G_OBJECT_CLASS (klass);
-  gobject_class->finalize = mousepad_undo_finalize;
-
-  undo_signals[CAN_UNDO] =
-    g_signal_new (I_("can-undo"),
-                  G_TYPE_FROM_CLASS (gobject_class),
-                  G_SIGNAL_RUN_LAST,
-                  0, NULL, NULL,
-                  g_cclosure_marshal_VOID__BOOLEAN,
-                  G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
-
-  undo_signals[CAN_REDO] =
-    g_signal_new (I_("can-redo"),
-                  G_TYPE_FROM_CLASS (gobject_class),
-                  G_SIGNAL_RUN_LAST,
-                  0, NULL, NULL,
-                  g_cclosure_marshal_VOID__BOOLEAN,
-                  G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
-}
-
-
-
-static void
-mousepad_undo_init (MousepadUndo *undo)
-{
-  /* initialize */
-  undo->locked = 0;
-  undo->grouping = 0;
-  undo->n_steps = 0;
-  undo->can_undo = FALSE;
-  undo->can_redo = FALSE;
-  undo->steps = NULL;
-  undo->needle = NULL;
-  undo->saved = NULL;
-
-  /* initialize the cache */
-  undo->cache = NULL;
-  mousepad_undo_cache_reset (undo);
-}
-
-
-
-static void
-mousepad_undo_finalize (GObject *object)
-{
-  MousepadUndo *undo = MOUSEPAD_UNDO (object);
-
-  /* lock to avoid updates */
-  mousepad_undo_lock (undo);
-
-  /* clear the undo manager */
-  mousepad_undo_clear (undo);
-
-  /* release the undo reference from the buffer */
-  g_object_unref (G_OBJECT (undo->buffer));
-
-  (*G_OBJECT_CLASS (mousepad_undo_parent_class)->finalize) (object);
-}
-
-
-
-static void
-mousepad_undo_emit_signals (MousepadUndo *undo)
-{
-  gboolean can_undo, can_redo;
-
-  /* detect if we can undo or redo */
-  can_undo = (undo->needle != NULL || undo->cache_start != undo->cache_end);
-  can_redo = (undo->needle == NULL || g_list_previous (undo->needle) != NULL);
-
-  /* emit signals if needed */
-  if (undo->can_undo != can_undo)
-    {
-      undo->can_undo = can_undo;
-      g_signal_emit (G_OBJECT (undo), undo_signals[CAN_UNDO], 0, can_undo);
-    }
-
-  if (undo->can_redo != can_redo)
-    {
-      undo->can_redo = can_redo;
-      g_signal_emit (G_OBJECT (undo), undo_signals[CAN_REDO], 0, can_redo);
-    }
-}
-
-
-
-static void
-mousepad_undo_step_free (MousepadUndoStep *step)
-{
-  /* free the string */
-  g_free (step->data);
-
-  /* free the slice */
-  g_slice_free (MousepadUndoStep, step);
-}
-
-
-
-static void
-mousepad_undo_step (MousepadUndo *undo,
-                    gboolean      redo)
-{
-  MousepadUndoStep   *step;
-  MousepadUndoAction  action;
-  GtkTextIter         start_iter, end_iter;
-  GList              *li;
-
-  /* lock */
-  mousepad_undo_lock (undo);
-
-  /* flush the cache */
-  mousepad_undo_cache_to_step (undo);
-
-  /* set the previous element for redoing */
-  if (redo)
-    {
-      if (undo->needle)
-        undo->needle = g_list_previous (undo->needle);
-      else
-        undo->needle = g_list_last (undo->steps);
-    }
-
-  /* freeze buffer notifications */
-  g_object_freeze_notify (G_OBJECT (undo->buffer));
-
-  for (li = undo->needle; li != NULL; li = (redo ? li->prev : li->next))
-    {
-      /* get the step */
-      step = li->data;
-
-      /* get the action */
-      action = step->action;
-
-      /* invert the action if needed */
-      if (redo)
-        action = (action == INSERT ? DELETE : INSERT);
-
-      /* get the start iter */
-      gtk_text_buffer_get_iter_at_offset (undo->buffer, &start_iter, step->start);
-
-      switch (action)
-        {
-          case INSERT:
-            /* debug check */
-            mousepad_return_if_fail (step->data == NULL);
-
-            /* get the end iter */
-            gtk_text_buffer_get_iter_at_offset (undo->buffer, &end_iter, step->end);
-
-            /* set the deleted for redo */
-            step->data = gtk_text_buffer_get_slice (undo->buffer, &start_iter, &end_iter, TRUE);
-
-            /* delete the inserted text */
-            gtk_text_buffer_delete (undo->buffer, &start_iter, &end_iter);
-            break;
-
-          case DELETE:
-            /* debug check */
-            mousepad_return_if_fail (step->data != NULL);
-
-            /* insert the deleted text */
-            gtk_text_buffer_insert (undo->buffer, &start_iter, step->data, -1);
-
-            /* and cleanup */
-            g_free (step->data);
-            step->data = NULL;
-            break;
-
-          default:
-            mousepad_assert_not_reached ();
-            break;
-        }
-
-      /* get the previous item when we redo */
-      if (redo)
-        {
-          if (g_list_previous (li) != NULL)
-            step = g_list_previous (li)->data;
-          else
-            step = NULL;
-        }
-
-      /* break when the step is not part of a group */
-      if (step == NULL || step->in_group == FALSE)
-        break;
-    }
-
-  /* thawn buffer notifications */
-  g_object_thaw_notify (G_OBJECT (undo->buffer));
-
-  /* set the needle */
-  if (redo)
-    undo->needle = li;
-  else
-    undo->needle = g_list_next (li);
-
-  /* check if we've somehow reached the save point */
-  gtk_text_buffer_set_modified (undo->buffer, undo->needle != undo->saved);
-
-  /* emit undo and redo signals */
-  mousepad_undo_emit_signals (undo);
-
-  /* unlock */
-  mousepad_undo_unlock (undo);
-}
-
-
-
-static void
-mousepad_undo_clear_oldest_step (MousepadUndo *undo)
-{
-  GList            *li, *lprev;
-  MousepadUndoStep *step;
-  gint              to_remove;
-
-  mousepad_return_if_fail (undo->n_steps > MOUSEPAD_UNDO_MAX_STEPS);
-
-  /* number of steps to remove */
-  to_remove = undo->n_steps - MOUSEPAD_UNDO_MAX_STEPS;
-
-  /* get end of steps list and remove the entire group */
-  for (li = g_list_last (undo->steps); li != NULL; li = lprev)
-    {
-      step = li->data;
-
-      /* update counters */
-      if (step->in_group == FALSE)
-        {
-          if (to_remove == 0)
-            break;
-
-          /* update counter */
-          to_remove--;
-          undo->n_steps--;
-        }
-
-      /* cleanup */
-      mousepad_undo_step_free (step);
-
-      /* previous step */
-      lprev = li->prev;
-
-      /* remove from list */
-      undo->steps = g_list_delete_link (undo->steps, li);
-    }
-}
-
-
-
-static void
-mousepad_undo_cache_reset (MousepadUndo *undo)
-{
-  mousepad_return_if_fail (undo->cache == NULL);
-
-  /* reset variables */
-  undo->cache_start = undo->cache_end = -1;
-  undo->cache_in_group = FALSE;
-  undo->cache_is_space = FALSE;
-  undo->cache_points = 0;
-}
-
-
-
-static void
-mousepad_undo_cache_to_step (MousepadUndo *undo)
-{
-  MousepadUndoStep *step;
-
-  /* only add when the cache contains changes */
-  if (G_LIKELY (undo->cache_start != undo->cache_end))
-    {
-      /* make sure the needle has been reset */
-      mousepad_return_if_fail (undo->needle == undo->steps);
-
-      /* allocate slice */
-      step = g_slice_new0 (MousepadUndoStep);
-
-      /* set data */
-      step->action = undo->cache_action;
-      step->start = undo->cache_start;
-      step->end = undo->cache_end;
-      step->in_group = undo->cache_in_group;
-
-      /* increase real step counter */
-      if (step->in_group == FALSE)
-        if (++undo->n_steps > MOUSEPAD_UNDO_MAX_STEPS)
-          mousepad_undo_clear_oldest_step (undo);
-
-      if (step->action == DELETE)
-        {
-          /* free cache and set the data */
-          step->data = g_string_free (undo->cache, FALSE);
-
-          /* null the cache */
-          undo->cache = NULL;
-        }
-      else
-        {
-          /* null the data */
-          step->data = NULL;
-        }
-
-      /* prepend the new step */
-      undo->needle = undo->steps = g_list_prepend (undo->steps, step);
-
-      /* reset the cache */
-      mousepad_undo_cache_reset (undo);
-    }
-}
-
-
-
-static void
-mousepad_undo_needle_reset (MousepadUndo *undo)
-{
-  MousepadUndoStep *step;
-
-  /* remove steps from the list until we reach the needle */
-  while (undo->steps != undo->needle)
-    {
-      step = undo->steps->data;
-
-      /* decrease real step counter */
-      if (step->in_group == FALSE)
-        undo->n_steps--;
-
-      /* free the step data */
-      mousepad_undo_step_free (step);
-
-      /* delete the element from the list */
-      undo->steps = g_list_delete_link (undo->steps, undo->steps);
-    }
-
-  /* debug check */
-  mousepad_return_if_fail (undo->needle == undo->steps);
-}
-
-
-
-static void
-mousepad_undo_buffer_changed (MousepadUndo       *undo,
-                              MousepadUndoAction  action,
-                              const gchar        *text,
-                              gint                length,
-                              gint                start_offset,
-                              gint                end_offset)
-{
-  gunichar c;
-  gboolean is_space, is_newline;
-
-  /* when grouping is 0 we going to detect if it's needed to create a
-   * new step (existing data in buffer, points, etc). when grouping is
-   * > 0 it means we're already inside a group and thus merge as much
-   * as possible. */
-  if (undo->grouping == 0)
-    {
-      if (length > 1 || undo->cache_in_group)
-        {
-          /* the buffer contains still data from a grouped step or more then one
-           * character has been changed. in this case we always create a new step */
-          goto create_new_step;
-        }
-      else /* single char changed */
-        {
-          /* get the changed character */
-          c = g_utf8_get_char (text);
-
-          /* if the character is a space */
-          is_space = g_unichar_isspace (c);
-
-          /* when the maximum number of points has been passed and the
-           * last charater in the buffer differs from the new one, we
-           * force a new step */
-          if (undo->cache_points > MOUSEPAD_UNDO_POINTS
-              && undo->cache_is_space != is_space)
-            {
-              goto create_new_step;
-            }
-
-          /* set the new last character type */
-          undo->cache_is_space = is_space;
-
-          /* if the changed character is a new line */
-          is_newline = (c == '\n' || c == '\r');
-
-          /* update the point statistics */
-          if (is_newline)
-            undo->cache_points += MOUSEPAD_UNDO_POINTS_NEW_LINE;
-          else if (is_space)
-            undo->cache_points += MOUSEPAD_UNDO_POINTS_WORD_BREAK;
-          else
-            undo->cache_points += MOUSEPAD_UNDO_POINTS_CHAR;
-        }
-    }
-
-  /* try to merge the new change with the buffer. if this is not possible
-   * new put the cache in a new step and insert the last change in the buffer */
-  if (undo->cache_action == action
-      && action == INSERT
-      && undo->cache_end == start_offset)
-    {
-      /* we can merge with the previous insert */
-      undo->cache_end = end_offset;
-    }
-  else if (undo->cache_action == action
-           && action == DELETE
-           && undo->cache_start == end_offset)
-    {
-      /* we can merge with the previous delete */
-      undo->cache_start = start_offset;
-
-      /* label */
-      prepend_deleted_text:
-
-      /* create a new cache if needed */
-      if (undo->cache == NULL)
-        undo->cache = g_string_sized_new (MOUSEPAD_UNDO_BUFFER_SIZE);
-
-      /* prepend removed characters */
-      undo->cache = g_string_prepend_len (undo->cache, text, length);
-    }
-  else
-    {
-      /* label */
-      create_new_step:
-
-      /* reset the needle of the steps list */
-      mousepad_undo_needle_reset (undo);
-
-      /* put the cache in a new step */
-      mousepad_undo_cache_to_step (undo);
-
-      /* set the new cache variables */
-      undo->cache_start = start_offset;
-      undo->cache_end = end_offset;
-      undo->cache_action = action;
-      undo->cache_is_space = FALSE;
-      undo->cache_in_group = (undo->grouping > 0);
-
-      /* prepend deleted text */
-      if (action == DELETE)
-        goto prepend_deleted_text;
-    }
-
-  /* increase the grouping counter */
-  undo->grouping++;
-
-  /* emit signals */
-  mousepad_undo_emit_signals (undo);
-}
-
-
-
-static void
-mousepad_undo_buffer_insert (GtkTextBuffer *buffer,
-                             GtkTextIter   *pos,
-                             const gchar   *text,
-                             gint           length,
-                             MousepadUndo  *undo)
-{
-  gint start_pos, end_pos;
-
-  mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
-  mousepad_return_if_fail (buffer == undo->buffer);
-
-  /* leave when locked */
-  if (G_LIKELY (undo->locked == 0))
-    {
-      /* buffer positions */
-      start_pos = gtk_text_iter_get_offset (pos);
-      end_pos = start_pos + length;
-
-      /* handle the change */
-      mousepad_undo_buffer_changed (undo, INSERT, text,
-                                    length, start_pos, end_pos);
-    }
-}
-
-
-
-static void
-mousepad_undo_buffer_delete (GtkTextBuffer *buffer,
-                             GtkTextIter   *start_iter,
-                             GtkTextIter   *end_iter,
-                             MousepadUndo  *undo)
-{
-  gchar *text;
-  gint   start_pos, end_pos;
-
-  mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
-  mousepad_return_if_fail (buffer == undo->buffer);
-
-  /* no nothing when locked */
-  if (G_LIKELY (undo->locked == 0))
-    {
-      /* get the removed string */
-      text = gtk_text_buffer_get_slice (buffer, start_iter, end_iter, FALSE);
-
-      /* buffer positions */
-      start_pos = gtk_text_iter_get_offset (start_iter);
-      end_pos = gtk_text_iter_get_offset (end_iter);
-
-      /* handle the change */
-      mousepad_undo_buffer_changed (undo, DELETE, text,
-                                    ABS (start_pos - end_pos),
-                                    start_pos, end_pos);
-
-      /* cleanup */
-      g_free (text);
-    }
-}
-
-
-
-static void
-mousepad_undo_buffer_begin_user_action (GtkTextBuffer *buffer,
-                                        MousepadUndo  *undo)
-{
-  mousepad_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
-  mousepad_return_if_fail (buffer == undo->buffer);
-
-  /* only reset the group counter when not locked */
-  if (G_LIKELY (undo->locked == 0))
-    {
-      /* reset the grouping counter */
-      undo->grouping = 0;
-    }
-}
-
-
-
-MousepadUndo *
-mousepad_undo_new (GtkTextBuffer *buffer)
-{
-  MousepadUndo *undo;
-
-  mousepad_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
-
-  /* create the undo object */
-  undo = g_object_new (MOUSEPAD_TYPE_UNDO, NULL);
-
-  /* set the buffer with a reference */
-  undo->buffer = g_object_ref (G_OBJECT (buffer));
-
-  /* connect signals to the buffer so we can monitor it */
-  g_signal_connect (G_OBJECT (buffer), "insert-text", G_CALLBACK (mousepad_undo_buffer_insert), undo);
-  g_signal_connect (G_OBJECT (buffer), "delete-range", G_CALLBACK (mousepad_undo_buffer_delete), undo);
-  g_signal_connect (G_OBJECT (buffer), "begin-user-action", G_CALLBACK (mousepad_undo_buffer_begin_user_action), undo);
-
-  return undo;
-}
-
-
-
-void
-mousepad_undo_clear (MousepadUndo *undo)
-{
-  GList *li;
-
-  mousepad_return_if_fail (MOUSEPAD_IS_UNDO (undo));
-
-  /* lock to avoid updates */
-  mousepad_undo_lock (undo);
-
-  /* clear cache string */
-  if (G_LIKELY (undo->cache))
-    g_string_free (undo->cache, TRUE);
-
-  /* cleanup the undo steps */
-  for (li = undo->steps; li != NULL; li = li->next)
-    mousepad_undo_step_free (li->data);
-
-  /* free the list */
-  g_list_free (undo->steps);
-
-  /* null */
-  undo->steps = undo->needle = undo->saved = NULL;
-  undo->cache = NULL;
-  undo->n_steps = 0;
-  mousepad_undo_cache_reset (undo);
-
-  /* release lock */
-  mousepad_undo_unlock (undo);
-}
-
-
-
-void
-mousepad_undo_lock (MousepadUndo *undo)
-{
-  mousepad_return_if_fail (MOUSEPAD_IS_UNDO (undo));
-
-  /* increase the lock count */
-  undo->locked++;
-}
-
-
-
-void
-mousepad_undo_unlock (MousepadUndo *undo)
-{
-  mousepad_return_if_fail (MOUSEPAD_IS_UNDO (undo));
-  mousepad_return_if_fail (undo->locked > 0);
-
-  /* decrease the lock count */
-  undo->locked--;
-}
-
-
-
-void
-mousepad_undo_save_point (MousepadUndo *undo)
-{
-  mousepad_return_if_fail (MOUSEPAD_IS_UNDO (undo));
-
-  /* reset the needle */
-  mousepad_undo_needle_reset (undo);
-
-  /* make sure the buffer is flushed */
-  mousepad_undo_cache_to_step (undo);
-
-  /* store the current needle position */
-  undo->saved = undo->needle;
-}
-
-
-
-gboolean
-mousepad_undo_can_undo (MousepadUndo *undo)
-{
-  mousepad_return_val_if_fail (MOUSEPAD_IS_UNDO (undo), FALSE);
-
-  return undo->can_undo;
-}
-
-
-
-gboolean
-mousepad_undo_can_redo (MousepadUndo *undo)
-{
-  mousepad_return_val_if_fail (MOUSEPAD_IS_UNDO (undo), FALSE);
-
-  return undo->can_redo;
-}
-
-
-
-void
-mousepad_undo_do_undo (MousepadUndo *undo)
-{
-  mousepad_return_if_fail (MOUSEPAD_IS_UNDO (undo));
-  mousepad_return_if_fail (mousepad_undo_can_undo (undo));
-
-  /* undo the last step */
-  mousepad_undo_step (undo, FALSE);
-}
-
-
-
-void
-mousepad_undo_do_redo (MousepadUndo *undo)
-{
-  mousepad_return_if_fail (MOUSEPAD_IS_UNDO (undo));
-  mousepad_return_if_fail (mousepad_undo_can_redo (undo));
-
-  /* redo the last undo-ed step */
-  mousepad_undo_step (undo, TRUE);
-}
diff --git a/mousepad/mousepad-undo.h b/mousepad/mousepad-undo.h
deleted file mode 100644
index b096ef5..0000000
--- a/mousepad/mousepad-undo.h
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 2 of the License, or (at your option)
- * any later version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-#ifndef __MOUSEPAD_UNDO_H__
-#define __MOUSEPAD_UNDO_H__
-
-G_BEGIN_DECLS
-
-typedef struct _MousepadUndoClass MousepadUndoClass;
-typedef struct _MousepadUndo      MousepadUndo;
-
-#define MOUSEPAD_TYPE_UNDO            (mousepad_undo_get_type ())
-#define MOUSEPAD_UNDO(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), MOUSEPAD_TYPE_UNDO, MousepadUndo))
-#define MOUSEPAD_UNDO_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), MOUSEPAD_TYPE_UNDO, MousepadUndoClass))
-#define MOUSEPAD_IS_UNDO(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MOUSEPAD_TYPE_UNDO))
-#define MOUSEPAD_IS_UNDO_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MOUSEPAD_TYPE_UNDO))
-#define MOUSEPAD_UNDO_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), MOUSEPAD_TYPE_UNDO, MousepadUndoClass))
-
-GType          mousepad_undo_get_type        (void) G_GNUC_CONST;
-
-MousepadUndo  *mousepad_undo_new             (GtkTextBuffer *buffer);
-
-void           mousepad_undo_clear           (MousepadUndo  *undo);
-
-void           mousepad_undo_lock            (MousepadUndo  *undo);
-
-void           mousepad_undo_unlock          (MousepadUndo  *undo);
-
-void           mousepad_undo_save_point      (MousepadUndo  *undo);
-
-gboolean       mousepad_undo_can_undo        (MousepadUndo  *undo);
-
-gboolean       mousepad_undo_can_redo        (MousepadUndo  *undo);
-
-void           mousepad_undo_do_undo         (MousepadUndo  *undo);
-
-void           mousepad_undo_do_redo         (MousepadUndo  *undo);
-
-G_END_DECLS
-
-#endif /* !__MOUSEPAD_UNDO_H__ */
diff --git a/mousepad/mousepad-window.c b/mousepad/mousepad-window.c
index b84ab6c..2d8b97c 100644
--- a/mousepad/mousepad-window.c
+++ b/mousepad/mousepad-window.c
@@ -148,9 +148,11 @@ static void              mousepad_window_overwrite_changed            (MousepadD
                                                                        gboolean                overwrite,
                                                                        MousepadWindow         *window);
 static void              mousepad_window_can_undo                     (MousepadWindow         *window,
-                                                                       gboolean                can_undo);
+                                                                       GParamSpec             *unused,
+                                                                       GObject                *buffer);
 static void              mousepad_window_can_redo                     (MousepadWindow         *window,
-                                                                       gboolean                can_redo);
+                                                                       GParamSpec             *unused,
+                                                                       GObject                *buffer);
 
 /* menu functions */
 static void              mousepad_window_menu_templates_fill          (MousepadWindow         *window,
@@ -985,13 +987,13 @@ mousepad_window_open_file (MousepadWindow   *window,
   retry:
 
   /* lock the undo manager */
-  mousepad_undo_lock (document->undo);
+  gtk_source_buffer_begin_not_undoable_action (GTK_SOURCE_BUFFER (document->buffer));
 
   /* read the content into the buffer */
   result = mousepad_file_open (document->file, NULL, &error);
 
   /* release the lock */
-  mousepad_undo_unlock (document->undo);
+  gtk_source_buffer_end_not_undoable_action (GTK_SOURCE_BUFFER (document->buffer));
 
   switch (result)
     {
@@ -1353,8 +1355,8 @@ mousepad_window_notebook_added (GtkNotebook     *notebook,
   g_signal_connect (G_OBJECT (page), "selection-changed", G_CALLBACK (mousepad_window_selection_changed), window);
   g_signal_connect (G_OBJECT (page), "overwrite-changed", G_CALLBACK (mousepad_window_overwrite_changed), window);
   g_signal_connect (G_OBJECT (page), "drag-data-received", G_CALLBACK (mousepad_window_drag_data_received), window);
-  g_signal_connect_swapped (G_OBJECT (document->undo), "can-undo", G_CALLBACK (mousepad_window_can_undo), window);
-  g_signal_connect_swapped (G_OBJECT (document->undo), "can-redo", G_CALLBACK (mousepad_window_can_redo), window);
+  g_signal_connect_swapped (G_OBJECT (document->buffer), "notify::can-undo", G_CALLBACK (mousepad_window_can_undo), window);
+  g_signal_connect_swapped (G_OBJECT (document->buffer), "notify::can-redo", G_CALLBACK (mousepad_window_can_redo), window);
   g_signal_connect_swapped (G_OBJECT (document->buffer), "modified-changed", G_CALLBACK (mousepad_window_modified_changed), window);
   g_signal_connect (G_OBJECT (document->textview), "populate-popup", G_CALLBACK (mousepad_window_menu_textview_popup), window);
 
@@ -1393,8 +1395,8 @@ mousepad_window_notebook_removed (GtkNotebook     *notebook,
   g_signal_handlers_disconnect_by_func (G_OBJECT (page), mousepad_window_selection_changed, window);
   g_signal_handlers_disconnect_by_func (G_OBJECT (page), mousepad_window_overwrite_changed, window);
   g_signal_handlers_disconnect_by_func (G_OBJECT (page), mousepad_window_drag_data_received, window);
-  g_signal_handlers_disconnect_by_func (G_OBJECT (document->undo), mousepad_window_can_undo, window);
-  g_signal_handlers_disconnect_by_func (G_OBJECT (document->undo), mousepad_window_can_redo, window);
+  g_signal_handlers_disconnect_by_func (G_OBJECT (document->buffer), mousepad_window_can_undo, window);
+  g_signal_handlers_disconnect_by_func (G_OBJECT (document->buffer), mousepad_window_can_redo, window);
   g_signal_handlers_disconnect_by_func (G_OBJECT (document->buffer), mousepad_window_modified_changed, window);
   g_signal_handlers_disconnect_by_func (G_OBJECT (document->textview), mousepad_window_menu_textview_popup, window);
 
@@ -1663,9 +1665,13 @@ mousepad_window_overwrite_changed (MousepadDocument *document,
 
 static void
 mousepad_window_can_undo (MousepadWindow *window,
-                          gboolean        can_undo)
+                          GParamSpec     *unused,
+                          GObject        *buffer)
 {
   GtkAction *action;
+  gboolean   can_undo;
+  
+  can_undo = gtk_source_buffer_can_undo (GTK_SOURCE_BUFFER (buffer));
 
   action = gtk_action_group_get_action (window->action_group, "undo");
   gtk_action_set_sensitive (action, can_undo);
@@ -1675,9 +1681,13 @@ mousepad_window_can_undo (MousepadWindow *window,
 
 static void
 mousepad_window_can_redo (MousepadWindow *window,
-                          gboolean        can_redo)
+                          GParamSpec     *unused,
+                          GObject        *buffer)
 {
   GtkAction *action;
+  gboolean   can_redo;
+  
+  can_redo = gtk_source_buffer_can_redo (GTK_SOURCE_BUFFER (buffer));
 
   action = gtk_action_group_get_action (window->action_group, "redo");
   gtk_action_set_sensitive (action, can_redo);
@@ -2118,8 +2128,8 @@ mousepad_window_update_actions (MousepadWindow *window)
       gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), active);
 
       /* set the sensitivity of the undo and redo actions */
-      mousepad_window_can_undo (window, mousepad_undo_can_undo (document->undo));
-      mousepad_window_can_redo (window, mousepad_undo_can_redo (document->undo));
+      mousepad_window_can_undo (window, NULL, G_OBJECT (document->buffer));
+      mousepad_window_can_redo (window, NULL, G_OBJECT (document->buffer));
 
       /* active this tab in the go menu */
       action = mousepad_object_get_data (G_OBJECT (document), "navigation-menu-action");
@@ -3095,13 +3105,13 @@ mousepad_window_action_new_from_template (GtkMenuItem    *item,
       g_object_ref_sink (G_OBJECT (document));
 
       /* lock the undo manager */
-      mousepad_undo_lock (document->undo);
+      gtk_source_buffer_begin_not_undoable_action (GTK_SOURCE_BUFFER (document->buffer));
 
       /* try to load the template into the buffer */
       result = mousepad_file_open (document->file, filename, &error);
 
       /* release the lock */
-      mousepad_undo_unlock (document->undo);
+      gtk_source_buffer_end_not_undoable_action (GTK_SOURCE_BUFFER (document->buffer));
 
       /* handle the result */
       if (G_LIKELY (result == 0))
@@ -3375,9 +3385,6 @@ mousepad_window_action_save (GtkAction      *action,
         {
           /* update the window title */
           mousepad_window_set_title (window);
-
-          /* store the save state in the undo manager */
-          mousepad_undo_save_point (document->undo);
         }
       else if (error != NULL)
         {
@@ -3490,10 +3497,8 @@ mousepad_window_action_save_all (GtkAction      *action,
           /* try to quickly save the file */
           succeed = mousepad_file_save (document->file, &error);
 
-          /* store save state, break on problems */
-          if (G_LIKELY (succeed))
-            mousepad_undo_save_point (document->undo);
-          else
+          /* break on problems */
+          if (G_UNLIKELY (!succeed))
             break;
         }
       else
@@ -3590,16 +3595,13 @@ mousepad_window_action_revert (GtkAction      *action,
     }
 
   /* lock the undo manager */
-  mousepad_undo_lock (document->undo);
-
-  /* clear the undo history */
-  mousepad_undo_clear (document->undo);
+  gtk_source_buffer_begin_not_undoable_action (GTK_SOURCE_BUFFER (document->buffer));
 
   /* reload the file */
   succeed = mousepad_file_reload (document->file, &error);
 
   /* release the lock */
-  mousepad_undo_unlock (document->undo);
+  gtk_source_buffer_end_not_undoable_action (GTK_SOURCE_BUFFER (document->buffer));
 
   if (G_UNLIKELY (succeed == FALSE))
     {
@@ -3743,7 +3745,7 @@ mousepad_window_action_undo (GtkAction      *action,
   mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* undo */
-  mousepad_undo_do_undo (window->active->undo);
+  gtk_source_buffer_undo (GTK_SOURCE_BUFFER (window->active->buffer));
 
   /* scroll to visible area */
   mousepad_view_scroll_to_cursor (window->active->textview);
@@ -3759,7 +3761,7 @@ mousepad_window_action_redo (GtkAction      *action,
   mousepad_return_if_fail (MOUSEPAD_IS_DOCUMENT (window->active));
 
   /* redo */
-  mousepad_undo_do_redo (window->active->undo);
+  gtk_source_buffer_redo (GTK_SOURCE_BUFFER (window->active->buffer));
 
   /* scroll to visible area */
   mousepad_view_scroll_to_cursor (window->active->textview);


More information about the Xfce4-commits mailing list