File: NSTextStorage.m

package info (click to toggle)
gnustep-gui 0.25.0-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,088 kB
  • ctags: 3,417
  • sloc: objc: 153,800; ansic: 18,239; cpp: 579; yacc: 462; makefile: 143; sh: 5
file content (401 lines) | stat: -rw-r--r-- 9,569 bytes parent folder | download | duplicates (2)
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
/** <title>NSTextStorage</title>

   Copyright (C) 1999 Free Software Foundation, Inc.

   Author: Richard Frith-Macdonald <richard@brainstorm.co.uk>
   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/NSNotification.h>
#import <Foundation/NSException.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSPortCoder.h>
#import "AppKit/NSAttributedString.h"
#import "AppKit/NSTextStorage.h"
#import "GNUstepGUI/GSLayoutManager.h"
#import "GSTextStorage.h"

@implementation NSTextStorage

static Class abstract;
static Class concrete;

static NSNotificationCenter *nc = nil;

+ (void) initialize
{
  if (self == [NSTextStorage class])
    {
      abstract = self;
      concrete = [GSTextStorage class];
      nc = [NSNotificationCenter defaultCenter];
    }
}

+ (id) allocWithZone: (NSZone*)zone
{
  if (self == abstract)
    return NSAllocateObject(concrete, 0, zone);
  else
    return NSAllocateObject(self, 0, zone);
}

- (void) dealloc
{
  [self setDelegate: nil];
  [_layoutManagers makeObjectsPerformSelector: @selector(setTextStorage:)
				   withObject: nil];
  RELEASE (_layoutManagers);
  [super dealloc];
}

/*
 *	The designated intialiser
 */
- (id) initWithString: (NSString*)aString
           attributes: (NSDictionary*)attributes
{
  _layoutManagers = [[NSMutableArray alloc] initWithCapacity: 2];
  return self;
}

/*
 * Return a string
 */

- (NSString*) string
{
  [self subclassResponsibility: _cmd];
  return nil;
}

/*
 *	Managing GSLayoutManagers
 */
- (void) addLayoutManager: (GSLayoutManager*)obj
{
  if ([_layoutManagers indexOfObjectIdenticalTo: obj] == NSNotFound)
    {
      [_layoutManagers addObject: obj];
      [obj setTextStorage: self];
    }
}

- (void) removeLayoutManager: (GSLayoutManager*)obj
{
  /* If the receiver belongs to a text network that is owned by its text view
     it could get deallocated by the call to -setTextStorage:. To prevent
     crashes we retain the receiver until the end of this method. */
  RETAIN(self);
  [obj setTextStorage: nil];
  [_layoutManagers removeObjectIdenticalTo: obj];
  RELEASE(self);
}

- (NSArray*) layoutManagers
{
  return _layoutManagers;
}

- (void) beginEditing
{
  _editCount++;
}

