1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
/* 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.
*/
#import <Foundation/NSGeometry.h>
#import <Foundation/NSDate.h>
#import <Foundation/NSException.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSLock.h>
#import <AppKit/NSApplication.h>
#import <AppKit/NSBitmapImageRep.h>
#import <AppKit/NSColorSampler.h>
#import <AppKit/NSCursor.h>
#import <AppKit/NSColor.h>
#import <AppKit/NSEvent.h>
#import <AppKit/NSImage.h>
#import <AppKit/NSScreen.h>
#import <AppKit/NSPanel.h>
#import <GNUstepGUI/GSDisplayServer.h>
static NSLock *_gs_gui_color_sampler_lock = nil;
static NSColorSampler *_gs_gui_color_sampler = nil;
@interface NSWindow (private)
- (void) _captureMouse: sender;
- (void) _releaseMouse: sender;
@end
@implementation NSColorSampler
+ (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;
}
if (self != _gs_gui_color_sampler)
{
RELEASE(self);
return _gs_gui_color_sampler;
}
return self;
}
- (void) showSamplerWithSelectionHandler: (GSColorSampleHandler)selectionHandler
{
NSEvent *currentEvent;
NSCursor *cursor;
NSRect contentRect = NSMakeRect(-1024,-1024,0,0);
unsigned int style = NSTitledWindowMask | NSClosableWindowMask
| NSResizableWindowMask | NSUtilityWindowMask;
NSPanel *w = nil;
NSColor *color = nil;
[_gs_gui_color_sampler_lock lock];
w = [[NSPanel alloc] initWithContentRect: contentRect
styleMask: style
backing: NSBackingStoreRetained
defer: NO
screen: nil];
[w setBecomesKeyOnlyIfNeeded: YES];
[w makeKeyAndOrderFront: self];
[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)
*/
cursor = [[NSCursor alloc] initWithImage: [NSImage imageNamed: @"MagnifyGlass"]
hotSpot: NSMakePoint(12, 13)];
AUTORELEASE(cursor);
[cursor push];
NS_DURING
{
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);
}
NS_HANDLER
{
NSLog(@"Exception occurred in -[NSColorSampler showSamplerWithSelectionHandler:] : %@",
localException);
}
NS_ENDHANDLER;
CALL_BLOCK(selectionHandler, color);
RELEASE(color);
[NSCursor pop];
[w _releaseMouse: self];
[w close];
[_gs_gui_color_sampler_lock unlock];
}
@end
|