summaryrefslogtreecommitdiffstats
path: root/app/core/gimplayerstack.c
blob: 1540189252a0fd2dc1a917737eaabaa0826dc11c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
 *
 * gimplayerstack.c
 * Copyright (C) 2017 Ell
 *
 * 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 <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "core-types.h"

#include "gimplayer.h"
#include "gimplayerstack.h"


/*  local function prototypes  */

static void   gimp_layer_stack_constructed             (GObject       *object);

static void   gimp_layer_stack_add                     (GimpContainer *container,
                                                        GimpObject    *object);
static void   gimp_layer_stack_remove                  (GimpContainer *container,
                                                        GimpObject    *object);
static void   gimp_layer_stack_reorder                 (GimpContainer *container,
                                                        GimpObject    *object,
                                                        gint           new_index);

static void   gimp_layer_stack_layer_active            (GimpLayer      *layer,
                                                        GimpLayerStack *stack);
static void   gimp_layer_stack_layer_excludes_backdrop (GimpLayer      *layer,
                                                        GimpLayerStack *stack);

static void   gimp_layer_stack_update_backdrop         (GimpLayerStack *stack,
                                                        GimpLayer      *layer,
                                                        gboolean        ignore_active,
                                                        gboolean        ignore_excludes_backdrop);
static void   gimp_layer_stack_update_range            (GimpLayerStack *stack,
                                                        gint            first,
                                                        gint            last);


G_DEFINE_TYPE (GimpLayerStack, gimp_layer_stack, GIMP_TYPE_DRAWABLE_STACK)

#define parent_class gimp_layer_stack_parent_class


static void
gimp_layer_stack_class_init (GimpLayerStackClass *klass)
{
  GObjectClass       *object_class    = G_OBJECT_CLASS (klass);
  GimpContainerClass *container_class = GIMP_CONTAINER_CLASS (klass);

  object_class->constructed = gimp_layer_stack_constructed;

  container_class->add      = gimp_layer_stack_add;
  container_class->remove   = gimp_layer_stack_remove;
  container_class->reorder  = gimp_layer_stack_reorder;
}

static void
gimp_layer_stack_init (GimpLayerStack *stack)
{
}

static void
gimp_layer_stack_constructed (GObject *object)
{
  GimpContainer *container = GIMP_CONTAINER (object);

  G_OBJECT_CLASS (parent_class)->constructed (object);

  gimp_assert (g_type_is_a (gimp_container_get_children_type (container),
                            GIMP_TYPE_LAYER));

  gimp_container_add_handler (container, "active-changed",
                              G_CALLBACK (gimp_layer_stack_layer_active),
                              container);
  gimp_container_add_handler (container, "excludes-backdrop-changed",
                              G_CALLBACK (gimp_layer_stack_layer_excludes_backdrop),
                              container);
}

static void
gimp_layer_stack_add (GimpContainer *container,
                      GimpObject    *object)
{
  GimpLayerStack *stack = GIMP_LAYER_STACK (container);

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

  gimp_layer_stack_update_backdrop (stack, GIMP_LAYER (object), FALSE, FALSE);
}

static void
gimp_layer_stack_remove (GimpContainer *container,
                         GimpObject    *object)
{
  GimpLayerStack *stack = GIMP_LAYER_STACK (container);
  gboolean        update_backdrop;
  gint            index;

  update_backdrop = gimp_filter_get_active (GIMP_FILTER (object)) &&
                    gimp_layer_get_excludes_backdrop (GIMP_LAYER (object));

  if (update_backdrop)
    index = gimp_container_get_child_index (container, object);

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

  if (update_backdrop)
    gimp_layer_stack_update_range (stack, index, -1);
}

static void
gimp_layer_stack_reorder (GimpContainer *container,
                          GimpObject    *object,
                          gint           new_index)
{
  GimpLayerStack *stack = GIMP_LAYER_STACK (container);
  gboolean        update_backdrop;
  gint            index;

  update_backdrop = gimp_filter_get_active (GIMP_FILTER (object)) &&
                    gimp_layer_get_excludes_backdrop (GIMP_LAYER (object));

  if (update_backdrop)
    index = gimp_container_get_child_index (container, object);

  GIMP_CONTAINER_CLASS (parent_class)->reorder (container, object, new_index);

  if (update_backdrop)
    gimp_layer_stack_update_range (stack, index, new_index);
}


/*  public functions  */

GimpContainer *
gimp_layer_stack_new (GType layer_type)
{
  g_return_val_if_fail (g_type_is_a (layer_type, GIMP_TYPE_LAYER), NULL);

  return g_object_new (GIMP_TYPE_LAYER_STACK,
                       "name",          g_type_name (layer_type),
                       "children-type", layer_type,
                       "policy",        GIMP_CONTAINER_POLICY_STRONG,
                       NULL);
}


/*  private functions  */

static void
gimp_layer_stack_layer_active (GimpLayer      *layer,
                               GimpLayerStack *stack)
{
  gimp_layer_stack_update_backdrop (stack, layer, TRUE, FALSE);
}

static void
gimp_layer_stack_layer_excludes_backdrop (GimpLayer      *layer,
                                          GimpLayerStack *stack)
{
  gimp_layer_stack_update_backdrop (stack, layer, FALSE, TRUE);
}

static void
gimp_layer_stack_update_backdrop (GimpLayerStack *stack,
                                  GimpLayer      *layer,
                                  gboolean        ignore_active,
                                  gboolean        ignore_excludes_backdrop)
{
  if ((ignore_active            || gimp_filter_get_active (GIMP_FILTER (layer))) &&
      (ignore_excludes_backdrop || gimp_layer_get_excludes_backdrop (layer)))
    {
      gint index;

      index = gimp_container_get_child_index (GIMP_CONTAINER (stack),
                                              GIMP_OBJECT (layer));

      gimp_layer_stack_update_range (stack, index + 1, -1);
    }
}

static void
gimp_layer_stack_update_range (GimpLayerStack *stack,
                               gint            first,
                               gint            last)
{
  GList *iter;

  g_return_if_fail (first >= 0 && last >= -1);

  /* if the range is reversed, flip first and last; note that last == -1 is
   * used to update all layers from first onward.
   */
  if (last >= 0 && last < first)
    {
      gint temp = first;

      first = last + 1;
      last  = temp + 1;
    }

  iter = gimp_item_stack_get_item_iter (GIMP_ITEM_STACK (stack));

  for (iter = g_list_nth (iter, first);
       iter && first != last;
       iter = g_list_next (iter), first++)
    {
      GimpItem *item = iter->data;

      if (gimp_filter_get_active (GIMP_FILTER (item)))
        {
          GeglRectangle bounding_box;

          bounding_box = gimp_drawable_get_bounding_box (GIMP_DRAWABLE (item));

          bounding_box.x += gimp_item_get_offset_x (item);
          bounding_box.y += gimp_item_get_offset_y (item);

          gimp_drawable_stack_update (GIMP_DRAWABLE_STACK (stack),
                                      bounding_box.x,     bounding_box.y,
                                      bounding_box.width, bounding_box.height);
        }
    }
}