Correctly Subclassing NSTableHeaderCell?

  • Hey all,

    I'm trying to subclass NSTableHeaderCell to get a darker look, a'la
    iTunes and iCal. I've got it drawing perfectly well and I'm happy with
    the look, but I can't get it to highlight when clicked or is the
    sorted column, and I can't get it to show the sort direction indicator
    either.

    I've searched the docs and online, but can't seem to find anything.
    Has anyone done this successfully?Thanks,

    -- Daniel
  • > I'm trying to subclass NSTableHeaderCell to get a darker look, a'la
    > iTunes and iCal. I've got it drawing perfectly well and I'm happy with
    > the look, but I can't get it to highlight when clicked or is the
    > sorted column, and I can't get it to show the sort direction indicator
    > either.

      I predict the problem lies somewhere within your code ... ;-)
    Perhaps you could share relevant bits so we can identify where that
    might be?

    --
    I.S.
  • Well, it's probably what I *haven't* done - this is a listing of what
    I've implemented in my NSTableHeaderCell subclass. The
    drawSortIndicatorWithFrame method draws a red rectangle where the
    indicator should go (and works when calling it from the end of my
    drawWithFrame: method). However, when clicking on the headers nothing
    changes visually at all. The table view does sort, however.

    @implementation KNTableHeader

    -(void)drawWithFrame:(NSRect)frame inView:(NSView *)view {

    if ([self isHighlighted]) {
      [[CTGradient unifiedDarkGradient] fillRect:frame angle:-90];
    } else {
      [[CTGradient unifiedNormalGradient] fillRect:frame angle:-90];
    }

    // Draw some stuff

    }

    -(void)highlight:(BOOL)flag withFrame:(NSRect)cellFrame inView:(NSView
    *)controlView {
    NSLog(@"Changed!");
    }

    -(void)drawSortIndicatorWithFrame:(NSRect)cellFrame inView:(NSView
    *)controlView ascending:(BOOL)ascending priority:(int)priority {

    [[NSColor redColor] set];
    [NSBezierPath fillRect:[self sortIndicatorRectForBounds:cellFrame]];

    }

    @end

    On 3 Jan 2008, at 16:37, I. Savant wrote:

    >> I'm trying to subclass NSTableHeaderCell to get a darker look, a'la
    >> iTunes and iCal. I've got it drawing perfectly well and I'm happy
    >> with
    >> the look, but I can't get it to highlight when clicked or is the
    >> sorted column, and I can't get it to show the sort direction
    >> indicator
    >> either.
    >
    > I predict the problem lies somewhere within your code ... ;-)
    > Perhaps you could share relevant bits so we can identify where that
    > might be?
    >
    > --
    > I.S.
    >

    Thanks,

    -- Daniel

    _______________________

      <daniel...>
      http://www.kennettnet.co.uk

    Please include previous messages in any reply you send.
  • On 3-Jan-2008, at 09:33 , Daniel Kennett wrote:

    > Hey all,
    >
    > I'm trying to subclass NSTableHeaderCell to get a darker look, a'la
    > iTunes and iCal. I've got it drawing perfectly well and I'm happy
    > with the look, but I can't get it to highlight when clicked or is
    > the sorted column, and I can't get it to show the sort direction
    > indicator either.
    >
    > I've searched the docs and online, but can't seem to find anything.
    > Has anyone done this successfully?

    NSTableHeaderCells behave a bit differently than other cells.
    Normally -[NSCell highlight:withFrame:inView:] just sets the
    isHighlighted flag (and probably marks the view as needing display).
    NSTableHeaderCell, on the other hand, performs the drawing
    immediately.  So -drawWithFrame:inView: is used to draw an unselected
    table header cell, and -highlight:withFrame:inView: is used to draw a
    selected table header cell.

    It uses the cell state for the extra highlight during mouse tracking.
    I can't remember if this is different from other cells or not.

    --
    Steve
    >
  • On 3 jan 2008, at 17.33, Daniel Kennett wrote:

    > Hey all,
    >
    > I'm trying to subclass NSTableHeaderCell to get a darker look, a'la
    > iTunes and iCal. I've got it drawing perfectly well and I'm happy
    > with the look, but I can't get it to highlight when clicked or is
    > the sorted column, and I can't get it to show the sort direction
    > indicator either.
    >
    > I've searched the docs and online, but can't seem to find anything.
    > Has anyone done this successfully?
    >
    > <Picture 2.png>
    >
    >
    >
    > Thanks,
    >
    > -- Daniel
    >
    > _______________________
    >
    > <daniel...>
    > http://www.kennettnet.co.uk
    >
    > Please include previous messages in any reply you send.

    Just use the methods setTextColor: and setBackgroundColor:
    which NSTableHeaderCell inherits from its bas class NSTextFieldCell;
    a good place to invoke them would be in awakeFromNib:
    ------------------------------------------------------
    "Home is not where you are born, but where your heart finds peace" -
    Tommy Nordgren, "The dying old crone"
    <tommy.nordgren...>
  • Thanks everyone for your help - I've figured it out:

    - When detecting a mouse click, you should check [self state] rather
    than [self highlighted].

    - Drawing the sort indicators needs more than is (easily) accessible
    inside the cell. So, the header cell gets this method:

    -(void)setSortAscending:(BOOL)asc priority:(int)pri {
    sortPriority = pri;
    sortAscending = asc;

    [(NSControl *)[self controlView] updateCell: self];
    }

    ... and in the drawing method:

    -(void)drawWithFrame:(NSRect)frame inView:(NSView *)view {
    [...]
    [self drawSortIndicatorWithFrame:frame inView:view
    ascending:sortAscending priority:sortPriority];
    }

    ... and finally, in the tableView's delegate (trackListController is
    an IBOutlet to the ArrayController that deals with the table's
    contents):

    - (void)tableView:(NSTableView *)tableView didClickTableColumn:
    (NSTableColumn *)tableColumn {
    [(KNTableHeader *)[tableColumn headerCell] setSortAscending:
    [[[trackListController sortDescriptors] objectAtIndex:0] ascending]
    priority:0];
    // Loop through the other columns and set their priority to 1.
    }

    This gives the cell all the details it needs to draw the sort
    indicators properly. Hope this is helpful to someone!Thanks,

    -- Daniel

      _______________________

        <daniel...>
        http://www.kennettnet.co.uk

    Please include previous messages in any reply you send.

    On 3 Jan 2008, at 16:33, Daniel Kennett wrote:

    > Hey all,
    >
    > I'm trying to subclass NSTableHeaderCell to get a darker look, a'la
    > iTunes and iCal. I've got it drawing perfectly well and I'm happy
    > with the look, but I can't get it to highlight when clicked or is
    > the sorted column, and I can't get it to show the sort direction
    > indicator either.
    >
    > I've searched the docs and online, but can't seem to find anything.
    > Has anyone done this successfully?
    >
    > <Picture 2.png>
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