gimpitemstack.c 8.5 KB
Newer Older
1 2 3 4 5 6
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
 *
 * gimpitemstack.c
 * Copyright (C) 2008 Michael Natterer <mitch@gimp.org>
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10 11 12 13 14 15 16 17
 * (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
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 20 21 22
 */

#include "config.h"

23 24
#include <string.h>

25
#include <cairo.h>
26
#include <gdk-pixbuf/gdk-pixbuf.h>
27 28
#include <gegl.h>

29 30
#include "libgimpcolor/gimpcolor.h"

31 32 33 34 35 36 37 38
#include "core-types.h"

#include "gimpitem.h"
#include "gimpitemstack.h"


/*  local function prototypes  */

39
static void   gimp_item_stack_constructed (GObject       *object);
40

41 42 43 44
static void   gimp_item_stack_add         (GimpContainer *container,
                                           GimpObject    *object);
static void   gimp_item_stack_remove      (GimpContainer *container,
                                           GimpObject    *object);
45 46


47
G_DEFINE_TYPE (GimpItemStack, gimp_item_stack, GIMP_TYPE_FILTER_STACK)
48 49 50 51 52 53 54 55 56 57

#define parent_class gimp_item_stack_parent_class


static void
gimp_item_stack_class_init (GimpItemStackClass *klass)
{
  GObjectClass       *object_class    = G_OBJECT_CLASS (klass);
  GimpContainerClass *container_class = GIMP_CONTAINER_CLASS (klass);

58
  object_class->constructed = gimp_item_stack_constructed;
59 60 61 62 63 64 65 66 67 68

  container_class->add      = gimp_item_stack_add;
  container_class->remove   = gimp_item_stack_remove;
}

static void
gimp_item_stack_init (GimpItemStack *stack)
{
}

69 70
static void
gimp_item_stack_constructed (GObject *object)
71
{
72
  GimpContainer *container = GIMP_CONTAINER (object);
73

74
  G_OBJECT_CLASS (parent_class)->constructed (object);
75

76 77
  gimp_assert (g_type_is_a (gimp_container_get_children_type (container),
                         GIMP_TYPE_ITEM));
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
}

static void
gimp_item_stack_add (GimpContainer *container,
                     GimpObject    *object)
{
  g_object_ref_sink (object);

  GIMP_CONTAINER_CLASS (parent_class)->add (container, object);

  g_object_unref (object);
}

static void
gimp_item_stack_remove (GimpContainer *container,
                        GimpObject    *object)
{
  GIMP_CONTAINER_CLASS (parent_class)->remove (container, object);
}


/*  public functions  */

GimpContainer *
gimp_item_stack_new (GType item_type)
{
  g_return_val_if_fail (g_type_is_a (item_type, GIMP_TYPE_ITEM), NULL);

  return g_object_new (GIMP_TYPE_ITEM_STACK,
                       "name",          g_type_name (item_type),
                       "children-type", item_type,
                       "policy",        GIMP_CONTAINER_POLICY_STRONG,
                       NULL);
}
112

113 114 115 116 117 118 119 120
gint
gimp_item_stack_get_n_items (GimpItemStack *stack)
{
  GList *list;
  gint   n_items = 0;

  g_return_val_if_fail (GIMP_IS_ITEM_STACK (stack), 0);

121
  for (list = GIMP_LIST (stack)->queue->head; list; list = g_list_next (list))
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    {
      GimpItem      *item = list->data;
      GimpContainer *children;

      n_items++;

      children = gimp_viewable_get_children (GIMP_VIEWABLE (item));

      if (children)
        n_items += gimp_item_stack_get_n_items (GIMP_ITEM_STACK (children));
    }

  return n_items;
}

137 138 139 140 141 142 143
gboolean
gimp_item_stack_is_flat (GimpItemStack *stack)
{
  GList *list;

  g_return_val_if_fail (GIMP_IS_ITEM_STACK (stack), TRUE);

144
  for (list = GIMP_LIST (stack)->queue->head; list; list = g_list_next (list))
145 146 147 148 149 150 151 152 153 154
    {
      GimpViewable *viewable = list->data;

      if (gimp_viewable_get_children (viewable))
        return FALSE;
    }

  return TRUE;
}

155 156 157 158 159
GList *
gimp_item_stack_get_item_iter (GimpItemStack *stack)
{
  g_return_val_if_fail (GIMP_IS_ITEM_STACK (stack), NULL);

160
  return GIMP_LIST (stack)->queue->head;
161 162
}

163 164 165 166 167 168 169 170
GList *
gimp_item_stack_get_item_list (GimpItemStack *stack)
{
  GList *list;
  GList *result = NULL;

  g_return_val_if_fail (GIMP_IS_ITEM_STACK (stack), NULL);

171
  for (list = GIMP_LIST (stack)->queue->head;
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
       list;
       list = g_list_next (list))
    {
      GimpViewable  *viewable = list->data;
      GimpContainer *children;

      result = g_list_prepend (result, viewable);

      children = gimp_viewable_get_children (viewable);

      if (children)
        {
          GList *child_list;

          child_list = gimp_item_stack_get_item_list (GIMP_ITEM_STACK (children));

          while (child_list)
            {
              result = g_list_prepend (result, child_list->data);

              child_list = g_list_remove (child_list, child_list->data);
            }
        }
    }

  return g_list_reverse (result);
}

200 201 202 203 204 205 206 207
GimpItem *
gimp_item_stack_get_item_by_tattoo (GimpItemStack *stack,
                                    GimpTattoo     tattoo)
{
  GList *list;

  g_return_val_if_fail (GIMP_IS_ITEM_STACK (stack), NULL);

208
  for (list = GIMP_LIST (stack)->queue->head; list; list = g_list_next (list))
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    {
      GimpItem      *item = list->data;
      GimpContainer *children;

      if (gimp_item_get_tattoo (item) == tattoo)
        return item;

      children = gimp_viewable_get_children (GIMP_VIEWABLE (item));

      if (children)
        {
          item = gimp_item_stack_get_item_by_tattoo (GIMP_ITEM_STACK (children),
                                                     tattoo);

          if (item)
            return item;
        }
    }

  return NULL;
}

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
GimpItem *
gimp_item_stack_get_item_by_path (GimpItemStack *stack,
                                  GList         *path)
{
  GimpContainer *container;
  GimpItem      *item = NULL;

  g_return_val_if_fail (GIMP_IS_ITEM_STACK (stack), NULL);
  g_return_val_if_fail (path != NULL, NULL);

  container = GIMP_CONTAINER (stack);

  while (path)
    {
      guint32 i = GPOINTER_TO_UINT (path->data);

      item = GIMP_ITEM (gimp_container_get_child_by_index (container, i));

      g_return_val_if_fail (GIMP_IS_ITEM (item), item);

      if (path->next)
        {
          container = gimp_viewable_get_children (GIMP_VIEWABLE (item));

          g_return_val_if_fail (GIMP_IS_ITEM_STACK (container), item);
        }

      path = path->next;
    }

  return item;
}

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
GimpItem *
gimp_item_stack_get_parent_by_path (GimpItemStack *stack,
                                    GList         *path,
                                    gint          *index)
{
  GimpItem *parent = NULL;
  guint32   i;

  g_return_val_if_fail (GIMP_IS_ITEM_STACK (stack), NULL);
  g_return_val_if_fail (path != NULL, NULL);

  i = GPOINTER_TO_UINT (path->data);

  if (index)
    *index = i;

  while (path->next)
    {
      GimpObject    *child;
      GimpContainer *children;

      child = gimp_container_get_child_by_index (GIMP_CONTAINER (stack), i);

      g_return_val_if_fail (GIMP_IS_ITEM (child), parent);

      children = gimp_viewable_get_children (GIMP_VIEWABLE (child));

      g_return_val_if_fail (GIMP_IS_ITEM_STACK (children), parent);

      parent = GIMP_ITEM (child);
      stack  = GIMP_ITEM_STACK (children);

      path = path->next;

      i = GPOINTER_TO_UINT (path->data);

      if (index)
        *index = i;
    }

  return parent;
}

307
static void
308
gimp_item_stack_viewable_invalidate_previews (GimpViewable *viewable)
309 310 311 312 313 314 315 316 317
{
  GimpContainer *children = gimp_viewable_get_children (viewable);

  if (children)
    gimp_item_stack_invalidate_previews (GIMP_ITEM_STACK (children));

  gimp_viewable_invalidate_preview (viewable);
}

318 319 320 321 322 323
void
gimp_item_stack_invalidate_previews (GimpItemStack *stack)
{
  g_return_if_fail (GIMP_IS_ITEM_STACK (stack));

  gimp_container_foreach (GIMP_CONTAINER (stack),
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
                          (GFunc) gimp_item_stack_viewable_invalidate_previews,
                          NULL);
}

static void
gimp_item_stack_viewable_profile_changed (GimpViewable *viewable)
{
  GimpContainer *children = gimp_viewable_get_children (viewable);

  if (children)
    gimp_item_stack_profile_changed (GIMP_ITEM_STACK (children));

  if (GIMP_IS_COLOR_MANAGED (viewable))
    gimp_color_managed_profile_changed (GIMP_COLOR_MANAGED (viewable));
}

void
gimp_item_stack_profile_changed (GimpItemStack *stack)
{
  g_return_if_fail (GIMP_IS_ITEM_STACK (stack));

  gimp_container_foreach (GIMP_CONTAINER (stack),
                          (GFunc) gimp_item_stack_viewable_profile_changed,
347 348
                          NULL);
}