gimptoolline.c 45.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimptoolline.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 <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gegl.h>
#include <gtk/gtk.h>
28
#include <gdk/gdkkeysyms.h>
29 30 31 32 33 34 35

#include "libgimpbase/gimpbase.h"
#include "libgimpmath/gimpmath.h"

#include "display-types.h"

#include "core/gimp-utils.h"
36
#include "core/gimpmarshal.h"
37 38 39

#include "widgets/gimpwidgets-utils.h"

40
#include "gimpcanvasgroup.h"
41 42 43
#include "gimpcanvashandle.h"
#include "gimpcanvasline.h"
#include "gimpdisplayshell.h"
44
#include "gimpdisplayshell-cursor.h"
45 46
#include "gimptoolline.h"

47 48
#include "gimp-intl.h"

49

50 51 52 53 54 55 56 57
#define SHOW_LINE            TRUE
#define GRAB_LINE_MASK       GDK_MOD1_MASK
#define ENDPOINT_HANDLE_TYPE GIMP_HANDLE_CROSS
#define ENDPOINT_HANDLE_SIZE GIMP_CANVAS_HANDLE_SIZE_CROSS
#define SLIDER_HANDLE_TYPE   GIMP_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)
58 59 60 61 62
#define SLIDER_TEAR_DISTANCE (5 * LINE_VICINITY)


/* hover-only "handles" */
#define HOVER_NEW_SLIDER     (GIMP_TOOL_LINE_HANDLE_NONE - 1)
63 64 65 66


typedef enum
{
67 68 69 70
  GRAB_NONE,
  GRAB_SELECTION,
  GRAB_LINE
} GimpToolLineGrab;
71 72 73 74 75 76 77

enum
{
  PROP_0,
  PROP_X1,
  PROP_Y1,
  PROP_X2,
78
  PROP_Y2,
79
  PROP_SLIDERS,
80
  PROP_SELECTION,
81
  PROP_STATUS_TITLE,
82 83
};

84 85
enum
{
86 87 88
  CAN_ADD_SLIDER,
  ADD_SLIDER,
  REMOVE_SLIDER,
89 90 91 92
  SELECTION_CHANGED,
  LAST_SIGNAL
};

93 94 95 96 97 98
struct _GimpToolLinePrivate
{
  gdouble            x1;
  gdouble            y1;
  gdouble            x2;
  gdouble            y2;
99
  GArray            *sliders;
100
  gint               selection;
101
  gchar             *status_title;
102 103 104 105 106

  gdouble            saved_x1;
  gdouble            saved_y1;
  gdouble            saved_x2;
  gdouble            saved_y2;
107
  gdouble            saved_slider_value;
108 109 110

  gdouble            mouse_x;
  gdouble            mouse_y;
111
  gint               hover;
112 113
  gdouble            new_slider_value;
  gboolean           remove_slider;
114
  GimpToolLineGrab   grab;
115 116

  GimpCanvasItem    *line;
117 118 119 120 121
  GimpCanvasItem    *start_handle;
  GimpCanvasItem    *end_handle;
  GimpCanvasItem    *slider_group;
  GArray            *slider_handles;
  GimpCanvasItem    *handle_circle;
122 123 124 125 126 127
};


/*  local function prototypes  */

static void     gimp_tool_line_constructed     (GObject               *object);
128
static void     gimp_tool_line_finalize        (GObject               *object);
129 130 131 132 133 134 135 136 137 138
static void     gimp_tool_line_set_property    (GObject               *object,
                                                guint                  property_id,
                                                const GValue          *value,
                                                GParamSpec            *pspec);
static void     gimp_tool_line_get_property    (GObject               *object,
                                                guint                  property_id,
                                                GValue                *value,
                                                GParamSpec            *pspec);

static void     gimp_tool_line_changed         (GimpToolWidget        *widget);
139
static gint     gimp_tool_line_button_press    (GimpToolWidget        *widget,
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
                                                const GimpCoords      *coords,
                                                guint32                time,
                                                GdkModifierType        state,
                                                GimpButtonPressType    press_type);
static void     gimp_tool_line_button_release  (GimpToolWidget        *widget,
                                                const GimpCoords      *coords,
                                                guint32                time,
                                                GdkModifierType        state,
                                                GimpButtonReleaseType  release_type);
static void     gimp_tool_line_motion          (GimpToolWidget        *widget,
                                                const GimpCoords      *coords,
                                                guint32                time,
                                                GdkModifierType        state);
