tags:

views:

1797

answers:

3

My gwt 1.6 application intercepts mouse clicks on hyperlinks, so when a user shift-clicks on links to "authors" they get an Edit... dialog box instead of navigating to the author's page. That's working nicely.

I'd now like to allow the user to control-click to select more than one author, but I can't figure out how to suppress the browser's default popup menu. This code handles shift-clicks correctly, but fails in the hosted browser when I control-click and half-fails in Firefox (handleCtrlClick() gets called, but I still get the browser menu):


  public void onModuleLoad() {
    Event.addNativePreviewHandler(this);
  }

  //
  // Preview events-- look for shift-clicks on paper/author links, and pops up
  // edit dialog boxes.
  // And looks for control-click to do multiple selection.
  //
  public void onPreviewNativeEvent(Event.NativePreviewEvent pe) {
    NativeEvent e = pe.getNativeEvent();
    switch (Event.getTypeInt(e.getType())) {
    case Event.ONCLICK:
      if (e.getShiftKey()) { handleShiftClick(e); }
      if (e.getCtrlKey()) { handleCtrlClick(e); }
      break;
    case Event.ONCONTEXTMENU:
      if (e.getCtrlKey()) {  // THIS IS NOT WORKING...
        e.preventDefault();
        e.stopPropagation();
      }
      break;
    }
  }

A breakpoint set inside the ONCONTEXTMENU case is never called.

A: 
gavinandresen
+1  A: 

IIRC ctrl + click is the correct way to select multiple items not ctrl + right click unless you're using a one button mouse (iMac), in that case I can't help you.

Could you provide more details?

Edit:

Why not override the contextmenu (e.g. disable it) then create your own context menu widget (perhaps based on vertical MenuBar + MenuItems) and display it only on Ctrl + RightClick?

In other words you'd create a MouseHandler somewhat like this (pseudo code):

public void onMouseDown(MouseDownEvent event) {
    Widget sender = (Widget) event.getSource();

    int button = event.getNativeButton();


    if (button == NativeEvent.BUTTON_LEFT) {
        if(event.is_ctrl_also)
        {
             // Add to selection
             selection = selection + sender;
        }
        else
        {
             // Lose selection and start a new one
             selection = sender;
        }
    }
    else if(button == NativeEvent.BUTTON_RIGHT) {
        if(event.is_ctrl_also)
        {
            // show context menu
            this.contextmenu.show();
        }
        else
        {
            // do something else
        }
    }

    return;
}

I've not encountered the bug with Ctrl-Leftclick firing a ContextMenu event, but I'm sure you could also make a workaround for Firefox only using permutations.

Hannson
The behavior I want: control-left-click : multiple select right-click : popup the browser's menu.Problem is that Firefox (at least) treats control-left-click as the SAME as right-click (it fires the oncontextmenu event).
gavinandresen
A: 

it does not work for IE 7

i was trying this.

protected native void addContextMenuEventListener(Element elem) /*-{
     elem.oncontextmenu = function(e) {
       alert("e:"+e);
       if (e && e.stopPropagation){
        e.stopPropagation();
       }else {
        e.cancelBubble=true
       }
       return false; 
     };
 }-*/;

in the alert box alert("e:"+e); gives undefined in IE 7 but it works properly in firefox.

Thanks for your help in advance

srinivas

Srinivas
BTW i am using GWT 2.0
Srinivas