2

I use this code to draw some rects in a view. This is the code:

CGContextRef myContext =  [[NSGraphicsContext currentContext] graphicsPort];
CGContextSetRGBFillColor (myContext, 1, 0, 0, 1);
CGContextFillRect (myContext, CGRectMake (0, 0, 200, 100 ));
CGContextSetRGBFillColor (myContext, 0, 0, 1, .5);
CGContextFillRect (myContext, CGRectMake (0, 0, 100, 200));

When I use the code in a simple AppDelegate, everything is okay. But, when I use it in any Document-based application (in Document.m), I receive this error:

<Error>: CGContextSetRGBFillColor: invalid context 0x0
<Error>: CGContextFillRects: invalid context 0x0
<Error>: CGContextSetRGBFillColor: invalid context 0x0
<Error>: CGContextFillRects: invalid context 0x0

What am I missing?

hichris123
  • 10,145
  • 15
  • 56
  • 70
Aug
  • 595
  • 9
  • 22

1 Answers1

3

I think I was an idiot !. the current context is always set to main window. I could mange it by subclassing a view and using this code:

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
}
hichris123
  • 10,145
  • 15
  • 56
  • 70
Aug
  • 595
  • 9
  • 22