Re: Source list with counter next to item title
Re: Source list with counter next to item title
- Subject: Re: Source list with counter next to item title
- From: Conor <email@hidden>
- Date: Tue, 20 Nov 2007 12:15:10 +0100
You can use bindings or a data source; either way return an NSNumber with
the count and then draw it in your custom cell. DVDpedia, Bookpedia, CDpedia
and Gamepedia use bindings directly to a core data managed object that
returns the count of a relationship:
return [NSNumber numberWithInt:[[self entries] count]];
For the custom cell a subclass of an NSImageCell:
@interface MyCountImageCell : NSImageCell {
@private
NSColor *blueOvalColor;
NSDictionary *selectedFontAttribute, *fontAttribute;
int previousCount;
}
@end
// http://www.cocoadev.com/index.pl?NSBezierPathCategory
@interface NSBezierPath (CocoaDevCategory)
+ (NSBezierPath*)bezierPathWithRoundRectInRect:(NSRect)aRect
radius:(float)radius;
@end
@implementation MyCountImageCell
- (id)init {
self = [super init];
if (self != nil) {
blueOvalColor = [[NSColor colorWithDeviceRed:154.0/255.0
green:171.0/255.0 blue:201.0/255.0 alpha:1.0] retain];
selectedFontAttribute = [[NSDictionary dictionaryWithObjectsAndKeys:
[NSFont fontWithName:@"Helvetica-Bold" size:13], NSFontAttributeName,
[NSColor colorWithDeviceRed:150.0/255.0 green:161.0/255.0 blue:183.0/255.0
alpha:1.0], NSForegroundColorAttributeName, nil] retain];
fontAttribute = [[NSDictionary dictionaryWithObjectsAndKeys: [NSFont
fontWithName:@"Helvetica-Bold" size:13], NSFontAttributeName, [NSColor
whiteColor], NSForegroundColorAttributeName, nil] retain];
}
return self;
}
- (void)setObjectValue:(id)aValue {
if ([aValue isKindOfClass:[NSImage class]]) {
[super setObjectValue:aValue];
return;
}
unsigned int count = [aValue intValue];
if (previousCount != count) {
if (count == -1) { // a Group colelction don't draw
[super setObjectValue:nil];
return;
}
int additonalSpace;
if (count < 10)
additonalSpace = 0;
else if (count < 100)
additonalSpace = 8;
else if (count < 1000)
additonalSpace = 16;
else
additonalSpace = 24 ;
NSString *numberString = [NSString stringWithFormat:@"%d", count];
NSSize imageSize = NSMakeSize(22 + additonalSpace, 18);
NSImage *dragImage = [[NSImage alloc] initWithSize:imageSize];
[dragImage lockFocus];
[blueOvalColor set];
NSBezierPath *p = [NSBezierPath
bezierPathWithRoundRectInRect:NSMakeRect(0, 2, imageSize.width - 2, 15)
radius:9.0];
[p fill];
[numberString drawInRect:NSMakeRect(6, 1, imageSize.width, 18)
withAttributes:fontAttribute];
[dragImage unlockFocus];
[super setObjectValue:[dragImage autorelease]];
previousCount = count;
}
}
@end
@implementation NSBezierPath (CocoaDevCategory)
+ (NSBezierPath*)bezierPathWithRoundRectInRect:(NSRect)aRect
radius:(float)radius
{
NSBezierPath* path = [self bezierPath];
radius = MIN(radius, 0.5f * MIN(NSWidth(aRect), NSHeight(aRect)));
NSRect rect = NSInsetRect(aRect, radius, radius);
[path appendBezierPathWithArcWithCenter:NSMakePoint(NSMinX(rect),
NSMinY(rect))
radius:radius startAngle:180.0
endAngle:270.0];
[path appendBezierPathWithArcWithCenter:NSMakePoint(NSMaxX(rect),
NSMinY(rect))
radius:radius startAngle:270.0
endAngle:360.0];
[path appendBezierPathWithArcWithCenter:NSMakePoint(NSMaxX(rect),
NSMaxY(rect))
radius:radius startAngle: 0.0
endAngle: 90.0];
[path appendBezierPathWithArcWithCenter:NSMakePoint(NSMinX(rect),
NSMaxY(rect))
radius:radius startAngle: 90.0
endAngle:180.0];
[path closePath];
return path;
}
@end
Regards,
Conor
http://www.bruji.com/
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden