-
Notifications
You must be signed in to change notification settings - Fork 1
/
PDAnnotatedRulerView.m
230 lines (147 loc) · 5.69 KB
/
PDAnnotatedRulerView.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
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
//
// PDAnnotatedRulerView.m
// SproutedInterface
//
// Created by Philip Dow on 5/23/07.
// Copyright Sprouted. All rights reserved.
// Inquiries should be directed to [email protected]
//
#import <SproutedInterface/PDAnnotatedRulerView.h>
// Ruler thickness value
#define RULER_THICKNESS 25
// Margin of displaying bookmarked line in a context menu.
#define STRIP_PREVIEW_MARGIN 15
// Default
#define DEFAULT_OPTION MNParagraphNumber | MNDrawBookmarks
@implementation PDAnnotatedRulerView
- (id)initWithScrollView:(NSScrollView *)aScrollView orientation:(NSRulerOrientation)orientation
{
if ( self = [super initWithScrollView:(NSScrollView *)aScrollView orientation:(NSRulerOrientation)orientation])
{
[self setScrollView:aScrollView];
[self setOrientation:orientation];
//load nib
//[NSBundle loadNibNamed:@"PDAnnotatedRuler" owner:self];
// Set default width
[self setRuleThickness:RULER_THICKNESS];
// Marker config
[self setReservedThicknessForMarkers:0];
[self setClientView:self]; // Markers ask me if I can add a marker.
// Add a dummy marker to draw properly
markerImage = [[NSImage alloc] initByReferencingFile:
[[NSBundle bundleForClass:[self class]] pathForResource:@"prioritymedium" ofType:@"tiff"]];
NSRulerMarker* aMarker = [self newMarker];
[self addMarker:aMarker];
[self removeMarker:aMarker];
// Set letter attributes
marginAttributes = [[NSMutableDictionary alloc] init];
[marginAttributes setObject:[NSFont labelFontOfSize:9] forKey: NSFontAttributeName];
[marginAttributes setObject:[NSColor darkGrayColor] forKey: NSForegroundColorAttributeName];
rulerOption = DEFAULT_OPTION;
markerDeleteReservationFlag = NO;
//
textView = [aScrollView documentView];
layoutManager = [textView layoutManager];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidUpdate:)
name:NSWindowDidUpdateNotification
object:[aScrollView window]];
[self setOption:MNDrawBookmarks];
}
return self;
}
#pragma mark -
- (void)drawMarkersInRect:(NSRect)aRect
{
//NSLog(@"drawMarkersInRect %@",NSStringFromRect(aRect));
if( (rulerOption & 0x10) == 0 )
return;
// *** (0) remove existing markers ***
// Delete markers unless while dragging.
NSArray* existingMarkers = [self markers];
unsigned hoge = 0;
for( hoge = 0; hoge < [existingMarkers count]; hoge++)
{
if( ! [[existingMarkers objectAtIndex:hoge] isDragging] )
[self removeMarker:[existingMarkers objectAtIndex:hoge]];
}
// Only get the visible part of the scroller view
NSRect documentVisibleRect = [[[layoutManager firstTextView] enclosingScrollView] documentVisibleRect];
// Find the glyph range for the visible glyphs
NSRange glyphRange = [layoutManager glyphRangeForBoundingRect: documentVisibleRect inTextContainer: [textView textContainer]];
NSRange charRange = [layoutManager characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL];
NSRange limitRange;
NSRange effectiveRange;
limitRange = charRange;
while (limitRange.length > 0) {
//for( hoge= charRange.location; hoge < NSMaxRange(charRange) ; hoge ++ )
//{
hoge = limitRange.location;
id attribute = [[textView textStorage] attribute:NSToolTipAttributeName
atIndex:limitRange.location longestEffectiveRange:&effectiveRange
inRange:limitRange];
if ( attribute != nil )
{
//if ( [[textView textStorage] hasBookmarkAtIndex:hoge inTextView:textView effectiveRange:effectiveRange] )
//{
//NSRange paragraphRange =
//[textView selectionRangeForProposedRange:NSMakeRange(hoge, 1) granularity:NSSelectByParagraph];
//unsigned glyphIndex = [layoutManager glyphRangeForCharacterRange:NSMakeRange(paragraphRange.location,1)
// actualCharacterRange:NULL].location;
unsigned glyphIndex = [layoutManager glyphRangeForCharacterRange:NSMakeRange(hoge,1)
actualCharacterRange:NULL].location;
NSRect drawingRect = [layoutManager lineFragmentRectForGlyphAtIndex:glyphIndex effectiveRange: NULL];
drawingRect.size.height = [markerImage size].height;
[self drawMarkerInRect:drawingRect ];
}
limitRange = NSMakeRange(NSMaxRange(effectiveRange), NSMaxRange(limitRange) - NSMaxRange(effectiveRange));
}
}
-(void)drawMarkerInRect:(NSRect)lineRect
// check if a marker should be drawn and draw it if necessary
{
lineRect = [textView convertRect:lineRect toView:self];
NSArray* markerObjects = [self markers];
unsigned hoge;
BOOL exist = NO;
for(hoge = 0; hoge < [markerObjects count]; hoge++)
{
//get represented object
NSRulerMarker* marker = [markerObjects objectAtIndex:hoge];
if( [[marker representedObject] isEqualToString:NSStringFromRect(lineRect) ] )
{
//if( ! [marker isDragging] )
// [marker setMarkerLocation: lineRect.origin.y + (lineRect.size.height / 2) ];
[[[self scrollView] backgroundColor] set];
NSRectFillUsingOperation(lineRect, NSCompositeSourceOver);
[marker drawRect:lineRect];
exist = YES;
}
}
if( exist == NO )
{
NSRulerMarker* aMarker = [self newMarker];
[aMarker setMarkerLocation: lineRect.origin.y + (lineRect.size.height / 2) ];
[[[self scrollView] backgroundColor] set];
NSRectFillUsingOperation(lineRect, NSCompositeSourceOver);
[aMarker drawRect:lineRect];
[aMarker setMovable:YES];
[aMarker setRemovable:YES];
[aMarker setRepresentedObject: NSStringFromRect(lineRect) ];
[self addMarker:aMarker];
}
}
#pragma mark -
- (void)mouseDown:(NSEvent *)theEvent
{
return;
}
- (void)mouseUp:(NSEvent *)theEvent
{
return;
}
- (void)mouseDragged:(NSEvent *)theEvent
{
return;
}
@end