gimptool.c 44.2 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
Nate Summers's avatar
Nate Summers committed
2
 * Copyright (C) 1995-2002 Spencer Kimball, Peter Mattis and others
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7 8 9 10 11 12 13 14
 * (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
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17 18 19
 */

#include "config.h"

20
#include <gegl.h>
21 22
#include <gtk/gtk.h>

23 24
#include "libgimpmath/gimpmath.h"

Michael Natterer's avatar
Michael Natterer committed
25
#include "tools-types.h"
26

Sven Neumann's avatar
Sven Neumann committed
27
#include "core/gimp.h"
28
#include "core/gimpcontainer.h"
29
#include "core/gimpimage.h"
30
#include "core/gimpprogress.h"
31
#include "core/gimptoolinfo.h"
32

33
#include "display/gimpdisplay.h"
Michael Natterer's avatar
Michael Natterer committed
34
#include "display/gimpdisplayshell.h"
35
#include "display/gimpdisplayshell-cursor.h"
36
#include "display/gimpstatusbar.h"
37

38
#include "gimptool.h"
39
#include "gimptool-progress.h"
40
#include "gimptoolcontrol.h"
41

42
#include "gimp-log.h"
43
#include "gimp-intl.h"
44

45

46 47 48 49 50 51 52
enum
{
  PROP_0,
  PROP_TOOL_INFO
};


53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
static void            gimp_tool_constructed         (GObject          *object);
static void            gimp_tool_dispose             (GObject          *object);
static void            gimp_tool_finalize            (GObject          *object);
static void            gimp_tool_set_property        (GObject          *object,
                                                      guint             property_id,
                                                      const GValue     *value,
                                                      GParamSpec       *pspec);
static void            gimp_tool_get_property        (GObject          *object,
                                                      guint             property_id,
                                                      GValue           *value,
                                                      GParamSpec       *pspec);

static gboolean        gimp_tool_real_has_display    (GimpTool         *tool,
                                                      GimpDisplay      *display);
static GimpDisplay   * gimp_tool_real_has_image      (GimpTool         *tool,
                                                      GimpImage        *image);
static gboolean        gimp_tool_real_initialize     (GimpTool         *tool,
                                                      GimpDisplay      *display,
                                                      GError          **error);
static void            gimp_tool_real_control        (GimpTool         *tool,
                                                      GimpToolAction    action,
                                                      GimpDisplay      *display);
static void            gimp_tool_real_button_press   (GimpTool         *tool,
                                                      const GimpCoords *coords,
                                                      guint32           time,
                                                      GdkModifierType   state,
                                                      GimpButtonPressType press_type,
                                                      GimpDisplay      *display);
static void            gimp_tool_real_button_release (GimpTool         *tool,
                                                      const GimpCoords *coords,
                                                      guint32           time,
                                                      GdkModifierType   state,
                                                      GimpButtonReleaseType release_type,
                                                      GimpDisplay      *display);
static void            gimp_tool_real_motion         (GimpTool         *tool,
                                                      const GimpCoords *coords,
                                                      guint32           time,
                                                      GdkModifierType   state,
                                                      GimpDisplay      *display);
static gboolean        gimp_tool_real_key_press      (GimpTool         *tool,
                                                      GdkEventKey      *kevent,
                                                      GimpDisplay      *display);
static gboolean        gimp_tool_real_key_release    (GimpTool         *tool,
                                                      GdkEventKey      *kevent,
                                                      GimpDisplay      *display);
static void            gimp_tool_real_modifier_key   (GimpTool         *tool,
                                                      GdkModifierType   key,
                                                      gboolean          press,
                                                      GdkModifierType   state,
                                                      GimpDisplay      *display);
static void       gimp_tool_real_active_modifier_key (GimpTool         *tool,
                                                      GdkModifierType   key,
                                                      gboolean          press,
                                                      GdkModifierType   state,
                                                      GimpDisplay      *display);
static void            gimp_tool_real_oper_update    (GimpTool         *tool,
                                                      const GimpCoords *coords,
                                                      GdkModifierType   state,
                                                      gboolean          proximity,
                                                      GimpDisplay      *display);
static void            gimp_tool_real_cursor_update  (GimpTool         *tool,
                                                      const GimpCoords *coords,
                                                      GdkModifierType   state,
                                                      GimpDisplay      *display);
117
static const gchar   * gimp_tool_real_can_undo       (GimpTool         *tool,
118
                                                      GimpDisplay      *display);
119
static const gchar   * gimp_tool_real_can_redo       (GimpTool         *tool,
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
                                                      GimpDisplay      *display);
static gboolean        gimp_tool_real_undo           (GimpTool         *tool,
                                                      GimpDisplay      *display);
static gboolean        gimp_tool_real_redo           (GimpTool         *tool,
                                                      GimpDisplay      *display);
static GimpUIManager * gimp_tool_real_get_popup      (GimpTool         *tool,
                                                      const GimpCoords *coords,
                                                      GdkModifierType   state,
                                                      GimpDisplay      *display,
                                                      const gchar     **ui_path);
static void            gimp_tool_real_options_notify (GimpTool         *tool,
                                                      GimpToolOptions  *options,
                                                      const GParamSpec *pspec);

static void            gimp_tool_options_notify      (GimpToolOptions  *options,
                                                      const GParamSpec *pspec,
                                                      GimpTool         *tool);
static void            gimp_tool_clear_status        (GimpTool         *tool);
138

139

140 141 142
G_DEFINE_TYPE_WITH_CODE (GimpTool, gimp_tool, GIMP_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_PROGRESS,
                                                gimp_tool_progress_iface_init))
143 144

#define parent_class gimp_tool_parent_class
145

146
static gint global_tool_ID = 1;
147 148 149 150 151


static void
gimp_tool_class_init (GimpToolClass *klass)
{
152
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
153

154
  object_class->constructed  = gimp_tool_constructed;
155
  object_class->dispose      = gimp_tool_dispose;
156 157 158
  object_class->finalize     = gimp_tool_finalize;
  object_class->set_property = gimp_tool_set_property;
  object_class->get_property = gimp_tool_get_property;
159

160 161 162 163 164 165 166 167
  klass->has_display         = gimp_tool_real_has_display;
  klass->has_image           = gimp_tool_real_has_image;
  klass->initialize          = gimp_tool_real_initialize;
  klass->control             = gimp_tool_real_control;
  klass->button_press        = gimp_tool_real_button_press;
  klass->button_release      = gimp_tool_real_button_release;
  klass->motion              = gimp_tool_real_motion;
  klass->key_press           = gimp_tool_real_key_press;
168
  klass->key_release         = gimp_tool_real_key_release;
169
  klass->modifier_key        = gimp_tool_real_modifier_key;
170
  klass->active_modifier_key = gimp_tool_real_active_modifier_key;
171 172
  klass->oper_update         = gimp_tool_real_oper_update;
  klass->cursor_update       = gimp_tool_real_cursor_update;
173 174
  klass->can_undo            = gimp_tool_real_can_undo;
  klass->can_redo            = gimp_tool_real_can_redo;
175 176
  klass->undo                = gimp_tool_real_undo;
  klass->redo                = gimp_tool_real_redo;
177
  klass->get_popup           = gimp_tool_real_get_popup;
178
  klass->options_notify      = gimp_tool_real_options_notify;
179 180 181 182 183

  g_object_class_install_property (object_class, PROP_TOOL_INFO,
                                   g_param_spec_object ("tool-info",
                                                        NULL, NULL,
                                                        GIMP_TYPE_TOOL_INFO,
184
                                                        GIMP_PARAM_READWRITE |
185
                                                        G_PARAM_CONSTRUCT_ONLY));
186 187 188 189 190
}

static void
gimp_tool_init (GimpTool *tool)
{
191 192 193 194 195 196 197 198 199
  tool->tool_info             = NULL;
  tool->ID                    = global_tool_ID++;
  tool->control               = g_object_new (GIMP_TYPE_TOOL_CONTROL, NULL);
  tool->display               = NULL;
  tool->drawable              = NULL;
  tool->focus_display         = NULL;
  tool->modifier_state        = 0;
  tool->active_modifier_state = 0;
  tool->button_press_state    = 0;
200 201
}

202 203 204 205 206
static void
gimp_tool_constructed (GObject *object)
{
  GimpTool *tool = GIMP_TOOL (object);

207
  G_OBJECT_CLASS (parent_class)->constructed (object);
208 209 210 211 212 213 214 215

  g_assert (GIMP_IS_TOOL_INFO (tool->tool_info));

  g_signal_connect_object (gimp_tool_get_options (tool), "notify",
                           G_CALLBACK (gimp_tool_options_notify),
                           tool, 0);
}

216 217 218 219 220 221 222 223 224 225
static void
gimp_tool_dispose (GObject *object)
{
  GimpTool *tool = GIMP_TOOL (object);

  gimp_tool_control (tool, GIMP_TOOL_ACTION_HALT, tool->display);

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

226 227 228 229
static void
gimp_tool_finalize (GObject *object)
{
  GimpTool *tool = GIMP_TOOL (object);
230

231 232 233 234 235 236
  g_clear_object (&tool->tool_info);
  g_clear_object (&tool->control);
  g_clear_pointer (&tool->label,     g_free);
  g_clear_pointer (&tool->undo_desc, g_free);
  g_clear_pointer (&tool->icon_name, g_free);
  g_clear_pointer (&tool->help_id,   g_free);
237 238 239 240

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

241 242 243 244 245 246
static void
gimp_tool_set_property (GObject      *object,
                        guint         property_id,
                        const GValue *value,
                        GParamSpec   *pspec)
{
247
  GimpTool *tool = GIMP_TOOL (object);
248 249 250 251

  switch (property_id)
    {
    case PROP_TOOL_INFO:
252
      tool->tool_info = g_value_dup_object (value);
253 254 255 256 257 258 259 260 261 262 263 264 265 266
      break;

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

static void
gimp_tool_get_property (GObject    *object,
                        guint       property_id,
                        GValue     *value,
                        GParamSpec *pspec)
{
267
  GimpTool *tool = GIMP_TOOL (object);
268 269 270 271 272 273 274 275 276 277 278 279 280

  switch (property_id)
    {
    case PROP_TOOL_INFO:
      g_value_set_object (value, tool->tool_info);
      break;

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

281 282 283

/*  standard member functions  */

284 285 286 287
static gboolean
gimp_tool_real_has_display (GimpTool    *tool,
                            GimpDisplay *display)
{
288 289
  return (display == tool->display ||
          g_list_find (tool->status_displays, display));
290 291 292 293 294 295 296 297
}

static GimpDisplay *
gimp_tool_real_has_image (GimpTool  *tool,
                          GimpImage *image)
{
  if (tool->display)
    {
298
      if (image && gimp_display_get_image (tool->display) == image)
299 300 301 302 303 304 305 306 307 308
        return tool->display;

      /*  NULL image means any display  */
      if (! image)
        return tool->display;
    }

  return NULL;
}

309
static gboolean
310 311 312
gimp_tool_real_initialize (GimpTool     *tool,
                           GimpDisplay  *display,
                           GError      **error)
313
{
314
  return TRUE;
315 316 317 318 319
}

static void
gimp_tool_real_control (GimpTool       *tool,
                        GimpToolAction  action,
320
                        GimpDisplay    *display)
321
{
322 323 324 325 326 327 328 329 330
  switch (action)
    {
    case GIMP_TOOL_ACTION_PAUSE:
    case GIMP_TOOL_ACTION_RESUME:
      break;

    case GIMP_TOOL_ACTION_HALT:
      tool->display = NULL;
      break;
331 332 333

    case GIMP_TOOL_ACTION_COMMIT:
      break;
334
    }
335 336
}

337
static void
338 339 340 341 342 343
gimp_tool_real_button_press (GimpTool            *tool,
                             const GimpCoords    *coords,
                             guint32              time,
                             GdkModifierType      state,
                             GimpButtonPressType  press_type,
                             GimpDisplay         *display)
344
{
345 346
  if (press_type == GIMP_BUTTON_PRESS_NORMAL)
    {
347 348
      GimpImage *image = gimp_display_get_image (display);

349
      tool->display  = display;
350
      tool->drawable = gimp_image_get_active_drawable (image);
351

352 353
      gimp_tool_control_activate (tool->control);
    }
354 355 356
}

static void
357
gimp_tool_real_button_release (GimpTool              *tool,
358
                               const GimpCoords      *coords,
359 360 361 362
                               guint32                time,
                               GdkModifierType        state,
                               GimpButtonReleaseType  release_type,
                               GimpDisplay           *display)
363 364 365
{
  gimp_tool_control_halt (tool->control);
}
366 367

static void
368 369 370 371 372
gimp_tool_real_motion (GimpTool         *tool,
                       const GimpCoords *coords,
                       guint32           time,
                       GdkModifierType   state,
                       GimpDisplay      *display)
373 374 375
{
}

376
static gboolean
377
gimp_tool_real_key_press (GimpTool    *tool,
378
                          GdkEventKey *kevent,
379
                          GimpDisplay *display)
380
{
381
  return FALSE;
382 383
}

384 385 386 387 388 389 390 391
static gboolean
gimp_tool_real_key_release (GimpTool    *tool,
                            GdkEventKey *kevent,
                            GimpDisplay *display)
{
  return FALSE;
}

392 393 394 395 396
static void
gimp_tool_real_modifier_key (GimpTool        *tool,
                             GdkModifierType  key,
                             gboolean         press,
                             GdkModifierType  state,
397
                             GimpDisplay     *display)
398 399 400
{
}

401 402 403 404 405 406 407 408 409
static void
gimp_tool_real_active_modifier_key (GimpTool        *tool,
                                    GdkModifierType  key,
                                    gboolean         press,
                                    GdkModifierType  state,
                                    GimpDisplay     *display)
{
}

410
static void
411 412 413 414 415
gimp_tool_real_oper_update (GimpTool         *tool,
                            const GimpCoords *coords,
                            GdkModifierType   state,
                            gboolean          proximity,
                            GimpDisplay      *display)
416 417 418 419
{
}

static void
420 421 422 423
gimp_tool_real_cursor_update (GimpTool         *tool,
                              const GimpCoords *coords,
                              GdkModifierType   state,
                              GimpDisplay      *display)
424
{
425
  gimp_tool_set_cursor (tool, display,
426 427 428
                        gimp_tool_control_get_cursor (tool->control),
                        gimp_tool_control_get_tool_cursor (tool->control),
                        gimp_tool_control_get_cursor_modifier (tool->control));
429 430
}

431
static const gchar *
432 433
gimp_tool_real_can_undo (GimpTool    *tool,
                         GimpDisplay *display)
434 435 436 437 438
{
  return NULL;
}

static const gchar *
439 440
gimp_tool_real_can_redo (GimpTool    *tool,
                         GimpDisplay *display)
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
{
  return NULL;
}

static gboolean
gimp_tool_real_undo (GimpTool    *tool,
                     GimpDisplay *display)
{
  return FALSE;
}

static gboolean
gimp_tool_real_redo (GimpTool    *tool,
                     GimpDisplay *display)
{
  return FALSE;
}

459 460
static GimpUIManager *
gimp_tool_real_get_popup (GimpTool         *tool,
461
                          const GimpCoords *coords,
462 463 464 465 466 467 468 469 470
                          GdkModifierType   state,
                          GimpDisplay      *display,
                          const gchar     **ui_path)
{
  *ui_path = NULL;

  return NULL;
}

471 472 473 474 475 476 477
static void
gimp_tool_real_options_notify (GimpTool         *tool,
                               GimpToolOptions  *options,
                               const GParamSpec *pspec)
{
}

478 479 480

/*  public functions  */

481 482 483 484 485 486 487 488 489
GimpToolOptions *
gimp_tool_get_options (GimpTool *tool)
{
  g_return_val_if_fail (GIMP_IS_TOOL (tool), NULL);
  g_return_val_if_fail (GIMP_IS_TOOL_INFO (tool->tool_info), NULL);

  return tool->tool_info->tool_options;
}

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
void
gimp_tool_set_label (GimpTool    *tool,
                     const gchar *label)
{
  g_return_if_fail (GIMP_IS_TOOL (tool));

  g_free (tool->label);
  tool->label = g_strdup (label);
}

const gchar *
gimp_tool_get_label (GimpTool *tool)
{
  g_return_val_if_fail (GIMP_IS_TOOL (tool), NULL);

  if (tool->label)
    return tool->label;

  return tool->tool_info->label;
}

511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
void
gimp_tool_set_undo_desc (GimpTool    *tool,
                         const gchar *undo_desc)
{
  g_return_if_fail (GIMP_IS_TOOL (tool));

  g_free (tool->undo_desc);
  tool->undo_desc = g_strdup (undo_desc);
}

const gchar *
gimp_tool_get_undo_desc (GimpTool *tool)
{
  g_return_val_if_fail (GIMP_IS_TOOL (tool), NULL);

  if (tool->undo_desc)
    return tool->undo_desc;

529
  return tool->tool_info->label;
530 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
}

void
gimp_tool_set_icon_name (GimpTool    *tool,
                         const gchar *icon_name)
{
  g_return_if_fail (GIMP_IS_TOOL (tool));

  g_free (tool->icon_name);
  tool->icon_name = g_strdup (icon_name);
}

const gchar *
gimp_tool_get_icon_name (GimpTool *tool)
{
  g_return_val_if_fail (GIMP_IS_TOOL (tool), NULL);

  if (tool->icon_name)
    return tool->icon_name;

  return gimp_viewable_get_icon_name (GIMP_VIEWABLE (tool->tool_info));
}

void
gimp_tool_set_help_id (GimpTool    *tool,
                       const gchar *help_id)
{
  g_return_if_fail (GIMP_IS_TOOL (tool));

  g_free (tool->help_id);
  tool->help_id = g_strdup (help_id);
}

const gchar *
gimp_tool_get_help_id (GimpTool *tool)
{
  g_return_val_if_fail (GIMP_IS_TOOL (tool), NULL);

  if (tool->help_id)
    return tool->help_id;

  return tool->tool_info->help_id;
}

574 575 576 577 578 579 580 581 582 583 584 585 586 587
gboolean
gimp_tool_has_display (GimpTool    *tool,
                       GimpDisplay *display)
{
  g_return_val_if_fail (GIMP_IS_TOOL (tool), FALSE);
  g_return_val_if_fail (GIMP_IS_DISPLAY (display), FALSE);

  return GIMP_TOOL_GET_CLASS (tool)->has_display (tool, display);
}

GimpDisplay *
gimp_tool_has_image (GimpTool  *tool,
                     GimpImage *image)
{
588 589
  GimpDisplay *display;

590 591 592
  g_return_val_if_fail (GIMP_IS_TOOL (tool), NULL);
  g_return_val_if_fail (image == NULL || GIMP_IS_IMAGE (image), NULL);

593 594 595 596 597 598 599 600 601 602 603 604 605
  display = GIMP_TOOL_GET_CLASS (tool)->has_image (tool, image);

  /*  check status displays last because they don't affect the tool
   *  itself (unlike tool->display or draw_tool->display)
   */
  if (! display && tool->status_displays)
    {
      GList *list;

      for (list = tool->status_displays; list; list = g_list_next (list))
        {
          GimpDisplay *status_display = list->data;

606
          if (gimp_display_get_image (status_display) == image)
607 608 609 610 611 612 613 614 615
            return status_display;
        }

      /*  NULL image means any display  */
      if (! image)
        return tool->status_displays->data;
    }

  return display;
616 617
}

618
gboolean
619
gimp_tool_initialize (GimpTool    *tool,
620
                      GimpDisplay *display)
621
{
622 623
  GError *error = NULL;

624
  g_return_val_if_fail (GIMP_IS_TOOL (tool), FALSE);
625
  g_return_val_if_fail (GIMP_IS_DISPLAY (display), FALSE);
626

627 628
  if (! GIMP_TOOL_GET_CLASS (tool)->initialize (tool, display, &error))
    {
629 630 631 632 633 634
      if (error)
        {
          gimp_tool_message_literal (tool, display, error->message);
          g_clear_error (&error);
        }

635 636 637 638
      return FALSE;
    }

  return TRUE;
639 640 641
}

void
642 643
gimp_tool_control (GimpTool       *tool,
                   GimpToolAction  action,
644
                   GimpDisplay    *display)
645 646 647 648 649
{
  g_return_if_fail (GIMP_IS_TOOL (tool));

  switch (action)
    {
650
    case GIMP_TOOL_ACTION_PAUSE:
651
      if (! gimp_tool_control_is_paused (tool->control))
652
        GIMP_TOOL_GET_CLASS (tool)->control (tool, action, display);
653 654 655 656

      gimp_tool_control_pause (tool->control);
      break;

657
    case GIMP_TOOL_ACTION_RESUME:
658 659 660 661
      if (gimp_tool_control_is_paused (tool->control))
        {
          gimp_tool_control_resume (tool->control);

662
          if (! gimp_tool_control_is_paused (tool->control))
663
            GIMP_TOOL_GET_CLASS (tool)->control (tool, action, display);
664 665 666
        }
      else
        {
667 668
          g_warning ("gimp_tool_control: unable to RESUME tool with "
                     "tool->control->paused_count == 0");
669 670 671
        }
      break;

672
    case GIMP_TOOL_ACTION_HALT:
673
      GIMP_TOOL_GET_CLASS (tool)->control (tool, action, display);
674

675 676
      if (gimp_tool_control_is_active (tool->control))
        gimp_tool_control_halt (tool->control);
677 678

      gimp_tool_clear_status (tool);
679
      break;
680 681

    case GIMP_TOOL_ACTION_COMMIT:
682 683 684 685 686
      /*  always HALT after COMMIT here and not in each tool individually;
       *  some tools interact with their subclasses (e.g. filter tool
       *  and operation tool), and it's essential that COMMIT runs
       *  through the entire class hierarchy before HALT
       */
687
      GIMP_TOOL_GET_CLASS (tool)->control (tool, action, display);
688
      GIMP_TOOL_GET_CLASS (tool)->control (tool, GIMP_TOOL_ACTION_HALT, display);
689
      break;
690 691 692 693
    }
}

void
694 695 696 697 698 699
gimp_tool_button_press (GimpTool            *tool,
                        const GimpCoords    *coords,
                        guint32              time,
                        GdkModifierType      state,
                        GimpButtonPressType  press_type,
                        GimpDisplay         *display)
700 701 702
{
  g_return_if_fail (GIMP_IS_TOOL (tool));
  g_return_if_fail (coords != NULL);
703
  g_return_if_fail (GIMP_IS_DISPLAY (display));
704

705
  GIMP_TOOL_GET_CLASS (tool)->button_press (tool, coords, time, state,
706
                                            press_type, display);
707

708 709
  if (press_type == GIMP_BUTTON_PRESS_NORMAL &&
      gimp_tool_control_is_active (tool->control))
710 711 712
    {
      tool->button_press_state    = state;
      tool->active_modifier_state = state;
713 714 715

      if (gimp_tool_control_get_wants_click (tool->control))
        {
716
          tool->in_click_distance   = TRUE;
717
          tool->got_motion_event    = FALSE;
718 719
          tool->button_press_coords = *coords;
          tool->button_press_time   = time;
720 721 722
        }
      else
        {
723
          tool->in_click_distance   = FALSE;
724 725 726 727
        }
    }
}

728
static gboolean
729 730 731 732
gimp_tool_check_click_distance (GimpTool         *tool,
                                const GimpCoords *coords,
                                guint32           time,
                                GimpDisplay      *display)
733
{
734 735 736
  GimpDisplayShell *shell;
  gint              double_click_time;
  gint              double_click_distance;
737 738 739

  if (! tool->in_click_distance)
    return FALSE;
740

741 742 743
  shell = gimp_display_get_shell (display);

  g_object_get (gtk_widget_get_settings (GTK_WIDGET (shell)),
744 745 746 747
                "gtk-double-click-time",     &double_click_time,
                "gtk-double-click-distance", &double_click_distance,
                NULL);

748
  if ((time - tool->button_press_time) > double_click_time)
749 750
    {
      tool->in_click_distance = FALSE;
751
    }
752 753
  else
    {
754
      GimpDisplayShell *shell = gimp_display_get_shell (display);
755 756 757 758 759 760
      gdouble           dx;
      gdouble           dy;

      dx = SCALEX (shell, tool->button_press_coords.x - coords->x);
      dy = SCALEY (shell, tool->button_press_coords.y - coords->y);

761
      if ((SQR (dx) + SQR (dy)) > SQR (double_click_distance))
762 763 764 765 766 767
        {
          tool->in_click_distance = FALSE;
        }
    }

  return tool->in_click_distance;
768 769 770
}

void
771 772 773 774 775
gimp_tool_button_release (GimpTool         *tool,
                          const GimpCoords *coords,
                          guint32           time,
                          GdkModifierType   state,
                          GimpDisplay      *display)
776
{
777
  GimpButtonReleaseType release_type = GIMP_BUTTON_RELEASE_NORMAL;
778
  GimpCoords            my_coords;
779

780 781
  g_return_if_fail (GIMP_IS_TOOL (tool));
  g_return_if_fail (coords != NULL);
782
  g_return_if_fail (GIMP_IS_DISPLAY (display));
783
  g_return_if_fail (gimp_tool_control_is_active (tool->control) == TRUE);
784

785 786
  g_object_ref (tool);

787 788
  my_coords = *coords;

789
  if (state & GDK_BUTTON3_MASK)
790 791 792
    {
      release_type = GIMP_BUTTON_RELEASE_CANCEL;
    }
793
  else if (gimp_tool_control_get_wants_click (tool->control))
794
    {
795 796 797 798 799
      if (gimp_tool_check_click_distance (tool, coords, time, display))
        {
          release_type = GIMP_BUTTON_RELEASE_CLICK;
          my_coords    = tool->button_press_coords;

800 801 802 803 804 805 806 807 808 809
          if (tool->got_motion_event)
            {
              /*  if there has been a motion() since button_press(),
               *  synthesize a motion() back to the recorded press
               *  coordinates
               */
              GIMP_TOOL_GET_CLASS (tool)->motion (tool, &my_coords, time,
                                                  state & GDK_BUTTON1_MASK,
                                                  display);
            }
810 811 812 813 814
        }
      else if (! tool->got_motion_event)
        {
          release_type = GIMP_BUTTON_RELEASE_NO_MOTION;
        }
815
    }
816

817
  GIMP_TOOL_GET_CLASS (tool)->button_release (tool, &my_coords, time, state,
818
                                              release_type, display);
819

820 821
  g_warn_if_fail (gimp_tool_control_is_active (tool->control) == FALSE);

822
  if (tool->active_modifier_state != 0)
823 824 825 826 827 828 829
    {
      gimp_tool_control_activate (tool->control);

      gimp_tool_set_active_modifier_state (tool, 0, display);

      gimp_tool_control_halt (tool->control);
    }
830 831 832 833

  tool->button_press_state = 0;

  g_object_unref (tool);
834 835 836
}

void
837 838 839 840 841
gimp_tool_motion (GimpTool         *tool,
                  const GimpCoords *coords,
                  guint32           time,
                  GdkModifierType   state,
                  GimpDisplay      *display)
842 843 844
{
  g_return_if_fail (GIMP_IS_TOOL (tool));
  g_return_if_fail (coords != NULL);
845
  g_return_if_fail (GIMP_IS_DISPLAY (display));
846
  g_return_if_fail (gimp_tool_control_is_active (tool->control) == TRUE);
847

848
  tool->got_motion_event = TRUE;
849

850
  GIMP_TOOL_GET_CLASS (tool)->motion (tool, coords, time, state, display);
851 852
}

853 854
void
gimp_tool_set_focus_display (GimpTool    *tool,
855
                             GimpDisplay *display)
856 857
{
  g_return_if_fail (GIMP_IS_TOOL (tool));
858
  g_return_if_fail (display == NULL || GIMP_IS_DISPLAY (display));
859
  g_return_if_fail (gimp_tool_control_is_active (tool->control) == FALSE);
860

861 862
  GIMP_LOG (TOOL_FOCUS, "tool: %p  focus_display: %p  tool->focus_display: %p",
            tool, display, tool->focus_display);
863

864
  if (display != tool->focus_display)
865 866 867
    {
      if (tool->focus_display)
        {
868
          if (tool->active_modifier_state != 0)
869 870 871 872 873 874 875
            {
              gimp_tool_control_activate (tool->control);

              gimp_tool_set_active_modifier_state (tool, 0, tool->focus_display);

              gimp_tool_control_halt (tool->control);
            }
876

877 878 879 880
          if (tool->modifier_state != 0)
            gimp_tool_set_modifier_state (tool, 0, tool->focus_display);
        }

881
      tool->focus_display = display;
882 883 884
    }
}

885
gboolean
886
gimp_tool_key_press (GimpTool    *tool,
887
                     GdkEventKey *kevent,
888
                     GimpDisplay *display)
889
{
890
  g_return_val_if_fail (GIMP_IS_TOOL (tool), FALSE);
891 892
  g_return_val_if_fail (GIMP_IS_DISPLAY (display), FALSE);
  g_return_val_if_fail (display == tool->focus_display, FALSE);
893 894
  g_return_val_if_fail (gimp_tool_control_is_active (tool->control) == FALSE,
                        FALSE);
895

896
  return GIMP_TOOL_GET_CLASS (tool)->key_press (tool, kevent, display);
897 898
}

899 900 901 902 903 904 905 906
gboolean
gimp_tool_key_release (GimpTool    *tool,
                       GdkEventKey *kevent,
                       GimpDisplay *display)
{
  g_return_val_if_fail (GIMP_IS_TOOL (tool), FALSE);
  g_return_val_if_fail (GIMP_IS_DISPLAY (display), FALSE);
  g_return_val_if_fail (display == tool->focus_display, FALSE);
907 908
  g_return_val_if_fail (gimp_tool_control_is_active (tool->control) == FALSE,
                        FALSE);
909 910 911 912

  return GIMP_TOOL_GET_CLASS (tool)->key_release (tool, kevent, display);
}

913
static void
914 915 916 917
gimp_tool_modifier_key (GimpTool        *tool,
                        GdkModifierType  key,
                        gboolean         press,
                        GdkModifierType  state,
918
                        GimpDisplay     *display)
919 920
{
  g_return_if_fail (GIMP_IS_TOOL (tool));
921 922
  g_return_if_fail (GIMP_IS_DISPLAY (display));
  g_return_if_fail (display == tool->focus_display);
923

924
  GIMP_TOOL_GET_CLASS (tool)->modifier_key (tool, key, press, state, display);
925 926
}

927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
static void
gimp_tool_active_modifier_key (GimpTool        *tool,
                               GdkModifierType  key,
                               gboolean         press,
                               GdkModifierType  state,
                               GimpDisplay     *display)
{
  g_return_if_fail (GIMP_IS_TOOL (tool));
  g_return_if_fail (GIMP_IS_DISPLAY (display));
  g_return_if_fail (display == tool->focus_display);

  GIMP_TOOL_GET_CLASS (tool)->active_modifier_key (tool, key, press, state,
                                                   display);
}

static gboolean
state_changed (GdkModifierType  old_state,
               GdkModifierType  new_state,
               GdkModifierType  modifier,
               gboolean        *press)
{
  if ((old_state & modifier) != (new_state & modifier))
    {
      *press = (new_state & modifier) ? TRUE : FALSE;

      return TRUE;
    }

  return FALSE;
}

958 959 960
void
gimp_tool_set_modifier_state (GimpTool        *tool,
                              GdkModifierType  state,
961
                              GimpDisplay     *display)
962
{
963 964
  gboolean press;

965
  g_return_if_fail (GIMP_IS_TOOL (tool));
966
  g_return_if_fail (GIMP_IS_DISPLAY (display));
967
  g_return_if_fail (gimp_tool_control_is_active (tool->control) == FALSE);
968

969 970
  GIMP_LOG (TOOL_FOCUS, "tool: %p  display: %p  tool->focus_display: %p",
            tool, display, tool->focus_display);
971

972
  g_return_if_fail (display == tool->focus_display);
973

974
  if (state_changed (tool->modifier_state, state, GDK_SHIFT_MASK, &press))
975 976
    {
      gimp_tool_modifier_key (tool, GDK_SHIFT_MASK,
977
                              press, state,
978
                              display);
979 980
    }

981
  if (state_changed (tool->modifier_state, state, GDK_CONTROL_MASK, &press))
982 983
    {
      gimp_tool_modifier_key (tool, GDK_CONTROL_MASK,
984
                              press, state,
985
                              display);
986 987
    }

988
  if (state_changed (tool->modifier_state, state, GDK_MOD1_MASK, &press))
989 990
    {
      gimp_tool_modifier_key (tool, GDK_MOD1_MASK,
991
                              press, state,
992
                              display);
993 994
    }

995
  if (state_changed (tool->modifier_state, state, GDK_MOD2_MASK, &press))
996 997
    {
      gimp_tool_modifier_key (tool, GDK_MOD2_MASK,
998
                              press, state,
999 1000 1001
                              display);
    }

1002 1003 1004
  tool->modifier_state = state;
}

1005 1006 1007 1008 1009
void
gimp_tool_set_active_modifier_state (GimpTool        *tool,
                                     GdkModifierType  state,
                                     GimpDisplay     *display)
{
1010 1011
  gboolean press;

1012 1013
  g_return_if_fail (GIMP_IS_TOOL (tool));
  g_return_if_fail (GIMP_IS_DISPLAY (display));
1014
  g_return_if_fail (gimp_tool_control_is_active (tool->control) == TRUE);
1015

1016 1017 1018
  GIMP_LOG (TOOL_FOCUS, "tool: %p  display: %p  tool->focus_display: %p",
            tool, display, tool->focus_display);

1019 1020
  g_return_if_fail (display == tool->focus_display);

1021 1022
  if (state_changed (tool->active_modifier_state, state, GDK_SHIFT_MASK,
                     &press))
1023 1024 1025
    {
#ifdef DEBUG_ACTIVE_STATE
      g_printerr ("%s: SHIFT %s\n", G_STRFUNC,
1026
                  press ? "pressed" : "released");
1027 1028
#endif

1029
      if (! press && (tool->button_press_state & GDK_SHIFT_MASK))
1030 1031 1032 1033 1034 1035
        {
          tool->button_press_state &= ~GDK_SHIFT_MASK;
        }
      else
        {
          gimp_tool_active_modifier_key (tool, GDK_SHIFT_MASK,
1036
                                         press, state,
1037 1038 1039 1040
                                         display);
        }
    }

1041 1042
  if (state_changed (tool->active_modifier_state, state, GDK_CONTROL_MASK,
                     &press))
1043 1044 1045
    {
#ifdef DEBUG_ACTIVE_STATE
      g_printerr ("%s: CONTROL %s\n", G_STRFUNC,
1046
                  press ? "pressed" : "released");
1047 1048
#endif

1049
      if (! press && (tool->button_press_state & GDK_CONTROL_MASK))
1050
        {
1051
          tool->button_press_state &= ~GDK_CONTROL_MASK;
1052 1053 1054 1055
        }
      else
        {
          gimp_tool_active_modifier_key (tool, GDK_CONTROL_MASK,
1056
                                         press, state,
1057 1058 1059 1060
                                         display);
        }
    }

1061 1062
  if (state_changed (tool->active_modifier_state, state, GDK_MOD1_MASK,
                     &press))
1063 1064 1065
    {
#ifdef DEBUG_ACTIVE_STATE
      g_printerr ("%s: ALT %s\n", G_STRFUNC,
1066
                  press ? "pressed" : "released");
1067 1068
#endif

1069
      if (! press && (tool->button_press_state & GDK_MOD1_MASK))
1070 1071 1072 1073 1074 1075
        {
          tool->button_press_state &= ~GDK_MOD1_MASK;
        }
      else
        {
          gimp_tool_active_modifier_key (tool, GDK_MOD1_MASK,
1076
                                         press, state,
1077 1078
                                         display);
        }
1079 1080
    }

1081 1082
  if (state_changed (tool->active_modifier_state, state, GDK_MOD2_MASK,
                     &press))
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
    {
#ifdef DEBUG_ACTIVE_STATE
      g_printerr ("%s: MOD2 %s\n", G_STRFUNC,
                  press ? "pressed" : "released");
#endif

      if (! press && (tool->button_press_state & GDK_MOD2_MASK))
        {
          tool->button_press_state &= ~GDK_MOD2_MASK;
        }
      else
        {
          gimp_tool_active_modifier_key (tool, GDK_MOD2_MASK,
                                         press, state,
                                         display);
        }
1099 1100 1101 1102 1103
    }

  tool->active_modifier_state = state;
}

1104
void
1105 1106 1107 1108 1109
gimp_tool_oper_update (GimpTool         *tool,
                       const GimpCoords *coords,
                       GdkModifierType   state,
                       gboolean          proximity,
                       GimpDisplay      *display)
1110 1111 1112
{
  g_return_if_fail (GIMP_IS_TOOL (tool));
  g_return_if_fail (coords != NULL);
1113
  g_return_if_fail (GIMP_IS_DISPLAY (display));
1114
  g_return_if_fail (gimp_tool_control_is_active (tool->control) == FALSE);
1115

1116
  GIMP_TOOL_GET_CLASS (tool)->oper_update (tool, coords, state, proximity,
1117
                                           display);
1118

1119
  if (G_UNLIKELY (gimp_image_is_empty (gimp_display_get_image (display)) &&
1120 1121 1122 1123 1124 1125 1126
                  ! gimp_tool_control_get_handle_empty_image (tool->control)))
    {
      gimp_tool_replace_status (tool, display,
                                "%s",
                                _("Can't work on an empty image, "
                                  "add a layer first"));
    }
1127 1128 1129
}

void
1130 1131 1132 1133
gimp_tool_cursor_update (GimpTool         *tool,
                         const GimpCoords *coords,
                         GdkModifierType   state,
                         GimpDisplay      *display)
1134 1135 1136
{
  g_return_if_fail (GIMP_IS_TOOL (tool));
  g_return_if_fail (coords != NULL);
1137
  g_return_if_fail (GIMP_IS_DISPLAY (display));
1138
  g_return_if_fail (gimp_tool_control_is_active (tool->control) == FALSE);
1139

1140
  GIMP_TOOL_GET_CLASS (tool)->cursor_update (tool, coords, state, display);
1141 1142
}

1143
const gchar *
1144 1145
gimp_tool_can_undo (GimpTool    *tool,
                    GimpDisplay *display)
1146 1147 1148 1149 1150
{
  g_return_val_if_fail (GIMP_IS_TOOL (tool), NULL);
  g_return_val_if_fail (GIMP_IS_DISPLAY (display), NULL);

  if (display == tool->display)
1151
    return GIMP_TOOL_GET_CLASS (tool)->can_undo (tool, display);
1152 1153 1154 1155 1156

  return NULL;
}

const gchar *
1157 1158
gimp_tool_can_redo (GimpTool    *tool,
                    GimpDisplay *display)
1159 1160 1161 1162 1163
{
  g_return_val_if_fail (GIMP_IS_TOOL (tool), NULL);
  g_return_val_if_fail (GIMP_IS_DISPLAY (display), NULL);

  if (display == tool->display)
1164
    return GIMP_TOOL_GET_CLASS (tool)->can_redo (tool, display);
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175

  return NULL;
}

gboolean
gimp_tool_undo (GimpTool    *tool,
                GimpDisplay *display)
{
  g_return_val_if_fail (GIMP_IS_TOOL (tool), FALSE);
  g_return_val_if_fail (GIMP_IS_DISPLAY (display), FALSE);

1176
  if (gimp_tool_can_undo (tool, display))
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
    return GIMP_TOOL_GET_CLASS (tool)->undo (tool, display);

  return FALSE;
}

gboolean
gimp_tool_redo (GimpTool    *tool,
                GimpDisplay *display)
{
  g_return_val_if_fail (GIMP_IS_TOOL (tool), FALSE);
  g_return_val_if_fail (GIMP_IS_DISPLAY (display), FALSE);

1189
  if (gimp_tool_can_redo (tool, display))
1190 1191 1192 1193 1194
    return GIMP_TOOL_GET_CLASS (tool)->redo (tool, display);

  return FALSE;
}

1195 1196
GimpUIManager *
gimp_tool_get_popup (GimpTool         *tool,
1197
                     const GimpCoords *coords,
1198 1199 1200 1201
                     GdkModifierType   state,
                     GimpDisplay      *display,
                     const gchar     **ui_path)
{
Sven Neumann's avatar
Sven Neumann committed
1202 1203 1204 1205
  g_return_val_if_fail (GIMP_IS_TOOL (tool), NULL);
  g_return_val_if_fail (coords != NULL, NULL);
  g_return_val_if_fail (GIMP_IS_DISPLAY (display), NULL);
  g_return_val_if_fail (ui_path != NULL, NULL);
1206 1207 1208 1209 1210

  return GIMP_TOOL_GET_CLASS (tool)->get_popup (tool, coords, state, display,
                                                ui_path);
}

1211 1212
void
gimp_tool_push_status (GimpTool    *tool,
1213
                       GimpDisplay *display,
1214 1215
                       const gchar *format,
                       ...)
1216
{
1217
  GimpDisplayShell *shell;
1218
  const gchar      *icon_name;
1219
  va_list           args;
1220 1221

  g_return_if_fail (GIMP_IS_TOOL (tool));
1222
  g_return_if_fail (GIMP_IS_DISPLAY (display));
1223
  g_return_if_fail (format != NULL);
1224

1225
  shell = gimp_display_get_shell (display);
1226

1227
  icon_name = gimp_viewable_get_icon_name (GIMP_VIEWABLE (tool->tool_info));
1228

1229
  va_start (args, format);
1230

1231
  gimp_statusbar_push_valist (gimp_display_shell_get_statusbar (shell),
1232
                              G_OBJECT_TYPE_NAME (tool), icon_name,
1233
                              format, args);
1234