forked from erndev/EDSidebar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEDSideBar.m
373 lines (290 loc) · 9.34 KB
/
EDSideBar.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
//
// ECSideBar.h
//
// Created by erndev
// BSD license.
//
#import "EDSideBar.h"
#define ED_DEFAULT_BUTTON_HEIGHT 60.0
#define ED_DEFAULT_ANIM_DURATION 0.15
#pragma mark -
#pragma mark ECSideBarButtonCell interface
/* default cell */
@interface ECSideBarButtonCell : NSButtonCell
{
}
@end
#pragma mark -
#pragma mark ECSideBarButtonCell Implementation
@implementation ECSideBarButtonCell
- (void)setTextColor:(NSColor *)textColor
{
NSMutableAttributedString *attrTitle = [[NSMutableAttributedString alloc]
initWithAttributedString:[self attributedTitle]];
NSUInteger len = [attrTitle length];
NSRange range = NSMakeRange(0, len);
[attrTitle addAttribute:NSForegroundColorAttributeName
value:textColor
range:range];
[attrTitle fixAttributesInRange:range];
[self setAttributedTitle:attrTitle];
[attrTitle release];
}
-(id)init
{
self = [super init];
if( self )
{
[self setTextColor:[NSColor whiteColor]];
[self setImagePosition:NSImageAbove];
[self setFont:[NSFont fontWithName:@"Lucida Grande" size:11]];
}
return self;
}
- (void)drawWithFrame:(NSRect)frame inView:(NSView *)view {
NSColor *cellColor = nil;
if( [self state] == NSOnState )
cellColor = [NSColor colorWithDeviceWhite:54.0/255.0 alpha:1.0];
else
cellColor = [NSColor colorWithDeviceWhite:65.0/255.0 alpha:1.0];
[cellColor setFill];
NSRectFill(frame);
[self drawBezelWithFrame:frame inView:view];
// Hard coded positions, just for testing... Don't use this cell :-)
NSRect rectTitle = NSMakeRect(frame.origin.x, frame.origin.y+frame.size.height-23, frame.size.width, 20);
NSRect rectImage = NSMakeRect(frame.origin.x, frame.origin.y+5, frame.size.width, frame.size.height - 25);
NSImage *image = nil;
if( [self state] == NSOnState )
{
// [super drawImage:(NSImage *)[self image] withFrame:(NSRect)rectImage inView:(NSView *)view];
[self setTextColor:[NSColor whiteColor]];
image = [self image];
}
else {
image = [self alternateImage]!=nil?[self alternateImage]:[self image];
[self setTextColor:[NSColor lightGrayColor]];
}
[super drawImage:image withFrame:rectImage inView:view];
[super drawTitle:(NSAttributedString *)[self attributedTitle] withFrame:(NSRect)rectTitle inView:(NSView *)view];
}
- (void)drawBezelWithFrame:(NSRect)frame inView:(NSView *)view {
NSColor *cellColor = nil;
if( [self state] == NSOnState )
cellColor = [NSColor colorWithDeviceWhite:54.0/255.0 alpha:1.0];
else
cellColor = [NSColor colorWithDeviceWhite:65.0/255.0 alpha:1.0];
[cellColor setFill];
NSRectFill(frame);
// Emboss
[[NSGraphicsContext currentContext] setShouldAntialias:NO];
CGContextRef ctx = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSetRGBFillColor(ctx, 37.0/255.0, 37.0/255.0, 37.0/255.0, 1.0);
CGContextFillRect(ctx, CGRectMake(0.5, NSMaxY(frame)-2, frame.size.width-1, 1));
CGContextSetRGBFillColor(ctx, 77.0/255.0, 77.0/255.0, 77.0/255.0, 1.0);
CGContextFillRect(ctx, CGRectMake(0.5, NSMaxY(frame)-1, frame.size.width-1, 1));
[[NSGraphicsContext currentContext] setShouldAntialias:YES];
if( [self state] == NSOnState )
{
// upper shadow
NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceWhite:(42.0/255.0) alpha:1.0]
endingColor:[NSColor colorWithDeviceWhite:(56.0/255.0) alpha:1.0]];
NSRect rectGradient = NSMakeRect(0-0.5, NSMinY(frame)-1-0.5, NSWidth(frame)-1, 4.0);
NSBezierPath *path = [NSBezierPath bezierPathWithRect:rectGradient];
[gradient drawInBezierPath:path angle:90];
[gradient release];
}
// Inset in the right
CGContextSetRGBFillColor(ctx, 53.0/255.0, 53.0/255.0, 53.0/255.0, 0.8);
CGContextFillRect(ctx, CGRectMake(NSMaxX(frame)-2, NSMinY(frame), 1.0, frame.size.height));
CGContextSetRGBFillColor(ctx, 18.0/255.0, 18.0/255.0, 18.0/255.0, 1.0);
CGContextFillRect(ctx, CGRectMake(NSMaxX(frame)-1, NSMinY(frame), 1.0, frame.size.height));
}
@end
#pragma mark -
#pragma mark ECSideBar(Private)
@interface EDSideBar(Private)
-(NSButtonCell*)addButton;
-(void)resizeMatrix;
-(void)moveSelectionImage;
@end
#pragma mark -
#pragma mark ECSideBar
@implementation EDSideBar
@synthesize backgroundColor=_backgroundColor;
@synthesize buttonsHeight;
@synthesize sidebarDelegate;
@synthesize layoutMode;
@synthesize cellClass;
@synthesize animateSelection;
@synthesize animationDuration;
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
layoutMode = ECSideBarLayoutCenter;
self.backgroundColor = [NSColor colorWithDeviceWhite:60.0/255.0 alpha:1.0];
animationDuration = ED_DEFAULT_ANIM_DURATION;
animateSelection = NO;
_matrix = [[NSMatrix alloc] initWithFrame:NSZeroRect];
[_matrix setBackgroundColor:_backgroundColor];
[_matrix setMode:NSRadioModeMatrix];
[_matrix setAllowsEmptySelection:NO];
// Defaults
[_matrix setCellClass:[ECSideBarButtonCell class]];
[self addSubview:_matrix];
[self setButtonsHeight :ED_DEFAULT_BUTTON_HEIGHT];
[_matrix setDrawsBackground:YES];
// Setup resize notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewResized:)
name:NSViewFrameDidChangeNotification object:self];
}
return self;
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[_backgroundColor dealloc];
[_matrix dealloc];
[super dealloc];
}
-(void)drawBackground:(NSRect)rect
{
[_backgroundColor set];
NSRectFill(rect);
}
- (void)drawRect:(NSRect)dirtyRect {
[self drawBackground:dirtyRect];
}
-(void)selectButtonAtRow:(NSUInteger)row
{
int rowToSelect = row;
if( row <0 || [_matrix numberOfRows]< row )
rowToSelect =0;
[_matrix setState:(NSInteger)NSOnState atRow:(NSInteger)rowToSelect column:(NSInteger)0];
[self moveSelectionImage];
}
-(void)addButtonWithTitle:(NSString*)title
{
NSCell * cell = [self addButton];
[cell setTitle:title];
}
-(void)addButtonWithTitle:(NSString*)title image:(NSImage*)image
{
NSCell * cell = [self addButton];
[cell setTitle:title];
[cell setImage:image];
}
-(void)addButtonWithTitle:(NSString*)title image:(NSImage*)image alternateImage:(NSImage*)alternateImage
{
NSButtonCell * cell = [self addButton];
[cell setTitle:title];
[cell setImage:image];
[cell setAlternateImage:alternateImage];
}
-(void)setCellClass:(Class)class
{
[_matrix setCellClass:class];
}
-(void)setLayoutMode:(ECSideBarLayoutMode)mode
{
layoutMode = mode;
[self resizeMatrix];
}
-(void)setButtonsHeight:(CGFloat)heigth
{
buttonsHeight = heigth;
[_matrix setCellSize:NSMakeSize([self frame].size.width, heigth)];
[_matrix setIntercellSpacing:NSMakeSize(0.0, 0.0)];
[self resizeMatrix];
}
-(void)setSelectionImage:(NSImage*)img
{
if( selectorImageView == nil )
{
NSRect r = [self frame];
selectorImageView = [[NSImageView alloc]initWithFrame:NSMakeRect(NSMaxX(r)-img.size.width, NSMinY(r), img.size.width, img.size.height)];
[selectorImageView setImage:img];
[selectorImageView setAutoresizingMask:NSViewNotSizable];
[_matrix addSubview:selectorImageView positioned:NSWindowAbove relativeTo:nil];
}
[selectorImageView setImage:img];
[self moveSelectionImage];
}
-(id)cellForItem:(NSInteger)index
{
if( index <0 || index > [_matrix numberOfRows] )
return nil;
id cell = [_matrix cellAtRow:index column:0];
return cell;
}
-(void)buttonClicked:(id)sender
{
[self moveSelectionImage];
if( sidebarDelegate && [sidebarDelegate respondsToSelector:@selector(sideBar:didSelectButton:)] )
{
NSInteger row = [_matrix selectedRow];
[sidebarDelegate sideBar:self didSelectButton:row];
}
}
@end
#pragma mark -
#pragma mark ECSideBar(Private)
@implementation EDSideBar(Private)
-(void)moveSelectionImage
{
if( selectorImageView == nil )
return;
NSInteger row = [_matrix selectedRow];
if( row == -1 )
return;
NSRect rect = [_matrix cellFrameAtRow:(NSInteger)row column:(NSInteger)0];
// Move image view to the new position
NSRect imgFrame = [selectorImageView frame];
imgFrame.origin.y = rect.origin.y + NSHeight(rect)/2.0 - [[selectorImageView image] size].height/2.0 ;
imgFrame.origin.x = rect.origin.x + NSWidth(rect)-[[selectorImageView image] size].width;
if( !animateSelection )
[selectorImageView setFrame:imgFrame];
else {
[[NSAnimationContext currentContext] setDuration:animationDuration];
[[selectorImageView animator] setFrame:imgFrame];
}
}
- (void)viewResized:(NSNotification *)notification
{
[self resizeMatrix];
}
-(NSButtonCell*)addButton
{
[_matrix addRow];
NSButtonCell * cell = [_matrix cellAtRow:[_matrix numberOfRows]-1 column:0];
[cell setButtonType:NSPushOnPushOffButton];
[cell setTarget:self];
[cell setAction:@selector(buttonClicked:)];
[self resizeMatrix];
return cell;
}
-(void)resizeMatrix
{
NSInteger numRows = [_matrix numberOfRows];
CGFloat matrixHeight = numRows * buttonsHeight;
NSRect rect = [self frame];
NSRect matrixRect = rect;
matrixRect.origin.x = 0.0;
matrixRect.size.width = NSWidth(rect);
matrixRect.size.height = matrixHeight;
if( layoutMode == ECSideBarLayoutTop )
{
matrixRect.origin.y = NSHeight(rect)-matrixHeight;
}
else if ( layoutMode == ECSideBarLayoutCenter )
{
matrixRect.origin.y = ((NSHeight(rect)-matrixHeight)/2.0);
}
else if ( layoutMode == ECSideBarLayoutBottom)
{
matrixRect.origin.y = 0.0;
}
[_matrix setFrame:matrixRect];
[self moveSelectionImage];
}
@end