Skip to content

Commit

Permalink
New table view delegate method to restrict selection of row based on …
Browse files Browse the repository at this point in the history
…the event

Fixed table view cell higlighting on right mouse click
  • Loading branch information
brutella committed Aug 6, 2011
1 parent 3fc5ed4 commit 07a15fd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
8 changes: 6 additions & 2 deletions ExampleProject/ConcordeExample/ExampleView.m
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,15 @@ - (void)tableView:(TUITableView *)tableView didClickRowAtIndexPath:(TUIFastIndex
}

if(event.type == NSRightMouseUp){
NSLog(@"right mouse up");
// show context menu
}
}
- (BOOL)tableView:(TUITableView *)tableView shouldSelectRowAtIndexPath:(TUIFastIndexPath *)indexPath forEvent:(NSEvent *)event{
switch (event.type) {
case NSRightMouseDown:
return NO;
}

- (BOOL)tableViewShouldSelectRowOnRightClick:(TUITableView*)tableView{
return YES;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/UIKit/TUITableView.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ typedef enum {
@optional

- (void)tableView:(TUITableView *)tableView willDisplayCell:(TUITableViewCell *)cell forRowAtIndexPath:(TUIFastIndexPath *)indexPath; // not implemented yet
- (void)tableView:(TUITableView *)tableView didSelectRowAtIndexPath:(TUIFastIndexPath *)indexPath; // happens on mouse down
- (void)tableView:(TUITableView *)tableView didSelectRowAtIndexPath:(TUIFastIndexPath *)indexPath; // happens on left/right mouse down, key up/down
- (void)tableView:(TUITableView *)tableView didDeselectRowAtIndexPath:(TUIFastIndexPath *)indexPath;
- (void)tableView:(TUITableView *)tableView didClickRowAtIndexPath:(TUIFastIndexPath *)indexPath withEvent:(NSEvent *)event; // happens on mouse up (can look at clickCount)
- (void)tableView:(TUITableView *)tableView didClickRowAtIndexPath:(TUIFastIndexPath *)indexPath withEvent:(NSEvent *)event; // happens on left/right mouse up (can look at clickCount)

- (BOOL)tableViewShouldSelectRowOnRightClick:(TUITableView*)tableView; // NO, if not implemented
- (BOOL)tableView:(TUITableView*)tableView shouldSelectRowAtIndexPath:(TUIFastIndexPath*)indexPath forEvent:(NSEvent*)event; // YES, if not implemented

@end

Expand Down
11 changes: 7 additions & 4 deletions lib/UIKit/TUITableView.m
Original file line number Diff line number Diff line change
Expand Up @@ -873,9 +873,10 @@ - (BOOL)performKeyAction:(NSEvent *)event
}
newIndexPath = [TUIFastIndexPath indexPathForRow:row inSection:section];
}

[self selectRowAtIndexPath:newIndexPath animated:self.animateSelectionChanges scrollPosition:TUITableViewScrollPositionToVisible];

if(![_delegate respondsToSelector:@selector(tableView:shouldSelectRowAtIndexPath:forEvent:)] || [_delegate tableView:self shouldSelectRowAtIndexPath:newIndexPath forEvent:event]){
[self selectRowAtIndexPath:newIndexPath animated:self.animateSelectionChanges scrollPosition:TUITableViewScrollPositionToVisible];
}

return YES;
}

Expand Down Expand Up @@ -903,7 +904,9 @@ - (BOOL)performKeyAction:(NSEvent *)event
newIndexPath = [TUIFastIndexPath indexPathForRow:row inSection:section];
}

[self selectRowAtIndexPath:newIndexPath animated:self.animateSelectionChanges scrollPosition:TUITableViewScrollPositionToVisible];
if(![_delegate respondsToSelector:@selector(tableView:shouldSelectRowAtIndexPath:forEvent:)] || [_delegate tableView:self shouldSelectRowAtIndexPath:newIndexPath forEvent:event]){
[self selectRowAtIndexPath:newIndexPath animated:self.animateSelectionChanges scrollPosition:TUITableViewScrollPositionToVisible];
}

return YES;
}
Expand Down
16 changes: 11 additions & 5 deletions lib/UIKit/TUITableViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,14 @@ - (BOOL)acceptsFirstMouse:(NSEvent *)event

- (void)mouseDown:(NSEvent *)event
{
[super mouseDown:event];

TUITableView *tableView = self.tableView;
[tableView selectRowAtIndexPath:self.indexPath animated:tableView.animateSelectionChanges scrollPosition:TUITableViewScrollPositionNone];
[super mouseDown:event]; // may make the text renderer first responder, so we want to do the selection before this
_tableViewCellFlags.highlighted = 1;
[self setNeedsDisplay];
if(![tableView.delegate respondsToSelector:@selector(tableView:shouldSelectRowAtIndexPath:forEvent:)] || [tableView.delegate tableView:tableView shouldSelectRowAtIndexPath:self.indexPath forEvent:event]){
[tableView selectRowAtIndexPath:self.indexPath animated:tableView.animateSelectionChanges scrollPosition:TUITableViewScrollPositionNone];
_tableViewCellFlags.highlighted = 1;
[self setNeedsDisplay];
}
}

- (void)mouseUp:(NSEvent *)event
Expand All @@ -99,7 +102,7 @@ - (void)rightMouseDown:(NSEvent *)event{
[super rightMouseDown:event];

TUITableView *tableView = self.tableView;
if([tableView.delegate respondsToSelector:@selector(tableViewShouldSelectRowOnRightClick:)] && [tableView.delegate tableViewShouldSelectRowOnRightClick:tableView]){
if(![tableView.delegate respondsToSelector:@selector(tableView:shouldSelectRowAtIndexPath:forEvent:)] || [tableView.delegate tableView:tableView shouldSelectRowAtIndexPath:self.indexPath forEvent:event]){
[tableView selectRowAtIndexPath:self.indexPath animated:tableView.animateSelectionChanges scrollPosition:TUITableViewScrollPositionNone];
_tableViewCellFlags.highlighted = 1;
[self setNeedsDisplay];
Expand All @@ -108,6 +111,9 @@ - (void)rightMouseDown:(NSEvent *)event{

- (void)rightMouseUp:(NSEvent *)event{
[super rightMouseUp:event];
_tableViewCellFlags.highlighted = 0;
[self setNeedsDisplay];

if([self eventInside:event]) {
TUITableView *tableView = self.tableView;
if([tableView.delegate respondsToSelector:@selector(tableView:didClickRowAtIndexPath:withEvent:)]){
Expand Down

0 comments on commit 07a15fd

Please sign in to comment.