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

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

#include "libpikabase/pikabase.h"
#include "libpikacolor/pikacolor.h"
#include "libpikaconfig/pikaconfig.h"
#include "libpikawidgets/pikawidgets.h"

#include "tools-types.h"

#include "operations/pikacurvesconfig.h"
#include "operations/pikaoperationcurves.h"

#include "core/pika.h"
#include "core/pikacurve.h"
#include "core/pikacurve-map.h"
#include "core/pikadrawable.h"
#include "core/pikadrawable-histogram.h"
#include "core/pikaerror.h"
#include "core/pikahistogram.h"
#include "core/pikaimage.h"

#include "widgets/pikacolorbar.h"
#include "widgets/pikacurveview.h"
#include "widgets/pikahelp-ids.h"
#include "widgets/pikapropwidgets.h"
#include "widgets/pikawidgets-utils.h"

#include "display/pikadisplay.h"

#include "pikacurvestool.h"
#include "pikahistogramoptions.h"

#include "pika-intl.h"


#define GRAPH_SIZE 256
#define BAR_SIZE    12
#define RADIUS       4


/*  local function prototypes  */

static gboolean   pika_curves_tool_initialize      (PikaTool             *tool,
                                                    PikaDisplay          *display,
                                                    GError              **error);
static void       pika_curves_tool_button_release  (PikaTool             *tool,
                                                    const PikaCoords     *coords,
                                                    guint32               time,
                                                    GdkModifierType       state,
                                                    PikaButtonReleaseType release_type,
                                                    PikaDisplay          *display);
static gboolean   pika_curves_tool_key_press       (PikaTool             *tool,
                                                    GdkEventKey          *kevent,
                                                    PikaDisplay          *display);
static void       pika_curves_tool_oper_update     (PikaTool             *tool,
                                                    const PikaCoords     *coords,
                                                    GdkModifierType       state,
                                                    gboolean              proximity,
                                                    PikaDisplay          *display);

static gchar    * pika_curves_tool_get_operation   (PikaFilterTool       *filter_tool,
                                                    gchar               **description);
static void       pika_curves_tool_dialog          (PikaFilterTool       *filter_tool);
static void       pika_curves_tool_reset           (PikaFilterTool       *filter_tool);
static void       pika_curves_tool_config_notify   (PikaFilterTool       *filter_tool,
                                                    PikaConfig           *config,
                                                    const GParamSpec     *pspec);
static gboolean   pika_curves_tool_settings_import (PikaFilterTool       *filter_tool,
                                                    GInputStream         *input,
                                                    GError              **error);
static gboolean   pika_curves_tool_settings_export (PikaFilterTool       *filter_tool,
                                                    GOutputStream        *output,
                                                    GError              **error);
static void       pika_curves_tool_color_picked    (PikaFilterTool       *filter_tool,
                                                    gpointer              identifier,
                                                    gdouble               x,
                                                    gdouble               y,
                                                    const Babl           *sample_format,
                                                    const PikaRGB        *color);

static void       pika_curves_tool_export_setup    (PikaSettingsBox      *settings_box,
                                                    GtkFileChooserDialog *dialog,
                                                    gboolean              export,
                                                    PikaCurvesTool       *tool);
static void       pika_curves_tool_update_channel  (PikaCurvesTool       *tool);
static void       pika_curves_tool_update_point    (PikaCurvesTool       *tool);

static void       curves_curve_dirty_callback      (PikaCurve            *curve,
                                                    PikaCurvesTool       *tool);

static void       curves_channel_callback          (GtkWidget            *widget,
                                                    PikaCurvesTool       *tool);
static void       curves_channel_reset_callback    (GtkWidget            *widget,
                                                    PikaCurvesTool       *tool);

static gboolean   curves_menu_sensitivity          (gint                  value,
                                                    gpointer              data);

static void       curves_graph_selection_callback  (GtkWidget            *widget,
                                                    PikaCurvesTool       *tool);

static void       curves_point_coords_callback     (GtkWidget            *widget,
                                                    PikaCurvesTool       *tool);
static void       curves_point_type_callback       (GtkWidget            *widget,
                                                    PikaCurvesTool       *tool);

static void       curves_curve_type_callback       (GtkWidget            *widget,
                                                    PikaCurvesTool       *tool);

static gboolean   curves_get_channel_color         (GtkWidget            *widget,
                                                    PikaHistogramChannel  channel,
                                                    PikaRGB              *color);


G_DEFINE_TYPE (PikaCurvesTool, pika_curves_tool, PIKA_TYPE_FILTER_TOOL)

#define parent_class pika_curves_tool_parent_class


/*  public functions  */

void
pika_curves_tool_register (PikaToolRegisterCallback  callback,
                           gpointer                  data)
{
  (* callback) (PIKA_TYPE_CURVES_TOOL,
                PIKA_TYPE_HISTOGRAM_OPTIONS,
                pika_color_options_gui,
                0,
                "pika-curves-tool",
                _("Curves"),
                _("Adjust color curves"),
                N_("_Curves..."), NULL,
                NULL, PIKA_HELP_TOOL_CURVES,
                PIKA_ICON_TOOL_CURVES,
                data);
}


/*  private functions  */

