gimpdisplayshell-tool-events.c 71.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23
 */

#include "config.h"

#include <gegl.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>

24 25
#include "libgimpwidgets/gimpwidgets.h"

26 27 28 29 30 31
#include "display-types.h"
#include "tools/tools-types.h"

#include "config/gimpdisplayconfig.h"

#include "core/gimp.h"
32
#include "core/gimp-filter-history.h"
33
#include "core/gimpcontext.h"
34
#include "core/gimpimage.h"
35
#include "core/gimpimage-pick-item.h"
36
#include "core/gimpitem.h"
37 38 39

#include "widgets/gimpcontrollers.h"
#include "widgets/gimpcontrollerkeyboard.h"
40
#include "widgets/gimpcontrollermouse.h"
41 42 43
#include "widgets/gimpcontrollerwheel.h"
#include "widgets/gimpdeviceinfo.h"
#include "widgets/gimpdeviceinfo-coords.h"
44
#include "widgets/gimpdevicemanager.h"
45 46 47
#include "widgets/gimpdevices.h"
#include "widgets/gimpdialogfactory.h"
#include "widgets/gimpuimanager.h"
48
#include "widgets/gimpwidgets-utils.h"
49

50
#include "tools/gimpguidetool.h"
51
#include "tools/gimpmovetool.h"
52
#include "tools/gimpsamplepointtool.h"
53 54 55
#include "tools/gimptoolcontrol.h"
#include "tools/tool_manager.h"

56
#include "gimpcanvas.h"
57 58 59 60 61 62
#include "gimpdisplay.h"
#include "gimpdisplayshell.h"
#include "gimpdisplayshell-autoscroll.h"
#include "gimpdisplayshell-cursor.h"
#include "gimpdisplayshell-grab.h"
#include "gimpdisplayshell-layer-select.h"
63
#include "gimpdisplayshell-rotate.h"
64 65 66 67 68
#include "gimpdisplayshell-scale.h"
#include "gimpdisplayshell-scroll.h"
#include "gimpdisplayshell-tool-events.h"
#include "gimpdisplayshell-transform.h"
#include "gimpimagewindow.h"
69
#include "gimpmotionbuffer.h"
70
#include "gimpstatusbar.h"
71

72
#include "gimp-intl.h"
73 74 75 76 77 78
#include "gimp-log.h"


/*  local function prototypes  */

static GdkModifierType
Ell's avatar
Ell committed
79
                  gimp_display_shell_key_to_state             (gint               key);
80
static GdkModifierType
Ell's avatar
Ell committed
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
                  gimp_display_shell_button_to_state          (gint               button);

static void       gimp_display_shell_proximity_in             (GimpDisplayShell  *shell);
static void       gimp_display_shell_proximity_out            (GimpDisplayShell  *shell);

static gboolean   gimp_display_shell_check_device             (GimpDisplayShell  *shell,
                                                               GdkEvent          *event,
                                                               gboolean          *device_changed);
static void       gimp_display_shell_check_device_cursor      (GimpDisplayShell  *shell);

static void       gimp_display_shell_start_scrolling          (GimpDisplayShell  *shell,
                                                               const GdkEvent    *event,
                                                               GdkModifierType    state,
                                                               gint               x,
                                                               gint               y);
static void       gimp_display_shell_stop_scrolling           (GimpDisplayShell  *shell,
                                                               const GdkEvent    *event);
static void       gimp_display_shell_handle_scrolling         (GimpDisplayShell  *shell,
                                                               GdkModifierType    state,
                                                               gint               x,
                                                               gint               y);

static void       gimp_display_shell_space_pressed            (GimpDisplayShell  *shell,
                                                               const GdkEvent    *event);
static void       gimp_display_shell_released                 (GimpDisplayShell  *shell,
                                                               const GdkEvent    *event,
                                                               const GimpCoords  *image_coords);

static gboolean   gimp_display_shell_tab_pressed              (GimpDisplayShell  *shell,
                                                               const GdkEventKey *event);

static void       gimp_display_shell_update_focus             (GimpDisplayShell  *shell,
                                                               gboolean           focus_in,
                                                               const GimpCoords  *image_coords,
                                                               GdkModifierType    state);
static void       gimp_display_shell_update_cursor            (GimpDisplayShell  *shell,
                                                               const GimpCoords  *display_coords,
                                                               const GimpCoords  *image_coords,
                                                               GdkModifierType    state,
                                                               gboolean           update_software_cursor);

static gboolean   gimp_display_shell_initialize_tool          (GimpDisplayShell  *shell,
                                                               const GimpCoords  *image_coords,
                                                               GdkModifierType    state);

static void       gimp_display_shell_get_event_coords         (GimpDisplayShell  *shell,
                                                               const GdkEvent    *event,
                                                               GimpCoords        *display_coords,
                                                               GdkModifierType   *state,
                                                               guint32           *time);
static void       gimp_display_shell_untransform_event_coords (GimpDisplayShell  *shell,
                                                               const GimpCoords  *display_coords,
                                                               GimpCoords        *image_coords,
                                                               gboolean          *update_software_cursor);
135

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

/*  public functions  */

