Skip to content
This repository has been archived by the owner on Sep 15, 2021. It is now read-only.

Commit

Permalink
Updated NoodleLineNumberView and NoodleLineNumberMaker for 64bit / Lion
Browse files Browse the repository at this point in the history
  • Loading branch information
scottharwell committed Dec 22, 2011
1 parent c8f439c commit 17162e5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 29 deletions.
3 changes: 3 additions & 0 deletions NoodleLineNumberView.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@
NSColor *_textColor; NSColor *_textColor;
NSColor *_alternateTextColor; NSColor *_alternateTextColor;
NSColor *_backgroundColor; NSColor *_backgroundColor;
float value;
} }


- (id)initWithScrollView:(NSScrollView *)aScrollView; - (id)initWithScrollView:(NSScrollView *)aScrollView;


- (id)initWithScrollView:(NSScrollView *)scrollView orientation:(NSRulerOrientation)orientation;

- (void)setFont:(NSFont *)aFont; - (void)setFont:(NSFont *)aFont;
- (NSFont *)font; - (NSFont *)font;


Expand Down
81 changes: 52 additions & 29 deletions NoodleLineNumberView.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ - (NSDictionary *)markerTextAttributes;


@implementation NoodleLineNumberView @implementation NoodleLineNumberView


- (id)initWithScrollView:(NSScrollView *)aScrollView - (id)initWithScrollView: (NSScrollView *)aScrollView
{
return [self initWithScrollView:aScrollView orientation:NSVerticalRuler];

}

- (id)initWithScrollView:(NSScrollView *)aScrollView orientation: (NSRulerOrientation)orientation
{ {
if ((self = [super initWithScrollView:aScrollView orientation:NSVerticalRuler]) != nil) if ((self = [super initWithScrollView:aScrollView orientation:NSVerticalRuler]) != nil)
{ {
Expand All @@ -58,6 +64,7 @@ - (id)initWithScrollView:(NSScrollView *)aScrollView
return self; return self;
} }



- (void)awakeFromNib - (void)awakeFromNib
{ {
_linesToMarkers = [[NSMutableDictionary alloc] init]; _linesToMarkers = [[NSMutableDictionary alloc] init];
Expand Down Expand Up @@ -157,7 +164,7 @@ - (void)setClientView:(NSView *)aView
if ((aView != nil) && [aView isKindOfClass:[NSTextView class]]) if ((aView != nil) && [aView isKindOfClass:[NSTextView class]])
{ {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:NSTextStorageDidProcessEditingNotification object:[(NSTextView *)aView textStorage]]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:NSTextStorageDidProcessEditingNotification object:[(NSTextView *)aView textStorage]];

[self invalidateLineIndices]; [self invalidateLineIndices];
} }
} }
Expand Down Expand Up @@ -195,12 +202,12 @@ - (NSUInteger)lineNumberForLocation:(CGFloat)location
NSRange nullRange; NSRange nullRange;
NSMutableArray *lines; NSMutableArray *lines;
id view; id view;
view = [self clientView]; view = [self clientView];
visibleRect = [[[self scrollView] contentView] bounds]; visibleRect = [[[self scrollView] contentView] bounds];


lines = [self lineIndices]; lines = [self lineIndices];

location += NSMinY(visibleRect); location += NSMinY(visibleRect);


if ([view isKindOfClass:[NSTextView class]]) if ([view isKindOfClass:[NSTextView class]])
Expand Down Expand Up @@ -240,7 +247,7 @@ - (NoodleLineNumberMarker *)markerAtLine:(NSUInteger)line
- (void)calculateLines - (void)calculateLines
{ {
id view; id view;

view = [self clientView]; view = [self clientView];


if ([view isKindOfClass:[NSTextView class]]) if ([view isKindOfClass:[NSTextView class]])
Expand All @@ -265,14 +272,14 @@ - (void)calculateLines
numberOfLines++; numberOfLines++;
} }
while (index < stringLength); while (index < stringLength);

// Check if text ends with a new line. // Check if text ends with a new line.
[text getLineStart:NULL end:&lineEnd contentsEnd:&contentEnd forRange:NSMakeRange([[_lineIndices lastObject] unsignedIntegerValue], 0)]; [text getLineStart:NULL end:&lineEnd contentsEnd:&contentEnd forRange:NSMakeRange([[_lineIndices lastObject] unsignedIntegerValue], 0)];
if (contentEnd < lineEnd) if (contentEnd < lineEnd)
{ {
[_lineIndices addObject:[NSNumber numberWithUnsignedInteger:index]]; [_lineIndices addObject:[NSNumber numberWithUnsignedInteger:index]];
} }

