Fix handling of NSTextContainer and NSTextStorage in NSTextView.subproj

Fix issue where a layout manager's NSTextStorage could be `nil` at initialization
but later set by introducing the `_setTextStorage:` method to NSTextView.

Add stubs a for missing properties in NSTextView.

Refactor `firstTextView` to properly find and return the first text view,
or `nil` if there are no text views.

Use 64-bit ready types where applicable.

Relocate unrelated code from _setTextStorage
This commit is contained in:
ckegel 2023-04-20 22:41:22 -04:00
parent fd68ee23c7
commit 5300f80f18
4 changed files with 161 additions and 49 deletions

View File

@ -30,7 +30,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#import <AppKit/NSTypesetter.h> #import <AppKit/NSTypesetter.h>
#import <AppKit/NSWindow.h> #import <AppKit/NSWindow.h>
#import <Foundation/NSRangeEntries.h>
#import <AppKit/NSAttributedString.h> #import <AppKit/NSAttributedString.h>
#import <AppKit/NSColor.h> #import <AppKit/NSColor.h>
#import <AppKit/NSGraphics.h> #import <AppKit/NSGraphics.h>
@ -39,6 +38,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#import <ApplicationServices/ApplicationServices.h> #import <ApplicationServices/ApplicationServices.h>
#import <Foundation/NSKeyedArchiver.h> #import <Foundation/NSKeyedArchiver.h>
#import <Foundation/NSRaiseException.h> #import <Foundation/NSRaiseException.h>
#import <Foundation/NSRangeEntries.h>
#import "NSBidiHelper.h" #import "NSBidiHelper.h"
#import "NSRulerMarker+NSTextExtensions.h" #import "NSRulerMarker+NSTextExtensions.h"
@ -177,13 +177,17 @@ static inline NSGlyphFragment *fragmentAtGlyphIndex(NSLayoutManager *self,
} }
- (NSTextView *) firstTextView { - (NSTextView *) firstTextView {
if([_textContainers count] < 1) for (NSTextContainer *container in _textContainers) {
return nil; NSTextView *textView = [container textView];
return [[_textContainers objectAtIndex: 0] textView]; if (textView) {
return textView;
}
}
return nil;
} }
- (NSTextView *) textViewForBeginningOfSelection { - (NSTextView *) textViewForBeginningOfSelection {
return [[_textContainers objectAtIndex: 0] textView]; return [self firstTextView];
} }
- (BOOL) layoutManagerOwnsFirstResponderInWindow: (NSWindow *) window { - (BOOL) layoutManagerOwnsFirstResponderInWindow: (NSWindow *) window {
@ -729,7 +733,6 @@ static inline NSGlyphFragment *fragmentAtGlyphIndex(NSLayoutManager *self,
} }
- (BOOL) allowsNonContiguousLayout { - (BOOL) allowsNonContiguousLayout {
NSUnimplementedMethod();
return _allowsNonContiguousLayout; return _allowsNonContiguousLayout;
} }
@ -1381,7 +1384,8 @@ static inline void _appendRectToCache(NSLayoutManager *self, NSRect rect) {
NSRange intersect; NSRange intersect;
if (remainder.length > 0) if (remainder.length > 0)
intersect = NSIntersectionRange(remainder, range); intersect = NSIntersectionRange(remainder, range);
else // NSIntersectionRange's returned location is undefined for 0-length ranges else // NSIntersectionRange's returned location is undefined for
// 0-length ranges
intersect = NSMakeRange(remainder.location, 0); intersect = NSMakeRange(remainder.location, 0);
#if DEBUG_rectArrayForGlyphRange_withinSelectedGlyphRange_inTextContainer_rectCount #if DEBUG_rectArrayForGlyphRange_withinSelectedGlyphRange_inTextContainer_rectCount

View File

@ -189,6 +189,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- (void) setLayoutManager: (NSLayoutManager *) layoutManager { - (void) setLayoutManager: (NSLayoutManager *) layoutManager {
_layoutManager = layoutManager; _layoutManager = layoutManager;
[_textView _setTextStorage: [_layoutManager textStorage]];
} }
- (void) replaceLayoutManager: (NSLayoutManager *) layoutManager { - (void) replaceLayoutManager: (NSLayoutManager *) layoutManager {

View File

@ -143,6 +143,36 @@ NSString *const NSAllRomanInputSourcesLocaleIdentifier =
NSUnimplementedMethod(); NSUnimplementedMethod();
} }
- (void) _setTextStorage: (NSTextStorage *) storage {
if (_ownsTextStorage)
[_textStorage release];
_textStorage = storage;
_ownsTextStorage = NO;
NSMutableDictionary *typingAttributes =
[[_textStorage attributesAtIndex: 0
effectiveRange: NULL] mutableCopy];
if (![typingAttributes objectForKey: NSFontAttributeName]) {
[typingAttributes setObject: _font forKey: NSFontAttributeName];
}
if (![typingAttributes objectForKey: NSForegroundColorAttributeName]) {
[typingAttributes setObject: _textColor
forKey: NSForegroundColorAttributeName];
}
[_typingAttributes release];
_typingAttributes = typingAttributes;
if ([typingAttributes objectForKey: NSParagraphStyleAttributeName]) {
_defaultParagraphStyle = [[typingAttributes
objectForKey: NSParagraphStyleAttributeName] copy];
} else {
_defaultParagraphStyle =
[[NSParagraphStyle defaultParagraphStyle] copy];
}
}
- initWithCoder: (NSCoder *) coder { - initWithCoder: (NSCoder *) coder {
[super initWithCoder: coder]; [super initWithCoder: coder];
@ -240,8 +270,6 @@ NSString *const NSAllRomanInputSourcesLocaleIdentifier =
- initWithFrame: (NSRect) frame textContainer: (NSTextContainer *) container { - initWithFrame: (NSRect) frame textContainer: (NSTextContainer *) container {
[super initWithFrame: frame]; [super initWithFrame: frame];
_textStorage = [[container layoutManager] textStorage];
_ownsTextStorage = NO;
_textContainer = [container retain]; _textContainer = [container retain];
[_textContainer setTextView: self]; [_textContainer setTextView: self];
_textContainerInset = NSMakeSize(0, 0); _textContainerInset = NSMakeSize(0, 0);
@ -263,26 +291,6 @@ NSString *const NSAllRomanInputSourcesLocaleIdentifier =
_selectedRanges = [[NSMutableArray alloc] init]; _selectedRanges = [[NSMutableArray alloc] init];
[_selectedRanges addObject: [NSValue valueWithRange: NSMakeRange(0, 0)]]; [_selectedRanges addObject: [NSValue valueWithRange: NSMakeRange(0, 0)]];
NSMutableDictionary *typingAttributes =
[[_textStorage attributesAtIndex: 0
effectiveRange: NULL] mutableCopy];
if (![typingAttributes objectForKey: NSFontAttributeName]) {
[typingAttributes setObject: _font forKey: NSFontAttributeName];
}
if (![typingAttributes objectForKey: NSForegroundColorAttributeName]) {
[typingAttributes setObject: _textColor
forKey: NSForegroundColorAttributeName];
}
_typingAttributes = typingAttributes;
if ([typingAttributes objectForKey: NSParagraphStyleAttributeName]) {
_defaultParagraphStyle = [[typingAttributes
objectForKey: NSParagraphStyleAttributeName] copy];
} else {
_defaultParagraphStyle =
[[NSParagraphStyle defaultParagraphStyle] copy];
}
_rangeForUserCompletion = NSMakeRange(NSNotFound, 0); _rangeForUserCompletion = NSMakeRange(NSNotFound, 0);
_selectedTextAttributes = [[NSDictionary _selectedTextAttributes = [[NSDictionary
dictionaryWithObjectsAndKeys: [NSColor selectedTextColor], dictionaryWithObjectsAndKeys: [NSColor selectedTextColor],
@ -291,9 +299,12 @@ NSString *const NSAllRomanInputSourcesLocaleIdentifier =
NSBackgroundColorAttributeName, nil] NSBackgroundColorAttributeName, nil]
retain]; retain];
[self _setTextStorage: [[container layoutManager] textStorage]];
[self setBoundsOrigin: NSMakePoint(-_textContainerInset.width, [self setBoundsOrigin: NSMakePoint(-_textContainerInset.width,
-_textContainerInset.height)]; -_textContainerInset.height)];
[self configureMenu]; [self configureMenu];
[self registerForDraggedTypes: [NSArray [self registerForDraggedTypes: [NSArray
arrayWithObjects: NSRTFPboardType, arrayWithObjects: NSRTFPboardType,
NSStringPboardType, NSStringPboardType,
@ -3389,9 +3400,7 @@ NSString *const NSAllRomanInputSourcesLocaleIdentifier =
- (void) _continuousSpellCheckWithInvalidatedRange: (NSRange) invalidatedRange { - (void) _continuousSpellCheckWithInvalidatedRange: (NSRange) invalidatedRange {
NSString *string = [self string]; NSString *string = [self string];
NSUInteger start, end; NSUInteger start, end;
// TODO, truncate invalidated range to string size if needed // TODO, truncate invalidated range to string size if needed
// round range to nearest paragraphs // round range to nearest paragraphs
[string getParagraphStart: &start [string getParagraphStart: &start
@ -3433,10 +3442,8 @@ NSString *const NSAllRomanInputSourcesLocaleIdentifier =
} }
- (void) _continuousSpellCheck { - (void) _continuousSpellCheck {
[self _continuousSpellCheckWithInvalidatedRange: NSMakeRange( NSRange invalidatedRange = NSMakeRange(0, [[self string] length]);
0, [self _continuousSpellCheckWithInvalidatedRange: invalidatedRange];
[[self string]
length])];
} }
- (void) checkSpelling: sender { - (void) checkSpelling: sender {
@ -3978,29 +3985,104 @@ NSString *const NSAllRomanInputSourcesLocaleIdentifier =
return _layoutOrientation; return _layoutOrientation;
} }
- (void)setLayoutOrientation:(NSTextLayoutOrientation)orientation { - (void) setLayoutOrientation: (NSTextLayoutOrientation) orientation {
_layoutOrientation = orientation; _layoutOrientation = orientation;
NSUnimplementedMethod(); NSUnimplementedMethod();
} }
- (BOOL) usesFindBar {
NSUnimplementedMethod();
return _usesFindBar;
}
- (void) setUsesFindBar: (BOOL) value {
NSUnimplementedMethod();
_usesFindBar = value;
}
- (BOOL) isIncrementalSearchingEnabled { - (BOOL) isIncrementalSearchingEnabled {
NSUnimplementedMethod(); NSUnimplementedMethod();
return _incrementalSearchingEnabled; return _incrementalSearchingEnabled;
} }
- (BOOL) usesFindBar {
NSUnimplementedMethod();
return _usesFindBar;
}
- (BOOL) usesInspectorBar {
NSUnimplementedMethod();
return _usesInspectorBar;
}
- (void) setIncrementalSearchingEnabled: (BOOL) value { - (void) setIncrementalSearchingEnabled: (BOOL) value {
NSUnimplementedMethod(); NSUnimplementedMethod();
_incrementalSearchingEnabled = value; _incrementalSearchingEnabled = value;
} }
- (void) setUsesFindBar: (BOOL) value {
NSUnimplementedMethod();
_usesFindBar = value;
}
- (void) setUsesInspectorBar: (BOOL) value {
NSUnimplementedMethod();
_usesInspectorBar = value;
}
- (BOOL) isGrammarCheckingEnabled {
NSUnimplementedMethod();
return _grammarCheckingEnabled;
}
- (BOOL) isAutomaticQuoteSubstitutionEnabled {
NSUnimplementedMethod();
return _automaticQuoteSubstitutionEnabled;
}
- (BOOL) isAutomaticDashSubstitutionEnabled {
NSUnimplementedMethod();
return _automaticDashSubstitutionEnabled;
}
- (BOOL) isAutomaticLinkDetectionEnabled {
NSUnimplementedMethod();
return _automaticLinkDetectionEnabled;
}
- (BOOL) isAutomaticDataDetectionEnabled {
NSUnimplementedMethod();
return _automaticDataDetectionEnabled;
}
- (BOOL) isAutomaticTextReplacementEnabled {
NSUnimplementedMethod();
return _automaticTextReplacementEnabled;
}
- (void) setGrammarCheckingEnabled: (BOOL) value {
NSUnimplementedMethod();
_grammarCheckingEnabled = value;
}
- (void) setAutomaticQuoteSubstitutionEnabled: (BOOL) value {
NSUnimplementedMethod();
_automaticQuoteSubstitutionEnabled = value;
}
- (void) setAutomaticDashSubstitutionEnabled: (BOOL) value {
NSUnimplementedMethod();
_automaticDashSubstitutionEnabled = value;
}
- (void) setAutomaticLinkDetectionEnabled: (BOOL) value {
NSUnimplementedMethod();
_automaticLinkDetectionEnabled = value;
}
- (void) setAutomaticDataDetectionEnabled: (BOOL) value {
NSUnimplementedMethod();
_automaticDataDetectionEnabled = value;
}
- (void) setAutomaticTextReplacementEnabled: (BOOL) value {
NSUnimplementedMethod();
_automaticTextReplacementEnabled = value;
}
// Is defined in NSText but throws an NSInvalidAbstractInvocation Exception
- (void) setImportsGraphics: (BOOL) value {
NSUnimplementedMethod();
}
@end @end

View File

@ -139,12 +139,22 @@ APPKIT_EXPORT NSString *const NSAllRomanInputSourcesLocaleIdentifier;
NSTextLayoutOrientation _layoutOrientation; NSTextLayoutOrientation _layoutOrientation;
BOOL _incrementalSearchingEnabled; BOOL _incrementalSearchingEnabled;
BOOL _grammarCheckingEnabled;
BOOL _automaticQuoteSubstitutionEnabled;
BOOL _automaticDashSubstitutionEnabled;
BOOL _automaticLinkDetectionEnabled;
BOOL _automaticDataDetectionEnabled;
BOOL _automaticTextReplacementEnabled;
BOOL _usesInspectorBar;
} }
- initWithFrame: (NSRect) frame textContainer: (NSTextContainer *) container; - initWithFrame: (NSRect) frame textContainer: (NSTextContainer *) container;
- initWithFrame: (NSRect) frame; - initWithFrame: (NSRect) frame;
- (void) _setTextStorage: (NSTextStorage *) storage;
- (NSTextContainer *) textContainer; - (NSTextContainer *) textContainer;
- (NSSize) textContainerInset; - (NSSize) textContainerInset;
@ -266,13 +276,28 @@ APPKIT_EXPORT NSString *const NSAllRomanInputSourcesLocaleIdentifier;
- (void) setSpellingState: (NSInteger) value range: (NSRange) characterRange; - (void) setSpellingState: (NSInteger) value range: (NSRange) characterRange;
- (BOOL) usesFindBar;
- (void) setUsesFindBar: (BOOL) value;
- (BOOL) isIncrementalSearchingEnabled; - (BOOL) isIncrementalSearchingEnabled;
- (BOOL) usesFindBar;
- (BOOL) usesInspectorBar;
- (void) setIncrementalSearchingEnabled: (BOOL) value; - (void) setIncrementalSearchingEnabled: (BOOL) value;
- (void) setUsesFindBar: (BOOL) value;
- (void) setUsesInspectorBar: (BOOL) value;
- (void) setLayoutOrientation:(NSTextLayoutOrientation)orientation; - (void) setLayoutOrientation:(NSTextLayoutOrientation)orientation;
- (BOOL) isGrammarCheckingEnabled;
- (BOOL) isAutomaticQuoteSubstitutionEnabled;
- (BOOL) isAutomaticDashSubstitutionEnabled;
- (BOOL) isAutomaticLinkDetectionEnabled;
- (BOOL) isAutomaticDataDetectionEnabled;
- (BOOL) isAutomaticTextReplacementEnabled;
- (void) setGrammarCheckingEnabled: (BOOL) value;
- (void) setAutomaticQuoteSubstitutionEnabled: (BOOL) value;
- (void) setAutomaticDashSubstitutionEnabled: (BOOL) value;
- (void) setAutomaticLinkDetectionEnabled: (BOOL) value;
- (void) setAutomaticDataDetectionEnabled: (BOOL) value;
- (void) setAutomaticTextReplacementEnabled: (BOOL) value;
@end @end
@interface NSObject (NSTextView_undoManager) @interface NSObject (NSTextView_undoManager)