~ubuntu-branches/ubuntu/vivid/gimp/vivid

« back to all changes in this revision

Viewing changes to app/core/gimpitemstack.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach
  • Date: 2012-05-08 18:50:03 UTC
  • mto: (1.1.26) (0.5.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 71.
  • Revision ID: package-import@ubuntu.com-20120508185003-tltkvbaysf8d2426
ImportĀ upstreamĀ versionĀ 2.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GIMP - The GNU Image Manipulation Program
 
2
 * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * gimpitemstack.c
 
5
 * Copyright (C) 2008 Michael Natterer <mitch@gimp.org>
 
6
 *
 
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
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
 
 
23
#include <string.h>
 
24
 
 
25
#include <gegl.h>
 
26
 
 
27
#include "core-types.h"
 
28
 
 
29
#include "gimpitem.h"
 
30
#include "gimpitemstack.h"
 
31
 
 
32
 
 
33
/*  local function prototypes  */
 
34
 
 
35
static void   gimp_item_stack_constructed (GObject       *object);
 
36
 
 
37
static void   gimp_item_stack_add         (GimpContainer *container,
 
38
                                           GimpObject    *object);
 
39
static void   gimp_item_stack_remove      (GimpContainer *container,
 
40
                                           GimpObject    *object);
 
41
 
 
42
 
 
43
G_DEFINE_TYPE (GimpItemStack, gimp_item_stack, GIMP_TYPE_LIST)
 
44
 
 
45
#define parent_class gimp_item_stack_parent_class
 
46
 
 
47
 
 
48
static void
 
49
gimp_item_stack_class_init (GimpItemStackClass *klass)
 
50
{
 
51
  GObjectClass       *object_class    = G_OBJECT_CLASS (klass);
 
52
  GimpContainerClass *container_class = GIMP_CONTAINER_CLASS (klass);
 
53
 
 
54
  object_class->constructed = gimp_item_stack_constructed;
 
55
 
 
56
  container_class->add      = gimp_item_stack_add;
 
57
  container_class->remove   = gimp_item_stack_remove;
 
58
}
 
59
 
 
60
static void
 
61
gimp_item_stack_init (GimpItemStack *stack)
 
62
{
 
63
}
 
64
 
 
65
static void
 
66
gimp_item_stack_constructed (GObject *object)
 
67
{
 
68
  GimpContainer *container = GIMP_CONTAINER (object);
 
69
 
 
70
  if (G_OBJECT_CLASS (parent_class)->constructed)
 
71
    G_OBJECT_CLASS (parent_class)->constructed (object);
 
72
 
 
73
  g_assert (g_type_is_a (gimp_container_get_children_type (container),
 
74
                         GIMP_TYPE_ITEM));
 
75
}
 
76
 
 
77
static void
 
78
gimp_item_stack_add (GimpContainer *container,
 
79
                     GimpObject    *object)
 
80
{
 
81
  g_object_ref_sink (object);
 
82
 
 
83
  GIMP_CONTAINER_CLASS (parent_class)->add (container, object);
 
84
 
 
85
  g_object_unref (object);
 
86
}
 
87
 
 
88
static void
 
89
gimp_item_stack_remove (GimpContainer *container,
 
90
                        GimpObject    *object)
 
91
{
 
92
  GIMP_CONTAINER_CLASS (parent_class)->remove (container, object);
 
93
}
 
94
 
 
95
 
 
96
/*  public functions  */
 
97
 
 
98
GimpContainer *
 
99
gimp_item_stack_new (GType item_type)
 
100
{
 
101
  g_return_val_if_fail (g_type_is_a (item_type, GIMP_TYPE_ITEM), NULL);
 
102
 
 
103
  return g_object_new (GIMP_TYPE_ITEM_STACK,
 
104
                       "name",          g_type_name (item_type),
 
105
                       "children-type", item_type,
 
106
                       "policy",        GIMP_CONTAINER_POLICY_STRONG,
 
107
                       NULL);
 
108
}
 
109
 
 
110
gint
 
111
gimp_item_stack_get_n_items (GimpItemStack *stack)
 
112
{
 
113
  GList *list;
 
114
  gint   n_items = 0;
 
115
 
 
116
  g_return_val_if_fail (GIMP_IS_ITEM_STACK (stack), 0);
 
117
 
 
118
  for (list = GIMP_LIST (stack)->list; list; list = g_list_next (list))
 
119
    {
 
120
      GimpItem      *item = list->data;
 
121
      GimpContainer *children;
 
122
 
 
123
      n_items++;
 
124
 
 
125
      children = gimp_viewable_get_children (GIMP_VIEWABLE (item));
 
126
 
 
127
      if (children)
 
128
        n_items += gimp_item_stack_get_n_items (GIMP_ITEM_STACK (children));
 
129
    }
 
130
 
 
131
  return n_items;
 
132
}
 
133
 
 
134
gboolean
 
135
gimp_item_stack_is_flat (GimpItemStack *stack)
 
136
{
 
137
  GList *list;
 
138
 
 
139
  g_return_val_if_fail (GIMP_IS_ITEM_STACK (stack), TRUE);
 
140
 
 
141
  for (list = GIMP_LIST (stack)->list; list; list = g_list_next (list))
 
142
    {
 
143
      GimpViewable *viewable = list->data;
 
144
 
 
145
      if (gimp_viewable_get_children (viewable))
 
146
        return FALSE;
 
147
    }
 
148
 
 
149
  return TRUE;
 
150
}
 
151
 
 
152
GList *
 
153
gimp_item_stack_get_item_iter (GimpItemStack *stack)
 
154
{
 
155
  g_return_val_if_fail (GIMP_IS_ITEM_STACK (stack), NULL);
 
156
 
 
157
  return GIMP_LIST (stack)->list;
 
158
}
 
159
 
 
160
GList *
 
161
gimp_item_stack_get_item_list (GimpItemStack *stack)
 
162
{
 
163
  GList *list;
 
164
  GList *result = NULL;
 
165
 
 
166
  g_return_val_if_fail (GIMP_IS_ITEM_STACK (stack), NULL);
 
167
 
 
168
  for (list = GIMP_LIST (stack)->list;
 
169
       list;
 
170
       list = g_list_next (list))
 
171
    {
 
172
      GimpViewable  *viewable = list->data;
 
173
      GimpContainer *children;
 
174
 
 
175
      result = g_list_prepend (result, viewable);
 
176
 
 
177
      children = gimp_viewable_get_children (viewable);
 
178
 
 
179
      if (children)
 
180
        {
 
181
          GList *child_list;
 
182
 
 
183
          child_list = gimp_item_stack_get_item_list (GIMP_ITEM_STACK (children));
 
184
 
 
185
          while (child_list)
 
186
            {
 
187
              result = g_list_prepend (result, child_list->data);
 
188
 
 
189
              child_list = g_list_remove (child_list, child_list->data);
 
190
            }
 
191
        }
 
192
    }
 
193
 
 
194
  return g_list_reverse (result);
 
195
}
 
196
 
 
197
GimpItem *
 
198
gimp_item_stack_get_item_by_tattoo (GimpItemStack *stack,
 
199
                                    GimpTattoo     tattoo)
 
200
{
 
201
  GList *list;
 
202
 
 
203
  g_return_val_if_fail (GIMP_IS_ITEM_STACK (stack), NULL);
 
204
 
 
205
  for (list = GIMP_LIST (stack)->list; list; list = g_list_next (list))
 
206
    {
 
207
      GimpItem      *item = list->data;
 
208
      GimpContainer *children;
 
209
 
 
210
      if (gimp_item_get_tattoo (item) == tattoo)
 
211
        return item;
 
212
 
 
213
      children = gimp_viewable_get_children (GIMP_VIEWABLE (item));
 
214
 
 
215
      if (children)
 
216
        {
 
217
          item = gimp_item_stack_get_item_by_tattoo (GIMP_ITEM_STACK (children),
 
218
                                                     tattoo);
 
219
 
 
220
          if (item)
 
221
            return item;
 
222
        }
 
223
    }
 
224
 
 
225
  return NULL;
 
226
}
 
227
 
 
228
GimpItem *
 
229
gimp_item_stack_get_item_by_path (GimpItemStack *stack,
 
230
                                  GList         *path)
 
231
{
 
232
  GimpContainer *container;
 
233
  GimpItem      *item = NULL;
 
234
 
 
235
  g_return_val_if_fail (GIMP_IS_ITEM_STACK (stack), NULL);
 
236
  g_return_val_if_fail (path != NULL, NULL);
 
237
 
 
238
  container = GIMP_CONTAINER (stack);
 
239
 
 
240
  while (path)
 
241
    {
 
242
      guint32 i = GPOINTER_TO_UINT (path->data);
 
243
 
 
244
      item = GIMP_ITEM (gimp_container_get_child_by_index (container, i));
 
245
 
 
246
      g_return_val_if_fail (GIMP_IS_ITEM (item), item);
 
247
 
 
248
      if (path->next)
 
249
        {
 
250
          container = gimp_viewable_get_children (GIMP_VIEWABLE (item));
 
251
 
 
252
          g_return_val_if_fail (GIMP_IS_ITEM_STACK (container), item);
 
253
        }
 
254
 
 
255
      path = path->next;
 
256
    }
 
257
 
 
258
  return item;
 
259
}
 
260
 
 
261
GimpItem *
 
262
gimp_item_stack_get_parent_by_path (GimpItemStack *stack,
 
263
                                    GList         *path,
 
264
                                    gint          *index)
 
265
{
 
266
  GimpItem *parent = NULL;
 
267
  guint32   i;
 
268
 
 
269
  g_return_val_if_fail (GIMP_IS_ITEM_STACK (stack), NULL);
 
270
  g_return_val_if_fail (path != NULL, NULL);
 
271
 
 
272
  i = GPOINTER_TO_UINT (path->data);
 
273
 
 
274
  if (index)
 
275
    *index = i;
 
276
 
 
277
  while (path->next)
 
278
    {
 
279
      GimpObject    *child;
 
280
      GimpContainer *children;
 
281
 
 
282
      child = gimp_container_get_child_by_index (GIMP_CONTAINER (stack), i);
 
283
 
 
284
      g_return_val_if_fail (GIMP_IS_ITEM (child), parent);
 
285
 
 
286
      children = gimp_viewable_get_children (GIMP_VIEWABLE (child));
 
287
 
 
288
      g_return_val_if_fail (GIMP_IS_ITEM_STACK (children), parent);
 
289
 
 
290
      parent = GIMP_ITEM (child);
 
291
      stack  = GIMP_ITEM_STACK (children);
 
292
 
 
293
      path = path->next;
 
294
 
 
295
      i = GPOINTER_TO_UINT (path->data);
 
296
 
 
297
      if (index)
 
298
        *index = i;
 
299
    }
 
300
 
 
301
  return parent;
 
302
}
 
303
 
 
304
static void
 
305
gimp_item_stack_invalidate_preview (GimpViewable *viewable)
 
306
{
 
307
  GimpContainer *children = gimp_viewable_get_children (viewable);
 
308
 
 
309
  if (children)
 
310
    gimp_item_stack_invalidate_previews (GIMP_ITEM_STACK (children));
 
311
 
 
312
  gimp_viewable_invalidate_preview (viewable);
 
313
}
 
314
 
 
315
void
 
316
gimp_item_stack_invalidate_previews (GimpItemStack *stack)
 
317
{
 
318
  g_return_if_fail (GIMP_IS_ITEM_STACK (stack));
 
319
 
 
320
  gimp_container_foreach (GIMP_CONTAINER (stack),
 
321
                          (GFunc) gimp_item_stack_invalidate_preview,
 
322
                          NULL);
 
323
}