NSBrowser title rendering....
NSBrowser title rendering....
- Subject: NSBrowser title rendering....
- From: Bill Bumgarner <email@hidden>
- Date: Tue, 4 Dec 2001 18:22:43 -0500
... the adventure continues.
As it turns out, the fact that NSBrowser is not flipped was [most of the]
reason why the NSPopUpButtonCell would fail to render correctly. As such,
the popup needs to be rendered via the NSControl class (or subclass--
NSPopUpButton immediately comes to mind :-) for the button to be rendered
correctly.
To do this, I'm rendering the NSPopUpButton into an NSImage and
compositing the image into the title rect of the NSBrowser column (thanks
again, Nat!).
Unfortunately, this *only* works if the NSPopUpButton is a subview of the
window's content view (doesn't have to be a subview of the browser). What
is particularly odd is that if the NSPopUpButton is *not* a subview, the
popup renders everything but the text and the little popup area is grey
instead of blue!
The completely unoptimized/non-refactored code is shown below (this is
from a subclass of NSBrowser).
My current thinking is that I will either add the subview for the duration
of the -drawTitleOfColumn:inRect: method or, if it proves disastrous to
muck with the view hierarchy in the middle of drawing, I'll adjust the
frame after rendering such that it is well outside of the visible/rendered
area.
Unfortunately, adding a set of popup buttons to the view tiling of the
NSBrowser appears to be a lose-- it would abuse of the various NSBrowser
APIs that I am not comfortable with. This solution, at least, uses
nothing but documented API.
Or something like that.... comments, suggestions, etc. would be most
welcome. Feel free to laugh at the code... just point at what your
laughing at and give reason.
b.bum
- (void) awakeFromNib
{
[_titleTemplateButton retain];
//! [_titleTemplateButton removeFromSuperview];
}
- (void)drawTitleOfColumn:(int)column inRect:(NSRect)aRect;
{
NSPopUpButtonCell *button;
NSImage *renderedPopUp;
NSRect renderRect;
if (column >= [_titleCells count])
return;
renderRect = NSMakeRect(0,0,aRect.size.width,aRect.size.height);
renderedPopUp = [[NSImage alloc] initWithSize: aRect.size];
[renderedPopUp setFlipped:YES];
[renderedPopUp lockFocus];
button = [self titleCellOfColumn: column];
[_titleTemplateButton setCell: button];
[_titleTemplateButton setFrame: renderRect];
[_titleTemplateButton setBounds: renderRect];
[_titleTemplateButton drawRect: renderRect];
[renderedPopUp unlockFocus];
[renderedPopUp compositeToPoint:aRect.origin
operation:NSCompositeSourceOver];
}