Re: NSButtonCell inside custom NSCell not responding to mouse events
Re: NSButtonCell inside custom NSCell not responding to mouse events
- Subject: Re: NSButtonCell inside custom NSCell not responding to mouse events
- From: Oliver Quas <email@hidden>
- Date: Mon, 4 Jun 2007 12:20:57 +0200
Hello again,
i think i got some solution by myself. Although it's still not
perfect (the NSButtonCell doesn't react visually on mouse events yet,
like showing its alternate image...). I had to override mouse
tracking inside the custom NSCell (OQCell). Now the target/action of
the NSButtonCell get's called.
Here is what i added to the code in my previous mail:
- (BOOL)trackMouse:(NSEvent *)theEvent inRect:(NSRect)cellFrame
ofView:(NSView *)controlView untilMouseUp:(BOOL)flag
{
NSPoint point;
NSRect rect, closeRect;
// get the location of the event in screen coordinates and convert it
to the control view's corrdinate
point = [theEvent locationInWindow];
point = [controlView convertPoint:point fromView:nil];
rect = [self drawingRectForBounds:cellFrame];
closeRect = [self closeButtonCellRectForBounds:rect];
if (NSPointInRect(point, closeRect)) {
[closeButtonCell trackMouse:theEvent inRect:closeRect
ofView:controlView untilMouseUp:flag];
return YES;
}
return NO;
}
Note, that OQCell by default does nothing when being clicked. The
tracking takes place only inside the NSButtonCell's drawing
rectangle, which is returned by the method (void)
closeButtonCellRectForBounds:(NSRect)aRect.
Any comments?
Cheers,
oliver
Am 04.06.2007 um 11:40 schrieb Oliver Quas:
Hello,
i implemented a custom NSCell class named OQCell (see code below)
for use as NSTableView's dataCell. For user interaction purposes,
OQCell contains a NSButtonCell inside, which when clicked, should
invoke @selector(closeButtonAction:) on OQCell. Right now, the
NSButtonCell draws fine inside OQCell, but when clicking it,
nothing happens.
I guess it has something to do with mouse tracking inside the
NSButtonCell which never begins its job, right? How would i have to
implement this, so the NSButtonCell would work correctly?
Sorry if this has been asked/answered before - but i couldn't find
any solution to the problem, neither on the web, nor in the docs,
and my own implementation doesn't work as expected. So any help is
greatly appreciated.
Thanks in advance!
oliver
here's an excerpt from the code of my custom cell:
@interface OQCell : NSCell <NSCoding, NSCopying> {
NSButtonCell *closeButtonCell;
}
- (void)closeButtonAction:(id)sender;
@end
@implementation OQCell
#pragma mark -
#pragma mark Init & Destroy
- (id)initTextCell:(NSString *)aString
{
if (self = [super initTextCell:aString]) {
closeButtonCell = [[NSButtonCell alloc] init];
[closeButtonCell setButtonType:NSPushInCell];
[closeButtonCell setTarget:self];
[closeButtonCell setAction:@selector(closeButtonAction:)];
}
return self;
}
- (void)dealloc
{
[closeButtonCell release];
[super dealloc];
}
#pragma mark -
#pragma mark NSCoding
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
[decoder decodeObjectForKey:@"closeButtonCell"];
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[super encodeWithCoder:coder];
[coder encodeObject:closeButtonCell forKey:@"closeButtonCell"];
}
#pragma mark -
#pragma mark NSCopying
- (id)copyWithZone:(NSZone *)zone
{
OQCell *returnValue;
returnValue = [super copyWithZone:zone];
returnValue->closeButtonCell = [closeButtonCell copy];
return returnValue;
}
#pragma mark -
#pragma mark Accessors
- (void)closeButtonAction:(id)sender
{
// if the buttonCell is clicked, this method should be invoked
NSLog(@"yay! :)");
}
#pragma mark -
#pragma mark NSCell Subclass
- (void)setControlView:(NSView *)view
{
[super setControlView:view];
// keep our buttonCell updated as well, because it's inside the
same controlView
[closeButtonCell setControlView:view];
}
#pragma mark -
#pragma mark Drawing
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)
controlView
{
// do some drawing...
// now draw our buttonCell...
[closeButtonCell drawWithFrame:someRect inView:controlView];
}
@end
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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)
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