forked from erkyrath/Inform7-IDE-Mac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIFSectionalView.m
520 lines (395 loc) · 13.4 KB
/
IFSectionalView.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//
// IFSectionalView.m
// Inform-xc2
//
// Created by Andrew Hunter on 28/08/2006.
// Copyright 2006 Andrew Hunter. All rights reserved.
//
#import "IFSectionalView.h"
#import "IFSectionalSection.h"
// TODO: deal with clicks
// TODO: tracking boxes
static NSFont* sectionFont = nil;
static NSFont* headingFont = nil;
static NSImage* seeSubsections = nil;
@implementation IFSectionalView
// = Initialisation =
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
contents = [[NSMutableArray alloc] init];
tracking = [[NSMutableArray alloc] init];
calculateSizes = YES;
if (sectionFont == nil) {
sectionFont = [[NSFont systemFontOfSize: 13] retain];
headingFont = [[NSFont boldSystemFontOfSize: 11] retain];
seeSubsections = [[NSImage imageNamed: @"Arrow-Closed"] retain];
}
}
return self;
}
- (void) dealloc {
[contents release];
[tracking release];
[highlighted release];
[super dealloc];
}
// = Updating =
- (void) redisplaySection: (IFSectionalSection*) section {
if (section == nil) return;
NSRect sectionBounds = [section bounds];
sectionBounds.size.width = [self bounds].size.width;
[self setNeedsDisplayInRect: sectionBounds];
}
// = Recalculating sizes =
- (NSString*) fitString: (NSString*) string
toWidth: (float) width
withFont: (NSFont*) font {
// Fits a string to the specified width, using the specified font.
// Get the string attributes
NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys: font, NSFontAttributeName, nil];
// Measure the length of the string
NSSize stringSize = [string sizeWithAttributes: attributes];
if (stringSize.width <= width) return string;
// This string needs to be shortened.. measure the length of an ellipsis
NSSize ellipsisSize = [@"..." sizeWithAttributes: attributes];
// Use a binary search to find the ideal length of this string
int bottom = 0;
int top = [string length];
while (top > bottom) {
int middle = (top+bottom)>>1;
NSString* testString = [string substringToIndex: middle];
NSSize testSize = [testString sizeWithAttributes: attributes];
testSize.width += ellipsisSize.width;
if (testSize.width <= width) {
bottom = middle + 1;
} else if (testSize.width > width) {
top = middle - 1;
}
}
// Return the result (top will contain the length of the first string shorter than the specified length)
if (top < 0) top = 0;
return [[string substringToIndex: top] stringByAppendingString: @"..."];
}
- (void) removeTrackingRects {
[highlighted release];
highlighted = nil;
// Remove any existing tracking rectangles
NSEnumerator* trackingEnum = [tracking objectEnumerator];
NSNumber* trackingTag;
while (trackingTag = [trackingEnum nextObject]) {
[self removeTrackingRect: [trackingTag intValue]];
}
[tracking removeAllObjects];
}
- (void) setTrackingRects {
NSRect bounds = [self bounds];
[self removeTrackingRects];
// Create tracking rectangles for all of the sections
NSEnumerator* sectionEnum = [contents objectEnumerator];
IFSectionalSection* section;
float rightMargin = [seeSubsections size].width * 2;
while (section = [sectionEnum nextObject]) {
NSRect trackingRect = [section bounds];
trackingRect.size.width = bounds.size.width-rightMargin;
int tag = [self addTrackingRect: trackingRect
owner: self
userData: section
assumeInside: NO];
[tracking addObject: [NSNumber numberWithInt: tag]];
}
}
- (void) compactStrings {
// Compacts the strings so they fit within the bounds of this view
compactStrings = NO;
NSRect bounds = [self bounds];
// Iterate through the sections
NSEnumerator* sectionEnum = [contents objectEnumerator];
IFSectionalSection* section;
float rightMargin = [seeSubsections size].width * 2;
while (section = [sectionEnum nextObject]) {
// Get some information about this section
NSString* text = [section title];
NSFont* font;
float leftMargin;
if (![section isHeading]) {
font = sectionFont;
leftMargin = 16;
} else {
font = headingFont;
leftMargin = 8;
}
// Compact this string
[section setStringToRender: [self fitString: text
toWidth: bounds.size.width - leftMargin*2 - rightMargin
withFont: font]];
}
// Update the tracking rectangles
[self setTrackingRects];
}
- (void) recalculate {
// No need to calculate any more after this has been called
calculateSizes = NO;
compactStrings = NO;
NSLayoutManager* layoutMgr = [[[NSLayoutManager alloc] init] autorelease];
// Some font attributes
NSDictionary* headingAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
headingFont, NSFontAttributeName,
nil];
NSDictionary* sectionAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
sectionFont, NSFontAttributeName,
nil];
// Reset the general calculated items
idealSize = NSMakeSize(0,0);
float rightMargin = [seeSubsections size].width * 2;
// Iterate through the sections
NSEnumerator* sectionEnum = [contents objectEnumerator];
IFSectionalSection* section;
float lastY = 0;
while (section = [sectionEnum nextObject]) {
// Get some information about this section
NSString* text = [section title];
NSDictionary* attributes;
NSFont* font;
float leftMargin;
if (![section isHeading]) {
attributes = sectionAttributes;
font = sectionFont;
leftMargin = 16;
} else {
attributes = headingAttributes;
font = headingFont;
leftMargin = 8;
}
// Measure this section
float height = [layoutMgr defaultLineHeightForFont: font]+4;
NSSize sectionSize = [text sizeWithAttributes: attributes];
// Work out the bounds
NSRect sectionBounds;
sectionBounds.origin.x = 0;
sectionBounds.origin.y = lastY;
sectionBounds.size.width = sectionSize.width + leftMargin;
sectionBounds.size.height = height;
if (sectionBounds.size.width > idealSize.width) idealSize.width = sectionBounds.size.width;
if (NSMaxY(sectionBounds) > idealSize.height) idealSize.height = NSMaxY(sectionBounds);
// Store the results
[section setBounds: sectionBounds];
// Mark this string as needing compacting
compactStrings = YES;
// Move on
lastY = NSMaxY(sectionBounds);
}
idealSize.width += rightMargin;
}
// = Setting up the contents =
- (void) clear {
[contents removeAllObjects];
[self setNeedsDisplay: YES];
}
- (void) addSection: (IFSectionalSection*) newSection {
calculateSizes = YES;
[contents addObject: newSection];
[self setNeedsDisplay: YES];
}
- (void) addHeading: (NSString*) heading
tag: (id) tag {
IFSectionalSection* newSection = [[IFSectionalSection alloc] init];
[newSection setTitle: heading];
[newSection setHeading: YES];
[newSection setHasSubsections: NO];
[newSection setTag: tag];
[self addSection: [newSection autorelease]];
}
- (void) addSection: (NSString*) section
subSections: (BOOL) hasSubsections
tag: (id) tag {
IFSectionalSection* newSection = [[IFSectionalSection alloc] init];
[newSection setTitle: section];
[newSection setHeading: NO];
[newSection setHasSubsections: hasSubsections];
[newSection setTag: tag];
[self addSection: [newSection autorelease]];
}
- (NSSize) idealSize {
if (calculateSizes) [self recalculate];
return idealSize;
}
- (void) setFrame: (NSRect) frame {
compactStrings = YES;
[super setFrame: frame];
}
// = Actions =
- (void) setTarget: (id) newTarget {
// (We don't retain the target in case this could cause a retention loop)
target = newTarget;
}
- (void) setSelectedItemAction: (SEL) action {
selectedItem = action;
}
- (void) setGotoSubsectionAction: (SEL) action {
gotoSubsection = action;
}
// = Tracking =
- (void)mouseEntered:(NSEvent *)theEvent {
[self redisplaySection: highlighted];
[highlighted release];
highlighted = [(IFSectionalSection*)[theEvent userData] retain];
[self redisplaySection: highlighted];
}
- (void)mouseExited:(NSEvent *)theEvent {
if (highlighted != [theEvent userData]) return;
[self redisplaySection: highlighted];
[highlighted release];
highlighted = nil;
}
- (id) highlightedTag {
return [highlighted tag];
}
- (void) keyDown: (NSEvent*) ev {
// Perform an action based on the key pressed
NSInteger highlightedIndex = [contents indexOfObjectIdenticalTo: highlighted];
NSInteger newHighlighted = highlightedIndex;
int direction = 0;
// Move up or down as required
if ([[ev characters] characterAtIndex: 0] == NSDownArrowFunctionKey) {
if (highlightedIndex == NSNotFound)
newHighlighted = 0;
else
newHighlighted = highlightedIndex+1;
direction = 1;
} else if ([[ev characters] characterAtIndex: 0] == NSUpArrowFunctionKey) {
if (highlightedIndex == NSNotFound)
newHighlighted = [contents count]-1;
else
newHighlighted = highlightedIndex-1;
direction = -1;
} else if ([[ev characters] characterAtIndex: 0] == '\n' ||
[[ev characters] characterAtIndex: 0] == '\r') {
if (highlighted != nil) {
SEL selector = selectedItem;
if ([target respondsToSelector: selector]) {
[target performSelector: selector
withObject: [highlighted tag]];
}
return;
}
}
// If the section to highlight has changed, move it to avoid any headings (which don't highlight)
if (newHighlighted != highlightedIndex) {
// Work out the new highlighted section
if (newHighlighted < 0) return;
if (newHighlighted >= [contents count]) return;
if (newHighlighted < 0 || [contents count] == 0) return;
// TODO: if we have a block entirely consisting of headings, this will crash
IFSectionalSection* section = [contents objectAtIndex: newHighlighted];
while ([section isHeading]) {
newHighlighted += direction;
if (newHighlighted < 0) return;
if (newHighlighted >= [contents count]) return;
section = [contents objectAtIndex: newHighlighted];
}
// Redisplay the current highlighted section
[self redisplaySection: highlighted];
// Set the newly highlighted section
[highlighted release];
highlighted = [section retain];
[self redisplaySection: highlighted];
}
}
// = Clicking =
- (void) mouseUp: (NSEvent*) theEvent {
// Get some information about the state of this click
NSPoint location = [self convertPoint: [theEvent locationInWindow]
fromView: nil];
NSRect bounds = [self bounds];
float rightMargin = [seeSubsections size].width * 2;
BOOL isGoto = location.x > NSMaxX(bounds)-rightMargin;
// Work out which section was clicked
NSEnumerator* sectionEnum = [contents objectEnumerator];
IFSectionalSection* section;
while (section = [sectionEnum nextObject]) {
NSRect sectionBounds = [section bounds];
if (NSMinY(sectionBounds) < location.y && NSMaxY(sectionBounds) > location.y)
break;
}
// Do not send goto requests to items with no subsections
if (isGoto && ![section hasSubsections]) {
return;
}
// Send the action
if (section != nil) {
SEL selector = isGoto?gotoSubsection:selectedItem;
if ([target respondsToSelector: selector]) {
[target performSelector: selector
withObject: [section tag]];
}
}
}
// = Drawing =
- (BOOL)isFlipped {
return YES;
}
- (void)drawRect:(NSRect)rect {
// Perform recalculation if required
if (calculateSizes) [self recalculate];
if (compactStrings) [self compactStrings];
// Some font attributes
NSDictionary* headingAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
headingFont, NSFontAttributeName,
nil];
NSDictionary* sectionAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
sectionFont, NSFontAttributeName,
nil];
NSSize subsectionSize = [seeSubsections size];
NSRect ourBounds = [self bounds];
float rightMargin = subsectionSize.width * 2;
// Draw the items
NSEnumerator* sectionEnum = [contents objectEnumerator];
IFSectionalSection* section;
while (section = [sectionEnum nextObject]) {
NSRect bounds = [section bounds];
if (NSIntersectsRect(rect, bounds)) {
// Get some information about this section
NSString* text = [section stringToRender];
NSDictionary* attributes;
NSFont* font;
float leftMargin;
if (![section isHeading]) {
attributes = sectionAttributes;
font = sectionFont;
leftMargin = 16;
} else {
attributes = headingAttributes;
font = headingFont;
leftMargin = 8;
}
// Measure this section
float height = ([font ascender] - [font descender]);
// Draw the selection rectangle
if (highlighted == section && ![section isHeading]) {
NSRect highlightRect = bounds;
highlightRect.size.width = ourBounds.size.width-rightMargin;
[[NSColor selectedMenuItemColor] set];
NSRectFill(highlightRect);
NSMutableDictionary* lightAttributes = [[attributes mutableCopy] autorelease];
[lightAttributes setObject: [NSColor selectedMenuItemTextColor]
forKey: NSForegroundColorAttributeName];
attributes = lightAttributes;
}
// Draw this section
[text drawAtPoint: NSMakePoint(leftMargin, floorf(bounds.origin.y + (bounds.size.height - height)/2)-1)
withAttributes: attributes];
if ([section hasSubsections]) {
NSRect subsectionArea;
subsectionArea.size = subsectionSize;
subsectionArea.origin.x = floorf(NSMaxX(ourBounds)-subsectionSize.width*1.5);
subsectionArea.origin.y = floorf(NSMinY(bounds) + (bounds.size.height - subsectionSize.height)/2.0);
[seeSubsections drawInRect: subsectionArea
fromRect: NSMakeRect(0,0, subsectionSize.width, subsectionSize.height)
operation: NSCompositeSourceOver
fraction: 1.0];
}
}
}
}
@end