Re: Checkbox as title for NSBox
Re: Checkbox as title for NSBox
- Subject: Re: Checkbox as title for NSBox
- From: email@hidden
- Date: Tue, 21 May 2002 12:30:02 -0700
Doug Brown wrote:
|I want to have an NSBox with a checkbox for its title for
|enabling/disabling entire features. I'm sure you know what I mean,
|because it's used in many applications. Today, I made some sample code
|to attempt to get this working, by looking in NSBox.h. (dirty code alert)
|
| [original code omitted]
Being a great believer in not doing work I don't have to do, I found a way to make the NSBox do all the calculations:
- (void) setTitleCell: (NSCell *) cell
{
[_titleCell autorelease];
_titleCell = [cell retain];
[self setTitlePosition: NSAtBottom];
[self setTitlePosition: NSAtTop];
}
The secret is that setTitlePosition: must actually *change* the position of the title cell before it will recalculate all the various coordinates.
|This code works pretty well when I pass the cell of a checkbox to the
|setTitleCell: method I made. I have two problems:
|
|1) The checkbox appears disabled in the box's title, whether I send
|setEnabled:YES to it or not. It appears checked no matter what, because
|I set its state to NSOnState. I can set it to NSOffState and it appears
|unchecked, but still disabled.
For whatever reason, with the above code I don't see the "always disabled" behavior you describe.
|2) Clicking on the checkbox does nothing. This is probably a side effect
|of the button appearing disabled.
I suspect that what you're seeing is NSBox ignoring mouse-down events. It's basically decoration, after all, and doesn't need to respond to them. To make it do something, you'll probably have to roll your own mouse-tracking code. (/Developer/Documentation/Cocoa/TasksAndConcepts/ProgrammingTopics/BasicEventHandling/Tasks/HandlingMouseEvents.html gives one example, but I think it's aimed at NSCell subclasses, rather than non-NSCell classes that use cells. The NSCell documentation lists various methods which are there to do much of the tracking work for you.)
What experimentation I did here revealed one major stumbling block: in the mouseDown: method, you have to call [self display] to get the checkbox redrawn. Given the code
- (void) mouseDown: (NSEvent *) event
{ NSRect frame = [self titleRect];
NSPoint localPt = [self convertPoint: [event locationInWindow] fromView:nil];
if(NSPointInRect(localPt, frame))
[_titleCell setState: YES];
else
[_titleCell setState: NO];
[self display];
}
that last "[self display]" is necessary. Without it, the cell state will change, but nothing shows up on the screen. Since tracking requires having the cell redrawn constantly as the mouse moves in and out of the cell, you'll have to find a way to make that happen. (It may be that the NSCell tracking methods do this. However, I tried the above code because the tracking didn't seem to be doing anything.)
Glen Fisher
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.