Checking if NSButton is down on drawRect
Asked Answered
B

4

6

I would like to check if my custom NSButton is currently in a pressed state (the user is clicking down on it) in my custom drawRect method. Something like this:

- (void)drawRect:(NSRect)dirtyRect{
    if ([self buttonIsInPressedState]) {
        [[self alternateBGImage] drawInRect:dirtyRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0f];
    }else{
        [[self BGImage] drawInRect:dirtyRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0f];
    }
    [super drawRect:dirtyRect];
}

How would you check a thing like that? Is it possible?

SOLUTION

I ended up checking the mouseDownFlags on the buttons cell. Don't know if it's the "right" way to do it, so let me know if you have a better suggestion:

- (void)drawRect:(NSRect)dirtyRect{        
    if ([self.cell mouseDownFlags] == 0) {
        [[self BGImage] drawInRect:dirtyRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0f];
    }else{
        [[self alternateBGImage] drawInRect:dirtyRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0f];
    }

    [super drawRect:dirtyRect];
}
Bondage answered 22/2, 2013 at 4:48 Comment(0)
B
1

I ended up checking the mouseDownFlags on the buttons cell. Don't know if it's the "right" way to do it, so let me know if you have a better suggestion. Solution is updated in the question above.

Bondage answered 22/2, 2013 at 20:31 Comment(0)
A
5

I solved this by checking [self isHighlighted].

Antimatter answered 12/1, 2015 at 9:3 Comment(1)
I was about to get very angry with lifeHardtack
P
3

I don't think "drawRect:" is the right place to try to catch this.

You can subclass NSButton and then override either mouseDown: or setState: (looking for "NSOnState", which signifies the button is either selected or being pressed on).

Precis answered 22/2, 2013 at 5:21 Comment(1)
I've tried self.state, but it only works for wether the button is "On" or "Off"! So the button is "On", once I've pressed it down AND released it. I want to check for if the use is CURRENTLY pressing down. MouseDown is also an option, but I can't get mouseUp to work for some reason.Bondage
B
1

I ended up checking the mouseDownFlags on the buttons cell. Don't know if it's the "right" way to do it, so let me know if you have a better suggestion. Solution is updated in the question above.

Bondage answered 22/2, 2013 at 20:31 Comment(0)
G
0
if([pBtn state]==NSOnState){
   NSLog(@" button Pressed ")
}else{
   NSLog(@" button not pressed ");

}
Greenock answered 22/2, 2013 at 6:21 Comment(3)
I've tried self.state, but it only works for wether the button is "On" or "Off"! So the button is "On", once I've pressed it down AND released it. I want to check for if the use is CURRENTLY pressing down.Bondage
Can you make use of Push button, its basically a toggle button, perhaps you can get what you want to achieve..Greenock
I tried. Doesn't work. I ended up doing the above. Let me know if you have a better solution.Bondage

© 2022 - 2024 — McMap. All rights reserved.