forked from glebd/bwtoolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BWAnchoredButtonBar.m
409 lines (333 loc) · 13.2 KB
/
BWAnchoredButtonBar.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//
// BWAnchoredButtonBar.m
// BWToolkit
//
// Created by Brandon Walkin (www.brandonwalkin.com)
// All code is provided under the New BSD license.
//
#import "BWAnchoredButtonBar.h"
#import "NSColor+BWAdditions.h"
#import "NSView+BWAdditions.h"
#import "BWAnchoredButton.h"
#import "BWSplitView.h"
static NSColor *topLineColor, *bottomLineColor;
static NSColor *topColor, *middleTopColor, *middleBottomColor, *bottomColor;
static NSColor *sideInsetColor, *borderedTopLineColor;
static NSColor *resizeHandleColor, *resizeInsetColor;
static NSGradient *gradient;
static BOOL wasBorderedBar;
static float scaleFactor = 0.0f;
@interface BWAnchoredButtonBar (BWABBPrivate)
- (void)drawResizeHandleInRect:(NSRect)handleRect withColor:(NSColor *)color;
- (void)drawLastButtonInsetInRect:(NSRect)rect;
- (BOOL)isInLastSubview;
- (NSSplitView *)splitView;
- (NSInteger)dividerIndexNearestToHandle;
@end
@implementation BWAnchoredButtonBar
@synthesize selectedIndex, isAtBottom, isResizable, handleIsRightAligned, splitViewDelegate;
+ (void)initialize;
{
topLineColor = [[NSColor colorWithCalibratedWhite:(202.0f / 255.0f) alpha:1] retain];
bottomLineColor = [[NSColor colorWithCalibratedWhite:(170.0f / 255.0f) alpha:1] retain];
topColor = [[NSColor colorWithCalibratedWhite:(253.0f / 255.0f) alpha:1] retain];
middleTopColor = [[NSColor colorWithCalibratedWhite:(242.0f / 255.0f) alpha:1] retain];
middleBottomColor = [[NSColor colorWithCalibratedWhite:(230.0f / 255.0f) alpha:1] retain];
bottomColor = [[NSColor colorWithCalibratedWhite:(230.0f / 255.0f) alpha:1] retain];
sideInsetColor = [[NSColor colorWithCalibratedWhite:(255.0f / 255.0f) alpha:0.5] retain];
borderedTopLineColor = [[NSColor colorWithCalibratedWhite:(190.0f / 255.0f) alpha:1] retain];
gradient = [[NSGradient alloc] initWithColorsAndLocations:
topColor, (CGFloat)0.0,
middleTopColor, (CGFloat)0.45454,
middleBottomColor, (CGFloat)0.45454,
bottomColor, (CGFloat)1.0,
nil];
resizeHandleColor = [[NSColor colorWithCalibratedWhite:(0.0f / 255.0f) alpha:0.598] retain];
resizeInsetColor = [[NSColor colorWithCalibratedWhite:(255.0f / 255.0f) alpha:0.55] retain];
}
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
scaleFactor = [[NSScreen mainScreen] userSpaceScaleFactor];
[self setIsResizable:YES];
[self setIsAtBottom:YES];
}
return self;
}
- (id)initWithCoder:(NSCoder *)decoder;
{
if ((self = [super initWithCoder:decoder]) != nil)
{
[self setIsResizable:[decoder decodeBoolForKey:@"BWABBIsResizable"]];
[self setIsAtBottom:[decoder decodeBoolForKey:@"BWABBIsAtBottom"]];
[self setHandleIsRightAligned:[decoder decodeBoolForKey:@"BWABBHandleIsRightAligned"]];
[self setSelectedIndex:[decoder decodeIntForKey:@"BWABBSelectedIndex"]];
}
return self;
}
- (void)encodeWithCoder:(NSCoder*)coder
{
[super encodeWithCoder:coder];
[coder encodeBool:[self isResizable] forKey:@"BWABBIsResizable"];
[coder encodeBool:[self isAtBottom] forKey:@"BWABBIsAtBottom"];
[coder encodeBool:[self handleIsRightAligned] forKey:@"BWABBHandleIsRightAligned"];
[coder encodeInt:[self selectedIndex] forKey:@"BWABBSelectedIndex"];
}
- (void)awakeFromNib
{
scaleFactor = [[NSScreen mainScreen] userSpaceScaleFactor];
// See if we're in a split view, and set its delegate
NSSplitView *splitView = [self splitView];
if (splitView != nil && [splitView isVertical] && [self isResizable])
{
if ([splitView delegate] != nil && ([[splitView delegate] isKindOfClass:[BWAnchoredButtonBar class]] ||
[splitView isKindOfClass:[BWSplitView class]] && [[(BWSplitView *)splitView secondaryDelegate] isKindOfClass:[BWAnchoredButtonBar class]]))
{
// There's already an Anchored Button Bar set as the delegate so we need to set ourself as the split view delegate on
// the button bar. But since there can be multiple button bars, we need to set ourself as the delegate on the last
// button bar of the delegate chain.
BWAnchoredButtonBar *currentDelegate = [splitView isKindOfClass:[BWSplitView class]] ? [(BWSplitView *)splitView secondaryDelegate] : [splitView delegate];
BWAnchoredButtonBar *lastDelegate = currentDelegate;
while (currentDelegate != nil)
{
if ([currentDelegate splitViewDelegate] != nil && [[currentDelegate splitViewDelegate] isKindOfClass:[BWAnchoredButtonBar class]])
{
currentDelegate = [currentDelegate splitViewDelegate];
lastDelegate = currentDelegate;
}
else
{
currentDelegate = nil;
}
}
[lastDelegate setSplitViewDelegate:self];
}
else
{
[splitView setDelegate:self];
}
}
[self bwBringToFront];
}
- (void)drawRect:(NSRect)rect
{
rect = self.bounds;
// Draw gradient
NSRect gradientRect;
if (isAtBottom)
gradientRect = NSMakeRect(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height - 1);
else
gradientRect = NSInsetRect(rect, 0, 1);
[gradient drawInRect:gradientRect angle:270];
// Draw top line
if (isAtBottom)
[topLineColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:YES flip:YES];
else
[borderedTopLineColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:YES flip:YES];
// Draw resize handle
if (isResizable)
{
NSRect handleRect = NSMakeRect(NSMaxX(rect)-11,6,6,10);
if ([self handleIsRightAligned])
handleRect.origin.x = 4;
[self drawResizeHandleInRect:handleRect withColor:resizeHandleColor];
NSRect insetRect = NSOffsetRect(handleRect,1,-1);
[self drawResizeHandleInRect:insetRect withColor:resizeInsetColor];
}
[self drawLastButtonInsetInRect:rect];
// Draw bottom line and sides if it's in non-bottom mode
if (!isAtBottom)
{
[bottomLineColor bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:rect inView:self horizontal:YES flip:NO];
[bottomLineColor bwDrawPixelThickLineAtPosition:0 withInset:1 inRect:rect inView:self horizontal:NO flip:NO];
[bottomLineColor bwDrawPixelThickLineAtPosition:0 withInset:1 inRect:rect inView:self horizontal:NO flip:YES];
}
}
- (void)drawResizeHandleInRect:(NSRect)handleRect withColor:(NSColor *)color
{
[color bwDrawPixelThickLineAtPosition:0 withInset:0 inRect:handleRect inView:self horizontal:NO flip:NO];
[color bwDrawPixelThickLineAtPosition:3 withInset:0 inRect:handleRect inView:self horizontal:NO flip:NO];
[color bwDrawPixelThickLineAtPosition:6 withInset:0 inRect:handleRect inView:self horizontal:NO flip:NO];
}
- (void)drawLastButtonInsetInRect:(NSRect)rect
{
NSView *rightMostView = nil;
if ([[self subviews] count] > 0)
{
rightMostView = [[self subviews] objectAtIndex:0];
NSView *currentSubview = nil;
for (currentSubview in [self subviews])
{
if ([[currentSubview className] isEqualToString:@"BWAnchoredButton"] || [[currentSubview className] isEqualToString:@"BWAnchoredPopUpButton"])
{
if (NSMaxX([currentSubview frame]) > NSMaxX([rightMostView frame]))
rightMostView = currentSubview;
if ([currentSubview frame].origin.x == 0)
[(BWAnchoredButton *)currentSubview setIsAtLeftEdgeOfBar:YES];
else
[(BWAnchoredButton *)currentSubview setIsAtLeftEdgeOfBar:NO];
if (NSMaxX([currentSubview frame]) == NSMaxX([self bounds]))
[(BWAnchoredButton *)currentSubview setIsAtRightEdgeOfBar:YES];
else
[(BWAnchoredButton *)currentSubview setIsAtRightEdgeOfBar:NO];
}
}
}
if (rightMostView != nil && ([[rightMostView className] isEqualToString:@"BWAnchoredButton"] || [[rightMostView className] isEqualToString:@"BWAnchoredPopUpButton"]))
{
NSRect newRect = NSOffsetRect(rect,0,-1);
[sideInsetColor bwDrawPixelThickLineAtPosition:NSMaxX([rightMostView frame]) withInset:0 inRect:newRect inView:self horizontal:NO flip:NO];
}
}
- (void)viewDidMoveToSuperview
{
if ([self splitView] != nil)
self.handleIsRightAligned = [self isInLastSubview];
}
- (BOOL)isInLastSubview
{
// This method could be made more robust. Right now it assumes that the button bar's direct parent is the split view.
if ([self splitView] != nil && [self superview] == [[[self splitView] subviews] lastObject])
return YES;
return NO;
}
- (NSInteger)dividerIndexNearestToHandle
{
if ([self isInLastSubview])
return self.splitView.subviews.count - 2;
return [[[self splitView] subviews] indexOfObject:[self superview]];
}
- (NSSplitView *)splitView
{
NSSplitView *splitView = nil;
id currentView = self;
while (![currentView isKindOfClass:[NSSplitView class]] && currentView != nil)
{
currentView = [currentView superview];
if ([currentView isKindOfClass:[NSSplitView class]])
splitView = currentView;
}
return splitView;
}
- (void)setIsAtBottom:(BOOL)flag
{
isAtBottom = flag;
if (flag)
{
[self setFrameSize:NSMakeSize(self.frame.size.width,23)];
wasBorderedBar = NO;
}
else
{
[self setFrameSize:NSMakeSize(self.frame.size.width,24)];
wasBorderedBar = YES;
}
[self setNeedsDisplay:YES];
}
- (void)setSelectedIndex:(int)anIndex
{
if (anIndex == 0)
{
[self setIsAtBottom:YES];
[self setIsResizable:YES];
}
else if (anIndex == 1)
{
[self setIsAtBottom:YES];
[self setIsResizable:NO];
}
else if (anIndex == 2)
{
[self setIsAtBottom:NO];
[self setIsResizable:NO];
}
selectedIndex = anIndex;
[self setNeedsDisplay:YES];
}
+ (BOOL)wasBorderedBar
{
return wasBorderedBar;
}
- (void)dealloc
{
NSSplitView *splitView = [self splitView];
if ([splitView delegate] == self)
[splitView setDelegate:nil];
[super dealloc];
}
#pragma mark NSSplitView Delegate Methods
// Add the resize handle rect to the split view hot zone
- (NSRect)splitView:(NSSplitView *)aSplitView additionalEffectiveRectOfDividerAtIndex:(NSInteger)dividerIndex
{
if (dividerIndex == [self dividerIndexNearestToHandle])
{
NSRect paddedHandleRect;
paddedHandleRect.origin.y = [aSplitView frame].size.height - [self frame].origin.y - [self bounds].size.height;
// Note: Assumes button bar is nested directly in a split view subview
if (self.handleIsRightAligned)
paddedHandleRect.origin.x = NSMinX([[self superview] frame]);
else
paddedHandleRect.origin.x = NSMaxX([[self superview] frame]) - 15;
paddedHandleRect.size.width = 15;
paddedHandleRect.size.height = [self bounds].size.height;
return paddedHandleRect;
}
else
{
if ([splitViewDelegate respondsToSelector:@selector(splitView:additionalEffectiveRectOfDividerAtIndex:)])
return [splitViewDelegate splitView:aSplitView additionalEffectiveRectOfDividerAtIndex:dividerIndex];
}
return NSZeroRect;
}
// Remaining delegate methods. They test for an implementation by the splitViewDelegate (otherwise perform default behavior)
- (CGFloat)splitView:(NSSplitView *)sender constrainMinCoordinate:(CGFloat)proposedMin ofSubviewAt:(NSInteger)offset
{
if ([splitViewDelegate respondsToSelector:@selector(splitView:constrainMinCoordinate:ofSubviewAt:)])
return [splitViewDelegate splitView:sender constrainMinCoordinate:proposedMin ofSubviewAt:offset];
return proposedMin;
}
- (CGFloat)splitView:(NSSplitView *)sender constrainMaxCoordinate:(CGFloat)proposedMax ofSubviewAt:(NSInteger)offset
{
if ([splitViewDelegate respondsToSelector:@selector(splitView:constrainMaxCoordinate:ofSubviewAt:)])
return [splitViewDelegate splitView:sender constrainMaxCoordinate:proposedMax ofSubviewAt:offset];
return proposedMax;
}
- (void)splitView:(NSSplitView*)sender resizeSubviewsWithOldSize:(NSSize)oldSize
{
if ([splitViewDelegate respondsToSelector:@selector(splitView:resizeSubviewsWithOldSize:)])
return [splitViewDelegate splitView:sender resizeSubviewsWithOldSize:oldSize];
[sender adjustSubviews];
}
- (BOOL)splitView:(NSSplitView *)sender canCollapseSubview:(NSView *)subview
{
if ([splitViewDelegate respondsToSelector:@selector(splitView:canCollapseSubview:)])
return [splitViewDelegate splitView:sender canCollapseSubview:subview];
return NO;
}
- (CGFloat)splitView:(NSSplitView *)sender constrainSplitPosition:(CGFloat)proposedPosition ofSubviewAt:(NSInteger)offset
{
if ([splitViewDelegate respondsToSelector:@selector(splitView:constrainSplitPosition:ofSubviewAt:)])
return [splitViewDelegate splitView:sender constrainSplitPosition:proposedPosition ofSubviewAt:offset];
return proposedPosition;
}
- (NSRect)splitView:(NSSplitView *)splitView effectiveRect:(NSRect)proposedEffectiveRect forDrawnRect:(NSRect)drawnRect ofDividerAtIndex:(NSInteger)dividerIndex
{
if ([splitViewDelegate respondsToSelector:@selector(splitView:effectiveRect:forDrawnRect:ofDividerAtIndex:)])
return [splitViewDelegate splitView:splitView effectiveRect:proposedEffectiveRect forDrawnRect:drawnRect ofDividerAtIndex:dividerIndex];
return proposedEffectiveRect;
}
- (BOOL)splitView:(NSSplitView *)splitView shouldCollapseSubview:(NSView *)subview forDoubleClickOnDividerAtIndex:(NSInteger)dividerIndex
{
if ([splitViewDelegate respondsToSelector:@selector(splitView:shouldCollapseSubview:forDoubleClickOnDividerAtIndex:)])
return [splitViewDelegate splitView:splitView shouldCollapseSubview:subview forDoubleClickOnDividerAtIndex:dividerIndex];
return NO;
}
- (BOOL)splitView:(NSSplitView *)splitView shouldHideDividerAtIndex:(NSInteger)dividerIndex
{
if ([splitViewDelegate respondsToSelector:@selector(splitView:shouldHideDividerAtIndex:)])
return [splitViewDelegate splitView:splitView shouldHideDividerAtIndex:dividerIndex];
return NO;
}
@end