gimplayerstack.c 7.95 KB
Newer Older
Ell's avatar
Ell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
Ell's avatar
Ell committed
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
 */

#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);

44
static void   gimp_layer_stack_layer_active            (GimpLayer      *layer,
Ell's avatar
Ell committed
45 46 47 48 49 50
                                                        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,
51
                                                        gboolean        ignore_active,
Ell's avatar
Ell committed
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
                                                        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);

88 89
  gimp_assert (g_type_is_a (gimp_container_get_children_type (container),
                            GIMP_TYPE_LAYER));
Ell's avatar
Ell committed
90

91 92
  gimp_container_add_handler (container, "active-changed",
                              G_CALLBACK (gimp_layer_stack_layer_active),
Ell's avatar
Ell committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
                              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);
115 116
  gboolean        update_backdrop;
  gint            index;
Ell's avatar
Ell committed
117

118
  update_backdrop = gimp_filter_get_active (GIMP_FILTER (object)) &&
119 120 121 122
                    gimp_layer_get_excludes_backdrop (GIMP_LAYER (object));

  if (update_backdrop)
    index = gimp_container_get_child_index (container, object);
Ell's avatar
Ell committed
123 124

  GIMP_CONTAINER_CLASS (parent_class)->remove (container, object);
125 126 127

  if (update_backdrop)
    gimp_layer_stack_update_range (stack, index, -1);
Ell's avatar
Ell committed
128 129 130 131 132 133 134 135
}

static void
gimp_layer_stack_reorder (GimpContainer *container,
                          GimpObject    *object,
                          gint           new_index)
{
  GimpLayerStack *stack = GIMP_LAYER_STACK (container);
136
  gboolean        update_backdrop;
Ell's avatar
Ell committed
137 138
  gint            index;

139
  update_backdrop = gimp_filter_get_active (GIMP_FILTER (object)) &&
140
                    gimp_layer_get_excludes_backdrop (GIMP_LAYER (object));
Ell's avatar
Ell committed
141

142
  if (update_backdrop)
Ell's avatar
Ell committed
143 144 145 146
    index = gimp_container_get_child_index (container, object);

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

147
  if (update_backdrop)
Ell's avatar
Ell committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    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
170 171
gimp_layer_stack_layer_active (GimpLayer      *layer,
                               GimpLayerStack *stack)
Ell's avatar
Ell committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185
{
  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,
186
                                  gboolean        ignore_active,
Ell's avatar
Ell committed
187 188
                                  gboolean        ignore_excludes_backdrop)
{
189
  if ((ignore_active            || gimp_filter_get_active (GIMP_FILTER (layer))) &&
Ell's avatar
Ell committed
190 191 192 193 194 195 196
      (ignore_excludes_backdrop || gimp_layer_get_excludes_backdrop (layer)))
    {
      gint index;

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

197
      gimp_layer_stack_update_range (stack, index + 1, -1);
Ell's avatar
Ell committed
198 199 200 201 202 203 204 205 206 207
    }
}

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

208
  g_return_if_fail (first >= 0 && last >= -1);
Ell's avatar
Ell committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

  /* 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;

229
      if (gimp_filter_get_active (GIMP_FILTER (item)))
Ell's avatar
Ell committed
230 231 232 233 234 235 236 237 238 239 240 241 242
        {
          gint offset_x;
          gint offset_y;

          gimp_item_get_offset (item, &offset_x, &offset_y);

          gimp_drawable_stack_update (GIMP_DRAWABLE_STACK (stack),
                                      offset_x, offset_y,
                                      gimp_item_get_width  (item),
                                      gimp_item_get_height (item));
        }
    }
}