From 368cfb8838c5526d675b441e82dd1a5db8418ccb Mon Sep 17 00:00:00 2001 From: Airy ANDRE Date: Tue, 2 Apr 2013 14:59:36 -0700 Subject: [PATCH] Better handling of various textview/container offset by rulers & markers --- AppKit/NSRulerMarker.m | 21 ++++++----------- AppKit/NSTextView.subproj/NSLayoutManager.m | 2 +- AppKit/NSTextView.subproj/NSTextView.m | 25 +++++++++++++++------ 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/AppKit/NSRulerMarker.m b/AppKit/NSRulerMarker.m index a4f53313..97d22b2d 100755 --- a/AppKit/NSRulerMarker.m +++ b/AppKit/NSRulerMarker.m @@ -147,13 +147,10 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI location = [_ruler convertPoint:location fromView:[[_ruler enclosingScrollView] documentView]]; } - float offset = [_ruler originOffset]; if ([_ruler orientation] == NSHorizontalRuler) { rect.origin.x += location.x; - rect.origin.x += offset; } else { rect.origin.y += location.y; // how does a flipped system affect this? hm - rect.origin.y += offset; } rect.origin.x -= _imageOrigin.x; rect.origin.y -= _imageOrigin.y; @@ -184,11 +181,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI - (float)_markerLocationFromLocation:(NSPoint)point { - if ([_ruler orientation] == NSHorizontalRuler) { - point.x -= [_ruler originOffset]; - } else { - point.y -= [_ruler originOffset]; - } NSView *trackedView = _ruler.clientView; if (trackedView == nil) { trackedView = _ruler.enclosingScrollView.documentView; @@ -196,11 +188,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI point = [_ruler convertPoint:point toView:trackedView]; float newLocation = [_ruler orientation] == NSHorizontalRuler ? point.x : point.y; - if ([[_ruler clientView] respondsToSelector:@selector(rulerView:willAddMarker:atLocation:)]) - newLocation = [[_ruler clientView] rulerView:_ruler willAddMarker:self atLocation:newLocation]; - else - newLocation = newLocation; - return newLocation; } @@ -227,7 +214,10 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI } _markerLocation = [self _markerLocationFromLocation:point]; - + if ([[_ruler clientView] respondsToSelector:@selector(rulerView:willAddMarker:atLocation:)]) { + _markerLocation = [[_ruler clientView] rulerView:_ruler willAddMarker:self atLocation:_markerLocation]; + } + // Draw the ruler + the new marker [_ruler lockFocus]; [_ruler drawRect:[_ruler bounds]]; @@ -275,6 +265,9 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI } } _markerLocation = [self _markerLocationFromLocation:point]; + if ([[_ruler clientView] respondsToSelector:@selector(rulerView:willMoveMarker:toLocation:)]) { + _markerLocation = [[_ruler clientView] rulerView:_ruler willMoveMarker:self toLocation:_markerLocation]; + } [_ruler setNeedsDisplay:YES]; } event = [[_ruler window] nextEventMatchingMask:NSLeftMouseUpMask|NSLeftMouseDraggedMask]; diff --git a/AppKit/NSTextView.subproj/NSLayoutManager.m b/AppKit/NSTextView.subproj/NSLayoutManager.m index 27ffadf8..04f961c0 100755 --- a/AppKit/NSTextView.subproj/NSLayoutManager.m +++ b/AppKit/NSTextView.subproj/NSLayoutManager.m @@ -1744,7 +1744,7 @@ static inline void _appendRectToCache(NSLayoutManager *self,NSRect rect){ { NSMutableArray *markers = [NSMutableArray array]; - float delta = view.textContainer.lineFragmentPadding; + float delta = view.textContainer.lineFragmentPadding + view.textContainerOrigin.x; // Add the margins markers #if 0 diff --git a/AppKit/NSTextView.subproj/NSTextView.m b/AppKit/NSTextView.subproj/NSTextView.m index 860d1d9c..e6d8e256 100755 --- a/AppKit/NSTextView.subproj/NSTextView.m +++ b/AppKit/NSTextView.subproj/NSTextView.m @@ -725,7 +725,7 @@ NSString * const NSOldSelectedCharacterRange=@"NSOldSelectedCharacterRange"; NSRulerView *ruler = [[self enclosingScrollView] horizontalRulerView]; if(ruler!=nil){ - [ruler setOriginOffset:[self textContainerOrigin].x]; + [ruler setOriginOffset:self.textContainerOrigin.x + self.textContainer.lineFragmentPadding]; NSDictionary *typingAttributes = [self typingAttributes]; NSParagraphStyle *style=[typingAttributes objectForKey:NSParagraphStyleAttributeName]; @@ -3054,8 +3054,9 @@ NSString * const NSOldSelectedCharacterRange=@"NSOldSelectedCharacterRange"; { // Add a new tab stop - PQRulerView style NSPoint point = [self convertPoint: event.locationInWindow fromView: nil]; + float delta = rulerView.originOffset; NSRulerMarker *marker = [NSRulerMarker leftTabMarkerWithRulerView:rulerView - location:point.x + self.textContainer.lineFragmentPadding]; + location:point.x + delta]; NSTextTab *tabstop = [[[NSTextTab alloc] initWithType: NSLeftTabStopType location: point.x] autorelease]; [marker setRepresentedObject: tabstop]; [rulerView trackMarker: marker withMouseEvent: event]; @@ -3068,12 +3069,18 @@ NSString * const NSOldSelectedCharacterRange=@"NSOldSelectedCharacterRange"; -(float)rulerView:(NSRulerView *)rulerView willMoveMarker:(NSRulerMarker *)marker toLocation:(float)location { + if (location < rulerView.originOffset) { + location = rulerView.originOffset; + } + if (location > self.textContainer.containerSize.width - self.textContainer.lineFragmentPadding) { + location = self.textContainer.containerSize.width - self.textContainer.lineFragmentPadding; + } return location; } -(void)rulerView:(NSRulerView *)rulerView didMoveMarker:(NSRulerMarker *)marker { - float delta = self.textContainer.lineFragmentPadding; + float delta = rulerView.originOffset; float location = marker.markerLocation - delta; id representedObject = marker.representedObject; @@ -3146,12 +3153,19 @@ NSString * const NSOldSelectedCharacterRange=@"NSOldSelectedCharacterRange"; -(float)rulerView:(NSRulerView *)rulerView willAddMarker:(NSRulerMarker *)marker atLocation:(float)location { + if (location < rulerView.originOffset) { + location = rulerView.originOffset; + } + if (location > self.textContainer.containerSize.width - self.textContainer.lineFragmentPadding) { + location = self.textContainer.containerSize.width - self.textContainer.lineFragmentPadding; + } return location; } -(void)rulerView:(NSRulerView *)rulerView didAddMarker:(NSRulerMarker *)marker { - float delta = self.textContainer.lineFragmentPadding; + float delta = rulerView.originOffset; + float location = marker.markerLocation - delta; id representedObject = marker.representedObject; @@ -3215,9 +3229,6 @@ NSString * const NSOldSelectedCharacterRange=@"NSOldSelectedCharacterRange"; -(void)rulerView:(NSRulerView *)rulerView didRemoveMarker:(NSRulerMarker *)marker { - float delta = self.textContainer.lineFragmentPadding; - float location = marker.markerLocation - delta; - id representedObject = marker.representedObject; if ([representedObject isKindOfClass:[NSTextTab class]]) { NSTextTab *textTab = (NSTextTab *)representedObject;