/
CWTextView.m
266 lines (220 loc) · 5.39 KB
/
CWTextView.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
//
// CWTextView.m
// CommitWindow
//
// Created by Chris Thomas on 3/7/05.
// Copyright 2005-2006 Chris Thomas. All rights reserved.
// MIT license.
//
#import "CWTextView.h"
@implementation CWTextView
- (void) awakeFromNib
{
// Arbitrary factory settings
fMinHeight = 40.0f;
fMaxHeight = 32767.0f;
fMinWidth = 100.0f;
fMaxWidth = 32767.0f;
fAllowGrowHorizontally = NO;
fAllowGrowVertically = YES;
}
#if 0
#pragma mark -
#pragma mark Do not eat the enter key
#endif
- (void) keyDown:(NSEvent *)event
{
// don't let the textview eat the enter key
if( [[event characters] isEqualToString:@"\x03"] )
{
[[self nextResponder] keyDown:event];
}
else
{
[super keyDown:event];
}
}
#if 0
#pragma mark -
#pragma mark Resize box
#endif
- (NSRect) growBoxRect
{
NSRect bounds = [self bounds];
NSRect growBoxRect;
growBoxRect.size.width = 16;
growBoxRect.size.height = 16;
growBoxRect.origin.y = NSMaxY(bounds) - growBoxRect.size.height;
growBoxRect.origin.x = NSMaxX(bounds) - growBoxRect.size.width;
return growBoxRect;
}
- (void) drawRect:(NSRect)rect
{
// NSRect bounds = [self bounds];
NSRect growBoxRect = [self growBoxRect];
[super drawRect:rect];
if( NSContainsRect(rect, growBoxRect) )
{
[NSGraphicsContext saveGraphicsState];
[[NSColor darkGrayColor] set];
[NSBezierPath clipRect:NSInsetRect(growBoxRect, 1, 1)];
[NSBezierPath strokeLineFromPoint:NSMakePoint(growBoxRect.origin.x, growBoxRect.origin.y + 20 )
toPoint:NSMakePoint(growBoxRect.origin.x + 20, growBoxRect.origin.y)];
[NSBezierPath strokeLineFromPoint:NSMakePoint(growBoxRect.origin.x, growBoxRect.origin.y + 24)
toPoint:NSMakePoint(growBoxRect.origin.x + 24, growBoxRect.origin.y)];
[NSBezierPath strokeLineFromPoint:NSMakePoint(growBoxRect.origin.x, growBoxRect.origin.y + 28)
toPoint:NSMakePoint(growBoxRect.origin.x + 28, growBoxRect.origin.y)];
[NSGraphicsContext restoreGraphicsState];
}
}
- (void) mouseDown:(NSEvent *)event
{
NSPoint locationInWindow = [event locationInWindow];
NSPoint locationInView = [self convertPoint:locationInWindow fromView:nil];
NSRect growBoxRect = [self growBoxRect];
if( NSMouseInRect(locationInView, growBoxRect, YES) )
{
fInitialViewFrame = [[self enclosingScrollView] frame];
fInitialMousePoint = locationInWindow;
fTrackingGrowBox = YES;
}
else
{
[super mouseDown:event];
}
}
- (void) mouseUp:(NSEvent *)event
{
if(fTrackingGrowBox)
{
fTrackingGrowBox = NO;
}
else
{
[super mouseUp:event];
}
}
- (void) mouseDragged:(NSEvent *)event
{
NSPoint currentPoint = [event locationInWindow];//[self convertPoint: fromView:nil];
if(fTrackingGrowBox)
{
NSScrollView * scrollView = [self enclosingScrollView];
NSRect scrollFrame = [scrollView frame];
NSRect newFrame = scrollFrame;
float deltaY;
// Horizontal
if( fAllowGrowHorizontally )
{
newFrame.size.width = fInitialViewFrame.size.width + (currentPoint.x - fInitialMousePoint.x);
if(newFrame.size.width < fMinWidth )
{
newFrame.size.width = fMinWidth;
}
else if(newFrame.size.width > fMaxWidth )
{
newFrame.size.width = fMaxWidth;
}
}
// Vertical (FIXME: assumes the scroll view's superview is _not_ flipped)
if( fAllowGrowVertically )
{
deltaY = currentPoint.y - fInitialMousePoint.y;
newFrame.size.height = fInitialViewFrame.size.height - deltaY;
// Check size
if(newFrame.size.height < fMinHeight )
{
newFrame.size.height = fMinHeight;
}
else if(newFrame.size.height > fMaxHeight )
{
newFrame.size.height = fMaxHeight;
}
// Adjust origin of frame
newFrame.origin.y += scrollFrame.size.height - newFrame.size.height;
}
[scrollView setNeedsDisplayInRect:[scrollView bounds]];
[scrollView setFrame:newFrame];
[[NSCursor arrowCursor] set];
}
else
{
[super mouseDragged:event];
}
}
// This alone is not enough -- see mouseMoved: below -- but it does cause the arrow to be correctly displayed during resize
- (void) resetCursorRects
{
[super resetCursorRects];
[self addCursorRect:[self growBoxRect] cursor:[NSCursor arrowCursor]];
}
// Required to override NSTextView's setting of the cursor during mouseMoved events
- (void) mouseMoved:(NSEvent *)event
{
NSPoint locationInWindow = [event locationInWindow];
NSPoint locationInView = [self convertPoint:locationInWindow fromView:nil];
NSRect growBoxRect = [self growBoxRect];
if( NSMouseInRect(locationInView, growBoxRect, YES) )
{
[[NSCursor arrowCursor] set];
}
else
{
[super mouseMoved:event];
}
}
#if 0
#pragma mark -
#pragma mark Simple accessors
#endif
// Grow planes
- (BOOL)allowHorizontalResize
{
return fAllowGrowHorizontally;
}
- (void)setAllowHorizontalResize:(BOOL)newAllowGrowHorizontally
{
fAllowGrowHorizontally = newAllowGrowHorizontally;
}
- (BOOL)allowVerticalResize
{
return fAllowGrowVertically;
}
- (void)setAllowVerticalResize:(BOOL)newAllowGrowVertically
{
fAllowGrowVertically = newAllowGrowVertically;
}
// Geometry
- (float)maxWidth
{
return fMaxWidth;
}
- (void)setMaxWidth:(float)newMaxWidth
{
fMaxWidth = newMaxWidth;
}
- (float)minWidth
{
return fMinWidth;
}
- (void)setMinWidth:(float)newMinWidth
{
fMinWidth = newMinWidth;
}
- (float)minHeight
{
return fMinHeight;
}
- (void)setMinHeight:(float)newMinHeight
{
fMinHeight = newMinHeight;
}
- (float)maxHeight
{
return fMaxHeight;
}
- (void)setMaxHeight:(float)newMaxHeight
{
fMaxHeight = newMaxHeight;
}
@end