Add a bunch of new NSView classes (to form a meta-view BSSourceView) that adds a...
[macgdbp.git] / Source / BSLineNumberView.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2008, Blue Static <http://www.bluestatic.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17 #import "BSLineNumberView.h"
18
19
20 @implementation BSLineNumberView
21
22 @synthesize sourceView;
23
24 /**
25 * Initializer for the line number view
26 */
27 - (id)initWithFrame:(NSRect)frame
28 {
29 if (self = [super initWithFrame:frame])
30 {
31
32 }
33 return self;
34 }
35
36 /**
37 * Flip the coordinates
38 */
39 - (BOOL)isFlipped
40 {
41 return YES;
42 }
43
44 /**
45 * Draws the line numbers whenever necessary
46 */
47 - (void)drawRect:(NSRect)rect
48 {
49 // background color
50 [[NSColor colorWithDeviceRed:0.871 green:0.871 blue:0.871 alpha:1] set];
51 [NSBezierPath fillRect:rect];
52
53 // font attributes for the line number
54 NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"Monaco" size:10.0], NSFontAttributeName, [NSColor grayColor], NSForegroundColorAttributeName, nil];
55
56 unsigned i = 0, line = 1;
57 while (i < [[[sourceView textView] layoutManager] numberOfGlyphs])
58 {
59 NSRange fragRange;
60 NSRect fragRect = [self convertRect:[[[sourceView textView] layoutManager] lineFragmentRectForGlyphAtIndex:i effectiveRange:&fragRange] fromView:[sourceView textView]];
61 fragRect.origin.x = rect.origin.x; // horizontal scrolling matters not
62
63 // we want to paint the top and bottom line number even if they're cut off
64 NSRect testRect = rect;
65 testRect.origin.y -= fragRect.size.height - 1;
66 testRect.size.height += fragRect.size.height - 1;
67 if (NSPointInRect(fragRect.origin, testRect))
68 {
69 NSString *num = [NSString stringWithFormat:@"%u", line];
70 NSSize strSize = [num sizeWithAttributes:attrs];
71 [num drawAtPoint:NSMakePoint([self frame].size.width - strSize.width - 3, fragRect.origin.y) withAttributes:attrs];
72 }
73
74 i += fragRange.length;
75 line++;
76 }
77 }
78
79 @end