- (void) endEditing
{
  if (_editCount == 0)
    {
      [NSException raise: NSGenericException
		   format: @"endEditing without corresponding beginEditing"];
    }
  if (--_editCount == 0)
    {
      [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
{

  NSDebugLLog(@"NSText", @"edited:range:changeInLength: called");

  /*
   * Extend edited range to encompass the latest edit.
   */
  if (_editedMask == 0)
    {
      _editedRange = old;		// First edit.
    }
  else
    {
      _editedRange = NSUnionRange (_editedRange, old);
    }

  /*
   * Add in any new flags for this edit.
   */
  _editedMask |= mask;

  /*
   * If the number of characters has been increased or decreased -
   * adjust the delta accordingly.
   */
  if ((mask & NSTextStorageEditedCharacters) && delta)
    {
      if (delta < 0)
	{
	  NSAssert (old.length >= (unsigned)-delta, NSInvalidArgumentException);
	}
      _editedRange.length += delta; 
      _editedDelta += delta;
    }

  if (_editCount == 0)
    [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;
  int original_delta;
  unsigned int i;
  unsigned length;

  NSDebugLLog(@"NSText", @"processEditing called in NSTextStorage.");

  /*
   * 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++;
  [nc postNotificationName: NSTextStorageWillProcessEditingNotification
		    object: self];

  /* Very important: we save the current _editedRange */
  r = _editedRange;
  original_delta = _editedDelta;
  length = [self length];
  // Multiple adds at the end might give a too long result
  if (NSMaxRange(r) > length)
    {
      r.length = length - r.location;
    }
  
  /* 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. */
  [self invalidateAttributesInRange: r];

  [nc postNotificationName: NSTextStorageDidProcessEditingNotification
                    object: self];
  _editCount--;

  /*
  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
	{
	  if ((unsigned)(original_delta - _editedDelta) > r.length)
	    {
	      r.length = 0;
	      if (r.location > [self length])
		r.location = [self length];
	    }
	  else
	    {
	      r.length += _editedDelta - original_delta;
	    }
	}
    }

  /*
   * Calls textStorage:edited:range:changeInLength:invalidatedRange: for
   * every layoutManager.
   */

  for (i = 0; i < [_layoutManagers count]; i++)
    {
      GSLayoutManager *lManager = [_layoutManagers objectAtIndex: i];

      [lManager textStorage: self  edited: _editedMask  range: r
		changeInLength: _editedDelta  invalidatedRange: _editedRange];
    }

  /*
   * edited values reset to be used again in the next pass.
   */

  _editedRange = NSMakeRange (0, 0);
  _editedDelta = 0;
  _editedMask = 0;
}

/*
 *	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
{
  return _editedMask;
}

- (NSRange) editedRange
{
  return _editedRange;
}

- (int) changeInLength
{
  return _editedDelta;
}

/**
 * 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.
 */
- (void) setDelegate: (id)delegate
{
  if (_delegate != nil)
    {
      [nc removeObserver: _delegate  name: nil  object: self];
    }
  _delegate = delegate;

#define SET_DELEGATE_NOTIFICATION(notif_name) \
  if ([_delegate respondsToSelector: @selector(textStorage##notif_name:)]) \
    [nc addObserver: _delegate \
	   selector: @selector(textStorage##notif_name:) \
	       name: NSTextStorage##notif_name##Notification object: self]

  SET_DELEGATE_NOTIFICATION(DidProcessEditing);
  SET_DELEGATE_NOTIFICATION(WillProcessEditing);
}

/**
 * Returns the value most recently set usiong the -setDelegate: method.
 */
- (id) delegate
{
  return _delegate;
}

- (void) ensureAttributesAreFixedInRange: (NSRange)range
{
  // Do nothing as the default is not lazy fixing, so all is done already
}

- (BOOL) fixesAttributesLazily
{
  return NO;
}

- (void) invalidateAttributesInRange: (NSRange)range
{
  [self fixAttributesInRange: range];
}

- (Class) classForCoder
{
  return abstract;
}

- (id) replacementObjectForPortCoder: (NSPortCoder*)aCoder
{
  return self;
}

- (id) initWithCoder: (NSCoder*)aDecoder
{
  self = [super initWithCoder: aDecoder];
  if ([aDecoder allowsKeyedCoding])
    {
      id delegate = [aDecoder decodeObjectForKey: @"NSDelegate"];

      [self setDelegate: delegate];
    }
  else
    {
    }
     
  return self;
}

- (void) encodeWithCoder: (NSCoder *)coder
{
  [super encodeWithCoder: coder];
  if ([coder allowsKeyedCoding])
    {
      [coder encodeObject: [self delegate] forKey: @"NSDelegate"];
    }
  else
    {
    }
}

@end

@implementation NSTextStorage (Scripting)

- (NSFont*) font
{
  return [self attribute: NSFontAttributeName atIndex: 0 effectiveRange: NULL];
}

- (void) setFont: (NSFont*)font
{
  if (font != nil)
    {
      [self addAttribute: NSFontAttributeName
		   value: font
		   range: NSMakeRange(0, [self length])];
    }
}

@end