forked from adlr/formulatepro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FPTextAreaB.m
374 lines (334 loc) · 13.5 KB
/
FPTextAreaB.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
//
// FPTextAreaB.m
// FormulatePro
//
// Created by Andrew de los Reyes on 12/12/06.
// Copyright 2006 Andrew de los Reyes. All rights reserved.
//
#import "FPTextAreaB.h"
#import "NSMutableDictionaryAdditions.h"
#import "FPArchiveExtras.h"
#import "FPLogging.h"
@implementation FPTextAreaB
+ (NSString *)archivalClassName;
{
return @"TextArea";
}
- (id)initWithGraphic:(FPGraphic *)graphic
{
self = [super initWithGraphic:graphic];
assert([graphic class] == [FPTextAreaB class]);
if (self) {
FPTextAreaB *gr = (FPTextAreaB *)graphic;
self->_textStorage = [[NSTextStorage allocWithZone:[self zone]] init];
[_textStorage replaceCharactersInRange:NSMakeRange(0, [_textStorage length])
withAttributedString:gr->_textStorage];
self->_editor = nil;
self->_isPlacing = gr->_isPlacing;
self->_isEditing = gr->_isEditing;
self->_isAutoSizedX = gr->_isAutoSizedX;
self->_isAutoSizedY = gr->_isAutoSizedY;
self->_editorScaleFactor = gr->_editorScaleFactor;
}
return self;
}
static NSString *editorFrameKey = @"editorFrame";
static NSString *editorTextStorageKey = @"editorTextStorage";
static NSString *autoSizedXArchiveKey = @"autoSizedX";
static NSString *autoSizedYArchiveKey = @"autoSizedY";
// caller owns returned object
- (void)instantiateVariableWidthEditor
{
NSTextView *ret =
[[NSTextView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 40.0, 40.0)];
[[ret textContainer] setLineFragmentPadding:1.0];
[ret setDrawsBackground:NO];
[ret setPostsFrameChangedNotifications:YES];
[ret setPostsBoundsChangedNotifications:YES];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(myFrameChanged:)
name:NSViewFrameDidChangeNotification
object:ret];
[[ret textContainer] setWidthTracksTextView:NO];
[[ret textContainer] setContainerSize:NSMakeSize(1.0e6, 1.0e6)];
[ret setHorizontallyResizable:YES];
[ret setMinSize:NSMakeSize(1.0, 1.0)];
[ret setMaxSize:NSMakeSize(1.0e6, 1.0e6)];
[[ret textContainer] setHeightTracksTextView:NO];
[ret setVerticallyResizable:YES];
assert(ret);
_editor = [ret retain];
}
- (id)initWithArchivalDictionary:(NSDictionary *)dict
inDocumentView:(FPDocumentView *)docView
{
self = [super initWithArchivalDictionary:dict
inDocumentView:docView];
if (self) {
NSData *rtf_data = [[dict objectForKey:editorTextStorageKey]
dataUsingEncoding:NSUTF8StringEncoding];
_textStorage = [[NSTextStorage alloc] initWithRTF:rtf_data
documentAttributes:nil];
if (nil == _textStorage) // decoding error
_textStorage = [[NSTextStorage alloc] init];
_isPlacing = NO;
_isEditing = NO;
_isAutoSizedX = [[dict objectForKey:autoSizedXArchiveKey] boolValue];
_isAutoSizedY = [[dict objectForKey:autoSizedYArchiveKey] boolValue];
_editorScaleFactor = 1.0;
_gFlags.drawsStroke = NO;
}
return self;
}
- (NSDictionary *)archivalDictionary
{
NSMutableDictionary *ret =
[NSMutableDictionary
dictionaryWithDictionary:[super archivalDictionary]];
[ret setObject:arrayFromRect([_editor frame])
forNonexistentKey:editorFrameKey];
NSData *d =
[_textStorage RTFFromRange:NSMakeRange(0, [_textStorage length])
documentAttributes:nil];
NSString *rtfstr =
[[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding];
[ret setObject:rtfstr forNonexistentKey:editorTextStorageKey];
[ret setObject:[NSNumber numberWithBool:_isAutoSizedX]
forNonexistentKey:autoSizedXArchiveKey];
[ret setObject:[NSNumber numberWithBool:_isAutoSizedY]
forNonexistentKey:autoSizedYArchiveKey];
return ret;
}
- (id)initInDocumentView:(FPDocumentView *)docView
{
self = [super initInDocumentView:docView];
if (self) {
_editor = nil;
_textStorage = [[NSTextStorage alloc] init];
_isPlacing = NO;
_isEditing = NO;
_isAutoSizedX = YES;
_isAutoSizedY = YES;
_editorScaleFactor = 1.0;
_gFlags.drawsStroke = NO;
_knobMask = MiddleLeftKnob | MiddleRightKnob;
}
return self;
}
- (BOOL)isEditable
{
return YES;
}
- (BOOL)placeWithEvent:(NSEvent *)theEvent
{
_isPlacing = YES;
_page = [_docView pageForPointFromEvent:theEvent];
NSPoint point = [_docView pagePointForPointFromEvent:theEvent page:_page];
_bounds.origin = point;
point = [_docView pagePointForPointFromEvent:theEvent page:_page];
_bounds.size = NSMakeSize(10.0,10.0);
_naturalBounds.origin = point;
_naturalBounds.size = NSMakeSize(1.0, 1.0);
// if the next event is mouse up, then the user didn't drag at all
theEvent = [[_docView window] nextEventMatchingMask:
(NSLeftMouseDraggedMask | NSLeftMouseUpMask)];
_isAutoSizedX = _isAutoSizedY = YES;
if ([theEvent type] != NSLeftMouseUp) {
[self resizeWithEvent:theEvent byKnob:LowerRightKnob];
}
_isPlacing = NO;
return YES;
}
static NSLayoutManager *sharedDrawingLayoutManager();
- (void)setBounds:(NSRect)bounds
{
if ((!_isPlacing) && (!_isEditing)) {
//NSRect pixelBounds = [_docView convertRect:bounds fromPage:_page];
NSLayoutManager *lm = sharedDrawingLayoutManager();
NSTextContainer *tc = [[lm textContainers] objectAtIndex:0];
NSRange glyphRange;
[tc setContainerSize:NSMakeSize(NSWidth(bounds), 1.0e6)];
[_textStorage addLayoutManager:lm];
// Force layout of the text and find out how much of it fits in
// the container.
glyphRange = [lm glyphRangeForTextContainer:tc];
NSRect glyphRect = [lm usedRectForTextContainer:tc];
[_textStorage removeLayoutManager:lm];
float heightChange = NSHeight(glyphRect) - NSHeight(bounds);
bounds.origin.y -= heightChange;
bounds.size.height = NSHeight(glyphRect);
DLog(@"glyph: %@\n", NSStringFromRect(glyphRect));
}
// Correct bounds that have a decimal part like .999... as it screws up text
// layout on tiger, leopard (but not snow leopard)
bounds.size.width = round(bounds.size.width);
bounds.size.height = round(bounds.size.height);
[super setBounds:bounds];
}
- (void)resizeWithEvent:(NSEvent *)theEvent byKnob:(int)knob
{
[super resizeWithEvent:theEvent byKnob:knob];
if (NSWidth([self bounds]) >= 10.0) // only go fixed size if it's wide enough
_isAutoSizedX = NO;
}
static NSLayoutManager *sharedDrawingLayoutManager() {
// This method returns an NSLayoutManager that can be used to draw the contents of a SKTTextArea.
static NSLayoutManager *sharedLM = nil;
if (!sharedLM) {
NSTextContainer *tc = [[NSTextContainer allocWithZone:NULL] initWithContainerSize:NSMakeSize(1.0e6, 1.0e6)];
sharedLM = [[NSLayoutManager allocWithZone:NULL] init];
[tc setWidthTracksTextView:NO];
[tc setHeightTracksTextView:NO];
[tc setLineFragmentPadding:1.0];
[sharedLM addTextContainer:tc];
[tc release];
}
return sharedLM;
}
- (void)draw:(BOOL)selected
{
if (_gFlags.drawsStroke) {
NSBezierPath *path = [NSBezierPath bezierPathWithRect:[self bounds]];
[path setLineWidth:[self strokeWidth]];
[[NSColor blackColor] set];
[path stroke];
} else if (selected || _isPlacing) {
NSBezierPath *path = [NSBezierPath bezierPathWithRect:[self bounds]];
[path setLineWidth:[self strokeWidth]];
[[NSColor lightGrayColor] set];
[path stroke];
}
if (!_isEditing) {
NSAffineTransform *transform = [NSAffineTransform transform];
[transform scaleXBy:1.0 yBy:-1.0];
[transform translateXBy:0.0
yBy:(-1.0*(NSMinY(_bounds) + NSMaxY(_bounds)))];
[transform concat];
//NSTextStorage *contents = [_editor textStorage];
if ([_textStorage length] > 0) {
NSLayoutManager *lm = sharedDrawingLayoutManager();
NSTextContainer *tc = [[lm textContainers] objectAtIndex:0];
NSRange glyphRange;
[tc setContainerSize:_bounds.size];
[_textStorage addLayoutManager:lm];
// Force layout of the text and find out how much of it fits in
// the container.
glyphRange = [lm glyphRangeForTextContainer:tc];
if (glyphRange.length > 0) {
[lm drawBackgroundForGlyphRange:glyphRange
atPoint:_bounds.origin];
[lm drawGlyphsForGlyphRange:glyphRange
atPoint:_bounds.origin];
}
[_textStorage removeLayoutManager:lm];
}
[transform invert];
[transform concat];
}
}
- (void)documentDidZoom
{
DLog(@"document did zoom\n");
if (nil == _editor) return;
[_editor scaleUnitSquareToSize:NSMakeSize([_docView scaleFactor] /
_editorScaleFactor,
[_docView scaleFactor] /
_editorScaleFactor)];
_editorScaleFactor = [_docView scaleFactor];
[_editor setFrame:[_docView convertRect:_bounds fromPage:_page]];
}
- (void)myFrameChanged:(NSTextView *)foo
{
// DLog(@"frame changed\n");
// DLog(@"editor frame: %@\n", NSStringFromRect([_editor frame]));
// DLog(@"editor bounds: %@\n", NSStringFromRect([_editor bounds]));
DLog(@"myFrameChanged:\n");
[_docView setNeedsDisplayInRect:[_docView convertRect:[self safeBounds]
fromPage:_page]];
DLog(@"new bounds: %@\n", NSStringFromRect([self bounds]));
// NSRect frame = [_editor frame];
// DLog(@"my frame: %@\n", NSStringFromRect(frame));
// DLog(@"used rect fc: %@\n", NSStringFromRect([[_editor layoutManager] usedRectForTextContainer:[_editor textContainer]]));
// [self setBounds:[_docView convertRect:frame toPage:_page]];
// [_docView setNeedsDisplayInRect:[_docView convertRect:[self safeBounds] fromPage:_page]];
}
- (void)startEditing
{
//NSTextStorage *contents = [[NSTextStorage allocWithZone:[self zone]] init];
DLog(@"mouse down\n");
_isEditing = YES;
BOOL isFirstEdit = NO;
if (_editor == nil) {
isFirstEdit = YES;
DLog(@"allocating\n");
[self instantiateVariableWidthEditor];
}
[self documentDidZoom];
[_textStorage addLayoutManager:[_editor layoutManager]];
if (_isAutoSizedX) {
NSRect frame = _bounds;
[[_editor layoutManager]
glyphRangeForTextContainer:[_editor textContainer]];
frame.size = [[_editor layoutManager]
usedRectForTextContainer:[_editor textContainer]].size;
[[_editor textContainer] setContainerSize:NSMakeSize(1.0e6,
1.0e6)];
[[_editor textContainer] setWidthTracksTextView:NO];
[_editor setHorizontallyResizable:YES];
} else {
[[_editor textContainer] setContainerSize:NSMakeSize(NSWidth(_bounds),
1.0e6)];
[[_editor textContainer] setWidthTracksTextView:NO];
[_editor setHorizontallyResizable:NO];
}
[_editor setFrame:[_docView convertRect:_bounds fromPage:_page]];
[_docView addSubview:_editor];
if (isFirstEdit) {
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[_docView currentFont], NSFontAttributeName,
_strokeColor, NSForegroundColorAttributeName,
nil];
[_editor setTypingAttributes:attributes];
DLog(@"editingn w/ attributes: %@\n", attributes);
}
[_editor setDelegate:self];
[[_docView window] makeFirstResponder:_editor];
}
// When text changes, we may need to update the frame of the in-use NSTextView
// and our bounds.
- (void)textDidChange:(NSNotification *)notification {
DLog(@"textDidChange:\n");
// first invalidate the current (ie, old) bounds
[_docView setNeedsDisplayInRect:[_docView convertRect:[self safeBounds]
fromPage:_page]];
// get the frame of the editor and use it to compute the new bounds
NSRect frame = [_editor frame];
[[_editor layoutManager]
glyphRangeForTextContainer:[_editor textContainer]];
frame.size = [[_editor layoutManager]
usedRectForTextContainer:[_editor textContainer]].size;
frame.size.height *= _editorScaleFactor;
frame.size.width *= _editorScaleFactor;
if (NO == _isAutoSizedX)
frame.size.width = NSWidth([self bounds]) * _editorScaleFactor;
// set the frame to the text view and ourself
[_editor setFrame:frame];
[self setBounds:[_docView convertRect:frame toPage:_page]];
// trigger redrawing in the new bounds
[_docView setNeedsDisplayInRect:[_docView convertRect:[self safeBounds]
fromPage:_page]];
DLog(@"new bounds: %@\n", NSStringFromRect([self bounds]));
}
- (void)stopEditing
{
DLog(@"stop editing\n");
assert(_editor);
[_textStorage removeLayoutManager:[_editor layoutManager]];
[_editor setSelectedRange:NSMakeRange(0, 0)];
[_editor setDelegate:nil];
//[[_editor layoutManager] setDelegate:nil];
[_editor removeFromSuperview];
_isEditing = NO;
}
@end