static void     gimp_tool_line_hover           (GimpToolWidget        *widget,
                                                const GimpCoords      *coords,
                                                GdkModifierType        state,
                                                gboolean               proximity);
157 158
static gboolean gimp_tool_line_key_press       (GimpToolWidget        *widget,
                                                GdkEventKey           *kevent);
159 160 161 162 163 164 165 166 167
static void     gimp_tool_line_motion_modifier (GimpToolWidget        *widget,
                                                GdkModifierType        key,
                                                gboolean               press,
                                                GdkModifierType        state);
static gboolean gimp_tool_line_get_cursor      (GimpToolWidget        *widget,
                                                const GimpCoords      *coords,
                                                GdkModifierType        state,
                                                GimpCursorType        *cursor,
                                                GimpToolCursorType    *tool_cursor,
168
                                                GimpCursorModifier    *modifier);
169

170 171 172
static GimpControllerSlider *
                gimp_tool_line_get_slider      (GimpToolLine          *line,
                                                gint                   slider);
173 174 175
static GimpCanvasItem *
                gimp_tool_line_get_handle      (GimpToolLine          *line,
                                                gint                   handle);
176 177 178 179 180
static gdouble  gimp_tool_line_project_point   (GimpToolLine          *line,
                                                gdouble                x,
                                                gdouble                y,
                                                gboolean               constrain,
                                                gdouble               *dist);
181 182 183

static gboolean
               gimp_tool_line_selection_motion (GimpToolLine          *line,
184
                                                gboolean               constrain);
185

186
static void     gimp_tool_line_update_handles  (GimpToolLine          *line);
187
static void     gimp_tool_line_update_hilight  (GimpToolLine          *line);
188 189 190
static void     gimp_tool_line_update_status   (GimpToolLine          *line,
                                                GdkModifierType        state,
                                                gboolean               proximity);
191

192 193 194 195 196
static gboolean gimp_tool_line_handle_hit      (GimpCanvasItem        *handle,
                                                gdouble                x,
                                                gdouble                y,
                                                gdouble               *min_dist);

197 198 199 200 201

G_DEFINE_TYPE (GimpToolLine, gimp_tool_line, GIMP_TYPE_TOOL_WIDGET)

#define parent_class gimp_tool_line_parent_class

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

204 205 206 207 208 209 210 211

static void
gimp_tool_line_class_init (GimpToolLineClass *klass)
{
  GObjectClass        *object_class = G_OBJECT_CLASS (klass);
  GimpToolWidgetClass *widget_class = GIMP_TOOL_WIDGET_CLASS (klass);

  object_class->constructed     = gimp_tool_line_constructed;
212
  object_class->finalize        = gimp_tool_line_finalize;
213 214 215 216 217 218 219 220
  object_class->set_property    = gimp_tool_line_set_property;
  object_class->get_property    = gimp_tool_line_get_property;

  widget_class->changed         = gimp_tool_line_changed;
  widget_class->button_press    = gimp_tool_line_button_press;
  widget_class->button_release  = gimp_tool_line_button_release;
  widget_class->motion          = gimp_tool_line_motion;
  widget_class->hover           = gimp_tool_line_hover;
221
  widget_class->key_press       = gimp_tool_line_key_press;
222 223 224
  widget_class->motion_modifier = gimp_tool_line_motion_modifier;
  widget_class->get_cursor      = gimp_tool_line_get_cursor;

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  line_signals[CAN_ADD_SLIDER] =
    g_signal_new ("can-add-slider",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GimpToolLineClass, can_add_slider),
                  NULL, NULL,
                  gimp_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 (GimpToolLineClass, add_slider),
                  NULL, NULL,
                  gimp_marshal_INT__DOUBLE,
                  G_TYPE_INT, 1,
                  G_TYPE_DOUBLE);

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

255 256 257 258 259 260 261 262 263
  line_signals[SELECTION_CHANGED] =
    g_signal_new ("selection-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpToolLineClass, selection_changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
  g_object_class_install_property (object_class, PROP_X1,
                                   g_param_spec_double ("x1", NULL, NULL,
                                                        -GIMP_MAX_IMAGE_SIZE,
                                                        GIMP_MAX_IMAGE_SIZE, 0,
                                                        GIMP_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_Y1,
                                   g_param_spec_double ("y1", NULL, NULL,
                                                        -GIMP_MAX_IMAGE_SIZE,
                                                        GIMP_MAX_IMAGE_SIZE, 0,
                                                        GIMP_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_X2,
                                   g_param_spec_double ("x2", NULL, NULL,
                                                        -GIMP_MAX_IMAGE_SIZE,
                                                        GIMP_MAX_IMAGE_SIZE, 0,
                                                        GIMP_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_Y2,
                                   g_param_spec_double ("y2", NULL, NULL,
                                                        -GIMP_MAX_IMAGE_SIZE,
                                                        GIMP_MAX_IMAGE_SIZE, 0,
                                                        GIMP_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));

292 293 294 295 296
  g_object_class_install_property (object_class, PROP_SLIDERS,
                                   g_param_spec_boxed ("sliders", NULL, NULL,
                                                       G_TYPE_ARRAY,
                                                       GIMP_PARAM_READWRITE));

297 298 299 300 301 302 303 304
  g_object_class_install_property (object_class, PROP_SELECTION,
                                   g_param_spec_int ("selection", NULL, NULL,
                                                     GIMP_TOOL_LINE_HANDLE_NONE,
                                                     G_MAXINT,
                                                     GIMP_TOOL_LINE_HANDLE_NONE,
                                                     GIMP_PARAM_READWRITE |
                                                     G_PARAM_CONSTRUCT));

305 306 307 308 309 310 311
  g_object_class_install_property (object_class, PROP_STATUS_TITLE,
                                   g_param_spec_string ("status-title",
                                                        NULL, NULL,
                                                        _("Line: "),
                                                        GIMP_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));

312 313 314 315 316 317
  g_type_class_add_private (klass, sizeof (GimpToolLinePrivate));
}

static void
gimp_tool_line_init (GimpToolLine *line)
{
318
  GimpToolLinePrivate *private;
319

320 321 322 323 324 325
  private = line->private = G_TYPE_INSTANCE_GET_PRIVATE (line,
                                                         GIMP_TYPE_TOOL_LINE,
                                                         GimpToolLinePrivate);

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

326 327 328 329 330 331
  private->selection = GIMP_TOOL_LINE_HANDLE_NONE;
  private->hover     = GIMP_TOOL_LINE_HANDLE_NONE;
  private->grab      = GRAB_NONE;

  private->slider_handles = g_array_new (FALSE, TRUE,
                                         sizeof (GimpCanvasItem *));
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
}

static void
gimp_tool_line_constructed (GObject *object)
{
  GimpToolLine        *line    = GIMP_TOOL_LINE (object);
  GimpToolWidget      *widget  = GIMP_TOOL_WIDGET (object);
  GimpToolLinePrivate *private = line->private;

  G_OBJECT_CLASS (parent_class)->constructed (object);

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

  gimp_canvas_item_set_visible (private->line, SHOW_LINE);

351
  private->start_handle =
352
    gimp_tool_widget_add_handle (widget,
353
                                 ENDPOINT_HANDLE_TYPE,
354 355
                                 private->x1,
                                 private->y1,
356 357
                                 ENDPOINT_HANDLE_SIZE,
                                 ENDPOINT_HANDLE_SIZE,
358 359
                                 GIMP_HANDLE_ANCHOR_CENTER);

360
  private->end_handle =
361
    gimp_tool_widget_add_handle (widget,
362
                                 ENDPOINT_HANDLE_TYPE,
363 364
                                 private->x2,
                                 private->y2,
365 366
                                 ENDPOINT_HANDLE_SIZE,
                                 ENDPOINT_HANDLE_SIZE,
367 368
                                 GIMP_HANDLE_ANCHOR_CENTER);

369 370 371 372 373 374
  private->slider_group =
    gimp_canvas_group_new (gimp_tool_widget_get_shell (widget));
  gimp_tool_widget_add_item (widget, private->slider_group);
  g_object_unref (private->slider_group);

  private->handle_circle =
375
    gimp_tool_widget_add_handle (widget,
376 377 378 379 380
                                 GIMP_HANDLE_CIRCLE,
                                 private->x1,
                                 private->y1,
                                 ENDPOINT_HANDLE_SIZE * HANDLE_CIRCLE_SCALE,
                                 ENDPOINT_HANDLE_SIZE * HANDLE_CIRCLE_SCALE,
381
                                 GIMP_HANDLE_ANCHOR_CENTER);
382 383

  gimp_tool_line_changed (widget);
384 385
}

386 387 388 389 390 391
static void
gimp_tool_line_finalize (GObject *object)
{
  GimpToolLine        *line    = GIMP_TOOL_LINE (object);
  GimpToolLinePrivate *private = line->private;

392 393
  g_clear_pointer (&private->sliders, g_array_unref);
  g_clear_pointer (&private->status_title, g_free);
394
  g_clear_pointer (&private->slider_handles, g_array_unref);
395

396 397 398
  G_OBJECT_CLASS (parent_class)->finalize (object);
}

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
static void
gimp_tool_line_set_property (GObject      *object,
                             guint         property_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  GimpToolLine        *line    = GIMP_TOOL_LINE (object);
  GimpToolLinePrivate *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;

423
    case PROP_SLIDERS:
424 425 426 427 428 429 430 431 432 433 434 435 436
      {
        GArray *sliders = g_value_dup_boxed (value);

        g_return_if_fail (sliders != NULL);

        if (GIMP_TOOL_LINE_HANDLE_IS_SLIDER (private->selection) &&
            sliders->len != private->sliders->len)
          {
            gimp_tool_line_set_selection (line, GIMP_TOOL_LINE_HANDLE_NONE);
          }

        if (GIMP_TOOL_LINE_HANDLE_IS_SLIDER (private->hover))
          private->hover = GIMP_TOOL_LINE_HANDLE_NONE;
437

438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
        g_array_unref (private->sliders);
        private->sliders = sliders;
      }
      break;

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

        g_return_if_fail (selection < (gint) private->sliders->len);

        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);
          }
      }
459 460
      break;

461 462 463 464 465 466 467
    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;

468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_tool_line_get_property (GObject    *object,
                             guint       property_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
  GimpToolLine        *line    = GIMP_TOOL_LINE (object);
  GimpToolLinePrivate *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;

498 499 500 501
    case PROP_SLIDERS:
      g_value_set_boxed (value, private->sliders);
      break;

502 503 504 505
    case PROP_SELECTION:
      g_value_set_int (value, private->selection);
      break;

506 507 508 509
    case PROP_STATUS_TITLE:
      g_value_set_string (value, private->status_title);
      break;

510 511 512 513 514 515 516 517 518 519 520
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_tool_line_changed (GimpToolWidget *widget)
{
  GimpToolLine        *line    = GIMP_TOOL_LINE (widget);
  GimpToolLinePrivate *private = line->private;
521
  gint                 i;
522 523 524 525 526 527 528

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

529
  gimp_canvas_handle_set_position (private->start_handle,
530 531 532
                                   private->x1,
                                   private->y1);

533
  gimp_canvas_handle_set_position (private->end_handle,
534 535 536
                                   private->x2,
                                   private->y2);

537
  /* remove excessive slider handles */
538
  for (i = private->sliders->len; i < private->slider_handles->len; i++)
539
    {
540 541
      gimp_canvas_group_remove_item (GIMP_CANVAS_GROUP (private->slider_group),
                                     gimp_tool_line_get_handle (line, i));
542 543
    }

544
  g_array_set_size (private->slider_handles, private->sliders->len);
545 546 547

  for (i = 0; i < private->sliders->len; i++)
    {
548
      gdouble          value;
549 550
      gdouble          x;
      gdouble          y;
551
      GimpCanvasItem **handle;
552

553
      value = gimp_tool_line_get_slider (line, i)->value;
554

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

558
      handle = &g_array_index (private->slider_handles, GimpCanvasItem *, i);
559

560
      if (*handle)
561
        {
562
          gimp_canvas_handle_set_position (*handle, x, y);
563 564 565
        }
      else
        {
566 567 568 569 570 571 572 573 574 575 576
          *handle = gimp_canvas_handle_new (gimp_tool_widget_get_shell (widget),
                                            SLIDER_HANDLE_TYPE,
                                            GIMP_HANDLE_ANCHOR_CENTER,
                                            x,
                                            y,
                                            SLIDER_HANDLE_SIZE,
                                            SLIDER_HANDLE_SIZE);

          gimp_canvas_group_add_item (GIMP_CANVAS_GROUP (private->slider_group),
                                      *handle);
          g_object_unref (*handle);
577 578 579
        }
    }

580
  gimp_tool_line_update_handles (line);
581 582 583 584 585 586 587 588 589 590 591 592 593
  gimp_tool_line_update_hilight (line);
}

gboolean
gimp_tool_line_button_press (GimpToolWidget      *widget,
                             const GimpCoords    *coords,
                             guint32              time,
                             GdkModifierType      state,
                             GimpButtonPressType  press_type)
{
  GimpToolLine        *line    = GIMP_TOOL_LINE (widget);
  GimpToolLinePrivate *private = line->private;

594 595
  private->grab          = GRAB_NONE;
  private->remove_slider = FALSE;
596 597 598 599 600 601 602

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

  if (GIMP_TOOL_LINE_HANDLE_IS_SLIDER (private->hover))
603
    {
604 605 606
      private->saved_slider_value =
        gimp_tool_line_get_slider (line, private->hover)->value;
    }
607

608
  if (private->hover > GIMP_TOOL_LINE_HANDLE_NONE)
609 610
    {
      gimp_tool_line_set_selection (line, private->hover);
611

612 613
      private->grab = GRAB_SELECTION;
    }
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
  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)
        {
          gimp_tool_line_set_selection (line, slider);

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

          private->grab = GRAB_SELECTION;
        }
    }
633 634 635 636
  else if (state & GRAB_LINE_MASK)
    {
      private->grab = GRAB_LINE;
    }
637

638 639 640
  if (grab == GRAB_NONE)
    {
      private->hover = GIMP_TOOL_LINE_HANDLE_NONE;
641

642
      gimp_tool_line_set_selection (line, GIMP_TOOL_LINE_HANDLE_NONE);
643 644
    }

645
  gimp_tool_line_update_handles (line);
646 647
  gimp_tool_line_update_status (line, state, TRUE);

648
  return private->grab != GRAB_NONE;
649 650 651 652 653 654 655 656 657 658 659
}

void
gimp_tool_line_button_release (GimpToolWidget        *widget,
                               const GimpCoords      *coords,
                               guint32                time,
                               GdkModifierType        state,
                               GimpButtonReleaseType  release_type)
{
  GimpToolLine        *line    = GIMP_TOOL_LINE (widget);
  GimpToolLinePrivate *private = line->private;
660
  GimpToolLineGrab     grab    = private->grab;
661

662 663 664
  private->grab = GRAB_NONE;

  if (release_type == GIMP_BUTTON_RELEASE_CANCEL)
665
    {
666
      if (grab != GRAB_NONE)
667
        {
668 669 670 671 672 673
          if (grab == GRAB_SELECTION &&
              GIMP_TOOL_LINE_HANDLE_IS_SLIDER (private->selection))
            {
              gimp_tool_line_get_slider (line, private->selection)->value =
                private->saved_slider_value;
            }
674

675 676 677 678 679 680 681 682 683 684 685 686
          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 && private->remove_slider)
    {
      g_signal_emit (line, line_signals[REMOVE_SLIDER], 0,
                     private->selection);
687 688 689 690 691 692 693 694 695 696 697
    }
}

void
gimp_tool_line_motion (GimpToolWidget   *widget,
                       const GimpCoords *coords,
                       guint32           time,
                       GdkModifierType   state)
{
  GimpToolLine        *line    = GIMP_TOOL_LINE (widget);
  GimpToolLinePrivate *private = line->private;
698 699
  gdouble              diff_x  = coords->x - private->mouse_x;
  gdouble              diff_y  = coords->y - private->mouse_y;
700 701 702 703

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

704
  if (private->grab == GRAB_LINE)
705 706
    {
      g_object_set (line,
707 708 709 710
                    "x1", private->x1 + diff_x,
                    "y1", private->y1 + diff_y,
                    "x2", private->x2 + diff_x,
                    "y2", private->y2 + diff_y,
711 712 713 714 715 716
                    NULL);
    }
  else
    {
      gboolean constrain = (state & gimp_get_constrain_behavior_mask ()) != 0;

717
      gimp_tool_line_selection_motion (line, constrain);
718
    }
719 720

  gimp_tool_line_update_status (line, state, TRUE);
721 722 723 724 725 726 727 728 729 730
}

void
gimp_tool_line_hover (GimpToolWidget   *widget,
                      const GimpCoords *coords,
                      GdkModifierType   state,
                      gboolean          proximity)
{
  GimpToolLine        *line    = GIMP_TOOL_LINE (widget);
  GimpToolLinePrivate *private = line->private;
731
  gint                 i;
732 733 734 735

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

736
  private->hover = GIMP_TOOL_LINE_HANDLE_NONE;
737

738
  if (! (state & GRAB_LINE_MASK))
739
    {
740 741 742 743 744 745 746 747 748 749 750 751
      /* find the closest handle to the cursor */
      gdouble min_dist     = G_MAXDOUBLE;
      gint    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 > GIMP_TOOL_LINE_HANDLE_NONE; i--)
752
        {
753
          GimpCanvasItem *handle;
754

755
          handle = gimp_tool_line_get_handle (line, i);
756

757
          if (gimp_tool_line_handle_hit (handle,
758
                                         private->mouse_x,
759 760
                                         private->mouse_y,
                                         &min_dist))
761
            {
762
              private->hover = i;
763 764
            }
        }
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793

      if (private->hover == GIMP_TOOL_LINE_HANDLE_NONE)
        {
          gboolean constrain;
          gdouble  value;
          gdouble  dist;

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

          value = gimp_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)
                {
                  private->hover            = HOVER_NEW_SLIDER;
                  private->new_slider_value = value;
                }
            }
        }
794 795
    }

796
  gimp_tool_line_update_handles (line);
797
  gimp_tool_line_update_status (line, state, proximity);
798 799
}

800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
static gboolean
gimp_tool_line_key_press (GimpToolWidget *widget,
                          GdkEventKey    *kevent)
{
  GimpToolLine        *line    = GIMP_TOOL_LINE (widget);
  GimpToolLinePrivate *private = line->private;
  GimpDisplayShell    *shell;
  gdouble              pixels  = 1.0;
  gboolean             move_line;

  move_line = kevent->state & GRAB_LINE_MASK;

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

  shell = gimp_tool_widget_get_shell (widget);

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

  if (kevent->state & gimp_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 == GIMP_TOOL_LINE_HANDLE_START || move_line)
            {
              g_object_set (line,
                            "x1", private->x1 + dx,
                            "y1", private->y1 + dy,
                            NULL);
            }

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

          dist = gimp_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)
            {
              GimpControllerSlider *slider;

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

              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;
919 920 921 922 923 924 925 926 927

    case GDK_KEY_BackSpace:
    case GDK_KEY_Delete:
      if (GIMP_TOOL_LINE_HANDLE_IS_SLIDER (private->selection))
        {
          g_signal_emit (line, line_signals[REMOVE_SLIDER], 0,
                         private->selection);
        }
      return TRUE;
928 929 930 931 932
    }

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

