ApplicationCommandmanger + Menu + submenu


#1

Hi There,
I have the following problem, maybe i’m missing something obvious but maybe writing it down helps to clear my thoughts.
I have a ApplicationCommandManager wich i add commands to, this commandmanager is used to populate my my application menu in my main window. In this application menu i also have submenus wich mostly are dynamically generated.
I have the following problem, if i click a submenu i the menuItemSelected callback is triggered but there is no way to found out wich particular item was clicked.

const PopupMenu MainWindow::getMenuForIndex (int menuIndex, const String& menuName)
{   
    PopupMenu menu;
	
	if ( menuIndex == 0) { //

		menu.addCommandItem( commandManager,  ....		
		menu.addCommandItem( commandManager,  ....		
		
		PopupMenu subMenu;
		for (int i=0; i < ...
			subMenu.addItem( ...
			subMenu.addItem( ...
		}
		
		menu.addSubMenu( T("options"), subMenu);
     }
}
void MainWindow::menuItemSelected (int menuItemID, int topLevelMenuIndex)
{ 

  ?

}

Note that i can’t use customItems for my popupmenu because we are using the native mac menu. Has someone got an idea on how to get around this problem.

Cheers
Edwin

[/code]


#2

yes, you’re only missing something fairly straightforward, you shouldn’t have too much trouble getting it solved.

When you make your menus, you are given a menu index to identify the menu you are creating. From this, you add items, and each item you add has a unique ID.

When you are responding to a menu, you have the menu id (to identify the top level menu which you returned earlier) and the item id (which identifies one of the items you added). You use these two values to determine which item has been clicked [you created these items within the same class definition, so you know which unique ID applies to each one].


#3

Hi Haydxn,
Hmmm yes, it sounds simple but then each item in the menu has to has it’s own uniqueid, and my subitem’s options are only unique within their own scope.

Imagine the switch statement this menu would force me to write.
If i could know to which item the selected subitem belongs it would make things much easier.

Maybe an extra integer or pointer to userdata which can be passed into menuItemSelected would be a nice idea.

This is my problem:

	Options

	option1
	option2
	option3	->	value1 (1)
				value2 (2)
				value3 (3)
				value4 (4)
	
	option4	->	value1 (1)
				value2 (2)
				value3 (3)
				value4 (4)

anyway thatnx for the help, i think i just need to rethink some things.

Cheers
Edwin


#4

it IS simple if you give each item its own unique ID. There is no reason to give them all ‘1, 2, 3, 4…’ in each submenu - just don’t do that and you’ll be fine.

You can choose the item ids in order to determine which item they resided in. For instance

MENU 1:
-submenu 1
–item1
–item2
–item3
submenu 2
–item21
–item22

etc…

Just stop convincing yourself that you can’t make them all unique, and you’ll be fine.