forked from erkyrath/Inform7-IDE-Mac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIFSourceFileView.m
403 lines (320 loc) · 12.1 KB
/
IFSourceFileView.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
//
// IFSourceFileView.m
// Inform
//
// Created by Andrew Hunter on Mon Feb 16 2004.
// Copyright (c) 2004 Andrew Hunter. All rights reserved.
//
#import "IFSourceFileView.h"
#import "IFProjectController.h"
#import "IFContextMatchWindow.h"
#import "IFRestrictedTextStorage.h"
#import "IFNaturalIntel.h"
static NSImage* topTear = nil;
static NSImage* bottomTear = nil;
static NSImage* arrowNotPressed = nil;
static NSImage* arrowPressed = nil;
@implementation IFSourceFileView
- (void) loadImages {
if (!topTear) topTear = [[NSImage imageNamed: @"torn_top"] retain];
if (!bottomTear) bottomTear = [[NSImage imageNamed: @"torn_bottom"] retain];
if (!arrowNotPressed) arrowNotPressed = [[NSImage imageNamed: @"TearArrow"] retain];
if (!arrowPressed) arrowPressed = [[NSImage imageNamed: @"TearArrowPressed"] retain];
}
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
[syntaxDictionary release];
[self loadImages];
}
return self;
}
- (void) dealloc {
[syntaxDictionary release];
[super dealloc];
}
- (void) keyDown: (NSEvent*) event {
IFProjectController* controller = [[self window] windowController];
if ([controller isKindOfClass: [IFProjectController class]]) {
[[NSRunLoop currentRunLoop] performSelector: @selector(removeAllTemporaryHighlights)
target: controller
argument: nil
order: 8
modes: [NSArray arrayWithObject: NSDefaultRunLoopMode]];
}
[super keyDown: event];
}
- (void) setSyntaxDictionaryMatcher: (IFContextMatcher*) matcher {
[syntaxDictionary release];
syntaxDictionary = [matcher retain];
}
- (void) removeRestriction {
// Expand to show the whole view if possible
IFRestrictedTextStorage* storage = (IFRestrictedTextStorage*)[self textStorage];
if ([storage isKindOfClass: [IFRestrictedTextStorage class]]) {
// Get the old restriction, and first visible character
NSLayoutManager* layout = [self layoutManager];
NSRange oldRestriction = [storage restrictionRange];
NSRange selected = [self selectedRange];
NSRect visibleRect = [self visibleRect];
NSPoint containerOrigin = [self textContainerOrigin];
NSPoint containerLocation = NSMakePoint(NSMinX(visibleRect)-containerOrigin.x, NSMinY(visibleRect)-containerOrigin.y);
NSUInteger characterIndex = NSNotFound;
NSUInteger glyphIndex = [layout glyphIndexForPoint: containerLocation
inTextContainer: [self textContainer]];
if (glyphIndex != NSNotFound) {
characterIndex = [layout characterIndexForGlyphAtIndex: glyphIndex];
}
// Remove the storage restriction
BOOL wasTornAtTop = tornAtTop;
[storage removeRestriction];
[self setTornAtTop: NO];
[self setTornAtBottom: NO];
// Reset the selection
selected.location += oldRestriction.location;
[self setSelectedRange: selected];
// Scroll to the last visible character
if (characterIndex != NSNotFound) {
characterIndex += oldRestriction.location;
// Scroll to the most recently visible character
NSRange glyphs = [layout glyphRangeForCharacterRange: NSMakeRange(characterIndex, 65536)
actualCharacterRange: nil];
NSPoint charPoint = [layout boundingRectForGlyphRange: glyphs
inTextContainer: [self textContainer]].origin;
charPoint.x = 0;
charPoint.y += containerOrigin.y;
[self scrollPoint: charPoint];
} else {
// Scroll to the top of the restricted range
NSRange glyphs = [layout glyphRangeForCharacterRange: NSMakeRange(oldRestriction.location, 65536)
actualCharacterRange: nil];
NSPoint charPoint = [layout boundingRectForGlyphRange: glyphs
inTextContainer: [self textContainer]].origin;
charPoint.x = 0;
charPoint.y += containerOrigin.y - wasTornAtTop?[topTear size].height:0;
[self scrollPoint: charPoint];
}
}
}
- (void) mouseDown: (NSEvent*) event {
unsigned modifiers = [event modifierFlags];
if ((modifiers&NSCommandKeyMask) != 0 &&
(modifiers&(NSShiftKeyMask|NSControlKeyMask|NSAlternateKeyMask)) == 0) {
// Cmd+click shows the syntax element for the area the mouse is over
if (syntaxDictionary == nil) return;
// Work out which character the mouse was clicked over
NSPoint containerOrigin = [self textContainerOrigin];
NSPoint viewLocation = [self convertPoint: [event locationInWindow]
fromView: nil];
NSPoint containerLocation = NSMakePoint(viewLocation.x-containerOrigin.x, viewLocation.y-containerOrigin.y);
NSUInteger characterIndex = NSNotFound;
NSUInteger glyphIndex = [[self layoutManager] glyphIndexForPoint: containerLocation
inTextContainer: [self textContainer]];
if (glyphIndex != NSNotFound) {
characterIndex = [[self layoutManager] characterIndexForGlyphAtIndex: glyphIndex];
}
if (characterIndex == NSNotFound) return;
// Build the window to display the results in
IFContextMatchWindow* window = [[[IFContextMatchWindow alloc] init] autorelease];
[window setDelegate: self];
// Run the syntax matcher to find out what help we need to display
BOOL contextOk = NO;
[syntaxDictionary setCaseSensitive: YES];
NSArray* context = [syntaxDictionary getContextAtPoint: characterIndex inString: [[self textStorage] string]];
if (!context || ![window setElements: context]) {
[syntaxDictionary setCaseSensitive: NO];
context = [syntaxDictionary getContextAtPoint: characterIndex inString: [[self textStorage] string]];
if (context) contextOk = [window setElements: context];
} else {
contextOk = YES;
}
if (context && contextOk) {
[window popupAtLocation: [event locationInWindow]
onWindow: [self window]];
}
return;
}
if ((modifiers&(NSShiftKeyMask|NSControlKeyMask|NSAlternateKeyMask|NSCommandKeyMask)) == 0
&& [event clickCount] == 1) {
// Single click, no modifiers
NSRect bounds = [self bounds];
NSRect usedRect = [[self layoutManager] usedRectForTextContainer: [self textContainer]];
NSPoint mousePoint = [self convertPoint: [event locationInWindow]
fromView: nil];
if ((tornAtTop && mousePoint.y < NSMinY(bounds)+[topTear size].height)) {
if ([self delegate] && [[self delegate] respondsToSelector: @selector(sourceFileShowPreviousSection:)]) {
[[self delegate] sourceFileShowPreviousSection: self];
} else {
[self removeRestriction];
}
return;
} else if (tornAtBottom && mousePoint.y > NSMinY(bounds)+[self textContainerOrigin].y+NSMaxY(usedRect)) {
if ([self delegate] && [[self delegate] respondsToSelector: @selector(sourceFileShowNextSection:)]) {
[[self delegate] sourceFileShowNextSection: self];
} else {
[self removeRestriction];
}
// Finished handling this event
return;
}
}
// Process this event as normal
[super mouseDown: event];
}
- (BOOL) openStringUrl: (NSString*) url {
if ([[self delegate] respondsToSelector:@selector(openStringUrl:)]) {
return [[self delegate] openStringUrl: url];
}
return NO;
}
// = Drawing =
- (void) drawRect: (NSRect) rect {
// Perform normal drawing
[[NSGraphicsContext currentContext] saveGraphicsState];
[super drawRect: rect];
[[NSGraphicsContext currentContext] restoreGraphicsState];
// Draw the 'page tears' if necessary
NSRect bounds = [self bounds];
NSColor* tearColour = [NSColor colorWithDeviceRed: 0.95
green: 0.95
blue: 0.9
alpha: 1.0];
if (tornAtTop) {
NSSize tornSize = [topTear size];
// Draw the grey background
[tearColour set];
NSRectFill(NSMakeRect(NSMinX(bounds), NSMinY(bounds), bounds.size.width, tornSize.height));
// Draw the tear
[topTear setFlipped: YES];
[topTear drawInRect: NSMakeRect(NSMinX(bounds), NSMinY(bounds), bounds.size.width, tornSize.height)
fromRect: NSMakeRect(0,0, bounds.size.width, tornSize.height)
operation: NSCompositeSourceOver
fraction: 1.0];
// Draw the 'up' arrow
NSImage* arrow = arrowNotPressed;
NSSize upSize = [arrowNotPressed size];
NSRect upRect;
upRect.origin = NSMakePoint(floorf(NSMinX(bounds) + (bounds.size.width - upSize.width)/2), floorf(NSMinY(bounds) + (tornSize.height - upSize.height)/2));
upRect.size = upSize;
[arrow setFlipped: YES];
[arrow drawInRect: upRect
fromRect: NSMakeRect(0,0, upSize.width, upSize.height)
operation: NSCompositeSourceOver
fraction: 1.0];
}
if (tornAtBottom) {
NSSize tornSize = [bottomTear size];
NSPoint origin = [self textContainerOrigin];
NSRect usedRect = [[self layoutManager] usedRectForTextContainer: [self textContainer]];
NSSize containerSize = NSMakeSize(NSMaxX(usedRect), NSMaxY(usedRect));
// Draw the grey background
[tearColour set];
NSRectFill(NSMakeRect(NSMinX(bounds), origin.y + containerSize.height, bounds.size.width, bounds.size.height - (origin.y + containerSize.height)));
// Draw the tear
[bottomTear setFlipped: YES];
[bottomTear drawInRect: NSMakeRect(NSMinX(bounds), origin.y + containerSize.height, bounds.size.width, tornSize.height)
fromRect: NSMakeRect(0,0, bounds.size.width, tornSize.height)
operation: NSCompositeSourceOver
fraction: 1.0];
// Draw the 'down' arrow
NSImage* arrow = arrowNotPressed;
NSSize upSize = [arrowNotPressed size];
NSRect upRect;
upRect.origin = NSMakePoint(floorf(NSMinX(bounds) + (bounds.size.width - upSize.width)/2), floorf(origin.y + containerSize.height + (tornSize.height - upSize.height)/2));
upRect.size = upSize;
[arrow setFlipped: NO];
[arrow drawInRect: upRect
fromRect: NSMakeRect(0,0, upSize.width, upSize.height)
operation: NSCompositeSourceOver
fraction: 1.0];
}
}
// = Drawing 'tears' at the top and bottom =
- (void) updateTearing {
// Load the images if they aren't already available
[self loadImages];
// Work out the inset to use
NSSize inset = NSMakeSize(3,6);
if (tornAtTop) {
inset.height += floorf([topTear size].height);
}
if (tornAtBottom) {
inset.height += floorf([bottomTear size].height);
}
inset.height = floorf(inset.height/2);
// Update the display
[self setTextContainerInset: inset];
[self invalidateTextContainerOrigin];
lastUsedRect = [[self layoutManager] usedRectForTextContainer: [self textContainer]];
[self setNeedsDisplay: YES];
}
- (BOOL) checkForRedraw {
if (tornAtBottom) {
// If the last used rect is different from the current used rect, then redraw the bottom of the display as well
NSRect newUsedRect = [[self layoutManager] usedRectForTextContainer: [self textContainer]];
if (NSMaxY(newUsedRect) != NSMaxY(lastUsedRect)) {
NSRect bounds = [self bounds];
lastUsedRect = newUsedRect;
float minY = NSMinY(lastUsedRect);
if (NSMinY(newUsedRect) < minY) minY = NSMinY(newUsedRect);
NSRect needsDisplay = NSMakeRect(NSMinX(bounds), minY, bounds.size.width, NSMaxY(bounds)-minY);
[super setNeedsDisplayInRect: needsDisplay];
return YES;
}
}
return NO;
}
- (void)setNeedsDisplayInRect: (NSRect)invalidRect {
[super setNeedsDisplayInRect: invalidRect];
[self checkForRedraw];
}
- (void) unlockFocus {
if ([self checkForRedraw]) {
[self displayIfNeeded];
}
[super unlockFocus];
}
- (NSPoint) textContainerOrigin {
// Calculate the origin
NSPoint origin = NSMakePoint(3,3);
if (tornAtTop) {
origin.y += [topTear size].height;
}
return origin;
}
- (void) setTornAtTop: (BOOL) newTornAtTop {
if (tornAtTop != newTornAtTop) {
tornAtTop = newTornAtTop;
[self updateTearing];
}
}
- (void) setTornAtBottom: (BOOL) newTornAtBottom {
if (tornAtBottom != newTornAtBottom) {
tornAtBottom = newTornAtBottom;
[self updateTearing];
}
}
///
/// Undo/redo
///
- (void) undo: (id) sender {
// Get the undo manager
NSUndoManager* undo = [self undoManager];
// Disable indentation and other intelligence
[IFNaturalIntel disableIndentation];
// Perform the undo
[undo undoNestedGroup];
// Re-enable indentation, etc
[IFNaturalIntel enableIndentation];
}
- (void) redo: (id) sender {
// Get the undo manager
NSUndoManager* undo = [self undoManager];
// Disable indentation and other intelligence
[IFNaturalIntel disableIndentation];
// Perform the undo
[undo redo];
// Re-enable indentation, etc
[IFNaturalIntel enableIndentation];
}
@end