1 /* Copyright (c) 2006-2007 Christopher J. W. Lloyd <cjwl@objc.net>
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
9 #import <AppKit/AppKit.h>
10 #import <AppKit/NSColorPickerColorList.h>
11 #import <AppKit/NSColorPickerSliders.h>
12 #import <AppKit/NSColorPickerWheel.h>
13 #import <AppKit/NSRaise.h>
15 NSString * const NSColorPanelColorDidChangeNotification=@"NSColorPanelColorDidChangeNotification";
17 @interface NSColorPicker(PrivateInterface)
19 - (void)setColor:(NSColor*)color;
23 @implementation NSColorPanel
25 static NSColorPanel *_colorPanel=nil;
26 static NSUInteger _pickerMask=0;
28 +(void)setColorPanel:(NSColorPanel *)colorPanel {
29 [_colorPanel release];
30 _colorPanel=[colorPanel retain];
33 +(BOOL)sharedColorPanelExists {
34 return (_colorPanel!=nil)?YES:NO;
37 +(NSColorPanel *)sharedColorPanel {
39 if(![NSBundle loadNibNamed:@"NSColorPanel" owner:self])
40 NSLog(@"Cannot load NSColorPanel.nib");
42 if(![_colorPanel setFrameUsingName:@"NSColorPanel"])
45 [_colorPanel setFrameAutosaveName:@"NSColorPanel"];
51 +(void)setPickerMask:(NSUInteger)mask {
52 if (_colorPanel == nil) // only works if color panel is not yet created
56 + (void)setPickerMode:(NSColorPanelMode)mode {
57 NSUnimplementedMethod();
60 +(BOOL)dragColor:(NSColor *)color withEvent:(NSEvent *)event fromView:(NSView *)view {
61 NSPasteboard *pboard=[NSPasteboard pasteboardWithName:NSDragPboard];
62 NSSize size=NSMakeSize(12,12);
63 NSImage *image=[[[NSImage alloc] initWithSize:size] autorelease];
65 [image setCachedSeparately:YES];
67 [color drawSwatchInRect:NSMakeRect(0,0,12,12)];
70 [pboard declareTypes:[NSArray arrayWithObject:NSColorPboardType] owner:nil];
71 [color writeToPasteboard:pboard];
73 [view dragImage:image at:NSMakePoint(0,0) offset:NSMakeSize(0,0) event:event pasteboard:pboard source:view slideBack:YES];
77 -(void)swapInNewView:sender {
78 NSColorPicker<NSColorPickingCustom> *chosenColorPicker=[_colorPickers objectAtIndex:[sender selectedTag]];
81 newView=[chosenColorPicker provideNewView:YES];
83 if (currentColorPickerView != newView) {
84 [[sender selectedCell] setImage:[chosenColorPicker provideNewButtonImage]];
86 if (currentColorPickerView != nil)
87 [newView setFrame:[currentColorPickerView frame]];
89 [newView setFrame:[colorPickerView bounds]];
91 [currentColorPickerView retain];
92 [currentColorPickerView removeFromSuperview];
94 [colorPickerView addSubview:newView];
95 // [splitView adjustSubviews];
96 currentColorPickerView=[newView retain];
100 -(void)awakeFromNib {
101 // time to load the color pickers. theoretically we should be searching all the /Library/ColorPickers out there, but...
102 NSArray *colorPickersClassArray=[NSArray arrayWithObjects:
103 [NSColorPickerWheel class],
104 [NSColorPickerSliders class],
106 // Disabled to see if it fixes a nib instantiation issue
107 [NSColorPickerColorList class],
110 unsigned i,count=[colorPickersClassArray count];
112 [colorWell setBordered:NO];
114 _colorPickers=[[NSMutableArray alloc] init];
115 [colorPickersMatrix renewRows:1 columns:count];
117 for(i=0;i<count;i++){
118 Class colorPickerClass=[colorPickersClassArray objectAtIndex:i];
119 NSColorPicker *newPicker=[[[colorPickerClass alloc] initWithPickerMask:_pickerMask colorPanel:self] autorelease];
120 NSCell *cell=[colorPickersMatrix cellAtRow:0 column:i];
122 [_colorPickers addObject:newPicker];
124 [cell setImage:[newPicker provideNewButtonImage]];
126 [cell setTarget:self];
127 [cell setAction:@selector(swapInNewView:)];
130 [colorPickersMatrix selectCellAtRow:0 column:0];
131 [self swapInNewView:colorPickersMatrix];
133 [opacityTitle setHidden:YES];
134 [opacitySlider setHidden:YES];
135 [opacityTextField setHidden:YES];
136 [opacityPercentLabel setHidden: YES];
137 [opacitySlider setTarget:self];
138 [opacitySlider setAction:@selector(_alphaChanged:)];
139 [opacityTextField setTarget:self];
140 [opacityTextField setAction:@selector(_alphaChanged:)];
144 return [colorWell color];
148 return _showsAlpha?MIN(MAX(0.0,[opacitySlider floatValue]/100.0),1.0):1.0f;
151 -(NSColorPanelMode)mode {
159 - (BOOL)isContinuous {
163 -(NSView *)accessoryView {
164 NSUnimplementedMethod();
168 -(void)setColorButtonClicked:sender {
169 [NSApp sendAction:_action to:_target from:self];
172 - (NSColorPicker*)_selectedColorPicker
174 int index = [colorPickersMatrix selectedTag];
175 NSColorPicker *picker = [_colorPickers objectAtIndex: index];
179 -(void)setColor:(NSColor *)color {
180 [colorWell setColor:color];
181 [self setColorButtonClicked:nil];
183 NSColorPicker *picker = [self _selectedColorPicker];
184 if ([picker respondsToSelector: @selector(setColor:)]) {
185 [picker setColor: color];
187 float alpha = [color alphaComponent];
188 [opacitySlider setFloatValue: alpha * 100.f];
189 [opacityTextField setFloatValue: alpha * 100.f];
191 [[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification notificationWithName:NSColorPanelColorDidChangeNotification object:self] postingStyle:NSPostNow coalesceMask:NSNotificationCoalescingOnName forModes:nil];
194 -(void)setMode:(NSColorPanelMode)mode {
198 -(void)setShowsAlpha:(BOOL)flag {
200 [opacityTitle setHidden:_showsAlpha?NO:YES];
201 [opacitySlider setHidden:_showsAlpha?NO:YES];
202 [opacityTextField setHidden:_showsAlpha?NO:YES];
203 [opacityPercentLabel setHidden:_showsAlpha?NO:YES];
206 // Update the controls!
207 NSColor* color = [self color];
208 float alpha = [color alphaComponent];
209 [opacitySlider setFloatValue: alpha * 100.f];
210 [opacityTextField setFloatValue: alpha * 100.f];
214 -(void)setContinuous:(BOOL)flag {
218 -(void)setAccessoryView:(NSView *)view {
219 NSUnimplementedMethod();
222 -(void)setAction:(SEL)action {
226 -(void)setTarget:target {
230 -(void)attachColorList:(NSColorList *)colorList {
231 NSUnimplementedMethod();
234 -(void)detachColorList:(NSColorList *)colorList {
235 NSUnimplementedMethod();
238 -(void)_alphaChanged:sender {
239 CGFloat alpha=MIN(MAX(0.0,[sender floatValue]/100.0),1.0);
240 NSColor *color=[[self color] colorWithAlphaComponent:alpha];
242 if (sender == opacitySlider) {
243 [opacityTextField setFloatValue: [sender floatValue]];
245 [opacitySlider setFloatValue: [sender floatValue]];
247 [self setColor:color];