1 /*
2 * Copyright 2006 Robert Hanson <iamroberthanson AT gmail.com>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.gwtwidgets.client.temp;
18
19 import com.google.gwt.user.client.DOM;
20 import com.google.gwt.user.client.Event;
21 import com.google.gwt.user.client.ui.MouseListenerCollection;
22 import com.google.gwt.user.client.ui.Widget;
23
24
25 /***
26 * Fixed (?) coords for FocusPanel. The FocusPanel that
27 * comes with GWT returns very strange coords for the
28 * content in the panel. This panel will report the coords
29 * as 0,0 for top-left of the panel, meaning the coords
30 * are relative to the panel, not to the window.
31 *
32 * @author rhanson
33 */
34 public class TMouseListenerCollection extends MouseListenerCollection
35 {
36 private static final long serialVersionUID = 7877053813928824224L;
37
38
39 public void fireMouseEvent (Widget sender, Event event)
40 {
41 int x = 0, y = 0;
42
43 /*
44 * this should not be needed, but for some reason will
45 * throw the occasional exception, usually when hovering
46 * the mouse right on the edge of the focus panel. the
47 * error in hosted mode (Windows) is:
48 * org.eclipse.swt.SWTException: Failed to change
49 * Variant type result = -2147352566.
50 * the error does not seem to be related to the values
51 * that are retrieved from getAbsoluteLeft/Top().
52 */
53 try {
54 x = eventGetOffsetX(event, DOM.getAbsoluteLeft(sender.getElement()));
55 y = eventGetOffsetY(event, DOM.getAbsoluteTop(sender.getElement()));
56 }
57 catch (Exception e) {
58 return;
59 }
60
61 switch (DOM.eventGetType(event)) {
62 case Event.ONMOUSEDOWN:
63 fireMouseDown(sender, x, y);
64 break;
65 case Event.ONMOUSEUP:
66 fireMouseUp(sender, x, y);
67 break;
68 case Event.ONMOUSEMOVE:
69 fireMouseMove(sender, x, y);
70 break;
71 default:
72 super.fireMouseEvent(sender, event);
73 }
74 }
75
76
77 private native int eventGetOffsetX (Event event, int left) /*-{
78 return event.offsetX || event.pageX - left;
79 }-*/;
80
81 private native int eventGetOffsetY (Event event, int top) /*-{
82 return event.offsetY || event.pageY - top;
83 }-*/;
84
85 }