[gimp] app: make undo compression in the text tool work again



commit 9a9d7489d1896ceb0d0ae57794f1c7919e7520d2
Author: Michael Natterer <mitch gimp org>
Date:   Tue Mar 2 21:54:24 2010 +0100

    app: make undo compression in the text tool work again
    
    - in GimpText, make "text" and "markup" mutually exclusive, so that
      whenever one is set to non-NULL, the other is cleared automatically.
    - add gimp_text_buffer_has_markup() which returns TRUE if any char
      in the buffer is tagged.
    - in the text tool, only set "markup" on the text proxy if there is
      actually markup in the buffer, and set "text" otherwise.
    
    This way we don't push "text" *and* "markup" undos on each keystroke,
    and undo compression works the way it did before.

 app/text/gimptext.c          |   12 ++++++++
 app/tools/gimptexttool.c     |   59 +++++++++++++++++++++++++++--------------
 app/widgets/gimptextbuffer.c |   24 +++++++++++++++++
 app/widgets/gimptextbuffer.h |    2 +
 4 files changed, 77 insertions(+), 20 deletions(-)
---
diff --git a/app/text/gimptext.c b/app/text/gimptext.c
index 117ed94..1666030 100644
--- a/app/text/gimptext.c
+++ b/app/text/gimptext.c
@@ -399,10 +399,22 @@ gimp_text_set_property (GObject      *object,
     case PROP_TEXT:
       g_free (text->text);
       text->text = g_value_dup_string (value);
+      if (text->text && text->markup)
+        {
+          g_free (text->markup);
+          text->markup = NULL;
+          g_object_notify (object, "markup");
+        }
       break;
     case PROP_MARKUP:
       g_free (text->markup);
       text->markup = g_value_dup_string (value);
+      if (text->markup && text->text)
+        {
+          g_free (text->text);
+          text->text = NULL;
+          g_object_notify (object, "text");
+        }
       break;
     case PROP_FONT:
       {
diff --git a/app/tools/gimptexttool.c b/app/tools/gimptexttool.c
index f90fdc2..6aa1231 100644
--- a/app/tools/gimptexttool.c
+++ b/app/tools/gimptexttool.c
@@ -164,7 +164,7 @@ static gboolean  gimp_text_tool_set_drawable    (GimpTextTool      *text_tool,
                                                  GimpDrawable      *drawable,
                                                  gboolean           confirm);
 
-static void      gimp_text_tool_buffer_edited   (GtkTextBuffer     *buffer,
+static void      gimp_text_tool_buffer_edited   (GimpTextBuffer    *buffer,
                                                  GimpTextTool      *text_tool);
 
 
@@ -1272,17 +1272,28 @@ gimp_text_tool_create_layer (GimpTextTool *text_tool,
     }
   else
     {
-      gchar *string = gimp_text_buffer_get_text (text_tool->buffer);
-      gchar *markup = gimp_text_buffer_get_markup (text_tool->buffer);
+      gchar *string;
 
-      g_object_set (text_tool->proxy,
-                    "text",     string,
-                    "markup",   markup,
-                    "box-mode", GIMP_TEXT_BOX_DYNAMIC,
-                    NULL);
+      if (gimp_text_buffer_has_markup (text_tool->buffer))
+        {
+          string = gimp_text_buffer_get_markup (text_tool->buffer);
+
+          g_object_set (text_tool->proxy,
+                        "markup",   string,
+                        "box-mode", GIMP_TEXT_BOX_DYNAMIC,
+                        NULL);
+        }
+      else
+        {
+          string = gimp_text_buffer_get_text (text_tool->buffer);
+
+          g_object_set (text_tool->proxy,
+                        "text",     string,
+                        "box-mode", GIMP_TEXT_BOX_DYNAMIC,
+                        NULL);
+        }
 
       g_free (string);
-      g_free (markup);
 
       text = gimp_config_duplicate (GIMP_CONFIG (text_tool->proxy));
     }
@@ -1577,23 +1588,31 @@ gimp_text_tool_set_drawable (GimpTextTool *text_tool,
 }
 
 static void
-gimp_text_tool_buffer_edited (GtkTextBuffer *buffer,
-                              GimpTextTool  *text_tool)
+gimp_text_tool_buffer_edited (GimpTextBuffer *buffer,
+                              GimpTextTool   *text_tool)
 {
   if (text_tool->text)
     {
-      gchar *text   = gimp_text_buffer_get_text (GIMP_TEXT_BUFFER (buffer));
-      gchar *markup = gimp_text_buffer_get_markup (GIMP_TEXT_BUFFER (buffer));
+      gchar *string;
 
-      g_object_set (text_tool->proxy,
-                    "text",   text,
-                    "markup", markup,
-                    NULL);
+      if (gimp_text_buffer_has_markup (buffer))
+        {
+          string = gimp_text_buffer_get_markup (buffer);
+
+          g_object_set (text_tool->proxy,
+                        "markup", string,
+                        NULL);
+        }
+      else
+        {
+          string = gimp_text_buffer_get_text (buffer);
 
-      /* g_printerr ("markup: %s\n", markup); */
+          g_object_set (text_tool->proxy,
+                        "text", string,
+                        NULL);
+        }
 
-      g_free (text);
-      g_free (markup);
+      g_free (string);
     }
   else
     {
diff --git a/app/widgets/gimptextbuffer.c b/app/widgets/gimptextbuffer.c
index be0a14f..cefb480 100644
--- a/app/widgets/gimptextbuffer.c
+++ b/app/widgets/gimptextbuffer.c
@@ -303,6 +303,30 @@ gimp_text_buffer_get_markup (GimpTextBuffer *buffer)
   return markup;
 }
 
+gboolean
+gimp_text_buffer_has_markup (GimpTextBuffer *buffer)
+{
+  GtkTextIter iter;
+
+  g_return_val_if_fail (GIMP_IS_TEXT_BUFFER (buffer), FALSE);
+
+  gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (buffer), &iter);
+
+  do
+    {
+      GSList *tags = gtk_text_iter_get_tags (&iter);
+
+      if (tags)
+        {
+          g_slist_free (tags);
+          return TRUE;
+        }
+    }
+  while (gtk_text_iter_forward_char (&iter));
+
+  return FALSE;
+}
+
 GtkTextTag *
 gimp_text_buffer_get_iter_baseline (GimpTextBuffer    *buffer,
                                     const GtkTextIter *iter,
diff --git a/app/widgets/gimptextbuffer.h b/app/widgets/gimptextbuffer.h
index c35a170..34db1d4 100644
--- a/app/widgets/gimptextbuffer.h
+++ b/app/widgets/gimptextbuffer.h
@@ -69,6 +69,8 @@ void             gimp_text_buffer_set_markup        (GimpTextBuffer    *buffer,
                                                      const gchar       *markup);
 gchar          * gimp_text_buffer_get_markup        (GimpTextBuffer    *buffer);
 
+gboolean         gimp_text_buffer_has_markup        (GimpTextBuffer    *buffer);
+
 GtkTextTag     * gimp_text_buffer_get_iter_baseline (GimpTextBuffer    *buffer,
                                                      const GtkTextIter *iter,
                                                      gint              *baseline);



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