Skip to content

Instantly share code, notes, and snippets.

@tinomen
Created April 16, 2010 07:39
Show Gist options
  • Save tinomen/368138 to your computer and use it in GitHub Desktop.
Save tinomen/368138 to your computer and use it in GitHub Desktop.
//Add the following category to CPToolbar
@implementation CPToolbar (selectables)
- (CPToolbar)initWithSelector:(CPString)anId forWindow:(CPWindow)aWindow
{
if(self = [self initWithIdentifier:anId])
{
var mainBundle = [CPBundle mainBundle],
image = [[CPImage alloc] initWithContentsOfFile:[mainBundle pathForResource:@"selected.png"]
size:CGSizeMake(44, 57)];
//- this image does not scale
//- hack on new property
self._toolbarSelection = [[CPImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 52.0, 57.0)];
[_toolbarSelection setImage:image];
[_toolbarSelection setTag:SELECTED_TOOLBAR_ITEM];
[_toolbarSelection setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin];
[aWindow setToolbar:self]
}
return self ;
}
-(void)setSelectedToolbarItem:(CPString)aTag
{
var selectionFrame = [_toolbarSelection frame],
subviews = [_toolbarView subviews];
if([[_toolbarView subviews] indexOfObject:_toolbarSelection] == CPNotFound)
[_toolbarView addSubview:_toolbarSelection];
//- hack to move selection image behind the selected toolbar button
for(var enumerator = [subviews objectEnumerator], toolbarItemView = nil; toolbarItemView = [enumerator nextObject]; )
{
if(aTag === toolbarItemView._toolbarItem._tag)
break;
}
var frame = [toolbarItemView convertRect:[toolbarItemView bounds] toView:_toolbarView];
[_toolbarSelection setFrameOrigin:CGPointMake(CGRectGetMinX(frame) + (CGRectGetWidth(frame) - CGRectGetWidth(selectionFrame)) / 2.0, 0.0)];
}
@end
// create toolbar using new init
var toolbar = [[CPToolbar alloc] initWithSelector:@"Tools" forWindow:theWindow];
// add this line to the callback method
[[sender toolbar] setSelectedToolbarItem:[sender tag]];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment