/* PIKA - Photo and Image Kooker Application
 * a rebranding of The GNU Image Manipulation Program (created with heckimp)
 * A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
 *
 * Original copyright, applying to most contents (license remains unchanged): 
 * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
 *
 * pikadrawablestack.c
 * Copyright (C) 2008 Michael Natterer <mitch@gimp.org>
 *
 * 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 "pikadrawable.h"
#include "pikadrawablestack.h"
#include "pikamarshal.h"


enum
{
  UPDATE,
  LAST_SIGNAL
};


/*  local function prototypes  */

static void   pika_drawable_stack_constructed      (GObject           *object);

static void   pika_drawable_stack_add              (PikaContainer     *container,
                                                    PikaObject        *object);
static void   pika_drawable_stack_remove           (PikaContainer     *container,
                                                    PikaObject        *object);
static void   pika_drawable_stack_reorder          (PikaContainer     *container,
                                                    PikaObject        *object,
                                                    gint               new_index);

static void   pika_drawable_stack_drawable_update  (PikaItem          *item,
                                                    gint               x,
                                                    gint               y,
                                                    gint               width,
                                                    gint               height,
                                                    PikaDrawableStack *stack);
static void   pika_drawable_stack_drawable_active  (PikaItem          *item,
                                                    PikaDrawableStack *stack);


G_DEFINE_TYPE (PikaDrawableStack, pika_drawable_stack, PIKA_TYPE_ITEM_STACK)

#define parent_class pika_drawable_stack_parent_class

static guint stack_signals[LAST_SIGNAL] = { 0 };


static void
pika_drawable_stack_class_init (PikaDrawableStackClass *klass)
{
  GObjectClass       *object_class    = G_OBJECT_CLASS (klass);
  PikaContainerClass *container_class = PIKA_CONTAINER_CLASS (klass);

  stack_signals[UPDATE] =
    g_signal_new ("update",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (PikaDrawableStackClass, update),
                  NULL, NULL,
                  pika_marshal_VOID__INT_INT_INT_INT,
                  G_TYPE_NONE, 4,
                  G_TYPE_INT,
                  G_TYPE_INT,
                  G_TYPE_INT,
                  G_TYPE_INT);

  object_class->constructed = pika_drawable_stack_constructed;

  container_class->add      = pika_drawable_stack_add;
  container_class->remove   = pika_drawable_stack_remove;
  container_class->reorder  = pika_drawable_stack_reorder;
}

static void
pika_drawable_stack_init (PikaDrawableStack *stack)
{
}

static void
pika_drawable_stack_constructed (GObject *object)
{
  PikaContainer *container = PIKA_CONTAINER (object);

  G_OBJECT_CLASS (parent_class)->constructed (object);

  pika_assert (g_type_is_a (pika_container_get_children_type (container),
                            PIKA_TYPE_DRAWABLE));

  pika_container_add_handler (container, "update",
                              G_CALLBACK (pika_drawable_stack_drawable_update),
                              container);
  pika_container_add_handler (container, "active-changed",
                              G_CALLBACK (pika_drawable_stack_drawable_active),
                              container);
}

static void
pika_drawable_stack_add (PikaContainer *container,
                         PikaObject    *object)
{
  PikaDrawableStack *stack = PIKA_DRAWABLE_STACK (container);

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

  if (pika_filter_get_active (PIKA_FILTER (object)))
    pika_drawable_stack_drawable_active (PIKA_ITEM (object), stack);
}

static void
pika_drawable_stack_remove (PikaContainer *container,
                            PikaObject    *object)
{
  PikaDrawableStack *stack = PIKA_DRAWABLE_STACK (container);

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

  if (pika_filter_get_active (PIKA_FILTER (object)))
    pika_drawable_stack_drawable_active (PIKA_ITEM (object), stack);
}

static void
pika_drawable_stack_reorder (PikaContainer *container,
                             PikaObject    *object,
                             gint           new_index)
{
  PikaDrawableStack *stack  = PIKA_DRAWABLE_STACK (container);

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

  if (pika_filter_get_active (PIKA_FILTER (object)))
    pika_drawable_stack_drawable_active (PIKA_ITEM (object), stack);
}


/*  public functions  */

PikaContainer *
pika_drawable_stack_new (GType drawable_type)
{
  g_return_val_if_fail (g_type_is_a (drawable_type, PIKA_TYPE_DRAWABLE), NULL);

  return g_object_new (PIKA_TYPE_DRAWABLE_STACK,
                       "name",          g_type_name (drawable_type),
                       "children-type", drawable_type,
                       "policy",        PIKA_CONTAINER_POLICY_STRONG,
                       NULL);
}


/*  protected functions  */

void
pika_drawable_stack_update (PikaDrawableStack *stack,
                            gint               x,
                            gint               y,
                            gint               width,
                            gint               height)
{
  g_return_if_fail (PIKA_IS_DRAWABLE_STACK (stack));

  g_signal_emit (stack, stack_signals[UPDATE], 0,
                 x, y, width, height);
}


/*  private functions  */

static void
pika_drawable_stack_drawable_update (PikaItem          *item,
                                     gint               x,
                                     gint               y,
                                     gint               width,
                                     gint               height,
                                     PikaDrawableStack *stack)
{
  if (pika_filter_get_active (PIKA_FILTER (item)))
    {
      gint offset_x;
      gint offset_y;

      pika_item_get_offset (item, &offset_x, &offset_y);

      pika_drawable_stack_update (stack,
                                  x + offset_x, y + offset_y,
                                  width, height);
    }
}

static void
pika_drawable_stack_drawable_active (PikaItem          *item,
                                     PikaDrawableStack *stack)
{
  GeglRectangle bounding_box;

  bounding_box = pika_drawable_get_bounding_box (PIKA_DRAWABLE (item));

  bounding_box.x += pika_item_get_offset_x (item);
  bounding_box.y += pika_item_get_offset_y (item);

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