![]() |
API 0.9.5
|
00001 00002 00003 00004 @implementation CPButtonBar : CPView 00005 { 00006 BOOL _hasResizeControl; 00007 BOOL _resizeControlIsLeftAligned; 00008 CPArray _buttons; 00009 } 00010 00011 + (id)plusButton 00012 { 00013 var button = [[CPButton alloc] initWithFrame:CGRectMake(0, 0, 35, 25)], 00014 image = [[CPImage alloc] initWithContentsOfFile:[[CPBundle bundleForClass:[CPButtonBar class]] pathForResource:@"plus_button.png"] size:CGSizeMake(11, 12)]; 00015 00016 [button setBordered:NO]; 00017 [button setImage:image]; 00018 [button setImagePosition:CPImageOnly]; 00019 00020 return button; 00021 } 00022 00023 + (id)minusButton 00024 { 00025 var button = [[CPButton alloc] initWithFrame:CGRectMake(0, 0, 35, 25)], 00026 image = [[CPImage alloc] initWithContentsOfFile:[[CPBundle bundleForClass:[CPButtonBar class]] pathForResource:@"minus_button.png"] size:CGSizeMake(11, 4)]; 00027 00028 [button setBordered:NO]; 00029 [button setImage:image]; 00030 [button setImagePosition:CPImageOnly]; 00031 00032 return button; 00033 } 00034 00035 + (id)actionPopupButton 00036 { 00037 var button = [[CPPopUpButton alloc] initWithFrame:CGRectMake(0, 0, 35, 25)], 00038 image = [[CPImage alloc] initWithContentsOfFile:[[CPBundle bundleForClass:[CPButtonBar class]] pathForResource:@"action_button.png"] size:CGSizeMake(22, 14)]; 00039 00040 [button addItemWithTitle:nil]; 00041 [[button lastItem] setImage:image]; 00042 [button setImagePosition:CPImageOnly]; 00043 [button setValue:CGInsetMake(0, 0, 0, 0) forThemeAttribute:"content-inset"]; 00044 00045 [button setPullsDown:YES]; 00046 00047 return button; 00048 } 00049 00050 + (CPString)defaultThemeClass 00051 { 00052 return @"button-bar"; 00053 } 00054 00055 + (id)themeAttributes 00056 { 00057 return [CPDictionary dictionaryWithObjects:[CGInsetMake(0.0, 0.0, 0.0, 0.0), CGSizeMakeZero(), [CPNull null], [CPNull null], [CPNull null], [CPNull null]] 00058 forKeys:[@"resize-control-inset", @"resize-control-size", @"resize-control-color", @"bezel-color", @"button-bezel-color", @"button-text-color"]]; 00059 } 00060 00061 - (id)initWithFrame:(CGRect)aFrame 00062 { 00063 self = [super initWithFrame:aFrame]; 00064 00065 if (self) 00066 { 00067 _buttons = []; 00068 [self setNeedsLayout]; 00069 } 00070 00071 return self; 00072 } 00073 00074 - (void)awakeFromCib 00075 { 00076 var view = [self superview], 00077 subview = self; 00078 00079 while (view) 00080 { 00081 if ([view isKindOfClass:[CPSplitView class]]) 00082 { 00083 var viewIndex = [[view subviews] indexOfObject:subview]; 00084 [view setButtonBar:self forDividerAtIndex:viewIndex]; 00085 00086 break; 00087 } 00088 00089 subview = view; 00090 view = [view superview]; 00091 } 00092 } 00093 00094 - (void)setButtons:(CPArray)buttons 00095 { 00096 _buttons = [CPArray arrayWithArray:buttons]; 00097 00098 for (var i = 0, count = [_buttons count]; i < count; i++) 00099 [_buttons[i] setBordered:YES]; 00100 00101 [self setNeedsLayout]; 00102 } 00103 00104 - (CPArray)buttons 00105 { 00106 return [CPArray arrayWithArray:_buttons]; 00107 } 00108 00109 - (void)setHasResizeControl:(BOOL)shouldHaveResizeControl 00110 { 00111 if (_hasResizeControl === shouldHaveResizeControl) 00112 return; 00113 00114 _hasResizeControl = !!shouldHaveResizeControl; 00115 [self setNeedsLayout]; 00116 } 00117 00118 - (BOOL)hasResizeControl 00119 { 00120 return _hasResizeControl; 00121 } 00122 00123 - (void)setResizeControlIsLeftAligned:(BOOL)shouldBeLeftAligned 00124 { 00125 if (_resizeControlIsLeftAligned === shouldBeLeftAligned) 00126 return; 00127 00128 _resizeControlIsLeftAligned = !!shouldBeLeftAligned; 00129 [self setNeedsLayout]; 00130 } 00131 00132 - (BOOL)resizeControlIsLeftAligned 00133 { 00134 return _resizeControlIsLeftAligned; 00135 } 00136 00137 - (CGRect)resizeControlFrame 00138 { 00139 var inset = [self currentValueForThemeAttribute:@"resize-control-inset"], 00140 size = [self currentValueForThemeAttribute:@"resize-control-size"], 00141 currentSize = [self bounds], 00142 leftOrigin = _resizeControlIsLeftAligned ? 0 : currentSize.size.width - size.width - inset.right - inset.left; 00143 00144 return CGRectMake(leftOrigin, 0, size.width + inset.left + inset.right, size.height + inset.top + inset.bottom); 00145 } 00146 00147 - (CGRect)rectForEphemeralSubviewNamed:(CPString)aName 00148 { 00149 if (aName === "resize-control-view") 00150 { 00151 var inset = [self currentValueForThemeAttribute:@"resize-control-inset"], 00152 size = [self currentValueForThemeAttribute:@"resize-control-size"], 00153 currentSize = [self bounds]; 00154 00155 if (_resizeControlIsLeftAligned) 00156 return CGRectMake(inset.left, inset.top, size.width, size.height); 00157 else 00158 return CGRectMake(currentSize.size.width - size.width - inset.right, inset.top, size.width, size.height); 00159 } 00160 00161 return [super rectForEphemeralSubviewNamed:aName]; 00162 } 00163 00164 - (CPView)createEphemeralSubviewNamed:(CPString)aName 00165 { 00166 if (aName === "resize-control-view") 00167 return [[CPView alloc] initWithFrame:CGRectMakeZero()]; 00168 00169 return [super createEphemeralSubviewNamed:aName]; 00170 } 00171 00172 - (void)layoutSubviews 00173 { 00174 [self setBackgroundColor:[self currentValueForThemeAttribute:@"bezel-color"]]; 00175 00176 var normalColor = [self valueForThemeAttribute:@"button-bezel-color" inState:CPThemeStateNormal], 00177 highlightedColor = [self valueForThemeAttribute:@"button-bezel-color" inState:CPThemeStateHighlighted], 00178 disabledColor = [self valueForThemeAttribute:@"button-bezel-color" inState:CPThemeStateDisabled], 00179 textColor = [self valueForThemeAttribute:@"button-text-color" inState:CPThemeStateNormal]; 00180 00181 var buttonsNotHidden = [CPArray arrayWithArray:_buttons], 00182 count = [buttonsNotHidden count]; 00183 00184 while (count--) 00185 if ([buttonsNotHidden[count] isHidden]) 00186 [buttonsNotHidden removeObject:buttonsNotHidden[count]]; 00187 00188 var currentButtonOffset = _resizeControlIsLeftAligned ? CGRectGetMaxX([self bounds]) + 1 : -1, 00189 bounds = [self bounds], 00190 height = CGRectGetHeight(bounds) - 1, 00191 frameWidth = CGRectGetWidth(bounds), 00192 resizeRect = _hasResizeControl ? [self rectForEphemeralSubviewNamed:"resize-control-view"] : CGRectMakeZero(), 00193 resizeWidth = CGRectGetWidth(resizeRect), 00194 availableWidth = frameWidth - resizeWidth - 1; 00195 00196 for (var i = 0, count = [buttonsNotHidden count]; i < count; i++) 00197 { 00198 var button = buttonsNotHidden[i], 00199 width = CGRectGetWidth([button frame]); 00200 00201 if (availableWidth > width) 00202 availableWidth -= width; 00203 else 00204 break; 00205 00206 if (_resizeControlIsLeftAligned) 00207 { 00208 [button setFrame:CGRectMake(currentButtonOffset - width, 1, width, height)]; 00209 currentButtonOffset -= width - 1; 00210 } 00211 else 00212 { 00213 [button setFrame:CGRectMake(currentButtonOffset, 1, width, height)]; 00214 currentButtonOffset += width - 1; 00215 } 00216 00217 [button setValue:normalColor forThemeAttribute:@"bezel-color" inState:CPThemeStateNormal | CPThemeStateBordered]; 00218 [button setValue:highlightedColor forThemeAttribute:@"bezel-color" inState:CPThemeStateHighlighted | CPThemeStateBordered]; 00219 [button setValue:disabledColor forThemeAttribute:@"bezel-color" inState:CPThemeStateDisabled | CPThemeStateBordered]; 00220 [button setValue:textColor forThemeAttribute:@"text-color" inState:CPThemeStateBordered]; 00221 00222 // FIXME shouldn't need this 00223 [button setValue:normalColor forThemeAttribute:@"bezel-color" inState:CPThemeStateNormal | CPThemeStateBordered | CPPopUpButtonStatePullsDown]; 00224 [button setValue:highlightedColor forThemeAttribute:@"bezel-color" inState:CPThemeStateHighlighted | CPThemeStateBordered | CPPopUpButtonStatePullsDown]; 00225 [button setValue:disabledColor forThemeAttribute:@"bezel-color" inState:CPThemeStateDisabled | CPThemeStateBordered | CPPopUpButtonStatePullsDown]; 00226 00227 [self addSubview:button]; 00228 } 00229 00230 if (_hasResizeControl) 00231 { 00232 var resizeControlView = [self layoutEphemeralSubviewNamed:@"resize-control-view" 00233 positioned:CPWindowAbove 00234 relativeToEphemeralSubviewNamed:nil]; 00235 00236 [resizeControlView setAutoresizingMask: _resizeControlIsLeftAligned ? CPViewMaxXMargin : CPViewMinXMargin]; 00237 [resizeControlView setBackgroundColor:[self currentValueForThemeAttribute:@"resize-control-color"]]; 00238 } 00239 } 00240 00241 - (void)setFrameSize:(CGSize)aSize 00242 { 00243 [super setFrameSize:aSize]; 00244 [self setNeedsLayout]; 00245 } 00246 00247 @end 00248 00249 var CPButtonBarHasResizeControlKey = @"CPButtonBarHasResizeControlKey", 00250 CPButtonBarResizeControlIsLeftAlignedKey = @"CPButtonBarResizeControlIsLeftAlignedKey", 00251 CPButtonBarButtonsKey = @"CPButtonBarButtonsKey"; 00252 00253 @implementation CPButtonBar (CPCoding) 00254 00255 - (void)encodeWithCoder:(CPCoder)aCoder 00256 { 00257 [super encodeWithCoder:aCoder]; 00258 00259 [aCoder encodeBool:_hasResizeControl forKey:CPButtonBarHasResizeControlKey]; 00260 [aCoder encodeBool:_resizeControlIsLeftAligned forKey:CPButtonBarResizeControlIsLeftAlignedKey]; 00261 [aCoder encodeObject:_buttons forKey:CPButtonBarButtonsKey]; 00262 } 00263 00264 - (id)initWithCoder:(CPCoder)aCoder 00265 { 00266 if (self = [super initWithCoder:aCoder]) 00267 { 00268 _buttons = [aCoder decodeObjectForKey:CPButtonBarButtonsKey] || []; 00269 _hasResizeControl = [aCoder decodeBoolForKey:CPButtonBarHasResizeControlKey]; 00270 _resizeControlIsLeftAligned = [aCoder decodeBoolForKey:CPButtonBarResizeControlIsLeftAlignedKey]; 00271 } 00272 00273 return self; 00274 } 00275 00276 @end