Let gimp_item_tree_get_insert_pos() return a boolean indicating success

and move more precondition checks there. Remove lots and lots of
checks from all its callers and simply bail out if it returns FALSE.
This commit is contained in:
Michael Natterer 2010-02-09 17:47:08 +01:00
parent e83954c49a
commit 8afdbd8053
3 changed files with 92 additions and 134 deletions

View File

@ -3259,30 +3259,16 @@ gimp_image_add_layer (GimpImage *image,
gboolean old_has_alpha;
g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
g_return_val_if_fail (GIMP_IS_LAYER (layer), FALSE);
g_return_val_if_fail (! gimp_item_is_attached (GIMP_ITEM (layer)), FALSE);
g_return_val_if_fail (gimp_item_get_image (GIMP_ITEM (layer)) == image,
FALSE);
g_return_val_if_fail (parent == NULL ||
parent == GIMP_IMAGE_ACTIVE_PARENT ||
GIMP_IS_LAYER (parent), FALSE);
g_return_val_if_fail (parent == NULL ||
parent == GIMP_IMAGE_ACTIVE_PARENT ||
gimp_item_is_attached (GIMP_ITEM (parent)), FALSE);
g_return_val_if_fail (parent == NULL ||
parent == GIMP_IMAGE_ACTIVE_PARENT ||
gimp_item_get_image (GIMP_ITEM (parent)) == image,
FALSE);
g_return_val_if_fail (parent == NULL ||
parent == GIMP_IMAGE_ACTIVE_PARENT ||
gimp_viewable_get_children (GIMP_VIEWABLE (parent)),
FALSE);
private = GIMP_IMAGE_GET_PRIVATE (image);
parent = GIMP_LAYER (gimp_item_tree_get_insert_pos (private->layers,
(GimpItem *) parent,
&position));
/* item and parent are type-checked in GimpItemTree
*/
if (! gimp_item_tree_get_insert_pos (private->layers,
(GimpItem *) layer,
(GimpItem **) &parent,
&position))
return FALSE;
/* If there is a floating selection (and this isn't it!),
* make sure the insert position is greater than 0
@ -3449,24 +3435,16 @@ gimp_image_add_layers (GimpImage *image,
g_return_if_fail (GIMP_IS_IMAGE (image));
g_return_if_fail (layers != NULL);
g_return_if_fail (parent == NULL ||
parent == GIMP_IMAGE_ACTIVE_PARENT ||
GIMP_IS_LAYER (parent));
g_return_if_fail (parent == NULL ||
parent == GIMP_IMAGE_ACTIVE_PARENT ||
gimp_item_is_attached (GIMP_ITEM (parent)));
g_return_if_fail (parent == NULL ||
parent == GIMP_IMAGE_ACTIVE_PARENT ||
gimp_item_get_image (GIMP_ITEM (parent)) == image);
g_return_if_fail (parent == NULL ||
parent == GIMP_IMAGE_ACTIVE_PARENT ||
gimp_viewable_get_children (GIMP_VIEWABLE (parent)));
private = GIMP_IMAGE_GET_PRIVATE (image);
parent = GIMP_LAYER (gimp_item_tree_get_insert_pos (private->layers,
(GimpItem *) parent,
&position));
/* item and parent are type-checked in GimpItemTree
*/
if (! gimp_item_tree_get_insert_pos (private->layers,
(GimpItem *) layers->data,
(GimpItem **) &parent,
&position))
return;
for (list = layers; list; list = g_list_next (list))
{
@ -3622,30 +3600,16 @@ gimp_image_add_channel (GimpImage *image,
GimpImagePrivate *private;
g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
g_return_val_if_fail (GIMP_IS_CHANNEL (channel), FALSE);
g_return_val_if_fail (! gimp_item_is_attached (GIMP_ITEM (channel)), FALSE);
g_return_val_if_fail (gimp_item_get_image (GIMP_ITEM (channel)) == image,
FALSE);
g_return_val_if_fail (parent == NULL ||
parent == GIMP_IMAGE_ACTIVE_PARENT ||
GIMP_IS_CHANNEL (parent), FALSE);
g_return_val_if_fail (parent == NULL ||
parent == GIMP_IMAGE_ACTIVE_PARENT ||
gimp_item_is_attached (GIMP_ITEM (parent)), FALSE);
g_return_val_if_fail (parent == NULL ||
parent == GIMP_IMAGE_ACTIVE_PARENT ||
gimp_item_get_image (GIMP_ITEM (parent)) == image,
FALSE);
g_return_val_if_fail (parent == NULL ||
parent == GIMP_IMAGE_ACTIVE_PARENT ||
gimp_viewable_get_children (GIMP_VIEWABLE (parent)),
FALSE);
private = GIMP_IMAGE_GET_PRIVATE (image);
parent = GIMP_CHANNEL (gimp_item_tree_get_insert_pos (private->channels,
(GimpItem *) parent,
&position));
/* item and parent are type-checked in GimpItemTree
*/
if (! gimp_item_tree_get_insert_pos (private->channels,
(GimpItem *) channel,
(GimpItem **) &parent,
&position))
return FALSE;
if (push_undo)
gimp_image_undo_push_channel_add (image, _("Add Channel"),
@ -3837,30 +3801,16 @@ gimp_image_add_vectors (GimpImage *image,
GimpImagePrivate *private;
g_return_val_if_fail (GIMP_IS_IMAGE (image), FALSE);
g_return_val_if_fail (GIMP_IS_VECTORS (vectors), FALSE);
g_return_val_if_fail (! gimp_item_is_attached (GIMP_ITEM (vectors)), FALSE);
g_return_val_if_fail (gimp_item_get_image (GIMP_ITEM (vectors)) == image,
FALSE);
g_return_val_if_fail (parent == NULL ||
parent == GIMP_IMAGE_ACTIVE_PARENT ||
GIMP_IS_VECTORS (parent), FALSE);
g_return_val_if_fail (parent == NULL ||
parent == GIMP_IMAGE_ACTIVE_PARENT ||
gimp_item_is_attached (GIMP_ITEM (parent)), FALSE);
g_return_val_if_fail (parent == NULL ||
parent == GIMP_IMAGE_ACTIVE_PARENT ||
gimp_item_get_image (GIMP_ITEM (parent)) == image,
FALSE);
g_return_val_if_fail (parent == NULL ||
parent == GIMP_IMAGE_ACTIVE_PARENT ||
gimp_viewable_get_children (GIMP_VIEWABLE (parent)),
FALSE);
private = GIMP_IMAGE_GET_PRIVATE (image);
parent = GIMP_VECTORS (gimp_item_tree_get_insert_pos (private->vectors,
(GimpItem *) parent,
&position));
/* item and parent are type-checked in GimpItemTree
*/
if (! gimp_item_tree_get_insert_pos (private->vectors,
(GimpItem *) vectors,
(GimpItem **) &parent,
&position))
return FALSE;
if (push_undo)
gimp_image_undo_push_vectors_add (image, _("Add Path"),

View File

@ -325,37 +325,43 @@ gimp_item_tree_get_item_by_name (GimpItemTree *tree,
name);
}
GimpItem *
gimp_item_tree_get_insert_pos (GimpItemTree *tree,
GimpItem *parent,
gint *position)
gboolean
gimp_item_tree_get_insert_pos (GimpItemTree *tree,
GimpItem *item,
GimpItem **parent,
gint *position)
{
GimpItemTreePrivate *private;
GimpContainer *container;
g_return_val_if_fail (GIMP_IS_ITEM_TREE (tree), NULL);
g_return_val_if_fail (GIMP_IS_ITEM_TREE (tree), FALSE);
g_return_val_if_fail (parent != NULL, FALSE);
private = GIMP_ITEM_TREE_GET_PRIVATE (tree);
g_return_val_if_fail (parent == NULL ||
parent == GIMP_IMAGE_ACTIVE_PARENT ||
G_TYPE_CHECK_INSTANCE_TYPE (parent, private->item_type),
g_return_val_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (item, private->item_type),
FALSE);
g_return_val_if_fail (parent == NULL ||
parent == GIMP_IMAGE_ACTIVE_PARENT ||
gimp_item_is_attached (parent), FALSE);
g_return_val_if_fail (parent == NULL ||
parent == GIMP_IMAGE_ACTIVE_PARENT ||
gimp_item_get_image (parent) == private->image,
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),
FALSE);
g_return_val_if_fail (parent == NULL ||
parent == GIMP_IMAGE_ACTIVE_PARENT ||
gimp_viewable_get_children (GIMP_VIEWABLE (parent)),
g_return_val_if_fail (*parent == NULL ||
*parent == GIMP_IMAGE_ACTIVE_PARENT ||
gimp_item_is_attached (*parent), FALSE);
g_return_val_if_fail (*parent == NULL ||
*parent == GIMP_IMAGE_ACTIVE_PARENT ||
gimp_item_get_image (*parent) == private->image,
FALSE);
g_return_val_if_fail (position != NULL, NULL);
g_return_val_if_fail (*parent == NULL ||
*parent == GIMP_IMAGE_ACTIVE_PARENT ||
gimp_viewable_get_children (GIMP_VIEWABLE (*parent)),
FALSE);
g_return_val_if_fail (position != NULL, FALSE);
/* if we want to insert in the active item's parent container */
if (parent == GIMP_IMAGE_ACTIVE_PARENT)
if (*parent == GIMP_IMAGE_ACTIVE_PARENT)
{
if (private->active_item)
{
@ -365,23 +371,23 @@ gimp_item_tree_get_insert_pos (GimpItemTree *tree,
*/
if (gimp_viewable_get_children (GIMP_VIEWABLE (private->active_item)))
{
parent = private->active_item;
*parent = private->active_item;
*position = 0;
}
else
{
parent = gimp_item_get_parent (private->active_item);
*parent = gimp_item_get_parent (private->active_item);
}
}
else
{
/* use the toplevel container if there is no active item */
parent = NULL;
*parent = NULL;
}
}
if (parent)
container = gimp_viewable_get_children (GIMP_VIEWABLE (parent));
if (*parent)
container = gimp_viewable_get_children (GIMP_VIEWABLE (*parent));
else
container = tree->container;
@ -389,8 +395,9 @@ gimp_item_tree_get_insert_pos (GimpItemTree *tree,
if (*position == -1)
{
if (private->active_item)
*position = gimp_container_get_child_index (container,
GIMP_OBJECT (private->active_item));
*position =
gimp_container_get_child_index (container,
GIMP_OBJECT (private->active_item));
/* if the active item is not in the specified parent container,
* fall back to index 0
@ -402,7 +409,7 @@ gimp_item_tree_get_insert_pos (GimpItemTree *tree,
/* don't add at a non-existing index */
*position = CLAMP (*position, 0, gimp_container_get_n_children (container));
return parent;
return TRUE;
}
void

View File

@ -48,41 +48,42 @@ struct _GimpItemTreeClass
GType gimp_item_tree_get_type (void) G_GNUC_CONST;
GimpItemTree * gimp_item_tree_new (GimpImage *image,
GType container_type,
GType item_type);
GimpItemTree * gimp_item_tree_new (GimpImage *image,
GType container_type,
GType item_type);
GimpItem * gimp_item_tree_get_active_item (GimpItemTree *tree);
void gimp_item_tree_set_active_item (GimpItemTree *tree,
GimpItem *item);
GimpItem * gimp_item_tree_get_active_item (GimpItemTree *tree);
void gimp_item_tree_set_active_item (GimpItemTree *tree,
GimpItem *item);
GimpItem * gimp_item_tree_get_item_by_name (GimpItemTree *tree,
const gchar *name);
GimpItem * gimp_item_tree_get_item_by_name (GimpItemTree *tree,
const gchar *name);
GimpItem * gimp_item_tree_get_insert_pos (GimpItemTree *tree,
GimpItem *parent,
gint *position);
gboolean gimp_item_tree_get_insert_pos (GimpItemTree *tree,
GimpItem *item,
GimpItem **parent,
gint *position);
void gimp_item_tree_add_item (GimpItemTree *tree,
GimpItem *item,
GimpItem *parent,
gint position);
GimpItem * gimp_item_tree_remove_item (GimpItemTree *tree,
GimpItem *item,
GimpItem *new_active);
void gimp_item_tree_add_item (GimpItemTree *tree,
GimpItem *item,
GimpItem *parent,
gint position);
GimpItem * gimp_item_tree_remove_item (GimpItemTree *tree,
GimpItem *item,
GimpItem *new_active);
gboolean gimp_item_tree_reorder_item (GimpItemTree *tree,
GimpItem *item,
GimpItem *new_parent,
gint new_index,
gboolean push_undo,
const gchar *undo_desc);
gboolean gimp_item_tree_reorder_item (GimpItemTree *tree,
GimpItem *item,
GimpItem *new_parent,
gint new_index,
gboolean push_undo,
const gchar *undo_desc);
void gimp_item_tree_rename_item (GimpItemTree *tree,
GimpItem *item,
const gchar *new_name,
gboolean push_undo,
const gchar *undo_desc);
void gimp_item_tree_rename_item (GimpItemTree *tree,
GimpItem *item,
const gchar *new_name,
gboolean push_undo,
const gchar *undo_desc);
#endif /* __GIMP_ITEM_TREE_H__ */