NSButtonCell in NSTableView doesn't receive setHighlighted:

  • Hi,

    I just found out that an NSButtonCell that sits in an NSTableView
    doesn't receive the setHighlighted: message from the table when it's
    displayed for a selected row. In the case of NSImageCell and
    NSTextFieldCell it DOES happen, they both receive that message from
    the table's preparedCellAtColumn:row: method.

    I couldn't find an answer to this in previous posts.

    I worked around this by subclassing NSTableView and overriding that
    method to make sure all cells get the setHighlighted:

    - (NSCell *)preparedCellAtColumn:(NSInteger)column row:(NSInteger)row
    {
    NSCell* cell = [super preparedCellAtColumn:column row:row];

    [cell setHighlighted:(row == [self selectedRow])];

    return cell;
    }

    It's not a big deal of course, but since setHighlight: is a superclass
    method from NSCell it seems strange to me that the table doesn't treat
    all cells equally. Is this expected behaviour or is it worth filing a
    bug?

    Sebastian
  • > It's not a big deal of course, but since setHighlight: is a superclass
    > method from NSCell it seems strange to me that the table doesn't treat
    > all cells equally. Is this expected behaviour or is it worth filing a
    > bug?

    It's always fine to file a bug, but this probably isn't going to
    change.  There are too many clients that rely on the existing
    behavior, whether they're aware of it or not.

    The problem is that 'highlight' does not have a sufficiently defined
    meaning at the NSCell level.  Different cells use the flag in
    different ways, which makes it difficult for a client that deals with
    cells generically to use it.  For button and button-like objects,
    highlighted means pressed.  You probably don't want your buttons to
    look pressed in selected table rows.  NSTableView, sadly, special
    cases those cells types that should not be highlighted in selected
    rows.

    If you can require 10.5, I suggest you take a look at -[NSCell
    backgroundStyle] and related API instead.  The most common reason for
    a cell wanting to know that it's in a selected row is to draw
    differently.  For example, text drawn on top of selected rows of a
    table should be white.  In this case, and usually, it isn't really
    that the cell is in a selected table row that's important, it's that
    it's being drawn on top of something dark.  The text needs to be white
    for contrast.

    This information is what the background style API gives you.  It lets
    you know some very high level information about the context where the
    cell is being drawn, that it's on something 'light', 'dark', 'raised'
    or 'lowered'.  Please see the AppKit Leopard release notes for more
    detail, and also the NSCell documentation.

    -Ken
    Cocoa Frameworks

    On Jan 5, 2008 6:24 AM, Sebastian Morsch <baiste...> wrote:
    > Hi,
    >
    > I just found out that an NSButtonCell that sits in an NSTableView
    > doesn't receive the setHighlighted: message from the table when it's
    > displayed for a selected row. In the case of NSImageCell and
    > NSTextFieldCell it DOES happen, they both receive that message from
    > the table's preparedCellAtColumn:row: method.
    >
    > I couldn't find an answer to this in previous posts.
    >
    > I worked around this by subclassing NSTableView and overriding that
    > method to make sure all cells get the setHighlighted:
    >
    > - (NSCell *)preparedCellAtColumn:(NSInteger)column row:(NSInteger)row
    > {
    > NSCell* cell = [super preparedCellAtColumn:column row:row];
    >
    > [cell setHighlighted:(row == [self selectedRow])];
    >
    > return cell;
    > }
    >
    >
    > It's not a big deal of course, but since setHighlight: is a superclass
    > method from NSCell it seems strange to me that the table doesn't treat
    > all cells equally. Is this expected behaviour or is it worth filing a
    > bug?
    >
    >
    > Sebastian
    >
  • Thanks for your reply Ken, I link against 10.5 and your suggestion
    worked great! Since I really don't want my buttons look pressed in
    selected rows as you pointed out, I don't consider it a bug anymore.
    It's maybe more something like a "historically derived little flaw",
    so whatever.

    Thanks!

    Sebastian

    Am 05.01.2008 um 19:29 schrieb Ken Ferry:

    >> It's not a big deal of course, but since setHighlight: is a
    >> superclass
    >> method from NSCell it seems strange to me that the table doesn't
    >> treat
    >> all cells equally. Is this expected behaviour or is it worth filing a
    >> bug?
    >
    > It's always fine to file a bug, but this probably isn't going to
    > change.  There are too many clients that rely on the existing
    > behavior, whether they're aware of it or not.
    >
    > The problem is that 'highlight' does not have a sufficiently defined
    > meaning at the NSCell level.  Different cells use the flag in
    > different ways, which makes it difficult for a client that deals with
    > cells generically to use it.  For button and button-like objects,
    > highlighted means pressed.  You probably don't want your buttons to
    > look pressed in selected table rows.  NSTableView, sadly, special
    > cases those cells types that should not be highlighted in selected
    > rows.
    >
    > If you can require 10.5, I suggest you take a look at -[NSCell
    > backgroundStyle] and related API instead.  The most common reason for
    > a cell wanting to know that it's in a selected row is to draw
    > differently.  For example, text drawn on top of selected rows of a
    > table should be white.  In this case, and usually, it isn't really
    > that the cell is in a selected table row that's important, it's that
    > it's being drawn on top of something dark.  The text needs to be white
    > for contrast.
    >
    > This information is what the background style API gives you.  It lets
    > you know some very high level information about the context where the
    > cell is being drawn, that it's on something 'light', 'dark', 'raised'
    > or 'lowered'.  Please see the AppKit Leopard release notes for more
    > detail, and also the NSCell documentation.
    >
    > -Ken
    > Cocoa Frameworks
    >
    > On Jan 5, 2008 6:24 AM, Sebastian Morsch <baiste...> wrote:
    >> Hi,
    >>
    >> I just found out that an NSButtonCell that sits in an NSTableView
    >> doesn't receive the setHighlighted: message from the table when it's
    >> displayed for a selected row. In the case of NSImageCell and
    >> NSTextFieldCell it DOES happen, they both receive that message from
    >> the table's preparedCellAtColumn:row: method.
    >>
    >> I couldn't find an answer to this in previous posts.
    >>
    >> I worked around this by subclassing NSTableView and overriding that
    >> method to make sure all cells get the setHighlighted:
    >>
    >> - (NSCell *)preparedCellAtColumn:(NSInteger)column row:(NSInteger)row
    >> {
    >> NSCell* cell = [super preparedCellAtColumn:column row:row];
    >>
    >> [cell setHighlighted:(row == [self selectedRow])];
    >>
    >> return cell;
    >> }
    >>
    >>
    >> It's not a big deal of course, but since setHighlight: is a
    >> superclass
    >> method from NSCell it seems strange to me that the table doesn't
    >> treat
    >> all cells equally. Is this expected behaviour or is it worth filing a
    >> bug?
    >>
    >>
    >> Sebastian
    >>
previous month january 2008 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today