/* 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
 *
 * pikacontainerbox.c
 * Copyright (C) 2004 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 <string.h>

#include <gegl.h>
#include <gtk/gtk.h>

#include "libpikawidgets/pikawidgets.h"

#include "widgets-types.h"

#include "core/pikacontainer.h"
#include "core/pikacontext.h"

#include "pikacontainerbox.h"
#include "pikacontainerview.h"
#include "pikadnd.h"
#include "pikadocked.h"
#include "pikapropwidgets.h"
#include "pikaview.h"
#include "pikaviewrenderer.h"


static void   pika_container_box_view_iface_init   (PikaContainerViewInterface *iface);
static void   pika_container_box_docked_iface_init (PikaDockedInterface *iface);

static void   pika_container_box_constructed       (GObject      *object);

static GtkWidget * pika_container_box_get_preview  (PikaDocked   *docked,
                                                    PikaContext  *context,
                                                    GtkIconSize   size);
static void        pika_container_box_set_context  (PikaDocked   *docked,
                                                    PikaContext  *context);


G_DEFINE_TYPE_WITH_CODE (PikaContainerBox, pika_container_box,
                         PIKA_TYPE_EDITOR,
                         G_IMPLEMENT_INTERFACE (PIKA_TYPE_CONTAINER_VIEW,
                                                pika_container_box_view_iface_init)
                         G_IMPLEMENT_INTERFACE (PIKA_TYPE_DOCKED,
                                                pika_container_box_docked_iface_init))

#define parent_class pika_container_box_parent_class


static void
pika_container_box_class_init (PikaContainerBoxClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed  = pika_container_box_constructed;
  object_class->set_property = pika_container_view_set_property;
  object_class->get_property = pika_container_view_get_property;

  pika_container_view_install_properties (object_class);
}

static void
pika_container_box_init (PikaContainerBox *box)
{
  GtkWidget *sb;

  box->scrolled_win = gtk_scrolled_window_new (NULL, NULL);
  gtk_box_pack_start (GTK_BOX (box), box->scrolled_win, TRUE, TRUE, 0);
  gtk_widget_show (box->scrolled_win);

  sb = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (box->scrolled_win));

  gtk_widget_set_can_focus (sb, FALSE);
}

static void
pika_container_box_view_iface_init (PikaContainerViewInterface *iface)
{
}

static void
pika_container_box_docked_iface_init (PikaDockedInterface *iface)
{
  iface->get_preview = pika_container_box_get_preview;
  iface->set_context = pika_container_box_set_context;
}

static void
pika_container_box_constructed (GObject *object)
{
  PikaContainerBox *box = PIKA_CONTAINER_BOX (object);

  /* This is evil: the hash table of "insert_data" is created on
   * demand when PikaContainerView API is used, using a
   * value_free_func that is set in the interface_init functions of
   * its implementors. Therefore, no PikaContainerView API must be
   * called from any init() function, because the interface_init()
   * function of a subclass that sets the right value_free_func might
   * not have been called yet, leaving the insert_data hash table
   * without memory management.
   *
   * Call PikaContainerView API from GObject::constructed() instead,
   * which runs after everything is set up correctly.
   */
  pika_container_view_set_dnd_widget (PIKA_CONTAINER_VIEW (box),
                                      box->scrolled_win);

  G_OBJECT_CLASS (parent_class)->constructed (object);
}

void
pika_container_box_set_size_request (PikaContainerBox *box,
                                     gint              width,
                                     gint              height)
{
  PikaContainerView      *view;
  GtkScrolledWindowClass *sw_class;
  GtkStyleContext        *sw_style;
  GtkWidget              *sb;
  GtkRequisition          req;
  GtkBorder               border;
  gint                    view_size;
  gint                    scrollbar_width;
  gint                    border_x;
  gint                    border_y;

  g_return_if_fail (PIKA_IS_CONTAINER_BOX (box));

  view = PIKA_CONTAINER_VIEW (box);

  view_size = pika_container_view_get_view_size (view, NULL);

  g_return_if_fail (width  <= 0 || width  >= view_size);
  g_return_if_fail (height <= 0 || height >= view_size);

  sw_class = GTK_SCROLLED_WINDOW_GET_CLASS (box->scrolled_win);

  if (sw_class->scrollbar_spacing >= 0)
    scrollbar_width = sw_class->scrollbar_spacing;
  else
    gtk_widget_style_get (GTK_WIDGET (box->scrolled_win),
                          "scrollbar-spacing", &scrollbar_width,
                          NULL);

  sb = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (box->scrolled_win));

  gtk_widget_get_preferred_size (sb, &req, NULL);
  scrollbar_width += req.width;

  border_x = border_y = gtk_container_get_border_width (GTK_CONTAINER (box));

  sw_style = gtk_widget_get_style_context (box->scrolled_win);

  gtk_style_context_get_border (sw_style, gtk_widget_get_state_flags (box->scrolled_win), &border);

  border_x += border.left + border.right + scrollbar_width;
  border_y += border.top + border.bottom;

  gtk_widget_set_size_request (box->scrolled_win,
                               width  > 0 ? width  + border_x : -1,
                               height > 0 ? height + border_y : -1);
}

static void
pika_container_box_set_context (PikaDocked  *docked,
                                PikaContext *context)
{
  pika_container_view_set_context (PIKA_CONTAINER_VIEW (docked), context);
}

static GtkWidget *
pika_container_box_get_preview (PikaDocked   *docked,
                                PikaContext  *context,
                                GtkIconSize   size)
{
  PikaContainerView *view = PIKA_CONTAINER_VIEW (docked);
  PikaContainer     *container;
  GtkWidget         *preview;
  gint               width;
  gint               height;
  gint               border_width = 1;
  const gchar       *prop_name;

  container = pika_container_view_get_container (view);

  g_return_val_if_fail (container != NULL, NULL);

  gtk_icon_size_lookup (size, &width, &height);

  prop_name = pika_context_type_to_prop_name (pika_container_get_children_type (container));

  preview = pika_prop_view_new (G_OBJECT (context), prop_name,
                                context, height);
  PIKA_VIEW (preview)->renderer->size = -1;

  pika_container_view_get_view_size (view, &border_width);

  border_width = MIN (1, border_width);

  pika_view_renderer_set_size_full (PIKA_VIEW (preview)->renderer,
                                    width, height, border_width);

  return preview;
}