oldThickness = [self ruleThickness]; oldThickness = [self ruleThickness];
newThickness = [self requiredThickness]; newThickness = [self requiredThickness];
if (fabs(oldThickness - newThickness) > 1) if (fabs(oldThickness - newThickness) > 1)
Expand All @@ -295,13 +302,13 @@ - (NSUInteger)lineNumberForCharacterIndex:(NSUInteger)index inText:(NSString *)t
{ {
NSUInteger left, right, mid, lineStart; NSUInteger left, right, mid, lineStart;
NSMutableArray *lines; NSMutableArray *lines;

lines = [self lineIndices]; lines = [self lineIndices];


// Binary search // Binary search
left = 0; left = 0;
right = [lines count]; right = [lines count];

while ((right - left) > 1) while ((right - left) > 1)
{ {
mid = (right + left) / 2; mid = (right + left) / 2;
Expand Down Expand Up @@ -333,10 +340,10 @@ - (NSDictionary *)textAttributes


- (NSDictionary *)markerTextAttributes - (NSDictionary *)markerTextAttributes
{ {
return [NSDictionary dictionaryWithObjectsAndKeys: return [NSDictionary dictionaryWithObjectsAndKeys:
[self font], NSFontAttributeName, [self font], NSFontAttributeName,
[self alternateTextColor], NSForegroundColorAttributeName, [self alternateTextColor], NSForegroundColorAttributeName,
nil]; nil];
} }


- (CGFloat)requiredThickness - (CGFloat)requiredThickness
Expand All @@ -357,7 +364,7 @@ - (CGFloat)requiredThickness
} }


stringSize = [sampleString sizeWithAttributes:[self textAttributes]]; stringSize = [sampleString sizeWithAttributes:[self textAttributes]];

// Round up the value. There is a bug on 10.4 where the display gets all wonky when scrolling if you don't // Round up the value. There is a bug on 10.4 where the display gets all wonky when scrolling if you don't
// return an integral value here. // return an integral value here.
return ceil(MAX(DEFAULT_THICKNESS, stringSize.width + RULER_MARGIN * 2)); return ceil(MAX(DEFAULT_THICKNESS, stringSize.width + RULER_MARGIN * 2));
Expand All @@ -367,14 +374,27 @@ - (void)drawHashMarksAndLabelsInRect:(NSRect)aRect
{ {
id view; id view;
NSRect bounds; NSRect bounds;


bounds = [self bounds];

bounds = [self bounds];

/*
if (value > 10.0) {
[[NSColor colorWithDeviceRed: 1.0 green: 0.5 blue: 0.5 alpha: 0.5] set];
NSRectFill(aRect);
}
value = value + 0.1;
*/



if (_backgroundColor != nil) if (_backgroundColor != nil)
{ {
[_backgroundColor set]; [_backgroundColor set];
NSRectFill(bounds); NSRectFill(bounds);
[[NSColor colorWithCalibratedWhite:0.58 alpha:1.0] set]; [[NSColor colorWithCalibratedWhite:0.58 alpha:1.0] set];
[NSBezierPath strokeLineFromPoint:NSMakePoint(NSMaxX(bounds) - 0/5, NSMinY(bounds)) toPoint:NSMakePoint(NSMaxX(bounds) - 0.5, NSMaxY(bounds))]; [NSBezierPath strokeLineFromPoint:NSMakePoint(NSMaxX(bounds) - 0/5, NSMinY(bounds)) toPoint:NSMakePoint(NSMaxX(bounds) - 0.5, NSMaxY(bounds))];
} }
Expand All @@ -390,25 +410,25 @@ - (void)drawHashMarksAndLabelsInRect:(NSRect)aRect
NSString *text, *labelText; NSString *text, *labelText;
NSUInteger rectCount, index, line, count; NSUInteger rectCount, index, line, count;
NSRectArray rects; NSRectArray rects;
CGFloat ypos, yinset; CGFloat ypos, yinset, x;
NSDictionary *textAttributes, *currentTextAttributes; NSDictionary *textAttributes, *currentTextAttributes;
NSSize stringSize, markerSize; NSSize stringSize, markerSize;
NoodleLineNumberMarker *marker; NoodleLineNumberMarker *marker;
NSImage *markerImage; NSImage *markerImage;
NSMutableArray *lines; NSMutableArray *lines;

layoutManager = [view layoutManager]; layoutManager = [view layoutManager];
container = [view textContainer]; container = [view textContainer];
text = [view string]; text = [view string];
nullRange = NSMakeRange(NSNotFound, 0); nullRange = NSMakeRange(NSNotFound, 0);


yinset = [view textContainerInset].height; yinset = [view textContainerInset].height;
visibleRect = [[[self scrollView] contentView] bounds]; visibleRect = [[[self scrollView] contentView] bounds];

textAttributes = [self textAttributes]; textAttributes = [self textAttributes];


lines = [self lineIndices]; lines = [self lineIndices];

// Find the characters that are currently visible // Find the characters that are currently visible
glyphRange = [layoutManager glyphRangeForBoundingRect:visibleRect inTextContainer:container]; glyphRange = [layoutManager glyphRangeForBoundingRect:visibleRect inTextContainer:container];
range = [layoutManager characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL]; range = [layoutManager characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL];
Expand All @@ -419,6 +439,7 @@ - (void)drawHashMarksAndLabelsInRect:(NSRect)aRect


count = [lines count]; count = [lines count];



for (line = [self lineNumberForCharacterIndex:range.location inText:text]; line < count; line++) for (line = [self lineNumberForCharacterIndex:range.location inText:text]; line < count; line++)
{ {
index = [[lines objectAtIndex:line] unsignedIntegerValue]; index = [[lines objectAtIndex:line] unsignedIntegerValue];
Expand All @@ -440,22 +461,23 @@ - (void)drawHashMarksAndLabelsInRect:(NSRect)aRect


if (marker != nil) if (marker != nil)
{ {
markerImage = [marker image];
markerImage = [marker image];
markerSize = [markerImage size]; markerSize = [markerImage size];
markerRect = NSMakeRect(0.0, 0.0, markerSize.width, markerSize.height); markerRect = NSMakeRect(0.0, 0.0, markerSize.width, markerSize.height);

// Marker is flush right and centered vertically within the line. // Marker is flush right and centered vertically within the line.
markerRect.origin.x = NSWidth(bounds) - [markerImage size].width - 1.0; markerRect.origin.x = NSWidth(bounds) - [markerImage size].width - 1.0;
markerRect.origin.y = ypos + NSHeight(rects[0]) / 2.0 - [marker imageOrigin].y; markerRect.origin.y = ypos + NSHeight(rects[0]) / 2.0 - [marker imageOrigin].y;

[markerImage drawInRect:markerRect fromRect:NSMakeRect(0, 0, markerSize.width, markerSize.height) operation:NSCompositeSourceOver fraction:1.0]; [markerImage drawInRect:markerRect fromRect:NSMakeRect(0, 0, markerSize.width, markerSize.height) operation:NSCompositeSourceOver fraction:1.0];
} }


// Line numbers are internally stored starting at 0 // Line numbers are internally stored starting at 0
labelText = [NSString stringWithFormat:@"%jd", (intmax_t)line + 1]; labelText = [NSString stringWithFormat:@"%jd", (intmax_t)line + 1];


stringSize = [labelText sizeWithAttributes:textAttributes]; stringSize = [labelText sizeWithAttributes:textAttributes];

if (marker == nil) if (marker == nil)
{ {
currentTextAttributes = textAttributes; currentTextAttributes = textAttributes;
Expand All @@ -466,10 +488,11 @@ - (void)drawHashMarksAndLabelsInRect:(NSRect)aRect
} }


// Draw string flush right, centered vertically within the line // Draw string flush right, centered vertically within the line
x = ypos + (NSHeight(rects[0]) - stringSize.height) / 2.0;
[labelText drawInRect: [labelText drawInRect:
NSMakeRect(NSWidth(bounds) - stringSize.width - RULER_MARGIN, NSMakeRect(NSWidth(bounds) - stringSize.width - RULER_MARGIN,
ypos + (NSHeight(rects[0]) - stringSize.height) / 2.0, ypos + (NSHeight(rects[0]) - stringSize.height) / 2.0,
NSWidth(bounds) - RULER_MARGIN * 2.0, NSHeight(rects[0])) NSWidth(bounds) - RULER_MARGIN * 2.0, NSHeight(rects[0]))
withAttributes:currentTextAttributes]; withAttributes:currentTextAttributes];
} }
} }
Expand All @@ -488,7 +511,7 @@ - (void)setMarkers:(NSArray *)markers


[_linesToMarkers removeAllObjects]; [_linesToMarkers removeAllObjects];
[super setMarkers:nil]; [super setMarkers:nil];

enumerator = [markers objectEnumerator]; enumerator = [markers objectEnumerator];
while ((marker = [enumerator nextObject]) != nil) while ((marker = [enumerator nextObject]) != nil)
{ {
Expand Down

0 comments on commit 17162e5

Please sign in to comment.