-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIFHeadingsBrowser.m
299 lines (230 loc) · 7.7 KB
/
IFHeadingsBrowser.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
//
// IFHeadingsBrowser.m
// Inform-xc2
//
// Created by Andrew Hunter on 24/08/2006.
// Copyright 2006 Andrew Hunter. All rights reserved.
//
#import "IFHeadingsBrowser.h"
#import "IFCustomPopup.h"
@implementation IFHeadingsBrowser
- (id) init {
self = [super init];
if (self) {
[NSBundle loadNibNamed: @"HeadingsBrowser"
owner: self];
animator = [[IFViewAnimator alloc] initWithFrame: NSMakeRect(0,0,1,1)];
}
return self;
}
- (void) dealloc {
[headingsView release];
[animator release];
[intel release];
[root release];
[super dealloc];
}
// = Getting information about this browser =
- (NSView*) view {
return headingsView;
}
// = Setting what to browse =
- (NSString*) crumbForHeading: (NSString*) fullHeading {
static NSDictionary* stopwords = nil;
if (stopwords == nil) {
stopwords = [[NSDictionary dictionaryWithObjectsAndKeys:
@"a", @"a",
@"the", @"the",
@"an", @"an",
@"and", @"and",
nil] retain];
}
// Get the parts of the heading
NSArray* parts = [fullHeading componentsSeparatedByString: @" "];
if ([parts count] < 2) return fullHeading;
// Create the result
NSMutableString* result = [[[NSMutableString alloc] init] autorelease];
// Format is 'X - heading something[...]'
[result appendFormat: @"%@ -", [parts objectAtIndex: 1]];
// Iterate through the parts of the name after the section dash
int part;
int displayed = 0;
for (part=2; part<[parts count] && displayed < 2; part++) {
NSString* thisPart = [[parts objectAtIndex: part] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
// Check that this part is not too short
if ([thisPart length] <= 1) continue;
if ([stopwords objectForKey: [thisPart lowercaseString]] != nil) continue;
// Add this part to the list displayed
displayed++;
[result appendFormat: @" %@", thisPart];
}
// Add an ellipsis
if (part < [parts count]) {
unichar ellipsis = 0x2026;
[result appendString: [NSString stringWithCharacters: &ellipsis
length: 1]];
}
// Return the result
return result;
}
- (NSString*) typeForHeading: (NSString*) fullHeading {
// Get the parts of the heading
NSArray* parts = [fullHeading componentsSeparatedByString: @" "];
if ([parts count] < 1) return fullHeading;
return [[parts objectAtIndex: 0] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (NSString*) titleForHeading: (NSString*) fullHeading {
// Get the parts of the heading
NSArray* parts = [fullHeading componentsSeparatedByString: @" "];
if ([parts count] < 2) return fullHeading;
// Create the result
NSMutableString* result = [[[NSMutableString alloc] init] autorelease];
// Format is 'X - heading something[...]'
[result appendFormat: @"%@", [parts objectAtIndex: 1]];
// Iterate through the parts of the name after the section dash
int part;
for (part=2; part<[parts count]; part++) {
NSString* thisPart = [[parts objectAtIndex: part] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
// Add this part to the list displayed
[result appendFormat: @" %@", thisPart];
}
// Add an ellipsis
if (part < [parts count]) {
unichar ellipsis = 0x2026;
[result appendString: [NSString stringWithCharacters: &ellipsis
length: 1]];
}
// Return the result
return result;
}
- (void) updateViews {
[breadcrumb removeAllBreadcrumbs];
[animator prepareToAnimateView: sectionView];
// Add the top level 'bullet' breadcrumb
unichar bullet = 0x2022;
[breadcrumb addBreadcrumbWithText: [NSString stringWithCharacters: &bullet
length: 1]
tag: -1];
// Calculate the rest of the sections
NSMutableArray* sections = [NSMutableArray array];
IFIntelSymbol* parent = [root parent];
int tagCount = 0;
while (parent != nil) {
if ([parent level] <= 0) {
break;
}
tagCount++;
[sections addObject: [parent name]];
parent = [parent parent];
}
// Add them as breadcrumb sections
NSEnumerator* crumbEnum = [sections reverseObjectEnumerator];
NSString* crumb;
while (crumb = [crumbEnum nextObject]) {
[breadcrumb addBreadcrumbWithText: [self crumbForHeading: crumb]
tag: tagCount--];
}
// Update the section view
IFIntelSymbol* section = [[root parent] child];
[sectionView clear];
if (section == nil) section = [intel firstSymbol];
if ([section level] == 0) section = [section child];
int level = -1;
while (section != nil) {
if ([section level] != level) {
[sectionView addHeading: [[self typeForHeading: [section name]] stringByAppendingString: @"..."]
tag: nil];
level = [section level];
}
[sectionView addSection: [self titleForHeading: [section name]]
subSections: [section child] != nil
tag: section];
section = [section sibling];
}
// Use this opportunity to update the action of the section view
[sectionView setTarget: self];
[sectionView setGotoSubsectionAction: @selector(gotoSubsection:)];
[sectionView setSelectedItemAction: @selector(selectedItem:)];
[breadcrumb setTarget: self];
[breadcrumb setAction: @selector(gotoBreadcrumb:)];
// Set the size of the section view
NSRect viewRect = [sectionView frame];
NSSize idealSize = [sectionView idealSize];
if (viewRect.size.height < idealSize.height || ![[headingsView window] isVisible]) {
viewRect.size.height = idealSize.height + 4;
NSRect overallRect = [headingsView frame];
overallRect.size.height = NSMaxY(viewRect) + [breadcrumb frame].size.height;
[headingsView setFrame: overallRect];
[sectionView setFrame: viewRect];
}
// Animate to the new view
if ([[headingsView window] isVisible]) {
[animator animateTo: sectionView
style: animStyle];
} else {
[animator finishAnimation];
}
animStyle = IFAnimateLeft;
}
- (void) setIntel: (IFIntelFile*) newIntel {
[intel release];
intel = [newIntel retain];
// Shrink this view down to a minimum size
NSRect smallRect = [sectionView frame];
smallRect.size.height = 4;
NSRect overallRect = [headingsView frame];
overallRect.size.height = NSMaxY(smallRect) + [breadcrumb frame].size.height;
[headingsView setFrame: overallRect];
}
- (void) setSection: (IFIntelSymbol*) newSection {
[root release];
root = [newSection retain];
[self updateViews];
}
- (void) setSectionByLine: (int) line {
[self setSection: [intel nearestSymbolToLine: line]];
}
// = Actions =
- (void) keyDown: (NSEvent*) ev {
if ([[ev characters] characterAtIndex: 0] == NSLeftArrowFunctionKey) {
if (root == nil || [[root parent] level] == 0) return;
animStyle = IFAnimateRight;
[self setSection: [root parent]];
} else if ([[ev characters] characterAtIndex: 0] == NSRightArrowFunctionKey) {
IFIntelSymbol* newSection = [(IFIntelSymbol*)[sectionView highlightedTag] child];
if (newSection != nil) [self setSection: newSection];
} else {
[sectionView keyDown: ev];
}
}
- (void) gotoSubsection: (IFIntelSymbol*) subsection {
[self setSection: [subsection child]];
}
- (void) selectedItem: (IFIntelSymbol*) subsection {
if (subsection == nil) {
// If you select a section name (ie, a 'Chapter...' part, then go to the section above)
if (root == nil || [[root parent] level] == 0) return;
animStyle = IFAnimateRight;
[self setSection: [root parent]];
} else {
// If you select an actual section, then close the popup
[IFCustomPopup closeAllPopupsWithSender: subsection];
}
}
- (void) gotoBreadcrumb: (IFBreadcrumbCell*) crumb {
int tag = [crumb tag];
IFIntelSymbol* symbol = root;
if (tag == -1) {
symbol = nil;
if (root == nil || [[root parent] level] == 0) return;
} else {
int x;
if (tag == 1) return;
for (x=1; x<tag; x++) {
symbol = [symbol parent];
}
}
animStyle = IFAnimateRight;
[self setSection: symbol];
}
@end