Using custom, yet animated outline cell images in NSOutlineViews
Using custom, yet animated outline cell images in NSOutlineViews
- Subject: Using custom, yet animated outline cell images in NSOutlineViews
- From: Peter Maurer <email@hidden>
- Date: Thu, 24 Jan 2008 00:25:26 +0100
When implementing my own little grouped source list for one of my
applications (can't use Leopard's default solution for several
reasons), I came across a problem that has been a topic on this list a
couple of times, usually with regard to either source lists or
NSOutlineViews in HUD windows:
There are times when you want to replace the default outline cell
images with your own images. And doing so is dead easy -- you just set
the cell's image and alternate image in [[NSOutlineView delegate]
outlineView:willDisplayOutlineCell:forTableColumn:item:].
There's a catch, however: If you do it that way, your disclosure
triangles won't animate anymore. They'll just jump from collapsed to
expanded and back.
After investigating this for about an hour, I realized the cell does
originally tell you that it's in an intermediate state by having an
intValue of -1. But it appears that the cell stops going through this
intermediate state as soon as you set custom images. Hence, the
animation goes away.
So if I couldn't replace the cell's images, the only solution I could
think of was drawing those triangles myself. Now this might not be the
most elegant way to do it, but I think it's "legal". Here's a quick
and dirty example, which I've simplified for this mail -- it might not
actually work, but you'll get the basic idea:
--
- (void)outlineView: (NSOutlineView*)outlineView
willDisplayOutlineCell: (id)cell forTableColumn:
(NSTableColumn*)tableColumn item: (id)item {
NSString *theImageName;
NSInteger theCellValue = [cell integerValue];
if (theCellValue==1) {
theImageName = @"PMOutlineCellOn";
} else if (theCellValue==0) {
theImageName = @"PMOutlineCellOff";
} else {
theImageName = @"PMOutlineCellMixed";
}
if ([[outlineView selectedRowIndexes] containsIndex: (unsigned int)
[outlineView rowForItem: item]]) {
theImageName = [theImageName stringByAppendingString: @"White"];
}
NSImage *theImage = [NSImage imageNamed: theImageName];
NSRect theFrame = [outlineView frameOfOutlineCellAtRow: [outlineView
rowForItem: item]];
// adjust theFrame here to position your image
[theImage compositeToPoint: theFrame.origin operation:
NSCompositeSourceOver];
[cell setImagePosition: NSNoImage];
}
--
And here's a screenshot from my application, showing you what the
result looks like:
<http://www.manytricks.com/sandbox/cocoadev/animatedcustomoutlinecell.png
>
If you _need_ to have this work on 10.4 or earlier, you can't use
[NSOutlineView frameOfOutlineCellAtRow:], obvisouly. But if you're
feeling adventurous, you can resort to the undocumented [NSOutlineView
_frameOfOutlineCellAtRow:], which has always been there, as far as I
know. But again, this is totally undocumented, and you really really
really shouldn't do it. ;-)
Anyway, I hope some of you will find this useful. Cheers,
Peter.
--
manytricks.com | petermaurer.de
_______________________________________________
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