2001-12-17 16:51:51 +00:00
|
|
|
/** <title>NSTextStorage</title>
|
1999-03-09 20:38:22 +00:00
|
|
|
|
|
|
|
Copyright (C) 1999 Free Software Foundation, Inc.
|
|
|
|
|
2001-12-17 16:51:51 +00:00
|
|
|
Author: Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
1999-03-09 20:38:22 +00:00
|
|
|
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 Library 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
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; see the file COPYING.LIB.
|
|
|
|
If not, write to the Free Software Foundation,
|
2005-05-26 02:52:46 +00:00
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
1999-03-09 20:38:22 +00:00
|
|
|
*/
|
|
|
|
|
2003-01-26 19:21:40 +00:00
|
|
|
#include <Foundation/NSNotification.h>
|
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
#include <Foundation/NSDebug.h>
|
2003-06-13 15:01:12 +00:00
|
|
|
#include "AppKit/NSAttributedString.h"
|
|
|
|
#include "AppKit/NSTextStorage.h"
|
2003-07-31 23:52:10 +00:00
|
|
|
#include "GNUstepGUI/GSLayoutManager.h"
|
2005-01-21 21:43 Alexander Malmberg <alexander@malmberg.org>
Various whitespace cleanups, comment type fixes, and changes
to avoid warnings from recent versions of gcc.
* Headers/Additions/GNUstepGUI/GSToolbar.h (-_toolbars): Declare.
* Source/NSWindow+Toolbar.m: Remove conflicting declaration of
[NSToolbar -_toolbars].
* Headers/Additions/GNUstepGUI/GSServicesManager.h,
Source/GSServicesMananger.m (-item2title:, -validateMenuItem:):
Adjust argument types.
* Headers/AppKit/NSMenu.h (-validateMenuItem:): Adjust argument
type.
* Source/NSTextView.m (-sizeToFit): Don't use size uninitialized
if neither resizable flags is set.
(-insertText:): Adjust argument type.
* Headers/AppKit/NSResponder.h, Source/NSResponder.m (-insertText:):
Adjust argument type. Document.
* Headers/AppKit/NSView.h: Change type of ivar _window to NSWindow *.
* Source/GSTitleView.m (-mouseDown:): Always initialize
startWindowOrigin.
* Source/NSApplication.m (-setApplicationIconImage:): Add casts
to avoid warnings.
* Source/NSCell.m (-cellSize): Add default: case.
* Source/NSPasteboard.m
([GSFiltered -pasteboard:provideDataForType:]): Detect and warn if we
can't find a filter that will get us the desired type.
* Source/NSProgressIndicator.m: Comment out unused variable 'images'.
* Source/NSBezierPath.m: Declare GSBezierPath fully before using it.
(-bezierPathByFlatteningPath, -bezierPathByReversingPath): Make sure
variables are always initialized.
* Source/NSMenuView.m,
* Source/NSPrintOperation.m,
* Source/NSSplitView.m,
* Source/NSTableHeaderView.m: Make sure variables are always
initialized.
* Source/NSBox.m,
* Source/NSImageview.m,
* Source/NSText.m,
* Source/NSTextStorage.m: Add missing includes.
* Source/GSKeyBindingTable.m,
* Source/GSLayoutManager.m,
* Source/NSBitmapImageRep+PNM.m,
* Source/NSBundleAdditions.m,
* Source/NSLayoutManager.m,
* Source/nsimage-tiff.h,
* Source/tiff.m,
* Headers/Additions/GNUstepGUI/GSDisplayServer.h,
* Source/GSDisplayServer.m: Change signedness of various variables.
* Source/NSPanel.m (-sendEvent:): Remove.
* Source/NSWindow.m (-becomesKeyOnlyIfNeeded): New method.
(-_sendEvent:becomesKeyOnlyIfNeeded:): Remove. Move code ...
(-sendEvent:): ... here. Use -becomesKeyOnlyIfNeeded instead
of the argument.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@20590 72102866-910b-0410-8b05-ffd578937521
2005-01-21 20:39:18 +00:00
|
|
|
#include "GSTextStorage.h"
|
1999-03-09 20:38:22 +00:00
|
|
|
|
|
|
|
@implementation NSTextStorage
|
|
|
|
|
1999-07-15 05:52:55 +00:00
|
|
|
static Class abstract;
|
|
|
|
static Class concrete;
|
|
|
|
|
2002-09-24 01:32:42 +00:00
|
|
|
static NSNotificationCenter *nc = nil;
|
|
|
|
|
1999-07-15 05:52:55 +00:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (self == [NSTextStorage class])
|
|
|
|
{
|
|
|
|
abstract = self;
|
|
|
|
concrete = [GSTextStorage class];
|
2002-09-24 01:32:42 +00:00
|
|
|
nc = [NSNotificationCenter defaultCenter];
|
1999-07-15 05:52:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) allocWithZone: (NSZone*)zone
|
|
|
|
{
|
|
|
|
if (self == abstract)
|
|
|
|
return NSAllocateObject(concrete, 0, zone);
|
|
|
|
else
|
|
|
|
return NSAllocateObject(self, 0, zone);
|
|
|
|
}
|
|
|
|
|
1999-03-09 20:38:22 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
2000-12-16 02:21:59 +00:00
|
|
|
RELEASE (_layoutManagers);
|
2002-09-24 01:32:42 +00:00
|
|
|
if (_delegate != nil)
|
|
|
|
{
|
|
|
|
[nc removeObserver: _delegate name: nil object: self];
|
|
|
|
_delegate = nil;
|
|
|
|
}
|
1999-03-09 20:38:22 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
1999-07-15 05:52:55 +00:00
|
|
|
/*
|
|
|
|
* The designated intialiser
|
|
|
|
*/
|
|
|
|
- (id) initWithString: (NSString*)aString
|
|
|
|
attributes: (NSDictionary*)attributes
|
1999-03-09 20:38:22 +00:00
|
|
|
{
|
2000-12-16 02:21:59 +00:00
|
|
|
_layoutManagers = [[NSMutableArray alloc] initWithCapacity: 2];
|
1999-03-09 20:38:22 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
1999-07-25 03:34:10 +00:00
|
|
|
/*
|
|
|
|
* Return a string
|
|
|
|
*/
|
|
|
|
|
|
|
|
- (NSString*) string
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
1999-03-09 20:38:22 +00:00
|
|
|
/*
|
2003-01-26 19:21:40 +00:00
|
|
|
* Managing GSLayoutManagers
|
1999-03-09 20:38:22 +00:00
|
|
|
*/
|
2003-01-26 19:21:40 +00:00
|
|
|
- (void) addLayoutManager: (GSLayoutManager*)obj
|
1999-03-09 20:38:22 +00:00
|
|
|
{
|
2000-12-16 02:21:59 +00:00
|
|
|
if ([_layoutManagers indexOfObjectIdenticalTo: obj] == NSNotFound)
|
1999-08-19 23:18:25 +00:00
|
|
|
{
|
2000-12-16 02:21:59 +00:00
|
|
|
[_layoutManagers addObject: obj];
|
|
|
|
[obj setTextStorage: self];
|
1999-08-19 23:18:25 +00:00
|
|
|
}
|
1999-03-09 20:38:22 +00:00
|
|
|
}
|
|
|
|
|
2003-01-26 19:21:40 +00:00
|
|
|
- (void) removeLayoutManager: (GSLayoutManager*)obj
|
1999-03-09 20:38:22 +00:00
|
|
|
{
|
2002-10-08 17:58:09 +00:00
|
|
|
[obj setTextStorage: nil];
|
2000-12-16 02:21:59 +00:00
|
|
|
[_layoutManagers removeObjectIdenticalTo: obj];
|
1999-03-09 20:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray*) layoutManagers
|
|
|
|
{
|
2000-12-16 02:21:59 +00:00
|
|
|
return _layoutManagers;
|
1999-03-09 20:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) beginEditing
|
|
|
|
{
|
2000-12-16 02:21:59 +00:00
|
|
|
_editCount++;
|
1999-03-09 20:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) endEditing
|
|
|
|
{
|
2000-12-16 02:21:59 +00:00
|
|
|
if (_editCount == 0)
|
|
|
|
{
|
|
|
|
[NSException raise: NSGenericException
|
|
|
|
format: @"endEditing without corresponding beginEditing"];
|
|
|
|
}
|
|
|
|
if (--_editCount == 0)
|
1999-03-09 20:38:22 +00:00
|
|
|
{
|
|
|
|
[self processEditing];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there are no outstanding beginEditing calls, this method calls
|
|
|
|
* processEditing to cause post-editing stuff to happen. This method
|
|
|
|
* has to be called by the primitives after changes are made.
|
|
|
|
* The range argument to edited:... is the range in the original string
|
|
|
|
* (before the edit).
|
|
|
|
*/
|
|
|
|
- (void) edited: (unsigned)mask range: (NSRange)old changeInLength: (int)delta
|
|
|
|
{
|
1999-07-25 20:49:08 +00:00
|
|
|
|
1999-09-09 02:56:20 +00:00
|
|
|
NSDebugLLog(@"NSText", @"edited:range:changeInLength: called");
|
1999-07-25 20:49:08 +00:00
|
|
|
|
1999-03-09 20:38:22 +00:00
|
|
|
/*
|
|
|
|
* Add in any new flags for this edit.
|
|
|
|
*/
|
2000-12-16 02:21:59 +00:00
|
|
|
_editedMask |= mask;
|
1999-03-09 20:38:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Extend edited range to encompass the latest edit.
|
|
|
|
*/
|
2000-12-16 02:21:59 +00:00
|
|
|
if (_editedRange.length == 0)
|
1999-03-09 20:38:22 +00:00
|
|
|
{
|
2000-12-16 02:21:59 +00:00
|
|
|
_editedRange = old; // First edit.
|
1999-03-09 20:38:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-12-16 02:21:59 +00:00
|
|
|
_editedRange = NSUnionRange (_editedRange, old);
|
1999-03-09 20:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the number of characters has been increased or decreased -
|
|
|
|
* adjust the delta accordingly.
|
|
|
|
*/
|
|
|
|
if ((mask & NSTextStorageEditedCharacters) && delta)
|
|
|
|
{
|
|
|
|
if (delta < 0)
|
|
|
|
{
|
2003-06-23 15:50:46 +00:00
|
|
|
NSAssert (old.length >= (unsigned)-delta, NSInvalidArgumentException);
|
1999-03-09 20:38:22 +00:00
|
|
|
}
|
2000-12-16 02:21:59 +00:00
|
|
|
_editedRange.length += delta;
|
|
|
|
_editedDelta += delta;
|
1999-03-09 20:38:22 +00:00
|
|
|
}
|
|
|
|
|
2000-12-16 02:21:59 +00:00
|
|
|
if (_editCount == 0)
|
1999-03-09 20:38:22 +00:00
|
|
|
[self processEditing];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is called from edited:range:changeInLength: or endEditing.
|
|
|
|
* This method sends out NSTextStorageWillProcessEditing, then fixes
|
|
|
|
* the attributes, then sends out NSTextStorageDidProcessEditing,
|
|
|
|
* and finally notifies the layout managers of change with the
|
|
|
|
* textStorage:edited:range:changeInLength:invalidatedRange: method.
|
|
|
|
*/
|
|
|
|
- (void) processEditing
|
|
|
|
{
|
|
|
|
NSRange r;
|
2003-02-11 15:39:49 +00:00
|
|
|
int original_delta;
|
2003-06-23 15:50:46 +00:00
|
|
|
unsigned int i;
|
2000-12-16 02:21:59 +00:00
|
|
|
unsigned length;
|
1999-03-09 20:38:22 +00:00
|
|
|
|
1999-09-09 02:56:20 +00:00
|
|
|
NSDebugLLog(@"NSText", @"processEditing called in NSTextStorage.");
|
1999-07-25 04:58:39 +00:00
|
|
|
|
2000-03-23 11:31:25 +00:00
|
|
|
/*
|
2000-12-16 02:21:59 +00:00
|
|
|
* The _editCount gets decreased later again, so that changes by the
|
|
|
|
* delegate or by ourselves when we fix attributes dont trigger a
|
|
|
|
* new processEditing */
|
|
|
|
_editCount++;
|
1999-03-09 20:38:22 +00:00
|
|
|
[nc postNotificationName: NSTextStorageWillProcessEditingNotification
|
|
|
|
object: self];
|
|
|
|
|
2000-12-16 02:21:59 +00:00
|
|
|
/* Very important: we save the current _editedRange */
|
|
|
|
r = _editedRange;
|
2003-02-11 15:39:49 +00:00
|
|
|
original_delta = _editedDelta;
|
2000-12-16 02:21:59 +00:00
|
|
|
length = [self length];
|
2000-06-16 17:05:28 +00:00
|
|
|
// Multiple adds at the end might give a too long result
|
2000-12-16 02:21:59 +00:00
|
|
|
if (NSMaxRange(r) > length)
|
2001-08-30 20:14:50 +00:00
|
|
|
{
|
2000-12-16 02:21:59 +00:00
|
|
|
r.length = length - r.location;
|
2001-08-30 20:14:50 +00:00
|
|
|
}
|
|
|
|
|
2000-12-16 02:21:59 +00:00
|
|
|
/* The following call will potentially fix attributes. These changes
|
|
|
|
are done through NSTextStorage methods, which records the changes
|
|
|
|
by calling edited:range:changeInLength: - which modifies editedRange.
|
|
|
|
|
|
|
|
As a consequence, if any attribute has been fixed, r !=
|
|
|
|
editedRange after this call. This is why we saved r in the first
|
|
|
|
place. */
|
2001-11-23 00:27:53 +00:00
|
|
|
[self invalidateAttributesInRange: r];
|
1999-03-09 20:38:22 +00:00
|
|
|
|
|
|
|
[nc postNotificationName: NSTextStorageDidProcessEditingNotification
|
|
|
|
object: self];
|
2000-12-16 02:21:59 +00:00
|
|
|
_editCount--;
|
1999-07-25 03:34:10 +00:00
|
|
|
|
2003-02-11 15:39:49 +00:00
|
|
|
/*
|
|
|
|
The attribute fixes might have added or removed characters. We must make
|
|
|
|
sure that range and delta we give to the layout managers is valid.
|
|
|
|
*/
|
|
|
|
if (original_delta != _editedDelta)
|
|
|
|
{
|
|
|
|
if (_editedDelta - original_delta > 0)
|
|
|
|
{
|
|
|
|
r.length += _editedDelta - original_delta;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-06-23 15:50:46 +00:00
|
|
|
if ((unsigned)(original_delta - _editedDelta) > r.length)
|
2003-02-11 15:39:49 +00:00
|
|
|
{
|
|
|
|
r.length = 0;
|
|
|
|
if (r.location > [self length])
|
|
|
|
r.location = [self length];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
r.length += _editedDelta - original_delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-07-25 03:34:10 +00:00
|
|
|
/*
|
|
|
|
* Calls textStorage:edited:range:changeInLength:invalidatedRange: for
|
|
|
|
* every layoutManager.
|
|
|
|
*/
|
|
|
|
|
2000-12-16 02:21:59 +00:00
|
|
|
for (i = 0; i < [_layoutManagers count]; i++)
|
1999-07-25 03:34:10 +00:00
|
|
|
{
|
2003-01-26 19:21:40 +00:00
|
|
|
GSLayoutManager *lManager = [_layoutManagers objectAtIndex: i];
|
1999-07-25 03:34:10 +00:00
|
|
|
|
2000-12-16 02:21:59 +00:00
|
|
|
[lManager textStorage: self edited: _editedMask range: r
|
|
|
|
changeInLength: _editedDelta invalidatedRange: _editedRange];
|
1999-07-25 03:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-07-25 20:49:08 +00:00
|
|
|
* edited values reset to be used again in the next pass.
|
1999-07-25 03:34:10 +00:00
|
|
|
*/
|
|
|
|
|
2000-12-16 02:21:59 +00:00
|
|
|
_editedRange = NSMakeRange (0, 0);
|
|
|
|
_editedDelta = 0;
|
|
|
|
_editedMask = 0;
|
1999-03-09 20:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These methods return information about the editing status.
|
|
|
|
* Especially useful when there are outstanding beginEditing calls or
|
|
|
|
* during processEditing... editedRange.location will be NSNotFound if
|
|
|
|
* nothing has been edited.
|
|
|
|
*/
|
|
|
|
- (unsigned) editedMask
|
|
|
|
{
|
2000-12-16 02:21:59 +00:00
|
|
|
return _editedMask;
|
1999-03-09 20:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSRange) editedRange
|
|
|
|
{
|
2000-12-16 02:21:59 +00:00
|
|
|
return _editedRange;
|
1999-03-09 20:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (int) changeInLength
|
|
|
|
{
|
2000-12-16 02:21:59 +00:00
|
|
|
return _editedDelta;
|
1999-03-09 20:38:22 +00:00
|
|
|
}
|
|
|
|
|
2002-10-13 10:26:57 +00:00
|
|
|
/**
|
|
|
|
* Set the delegate (adds it as an observer for text storage notifications)
|
|
|
|
* and removes any old value (removes it as an observer).<br />
|
|
|
|
* The delegate is <em>not</em> retained.
|
1999-03-09 20:38:22 +00:00
|
|
|
*/
|
2002-10-13 10:26:57 +00:00
|
|
|
- (void) setDelegate: (id)delegate
|
1999-03-09 20:38:22 +00:00
|
|
|
{
|
2002-10-13 10:26:57 +00:00
|
|
|
if (_delegate != nil)
|
|
|
|
{
|
|
|
|
[nc removeObserver: _delegate name: nil object: self];
|
|
|
|
}
|
|
|
|
_delegate = delegate;
|
1999-03-09 20:38:22 +00:00
|
|
|
|
|
|
|
#define SET_DELEGATE_NOTIFICATION(notif_name) \
|
2000-12-16 02:21:59 +00:00
|
|
|
if ([_delegate respondsToSelector: @selector(textStorage##notif_name:)]) \
|
|
|
|
[nc addObserver: _delegate \
|
1999-03-09 20:38:22 +00:00
|
|
|
selector: @selector(textStorage##notif_name:) \
|
|
|
|
name: NSTextStorage##notif_name##Notification object: self]
|
|
|
|
|
|
|
|
SET_DELEGATE_NOTIFICATION(DidProcessEditing);
|
|
|
|
SET_DELEGATE_NOTIFICATION(WillProcessEditing);
|
|
|
|
}
|
|
|
|
|
2002-10-13 10:26:57 +00:00
|
|
|
/**
|
|
|
|
* Returns the value most recently set usiong the -setDelegate: method.
|
|
|
|
*/
|
1999-03-09 20:38:22 +00:00
|
|
|
- (id) delegate
|
|
|
|
{
|
2000-12-16 02:21:59 +00:00
|
|
|
return _delegate;
|
1999-03-09 20:38:22 +00:00
|
|
|
}
|
|
|
|
|
2001-11-23 00:27:53 +00:00
|
|
|
- (void) ensureAttributesAreFixedInRange: (NSRange)range
|
|
|
|
{
|
|
|
|
// Do nothing as the default is not lazy fixing, so all is done already
|
|
|
|
}
|
1999-03-09 20:38:22 +00:00
|
|
|
|
2001-11-23 00:27:53 +00:00
|
|
|
- (BOOL) fixesAttributesLazily
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
1999-03-09 20:38:22 +00:00
|
|
|
|
2001-11-23 00:27:53 +00:00
|
|
|
- (void) invalidateAttributesInRange: (NSRange)range
|
|
|
|
{
|
|
|
|
[self fixAttributesInRange: range];
|
|
|
|
}
|
1999-03-09 20:38:22 +00:00
|
|
|
|
2004-02-15 18:23:13 +00:00
|
|
|
- (id) initWithCoder: (NSCoder*)aDecoder
|
|
|
|
{
|
|
|
|
if ([aDecoder allowsKeyedCoding])
|
|
|
|
{
|
|
|
|
id delegate = [aDecoder decodeObjectForKey: @"NSDelegate"];
|
|
|
|
NSString *string = [aDecoder decodeObjectForKey: @"NSString"];
|
|
|
|
|
|
|
|
self = [self initWithString: string];
|
|
|
|
[self setDelegate: delegate];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
self = [super initWithCoder: aDecoder];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2006-09-03 16:41:10 +00:00
|
|
|
- (void) encodeWithCoder: (NSCoder *)coder
|
|
|
|
{
|
|
|
|
if([coder allowsKeyedCoding])
|
|
|
|
{
|
|
|
|
[coder encodeObject: [self delegate] forKey: @"NSDelegate"];
|
|
|
|
[coder encodeObject: [self string] forKey: @"NSString"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[super encodeWithCoder: coder];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-23 00:27:53 +00:00
|
|
|
@end
|