gimptoolwidget.c 31.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimptoolwidget.c
 * Copyright (C) 2017 Michael Natterer <mitch@gimp.org>
 *
 * 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
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 20 21 22
 */

#include "config.h"

23 24
#include <stdarg.h>

25 26
#include <gegl.h>
#include <gtk/gtk.h>
27
#include <gdk/gdkkeysyms.h>
28

29 30
#include "libgimpbase/gimpbase.h"

31 32
#include "display-types.h"

33 34
#include "core/gimpmarshal.h"

35
#include "gimpcanvasarc.h"
36
#include "gimpcanvascorner.h"
37 38 39
#include "gimpcanvasgroup.h"
#include "gimpcanvashandle.h"
#include "gimpcanvasline.h"
40
#include "gimpcanvaspath.h"
41
#include "gimpcanvaspolygon.h"
42 43
#include "gimpcanvasrectangle.h"
#include "gimpcanvasrectangleguides.h"
44
#include "gimpcanvastransformguides.h"
45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include "gimpdisplayshell.h"
#include "gimptoolwidget.h"


enum
{
  PROP_0,
  PROP_SHELL,
  PROP_ITEM
};

enum
{
  CHANGED,
59
  RESPONSE,
60
  SNAP_OFFSETS,
61
  STATUS,
62
  STATUS_COORDS,
63
  MESSAGE,
64
  FOCUS_CHANGED,
65 66 67 68 69 70 71 72
  LAST_SIGNAL
};

struct _GimpToolWidgetPrivate
{
  GimpDisplayShell *shell;
  GimpCanvasItem   *item;
  GList            *group_stack;
73 74 75 76 77

  gint              snap_offset_x;
  gint              snap_offset_y;
  gint              snap_width;
  gint              snap_height;
78

79
  gboolean          visible;
80
  gboolean          focus;
81 82 83 84 85
};


/*  local function prototypes  */

86 87 88 89 90 91 92 93 94 95 96 97 98 99
static void     gimp_tool_widget_finalize           (GObject         *object);
static void     gimp_tool_widget_constructed        (GObject         *object);
static void     gimp_tool_widget_set_property       (GObject         *object,
                                                     guint            property_id,
                                                     const GValue    *value,
                                                     GParamSpec      *pspec);
static void     gimp_tool_widget_get_property       (GObject         *object,
                                                     guint            property_id,
                                                     GValue          *value,
                                                     GParamSpec      *pspec);
static void     gimp_tool_widget_properties_changed (GObject         *object,
                                                     guint            n_pspecs,
                                                     GParamSpec     **pspecs);

100
static void     gimp_tool_widget_real_leave_notify  (GimpToolWidget  *widget);
101 102
static gboolean gimp_tool_widget_real_key_press     (GimpToolWidget  *widget,
                                                     GdkEventKey     *kevent);
103 104


105
G_DEFINE_TYPE_WITH_PRIVATE (GimpToolWidget, gimp_tool_widget, GIMP_TYPE_OBJECT)
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

#define parent_class gimp_tool_widget_parent_class

static guint widget_signals[LAST_SIGNAL] = { 0 };


static void
gimp_tool_widget_class_init (GimpToolWidgetClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize                    = gimp_tool_widget_finalize;
  object_class->constructed                 = gimp_tool_widget_constructed;
  object_class->set_property                = gimp_tool_widget_set_property;
  object_class->get_property                = gimp_tool_widget_get_property;
  object_class->dispatch_properties_changed = gimp_tool_widget_properties_changed;

123
  klass->leave_notify                       = gimp_tool_widget_real_leave_notify;
124 125
  klass->key_press                          = gimp_tool_widget_real_key_press;

126 127 128 129 130 131 132 133 134
  widget_signals[CHANGED] =
    g_signal_new ("changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpToolWidgetClass, changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

135 136 137 138 139 140 141 142 143 144
  widget_signals[RESPONSE] =
    g_signal_new ("response",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpToolWidgetClass, response),
                  NULL, NULL,
                  gimp_marshal_VOID__INT,
                  G_TYPE_NONE, 1,
                  G_TYPE_INT);

145 146 147 148 149 150 151 152 153 154 155 156 157
  widget_signals[SNAP_OFFSETS] =
    g_signal_new ("snap-offsets",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpToolWidgetClass, snap_offsets),
                  NULL, NULL,
                  gimp_marshal_VOID__INT_INT_INT_INT,
                  G_TYPE_NONE, 4,
                  G_TYPE_INT,
                  G_TYPE_INT,
                  G_TYPE_INT,
                  G_TYPE_INT);

