2001-12-17 16:51:51 +00:00
|
|
|
/** <title>NSColorWell</title>
|
1996-05-30 20:03:15 +00:00
|
|
|
|
2001-12-17 16:51:51 +00:00
|
|
|
<abstract>Control for selecting and display a single color value.</abstract>
|
1996-05-30 20:03:15 +00:00
|
|
|
|
|
|
|
Copyright (C) 1996 Free Software Foundation, Inc.
|
|
|
|
|
2001-12-17 16:51:51 +00:00
|
|
|
Author: Scott Christley <scottc@net-community.com>
|
1996-05-30 20:03:15 +00:00
|
|
|
Date: 1996
|
2001-12-17 16:51:51 +00:00
|
|
|
Author: Felipe A. Rodriguez <far@ix.netcom.com>
|
1998-11-25 17:16:48 +00:00
|
|
|
Date: May 1998
|
1996-05-30 20:03:15 +00:00
|
|
|
|
|
|
|
This file is part of the GNUstep GUI Library.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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,
|
|
|
|
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
1996-05-30 20:03:15 +00:00
|
|
|
*/
|
|
|
|
|
1997-09-23 22:43:24 +00:00
|
|
|
#include <gnustep/gui/config.h>
|
1999-12-07 15:57:21 +00:00
|
|
|
#include <AppKit/NSActionCell.h>
|
2000-04-28 00:34:46 +00:00
|
|
|
#include <AppKit/NSApplication.h>
|
2000-04-18 02:28:13 +00:00
|
|
|
#include <AppKit/NSColorPanel.h>
|
1997-02-18 00:29:25 +00:00
|
|
|
#include <AppKit/NSColorWell.h>
|
1997-03-05 01:11:17 +00:00
|
|
|
#include <AppKit/NSColor.h>
|
2002-03-28 03:53:18 +00:00
|
|
|
#include <AppKit/NSDragging.h>
|
1999-02-15 19:35:02 +00:00
|
|
|
#include <AppKit/NSGraphics.h>
|
2000-04-18 02:28:13 +00:00
|
|
|
#include <AppKit/NSPasteboard.h>
|
2000-04-28 00:34:46 +00:00
|
|
|
#include <AppKit/NSWindow.h>
|
2002-04-11 23:17:42 +00:00
|
|
|
#include <Foundation/NSDebug.h>
|
2000-04-28 00:34:46 +00:00
|
|
|
#include <Foundation/NSNotification.h>
|
1998-11-25 17:16:48 +00:00
|
|
|
|
2000-04-28 00:34:46 +00:00
|
|
|
static NSString *GSColorWellDidBecomeExclusiveNotification =
|
|
|
|
@"GSColorWellDidBecomeExclusiveNotification";
|
1998-11-25 17:16:48 +00:00
|
|
|
|
1996-05-30 20:03:15 +00:00
|
|
|
@implementation NSColorWell
|
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
/*
|
|
|
|
* Class methods
|
|
|
|
*/
|
|
|
|
+ (void) initialize
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
1999-03-16 20:34:05 +00:00
|
|
|
if (self == [NSColorWell class])
|
2001-02-18 15:53:19 +00:00
|
|
|
{
|
|
|
|
[self setVersion: 1];
|
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
/*
|
|
|
|
* Instance methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
- (BOOL) acceptsFirstMouse: (NSEvent *)event
|
1999-12-07 15:57:21 +00:00
|
|
|
{
|
2001-02-18 15:53:19 +00:00
|
|
|
return YES;
|
1999-12-07 15:57:21 +00:00
|
|
|
}
|
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
- (SEL) action
|
1997-03-05 01:11:17 +00:00
|
|
|
{
|
2001-02-18 15:53:19 +00:00
|
|
|
return _action;
|
|
|
|
}
|
1997-03-05 01:11:17 +00:00
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
- (void) activate: (BOOL)exclusive
|
|
|
|
{
|
|
|
|
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
|
|
|
|
NSColorPanel *colorPanel = [NSColorPanel sharedColorPanel];
|
1997-03-05 01:11:17 +00:00
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
if (exclusive == YES)
|
|
|
|
{
|
|
|
|
[nc postNotificationName: GSColorWellDidBecomeExclusiveNotification
|
|
|
|
object: self];
|
|
|
|
}
|
2000-04-18 02:28:13 +00:00
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
[nc addObserver: self
|
|
|
|
selector: @selector(deactivate)
|
|
|
|
name: GSColorWellDidBecomeExclusiveNotification
|
|
|
|
object: nil];
|
|
|
|
|
2002-10-27 22:33:16 +00:00
|
|
|
[nc addObserver: self
|
|
|
|
selector: @selector(_takeColorFromPanel:)
|
|
|
|
name: NSColorPanelColorChangedNotification
|
|
|
|
object: nil];
|
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
_is_active = YES;
|
|
|
|
|
|
|
|
[colorPanel setColor: _the_color];
|
|
|
|
[colorPanel orderFront: self];
|
|
|
|
|
|
|
|
[self setNeedsDisplay: YES];
|
1997-03-05 01:11:17 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
- (NSColor *) color
|
1997-10-09 22:55:31 +00:00
|
|
|
{
|
2001-02-18 15:53:19 +00:00
|
|
|
return _the_color;
|
|
|
|
}
|
2001-01-08 01:05:32 +00:00
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
- (void) deactivate
|
|
|
|
{
|
|
|
|
_is_active = NO;
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver: self];
|
|
|
|
|
|
|
|
[self setNeedsDisplay: YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
if (_is_active == YES)
|
|
|
|
{
|
|
|
|
[self deactivate];
|
|
|
|
}
|
2000-04-28 00:34:46 +00:00
|
|
|
TEST_RELEASE(_the_color);
|
2000-04-18 02:28:13 +00:00
|
|
|
[self unregisterDraggedTypes];
|
1997-10-09 22:55:31 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
- (unsigned int) draggingEntered: (id <NSDraggingInfo>)sender
|
|
|
|
{
|
|
|
|
NSPasteboard *pb;
|
|
|
|
NSDragOperation sourceDragMask;
|
|
|
|
|
|
|
|
NSDebugLLog(@"NSColorWell", @"%@: draggingEntered", self);
|
|
|
|
sourceDragMask = [sender draggingSourceOperationMask];
|
|
|
|
pb = [sender draggingPasteboard];
|
|
|
|
|
|
|
|
if ([[pb types] indexOfObject: NSColorPboardType] != NSNotFound)
|
|
|
|
{
|
|
|
|
if (sourceDragMask & NSDragOperationCopy)
|
|
|
|
{
|
|
|
|
return NSDragOperationCopy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NSDragOperationNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (unsigned int) draggingSourceOperationMaskForLocal: (BOOL)flag
|
|
|
|
{
|
|
|
|
return NSDragOperationCopy;
|
|
|
|
}
|
|
|
|
|
2000-04-28 00:34:46 +00:00
|
|
|
- (void) drawRect: (NSRect)clipRect
|
1996-12-05 16:16:21 +00:00
|
|
|
{
|
2000-01-10 02:04:25 +00:00
|
|
|
NSRect aRect = _bounds;
|
1998-11-25 17:16:48 +00:00
|
|
|
|
2000-04-28 00:34:46 +00:00
|
|
|
if (NSIntersectsRect(aRect, clipRect) == NO)
|
2001-02-18 15:53:19 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
1997-03-05 01:11:17 +00:00
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
if (_is_bordered == YES)
|
1999-03-03 17:07:42 +00:00
|
|
|
{
|
|
|
|
/*
|
2000-04-28 00:34:46 +00:00
|
|
|
* Draw border.
|
1999-03-03 17:07:42 +00:00
|
|
|
*/
|
2000-04-28 00:34:46 +00:00
|
|
|
NSDrawButton(aRect, clipRect);
|
1999-03-03 17:07:42 +00:00
|
|
|
|
|
|
|
/*
|
1999-08-31 09:19:39 +00:00
|
|
|
* Fill in control color.
|
1999-03-03 17:07:42 +00:00
|
|
|
*/
|
|
|
|
aRect = NSInsetRect(aRect, 2.0, 2.0);
|
2001-02-18 15:53:19 +00:00
|
|
|
if (_is_active == YES)
|
|
|
|
{
|
|
|
|
[[NSColor selectedControlColor] set];
|
|
|
|
}
|
2000-04-28 00:34:46 +00:00
|
|
|
else
|
2001-02-18 15:53:19 +00:00
|
|
|
{
|
|
|
|
[[NSColor controlColor] set];
|
|
|
|
}
|
2000-04-28 00:34:46 +00:00
|
|
|
NSRectFill(NSIntersectionRect(aRect, clipRect));
|
1999-12-07 15:57:21 +00:00
|
|
|
|
1999-03-03 17:07:42 +00:00
|
|
|
/*
|
2000-04-28 00:34:46 +00:00
|
|
|
* Set an inset rect for the color area
|
1999-03-03 17:07:42 +00:00
|
|
|
*/
|
2000-04-28 00:34:46 +00:00
|
|
|
_wellRect = NSInsetRect(_bounds, 8.0, 8.0);
|
1999-03-03 17:07:42 +00:00
|
|
|
}
|
|
|
|
else
|
2001-02-18 15:53:19 +00:00
|
|
|
{
|
|
|
|
_wellRect = _bounds;
|
|
|
|
}
|
2000-04-28 00:34:46 +00:00
|
|
|
|
|
|
|
aRect = _wellRect;
|
1999-03-03 17:07:42 +00:00
|
|
|
|
2000-04-28 00:34:46 +00:00
|
|
|
/*
|
|
|
|
* OpenStep 4.2 behavior is to omit the inner border for
|
|
|
|
* non-enabled NSColorWell objects.
|
|
|
|
*/
|
|
|
|
if ([self isEnabled])
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Draw inner frame.
|
|
|
|
*/
|
|
|
|
NSDrawGrayBezel(aRect, clipRect);
|
|
|
|
aRect = NSInsetRect(aRect, 2.0, 2.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
[self drawWellInside: NSIntersectionRect(aRect, clipRect)];
|
1996-12-05 16:16:21 +00:00
|
|
|
}
|
|
|
|
|
1999-03-03 17:07:42 +00:00
|
|
|
- (void) drawWellInside: (NSRect)insideRect
|
1996-12-05 16:16:21 +00:00
|
|
|
{
|
1999-03-03 17:07:42 +00:00
|
|
|
if (NSIsEmptyRect(insideRect))
|
2001-01-27 12:53:19 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2000-04-28 00:34:46 +00:00
|
|
|
[_the_color drawSwatchInRect: insideRect];
|
1996-12-05 16:16:21 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
2001-01-27 12:57:06 +00:00
|
|
|
{
|
2001-02-18 15:53:19 +00:00
|
|
|
[super encodeWithCoder: aCoder];
|
|
|
|
[aCoder encodeObject: _the_color];
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &_is_active];
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &_is_bordered];
|
|
|
|
[aCoder encodeConditionalObject: _target];
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(SEL) at: &_action];
|
2001-01-27 12:57:06 +00:00
|
|
|
}
|
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
- (id) initWithCoder: (NSCoder*)aDecoder
|
2000-04-18 02:28:13 +00:00
|
|
|
{
|
2001-02-18 15:53:19 +00:00
|
|
|
self = [super initWithCoder: aDecoder];
|
|
|
|
if (self != nil)
|
2000-04-28 00:34:46 +00:00
|
|
|
{
|
2001-02-18 15:53:19 +00:00
|
|
|
[aDecoder decodeValueOfObjCType: @encode(id) at: &_the_color];
|
|
|
|
[aDecoder decodeValueOfObjCType: @encode(BOOL) at: &_is_active];
|
|
|
|
[aDecoder decodeValueOfObjCType: @encode(BOOL) at: &_is_bordered];
|
|
|
|
[aDecoder decodeValueOfObjCType: @encode(id) at: &_target];
|
|
|
|
// Undo RETAIN by decoder
|
|
|
|
TEST_RELEASE(_target);
|
|
|
|
[aDecoder decodeValueOfObjCType: @encode(SEL) at: &_action];
|
2002-10-27 22:33:16 +00:00
|
|
|
[self registerForDraggedTypes:
|
|
|
|
[NSArray arrayWithObjects: NSColorPboardType, nil]];
|
2000-04-28 00:34:46 +00:00
|
|
|
}
|
2001-02-18 15:53:19 +00:00
|
|
|
return self;
|
1999-11-16 23:49:35 +00:00
|
|
|
}
|
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
- (id) initWithFrame: (NSRect)frameRect
|
1996-12-05 16:16:21 +00:00
|
|
|
{
|
2001-02-18 15:53:19 +00:00
|
|
|
self = [super initWithFrame: frameRect];
|
|
|
|
if (self != nil)
|
2000-04-28 00:34:46 +00:00
|
|
|
{
|
2001-02-18 15:53:19 +00:00
|
|
|
_is_bordered = YES;
|
|
|
|
_is_active = NO;
|
|
|
|
_the_color = RETAIN([NSColor blackColor]);
|
2000-04-28 00:34:46 +00:00
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
[self registerForDraggedTypes:
|
|
|
|
[NSArray arrayWithObjects: NSColorPboardType, nil]];
|
|
|
|
}
|
|
|
|
return self;
|
1996-12-05 16:16:21 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
- (BOOL) isActive
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2000-04-28 00:34:46 +00:00
|
|
|
return _is_active;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
- (BOOL) isBordered
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2001-02-18 15:53:19 +00:00
|
|
|
return _is_bordered;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
- (BOOL) isOpaque
|
1996-12-05 16:16:21 +00:00
|
|
|
{
|
2001-02-18 15:53:19 +00:00
|
|
|
return _is_bordered;
|
1996-12-05 16:16:21 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
- (void) mouseDown: (NSEvent *)theEvent
|
1996-12-05 16:16:21 +00:00
|
|
|
{
|
2001-02-18 15:53:19 +00:00
|
|
|
NSPoint point = [self convertPoint: [theEvent locationInWindow]
|
|
|
|
fromView: nil];
|
|
|
|
|
|
|
|
if ([self mouse: point inRect: _wellRect])
|
2001-01-27 12:53:19 +00:00
|
|
|
{
|
2001-02-18 15:53:19 +00:00
|
|
|
[NSColorPanel dragColor: _the_color
|
|
|
|
withEvent: theEvent
|
|
|
|
fromView: self];
|
|
|
|
}
|
|
|
|
else if (_is_active == NO)
|
|
|
|
{
|
|
|
|
[self activate: YES];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self deactivate];
|
2001-01-27 12:53:19 +00:00
|
|
|
}
|
1996-12-05 16:16:21 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
- (BOOL) performDragOperation: (id <NSDraggingInfo>)sender
|
2001-02-14 22:50:06 +00:00
|
|
|
{
|
2001-02-18 15:53:19 +00:00
|
|
|
NSPasteboard *pb = [sender draggingPasteboard];
|
|
|
|
|
|
|
|
NSDebugLLog(@"NSColorWell", @"%@: performDragOperation", self);
|
|
|
|
[self setColor: [NSColor colorFromPasteboard: pb]];
|
|
|
|
return YES;
|
2001-02-14 22:50:06 +00:00
|
|
|
}
|
2001-02-18 15:53:19 +00:00
|
|
|
|
|
|
|
- (BOOL) prepareForDragOperation: (id <NSDraggingInfo>)sender
|
2001-02-14 22:50:06 +00:00
|
|
|
{
|
2001-02-18 15:53:19 +00:00
|
|
|
return YES;
|
2001-02-14 22:50:06 +00:00
|
|
|
}
|
2001-02-18 15:53:19 +00:00
|
|
|
|
|
|
|
- (void) setAction: (SEL)action
|
2001-02-14 22:50:06 +00:00
|
|
|
{
|
2001-02-18 15:53:19 +00:00
|
|
|
_action = action;
|
2001-02-14 22:50:06 +00:00
|
|
|
}
|
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
- (void) setBordered: (BOOL)bordered
|
2001-02-14 22:50:06 +00:00
|
|
|
{
|
2001-02-18 15:53:19 +00:00
|
|
|
_is_bordered = bordered;
|
|
|
|
[self setNeedsDisplay: YES];
|
2001-02-14 22:50:06 +00:00
|
|
|
}
|
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
- (void) setColor: (NSColor *)color
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2001-02-18 15:53:19 +00:00
|
|
|
ASSIGN(_the_color, color);
|
|
|
|
/*
|
|
|
|
* Experimentation with NeXTstep shows that when the color of an active
|
|
|
|
* colorwell is set, the color of the shared color panel is set too,
|
|
|
|
* though this does not raise the color panel, only the event of
|
|
|
|
* activation does that.
|
|
|
|
*/
|
|
|
|
if ([self isActive])
|
|
|
|
{
|
|
|
|
NSColorPanel *colorPanel = [NSColorPanel sharedColorPanel];
|
1996-05-30 20:03:15 +00:00
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
[colorPanel setColor: _the_color];
|
|
|
|
}
|
|
|
|
// Notify our target of colour change
|
|
|
|
[self sendAction: _action to: _target];
|
2000-04-28 00:34:46 +00:00
|
|
|
[self setNeedsDisplay: YES];
|
1996-12-05 16:16:21 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
- (void) setTarget: (id)target
|
2000-04-18 02:28:13 +00:00
|
|
|
{
|
2001-02-18 15:53:19 +00:00
|
|
|
_target = target;
|
2000-04-18 02:28:13 +00:00
|
|
|
}
|
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
- (void) takeColorFrom: (id)sender
|
2000-04-18 02:28:13 +00:00
|
|
|
{
|
2001-02-18 15:53:19 +00:00
|
|
|
if ([sender respondsToSelector: @selector(color)])
|
2000-04-18 02:28:13 +00:00
|
|
|
{
|
2001-02-18 15:53:19 +00:00
|
|
|
[self setColor: [sender color]];
|
2000-04-18 02:28:13 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
2002-10-27 22:33:16 +00:00
|
|
|
- (void) _takeColorFromPanel: (NSNotification *) notification
|
|
|
|
{
|
|
|
|
id sender = [notification object];
|
|
|
|
|
|
|
|
if ([sender respondsToSelector: @selector(color)])
|
|
|
|
{
|
|
|
|
ASSIGN(_the_color, [(id)sender color]);
|
|
|
|
|
|
|
|
[self sendAction: _action to: _target];
|
|
|
|
[self setNeedsDisplay: YES];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-18 15:53:19 +00:00
|
|
|
- (id) target
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2001-02-18 15:53:19 +00:00
|
|
|
return _target;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
1997-03-05 01:11:17 +00:00
|
|
|
|