/**
TODO : Desciption
*/ @implementation NSCustomImageRep /**Initializes a new NSCustomImageRep with. When a -draw message is recieved it send aSelector message to the delegate anObject.
See Also: -delegate -drawSelector [NSImageRep-draw]
*/ - (id) initWithDrawSelector: (SEL)aSelector delegate: (id)anObject { if ( ! ( self = [super init] ) ) return nil; /* WARNING: Retaining the delegate may or may not create a cyclic graph */ _delegate = RETAIN(anObject); _selector = aSelector; return self; } - (void) dealloc { RELEASE(_delegate); [super dealloc]; } /**Returns the NSCustomImageRep's delegate.
See Also: -initWithDrawSelector:delegate:
*/ - (id) delegate { return _delegate; } /**Returns the draw method sent to the delegate when NSCustomImageRep recieved a -draw message.
See Also: -initWithDrawSelector:delegate: [NSImageRep-draw]
*/ - (SEL) drawSelector { return _selector; } - (BOOL) draw { [_delegate performSelector: _selector withObject: self]; return YES; } // // TODO: For both of the following methods we can extract the // logic in the superclass to another method and call it here // if the delegate is set from both places. // - (BOOL) drawAtPoint: (NSPoint)aPoint { BOOL ok, reset; NSGraphicsContext *ctxt; NSAffineTransform *ctm = nil; // if both are zero and the delegate isn't set, return no. if (_size.width == 0 && _size.height == 0 && _delegate == nil) return NO; NSDebugLLog(@"NSImage", @"Drawing at point %f %f\n", aPoint.x, aPoint.y); reset = 0; ctxt = GSCurrentContext(); if (aPoint.x != 0 || aPoint.y != 0) { if ([[ctxt focusView] isFlipped]) aPoint.y -= _size.height; ctm = GSCurrentCTM(ctxt); DPStranslate(ctxt, aPoint.x, aPoint.y); reset = 1; } ok = [self draw]; if (reset) GSSetCTM(ctxt, ctm); return ok; } - (BOOL) drawInRect: (NSRect)aRect { NSSize scale; BOOL ok; NSGraphicsContext *ctxt; NSAffineTransform *ctm; NSDebugLLog(@"NSImage", @"Drawing in rect (%f %f %f %f)\n", NSMinX(aRect), NSMinY(aRect), NSWidth(aRect), NSHeight(aRect)); // if both are zero and the delegate isn't set. if (_size.width == 0 && _size.height == 0 && _delegate == nil) return NO; ctxt = GSCurrentContext(); // if either is zero, don't scale at all. if (_size.width == 0 || _size.height == 0) { scale = NSMakeSize(NSWidth(aRect), NSHeight(aRect)); } else { scale = NSMakeSize(NSWidth(aRect) / _size.width, NSHeight(aRect) / _size.height); } if ([[ctxt focusView] isFlipped]) aRect.origin.y -= NSHeight(aRect); ctm = GSCurrentCTM(ctxt); DPStranslate(ctxt, NSMinX(aRect), NSMinY(aRect)); DPSscale(ctxt, scale.width, scale.height); ok = [self draw]; GSSetCTM(ctxt, ctm); return ok; } // NSCoding protocol - (void) encodeWithCoder: (NSCoder*)aCoder { [super encodeWithCoder: aCoder]; [aCoder encodeObject: _delegate]; [aCoder encodeValueOfObjCType: @encode(SEL) at: &_selector]; } - (id) initWithCoder: (NSCoder*)aDecoder { self = [super initWithCoder: aDecoder]; [aDecoder decodeValueOfObjCType: @encode(id) at: &_delegate]; [aDecoder decodeValueOfObjCType: @encode(SEL) at: &_selector]; return self; } @end