/* 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
 *
 * 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 <stdlib.h>
#include <string.h>

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

#include "libpikawidgets/pikawidgets.h"

#include "widgets-types.h"

#include "core/pika.h"
#include "core/pikabrush.h"
#include "core/pikacontext.h"
#include "core/pikagradient.h"
#include "core/pikapattern.h"

#include "pikadnd.h"
#include "pikahelp-ids.h"
#include "pikatoolbox.h"
#include "pikatoolbox-indicator-area.h"
#include "pikaview.h"
#include "pikawidgets-utils.h"
#include "pikawindowstrategy.h"

#include "pika-intl.h"


#define CELL_SIZE        24  /*  The size of the previews                  */
#define GRAD_CELL_WIDTH  52  /*  The width of the gradient preview         */
#define GRAD_CELL_HEIGHT 12  /*  The height of the gradient preview        */
#define CELL_SPACING      2  /*  How much between brush and pattern cells  */


static void
brush_preview_clicked (GtkWidget       *widget,
                       GdkModifierType  state,
                       PikaToolbox     *toolbox)
{
  PikaContext *context = pika_toolbox_get_context (toolbox);

  pika_window_strategy_show_dockable_dialog (PIKA_WINDOW_STRATEGY (pika_get_window_strategy (context->pika)),
                                             context->pika,
                                             pika_dock_get_dialog_factory (PIKA_DOCK (toolbox)),
                                             pika_widget_get_monitor (widget),
                                             "pika-brush-grid|pika-brush-list");
}

static void
brush_preview_drop_brush (GtkWidget    *widget,
                          gint          x,
                          gint          y,
                          PikaViewable *viewable,
                          gpointer      data)
{
  PikaContext *context = PIKA_CONTEXT (data);

  pika_context_set_brush (context, PIKA_BRUSH (viewable));
}

static void
pattern_preview_clicked (GtkWidget       *widget,
                         GdkModifierType  state,
                         PikaToolbox     *toolbox)
{
  PikaContext *context = pika_toolbox_get_context (toolbox);

  pika_window_strategy_show_dockable_dialog (PIKA_WINDOW_STRATEGY (pika_get_window_strategy (context->pika)),
                                             context->pika,
                                             pika_dock_get_dialog_factory (PIKA_DOCK (toolbox)),
                                             pika_widget_get_monitor (widget),
                                             "pika-pattern-grid|pika-pattern-list");
}

static void
pattern_preview_drop_pattern (GtkWidget    *widget,
                              gint          x,
                              gint          y,
                              PikaViewable *viewable,
                              gpointer      data)
{
  PikaContext *context = PIKA_CONTEXT (data);

  pika_context_set_pattern (context, PIKA_PATTERN (viewable));
}

static void
gradient_preview_clicked (GtkWidget       *widget,
                          GdkModifierType  state,
                          PikaToolbox     *toolbox)
{
  PikaContext *context = pika_toolbox_get_context (toolbox);

  pika_window_strategy_show_dockable_dialog (PIKA_WINDOW_STRATEGY (pika_get_window_strategy (context->pika)),
                                             context->pika,
                                             pika_dock_get_dialog_factory (PIKA_DOCK (toolbox)),
                                             pika_widget_get_monitor (widget),
                                             "pika-gradient-list|pika-gradient-grid");
}

static void
gradient_preview_drop_gradient (GtkWidget    *widget,
                                gint          x,
                                gint          y,
                                PikaViewable *viewable,
                                gpointer      data)
{
  PikaContext *context = PIKA_CONTEXT (data);

  pika_context_set_gradient (context, PIKA_GRADIENT (viewable));
}


/*  public functions  */

