/**
TODO Description
*/ @implementation NSColorPanel /* * Class methods */ + (void) initialize { if (self == [NSColorPanel class]) { // Initial version [self setVersion: 1]; _gs_gui_color_panel_lock = [NSLock new]; } } /**Creates ( if needed ) and returns the shared NSColorPanel.
*/ + (NSColorPanel *)sharedColorPanel { if (_gs_gui_color_panel == nil) { [_gs_gui_color_panel_lock lock]; if (!_gs_gui_color_panel) { // Keep this two lines separated so the check in [init] works. _gs_gui_color_panel = [self alloc]; [_gs_gui_color_panel init]; } [_gs_gui_color_panel_lock unlock]; } return _gs_gui_color_panel; } /**Returns whether the NSColorPanel has been already created.
*/ + (BOOL) sharedColorPanelExists { return (_gs_gui_color_panel == nil) ? NO : YES; } + (void) setPickerMask: (int)mask { _gs_gui_color_picker_mask = mask; } /** */ + (void) setPickerMode: (int)mode { _gs_gui_color_picker_mode = mode; } /**Drags aColor frome sourceView at the location give by the event anEvent ( [NSView-convertPoint:fromView:] ). The type declare into the pasteboard is NSColorPboardType
See Also: [NSView-convertPoint:fromView:] [NSView-dragImage:at:offset:event:pasteboard:source:slideBack:
*/ + (BOOL) dragColor: (NSColor *)aColor withEvent: (NSEvent *)anEvent fromView: (NSView *)sourceView { NSPasteboard *pb = [NSPasteboard pasteboardWithName: NSDragPboard]; NSImage *image = [NSImage imageNamed: @"common_ColorSwatch"]; NSSize size; NSPoint point; [pb declareTypes: [NSArray arrayWithObjects: NSColorPboardType, nil] owner: aColor]; [aColor writeToPasteboard: pb]; [image setBackgroundColor: aColor]; size = [image size]; point = [sourceView convertPoint: [anEvent locationInWindow] fromView: nil]; point.x -= size.width/2; point.y -= size.width/2; [sourceView dragImage: image at: point offset: NSMakeSize(0,0) event: anEvent pasteboard: pb source: sourceView slideBack: NO]; return YES; } /* * Instance methods */ - (id) init { if (self != _gs_gui_color_panel) { RELEASE(self); return _gs_gui_color_panel; } // if (![NSBundle loadNibNamed: @"ColorPanel" owner: self]); [self _initWithoutGModel]; [self _loadPickers]; [self _setupPickers]; [self setMode: _gs_gui_color_picker_mode]; [self setShowsAlpha: ![NSColor ignoresAlpha]]; return self; } - (void) dealloc { // As there is only one this will never be called RELEASE(_topView); RELEASE(_colorWell); RELEASE(_magnifyButton); RELEASE(_pickerMatrix); RELEASE(_pickerBox); RELEASE(_alphaSlider); RELEASE(_splitView); RELEASE(_pickers); [super dealloc]; } /**Returns the NSColorPanel's accessory view if it exists, nil otherwise.
See Also: -setAccessoryView:
*/ - (NSView *) accessoryView { return _accessoryView; } /**Returns whether the NSColorPanel continuously sends its action message.
See Also: -setContinuous:-setAction: -setTarget:
*/ - (BOOL) isContinuous { return _isContinuous; } /**Returns the current mode of the NSColorPanel.
See Also: -setMode:
*/ - (int) mode { if (_currentPicker != nil) return [_currentPicker currentMode]; else return 0; } /**Sets the accessoryView to a view. The old view ( if exists ) will be remove ( and release ). You need to retain it if you want to use it later
See Also: -accessoryView
*/ - (void) setAccessoryView: (NSView *)aView { if (_accessoryView == aView) return; if (_accessoryView != nil) [_accessoryView removeFromSuperview]; _accessoryView = aView; [_splitView addSubview: _accessoryView]; } /**Sets the NSColorPanl action method to aSelector The action message is usally send in -setColor:, when the picker is updated, when a new picker is show, when the alpha is changed or when one of the color well at the bottom is selected.
*/ - (void) setAction: (SEL)aSelector { _action = aSelector; } /**Sets whether the NSColorPanel sends continuously action messages
See Also: -isContinuous
*/ - (void) setContinuous: (BOOL)flag { _isContinuous = flag; } /**Set the NSColorPanel mode to mode.
See Also: -mode
*/ - (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]) { [_pickerMatrix selectCellWithTag: i]; [self _showNewPicker: _pickerMatrix]; [_currentPicker setMode: mode]; break; } } } /**Sets whether the NSColorPanel shows alpha values and the alpha slider.
See Also: -showsAlpha
*/ - (void) setShowsAlpha: (BOOL)flag { if (flag == _showsAlpha) return; if (flag) { NSRect newFrame = [_pickerBox frame]; float offset = [_alphaSlider frame].size.height + 4; [_alphaSlider setFrameOrigin: newFrame.origin]; [[_pickerBox superview] addSubview: _alphaSlider]; newFrame.origin.y += offset; newFrame.size.height -= offset; [_pickerBox setFrame: newFrame]; } else { // Remove the alpha slider, and add its size to the pickeBox [_alphaSlider removeFromSuperview]; [_pickerBox setFrame: NSUnionRect([_pickerBox frame], [_alphaSlider frame])]; } _showsAlpha = flag; [_pickers makeObjectsPerformSelector: @selector(alphaControlAddedOrRemoved:) withObject: self]; [_topView setNeedsDisplay: YES]; } /**Sets the target object to anObject
*/ - (void) setTarget: (id)anObject { _target = anObject; } /**Returns whether the NSColorPanel shows alpha values and the alpha slider
See Also: -setShowsAlpha:
*/ - (BOOL) showsAlpha { return _showsAlpha; } // // Attaching a Color List // - (void) attachColorList: (NSColorList *)aColorList { [_pickers makeObjectsPerformSelector: @selector(attachColorList:) withObject: aColorList]; } - (void) detachColorList: (NSColorList *)aColorList { [_pickers makeObjectsPerformSelector: @selector(detachColorList:) withObject: aColorList]; } /**Returns the alpha value of the NSColorPanel. Returns 1.0 if the NSColorPanel does not show alpha
See Also: -showsAlpha -setShowsAlpha:
*/ - (float) alpha { if ([self showsAlpha]) return [_alphaSlider floatValue] / MAX_ALPHA_VALUE; else return 1.0; } /**Returns the current NSColor displayed by the NSColorPanel.
See Also : -setColor:
*/ - (NSColor *) color { return [_colorWell color]; } /**Sets the NSColor displayed to aColor. This method post a NSColorPanelColorDidChangeNotification notification if needed.
See Also: -color [NSColorWell-setColor:]
*/ - (void) setColor: (NSColor *)aColor { [_colorWell setColor: aColor]; [_currentPicker setColor: aColor]; if ([self showsAlpha]) [_alphaSlider setFloatValue: [aColor alphaComponent] * MAX_ALPHA_VALUE]; if (_isContinuous && (_action) && (_target != nil)) [NSApp sendAction: _action to: _target from: self]; [[NSNotificationCenter defaultCenter] postNotificationName: NSColorPanelColorDidChangeNotification object: (id)self]; } - (BOOL) worksWhenModal { return YES; } // // NSCoding protocol // - (void) encodeWithCoder: (NSCoder*)aCoder { [super encodeWithCoder: aCoder]; } - (id) initWithCoder: (NSCoder*)aDecoder { self = [super initWithCoder: aDecoder]; if (nil == self) return nil; return self; } @end