Re: Implementing a Slightly Unusual NSTableView
Re: Implementing a Slightly Unusual NSTableView
- Subject: Re: Implementing a Slightly Unusual NSTableView
- From: Graham Cox <email@hidden>
- Date: Sun, 20 Apr 2008 11:04:50 +1000
Hi Peter,
I have solved this problem in the past (under Tiger, same code still
works fine on Leopard) and here's what I did - it may not be
completely generic but it but should give you the general idea. There
might be an easier/cleaner/better way to do it, but it did the trick
for me. On Leopard only I think there's more built-in support for this.
I had to subclass both NSTableView and the checkbox cell:
in MyTableView (subclasses NSTableView):
- (void) mouseDown:(NSEvent*) event
{
NSPoint p = [self convertPoint:[event locationInWindow] fromView:nil];
// which column and cell has been hit?
int column = [self columnAtPoint:p];
int row = [self rowAtPoint:p];
NSTableColumn* theColumn = [[self tableColumns] objectAtIndex:column];
id dataCell = [theColumn dataCellForRow:row];
// if the checkbox column, handle click in checkbox without selecting
the row
if ([dataCell isKindOfClass:[NSButtonCell class]])
{
// no way to get the button type for further testing, so we'll
plough on blindly
NSRect cellFrame = [self frameOfCellAtColumn:column row:row];
// track the button - this keeps control until the mouse goes up. If
the mouse was in on release,
// it will have changed the button's state and returns YES.
if ([dataCell trackMouse:event inRect:cellFrame ofView:self
untilMouseUp:YES])
{
// call the delegate to handle the checkbox state change as normal
[[self delegate] tableView:self setObjectValue:dataCell
forTableColumn:theColumn row:row];
}
}
else
[super mouseDown:event]; // for all other columns, work as normal
}
in MyCheckBoxCell (subclasses NSCell):
- (BOOL) trackMouse:(NSEvent *)theEvent inRect:(NSRect)cellFrame
ofView:(NSView *)controlView untilMouseUp:(BOOL)untilMouseUp
{
#pragma unused (theEvent, untilMouseUp)
[self setHighlighted:YES];
[controlView setNeedsDisplayInRect:cellFrame];
// keep control until mouse up
NSEvent* evt;
BOOL loop = YES;
BOOL wasIn, isIn;
int mask = NSLeftMouseUpMask | NSLeftMouseDraggedMask;
wasIn = YES;
while( loop )
{
evt = [[controlView window] nextEventMatchingMask:mask];
switch([evt type])
{
case NSLeftMouseDragged:
{
NSPoint p = [controlView convertPoint:[evt locationInWindow]
fromView:nil];
isIn = NSPointInRect( p, cellFrame );
if ( isIn != wasIn )
{
[self setHighlighted:isIn];
[controlView setNeedsDisplayInRect:cellFrame];
wasIn = isIn;
}
}
break;
case NSLeftMouseUp:
loop = NO;
break;
default:
break;
}
}
[self setHighlighted:NO];
// if the mouse was in the cell when it was released, flip the
checkbox state
if ( wasIn )
[self setIntValue:![self intValue]];
[controlView setNeedsDisplayInRect:cellFrame];
return wasIn;
}
hope this is useful,
G.
On 19 Apr 2008, at 8:05 pm, Peter Zegelin wrote:
I have a tableview representing the layers of a drawing application.
The tableview has several columns of checkboxes, some status columns
and an editable text column for the layer name. The rows of the
tableview are selectable (multiple selection) so they can be
reordered and cut, copied and pasted. I have everything except row
reordering working fine ( and this isn't a problem - so far!).
However as things currently are, every time a checkbox is clicked on
the row is selected as well, and the focus is shifted away from my
drawing view to this tableview. What I would really like to happen
is that if a user clicks on a checkbox then the row is *not*
selected at all and only if they click elsewhere would the tableview
select the row and get the focus. Even though the checkbox columns
are quite narrow there is still quite a bit of space where the
checkbox isn't affected and a couple of columns show only status
info so could be used to select the row.
The problem is I'm pretty new to Cocoa and haven't clue where to
start with this. Any suggestions how I might go about it would be
much appreciated.
thanks!
Peter
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden