2020-03-12 08:14:09 +00:00
|
|
|
/* Implementation of class NSColorSampler
|
|
|
|
Copyright (C) 2019 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
By: Gregory John Casamento
|
|
|
|
Date: Thu Mar 12 03:11:27 EDT 2020
|
|
|
|
|
|
|
|
This file is part of the GNUstep Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser 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
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110 USA.
|
|
|
|
*/
|
|
|
|
|
2020-03-15 22:39:22 +00:00
|
|
|
#import <Foundation/NSGeometry.h>
|
|
|
|
#import <Foundation/NSDate.h>
|
|
|
|
#import <Foundation/NSException.h>
|
|
|
|
#import <Foundation/NSAutoreleasePool.h>
|
2020-03-20 08:25:42 +00:00
|
|
|
#import <Foundation/NSLock.h>
|
2020-03-15 22:39:22 +00:00
|
|
|
|
|
|
|
#import <AppKit/NSApplication.h>
|
|
|
|
#import <AppKit/NSBitmapImageRep.h>
|
2020-03-12 08:14:09 +00:00
|
|
|
#import <AppKit/NSColorSampler.h>
|
2020-03-15 22:39:22 +00:00
|
|
|
#import <AppKit/NSCursor.h>
|
|
|
|
#import <AppKit/NSColor.h>
|
|
|
|
#import <AppKit/NSEvent.h>
|
|
|
|
#import <AppKit/NSImage.h>
|
|
|
|
#import <AppKit/NSScreen.h>
|
2020-03-17 04:11:40 +00:00
|
|
|
#import <AppKit/NSPanel.h>
|
2020-03-15 22:39:22 +00:00
|
|
|
|
|
|
|
#import <GNUstepGUI/GSDisplayServer.h>
|
|
|
|
|
2020-03-20 08:25:42 +00:00
|
|
|
static NSLock *_gs_gui_color_sampler_lock = nil;
|
|
|
|
static NSColorSampler *_gs_gui_color_sampler = nil;
|
|
|
|
|
2020-03-15 22:39:22 +00:00
|
|
|
@interface NSWindow (private)
|
|
|
|
- (void) _captureMouse: sender;
|
|
|
|
- (void) _releaseMouse: sender;
|
|
|
|
@end
|
2020-03-12 08:14:09 +00:00
|
|
|
|
|
|
|
@implementation NSColorSampler
|
|
|
|
|
2020-03-20 08:25:42 +00:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (self == [NSColorSampler class])
|
|
|
|
{
|
|
|
|
// Initial version
|
|
|
|
[self setVersion: 1];
|
|
|
|
_gs_gui_color_sampler_lock = [NSLock new];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype) init
|
|
|
|
{
|
|
|
|
if (_gs_gui_color_sampler == nil)
|
|
|
|
{
|
|
|
|
_gs_gui_color_sampler = self;
|
|
|
|
}
|
2020-03-20 08:35:13 +00:00
|
|
|
|
|
|
|
if (self != _gs_gui_color_sampler)
|
2020-03-20 08:25:42 +00:00
|
|
|
{
|
|
|
|
RELEASE(self);
|
|
|
|
return _gs_gui_color_sampler;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-03-12 08:14:09 +00:00
|
|
|
- (void) showSamplerWithSelectionHandler: (GSColorSampleHandler)selectionHandler
|
|
|
|
{
|
2020-03-15 22:39:22 +00:00
|
|
|
NSEvent *currentEvent;
|
|
|
|
NSCursor *cursor;
|
2020-03-17 04:11:40 +00:00
|
|
|
NSRect contentRect = NSMakeRect(-1024,-1024,0,0);
|
|
|
|
unsigned int style = NSTitledWindowMask | NSClosableWindowMask
|
|
|
|
| NSResizableWindowMask | NSUtilityWindowMask;
|
2020-03-20 08:52:18 +00:00
|
|
|
NSPanel *w = nil;
|
2020-03-15 23:14:15 +00:00
|
|
|
NSColor *color = nil;
|
2020-03-20 08:25:42 +00:00
|
|
|
|
|
|
|
[_gs_gui_color_sampler_lock lock];
|
2020-03-20 08:52:18 +00:00
|
|
|
|
|
|
|
w = [[NSPanel alloc] initWithContentRect: contentRect
|
2020-03-20 08:53:20 +00:00
|
|
|
styleMask: style
|
|
|
|
backing: NSBackingStoreRetained
|
|
|
|
defer: NO
|
|
|
|
screen: nil];
|
2020-03-20 08:35:13 +00:00
|
|
|
|
|
|
|
[w setBecomesKeyOnlyIfNeeded: YES];
|
|
|
|
[w makeKeyAndOrderFront: self];
|
2020-03-15 22:39:22 +00:00
|
|
|
[w _captureMouse: self];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* There was code here to dynamically generate a magnifying glass
|
|
|
|
* cursor with a magnified portion of the screenshot in it,
|
|
|
|
* but changing the cursor rapidly on X seems to cause flicker,
|
|
|
|
* so we just use a plain magnifying glass. (dynamic code is in r33543)
|
|
|
|
*/
|
2020-03-20 08:54:02 +00:00
|
|
|
cursor = [[NSCursor alloc] initWithImage: [NSImage imageNamed: @"MagnifyGlass"]
|
|
|
|
hotSpot: NSMakePoint(12, 13)];
|
|
|
|
AUTORELEASE(cursor);
|
2020-03-15 22:39:22 +00:00
|
|
|
[cursor push];
|
|
|
|
|
|
|
|
NS_DURING
|
|
|
|
{
|
2020-03-17 04:11:40 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
NSPoint mouseLoc;
|
|
|
|
NSImage *img;
|
|
|
|
CREATE_AUTORELEASE_POOL(pool);
|
|
|
|
|
|
|
|
RELEASE(color);
|
|
|
|
currentEvent = [NSApp nextEventMatchingMask: NSLeftMouseDownMask | NSLeftMouseUpMask | NSMouseMovedMask
|
|
|
|
untilDate: [NSDate distantFuture]
|
|
|
|
inMode: NSEventTrackingRunLoopMode
|
|
|
|
dequeue: YES];
|
|
|
|
|
|
|
|
mouseLoc = [w convertBaseToScreen: [w mouseLocationOutsideOfEventStream]];
|
|
|
|
|
|
|
|
img = [GSCurrentServer() contentsOfScreen: [[w screen] screenNumber]
|
|
|
|
inRect: NSMakeRect(mouseLoc.x, mouseLoc.y, 1, 1)];
|
|
|
|
|
|
|
|
if (nil != img)
|
|
|
|
{
|
|
|
|
NSBitmapImageRep *rep = (NSBitmapImageRep *)[img bestRepresentationForDevice: nil];
|
|
|
|
color = [rep colorAtX: 0 y: 0];
|
|
|
|
RETAIN(color);
|
|
|
|
}
|
|
|
|
[pool drain];
|
|
|
|
} while ([currentEvent type] != NSLeftMouseUp &&
|
|
|
|
[currentEvent type] != NSLeftMouseDown);
|
2020-03-15 22:39:22 +00:00
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
NSLog(@"Exception occurred in -[NSColorSampler showSamplerWithSelectionHandler:] : %@",
|
|
|
|
localException);
|
|
|
|
}
|
2020-03-15 23:14:15 +00:00
|
|
|
NS_ENDHANDLER;
|
2020-03-17 04:11:40 +00:00
|
|
|
|
2020-03-15 23:14:15 +00:00
|
|
|
CALL_BLOCK(selectionHandler, color);
|
2020-03-15 23:44:11 +00:00
|
|
|
RELEASE(color);
|
2020-03-15 22:39:22 +00:00
|
|
|
[NSCursor pop];
|
|
|
|
[w _releaseMouse: self];
|
|
|
|
[w close];
|
2020-03-20 08:25:42 +00:00
|
|
|
|
|
|
|
[_gs_gui_color_sampler_lock unlock];
|
2020-03-12 08:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|