From 180579d026ffae575a25e698c279c465dbd1bd6b Mon Sep 17 00:00:00 2001 From: rowanbeentje Date: Fri, 24 Apr 2009 01:07:20 +0000 Subject: - Add support for click and click-and-drag selection when the line number view receives mouse events. Enables easy selection of lines. --- Source/NoodleLineNumberView.h | 3 ++ Source/NoodleLineNumberView.m | 65 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) (limited to 'Source') diff --git a/Source/NoodleLineNumberView.h b/Source/NoodleLineNumberView.h index 7d12d47d..85316dd0 100644 --- a/Source/NoodleLineNumberView.h +++ b/Source/NoodleLineNumberView.h @@ -39,6 +39,9 @@ NSColor *textColor; NSColor *alternateTextColor; NSColor *backgroundColor; + + // Add support for selection by clicking/dragging + unsigned dragSelectionStartLine; } - (id)initWithScrollView:(NSScrollView *)aScrollView; diff --git a/Source/NoodleLineNumberView.m b/Source/NoodleLineNumberView.m index d1ecbd96..a4383102 100644 --- a/Source/NoodleLineNumberView.m +++ b/Source/NoodleLineNumberView.m @@ -28,7 +28,7 @@ // // This version of the NoodleLineNumberView for Sequel Pro removes marker -// functionality. +// functionality and adds selection by clicking on the ruler. #import "NoodleLineNumberView.h" @@ -439,6 +439,69 @@ } } +- (void)mouseDown:(NSEvent *)theEvent +{ + NSPoint location; + unsigned line; + NSTextView *view; + + if (![[self clientView] isKindOfClass:[NSTextView class]]) return; + view = (NSTextView *)[self clientView]; + + location = [self convertPoint:[theEvent locationInWindow] fromView:nil]; + line = [self lineNumberForLocation:location.y]; + dragSelectionStartLine = line; + + if (line != NSNotFound) + { + unsigned int selectionStart, selectionEnd; + NSMutableArray *lines = [self lineIndices]; + + selectionStart = [[lines objectAtIndex:(line - 1)] unsignedIntValue]; + if (line < [lines count]) { + selectionEnd = [[lines objectAtIndex:line] unsignedIntValue]; + } else { + selectionEnd = [[view string] length]; + } + [view setSelectedRange:NSMakeRange(selectionStart, selectionEnd - selectionStart)]; + } +} + +- (void)mouseDragged:(NSEvent *)theEvent +{ + NSPoint location; + unsigned line, startLine, endLine; + NSTextView *view; + + if (![[self clientView] isKindOfClass:[NSTextView class]] || dragSelectionStartLine == NSNotFound) return; + view = (NSTextView *)[self clientView]; + + location = [self convertPoint:[theEvent locationInWindow] fromView:nil]; + line = [self lineNumberForLocation:location.y]; + + if (line != NSNotFound) + { + unsigned int selectionStart, selectionEnd; + NSMutableArray *lines = [self lineIndices]; + if (line >= dragSelectionStartLine) { + startLine = dragSelectionStartLine; + endLine = line; + } else { + startLine = line; + endLine = dragSelectionStartLine; + } + + selectionStart = [[lines objectAtIndex:(startLine - 1)] unsignedIntValue]; + if (endLine < [lines count]) { + selectionEnd = [[lines objectAtIndex:endLine] unsignedIntValue]; + } else { + selectionEnd = [[view string] length]; + } + [view setSelectedRange:NSMakeRange(selectionStart, selectionEnd - selectionStart)]; + } + + [view autoscroll:theEvent]; +} #pragma mark NSCoding methods -- cgit v1.2.3