-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIFBreadcrumbControl.m
290 lines (214 loc) · 6.53 KB
/
IFBreadcrumbControl.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
//
// IFBreadcrumbControl.m
// Inform-xc2
//
// Created by Andrew Hunter on 20/08/2006.
// Copyright 2006 Andrew Hunter. All rights reserved.
//
#import "IFBreadcrumbControl.h"
#import "IFBreadcrumbCell.h"
@implementation IFBreadcrumbControl
+ (Class) cellClass
{
return [IFBreadcrumbCell class];
}
// = Initialisation =
- (id) initWithFrame: (NSRect) frame {
self = [super initWithFrame: frame];
if (self) {
cells = [[NSMutableArray alloc] init];
}
return self;
}
- (void) dealloc {
[cells release];
[cellRects release];
[selectedCell release];
[super dealloc];
}
// = Maintaining cells =
- (void) calcSize {
needsCalculation = NO;
NSPoint origin = [self bounds].origin;
NSEnumerator* cellEnum = [cells objectEnumerator];
IFBreadcrumbCell* cell;
// Set the left/right cells
while (cell = [cellEnum nextObject]) {
[cell setIsLeft: NO];
[cell setIsRight: NO];
}
if ([cells count] == 0) {
return;
}
[[cells objectAtIndex: 0] setIsLeft: YES];
[[cells lastObject] setIsRight: YES];
// Clear out the list of rectangles
[cellRects release];
cellRects = [[NSMutableArray alloc] init];
cellEnum = [cells objectEnumerator];
float width = 0;
float height = 0;
while (cell = [cellEnum nextObject]) {
// Recalculate the bounds for this cell
[cell calcDrawInfo: [self bounds]];
// Move the origin on appropriately
NSSize cellSize = [cell cellSize];
NSRect cellRect;
cellRect.origin = origin;
cellRect.size = cellSize;
if (cellSize.height > height) height = cellSize.height;
[cellRects addObject: [NSValue valueWithRect: cellRect]];
origin.x += cellRect.size.width - [cell overlap];
width += cellRect.size.width - [cell overlap];
}
// Store the ideal size
idealSize = NSMakeSize(width, height);
// Compress the cells if necessary
NSRect bounds = [self bounds];
// If the crumbs are wider than this view, then compact them
if ([cellRects count] > 0) {
NSRect lastRect = [[cellRects lastObject] rectValue];
horizontalRatio = bounds.size.width/NSMaxX(lastRect);
if (horizontalRatio > 1.0) horizontalRatio = 1.0;
}
// Perform the compression
NSEnumerator* rectEnum = [cellRects objectEnumerator];
NSValue* rect;
[cellRects autorelease];
cellRects = [[NSMutableArray alloc] init];
cellEnum = [cells objectEnumerator];
float xpos = 0;
while (rect = [rectEnum nextObject]) {
cell = [cellEnum nextObject];
NSRect oldRect = [rect rectValue];
float overlap = [cell overlap];
oldRect.origin.x = xpos;
oldRect.size.width = floorf((oldRect.size.width-overlap) * horizontalRatio + overlap);
xpos += oldRect.size.width-overlap;
[cellRects addObject: [NSValue valueWithRect: oldRect]];
}
}
- (NSSize) idealSize {
if (needsCalculation) [self calcSize];
return idealSize;
}
// = Adding cells =
- (void) removeAllBreadcrumbs {
[cells removeAllObjects];
needsCalculation = YES;
[self setNeedsDisplay: YES];
}
- (void) addBreadcrumbWithText: (NSString*) text
tag: (int) tag {
IFBreadcrumbCell* newCell = [[IFBreadcrumbCell alloc] initTextCell: text];
[newCell setTag: tag];
[cells addObject: [newCell autorelease]];
needsCalculation = YES;
[self setNeedsDisplay: YES];
}
- (void) addBreadcrumbWithImage: (NSImage*) image
tag: (int) tag {
IFBreadcrumbCell* newCell = [[IFBreadcrumbCell alloc] initImageCell: image];
[newCell setTag: tag];
[cells addObject: [newCell autorelease]];
needsCalculation = YES;
[self setNeedsDisplay: YES];
}
// = Rendering =
- (void) drawRect: (NSRect) rect {
if (needsCalculation) [self calcSize];
// Draw the background
NSRect bounds = [self bounds];
[[NSColor lightGrayColor] set];
bounds.origin.y += 2;
bounds.size.height -= 2;
NSRectFill(bounds);
// Draw the cells
NSEnumerator* cellEnum = [cells reverseObjectEnumerator];
NSEnumerator* rectEnum = [cellRects reverseObjectEnumerator];
IFBreadcrumbCell* cell;
while (cell = [cellEnum nextObject]) {
NSRect cellRect = [[rectEnum nextObject] rectValue];
if (NSIntersectsRect(rect, cellRect)) {
[cell drawWithFrame: cellRect
inView: self];
}
}
}
// = Dealing with mouse events =
- (void) updateCell: (IFBreadcrumbCell*) whichCell {
int cellIndex = [cells indexOfObjectIdenticalTo: whichCell];
if (cellIndex == NSNotFound) return;
NSRect rect = [[cellRects objectAtIndex: cellIndex] rectValue];
[self setNeedsDisplayInRect: rect];
}
- (void) selectCell: (IFBreadcrumbCell*) whichCell {
if (selectedCell) {
[selectedCell setState: NSOffState];
[self updateCell: selectedCell];
[selectedCell release];
selectedCell = nil;
}
selectedCell = [whichCell retain];
[selectedCell setState: NSOnState];
[self updateCell: selectedCell];
}
- (IFBreadcrumbCell*) cellAtPoint: (NSPoint) position {
if (needsCalculation) [self calcSize];
NSEnumerator* cellEnum = [cells objectEnumerator];
NSEnumerator* rectEnum = [cellRects objectEnumerator];
IFBreadcrumbCell* cell;
while (cell = [cellEnum nextObject]) {
NSRect rect = [[rectEnum nextObject] rectValue];
if (NSPointInRect(position, rect)) {
// Get the next cell
IFBreadcrumbCell* nextCell = [cellEnum nextObject];
// Perform a hit test to see if we're now in this cell
if (position.x > NSMaxX(rect)-[cell overlap]) {
if ([nextCell hitTest: NSMakePoint(position.x - (NSMaxX(rect)-[cell overlap]),
position.y - NSMinY(rect))]) {
return nextCell;
}
}
return cell;
}
}
return nil;
}
- (void) mouseDown: (NSEvent*) evt {
NSPoint mousePoint = [self convertPoint: [evt locationInWindow]
fromView: nil];
IFBreadcrumbCell* cell = [self cellAtPoint: mousePoint];
[self selectCell: cell];
}
- (void) mouseDragged: (NSEvent*) evt {
NSPoint mousePoint = [self convertPoint: [evt locationInWindow]
fromView: nil];
IFBreadcrumbCell* cell = [self cellAtPoint: mousePoint];
[self selectCell: cell];
}
- (void) mouseUp: (NSEvent*) evt {
NSPoint mousePoint = [self convertPoint: [evt locationInWindow]
fromView: nil];
IFBreadcrumbCell* cell = [self cellAtPoint: mousePoint];
if ([cell action] != nil) {
[NSApp sendAction: [cell action]
to: [cell target]
from: cell];
}
if ([self action] != nil) {
// For some reason, [NSApp sendAction:] fails to do what it says on the tin
if ([[self target] respondsToSelector: [self action]]) {
[[self target] performSelector: [self action]
withObject: cell];
} else {
[NSApp sendAction: [self action]
to: [self target]
from: cell];
}
}
[self selectCell: nil];
}
// TODO: accessibility
// TODO: keyboard?
@end