Skip to content
This repository
Newer
Older
100644 117 lines (88 sloc) 3.728 kb
05eedb5e »
2010-01-25 initial add
1 //
2 // TDSourceCodeTextView.m
3 // TextTest
4 //
5 // Created by Todd Ditchendorf on 9/9/08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "TDSourceCodeTextView.h"
10 #import "TDGutterView.h"
11
12 @interface TDSourceCodeTextView ()
13 - (void)registerForNotifications;
14 - (void)getRectsOfVisibleLines:(NSArray **)outRects startingLineNumber:(NSUInteger *)outRect;
15 - (NSUInteger)lineNumberForIndex:(NSUInteger)inIndex;
16 @end
17
18 @implementation TDSourceCodeTextView
19
20 - (void)dealloc {
21 [[NSNotificationCenter defaultCenter] removeObserver:self];
22 self.gutterView = nil;
23 [super dealloc];
24 }
25
26
27 - (void)awakeFromNib {
28 [self registerForNotifications];
29 [self renderGutter];
30 }
31
32
33 - (void)textDidChange:(NSNotification *)n {
34 [self renderGutter];
35 }
36
37
38 - (void)viewBoundsChanged:(NSNotification *)n {
39 [self renderGutter];
40 }
41
42
43 - (void)renderGutter {
44 if (![[self window] isVisible]) return;
45 NSArray *rects = nil;
46 NSUInteger start = 0;
47 [self getRectsOfVisibleLines:&rects startingLineNumber:&start];
48 gutterView.lineNumberRects = rects;
49 gutterView.startLineNumber = start;
50 [gutterView setNeedsDisplay:YES];
51 }
52
53
54 - (void)registerForNotifications {
55 [[NSNotificationCenter defaultCenter] addObserver:self
56 selector:@selector(textDidChange:)
57 name:NSTextDidChangeNotification
58 object:self];
59
60 [scrollView.contentView setPostsBoundsChangedNotifications:YES];
61 [[NSNotificationCenter defaultCenter] addObserver:self
62 selector:@selector(viewBoundsChanged:)
63 name:NSViewBoundsDidChangeNotification
64 object:scrollView.contentView];
65 [[NSNotificationCenter defaultCenter] addObserver:self
66 selector:@selector(viewBoundsChanged:)
67 name:NSWindowDidResizeNotification
68 object:self.window];
69 }
70
71
72 //- (void)showFindIndicatorForRange:(NSRange)charRange;
73
74 - (void)getRectsOfVisibleLines:(NSArray **)outRects startingLineNumber:(NSUInteger *)outStart {
75 NSMutableArray *result = [NSMutableArray array];
76 NSString *s = self.string;
77
78 NSLayoutManager *layoutMgr = self.textContainer.layoutManager;
79 NSRect boundingRect = [scrollView.contentView documentVisibleRect];
80 CGFloat scrollY = boundingRect.origin.y;
81 NSRange visibleGlyphRange = [layoutMgr glyphRangeForBoundingRect:boundingRect inTextContainer:self.textContainer];
82
83 NSUInteger index = visibleGlyphRange.location;
253c2e01 »
2011-12-04 merging changes from google code
84 NSUInteger length = index + visibleGlyphRange.length;
05eedb5e »
2010-01-25 initial add
85
86 (*outStart) = [self lineNumberForIndex:index + 1];
87
88 while (index < length) {
89 NSRange r = [s lineRangeForRange:NSMakeRange(index, 0)];
90 index = NSMaxRange(r);
91 NSRect rect = [layoutMgr lineFragmentRectForGlyphAtIndex:r.location effectiveRange:NULL withoutAdditionalLayout:YES];
92 rect.origin.y -= scrollY;
93 [result addObject:[NSValue valueWithRect:rect]];
94 }
95
96 (*outRects) = result;
97 }
98
99
100 - (NSUInteger)lineNumberForIndex:(NSUInteger)inIndex {
101 NSString *s = self.string;
253c2e01 »
2011-12-04 merging changes from google code
102 NSUInteger numberOfLines, index, stringLength = s.length;
05eedb5e »
2010-01-25 initial add
103
104 for (index = 0, numberOfLines = 0; index < stringLength; numberOfLines++) {
105 NSRange r = [s lineRangeForRange:NSMakeRange(index, 0)];
106 index = NSMaxRange(r);
107 if (inIndex <= index) {
108 break;
109 }
110 }
111
112 return numberOfLines;
113 }
114
115 @synthesize gutterView;
116 @end
Something went wrong with that request. Please try again.