static void
pika_curves_tool_class_init (PikaCurvesToolClass *klass)
{
  PikaToolClass       *tool_class        = PIKA_TOOL_CLASS (klass);
  PikaFilterToolClass *filter_tool_class = PIKA_FILTER_TOOL_CLASS (klass);

  tool_class->initialize             = pika_curves_tool_initialize;
  tool_class->button_release         = pika_curves_tool_button_release;
  tool_class->key_press              = pika_curves_tool_key_press;
  tool_class->oper_update            = pika_curves_tool_oper_update;

  filter_tool_class->get_operation   = pika_curves_tool_get_operation;
  filter_tool_class->dialog          = pika_curves_tool_dialog;
  filter_tool_class->reset           = pika_curves_tool_reset;
  filter_tool_class->config_notify   = pika_curves_tool_config_notify;
  filter_tool_class->settings_import = pika_curves_tool_settings_import;
  filter_tool_class->settings_export = pika_curves_tool_settings_export;
  filter_tool_class->color_picked    = pika_curves_tool_color_picked;
}

static void
pika_curves_tool_init (PikaCurvesTool *tool)
{
  gint i;

  for (i = 0; i < G_N_ELEMENTS (tool->picked_color); i++)
    tool->picked_color[i] = -1.0;
}

static gboolean
pika_curves_tool_initialize (PikaTool     *tool,
                             PikaDisplay  *display,
                             GError      **error)
{
  PikaFilterTool       *filter_tool = PIKA_FILTER_TOOL (tool);
  PikaCurvesTool       *c_tool      = PIKA_CURVES_TOOL (tool);
  PikaImage            *image       = pika_display_get_image (display);
  GList                *drawables;
  PikaDrawable         *drawable;
  PikaCurvesConfig     *config;
  PikaHistogram        *histogram;
  PikaHistogramChannel  channel;

  if (! PIKA_TOOL_CLASS (parent_class)->initialize (tool, display, error))
    {
      return FALSE;
    }

  drawables = pika_image_get_selected_drawables (image);
  if (g_list_length (drawables) != 1)
    {
      if (g_list_length (drawables) > 1)
        pika_tool_message_literal (tool, display,
                                   _("Cannot modify multiple drawables. Select only one."));
      else
        pika_tool_message_literal (tool, display, _("No selected drawables."));

      g_list_free (drawables);
      return FALSE;
    }
  drawable = drawables->data;
  g_list_free (drawables);

  config = PIKA_CURVES_CONFIG (filter_tool->config);

  histogram = pika_histogram_new (config->trc);
  g_object_unref (pika_drawable_calculate_histogram_async (drawable, histogram,
                                                           FALSE));
  pika_histogram_view_set_background (PIKA_HISTOGRAM_VIEW (c_tool->graph),
                                      histogram);
  g_object_unref (histogram);

  if (pika_drawable_get_component_type (drawable) == PIKA_COMPONENT_TYPE_U8)
    {
      c_tool->scale = 255.0;

      gtk_spin_button_set_digits (GTK_SPIN_BUTTON (c_tool->point_input),  0);
      gtk_spin_button_set_digits (GTK_SPIN_BUTTON (c_tool->point_output), 0);

      gtk_entry_set_width_chars (GTK_ENTRY (c_tool->point_input),  3);
      gtk_entry_set_width_chars (GTK_ENTRY (c_tool->point_output), 3);
    }
  else
    {
      c_tool->scale = 100.0;

      gtk_spin_button_set_digits (GTK_SPIN_BUTTON (c_tool->point_input),  2);
      gtk_spin_button_set_digits (GTK_SPIN_BUTTON (c_tool->point_output), 2);

      gtk_entry_set_width_chars (GTK_ENTRY (c_tool->point_input),  6);
      gtk_entry_set_width_chars (GTK_ENTRY (c_tool->point_output), 6);
    }

  pika_curve_view_set_range_x (PIKA_CURVE_VIEW (c_tool->graph),
                               0, c_tool->scale);
  pika_curve_view_set_range_y (PIKA_CURVE_VIEW (c_tool->graph),
                               0, c_tool->scale);

  gtk_spin_button_set_range (GTK_SPIN_BUTTON (c_tool->point_output),
                             0, c_tool->scale);

  for (channel = PIKA_HISTOGRAM_VALUE;
       channel <= PIKA_HISTOGRAM_ALPHA;
       channel++)
    {
      g_signal_connect (config->curve[channel], "dirty",
                        G_CALLBACK (curves_curve_dirty_callback),
                        tool);
    }

  pika_curves_tool_update_point (c_tool);

  /*  always pick colors  */
  pika_filter_tool_enable_color_picking (filter_tool, NULL, FALSE);

  return TRUE;
}

