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

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

#include "libpikabase/pikabase.h"
#include "libpikamath/pikamath.h"
#include "libpikacolor/pikacolor.h"
#include "libpikawidgets/pikawidgets.h"

#include "widgets-types.h"

#include "core/pika.h"
#include "core/pikacontainer.h"
#include "core/pikacontext.h"
#include "core/pikadatafactory.h"
#include "core/pikapalette.h"

#include "pikacolordialog.h"
#include "pikadnd.h"
#include "pikadocked.h"
#include "pikadialogfactory.h"
#include "pikahelp-ids.h"
#include "pikapaletteeditor.h"
#include "pikapaletteview.h"
#include "pikasessioninfo-aux.h"
#include "pikauimanager.h"
#include "pikaviewrendererpalette.h"
#include "pikawidgets-utils.h"

#include "pika-intl.h"


#define ENTRY_WIDTH  12
#define ENTRY_HEIGHT 10
#define SPACING       1
#define COLUMNS      16
#define ROWS         11

#define PREVIEW_WIDTH  ((ENTRY_WIDTH  + SPACING) * COLUMNS + 1)
#define PREVIEW_HEIGHT ((ENTRY_HEIGHT + SPACING) * ROWS    + 1)


/*  local function prototypes  */

static void   pika_palette_editor_docked_iface_init (PikaDockedInterface *face);

static void   pika_palette_editor_constructed      (GObject           *object);
static void   pika_palette_editor_dispose          (GObject           *object);

static void   pika_palette_editor_unmap            (GtkWidget         *widget);

static void   pika_palette_editor_set_data         (PikaDataEditor    *editor,
                                                    PikaData          *data);

static void   pika_palette_editor_set_context      (PikaDocked        *docked,
                                                    PikaContext       *context);
static void   pika_palette_editor_set_aux_info     (PikaDocked        *docked,
                                                    GList             *aux_info);
static GList *pika_palette_editor_get_aux_info     (PikaDocked        *docked);

static void   palette_editor_invalidate_preview    (PikaPalette       *palette,
                                                    PikaPaletteEditor *editor);

static void   palette_editor_viewport_size_allocate(GtkWidget         *widget,
                                                    GtkAllocation     *allocation,
                                                    PikaPaletteEditor *editor);

static void   palette_editor_drop_palette          (GtkWidget         *widget,
                                                    gint               x,
                                                    gint               y,
                                                    PikaViewable      *viewable,
                                                    gpointer           data);
static void   palette_editor_drop_color            (GtkWidget         *widget,
                                                    gint               x,
                                                    gint               y,
                                                    const PikaRGB     *color,
                                                    gpointer           data);

static void   palette_editor_entry_clicked         (PikaPaletteView   *view,
                                                    PikaPaletteEntry  *entry,
                                                    GdkModifierType    state,
                                                    PikaPaletteEditor *editor);
static void   palette_editor_entry_selected        (PikaPaletteView   *view,
                                                    PikaPaletteEntry  *entry,
                                                    PikaPaletteEditor *editor);
static void   palette_editor_entry_activated       (PikaPaletteView   *view,
                                                    PikaPaletteEntry  *entry,
                                                    PikaPaletteEditor *editor);
static gboolean palette_editor_button_press_event  (GtkWidget *widget,
                                                    GdkEvent  *event,
                                                    gpointer   user_data);
static gboolean palette_editor_popup_menu          (GtkWidget *widget,
                                                    gpointer   user_data);
static void   palette_editor_color_dropped         (PikaPaletteView   *view,
                                                    PikaPaletteEntry  *entry,
                                                    const PikaRGB     *color,
                                                    PikaPaletteEditor *editor);

static void   palette_editor_color_name_changed    (GtkWidget         *widget,
                                                    PikaPaletteEditor *editor);
static void   palette_editor_columns_changed       (GtkAdjustment     *adj,
                                                    PikaPaletteEditor *editor);

static void   palette_editor_resize                (PikaPaletteEditor *editor,
                                                    gint               width,
                                                    gdouble            zoom_factor);
static void   palette_editor_scroll_top_left       (PikaPaletteEditor *editor);
static void   palette_editor_edit_color_update     (PikaColorDialog   *dialog,
                                                    const PikaRGB     *color,
                                                    PikaColorDialogState state,
                                                    PikaPaletteEditor *editor);


G_DEFINE_TYPE_WITH_CODE (PikaPaletteEditor, pika_palette_editor,
                         PIKA_TYPE_DATA_EDITOR,
                         G_IMPLEMENT_INTERFACE (PIKA_TYPE_DOCKED,
                                                pika_palette_editor_docked_iface_init))

#define parent_class pika_palette_editor_parent_class

static PikaDockedInterface *parent_docked_iface = NULL;


static void
pika_palette_editor_class_init (PikaPaletteEditorClass *klass)
{
  GObjectClass        *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass      *widget_class = GTK_WIDGET_CLASS (klass);
  PikaDataEditorClass *editor_class = PIKA_DATA_EDITOR_CLASS (klass);

  object_class->constructed = pika_palette_editor_constructed;
  object_class->dispose     = pika_palette_editor_dispose;

  widget_class->unmap       = pika_palette_editor_unmap;

  editor_class->set_data    = pika_palette_editor_set_data;
  editor_class->title       = _("Palette Editor");
}

static void
pika_palette_editor_docked_iface_init (PikaDockedInterface *iface)
{
  parent_docked_iface = g_type_interface_peek_parent (iface);

  if (! parent_docked_iface)
    parent_docked_iface = g_type_default_interface_peek (PIKA_TYPE_DOCKED);

  iface->set_context  = pika_palette_editor_set_context;
  iface->set_aux_info = pika_palette_editor_set_aux_info;
  iface->get_aux_info = pika_palette_editor_get_aux_info;
}

static void
pika_palette_editor_init (PikaPaletteEditor *editor)
{
  PikaDataEditor *data_editor = PIKA_DATA_EDITOR (editor);
  GtkWidget      *viewport;
  GtkWidget      *hbox;
  GtkWidget      *icon;
  GtkWidget      *spinbutton;

  editor->zoom_factor = 1.0;
  editor->col_width   = 0;
  editor->last_width  = 0;
  editor->columns     = COLUMNS;

  data_editor->view = gtk_scrolled_window_new (NULL, NULL);
  gtk_widget_set_size_request (data_editor->view, -1, PREVIEW_HEIGHT);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (data_editor->view),
                                  GTK_POLICY_AUTOMATIC,
                                  GTK_POLICY_AUTOMATIC);
  gtk_box_pack_start (GTK_BOX (editor), data_editor->view, TRUE, TRUE, 0);
  gtk_widget_show (data_editor->view);

  viewport = gtk_viewport_new (NULL, NULL);
  gtk_container_add (GTK_CONTAINER (data_editor->view), viewport);
  gtk_widget_show (viewport);

  editor->view = pika_view_new_full_by_types (NULL,
                                              PIKA_TYPE_PALETTE_VIEW,
                                              PIKA_TYPE_PALETTE,
                                              PREVIEW_WIDTH, PREVIEW_HEIGHT, 0,
                                              FALSE, TRUE, FALSE);
  pika_view_renderer_palette_set_cell_size
    (PIKA_VIEW_RENDERER_PALETTE (PIKA_VIEW (editor->view)->renderer), -1);
  pika_view_renderer_palette_set_draw_grid
    (PIKA_VIEW_RENDERER_PALETTE (PIKA_VIEW (editor->view)->renderer), TRUE);
  gtk_container_add (GTK_CONTAINER (viewport), editor->view);
  gtk_widget_show (editor->view);

  g_signal_connect (gtk_widget_get_parent (editor->view), "size-allocate",
                    G_CALLBACK (palette_editor_viewport_size_allocate),
                    editor);

  g_signal_connect (editor->view, "entry-clicked",
                    G_CALLBACK (palette_editor_entry_clicked),
                    editor);
  g_signal_connect (editor->view, "entry-selected",
                    G_CALLBACK (palette_editor_entry_selected),
                    editor);
  g_signal_connect (editor->view, "entry-activated",
                    G_CALLBACK (palette_editor_entry_activated),
                    editor);
  g_signal_connect (editor->view, "color-dropped",
                    G_CALLBACK (palette_editor_color_dropped),
                    editor);
  g_signal_connect (editor->view, "button-press-event",
                    G_CALLBACK (palette_editor_button_press_event),
                    editor);
  g_signal_connect (editor->view, "popup-menu",
                    G_CALLBACK (palette_editor_popup_menu),
                    editor);

  pika_dnd_viewable_dest_add (editor->view,
                              PIKA_TYPE_PALETTE,
                              palette_editor_drop_palette,
                              editor);
  pika_dnd_viewable_dest_add (gtk_widget_get_parent (editor->view),
                              PIKA_TYPE_PALETTE,
                              palette_editor_drop_palette,
                              editor);

  pika_dnd_color_dest_add (gtk_widget_get_parent (editor->view),
                           palette_editor_drop_color,
                           editor);

  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 2);
  gtk_box_pack_start (GTK_BOX (editor), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

  /*  The color index number  */
  editor->index_label = gtk_label_new ("####");
  gtk_box_pack_start (GTK_BOX (hbox), editor->index_label, FALSE, FALSE, 0);
  pika_label_set_attributes (GTK_LABEL (editor->index_label),
                             PANGO_ATTR_FAMILY, "Monospace", -1);
  gtk_widget_show (editor->index_label);

  /*  The color name entry  */
  editor->color_name = gtk_entry_new ();
  gtk_box_pack_start (GTK_BOX (hbox), editor->color_name, TRUE, TRUE, 0);
  gtk_entry_set_width_chars (GTK_ENTRY (editor->color_name), 1);
  gtk_entry_set_text (GTK_ENTRY (editor->color_name), _("Undefined"));
  gtk_editable_set_editable (GTK_EDITABLE (editor->color_name), FALSE);
  gtk_widget_show (editor->color_name);

  g_signal_connect (editor->color_name, "changed",
                    G_CALLBACK (palette_editor_color_name_changed),
                    editor);

  icon = gtk_image_new_from_icon_name (PIKA_ICON_GRID, GTK_ICON_SIZE_MENU);
  gtk_widget_set_margin_start (icon, 2);
  gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
  gtk_widget_show (icon);

  editor->columns_adj = gtk_adjustment_new (0, 0, 64, 1, 4, 0);
  spinbutton = pika_spin_button_new (editor->columns_adj, 1.0, 0);
  gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbutton), TRUE);
  gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
  gtk_widget_show (spinbutton);

  pika_help_set_help_data (spinbutton, _("Set the number of columns"), NULL);

  g_signal_connect (editor->columns_adj, "value-changed",
                    G_CALLBACK (palette_editor_columns_changed),
                    editor);
}

