/* 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
 *
 * pikabrushfactoryview.c
 * Copyright (C) 2001 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 <gegl.h>
#include <gtk/gtk.h>

#include "libpikawidgets/pikawidgets.h"

#include "widgets-types.h"

#include "core/pikacontainer.h"
#include "core/pikacontext.h"
#include "core/pikabrush.h"
#include "core/pikabrushgenerated.h"
#include "core/pikadatafactory.h"

#include "pikabrushfactoryview.h"
#include "pikacontainerview.h"
#include "pikaeditor.h"
#include "pikamenufactory.h"
#include "pikaviewrenderer.h"

#include "pika-intl.h"

enum
{
  SPACING_CHANGED,
  LAST_SIGNAL
};


static void   pika_brush_factory_view_dispose         (GObject              *object);

static void   pika_brush_factory_view_select_item     (PikaContainerEditor  *editor,
                                                       PikaViewable         *viewable);

static void   pika_brush_factory_view_spacing_changed (PikaBrush            *brush,
                                                       PikaBrushFactoryView *view);
static void   pika_brush_factory_view_spacing_update  (GtkAdjustment        *adjustment,
                                                       PikaBrushFactoryView *view);


G_DEFINE_TYPE (PikaBrushFactoryView, pika_brush_factory_view,
               PIKA_TYPE_DATA_FACTORY_VIEW)

#define parent_class pika_brush_factory_view_parent_class

static guint pika_brush_factory_view_signals[LAST_SIGNAL] = { 0 };


static void
pika_brush_factory_view_class_init (PikaBrushFactoryViewClass *klass)
{
  GObjectClass             *object_class = G_OBJECT_CLASS (klass);
  PikaContainerEditorClass *editor_class = PIKA_CONTAINER_EDITOR_CLASS (klass);

  object_class->dispose     = pika_brush_factory_view_dispose;

  editor_class->select_item = pika_brush_factory_view_select_item;

  /**
   * PikaBrushFactoryView::spacing-changed:
   * @view: the #PikaBrushFactoryView.
   *
   * Emitted when the spacing changed.
   */
  pika_brush_factory_view_signals[SPACING_CHANGED] =
    g_signal_new ("spacing-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (PikaBrushFactoryViewClass, spacing_changed),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 0);
}

static void
pika_brush_factory_view_init (PikaBrushFactoryView *view)
{
  view->spacing_adjustment = gtk_adjustment_new (0.0, 1.0, 5000.0,
                                                 1.0, 10.0, 0.0);

  view->spacing_scale = pika_spin_scale_new (view->spacing_adjustment,
                                             _("Spacing"), 1);
  pika_spin_scale_set_scale_limits (PIKA_SPIN_SCALE (view->spacing_scale),
                                   1.0, 200.0);
  pika_help_set_help_data (view->spacing_scale,
                           _("Percentage of width of brush"),
                           NULL);

  g_signal_connect (view->spacing_adjustment, "value-changed",
                    G_CALLBACK (pika_brush_factory_view_spacing_update),
                    view);
}

static void
pika_brush_factory_view_dispose (GObject *object)
{
  PikaBrushFactoryView *view   = PIKA_BRUSH_FACTORY_VIEW (object);
  PikaContainerEditor  *editor = PIKA_CONTAINER_EDITOR (object);

  if (view->spacing_changed_handler_id)
    {
      PikaDataFactory *factory;
      PikaContainer   *container;

      factory   = pika_data_factory_view_get_data_factory (PIKA_DATA_FACTORY_VIEW (editor));
      container = pika_data_factory_get_container (factory);

      pika_container_remove_handler (container,
                                     view->spacing_changed_handler_id);

      view->spacing_changed_handler_id = 0;
    }

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

GtkWidget *
pika_brush_factory_view_new (PikaViewType     view_type,
                             PikaDataFactory *factory,
                             PikaContext     *context,
                             gboolean         change_brush_spacing,
                             gint             view_size,
                             gint             view_border_width,
                             PikaMenuFactory *menu_factory)
{
  PikaBrushFactoryView *factory_view;
  PikaContainerEditor  *editor;

  g_return_val_if_fail (PIKA_IS_DATA_FACTORY (factory), NULL);
  g_return_val_if_fail (PIKA_IS_CONTEXT (context), NULL);
  g_return_val_if_fail (view_size > 0 &&
                        view_size <= PIKA_VIEWABLE_MAX_PREVIEW_SIZE, NULL);
  g_return_val_if_fail (view_border_width >= 0 &&
                        view_border_width <= PIKA_VIEW_MAX_BORDER_WIDTH,
                        NULL);
  g_return_val_if_fail (menu_factory == NULL ||
                        PIKA_IS_MENU_FACTORY (menu_factory), NULL);

  factory_view = g_object_new (PIKA_TYPE_BRUSH_FACTORY_VIEW,
                               "view-type",         view_type,
                               "data-factory",      factory,
                               "context",           context,
                               "view-size",         view_size,
                               "view-border-width", view_border_width,
                               "menu-factory",      menu_factory,
                               "menu-identifier",   "<Brushes>",
                               "ui-path",           "/brushes-popup",
                               "action-group",      "brushes",
                               NULL);

  factory_view->change_brush_spacing = change_brush_spacing;

  editor = PIKA_CONTAINER_EDITOR (factory_view);

  pika_editor_add_action_button (PIKA_EDITOR (editor->view),
                                 "brushes", "brushes-open-as-image",
                                 NULL);

  gtk_box_pack_end (GTK_BOX (editor->view), factory_view->spacing_scale,
                    FALSE, FALSE, 0);
  gtk_widget_show (factory_view->spacing_scale);

  factory_view->spacing_changed_handler_id =
    pika_container_add_handler (pika_data_factory_get_container (factory), "spacing-changed",
                                G_CALLBACK (pika_brush_factory_view_spacing_changed),
                                factory_view);

  return GTK_WIDGET (factory_view);
}

static void
pika_brush_factory_view_select_item (PikaContainerEditor *editor,
                                     PikaViewable        *viewable)
{
  PikaBrushFactoryView *view = PIKA_BRUSH_FACTORY_VIEW (editor);
  PikaContainer        *container;
  gboolean              spacing_sensitive = FALSE;

  if (PIKA_CONTAINER_EDITOR_CLASS (parent_class)->select_item)
    PIKA_CONTAINER_EDITOR_CLASS (parent_class)->select_item (editor, viewable);

  container = pika_container_view_get_container (editor->view);

  if (viewable && pika_container_have (container, PIKA_OBJECT (viewable)))
    {
      PikaBrush *brush = PIKA_BRUSH (viewable);

      spacing_sensitive = TRUE;

      g_signal_handlers_block_by_func (view->spacing_adjustment,
                                       pika_brush_factory_view_spacing_update,
                                       view);

      gtk_adjustment_set_value (view->spacing_adjustment,
                                pika_brush_get_spacing (brush));

      g_signal_handlers_unblock_by_func (view->spacing_adjustment,
                                         pika_brush_factory_view_spacing_update,
                                         view);
    }

  gtk_widget_set_sensitive (view->spacing_scale, spacing_sensitive);
}

static void
pika_brush_factory_view_spacing_changed (PikaBrush            *brush,
                                         PikaBrushFactoryView *view)
{
  PikaContainerEditor *editor = PIKA_CONTAINER_EDITOR (view);
  PikaContext         *context;

  context = pika_container_view_get_context (editor->view);

  if (brush == pika_context_get_brush (context))
    {
      g_signal_handlers_block_by_func (view->spacing_adjustment,
                                       pika_brush_factory_view_spacing_update,
                                       view);

      gtk_adjustment_set_value (view->spacing_adjustment,
                                pika_brush_get_spacing (brush));

      g_signal_handlers_unblock_by_func (view->spacing_adjustment,
                                         pika_brush_factory_view_spacing_update,
                                         view);
    }
}

static void
pika_brush_factory_view_spacing_update (GtkAdjustment        *adjustment,
                                        PikaBrushFactoryView *view)
{
  PikaContainerEditor *editor = PIKA_CONTAINER_EDITOR (view);
  PikaContext         *context;
  PikaBrush           *brush;

  context = pika_container_view_get_context (editor->view);

  brush = pika_context_get_brush (context);

  if (brush && view->change_brush_spacing)
    {
      g_signal_handlers_block_by_func (brush,
                                       pika_brush_factory_view_spacing_changed,
                                       view);

      pika_brush_set_spacing (brush, gtk_adjustment_get_value (adjustment));

      g_signal_handlers_unblock_by_func (brush,
                                         pika_brush_factory_view_spacing_changed,
                                         view);
    }
  g_signal_emit (view, pika_brush_factory_view_signals[SPACING_CHANGED], 0);
}
