Re: NSToolbar ?
Re: NSToolbar ?
- Subject: Re: NSToolbar ?
- From: Chuck Pisula <email@hidden>
- Date: Sat, 5 May 2001 10:29:49 -0700
Here is some code to put a TextField in the toolbar, the popup, and
textview would be similar as they are also just views.... in the
example below, "SearchDocToolbarItemIdentifier" refers to the identifier
for my search field (a text field) that I place in the toolbar. Also,
"searchFieldOutlet" is a reference to a textfield in my nib.
HTH,
-chuck
- (void) setupToolbar {
// Create a new toolbar instance, and attach it to our document
window
NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier:
MyDocToolbarIdentifier] autorelease];
// Set up toolbar properties: Allow customization, give a default
display mode, and remember state in user defaults
[toolbar setAllowsUserCustomization: YES];
[toolbar setAutosavesConfiguration: YES];
[toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
// We are the delegate
[toolbar setDelegate: self];
// Attach the toolbar to the document window
[documentWindow setToolbar: toolbar];
}
- (NSToolbarItem *) toolbar: (NSToolbar *)toolbar itemForItemIdentifier:
(NSString *) itemIdent willBeInsertedIntoToolbar:(BOOL) willBeInserted {
// Required delegate method Given an item identifier, self method
returns an item
// The toolbar will use self method to obtain toolbar items that can
be displayed in the customization sheet, or in the toolbar itself
NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc]
initWithItemIdentifier: itemIdent] autorelease];
if([itemIdent isEqual: SearchDocToolbarItemIdentifier]) {
// Set up the standard properties
[toolbarItem setLabel: @"Search"];
[toolbarItem setPaletteLabel: @"Search"];
[toolbarItem setToolTip: @"Search Your Document"];
// Use a custom view, a text field, for the search item
// Note that since this code lives with my document, I know there
is only 1 toolbar so I don't have to worry about possibly making a
copying the view
[toolbarItem setView: searchFieldOutlet];
[toolbarItem setMinSize:NSMakeSize(30, NSHeight([searchFieldOutlet
frame]))];
[toolbarItem setMaxSize:NSMakeSize(400,NSHeight([searchFieldOutlet
frame]))];
} else {
// itemIdent refered to a toolbar item that is not provide or
supported by us or cocoa
// Returning nil will inform the toolbar self kind of item is not
supported
toolbarItem = nil;
}
return toolbarItem;
}
- (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar {
// Required delegate method Returns the ordered list of items to
be shown in the toolbar by default
// If during the toolbar's initialization, no overriding values are
found in the user defaults, or if the
// user chooses to revert to the default items self set will be used
return [NSArray arrayWithObjects:
.........SearchDocToolbarItemIdentifier, nil];
}
- (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar {
// Required delegate method Returns the list of all allowed items
by identifier By default, the toolbar
// does not assume any items are allowed, even the separator So,
every allowed item must be explicitly listed
// The set of allowed items is used to construct the customization
palette
return [NSArray arrayWithObjects:
..........SearchDocToolbarItemIdentifier, nil];
}
- (void) toolbarWillAddItem: (NSNotification *) notif {
// Optional delegate method Before an new item is added to the
toolbar, self notification is posted
// self is the best place to notice a new item is going into the
toolbar For instance, if you need to
// cache a reference to the toolbar item or need to set up some
initial state, self is the best place
// to do it The notification object is the toolbar to which the
item is being added The item being
// added is found by referencing the @"item" key in the userInfo
NSToolbarItem *addedItem = [[notif userInfo] objectForKey: @"item"];
if([[addedItem itemIdentifier] isEqual:
SearchDocToolbarItemIdentifier]) {
activeSearchItem = [addedItem retain];
[activeSearchItem setTarget: self];
[activeSearchItem setAction: @selector(searchUsingToolbarTextField:)];
}
}
- (void) toolbarDidRemoveItem: (NSNotification *) notif {
// Optional delegate method After an item is removed from a
toolbar the notification is sent self allows
// the chance to tear down information related to the item that may
have been cached The notification object
// is the toolbar to which the item is being added The item being
added is found by referencing the @"item"
// key in the userInfo
NSToolbarItem *removedItem = [[notif userInfo] objectForKey:
@"item"];
if (removedItem==activeSearchItem) {
[activeSearchItem autorelease];
activeSearchItem = nil;
}
}
On Friday, May 4, 2001, at 09:51 PM, Steve Gehrman wrote:
Does anyone have a code snippet that puts a popup menu, or textview in
an NSToolbar?
steve
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
References: | |
| >NSToolbar ? (From: Steve Gehrman <email@hidden>) |