0

How do I get the button's rect in this method?

In the below method the NSRect buttonRect = ??? is the place where I need to get the frame / rect of NSButtonCell object named _buttonCell.

- (NSCellHitResult)hitTestForEvent:(NSEvent *)event
                            inRect:(NSRect)cellFrame
                            ofView:(NSView *)controlView {

    NSCellHitResult hitType = [super hitTestForEvent:event inRect:cellFrame ofView:controlView];

    NSPoint location = [event locationInWindow];
    location = [controlView convertPoint:location fromView:nil];

    NSRect buttonRect = ??? //_buttonCell.frame;

    if (NSMouseInRect(location, buttonRect, [controlView isFlipped])) {
        // We are only sent tracking messages for trackable areas.

        hitType |= NSCellHitTrackableArea;

        NSLog(@"hit ");
        [_buttonCell setState:NSControlStateValueOn];
    }
    return hitType;
}

The _buttonCell is drawn as:

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView{

    int x = cellFrame.origin.x;
    int y = cellFrame.origin.y;

    NSRect buttonRect = NSMakeRect(x, y, 24, 24);
    NSRect imageRect = NSMakeRect(x+24, y, 24, 24);
    NSRect textRect = NSMakeRect(x+48, y, cellFrame.size.width-50, 24);

    [_buttonCell drawWithFrame:buttonRect inView:controlView];
    [_imageCell drawWithFrame:imageRect inView:controlView];
    [_textCell drawWithFrame:textRect inView:controlView];

}
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • Is the `cellFrame` argument what you're looking for? – Willeke Jun 14 '20 at 08:32
  • @Willeke: yes kind of. But NSButtonCell has no parameter, method to give cellFrame. – Anoop Vaidya Jun 14 '20 at 13:15
  • Please edit the question and explain which frame of which cell in which method you need. – Willeke Jun 14 '20 at 13:47
  • @Willeke: Edited, hope this is explanatory now. – Anoop Vaidya Jun 15 '20 at 05:42
  • 1
    Cells don't have frames. My assumption is that @AnoopVaidya wants to use `buttonRect` inside the `hitTestForEvent:inRect:ofView:` method which he calculates in the `drawWithFrame:inView:`. Not sure if I'm right. – zrzka Jun 15 '20 at 05:42
  • @zrzka: you are right – Anoop Vaidya Jun 15 '20 at 06:09
  • Is the [ComplexBrowser](https://developer.apple.com/library/archive/samplecode/ComplexBrowser/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008829) sample code of any help? – Willeke Jun 15 '20 at 11:32
  • @Willeke: sadly no. they dont have actions on the nsbrowsercell. I need to capture the event on the checkbox button. The row selection's event is gettable from the NSBrowser, but I need to work on the NSBrowserCell. – Anoop Vaidya Jun 15 '20 at 12:21
  • The question is "How to get a cell's frame", not "How to implement an action" or "how to capture the event". – Willeke Jun 15 '20 at 13:18
  • @Willeke: Rest I have managed to get the action. I just need the button's frame. – Anoop Vaidya Jun 15 '20 at 13:39

0 Answers0