Adding a new class called BreakpointManager so we don't clutter up AppDelegate
[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 #import "Breakpoint.h"
19 #import "BSSourceView.h"
20
21 @implementation BSLineNumberView
22
23 @synthesize sourceView, lineNumberRange, markers;
24
25 /**
26 * Initializer for the line number view
27 */
28 - (id)initWithFrame:(NSRect)frame
29 {
30 if (self = [super initWithFrame:frame])
31 {
32 lineNumberRange = NSMakeRange(0, 0);
33 }
34 return self;
35 }
36
37 /**
38 * Flip the coordinates
39 */
40 - (BOOL)isFlipped
41 {
42 return YES;
43 }
44
45 /**
46 * Draws the line numbers whenever necessary
47 */
48 - (void)drawRect:(NSRect)rect
49 {
50 // background color
51 [[NSColor colorWithDeviceRed:0.871 green:0.871 blue:0.871 alpha:1] set];
52 [NSBezierPath fillRect:rect];
53
54 [[NSColor blackColor] set];
55 [NSBezierPath strokeLineFromPoint:NSMakePoint(rect.origin.x, rect.origin.y) toPoint:NSMakePoint(rect.origin.x + rect.size.width, rect.origin.y)];
56
57 [[NSColor grayColor] set];
58 [NSBezierPath strokeLineFromPoint:NSMakePoint(rect.origin.x, rect.size.height) toPoint:NSMakePoint(rect.origin.x + rect.size.width, rect.size.height)];
59
60 // font attributes for the line number
61 NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"Monaco" size:9.0], NSFontAttributeName, [NSColor grayColor], NSForegroundColorAttributeName, nil];
62
63 lineNumberRange = NSMakeRange(0, 0);
64
65 unsigned i = 0, line = 1;
66 while (i < [[[sourceView textView] layoutManager] numberOfGlyphs])
67 {
68 NSRange fragRange;
69 NSRect fragRect = [self convertRect:[[[sourceView textView] layoutManager] lineFragmentRectForGlyphAtIndex:i effectiveRange:&fragRange] fromView:[sourceView textView]];
70 fragRect.origin.x = rect.origin.x; // horizontal scrolling matters not
71
72 // we want to paint the top and bottom line number even if they're cut off
73 NSRect testRect = rect;
74 testRect.origin.y -= fragRect.size.height - 1;
75 testRect.size.height += fragRect.size.height - 1;
76 if (NSPointInRect(fragRect.origin, testRect))
77 {
78 lineNumberRange.location = (lineNumberRange.length == 0 ? line : lineNumberRange.location);
79 lineNumberRange.length++;
80 NSString *num = [NSString stringWithFormat:@"%u", line];
81 NSSize strSize = [num sizeWithAttributes:attrs];
82 [num drawAtPoint:NSMakePoint([self frame].size.width - strSize.width - 3, fragRect.origin.y + ((fragRect.size.height - strSize.height) / 2)) withAttributes:attrs];
83 if ([markers containsObject:[[Breakpoint alloc] initWithLine:line inFile:[sourceView file]]])
84 {
85 NSLog(@"marking %i", line);
86 }
87 }
88
89 i += fragRange.length;
90 line++;
91 }
92 }
93
94 /**
95 * Handles the mouse down event (which is adding, deleting, and toggling breakpoints)
96 */
97 - (void)mouseDown:(NSEvent *)event
98 {
99 NSTextView *textView = [sourceView textView];
100
101 NSPoint clickLoc = [self convertPoint:[event locationInWindow] fromView:nil];
102
103 unsigned line = 1;
104 unsigned i = 0;
105 while (i < [[textView layoutManager] numberOfGlyphs])
106 {
107 NSRange fragRange;
108 NSRect fragRect = [[textView layoutManager] lineFragmentRectForGlyphAtIndex:i effectiveRange:&fragRange];
109 fragRect.size.width = [self bounds].size.width;
110 if (NSPointInRect(clickLoc, fragRect))
111 {
112 [[sourceView delegate] gutterClickedAtLine:(line + lineNumberRange.location - 1) forFile:[sourceView file]];
113 return;
114 }
115
116 i += fragRange.length;
117 line++;
118 }
119 }
120
121 @end