Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSMenuValidation problems

NSMenuValidation problems
Thread Tools
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status: Offline
Reply With Quote
Jan 23, 2001, 05:14 PM
 
I am writing a program where when certain windows or sheets are on the screen I need to enable and disable menu items. It looks like I should put a - (BOOL)validateMenuItem id <NSMenuItem> )menuItem method into the class definitions of those windows in which I want to control the behavior of the menus. There's only one problem. You see, in my program you can open and save stuff but there is only one document open at a time. This is on purpose. What I am worried about is that if a user switches from the main window, which might have a sheet open that I have told to disable a few menus, to another window, then those menus will become enabled again. The program will either crash or act strangely if the user chooses one of those menu items when it is supposed to be disabled.

In order to cope with that I tried forgetting about NSMenuValidation at all and explicitly enabling my menus and disabling them when the time came. I turned autoenabling off using the - (void)setAutoenablesItems BOOL)flag in NSMenu, however, I found that - (void)setEnabled BOOL)flag in NSMenuItem didn't do anything at all. Yes, after I used it - (BOOL)isEnabled in NSMenuItem would return NO, but the menu would never change. I tried using - (void)itemChanged id <NSMenuItem> )anObject in NSMEnu but that didn't work either.

So I tried leaving autoenabling on and simply setting the targets of the menu items I wanted to disable to NULL. This worked most of the time but some of the time it didn't. I think that it has to do with when NSMenuValidation checks to see if the menu items were changed or not.

So, what is the best way for me to enable or disable menu items in my program?

- update: Fixed the smilies.

[This message has been edited by Dalgo (edited 01-24-2001).]
     
Fresh-Faced Recruit
Join Date: Jan 2001
Location: San Jose, CA USA
Status: Offline
Reply With Quote
Jan 23, 2001, 07:54 PM
 
There are two ways to manage menu items that are specific to certain windows (or types of windows).

(1) In the .nib file, wire the menu items directly to the window or its delegate. This does require that the window be in the same nib file as the menu bar, which effectively limits its use to a singleton window.

(2) Wire the menu items to "First Responder", which is probably what you're doing, and either ensure that only the right window's delegate responds to the messages the menu items send; or use validation, as you're doing, to disable them when the wrong window is active. It sounds like you're basically doing validation correctly. All you need to do is check whether the window is the main window [NSWindow isMainWindow] and return NO if it isn't.
     
Fresh-Faced Recruit
Join Date: Jan 2001
Location: San Jose, CA USA
Status: Offline
Reply With Quote
Jan 23, 2001, 11:08 PM
 
On second thought I think you may just be confused about the way menu validation works. Menu items are validated at the moment that the mouse is pressed in the menu bar or a command key is pressed. So their state is always up to date. It doesn't matter whether a user clicks in another window and then switches back to your document window. When they do something with a menu, your document's menu validation method will get called and will presumably set the state of the items the way you want it.
Is something actually going wrong in your program or are you just afraid something might? I think you're actually OK. Note that while a sheet is displayed your window isn't in the responder chain, so your custom document menu items will automatically be disabled anyway.
     
Dalgo  (op)
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status: Offline
Reply With Quote
Jan 24, 2001, 08:21 AM
 
Thanks.I did find that the reason it wasn't always disbling the menu items when I set the target to NULL was because I didn't always set the target to NULL when I thought that I did. (duh!) Since it checks for valid menus everytime that I open the menu I guess that setting the target to NULL when I want to diable it is fine. I had read that it updates the menu in reponse to user actions... I just didn't think that actually opening a menu would be considered one of those actions.

Just one thing, though, The setEnabled method just doesn't seem to do anything except change the return value for the isEnabled method. Is the setEnabled nothing more than a method which sets that flag? Does it not do anything to enable or diable menu items? Now that I know it's safe to just set the target to NULL in order to disable the menu item I won't worry about this but I am still curious as to why it doesn't work. I did remember to turn autoenabling off when I used the setEnabled method.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:09 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2