static void
pika_palette_editor_constructed (GObject *object)
{
  PikaPaletteEditor *editor = PIKA_PALETTE_EDITOR (object);

  G_OBJECT_CLASS (parent_class)->constructed (object);

  pika_editor_add_action_button (PIKA_EDITOR (editor), "palette-editor",
                                 "palette-editor-edit-color", NULL);

  pika_editor_add_action_button (PIKA_EDITOR (editor), "palette-editor",
                                 "palette-editor-new-color-fg",
                                 "palette-editor-new-color-bg",
                                 pika_get_toggle_behavior_mask (),
                                 NULL);

  pika_editor_add_action_button (PIKA_EDITOR (editor), "palette-editor",
                                 "palette-editor-delete-color", NULL);

  pika_editor_add_action_button (PIKA_EDITOR (editor), "palette-editor",
                                 "palette-editor-zoom-out", NULL);

  pika_editor_add_action_button (PIKA_EDITOR (editor), "palette-editor",
                                 "palette-editor-zoom-in", NULL);

  pika_editor_add_action_button (PIKA_EDITOR (editor), "palette-editor",
                                 "palette-editor-zoom-all", NULL);
}

static void
pika_palette_editor_dispose (GObject *object)
{
  PikaPaletteEditor *editor = PIKA_PALETTE_EDITOR (object);

  g_clear_pointer (&editor->color_dialog, gtk_widget_destroy);

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

static void
pika_palette_editor_unmap (GtkWidget *widget)
{
  PikaPaletteEditor *editor = PIKA_PALETTE_EDITOR (widget);

  if (editor->color_dialog)
    gtk_widget_hide (editor->color_dialog);

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

static void
pika_palette_editor_set_data (PikaDataEditor *editor,
                              PikaData       *data)
{
  PikaPaletteEditor *palette_editor = PIKA_PALETTE_EDITOR (editor);

  g_signal_handlers_block_by_func (palette_editor->columns_adj,
                                   palette_editor_columns_changed,
                                   editor);

  if (editor->data)
    {
      if (palette_editor->color_dialog)
        {
          gtk_widget_destroy (palette_editor->color_dialog);
          palette_editor->color_dialog = NULL;
        }

      g_signal_handlers_disconnect_by_func (editor->data,
                                            palette_editor_invalidate_preview,
                                            editor);

      gtk_adjustment_set_value (palette_editor->columns_adj, 0);
    }

  PIKA_DATA_EDITOR_CLASS (parent_class)->set_data (editor, data);

  pika_view_set_viewable (PIKA_VIEW (palette_editor->view),
                          PIKA_VIEWABLE (data));

  if (editor->data)
    {
      PikaPalette *palette = PIKA_PALETTE (editor->data);

      g_signal_connect (editor->data, "invalidate-preview",
                        G_CALLBACK (palette_editor_invalidate_preview),
                        editor);

      gtk_adjustment_set_value (palette_editor->columns_adj,
                                pika_palette_get_columns (palette));

      palette_editor_scroll_top_left (palette_editor);

      palette_editor_invalidate_preview (PIKA_PALETTE (editor->data),
                                         palette_editor);
    }

  g_signal_handlers_unblock_by_func (palette_editor->columns_adj,
                                     palette_editor_columns_changed,
                                     editor);
}

static void
pika_palette_editor_set_context (PikaDocked  *docked,
                                 PikaContext *context)
{
  PikaPaletteEditor *editor = PIKA_PALETTE_EDITOR (docked);

  parent_docked_iface->set_context (docked, context);

  pika_view_renderer_set_context (PIKA_VIEW (editor->view)->renderer,
                                  context);
}

#define AUX_INFO_ZOOM_FACTOR "zoom-factor"

static void
pika_palette_editor_set_aux_info (PikaDocked *docked,
                                  GList      *aux_info)
{
  PikaPaletteEditor *editor = PIKA_PALETTE_EDITOR (docked);
  GList             *list;

  parent_docked_iface->set_aux_info (docked, aux_info);

  for (list = aux_info; list; list = g_list_next (list))
    {
      PikaSessionInfoAux *aux = list->data;

      if (! strcmp (aux->name, AUX_INFO_ZOOM_FACTOR))
        {
          gdouble zoom_factor;

          zoom_factor = g_ascii_strtod (aux->value, NULL);

          editor->zoom_factor = CLAMP (zoom_factor, 0.1, 4.0);
        }
    }
}

static GList *
pika_palette_editor_get_aux_info (PikaDocked *docked)
{
  PikaPaletteEditor *editor = PIKA_PALETTE_EDITOR (docked);
  GList             *aux_info;

  aux_info = parent_docked_iface->get_aux_info (docked);

  if (editor->zoom_factor != 1.0)
    {
      PikaSessionInfoAux *aux;
      gchar               value[G_ASCII_DTOSTR_BUF_SIZE];

      g_ascii_formatd (value, sizeof (value), "%.2f", editor->zoom_factor);

      aux = pika_session_info_aux_new (AUX_INFO_ZOOM_FACTOR, value);
      aux_info = g_list_append (aux_info, aux);
    }

  return aux_info;
}


/*  public functions  */

GtkWidget *
pika_palette_editor_new (PikaContext     *context,
                         PikaMenuFactory *menu_factory)
{
  g_return_val_if_fail (PIKA_IS_CONTEXT (context), NULL);

  return g_object_new (PIKA_TYPE_PALETTE_EDITOR,
                       "menu-factory",    menu_factory,
                       "menu-identifier", "<PaletteEditor>",
                       "ui-path",         "/palette-editor-popup",
                       "data-factory",    context->pika->palette_factory,
                       "context",         context,
                       "data",            pika_context_get_palette (context),
                       NULL);
}

void
pika_palette_editor_edit_color (PikaPaletteEditor *editor)
{
  PikaDataEditor *data_editor;
  PikaPalette    *palette;

  g_return_if_fail (PIKA_IS_PALETTE_EDITOR (editor));

  data_editor = PIKA_DATA_EDITOR (editor);

  if (! (data_editor->data_editable && editor->color))
    return;

  palette = PIKA_PALETTE (pika_data_editor_get_data (data_editor));

  if (! editor->color_dialog)
    {
      editor->color_dialog =
        pika_color_dialog_new (PIKA_VIEWABLE (palette),
                               data_editor->context,
                               FALSE,
                               _("Edit Palette Color"),
                               PIKA_ICON_PALETTE,
                               _("Edit Color Palette Entry"),
                               GTK_WIDGET (editor),
                               pika_dialog_factory_get_singleton (),
                               "pika-palette-editor-color-dialog",
                               &editor->color->color,
                               FALSE, FALSE);

      g_signal_connect (editor->color_dialog, "destroy",
                        G_CALLBACK (gtk_widget_destroyed),
                        &editor->color_dialog);

      g_signal_connect (editor->color_dialog, "update",
                        G_CALLBACK (palette_editor_edit_color_update),
                        editor);
    }
  else
    {
      pika_viewable_dialog_set_viewables (PIKA_VIEWABLE_DIALOG (editor->color_dialog),
                                          g_list_prepend (NULL, palette),
                                          data_editor->context);
      pika_color_dialog_set_color (PIKA_COLOR_DIALOG (editor->color_dialog),
                                   &editor->color->color);

      if (! gtk_widget_get_visible (editor->color_dialog))
        pika_dialog_factory_position_dialog (pika_dialog_factory_get_singleton (),
                                             "pika-palette-editor-color-dialog",
                                             editor->color_dialog,
                                             pika_widget_get_monitor (GTK_WIDGET (editor)));
    }

  gtk_window_present (GTK_WINDOW (editor->color_dialog));
}

void
pika_palette_editor_pick_color (PikaPaletteEditor  *editor,
                                const PikaRGB      *color,
                                PikaColorPickState  pick_state)
{
  g_return_if_fail (PIKA_IS_PALETTE_EDITOR (editor));
  g_return_if_fail (color != NULL);

  if (PIKA_DATA_EDITOR (editor)->data_editable)
    {
      PikaPaletteEntry *entry;
      PikaData         *data;
      gint              index = -1;

      data = pika_data_editor_get_data (PIKA_DATA_EDITOR (editor));
      if (editor->color)
        index = pika_palette_get_entry_position (PIKA_PALETTE (data),
                                                 editor->color);

      switch (pick_state)
        {
        case PIKA_COLOR_PICK_STATE_START:
          if (editor->color)
            index += 1;

          entry = pika_palette_add_entry (PIKA_PALETTE (data), index,
                                          NULL, color);
          pika_palette_view_select_entry (PIKA_PALETTE_VIEW (editor->view),
                                          entry);
          break;

        case PIKA_COLOR_PICK_STATE_UPDATE:
        case PIKA_COLOR_PICK_STATE_END:
          pika_palette_set_entry_color (PIKA_PALETTE (data),
                                        index, color, FALSE);
          break;
        }
    }
}

void
pika_palette_editor_zoom (PikaPaletteEditor  *editor,
                          PikaZoomType        zoom_type)
{
  PikaPalette *palette;
  gdouble      zoom_factor;

  g_return_if_fail (PIKA_IS_PALETTE_EDITOR (editor));

  palette = PIKA_PALETTE (PIKA_DATA_EDITOR (editor)->data);

  if (! palette)
    return;

  zoom_factor = editor->zoom_factor;

  switch (zoom_type)
    {
    case PIKA_ZOOM_IN_MAX:
    case PIKA_ZOOM_IN_MORE:
    case PIKA_ZOOM_IN:
      zoom_factor += 0.1;
      break;

    case PIKA_ZOOM_OUT_MORE:
    case PIKA_ZOOM_OUT:
      zoom_factor -= 0.1;
      break;

    case PIKA_ZOOM_OUT_MAX:
    case PIKA_ZOOM_TO: /* abused as ZOOM_ALL */
      {
        GtkWidget     *scrolled_win = PIKA_DATA_EDITOR (editor)->view;
        GtkWidget     *viewport     = gtk_bin_get_child (GTK_BIN (scrolled_win));
        GtkAllocation  allocation;
        gint           columns;
        gint           rows;

        gtk_widget_get_allocation (viewport, &allocation);

        columns = pika_palette_get_columns (palette);
        if (columns == 0)
          columns = COLUMNS;

        rows = pika_palette_get_n_colors (palette) / columns;
        if (pika_palette_get_n_colors (palette) % columns)
          rows += 1;

        rows = MAX (1, rows);

        zoom_factor = (((gdouble) allocation.height - 2 * SPACING) /
                       (gdouble) rows - SPACING) / ENTRY_HEIGHT;
      }
      break;

    case PIKA_ZOOM_SMOOTH:
    case PIKA_ZOOM_PINCH: /* can't happen */
      g_return_if_reached ();
    }

  zoom_factor = CLAMP (zoom_factor, 0.1, 4.0);

  editor->columns = pika_palette_get_columns (palette);
  if (editor->columns == 0)
    editor->columns = COLUMNS;

  palette_editor_resize (editor, editor->last_width, zoom_factor);

  palette_editor_scroll_top_left (editor);
}

gint
pika_palette_editor_get_index (PikaPaletteEditor *editor,
                               const PikaRGB     *search)
{
  PikaPalette *palette;

  g_return_val_if_fail (PIKA_IS_PALETTE_EDITOR (editor), -1);
  g_return_val_if_fail (search != NULL, -1);

  palette = PIKA_PALETTE (PIKA_DATA_EDITOR (editor)->data);

  if (palette && pika_palette_get_n_colors (palette) > 0)
    {
      PikaPaletteEntry *entry;

      entry = pika_palette_find_entry (palette, search, editor->color);

      if (entry)
        return pika_palette_get_entry_position (palette, entry);
    }

  return -1;
}

gboolean
pika_palette_editor_set_index (PikaPaletteEditor *editor,
                               gint               index,
                               PikaRGB           *color)
{
  PikaPalette *palette;

  g_return_val_if_fail (PIKA_IS_PALETTE_EDITOR (editor), FALSE);

  palette = PIKA_PALETTE (PIKA_DATA_EDITOR (editor)->data);

  if (palette && pika_palette_get_n_colors (palette) > 0)
    {
      PikaPaletteEntry *entry;

      index = CLAMP (index, 0, pika_palette_get_n_colors (palette) - 1);

      entry = pika_palette_get_entry (palette, index);

      pika_palette_view_select_entry (PIKA_PALETTE_VIEW (editor->view),
                                      entry);

      if (color)
        *color = editor->color->color;

      return TRUE;
    }

  return FALSE;
}

gint
pika_palette_editor_max_index (PikaPaletteEditor *editor)
{
  PikaPalette *palette;

  g_return_val_if_fail (PIKA_IS_PALETTE_EDITOR (editor), -1);

  palette = PIKA_PALETTE (PIKA_DATA_EDITOR (editor)->data);

  if (palette && pika_palette_get_n_colors (palette) > 0)
    {
      return pika_palette_get_n_colors (palette) - 1;
    }

  return -1;
}


/*  private functions  */

static void
palette_editor_invalidate_preview (PikaPalette       *palette,
                                   PikaPaletteEditor *editor)
{
  editor->columns = pika_palette_get_columns (palette);
  if (editor->columns == 0)
    editor->columns = COLUMNS;

  palette_editor_resize (editor, editor->last_width, editor->zoom_factor);
}

static void
palette_editor_viewport_size_allocate (GtkWidget         *widget,
                                       GtkAllocation     *allocation,
                                       PikaPaletteEditor *editor)
{
  if (allocation->width != editor->last_width)
    {
      palette_editor_resize (editor, allocation->width,
                             editor->zoom_factor);
    }
}

static void
palette_editor_drop_palette (GtkWidget    *widget,
                             gint          x,
                             gint          y,
                             PikaViewable *viewable,
                             gpointer      data)
{
  pika_data_editor_set_data (PIKA_DATA_EDITOR (data), PIKA_DATA (viewable));
}

static void
palette_editor_drop_color (GtkWidget     *widget,
                           gint           x,
                           gint           y,
                           const PikaRGB *color,
                           gpointer       data)
{
  PikaPaletteEditor *editor = data;

  if (PIKA_DATA_EDITOR (editor)->data_editable)
    {
      PikaPalette      *palette = PIKA_PALETTE (PIKA_DATA_EDITOR (editor)->data);
      PikaPaletteEntry *entry;

      entry = pika_palette_add_entry (palette, -1, NULL, color);
      pika_palette_view_select_entry (PIKA_PALETTE_VIEW (editor->view), entry);
    }
}


/*  palette view callbacks  */

static void
palette_editor_entry_clicked (PikaPaletteView   *view,
                              PikaPaletteEntry  *entry,
                              GdkModifierType    state,
                              PikaPaletteEditor *editor)
{
  if (entry)
    {
      PikaDataEditor *data_editor = PIKA_DATA_EDITOR (editor);

      if (state & pika_get_toggle_behavior_mask ())
        pika_context_set_background (data_editor->context, &entry->color);
      else
        pika_context_set_foreground (data_editor->context, &entry->color);
    }
}

static void
palette_editor_entry_selected (PikaPaletteView   *view,
                               PikaPaletteEntry  *entry,
                               PikaPaletteEditor *editor)
{
  PikaDataEditor *data_editor = PIKA_DATA_EDITOR (editor);

  if (editor->color != entry)
    {
      gchar index[8];

      editor->color = entry;

      if (entry)
        {
          PikaPalette *palette = PIKA_PALETTE (data_editor->data);
          gint         pos;

          pos = pika_palette_get_entry_position (palette, entry);
          g_snprintf (index, sizeof (index), "%04i", pos);
        }
      else
        {
          g_snprintf (index, sizeof (index), "####");
        }

      gtk_label_set_text (GTK_LABEL (editor->index_label), index);

      g_signal_handlers_block_by_func (editor->color_name,
                                       palette_editor_color_name_changed,
                                       editor);

      gtk_entry_set_text (GTK_ENTRY (editor->color_name),
                          entry ? entry->name : _("Undefined"));

      g_signal_handlers_unblock_by_func (editor->color_name,
                                         palette_editor_color_name_changed,
                                         editor);

      gtk_editable_set_editable (GTK_EDITABLE (editor->color_name),
                                 entry && data_editor->data_editable);

      pika_ui_manager_update (pika_editor_get_ui_manager (PIKA_EDITOR (editor)),
                              pika_editor_get_popup_data (PIKA_EDITOR (editor)));
    }
}

static void
palette_editor_entry_activated (PikaPaletteView   *view,
                                PikaPaletteEntry  *entry,
                                PikaPaletteEditor *editor)
{
  if (PIKA_DATA_EDITOR (editor)->data_editable && entry == editor->color)
    {
      pika_ui_manager_activate_action (pika_editor_get_ui_manager (PIKA_EDITOR (editor)),
                                       "palette-editor",
                                       "palette-editor-edit-color");
    }
}

static gboolean
palette_editor_button_press_event (GtkWidget *widget,
                                   GdkEvent  *event,
                                   gpointer   user_data)
{
  PikaPaletteEditor *editor = PIKA_PALETTE_EDITOR (user_data);

  if (gdk_event_triggers_context_menu (event))
    {
      pika_editor_popup_menu_at_pointer (PIKA_EDITOR (editor), event);
      return GDK_EVENT_STOP;
    }

  return GDK_EVENT_PROPAGATE;
}

static gboolean
palette_editor_popup_menu (GtkWidget *widget,
                           gpointer   user_data)
{
  PikaPaletteEditor *editor = PIKA_PALETTE_EDITOR (user_data);
  PikaPaletteEntry *selected;
  GdkRectangle rect;

  selected = pika_palette_view_get_selected_entry (PIKA_PALETTE_VIEW (editor->view));
  if (!selected)
    return GDK_EVENT_PROPAGATE;

  pika_palette_view_get_entry_rect (PIKA_PALETTE_VIEW (editor->view), selected, &rect);
  return pika_editor_popup_menu_at_rect (PIKA_EDITOR (editor),
                                         gtk_widget_get_window (GTK_WIDGET (editor->view)),
                                         &rect, GDK_GRAVITY_CENTER, GDK_GRAVITY_NORTH_WEST,
                                         NULL);
}

static void
palette_editor_color_dropped (PikaPaletteView   *view,
                              PikaPaletteEntry  *entry,
                              const PikaRGB     *color,
                              PikaPaletteEditor *editor)
{
  if (PIKA_DATA_EDITOR (editor)->data_editable)
    {
      PikaPalette *palette = PIKA_PALETTE (PIKA_DATA_EDITOR (editor)->data);
      gint         pos     = -1;

      if (entry)
        pos = pika_palette_get_entry_position (palette, entry);

      entry = pika_palette_add_entry (palette, pos, NULL, color);
      pika_palette_view_select_entry (PIKA_PALETTE_VIEW (editor->view), entry);
    }
}


/*  color name and columns callbacks  */

static void
palette_editor_color_name_changed (GtkWidget         *widget,
                                   PikaPaletteEditor *editor)
{
  if (PIKA_DATA_EDITOR (editor)->data)
    {
      PikaPalette *palette = PIKA_PALETTE (PIKA_DATA_EDITOR (editor)->data);
      const gchar *name;
      gint         pos;

      name = gtk_entry_get_text (GTK_ENTRY (editor->color_name));

      pos = pika_palette_get_entry_position (palette, editor->color);
      pika_palette_set_entry_name (palette, pos, name);
    }
}

static void
palette_editor_columns_changed (GtkAdjustment     *adj,
                                PikaPaletteEditor *editor)
{
  if (PIKA_DATA_EDITOR (editor)->data)
    {
      PikaPalette *palette = PIKA_PALETTE (PIKA_DATA_EDITOR (editor)->data);

      pika_palette_set_columns (palette,
                                ROUND (gtk_adjustment_get_value (adj)));
    }
}


/*  misc utils  */

static void
palette_editor_resize (PikaPaletteEditor *editor,
                       gint               width,
                       gdouble            zoom_factor)
{
  PikaPalette *palette;
  gint         rows;
  gint         preview_width;
  gint         preview_height;

  palette = PIKA_PALETTE (PIKA_DATA_EDITOR (editor)->data);

  if (! palette)
    return;

  width = MIN (width, PIKA_VIEWABLE_MAX_PREVIEW_SIZE);

  editor->zoom_factor = zoom_factor;
  editor->last_width  = width;
  editor->col_width   = width / (editor->columns + 1) - SPACING;

  if (editor->col_width < 0)
    editor->col_width = 0;

  rows = pika_palette_get_n_colors (palette) / editor->columns;
  if (pika_palette_get_n_colors (palette) % editor->columns)
    rows += 1;

  preview_width  = (editor->col_width + SPACING) * editor->columns;
  preview_height = (rows *
                    (SPACING + (gint) (ENTRY_HEIGHT * editor->zoom_factor)));

  if (preview_height > PIKA_VIEWABLE_MAX_PREVIEW_SIZE)
    preview_height = ((PIKA_VIEWABLE_MAX_PREVIEW_SIZE - SPACING) / rows) * rows;

  pika_view_renderer_set_size_full (PIKA_VIEW (editor->view)->renderer,
                                    preview_width  + SPACING,
                                    preview_height + SPACING, 0);
}

static void
palette_editor_scroll_top_left (PikaPaletteEditor *palette_editor)
{
  PikaDataEditor *data_editor = PIKA_DATA_EDITOR (palette_editor);
  GtkAdjustment  *hadj;
  GtkAdjustment  *vadj;

  if (! data_editor->view)
    return;

  hadj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (data_editor->view));
  vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (data_editor->view));

  if (hadj)
    gtk_adjustment_set_value (hadj, 0.0);
  if (vadj)
    gtk_adjustment_set_value (vadj, 0.0);
}

static void
palette_editor_edit_color_update (PikaColorDialog      *dialog,
                                  const PikaRGB        *color,
                                  PikaColorDialogState  state,
                                  PikaPaletteEditor    *editor)
{
  PikaPalette *palette = PIKA_PALETTE (PIKA_DATA_EDITOR (editor)->data);

  switch (state)
    {
    case PIKA_COLOR_DIALOG_UPDATE:
      break;

    case PIKA_COLOR_DIALOG_OK:
      if (editor->color)
        {
          editor->color->color = *color;
          pika_data_dirty (PIKA_DATA (palette));
        }
      /* Fallthrough */

    case PIKA_COLOR_DIALOG_CANCEL:
      gtk_widget_hide (editor->color_dialog);
      break;
    }
}