933 934 935 936 937 938 939 940 941 942
static void
gimp_tool_line_motion_modifier (GimpToolWidget  *widget,
                                GdkModifierType  key,
                                gboolean         press,
                                GdkModifierType  state)
{
  GimpToolLine *line = GIMP_TOOL_LINE (widget);

  if (key == gimp_get_constrain_behavior_mask ())
    {
943
      gimp_tool_line_selection_motion (line, press);
944 945

      gimp_tool_line_update_status (line, state, TRUE);
946 947 948 949 950 951 952 953 954
    }
}

static gboolean
gimp_tool_line_get_cursor (GimpToolWidget     *widget,
                           const GimpCoords   *coords,
                           GdkModifierType     state,
                           GimpCursorType     *cursor,
                           GimpToolCursorType *tool_cursor,
955
                           GimpCursorModifier *modifier)
956
{
957 958
  GimpToolLine        *line    = GIMP_TOOL_LINE (widget);
  GimpToolLinePrivate *private = line->private;
959

960 961 962 963 964 965 966 967
  if (private->grab ==GRAB_LINE || (state & GRAB_LINE_MASK))
    {
      *modifier = GIMP_CURSOR_MODIFIER_MOVE;

      return TRUE;
    }
  else if (private->grab  == GRAB_SELECTION ||
           private->hover >  GIMP_TOOL_LINE_HANDLE_NONE)
968
    {
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
      if (private->grab == GRAB_SELECTION                      &&
          GIMP_TOOL_LINE_HANDLE_IS_SLIDER (private->selection) &&
          private->remove_slider)
        {
          *modifier = GIMP_CURSOR_MODIFIER_MINUS;
        }
      else
        {
          *modifier = GIMP_CURSOR_MODIFIER_MOVE;
        }

      return TRUE;
    }
  else if (private->hover == HOVER_NEW_SLIDER)
    {
      *modifier = GIMP_CURSOR_MODIFIER_PLUS;
985 986 987 988 989 990 991

      return TRUE;
    }

  return FALSE;
}

