Re: NSCell question (Last one for today)
Re: NSCell question (Last one for today)
- Subject: Re: NSCell question (Last one for today)
- From: Brian Webster <email@hidden>
- Date: Wed, 19 Nov 2003 16:22:55 -0600
Michael,
This is how I would go about this, although of course there are
probably many ways that it can be done. I've never actually done this
before, so there's probably some details that I'm missing, but I think
this should work, at least in theory. Anyone else out there feel free
to pipe in if there's something I've missed.
1. Declare three instance variables in your subclass of NSCell, all
three as NSButtonCells (or whatever else you might want).
2. In your init method(s), create the three button cells. Note that
the designated initializers for NSCell are initTextCell: and
initImageCell:, so you should probably be putting your init code in one
of these. You should also call one of these two for each NSButtonCell
you instantiate instead of init (I'm not sure offhand which method is
appropriate to use for NSButtonCell, so you might have to play around
with it).
3. In your NSCell subclass, override drawInteriorWithFrame:inView:.
Inside this method, you will have to calculate what rectangle each of
your three buttons will want to draw in, depending on the cellFrame
that gets passed in. Once you do that, then call
drawInteriorWithFrame:inView: on each of the three button cells with
the appropriate rect for that cell.
4. If you want to handle mouse events, you'll need to override the
NSCell method called trackMouse:inRect:ofView:untilMouseUp:. In this
method, you should determine which of your three buttons the mouse has
been clicked in (if any), and then pass on the mouse event by calling
the same method on the button cell that's being clicked.
There's the possibility that because of the way NSTableView handles
mouse clicks, that some of this might not work quite right. You also
might want to subclass NSActionCell instead of NSCell, since it gives
you some additional functionality, although it might not be necessary
in your case. Most Cocoa cells actually inherit from this and not
directly from NSCell. Also, whenever you subclass NSCell, you will
need to override copyWithZone: in order to implement the NSCopying
protocol, which is used by NSTableView sometimes. I've been burned by
that one before.
As far as hooking up actions etc. for the buttons, this should probably
be done by providing accessor methods for the button cells, and then
writing code in your controller that sets the appropriate target and
action for each cell.
On Nov 19, 2003, at 1:29 PM, Michael Becker wrote:
Hi!
This will be my last question for today, i promise :-)
I am trying to display 3 buttons within one cell in a tableview,
programmatically. I think the way to go is to subclass NSCell and draw
the buttons in it, connecting each one to their specific
targets/actions. Now I don't know at all how to DRAW these buttons. I
assume I should use drawInteriorWithFrame:, but I don't know what to do
with it. Here is my (most probably totally laughable) code for one
button (in a subclass of NSCell):
- (void)drawInteriorWithFrame:(NSRect)cellFrame
inView:(NSView*)controlView {
NSButton* theButton = [[ NSButton alloc] init];
[ theButton setButtonType:NSMomentaryLight];
[ theButton setTitle:@"Push me"];
[ controlView lockFocus];
[ theButton drawRect:cellFrame];
[ theButton setNeedsDisplay:YES];
[ controlView unlockFocus];
return;
}
Since this doesn't display anything (the method is invoked though) I
guess it's wrong. What would be the right way to do what I want to do?
Maybe even use an NSMatrix, or is that just for good looks? When it
works, how can I access the targets (which are obviously different
classes from the "outside")?
Thank you for bearing with me and my question :-)
Bye,
Michael
_______________________________________________
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.
--
Brian Webster
email@hidden
http://homepage.mac.com/bwebster
_______________________________________________
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.