From f0222bc3fd0476237a61d8947ebe25e90da491bc Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Tue, 1 Apr 2008 00:33:22 -0400 Subject: [PATCH] We now properly handle the mouseDown event in the gutter * Source/BSLineNumberView.h: New property called lineNumberRange * Source/BSLineNumberView.m: Synthesize lineNumberRange and set it to be empty in -[init] ([BSLineNumberView drawRect:]): Set lineNumberRange to be the range of visible lines in the source viewer ([BSLineNumberView mouseDown:]): Use the line number range to simplify the code of determining which line was clicked * Source/BSSourceView.h: New property file to hold the filename; also create delegate category * Source/BSSourceView.h: Synthesize file ([BSSourceView setFile:]): Sets the file property as well as setting the string of the textView object * Source/DebuggerWindowController.m: ([DebuggerWindowController updateSourceViewer]): Use the new -[setFile:] method --- Source/BSLineNumberView.h | 2 ++ Source/BSLineNumberView.m | 21 ++++++++------------- Source/BSSourceView.h | 7 +++++++ Source/BSSourceView.m | 11 ++++++++++- Source/DebuggerWindowController.m | 3 +-- 5 files changed, 28 insertions(+), 16 deletions(-) diff --git a/Source/BSLineNumberView.h b/Source/BSLineNumberView.h index b61aa2e..70e93de 100644 --- a/Source/BSLineNumberView.h +++ b/Source/BSLineNumberView.h @@ -21,8 +21,10 @@ @interface BSLineNumberView : NSView { BSSourceView *sourceView; + NSRange lineNumberRange; } @property(readwrite, assign) BSSourceView *sourceView; +@property(readonly) NSRange lineNumberRange; @end diff --git a/Source/BSLineNumberView.m b/Source/BSLineNumberView.m index d7c9f57..c20f1f2 100644 --- a/Source/BSLineNumberView.m +++ b/Source/BSLineNumberView.m @@ -19,7 +19,7 @@ @implementation BSLineNumberView -@synthesize sourceView; +@synthesize sourceView, lineNumberRange; /** * Initializer for the line number view @@ -28,7 +28,7 @@ { if (self = [super initWithFrame:frame]) { - + lineNumberRange = NSMakeRange(0, 0); } return self; } @@ -59,6 +59,8 @@ // font attributes for the line number NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"Monaco" size:9.0], NSFontAttributeName, [NSColor grayColor], NSForegroundColorAttributeName, nil]; + lineNumberRange = NSMakeRange(0, 0); + unsigned i = 0, line = 1; while (i < [[[sourceView textView] layoutManager] numberOfGlyphs]) { @@ -72,6 +74,8 @@ testRect.size.height += fragRect.size.height - 1; if (NSPointInRect(fragRect.origin, testRect)) { + lineNumberRange.location = (lineNumberRange.length == 0 ? line : lineNumberRange.location); + lineNumberRange.length++; NSString *num = [NSString stringWithFormat:@"%u", line]; NSSize strSize = [num sizeWithAttributes:attrs]; [num drawAtPoint:NSMakePoint([self frame].size.width - strSize.width - 3, fragRect.origin.y + ((fragRect.size.height - strSize.height) / 2)) withAttributes:attrs]; @@ -87,17 +91,10 @@ */ - (void)mouseDown:(NSEvent *)event { - // simplify our code a bit - NSScrollView *scrollView = [sourceView scrollView]; NSTextView *textView = [sourceView textView]; NSPoint clickLoc = [self convertPoint:[event locationInWindow] fromView:nil]; - // calculate the relative difference between height of the line number view and the document view - float adjust = (int)([scrollView documentVisibleRect].size.height + [scrollView documentVisibleRect].origin.y) % (int)[self bounds].size.height; - adjust += [[textView layoutManager] lineFragmentRectForGlyphAtIndex:0 effectiveRange:NULL].size.height; - clickLoc.y += adjust; // apply that to the click location to make it seem like we're clicking in an unclipped region - unsigned line = 1; unsigned i = 0; while (i < [[textView layoutManager] numberOfGlyphs]) @@ -105,15 +102,13 @@ NSRange fragRange; NSRect fragRect = [[textView layoutManager] lineFragmentRectForGlyphAtIndex:i effectiveRange:&fragRange]; fragRect.size.width = [self bounds].size.width; - //fragRect.origin.y += adjust; if (NSPointInRect(clickLoc, fragRect)) { - NSLog(@"clicked in %i", line); - break; + [[sourceView delegate] gutterClickedAtLine:line forFile:[sourceView file]]; + return; } i += fragRange.length; - //p.y += fragRect.size.height; line++; } } diff --git a/Source/BSSourceView.h b/Source/BSSourceView.h index da26f82..abf3f18 100644 --- a/Source/BSSourceView.h +++ b/Source/BSSourceView.h @@ -24,6 +24,7 @@ BSSourceViewTextView *textView; NSScrollView *scrollView; + NSString *file; int markedLine; id delegate; @@ -32,9 +33,15 @@ @property(readwrite, assign) BSLineNumberView *numberView; @property(readwrite, assign) BSSourceViewTextView *textView; @property(readwrite, assign) NSScrollView *scrollView; +@property(readwrite, assign) NSString *file; @property(readwrite, assign) int markedLine; @property(readwrite, assign) id delegate; +- (void)setFile:(NSString *)f; - (void)scrollToLine:(int)line; @end + +@interface NSObject (BSSourceViewDelegate) +- (void)gutterClickedAtLine:(int)line forFile:(NSString *)file; +@end diff --git a/Source/BSSourceView.m b/Source/BSSourceView.m index 6aef6b2..cf0d7e4 100644 --- a/Source/BSSourceView.m +++ b/Source/BSSourceView.m @@ -22,7 +22,7 @@ @implementation BSSourceView -@synthesize numberView, textView, scrollView, markedLine, delegate; +@synthesize numberView, textView, scrollView, markedLine, delegate, file; /** * Initializes the source view with the path of a file @@ -36,6 +36,15 @@ return self; } +/** + * Sets the file name as well as the text of the source view + */ +- (void)setFile:(NSString *)f +{ + file = f; + [textView setString:[NSString stringWithContentsOfFile:f]]; +} + /** * Flip the coordinates */ diff --git a/Source/DebuggerWindowController.m b/Source/DebuggerWindowController.m index 4806ce9..c5cd174 100644 --- a/Source/DebuggerWindowController.m +++ b/Source/DebuggerWindowController.m @@ -216,8 +216,7 @@ // get the filename and then set the text NSString *filename = [[stack objectAtIndex:selection] valueForKey:@"filename"]; filename = [[NSURL URLWithString:filename] path]; - NSString *text = [NSString stringWithContentsOfFile:filename]; - [[sourceViewer textView] setString:text]; + [sourceViewer setFile:filename]; int line = [[[stack objectAtIndex:selection] valueForKey:@"lineno"] intValue]; [sourceViewer setMarkedLine:line]; -- 2.43.5