GtkWidget *
pika_toolbox_indicator_area_create (PikaToolbox *toolbox)
{
  PikaContext *context;
  GtkWidget   *grid;
  GtkWidget   *brush_view;
  GtkWidget   *pattern_view;
  GtkWidget   *gradient_view;

  g_return_val_if_fail (PIKA_IS_TOOLBOX (toolbox), NULL);

  context = pika_toolbox_get_context (toolbox);

  grid = gtk_grid_new ();
  gtk_grid_set_row_spacing (GTK_GRID (grid), CELL_SPACING);
  gtk_grid_set_column_spacing (GTK_GRID (grid), CELL_SPACING);

  pika_help_set_help_data (grid, NULL, PIKA_HELP_TOOLBOX_INDICATOR_AREA);

  /*  brush view  */

  brush_view =
    pika_view_new_full_by_types (context,
                                 PIKA_TYPE_VIEW, PIKA_TYPE_BRUSH,
                                 CELL_SIZE, CELL_SIZE, 1,
                                 FALSE, TRUE, TRUE);
  pika_view_set_viewable (PIKA_VIEW (brush_view),
                          PIKA_VIEWABLE (pika_context_get_brush (context)));
  gtk_grid_attach (GTK_GRID (grid), brush_view, 0, 0, 1, 1);
  gtk_widget_show (brush_view);

  pika_help_set_help_data (brush_view,
                           _("The active brush.\n"
                             "Click to open the Brush Dialog."), NULL);

  g_signal_connect_object (context, "brush-changed",
                           G_CALLBACK (pika_view_set_viewable),
                           brush_view,
                           G_CONNECT_SWAPPED);

  g_signal_connect (brush_view, "clicked",
                    G_CALLBACK (brush_preview_clicked),
                    toolbox);

  pika_dnd_viewable_dest_add (brush_view,
                              PIKA_TYPE_BRUSH,
                              brush_preview_drop_brush,
                              context);

  /*  pattern view  */

  pattern_view =
    pika_view_new_full_by_types (context,
                                 PIKA_TYPE_VIEW, PIKA_TYPE_PATTERN,
                                 CELL_SIZE, CELL_SIZE, 1,
                                 FALSE, TRUE, TRUE);
  pika_view_set_viewable (PIKA_VIEW (pattern_view),
                          PIKA_VIEWABLE (pika_context_get_pattern (context)));

  gtk_grid_attach (GTK_GRID (grid), pattern_view, 1, 0, 1, 1);
  gtk_widget_show (pattern_view);

  pika_help_set_help_data (pattern_view,
                           _("The active pattern.\n"
                             "Click to open the Pattern Dialog."), NULL);

  g_signal_connect_object (context, "pattern-changed",
                           G_CALLBACK (pika_view_set_viewable),
                           pattern_view,
                           G_CONNECT_SWAPPED);

  g_signal_connect (pattern_view, "clicked",
                    G_CALLBACK (pattern_preview_clicked),
                    toolbox);

  pika_dnd_viewable_dest_add (pattern_view,
                              PIKA_TYPE_PATTERN,
                              pattern_preview_drop_pattern,
                              context);

  /*  gradient view  */

  gradient_view =
    pika_view_new_full_by_types (context,
                                 PIKA_TYPE_VIEW, PIKA_TYPE_GRADIENT,
                                 GRAD_CELL_WIDTH, GRAD_CELL_HEIGHT, 1,
                                 FALSE, TRUE, TRUE);
  pika_view_set_viewable (PIKA_VIEW (gradient_view),
                          PIKA_VIEWABLE (pika_context_get_gradient (context)));

  gtk_grid_attach (GTK_GRID (grid), gradient_view, 0, 1, 2, 1);
  gtk_widget_show (gradient_view);

  pika_help_set_help_data (gradient_view,
                           _("The active gradient.\n"
                             "Click to open the Gradient Dialog."), NULL);

  g_signal_connect_object (context, "gradient-changed",
                           G_CALLBACK (pika_view_set_viewable),
                           gradient_view,
                           G_CONNECT_SWAPPED);

  g_signal_connect (gradient_view, "clicked",
                    G_CALLBACK (gradient_preview_clicked),
                    toolbox);

  pika_dnd_viewable_dest_add (gradient_view,
                              PIKA_TYPE_GRADIENT,
                              gradient_preview_drop_gradient,
                              context);

  gtk_widget_show (grid);

  return grid;
}
