views:

158

answers:

1

I've got a custom cell composed out of several cells, one of which is an NSPopUpButtonCell.

When drawing my custom cell highlighted, I want to cause all the sub-cells to highlight as well (typically by turning white).

With, for example an NSTextCell, if I call setHighlighted:YES before calling drawWithFrame:inView the cell will be drawn with white text, exactly as I want it.

This does not work with NSPopUpButtonCells. The text just continues to draw as black.

It seems like this should be possible, since dropping an NSPopUpButtonCell into an NSTableView highlights properly.

Can somebody point me in the right direction for fixing this?

+1  A: 

Where are you hosting this custom+composite NSCell subclass?

-setHighlighted:YES isn't what you are looking for. From the documentation:

By default, this method does nothing. The NSButtonCell class overrides this method to draw the button with the appearance specified by NSCellLightsByBackground, NSCellLightsByContents, or NSCellLightsByGray.

Typically the host view for a cell will set the cell's background style, and the cell will use that at draw time to display itself appropriately. Propagate the background style from the master cell to the sub-cells.

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    NSRect textRect, popUpRect;
    NSDivideRect(cellFrame, &textRect, &popUpRect, NSWidth(cellFrame) / 2, NSMinXEdge);

    /* Draw the text cell (self) */
    [super drawInteriorWithFrame: textRect inView: controlView];

    /* Draw our compound popup cell - create & release every time drawn only in example */
    NSPopUpButtonCell *popUpCell = [[NSPopUpButtonCell alloc] initTextCell: @"popup title"];
    [popUpCell setBordered: NO];
    [popUpCell setBackgroundStyle: [self backgroundStyle]];
    [popUpCell drawWithFrame: popUpRect inView: controlView];
    [popUpCell release];
}

If you are hosting this composite cell in an NSTableView, that should be sufficient to get the correct background for selected rows.

If you are hosting this in your own view, you may need to do additional work. (And need to provide additional details about the host environment before we can offer advice.)

Jim Correia
That's exactly what I needed. Thanks Jim.
Lawrence Johnston