Guest User

Untitled

a guest
Apr 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. //
  2. // TextView.m
  3. // TextDidChange
  4. //
  5. // Created by Timothy J. Wood on 10/27/09.
  6. // Copyright 2009 The Omni Group. All rights reserved.
  7. //
  8.  
  9.  
  10. /*
  11.  
  12. A button hooked up to -insertFoo: yields:
  13. 2009-10-27 13:18:58.784 TextDidChange[1801:a0b] insert range:{0, 0}
  14. 2009-10-27 13:18:58.786 TextDidChange[1801:a0b] in processEditing editedMask:2 editedRange:{0, 3} changeInLength:3
  15. 2009-10-27 13:18:58.787 TextDidChange[1801:a0b] rangeForUserTextChange = {3, 0}
  16. 2009-10-27 13:18:58.787 TextDidChange[1801:a0b] textStorage.editedRange = {9223372036854775807, 3}
  17.  
  18. copying & pasting that "foo" right after yields:
  19. 2009-10-27 13:19:02.425 TextDidChange[1801:a0b] in processEditing editedMask:3 editedRange:{3, 3} changeInLength:3
  20. 2009-10-27 13:19:02.426 TextDidChange[1801:a0b] rangeForUserTextChange = {6, 0}
  21. 2009-10-27 13:19:02.427 TextDidChange[1801:a0b] textStorage.editedRange = {9223372036854775807, 3}
  22.  
  23. then, selecting and deleting the first foo:
  24. 2009-10-27 13:19:48.929 TextDidChange[1801:a0b] in processEditing editedMask:2 editedRange:{0, 0} changeInLength:-3
  25. 2009-10-27 13:19:48.930 TextDidChange[1801:a0b] rangeForUserTextChange = {0, 0}
  26. 2009-10-27 13:19:48.930 TextDidChange[1801:a0b] textStorage.editedRange = {9223372036854775807, 0}
  27.  
  28. */
  29. #import "TextView.h"
  30.  
  31. @interface TextStorage : NSTextStorage
  32. {
  33. NSMutableAttributedString *_contents;
  34. }
  35. @end
  36. @implementation TextStorage
  37.  
  38. // NSTextStorage is abstract; minimal concrete subclass
  39. - init;
  40. {
  41. if (!(self = [super init]))
  42. return nil;
  43. _contents = [[NSMutableAttributedString alloc] initWithString:@"" attributes:nil];
  44. return self;
  45. }
  46.  
  47. - (void)dealloc;
  48. {
  49. [_contents release];
  50. [super dealloc];
  51. }
  52.  
  53. #pragma mark -
  54. #pragma NSAttributedString subclass
  55.  
  56. - (NSUInteger)length;
  57. {
  58. return [_contents length];
  59. }
  60.  
  61. - (NSString *)string;
  62. {
  63. return [_contents string];
  64. }
  65.  
  66. - (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range;
  67. {
  68. return [_contents attributesAtIndex:location effectiveRange:range];
  69. }
  70.  
  71. #pragma mark -
  72. #pragma NSMutableAttributedString subclass
  73.  
  74. - (void)beginEditing;
  75. {
  76. [super beginEditing];
  77. [_contents beginEditing];
  78. }
  79.  
  80. - (void)endEditing;
  81. {
  82. [_contents endEditing];
  83. [super endEditing];
  84. }
  85.  
  86. - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str;
  87. {
  88. [_contents replaceCharactersInRange:range withString:str];
  89. [self edited:NSTextStorageEditedCharacters range:range changeInLength:[str length] - range.length];
  90. }
  91.  
  92. - (void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attributedString;
  93. {
  94. [_contents replaceCharactersInRange:range withAttributedString:attributedString];
  95. [self edited:NSTextStorageEditedAttributes|NSTextStorageEditedCharacters range:range changeInLength:[attributedString length] - range.length];
  96. }
  97.  
  98. - (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
  99. {
  100. [_contents setAttributes:attrs range:range];
  101. [self edited:NSTextStorageEditedAttributes range:range changeInLength:0];
  102. }
  103.  
  104. #pragma mark -
  105. #pragma mark NSTextStorage subclass
  106.  
  107. - (void)processEditing;
  108. {
  109. NSLog(@"in processEditing editedMask:%ld editedRange:%@ changeInLength:%ld", [self editedMask], NSStringFromRange([self editedRange]), [self changeInLength]);
  110. [super processEditing];
  111. }
  112.  
  113. @end
  114.  
  115. @implementation TextView
  116.  
  117. - (void)awakeFromNib;
  118. {
  119. // Terrible
  120. TextStorage *textStorage = [[TextStorage alloc] init];
  121. [[self layoutManager] replaceTextStorage:textStorage];
  122. [textStorage release];
  123. }
  124.  
  125. - (void)didChangeText;
  126. {
  127. NSLog(@"rangeForUserTextChange = %@", NSStringFromRange([self rangeForUserTextChange]));
  128. NSLog(@"textStorage.editedRange = %@", NSStringFromRange([self.textStorage editedRange]));
  129.  
  130. [super didChangeText];
  131. }
  132.  
  133. - (IBAction)insertFoo:(id)sender;
  134. {
  135. // ... not bothering with multi-range for test ...
  136. NSRange range = [self selectionRangeForProposedRange:[self selectedRange] granularity:[self selectionGranularity]];
  137. NSLog(@"insert range:%@", NSStringFromRange(range));
  138.  
  139. if ([self shouldChangeTextInRange:range replacementString:@"foo"]) {
  140. NSTextStorage *textStorage = [self textStorage];
  141. [textStorage beginEditing];
  142. [textStorage replaceCharactersInRange:range withString:@"foo"];
  143. [textStorage endEditing];
  144. [self didChangeText];
  145. }
  146. }
  147.  
  148. @end
Add Comment
Please, Sign In to add comment