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 14:34:41 -0500
Note that for my custom cell class I have:
+ (BOOL)prefersTrackingUntilMouseUp {
// NSCell returns NO for this by default.
// If you want to have trackMouse:inRect:ofView:untilMouseUp:
// always track until the mouse is up, then you MUST return YES.
// Otherwise, strange things will happen.
return YES;
}
which I need for normal mouse clicks when when shift or cmd key is not
down. But I am not sure if this would be the cause of the problem or
not...just something to note for the moment.
What I think is happening is that when I call [super
mouseDown:theEvent], it consumes the mouseUp event. As such, when my
custom tracking occurs, there is no mouseUp event for it to process.
While I might be able to place the call to [super mouseDown:theEvent]
below my custom tracking code, it is unclear this would work as [super
mouseDown:theEvent] needs an equal chance to process events and my
trackMouse code can consume events and not tell anyone, when the shift
or cmd key is pressed, that [super mouseDown:theEvent] needs to know
about to execute correctly.
On Jan 16, 2009, at 12:35 PM, Eric Gorr wrote:
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:
@ericgorr.net
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
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>) |
| >Re: NSOutlineView, tracking in a custom NSTextFieldCell, & Shift&Cmd Keys (From: Eric Gorr <email@hidden>) |