Specificity of selection for submenus with PopupContextualMenu

I have run into some behavior that I don't know how to resolve (Igor 8, Mac).  I have an interface that uses PopupContextualMenu, and the menu has a submenu of a list of waves.  I want to add a second submenu that offers a different operation but on the same list of waves.  The problem is that v_value and s_selection seem to return values referring to their individual submenus, and do not report which item in the parent menu was selected.

I've written a simplified example to illustrate the basic problem:

menu "Tester", contextualmenu
    submenu "action 1"
        "wave1"
        "wave2"
    end
    submenu "action 2"
        "wave1"
        "wave2"
    end
end

function testMenu ()
    PopupContextualMenu /N "Tester"
    print v_kind, v_flag, s_selection
end

If you enter "testmenu ()" on the command line, it will give you the popup menu.  If you select action1->wave1, you get the same v_kind, v_flag, and s_selection as if you had selected action2->wave1.  So, how can I distinguish the two?  Any suggestions for a work-around?  Is renaming the items in my submenus the best choice?   I am grateful for suggestions.

-Matthew

menu "Tester", contextualmenu
    submenu "action 1"
        "wave1", SaveMenu("action 1")
        "wave2", SaveMenu("action 1")
    end
    submenu "action 2"
        "wave1", SaveMenu("action 2")
        "wave2", SaveMenu("action 2")
    end
end

function testMenu ()
    PopupContextualMenu /N "Tester"
    SVAR strMenu
    print v_kind, v_flag, s_selection, strMenu  
end

function SaveMenu(strSubmenu)
    string strSubmenu
   
    string /G strMenu=strSubmenu
    return 0
end

 

Here's a workaround similar to Tony's, but it doesn't require a global variable

menu "Tester", contextualmenu
    submenu "action 1"
        "wave1",menuAction("action 1")
        "wave2",menuAction("action 1")
    end
    submenu "action 2"
        "wave1",menuAction("action 2")
        "wave2",menuAction("action 2")
    end
end

function menuAction (menuName)
    String menuName
   
    getlastUserMenuInfo
    print "selection index=",v_value,"wave name=",s_value,"menuName=",menuName
end

function testMenu ()
    PopupContextualMenu /N "Tester"
end

 

Actually, you don't even need the extra function if you take the global route:

menu "Tester", contextualmenu
    submenu "action 1"
        "wave1", string /G strMenu="action 1"
        "wave2", string /G strMenu="action 1"
    end
    submenu "action 2"
        "wave1", string /G strMenu="action 2"
        "wave2", string /G strMenu="action 2"
    end
end

function testMenu()
    PopupContextualMenu /N "Tester"
    SVAR strMenu
    print v_kind, v_flag, s_selection, strMenu
    killStrings/Z strMenu
end

The advantage of using global string is that you don't transfer execution to a different, isolated function. The disadvantage is that it uses a global string.

That's a nice trick.  I was a bit worried if it would work for my actual problem.  The submenus are dynamic, lists of waves returned by a string function.  I think that function calls can't be attached to individual menu items specified using semicolon-delimited strings ("item1;item2").  However, I can put the function call after the dynamic call.  So, the below is a better representation of my situation, and your solutions will work.  So, thanks again!

-Matthew

menu "Tester", contextualmenu, dynamic
    submenu "action 1"
        listitems(), string /g strmenu="action1"
    end
    submenu "action 2"
        listitems(), string /g strmenu="action2"
    end
end
function /t listitems ()
    string returnstr
    returnstr = "item1;item2"
    return (returnstr)
end
function testMenu ()
    PopupContextualMenu /N "Tester"
    SVAR strmenu
    print v_kind, v_flag, s_selection, strmenu
end

 

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More