Setting a shadow in -willDisplayCell causes CGContextSetStyle error?

  • Hi,

    A couple of months ago, I posted about an apparently
    benign error my app was generating on the console:

    CGContextSetStyle: invalid context

    (The original thread can be found here:
    http://www.cocoabuilder.com/archive/message/cocoa/2007/8/24/188258)

    It has had me stumped ever since, but today I *think*
    I have finally found the cause. (The strange thing was
    that this error only ever appeared if projects were
    opened via File > Open; my app automatically opens the
    last project when it is launched, and the error never
    seems to occur in this circumstance so I kept missing
    it - weird.)

    It seems to come from the fact that I add a shadow to
    the text of my NSOutlineView source list using
    -outlineView:willDisplayCell:forTableColumn:item: as
    follows:

    - (void)outlineView:(NSOutlineView *)olv
    willDisplayCell:(id)cell
        forTableColumn:(NSTableColumn *)tableColumn
    item:(id)item
    {
    NSShadow *shadow = [[NSShadow alloc] init];
    [shadow setShadowOffset:NSMakeSize(0, -1.1)];
    [shadow setShadowColor:[NSColor grayColor]];
    [shadow set];
    [shadow autorelease];
    }

    The above code seems to cause a "CGContextSetStyle:
    invalid context" console error whenever it is called
    if the item is selected (which, unfortunately, is the
    exact case in which I want to add a shadow in my code
    - for selected items).

    Can anyone see what I am doing wrong here, or why this
    might be generating such an error? Is there a better
    way of setting the shadow that will avoid this console
    error?

    Many thanks in advance,
    Keith

          ____________________________________________________________________________________
    Catch up on fall's hot new shows on Yahoo! TV. Watch previews, get listings, and more!
    http://tv.yahoo.com/collections/3658
  • You never can't assume willDisplayCell: will be called inside a drawing
    context (it's "willDisplay:", not "willDraw:" method, after all ;-) ).
    If you want to add niceties to your cells, you should:

    1) subclass the NSCell classes you use and use them as your cell class
    prototype:

    @interface KBShadowyTextFieldCell : NSTextFieldCell
    {
        NSShadow* cellShadow;
    }

    - (void)setShadow:(NSShadow *)aShadow;
    - (NSShadow *)shadow;

    @end

    @implementation KBShadowyTextFieldCell

    - (void)dealloc
    {
        [shadow release];
        [super dealloc];
    }

    - (void)setShadow:(NSShadow *)aShadow
    {
        if(aShadow != shadow) {
          [aShadow retain];
          [shadow release];
          shadow = aShadow;
        }
    }

    - (NSShadow *)shadow
    {
        return shadow;
    }

    - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
    {
        [NSGraphicsContext saveGraphicsState];
        [shadow set];
        [super drawWithFrame:cellFrame inView:controlView];
        [NSGraphicsContext restoreGraphicsState];
    }

    @end

    [... and in your delegate method ...]

    - (void)outlineView:(NSOutlineView *)olv
    willDisplayCell:(id)cell
            forTableColumn:(NSTableColumn *)tableColumn
    item:(id)item
    {
          NSShadow *shadow = [[NSShadow alloc] init];
          [shadow setShadowOffset:NSMakeSize(0, -1.1)];
          [shadow setShadowColor:[NSColor grayColor]];
          [(KBShadowyTextFieldCell *)cell ];
          [shadow release];
    }

    On 10/11/07, Keith Blount <keithblount...> wrote:
    >
    > Hi,
    >
    > A couple of months ago, I posted about an apparently
    > benign error my app was generating on the console:
    >
    > CGContextSetStyle: invalid context
    >
    > (The original thread can be found here:
    > http://www.cocoabuilder.com/archive/message/cocoa/2007/8/24/188258)
    >
    > It has had me stumped ever since, but today I *think*
    > I have finally found the cause. (The strange thing was
    > that this error only ever appeared if projects were
    > opened via File > Open; my app automatically opens the
    > last project when it is launched, and the error never
    > seems to occur in this circumstance so I kept missing
    > it - weird.)
    >
    > It seems to come from the fact that I add a shadow to
    > the text of my NSOutlineView source list using
    > -outlineView:willDisplayCell:forTableColumn:item: as
    > follows:
    >
    > - (void)outlineView:(NSOutlineView *)olv
    > willDisplayCell:(id)cell
    > forTableColumn:(NSTableColumn *)tableColumn
    > item:(id)item
    > {
    > NSShadow *shadow = [[NSShadow alloc] init];
    > [shadow setShadowOffset:NSMakeSize(0, -1.1)];
    > [shadow setShadowColor:[NSColor grayColor]];
    > [shadow set];
    > [shadow autorelease];
    > }
    >
    > The above code seems to cause a "CGContextSetStyle:
    > invalid context" console error whenever it is called
    > if the item is selected (which, unfortunately, is the
    > exact case in which I want to add a shadow in my code
    > - for selected items).
    >
    > Can anyone see what I am doing wrong here, or why this
    > might be generating such an error? Is there a better
    > way of setting the shadow that will avoid this console
    > error?
    >
    > Many thanks in advance,
    > Keith
    >
    >
    > ____________________________________________________________
    > ________________________
    > Catch up on fall's hot new shows on Yahoo! TV. Watch previews, get
    > listings, and more!
    > http://tv.yahoo.com/collections/3658
    >
previous month october 2007 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