static void
pika_curves_tool_button_release (PikaTool              *tool,
                                 const PikaCoords      *coords,
                                 guint32                time,
                                 GdkModifierType        state,
                                 PikaButtonReleaseType  release_type,
                                 PikaDisplay           *display)
{
  PikaCurvesTool   *c_tool      = PIKA_CURVES_TOOL (tool);
  PikaFilterTool   *filter_tool = PIKA_FILTER_TOOL (tool);
  PikaCurvesConfig *config      = PIKA_CURVES_CONFIG (filter_tool->config);

  if (state & pika_get_extend_selection_mask ())
    {
      PikaCurve *curve = config->curve[config->channel];
      gdouble    value = c_tool->picked_color[config->channel];
      gint       point;

      point = pika_curve_get_point_at (curve, value);

      if (point < 0)
        {
          PikaCurvePointType type = PIKA_CURVE_POINT_SMOOTH;

          point = pika_curve_view_get_selected (
            PIKA_CURVE_VIEW (c_tool->graph));

          if (point >= 0)
            type = pika_curve_get_point_type (curve, point);

          point = pika_curve_add_point (
            curve,
            value, pika_curve_map_value (curve, value));

          pika_curve_set_point_type (curve, point, type);
        }

      pika_curve_view_set_selected (PIKA_CURVE_VIEW (c_tool->graph), point);
    }
  else if (state & pika_get_toggle_behavior_mask ())
    {
      PikaHistogramChannel channel;
      PikaCurvePointType   type = PIKA_CURVE_POINT_SMOOTH;
      gint                 point;

      point = pika_curve_view_get_selected (PIKA_CURVE_VIEW (c_tool->graph));

      if (point >= 0)
        {
          type = pika_curve_get_point_type (config->curve[config->channel],
                                            point);
        }

      for (channel = PIKA_HISTOGRAM_VALUE;
           channel <= PIKA_HISTOGRAM_ALPHA;
           channel++)
        {
          PikaCurve *curve = config->curve[channel];
          gdouble    value = c_tool->picked_color[channel];

          if (value != -1)
            {
              point = pika_curve_get_point_at (curve, value);

              if (point < 0)
                {
                  point = pika_curve_add_point (
                    curve,
                    value, pika_curve_map_value (curve, value));

                  pika_curve_set_point_type (curve, point, type);
                }

              if (channel == config->channel)
                {
                  pika_curve_view_set_selected (PIKA_CURVE_VIEW (c_tool->graph),
                                                point);
                }
            }
        }
    }

  /*  chain up to halt the tool */
  PIKA_TOOL_CLASS (parent_class)->button_release (tool, coords, time, state,
                                                  release_type, display);
}

static gboolean
pika_curves_tool_key_press (PikaTool    *tool,
                            GdkEventKey *kevent,
                            PikaDisplay *display)
{
  PikaCurvesTool *c_tool = PIKA_CURVES_TOOL (tool);

  if (tool->display && c_tool->graph)
    {
      if (gtk_widget_event (c_tool->graph, (GdkEvent *) kevent))
        return TRUE;
    }

  return PIKA_TOOL_CLASS (parent_class)->key_press (tool, kevent, display);
}

static void
pika_curves_tool_oper_update (PikaTool         *tool,
                              const PikaCoords *coords,
                              GdkModifierType   state,
                              gboolean          proximity,
                              PikaDisplay      *display)
{
  if (pika_filter_tool_on_guide (PIKA_FILTER_TOOL (tool),
                                 coords, display))
    {
      PIKA_TOOL_CLASS (parent_class)->oper_update (tool, coords, state, proximity,
                                                   display);
    }
  else
    {
      PikaColorPickTarget  target;
      gchar               *status      = NULL;
      GdkModifierType      extend_mask = pika_get_extend_selection_mask ();
      GdkModifierType      toggle_mask = pika_get_toggle_behavior_mask ();

      pika_tool_pop_status (tool, display);

      if (state & extend_mask)
        {
          target = PIKA_COLOR_PICK_TARGET_PALETTE;
          status = g_strdup (_("Click to add a control point"));
        }
      else if (state & toggle_mask)
        {
          target = PIKA_COLOR_PICK_TARGET_PALETTE;
          status = g_strdup (_("Click to add control points to all channels"));
        }
      else
        {
          target = PIKA_COLOR_PICK_TARGET_NONE;
          status = pika_suggest_modifiers (_("Click to locate on curve"),
                                           (extend_mask | toggle_mask) & ~state,
                                           _("%s: add control point"),
                                           _("%s: add control points to all channels"),
                                           NULL);
        }

      PIKA_COLOR_TOOL (tool)->pick_target = target;

      if (proximity)
        pika_tool_push_status (tool, display, "%s", status);

      g_free (status);
    }
}

static gchar *
pika_curves_tool_get_operation (PikaFilterTool  *filter_tool,
                                gchar          **description)
{
  *description = g_strdup (_("Adjust Color Curves"));

  return g_strdup ("pika:curves");
}


/*******************/
/*  Curves dialog  */
/*******************/

static void
pika_curves_tool_dialog (PikaFilterTool *filter_tool)
{
  PikaCurvesTool   *tool         = PIKA_CURVES_TOOL (filter_tool);
  PikaToolOptions  *tool_options = PIKA_TOOL_GET_OPTIONS (filter_tool);
  PikaCurvesConfig *config       = PIKA_CURVES_CONFIG (filter_tool->config);
  GtkListStore     *store;
  GtkWidget        *main_vbox;
  GtkWidget        *frame_vbox;
  GtkWidget        *vbox;
  GtkWidget        *hbox;
  GtkWidget        *hbox2;
  GtkWidget        *label;
  GtkWidget        *main_frame;
  GtkWidget        *frame;
  GtkWidget        *grid;
  GtkWidget        *button;
  GtkWidget        *bar;
  GtkWidget        *combo;

  g_signal_connect (filter_tool->settings_box, "file-dialog-setup",
                    G_CALLBACK (pika_curves_tool_export_setup),
                    filter_tool);

  main_vbox = pika_filter_tool_dialog_get_vbox (filter_tool);

  /*  The combo box for selecting channels  */
  main_frame = pika_frame_new (NULL);
  gtk_box_pack_start (GTK_BOX (main_vbox), main_frame, TRUE, TRUE, 0);
  gtk_widget_show (main_frame);

  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
  gtk_frame_set_label_widget (GTK_FRAME (main_frame), hbox);
  gtk_widget_show (hbox);

  label = gtk_label_new_with_mnemonic (_("Cha_nnel:"));
  pika_label_set_attributes (GTK_LABEL (label),
                             PANGO_ATTR_WEIGHT, PANGO_WEIGHT_BOLD,
                             -1);
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

  store = pika_enum_store_new_with_range (PIKA_TYPE_HISTOGRAM_CHANNEL,
                                          PIKA_HISTOGRAM_VALUE,
                                          PIKA_HISTOGRAM_ALPHA);
  g_set_weak_pointer (&tool->channel_menu,
                      pika_enum_combo_box_new_with_model (PIKA_ENUM_STORE (store)));
  g_object_unref (store);

  pika_int_combo_box_set_active (PIKA_INT_COMBO_BOX (tool->channel_menu),
                                 config->channel);
  pika_enum_combo_box_set_icon_prefix (PIKA_ENUM_COMBO_BOX (tool->channel_menu),
                                       "pika-channel");
  pika_int_combo_box_set_sensitivity (PIKA_INT_COMBO_BOX (tool->channel_menu),
                                      curves_menu_sensitivity, filter_tool, NULL);
  gtk_box_pack_start (GTK_BOX (hbox), tool->channel_menu, FALSE, FALSE, 0);
  gtk_widget_show (tool->channel_menu);

  g_signal_connect (tool->channel_menu, "changed",
                    G_CALLBACK (curves_channel_callback),
                    tool);

  gtk_label_set_mnemonic_widget (GTK_LABEL (label), tool->channel_menu);

  button = gtk_button_new_with_mnemonic (_("R_eset Channel"));
  gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
  gtk_widget_show (button);

  g_signal_connect (button, "clicked",
                    G_CALLBACK (curves_channel_reset_callback),
                    tool);

  /*  The histogram scale radio buttons  */
  hbox2 = pika_prop_enum_icon_box_new (G_OBJECT (tool_options),
                                       "histogram-scale", "pika-histogram",
                                       0, 0);
  gtk_box_pack_end (GTK_BOX (hbox), hbox2, FALSE, FALSE, 0);

  /*  The linear/perceptual radio buttons  */
  hbox2 = pika_prop_enum_icon_box_new (G_OBJECT (config), "trc",
                                       "pika-color-space",
                                       -1, -1);
  gtk_box_pack_end (GTK_BOX (hbox), hbox2, FALSE, FALSE, 0);

  frame_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 4);
  gtk_container_add (GTK_CONTAINER (main_frame), frame_vbox);
  gtk_widget_show (frame_vbox);

  /*  The grid for the color bars and the graph  */
  grid = gtk_grid_new ();
  gtk_grid_set_column_spacing (GTK_GRID (grid), 2);
  gtk_grid_set_row_spacing (GTK_GRID (grid), 2);
  gtk_box_pack_start (GTK_BOX (frame_vbox), grid, TRUE, TRUE, 0);

  /*  The left color bar  */
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  gtk_grid_attach (GTK_GRID (grid), vbox, 0, 0, 1, 1);
  gtk_widget_show (vbox);

  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, RADIUS);
  gtk_widget_show (frame);

  tool->yrange = pika_color_bar_new (GTK_ORIENTATION_VERTICAL);
  gtk_widget_set_size_request (tool->yrange, BAR_SIZE, -1);
  gtk_container_add (GTK_CONTAINER (frame), tool->yrange);
  gtk_widget_show (tool->yrange);

  /*  The curves graph  */
  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  gtk_widget_set_hexpand (frame, TRUE);
  gtk_widget_set_vexpand (frame, TRUE);
  gtk_grid_attach (GTK_GRID (grid), frame, 1, 0, 1, 1);
  gtk_widget_show (frame);

  g_set_weak_pointer (&tool->graph, pika_curve_view_new ());

  pika_curve_view_set_range_x (PIKA_CURVE_VIEW (tool->graph), 0, 255);
  pika_curve_view_set_range_y (PIKA_CURVE_VIEW (tool->graph), 0, 255);
  gtk_widget_set_size_request (tool->graph,
                               GRAPH_SIZE + RADIUS * 2,
                               GRAPH_SIZE + RADIUS * 2);
  g_object_set (tool->graph,
                "border-width", RADIUS,
                "subdivisions", 1,
                NULL);
  gtk_container_add (GTK_CONTAINER (frame), tool->graph);
  gtk_widget_show (tool->graph);

  g_object_bind_property (G_OBJECT (tool_options), "histogram-scale",
                          G_OBJECT (tool->graph),  "histogram-scale",
                          G_BINDING_SYNC_CREATE |
                          G_BINDING_BIDIRECTIONAL);

  g_signal_connect (tool->graph, "selection-changed",
                    G_CALLBACK (curves_graph_selection_callback),
                    tool);

  /*  The bottom color bar  */
  hbox2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
  gtk_grid_attach (GTK_GRID (grid), hbox2, 1, 1, 1, 1);
  gtk_widget_show (hbox2);

  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  gtk_box_pack_start (GTK_BOX (hbox2), frame, TRUE, TRUE, RADIUS);
  gtk_widget_show (frame);

  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  gtk_box_set_homogeneous (GTK_BOX (vbox), TRUE);
  gtk_container_add (GTK_CONTAINER (frame), vbox);
  gtk_widget_show (vbox);

  tool->xrange = pika_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_widget_set_size_request (tool->xrange, -1, BAR_SIZE / 2);
  gtk_box_pack_start (GTK_BOX (vbox), tool->xrange, TRUE, TRUE, 0);
  gtk_widget_show (tool->xrange);

  bar = pika_color_bar_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_box_pack_start (GTK_BOX (vbox), bar, TRUE, TRUE, 0);
  gtk_widget_show (bar);

  gtk_widget_show (grid);

  /*  The point properties box  */
  tool->point_box = hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
  gtk_box_pack_start (GTK_BOX (frame_vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (tool->point_box);

  label = gtk_label_new_with_mnemonic (_("_Input:"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

  tool->point_input = pika_spin_button_new_with_range (0.0, 0.0, 1.0);
  gtk_box_pack_start (GTK_BOX (hbox), tool->point_input, FALSE, FALSE, 0);
  gtk_widget_show (tool->point_input);

  g_signal_connect (tool->point_input, "value-changed",
                    G_CALLBACK (curves_point_coords_callback),
                    tool);

  gtk_label_set_mnemonic_widget (GTK_LABEL (label), tool->point_input);

  label = gtk_label_new_with_mnemonic (_("O_utput:"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

  tool->point_output = pika_spin_button_new_with_range (0.0, 0.0, 1.0);
  gtk_box_pack_start (GTK_BOX (hbox), tool->point_output, FALSE, FALSE, 0);
  gtk_widget_show (tool->point_output);

  g_signal_connect (tool->point_output, "value-changed",
                    G_CALLBACK (curves_point_coords_callback),
                    tool);

  gtk_label_set_mnemonic_widget (GTK_LABEL (label), tool->point_output);

  label = gtk_label_new_with_mnemonic (_("T_ype:"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

  hbox2 = pika_enum_icon_box_new (PIKA_TYPE_CURVE_POINT_TYPE,
                                  "pika-curve-point",
                                  GTK_ICON_SIZE_MENU,
                                  G_CALLBACK (curves_point_type_callback),
                                  tool, NULL,
                                  &tool->point_type);
  gtk_box_pack_start (GTK_BOX (hbox), hbox2, FALSE, FALSE, 0);
  gtk_widget_show (hbox2);

  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
  gtk_box_pack_start (GTK_BOX (frame_vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);

  gtk_label_set_mnemonic_widget (GTK_LABEL (label), tool->point_type);

  label = gtk_label_new_with_mnemonic (_("Curve _type:"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

  /*  The curve-type combo  */
  tool->curve_type = combo = pika_enum_combo_box_new (PIKA_TYPE_CURVE_TYPE);
  pika_enum_combo_box_set_icon_prefix (PIKA_ENUM_COMBO_BOX (combo),
                                       "pika-curve");
  pika_int_combo_box_connect (PIKA_INT_COMBO_BOX (combo), 0,
                              G_CALLBACK (curves_curve_type_callback),
                              tool, NULL);
  gtk_box_pack_start (GTK_BOX (hbox), combo, TRUE, TRUE, 0);
  gtk_widget_show (combo);

  gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);

  pika_curves_tool_update_channel (tool);
}

static void
pika_curves_tool_reset (PikaFilterTool *filter_tool)
{
  PikaHistogramChannel channel;

  g_object_get (filter_tool->config,
                "channel", &channel,
                NULL);

  PIKA_FILTER_TOOL_CLASS (parent_class)->reset (filter_tool);

  g_object_set (filter_tool->config,
                "channel", channel,
                NULL);
}

static void
pika_curves_tool_config_notify (PikaFilterTool   *filter_tool,
                                PikaConfig       *config,
                                const GParamSpec *pspec)
{
  PikaCurvesTool   *curves_tool   = PIKA_CURVES_TOOL (filter_tool);
  PikaCurvesConfig *curves_config = PIKA_CURVES_CONFIG (config);
  PikaCurve        *curve         = curves_config->curve[curves_config->channel];

  PIKA_FILTER_TOOL_CLASS (parent_class)->config_notify (filter_tool,
                                                        config, pspec);

  if (! curves_tool->channel_menu ||
      ! curves_tool->graph)
    return;

  if (! strcmp (pspec->name, "trc"))
    {
      PikaHistogram *histogram;

      histogram = pika_histogram_new (curves_config->trc);
      g_object_unref (pika_drawable_calculate_histogram_async
                      (PIKA_TOOL (filter_tool)->drawables->data, histogram, FALSE));
      pika_histogram_view_set_background (PIKA_HISTOGRAM_VIEW (curves_tool->graph),
                                          histogram);
      g_object_unref (histogram);
    }
  else if (! strcmp (pspec->name, "channel"))
    {
      pika_curves_tool_update_channel (PIKA_CURVES_TOOL (filter_tool));
    }
  else if (! strcmp (pspec->name, "curve"))
    {
      pika_int_combo_box_set_active (PIKA_INT_COMBO_BOX (curves_tool->curve_type),
                                     curve->curve_type);
    }
}

static gboolean
pika_curves_tool_settings_import (PikaFilterTool  *filter_tool,
                                  GInputStream    *input,
                                  GError         **error)
{
  PikaCurvesConfig *config = PIKA_CURVES_CONFIG (filter_tool->config);
  gchar             header[64];
  gsize             bytes_read;

  if (! g_input_stream_read_all (input, header, sizeof (header),
                                 &bytes_read, NULL, error) ||
      bytes_read != sizeof (header))
    {
      g_prefix_error (error, _("Could not read header: "));
      return FALSE;
    }

  g_seekable_seek (G_SEEKABLE (input), 0, G_SEEK_SET, NULL, NULL);

  if (g_str_has_prefix (header, "# PIKA Curves File\n"))
    return pika_curves_config_load_cruft (config, input, error);

  return PIKA_FILTER_TOOL_CLASS (parent_class)->settings_import (filter_tool,
                                                                 input,
                                                                 error);
}

static gboolean
pika_curves_tool_settings_export (PikaFilterTool  *filter_tool,
                                  GOutputStream   *output,
                                  GError         **error)
{
  PikaCurvesTool   *tool   = PIKA_CURVES_TOOL (filter_tool);
  PikaCurvesConfig *config = PIKA_CURVES_CONFIG (filter_tool->config);

  if (tool->export_old_format)
    return pika_curves_config_save_cruft (config, output, error);

  return PIKA_FILTER_TOOL_CLASS (parent_class)->settings_export (filter_tool,
                                                                 output,
                                                                 error);
}

static void
pika_curves_tool_color_picked (PikaFilterTool *filter_tool,
                               gpointer        identifier,
                               gdouble         x,
                               gdouble         y,
                               const Babl     *sample_format,
                               const PikaRGB  *color)
{
  PikaCurvesTool   *tool     = PIKA_CURVES_TOOL (filter_tool);
  PikaCurvesConfig *config   = PIKA_CURVES_CONFIG (filter_tool->config);
  PikaDrawable     *drawable = PIKA_TOOL (tool)->drawables->data;
  PikaRGB           rgb      = *color;

  if (config->trc == PIKA_TRC_LINEAR)
    babl_process (babl_fish (babl_format ("R'G'B'A double"),
                             babl_format ("RGBA double")),
                  &rgb, &rgb, 1);

  tool->picked_color[PIKA_HISTOGRAM_RED]   = rgb.r;
  tool->picked_color[PIKA_HISTOGRAM_GREEN] = rgb.g;
  tool->picked_color[PIKA_HISTOGRAM_BLUE]  = rgb.b;

  if (pika_drawable_has_alpha (drawable))
    tool->picked_color[PIKA_HISTOGRAM_ALPHA] = rgb.a;
  else
    tool->picked_color[PIKA_HISTOGRAM_ALPHA] = -1;

  tool->picked_color[PIKA_HISTOGRAM_VALUE] = MAX (MAX (rgb.r, rgb.g), rgb.b);

  pika_curve_view_set_xpos (PIKA_CURVE_VIEW (tool->graph),
                            tool->picked_color[config->channel]);
}

static void
pika_curves_tool_export_setup (PikaSettingsBox      *settings_box,
                               GtkFileChooserDialog *dialog,
                               gboolean              export,
                               PikaCurvesTool       *tool)
{
  GtkWidget *button;

  if (! export)
    return;

  button = gtk_check_button_new_with_mnemonic (_("Use _old curves file format"));
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button),
                                tool->export_old_format);
  gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (dialog), button);
  gtk_widget_show (button);

  g_signal_connect (button, "toggled",
                    G_CALLBACK (pika_toggle_button_update),
                    &tool->export_old_format);
}

static void
pika_curves_tool_update_channel (PikaCurvesTool *tool)
{
  PikaFilterTool       *filter_tool = PIKA_FILTER_TOOL (tool);
  PikaCurvesConfig     *config      = PIKA_CURVES_CONFIG (filter_tool->config);
  PikaCurve            *curve       = config->curve[config->channel];
  PikaHistogramChannel  channel;

  pika_int_combo_box_set_active (PIKA_INT_COMBO_BOX (tool->channel_menu),
                                 config->channel);

  switch (config->channel)
    {
      guchar r[256];
      guchar g[256];
      guchar b[256];

    case PIKA_HISTOGRAM_VALUE:
    case PIKA_HISTOGRAM_ALPHA:
    case PIKA_HISTOGRAM_RGB:
    case PIKA_HISTOGRAM_LUMINANCE:
      pika_curve_get_uchar (curve, sizeof (r), r);

      pika_color_bar_set_buffers (PIKA_COLOR_BAR (tool->xrange),
                                  r, r, r);
      break;

    case PIKA_HISTOGRAM_RED:
    case PIKA_HISTOGRAM_GREEN:
    case PIKA_HISTOGRAM_BLUE:
      pika_curve_get_uchar (config->curve[PIKA_HISTOGRAM_RED],
                            sizeof (r), r);
      pika_curve_get_uchar (config->curve[PIKA_HISTOGRAM_GREEN],
                            sizeof (g), g);
      pika_curve_get_uchar (config->curve[PIKA_HISTOGRAM_BLUE],
                            sizeof (b), b);

      pika_color_bar_set_buffers (PIKA_COLOR_BAR (tool->xrange),
                                  r, g, b);
      break;
    }

  pika_histogram_view_set_channel (PIKA_HISTOGRAM_VIEW (tool->graph),
                                   config->channel);
  pika_curve_view_set_xpos (PIKA_CURVE_VIEW (tool->graph),
                            tool->picked_color[config->channel]);

  pika_color_bar_set_channel (PIKA_COLOR_BAR (tool->yrange),
                              config->channel);

  pika_curve_view_remove_all_backgrounds (PIKA_CURVE_VIEW (tool->graph));

  for (channel = PIKA_HISTOGRAM_VALUE;
       channel <= PIKA_HISTOGRAM_ALPHA;
       channel++)
    {
      PikaRGB  curve_color;
      gboolean has_color;

      has_color = curves_get_channel_color (tool->graph, channel, &curve_color);

      if (channel == config->channel)
        {
          pika_curve_view_set_curve (PIKA_CURVE_VIEW (tool->graph), curve,
                                     has_color ? &curve_color : NULL);
        }
      else
        {
          pika_curve_view_add_background (PIKA_CURVE_VIEW (tool->graph),
                                          config->curve[channel],
                                          has_color ? &curve_color : NULL);
        }
    }

  pika_int_combo_box_set_active (PIKA_INT_COMBO_BOX (tool->curve_type),
                                 curve->curve_type);

  pika_curves_tool_update_point (tool);
}

static void
pika_curves_tool_update_point (PikaCurvesTool *tool)
{
  PikaFilterTool   *filter_tool = PIKA_FILTER_TOOL (tool);
  PikaCurvesConfig *config      = PIKA_CURVES_CONFIG (filter_tool->config);
  PikaCurve        *curve       = config->curve[config->channel];
  gint              point;

  point = pika_curve_view_get_selected (PIKA_CURVE_VIEW (tool->graph));

  gtk_widget_set_sensitive (tool->point_box, point >= 0);

  if (point >= 0)
    {
      gdouble min = 0.0;
      gdouble max = 1.0;
      gdouble x;
      gdouble y;

      if (point > 0)
        pika_curve_get_point (curve, point - 1, &min, NULL);

      if (point < pika_curve_get_n_points (curve) - 1)
        pika_curve_get_point (curve, point + 1, &max, NULL);

      pika_curve_get_point (curve, point, &x, &y);

      x   *= tool->scale;
      y   *= tool->scale;
      min *= tool->scale;
      max *= tool->scale;

      g_signal_handlers_block_by_func (tool->point_input,
                                       curves_point_coords_callback,
                                       tool);
      g_signal_handlers_block_by_func (tool->point_output,
                                       curves_point_coords_callback,
                                       tool);

      gtk_spin_button_set_range (GTK_SPIN_BUTTON (tool->point_input), min, max);

      gtk_spin_button_set_value (GTK_SPIN_BUTTON (tool->point_input),  x);
      gtk_spin_button_set_value (GTK_SPIN_BUTTON (tool->point_output), y);


      g_signal_handlers_unblock_by_func (tool->point_input,
                                         curves_point_coords_callback,
                                         tool);
      g_signal_handlers_unblock_by_func (tool->point_output,
                                         curves_point_coords_callback,
                                         tool);

      g_signal_handlers_block_by_func (tool->point_type,
                                       curves_point_type_callback,
                                       tool);

      pika_int_radio_group_set_active (
        GTK_RADIO_BUTTON (tool->point_type),
        pika_curve_get_point_type (curve, point));

      g_signal_handlers_unblock_by_func (tool->point_type,
                                         curves_point_type_callback,
                                         tool);
    }
}

static void
curves_curve_dirty_callback (PikaCurve      *curve,
                             PikaCurvesTool *tool)
{
  if (tool->graph &&
      pika_curve_view_get_curve (PIKA_CURVE_VIEW (tool->graph)) == curve)
    {
      pika_curves_tool_update_point (tool);
    }
}

static void
curves_channel_callback (GtkWidget      *widget,
                         PikaCurvesTool *tool)
{
  PikaFilterTool   *filter_tool = PIKA_FILTER_TOOL (tool);
  PikaCurvesConfig *config      = PIKA_CURVES_CONFIG (filter_tool->config);
  gint              value;

  if (pika_int_combo_box_get_active (PIKA_INT_COMBO_BOX (widget), &value) &&
      config->channel != value)
    {
      g_object_set (config,
                    "channel", value,
                    NULL);
    }
}

static void
curves_channel_reset_callback (GtkWidget      *widget,
                               PikaCurvesTool *tool)
{
  PikaFilterTool   *filter_tool = PIKA_FILTER_TOOL (tool);
  PikaCurvesConfig *config      = PIKA_CURVES_CONFIG (filter_tool->config);

  pika_curve_reset (config->curve[config->channel], FALSE);
}

static gboolean
curves_menu_sensitivity (gint      value,
                         gpointer  data)
{
  PikaDrawable         *drawable = PIKA_TOOL (data)->drawables->data;
  PikaHistogramChannel  channel  = value;

  if (!drawable)
    return FALSE;

  switch (channel)
    {
    case PIKA_HISTOGRAM_VALUE:
      return TRUE;

    case PIKA_HISTOGRAM_RED:
    case PIKA_HISTOGRAM_GREEN:
    case PIKA_HISTOGRAM_BLUE:
      return pika_drawable_is_rgb (drawable);

    case PIKA_HISTOGRAM_ALPHA:
      return pika_drawable_has_alpha (drawable);

    case PIKA_HISTOGRAM_RGB:
      return FALSE;

    case PIKA_HISTOGRAM_LUMINANCE:
      return FALSE;
    }

  return FALSE;
}

static void
curves_graph_selection_callback (GtkWidget      *widget,
                                 PikaCurvesTool *tool)
{
  pika_curves_tool_update_point (tool);
}

static void
curves_point_coords_callback (GtkWidget      *widget,
                              PikaCurvesTool *tool)
{
  PikaFilterTool   *filter_tool = PIKA_FILTER_TOOL (tool);
  PikaCurvesConfig *config      = PIKA_CURVES_CONFIG (filter_tool->config);
  PikaCurve        *curve       = config->curve[config->channel];
  gint              point;

  point = pika_curve_view_get_selected (PIKA_CURVE_VIEW (tool->graph));

  if (point >= 0)
    {
      gdouble x;
      gdouble y;

      x = gtk_spin_button_get_value (GTK_SPIN_BUTTON (tool->point_input));
      y = gtk_spin_button_get_value (GTK_SPIN_BUTTON (tool->point_output));

      x /= tool->scale;
      y /= tool->scale;

      pika_curve_set_point (curve, point, x, y);
    }
}

static void
curves_point_type_callback (GtkWidget      *widget,
                            PikaCurvesTool *tool)
{
  PikaFilterTool   *filter_tool = PIKA_FILTER_TOOL (tool);
  PikaCurvesConfig *config      = PIKA_CURVES_CONFIG (filter_tool->config);
  PikaCurve        *curve       = config->curve[config->channel];
  gint              point;

  point = pika_curve_view_get_selected (PIKA_CURVE_VIEW (tool->graph));

  if (point >= 0 && gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
    {
      PikaCurvePointType type;

      type = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget),
                                                 "pika-item-data"));

      pika_curve_set_point_type (curve, point, type);
    }
}

static void
curves_curve_type_callback (GtkWidget      *widget,
                            PikaCurvesTool *tool)
{
  gint value;

  if (pika_int_combo_box_get_active (PIKA_INT_COMBO_BOX (widget), &value))
    {
      PikaFilterTool   *filter_tool = PIKA_FILTER_TOOL (tool);
      PikaCurvesConfig *config      = PIKA_CURVES_CONFIG (filter_tool->config);
      PikaCurveType     curve_type  = value;

      if (config->curve[config->channel]->curve_type != curve_type)
        pika_curve_set_curve_type (config->curve[config->channel], curve_type);
    }
}

static gboolean
curves_get_channel_color (GtkWidget            *widget,
                          PikaHistogramChannel  channel,
                          PikaRGB              *color)
{
  static const PikaRGB channel_colors[PIKA_HISTOGRAM_RGB] =
  {
    { 0.0, 0.0, 0.0, 1.0 },
    { 1.0, 0.0, 0.0, 1.0 },
    { 0.0, 1.0, 0.0, 1.0 },
    { 0.0, 0.0, 1.0, 1.0 },
    { 0.5, 0.5, 0.5, 1.0 }
  };

  GdkRGBA rgba;

  if (channel == PIKA_HISTOGRAM_VALUE)
    return FALSE;

  if (channel == PIKA_HISTOGRAM_ALPHA)
    {
      GtkStyleContext *style = gtk_widget_get_style_context (widget);
      gdouble          lum;

      gtk_style_context_get_color (style, gtk_style_context_get_state (style),
                                   &rgba);

      lum = PIKA_RGB_LUMINANCE (rgba.red, rgba.green, rgba.blue);

      if (lum > 0.5)
        {
          pika_rgba_set (color, lum - 0.3, lum - 0.3, lum - 0.3, 1.0);
        }
      else
        {
          pika_rgba_set (color, lum + 0.3, lum + 0.3, lum + 0.3, 1.0);
        }

      return TRUE;
    }

  *color = channel_colors[channel];

  return TRUE;
}
