2001-12-17 16:51:51 +00:00
|
|
|
/** <title>NSCustomImageRep</title>
|
1996-05-30 20:03:15 +00:00
|
|
|
|
2001-12-17 16:51:51 +00:00
|
|
|
<abstract>Custom image representation.</abstract>
|
1996-05-30 20:03:15 +00:00
|
|
|
|
|
|
|
Copyright (C) 1996 Free Software Foundation, Inc.
|
|
|
|
|
1996-10-18 17:14:13 +00:00
|
|
|
Author: Adam Fedor <fedor@colorado.edu>
|
1996-08-22 18:51:08 +00:00
|
|
|
Date: Mar 1996
|
|
|
|
|
|
|
|
This file is part of the GNUstep Application Kit Library.
|
1996-05-30 20:03:15 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
1996-08-22 18:51:08 +00:00
|
|
|
|
1996-05-30 20:03:15 +00:00
|
|
|
You should have received a copy of the GNU Library General Public
|
1996-10-18 17:14:13 +00:00
|
|
|
License along with this library; see the file COPYING.LIB.
|
|
|
|
If not, write to the Free Software Foundation,
|
2005-05-26 02:52:46 +00:00
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
1996-08-22 18:51:08 +00:00
|
|
|
*/
|
1996-05-30 20:03:15 +00:00
|
|
|
|
2003-07-31 23:52:10 +00:00
|
|
|
#include "config.h"
|
2002-04-02 11:34:24 +00:00
|
|
|
#include <Foundation/NSCoder.h>
|
2005-04-03 19:03:38 +00:00
|
|
|
#include <Foundation/NSDebug.h>
|
|
|
|
#include "AppKit/NSCustomImageRep.h"
|
|
|
|
#include "AppKit/NSGraphicsContext.h"
|
|
|
|
#include "AppKit/NSView.h"
|
|
|
|
#include "AppKit/NSColor.h"
|
|
|
|
#include "AppKit/DPSOperators.h"
|
1996-05-30 20:03:15 +00:00
|
|
|
|
2005-11-17 10:56:58 +00:00
|
|
|
/** <p>TODO : Desciption</p>
|
|
|
|
*/
|
1996-05-30 20:03:15 +00:00
|
|
|
@implementation NSCustomImageRep
|
|
|
|
|
2005-11-17 10:56:58 +00:00
|
|
|
/** Initializes a new NSCustomImageRep with .When a -draw message is recieved
|
|
|
|
it send aSelector message to the delegate anObject.
|
|
|
|
*/
|
1996-08-22 18:51:08 +00:00
|
|
|
- (id) initWithDrawSelector: (SEL)aSelector
|
1999-03-02 08:58:30 +00:00
|
|
|
delegate: (id)anObject
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2005-11-17 10:56:58 +00:00
|
|
|
if ( ! ( self = [super init] ) )
|
|
|
|
return nil;
|
1996-05-30 20:03:15 +00:00
|
|
|
|
1996-08-22 18:51:08 +00:00
|
|
|
/* WARNING: Retaining the delegate may or may not create a cyclic graph */
|
2000-12-23 14:20:29 +00:00
|
|
|
_delegate = RETAIN(anObject);
|
|
|
|
_selector = aSelector;
|
1996-08-22 18:51:08 +00:00
|
|
|
return self;
|
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
1996-08-22 18:51:08 +00:00
|
|
|
- (void) dealloc
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2000-12-23 14:20:29 +00:00
|
|
|
RELEASE(_delegate);
|
1996-08-22 18:51:08 +00:00
|
|
|
[super dealloc];
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
2005-11-17 10:56:58 +00:00
|
|
|
/** <p>Returns the NSCustomImageRep's delegate</p>
|
|
|
|
<p>See Also: -initWithDrawSelector:delegate:</p>
|
|
|
|
*/
|
1996-08-22 18:51:08 +00:00
|
|
|
- (id) delegate
|
|
|
|
{
|
2000-12-23 14:20:29 +00:00
|
|
|
return _delegate;
|
1996-08-22 18:51:08 +00:00
|
|
|
}
|
|
|
|
|
2005-11-17 10:56:58 +00:00
|
|
|
/**<p>Returns the draw method selector sent to the delegate when
|
|
|
|
NSCustomImageRep recieved a -draw message</p>
|
|
|
|
*/
|
1996-08-22 18:51:08 +00:00
|
|
|
- (SEL) drawSelector
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2000-12-23 14:20:29 +00:00
|
|
|
return _selector;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
1996-08-22 18:51:08 +00:00
|
|
|
- (BOOL) draw
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2000-12-23 14:20:29 +00:00
|
|
|
[_delegate performSelector: _selector withObject: self];
|
1996-08-22 18:51:08 +00:00
|
|
|
return YES;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
2005-04-03 19:03:38 +00:00
|
|
|
//
|
|
|
|
// 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.
|
2005-11-16 11:34:25 +00:00
|
|
|
if (_size.width == 0 || _size.height == 0)
|
2005-04-03 19:03:38 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
1996-05-30 20:03:15 +00:00
|
|
|
// NSCoding protocol
|
1999-03-02 08:58:30 +00:00
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
1996-08-22 18:51:08 +00:00
|
|
|
[super encodeWithCoder: aCoder];
|
|
|
|
|
2000-12-23 14:20:29 +00:00
|
|
|
[aCoder encodeObject: _delegate];
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(SEL) at: &_selector];
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:58:30 +00:00
|
|
|
- (id) initWithCoder: (NSCoder*)aDecoder
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
1996-08-22 18:51:08 +00:00
|
|
|
self = [super initWithCoder: aDecoder];
|
1996-05-30 20:03:15 +00:00
|
|
|
|
2000-12-23 14:20:29 +00:00
|
|
|
[aDecoder decodeValueOfObjCType: @encode(id) at: &_delegate];
|
|
|
|
[aDecoder decodeValueOfObjCType: @encode(SEL) at: &_selector];
|
1996-05-30 20:03:15 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|