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
|
/** <title>NSTextContainer</title>
Copyright (C) 1999 Free Software Foundation, Inc.
Author: Alexander Malmberg <alexander@malmberg.org>
Date: 2002-11-23
Author: Jonathan Gapen <jagapen@smithlab.chem.wisc.edu>
Date: 1999
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; see the file COPYING.LIB.
If not, see <http://www.gnu.org/licenses/> or write to the
Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#import <Foundation/NSGeometry.h>
#import <Foundation/NSNotification.h>
#import <Foundation/NSDebug.h>
#import "AppKit/NSLayoutManager.h"
#import "AppKit/NSTextContainer.h"
#import "AppKit/NSTextStorage.h"
#import "AppKit/NSTextView.h"
#import "GNUstepGUI/GSLayoutManager.h"
#import "GSGuiPrivate.h"
@interface NSTextContainer (TextViewObserver)
- (void) _textViewFrameChanged: (NSNotification*)aNotification;
@end
/* TODO: rethink how this is is triggered.
use bounds rectangle instead of frame? */
@implementation NSTextContainer (TextViewObserver)
- (void) _textViewFrameChanged: (NSNotification*)aNotification
{
if (_observingFrameChanges)
{
id textView;
NSSize newTextViewSize;
NSSize size;
NSSize inset;
textView = [aNotification object];
if (textView != _textView)
{
NSDebugLog(@"NSTextContainer got notification for wrong View %@",
textView);
return;
}
newTextViewSize = [textView frame].size;
size = _containerRect.size;
inset = [textView textContainerInset];
if (_widthTracksTextView)
{
size.width = MAX(newTextViewSize.width - (inset.width * 2.0), 0.0);
}
if (_heightTracksTextView)
{
size.height = MAX(newTextViewSize.height - (inset.height * 2.0), 0.0);
}
[self setContainerSize: size];
}
}
@end /* NSTextContainer (TextViewObserver) */
@implementation NSTextContainer
+ (void) initialize
{
if (self == [NSTextContainer class])
{
[self setVersion: 1];
}
}
- (id) initWithContainerSize: (NSSize)aSize
{
NSDebugLLog(@"NSText", @"NSTextContainer initWithContainerSize");
if (aSize.width < 0)
{
NSWarnMLog(@"given negative width");
aSize.width = 0;
}
if (aSize.height < 0)
{
NSWarnMLog(@"given negative height");
aSize.height = 0;
}
_layoutManager = nil;
_textView = nil;
_containerRect.size = aSize;
// Tests on Cocoa indicate the default value is 5.
_lineFragmentPadding = 5.0;
_observingFrameChanges = NO;
_widthTracksTextView = NO;
_heightTracksTextView = NO;
return self;
}
- (id) init
{
return [self initWithContainerSize: NSMakeSize(1e7, 1e7)];
}
- (void) dealloc
{
if (_textView != nil)
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver: self
name: NSViewFrameDidChangeNotification
object: _textView];
[_textView setTextContainer: nil];
RELEASE(_textView);
}
[super dealloc];
}
/*
See [NSTextView -setTextContainer:] for more information about these calls.
*/
- (void) setLayoutManager: (GSLayoutManager*)aLayoutManager
{
/* The layout manager owns us - so he retains us and we don't retain
him. */
_layoutManager = aLayoutManager;
/* Tell our text view about the change. */
[_textView setTextContainer: self];
}
- (GSLayoutManager*) layoutManager
{
return _layoutManager;
}
/*
Replaces the layout manager while maintaining the text object
framework intact.
*/
- (void) replaceLayoutManager: (GSLayoutManager*)aLayoutManager
{
if (aLayoutManager != _layoutManager)
{
NSTextStorage *textStorage = [_layoutManager textStorage];
NSArray *textContainers = [_layoutManager textContainers];
NSUInteger i, count = [textContainers count];
GSLayoutManager *oldLayoutManager = _layoutManager;
RETAIN(oldLayoutManager);
RETAIN(textStorage);
[textStorage removeLayoutManager: _layoutManager];
[textStorage addLayoutManager: aLayoutManager];
for (i = 0; i < count; i++)
{
NSTextContainer *container;
container = RETAIN([textContainers objectAtIndex: i]);
[oldLayoutManager removeTextContainerAtIndex: i];
/* One of these calls will result in our _layoutManager being
changed. */
[aLayoutManager addTextContainer: container];
/* The textview is caching the layout manager; refresh the
* cache with this do-nothing call. */
/* TODO: probably unnecessary; the call in -setLayoutManager:
should be enough */
[[container textView] setTextContainer: container];
RELEASE(container);
}
RELEASE(textStorage);
RELEASE(oldLayoutManager);
}
}
- (void) setTextView: (NSTextView*)aTextView
{
NSNotificationCenter *nc;
nc = [NSNotificationCenter defaultCenter];
if (_textView)
{
[_textView setTextContainer: nil];
[nc removeObserver: self name: NSViewFrameDidChangeNotification
object: _textView];
/* NB: We do not set posts frame change notifications for the
text view to NO because there could be other observers for
the frame change notifications. */
}
ASSIGN(_textView, aTextView);
if (aTextView != nil)
{
[_textView setTextContainer: self];
if (_observingFrameChanges)
{
[_textView setPostsFrameChangedNotifications: YES];
[nc addObserver: self
selector: @selector(_textViewFrameChanged:)
name: NSViewFrameDidChangeNotification
object: _textView];
[self _textViewFrameChanged:
[NSNotification notificationWithName: NSViewFrameDidChangeNotification
object: _textView]];
}
}
/* If someone's trying to set a NSTextView for us, the layout manager we
have must be capable of handling NSTextView:s. */
[(NSLayoutManager *)_layoutManager textContainerChangedTextView: self];
}
- (NSTextView*) textView
{
return _textView;
}
- (void) setContainerSize: (NSSize)aSize
{
if (NSEqualSizes(_containerRect.size, aSize))
{
return;
}
if (aSize.width < 0)
{
NSWarnMLog(@"given negative width");
aSize.width = 0;
}
if (aSize.height < 0)
{
NSWarnMLog(@"given negative height");
aSize.height = 0;
}
_containerRect = NSMakeRect(0, 0, aSize.width, aSize.height);
if (_layoutManager)
{
[_layoutManager textContainerChangedGeometry: self];
}
}
- (NSSize) containerSize
{
return _containerRect.size;
}
- (void) setWidthTracksTextView: (BOOL)flag
{
NSNotificationCenter *nc;
BOOL old_observing = _observingFrameChanges;
_widthTracksTextView = flag;
_observingFrameChanges = _widthTracksTextView | _heightTracksTextView;
if (_textView == nil)
return;
if (_observingFrameChanges == old_observing)
return;
nc = [NSNotificationCenter defaultCenter];
if (_observingFrameChanges)
{
[_textView setPostsFrameChangedNotifications: YES];
[nc addObserver: self
selector: @selector(_textViewFrameChanged:)
name: NSViewFrameDidChangeNotification
object: _textView];
}
else
{
[nc removeObserver: self name: NSViewFrameDidChangeNotification
object: _textView];
}
}
- (BOOL) widthTracksTextView
{
return _widthTracksTextView;
}
- (void) setHeightTracksTextView: (BOOL)flag
{
NSNotificationCenter *nc;
BOOL old_observing = _observingFrameChanges;
_heightTracksTextView = flag;
_observingFrameChanges = _widthTracksTextView | _heightTracksTextView;
if (_textView == nil)
return;
if (_observingFrameChanges == old_observing)
return;
nc = [NSNotificationCenter defaultCenter];
if (_observingFrameChanges)
{
[_textView setPostsFrameChangedNotifications: YES];
[nc addObserver: self
selector: @selector(_textViewFrameChanged:)
name: NSViewFrameDidChangeNotification
object: _textView];
}
else
{
[nc removeObserver: self name: NSViewFrameDidChangeNotification
object: _textView];
}
}
- (BOOL) heightTracksTextView
{
return _heightTracksTextView;
}
- (void) setLineFragmentPadding: (CGFloat)aFloat
{
_lineFragmentPadding = aFloat;
if (_layoutManager)
[_layoutManager textContainerChangedGeometry: self];
}
- (CGFloat) lineFragmentPadding
{
return _lineFragmentPadding;
}
- (NSRect) lineFragmentRectForProposedRect: (NSRect)proposedRect
sweepDirection: (NSLineSweepDirection)sweepDir
movementDirection: (NSLineMovementDirection)moveDir
remainingRect: (NSRect *)remainingRect
{
CGFloat minx, maxx, miny, maxy;
CGFloat cminx, cmaxx, cminy, cmaxy;
minx = NSMinX(proposedRect);
maxx = NSMaxX(proposedRect);
miny = NSMinY(proposedRect);
maxy = NSMaxY(proposedRect);
cminx = NSMinX(_containerRect) + _lineFragmentPadding;
cmaxx = NSMaxX(_containerRect) - _lineFragmentPadding;
cminy = NSMinY(_containerRect);
cmaxy = NSMaxY(_containerRect);
*remainingRect = NSZeroRect;
if (minx >= cminx && maxx <= cmaxx && miny >= cminy && maxy <= cmaxy)
{
return proposedRect;
}
switch (moveDir)
{
case NSLineMovesLeft:
if (maxx < cminx)
return NSZeroRect;
if (maxx > cmaxx)
{
minx -= maxx-cmaxx;
maxx = cmaxx;
}
break;
case NSLineMovesRight:
if (minx > cmaxx)
return NSZeroRect;
if (minx < cminx)
{
maxx += cminx-minx;
minx = cminx;
}
break;
case NSLineMovesDown:
if (miny > cmaxy)
return NSZeroRect;
if (miny < cminy)
{
maxy += cminy - miny;
miny = cminy;
}
break;
case NSLineMovesUp:
if (maxy < cminy)
return NSZeroRect;
if (maxy > cmaxy)
{
miny -= maxy - cmaxy;
maxy = cmaxy;
}
break;
case NSLineDoesntMove:
break;
}
switch (sweepDir)
{
case NSLineSweepLeft:
case NSLineSweepRight:
if (minx < cminx)
minx = cminx;
if (maxx > cmaxx)
maxx = cmaxx;
break;
case NSLineSweepDown:
case NSLineSweepUp:
if (miny < cminy)
miny = cminy;
if (maxy > cmaxy)
maxy = cmaxy;
break;
}
if (minx < cminx || maxx > cmaxx ||
miny < cminy || maxy > cmaxy)
{
return NSZeroRect;
}
return NSMakeRect(minx, miny,
(maxx > minx) ? maxx - minx : 0.0,
(maxy > miny) ? maxy - miny : 0.0);
}
- (BOOL) isSimpleRectangularTextContainer
{
// sub-classes may say no; this class always says yes
return YES;
}
- (BOOL) containsPoint: (NSPoint)aPoint
{
return NSPointInRect(aPoint, _containerRect);
}
- (id) initWithCoder: (NSCoder*)aDecoder
{
if ([aDecoder allowsKeyedCoding])
{
NSSize size = NSMakeSize(1e7, 1e7);
if ([aDecoder containsValueForKey: @"NSWidth"])
{
size.width = [aDecoder decodeFloatForKey: @"NSWidth"];
}
self = [self initWithContainerSize: size];
if ([aDecoder containsValueForKey: @"NSTCFlags"])
{
int flags = [aDecoder decodeIntForKey: @"NSTCFlags"];
// decode the flags.
_widthTracksTextView = (flags & 1) != 0;
_heightTracksTextView = (flags & 2) != 0;
// Mac OS X doesn't seem to save this flag
_observingFrameChanges = _widthTracksTextView | _heightTracksTextView;
}
// decoding the manager adds this text container automatically...
if ([aDecoder containsValueForKey: @"NSLayoutManager"])
{
_layoutManager = [aDecoder decodeObjectForKey: @"NSLayoutManager"];
}
return self;
}
else
{
return self;
}
}
- (void) encodeWithCoder: (NSCoder *)coder
{
if ([coder allowsKeyedCoding])
{
NSSize size = _containerRect.size;
int flags = ((_widthTracksTextView)?1:0) |
((_heightTracksTextView)?2:0) |
((_observingFrameChanges)?4:0);
[coder encodeObject: _textView forKey: @"NSTextView"];
[coder encodeObject: _layoutManager forKey: @"NSLayoutManager"];
[coder encodeFloat: size.width forKey: @"NSWidth"];
[coder encodeInt: flags forKey: @"NSTCFlags"];
}
}
@end /* NSTextContainer */
|