Re: NSTableView of NSColorWell
Re: NSTableView of NSColorWell
- Subject: Re: NSTableView of NSColorWell
- From: ber <email@hidden>
- Date: Sun, 22 Sep 2002 23:34:47 -0400
Wow. Thanks,
brian
On Sunday, September 22, 2002, at 11:30 PM, John Harte wrote:
>
>
On Sunday, September 22, 2002, at 09:53 PM, ber wrote:
>
>
> I'm want to create a table of color wells. Presumably I need to set
>
> the dataCell for the tableColumn.
>
> I read that Controls typically have Cells associated with them but I
>
> haven't been able to find the cell.
>
> [ myColorWell cell] and [ NSColorWell cellClass] return nil. Perhaps
>
> a color well is atypical. I'd be
>
> happy to use IB to set up the table but I haven't figured that out
>
> either. Jeez what a loser.
>
>
>
> Any pointers would be appreciated.
>
>
>
>
Here is the code I just wrote to do this very thing. Here is the code
>
from the test program I developed it in.
>
>
John
>
>
------ColorCell.h-----------------------------------------------
>
//
>
// ColorCell.h
>
// color cell test
>
//
>
// Created by John Harte on Sat Sep 14 2002.
>
// Copyright (c) 2002 __MyCompanyName__. All rights reserved.
>
//
>
>
#import <Cocoa/Cocoa.h>
>
>
@interface ColorCell : NSActionCell {}
>
>
@end
>
>
------ColorCell.m-----------------------------------------------
>
//
>
// ColorCell.m
>
// color cell test
>
//
>
// Created by John Harte on Sat Sep 14 2002.
>
// Copyright (c) 2002 __MyCompanyName__. All rights reserved.
>
//
>
>
#import "ColorCell.h"
>
>
@implementation ColorCell
>
>
- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView*)
>
controlView {
>
NSRect sqare = NSInsetRect (cellFrame, 0.5, 0.5);
>
>
// use the smallest size to sqare off the box & center the box
>
if (sqare.size.height < sqare.size.width) {
>
sqare.size.width = sqare.size.height;
>
sqare.origin.x = sqare.origin.x + (cellFrame.size.width -
>
sqare.size.width) / 2.0;
>
} else {
>
sqare.size.height = sqare.size.width;
>
sqare.origin.y = sqare.origin.y + (cellFrame.size.height -
>
sqare.size.height) / 2.0;
>
}
>
>
[[NSColor blackColor] set];
>
[NSBezierPath strokeRect: sqare];
>
>
[(NSColor*) [self objectValue] set];
>
[NSBezierPath fillRect: NSInsetRect (sqare, 2.0, 2.0)];
>
}
>
>
@end
>
>
------Controller.h-----------------------------------------------
>
//
>
// Controller.h
>
// color cell test
>
//
>
// Created by John Harte on Sat Sep 14 2002.
>
// Copyright (c) 2002 __MyCompanyName__. All rights reserved.
>
//
>
>
#import <AppKit/AppKit.h>
>
>
>
@interface Controller : NSWindowController {
>
IBOutlet NSTableView* table;
>
>
NSMutableArray* colors;
>
int colorRow; // the row color changes apply to
>
}
>
>
@end
>
>
------Controller.m-----------------------------------------------
>
//
>
// Controller.m
>
// color cell test
>
//
>
// Created by John Harte on Sat Sep 14 2002.
>
// Copyright (c) 2002 __MyCompanyName__. All rights reserved.
>
//
>
>
#import "Controller.h"
>
#import "ColorCell.h"
>
>
>
@implementation Controller
>
>
- (void) awakeFromNib {
>
NSTableColumn* column;
>
ColorCell* colorCell;
>
>
column = [[table tableColumns] objectAtIndex: 0];
>
colorCell = [[[ColorCell alloc] init] autorelease];
>
[colorCell setEditable: YES];
>
[colorCell setTarget: self];
>
[colorCell setAction: @selector (colorClick:)];
>
[column setDataCell: colorCell];
>
>
colors = [[NSMutableArray arrayWithObjects:
>
[NSColor redColor], [NSColor greenColor], [NSColor cyanColor],
>
[NSColor blueColor], [NSColor magentaColor], [NSColor orangeColor],
>
[NSColor purpleColor], [NSColor yellowColor], [NSColor
>
lightGrayColor],
>
[NSColor darkGrayColor], nil] retain];
>
}
>
>
- (void) colorClick: (id) sender { // sender is the table view
>
NSColorPanel* panel;
>
>
colorRow = [sender clickedRow];
>
panel = [NSColorPanel sharedColorPanel];
>
[panel setTarget: self];
>
[panel setAction: @selector (colorChanged:)];
>
[panel setColor: [colors objectAtIndex: colorRow]];
>
[panel makeKeyAndOrderFront: self];
>
}
>
>
- (void) colorChanged: (id) sender { // sender is the NSColorPanel
>
[colors replaceObjectAtIndex: colorRow withObject: [sender color]];
>
[table reloadData];
>
}
>
>
>
// Table View
>
- (int) numberOfRowsInTableView: (NSTableView*) tableView {
>
return 10;
>
}
>
>
- (id) tableView: (NSTableView*) aTableView objectValueForTableColumn:
>
(NSTableColumn*) aTableColumn row: (int) rowIndex {
>
return [colors objectAtIndex: rowIndex];
>
}
>
>
@end
_______________________________________________
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.