mirror of
https://github.com/gnustep/libs-gui.git
synced 2025-04-22 20:50:44 +00:00
Merge pull request #57 from gnustep/NSColorSampler_branch
NSColorSampler branch
This commit is contained in:
commit
b161cfaf5c
5 changed files with 215 additions and 1 deletions
|
@ -141,6 +141,7 @@
|
|||
#import <AppKit/NSArrayController.h>
|
||||
#import <AppKit/NSBezierPath.h>
|
||||
#import <AppKit/NSButtonTouchBarItem.h>
|
||||
#import <AppKit/NSColorSampler.h>
|
||||
#import <AppKit/NSColorSpace.h>
|
||||
#import <AppKit/NSComboBox.h>
|
||||
#import <AppKit/NSComboBoxCell.h>
|
||||
|
|
53
Headers/AppKit/NSColorSampler.h
Normal file
53
Headers/AppKit/NSColorSampler.h
Normal file
|
@ -0,0 +1,53 @@
|
|||
/* Definition of class NSColorSampler
|
||||
Copyright (C) 2020 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.1 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 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.
|
||||
*/
|
||||
|
||||
#ifndef _NSColorSampler_h_GNUSTEP_GUI_INCLUDE
|
||||
#define _NSColorSampler_h_GNUSTEP_GUI_INCLUDE
|
||||
|
||||
#import <Foundation/NSObject.h>
|
||||
|
||||
#if OS_API_VERSION(MAC_OS_X_VERSION_10_0, GS_API_LATEST)
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
@class NSColor;
|
||||
|
||||
DEFINE_BLOCK_TYPE(GSColorSampleHandler, void, NSColor*);
|
||||
|
||||
@interface NSColorSampler : NSObject
|
||||
|
||||
- (void) showSamplerWithSelectionHandler: (GSColorSampleHandler)selectionHandler;
|
||||
|
||||
@end
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* GS_API_MACOSX */
|
||||
|
||||
#endif /* _NSColorSampler_h_GNUSTEP_GUI_INCLUDE */
|
||||
|
2
MISSING
2
MISSING
|
@ -12,7 +12,6 @@ MISSING HEADERS
|
|||
> NSCollectionViewGridLayout.h
|
||||
> NSCollectionViewLayout.h
|
||||
> NSCollectionViewTransitionLayout.h
|
||||
> NSColorSampler.h
|
||||
> NSDictionaryController.h
|
||||
> NSDiffableDataSource.h
|
||||
> NSDraggingItem.h
|
||||
|
@ -85,6 +84,7 @@ Completed
|
|||
> NSTouch.h
|
||||
> NSTouchBar.h
|
||||
> NSSpeechRecognizer.h
|
||||
> NSColorSampler.h
|
||||
|
||||
Mac Specific
|
||||
---
|
||||
|
|
|
@ -81,6 +81,7 @@ NSColorList.m \
|
|||
NSColorPanel.m \
|
||||
NSColorPicker.m \
|
||||
NSColorPickerTouchBarItem.m \
|
||||
NSColorSampler.m \
|
||||
NSColorSpace.m \
|
||||
NSColorWell.m \
|
||||
NSComboBox.m \
|
||||
|
@ -369,6 +370,7 @@ NSColorList.h \
|
|||
NSColorPanel.h \
|
||||
NSColorPicker.h \
|
||||
NSColorPickerTouchBarItem.h \
|
||||
NSColorSampler.h \
|
||||
NSColorSpace.h \
|
||||
NSColorWell.h \
|
||||
NSComboBox.h \
|
||||
|
|
158
Source/NSColorSampler.m
Normal file
158
Source/NSColorSampler.m
Normal file
|
@ -0,0 +1,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
|
||||
|
Loading…
Reference in a new issue