Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: b124f51a9e
Fetching contributors…

Cannot retrieve contributors at this time

file 597 lines (497 sloc) 18.144 kb
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 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
//
// NoodleLineNumberView.m
// NoodleKit
//
// Created by Paul Kim on 9/28/08.
// Copyright (c) 2008 Noodlesoft, LLC. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//

#import "NoodleLineNumberView.h"
#import "NoodleLineNumberMarker.h"
#import <tgmath.h>

#define DEFAULT_THICKNESS 22.0
#define RULER_MARGIN 5.0

@interface NoodleLineNumberView (Private)

- (NSFont *)defaultFont;
- (NSColor *)defaultTextColor;
- (NSColor *)defaultAlternateTextColor;
- (NSMutableArray *)lineIndices;
- (void)invalidateLineIndicesFromCharacterIndex:(NSUInteger)charIndex;
- (void)calculateLines;
- (NSUInteger)lineNumberForCharacterIndex:(NSUInteger)index inText:(NSString *)text;
- (NSDictionary *)textAttributes;
- (NSDictionary *)markerTextAttributes;

@end

@implementation NoodleLineNumberView

@synthesize font = _font;
@synthesize textColor = _textColor;
@synthesize alternateTextColor = _alternateTextColor;
@synthesize backgroundColor = _backgroundColor;

- (id)initWithScrollView:(NSScrollView *)aScrollView
{
    if ((self = [super initWithScrollView:aScrollView orientation:NSVerticalRuler]) != nil)
    {
        _lineIndices = [[NSMutableArray alloc] init];
_linesToMarkers = [[NSMutableDictionary alloc] init];

        [self setClientView:[aScrollView documentView]];
    }
    return self;
}

- (void)awakeFromNib
{
    _lineIndices = [[NSMutableArray alloc] init];
_linesToMarkers = [[NSMutableDictionary alloc] init];
[self setClientView:[[self scrollView] documentView]];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    [_lineIndices release];
[_linesToMarkers release];
    [_font release];
    [_textColor release];
    [_alternateTextColor release];
    [_backgroundColor release];
    
    [super dealloc];
}

- (NSFont *)defaultFont
{
    return [NSFont labelFontOfSize:[NSFont systemFontSizeForControlSize:NSMiniControlSize]];
}

- (NSColor *)defaultTextColor
{
    return [NSColor colorWithCalibratedWhite:0.42 alpha:1.0];
}

- (NSColor *)defaultAlternateTextColor
{
    return [NSColor whiteColor];
}

- (void)setClientView:(NSView *)aView
{
id oldClientView;

oldClientView = [self clientView];

    if ((oldClientView != aView) && [oldClientView isKindOfClass:[NSTextView class]])
    {
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSTextStorageDidProcessEditingNotification object:[(NSTextView *)oldClientView textStorage]];
    }
    [super setClientView:aView];
    if ((aView != nil) && [aView isKindOfClass:[NSTextView class]])
    {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textStorageDidProcessEditing:) name:NSTextStorageDidProcessEditingNotification object:[(NSTextView *)aView textStorage]];

[self invalidateLineIndicesFromCharacterIndex:0];
    }
}

- (NSMutableArray *)lineIndices
{
if (_invalidCharacterIndex < NSUIntegerMax)
{
[self calculateLines];
}
return _lineIndices;
}

// Forces recalculation of line indicies starting from the given index
- (void)invalidateLineIndicesFromCharacterIndex:(NSUInteger)charIndex
{
    _invalidCharacterIndex = MIN(charIndex, _invalidCharacterIndex);
}

- (void)textStorageDidProcessEditing:(NSNotification *)notification
{
    NSTextStorage *storage;
    NSRange range;
    
    storage = [notification object];

    // Invalidate the line indices. They will be recalculated and re-cached on demand.
    range = [storage editedRange];
    if (range.location != NSNotFound)
    {
        [self invalidateLineIndicesFromCharacterIndex:range.location];
        [self setNeedsDisplay:YES];
    }
}

- (void)calculateLines
{
    id view;

    view = [self clientView];
    
    if ([view isKindOfClass:[NSTextView class]])
    {
        NSUInteger charIndex, stringLength, lineEnd, contentEnd, count, lineIndex;
        NSString *text;
        CGFloat oldThickness, newThickness;
        
        text = [view string];
        stringLength = [text length];
        count = [_lineIndices count];

        charIndex = 0;
        lineIndex = [self lineNumberForCharacterIndex:_invalidCharacterIndex inText:text];
        if (count > 0)
        {
            charIndex = [[_lineIndices objectAtIndex:lineIndex] unsignedIntegerValue];
        }
        
        do
        {
            if (lineIndex < count)
            {
                [_lineIndices replaceObjectAtIndex:lineIndex withObject:[NSNumber numberWithUnsignedInteger:charIndex]];
            }
            else
            {
                [_lineIndices addObject:[NSNumber numberWithUnsignedInteger:charIndex]];
            }
            
            charIndex = NSMaxRange([text lineRangeForRange:NSMakeRange(charIndex, 0)]);
            lineIndex++;
        }
        while (charIndex < stringLength);
        
        if (lineIndex < count)
        {
            [_lineIndices removeObjectsInRange:NSMakeRange(lineIndex, count - lineIndex)];
        }
        _invalidCharacterIndex = NSUIntegerMax;

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

        // See if we need to adjust the width of the view
        oldThickness = [self ruleThickness];
        newThickness = [self requiredThickness];
        if (fabs(oldThickness - newThickness) > 1)
        {
NSInvocation *invocation;

// Not a good idea to resize the view during calculations (which can happen during
// display). Do a delayed perform (using NSInvocation since arg is a float).
invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(setRuleThickness:)]];
[invocation setSelector:@selector(setRuleThickness:)];
[invocation setTarget:self];
[invocation setArgument:&newThickness atIndex:2];

[invocation performSelector:@selector(invoke) withObject:nil afterDelay:0.0];
        }
}
}

- (NSUInteger)lineNumberForCharacterIndex:(NSUInteger)charIndex inText:(NSString *)text
{
    NSUInteger left, right, mid, lineStart;
NSMutableArray *lines;

    if (_invalidCharacterIndex < NSUIntegerMax)
    {
        // We do not want to risk calculating the indices again since we are probably doing it right now, thus
        // possibly causing an infinite loop.
        lines = _lineIndices;
    }
    else
    {
        lines = [self lineIndices];
    }

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

    while ((right - left) > 1)
    {
        mid = (right + left) / 2;
        lineStart = [[lines objectAtIndex:mid] unsignedIntegerValue];
        
        if (charIndex < lineStart)
        {
            right = mid;
        }
        else if (charIndex > lineStart)
        {
            left = mid;
        }
        else
        {
            return mid;
        }
    }
    return left;
}

- (NSDictionary *)textAttributes
{
    NSFont *font;
    NSColor *color;
    
    font = [self font];
    if (font == nil)
    {
        font = [self defaultFont];
    }
    
    color = [self textColor];
    if (color == nil)
    {
        color = [self defaultTextColor];
    }
    
    return [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, color, NSForegroundColorAttributeName, nil];
}

- (NSDictionary *)markerTextAttributes
{
    NSFont *font;
    NSColor *color;
    
    font = [self font];
    if (font == nil)
    {
        font = [self defaultFont];
    }
    
    color = [self alternateTextColor];
    if (color == nil)
    {
        color = [self defaultAlternateTextColor];
    }
    
    return [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, color, NSForegroundColorAttributeName, nil];
}

- (CGFloat)requiredThickness
{
    NSUInteger lineCount, digits, i;
    NSMutableString *sampleString;
    NSSize stringSize;
    
    lineCount = [[self lineIndices] count];
    digits = 1;
    if (lineCount > 0)
    {
        digits = (NSUInteger)log10(lineCount) + 1;
    }
sampleString = [NSMutableString string];
    for (i = 0; i < digits; i++)
    {
        // Use "8" since it is one of the fatter numbers. Anything but "1"
        // will probably be ok here. I could be pedantic and actually find the fattest
// number for the current font but nah.
        [sampleString appendString:@"8"];
    }
    
    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
// return an integral value here.
    return ceil(MAX(DEFAULT_THICKNESS, stringSize.width + RULER_MARGIN * 2));
}

- (void)drawHashMarksAndLabelsInRect:(NSRect)aRect
{
    id view;
NSRect bounds;

bounds = [self bounds];

if (_backgroundColor != nil)
{
[_backgroundColor set];
NSRectFill(bounds);

[[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))];
}

    view = [self clientView];

    if ([view isKindOfClass:[NSTextView class]])
    {
        NSLayoutManager *layoutManager;
        NSTextContainer *container;
        NSRect visibleRect, markerRect;
        NSRange range, glyphRange, nullRange;
        NSString *text, *labelText;
        NSUInteger rectCount, index, line, count;
        NSRectArray rects;
        CGFloat ypos, yinset;
        NSDictionary *textAttributes, *currentTextAttributes;
        NSSize stringSize, markerSize;
NoodleLineNumberMarker *marker;
NSImage *markerImage;
NSMutableArray *lines;

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

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

        textAttributes = [self textAttributes];

lines = [self lineIndices];

        // Find the characters that are currently visible
        glyphRange = [layoutManager glyphRangeForBoundingRect:visibleRect inTextContainer:container];
        range = [layoutManager characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL];
        
        // Fudge the range a tad in case there is an extra new line at end.
        // It doesn't show up in the glyphs so would not be accounted for.
        range.length++;
        
        count = [lines count];
        
        for (line = [self lineNumberForCharacterIndex:range.location inText:text]; line < count; line++)
        {
            index = [[lines objectAtIndex:line] unsignedIntegerValue];
            
            if (NSLocationInRange(index, range))
            {
                rects = [layoutManager rectArrayForCharacterRange:NSMakeRange(index, 0)
                                     withinSelectedCharacterRange:nullRange
                                                  inTextContainer:container
                                                        rectCount:&rectCount];

                if (rectCount > 0)
                {
                    // Note that the ruler view is only as tall as the visible
                    // portion. Need to compensate for the clipview's coordinates.
                    ypos = yinset + NSMinY(rects[0]) - NSMinY(visibleRect);

marker = [_linesToMarkers objectForKey:[NSNumber numberWithUnsignedInteger:line]];

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

// Marker is flush right and centered vertically within the line.
markerRect.origin.x = NSWidth(bounds) - [markerImage size].width - 1.0;
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];
}
                    
                    // Line numbers are internally stored starting at 0
                    labelText = [NSString stringWithFormat:@"%jd", (intmax_t)line + 1];
                    
                    stringSize = [labelText sizeWithAttributes:textAttributes];

if (marker == nil)
{
currentTextAttributes = textAttributes;
}
else
{
currentTextAttributes = [self markerTextAttributes];
}

                    // Draw string flush right, centered vertically within the line
                    [labelText drawInRect:
                       NSMakeRect(NSWidth(bounds) - stringSize.width - RULER_MARGIN,
                                  ypos + (NSHeight(rects[0]) - stringSize.height) / 2.0,
                                  NSWidth(bounds) - RULER_MARGIN * 2.0, NSHeight(rects[0]))
                           withAttributes:currentTextAttributes];
                }
            }
if (index > NSMaxRange(range))
{
break;
}
        }
    }
}


- (NSUInteger)lineNumberForLocation:(CGFloat)location
{
NSUInteger line, count, index, rectCount, i;
NSRectArray rects;
NSRect visibleRect;
NSLayoutManager *layoutManager;
NSTextContainer *container;
NSRange nullRange;
NSMutableArray *lines;
id view;
    
view = [self clientView];
visibleRect = [[[self scrollView] contentView] bounds];

lines = [self lineIndices];
    
location += NSMinY(visibleRect);

if ([view isKindOfClass:[NSTextView class]])
{
nullRange = NSMakeRange(NSNotFound, 0);
layoutManager = [view layoutManager];
container = [view textContainer];
count = [lines count];

for (line = 0; line < count; line++)
{
index = [[lines objectAtIndex:line] unsignedIntegerValue];

rects = [layoutManager rectArrayForCharacterRange:NSMakeRange(index, 0)
withinSelectedCharacterRange:nullRange
inTextContainer:container
rectCount:&rectCount];

for (i = 0; i < rectCount; i++)
{
if ((location >= NSMinY(rects[i])) && (location < NSMaxY(rects[i])))
{
return line + 1;
}
}
}
}
return NSNotFound;
}

- (NoodleLineNumberMarker *)markerAtLine:(NSUInteger)line
{
return [_linesToMarkers objectForKey:[NSNumber numberWithUnsignedInteger:line - 1]];
}

- (void)setMarkers:(NSArray *)markers
{
NSEnumerator *enumerator;
NSRulerMarker *marker;

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

enumerator = [markers objectEnumerator];
while ((marker = [enumerator nextObject]) != nil)
{
[self addMarker:marker];
}
}

- (void)addMarker:(NSRulerMarker *)aMarker
{
if ([aMarker isKindOfClass:[NoodleLineNumberMarker class]])
{
[_linesToMarkers setObject:aMarker
forKey:[NSNumber numberWithUnsignedInteger:[(NoodleLineNumberMarker *)aMarker lineNumber] - 1]];
}
else
{
[super addMarker:aMarker];
}
}

- (void)removeMarker:(NSRulerMarker *)aMarker
{
if ([aMarker isKindOfClass:[NoodleLineNumberMarker class]])
{
[_linesToMarkers removeObjectForKey:[NSNumber numberWithUnsignedInteger:[(NoodleLineNumberMarker *)aMarker lineNumber] - 1]];
}
else
{
[super removeMarker:aMarker];
}
}

#pragma mark NSCoding methods

#define NOODLE_FONT_CODING_KEY @"font"
#define NOODLE_TEXT_COLOR_CODING_KEY @"textColor"
#define NOODLE_ALT_TEXT_COLOR_CODING_KEY @"alternateTextColor"
#define NOODLE_BACKGROUND_COLOR_CODING_KEY @"backgroundColor"

- (id)initWithCoder:(NSCoder *)decoder
{
if ((self = [super initWithCoder:decoder]) != nil)
{
if ([decoder allowsKeyedCoding])
{
_font = [[decoder decodeObjectForKey:NOODLE_FONT_CODING_KEY] retain];
_textColor = [[decoder decodeObjectForKey:NOODLE_TEXT_COLOR_CODING_KEY] retain];
_alternateTextColor = [[decoder decodeObjectForKey:NOODLE_ALT_TEXT_COLOR_CODING_KEY] retain];
_backgroundColor = [[decoder decodeObjectForKey:NOODLE_BACKGROUND_COLOR_CODING_KEY] retain];
}
else
{
_font = [[decoder decodeObject] retain];
_textColor = [[decoder decodeObject] retain];
_alternateTextColor = [[decoder decodeObject] retain];
_backgroundColor = [[decoder decodeObject] retain];
}

_linesToMarkers = [[NSMutableDictionary alloc] init];
}
return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
[super encodeWithCoder:encoder];

if ([encoder allowsKeyedCoding])
{
[encoder encodeObject:_font forKey:NOODLE_FONT_CODING_KEY];
[encoder encodeObject:_textColor forKey:NOODLE_TEXT_COLOR_CODING_KEY];
[encoder encodeObject:_alternateTextColor forKey:NOODLE_ALT_TEXT_COLOR_CODING_KEY];
[encoder encodeObject:_backgroundColor forKey:NOODLE_BACKGROUND_COLOR_CODING_KEY];
}
else
{
[encoder encodeObject:_font];
[encoder encodeObject:_textColor];
[encoder encodeObject:_alternateTextColor];
[encoder encodeObject:_backgroundColor];
}
}

@end
Something went wrong with that request. Please try again.