Context Menu Guidelines

OpenLaszlo allows users to define custom context menu (i.e. right-click menu) options throughout your applications. There’s two classes involved here: LzContextMenu and LzContextMenuItem. The former represents the context menu itself, and the latter represents a single option within it.

Some capabilities and guidelines:

  • Stick to Flash Player 8 or DHTML for your runtime, if you plan to use custom context menus in your application.
  • Right-click behavior works very differently from left-click behavior: The view need not be clickable to respond to right-clicks. However all views, clickable or not, intercept the right mouse click. This means that a subview will always trap a right-click event before its parent.
  • You can specify a context menu on a per-view basis. You can also specify a generic application-level context menu.
  • There are some words you cannot seem to use as LzContextMenuItem captions. “Save” and “Delete” are two I came across. You can add a space character the name – e.g. “Save ” – to work around this.
  • Views have a .contextMenu attribute as well as getContextMenu() and setContextMenu() methods.
  • Context menus can be created on-the-fly.

The second point above is probably the most important thing to bear in mind. Consider a view containing a text field. The view has a context menu:

Notice how if you click on the text field, you don't get a custom option in the context menu, but if you carefully click in the yellow area above, below or to the right of the text, you do see a "Chop" option?

The workaround here is to place a transparent view in front of the subviews, that's the full height and width of its parent. If it's not clickable, it will allow left clicks to pass through, but right clicks will pass through. Unfortunately, because of a bug, that view can't really be transparent, but instead it needs to have a bgcolor, and a low opacity. You can see the improved effect in the application below:

Source code for simple and better context menu.

Finally, remember that the LzContextMenu object will send an onmenuopen event, when the menu is opened. You can use a delegate to trap this event, and call a method to create all the entries in a context menu, just before it is displayed.

Leave a Reply

Your email address will not be published. Required fields are marked *