gboolean
gimp_display_shell_events (GtkWidget        *widget,
                           GdkEvent         *event,
                           GimpDisplayShell *shell)
{
  Gimp     *gimp;
  gboolean  set_display = FALSE;

  /*  are we in destruction?  */
  if (! shell->display || ! gimp_display_get_shell (shell->display))
    return TRUE;

  gimp = gimp_display_get_gimp (shell->display);

  switch (event->type)
    {
    case GDK_KEY_PRESS:
    case GDK_KEY_RELEASE:
      {
        GdkEventKey *kevent = (GdkEventKey *) event;

        if (gimp->busy)
          return TRUE;

163
        /*  do not process most key events while BUTTON1 is down. We do this
164 165 166 167 168 169 170 171 172 173
         *  so tools keep the modifier state they were in when BUTTON1 was
         *  pressed and to prevent accelerators from being invoked.
         */
        if (kevent->state & GDK_BUTTON1_MASK)
          {
            if (kevent->keyval == GDK_KEY_Shift_L   ||
                kevent->keyval == GDK_KEY_Shift_R   ||
                kevent->keyval == GDK_KEY_Control_L ||
                kevent->keyval == GDK_KEY_Control_R ||
                kevent->keyval == GDK_KEY_Alt_L     ||
174 175
                kevent->keyval == GDK_KEY_Alt_R     ||
                kevent->keyval == GDK_KEY_Meta_L    ||
176 177 178
                kevent->keyval == GDK_KEY_Meta_R    ||
                kevent->keyval == GDK_KEY_space     ||
                kevent->keyval == GDK_KEY_KP_Space)
179 180 181 182 183 184 185 186 187 188 189 190 191 192
              {
                break;
              }

            return TRUE;
          }

        switch (kevent->keyval)
          {
          case GDK_KEY_Left:      case GDK_KEY_Right:
          case GDK_KEY_Up:        case GDK_KEY_Down:
          case GDK_KEY_space:
          case GDK_KEY_KP_Space:
          case GDK_KEY_Tab:
193
          case GDK_KEY_KP_Tab:
194 195 196 197
          case GDK_KEY_ISO_Left_Tab:
          case GDK_KEY_Alt_L:     case GDK_KEY_Alt_R:
          case GDK_KEY_Shift_L:   case GDK_KEY_Shift_R:
          case GDK_KEY_Control_L: case GDK_KEY_Control_R:
198
          case GDK_KEY_Meta_L:    case GDK_KEY_Meta_R:
199 200 201 202 203 204 205 206
          case GDK_KEY_Return:
          case GDK_KEY_KP_Enter:
          case GDK_KEY_ISO_Enter:
          case GDK_KEY_BackSpace:
          case GDK_KEY_Escape:
            break;

          default:
207 208 209
            if (shell->space_release_pending   ||
                shell->button1_release_pending ||
                shell->scrolling)
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
              return TRUE;
            break;
          }

        set_display = TRUE;
        break;
      }

    case GDK_BUTTON_PRESS:
    case GDK_SCROLL:
      set_display = TRUE;
      break;

    case GDK_FOCUS_CHANGE:
      {
        GdkEventFocus *fevent = (GdkEventFocus *) event;

        if (fevent->in && shell->display->config->activate_on_focus)
          set_display = TRUE;
      }
      break;

    default:
      break;
    }

  /*  Setting the context's display automatically sets the image, too  */
  if (set_display)
    gimp_context_set_display (gimp_get_user_context (gimp), shell->display);

  return FALSE;
}

243 244 245 246
static gboolean
gimp_display_shell_canvas_no_image_events (GtkWidget        *canvas,
                                           GdkEvent         *event,
                                           GimpDisplayShell *shell)
247
{
248 249
  switch (event->type)
    {
250 251 252 253 254 255 256 257 258 259 260 261 262 263
    case GDK_2BUTTON_PRESS:
      {
        GdkEventButton  *bevent = (GdkEventButton *) event;
        if (bevent->button == 1)
          {
            GimpImageWindow *window  = gimp_display_shell_get_window (shell);
            GimpUIManager   *manager = gimp_image_window_get_ui_manager (window);

            gimp_ui_manager_activate_action (manager, "file", "file-open");
          }
        return TRUE;
      }
      break;

264
    case GDK_BUTTON_PRESS:
265 266
      if (gdk_event_triggers_context_menu (event))
        {
267 268 269 270
          gimp_ui_manager_ui_popup_at_pointer (shell->popup_manager,
                                               "/dummy-menubar/image-popup",
                                               (GdkEvent *) event,
                                               NULL, NULL);
271 272
          return TRUE;
        }
273
      break;
274

275 276 277
    case GDK_KEY_PRESS:
      {
        GdkEventKey *kevent = (GdkEventKey *) event;
278

279 280
        if (kevent->keyval == GDK_KEY_Tab    ||
            kevent->keyval == GDK_KEY_KP_Tab ||
281 282
            kevent->keyval == GDK_KEY_ISO_Left_Tab)
          {
283
            return gimp_display_shell_tab_pressed (shell, kevent);
284 285 286
          }
      }
      break;
287

288 289 290
    default:
      break;
    }
291

292
  return FALSE;
293 294
}

295 296 297 298
gboolean
gimp_display_shell_canvas_tool_events (GtkWidget        *canvas,
                                       GdkEvent         *event,
                                       GimpDisplayShell *shell)
299
{
300 301 302 303 304 305 306 307 308 309
  GimpDisplay     *display;
  GimpImage       *image;
  Gimp            *gimp;
  GimpCoords       display_coords;
  GimpCoords       image_coords;
  GdkModifierType  state;
  guint32          time;
  gboolean         device_changed   = FALSE;
  gboolean         return_val       = FALSE;
  gboolean         update_sw_cursor = FALSE;
310

Ell's avatar
Ell committed
311
  g_return_val_if_fail (gtk_widget_get_realized (canvas), FALSE);
312

313 314 315
  /*  are we in destruction?  */
  if (! shell->display || ! gimp_display_get_shell (shell->display))
    return TRUE;
316

317 318 319
  /*  set the active display before doing any other canvas event processing  */
  if (gimp_display_shell_events (canvas, event, shell))
    return TRUE;
320

321 322 323
  /*  events on overlays have a different window, but these windows'
   *  user_data can still be the canvas, we need to check manually if
   *  the event's window and the canvas' window are different.
324
   */
325
  if (event->any.window != gtk_widget_get_window (canvas))
326
    {
327 328 329 330 331 332 333 334 335
      GtkWidget *event_widget;

      gdk_window_get_user_data (event->any.window, (gpointer) &event_widget);

      /*  if the event came from a different window than the canvas',
       *  check if it came from a canvas child and bail out.
       */
      if (gtk_widget_get_ancestor (event_widget, GIMP_TYPE_CANVAS))
        return FALSE;
336
    }
337

338 339 340
  display = shell->display;
  gimp    = gimp_display_get_gimp (display);
  image   = gimp_display_get_image (display);
341

342 343
  if (! image)
    return gimp_display_shell_canvas_no_image_events (canvas, event, shell);
344

345 346 347
  GIMP_LOG (TOOL_EVENTS, "event (display %p): %s",
            display, gimp_print_event (event));

348 349
  if (gimp_display_shell_check_device (shell, event, &device_changed))
    return TRUE;
350

351 352 353 354 355 356 357 358 359 360 361 362
  gimp_display_shell_get_event_coords (shell, event,
                                       &display_coords,
                                       &state, &time);
  gimp_display_shell_untransform_event_coords (shell,
                                               &display_coords, &image_coords,
                                               &update_sw_cursor);

  /*  If the device (and maybe the tool) has changed, update the new
   *  tool's state
   */
  if (device_changed && gtk_widget_has_focus (canvas))
    {
363 364
      gimp_display_shell_update_focus (shell, TRUE,
                                       &image_coords, state);
365 366 367 368 369
    }

  switch (event->type)
    {
    case GDK_ENTER_NOTIFY:
370
      {
371
        GdkEventCrossing *cevent = (GdkEventCrossing *) event;
372

373 374
        if (cevent->mode != GDK_CROSSING_NORMAL)
          return TRUE;
375

376
        /*  ignore enter notify while we have a grab  */
377
        if (shell->grab_pointer)
378 379
          return TRUE;

380
        gimp_display_shell_proximity_in (shell);
381 382 383 384 385 386
        update_sw_cursor = TRUE;

        tool_manager_oper_update_active (gimp,
                                         &image_coords, state,
                                         shell->proximity,
                                         display);
387 388 389
      }
      break;

390
    case GDK_LEAVE_NOTIFY:
391
      {
392
        GdkEventCrossing *cevent = (GdkEventCrossing *) event;
393

394 395
        if (cevent->mode != GDK_CROSSING_NORMAL)
          return TRUE;
396

397
        /*  ignore leave notify while we have a grab  */
398
        if (shell->grab_pointer)
399 400
          return TRUE;

401
        gimp_display_shell_proximity_out (shell);
402

403 404 405 406
        tool_manager_oper_update_active (gimp,
                                         &image_coords, state,
                                         shell->proximity,
                                         display);
407 408 409
      }
      break;

410
    case GDK_PROXIMITY_IN:
411
      gimp_display_shell_proximity_in (shell);
412

413 414 415 416 417
      tool_manager_oper_update_active (gimp,
                                       &image_coords, state,
                                       shell->proximity,
                                       display);
      break;
418

419
    case GDK_PROXIMITY_OUT:
420
      gimp_display_shell_proximity_out (shell);
421

422 423 424 425
      tool_manager_oper_update_active (gimp,
                                       &image_coords, state,
                                       shell->proximity,
                                       display);
426 427
      break;

428
    case GDK_FOCUS_CHANGE:
429
      {
430
        GdkEventFocus *fevent = (GdkEventFocus *) event;
431

432 433 434 435
        if (fevent->in)
          {
            if (G_UNLIKELY (! gtk_widget_has_focus (canvas)))
              g_warning ("%s: FOCUS_IN but canvas has no focus", G_STRFUNC);
436

437
            /*  ignore focus changes while we have a grab  */
438
            if (shell->grab_pointer)
439 440 441 442 443
              return TRUE;

            /*   press modifier keys when the canvas gets the focus  */
            gimp_display_shell_update_focus (shell, TRUE,
                                             &image_coords, state);
444 445 446
          }
        else
          {
447
            if (G_UNLIKELY (gtk_widget_has_focus (canvas)))
448 449
              g_warning ("%s: FOCUS_OUT but canvas has focus", G_STRFUNC);

450
            /*  ignore focus changes while we have a grab  */
451
            if (shell->grab_pointer)
452 453
              return TRUE;

454
            /*  release modifier keys when the canvas loses the focus  */
455 456
            gimp_display_shell_update_focus (shell, FALSE,
                                             &image_coords, 0);
457 458 459 460 461 462
          }
      }
      break;

    case GDK_BUTTON_PRESS:
      {
463 464
        GdkEventButton  *bevent = (GdkEventButton *) event;
        GdkModifierType  button_state;
465

466
        /*  ignore new mouse events  */
467
        if (gimp->busy || shell->scrolling || shell->grab_pointer ||
468
            shell->button1_release_pending)
469 470
          return TRUE;

471 472 473
        button_state = gimp_display_shell_button_to_state (bevent->button);

        state |= button_state;
474

475
        /* ignore new buttons while another button is down */
476 477 478 479 480 481
        if (((state & (GDK_BUTTON1_MASK)) && (state & (GDK_BUTTON2_MASK |
                                                       GDK_BUTTON3_MASK))) ||
            ((state & (GDK_BUTTON2_MASK)) && (state & (GDK_BUTTON1_MASK |
                                                       GDK_BUTTON3_MASK))) ||
            ((state & (GDK_BUTTON3_MASK)) && (state & (GDK_BUTTON1_MASK |
                                                       GDK_BUTTON2_MASK))))
482 483
          return TRUE;

484 485 486 487 488 489 490 491 492 493
        /*  focus the widget if it isn't; if the toplevel window
         *  already has focus, this will generate a FOCUS_IN on the
         *  canvas immediately, therefore we do this before logging
         *  the BUTTON_PRESS.
         */
        if (! gtk_widget_has_focus (canvas))
          gtk_widget_grab_focus (canvas);

        /*  if the toplevel window didn't have focus, the above
         *  gtk_widget_grab_focus() didn't set the canvas' HAS_FOCUS
494 495 496
         *  flags, and didn't trigger a FOCUS_IN, but the tool needs
         *  to be set up correctly regardless, so simply do the
         *  same things here, it's safe to do them redundantly.
497
         */
498 499
        gimp_display_shell_update_focus (shell, TRUE,
                                         &image_coords, state);
500
        gimp_display_shell_update_cursor (shell, &display_coords,
501 502
                                          &image_coords, state & ~button_state,
                                          FALSE);
503

504
        if (gdk_event_triggers_context_menu (event))
505
          {
506 507
            GimpUIManager *ui_manager;
            const gchar   *ui_path;
508

509 510 511 512 513
            ui_manager = tool_manager_get_popup_active (gimp,
                                                        &image_coords, state,
                                                        display,
                                                        &ui_path);

514
            if (! ui_manager)
515
              {
516 517
                ui_manager = shell->popup_manager;
                ui_path    = "/dummy-menubar/image-popup";
518
              }
519 520 521

            gimp_ui_manager_ui_popup_at_pointer (ui_manager, ui_path, event,
                                                 NULL, NULL);
522 523 524
          }
        else if (bevent->button == 1)
          {
525 526 527
            if (! gimp_display_shell_pointer_grab (shell, event,
                                                   GDK_POINTER_MOTION_MASK |
                                                   GDK_BUTTON_RELEASE_MASK))
528 529
              return TRUE;

530
            if (! shell->space_release_pending)
531 532
              if (! gimp_display_shell_keyboard_grab (shell, event))
                {
533
                  gimp_display_shell_pointer_ungrab (shell, event);
534 535 536
                  return TRUE;
                }

537 538
            if (gimp_display_shell_initialize_tool (shell,
                                                    &image_coords, state))
539
              {
Ell's avatar
Ell committed
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
                GimpTool       *active_tool;
                GimpMotionMode  motion_mode;
                GimpCoords      last_motion;

                active_tool = tool_manager_get_active (gimp);
                motion_mode = gimp_tool_control_get_motion_mode (
                                active_tool->control);

                if (motion_mode == GIMP_MOTION_MODE_EXACT)
                  {
                    /* enable motion compression for the canvas window for the
                     * duration of the stroke
                     */
                    gdk_window_set_event_compression (
                      gtk_widget_get_window (canvas), FALSE);
                  }
556

557
                /* Use the last evaluated velocity&direction instead of the
558 559
                 * button_press event's ones because the click is
                 * usually at the same spot as the last motion event
560
                 * which would give us bogus derivate dynamics.
561
                 */
562
                gimp_motion_buffer_begin_stroke (shell->motion_buffer, time,
563
                                                 &last_motion);
564

565 566 567
                image_coords.velocity = last_motion.velocity;
                image_coords.direction = last_motion.direction;

568 569 570 571 572
                tool_manager_button_press_active (gimp,
                                                  &image_coords,
                                                  time, state,
                                                  GIMP_BUTTON_PRESS_NORMAL,
                                                  display);
573
              }
574 575 576
          }
        else if (bevent->button == 2)
          {
577
            gimp_display_shell_start_scrolling (shell, event, state,
578
                                                bevent->x, bevent->y);
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
          }

        return_val = TRUE;
      }
      break;

    case GDK_2BUTTON_PRESS:
      {
        GdkEventButton *bevent = (GdkEventButton *) event;
        GimpTool       *active_tool;

        if (gimp->busy)
          return TRUE;

        active_tool = tool_manager_get_active (gimp);

        if (bevent->button == 1                                &&
            active_tool                                        &&
            gimp_tool_control_is_active (active_tool->control) &&
            gimp_tool_control_get_wants_double_click (active_tool->control))
          {
            tool_manager_button_press_active (gimp,
                                              &image_coords,
                                              time, state,
                                              GIMP_BUTTON_PRESS_DOUBLE,
                                              display);
          }

607 608
        /*  don't update the cursor again on double click  */
        return TRUE;
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
      }
      break;

    case GDK_3BUTTON_PRESS:
      {
        GdkEventButton *bevent = (GdkEventButton *) event;
        GimpTool       *active_tool;

        if (gimp->busy)
          return TRUE;

        active_tool = tool_manager_get_active (gimp);

        if (bevent->button == 1                                &&
            active_tool                                        &&
            gimp_tool_control_is_active (active_tool->control) &&
            gimp_tool_control_get_wants_triple_click (active_tool->control))
          {
            tool_manager_button_press_active (gimp,
                                              &image_coords,
                                              time, state,
                                              GIMP_BUTTON_PRESS_TRIPLE,
                                              display);
          }

634 635
        /*  don't update the cursor again on triple click  */
        return TRUE;
636 637 638 639 640 641 642 643 644 645
      }
      break;

    case GDK_BUTTON_RELEASE:
      {
        GdkEventButton *bevent = (GdkEventButton *) event;
        GimpTool       *active_tool;

        gimp_display_shell_autoscroll_stop (shell);

646 647 648 649 650 651
        if (bevent->button == 1 && shell->button1_release_pending)
          {
            gimp_display_shell_released (shell, event, NULL);
            return TRUE;
          }

652 653 654 655 656
        if (gimp->busy)
          return TRUE;

        active_tool = tool_manager_get_active (gimp);

657
        state &= ~gimp_display_shell_button_to_state (bevent->button);
658

659 660
        if (bevent->button == 1)
          {
661 662 663 664 665
            /*  If we don't have a grab, this is a release paired with
             *  a button press we intentionally ignored because we had
             *  a grab on another device at the time of the press
             */
            if (! shell->grab_pointer || shell->scrolling)
666 667
              return TRUE;

668
            if (! shell->space_release_pending)
669 670 671 672 673 674
              gimp_display_shell_keyboard_ungrab (shell, event);

            if (active_tool &&
                (! gimp_image_is_empty (image) ||
                 gimp_tool_control_get_handle_empty_image (active_tool->control)))
              {
675
                gimp_motion_buffer_end_stroke (shell->motion_buffer);
676

Ell's avatar
Ell committed
677 678 679
                gdk_window_set_event_compression (
                  gtk_widget_get_window (canvas), TRUE);

680 681 682 683 684 685 686 687 688 689 690 691
                if (gimp_tool_control_is_active (active_tool->control))
                  {
                    tool_manager_button_release_active (gimp,
                                                        &image_coords,
                                                        time, state,
                                                        display);
                  }
              }

            /*  update the tool's modifier state because it didn't get
             *  key events while BUTTON1 was down
             */
692 693 694 695 696 697
            if (gtk_widget_has_focus (canvas))
              gimp_display_shell_update_focus (shell, TRUE,
                                               &image_coords, state);
            else
              gimp_display_shell_update_focus (shell, FALSE,
                                               &image_coords, 0);
698

699
            gimp_display_shell_pointer_ungrab (shell, event);
700 701 702
          }
        else if (bevent->button == 2)
          {
703
            if (shell->scrolling)
704
              gimp_display_shell_stop_scrolling (shell, event);
705 706 707 708 709 710 711 712
          }
        else if (bevent->button == 3)
          {
            /* nop */
          }
        else
          {
            GdkEventButton *bevent = (GdkEventButton *) event;
713
            GimpController *mouse  = gimp_controllers_get_mouse (gimp);
714

715
            if (!(shell->scrolling || shell->grab_pointer) &&
716 717
                mouse && gimp_controller_mouse_button (GIMP_CONTROLLER_MOUSE (mouse),
                                                       bevent))
718 719 720
              {
                return TRUE;
              }
721 722 723 724 725 726 727 728
          }

        return_val = TRUE;
      }
      break;

    case GDK_SCROLL:
      {
729 730
        GdkEventScroll *sevent = (GdkEventScroll *) event;
        GimpController *wheel  = gimp_controllers_get_wheel (gimp);
731

732 733 734
        if (! wheel ||
            ! gimp_controller_wheel_scroll (GIMP_CONTROLLER_WHEEL (wheel),
                                            sevent))
735
          {
736
            if (state & gimp_get_toggle_behavior_mask ())
737
              {
738 739
                gdouble delta;

740
                switch (sevent->direction)
741 742 743 744 745
                  {
                  case GDK_SCROLL_UP:
                    gimp_display_shell_scale (shell,
                                              GIMP_ZOOM_IN,
                                              0.0,
746
                                              GIMP_ZOOM_FOCUS_POINTER);
747 748 749 750 751 752
                    break;

                  case GDK_SCROLL_DOWN:
                    gimp_display_shell_scale (shell,
                                              GIMP_ZOOM_OUT,
                                              0.0,
753
                                              GIMP_ZOOM_FOCUS_POINTER);
754 755 756 757 758 759 760 761
                    break;

                  case GDK_SCROLL_SMOOTH:
                    gdk_event_get_scroll_deltas (event, NULL, &delta);
                    gimp_display_shell_scale (shell,
                                              GIMP_ZOOM_SMOOTH,
                                              delta,
                                              GIMP_ZOOM_FOCUS_POINTER);
762 763 764 765 766
                    break;

                  default:
                    break;
                  }
767
              }
768
            else
769
              {
770 771
                gdouble value_x;
                gdouble value_y;
772

773 774 775 776
                gimp_scroll_adjustment_values (sevent,
                                               shell->hsbdata,
                                               shell->vsbdata,
                                               &value_x, &value_y);
777

778 779
                gtk_adjustment_set_value (shell->hsbdata, value_x);
                gtk_adjustment_set_value (shell->vsbdata, value_y);
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
              }
          }

        gimp_display_shell_untransform_event_coords (shell,
                                                     &display_coords,
                                                     &image_coords,
                                                     &update_sw_cursor);

        tool_manager_oper_update_active (gimp,
                                         &image_coords, state,
                                         shell->proximity,
                                         display);

        return_val = TRUE;
      }
      break;

    case GDK_MOTION_NOTIFY:
      {
Ell's avatar
Ell committed
799
        GdkEventMotion *mevent = (GdkEventMotion *) event;
800 801 802 803

        if (gimp->busy)
          return TRUE;

804 805 806 807 808
        /*  call proximity_in() here because the pointer might already
         *  be in proximity when the canvas starts to receive events,
         *  like when a new image has been created into an empty
         *  display
         */
809 810
        gimp_display_shell_proximity_in (shell);
        update_sw_cursor = TRUE;
811 812 813

        if (shell->scrolling)
          {
Ell's avatar
Ell committed
814 815
            gimp_display_shell_handle_scrolling (shell,
                                                 state, mevent->x, mevent->y);
816 817 818
          }
        else if (state & GDK_BUTTON1_MASK)
          {
Ell's avatar
Ell committed
819 820 821 822 823 824 825
            GimpTool       *active_tool;
            GimpMotionMode  motion_mode;

            active_tool = tool_manager_get_active (gimp);
            motion_mode = gimp_tool_control_get_motion_mode (
                            active_tool->control);

826 827 828 829 830 831 832
            if (active_tool                                        &&
                gimp_tool_control_is_active (active_tool->control) &&
                (! gimp_image_is_empty (image) ||
                 gimp_tool_control_get_handle_empty_image (active_tool->control)))
              {
                GdkTimeCoord **history_events;
                gint           n_history_events;
833
                guint32        last_motion_time;
834 835 836 837 838 839 840 841 842 843 844 845 846

                /*  if the first mouse button is down, check for automatic
                 *  scrolling...
                 */
                if ((mevent->x < 0                 ||
                     mevent->y < 0                 ||
                     mevent->x > shell->disp_width ||
                     mevent->y > shell->disp_height) &&
                    ! gimp_tool_control_get_scroll_lock (active_tool->control))
                  {
                    gimp_display_shell_autoscroll_start (shell, state, mevent);
                  }

847 848 849 850
                /* gdk_device_get_history() has several quirks. First
                 * is that events with borderline timestamps at both
                 * ends are included. Because of that we need to add 1
                 * to lower border. The second is due to poor X event
851 852
                 * resolution. We need to do -1 to ensure that the
                 * amount of events between timestamps is final or
853
                 * risk losing some.
854
                 */
855 856 857
                last_motion_time =
                  gimp_motion_buffer_get_last_motion_time (shell->motion_buffer);

858
                if (motion_mode == GIMP_MOTION_MODE_EXACT     &&
859 860
                    shell->display->config->use_event_history &&
                    gdk_device_get_history (mevent->device, mevent->window,
861
                                            last_motion_time + 1,
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
                                            mevent->time - 1,
                                            &history_events,
                                            &n_history_events))
                  {
                    GimpDeviceInfo *device;
                    gint            i;

                    device = gimp_device_info_get_by_device (mevent->device);

                    for (i = 0; i < n_history_events; i++)
                      {
                        gimp_device_info_get_time_coords (device,
                                                          history_events[i],
                                                          &display_coords);

                        gimp_display_shell_untransform_event_coords (shell,
                                                                     &display_coords,
                                                                     &image_coords,
                                                                     NULL);

                        /* Early removal of useless events saves CPU time.
                         */
884 885 886 887
                        if (gimp_motion_buffer_motion_event (shell->motion_buffer,
                                                             &image_coords,
                                                             history_events[i]->time,
                                                             TRUE))
888
                          {
889
                            gimp_motion_buffer_request_stroke (shell->motion_buffer,
890 891
                                                               state,
                                                               history_events[i]->time);
892 893 894 895 896 897 898
                          }
                      }

                    gdk_device_free_history (history_events, n_history_events);
                  }
                else
                  {
899 900
                    gboolean event_fill = (motion_mode == GIMP_MOTION_MODE_EXACT);

901 902
                    /* Early removal of useless events saves CPU time.
                     */
903 904 905 906
                    if (gimp_motion_buffer_motion_event (shell->motion_buffer,
                                                         &image_coords,
                                                         time,
                                                         event_fill))
907
                      {
908
                        gimp_motion_buffer_request_stroke (shell->motion_buffer,
909 910
                                                           state,
                                                           time);
911 912 913 914 915 916 917 918 919
                      }
                  }
              }
          }

        if (! (state &
               (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK | GDK_BUTTON3_MASK)))
          {
            /* Early removal of useless events saves CPU time.
920
             * Pass event_fill = FALSE since we are only hovering.
921
             */
922 923 924 925
            if (gimp_motion_buffer_motion_event (shell->motion_buffer,
                                                 &image_coords,
                                                 time,
                                                 FALSE))
926
              {
927
                gimp_motion_buffer_request_hover (shell->motion_buffer,
928 929
                                                  state,
                                                  shell->proximity);
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
              }
          }

        return_val = TRUE;
      }
      break;

    case GDK_KEY_PRESS:
      {
        GdkEventKey *kevent = (GdkEventKey *) event;
        GimpTool    *active_tool;

        active_tool = tool_manager_get_active (gimp);

        if (state & GDK_BUTTON1_MASK)
          {
946 947 948 949 950 951 952 953
            if (kevent->keyval == GDK_KEY_Alt_L     ||
                kevent->keyval == GDK_KEY_Alt_R     ||
                kevent->keyval == GDK_KEY_Shift_L   ||
                kevent->keyval == GDK_KEY_Shift_R   ||
                kevent->keyval == GDK_KEY_Control_L ||
                kevent->keyval == GDK_KEY_Control_R ||
                kevent->keyval == GDK_KEY_Meta_L    ||
                kevent->keyval == GDK_KEY_Meta_R)
954
              {
955
                GdkModifierType key;
956

957 958
                key = gimp_display_shell_key_to_state (kevent->keyval);
                state |= key;
959

960 961 962 963 964 965 966
                if (active_tool                                        &&
                    gimp_tool_control_is_active (active_tool->control) &&
                    ! gimp_image_is_empty (image))
                  {
                    tool_manager_active_modifier_state_active (gimp, state,
                                                               display);
                  }
967 968 969 970
              }
          }
        else
          {
971 972
            gboolean arrow_key = FALSE;

973 974 975 976 977 978 979 980 981 982 983 984 985 986
            tool_manager_focus_display_active (gimp, display);

            if (gimp_tool_control_get_wants_all_key_events (active_tool->control))
              {
                if (tool_manager_key_press_active (gimp, kevent, display))
                  {
                    /* FIXME: need to do some of the stuff below, like
                     * calling oper_update()
                     */

                    return TRUE;
                  }
              }

987 988 989 990 991 992 993 994 995 996
            if (! gtk_widget_has_focus (shell->canvas))
              {
                /*  The event was in an overlay widget and not handled
                 *  there, make sure the overlay widgets are keyboard
                 *  navigatable by letting the generic widget handlers
                 *  deal with the event.
                 */
                return FALSE;
              }

997
            if (gimp_display_shell_key_to_state (kevent->keyval) == GDK_MOD1_MASK)
998
              /* Make sure the picked layer is reset. */
999 1000
              shell->picked_layer = NULL;

1001 1002
            switch (kevent->keyval)
              {
1003 1004 1005 1006 1007 1008
              case GDK_KEY_Left:
              case GDK_KEY_Right:
              case GDK_KEY_Up:
              case GDK_KEY_Down:
                arrow_key = TRUE;

1009 1010 1011 1012 1013
              case GDK_KEY_Return:
              case GDK_KEY_KP_Enter:
              case GDK_KEY_ISO_Enter:
              case GDK_KEY_BackSpace:
              case GDK_KEY_Escape:
1014 1015 1016 1017 1018 1019
                if (! gimp_image_is_empty (image))
                  return_val = tool_manager_key_press_active (gimp,
                                                              kevent,
                                                              display);

                if (! return_val)
1020 1021 1022 1023
                  {
                    GimpController *keyboard = gimp_controllers_get_keyboard (gimp);

                    if (keyboard)
1024 1025 1026
                      return_val =
                        gimp_controller_keyboard_key_press (GIMP_CONTROLLER_KEYBOARD (keyboard),
                                                            kevent);
1027
                  }
1028 1029 1030 1031

                /* always swallow arrow keys, we don't want focus keynav */
                if (! return_val)
                  return_val = arrow_key;
1032 1033 1034 1035
                break;

              case GDK_KEY_space:
              case GDK_KEY_KP_Space:
1036 1037 1038 1039
                if (shell->button1_release_pending)
                  shell->space_release_pending = TRUE;
                else
                  gimp_display_shell_space_pressed (shell, event);
1040 1041 1042 1043
                return_val = TRUE;
                break;

              case GDK_KEY_Tab:
1044
              case GDK_KEY_KP_Tab:
1045
              case GDK_KEY_ISO_Left_Tab:
1046 1047
                gimp_display_shell_tab_pressed (shell, kevent);
                return_val = TRUE;
1048 1049 1050 1051 1052 1053
                break;

                /*  Update the state based on modifiers being pressed  */
              case GDK_KEY_Alt_L:     case GDK_KEY_Alt_R:
              case GDK_KEY_Shift_L:   case GDK_KEY_Shift_R:
              case GDK_KEY_Control_L: case GDK_KEY_Control_R:
1054
              case GDK_KEY_Meta_L:    case GDK_KEY_Meta_R:
1055 1056 1057 1058 1059
                {
                  GdkModifierType key;

                  key = gimp_display_shell_key_to_state (kevent->keyval);
                  state |= key;
1060

1061 1062 1063 1064
                  if (! gimp_image_is_empty (image))
                    tool_manager_modifier_state_active (gimp, state, display);
                }
                break;
1065 1066
              }

1067 1068 1069 1070
            tool_manager_oper_update_active (gimp,
                                             &image_coords, state,
                                             shell->proximity,
                                             display);
1071 1072 1073 1074
          }
      }
      break;

1075
    case GDK_KEY_RELEASE:
1076 1077 1078 1079 1080 1081
      {
        GdkEventKey *kevent = (GdkEventKey *) event;
        GimpTool    *active_tool;

        active_tool = tool_manager_get_active (gimp);

1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
        if (gimp_display_shell_key_to_state (kevent->keyval) == GDK_MOD1_MASK &&
            shell->picked_layer)
          {
            GimpStatusbar *statusbar;

            statusbar = gimp_display_shell_get_statusbar (shell);
            gimp_statusbar_pop_temp (statusbar);

            shell->picked_layer = NULL;
          }

1093 1094 1095 1096
        if ((state & GDK_BUTTON1_MASK)      &&
            (! shell->space_release_pending ||
             (kevent->keyval != GDK_KEY_space &&
              kevent->keyval != GDK_KEY_KP_Space)))
1097
          {
1098 1099 1100 1101 1102 1103 1104 1105
            if (kevent->keyval == GDK_KEY_Alt_L     ||
                kevent->keyval == GDK_KEY_Alt_R     ||
                kevent->keyval == GDK_KEY_Shift_L   ||
                kevent->keyval == GDK_KEY_Shift_R   ||
                kevent->keyval == GDK_KEY_Control_L ||
                kevent->keyval == GDK_KEY_Control_R ||
                kevent->keyval == GDK_KEY_Meta_L    ||
                kevent->keyval == GDK_KEY_Meta_R)
1106
              {
1107
                GdkModifierType key;
1108

1109 1110
                key = gimp_display_shell_key_to_state (kevent->keyval);
                state &= ~key;
1111

1112 1113 1114 1115 1116 1117 1118
                if (active_tool                                        &&
                    gimp_tool_control_is_active (active_tool->control) &&
                    ! gimp_image_is_empty (image))
                  {
                    tool_manager_active_modifier_state_active (gimp, state,
                                                               display);
                  }
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
              }
          }
        else
          {
            tool_manager_focus_display_active (gimp, display);

            if (gimp_tool_control_get_wants_all_key_events (active_tool->control))
              {
                if (tool_manager_key_release_active (gimp, kevent, display))
                  {
                    /* FIXME: need to do some of the stuff below, like
                     * calling oper_update()
                     */

                    return TRUE;
                  }
              }

1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
            if (! gtk_widget_has_focus (shell->canvas))
              {
                /*  The event was in an overlay widget and not handled
                 *  there, make sure the overlay widgets are keyboard
                 *  navigatable by letting the generic widget handlers
                 *  deal with the event.
                 */
                return FALSE;
              }

1147 1148 1149 1150
            switch (kevent->keyval)
              {
              case GDK_KEY_space:
              case GDK_KEY_KP_Space:
1151 1152 1153 1154 1155 1156 1157
                if ((state & GDK_BUTTON1_MASK))
                  {
                    shell->button1_release_pending = TRUE;
                    shell->space_release_pending   = FALSE;
                    /* We need to ungrab the pointer in order to catch
                     * button release events.
                     */
1158
                    if (shell->grab_pointer)
1159 1160 1161 1162 1163 1164
                      gimp_display_shell_pointer_ungrab (shell, event);
                  }
                else
                  {
                    gimp_display_shell_released (shell, event, NULL);
                  }
1165 1166 1167 1168 1169 1170 1171
                return_val = TRUE;
                break;

                /*  Update the state based on modifiers being pressed  */
              case GDK_KEY_Alt_L:     case GDK_KEY_Alt_R:
              case GDK_KEY_Shift_L:   case GDK_KEY_Shift_R:
              case GDK_KEY_Control_L: case GDK_KEY_Control_R:
1172
              case GDK_KEY_Meta_L:    case GDK_KEY_Meta_R:
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
                {
                  GdkModifierType key;

                  key = gimp_display_shell_key_to_state (kevent->keyval);
                  state &= ~key;

                  /*  For all modifier keys: call the tools
                   *  modifier_state *and* oper_update method so tools
                   *  can choose if they are interested in the press
                   *  itself or only in the resulting state
                   */
                  if (! gimp_image_is_empty (image))
                    tool_manager_modifier_state_active (gimp, state, display);
                }
                break;
              }

            tool_manager_oper_update_active (gimp,
                                             &image_coords, state,
                                             shell->proximity,
                                             display);
          }
      }
      break;

    default:
      break;
    }

  /*  if we reached this point in gimp_busy mode, return now  */
  if (gimp->busy)
    return return_val;

  /*  cursor update   */
  gimp_display_shell_update_cursor (shell, &display_coords, &image_coords,
                                    state, update_sw_cursor);

  return return_val;
}

