Re: Enabling and Disabling menus
Re: Enabling and Disabling menus
- Subject: Re: Enabling and Disabling menus
- From: Ondra Cada <email@hidden>
- Date: Wed, 22 Aug 2001 22:28:39 +0200
sarat,
>
>>>>> email@hidden (s) wrote at Wed, 22 Aug 2001 14:46:22 -0400:
s> mainMenu = [NSApp mainMenu];
s> NSLog(@"menu title is %s and number of items is %d",[mainMenu
s> title], [mainMenu numberOfItems]);
s> [mainMenu setAutoenablesItems:YES];
Wow. First, you don't need to do that -- the menu autoenables items by
default (otherwise it would not work properly with eg. NSDocuments).
Besides, since the main menu is represented by the goddamned horisontal bar,
I guess there is no point in checking its title. Definitely so if you are
trying to write it out using %s!!!
s> itemArray = [mainMenu itemArray];
s> for (i=0; i< [itemArray count]; i++) {
s> [[itemArray objectAtIndex:i] setTarget:self];
s> }
Well, another two problems. With the horisontal menu bar, it is not
customary to place commands into the main menu directly; normally, all its
items represent submenus. So, there is no point of setting their targets.
Besides, you hardly ever set menu target programmatically (unless we are
speaking of a dynamically filled menus). InterfaceBuilder is the way to do
that.
s> - (BOOL) validateMenuItem: (NSMenuItem *)anItem {
s> if ([[anItem title] isEqualToString: @"New"])
s> return YES;
s> else
s> return NO;
s> }
Just trash all the code from the awakeFromNib of yours, bind menu commands
in the IB to the controller, and implement the above method in it. It should
be called automatically whenever a menu is opened.
s> This doesn't seem to have any effect at all on the application menu
s> items.
This should work.
s> Also I like to know what the mainMenu message to the NSApp fetch. Does
s> the object I get from this message refer to the whole Menu Bar with all
s> the Menu's or something else. Logically its much easier to think in
s> terms of Menu Bar, Menus and Menu Items.
Of course not! That is just a confusion caused by the blasted horisontal
bar. There are just two classes, which construct the complete menu system:
NSMenu: a list of menu items
NSMenuItem: a menu item, might represent a command (then it has
action/target), or a submenu (then it has an embedded NSMenu).
That's all. The main menu (drawn on screen as a menu bar) is just the very
very same NSMenu as any other.
s> PS: Is there a better way to tie the state of other controls in the
s> program like buttons to the state of menu items.
Actually, to tie the state of menu and other controls to the state of the
controller, you mean.
See NSUserInterfaceValidation.h (I guess it is not documented in some
HTML/PDF yet, but I might be mistaken -- I haven't searched!!!).
s> That way I can set the
s> state (enabled/disabled) of one control and all the other controls, menu
s> items that are related reflect the same state.
Well, you have to do it manually, so far. But of course you can select just
one widget as the "primary" one and program others to reflect its state, like
[xxx setEnabled:...];
[a setEnabled:[xxx isEnabled]];
[b setEnabled:[xxx isEnabled]];
[c setEnabled:[xxx isEnabled]];
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
private email@hidden
http://www.ocs.cz/oc