-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIFBreadcrumbCell.m
213 lines (168 loc) · 5.41 KB
/
IFBreadcrumbCell.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
//
// IFBreadcrumbCell.m
// Inform-xc2
//
// Created by Andrew Hunter on 20/08/2006.
// Copyright 2006 Andrew Hunter. All rights reserved.
//
#import "IFBreadcrumbCell.h"
static NSImage* leftUnselect;
static NSImage* rightUnselect;
static NSImage* centerUnselect;
static NSImage* leftArrowUnselect;
static NSImage* rightArrowUnselect;
static NSImage* leftSelect;
static NSImage* rightSelect;
static NSImage* centerSelect;
static NSImage* leftArrowSelect;
static NSImage* rightArrowSelect;
@implementation IFBreadcrumbCell
// = Properties =
+ (BOOL)prefersTrackingUntilMouseUp {
return YES;
}
+ (void) initialize {
leftUnselect = [NSImage imageNamed: @"bc_lu"];
rightUnselect = [NSImage imageNamed: @"bc_ru"];
centerUnselect = [NSImage imageNamed: @"bc_cu"];
leftArrowUnselect = [NSImage imageNamed: @"bc_alu"];
rightArrowUnselect = [NSImage imageNamed: @"bc_aru"];
leftSelect = [NSImage imageNamed: @"bc_ls"];
rightSelect = [NSImage imageNamed: @"bc_rs"];
centerSelect = [NSImage imageNamed: @"bc_cs"];
leftArrowSelect = [NSImage imageNamed: @"bc_als"];
rightArrowSelect = [NSImage imageNamed: @"bc_ars"];
}
// = Initialisation =
- (id) initTextCell: (NSString*) text {
self = [super initTextCell: text];
if (self) {
[self setAttributedStringValue:
[[[NSAttributedString alloc] initWithString: text
attributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[NSFont systemFontOfSize: 10], NSFontAttributeName,
nil]] autorelease]];
}
return self;
}
// = Layout information =
- (float) overlap {
if (!isRight) {
return [leftArrowUnselect size].width;
} else {
return 0;
}
}
- (void) setIsRight: (BOOL) ir {
isRight = ir;
}
- (void) setIsLeft: (BOOL) il {
isLeft = il;
}
// = NSCell overrides =
- (void) calcDrawInfo: (NSRect) bounds {
float contentWidth;
if ([self image] != nil) {
contentWidth = [[self image] size].width;
} else {
contentWidth = [[self attributedStringValue] size].width;
}
NSImage* left = isLeft?leftUnselect:leftArrowUnselect;
NSImage* right = isRight?rightUnselect:rightArrowUnselect;
NSSize leftSize = [left size];
NSSize rightSize = [right size];
size.height = leftSize.height;
size.width = leftSize.width + contentWidth + rightSize.width + 6;
}
- (NSSize) cellSize {
return size;
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame
inView:(NSView *)controlView {
NSImage* left;
NSImage* right;
NSImage* center;
if ([self state] != NSOnState) {
left = isLeft?leftUnselect:leftArrowUnselect;
right = isRight?rightUnselect:rightArrowUnselect;
center = centerUnselect;
} else {
left = isLeft?leftSelect:leftArrowSelect;
right = isRight?rightSelect:rightArrowSelect;
center = centerSelect;
}
NSSize leftSize = [left size];
NSSize rightSize = [right size];
NSSize centerSize = [center size];
// Draw the leftmost image
NSRect rect;
NSRect source = NSMakeRect(0,0,0,0);
rect.size = source.size = leftSize;
rect.origin = cellFrame.origin;
[left drawInRect: rect
fromRect: source
operation: NSCompositeSourceOver
fraction: 1.0];
// Draw the centermost image
source.size = centerSize;
rect.origin = cellFrame.origin;
rect.origin.x += leftSize.width;
rect.size.width = cellFrame.size.width - leftSize.width - rightSize.width;
rect.size.height = leftSize.height;
[center drawInRect: rect
fromRect: source
operation: NSCompositeSourceOver
fraction: 1.0];
// Draw the rightmost image
rect.origin = cellFrame.origin;
rect.origin.x += cellFrame.size.width - rightSize.width;
rect.size = source.size = rightSize;
[right drawInRect: rect
fromRect: source
operation: NSCompositeSourceOver
fraction: 1.0];
// Draw the text or image
if ([self image]) {
} else {
NSAttributedString* stringToDraw = [self attributedStringValue];
NSFont* font = [[stringToDraw attributesAtIndex: 0
effectiveRange: nil]
objectForKey: NSFontAttributeName];
NSSize textSize = [stringToDraw size];
// For some reason, [stringToDraw size] sometimes gives the height incorrectly
float fontHeight = [font ascender] - [font descender];
textSize.height = fontHeight;
// Work out the rectangle we should draw the string in
rect.origin.x = cellFrame.origin.x + leftSize.width + 3;
rect.origin.y = (cellFrame.size.height - textSize.height)/2 + cellFrame.origin.y;
rect.size.width = cellFrame.size.width - leftSize.width - rightSize.width - 3;
rect.size.height = textSize.height;
// If the string is too long to fit in, then choose a compacted version (TODO: is there a faster way to do this?)
if (rect.size.width < textSize.width) {
int newLength = [stringToDraw length];
while (rect.size.width < textSize.width && newLength > 0) {
newLength--;
stringToDraw = [stringToDraw attributedSubstringFromRange: NSMakeRange(0, newLength)];
textSize = [stringToDraw size];
textSize.height = fontHeight;
}
}
[stringToDraw drawAtPoint: rect.origin];
}
}
- (BOOL) hitTest: (NSPoint) relativeToCell {
// Technically, this is inaccurate, as we do not test on the right-hand side. However, as this is only ever
// called for the left-hand side, this will be No Problem(tm)
NSImage* hitImage = isLeft?leftUnselect:leftArrowUnselect;
NSSize hitSize = [hitImage size];
if (relativeToCell.x > hitSize.width) {
return YES;
}
NSColor* hitColour;
[hitImage lockFocus];
hitColour = NSReadPixel(relativeToCell);
[hitImage unlockFocus];
return [hitColour alphaComponent] > 0.5;
}
@end