2010-02-06 19:58:10 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
|
|
|
* Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* gimpitemtree.c
|
|
|
|
* Copyright (C) 2010 Michael Natterer <mitch@gimp.org>
|
|
|
|
*
|
|
|
|
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2010-02-07 23:23:02 +08:00
|
|
|
#include <stdlib.h>
|
2010-02-06 19:58:10 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <gegl.h>
|
|
|
|
|
|
|
|
#include "core-types.h"
|
|
|
|
|
2010-02-06 20:49:09 +08:00
|
|
|
#include "gimpimage.h"
|
2010-02-06 23:41:54 +08:00
|
|
|
#include "gimpimage-undo-push.h"
|
2010-02-06 19:58:10 +08:00
|
|
|
#include "gimpitem.h"
|
|
|
|
#include "gimpitemstack.h"
|
|
|
|
#include "gimpitemtree.h"
|
|
|
|
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
2010-02-06 20:49:09 +08:00
|
|
|
PROP_IMAGE,
|
2010-02-06 19:58:10 +08:00
|
|
|
PROP_CONTAINER_TYPE,
|
2010-02-07 21:52:34 +08:00
|
|
|
PROP_ITEM_TYPE,
|
|
|
|
PROP_ACTIVE_ITEM
|
2010-02-06 19:58:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct _GimpItemTreePrivate GimpItemTreePrivate;
|
|
|
|
|
|
|
|
struct _GimpItemTreePrivate
|
|
|
|
{
|
2010-02-07 23:23:02 +08:00
|
|
|
GimpImage *image;
|
2010-02-07 21:52:34 +08:00
|
|
|
|
2010-02-07 23:23:02 +08:00
|
|
|
GType container_type;
|
|
|
|
GType item_type;
|
2010-02-07 21:52:34 +08:00
|
|
|
|
2010-02-07 23:23:02 +08:00
|
|
|
GimpItem *active_item;
|
|
|
|
|
|
|
|
GHashTable *name_hash;
|
2010-02-06 19:58:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define GIMP_ITEM_TREE_GET_PRIVATE(object) \
|
|
|
|
G_TYPE_INSTANCE_GET_PRIVATE (object, \
|
|
|
|
GIMP_TYPE_ITEM_TREE, \
|
|
|
|
GimpItemTreePrivate)
|
|
|
|
|
|
|
|
|
|
|
|
/* local function prototypes */
|
|
|
|
|
2010-02-07 23:23:02 +08:00
|
|
|
static GObject * gimp_item_tree_constructor (GType type,
|
|
|
|
guint n_params,
|
|
|
|
GObjectConstructParam *params);
|
|
|
|
static void gimp_item_tree_finalize (GObject *object);
|
|
|
|
static void gimp_item_tree_set_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec);
|
|
|
|
static void gimp_item_tree_get_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec);
|
|
|
|
|
|
|
|
static gint64 gimp_item_tree_get_memsize (GimpObject *object,
|
|
|
|
gint64 *gui_size);
|
2010-02-06 19:58:10 +08:00
|
|
|
|
2010-02-07 23:23:02 +08:00
|
|
|
static void gimp_item_tree_uniquefy_name (GimpItemTree *tree,
|
|
|
|
GimpItem *item,
|
|
|
|
const gchar *new_name);
|
2010-02-06 19:58:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GimpItemTree, gimp_item_tree, GIMP_TYPE_OBJECT)
|
|
|
|
|
|
|
|
#define parent_class gimp_item_tree_parent_class
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_item_tree_class_init (GimpItemTreeClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
GimpObjectClass *gimp_object_class = GIMP_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->constructor = gimp_item_tree_constructor;
|
|
|
|
object_class->finalize = gimp_item_tree_finalize;
|
|
|
|
object_class->set_property = gimp_item_tree_set_property;
|
|
|
|
object_class->get_property = gimp_item_tree_get_property;
|
|
|
|
|
|
|
|
gimp_object_class->get_memsize = gimp_item_tree_get_memsize;
|
|
|
|
|
2010-02-06 20:49:09 +08:00
|
|
|
g_object_class_install_property (object_class, PROP_IMAGE,
|
|
|
|
g_param_spec_object ("image",
|
|
|
|
NULL, NULL,
|
|
|
|
GIMP_TYPE_IMAGE,
|
|
|
|
GIMP_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT_ONLY));
|
|
|
|
|
2010-02-06 19:58:10 +08:00
|
|
|
g_object_class_install_property (object_class, PROP_CONTAINER_TYPE,
|
|
|
|
g_param_spec_gtype ("container-type",
|
|
|
|
NULL, NULL,
|
|
|
|
GIMP_TYPE_ITEM_STACK,
|
|
|
|
GIMP_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT_ONLY));
|
|
|
|
|
|
|
|
g_object_class_install_property (object_class, PROP_ITEM_TYPE,
|
|
|
|
g_param_spec_gtype ("item-type",
|
|
|
|
NULL, NULL,
|
|
|
|
GIMP_TYPE_ITEM,
|
|
|
|
GIMP_PARAM_READWRITE |
|
|
|
|
G_PARAM_CONSTRUCT_ONLY));
|
|
|
|
|
2010-02-07 21:52:34 +08:00
|
|
|
g_object_class_install_property (object_class, PROP_ACTIVE_ITEM,
|
|
|
|
g_param_spec_object ("active-item",
|
|
|
|
NULL, NULL,
|
|
|
|
GIMP_TYPE_ITEM,
|
|
|
|
GIMP_PARAM_READWRITE));
|
|
|
|
|
2010-02-06 19:58:10 +08:00
|
|
|
g_type_class_add_private (klass, sizeof (GimpItemTreePrivate));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_item_tree_init (GimpItemTree *tree)
|
|
|
|
{
|
2010-02-07 23:23:02 +08:00
|
|
|
GimpItemTreePrivate *private = GIMP_ITEM_TREE_GET_PRIVATE (tree);
|
|
|
|
|
|
|
|
private->name_hash = g_hash_table_new (g_str_hash, g_str_equal);
|
2010-02-06 19:58:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static GObject *
|
|
|
|
gimp_item_tree_constructor (GType type,
|
|
|
|
guint n_params,
|
|
|
|
GObjectConstructParam *params)
|
|
|
|
{
|
|
|
|
GObject *object;
|
|
|
|
GimpItemTree *tree;
|
|
|
|
GimpItemTreePrivate *private;
|
|
|
|
|
|
|
|
object = G_OBJECT_CLASS (parent_class)->constructor (type, n_params, params);
|
|
|
|
|
|
|
|
tree = GIMP_ITEM_TREE (object);
|
|
|
|
private = GIMP_ITEM_TREE_GET_PRIVATE (tree);
|
|
|
|
|
2010-02-06 20:49:09 +08:00
|
|
|
g_assert (GIMP_IS_IMAGE (private->image));
|
2010-02-06 19:58:10 +08:00
|
|
|
g_assert (g_type_is_a (private->container_type, GIMP_TYPE_ITEM_STACK));
|
|
|
|
g_assert (g_type_is_a (private->item_type, GIMP_TYPE_ITEM));
|
|
|
|
g_assert (private->item_type != GIMP_TYPE_ITEM);
|
|
|
|
|
|
|
|
tree->container = g_object_new (private->container_type,
|
|
|
|
"name", g_type_name (private->item_type),
|
|
|
|
"children-type", private->item_type,
|
|
|
|
"policy", GIMP_CONTAINER_POLICY_STRONG,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_item_tree_finalize (GObject *object)
|
|
|
|
{
|
2010-02-07 23:23:02 +08:00
|
|
|
GimpItemTree *tree = GIMP_ITEM_TREE (object);
|
|
|
|
GimpItemTreePrivate *private = GIMP_ITEM_TREE_GET_PRIVATE (tree);
|
|
|
|
|
|
|
|
if (private->name_hash)
|
|
|
|
{
|
|
|
|
g_hash_table_unref (private->name_hash);
|
|
|
|
private->name_hash = NULL;
|
|
|
|
}
|
2010-02-06 19:58:10 +08:00
|
|
|
|
|
|
|
if (tree->container)
|
|
|
|
{
|
|
|
|
g_object_unref (tree->container);
|
|
|
|
tree->container = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_item_tree_set_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GimpItemTreePrivate *private = GIMP_ITEM_TREE_GET_PRIVATE (object);
|
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
{
|
2010-02-06 20:49:09 +08:00
|
|
|
case PROP_IMAGE:
|
|
|
|
private->image = g_value_get_object (value); /* don't ref */
|
|
|
|
break;
|
2010-02-06 19:58:10 +08:00
|
|
|
case PROP_CONTAINER_TYPE:
|
|
|
|
private->container_type = g_value_get_gtype (value);
|
|
|
|
break;
|
|
|
|
case PROP_ITEM_TYPE:
|
|
|
|
private->item_type = g_value_get_gtype (value);
|
|
|
|
break;
|
2010-02-07 21:52:34 +08:00
|
|
|
case PROP_ACTIVE_ITEM:
|
|
|
|
private->active_item = g_value_get_object (value); /* don't ref */
|
|
|
|
break;
|
2010-02-06 19:58:10 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_item_tree_get_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GimpItemTreePrivate *private = GIMP_ITEM_TREE_GET_PRIVATE (object);
|
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
{
|
2010-02-06 20:49:09 +08:00
|
|
|
case PROP_IMAGE:
|
|
|
|
g_value_set_object (value, private->image);
|
|
|
|
break;
|
2010-02-06 19:58:10 +08:00
|
|
|
case PROP_CONTAINER_TYPE:
|
|
|
|
g_value_set_gtype (value, private->container_type);
|
|
|
|
break;
|
|
|
|
case PROP_ITEM_TYPE:
|
|
|
|
g_value_set_gtype (value, private->item_type);
|
|
|
|
break;
|
2010-02-07 21:52:34 +08:00
|
|
|
case PROP_ACTIVE_ITEM:
|
|
|
|
g_value_set_object (value, private->active_item);
|
|
|
|
break;
|
2010-02-06 19:58:10 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint64
|
|
|
|
gimp_item_tree_get_memsize (GimpObject *object,
|
|
|
|
gint64 *gui_size)
|
|
|
|
{
|
|
|
|
GimpItemTree *tree = GIMP_ITEM_TREE (object);
|
|
|
|
gint64 memsize = 0;
|
|
|
|
|
|
|
|
memsize += gimp_object_get_memsize (GIMP_OBJECT (tree->container), gui_size);
|
|
|
|
|
|
|
|
return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
|
|
|
|
gui_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* public functions */
|
|
|
|
|
|
|
|
GimpItemTree *
|
2010-02-06 20:49:09 +08:00
|
|
|
gimp_item_tree_new (GimpImage *image,
|
|
|
|
GType container_type,
|
|
|
|
GType item_type)
|
2010-02-06 19:58:10 +08:00
|
|
|
{
|
2010-02-06 20:49:09 +08:00
|
|
|
g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
|
2010-02-06 19:58:10 +08:00
|
|
|
g_return_val_if_fail (g_type_is_a (container_type, GIMP_TYPE_ITEM_STACK), NULL);
|
|
|
|
g_return_val_if_fail (g_type_is_a (item_type, GIMP_TYPE_ITEM), NULL);
|
|
|
|
|
|
|
|
return g_object_new (GIMP_TYPE_ITEM_TREE,
|
2010-02-06 20:49:09 +08:00
|
|
|
"image", image,
|
2010-02-06 19:58:10 +08:00
|
|
|
"container-type", container_type,
|
|
|
|
"item-type", item_type,
|
|
|
|
NULL);
|
|
|
|
}
|
2010-02-06 23:00:06 +08:00
|
|
|
|
2010-02-07 21:52:34 +08:00
|
|
|
GimpItem *
|
|
|
|
gimp_item_tree_get_active_item (GimpItemTree *tree)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GIMP_IS_ITEM_TREE (tree), NULL);
|
|
|
|
|
|
|
|
return GIMP_ITEM_TREE_GET_PRIVATE (tree)->active_item;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_item_tree_set_active_item (GimpItemTree *tree,
|
|
|
|
GimpItem *item)
|
|
|
|
{
|
|
|
|
GimpItemTreePrivate *private;
|
|
|
|
|
|
|
|
g_return_if_fail (GIMP_IS_ITEM_TREE (tree));
|
|
|
|
|
|
|
|
private = GIMP_ITEM_TREE_GET_PRIVATE (tree);
|
|
|
|
|
|
|
|
g_return_if_fail (item == NULL ||
|
2010-02-08 08:11:30 +08:00
|
|
|
G_TYPE_CHECK_INSTANCE_TYPE (item, private->item_type));
|
2010-07-08 21:06:19 +08:00
|
|
|
g_return_if_fail (item == NULL || gimp_item_get_tree (item) == tree);
|
2010-02-07 21:52:34 +08:00
|
|
|
|
|
|
|
if (item != private->active_item)
|
|
|
|
{
|
|
|
|
private->active_item = item;
|
|
|
|
|
|
|
|
g_object_notify (G_OBJECT (tree), "active-item");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-07 23:34:44 +08:00
|
|
|
GimpItem *
|
|
|
|
gimp_item_tree_get_item_by_name (GimpItemTree *tree,
|
|
|
|
const gchar *name)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GIMP_IS_ITEM_TREE (tree), NULL);
|
|
|
|
g_return_val_if_fail (name != NULL, NULL);
|
|
|
|
|
|
|
|
return g_hash_table_lookup (GIMP_ITEM_TREE_GET_PRIVATE (tree)->name_hash,
|
|
|
|
name);
|
|
|
|
}
|
|
|
|
|
2010-02-10 00:47:08 +08:00
|
|
|
gboolean
|
|
|
|
gimp_item_tree_get_insert_pos (GimpItemTree *tree,
|
|
|
|
GimpItem *item,
|
|
|
|
GimpItem **parent,
|
|
|
|
gint *position)
|
2010-02-07 21:08:52 +08:00
|
|
|
{
|
2010-02-07 22:01:31 +08:00
|
|
|
GimpItemTreePrivate *private;
|
|
|
|
GimpContainer *container;
|
2010-02-07 21:08:52 +08:00
|
|
|
|
2010-02-10 00:47:08 +08:00
|
|
|
g_return_val_if_fail (GIMP_IS_ITEM_TREE (tree), FALSE);
|
|
|
|
g_return_val_if_fail (parent != NULL, FALSE);
|
2010-02-07 21:08:52 +08:00
|
|
|
|
2010-02-07 22:01:31 +08:00
|
|
|
private = GIMP_ITEM_TREE_GET_PRIVATE (tree);
|
|
|
|
|
2010-02-10 00:47:08 +08:00
|
|
|
g_return_val_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (item, private->item_type),
|
|
|
|
FALSE);
|
|
|
|
g_return_val_if_fail (! gimp_item_is_attached (item), FALSE);
|
|
|
|
g_return_val_if_fail (gimp_item_get_image (item) == private->image, FALSE);
|
|
|
|
g_return_val_if_fail (*parent == NULL ||
|
|
|
|
*parent == GIMP_IMAGE_ACTIVE_PARENT ||
|
|
|
|
G_TYPE_CHECK_INSTANCE_TYPE (*parent, private->item_type),
|
2010-02-09 07:33:41 +08:00
|
|
|
FALSE);
|
2010-02-10 00:47:08 +08:00
|
|
|
g_return_val_if_fail (*parent == NULL ||
|
|
|
|
*parent == GIMP_IMAGE_ACTIVE_PARENT ||
|
2010-07-08 21:06:19 +08:00
|
|
|
gimp_item_get_tree (*parent) == tree, FALSE);
|
2010-02-10 00:47:08 +08:00
|
|
|
g_return_val_if_fail (*parent == NULL ||
|
|
|
|
*parent == GIMP_IMAGE_ACTIVE_PARENT ||
|
|
|
|
gimp_viewable_get_children (GIMP_VIEWABLE (*parent)),
|
2010-02-09 07:33:41 +08:00
|
|
|
FALSE);
|
2010-02-10 00:47:08 +08:00
|
|
|
g_return_val_if_fail (position != NULL, FALSE);
|
2010-02-09 07:33:41 +08:00
|
|
|
|
2010-02-07 21:08:52 +08:00
|
|
|
/* if we want to insert in the active item's parent container */
|
2010-02-10 00:47:08 +08:00
|
|
|
if (*parent == GIMP_IMAGE_ACTIVE_PARENT)
|
2010-02-07 21:08:52 +08:00
|
|
|
{
|
2010-02-07 22:01:31 +08:00
|
|
|
if (private->active_item)
|
2010-02-07 21:08:52 +08:00
|
|
|
{
|
|
|
|
/* if the active item is a branch, add to the top of that
|
|
|
|
* branch; add to the active item's parent container
|
|
|
|
* otherwise
|
|
|
|
*/
|
2010-02-07 22:01:31 +08:00
|
|
|
if (gimp_viewable_get_children (GIMP_VIEWABLE (private->active_item)))
|
2010-02-07 21:08:52 +08:00
|
|
|
{
|
2010-02-10 00:47:08 +08:00
|
|
|
*parent = private->active_item;
|
2010-02-07 21:08:52 +08:00
|
|
|
*position = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-02-10 00:47:08 +08:00
|
|
|
*parent = gimp_item_get_parent (private->active_item);
|
2010-02-07 21:08:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* use the toplevel container if there is no active item */
|
2010-02-10 00:47:08 +08:00
|
|
|
*parent = NULL;
|
2010-02-07 21:08:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-10 00:47:08 +08:00
|
|
|
if (*parent)
|
|
|
|
container = gimp_viewable_get_children (GIMP_VIEWABLE (*parent));
|
2010-02-07 21:08:52 +08:00
|
|
|
else
|
|
|
|
container = tree->container;
|
|
|
|
|
|
|
|
/* if we want to add on top of the active item */
|
|
|
|
if (*position == -1)
|
|
|
|
{
|
2010-02-07 22:01:31 +08:00
|
|
|
if (private->active_item)
|
2010-02-10 00:47:08 +08:00
|
|
|
*position =
|
|
|
|
gimp_container_get_child_index (container,
|
|
|
|
GIMP_OBJECT (private->active_item));
|
2010-02-07 21:08:52 +08:00
|
|
|
|
|
|
|
/* if the active item is not in the specified parent container,
|
|
|
|
* fall back to index 0
|
|
|
|
*/
|
|
|
|
if (*position == -1)
|
|
|
|
*position = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* don't add at a non-existing index */
|
|
|
|
*position = CLAMP (*position, 0, gimp_container_get_n_children (container));
|
|
|
|
|
2010-02-10 00:47:08 +08:00
|
|
|
return TRUE;
|
2010-02-07 21:08:52 +08:00
|
|
|
}
|
|
|
|
|
2010-02-07 18:47:33 +08:00
|
|
|
void
|
|
|
|
gimp_item_tree_add_item (GimpItemTree *tree,
|
|
|
|
GimpItem *item,
|
|
|
|
GimpItem *parent,
|
|
|
|
gint position)
|
|
|
|
{
|
2010-02-07 22:07:30 +08:00
|
|
|
GimpItemTreePrivate *private;
|
|
|
|
GimpContainer *container;
|
2010-02-09 07:25:10 +08:00
|
|
|
GimpContainer *children;
|
2010-02-07 18:47:33 +08:00
|
|
|
|
|
|
|
g_return_if_fail (GIMP_IS_ITEM_TREE (tree));
|
2010-02-07 22:07:30 +08:00
|
|
|
|
|
|
|
private = GIMP_ITEM_TREE_GET_PRIVATE (tree);
|
|
|
|
|
2010-02-08 06:35:22 +08:00
|
|
|
g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (item, private->item_type));
|
2010-02-07 22:07:30 +08:00
|
|
|
g_return_if_fail (! gimp_item_is_attached (item));
|
|
|
|
g_return_if_fail (gimp_item_get_image (item) == private->image);
|
2010-02-08 06:35:22 +08:00
|
|
|
g_return_if_fail (parent == NULL ||
|
|
|
|
G_TYPE_CHECK_INSTANCE_TYPE (parent, private->item_type));
|
2010-07-08 21:06:19 +08:00
|
|
|
g_return_if_fail (parent == NULL || gimp_item_get_tree (parent) == tree);
|
2010-02-08 06:35:22 +08:00
|
|
|
g_return_if_fail (parent == NULL ||
|
|
|
|
gimp_viewable_get_children (GIMP_VIEWABLE (parent)));
|
2010-02-07 18:47:33 +08:00
|
|
|
|
2010-02-07 23:23:02 +08:00
|
|
|
gimp_item_tree_uniquefy_name (tree, item, NULL);
|
|
|
|
|
2010-02-09 07:25:10 +08:00
|
|
|
children = gimp_viewable_get_children (GIMP_VIEWABLE (item));
|
|
|
|
|
|
|
|
if (children)
|
|
|
|
{
|
|
|
|
GList *list = gimp_item_stack_get_item_list (GIMP_ITEM_STACK (children));
|
|
|
|
|
|
|
|
while (list)
|
|
|
|
{
|
|
|
|
gimp_item_tree_uniquefy_name (tree, list->data, NULL);
|
|
|
|
|
|
|
|
list = g_list_remove (list, list->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-07 18:47:33 +08:00
|
|
|
if (parent)
|
|
|
|
container = gimp_viewable_get_children (GIMP_VIEWABLE (parent));
|
|
|
|
else
|
|
|
|
container = tree->container;
|
|
|
|
|
|
|
|
if (parent)
|
|
|
|
gimp_viewable_set_parent (GIMP_VIEWABLE (item),
|
|
|
|
GIMP_VIEWABLE (parent));
|
|
|
|
|
|
|
|
gimp_container_insert (container, GIMP_OBJECT (item), position);
|
|
|
|
}
|
|
|
|
|
|
|
|
GimpItem *
|
|
|
|
gimp_item_tree_remove_item (GimpItemTree *tree,
|
|
|
|
GimpItem *item,
|
|
|
|
GimpItem *new_active)
|
|
|
|
{
|
2010-02-07 22:07:30 +08:00
|
|
|
GimpItemTreePrivate *private;
|
|
|
|
GimpItem *parent;
|
|
|
|
GimpContainer *container;
|
2010-02-09 07:25:10 +08:00
|
|
|
GimpContainer *children;
|
2010-02-07 22:07:30 +08:00
|
|
|
gint index;
|
2010-02-07 18:47:33 +08:00
|
|
|
|
|
|
|
g_return_val_if_fail (GIMP_IS_ITEM_TREE (tree), NULL);
|
2010-02-07 22:07:30 +08:00
|
|
|
|
|
|
|
private = GIMP_ITEM_TREE_GET_PRIVATE (tree);
|
|
|
|
|
2010-02-08 06:35:22 +08:00
|
|
|
g_return_val_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (item, private->item_type),
|
|
|
|
NULL);
|
2010-07-08 21:06:19 +08:00
|
|
|
g_return_val_if_fail (gimp_item_get_tree (item) == tree, NULL);
|
2010-02-07 18:47:33 +08:00
|
|
|
|
2010-02-07 20:46:46 +08:00
|
|
|
parent = gimp_item_get_parent (item);
|
2010-02-07 18:47:33 +08:00
|
|
|
container = gimp_item_get_container (item);
|
|
|
|
index = gimp_item_get_index (item);
|
|
|
|
|
|
|
|
g_object_ref (item);
|
|
|
|
|
2010-02-08 08:14:22 +08:00
|
|
|
g_hash_table_remove (private->name_hash,
|
|
|
|
gimp_object_get_name (item));
|
|
|
|
|
2010-02-09 07:25:10 +08:00
|
|
|
children = gimp_viewable_get_children (GIMP_VIEWABLE (item));
|
|
|
|
|
|
|
|
if (children)
|
|
|
|
{
|
|
|
|
GList *list = gimp_item_stack_get_item_list (GIMP_ITEM_STACK (children));
|
|
|
|
|
|
|
|
while (list)
|
|
|
|
{
|
|
|
|
g_hash_table_remove (private->name_hash,
|
|
|
|
gimp_object_get_name (list->data));
|
|
|
|
|
|
|
|
list = g_list_remove (list, list->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-07 18:47:33 +08:00
|
|
|
gimp_container_remove (container, GIMP_OBJECT (item));
|
|
|
|
|
|
|
|
if (parent)
|
|
|
|
gimp_viewable_set_parent (GIMP_VIEWABLE (item), NULL);
|
|
|
|
|
|
|
|
gimp_item_removed (item);
|
|
|
|
|
|
|
|
if (! new_active)
|
|
|
|
{
|
|
|
|
gint n_children = gimp_container_get_n_children (container);
|
|
|
|
|
|
|
|
if (n_children > 0)
|
|
|
|
{
|
|
|
|
index = CLAMP (index, 0, n_children - 1);
|
|
|
|
|
|
|
|
new_active =
|
|
|
|
GIMP_ITEM (gimp_container_get_child_by_index (container, index));
|
|
|
|
}
|
|
|
|
else if (parent)
|
|
|
|
{
|
|
|
|
new_active = parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_unref (item);
|
|
|
|
|
|
|
|
return new_active;
|
|
|
|
}
|
|
|
|
|
2010-02-06 23:00:06 +08:00
|
|
|
gboolean
|
2010-02-06 23:41:54 +08:00
|
|
|
gimp_item_tree_reorder_item (GimpItemTree *tree,
|
|
|
|
GimpItem *item,
|
|
|
|
GimpItem *new_parent,
|
|
|
|
gint new_index,
|
|
|
|
gboolean push_undo,
|
|
|
|
const gchar *undo_desc)
|
2010-02-06 23:00:06 +08:00
|
|
|
{
|
|
|
|
GimpItemTreePrivate *private;
|
|
|
|
GimpContainer *container;
|
|
|
|
GimpContainer *new_container;
|
|
|
|
gint n_items;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GIMP_IS_ITEM_TREE (tree), FALSE);
|
|
|
|
|
|
|
|
private = GIMP_ITEM_TREE_GET_PRIVATE (tree);
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (item, private->item_type),
|
|
|
|
FALSE);
|
2010-07-08 21:06:19 +08:00
|
|
|
g_return_val_if_fail (gimp_item_get_tree (item) == tree, FALSE);
|
2010-02-06 23:00:06 +08:00
|
|
|
g_return_val_if_fail (new_parent == NULL ||
|
|
|
|
G_TYPE_CHECK_INSTANCE_TYPE (new_parent,
|
|
|
|
private->item_type),
|
|
|
|
FALSE);
|
|
|
|
g_return_val_if_fail (new_parent == NULL ||
|
2010-07-08 21:06:19 +08:00
|
|
|
gimp_item_get_tree (new_parent) == tree, FALSE);
|
2010-02-06 23:00:06 +08:00
|
|
|
g_return_val_if_fail (new_parent == NULL ||
|
|
|
|
gimp_viewable_get_children (GIMP_VIEWABLE (new_parent)),
|
|
|
|
FALSE);
|
2010-02-08 07:47:57 +08:00
|
|
|
g_return_val_if_fail (item != new_parent, FALSE);
|
|
|
|
g_return_val_if_fail (new_parent == NULL ||
|
|
|
|
! gimp_viewable_is_ancestor (GIMP_VIEWABLE (item),
|
|
|
|
GIMP_VIEWABLE (new_parent)),
|
|
|
|
FALSE);
|
2010-02-06 23:00:06 +08:00
|
|
|
|
|
|
|
container = gimp_item_get_container (item);
|
|
|
|
|
|
|
|
if (new_parent)
|
|
|
|
new_container = gimp_viewable_get_children (GIMP_VIEWABLE (new_parent));
|
|
|
|
else
|
|
|
|
new_container = tree->container;
|
|
|
|
|
|
|
|
n_items = gimp_container_get_n_children (new_container);
|
|
|
|
|
|
|
|
if (new_container == container)
|
|
|
|
n_items--;
|
|
|
|
|
|
|
|
new_index = CLAMP (new_index, 0, n_items);
|
|
|
|
|
|
|
|
if (new_container != container ||
|
|
|
|
new_index != gimp_item_get_index (item))
|
|
|
|
{
|
2010-02-06 23:41:54 +08:00
|
|
|
if (push_undo)
|
|
|
|
gimp_image_undo_push_item_reorder (private->image, undo_desc, item);
|
2010-02-06 23:00:06 +08:00
|
|
|
|
|
|
|
if (new_container != container)
|
|
|
|
{
|
|
|
|
g_object_ref (item);
|
|
|
|
|
|
|
|
gimp_container_remove (container, GIMP_OBJECT (item));
|
|
|
|
|
|
|
|
gimp_viewable_set_parent (GIMP_VIEWABLE (item),
|
|
|
|
GIMP_VIEWABLE (new_parent));
|
|
|
|
|
|
|
|
gimp_container_insert (new_container, GIMP_OBJECT (item), new_index);
|
|
|
|
|
|
|
|
g_object_unref (item);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gimp_container_reorder (container, GIMP_OBJECT (item), new_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2010-02-07 22:39:42 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
gimp_item_tree_rename_item (GimpItemTree *tree,
|
|
|
|
GimpItem *item,
|
|
|
|
const gchar *new_name,
|
2010-02-07 22:56:53 +08:00
|
|
|
gboolean push_undo,
|
2010-02-07 22:39:42 +08:00
|
|
|
const gchar *undo_desc)
|
|
|
|
{
|
|
|
|
GimpItemTreePrivate *private;
|
|
|
|
|
|
|
|
g_return_if_fail (GIMP_IS_ITEM_TREE (tree));
|
|
|
|
|
|
|
|
private = GIMP_ITEM_TREE_GET_PRIVATE (tree);
|
|
|
|
|
2010-02-08 06:35:22 +08:00
|
|
|
g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (item, private->item_type));
|
2010-07-08 21:06:19 +08:00
|
|
|
g_return_if_fail (gimp_item_get_tree (item) == tree);
|
2010-02-07 22:39:42 +08:00
|
|
|
g_return_if_fail (new_name != NULL);
|
|
|
|
|
|
|
|
if (strcmp (new_name, gimp_object_get_name (item)))
|
|
|
|
{
|
2010-02-07 22:56:53 +08:00
|
|
|
if (push_undo)
|
|
|
|
gimp_image_undo_push_item_rename (item->image, undo_desc, item);
|
2010-02-07 22:39:42 +08:00
|
|
|
|
2010-02-07 23:23:02 +08:00
|
|
|
gimp_item_tree_uniquefy_name (tree, item, new_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* private functions */
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_item_tree_uniquefy_name (GimpItemTree *tree,
|
|
|
|
GimpItem *item,
|
|
|
|
const gchar *new_name)
|
|
|
|
{
|
|
|
|
GimpItemTreePrivate *private = GIMP_ITEM_TREE_GET_PRIVATE (tree);
|
|
|
|
|
|
|
|
if (new_name)
|
|
|
|
{
|
|
|
|
g_hash_table_remove (private->name_hash,
|
|
|
|
gimp_object_get_name (item));
|
|
|
|
|
2010-02-07 22:39:42 +08:00
|
|
|
gimp_object_set_name (GIMP_OBJECT (item), new_name);
|
|
|
|
}
|
2010-02-07 23:23:02 +08:00
|
|
|
|
2010-02-08 06:56:40 +08:00
|
|
|
if (g_hash_table_lookup (private->name_hash,
|
|
|
|
gimp_object_get_name (item)))
|
2010-02-07 23:23:02 +08:00
|
|
|
{
|
2010-02-08 06:56:40 +08:00
|
|
|
gchar *name = g_strdup (gimp_object_get_name (item));
|
|
|
|
gchar *ext = strrchr (name, '#');
|
|
|
|
gchar *new_name = NULL;
|
|
|
|
gint number = 0;
|
2010-02-07 23:23:02 +08:00
|
|
|
|
|
|
|
if (ext)
|
|
|
|
{
|
2010-02-08 06:56:40 +08:00
|
|
|
gchar ext_str[8];
|
2010-02-07 23:23:02 +08:00
|
|
|
|
|
|
|
number = atoi (ext + 1);
|
|
|
|
|
2010-02-08 06:56:40 +08:00
|
|
|
g_snprintf (ext_str, sizeof (ext_str), "%d", number);
|
2010-02-07 23:23:02 +08:00
|
|
|
|
|
|
|
/* check if the extension really is of the form "#<n>" */
|
|
|
|
if (! strcmp (ext_str, ext + 1))
|
|
|
|
{
|
2010-02-08 06:56:40 +08:00
|
|
|
if (ext > name && *(ext - 1) == ' ')
|
|
|
|
ext--;
|
|
|
|
|
2010-02-07 23:23:02 +08:00
|
|
|
*ext = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
number = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-08 06:56:40 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
number++;
|
|
|
|
|
|
|
|
g_free (new_name);
|
2010-02-07 23:23:02 +08:00
|
|
|
|
2010-02-08 06:56:40 +08:00
|
|
|
new_name = g_strdup_printf ("%s #%d", name, number);
|
|
|
|
}
|
|
|
|
while (g_hash_table_lookup (private->name_hash, new_name));
|
2010-02-07 23:23:02 +08:00
|
|
|
|
|
|
|
g_free (name);
|
2010-02-08 06:56:40 +08:00
|
|
|
|
|
|
|
gimp_object_take_name (GIMP_OBJECT (item), new_name);
|
2010-02-07 23:23:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
g_hash_table_insert (private->name_hash,
|
|
|
|
(gpointer) gimp_object_get_name (item),
|
|
|
|
item);
|
2010-02-07 22:39:42 +08:00
|
|
|
}
|