1

I have a totally custom NSControl with its totally custom NSCell. Now I want to implement some Mouse interaction. For example when user clicks over the control I want to change the control state to highlight so the questions are:

1) Where I have to deal with the mouse event? In the NSControl or directly in the NSCell?

At the moment I'm working with this code in the NSCell subclass:

-(BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView{
    [self setHighlighted:YES];

    return YES;
}

-(void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:(NSView *)controlView mouseIsUp:(BOOL)flag{
    [self setHighlighted:NO];
}

2) Is the NSCell state automatically managed by the NSControl? If I set the NSControl stete to highlight it will be mirrored to the NSCell?

3) and what about the enabled attributes? At the moment I wrote this code in the NSControl: And this code in the NSControl

-(void)setEnabled:(BOOL)flag{
    [super setEnabled:flag];
    [[self cell]setEnabled:flag];
    [self updateCell:[self cell]];
}

Have you particular suggestion to work with mouse event with a custom NSControl+NSCell?

MatterGoal
  • 16,038
  • 19
  • 109
  • 186

1 Answers1

0

You can do the following:

Create a NSTrackingArea, which will detect if the mouse got into your NSControl:

NSTrackingArea* trackingArea = [[[NSTrackingArea alloc] initWithRect:yourNSControlBoundsRect options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways owner:yourNSControl userInfo:nil];

Then you add your trackingArea to your NSControl:

[yourNSControl addTrackingArea:trackingArea];

In your NSControl subclass implement both

 - (void)mouseEntered:(NSEvent *)theEvent
 - (void)mouseExited:(NSEvent *)theEvent

There you can do something with your cell inside your control, or with your control itself.

Hope that helps

Boris Prohaska
  • 912
  • 6
  • 16
  • Why have I to modify the tracking area? – MatterGoal Dec 13 '12 at 16:02
  • You don't have to modify it at all. Its just a clean approach, because your NSControl subclass detects on its own when the mouse enters/exits and therefore it can act on it. – Boris Prohaska Dec 13 '12 at 16:11
  • I need to simulate a NSButton behavior so I don't need to know when mouse enters/exits my control. I just need to know which is the right way to manage the mouse event on a custom control. At the moment I'm trying to manage it at NSCell level. Do you think this is not a good idea? – MatterGoal Dec 13 '12 at 16:20
  • What do you mean by "the mouse event"?. If its only a click inside, you may need to override -(void)mouseDown:(NSEvent*)theEvent or -(void)mouseUp:(NSEvent*)theEvent. I would do this on the "Control" level, because if you add more subviews later to it, you don't need to change any code. – Boris Prohaska Dec 13 '12 at 16:24
  • Thank you for time Boris, Before creating this question I use exactly what you said in your previous comment. But MouseUp did't work as expected, reading some other incomplete answers I get that the right way is to work with tracking method in a NSCell. But this is the first time that I work with custom control and so I want to be sure that my choices are correct. You told me to work in a way that I already tried and that didn't work. – MatterGoal Dec 14 '12 at 07:52