Re: ColorCell, NSActionCell subclass
Re: ColorCell, NSActionCell subclass
- Subject: Re: ColorCell, NSActionCell subclass
- From: Carl Gieringer <email@hidden>
- Date: Mon, 18 Apr 2005 10:59:38 -0800
FYI, I have looked at John Harte's code
http://www.cocoabuilder.com/archive/message/cocoa/2003/4/4/82836
http://homepage.mac.com/johnharte/
and this code does not accomplish what I want (hopefully what I want is
possible.) John's _Controller_ object takes care of receiving the new
color and setting the data source to have the new color (the controller
object is the data source.) Whereas I want the _cell_ to take care of
setting the new color.
It should be possible to set the new color in the data source, since I
think this must happen with a text cell, i.e. the field editor lets the
user type in a new string, and then when the field editor relinquishes
first responder status, the old string has been replaced by the new
string in the cell and within the data source which the cell
represents.
On Apr 17, 2005, at 21:53, Carl Gieringer wrote:
Hello,
I have not found this question definitively answered in the archives
or via google, but I apologize if I missed something.
I am trying to create a subclass of NSActionCell that displays a
color. Clicking on the cell presents the color panel, which allows
the user to change the color of the cell. I am having a problem
propagating the selection of a color back to the object which is
"represented" by the cell. In particular, I do not understand how to
get an NSTableView to call
-tableView:setObjectValue:forTableColumn:row: on its dataSource when I
am using this custom cell in one of the NSTableView's columns. I
thought that calling -setObjectValue on my cell would suffice, but it
doesn't.
My implementation follows; thanks!
- (id) init
{
if ( self = [super init] ) {
[self setEditable:YES];
[self setTarget:self];
[self setAction:@selector(changeColor:)];
}
return self;
}
- (IBAction) changeColor: (id) sender
{
NSColorPanel* colorPanel = [NSColorPanel sharedColorPanel];
// don't send the panel nil
[colorPanel setColor:
( [self objectValue] ? [self objectValue] : [NSColor whiteColor] )];
[colorPanel setTarget:self];
[colorPanel setAction: @selector(commitColor:)];
[colorPanel setContinuous:YES];
[colorPanel makeKeyAndOrderFront:self];
}
- (IBAction) commitColor:(id) sender
{
[self setObjectValue:[sender color]];
}
- (void) drawWithFrame:(NSRect) cellFrame inView:(NSView *) controlView
{
NSRect borderRect = NSInsetRect (cellFrame, 0.5, 0.5);
// use the smallest size to sqare off the box & center the box
if (borderRect.size.height < borderRect.size.width) {
borderRect.size.width = borderRect.size.height;
borderRect.origin.x = borderRect.origin.x + (cellFrame.size.width -
borderRect.size.width) / 2.0;
} else {
borderRect.size.height = borderRect.size.width;
borderRect.origin.y = borderRect.origin.y + (cellFrame.size.height -
borderRect.size.height) / 2.0;
}
[[NSColor grayColor] set];
[NSBezierPath strokeRect: borderRect];
[(NSColor *)[self objectValue] drawSwatchInRect:NSInsetRect
(borderRect, 2.0, 2.0)];
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
email@hidden
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden