Friday, July 30, 2010

why doesn't this right-click capture work in IE?

I'm trying to capture right-clicks on a widget, to popup my own
context menu instead of the browser's. I've researched it a bit and
pieced together bits and I've got it working in Chrome and FF but not
IE. In IE it doesn't display the default browser context menu, but it
doesn't display my popup menu either. I'm just getting into GWT so I'm
assuming I'm not doing something right with the right kinds of
handlers or events. I'm also using the gwt-graphics module, that's
where the Rectangle class that I'm extending comes from, in case
that's relevant.

Here's my code:

public class RectangleRightClickable extends Rectangle {

public RectangleRightClickable(int x, int y, int width, int height) {
super(x, y, width, height);
sinkEvents(Event.ONCONTEXTMENU);
}

public void onBrowserEvent(Event event) {
GWT.log("onBrowserEvent");
event.stopPropagation();
event.preventDefault();
GWT.log("event type : " + DOM.eventGetType(event));
switch(DOM.eventGetType(event)) {
case Event.ONCONTEXTMENU:
if (DOM.eventGetButton(event) == Event.BUTTON_RIGHT) {
GWT.log("Event.BUTTON_RIGHT", null);
showMenu();
}
break;
default:
GWT.log(event.toString());
break;
}
}

protected void showMenu() {
final RectangleRightClickable parent = this;
final PopupMenu popMenu = new PopupMenu();
popMenu.addMenuItem(new Label("Add thing"));
popMenu.setPopupPositionAndShow(new PopupPanel.PositionCallback()
{
public void setPosition(int offsetWidth, int offsetHeight) {
int left = parent.getX() + parent.getWidth();
int top = parent.getY() + parent.getWidth();
popMenu.setPopupPosition(left, top);
}
});

}
}

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment