> I understand I need to have a NSToolbarItem subclass and override its
> validate method, but who is responsible for calling it? Will NSToolbar
> call it during its validation routine or do I need to come up with my
> own validation routine?

As Ashley says, NSToolbar will call validate on the toolbar item as it does 
with other items, which you can control via the NSToolbar delegate method 
-validateToolbarItem: or using the NSUserInterfaceValidations protocol's 
-validateUserInterfaceItem: method. However, the problem in the case of an 
NSToolbarItem that contains a view is that -validate will do nothing, as you 
note and as explained in the toolbar programming guide:

---
View item validation
Validation for view items is not automatic because a view item can be of 
unknown complexity. To implement validation for a view item, you must subclass 
NSToolbarItem and override validate (because NSToolbarItem’s implementation of 
validate does nothing for view items). In your override method, do the 
validation specific to the behavior of the view item and then enable or disable 
whatever you want in the contents of the view accordingly. If the view is an 
NSControl you can call setEnabled:, which will in turn call setEnabled: on the 
control.
---

This means that although you are not responsible for calling -validate, when it 
gets called automatically it will do nothing for view items. So in order for 
validation for work, when you subclass the -validate method, you can call on 
the delegate or user interface validation methods yourself, just as the 
superclass's method would for non-view items, to validate automatically.

Here is the -validate method in my own NSToolbarItem subclass:

- (void)validate
{
        if ([[self toolbar] delegate])
        {
                BOOL enabled = YES;
                
                if ([[[self toolbar] delegate] 
respondsToSelector:@selector(validateToolbarItem:)])
                        enabled = [[[self toolbar] delegate] 
validateToolbarItem:self];
                
                else if ([[[self toolbar] delegate] 
respondsToSelector:@selector(validateUserInterfaceItem:)])
                        enabled = [[[self toolbar] delegate] 
validateUserInterfaceItem:self];
                
                [self setEnabled:enabled];
        }
        
        else if ([self action])
        {
                if (![self target])
                        [self setEnabled:[[[[self view] window] firstResponder] 
respondsToSelector:[self action]]];
        
                else
                        [self setEnabled:[[self target] 
respondsToSelector:[self action]]];
        }
        
        else
                [super validate];
}

This allows you to control the validation of toolbar items with views in them 
the same as you would for standard image toolbar items. (Note that [self 
action] actually calls the control's -action, as the subclass passes things 
through to the control it holds.)

Looking at this code - I haven't reviewed it for a while - it's far from 
perfect. Really it should try to emulate going through the responder chain to 
look for -validateUserInterfaceItem: rather than just looking for that method 
in the delegate. However, for most apps (certainly for mine) where you have a 
window controller or NSDocument subclass as the delegate of your toolbar and 
validating most of your actions, it works fine. The rest of this very simple 
NSToolbarItem subclass for handling views can be downloaded from my site here: 
http://www.literatureandlatte.com/freestuff/ - it's called KBViewToolbarItem, 
surprisingly enough.

Hope that helps.
All the best,
Keith




_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to