mirror of
https://github.com/gnustep/libs-gui.git
synced 2025-05-30 23:50:47 +00:00
Add rough draft NSColorPanel.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@6475 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
d14f57527f
commit
a5b17abdd7
2 changed files with 294 additions and 25 deletions
|
@ -27,7 +27,145 @@
|
|||
*/
|
||||
|
||||
#include <gnustep/gui/config.h>
|
||||
#include <Foundation/NSBundle.h>
|
||||
#include <Foundation/NSEnumerator.h>
|
||||
#include <Foundation/NSFileManager.h>
|
||||
#include <Foundation/NSLock.h>
|
||||
#include <Foundation/NSPathUtilities.h>
|
||||
#include <AppKit/NSColor.h>
|
||||
#include <AppKit/NSColorPanel.h>
|
||||
#include <AppKit/NSColorPicker.h>
|
||||
#include <AppKit/NSColorPicking.h>
|
||||
#include <AppKit/NSColorWell.h>
|
||||
#include <AppKit/NSPasteboard.h>
|
||||
#include <AppKit/NSWindow.h>
|
||||
#include <AppKit/IMLoading.h>
|
||||
|
||||
static NSLock *_gs_gui_color_panel_lock = nil;
|
||||
static NSColorPanel *_gs_gui_color_panel = nil;
|
||||
static int _gs_gui_color_picker_mask = NSColorPanelAllModesMask;
|
||||
static int _gs_gui_color_picker_mode = NSRGBModeColorPanel;
|
||||
|
||||
@interface GSAppKitPanelController : NSObject
|
||||
{
|
||||
@public
|
||||
id panel;
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation GSAppKitPanelController
|
||||
@end
|
||||
|
||||
@interface NSColorPanel (PrivateMethods)
|
||||
- (void) _loadPickers;
|
||||
- (void) _loadPickerAtPath: (NSString *)path;
|
||||
- (void) _fixupMatrix;
|
||||
- (void) _setupPickers;
|
||||
- (void) _showNewPicker: (id)sender;
|
||||
@end
|
||||
|
||||
@implementation NSColorPanel (PrivateMethods)
|
||||
|
||||
- (void) _loadPickers
|
||||
{
|
||||
NSArray *paths;
|
||||
NSString *path;
|
||||
NSEnumerator *pathEnumerator;
|
||||
NSArray *bundles;
|
||||
NSEnumerator *bundleEnumerator;
|
||||
NSString *bundleName;
|
||||
|
||||
_pickers = [NSMutableArray new];
|
||||
|
||||
paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
|
||||
NSAllDomainsMask, YES);
|
||||
|
||||
pathEnumerator = [paths objectEnumerator];
|
||||
while ((path = [pathEnumerator nextObject]))
|
||||
{
|
||||
path = [path stringByAppendingPathComponent: @"ColorPickers"];
|
||||
bundles = [[NSFileManager defaultManager] directoryContentsAtPath: path];
|
||||
|
||||
bundleEnumerator = [bundles objectEnumerator];
|
||||
while ((bundleName = [bundleEnumerator nextObject]))
|
||||
[self _loadPickerAtPath:
|
||||
[path stringByAppendingPathComponent: bundleName]];
|
||||
}
|
||||
|
||||
paths = [[NSBundle mainBundle] pathsForResourcesOfType: @"bundle"
|
||||
inDirectory: @"ColorPickers"];
|
||||
|
||||
pathEnumerator = [paths objectEnumerator];
|
||||
while ((path = [pathEnumerator nextObject]))
|
||||
[self _loadPickerAtPath: path];
|
||||
}
|
||||
|
||||
- (void) _loadPickerAtPath: (NSString *)path
|
||||
{
|
||||
NSBundle *bundle;
|
||||
Class pickerClass;
|
||||
NSColorPicker *picker;
|
||||
|
||||
bundle = [NSBundle bundleWithPath: path];
|
||||
if (bundle && (pickerClass = [bundle principalClass]))
|
||||
{
|
||||
picker = [[pickerClass alloc] initWithPickerMask:_gs_gui_color_picker_mask
|
||||
colorPanel:_gs_gui_color_panel];
|
||||
if (picker && [picker conformsToProtocol:@protocol(NSColorPickingCustom)])
|
||||
{
|
||||
[picker provideNewView: YES];
|
||||
[_pickers addObject: picker];
|
||||
}
|
||||
else
|
||||
NSLog(@"%@ does not contain a valid color picker.");
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME - this is a HACK to get around problems in the gmodel code
|
||||
- (void) _fixupMatrix
|
||||
{
|
||||
[_pickerMatrix setFrame: NSMakeRect(4, 190, 192, 36)];
|
||||
}
|
||||
|
||||
- (void) _setupPickers
|
||||
{
|
||||
NSColorPicker *picker;
|
||||
NSButtonCell *cell;
|
||||
NSMutableArray *cells = [NSMutableArray new];
|
||||
int i, count;
|
||||
NSSize size = [_pickerMatrix frame].size;
|
||||
|
||||
count = [_pickers count];
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
cell = [[_pickerMatrix prototype] copy];
|
||||
[cell setTag: i];
|
||||
picker = [_pickers objectAtIndex: i];
|
||||
[picker insertNewButtonImage: [picker provideNewButtonImage] in: cell];
|
||||
[cells addObject: cell];
|
||||
}
|
||||
|
||||
[_pickerMatrix addRowWithCells: cells];
|
||||
[_pickerMatrix setCellSize: NSMakeSize(size.width / count, size.height)];
|
||||
[_pickerMatrix setTarget: self];
|
||||
[_pickerMatrix setAction: @selector(_showNewPicker:)];
|
||||
|
||||
// use the space occupied by the matrix of color picker buttons if the
|
||||
// button matrix is useless, i.e. it contains only one button
|
||||
if (count < 2)
|
||||
{
|
||||
[_pickerBox setFrame: NSUnionRect([_pickerBox frame],
|
||||
[_pickerMatrix frame])];
|
||||
[_pickerBox setNeedsDisplay: YES];
|
||||
}
|
||||
}
|
||||
|
||||
- (void) _showNewPicker: (id)sender
|
||||
{
|
||||
_currentPicker = [_pickers objectAtIndex: [sender selectedColumn]];
|
||||
[_pickerBox setContentView: [_currentPicker provideNewView: NO]];
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation NSColorPanel
|
||||
|
||||
|
@ -40,6 +178,7 @@
|
|||
{
|
||||
// Initial version
|
||||
[self setVersion:1];
|
||||
_gs_gui_color_panel_lock = [NSLock new];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,31 +187,71 @@
|
|||
//
|
||||
+ (NSColorPanel *)sharedColorPanel
|
||||
{
|
||||
return nil;
|
||||
if (_gs_gui_color_panel == nil)
|
||||
{
|
||||
[_gs_gui_color_panel_lock lock];
|
||||
if (!_gs_gui_color_panel)
|
||||
{
|
||||
GSAppKitPanelController *panelCtrl = [GSAppKitPanelController new];
|
||||
|
||||
if ([GMModel loadIMFile:@"ColorPanel" owner:panelCtrl])
|
||||
{
|
||||
_gs_gui_color_panel = panelCtrl->panel;
|
||||
[_gs_gui_color_panel _fixupMatrix];
|
||||
[_gs_gui_color_panel _loadPickers];
|
||||
[_gs_gui_color_panel _setupPickers];
|
||||
}
|
||||
}
|
||||
[_gs_gui_color_panel_lock unlock];
|
||||
}
|
||||
|
||||
//[_gs_gui_color_panel setMode: _gs_gui_color_picker_mode];
|
||||
[_gs_gui_color_panel setMode: NSColorListModeColorPanel];
|
||||
return _gs_gui_color_panel;
|
||||
}
|
||||
|
||||
+ (BOOL)sharedColorPanelExists
|
||||
{
|
||||
return NO;
|
||||
return (_gs_gui_color_panel == nil) ? NO : YES;
|
||||
}
|
||||
|
||||
//
|
||||
// Setting the NSColorPanel
|
||||
//
|
||||
+ (void)setPickerMask:(int)mask
|
||||
{}
|
||||
{
|
||||
_gs_gui_color_picker_mask = mask;
|
||||
}
|
||||
|
||||
+ (void)setPickerMode:(int)mode
|
||||
{}
|
||||
{
|
||||
_gs_gui_color_picker_mode = mode;
|
||||
}
|
||||
|
||||
//
|
||||
// Setting Color
|
||||
//
|
||||
+ (BOOL)dragColor:(NSColor **)aColor
|
||||
withEvent:(NSEvent *)anEvent
|
||||
fromView:(NSView *)sourceView
|
||||
+ (BOOL)dragColor:(NSColor *)aColor
|
||||
withEvent:(NSEvent *)anEvent
|
||||
fromView:(NSView *)sourceView
|
||||
{
|
||||
return NO;
|
||||
NSPasteboard *pb = [NSPasteboard pasteboardWithName: NSDragPboard];
|
||||
NSImage *image = [NSImage imageNamed: @"colorwell_DragImage"];
|
||||
|
||||
[pb declareTypes: [NSArray arrayWithObjects: NSColorPboardType, nil]
|
||||
owner: aColor];
|
||||
[aColor writeToPasteboard: pb];
|
||||
[image setBackgroundColor: aColor];
|
||||
|
||||
[sourceView dragImage: image
|
||||
at: [sourceView frame].origin
|
||||
offset: NSMakeSize(0,0)
|
||||
event: anEvent
|
||||
pasteboard: pb
|
||||
source: sourceView
|
||||
slideBack: NO];
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
//
|
||||
|
@ -84,66 +263,151 @@ fromView:(NSView *)sourceView
|
|||
//
|
||||
- (NSView *)accessoryView
|
||||
{
|
||||
return nil;
|
||||
return [_accessoryBox contentView];
|
||||
}
|
||||
|
||||
- (BOOL)isContinuous
|
||||
{
|
||||
return NO;
|
||||
return _isContinuous;
|
||||
}
|
||||
|
||||
- (int)mode
|
||||
{
|
||||
return 0;
|
||||
if (_currentPicker != nil)
|
||||
return [_currentPicker currentMode];
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
- (void)setAccessoryView:(NSView *)aView
|
||||
{}
|
||||
{
|
||||
[_accessoryBox setContentView: aView];
|
||||
// [_accessoryBox sizeToFit];
|
||||
}
|
||||
|
||||
- (void)setAction:(SEL)aSelector
|
||||
{}
|
||||
{
|
||||
_action = aSelector;
|
||||
}
|
||||
|
||||
- (void)setContinuous:(BOOL)flag
|
||||
{}
|
||||
{
|
||||
_isContinuous = flag;
|
||||
}
|
||||
|
||||
- (void)setMode:(int)mode
|
||||
{}
|
||||
{
|
||||
int i, count;
|
||||
|
||||
if (mode == [self mode])
|
||||
return;
|
||||
|
||||
count = [_pickers count];
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
if ([[_pickers objectAtIndex: i] supportsMode: mode])
|
||||
break;
|
||||
}
|
||||
|
||||
// if i == count, no picker was found
|
||||
if (i != count)
|
||||
{
|
||||
[_pickerMatrix selectCellWithTag: i];
|
||||
[self _showNewPicker: _pickerMatrix];
|
||||
}
|
||||
}
|
||||
|
||||
// This code is very simple-minded. Instead of removing the alpha slider,
|
||||
// why not just cover it up?
|
||||
|
||||
- (void)setShowsAlpha:(BOOL)flag
|
||||
{}
|
||||
{
|
||||
if (flag && ![self showsAlpha])
|
||||
{
|
||||
NSRect newFrame = [_pickerBox frame];
|
||||
float offset = [_alphaSlider frame].size.height + 4;
|
||||
|
||||
newFrame.origin.y += offset;
|
||||
newFrame.size.height -= offset;
|
||||
[_pickerBox setFrame: newFrame];
|
||||
}
|
||||
else
|
||||
{
|
||||
[_pickerBox setFrame: NSUnionRect([_pickerBox frame],
|
||||
[_alphaSlider frame])];
|
||||
}
|
||||
|
||||
[_pickers makeObjectsPerformSelector: @selector(alphaControlAddedOrRemoved:)
|
||||
withObject: self];
|
||||
|
||||
[_topView setNeedsDisplay: YES];
|
||||
}
|
||||
|
||||
- (void)setTarget:(id)anObject
|
||||
{}
|
||||
{
|
||||
ASSIGN(_target, anObject);
|
||||
}
|
||||
|
||||
- (BOOL)showsAlpha
|
||||
{
|
||||
return NO;
|
||||
if (NSIntersectsRect([_pickerBox frame], [_alphaSlider frame]))
|
||||
return NO;
|
||||
else
|
||||
return YES;
|
||||
}
|
||||
|
||||
//
|
||||
// Attaching a Color List
|
||||
//
|
||||
- (void)attachColorList:(NSColorList *)aColorList
|
||||
{}
|
||||
{
|
||||
NSEnumerator *enumerator;
|
||||
id picker;
|
||||
|
||||
if ((_pickers != nil) && ([_pickers count] > 0))
|
||||
{
|
||||
enumerator = [_pickers objectEnumerator];
|
||||
while ((picker = [enumerator nextObject]))
|
||||
[picker attachColorList: aColorList];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)detachColorList:(NSColorList *)aColorList
|
||||
{}
|
||||
{
|
||||
NSEnumerator *enumerator;
|
||||
id picker;
|
||||
|
||||
if ((_pickers != nil) && ([_pickers count] > 0))
|
||||
{
|
||||
enumerator = [_pickers objectEnumerator];
|
||||
while ((picker = [enumerator nextObject]))
|
||||
[picker detachColorList: aColorList];
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Setting Color
|
||||
//
|
||||
- (float)alpha
|
||||
{
|
||||
return 0;
|
||||
if ([self showsAlpha])
|
||||
return [_alphaSlider floatValue];
|
||||
else
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
- (NSColor *)color
|
||||
{
|
||||
return nil;
|
||||
return [_colorWell color];
|
||||
}
|
||||
|
||||
- (void)setColor:(NSColor *)aColor
|
||||
{}
|
||||
{
|
||||
[_colorWell setColor: aColor];
|
||||
[[NSNotificationCenter defaultCenter]
|
||||
postNotificationName: NSColorPanelColorChangedNotification
|
||||
object: (id)self];
|
||||
}
|
||||
|
||||
//
|
||||
// NSCoding protocol
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue