PDA

View Full Version : Carbon Menu Manager: how do I respond to a command?


chucker
2007-02-25, 06:17
I'm creating a new menu with an item inside like this:

OSStatus err;
static const EventTypeSpec menuEvents[] =
{
{ kEventClassCommand, kEventCommandProcess }
};
MenuRef testMenuRef;
MenuItemIndex howdyItem;

CreateNewMenu(202, 0, &testMenuRef);
SetMenuTitleWithCFString(testMenuRef, CFSTR("Test"));
InsertMenu(testMenuRef, 0);

AppendMenuItemTextWithCFString(testMenuRef, CFSTR("Howdy"), 0, 'Chib', &howdyItem);

This works fine. As soon as the relevant function gets called, I get a new menu named "Test", and an item named "Howdy" inside.

Now, my understanding is that I can't just assign a function to the command OSType ('Chib' in this case), but have to install a menu event handler instead which will check the command and act accordingly.

I do so like this:

err = InstallMenuEventHandler(testMenuRef, MenuHandler,
GetEventTypeCount(menuEvents), menuEvents,
GetMenuSelection(testMenuRef), NULL);

Now, MenuHandler and GetMenuSelection are defined later on, but the trouble is… they don't get called when I expect it. I have put printf() statements at their beginnings, and I don't see any of it right after selecting the menu item. And aside from that, this seems way too complicated. I would have expected a convenience function like, I dunno, RegisterFunctionWithCommand(OSType command, void function) where I can simply assign 'Chib' to a function of mine. An event handler seems like an inefficient kludge.

That said, if that's the way to go, so be it, but evidently I'm doing something wrong.

If I skip the handler registration (i.e., the line with InstallMenuEventHandler()), nothing happens. Even if I leave it in in Carbon apps, nothing happens. In Cocoa, on the other hand, I get this:
2007-02-25 12:15:36.600 someApp[16035] could not find associated NSMenu for 202 (item:1)

So it knows the menu's ID, but doesn't know what to do with it? Why not?