Creating Popup Menus

In addition to menus that reside inside a menu bar, there are also popup menus that can appear anywhere in the application. Normally, these menus are used as context-sensitive menus. Listing 9.3 shows code that creates a popup menu.

Listing 9.3. Creating Popup Menu
<canvas>
    <view name="main" width="$once{parent.width}"
            height="$once{parent.height}" clickable="true">
        <view name="dummy">
            <menu x="100" name="topmenu" attach="left">
                <menuitem text="Stop All Applications"/>
                <menuitem text="Icon Manager">
                    <menu>
                        <menuitem text="Arrange Icons"/>
                        <menuitem text="Default"/>
                    </menu>
                </menuitem>
            </menu>
        </view>
        <method event="onmousedown">
            this.dummy.x = this.getMouse("x");
            this.dummy.y = this.getMouse("y");
            this.dummy.topmenu.setOpen(true);
        </method>
    </view>
</canvas>

You cannot add a menu directly to the canvas, so we cover the canvas with an invisible view that has another view (named dummy) that is also invisible. Every time you click on the main view, the onmousedown event handler is invoked and it moves the dummy view to the click position and then open the menu.

Use this URL to compile and run the application:

http://localhost:8080/lps-4.0.x/app09/menuTest3.lzx

The application is shown in Figure 9.4. Click anywhere in the application to display the popup menu.

Figure 9.4. Popup menus


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset