/* 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 Spencer Kimball and Peter Mattis
 *
 * PikaOverlayBox
 * Copyright (C) 2009 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 <gtk/gtk.h>

#include "widgets-types.h"

#include "pikaoverlaybox.h"
#include "pikaoverlaychild.h"


/*  local function prototypes  */

static void        pika_overlay_box_set_property        (GObject        *object,
                                                         guint           property_id,
                                                         const GValue   *value,
                                                         GParamSpec     *pspec);
static void        pika_overlay_box_get_property        (GObject        *object,
                                                         guint           property_id,
                                                         GValue         *value,
                                                         GParamSpec     *pspec);

static void        pika_overlay_box_realize             (GtkWidget      *widget);
static void        pika_overlay_box_unrealize           (GtkWidget      *widget);
static void        pika_overlay_box_get_preferred_width (GtkWidget      *widget,
                                                         gint           *minimum_width,
                                                         gint           *natural_width);
static void        pika_overlay_box_get_preferred_height(GtkWidget      *widget,
                                                         gint           *minimum_height,
                                                         gint           *natural_height);
static void        pika_overlay_box_size_allocate       (GtkWidget      *widget,
                                                         GtkAllocation  *allocation);
static gboolean    pika_overlay_box_draw                (GtkWidget      *widget,
                                                         cairo_t        *cr);
static gboolean    pika_overlay_box_damage              (GtkWidget      *widget,
                                                         GdkEventExpose *event);

static void        pika_overlay_box_add                 (GtkContainer   *container,
                                                         GtkWidget      *widget);
static void        pika_overlay_box_remove              (GtkContainer   *container,
                                                         GtkWidget      *widget);
static void        pika_overlay_box_forall              (GtkContainer   *container,
                                                         gboolean        include_internals,
                                                         GtkCallback     callback,
                                                         gpointer        callback_data);
static GType       pika_overlay_box_child_type          (GtkContainer   *container);

static GdkWindow * pika_overlay_box_pick_embedded_child (GdkWindow      *window,
                                                         gdouble         x,
                                                         gdouble         y,
                                                         PikaOverlayBox *box);


G_DEFINE_TYPE (PikaOverlayBox, pika_overlay_box, GTK_TYPE_CONTAINER)

#define parent_class pika_overlay_box_parent_class


static void
pika_overlay_box_class_init (PikaOverlayBoxClass *klass)
{
  GObjectClass      *object_class    = G_OBJECT_CLASS (klass);
  GtkWidgetClass    *widget_class    = GTK_WIDGET_CLASS (klass);
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);

  object_class->set_property         = pika_overlay_box_set_property;
  object_class->get_property         = pika_overlay_box_get_property;

  widget_class->realize              = pika_overlay_box_realize;
  widget_class->unrealize            = pika_overlay_box_unrealize;
  widget_class->get_preferred_width  = pika_overlay_box_get_preferred_width;
  widget_class->get_preferred_height = pika_overlay_box_get_preferred_height;
  widget_class->size_allocate        = pika_overlay_box_size_allocate;
  widget_class->draw                 = pika_overlay_box_draw;

  g_signal_override_class_handler ("damage-event",
                                   PIKA_TYPE_OVERLAY_BOX,
                                   G_CALLBACK (pika_overlay_box_damage));

  container_class->add        = pika_overlay_box_add;
  container_class->remove     = pika_overlay_box_remove;
  container_class->forall     = pika_overlay_box_forall;
  container_class->child_type = pika_overlay_box_child_type;
}

static void
pika_overlay_box_init (PikaOverlayBox *box)
{
  gtk_widget_set_has_window (GTK_WIDGET (box), TRUE);
}

static void
pika_overlay_box_set_property (GObject      *object,
                               guint         property_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  switch (property_id)
    {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
pika_overlay_box_get_property (GObject    *object,
                               guint       property_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
  switch (property_id)
    {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
pika_overlay_box_realize (GtkWidget *widget)
{
  PikaOverlayBox *box = PIKA_OVERLAY_BOX (widget);
  GtkAllocation   allocation;
  GdkWindowAttr   attributes;
  gint            attributes_mask;
  GList          *list;

  gtk_widget_set_realized (widget, TRUE);

  gtk_widget_get_allocation (widget, &allocation);

  attributes.x           = allocation.x;
  attributes.y           = allocation.y;
  attributes.width       = allocation.width;
  attributes.height      = allocation.height;
  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.wclass      = GDK_INPUT_OUTPUT;
  attributes.visual      = gtk_widget_get_visual (widget);
  attributes.event_mask  = gtk_widget_get_events (widget);

  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;

  gtk_widget_set_window (widget,
                         gdk_window_new (gtk_widget_get_parent_window (widget),
                                         &attributes, attributes_mask));
  gdk_window_set_user_data (gtk_widget_get_window (widget), widget);

  g_signal_connect (gtk_widget_get_window (widget), "pick-embedded-child",
                    G_CALLBACK (pika_overlay_box_pick_embedded_child),
                    widget);

  for (list = box->children; list; list = g_list_next (list))
    pika_overlay_child_realize (box, list->data);
}

static void
pika_overlay_box_unrealize (GtkWidget *widget)
{
  PikaOverlayBox *box = PIKA_OVERLAY_BOX (widget);
  GList          *list;

  for (list = box->children; list; list = g_list_next (list))
    pika_overlay_child_unrealize (box, list->data);

  GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
}

static void
pika_overlay_box_get_preferred_width (GtkWidget *widget,
                                      gint      *minimum_width,
                                      gint      *natural_width)
{
  PikaOverlayBox *box = PIKA_OVERLAY_BOX (widget);
  GList          *list;

  *minimum_width = *natural_width = 0;

  for (list = box->children; list; list = g_list_next (list))
    {
      gint minimum;
      gint natural;

      pika_overlay_child_get_preferred_width (box, list->data,
                                              &minimum, &natural);

      *minimum_width = MAX (*minimum_width, minimum);
      *natural_width = MAX (*natural_width, natural);
    }

  *minimum_width = 0;

  *minimum_width +=
    2 * gtk_container_get_border_width (GTK_CONTAINER (widget)) + 1;

  *natural_width +=
    2 * gtk_container_get_border_width (GTK_CONTAINER (widget)) + 1;
}

static void
pika_overlay_box_get_preferred_height (GtkWidget *widget,
                                       gint      *minimum_height,
                                       gint      *natural_height)
{
  PikaOverlayBox *box = PIKA_OVERLAY_BOX (widget);
  GList          *list;

  *minimum_height = *natural_height = 0;

  for (list = box->children; list; list = g_list_next (list))
    {
      gint minimum;
      gint natural;

      pika_overlay_child_get_preferred_height (box, list->data,
                                               &minimum, &natural);

      *minimum_height = MAX (*minimum_height, minimum);
      *natural_height = MAX (*natural_height, natural);
    }

  *minimum_height = 0;

  *minimum_height +=
    2 * gtk_container_get_border_width (GTK_CONTAINER (widget)) + 1;

  *natural_height +=
    2 * gtk_container_get_border_width (GTK_CONTAINER (widget)) + 1;
}

static void
pika_overlay_box_size_allocate (GtkWidget     *widget,
                                GtkAllocation *allocation)
{
  PikaOverlayBox *box = PIKA_OVERLAY_BOX (widget);
  GList          *list;

  GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);

  for (list = box->children; list; list = g_list_next (list))
    pika_overlay_child_size_allocate (box, list->data);
}

static gboolean
pika_overlay_box_draw (GtkWidget *widget,
                       cairo_t   *cr)
{
  PikaOverlayBox *box = PIKA_OVERLAY_BOX (widget);
  GList          *list;

  for (list = box->children; list; list = g_list_next (list))
    {
      if (pika_overlay_child_draw (box, list->data, cr))
        return FALSE;
    }

  return FALSE;
}

static gboolean
pika_overlay_box_damage (GtkWidget      *widget,
                         GdkEventExpose *event)
{
  PikaOverlayBox *box = PIKA_OVERLAY_BOX (widget);
  GList          *list;

  for (list = box->children; list; list = g_list_next (list))
    {
      if (pika_overlay_child_damage (box, list->data, event))
        return FALSE;
    }

  return FALSE;
}

static void
pika_overlay_box_add (GtkContainer *container,
                      GtkWidget    *widget)
{
  pika_overlay_box_add_child (PIKA_OVERLAY_BOX (container), widget, 0.5, 0.5);
}

static void
pika_overlay_box_remove (GtkContainer *container,
                         GtkWidget    *widget)
{
  PikaOverlayBox   *box   = PIKA_OVERLAY_BOX (container);
  PikaOverlayChild *child = pika_overlay_child_find (box, widget);

  if (child)
    {
      if (gtk_widget_get_visible (widget))
        pika_overlay_child_invalidate (box, child);

      box->children = g_list_remove (box->children, child);

      pika_overlay_child_free (box, child);
    }
}

static void
pika_overlay_box_forall (GtkContainer *container,
                         gboolean      include_internals,
                         GtkCallback   callback,
                         gpointer      callback_data)
{
  PikaOverlayBox *box = PIKA_OVERLAY_BOX (container);
  GList          *list;

  list = box->children;
  while (list)
    {
      PikaOverlayChild *child = list->data;

      list = list->next;

      (* callback) (child->widget, callback_data);
    }
}

static GType
pika_overlay_box_child_type (GtkContainer *container)
{
  return GTK_TYPE_WIDGET;
}

static GdkWindow *
pika_overlay_box_pick_embedded_child (GdkWindow      *parent,
                                      gdouble         parent_x,
                                      gdouble         parent_y,
                                      PikaOverlayBox *box)
{
  GList *list;

  for (list = box->children; list; list = g_list_next (list))
    {
      PikaOverlayChild *child = list->data;

      if (pika_overlay_child_pick (box, child, parent_x, parent_y))
        return child->window;
    }

  return NULL;
}


/*  public functions  */

/**
 * pika_overlay_box_new:
 *
 * Creates a new #PikaOverlayBox widget.
 *
 * Returns: a new #PikaOverlayBox widget
 **/
GtkWidget *
pika_overlay_box_new (void)
{
  return g_object_new (PIKA_TYPE_OVERLAY_BOX, NULL);
}

void
pika_overlay_box_add_child (PikaOverlayBox *box,
                            GtkWidget      *widget,
                            gdouble         xalign,
                            gdouble         yalign)
{
  PikaOverlayChild *child;

  g_return_if_fail (PIKA_IS_OVERLAY_BOX (box));
  g_return_if_fail (GTK_IS_WIDGET (widget));

  child = pika_overlay_child_new (box, widget, xalign, yalign, 0.0, 0.85);

  box->children = g_list_append (box->children, child);
}

void
pika_overlay_box_set_child_alignment (PikaOverlayBox *box,
                                      GtkWidget      *widget,
                                      gdouble         xalign,
                                      gdouble         yalign)
{
  PikaOverlayChild *child = pika_overlay_child_find (box, widget);

  if (child)
    {
      xalign = CLAMP (xalign, 0.0, 1.0);
      yalign = CLAMP (yalign, 0.0, 1.0);

      if (child->has_position     ||
          child->xalign != xalign ||
          child->yalign != yalign)
        {
          pika_overlay_child_invalidate (box, child);

          child->has_position = FALSE;
          child->xalign       = xalign;
          child->yalign       = yalign;

          gtk_widget_queue_resize (widget);
        }
    }
}

void
pika_overlay_box_set_child_position (PikaOverlayBox *box,
                                     GtkWidget      *widget,
                                     gdouble         x,
                                     gdouble         y)
{
  PikaOverlayChild *child = pika_overlay_child_find (box, widget);

  if (child)
    {
      if (! child->has_position ||
          child->x != x         ||
          child->y != y)
        {
          pika_overlay_child_invalidate (box, child);

          child->has_position = TRUE;
          child->x            = x;
          child->y            = y;

          gtk_widget_queue_resize (widget);
        }
    }
}

void
pika_overlay_box_set_child_angle (PikaOverlayBox *box,
                                  GtkWidget      *widget,
                                  gdouble         angle)
{
  PikaOverlayChild *child = pika_overlay_child_find (box, widget);

  if (child)
    {
      if (child->angle != angle)
        {
          pika_overlay_child_invalidate (box, child);

          child->angle = angle;

          gtk_widget_queue_draw (widget);
        }
    }
}

void
pika_overlay_box_set_child_opacity (PikaOverlayBox *box,
                                    GtkWidget      *widget,
                                    gdouble         opacity)
{
  PikaOverlayChild *child = pika_overlay_child_find (box, widget);

  if (child)
    {
      opacity = CLAMP (opacity, 0.0, 1.0);

      if (child->opacity != opacity)
        {
          child->opacity = opacity;

          gtk_widget_queue_draw (widget);
        }
    }
}

/**
 * pika_overlay_box_scroll:
 * @box: the #PikaOverlayBox widget to scroll.
 * @offset_x: the x scroll amount.
 * @offset_y: the y scroll amount.
 *
 * Scrolls the box using gdk_window_scroll(), taking care of properly
 * handling overlay children.
 **/
void
pika_overlay_box_scroll (PikaOverlayBox *box,
                         gint            offset_x,
                         gint            offset_y)
{
  GtkWidget *widget;
  GdkWindow *window;
  GList     *list;

  g_return_if_fail (PIKA_IS_OVERLAY_BOX (box));

  widget = GTK_WIDGET (box);

  /* bug 761118 */
  if (! gtk_widget_get_realized (widget))
    return;

  window = gtk_widget_get_window (widget);

  /*  Undraw all overlays  */
  for (list = box->children; list; list = g_list_next (list))
    {
      PikaOverlayChild *child = list->data;

      pika_overlay_child_invalidate (box, child);
    }

  gdk_window_scroll (window, offset_x, offset_y);

  /*  Re-draw all overlays  */
  for (list = box->children; list; list = g_list_next (list))
    {
      PikaOverlayChild *child = list->data;

      pika_overlay_child_invalidate (box, child);
    }
}
