/* 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
 *
 * pikatoolline.c
 * Copyright (C) 2017 Michael Natterer <mitch@gimp.org>
 *
 * Major improvements for interactivity
 * Copyright (C) 2014 Michael Henning <drawoc@darkrefraction.com>
 *
 * 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 <gdk/gdkkeysyms.h>

#include "libpikabase/pikabase.h"
#include "libpikamath/pikamath.h"

#include "display-types.h"

#include "core/pika-utils.h"
#include "core/pikamarshal.h"

#include "widgets/pikawidgets-utils.h"

#include "pikacanvasgroup.h"
#include "pikacanvashandle.h"
#include "pikacanvasline.h"
#include "pikadisplayshell.h"
#include "pikadisplayshell-cursor.h"
#include "pikadisplayshell-utils.h"
#include "pikatoolline.h"

#include "pika-intl.h"


#define SHOW_LINE            TRUE
#define GRAB_LINE_MASK       GDK_MOD1_MASK
#define ENDPOINT_HANDLE_TYPE PIKA_HANDLE_CROSS
#define ENDPOINT_HANDLE_SIZE PIKA_CANVAS_HANDLE_SIZE_CROSS
#define SLIDER_HANDLE_TYPE   PIKA_HANDLE_FILLED_DIAMOND
#define SLIDER_HANDLE_SIZE   (ENDPOINT_HANDLE_SIZE * 2 / 3)
#define HANDLE_CIRCLE_SCALE  1.8
#define LINE_VICINITY        ((gint) (SLIDER_HANDLE_SIZE * HANDLE_CIRCLE_SCALE) / 2)
#define SLIDER_TEAR_DISTANCE (5 * LINE_VICINITY)


/* hover-only "handles" */
#define HOVER_NEW_SLIDER     (PIKA_TOOL_LINE_HANDLE_NONE - 1)


typedef enum
{
  GRAB_NONE,
  GRAB_SELECTION,
  GRAB_LINE
} PikaToolLineGrab;

enum
{
  PROP_0,
  PROP_X1,
  PROP_Y1,
  PROP_X2,
  PROP_Y2,
  PROP_SLIDERS,
  PROP_SELECTION,
  PROP_STATUS_TITLE,
};

enum
{
  CAN_ADD_SLIDER,
  ADD_SLIDER,
  PREPARE_TO_REMOVE_SLIDER,
  REMOVE_SLIDER,
  SELECTION_CHANGED,
  HANDLE_CLICKED,
  LAST_SIGNAL
};

struct _PikaToolLinePrivate
{
  gdouble            x1;
  gdouble            y1;
  gdouble            x2;
  gdouble            y2;
  GArray            *sliders;
  gint               selection;
  gchar             *status_title;

  gdouble            saved_x1;
  gdouble            saved_y1;
  gdouble            saved_x2;
  gdouble            saved_y2;
  gdouble            saved_slider_value;

  gdouble            mouse_x;
  gdouble            mouse_y;
  gint               hover;
  gdouble            new_slider_value;
  gboolean           remove_slider;
  PikaToolLineGrab   grab;

  PikaCanvasItem    *line;
  PikaCanvasItem    *start_handle;
  PikaCanvasItem    *end_handle;
  PikaCanvasItem    *slider_group;
  GArray            *slider_handles;
  PikaCanvasItem    *handle_circle;
};


/*  local function prototypes  */

static void     pika_tool_line_constructed     (GObject               *object);
static void     pika_tool_line_finalize        (GObject               *object);
static void     pika_tool_line_set_property    (GObject               *object,
                                                guint                  property_id,
                                                const GValue          *value,
                                                GParamSpec            *pspec);
static void     pika_tool_line_get_property    (GObject               *object,
                                                guint                  property_id,
                                                GValue                *value,
                                                GParamSpec            *pspec);

static void     pika_tool_line_changed         (PikaToolWidget        *widget);
static void     pika_tool_line_focus_changed   (PikaToolWidget        *widget);
static gint     pika_tool_line_button_press    (PikaToolWidget        *widget,
                                                const PikaCoords      *coords,
                                                guint32                time,
                                                GdkModifierType        state,
                                                PikaButtonPressType    press_type);
static void     pika_tool_line_button_release  (PikaToolWidget        *widget,
                                                const PikaCoords      *coords,
                                                guint32                time,
                                                GdkModifierType        state,
                                                PikaButtonReleaseType  release_type);
static void     pika_tool_line_motion          (PikaToolWidget        *widget,
                                                const PikaCoords      *coords,
                                                guint32                time,
                                                GdkModifierType        state);
static PikaHit  pika_tool_line_hit             (PikaToolWidget        *widget,
                                                const PikaCoords      *coords,
                                                GdkModifierType        state,
                                                gboolean               proximity);
static void     pika_tool_line_hover           (PikaToolWidget        *widget,
                                                const PikaCoords      *coords,
                                                GdkModifierType        state,
                                                gboolean               proximity);
static void     pika_tool_line_leave_notify    (PikaToolWidget        *widget);
static gboolean pika_tool_line_key_press       (PikaToolWidget        *widget,
                                                GdkEventKey           *kevent);
static void     pika_tool_line_motion_modifier (PikaToolWidget        *widget,
                                                GdkModifierType        key,
                                                gboolean               press,
                                                GdkModifierType        state);
static gboolean pika_tool_line_get_cursor      (PikaToolWidget        *widget,
                                                const PikaCoords      *coords,
                                                GdkModifierType        state,
                                                PikaCursorType        *cursor,
                                                PikaToolCursorType    *tool_cursor,
                                                PikaCursorModifier    *modifier);

static gint     pika_tool_line_get_hover       (PikaToolLine          *line,
                                                const PikaCoords      *coords,
                                                GdkModifierType        state);
static PikaControllerSlider *
                pika_tool_line_get_slider      (PikaToolLine          *line,
                                                gint                   slider);
static PikaCanvasItem *
                pika_tool_line_get_handle      (PikaToolLine          *line,
                                                gint                   handle);
static gdouble  pika_tool_line_project_point   (PikaToolLine          *line,
                                                gdouble                x,
                                                gdouble                y,
                                                gboolean               constrain,
                                                gdouble               *dist);

static gboolean
               pika_tool_line_selection_motion (PikaToolLine          *line,
                                                gboolean               constrain);

static void     pika_tool_line_update_handles  (PikaToolLine          *line);
static void     pika_tool_line_update_circle   (PikaToolLine          *line);
static void     pika_tool_line_update_hilight  (PikaToolLine          *line);
static void     pika_tool_line_update_status   (PikaToolLine          *line,
                                                GdkModifierType        state,
                                                gboolean               proximity);

static gboolean pika_tool_line_handle_hit      (PikaCanvasItem        *handle,
                                                gdouble                x,
                                                gdouble                y,
                                                gdouble               *min_dist);


G_DEFINE_TYPE_WITH_PRIVATE (PikaToolLine, pika_tool_line, PIKA_TYPE_TOOL_WIDGET)

#define parent_class pika_tool_line_parent_class

static guint line_signals[LAST_SIGNAL] = { 0, };


static void
pika_tool_line_class_init (PikaToolLineClass *klass)
{
  GObjectClass        *object_class = G_OBJECT_CLASS (klass);
  PikaToolWidgetClass *widget_class = PIKA_TOOL_WIDGET_CLASS (klass);

  object_class->constructed     = pika_tool_line_constructed;
  object_class->finalize        = pika_tool_line_finalize;
  object_class->set_property    = pika_tool_line_set_property;
  object_class->get_property    = pika_tool_line_get_property;

  widget_class->changed         = pika_tool_line_changed;
  widget_class->focus_changed   = pika_tool_line_focus_changed;
  widget_class->button_press    = pika_tool_line_button_press;
  widget_class->button_release  = pika_tool_line_button_release;
  widget_class->motion          = pika_tool_line_motion;
  widget_class->hit             = pika_tool_line_hit;
  widget_class->hover           = pika_tool_line_hover;
  widget_class->leave_notify    = pika_tool_line_leave_notify;
  widget_class->key_press       = pika_tool_line_key_press;
  widget_class->motion_modifier = pika_tool_line_motion_modifier;
  widget_class->get_cursor      = pika_tool_line_get_cursor;

  line_signals[CAN_ADD_SLIDER] =
    g_signal_new ("can-add-slider",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (PikaToolLineClass, can_add_slider),
                  NULL, NULL,
                  pika_marshal_BOOLEAN__DOUBLE,
                  G_TYPE_BOOLEAN, 1,
                  G_TYPE_DOUBLE);

  line_signals[ADD_SLIDER] =
    g_signal_new ("add-slider",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (PikaToolLineClass, add_slider),
                  NULL, NULL,
                  pika_marshal_INT__DOUBLE,
                  G_TYPE_INT, 1,
                  G_TYPE_DOUBLE);

  line_signals[PREPARE_TO_REMOVE_SLIDER] =
    g_signal_new ("prepare-to-remove-slider",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (PikaToolLineClass, prepare_to_remove_slider),
                  NULL, NULL,
                  pika_marshal_VOID__INT_BOOLEAN,
                  G_TYPE_NONE, 2,
                  G_TYPE_INT,
                  G_TYPE_BOOLEAN);

  line_signals[REMOVE_SLIDER] =
    g_signal_new ("remove-slider",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (PikaToolLineClass, remove_slider),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 1,
                  G_TYPE_INT);

  line_signals[SELECTION_CHANGED] =
    g_signal_new ("selection-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (PikaToolLineClass, selection_changed),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 0);

  line_signals[HANDLE_CLICKED] =
    g_signal_new ("handle-clicked",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (PikaToolLineClass, handle_clicked),
                  NULL, NULL,
                  pika_marshal_BOOLEAN__INT_UINT_ENUM,
                  G_TYPE_BOOLEAN, 3,
                  G_TYPE_INT,
                  G_TYPE_UINT,
                  PIKA_TYPE_BUTTON_PRESS_TYPE);

  g_object_class_install_property (object_class, PROP_X1,
                                   g_param_spec_double ("x1", NULL, NULL,
                                                        -PIKA_MAX_IMAGE_SIZE,
                                                        PIKA_MAX_IMAGE_SIZE, 0,
                                                        PIKA_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_Y1,
                                   g_param_spec_double ("y1", NULL, NULL,
                                                        -PIKA_MAX_IMAGE_SIZE,
                                                        PIKA_MAX_IMAGE_SIZE, 0,
                                                        PIKA_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_X2,
                                   g_param_spec_double ("x2", NULL, NULL,
                                                        -PIKA_MAX_IMAGE_SIZE,
                                                        PIKA_MAX_IMAGE_SIZE, 0,
                                                        PIKA_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_Y2,
                                   g_param_spec_double ("y2", NULL, NULL,
                                                        -PIKA_MAX_IMAGE_SIZE,
                                                        PIKA_MAX_IMAGE_SIZE, 0,
                                                        PIKA_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_SLIDERS,
                                   g_param_spec_boxed ("sliders", NULL, NULL,
                                                       G_TYPE_ARRAY,
                                                       PIKA_PARAM_READWRITE));

  g_object_class_install_property (object_class, PROP_SELECTION,
                                   g_param_spec_int ("selection", NULL, NULL,
                                                     PIKA_TOOL_LINE_HANDLE_NONE,
                                                     G_MAXINT,
                                                     PIKA_TOOL_LINE_HANDLE_NONE,
                                                     PIKA_PARAM_READWRITE |
                                                     G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_STATUS_TITLE,
                                   g_param_spec_string ("status-title",
                                                        NULL, NULL,
                                                        _("Line: "),
                                                        PIKA_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));
}

static void
pika_tool_line_init (PikaToolLine *line)
{
  PikaToolLinePrivate *private;

  private = line->private = pika_tool_line_get_instance_private (line);

  private->sliders = g_array_new (FALSE, FALSE, sizeof (PikaControllerSlider));

  private->selection = PIKA_TOOL_LINE_HANDLE_NONE;
  private->hover     = PIKA_TOOL_LINE_HANDLE_NONE;
  private->grab      = GRAB_NONE;

  private->slider_handles = g_array_new (FALSE, TRUE,
                                         sizeof (PikaCanvasItem *));
}

static void
pika_tool_line_constructed (GObject *object)
{
  PikaToolLine        *line    = PIKA_TOOL_LINE (object);
  PikaToolWidget      *widget  = PIKA_TOOL_WIDGET (object);
  PikaToolLinePrivate *private = line->private;

  G_OBJECT_CLASS (parent_class)->constructed (object);

  private->line = pika_tool_widget_add_line (widget,
                                             private->x1,
                                             private->y1,
                                             private->x2,
                                             private->y2);

  pika_canvas_item_set_visible (private->line, SHOW_LINE);

  private->start_handle =
    pika_tool_widget_add_handle (widget,
                                 ENDPOINT_HANDLE_TYPE,
                                 private->x1,
                                 private->y1,
                                 ENDPOINT_HANDLE_SIZE,
                                 ENDPOINT_HANDLE_SIZE,
                                 PIKA_HANDLE_ANCHOR_CENTER);

  private->end_handle =
    pika_tool_widget_add_handle (widget,
                                 ENDPOINT_HANDLE_TYPE,
                                 private->x2,
                                 private->y2,
                                 ENDPOINT_HANDLE_SIZE,
                                 ENDPOINT_HANDLE_SIZE,
                                 PIKA_HANDLE_ANCHOR_CENTER);

  private->slider_group =
    pika_canvas_group_new (pika_tool_widget_get_shell (widget));
  pika_tool_widget_add_item (widget, private->slider_group);
  g_object_unref (private->slider_group);

  private->handle_circle =
    pika_tool_widget_add_handle (widget,
                                 PIKA_HANDLE_CIRCLE,
                                 private->x1,
                                 private->y1,
                                 ENDPOINT_HANDLE_SIZE * HANDLE_CIRCLE_SCALE,
                                 ENDPOINT_HANDLE_SIZE * HANDLE_CIRCLE_SCALE,
                                 PIKA_HANDLE_ANCHOR_CENTER);

  pika_tool_line_changed (widget);
}

static void
pika_tool_line_finalize (GObject *object)
{
  PikaToolLine        *line    = PIKA_TOOL_LINE (object);
  PikaToolLinePrivate *private = line->private;

  g_clear_pointer (&private->sliders, g_array_unref);
  g_clear_pointer (&private->status_title, g_free);
  g_clear_pointer (&private->slider_handles, g_array_unref);

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

static void
pika_tool_line_set_property (GObject      *object,
                             guint         property_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  PikaToolLine        *line    = PIKA_TOOL_LINE (object);
  PikaToolLinePrivate *private = line->private;

  switch (property_id)
    {
    case PROP_X1:
      private->x1 = g_value_get_double (value);
      break;
    case PROP_Y1:
      private->y1 = g_value_get_double (value);
      break;
    case PROP_X2:
      private->x2 = g_value_get_double (value);
      break;
    case PROP_Y2:
      private->y2 = g_value_get_double (value);
      break;

    case PROP_SLIDERS:
      {
        GArray   *sliders = g_value_dup_boxed (value);
        gboolean  deselect;

        g_return_if_fail (sliders != NULL);

        deselect =
          PIKA_TOOL_LINE_HANDLE_IS_SLIDER (private->selection) &&
          (sliders->len != private->sliders->len               ||
           ! pika_tool_line_get_slider (line, private->selection)->selectable);

        g_array_unref (private->sliders);
        private->sliders = sliders;

        if (PIKA_TOOL_LINE_HANDLE_IS_SLIDER (private->hover))
          private->hover = PIKA_TOOL_LINE_HANDLE_NONE;

        if (deselect)
          pika_tool_line_set_selection (line, PIKA_TOOL_LINE_HANDLE_NONE);
      }
      break;

    case PROP_SELECTION:
      {
        gint selection = g_value_get_int (value);

        g_return_if_fail (selection < (gint) private->sliders->len);
        g_return_if_fail (selection < 0 ||
                          pika_tool_line_get_slider (line,
                                                     selection)->selectable);

        if (selection != private->selection)
          {
            private->selection = selection;

            if (private->grab == GRAB_SELECTION)
              private->grab = GRAB_NONE;

            g_signal_emit (line, line_signals[SELECTION_CHANGED], 0);
          }
      }
      break;

    case PROP_STATUS_TITLE:
      g_free (private->status_title);
      private->status_title = g_value_dup_string (value);
      if (! private->status_title)
        private->status_title = g_strdup (_("Line: "));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
pika_tool_line_get_property (GObject    *object,
                             guint       property_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
  PikaToolLine        *line    = PIKA_TOOL_LINE (object);
  PikaToolLinePrivate *private = line->private;

  switch (property_id)
    {
    case PROP_X1:
      g_value_set_double (value, private->x1);
      break;
    case PROP_Y1:
      g_value_set_double (value, private->y1);
      break;
    case PROP_X2:
      g_value_set_double (value, private->x2);
      break;
    case PROP_Y2:
      g_value_set_double (value, private->y2);
      break;

    case PROP_SLIDERS:
      g_value_set_boxed (value, private->sliders);
      break;

    case PROP_SELECTION:
      g_value_set_int (value, private->selection);
      break;

    case PROP_STATUS_TITLE:
      g_value_set_string (value, private->status_title);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
pika_tool_line_changed (PikaToolWidget *widget)
{
  PikaToolLine        *line    = PIKA_TOOL_LINE (widget);
  PikaToolLinePrivate *private = line->private;
  gint                 i;

  pika_canvas_line_set (private->line,
                        private->x1,
                        private->y1,
                        private->x2,
                        private->y2);

  pika_canvas_handle_set_position (private->start_handle,
                                   private->x1,
                                   private->y1);

  pika_canvas_handle_set_position (private->end_handle,
                                   private->x2,
                                   private->y2);

  /* remove excessive slider handles */
  for (i = private->sliders->len; i < private->slider_handles->len; i++)
    {
      pika_canvas_group_remove_item (PIKA_CANVAS_GROUP (private->slider_group),
                                     pika_tool_line_get_handle (line, i));
    }

  g_array_set_size (private->slider_handles, private->sliders->len);

  for (i = 0; i < private->sliders->len; i++)
    {
      gdouble          value;
      gdouble          x;
      gdouble          y;
      PikaCanvasItem **handle;

      value = pika_tool_line_get_slider (line, i)->value;

      x = private->x1 + (private->x2 - private->x1) * value;
      y = private->y1 + (private->y2 - private->y1) * value;

      handle = &g_array_index (private->slider_handles, PikaCanvasItem *, i);

      if (*handle)
        {
          pika_canvas_handle_set_position (*handle, x, y);
        }
      else
        {
          *handle = pika_canvas_handle_new (pika_tool_widget_get_shell (widget),
                                            SLIDER_HANDLE_TYPE,
                                            PIKA_HANDLE_ANCHOR_CENTER,
                                            x,
                                            y,
                                            SLIDER_HANDLE_SIZE,
                                            SLIDER_HANDLE_SIZE);

          pika_canvas_group_add_item (PIKA_CANVAS_GROUP (private->slider_group),
                                      *handle);
          g_object_unref (*handle);
        }
    }

  pika_tool_line_update_handles (line);
  pika_tool_line_update_circle (line);
  pika_tool_line_update_hilight (line);
}

static void
pika_tool_line_focus_changed (PikaToolWidget *widget)
{
  PikaToolLine *line = PIKA_TOOL_LINE (widget);

  pika_tool_line_update_hilight (line);
}

gboolean
pika_tool_line_button_press (PikaToolWidget      *widget,
                             const PikaCoords    *coords,
                             guint32              time,
                             GdkModifierType      state,
                             PikaButtonPressType  press_type)
{
  PikaToolLine        *line    = PIKA_TOOL_LINE (widget);
  PikaToolLinePrivate *private = line->private;
  gboolean             result  = FALSE;

  private->grab          = GRAB_NONE;
  private->remove_slider = FALSE;

  private->saved_x1 = private->x1;
  private->saved_y1 = private->y1;
  private->saved_x2 = private->x2;
  private->saved_y2 = private->y2;

  if (press_type         != PIKA_BUTTON_PRESS_NORMAL   &&
      private->hover      > PIKA_TOOL_LINE_HANDLE_NONE &&
      private->selection  > PIKA_TOOL_LINE_HANDLE_NONE)
    {
      g_signal_emit (line, line_signals[HANDLE_CLICKED], 0,
                     private->selection, state, press_type, &result);

      if (! result)
        pika_tool_widget_hover (widget, coords, state, TRUE);
    }

  if (press_type == PIKA_BUTTON_PRESS_NORMAL || ! result)
    {
      private->saved_x1 = private->x1;
      private->saved_y1 = private->y1;
      private->saved_x2 = private->x2;
      private->saved_y2 = private->y2;

      if (PIKA_TOOL_LINE_HANDLE_IS_SLIDER (private->hover))
        {
          private->saved_slider_value =
            pika_tool_line_get_slider (line, private->hover)->value;
        }

      if (private->hover > PIKA_TOOL_LINE_HANDLE_NONE)
        {
          pika_tool_line_set_selection (line, private->hover);

          private->grab = GRAB_SELECTION;
        }
      else if (private->hover == HOVER_NEW_SLIDER)
        {
          gint slider;

          g_signal_emit (line, line_signals[ADD_SLIDER], 0,
                         private->new_slider_value, &slider);

          g_return_val_if_fail (slider < (gint) private->sliders->len, FALSE);

          if (slider >= 0)
            {
              pika_tool_line_set_selection (line, slider);

              private->saved_slider_value =
                pika_tool_line_get_slider (line, private->selection)->value;

              private->grab = GRAB_SELECTION;
            }
        }
      else if (state & GRAB_LINE_MASK)
        {
          private->grab = GRAB_LINE;
        }

      result = (private->grab != GRAB_NONE);
    }

  if (! result)
    {
      private->hover = PIKA_TOOL_LINE_HANDLE_NONE;

      pika_tool_line_set_selection (line, PIKA_TOOL_LINE_HANDLE_NONE);
    }

  pika_tool_line_update_handles (line);
  pika_tool_line_update_circle (line);
  pika_tool_line_update_status (line, state, TRUE);

  return result;
}

void
pika_tool_line_button_release (PikaToolWidget        *widget,
                               const PikaCoords      *coords,
                               guint32                time,
                               GdkModifierType        state,
                               PikaButtonReleaseType  release_type)
{
  PikaToolLine        *line    = PIKA_TOOL_LINE (widget);
  PikaToolLinePrivate *private = line->private;
  PikaToolLineGrab     grab    = private->grab;

  private->grab = GRAB_NONE;

  if (release_type == PIKA_BUTTON_RELEASE_CANCEL)
    {
      if (grab != GRAB_NONE)
        {
          if (grab == GRAB_SELECTION &&
              PIKA_TOOL_LINE_HANDLE_IS_SLIDER (private->selection))
            {
              pika_tool_line_get_slider (line, private->selection)->value =
                private->saved_slider_value;

              if (private->remove_slider)
                {
                  private->remove_slider = FALSE;

                  g_signal_emit (line, line_signals[PREPARE_TO_REMOVE_SLIDER], 0,
                                 private->selection, FALSE);
                }
            }

          g_object_set (line,
                        "x1", private->saved_x1,
                        "y1", private->saved_y1,
                        "x2", private->saved_x2,
                        "y2", private->saved_y2,
                        NULL);
        }
    }
  else if (grab == GRAB_SELECTION)
    {
      if (private->remove_slider)
        {
          private->remove_slider = FALSE;

          g_signal_emit (line, line_signals[REMOVE_SLIDER], 0,
                         private->selection);
        }
      else if (release_type == PIKA_BUTTON_RELEASE_CLICK)
        {
          gboolean result;

          g_signal_emit (line, line_signals[HANDLE_CLICKED], 0,
                         private->selection, state, PIKA_BUTTON_PRESS_NORMAL,
                         &result);
        }
    }
}

void
pika_tool_line_motion (PikaToolWidget   *widget,
                       const PikaCoords *coords,
                       guint32           time,
                       GdkModifierType   state)
{
  PikaToolLine        *line    = PIKA_TOOL_LINE (widget);
  PikaToolLinePrivate *private = line->private;
  gdouble              diff_x  = coords->x - private->mouse_x;
  gdouble              diff_y  = coords->y - private->mouse_y;

  private->mouse_x = coords->x;
  private->mouse_y = coords->y;

  if (private->grab == GRAB_LINE)
    {
      g_object_set (line,
                    "x1", private->x1 + diff_x,
                    "y1", private->y1 + diff_y,
                    "x2", private->x2 + diff_x,
                    "y2", private->y2 + diff_y,
                    NULL);
    }
  else
    {
      gboolean constrain = (state & pika_get_constrain_behavior_mask ()) != 0;

      pika_tool_line_selection_motion (line, constrain);
    }

  pika_tool_line_update_status (line, state, TRUE);
}

PikaHit
pika_tool_line_hit (PikaToolWidget   *widget,
                    const PikaCoords *coords,
                    GdkModifierType   state,
                    gboolean          proximity)
{
  PikaToolLine *line = PIKA_TOOL_LINE (widget);

  if (! (state & GRAB_LINE_MASK))
    {
      gint hover = pika_tool_line_get_hover (line, coords, state);

      if (hover != PIKA_TOOL_LINE_HANDLE_NONE)
        return PIKA_HIT_DIRECT;
    }
  else
    {
      return PIKA_HIT_INDIRECT;
    }

  return PIKA_HIT_NONE;
}

void
pika_tool_line_hover (PikaToolWidget   *widget,
                      const PikaCoords *coords,
                      GdkModifierType   state,
                      gboolean          proximity)
{
  PikaToolLine        *line    = PIKA_TOOL_LINE (widget);
  PikaToolLinePrivate *private = line->private;

  private->mouse_x = coords->x;
  private->mouse_y = coords->y;

  if (! (state & GRAB_LINE_MASK))
    private->hover = pika_tool_line_get_hover (line, coords, state);
  else
    private->hover = PIKA_TOOL_LINE_HANDLE_NONE;

  pika_tool_line_update_handles (line);
  pika_tool_line_update_circle (line);
  pika_tool_line_update_status (line, state, proximity);
}

static void
pika_tool_line_leave_notify (PikaToolWidget *widget)
{
  PikaToolLine        *line    = PIKA_TOOL_LINE (widget);
  PikaToolLinePrivate *private = line->private;

  private->hover = PIKA_TOOL_LINE_HANDLE_NONE;

  pika_tool_line_update_handles (line);
  pika_tool_line_update_circle (line);

  PIKA_TOOL_WIDGET_CLASS (parent_class)->leave_notify (widget);
}

static gboolean
pika_tool_line_key_press (PikaToolWidget *widget,
                          GdkEventKey    *kevent)
{
  PikaToolLine        *line    = PIKA_TOOL_LINE (widget);
  PikaToolLinePrivate *private = line->private;
  PikaDisplayShell    *shell;
  gdouble              pixels  = 1.0;
  gboolean             move_line;

  move_line = kevent->state & GRAB_LINE_MASK;

  if (private->selection == PIKA_TOOL_LINE_HANDLE_NONE && ! move_line)
    return PIKA_TOOL_WIDGET_CLASS (parent_class)->key_press (widget, kevent);

  shell = pika_tool_widget_get_shell (widget);

  if (kevent->state & pika_get_extend_selection_mask ())
    pixels = 10.0;

  if (kevent->state & pika_get_toggle_behavior_mask ())
    pixels = 50.0;

  switch (kevent->keyval)
    {
    case GDK_KEY_Left:
    case GDK_KEY_Right:
    case GDK_KEY_Up:
    case GDK_KEY_Down:
      /* move an endpoint (or both endpoints) */
      if (private->selection < 0 || move_line)
        {
          gdouble xdist, ydist;
          gdouble dx,    dy;

          xdist = FUNSCALEX (shell, pixels);
          ydist = FUNSCALEY (shell, pixels);

          dx = 0.0;
          dy = 0.0;

          switch (kevent->keyval)
            {
            case GDK_KEY_Left:  dx = -xdist; break;
            case GDK_KEY_Right: dx = +xdist; break;
            case GDK_KEY_Up:    dy = -ydist; break;
            case GDK_KEY_Down:  dy = +ydist; break;
            }

          if (private->selection == PIKA_TOOL_LINE_HANDLE_START || move_line)
            {
              g_object_set (line,
                            "x1", private->x1 + dx,
                            "y1", private->y1 + dy,
                            NULL);
            }

          if (private->selection == PIKA_TOOL_LINE_HANDLE_END || move_line)
            {
              g_object_set (line,
                            "x2", private->x2 + dx,
                            "y2", private->y2 + dy,
                            NULL);
            }
        }
      /* move a slider */
      else
        {
          PikaControllerSlider *slider;
          gdouble               dist;
          gdouble               dvalue;

          slider = pika_tool_line_get_slider (line, private->selection);

          if (! slider->movable)
            break;

          dist = pika_canvas_item_transform_distance (private->line,
                                                      private->x1, private->y1,
                                                      private->x2, private->y2);

          if (dist > 0.0)
            dist = pixels / dist;

          dvalue = 0.0;

          switch (kevent->keyval)
            {
            case GDK_KEY_Left:
              if      (private->x1 < private->x2) dvalue = -dist;
              else if (private->x1 > private->x2) dvalue = +dist;
              break;

            case GDK_KEY_Right:
              if      (private->x1 < private->x2) dvalue = +dist;
              else if (private->x1 > private->x2) dvalue = -dist;
              break;

            case GDK_KEY_Up:
              if      (private->y1 < private->y2) dvalue = -dist;
              else if (private->y1 > private->y2) dvalue = +dist;
              break;

            case GDK_KEY_Down:
              if      (private->y1 < private->y2) dvalue = +dist;
              else if (private->y1 > private->y2) dvalue = -dist;
              break;
            }

          if (dvalue != 0.0)
            {
              slider->value += dvalue;
              slider->value  = CLAMP (slider->value, slider->min, slider->max);
              slider->value  = CLAMP (slider->value, 0.0, 1.0);

              g_object_set (line,
                            "sliders", private->sliders,
                            NULL);
            }
        }
      return TRUE;

    case GDK_KEY_BackSpace:
    case GDK_KEY_Delete:
      if (PIKA_TOOL_LINE_HANDLE_IS_SLIDER (private->selection))
        {
          if (pika_tool_line_get_slider (line, private->selection)->removable)
            {
              g_signal_emit (line, line_signals[REMOVE_SLIDER], 0,
                             private->selection);
            }
        }
      return TRUE;
    }

  return PIKA_TOOL_WIDGET_CLASS (parent_class)->key_press (widget, kevent);
}

static void
pika_tool_line_motion_modifier (PikaToolWidget  *widget,
                                GdkModifierType  key,
                                gboolean         press,
                                GdkModifierType  state)
{
  PikaToolLine *line = PIKA_TOOL_LINE (widget);

  if (key == pika_get_constrain_behavior_mask ())
    {
      pika_tool_line_selection_motion (line, press);

      pika_tool_line_update_status (line, state, TRUE);
    }
}

static gboolean
pika_tool_line_get_cursor (PikaToolWidget     *widget,
                           const PikaCoords   *coords,
                           GdkModifierType     state,
                           PikaCursorType     *cursor,
                           PikaToolCursorType *tool_cursor,
                           PikaCursorModifier *modifier)
{
  PikaToolLine        *line    = PIKA_TOOL_LINE (widget);
  PikaToolLinePrivate *private = line->private;

  if (private->grab ==GRAB_LINE || (state & GRAB_LINE_MASK))
    {
      *modifier = PIKA_CURSOR_MODIFIER_MOVE;

      return TRUE;
    }
  else if (private->grab  == GRAB_SELECTION ||
           private->hover >  PIKA_TOOL_LINE_HANDLE_NONE)
    {
      const PikaControllerSlider *slider = NULL;

      if (private->grab == GRAB_SELECTION)
        {
          if (PIKA_TOOL_LINE_HANDLE_IS_SLIDER (private->selection))
            slider = pika_tool_line_get_slider (line, private->selection);
        }
      else if (PIKA_TOOL_LINE_HANDLE_IS_SLIDER (private->hover))
        {
          slider = pika_tool_line_get_slider (line, private->hover);
        }

      if (private->grab == GRAB_SELECTION && slider && private->remove_slider)
        {
          *modifier = PIKA_CURSOR_MODIFIER_MINUS;

          return TRUE;
        }
      else if (! slider || slider->movable)
        {
          *modifier = PIKA_CURSOR_MODIFIER_MOVE;

          return TRUE;
        }
    }
  else if (private->hover == HOVER_NEW_SLIDER)
    {
      *modifier = PIKA_CURSOR_MODIFIER_PLUS;

      return TRUE;
    }

  return FALSE;
}

static gint
pika_tool_line_get_hover (PikaToolLine     *line,
                          const PikaCoords *coords,
                          GdkModifierType   state)
{
  PikaToolLinePrivate *private = line->private;
  gint                 hover   = PIKA_TOOL_LINE_HANDLE_NONE;
  gdouble              min_dist;
  gint                 first_handle;
  gint                 i;

  /* find the closest handle to the cursor */
  min_dist     = G_MAXDOUBLE;
  first_handle = private->sliders->len - 1;

  /* skip the sliders if the two endpoints are the same, in particular so
   * that if the line is created during a button-press event (as in the
   * blend tool), the end endpoint is dragged, instead of a slider.
   */
  if (private->x1 == private->x2 && private->y1 == private->y2)
    first_handle = -1;

  for (i = first_handle; i > PIKA_TOOL_LINE_HANDLE_NONE; i--)
    {
      PikaCanvasItem *handle;

      if (PIKA_TOOL_LINE_HANDLE_IS_SLIDER (i))
        {
          const PikaControllerSlider *slider;

          slider = pika_tool_line_get_slider (line, i);

          if (! slider->visible || ! slider->selectable)
            continue;
        }

      handle = pika_tool_line_get_handle (line, i);

      if (pika_tool_line_handle_hit (handle,
                                     private->mouse_x,
                                     private->mouse_y,
                                     &min_dist))
        {
          hover = i;
        }
    }

  if (hover == PIKA_TOOL_LINE_HANDLE_NONE)
    {
      gboolean constrain;
      gdouble  value;
      gdouble  dist;

      constrain = (state & pika_get_constrain_behavior_mask ()) != 0;

      value = pika_tool_line_project_point (line,
                                            private->mouse_x,
                                            private->mouse_y,
                                            constrain,
                                            &dist);

      if (value >= 0.0 && value <= 1.0 && dist <= LINE_VICINITY)
        {
          gboolean can_add;

          g_signal_emit (line, line_signals[CAN_ADD_SLIDER], 0,
                         value, &can_add);

          if (can_add)
            {
              hover                     = HOVER_NEW_SLIDER;
              private->new_slider_value = value;
            }
        }
    }

  return hover;
}

static PikaControllerSlider *
pika_tool_line_get_slider (PikaToolLine *line,
                           gint          slider)
{
  PikaToolLinePrivate *private = line->private;

  pika_assert (slider >= 0 && slider < private->sliders->len);

  return &g_array_index (private->sliders, PikaControllerSlider, slider);
}

static PikaCanvasItem *
pika_tool_line_get_handle (PikaToolLine *line,
                           gint          handle)
{
  PikaToolLinePrivate *private = line->private;

  switch (handle)
    {
    case PIKA_TOOL_LINE_HANDLE_NONE:
      return NULL;

    case PIKA_TOOL_LINE_HANDLE_START:
      return private->start_handle;

    case PIKA_TOOL_LINE_HANDLE_END:
      return private->end_handle;

    default:
      pika_assert (handle >= 0 &&
                   handle <  (gint) private->slider_handles->len);

      return g_array_index (private->slider_handles,
                            PikaCanvasItem *, handle);
    }
}

static gdouble
pika_tool_line_project_point (PikaToolLine *line,
                              gdouble       x,
                              gdouble       y,
                              gboolean      constrain,
                              gdouble      *dist)
{
  PikaToolLinePrivate *private = line->private;
  gdouble              length_sqr;
  gdouble              value   = 0.0;

  length_sqr = SQR (private->x2 - private->x1) +
               SQR (private->y2 - private->y1);

  /* don't calculate the projection for 0-length lines, since we'll just get
   * NaN.
   */
  if (length_sqr > 0.0)
    {
      value  = (private->x2 - private->x1) * (x - private->x1) +
               (private->y2 - private->y1) * (y - private->y1);
      value /= length_sqr;

      if (dist)
        {
          gdouble px;
          gdouble py;

          px = private->x1 + (private->x2 - private->x1) * value;
          py = private->y1 + (private->y2 - private->y1) * value;

          *dist = pika_canvas_item_transform_distance (private->line,
                                                       x,  y,
                                                       px, py);
        }

      if (constrain)
        value = RINT (12.0 * value) / 12.0;
    }
  else
    {
      if (dist)
        {
          *dist = pika_canvas_item_transform_distance (private->line,
                                                       x,           y,
                                                       private->x1, private->y1);
        }
    }

  return value;
}

static gboolean
pika_tool_line_selection_motion (PikaToolLine *line,
                                 gboolean      constrain)
{
  PikaToolLinePrivate *private = line->private;
  gdouble              x       = private->mouse_x;
  gdouble              y       = private->mouse_y;

  if (private->grab != GRAB_SELECTION)
    return FALSE;

  switch (private->selection)
    {
    case PIKA_TOOL_LINE_HANDLE_NONE:
      pika_assert_not_reached ();

    case PIKA_TOOL_LINE_HANDLE_START:
      if (constrain)
        {
          pika_display_shell_constrain_line (
            pika_tool_widget_get_shell (PIKA_TOOL_WIDGET (line)),
            private->x2, private->y2,
            &x, &y,
            PIKA_CONSTRAIN_LINE_15_DEGREES);
        }

      g_object_set (line,
                    "x1", x,
                    "y1", y,
                    NULL);
      return TRUE;

    case PIKA_TOOL_LINE_HANDLE_END:
      if (constrain)
        {
          pika_display_shell_constrain_line (
            pika_tool_widget_get_shell (PIKA_TOOL_WIDGET (line)),
            private->x1, private->y1,
            &x, &y,
            PIKA_CONSTRAIN_LINE_15_DEGREES);
        }

      g_object_set (line,
                    "x2", x,
                    "y2", y,
                    NULL);
      return TRUE;

    default:
      {
        PikaDisplayShell     *shell;
        PikaControllerSlider *slider;
        gdouble               value;
        gdouble               dist;
        gboolean              remove_slider;

        shell = pika_tool_widget_get_shell (PIKA_TOOL_WIDGET (line));

        slider = pika_tool_line_get_slider (line, private->selection);

        /* project the cursor position onto the line */
        value = pika_tool_line_project_point (line, x, y, constrain, &dist);

        /* slider dragging */
        if (slider->movable)
          {
            value = CLAMP (value, slider->min, slider->max);
            value = CLAMP (value, 0.0,         1.0);

            value = fabs (value); /* avoid negative zero */

            slider->value = value;

            g_object_set (line,
                          "sliders", private->sliders,
                          NULL);
          }

        /* slider tearing */
        remove_slider = slider->removable && dist > SLIDER_TEAR_DISTANCE;

        if (remove_slider != private->remove_slider)
          {
            private->remove_slider = remove_slider;

            g_signal_emit (line, line_signals[PREPARE_TO_REMOVE_SLIDER], 0,
                           private->selection, remove_slider);

            /* set the cursor modifier to a minus by talking to the shell
             * directly -- eek!
             */
            {
              PikaCursorType     cursor;
              PikaToolCursorType tool_cursor;
              PikaCursorModifier modifier;

              cursor      = shell->current_cursor;
              tool_cursor = shell->tool_cursor;
              modifier    = PIKA_CURSOR_MODIFIER_NONE;

              pika_tool_line_get_cursor (PIKA_TOOL_WIDGET (line), NULL, 0,
                                         &cursor, &tool_cursor, &modifier);

              pika_display_shell_set_cursor (shell, cursor, tool_cursor, modifier);
            }

            pika_tool_line_update_handles (line);
            pika_tool_line_update_circle (line);
            pika_tool_line_update_status (line,
                                          constrain ?
                                            pika_get_constrain_behavior_mask () :
                                            0,
                                          TRUE);
          }

        return TRUE;
      }
    }
}

static void
pika_tool_line_update_handles (PikaToolLine *line)
{
  PikaToolLinePrivate *private = line->private;
  gdouble              value;
  gdouble              dist;
  gint                 i;

  value = pika_tool_line_project_point (line,
                                        private->mouse_x,
                                        private->mouse_y,
                                        FALSE,
                                        &dist);

  for (i = 0; i < private->sliders->len; i++)
    {
      const PikaControllerSlider *slider;
      PikaCanvasItem             *handle;
      gint                        size;
      gint                        hit_radius;
      gboolean                    show_autohidden;
      gboolean                    visible;

      slider = pika_tool_line_get_slider (line, i);
      handle = pika_tool_line_get_handle (line, i);

      size = slider->size * SLIDER_HANDLE_SIZE;
      size = MAX (size, 1);

      hit_radius = (MAX (size, SLIDER_HANDLE_SIZE) * HANDLE_CIRCLE_SCALE) / 2;

      /* show a autohidden slider if it's selected, or if no other handle is
       * grabbed or hovered-over, and the cursor is close enough to the line,
       * between the slider's min and max values.
       */
      show_autohidden = private->selection == i                        ||
                        (private->grab == GRAB_NONE                    &&
                         (private->hover <= PIKA_TOOL_LINE_HANDLE_NONE ||
                          private->hover == i)                         &&
                         dist <= hit_radius                            &&
                         value >= slider->min                          &&
                         value <= slider->max);

      visible = slider->visible                         &&
                (! slider->autohide || show_autohidden) &&
                ! (private->selection == i && private->remove_slider);

      handle = pika_tool_line_get_handle (line, i);

      if (visible)
        {
          g_object_set (handle,
                        "type",   slider->type,
                        "width",  size,
                        "height", size,
                        NULL);
        }

      pika_canvas_item_set_visible (handle, visible);
    }
}

static void
pika_tool_line_update_circle (PikaToolLine *line)
{
  PikaToolLinePrivate *private = line->private;
  gboolean             visible;

  visible = (private->grab == GRAB_NONE                    &&
             private->hover != PIKA_TOOL_LINE_HANDLE_NONE) ||
            (private->grab == GRAB_SELECTION               &&
             private->remove_slider);

  if (visible)
    {
      gdouble  x;
      gdouble  y;
      gint     width;
      gint     height;
      gboolean dashed;

      if (private->grab == GRAB_NONE && private->hover == HOVER_NEW_SLIDER)
        {
          /* new slider */
          x = private->x1 +
              (private->x2 - private->x1) * private->new_slider_value;
          y = private->y1 +
              (private->y2 - private->y1) * private->new_slider_value;

          width = height = SLIDER_HANDLE_SIZE;

          dashed = TRUE;
        }
      else
        {
          PikaCanvasItem *handle;

          if (private->grab == GRAB_SELECTION)
            {
              /* tear slider */
              handle = pika_tool_line_get_handle (line, private->selection);
              dashed = TRUE;
            }
          else
            {
              /* hover over handle */
              handle = pika_tool_line_get_handle (line, private->hover);
              dashed = FALSE;
            }

          pika_canvas_handle_get_position (handle, &x,     &y);
          pika_canvas_handle_get_size     (handle, &width, &height);
        }

      width   = MAX (width,  SLIDER_HANDLE_SIZE);
      height  = MAX (height, SLIDER_HANDLE_SIZE);

      width  *= HANDLE_CIRCLE_SCALE;
      height *= HANDLE_CIRCLE_SCALE;

      pika_canvas_handle_set_position (private->handle_circle, x,     y);
      pika_canvas_handle_set_size     (private->handle_circle, width, height);

      g_object_set (private->handle_circle,
                    "type", dashed ? PIKA_HANDLE_DASHED_CIRCLE :
                                     PIKA_HANDLE_CIRCLE,
                    NULL);
    }

  pika_canvas_item_set_visible (private->handle_circle, visible);
}

static void
pika_tool_line_update_hilight (PikaToolLine *line)
{
  PikaToolLinePrivate *private = line->private;
  gboolean             focus;
  gint                 i;

  focus = pika_tool_widget_get_focus (PIKA_TOOL_WIDGET (line));

  for (i = PIKA_TOOL_LINE_HANDLE_NONE + 1;
       i < (gint) private->sliders->len;
       i++)
    {
      PikaCanvasItem *handle;

      handle = pika_tool_line_get_handle (line, i);

      pika_canvas_item_set_highlight (handle, focus && i == private->selection);
    }
}

static void
pika_tool_line_update_status (PikaToolLine    *line,
                              GdkModifierType  state,
                              gboolean         proximity)
{
  PikaToolLinePrivate *private = line->private;

  if (proximity)
    {
      PikaDisplayShell *shell;
      const gchar      *toggle_behavior_format = NULL;
      const gchar      *message                = NULL;
      gchar            *line_status            = NULL;
      gchar            *status;
      gint              handle;

      shell = pika_tool_widget_get_shell (PIKA_TOOL_WIDGET (line));

      if (private->grab == GRAB_SELECTION)
        handle = private->selection;
      else
        handle = private->hover;

      if (handle == PIKA_TOOL_LINE_HANDLE_START ||
          handle == PIKA_TOOL_LINE_HANDLE_END)
        {
          line_status = pika_display_shell_get_line_status (shell,
                                                            _("Click-Drag to move the endpoint"),
                                                            ". ",
                                                            private->x1,
                                                            private->y1,
                                                            private->x2,
                                                            private->y2);
          toggle_behavior_format = _("%s for constrained angles");
        }
      else if (PIKA_TOOL_LINE_HANDLE_IS_SLIDER (handle) ||
               handle == HOVER_NEW_SLIDER)
        {
          if (private->grab == GRAB_SELECTION && private->remove_slider)
            {
              message = _("Release to remove the slider");
            }
          else
            {
              toggle_behavior_format = _("%s for constrained values");

              if (PIKA_TOOL_LINE_HANDLE_IS_SLIDER (handle))
                {
                  if (pika_tool_line_get_slider (line, handle)->movable)
                    {
                      if (pika_tool_line_get_slider (line, handle)->removable)
                        {
                          if (private->grab == GRAB_SELECTION)
                            {
                              message = _("Click-Drag to move the slider; "
                                          "drag away to remove the slider");
                            }
                          else
                            {
                              message = _("Click-Drag to move or remove the slider");
                            }
                        }
                      else
                        {
                          message = _("Click-Drag to move the slider");
                        }
                    }
                  else
                    {
                      toggle_behavior_format = NULL;

                      if (pika_tool_line_get_slider (line, handle)->removable)
                        {
                          if (private->grab == GRAB_SELECTION)
                            {
                              message = _("Click-Drag away to remove the slider");
                            }
                          else
                            {
                              message = _("Click-Drag to remove the slider");
                            }
                        }
                      else
                        {
                          message = NULL;
                        }
                    }
                }
              else
                {
                  message = _("Click or Click-Drag to add a new slider");
                }
            }
        }
      else if (state & GDK_MOD1_MASK)
        {
          message = _("Click-Drag to move the line");
        }

      status =
        pika_suggest_modifiers (message ? message : (line_status ? line_status : ""),
                                ((toggle_behavior_format ?
                                   pika_get_constrain_behavior_mask () : 0) |
                                 (private->grab == GRAB_NONE ?
                                   GDK_MOD1_MASK : 0)) &
                                ~state,
                                NULL,
                                toggle_behavior_format,
                                _("%s to move the whole line"));

      if (message || line_status)
        {
          pika_tool_widget_set_status (PIKA_TOOL_WIDGET (line), status);
        }
      else
        {
          line_status = pika_display_shell_get_line_status (shell,
                                                            private->status_title,
                                                            ". ",
                                                            private->x1,
                                                            private->y1,
                                                            private->x2,
                                                            private->y2);
          pika_tool_widget_set_status_coords (PIKA_TOOL_WIDGET (line),
                                              line_status,
                                              private->x2 - private->x1,
                                              ", ",
                                              private->y2 - private->y1,
                                              status);
        }

      g_free (status);
      if (line_status)
        g_free (line_status);
    }
  else
    {
      pika_tool_widget_set_status (PIKA_TOOL_WIDGET (line), NULL);
    }
}

static gboolean
pika_tool_line_handle_hit (PikaCanvasItem *handle,
                           gdouble         x,
                           gdouble         y,
                           gdouble        *min_dist)
{
  gdouble handle_x;
  gdouble handle_y;
  gint    handle_width;
  gint    handle_height;
  gint    radius;
  gdouble dist;

  pika_canvas_handle_get_position (handle, &handle_x,     &handle_y);
  pika_canvas_handle_get_size     (handle, &handle_width, &handle_height);

  handle_width  = MAX (handle_width,  SLIDER_HANDLE_SIZE);
  handle_height = MAX (handle_height, SLIDER_HANDLE_SIZE);

  radius = ((gint) (handle_width * HANDLE_CIRCLE_SCALE)) / 2;
  radius = MAX (radius, LINE_VICINITY);

  dist = pika_canvas_item_transform_distance (handle,
                                              x, y, handle_x, handle_y);

  if (dist <= radius && dist < *min_dist)
    {
      *min_dist = dist;

      return TRUE;
    }
  else
    {
      return FALSE;
    }
}


/*  public functions  */

PikaToolWidget *
pika_tool_line_new (PikaDisplayShell *shell,
                    gdouble           x1,
                    gdouble           y1,
                    gdouble           x2,
                    gdouble           y2)
{
  g_return_val_if_fail (PIKA_IS_DISPLAY_SHELL (shell), NULL);

  return g_object_new (PIKA_TYPE_TOOL_LINE,
                       "shell", shell,
                       "x1",    x1,
                       "y1",    y1,
                       "x2",    x2,
                       "y2",    y2,
                       NULL);
}

void
pika_tool_line_set_sliders (PikaToolLine               *line,
                            const PikaControllerSlider *sliders,
                            gint                        n_sliders)
{
  PikaToolLinePrivate *private;

  g_return_if_fail (PIKA_IS_TOOL_LINE (line));
  g_return_if_fail (n_sliders == 0 || (n_sliders > 0 && sliders != NULL));

  private = line->private;

  if (PIKA_TOOL_LINE_HANDLE_IS_SLIDER (private->selection) &&
      private->sliders->len != n_sliders)
    {
      pika_tool_line_set_selection (line, PIKA_TOOL_LINE_HANDLE_NONE);
    }

  g_array_set_size (private->sliders, n_sliders);

  memcpy (private->sliders->data, sliders,
          n_sliders * sizeof (PikaControllerSlider));

  g_object_set (line,
                "sliders", private->sliders,
                NULL);
}

const PikaControllerSlider *
pika_tool_line_get_sliders (PikaToolLine *line,
                            gint         *n_sliders)
{
  PikaToolLinePrivate *private;

  g_return_val_if_fail (PIKA_IS_TOOL_LINE (line), NULL);

  private = line->private;

  if (n_sliders) *n_sliders = private->sliders->len;

  return (const PikaControllerSlider *) private->sliders->data;
}

void
pika_tool_line_set_selection (PikaToolLine *line,
                              gint          handle)
{
  PikaToolLinePrivate *private;

  g_return_if_fail (PIKA_IS_TOOL_LINE (line));

  private = line->private;

  g_return_if_fail (handle >= PIKA_TOOL_LINE_HANDLE_NONE &&
                    handle <  (gint) private->sliders->len);

  g_object_set (line,
                "selection", handle,
                NULL);
}

gint
pika_tool_line_get_selection (PikaToolLine *line)
{
  PikaToolLinePrivate *private;

  g_return_val_if_fail (PIKA_IS_TOOL_LINE (line), PIKA_TOOL_LINE_HANDLE_NONE);

  private = line->private;

  return private->selection;
}
