1/** <title>NSCustomImageRep</title> 2 3 <abstract>Custom image representation.</abstract> 4 5 Copyright (C) 1996 Free Software Foundation, Inc. 6 7 Author: Adam Fedor <fedor@colorado.edu> 8 Date: Mar 1996 9 10 This file is part of the GNUstep Application Kit Library. 11 12 This library is free software; you can redistribute it and/or 13 modify it under the terms of the GNU Lesser General Public 14 License as published by the Free Software Foundation; either 15 version 2 of the License, or (at your option) any later version. 16 17 This library is distributed in the hope that it will be useful, 18 but WITHOUT ANY WARRANTY; without even the implied warranty of 19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20 Lesser General Public License for more details. 21 22 You should have received a copy of the GNU Lesser General Public 23 License along with this library; see the file COPYING.LIB. 24 If not, see <http://www.gnu.org/licenses/> or write to the 25 Free Software Foundation, 51 Franklin Street, Fifth Floor, 26 Boston, MA 02110-1301, USA. 27*/ 28 29#include "config.h" 30#import <Foundation/NSCoder.h> 31#import <Foundation/NSDebug.h> 32#import "AppKit/NSCustomImageRep.h" 33#import "AppKit/NSGraphicsContext.h" 34#import "AppKit/NSView.h" 35#import "AppKit/NSColor.h" 36#import "AppKit/DPSOperators.h" 37 38/** <p>TODO : Desciption</p> 39 */ 40@implementation NSCustomImageRep 41 42/**<p> Initializes a new NSCustomImageRep with. When a -draw message is 43 recieved it send aSelector message to the delegate anObject. 44 The delegate is not retained, so it is the caller's responsibility 45 to ensure the delegate remains valid for the life of the receiver.</p> 46 <p>See Also: -delegate -drawSelector [NSImageRep-draw]</p> 47 */ 48- (id) initWithDrawSelector: (SEL)aSelector 49 delegate: (id)anObject 50{ 51 if ( ! ( self = [super init] ) ) 52 return nil; 53 54 _delegate = anObject; 55 _selector = aSelector; 56 return self; 57} 58 59- (void) dealloc 60{ 61 [super dealloc]; 62} 63 64/** <p>Returns the NSCustomImageRep's delegate.</p> 65 <p>See Also: -initWithDrawSelector:delegate:</p> 66 */ 67- (id) delegate 68{ 69 return _delegate; 70} 71 72/**<p>Returns the draw method sent to the delegate when NSCustomImageRep 73 recieved a -draw message.</p> 74 <p>See Also: -initWithDrawSelector:delegate: [NSImageRep-draw]</p> 75 */ 76- (SEL) drawSelector 77{ 78 return _selector; 79} 80 81- (BOOL) draw 82{ 83 [_delegate performSelector: _selector withObject: self]; 84 return YES; 85} 86 87// 88// TODO: For both of the following methods we can extract the 89// logic in the superclass to another method and call it here 90// if the delegate is set from both places. 91// 92- (BOOL) drawAtPoint: (NSPoint)aPoint 93{ 94 BOOL ok, reset; 95 NSGraphicsContext *ctxt; 96 NSAffineTransform *ctm = nil; 97 98 // if both are zero and the delegate isn't set, return no. 99 if (_size.width == 0 && _size.height == 0 && _delegate == nil) 100 return NO; 101 102 NSDebugLLog(@"NSImage", @"Drawing at point %f %f\n", aPoint.x, aPoint.y); 103 reset = 0; 104 ctxt = GSCurrentContext(); 105 if (aPoint.x != 0 || aPoint.y != 0) 106 { 107 ctm = GSCurrentCTM(ctxt); 108 DPStranslate(ctxt, aPoint.x, aPoint.y); 109 reset = 1; 110 } 111 ok = [self draw]; 112 if (reset) 113 GSSetCTM(ctxt, ctm); 114 return ok; 115} 116 117- (BOOL) drawInRect: (NSRect)aRect 118{ 119 NSSize scale; 120 BOOL ok; 121 NSGraphicsContext *ctxt; 122 NSAffineTransform *ctm; 123 124 NSDebugLLog(@"NSImage", @"Drawing in rect (%f %f %f %f)\n", 125 NSMinX(aRect), NSMinY(aRect), NSWidth(aRect), NSHeight(aRect)); 126 127 // if both are zero and the delegate isn't set. 128 if (_size.width == 0 && _size.height == 0 && _delegate == nil) 129 return NO; 130 131 ctxt = GSCurrentContext(); 132 133 // if either is zero, don't scale at all. 134 if (_size.width == 0 || _size.height == 0) 135 { 136 scale = NSMakeSize(1, 1); 137 } 138 else 139 { 140 scale = NSMakeSize(NSWidth(aRect) / _size.width, 141 NSHeight(aRect) / _size.height); 142 } 143 144 ctm = GSCurrentCTM(ctxt); 145 DPStranslate(ctxt, NSMinX(aRect), NSMinY(aRect)); 146 DPSscale(ctxt, scale.width, scale.height); 147 ok = [self draw]; 148 GSSetCTM(ctxt, ctm); 149 return ok; 150} 151 152// NSCoding protocol 153- (void) encodeWithCoder: (NSCoder*)aCoder 154{ 155 [super encodeWithCoder: aCoder]; 156 157 if ([aCoder allowsKeyedCoding]) 158 { 159 // FIXME 160 } 161 else 162 { 163 // FIXME: Should this be changed to encodeConditionalObject: ? 164 [aCoder encodeObject: _delegate]; 165 [aCoder encodeValueOfObjCType: @encode(SEL) at: &_selector]; 166 } 167} 168 169- (id) initWithCoder: (NSCoder*)aDecoder 170{ 171 self = [super initWithCoder: aDecoder]; 172 173 if ([aDecoder allowsKeyedCoding]) 174 { 175 // FIXME 176 } 177 else 178 { 179 [aDecoder decodeValueOfObjCType: @encode(id) at: &_delegate]; 180 [aDecoder decodeValueOfObjCType: @encode(SEL) at: &_selector]; 181 } 182 return self; 183} 184 185@end 186