forked from gavinkwoe/todparsekit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTDGutterView.m
92 lines (72 loc) · 2.23 KB
/
TDGutterView.m
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
//
// TDGutterView.m
// TextTest
//
// Created by Todd Ditchendorf on 9/9/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "TDGutterView.h"
@interface TDGutterView ()
@property (retain) NSDictionary *attrs;
@end
@implementation TDGutterView
- (void)awakeFromNib {
self.attrs = [NSDictionary dictionaryWithObjectsAndKeys:
[NSFont userFixedPitchFontOfSize:11.], NSFontAttributeName,
[NSColor grayColor], NSForegroundColorAttributeName,
nil];
}
- (void)dealloc {
self.sourceScrollView = nil;
self.sourceTextView = nil;
self.lineNumberRects = nil;
self.attrs = nil;
[super dealloc];
}
- (BOOL)isFlipped {
return YES;
}
- (NSUInteger)autoresizingMask {
return NSViewHeightSizable;
}
- (void)drawRect:(NSRect)rect {
NSDrawWindowBackground(rect);
CGFloat rectWidth = rect.size.width;
NSPoint p1 = NSMakePoint(rectWidth + 2., 0.);
NSPoint p2 = NSMakePoint(rectWidth + 2., rect.size.height);
[NSBezierPath strokeLineFromPoint:p1 toPoint:p2];
if (![lineNumberRects count]) {
return;
}
NSUInteger i = startLineNumber;
NSUInteger count = i + [lineNumberRects count];
for ( ; i < count; i++) {
NSRect r = [[lineNumberRects objectAtIndex:i - startLineNumber] rectValue];
// set the x origin of the number according to the number of digits it contains
CGFloat x = 0.;
if (i < 9) {
x = rectWidth - 14.;
} else if (i < 99) {
x = rectWidth - 21.;
} else if (i < 999) {
x = rectWidth - 28.;
} else if (i < 9999) {
x = rectWidth - 35.;
}
r.origin.x = x;
// center the number vertically for tall lines
if (r.origin.y) {
r.origin.y += r.size.height/2. - 7.;
}
NSString *s = [[NSNumber numberWithInteger:i + 1] stringValue];
NSAttributedString *as = [[NSAttributedString alloc] initWithString:s attributes:attrs];
[as drawAtPoint:r.origin];
[as release];
}
}
@synthesize sourceScrollView;
@synthesize sourceTextView;
@synthesize lineNumberRects;
@synthesize startLineNumber;
@synthesize attrs;
@end