158 159 160 161 162 163 164 165 166 167
  widget_signals[STATUS] =
    g_signal_new ("status",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpToolWidgetClass, status),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__STRING,
                  G_TYPE_NONE, 1,
                  G_TYPE_STRING);

168 169 170 171 172 173 174 175 176 177 178 179 180 181
  widget_signals[STATUS_COORDS] =
    g_signal_new ("status-coords",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpToolWidgetClass, status_coords),
                  NULL, NULL,
                  gimp_marshal_VOID__STRING_DOUBLE_STRING_DOUBLE_STRING,
                  G_TYPE_NONE, 5,
                  G_TYPE_STRING,
                  G_TYPE_DOUBLE,
                  G_TYPE_STRING,
                  G_TYPE_DOUBLE,
                  G_TYPE_STRING);

182 183 184 185 186 187 188 189 190 191
  widget_signals[MESSAGE] =
    g_signal_new ("message",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpToolWidgetClass, message),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__STRING,
                  G_TYPE_NONE, 1,
                  G_TYPE_STRING);

192 193 194 195 196 197 198 199 200
  widget_signals[FOCUS_CHANGED] =
    g_signal_new ("focus-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpToolWidgetClass, focus_changed),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  g_object_class_install_property (object_class, PROP_SHELL,
                                   g_param_spec_object ("shell",
                                                        NULL, NULL,
                                                        GIMP_TYPE_DISPLAY_SHELL,
                                                        GIMP_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));

  g_object_class_install_property (object_class, PROP_ITEM,
                                   g_param_spec_object ("item",
                                                        NULL, NULL,
                                                        GIMP_TYPE_CANVAS_ITEM,
                                                        GIMP_PARAM_READABLE));
}

static void
gimp_tool_widget_init (GimpToolWidget *widget)
{
218
  widget->private = gimp_tool_widget_get_instance_private (widget);
219 220

  widget->private->visible = TRUE;
221 222 223 224 225 226 227 228 229 230
}

static void
gimp_tool_widget_constructed (GObject *object)
{
  GimpToolWidget        *widget  = GIMP_TOOL_WIDGET (object);
  GimpToolWidgetPrivate *private = widget->private;

  G_OBJECT_CLASS (parent_class)->constructed (object);

231
  gimp_assert (GIMP_IS_DISPLAY_SHELL (private->shell));
232 233

  private->item = gimp_canvas_group_new (private->shell);
234 235

  gimp_canvas_item_set_visible (private->item, private->visible);
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
}

static void
gimp_tool_widget_finalize (GObject *object)
{
  GimpToolWidget        *widget  = GIMP_TOOL_WIDGET (object);
  GimpToolWidgetPrivate *private = widget->private;

  g_clear_object (&private->item);

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

static void
gimp_tool_widget_set_property (GObject      *object,
                               guint         property_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  GimpToolWidget        *widget  = GIMP_TOOL_WIDGET (object);
  GimpToolWidgetPrivate *private = widget->private;

  switch (property_id)
    {
    case PROP_SHELL:
      private->shell = g_value_get_object (value); /* don't ref */
      break;

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

static void
gimp_tool_widget_get_property (GObject    *object,
                               guint       property_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
  GimpToolWidget        *widget  = GIMP_TOOL_WIDGET (object);
  GimpToolWidgetPrivate *private = widget->private;

  switch (property_id)
    {
    case PROP_SHELL:
      g_value_set_object (value, private->shell);
      break;

285 286 287 288
    case PROP_ITEM:
      g_value_set_object (value, private->item);
      break;

289 290 291 292 293 294 295 296 297 298 299
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_tool_widget_properties_changed (GObject     *object,
                                     guint        n_pspecs,
                                     GParamSpec **pspecs)
{
Ell's avatar
Ell committed
300 301
  GimpToolWidget *widget = GIMP_TOOL_WIDGET (object);

302 303 304 305
  G_OBJECT_CLASS (parent_class)->dispatch_properties_changed (object,
                                                              n_pspecs,
                                                              pspecs);

Ell's avatar
Ell committed
306
  gimp_tool_widget_changed (widget);
307 308
}

309 310 311 312 313 314
static void
gimp_tool_widget_real_leave_notify (GimpToolWidget *widget)
{
  gimp_tool_widget_set_status (widget, NULL);
}

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
static gboolean
gimp_tool_widget_real_key_press (GimpToolWidget *widget,
                                 GdkEventKey    *kevent)
{
  switch (kevent->keyval)
    {
    case GDK_KEY_Return:
    case GDK_KEY_KP_Enter:
    case GDK_KEY_ISO_Enter:
      gimp_tool_widget_response (widget, GIMP_TOOL_WIDGET_RESPONSE_CONFIRM);
      return TRUE;

    case GDK_KEY_Escape:
      gimp_tool_widget_response (widget, GIMP_TOOL_WIDGET_RESPONSE_CANCEL);
      return TRUE;

    case GDK_KEY_BackSpace:
      gimp_tool_widget_response (widget, GIMP_TOOL_WIDGET_RESPONSE_RESET);
      return TRUE;

    default:
      break;
    }

  return FALSE;
}

342 343 344

/*  public functions  */

345 346 347 348 349 350 351 352
GimpDisplayShell *
gimp_tool_widget_get_shell (GimpToolWidget *widget)
{
  g_return_val_if_fail (GIMP_IS_TOOL_WIDGET (widget), NULL);

  return widget->private->shell;
}

353 354 355 356 357 358 359 360
GimpCanvasItem *
gimp_tool_widget_get_item (GimpToolWidget *widget)
{
  g_return_val_if_fail (GIMP_IS_TOOL_WIDGET (widget), NULL);

  return widget->private->item;
}

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
void
gimp_tool_widget_set_visible (GimpToolWidget *widget,
                              gboolean        visible)
{
  g_return_if_fail (GIMP_IS_TOOL_WIDGET (widget));

  if (visible != widget->private->visible)
    {
      widget->private->visible = visible;

      if (widget->private->item)
        gimp_canvas_item_set_visible (widget->private->item, visible);

      if (! visible)
        gimp_tool_widget_set_status (widget, NULL);
    }
}

gboolean
gimp_tool_widget_get_visible (GimpToolWidget *widget)
{
  g_return_val_if_fail (GIMP_IS_TOOL_WIDGET (widget), FALSE);

  return widget->private->visible;
}

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
void
gimp_tool_widget_set_focus (GimpToolWidget *widget,
                            gboolean        focus)
{
  g_return_if_fail (GIMP_IS_TOOL_WIDGET (widget));

  if (focus != widget->private->focus)
    {
      widget->private->focus = focus;

      g_signal_emit (widget, widget_signals[FOCUS_CHANGED], 0);
    }
}

gboolean
gimp_tool_widget_get_focus (GimpToolWidget *widget)
{
  g_return_val_if_fail (GIMP_IS_TOOL_WIDGET (widget), FALSE);

  return widget->private->focus;
}

Ell's avatar
Ell committed
409 410 411 412 413 414 415 416
void
gimp_tool_widget_changed (GimpToolWidget *widget)
{
  g_return_if_fail (GIMP_IS_TOOL_WIDGET (widget));

  g_signal_emit (widget, widget_signals[CHANGED], 0);
}

417 418 419 420 421 422 423 424 425 426
void
gimp_tool_widget_response (GimpToolWidget *widget,
                           gint            response_id)
{
  g_return_if_fail (GIMP_IS_TOOL_WIDGET (widget));

  g_signal_emit (widget, widget_signals[RESPONSE], 0,
                 response_id);
}

427
void
428 429 430 431 432
gimp_tool_widget_set_snap_offsets (GimpToolWidget *widget,
                                   gint            offset_x,
                                   gint            offset_y,
                                   gint            width,
                                   gint            height)
433
{
434 435
  GimpToolWidgetPrivate *private;

436 437
  g_return_if_fail (GIMP_IS_TOOL_WIDGET (widget));

438 439 440 441 442 443 444 445 446 447 448 449 450
  private = widget->private;

  if (offset_x != private->snap_offset_x ||
      offset_y != private->snap_offset_y ||
      width    != private->snap_width    ||
      height   != private->snap_height)
    {
      private->snap_offset_x = offset_x;
      private->snap_offset_y = offset_y;
      private->snap_width    = width;
      private->snap_height   = height;

      g_signal_emit (widget, widget_signals[SNAP_OFFSETS], 0,
451
                 offset_x, offset_y, width, height);
452
    }
453 454
}

455
void
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
gimp_tool_widget_get_snap_offsets (GimpToolWidget *widget,
                                   gint           *offset_x,
                                   gint           *offset_y,
                                   gint           *width,
                                   gint           *height)
{
  GimpToolWidgetPrivate *private;

  g_return_if_fail (GIMP_IS_TOOL_WIDGET (widget));

  private = widget->private;

  if (offset_x) *offset_x = private->snap_offset_x;
  if (offset_y) *offset_y = private->snap_offset_y;
  if (width)    *width    = private->snap_width;
  if (height)   *height   = private->snap_height;
}

void
gimp_tool_widget_set_status (GimpToolWidget *widget,
                             const gchar    *status)
477 478 479 480 481 482 483
{
  g_return_if_fail (GIMP_IS_TOOL_WIDGET (widget));

  g_signal_emit (widget, widget_signals[STATUS], 0,
                 status);
}

484 485 486 487 488 489 490 491 492 493 494 495 496 497
void
gimp_tool_widget_set_status_coords (GimpToolWidget *widget,
                                    const gchar    *title,
                                    gdouble         x,
                                    const gchar    *separator,
                                    gdouble         y,
                                    const gchar    *help)
{
  g_return_if_fail (GIMP_IS_TOOL_WIDGET (widget));

  g_signal_emit (widget, widget_signals[STATUS_COORDS], 0,
                 title, x, separator, y, help);
}

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
void
gimp_tool_widget_message (GimpToolWidget *widget,
                          const gchar    *format,
                          ...)
{
  va_list  args;
  gchar   *message;

  g_return_if_fail (GIMP_IS_TOOL_WIDGET (widget));
  g_return_if_fail (format != NULL);

  va_start (args, format);

  message = g_strdup_vprintf (format, args);

  va_end (args);

  gimp_tool_widget_message_literal (widget, message);

  g_free (message);
}

void
gimp_tool_widget_message_literal (GimpToolWidget *widget,
                                  const gchar    *message)
{
  g_return_if_fail (GIMP_IS_TOOL_WIDGET (widget));
  g_return_if_fail (message != NULL);

  g_signal_emit (widget, widget_signals[MESSAGE], 0,
                 message);
}

531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
void
gimp_tool_widget_add_item (GimpToolWidget *widget,
                           GimpCanvasItem *item)
{
  GimpCanvasGroup *group;

  g_return_if_fail (GIMP_IS_TOOL_WIDGET (widget));
  g_return_if_fail (GIMP_IS_CANVAS_ITEM (item));

  group = GIMP_CANVAS_GROUP (widget->private->item);

  if (widget->private->group_stack)
    group = widget->private->group_stack->data;

  gimp_canvas_group_add_item (group, item);
}

void
gimp_tool_widget_remove_item (GimpToolWidget *widget,
                              GimpCanvasItem *item)
{
  g_return_if_fail (GIMP_IS_TOOL_WIDGET (widget));
  g_return_if_fail (GIMP_IS_CANVAS_ITEM (item));

  gimp_canvas_group_remove_item (GIMP_CANVAS_GROUP (widget->private->item),
                                 item);
}

GimpCanvasGroup *
gimp_tool_widget_add_stroke_group (GimpToolWidget *widget)
{
  GimpCanvasItem *item;

  g_return_val_if_fail (GIMP_IS_TOOL_WIDGET (widget), NULL);

  item = gimp_canvas_group_new (widget->private->shell);
  gimp_canvas_group_set_group_stroking (GIMP_CANVAS_GROUP (item), TRUE);

  gimp_tool_widget_add_item (widget, item);
  g_object_unref (item);

  return GIMP_CANVAS_GROUP (item);
}

GimpCanvasGroup *
gimp_tool_widget_add_fill_group (GimpToolWidget *widget)
{
  GimpCanvasItem *item;

  g_return_val_if_fail (GIMP_IS_TOOL_WIDGET (widget), NULL);

  item = gimp_canvas_group_new (widget->private->shell);
  gimp_canvas_group_set_group_filling (GIMP_CANVAS_GROUP (item), TRUE);

  gimp_tool_widget_add_item (widget, item);
  g_object_unref (item);

  return GIMP_CANVAS_GROUP (item);
}

void
gimp_tool_widget_push_group (GimpToolWidget  *widget,
                             GimpCanvasGroup *group)
{
  g_return_if_fail (GIMP_IS_TOOL_WIDGET (widget));
  g_return_if_fail (GIMP_IS_CANVAS_GROUP (group));

  widget->private->group_stack = g_list_prepend (widget->private->group_stack,
                                                 group);
}

void
gimp_tool_widget_pop_group (GimpToolWidget *widget)
{
  g_return_if_fail (GIMP_IS_TOOL_WIDGET (widget));
  g_return_if_fail (widget->private->group_stack != NULL);

  widget->private->group_stack = g_list_remove (widget->private->group_stack,
                                                widget->private->group_stack->data);
}

/**
 * gimp_tool_widget_add_line:
 * @widget: the #GimpToolWidget
 * @x1:     start point X in image coordinates
 * @y1:     start point Y in image coordinates
 * @x2:     end point X in image coordinates
 * @y2:     end point Y in image coordinates
 *
 * This function adds a #GimpCanvasLine to @widget.
 **/
GimpCanvasItem *
gimp_tool_widget_add_line (GimpToolWidget *widget,
                           gdouble         x1,
                           gdouble         y1,
                           gdouble         x2,
                           gdouble         y2)
{
  GimpCanvasItem *item;

  g_return_val_if_fail (GIMP_IS_TOOL_WIDGET (widget), NULL);

  item = gimp_canvas_line_new (widget->private->shell,
                               x1, y1, x2, y2);

  gimp_tool_widget_add_item (widget, item);
  g_object_unref (item);

  return item;
}

642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
GimpCanvasItem *
gimp_tool_widget_add_rectangle (GimpToolWidget *widget,
                                gdouble         x,
                                gdouble         y,
                                gdouble         width,
                                gdouble         height,
                                gboolean        filled)
{
  GimpCanvasItem *item;

  g_return_val_if_fail (GIMP_IS_TOOL_WIDGET (widget), NULL);

  item = gimp_canvas_rectangle_new (widget->private->shell,
                                    x, y, width, height, filled);

  gimp_tool_widget_add_item (widget, item);
  g_object_unref (item);

  return item;
}

663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
GimpCanvasItem *
gimp_tool_widget_add_arc (GimpToolWidget *widget,
                          gdouble         center_x,
                          gdouble         center_y,
                          gdouble         radius_x,
                          gdouble         radius_y,
                          gdouble         start_angle,
                          gdouble         slice_angle,
                          gboolean        filled)
{
  GimpCanvasItem *item;

  g_return_val_if_fail (GIMP_IS_TOOL_WIDGET (widget), NULL);

  item = gimp_canvas_arc_new (widget->private->shell,
                              center_x, center_y,
                              radius_x, radius_y,
                              start_angle, slice_angle,
                              filled);

  gimp_tool_widget_add_item (widget, item);
  g_object_unref (item);

  return item;
}

689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
GimpCanvasItem *
gimp_tool_widget_add_polygon (GimpToolWidget    *widget,
                              GimpMatrix3       *transform,
                              const GimpVector2 *points,
                              gint               n_points,
                              gboolean           filled)
{
  GimpCanvasItem *item;

  g_return_val_if_fail (GIMP_IS_TOOL_WIDGET (widget), NULL);
  g_return_val_if_fail (points == NULL || n_points > 0, NULL);

  item = gimp_canvas_polygon_new (widget->private->shell,
                                  points, n_points,
                                  transform, filled);

  gimp_tool_widget_add_item (widget, item);
  g_object_unref (item);

  return item;
}

GimpCanvasItem *
gimp_tool_widget_add_polygon_from_coords (GimpToolWidget    *widget,
                                          GimpMatrix3       *transform,
                                          const GimpCoords  *points,
                                          gint               n_points,
                                          gboolean           filled)
{
  GimpCanvasItem *item;

  g_return_val_if_fail (GIMP_IS_TOOL_WIDGET (widget), NULL);
  g_return_val_if_fail (points == NULL || n_points > 0, NULL);

  item = gimp_canvas_polygon_new_from_coords (widget->private->shell,
                                              points, n_points,
                                              transform, filled);

  gimp_tool_widget_add_item (widget, item);
  g_object_unref (item);

  return item;
}

733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
GimpCanvasItem *
gimp_tool_widget_add_path (GimpToolWidget       *widget,
                           const GimpBezierDesc *desc)
{
  GimpCanvasItem *item;

  g_return_val_if_fail (GIMP_IS_TOOL_WIDGET (widget), NULL);

  item = gimp_canvas_path_new (widget->private->shell,
                               desc, 0, 0, FALSE, GIMP_PATH_STYLE_DEFAULT);

  gimp_tool_widget_add_item (widget, item);
  g_object_unref (item);

  return item;
}

750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
GimpCanvasItem *
gimp_tool_widget_add_handle (GimpToolWidget   *widget,
                             GimpHandleType    type,
                             gdouble           x,
                             gdouble           y,
                             gint              width,
                             gint              height,
                             GimpHandleAnchor  anchor)
{
  GimpCanvasItem *item;

  g_return_val_if_fail (GIMP_IS_TOOL_WIDGET (widget), NULL);

  item = gimp_canvas_handle_new (widget->private->shell,
                                 type, anchor, x, y, width, height);

  gimp_tool_widget_add_item (widget, item);
  g_object_unref (item);

  return item;
}

772
GimpCanvasItem *
773
gimp_tool_widget_add_corner (GimpToolWidget   *widget,
774 775 776 777
                             gdouble           x,
                             gdouble           y,
                             gdouble           width,
                             gdouble           height,
778
                             GimpHandleAnchor  anchor,
779 780
                             gint              corner_width,
                             gint              corner_height,
781
                             gboolean          outside)
782 783 784 785 786
{
  GimpCanvasItem *item;

  g_return_val_if_fail (GIMP_IS_TOOL_WIDGET (widget), NULL);

787
  item = gimp_canvas_corner_new (widget->private->shell,
788 789
                                 x, y, width, height,
                                 anchor, corner_width, corner_height,
790
                                 outside);
791 792 793 794 795 796 797

  gimp_tool_widget_add_item (widget, item);
  g_object_unref (item);

  return item;
}

798
GimpCanvasItem *
799 800 801 802 803 804
gimp_tool_widget_add_rectangle_guides (GimpToolWidget *widget,
                                       gdouble         x,
                                       gdouble         y,
                                       gdouble         width,
                                       gdouble         height,
                                       GimpGuidesType  type)
805 806 807 808 809
{
  GimpCanvasItem *item;

  g_return_val_if_fail (GIMP_IS_TOOL_WIDGET (widget), NULL);

810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
  item = gimp_canvas_rectangle_guides_new (widget->private->shell,
                                           x, y, width, height, type, 4);

  gimp_tool_widget_add_item (widget, item);
  g_object_unref (item);

  return item;
}

GimpCanvasItem *
gimp_tool_widget_add_transform_guides (GimpToolWidget    *widget,
                                       const GimpMatrix3 *transform,
                                       gdouble            x1,
                                       gdouble            y1,
                                       gdouble            x2,
                                       gdouble            y2,
                                       GimpGuidesType     type,
827 828
                                       gint               n_guides,
                                       gboolean           clip)
829 830 831 832 833 834 835
{
  GimpCanvasItem *item;

  g_return_val_if_fail (GIMP_IS_TOOL_WIDGET (widget), NULL);

  item = gimp_canvas_transform_guides_new (widget->private->shell,
                                           transform, x1, y1, x2, y2,
836
                                           type, n_guides, clip);
837 838 839 840 841 842 843

  gimp_tool_widget_add_item (widget, item);
  g_object_unref (item);

  return item;
}

844
gint
845 846 847 848 849 850
gimp_tool_widget_button_press (GimpToolWidget      *widget,
                               const GimpCoords    *coords,
                               guint32              time,
                               GdkModifierType      state,
                               GimpButtonPressType  press_type)
{
851 852
  g_return_val_if_fail (GIMP_IS_TOOL_WIDGET (widget), 0);
  g_return_val_if_fail (coords != NULL, 0);
853

854 855 856 857 858 859 860 861
  if (widget->private->visible &&
      GIMP_TOOL_WIDGET_GET_CLASS (widget)->button_press)
    {
      return GIMP_TOOL_WIDGET_GET_CLASS (widget)->button_press (widget,
                                                                coords, time,
                                                                state,
                                                                press_type);
    }
862

863
  return 0;
864 865 866 867 868 869 870 871 872 873 874 875
}

void
gimp_tool_widget_button_release (GimpToolWidget        *widget,
                                 const GimpCoords      *coords,
                                 guint32                time,
                                 GdkModifierType        state,
                                 GimpButtonReleaseType  release_type)
{
  g_return_if_fail (GIMP_IS_TOOL_WIDGET (widget));
  g_return_if_fail (coords != NULL);

876 877 878 879 880 881 882
  if (widget->private->visible &&
      GIMP_TOOL_WIDGET_GET_CLASS (widget)->button_release)
    {
      GIMP_TOOL_WIDGET_GET_CLASS (widget)->button_release (widget,
                                                           coords, time, state,
                                                           release_type);
    }
883 884 885 886 887 888 889 890 891 892 893
}

void
gimp_tool_widget_motion (GimpToolWidget   *widget,
                         const GimpCoords *coords,
                         guint32           time,
                         GdkModifierType   state)
{
  g_return_if_fail (GIMP_IS_TOOL_WIDGET (widget));
  g_return_if_fail (coords != NULL);

894 895 896 897 898 899
  if (widget->private->visible &&
      GIMP_TOOL_WIDGET_GET_CLASS (widget)->motion)
    {
      GIMP_TOOL_WIDGET_GET_CLASS (widget)->motion (widget,
                                                   coords, time, state);
    }
900 901
}

902 903 904 905 906 907 908 909 910
GimpHit
gimp_tool_widget_hit (GimpToolWidget   *widget,
                      const GimpCoords *coords,
                      GdkModifierType   state,
                      gboolean          proximity)
{
  g_return_val_if_fail (GIMP_IS_TOOL_WIDGET (widget), GIMP_HIT_NONE);
  g_return_val_if_fail (coords != NULL, GIMP_HIT_NONE);

911 912 913 914 915 916 917
  if (widget->private->visible &&
      GIMP_TOOL_WIDGET_GET_CLASS (widget)->hit)
    {
      return GIMP_TOOL_WIDGET_GET_CLASS (widget)->hit (widget,
                                                       coords, state,
                                                       proximity);
    }
918 919 920 921

  return GIMP_HIT_NONE;
}

922 923 924 925 926 927 928 929 930
void
gimp_tool_widget_hover (GimpToolWidget   *widget,
                        const GimpCoords *coords,
                        GdkModifierType   state,
                        gboolean          proximity)
{
  g_return_if_fail (GIMP_IS_TOOL_WIDGET (widget));
  g_return_if_fail (coords != NULL);

931 932 933 934 935 936
  if (widget->private->visible &&
      GIMP_TOOL_WIDGET_GET_CLASS (widget)->hover)
    {
      GIMP_TOOL_WIDGET_GET_CLASS (widget)->hover (widget,
                                                  coords, state, proximity);
    }
937 938
}

939 940 941 942 943
void
gimp_tool_widget_leave_notify (GimpToolWidget *widget)
{
  g_return_if_fail (GIMP_IS_TOOL_WIDGET (widget));

944 945 946 947 948
  if (widget->private->visible &&
      GIMP_TOOL_WIDGET_GET_CLASS (widget)->leave_notify)
    {
      GIMP_TOOL_WIDGET_GET_CLASS (widget)->leave_notify (widget);
    }
949 950
}

951 952 953 954 955 956 957
gboolean
gimp_tool_widget_key_press (GimpToolWidget *widget,
                            GdkEventKey    *kevent)
{
  g_return_val_if_fail (GIMP_IS_TOOL_WIDGET (widget), FALSE);
  g_return_val_if_fail (kevent != NULL, FALSE);

958 959 960 961 962
  if (widget->private->visible &&
      GIMP_TOOL_WIDGET_GET_CLASS (widget)->key_press)
    {
      return GIMP_TOOL_WIDGET_GET_CLASS (widget)->key_press (widget, kevent);
    }
963 964 965 966 967 968 969 970 971 972 973

  return FALSE;
}

gboolean
gimp_tool_widget_key_release (GimpToolWidget *widget,
                              GdkEventKey    *kevent)
{
  g_return_val_if_fail (GIMP_IS_TOOL_WIDGET (widget), FALSE);
  g_return_val_if_fail (kevent != NULL, FALSE);

974 975 976 977 978
  if (widget->private->visible &&
      GIMP_TOOL_WIDGET_GET_CLASS (widget)->key_release)
    {
      return GIMP_TOOL_WIDGET_GET_CLASS (widget)->key_release (widget, kevent);
    }
979 980 981 982

  return FALSE;
}

983 984 985 986 987 988 989 990
void
gimp_tool_widget_motion_modifier (GimpToolWidget  *widget,
                                  GdkModifierType  key,
                                  gboolean         press,
                                  GdkModifierType  state)
{
  g_return_if_fail (GIMP_IS_TOOL_WIDGET (widget));

991 992 993 994 995 996
  if (widget->private->visible &&
      GIMP_TOOL_WIDGET_GET_CLASS (widget)->motion_modifier)
    {
      GIMP_TOOL_WIDGET_GET_CLASS (widget)->motion_modifier (widget,
                                                            key, press, state);
    }
997 998 999 1000 1001 1002 1003 1004 1005 1006
}

void
gimp_tool_widget_hover_modifier (GimpToolWidget  *widget,
                                 GdkModifierType  key,
                                 gboolean         press,
                                 GdkModifierType  state)
{
  g_return_if_fail (GIMP_IS_TOOL_WIDGET (widget));

1007 1008 1009 1010 1011 1012
  if (widget->private->visible &&
      GIMP_TOOL_WIDGET_GET_CLASS (widget)->hover_modifier)
    {
      GIMP_TOOL_WIDGET_GET_CLASS (widget)->hover_modifier (widget,
                                                           key, press, state);
    }
1013 1014 1015 1016 1017 1018 1019 1020
}

gboolean
gimp_tool_widget_get_cursor (GimpToolWidget      *widget,
                             const GimpCoords    *coords,
                             GdkModifierType      state,
                             GimpCursorType      *cursor,
                             GimpToolCursorType  *tool_cursor,
1021
                             GimpCursorModifier  *modifier)
1022 1023 1024 1025 1026

{
  g_return_val_if_fail (GIMP_IS_TOOL_WIDGET (widget), FALSE);
  g_return_val_if_fail (coords != NULL, FALSE);

1027 1028
  if (widget->private->visible &&
      GIMP_TOOL_WIDGET_GET_CLASS (widget)->get_cursor)
1029 1030 1031
    {
      GimpCursorType     my_cursor;
      GimpToolCursorType my_tool_cursor;
1032
      GimpCursorModifier my_modifier;
1033

1034 1035 1036
      if (cursor)      my_cursor      = *cursor;
      if (tool_cursor) my_tool_cursor = *tool_cursor;
      if (modifier)    my_modifier    = *modifier;
1037 1038 1039 1040 1041

      if (GIMP_TOOL_WIDGET_GET_CLASS (widget)->get_cursor (widget, coords,
                                                           state,
                                                           &my_cursor,
                                                           &my_tool_cursor,
1042
                                                           &my_modifier))
1043
        {
1044 1045 1046
          if (cursor)      *cursor      = my_cursor;
          if (tool_cursor) *tool_cursor = my_tool_cursor;
          if (modifier)    *modifier    = my_modifier;
1047 1048 1049 1050 1051 1052 1053

          return TRUE;
        }
    }

  return FALSE;
}