« Running a method every x seconds | Main | Duplicate a solution in 10 minutes »

March 25, 2007

[Tip] Contextual menus

by David Workman
Data Mosaic

Picture_3 Contextual menus are simple using the popupmenu plugin. Unfortunately, Servoy does not detect the right mouse click so we have to use a modifier key to trigger the menu. Mac users are used to the control key as the trigger due to years of being told more than one button on a mouse was too complicated for them so that is what the following code uses.

The popupmenu plugin needs code attached to an object on a form. The menu will then appear just below that object. This means that any object on a form can have a pop up menu.

All you have to do to make the pop up menu a "contextual" menu is trap for the control key and only trigger the menu when it is pressed.

You can even make this work in list view and have a menu show up on the specific record control-clicked. To do this, create an invisible button that spans the row in list view and attach your menu code to this button.

Trigger method

//set selected record
globals.EMP_row_selected = id_employee;

//contextual menu
var pressed = application.getLastKeyModifiers()

//if control key
if (pressed == 18) {

    var menu = new Array(
        plugins.popupmenu.createMenuItem('New record', LIST_action_control ),
        plugins.popupmenu.createMenuItem('Delete record...', LIST_action_control)
    )
    
    var x = 0
    while (menu[x])
    {
        menu[x].setMethodArguments(x)
        x ++
    }
    
    var elem = elements[application.getMethodTriggerElementName()]
    if (elem != null)
    {
        plugins.popupmenu.showPopupMenu(elem, menu);
    }

}

LIST_action_control method

switch (arguments[0]) {

    case 0:    
            controller.newRecord(true)
            controller.saveData();
            
            break
            
    case 1:
            var dialogInput = plugins.dialogs.showWarningDialog(
                    'Warning!',
                    'Are you sure you want to delete this record?',
                    'Yes',
                    'No')        
    
            if (dialogInput == 'Yes') {
                controller.deleteRecord();
            }    
            
            break            
}

| Posted by David Workman on March 25, 2007 at 12:44 PM in Tips | Permalink

Comments

Nice tip David.

Thought you should know the menubar plug-in supports

- popup
- right-click popup
- mouseover popup

:)

Posted by: Marcel Trapman | Apr 6, 2007 4:21:43 PM

Post a comment