views:

79

answers:

2

In trying to learn the very fundamentals of menu handling. My test app's menubar has 3 menus -- namely "TestApp", "File" and "Help". I find I can remove these menus entirely, simply by calling say:

NSMenu* rootMenu = [NSApp mainMenu];
[rootMenu removeItemAtIndex:2];

However, I'd only ever want to temporarily disable them (gray them out). Is there an equally simple way to do this, please?

A: 

Get the menu item, then disable it.

Peter Hosey
Sounds like an excellent strategy ...an' how will I do that? I've already tried: [rootMenu setAutoenablesItems:NO]; [[rootMenu itemAtIndex:1] setEnabled:NO];with no effect.
Bender
Don't disable validation. That `setEnabled:` message should work just fine; where did you put that code?
Peter Hosey
It's in 'awakeFromNib', but even if I call it from the app when up and running the effect is the same.The aim is to check for a defaults file because the app needs some vital settings from the defaults dictionary and bad things would happen if the methods were run without *any* values (all menus are 'live' even when launched without a .plist file).But what I did notice is that the items under any menu disabled by the above code *DON'T WORK* even though they appear selectable. Seems more in keeping with Apple's UI guidelines if I could "gray out" any menus/items that don't do anything.
Bender
If these settings are so vital, why not load the defaults dictionary earlier? Load it in `applicationWillFinishLaunching:` or `main`.
Peter Hosey
+2  A: 

I may be misunderstanding your question, but it seems like you want to be able to gray-out the actual titles of menus that appear with the system's menu bar (Such as graying-out the "File" menu). I'm not sure if it's even possible, but it certainly goes against the Apple Human Interface Guidelines:

A menu’s title is displayed undimmed even if all of the menu’s commands are unavailable (dimmed) at the same time. Users should always be able to view a menu’s contents, whether or not they are currently available.

So, the real solution to the problem is to be able to gray-out all of the menu items within a certain menu when your application is in a certain state. To do this, implement the NSUserInterfaceValidations protocol. It only requires implementing the - (BOOL)validateUserInterfaceItem: method. Typically, when implementing this method, you simply check the selector of the user interface item being validated, and return YES if it should be enabled, or NO if it should not (which will gray-out the menu item).

CJ
Yes, you understood correctly, CJ. I *was* trying to dim out the "File" and/or "Edit" menus themselves - rather than their respective items - and wasn't aware of that critical paragraph in Apple's Human Interface Guidelines pointing out that I shouldn't be doing it that way.I included the - (BOOL)validateUserInterfaceItem: method in my code, and it works perfectly. Such a simple solution. Thanks a million :-)
Bender