SSSelectableToolbar.m (5155B)
1 // 2 // SSToolbar.m 3 // SelectableToolbarHelper 4 // 5 // Created by Steven Streeting on 19/06/2011. 6 // Copyright 2011 Torus Knot Software Ltd. All rights reserved. 7 // 8 9 #import "SSSelectableToolbar.h" 10 #import "SSSelectableToolbarItem.h" 11 12 13 @implementation SSSelectableToolbar 14 @synthesize window; 15 @synthesize defaultItemIndex; 16 17 - (id) initWithIdentifier:(NSString *)identifier 18 { 19 if (self = [super initWithIdentifier:identifier]) 20 { 21 blankView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)]; 22 } 23 return self; 24 } 25 26 - (void) dealloc 27 { 28 [blankView release]; 29 [window release]; 30 [super dealloc]; 31 32 } 33 -(IBAction)toolbarItemClicked:sender 34 { 35 // this is really only here so I can set up a target / action which makes button clickable 36 } 37 38 -(void)selectDefaultItem:sender 39 { 40 // select the default item first time becomes key 41 [self selectItemWithIndex:defaultItemIndex]; 42 // Don't tell us again 43 [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidBecomeKeyNotification object:window]; 44 45 } 46 47 -(void) awakeFromNib 48 { 49 // Set target / action on all buttons to make them clickable 50 NSArray* items = [self visibleItems]; 51 for (NSToolbarItem* item in items) 52 { 53 if ([item isKindOfClass:[SSSelectableToolbarItem class]]) 54 { 55 [item setTarget:self]; 56 [item setAction:@selector(toolbarItemClicked:)]; 57 } 58 } 59 60 // Wait until window displayed before sizing (important for displaying in sheets) 61 [[NSNotificationCenter defaultCenter] addObserver:self 62 selector:@selector(selectDefaultItem:) 63 name:NSWindowDidBecomeKeyNotification 64 object:window]; 65 66 67 } 68 69 -(NSToolbarItem*)itemWithIdentifier:(NSString*)identifier 70 { 71 for (NSToolbarItem* item in [self items]) 72 { 73 if ([[item itemIdentifier] isEqual:identifier]) 74 return item; 75 } 76 return nil; 77 } 78 79 - (void)setSelectedItemIdentifier:(NSString *)itemIdentifier 80 { 81 [super setSelectedItemIdentifier:itemIdentifier]; 82 83 NSToolbarItem* item = [self itemWithIdentifier:itemIdentifier]; 84 if ([item isKindOfClass:[SSSelectableToolbarItem class]]) 85 { 86 SSSelectableToolbarItem* ssitem = (SSSelectableToolbarItem*)item; 87 88 NSView* view = [ssitem linkedView]; 89 if (view && window) 90 { 91 NSView* oldView = [window contentView]; 92 NSRect oldFrame = [oldView frame]; 93 NSRect newFrame = [view frame]; 94 NSRect oldWinFrame = [window frame]; 95 NSRect winFrame = oldWinFrame; 96 float toolbarHeight = NSHeight(oldWinFrame) - NSHeight(oldFrame); 97 98 // resize 99 winFrame.size.width = newFrame.size.width; 100 winFrame.size.height = newFrame.size.height + toolbarHeight; 101 winFrame.origin.y -= NSHeight(winFrame) - NSHeight(oldWinFrame); 102 [window setContentView:blankView]; 103 [window setFrame:winFrame display:YES animate:YES]; 104 [window setContentView:view]; 105 106 // change title 107 [window setTitle:[ssitem label]]; 108 109 // tab to first control (if nextKeyView connected) 110 NSView* keyView = [view nextKeyView]; 111 if (keyView) 112 [window makeFirstResponder:keyView]; 113 114 115 } 116 } 117 } 118 119 -(void)selectItemWithIndex:(NSInteger)idx 120 { 121 NSInteger currIdx = 0; 122 for (NSToolbarItem* item in [self items]) 123 { 124 if ([item isKindOfClass:[SSSelectableToolbarItem class]]) 125 { 126 if (currIdx == idx) 127 { 128 [self setSelectedItemIdentifier:[item itemIdentifier]]; 129 return; 130 } 131 // We're only counting these types, ignore spacing and other default items 132 ++currIdx; 133 } 134 } 135 136 } 137 138 -(NSInteger)selectableItemIndexToMainIndex:(NSInteger)idx 139 { 140 NSInteger selIdx = 0; 141 NSInteger mainIdx = 0; 142 for (NSToolbarItem* item in [self items]) 143 { 144 if ([item isKindOfClass:[SSSelectableToolbarItem class]]) 145 { 146 if (selIdx == idx) 147 { 148 return mainIdx; 149 } 150 ++selIdx; 151 } 152 ++mainIdx; 153 } 154 155 return -1; 156 } 157 @end