Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Commit

Permalink
use setNeedsDisplayInRect: when drawing the selection
Browse files Browse the repository at this point in the history
  • Loading branch information
joshaber committed Jul 10, 2011
1 parent 6f1c002 commit 64d5b17
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/UIKit/TUITextRenderer+Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

- (CFIndex)stringIndexForPoint:(CGPoint)p;
- (void)resetSelection;
- (CGRect)rectForCurrentSelection;

- (void)copy:(id)sender;

Expand Down
40 changes: 37 additions & 3 deletions lib/UIKit/TUITextRenderer+Event.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ - (BOOL)beginWaitForDragInRange:(NSRange)range string:(NSString *)string

- (void)mouseDown:(NSEvent *)event
{
CGRect previousSelectionRect = [self rectForCurrentSelection];

switch([event clickCount]) {
case 4:
_selectionAffinity = TUITextSelectionAffinityParagraph;
Expand Down Expand Up @@ -180,13 +182,16 @@ - (void)mouseDown:(NSEvent *)event
self.hitRange = hitActiveRange;
}

[view setNeedsDisplay];
CGRect totalRect = CGRectUnion(previousSelectionRect, [self rectForCurrentSelection]);
[view setNeedsDisplayInRect:totalRect];
if([self acceptsFirstResponder])
[[view nsWindow] tui_makeFirstResponder:self];
}

- (void)mouseUp:(NSEvent *)event
{
CGRect previousSelectionRect = [self rectForCurrentSelection];

CFIndex i = [self stringIndexForEvent:event];
_selectionEnd = i;

Expand All @@ -204,14 +209,43 @@ - (void)mouseUp:(NSEvent *)event

_selectionAffinity = TUITextSelectionAffinityCharacter; // reset affinity

[view setNeedsDisplay];
CGRect totalRect = CGRectUnion(previousSelectionRect, [self rectForCurrentSelection]);
[view setNeedsDisplayInRect:totalRect];
}

- (void)mouseDragged:(NSEvent *)event
{
CGRect previousSelectionRect = [self rectForCurrentSelection];

CFIndex i = [self stringIndexForEvent:event];
_selectionEnd = i;
[view setNeedsDisplay];

CGRect totalRect = CGRectUnion(previousSelectionRect, [self rectForCurrentSelection]);
[view setNeedsDisplayInRect:totalRect];
}

- (CGRect)rectForCurrentSelection {
CTFrameRef textFrame = [self ctFrame];
CGRect totalRect = CGRectNull;
CFRange selectedRange = [self _selectedRange];
if(selectedRange.length > 0) {
CFIndex rectCount = 100;
CGRect rects[rectCount];
AB_CTFrameGetRectsForRange(textFrame, selectedRange, rects, &rectCount);

for(CFIndex i = 0; i < rectCount; ++i) {
CGRect rect = rects[i];
rect = CGRectIntegral(rect);

if(CGRectEqualToRect(totalRect, CGRectNull)) {
totalRect = rect;
} else {
totalRect = CGRectUnion(rect, totalRect);
}
}
}

return totalRect;
}

- (void)resetSelection
Expand Down

0 comments on commit 64d5b17

Please sign in to comment.