[gimp] app: add gimp_item_replace_item() which is a bad hack and documented as such



commit 0428be29449a84a38f0782d3214126f184830d89
Author: Michael Natterer <mitch gimp org>
Date:   Sun Jan 30 19:26:32 2011 +0100

    app: add gimp_item_replace_item() which is a bad hack and documented as such
    
    in order to get rid of fiddling with GimpItem internals inside
    gimptextlayer-xcf.c

 app/core/gimpitem.c          |   72 ++++++++++++++++++++++++++++++++++++++---
 app/core/gimpitem.h          |    3 ++
 app/text/gimptextlayer-xcf.c |   36 +--------------------
 3 files changed, 71 insertions(+), 40 deletions(-)
---
diff --git a/app/core/gimpitem.c b/app/core/gimpitem.c
index 35c6c49..407e124 100644
--- a/app/core/gimpitem.c
+++ b/app/core/gimpitem.c
@@ -1520,13 +1520,10 @@ gimp_item_set_image (GimpItem  *item,
 {
   g_return_if_fail (GIMP_IS_ITEM (item));
   g_return_if_fail (! gimp_item_is_attached (item));
-  g_return_if_fail (image == NULL || GIMP_IS_IMAGE (image));
+  g_return_if_fail (! gimp_item_is_removed (item));
+  g_return_if_fail (GIMP_IS_IMAGE (image));
 
-  if (image == NULL)
-    {
-      item->tattoo = 0;
-    }
-  else if (item->tattoo == 0 || item->image != image)
+  if (item->tattoo == 0 || item->image != image)
     {
       item->tattoo = gimp_image_get_new_tattoo (image);
     }
@@ -1534,6 +1531,69 @@ gimp_item_set_image (GimpItem  *item,
   item->image = image;
 }
 
+/**
+ * gimp_item_replace_item:
+ * @item: a newly allocated #GimpItem
+ * @replace: the #GimpItem to be replaced by @item
+ *
+ * This function shouly only be called right after @item has been
+ * newly allocated.
+ *
+ * Replaces @replace by @item, as far as possible within the #GimpItem
+ * class. The new @item takes over @replace's ID, tattoo, offset, size
+ * etc. and all these properties are set to %NULL on @replace.
+ *
+ * This function *only* exists to allow subclasses to do evil hacks
+ * like in XCF text layer loading. Don't ever use this function if you
+ * are not sure.
+ *
+ * After this function returns, @replace has become completely
+ * unusable, should only be used to steal everything it has (like its
+ * drawable properties if it's a drawable), and then be destroyed.
+ **/
+void
+gimp_item_replace_item (GimpItem *item,
+                        GimpItem *replace)
+{
+  gint offset_x;
+  gint offset_y;
+
+  g_return_if_fail (GIMP_IS_ITEM (item));
+  g_return_if_fail (! gimp_item_is_attached (item));
+  g_return_if_fail (! gimp_item_is_removed (item));
+  g_return_if_fail (GIMP_IS_ITEM (replace));
+
+  gimp_object_set_name (GIMP_OBJECT (item), gimp_object_get_name (replace));
+
+  item->ID = gimp_item_get_ID (replace);
+  g_hash_table_replace (gimp_item_get_image (item)->gimp->item_table,
+                        GINT_TO_POINTER (gimp_item_get_ID (item)),
+                        item);
+
+  /* Set image before tatoo so that the explicitly set tatoo overrides
+   * the one implicitly set when setting the image
+   */
+  gimp_item_set_image (item, gimp_item_get_image (replace));
+  replace->image  = NULL;
+
+  gimp_item_set_tattoo (item, gimp_item_get_tattoo (replace));
+  gimp_item_set_tattoo (replace, 0);
+
+  item->parasites = replace->parasites;
+  replace->parasites = NULL;
+
+  gimp_item_get_offset (replace, &offset_x, &offset_y);
+  gimp_item_set_offset (item, offset_x, offset_y);
+
+  gimp_item_set_size (item,
+                      gimp_item_get_width  (replace),
+                      gimp_item_get_height (replace));
+
+  gimp_item_set_visible      (item, gimp_item_get_visible (replace), FALSE);
+  gimp_item_set_linked       (item, gimp_item_get_linked (replace), FALSE);
+  gimp_item_set_lock_content (item, gimp_item_get_lock_content (replace), FALSE);
+}
+
 void
 gimp_item_parasite_attach (GimpItem           *item,
                            const GimpParasite *parasite)
diff --git a/app/core/gimpitem.h b/app/core/gimpitem.h
index a60ae7c..091c9a6 100644
--- a/app/core/gimpitem.h
+++ b/app/core/gimpitem.h
@@ -286,6 +286,9 @@ GimpImage     * gimp_item_get_image          (const GimpItem     *item);
 void            gimp_item_set_image          (GimpItem           *item,
                                               GimpImage          *image);
 
+void            gimp_item_replace_item       (GimpItem           *item,
+                                              GimpItem           *replace);
+
 void            gimp_item_parasite_attach    (GimpItem           *item,
                                               const GimpParasite *parasite);
 void            gimp_item_parasite_detach    (GimpItem           *item,
diff --git a/app/text/gimptextlayer-xcf.c b/app/text/gimptextlayer-xcf.c
index 77c6ca7..c4b4c56 100644
--- a/app/text/gimptextlayer-xcf.c
+++ b/app/text/gimptextlayer-xcf.c
@@ -175,48 +175,16 @@ gimp_text_layer_from_layer (GimpLayer *layer,
                             GimpText  *text)
 {
   GimpTextLayer *text_layer;
-  GimpItem      *item;
   GimpDrawable  *drawable;
-  gint           offset_x;
-  gint           offset_y;
 
   g_return_val_if_fail (GIMP_IS_LAYER (layer), NULL);
   g_return_val_if_fail (GIMP_IS_TEXT (text), NULL);
 
   text_layer = g_object_new (GIMP_TYPE_TEXT_LAYER, NULL);
 
-  item     = GIMP_ITEM (text_layer);
-  drawable = GIMP_DRAWABLE (text_layer);
-
-  gimp_object_set_name (GIMP_OBJECT (text_layer),
-                        gimp_object_get_name (layer));
-
-  item->ID = gimp_item_get_ID (GIMP_ITEM (layer));
-
-  /* Set image before tatoo so that the explicitly set tatoo overrides
-   * the one implicitly set when setting the image
-   */
-  gimp_item_set_image (item, gimp_item_get_image (GIMP_ITEM (layer)));
-  gimp_item_set_tattoo (item, gimp_item_get_tattoo (GIMP_ITEM (layer)));
+  gimp_item_replace_item (GIMP_ITEM (text_layer), GIMP_ITEM (layer));
 
-  gimp_item_set_image (GIMP_ITEM (layer), NULL);
-  g_hash_table_replace (gimp_item_get_image (item)->gimp->item_table,
-                        GINT_TO_POINTER (gimp_item_get_ID (item)),
-                        item);
-
-  item->parasites = GIMP_ITEM (layer)->parasites;
-  GIMP_ITEM (layer)->parasites = NULL;
-
-  gimp_item_get_offset (GIMP_ITEM (layer), &offset_x, &offset_y);
-  gimp_item_set_offset (item, offset_x, offset_y);
-
-  gimp_item_set_size (item,
-                      gimp_item_get_width  (GIMP_ITEM (layer)),
-                      gimp_item_get_height (GIMP_ITEM (layer)));
-
-  gimp_item_set_visible      (item, gimp_item_get_visible (GIMP_ITEM (layer)), FALSE);
-  gimp_item_set_linked       (item, gimp_item_get_linked (GIMP_ITEM (layer)), FALSE);
-  gimp_item_set_lock_content (item, gimp_item_get_lock_content (GIMP_ITEM (layer)), FALSE);
+  drawable = GIMP_DRAWABLE (text_layer);
 
   drawable->private->tiles = gimp_drawable_get_tiles (GIMP_DRAWABLE (layer));
   GIMP_DRAWABLE (layer)->private->tiles = NULL;



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