Re: Problem with custom control in NSToolbar
Re: Problem with custom control in NSToolbar
- Subject: Re: Problem with custom control in NSToolbar
- From: Laurent Daudelin <email@hidden>
- Date: Tue, 08 Jun 2004 11:03:09 -0400
On 08/06/04 10:55, "Chris Giordano" <email@hidden> wrote:
>
>
Laurent,
>
>
Are you creating copies of your view in the toolbar items, or just
>
setting the view to that of the original item?
>
>
For view-based toolbar items, one thing that I can say from (my very
>
meager) experience (again, in the test cases for the discussion from a
>
month ago) is that manually making a copy and returning it doesn't
>
yield the same results as returning [toolbarItem copy]. The latter
>
works, whereas the former will only display the view in one place.
>
Archiving and unarchiving the view to get a copy of it gives the
>
correct results, so my guess is that [toolbarItem copy] makes a copy of
>
the view by archiving/unarchiving it which is why that works. (In the
>
example I'm working with, I have verified that the view of a -copy'd
>
toolbar item's view and the original view are different, and the view
>
doesn't respond to copyWithZone:, so it can't be copying via that
>
route.)
>
>
So, am I correct that you're currently manually creating a copy of the
>
toolbar item and then setting the view on the copy to that of the
>
original? What happens if you do something like the following instead?
>
>
// item is the original NSToolbarItem, newItem is the new
>
NSToolbarItem to be returned
>
NSView * v = [item view];
>
NSData * d = [NSArchiver archivedDataWithRootObject:v];
>
v = [NSUnarchiver unarchiveObjectWithData:d];
>
[newItem setView:v];
>
>
>
chris
>
>
Chris,
Here is how I do it:
In class UKToolbarFactory (slightly modified to support custom views):
-(NSToolbarItem*)toolbar:(NSToolbar*)toolbar itemForItemIdentifier:
(NSString*)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag;
{
NSDictionary* allItems = [toolbarItems objectForKey: @"Items"];
NSDictionary* currItem;
NSToolbarItem* tbi = nil;
// One of the system-provided items?
if( [itemIdentifier isEqualToString: NSToolbarSeparatorItemIdentifier]
|| [itemIdentifier isEqualToString: NSToolbarSpaceItemIdentifier]
|| [itemIdentifier isEqualToString:
NSToolbarFlexibleSpaceItemIdentifier]
|| [itemIdentifier isEqualToString:
NSToolbarShowColorsItemIdentifier]
|| [itemIdentifier isEqualToString:
NSToolbarShowFontsItemIdentifier]
|| [itemIdentifier isEqualToString: NSToolbarPrintItemIdentifier]
|| [itemIdentifier isEqualToString:
NSToolbarCustomizeToolbarItemIdentifier] )
return [[[NSToolbarItem alloc] initWithItemIdentifier:
itemIdentifier] autorelease];
// Otherwise, look it up in our list of custom items:
currItem = [allItems objectForKey: itemIdentifier];
if( currItem )
{
SEL itemAction = NSSelectorFromString([currItem
objectForKey: @"Action"]);
NSString* itemLabel = [currItem objectForKey: @"Label"];
NSString* itemCustomLabel = [currItem objectForKey:
@"CustomizationLabel"];
NSString* itemTooltip = [currItem objectForKey: @"ToolTip"];
NSImage* itemImage = [NSImage imageNamed: itemIdentifier];
// ... and create an NSToolbarItem for it and set that up:
tbi = [[[NSToolbarItem alloc] initWithItemIdentifier:
itemIdentifier] autorelease];
[tbi setAction: itemAction];
[tbi setLabel: itemLabel];
// Modified by Laurent Daudelin
if (itemImage)
[tbi setImage: itemImage];
else
{
if ([owner delegate] && [[owner delegate]
respondsToSelector:NSSelectorFromString(itemIdentifier)])
{
// Here is the call to the window's delegate
NSView *tbiView = [[owner delegate]
performSelector:NSSelectorFromString(itemIdentifier)];
[tbi setView:tbiView];
[tbi setMinSize:NSMakeSize(NSWidth([tbiView frame]),
NSHeight([tbiView frame]))];
[tbi setMaxSize:[tbi minSize]];
}
}
// End modifications by Laurent Daudelin
if( itemCustomLabel )
[tbi setPaletteLabel: itemCustomLabel];
else
[tbi setPaletteLabel: itemLabel];
if( itemTooltip )
[tbi setToolTip: itemTooltip];
}
return tbi;
}
Now, in the window's delegate, here is the method that corresponds to the
item identifier:
- (NSView *)switchWebViewPopup
{
return [self createSwitchWebViewPopup];
}
and here is 'createSwitchWebViewPopup':
- (NSView *)createSwitchWebViewPopup
{
NSMenuItem *menuItem = nil;
NSImage *image = nil;
RYZImagePopUpButton *newSwitchWebViewPopUp = nil;
newSwitchWebViewPopUp = [[[RYZImagePopUpButton alloc]
initWithFrame:NSMakeRect(0, 0, 39, 32) pullsDown:NO] autorelease];
[[newSwitchWebViewPopUp cell] setUsesItemFromMenu:YES];
[newSwitchWebViewPopUp setArrowImage:[NSImage
imageNamed:@"ArrowPointingDown"]];
[newSwitchWebViewPopUp setImage:[NSImage imageNamed:@"SwitchTextView"]];
[newSwitchWebViewPopUp setShowsMenuWhenIconClicked:YES];
menuItem = [[NSMenuItem alloc]
initWithTitle:NSLocalizedString(@"TextView", @"Text View")
action:@selector(setWebViewToTextContent:) keyEquivalent: @""];
image = [NSImage imageNamed: @"SwitchTextView"];
[menuItem setImage: image];
[menuItem setTarget: self];
[[newSwitchWebViewPopUp menu] addItem:menuItem];
[menuItem release];
menuItem = [[NSMenuItem alloc]
initWithTitle:NSLocalizedString(@"HTMLView", @"HTML View")
action:@selector(setWebViewToHTMLContent:) keyEquivalent: @""];
image = [NSImage imageNamed: @"SwitchHTMLView"];
[menuItem setImage: image];
[menuItem setTarget: self];
[[newSwitchWebViewPopUp menu] addItem:menuItem];
[menuItem release];
return newSwitchWebViewPopUp;
}
So, as you can see, it's creating a new view everytime it is asked for one.
I haven't tried creating a copy of the current toolbar item if there is an
existing one, but will do so shortly.
Thanks!
-Laurent.
--
========================================================================
Laurent Daudelin Developer, Multifamily, ESO, Fannie Mae
mailto:email@hidden Washington, DC, USA
************************ Usual disclaimers apply ***********************
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.