summaryrefslogtreecommitdiffstats
path: root/awt/java/awt/MouseDispatcher.java
blob: df48f9d61f34af9c136e87a3cbd57d695a12a7bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
/**
 * @author Dmitry A. Durnev, Michael Danilov, Pavel Dolgov
 * @version $Revision$
 */
package java.awt;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.Dispatcher.MouseGrabManager;
import java.util.EventListener;

import org.apache.harmony.awt.wtk.NativeEvent;
import org.apache.harmony.awt.wtk.NativeWindow;


class MouseDispatcher {

    // Fields for synthetic mouse click events generation
    private static final int clickDelta = 5;
    private final long[] lastPressTime = new long[] {0l, 0l, 0l};
    private final Point[] lastPressPos = new Point[] {null, null, null};
    private final boolean[] buttonPressed = new boolean[] {false, false, false};
    private final int[] clickCount = new int[] {0, 0, 0};

    // Fields for mouse entered/exited support
    private Component lastUnderPointer = null;
    private final Point lastScreenPos = new Point(-1, -1);

    // Fields for redundant mouse moved/dragged filtering
    private Component lastUnderMotion = null;
    private Point lastLocalPos = new Point(-1, -1);

    private final MouseGrabManager mouseGrabManager;
    private final Toolkit toolkit;

    static Point convertPoint(Component src, int x, int y, Component dest) {
        Point srcPoint = getAbsLocation(src);
        Point destPoint = getAbsLocation(dest);

        return new Point(x + (srcPoint.x - destPoint.x),
                         y + (srcPoint.y - destPoint.y));
    }

    static Point convertPoint(Component src, Point p, Component dst) {
        return convertPoint(src, p.x, p.y, dst);
    }

    private static Point getAbsLocation(Component comp) {
        Point location = new Point(0, 0);
// BEGIN android-changed: AWT components not supported
//        for (Component parent = comp; parent != null; parent = parent.parent) {
//            Point parentPos = (parent instanceof EmbeddedWindow ?
//                               parent.getNativeWindow().getScreenPos() :
//                               parent.getLocation());
//
//            location.translate(parentPos.x, parentPos.y);
//
//            if (parent instanceof Window) {
//                break;
//            }
//        }
// END android-changed

        return location;
    }

    MouseDispatcher(MouseGrabManager mouseGrabManager,
                    Toolkit toolkit) {
        this.mouseGrabManager = mouseGrabManager;
        this.toolkit = toolkit;
    }

    Point getPointerPos() {
        return lastScreenPos;
    }

    boolean dispatch(Component src, NativeEvent event) {
        int id = event.getEventId();

        lastScreenPos.setLocation(event.getScreenPos());
        checkMouseEnterExit(event.getInputModifiers(), event.getTime());

        if (id == MouseEvent.MOUSE_WHEEL) {
// BEGIN android-changed: AWT components not supported
//            dispatchWheelEvent(src, event);
// END android-changed
        } else if ((id != MouseEvent.MOUSE_ENTERED) &&
                   (id != MouseEvent.MOUSE_EXITED)) {
            PointerInfo info = new PointerInfo(src, event.getLocalPos());

            mouseGrabManager.preprocessEvent(event);
            findEventSource(info);
            if ((id == MouseEvent.MOUSE_PRESSED) ||
                (id == MouseEvent.MOUSE_RELEASED)) {

                dispatchButtonEvent(info, event);
            } else if ((id == MouseEvent.MOUSE_MOVED) ||
                       (id == MouseEvent.MOUSE_DRAGGED)) {

                dispatchMotionEvent(info, event);
            }
        }

        return false;
    }

    private void checkMouseEnterExit(int modifiers, long when) {
// BEGIN android-changed: AWT components not supported
//        PointerInfo info = findComponentUnderPointer();
//        Component curUnderPointer =
//                propagateEvent(info, AWTEvent.MOUSE_EVENT_MASK,
//                               MouseListener.class, false).src;
//
//        if (curUnderPointer != lastUnderPointer) {
//            Point pos = info.position;
//            if ((lastUnderPointer != null) &&
//                 lastUnderPointer.isMouseExitedExpected()) {
//
//                Point exitPos = convertPoint(null, lastScreenPos.x,
//                                             lastScreenPos.y, lastUnderPointer);
//
//                postMouseEnterExit(MouseEvent.MOUSE_EXITED, modifiers, when,
//                                   exitPos.x, exitPos.y, lastUnderPointer);
//            }
//            setCursor(curUnderPointer);
//            if (curUnderPointer != null) {
//                postMouseEnterExit(MouseEvent.MOUSE_ENTERED, modifiers, when,
//                                   pos.x, pos.y, curUnderPointer);
//            }
//            lastUnderPointer = curUnderPointer;
//        }
// END android-changed
    }

    private void setCursor(Component comp) {
        if (comp == null) {
            return;
        }
        Component grabOwner = mouseGrabManager.getSyntheticGrabOwner();
        Component cursorComp = ((grabOwner != null) &&
                                 grabOwner.isShowing() ? grabOwner : comp);
        cursorComp.setCursor();
    }

    private void postMouseEnterExit(int id, int mod, long when,
                                    int x, int y, Component comp) {
        if (comp.isIndirectlyEnabled()) {
            toolkit.getSystemEventQueueImpl().postEvent(
                    new MouseEvent(comp, id, when, mod, x, y, 0, false));
            comp.setMouseExitedExpected(id == MouseEvent.MOUSE_ENTERED);
        } else {
            comp.setMouseExitedExpected(false);
        }
    }

 // BEGIN android-changed: AWT components not supported
//    private PointerInfo findComponentUnderPointer() {
//        NativeWindow nativeWindow = toolkit.getWindowFactory().
//        getWindowFromPoint(lastScreenPos);
//
//        if (nativeWindow != null) {
//            Component comp = toolkit.getComponentById(nativeWindow.getId());
//
//            if (comp != null) {
//                Window window = comp.getWindowAncestor();
//                Point pointerPos = convertPoint(null, lastScreenPos.x,
//                                                lastScreenPos.y, window);
//
//                if (window.getClient().contains(pointerPos)) {
//                    PointerInfo info = new PointerInfo(window, pointerPos);
//
//                    fall2Child(info);
//
//                    return info;
//                }
//            }
//        }
//
//        return new PointerInfo(null, null);
//    }
// END android-changed
    
    private void findEventSource(PointerInfo info) {
        Component grabOwner = mouseGrabManager.getSyntheticGrabOwner();

        if (grabOwner != null && grabOwner.isShowing()) {
            info.position = convertPoint(info.src, info.position, grabOwner);
            info.src = grabOwner;
        } else {
            //???AWT: rise2TopLevel(info);
            //???AWT: fall2Child(info);
        }
    }

 // BEGIN android-changed: AWT components not supported
//    private void rise2TopLevel(PointerInfo info) {
//        while (!(info.src instanceof Window)) {
//            info.position.translate(info.src.x, info.src.y);
//            info.src = info.src.parent;
//        }
//    }
//
//    private void fall2Child(PointerInfo info) {
//        Insets insets = info.src.getInsets();
//
//        final Point pos = info.position;
//        final int x = pos.x;
//        final int y = pos.y;
//        if ((x >= insets.left) && (y >= insets.top) &&
//                (x < (info.src.w - insets.right)) &&
//                (y < (info.src.h - insets.bottom)))
//        {
//            Component[] children = ((Container) info.src).getComponents();
//
//            for (Component child : children) {
//                if (child.isShowing()) {
//                    if (child.contains(x - child.getX(),
//                            y - child.getY()))
//                    {
//                        info.src = child;
//                        pos.translate(-child.x, -child.y);
//
//                        if (child instanceof Container) {
//                            fall2Child(info);
//                        }
//
//                        return;
//                    }
//                }
//            }
//        }
//    }
// END android-changed

    private void dispatchButtonEvent(PointerInfo info, NativeEvent event) {
        int button = event.getMouseButton();
        long time = event.getTime();
        int id = event.getEventId();
        int index = button - 1;
        boolean clickRequired = false;

        propagateEvent(info, AWTEvent.MOUSE_EVENT_MASK,
                       MouseListener.class, false);
        if (id == MouseEvent.MOUSE_PRESSED) {
            int clickInterval = toolkit.dispatcher.clickInterval;
            mouseGrabManager.onMousePressed(info.src);
            buttonPressed[index] = true;
            clickCount[index] = (!deltaExceeded(index, info) &&
                    ((time - lastPressTime[index]) <= clickInterval)) ?
                    clickCount[index] + 1 : 1;
            lastPressTime[index] = time;
            lastPressPos[index] = info.position;
        } else {
            mouseGrabManager.onMouseReleased(info.src);
            // set cursor back on synthetic mouse grab end:
// BEGIN android-changed: AWT components not supported
//            setCursor(findComponentUnderPointer().src);
// END android-changed
            if (buttonPressed[index]) {
                buttonPressed[index] = false;
                clickRequired = !deltaExceeded(index, info);
            } else {
                clickCount[index] = 0;
            }
        }
        if (info.src.isIndirectlyEnabled()) {
            final Point pos = info.position;
            final int mod = event.getInputModifiers();
            toolkit.getSystemEventQueueImpl().postEvent(
                            new MouseEvent(info.src, id, time, mod, pos.x,
                            pos.y, clickCount[index],
                            event.getTrigger(), button));
            if (clickRequired) {
                toolkit.getSystemEventQueueImpl().postEvent(
                            new MouseEvent(info.src,
                            MouseEvent.MOUSE_CLICKED,
                            time, mod, pos.x, pos.y,
                            clickCount[index], false,
                            button));
            }
        }
    }

    private boolean deltaExceeded(int index, PointerInfo info) {
        final Point lastPos = lastPressPos[index];
        if (lastPos == null) {
            return true;
        }
        return ((Math.abs(lastPos.x - info.position.x) > clickDelta) ||
                (Math.abs(lastPos.y - info.position.y) > clickDelta));
    }

    private void dispatchMotionEvent(PointerInfo info, NativeEvent event) {
        propagateEvent(info, AWTEvent.MOUSE_MOTION_EVENT_MASK,
                       MouseMotionListener.class, false);
        final Point pos = info.position;
        if ((lastUnderMotion != info.src) ||
            !lastLocalPos.equals(pos)) {

            lastUnderMotion = info.src;
            lastLocalPos = pos;

            if (info.src.isIndirectlyEnabled()) {
                toolkit.getSystemEventQueueImpl().postEvent(
                            new MouseEvent(info.src, event.getEventId(),
                            event.getTime(),
                            event.getInputModifiers(),
                            pos.x, pos.y, 0, false));
            }
        }
    }

    MouseWheelEvent createWheelEvent(Component src, NativeEvent event,
                                     Point where) {

        Integer scrollAmountProperty =
            (Integer)toolkit.getDesktopProperty("awt.wheelScrollingSize"); //$NON-NLS-1$
        int amount = 1;
        int type = MouseWheelEvent.WHEEL_UNIT_SCROLL;

        if (scrollAmountProperty != null) {
            amount = scrollAmountProperty.intValue();
            if (amount == -1) {
                type = MouseWheelEvent.WHEEL_BLOCK_SCROLL;
                amount = 1;
            }
        }
        return new MouseWheelEvent(src, event.getEventId(),
                event.getTime(), event.getInputModifiers(),
                where.x, where.y, 0, false, type, amount,
                event.getWheelRotation());
    }

// BEGIN android-changed: AWT components not supported
//    private void dispatchWheelEvent(Component src, NativeEvent event) {
//        PointerInfo info = findComponentUnderPointer();
//
//        if (info.src == null) {
//            info.src = src;
//            info.position = event.getLocalPos();
//        }
//
//        propagateEvent(info, AWTEvent.MOUSE_WHEEL_EVENT_MASK,
//                       MouseWheelListener.class, true);
//        if ((info.src != null) && info.src.isIndirectlyEnabled()) {
//            toolkit.getSystemEventQueueImpl().postEvent(
//                    createWheelEvent(info.src, event, info.position));
//        }
//    }
// END android-changed

    private PointerInfo propagateEvent(PointerInfo info, long mask,
                                       Class<? extends EventListener> type, boolean pierceHW) {
        Component src = info.src;
        while ((src != null) &&
               (src.isLightweight() || pierceHW) &&
              !(src.isMouseEventEnabled(mask) ||
               (src.getListeners(type).length > 0))) {

            info.position.translate(src.x, src.y);
// BEGIN android-changed: AWT components not supported
//            src = src.parent;
// END android-changed
            info.src = src;
        }

        return info;
    }

// BEGIN android-changed: AWT components not supported
//    Window findWindowAt(Point p) {
//        NativeWindow nativeWindow =
//            toolkit.getWindowFactory().getWindowFromPoint(p);
//
//        Window window = null;
//        if (nativeWindow != null) {
//            Component comp = toolkit.getComponentById(nativeWindow.getId());
//
//            if (comp != null) {
//                window = comp.getWindowAncestor();
//            }
//        }
//        return window;
//    }
// END android-changed

    private class PointerInfo {

        Component src;
        Point position;

        PointerInfo(Component src, Point position) {
            this.src = src;
            this.position = position;
        }

    }

}