How do I manually highlight an NSPopUpButtonCell when drawing it (draw it using white instead of black)?

StackOverflow https://stackoverflow.com/questions/1162414

Question

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?

Was it helpful?

Solution

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.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top