992 993 994 995 996 997 998 999 1000 1001 1002
static GimpControllerSlider *
gimp_tool_line_get_slider (GimpToolLine *line,
                           gint          slider)
{
  GimpToolLinePrivate *private = line->private;

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

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

1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
static GimpCanvasItem *
gimp_tool_line_get_handle (GimpToolLine *line,
                           gint          handle)
{
  GimpToolLinePrivate *private = line->private;

  switch (handle)
    {
    case GIMP_TOOL_LINE_HANDLE_NONE:
      return NULL;

    case GIMP_TOOL_LINE_HANDLE_START:
      return private->start_handle;

    case GIMP_TOOL_LINE_HANDLE_END:
      return private->end_handle;

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

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

1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
static gdouble
gimp_tool_line_project_point (GimpToolLine *line,
                              gdouble       x,
                              gdouble       y,
                              gboolean      constrain,
                              gdouble      *dist)
{
  GimpToolLinePrivate *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 = gimp_canvas_item_transform_distance (private->line,
                                                       x,  y,
                                                       px, py);
        }

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

  return value;
}

1081
static gboolean
1082 1083
gimp_tool_line_selection_motion (GimpToolLine *line,
                                 gboolean      constrain)
1084 1085 1086 1087 1088
{
  GimpToolLinePrivate *private = line->private;
  gdouble              x       = private->mouse_x;
  gdouble              y       = private->mouse_y;

1089 1090 1091 1092
  if (private->grab != GRAB_SELECTION)
    return FALSE;

  switch (private->selection)
1093
    {
1094 1095 1096 1097
    case GIMP_TOOL_LINE_HANDLE_NONE:
      g_assert_not_reached ();

    case GIMP_TOOL_LINE_HANDLE_START:
1098
      if (constrain)
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
        gimp_constrain_line (private->x2, private->y2,
                             &x, &y,
                             GIMP_CONSTRAIN_LINE_15_DEGREES);

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

1109
    case GIMP_TOOL_LINE_HANDLE_END:
1110
      if (constrain)
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
        gimp_constrain_line (private->x1, private->y1,
                             &x, &y,
                             GIMP_CONSTRAIN_LINE_15_DEGREES);

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

1121
    default:
1122
      {
1123 1124 1125 1126
        GimpDisplayShell     *shell;
        GimpControllerSlider *slider;
        gdouble               value;
        gdouble               dist;
1127

1128
        shell = gimp_tool_widget_get_shell (GIMP_TOOL_WIDGET (line));
1129

1130
        slider = gimp_tool_line_get_slider (line, private->selection);
1131

1132 1133
        /* project the cursor position onto the line */
        value = gimp_tool_line_project_point (line, x, y, constrain, &dist);
1134

1135 1136
        value = CLAMP (value, slider->min, slider->max);
        value = CLAMP (value, 0.0,         1.0);
1137

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

1140
        slider->value = value;
1141

1142 1143 1144
        g_object_set (line,
                      "sliders", private->sliders,
                      NULL);
1145

1146 1147
        /* slider tearing */
        private->remove_slider = dist > SLIDER_TEAR_DISTANCE;
1148

1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
        /* eek! */
        {
          GimpCursorType     cursor;
          GimpToolCursorType tool_cursor;
          GimpCursorModifier modifier;

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

          gimp_tool_line_get_cursor (GIMP_TOOL_WIDGET (line), NULL, 0,
                                     &cursor, &tool_cursor, &modifier);

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

        gimp_tool_line_update_handles (line);
1166 1167 1168

        return TRUE;
      }
1169 1170 1171 1172
    }
}

static void
1173
gimp_tool_line_update_handles (GimpToolLine *line)
1174 1175
{
  GimpToolLinePrivate *private = line->private;
1176
  gboolean             visible;
1177

1178 1179 1180 1181
  visible = (private->grab == GRAB_NONE                    &&
             private->hover != GIMP_TOOL_LINE_HANDLE_NONE) ||
            (private->grab == GRAB_SELECTION               &&
             private->remove_slider);
1182

1183
  if (visible)
1184
    {
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
      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;
1200

1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
          dashed = TRUE;
        }
      else
        {
          GimpCanvasItem *handle;

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

          gimp_canvas_handle_get_position (handle, &x,     &y);
          gimp_canvas_handle_get_size     (handle, &width, &height);
        }
1223

1224 1225
      width   = MAX (width,  SLIDER_HANDLE_SIZE);
      height  = MAX (height, SLIDER_HANDLE_SIZE);
1226

1227 1228
      width  *= HANDLE_CIRCLE_SCALE;
      height *= HANDLE_CIRCLE_SCALE;
1229

1230 1231
      gimp_canvas_handle_set_position (private->handle_circle, x,     y);
      gimp_canvas_handle_set_size     (private->handle_circle, width, height);
1232 1233 1234 1235 1236

      g_object_set (private->handle_circle,
                    "type", dashed ? GIMP_HANDLE_DASHED_CIRCLE :
                                     GIMP_HANDLE_CIRCLE,
                    NULL);
1237
    }
1238 1239

  gimp_canvas_item_set_visible (private->handle_circle, visible);
1240
}
1241

1242 1243 1244 1245
static void
gimp_tool_line_update_hilight (GimpToolLine *line)
{
  GimpToolLinePrivate *private = line->private;
1246
  gint                 i;
1247

1248 1249 1250
  for (i = GIMP_TOOL_LINE_HANDLE_NONE + 1;
       i < (gint) private->sliders->len;
       i++)
1251
    {
1252
      GimpCanvasItem *handle;
1253

1254
      handle = gimp_tool_line_get_handle (line, i);
1255

1256
      gimp_canvas_item_set_highlight (handle, i == private->selection);
1257
    }
1258 1259
}

1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
static void
gimp_tool_line_update_status (GimpToolLine    *line,
                              GdkModifierType  state,
                              gboolean         proximity)
{
  GimpToolLinePrivate *private = line->private;

  if (proximity)
    {
      gchar *status_help =
        gimp_suggest_modifiers ("",
                                (gimp_get_constrain_behavior_mask () |
                                 GDK_MOD1_MASK) &
                                ~state,
                                NULL,
                                _("%s for constrained angles"),
                                _("%s to move the whole line"));

      gimp_tool_widget_set_status_coords (GIMP_TOOL_WIDGET (line),
1279
                                          private->status_title,
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
                                          private->x2 - private->x1,
                                          ", ",
                                          private->y2 - private->y1,
                                          status_help);

      g_free (status_help);
    }
  else
    {
      gimp_tool_widget_set_status (GIMP_TOOL_WIDGET (line), NULL);
    }
}

1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
static gboolean
gimp_tool_line_handle_hit (GimpCanvasItem *handle,
                           gdouble         x,
                           gdouble         y,
                           gdouble        *min_dist)
{
  gdouble handle_x;
  gdouble handle_y;
  gint    handle_width;
  gint    handle_height;
  gint    radius;
  gdouble dist;

  gimp_canvas_handle_get_position (handle, &handle_x,     &handle_y);
  gimp_canvas_handle_get_size     (handle, &handle_width, &handle_height);

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

  dist = gimp_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;
    }
}

1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346

/*  public functions  */

GimpToolWidget *
gimp_tool_line_new (GimpDisplayShell *shell,
                    gdouble           x1,
                    gdouble           y1,
                    gdouble           x2,
                    gdouble           y2)
{
  g_return_val_if_fail (GIMP_IS_DISPLAY_SHELL (shell), NULL);

  return g_object_new (GIMP_TYPE_TOOL_LINE,
                       "shell", shell,
                       "x1",    x1,
                       "y1",    y1,
                       "x2",    x2,
                       "y2",    y2,
                       NULL);
}
1347 1348 1349 1350

void
gimp_tool_line_set_sliders (GimpToolLine               *line,
                            const GimpControllerSlider *sliders,
1351
                            gint                        n_sliders)
1352 1353 1354 1355
{
  GimpToolLinePrivate *private;

  g_return_if_fail (GIMP_IS_TOOL_LINE (line));
1356
  g_return_if_fail (n_sliders == 0 || (n_sliders > 0 && sliders != NULL));
1357 1358 1359

  private = line->private;

1360 1361 1362 1363 1364 1365
  if (GIMP_TOOL_LINE_HANDLE_IS_SLIDER (private->selection) &&
      private->sliders->len != n_sliders)
    {
      gimp_tool_line_set_selection (line, GIMP_TOOL_LINE_HANDLE_NONE);
    }

1366
  g_array_set_size (private->sliders, n_sliders);
1367 1368

  memcpy (private->sliders->data, sliders,
1369
          n_sliders * sizeof (GimpControllerSlider));
1370 1371 1372 1373 1374 1375 1376 1377

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

const GimpControllerSlider *
gimp_tool_line_get_sliders (GimpToolLine *line,
1378
                            gint         *n_sliders)
1379 1380 1381 1382 1383 1384 1385
{
  GimpToolLinePrivate *private;

  g_return_val_if_fail (GIMP_IS_TOOL_LINE (line), NULL);

  private = line->private;

1386
  if (n_sliders) *n_sliders = private->sliders->len;
1387 1388 1389

  return (const GimpControllerSlider *) private->sliders->data;
}
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419

void
gimp_tool_line_set_selection (GimpToolLine *line,
                              gint          handle)
{
  GimpToolLinePrivate *private;

  g_return_if_fail (GIMP_IS_TOOL_LINE (line));

  private = line->private;

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

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

gint
gimp_tool_line_get_selection (GimpToolLine *line)
{
  GimpToolLinePrivate *private;

  g_return_val_if_fail (GIMP_IS_TOOL_LINE (line), GIMP_TOOL_LINE_HANDLE_NONE);

  private = line->private;

  return private->selection;
}