Ell's avatar
Ell committed
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 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 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
void
gimp_display_shell_canvas_grab_notify (GtkWidget        *canvas,
                                       gboolean          was_grabbed,
                                       GimpDisplayShell *shell)
{
  GimpDisplay *display;
  GimpImage   *image;
  Gimp        *gimp;

  /*  are we in destruction?  */
  if (! shell->display || ! gimp_display_get_shell (shell->display))
    return;

  display = shell->display;
  gimp    = gimp_display_get_gimp (display);
  image   = gimp_display_get_image (display);

  if (! image)
    return;

  GIMP_LOG (TOOL_EVENTS, "grab_notify (display %p): was_grabbed = %s",
            display, was_grabbed ? "TRUE" : "FALSE");

  if (! was_grabbed)
    {
      if (! gimp_image_is_empty (image))
        {
          GimpTool *active_tool = tool_manager_get_active (gimp);

          if (active_tool && active_tool->focus_display == display)
            {
              tool_manager_modifier_state_active (gimp, 0, display);
            }
        }
    }
}

void
gimp_display_shell_buffer_stroke (GimpMotionBuffer *buffer,
                                  const GimpCoords *coords,
                                  guint32           time,
                                  GdkModifierType   state,
                                  GimpDisplayShell *shell)
{
  GimpDisplay *display = shell->display;
  Gimp        *gimp    = gimp_display_get_gimp (display);
  GimpTool    *active_tool;

  active_tool = tool_manager_get_active (gimp);

  if (active_tool &&
      gimp_tool_control_is_active (active_tool->control))
    {
      tool_manager_motion_active (gimp,
                                  coords, time, state,
                                  display);
    }
}

void
gimp_display_shell_buffer_hover (GimpMotionBuffer *buffer,
                                 const GimpCoords *coords,
                                 GdkModifierType   state,
                                 gboolean          proximity,
                                 GimpDisplayShell *shell)
{
  GimpDisplay *display = shell->display;
  Gimp        *gimp    = gimp_display_get_gimp (display);
  GimpTool    *active_tool;

  active_tool = tool_manager_get_active (gimp);

  if (active_tool &&
      ! gimp_tool_control_is_active (active_tool->control))
    {
      tool_manager_oper_update_active (gimp,
                                       coords, state, proximity,
                                       display);
    }
}

static gboolean
gimp_display_shell_ruler_button_press (GtkWidget           *widget,
                                       GdkEventButton      *event,
                                       GimpDisplayShell    *shell,
                                       GimpOrientationType  orientation)
{
  GimpDisplay *display = shell->display;

  if (display->gimp->busy)
    return TRUE;

  if (! gimp_display_get_image (display))
    return TRUE;

  if (event->type == GDK_BUTTON_PRESS && event->button == 1)
    {
      GimpTool *active_tool = tool_manager_get_active (display->gimp);

      if (active_tool)
        {
          gimp_display_shell_update_focus (shell, TRUE,
                                           NULL, event->state);

          if (gimp_display_shell_pointer_grab (shell, (GdkEvent *) event,
                                               GDK_POINTER_MOTION_MASK |
                                               GDK_BUTTON_RELEASE_MASK))
            {
              if (gimp_display_shell_keyboard_grab (shell, (GdkEvent *) event))
                {
                  if (event->state & gimp_get_toggle_behavior_mask ())
                    {
                      gimp_sample_point_tool_start_new (active_tool, display);
                    }
                  else
                    {
                      gimp_guide_tool_start_new (active_tool, display,
                                                 orientation);
                    }

                  return TRUE;
                }
              else
                {
                  gimp_display_shell_pointer_ungrab (shell, (GdkEvent *) event);
                }
            }
        }
    }

  return FALSE;
}

gboolean
gimp_display_shell_hruler_button_press (GtkWidget        *widget,
                                        GdkEventButton   *event,
                                        GimpDisplayShell *shell)
{
  return gimp_display_shell_ruler_button_press (widget, event, shell,
                                                GIMP_ORIENTATION_HORIZONTAL);
}

gboolean
gimp_display_shell_vruler_button_press (GtkWidget        *widget,
                                        GdkEventButton   *event,
                                        GimpDisplayShell *shell)
{
  return gimp_display_shell_ruler_button_press (widget, event, shell,
                                                GIMP_ORIENTATION_VERTICAL);
}


/*  private functions  */

1367 1368 1369
static GdkModifierType
gimp_display_shell_key_to_state (gint key)
{
1370 1371
  /* FIXME: need some proper GDK API to figure this */

1372 1373 1374 1375 1376
  switch (key)
    {
    case GDK_KEY_Alt_L:
    case GDK_KEY_Alt_R:
      return GDK_MOD1_MASK;
1377

1378 1379 1380
    case GDK_KEY_Shift_L:
    case GDK_KEY_Shift_R:
      return GDK_SHIFT_MASK;
1381

1382 1383 1384
    case GDK_KEY_Control_L:
    case GDK_KEY_Control_R:
      return GDK_CONTROL_MASK;
1385

1386 1387 1388 1389 1390
#ifdef GDK_WINDOWING_QUARTZ
    case GDK_KEY_Meta_L:
    case GDK_KEY_Meta_R:
      return GDK_MOD2_MASK;
#endif
1391

1392 1393 1394 1395 1396
    default:
      return 0;
    }
}

1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
static GdkModifierType
gimp_display_shell_button_to_state (gint button)
{
  if (button == 1)
    return GDK_BUTTON1_MASK;
  else if (button == 2)
    return GDK_BUTTON2_MASK;
  else if (button == 3)
    return GDK_BUTTON3_MASK;

  return 0;
}

1410 1411 1412
static void
gimp_display_shell_proximity_in (GimpDisplayShell *shell)
{
1413 1414 1415
  if (! shell->proximity)
    {
      shell->proximity = TRUE;
1416

1417 1418
      gimp_display_shell_check_device_cursor (shell);
    }
1419 1420 1421 1422 1423
}

static void
gimp_display_shell_proximity_out (GimpDisplayShell *shell)
{
1424 1425 1426
  if (shell->proximity)
    {
      shell->proximity = FALSE;
1427

1428 1429
      gimp_display_shell_clear_software_cursor (shell);
    }
1430 1431
}

1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
static gboolean
gimp_display_shell_check_device (GimpDisplayShell *shell,
                                 GdkEvent         *event,
                                 gboolean         *device_changed)
{
  Gimp      *gimp = gimp_display_get_gimp (shell->display);
  GdkDevice *device;
  GdkDevice *grab_device;

  /*  Find out what device the event occurred upon  */
  device = gimp_devices_get_from_event (gimp, event, &grab_device);

  if (device)
    {
      /*  While we have a grab, ignore all events from all other devices
       *  of the same type
       */
      if (event->type == GDK_KEY_PRESS   ||
          event->type == GDK_KEY_RELEASE ||
          event->type == GDK_FOCUS_CHANGE)
        {
          if ((shell->grab_keyboard && (shell->grab_keyboard != grab_device)) ||
              (shell->grab_keyboard_source && (shell->grab_keyboard_source != device)))
            {
              GIMP_LOG (TOOL_EVENTS,
                        "ignoring key event from '%s' while waiting for event from '%s'\n",
                        gdk_device_get_name (device),
                        gdk_device_get_name (shell->grab_keyboard_source));
              return TRUE;
            }
        }
      else
        {
          if ((shell->grab_pointer && (shell->grab_pointer != grab_device)) ||
              (shell->grab_pointer_source && (shell->grab_pointer_source != device)))
            {
              GIMP_LOG (TOOL_EVENTS,
                        "ignoring pointer event from '%s' while waiting for event from '%s'\n",
                        gdk_device_get_name (device),
                        gdk_device_get_name (shell->grab_pointer_source));
              return TRUE;
            }
        }

      if (! gimp->busy && gimp_devices_check_change (gimp, device))
        {
          gimp_display_shell_check_device_cursor (shell);

          *device_changed = TRUE;
        }
    }

  return FALSE;
}

1487 1488 1489
static void
gimp_display_shell_check_device_cursor (GimpDisplayShell *shell)
{
1490 1491 1492 1493
  GimpDeviceManager *manager;
  GimpDeviceInfo    *current_device;

  manager = gimp_devices_get_manager (shell->display->gimp);
1494

1495
  current_device = gimp_device_manager_get_current_device (manager);
1496 1497 1498 1499 1500 1501

  shell->draw_cursor = ! gimp_device_info_has_cursor (current_device);
}

static void
gimp_display_shell_start_scrolling (GimpDisplayShell *shell,
1502
                                    const GdkEvent   *event,