NSSegmentedCell - Custom Class

Discussion in 'Mac Programming' started by varsis, Sep 29, 2010.

  1. varsis macrumors regular

    Nov 30, 2005
    Ok, so Im trying to subclass NSSegmentedcell but it's doing some odd stuff.

    I can't seem to get it to work properly, There is no way to cover up the underlaying buttons properly, or get rid of them properly.

    So far this is what I have:
    - (void)drawSegment:(NSInteger)segment inFrame:(NSRect)rect withView:(NSView *)controlView {
     NSImage *backgroundImage = [NSImage imageNamed: @"statusbar_background"];
    	if ([self selectedSegment] != segment) {
     [backgroundImage setFlipped: YES];
     [backgroundImage setScalesWhenResized: YES];
     [backgroundImage setSize: rect.size];
     [backgroundImage drawInRect: rect fromRect:rect operation: NSCompositeSourceOver fraction: 1.0];
     [[NSColor colorWithCalibratedWhite:202.0/255.0 alpha:1.0] drawPixelThickLineAtPosition:rect.size.width withInset:0.0 inRect:rect inView:controlView horizontal:NO flip:NO];
     [[NSColor colorWithCalibratedWhite:202.0/255.0 alpha:1.0] drawPixelThickLineAtPosition:20.0 withInset:0.0 inRect:rect inView:controlView horizontal:YES flip:NO];
    Maybe I am taking the wrong approach? I have never done this before, still learning it i guess it all comes with practice it's pretty well ill documented.

  2. kainjow Moderator emeritus


    Jun 15, 2000
    The last time I attempted to make a custom NSSegmentedControl, I too was frustrated with the lack of customization that subclassing provides. Instead, rolled my own by subclassing NSMatrix and providing it with a custom NSButtonCell, and that did the trick. It's more work, but in the end you get something that works without hacks.
  3. varsis thread starter macrumors regular

    Nov 30, 2005
    hmm maybe that is the way i should go. Got any sample code you wouldn't mind sharing?

    More so of the custom NSMatrix, im trying to wrap my head around what you would need to subclass in there? SegmentedController is like a NSMatrix but with a the nsmatrix that doesn't allow an empty selection? thinking out loud here.
  4. kainjow Moderator emeritus


    Jun 15, 2000
    Sorry I can't post any code but I subclasses NSMatrix so I could provide the same methods that NSSegmentedControl does for getting and setting the label and image of a segment, and the selected segment, and also for overriding the drawing to provide rounded corners, although you could probably do that in the cell directly with some detection of the first and last segments. So technically you don't need to override it, but it provides better abstraction and allows you to use it as a drop-in replacement for NSSegmentedControl in certain situations.

Share This Page