Re: FW: Enabling and Disabling menus
Re: FW: Enabling and Disabling menus
- Subject: Re: FW: Enabling and Disabling menus
- From: Henri Lamiraux <email@hidden>
- Date: Wed, 22 Aug 2001 13:37:52 -0700
1 - yes
2 - No, -validateMenuItem: is called when the menu is pulled down (you
wan the user to see the menu disabled before he can select it) or when
the user press some Command key equivalent. So basically
validateMenuItem is called and the menu items enabled disabled before
the user has a change to select the menu item.
3 - No. just do it the way I showed you. There is no benefit in caching
anything.
4 - You don't have to reset anything. The state of the menu item (enable
or disabled) is basically not stored in the menu item itself but is set
every time the menu is made visible (or the user press a command key
equivalent). Just implement -validateMenuItem:, return YES or NO and
that it.
On Wednesday, August 22, 2001, at 01:21 PM, Stuppel, Searle @ San Diego
Central wrote:
>
So basically as i understand it:
>
>
1. Don't use my method becuase it is as sloppy as i imagined it to be
>
(even though it is effective)
>
2. Everytime it triggers a action call, it checks the menus. Then based
>
on which action was called, it checks the appropriate looping structure
>
within the - (BOOL)validateMenuItem:(NSMenuItem*)menuItem.
>
>
3. I should probably declare a BOOL variable that corresponds to each
>
of the menu items i want to enable disable, and then set them
>
accordingly so that when an action is called, it does the right thing.
>
>
- (BOOL)validateMenuItem:(NSMenuItem*)menuItem
>
{
>
SEL action = [menuItem action];
>
if (action == @selector(doSomething:)) {
>
if (menu1test == TRUE )
>
{return TRUE;}
>
else {return FALSE);
>
} else if (action == ........)
>
....
>
} .....
>
return [menuItem isEnabled];
>
}
>
>
4. Now how do i reset it again at the end of an action? As far as I can
>
see, this calls at the START of an action. Does it trigger AGAIN at the
>
end? This is really important for me to know.
>
>
Thanks for the help
>
>
searle
>
>
PS> Exactly why does setAutoenablesItems not work like it is specified
>
to?
>
>
Searle Stuppel
>
CB Richard Ellis, Inc.
>
Direct: 858-546-4600
>
Fax: 858-546-4616
>
Toll Free: 800-334-9347
>
email@hidden
>
>
>
-----Original Message-----
>
From: email@hidden
>
[mailto:email@hidden]On Behalf Of Henri Lamiraux
>
Sent: Wednesday, August 22, 2001 1:00 PM
>
To: 'email@hidden'
>
Subject: Re: FW: Enabling and Disabling menus
>
>
>
Sorry but setting and unsetting the action of a menu item to enable and
>
disable it is not the right way to do it.
>
>
First just connect your menu items in IB to the appropriate action of
>
the appropriate target object (don't do it by code unless you have a
>
good reason).
>
>
Lets say that you have connected the menu item titled "Do Something" to
>
the -doSomething: method of your target object (either an object
>
instantiated in your nib or the File's Owner or First Responder proxy
>
objects)
>
>
To enable or disable this item depending on some state of your app
>
simply implements the -validateMenuItem: method in the target object
>
>
@implementation
>
<WhatEverTheClassOfTheTargetObjectConnectedToThisMenuItem>
>
>
....
>
- (void)doSomething:(id)sender // the action method trickered by the
>
menu item
>
{
>
......
>
}
>
>
....
>
>
- (BOOL)validateMenuItem:(NSMenuItem*)menuItem
>
{
>
SEL action = [menuItem action];
>
>
if (action == @selector(doSomething:)) {
>
return <YES or NO depending on the state of your app>
>
} else if (action == ........)
>
....
>
} .....
>
>
return [menuItem isEnabled];
>
}
>
>
....
>
@end
>
>
Do not test the menu item title. This completely breaks localization.
>
>
On Wednesday, August 22, 2001, at 12:07 PM, Stuppel, Searle @ San Diego
>
Central wrote:
>
>
> this is not the way to do it... let me share with you the code that
>
> actually
>
> WORKS
>
>
>
> //// the .h:
>
> #import <Cocoa/Cocoa.h>
>
>
>
> @interface MyMenu : NSMenu
>
> {
>
> IBOutlet id menuClear;
>
> IBOutlet id menuGenerate;
>
> IBOutlet id menuSaveHTML;
>
> IBOutlet id menuSaveText;
>
> SEL actClear, actGenerate, actSaveHTML, actSaveText;
>
> }
>
> - (void)setMenu:(id)sender mG:(BOOL)gen mC:(BOOL)cle mST:(BOOL)mst
>
> mSH:(BOOL)msh;
>
> @end
>
>
>
>
>
> //// the .m
>
> #import "MyMenu.h"
>
> #import "Controller.h"
>
>
>
> @implementation MyMenu
>
>
>
> -(void)awakeFromNib
>
> {
>
> actGenerate = [menuGenerate action];
>
> actClear = [menuClear action];
>
> actSaveText = [menuSaveText action];
>
> actSaveHTML = [menuSaveHTML action];
>
> }
>
>
>
> - (void)setMenu:(id)sender mG:(BOOL)gen mC:(BOOL)cle mST:(BOOL)mst
>
> mSH:(BOOL)msh
>
> {
>
> if (gen) {
>
> [menuGenerate setAction:actGenerate];
>
> }
>
> else {
>
> [menuGenerate setAction:0];
>
> }
>
> if (cle) {
>
> [menuClear setAction:actClear];
>
> }
>
> else {
>
> [menuClear setAction:0];
>
> }
>
> if (mst) {
>
> [menuSaveText setAction:actSaveText];
>
> }
>
> else {
>
> [menuSaveText setAction:0];
>
> }
>
> if (msh) {
>
> [menuSaveHTML setAction:actSaveHTML];
>
> }
>
> else {
>
> [menuSaveHTML setAction:0];
>
> }
>
> }
>
>
>
> @end
>
>
>
>
>
>
>
> /// the actual call from another file:
>
>
>
> [menuLink setMenu:self mG:YES mC:NO mST:NO mSH:NO];
>
>
>
> --- where menuLink is an outlet link from the calling file to the menu
>
> file.
>
> --- also you need to import the menu files .h file if you land up
>
doing
>
> this
>
> in separate files
>
>
>
>
>
>
>
> let me know if this helps
>
>
>
> searle
>
>
>
>
>
>
>
> Searle Stuppel
>
> CB Richard Ellis, Inc.
>
> Direct: 858-546-4600
>
> Fax: 858-546-4616
>
> Toll Free: 800-334-9347
>
> email@hidden
>
>
>
>
>
> -----Original Message-----
>
> From: email@hidden
>
> [mailto:email@hidden]On Behalf Of email@hidden
>
> Sent: Wednesday, August 22, 2001 11:46 AM
>
> To: email@hidden
>
> Subject: Enabling and Disabling menus
>
>
>
>
>
> Hello,
>
> I like to know how I can enable and disable menus (actually Menu
>
Items)
>
> in my application based on the state of my application. I read the
>
docs
>
> in
>
>
>
Developer/Documentation/Cocoa/TasksAndConcepts/ProgrammingTopics/AppMenu.
>
> Specifically I read the documentation on NSMenu and NSMenuValidation.
>
> But I am still confused, below is the code I added to my controller
>
> object.
>
>
>
> - (void) awakeFromNib
>
> {
>
> NSMenu *mainMenu;
>
> NSArray *itemArray;
>
> int i;
>
>
>
> [some code here ...............]
>
>
>
> mainMenu = [NSApp mainMenu];
>
> NSLog(@"menu title is %s and number of items is %d",[mainMenu
>
> title], [mainMenu numberOfItems]);
>
> [mainMenu setAutoenablesItems:YES];
>
>
>
> itemArray = [mainMenu itemArray];
>
> for (i=0; i< [itemArray count]; i++) {
>
> [[itemArray objectAtIndex:i] setTarget:self];
>
> }
>
>
>
> }
>
>
>
> I am sending the message setAutoenableItems to the menu's in the above
>
> code.
>
>
>
> Also I implemented the below method in the same class
>
>
>
> - (BOOL) validateMenuItem: (NSMenuItem *)anItem {
>
> if ([[anItem title] isEqualToString: @"New"])
>
> return YES;
>
> else
>
> return NO;
>
> }
>
>
>
> This doesn't seem to have any effect at all on the application menu
>
> items.
>
> Also I like to know what the mainMenu message to the NSApp fetch. Does
>
> the object I get from this message refer to the whole Menu Bar with
>
all
>
> the Menu's or something else. Logically its much easier to think in
>
> terms of Menu Bar, Menus and Menu Items.
>
>
>
> Thanks.
>
> Sarat
>
>
>
> PS: Is there a better way to tie the state of other controls in the
>
> program like buttons to the state of menu items. That way I can set
>
the
>
> state (enabled/disabled) of one control and all the other controls,
>
menu
>
> items that are related reflect the same state.
>
> _______________________________________________
>
> cocoa-dev mailing list
>
> email@hidden
>
> http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
> _______________________________________________
>
> cocoa-dev mailing list
>
> email@hidden
>
> http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
>
_______________________________________
Henri Lamiraux
Engineering Manager
User Interface Tools Group
Apple
_______________________________________
>
_______________________________________________
>
cocoa-dev mailing list
>
email@hidden
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev