Re: NSOutlineView, tracking in a custom NSTextFieldCell, & Shift&Cmd Keys
Re: NSOutlineView, tracking in a custom NSTextFieldCell, & Shift&Cmd Keys
- Subject: Re: NSOutlineView, tracking in a custom NSTextFieldCell, & Shift&Cmd Keys
- From: Eric Gorr <email@hidden>
- Date: Fri, 16 Jan 2009 12:35:33 -0500
Unfortunately, this does not appear to be a working solution quite yet.
I have overridden the mouseDown method in my NSOutlineView subclass to
be:
- (void)mouseDown:(NSEvent *)theEvent
{
[super mouseDown:theEvent];
NSPoint eventLocation = [theEvent locationInWindow];
NSPoint localPoint = [self convertPoint:eventLocation
fromView:nil];
NSInteger row = [self rowAtPoint:localPoint];
if ( row >= 0 ) {
id item = [self itemAtRow:row];
BOOL isGroupItem = [[self delegate]
outlineView:self isGroupItem:item];
NSUInteger modifierFlags = [theEvent modifierFlags];
BOOL withShiftKey = ( modifierFlags &
NSShiftKeyMask ) == NSShiftKeyMask;
BOOL withCmdKey = ( modifierFlags &
NSCommandKeyMask ) == NSCommandKeyMask;
if ( !isGroupItem && ( withShiftKey || withCmdKey ) ) {
MyCell *aCell = (MyCell *)[self
preparedCellAtColumn:0 row:row];
NSRect cellRect = [self frameOfCellAtColumn:0 row:row];
[aCell trackMouse:theEvent inRect:cellRect ofView:self
untilMouseUp:YES];
}
}
}
The trackMouse method of MyCell looks like:
- (BOOL)trackMouse:(NSEvent *)theEvent inRect:(NSRect)cellFrame ofView:
(NSView *)controlView untilMouseUp:(BOOL)untilMouseUp
{
NSLog( @"trackMouse - %@", NSStringFromRect( cellFrame ) );
[self setControlView:controlView];
//NSRect infoButtonRect = [self infoButtonRectForBounds:cellFrame];
while ( [theEvent type] != NSLeftMouseUp ) {
// This is VERY simple event tracking.
NSPoint point = [controlView convertPoint:[theEvent
locationInWindow] fromView:nil];
if ([theEvent type] == NSMouseEntered || [theEvent type] ==
NSMouseExited) {
[NSApp sendEvent:theEvent];
}
// Note that we process mouse entered and exited events and
dispatch them to properly handle updates
theEvent = [[controlView window] nextEventMatchingMask:
(NSLeftMouseUpMask | NSLeftMouseDraggedMask | NSMouseEnteredMask |
NSMouseExitedMask)];
}
NSLog( @"trackMouse mouseUp - %@", self );
//
// some extra processing of the mouseUp removed
//
// We return YES since the mouse was released while we were
tracking.
// Not returning YES when you processed the mouse up is an easy
way to introduce bugs!
return YES;
}
While I do see that the trackMouse method is called, it seems to enter
into an endless loop and only after I click again do I see the log
message 'trackMouse mouseUp'. I have tried placing the call [super
mouseDown:theEvent]; at the end of the mouseDown method, but I see the
same behavior.
For some reason, the mouseUp event of the click has been consumed or
something else strange is going on which is preventing me getting the
notification that it has occurred.
If anyone has any thoughts on the matter, I would be interested.
I think I will go off a file a bug as well requesting that some
control be given to the delegate to decide whether or not a mouse
click with the Shift or Cmd Key held down should be passed off to a
cell.
On Jan 15, 2009, at 6:53 PM, Corbin Dunn wrote:
Reply for cocoa-dev:
Eric is right -- NSTableView won't do tracking in a cell if the
shift or alt keys are down. This is intentionally done, and has
always been the case.
A possible work around is what Eric is thinking about, which is to
override mouseDown and do his own trackMouse logic. Note that it
probably should call super first, in order to select the said row
before hand.
corbin
On Jan 15, 2009, at 2:10 PM, Eric Gorr wrote:
Thanks.
Any thoughts on whether or not it is even valid to call trackMouse
when obtaining a cell this (via preparedCellAtColumn) way?
On Jan 15, 2009, at 4:16 PM, Corbin Dunn wrote:
You want -frameOfCellAtRow:column:, not frameOfOutlineCellAtRow:row.
Do you get a call to this method, if implemented on your delegate/
datasource:
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldTrackCell:
(NSCell *)cell forTableColumn:(NSTableColumn *)tableColumn item:
(id)item
corbin
On Jan 15, 2009, at 12:53 PM, Eric Gorr wrote:
Now, one idea I had was to override the mouseDown method of my
NSOutlineView to do something like:
- (void)mouseDown:(NSEvent *)theEvent
{
NSPoint eventLocation = [theEvent locationInWindow];
NSPoint localPoint = [self convertPoint:eventLocation
fromView:nil];
NSUInteger modifierFlags = [theEvent modifierFlags];
BOOL withShiftKey = ( modifierFlags & NSShiftKeyMask )
== NSShiftKeyMask;
BOOL withCmdKey = ( modifierFlags &
NSCommandKeyMask ) == NSCommandKeyMask;
NSInteger row = [self rowAtPoint:localPoint];
id item = [self itemAtRow:row];
if ( withShiftKey || withCmdKey ) {
MyCellClass *aCell = (MyCellClass *)[self
preparedCellAtColumn:0 row:row];
NSRect cellRect = [self frameOfOutlineCellAtRow:row];
[aCell trackMouse:theEvent inRect:cellRect ofView:self
untilMouseUp:YES];
}
[super mouseDown:theEvent];
}
Now, obviously, this isn't complete. For example, a lot more
checking needs to be done to make sure if I really want to call
trackMouse as I do have group rows and no special processing is
needed for them.
Unfortunately, this also doesn't work as I am uncertain how to
get the correct frame for the cell in the table. I am also not
certain if it would even be valid to call trackMouse when
obtaining the cell in this way.
If anyone has any thoughts or comments, I am interested.
On Jan 14, 2009, at 4:19 PM, Eric Gorr wrote:
I am using the pattern found in the excellent PhotoSearch sample
code (http://developer.apple.com/samplecode/PhotoSearch/
index.html) to do some custom mouse tracking in a custom
NSTextFieldCell.
Unfortunately, if I am holding down the Cmd or Shift key when I
click in a cell, the trackMouse method in my custom cell is not
called.
In my custom cell, I have multiple items. These items can
themselves be selected or not and I need to be able to handle
the standard selection behavior normally associated with the
shift & cmd keys.
Is there anything I can do about it?
_______________________________________________
_______________________________________________
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
References: | |
| >Custom tracking in a NSTextFieldCell (From: Eric Gorr <email@hidden>) |
| >Re: Custom tracking in a NSTextFieldCell (From: Eric Gorr <email@hidden>) |
| >NSOutlineView, tracking in a custom NSTextFieldCell, & Shift&Cmd Keys (From: Eric Gorr <email@hidden>) |
| >Re: NSOutlineView, tracking in a custom NSTextFieldCell, & Shift&Cmd Keys (From: Eric Gorr <email@hidden>) |
| >Re: NSOutlineView, tracking in a custom NSTextFieldCell, & Shift&Cmd Keys (From: Corbin Dunn <email@hidden>) |
| >Re: NSOutlineView, tracking in a custom NSTextFieldCell, & Shift&Cmd Keys (From: Eric Gorr <email@hidden>) |
| >Re: NSOutlineView, tracking in a custom NSTextFieldCell, & Shift&Cmd Keys (From: Corbin Dunn <email@hidden>) |