00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import "CPButton.j"
00024 @import "CPCookie.j"
00025 @import "CPPanel.j"
00026 @import "CPView.j"
00027
00028 CPColorPanelColorDidChangeNotification = @"CPColorPanelColorDidChangeNotification";
00029
00030 var PREVIEW_HEIGHT = 20.0,
00031 TOOLBAR_HEIGHT = 32.0,
00032 SWATCH_HEIGHT = 14.0,
00033 ICON_WIDTH = 32.0,
00034 ICON_PADDING = 12.0;
00035
00036 var SharedColorPanel = nil,
00037 ColorPickerClasses = [];
00038
00039
00040
00041
00042
00043
00044 CPWheelColorPickerMode = 1;
00045
00046
00047
00048
00049
00050 CPSliderColorPickerMode = 2;
00051
00052 CPColorPickerViewWidth = 265,
00053 CPColorPickerViewHeight = 370;
00054
00061 @implementation CPColorPanel : CPPanel
00062 {
00063 _CPColorPanelToolbar _toolbar;
00064 _CPColorPanelSwatches _swatchView;
00065 _CPColorPanelPreview _previewView;
00066
00067 CPTextField _previewLabel;
00068 CPTextField _swatchLabel;
00069
00070 CPArray _colorPickers;
00071 CPView _currentView;
00072
00073 CPColor _color;
00074
00075 id _target;
00076 SEL _action;
00077
00078 int _mode;
00079 }
00080
00085 + (void)provideColorPickerClass:(Class)aColorPickerSubclass
00086 {
00087 ColorPickerClasses.push(aColorPickerSubclass);
00088 }
00089
00093 + (CPColorPanel)sharedColorPanel
00094 {
00095 if (!SharedColorPanel)
00096 SharedColorPanel = [[CPColorPanel alloc] init];
00097
00098 return SharedColorPanel;
00099 }
00100
00105 + (void)setPickerMode:(CPColorPanelMode)mode
00106 {
00107 var panel = [CPColorPanel sharedColorPanel];
00108 [panel setMode: mode];
00109 }
00110
00111
00112
00113
00114
00115 - (id)init
00116 {
00117 self = [super initWithContentRect:CGRectMake(500.0, 50.0, 218.0, 360.0)
00118 styleMask:(CPHUDBackgroundWindowMask | CPTitledWindowMask | CPClosableWindowMask | CPResizableWindowMask)];
00119
00120 if (self)
00121 {
00122 [self setTitle:@"Color Panel"];
00123 [self setLevel:CPFloatingWindowLevel];
00124
00125 [self setFloatingPanel:YES];
00126 [self setBecomesKeyOnlyIfNeeded:YES];
00127
00128 [self setMinSize:CGSizeMake(218.0, 360.0)];
00129 [self setMaxSize:CGSizeMake(327.0, 540.0)];
00130 }
00131
00132 return self;
00133 }
00134
00138 - (void)setColor:(CPColor)aColor
00139 {
00140 _color = aColor;
00141 [_previewView setBackgroundColor: _color];
00142
00143 [CPApp sendAction:@selector(changeColor:) to:nil from:self];
00144
00145 if (_target && _action)
00146 [CPApp sendAction:_action to:_target from:self];
00147
00148 [[CPNotificationCenter defaultCenter]
00149 postNotificationName:CPColorPanelColorDidChangeNotification
00150 object:self];
00151 }
00152
00158 - (void)setColor:(CPColor)aColor updatePicker:(BOOL)bool
00159 {
00160 [self setColor:aColor];
00161
00162 if (bool)
00163 [_activePicker setColor:_color];
00164 }
00165
00169 - (CPColor)color
00170 {
00171 return _color;
00172 }
00173
00178 - (void)setTarget:(id)aTarget
00179 {
00180 _target = aTarget;
00181 }
00182
00187 - (id)target
00188 {
00189 return _target;
00190 }
00191
00197 - (void)setAction:(selector)anAction
00198 {
00199 _action = anAction;
00200 }
00201
00205 - (selector)action
00206 {
00207 return _action;
00208 }
00209
00214 - (void)setMode:(CPColorPanelMode)mode
00215 {
00216 _mode = mode;
00217 }
00218
00219 - (void)_setPicker:(id)sender
00220 {
00221 var picker = _colorPickers[[sender tag]],
00222 view = [picker provideNewView:NO];
00223
00224 if (!view)
00225 view = [picker provideNewView:YES];
00226
00227 if (view == _currentView)
00228 return;
00229
00230 if (_currentView)
00231 [view setFrame:[_currentView frame]];
00232 else
00233 {
00234 var height = (TOOLBAR_HEIGHT+10+PREVIEW_HEIGHT+5+SWATCH_HEIGHT+10),
00235 bounds = [[self contentView] bounds];
00236
00237 [view setFrameSize: CPSizeMake(bounds.size.width - 10, bounds.size.height - height)];
00238 [view setFrameOrigin: CPPointMake(5, height)];
00239 }
00240
00241 [_currentView removeFromSuperview];
00242 [[self contentView] addSubview:view];
00243
00244 _currentView = view;
00245 _activePicker = picker;
00246
00247 [picker setColor:[self color]];
00248 }
00249
00253 - (CPColorPanelMode)mode
00254 {
00255 return _mode;
00256 }
00257
00258 - (void)orderFront:(id)aSender
00259 {
00260 [self _loadContentsIfNecessary];
00261 [super orderFront:aSender];
00262 }
00263
00264
00265 - (void)_loadContentsIfNecessary
00266 {
00267 if (_toolbar)
00268 return;
00269
00270 _colorPickers = [];
00271
00272 var count = [ColorPickerClasses count];
00273 for (var i=0; i<count; i++)
00274 {
00275 var currentPickerClass = ColorPickerClasses[i],
00276 currentPicker = [[currentPickerClass alloc] initWithPickerMask:0 colorPanel:self];
00277
00278 _colorPickers.push(currentPicker);
00279 }
00280
00281 var contentView = [self contentView],
00282 bounds = [contentView bounds];
00283
00284 _toolbar = [[CPView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(bounds), TOOLBAR_HEIGHT)];
00285 [_toolbar setAutoresizingMask: CPViewWidthSizable];
00286
00287 var totalToolbarWidth = count * ICON_WIDTH + (count - 1) * ICON_PADDING,
00288 leftOffset = (CGRectGetWidth(bounds) - totalToolbarWidth) / 2.0,
00289 buttonForLater = nil;
00290
00291 for (var i=0; i<count; i++)
00292 {
00293 var image = [_colorPickers[i] provideNewButtonImage],
00294 highlightImage = [_colorPickers[i] provideNewAlternateButtonImage],
00295 button = [[CPButton alloc] initWithFrame:CGRectMake(leftOffset + i*(ICON_WIDTH+ICON_PADDING), 0, ICON_WIDTH, ICON_WIDTH)];
00296
00297 [button setTag:i];
00298 [button setTarget:self];
00299 [button setAction:@selector(_setPicker:)];
00300 [button setBordered:NO];
00301 [button setAutoresizingMask:CPViewMinXMargin|CPViewMaxXMargin];
00302
00303 [button setImage:image];
00304 [button setAlternateImage:highlightImage];
00305
00306 [_toolbar addSubview:button];
00307
00308 if (!buttonForLater)
00309 buttonForLater = button;
00310 }
00311
00312
00313 var previewBox = [[CPView alloc] initWithFrame:CGRectMake(76, TOOLBAR_HEIGHT + 10, CGRectGetWidth(bounds) - 86, PREVIEW_HEIGHT)];
00314
00315 _previewView = [[_CPColorPanelPreview alloc] initWithFrame:CGRectInset([previewBox bounds], 2.0, 2.0)];
00316
00317 [_previewView setColorPanel:self];
00318 [_previewView setAutoresizingMask:CPViewWidthSizable];
00319
00320 [previewBox setBackgroundColor:[CPColor grayColor]];
00321 [previewBox setAutoresizingMask:CPViewWidthSizable];
00322
00323 [previewBox addSubview:_previewView];
00324
00325 _previewLabel = [[CPTextField alloc] initWithFrame: CPRectMake(10, TOOLBAR_HEIGHT + 14, 60, 15)];
00326 [_previewLabel setStringValue: "Preview:"];
00327 [_previewLabel setTextColor:[CPColor whiteColor]];
00328 [_previewLabel setAlignment:CPRightTextAlignment];
00329
00330
00331 var swatchBox = [[CPView alloc] initWithFrame:CGRectMake(76, TOOLBAR_HEIGHT + 10 + PREVIEW_HEIGHT + 5, CGRectGetWidth(bounds) - 86, SWATCH_HEIGHT + 2.0)];
00332
00333 [swatchBox setBackgroundColor:[CPColor grayColor]];
00334 [swatchBox setAutoresizingMask:CPViewWidthSizable];
00335
00336 _swatchView = [[_CPColorPanelSwatches alloc] initWithFrame:CGRectInset([swatchBox bounds], 1.0, 1.0)];
00337
00338 [_swatchView setColorPanel: self];
00339 [_swatchView setAutoresizingMask: CPViewWidthSizable];
00340
00341 [swatchBox addSubview:_swatchView];
00342
00343 _swatchLabel = [[CPTextField alloc] initWithFrame: CPRectMake(10, TOOLBAR_HEIGHT + 8 + PREVIEW_HEIGHT + 5, 60, 15)];
00344 [_swatchLabel setStringValue: "Swatches:"];
00345 [_swatchLabel setTextColor:[CPColor whiteColor]];
00346 [_swatchLabel setAlignment:CPRightTextAlignment];
00347
00348
00349 [contentView addSubview: _toolbar];
00350 [contentView addSubview: previewBox];
00351 [contentView addSubview: _previewLabel];
00352 [contentView addSubview: swatchBox];
00353 [contentView addSubview: _swatchLabel];
00354
00355 _target = nil;
00356 _action = nil;
00357 _activePicker = nil;
00358
00359 [self setColor:[CPColor whiteColor]];
00360
00361 if (buttonForLater)
00362 [self _setPicker:buttonForLater];
00363 }
00364
00365 @end
00366
00367
00368 CPColorDragType = "CPColorDragType";
00369 var CPColorPanelSwatchesCookie = "CPColorPanelSwatchesCookie";
00370
00371
00372 @implementation _CPColorPanelSwatches : CPView
00373 {
00374 CPView[] _swatches;
00375 CPColor _dragColor;
00376 CPColorPanel _colorPanel;
00377 CPCookie _swatchCookie;
00378 }
00379
00380 -(id)initWithFrame:(CPRect)aFrame
00381 {
00382 self = [super initWithFrame:aFrame];
00383
00384 [self setBackgroundColor: [CPColor grayColor]];
00385
00386 [self registerForDraggedTypes:[CPArray arrayWithObjects:CPColorDragType]];
00387
00388 var whiteColor = [CPColor whiteColor];
00389
00390 _swatchCookie = [[CPCookie alloc] initWithName: CPColorPanelSwatchesCookie];
00391 var colorList = [self startingColorList];
00392
00393 _swatches = [];
00394
00395 for(var i=0; i < 50; i++)
00396 {
00397
00398 var view = [[CPView alloc] initWithFrame: CPRectMake(13*i+1, 1, 12, 12)],
00399 fillView = [[CPView alloc] initWithFrame:CGRectInset([view bounds], 1.0, 1.0)];
00400
00401 [view setBackgroundColor:whiteColor];
00402 [fillView setBackgroundColor: (i < colorList.length) ? colorList[i] : whiteColor];
00403
00404 [view addSubview:fillView];
00405
00406 [self addSubview: view];
00407
00408 _swatches.push(view);
00409 }
00410
00411 return self;
00412 }
00413
00414 - (BOOL)isOpaque
00415 {
00416 return YES;
00417 }
00418
00419 - (CPArray)startingColorList
00420 {
00421 var cookieValue = [_swatchCookie value];
00422 if(cookieValue == "")
00423 {
00424 return [
00425 [CPColor blackColor],
00426 [CPColor darkGrayColor],
00427 [CPColor grayColor],
00428 [CPColor lightGrayColor],
00429 [CPColor whiteColor],
00430 [CPColor redColor],
00431 [CPColor greenColor],
00432 [CPColor blueColor],
00433 [CPColor yellowColor]
00434 ];
00435 }
00436
00437 var cookieValue = eval(cookieValue);
00438 var result = [];
00439
00440 for(var i=0; i<cookieValue.length; i++)
00441 result.push([CPColor colorWithHexString: cookieValue[i]]);
00442
00443 return result;
00444 }
00445
00446 - (CPArray)saveColorList
00447 {
00448 var result = [];
00449
00450 for(var i=0; i<_swatches.length; i++)
00451 result.push([[[_swatches[i] subviews][0] backgroundColor] hexString]);
00452
00453 var future = new Date();
00454 future.setYear(2019);
00455
00456 [_swatchCookie setValue: CPJSObjectCreateJSON(result) expires:future domain: nil];
00457 }
00458
00459 - (void)setColorPanel:(CPColorPanel)panel
00460 {
00461 _colorPanel = panel;
00462 }
00463
00464 - (CPColorPanel)colorPanel
00465 {
00466 return _colorPanel;
00467 }
00468
00469 - (CPColor)colorAtIndex:(int)index
00470 {
00471 return [[_swatches[index] subviews][0] backgroundColor];
00472 }
00473
00474 - (void)setColor:(CPColor)aColor atIndex:(int)index
00475 {
00476
00477 [[_swatches[index] subviews][0] setBackgroundColor:aColor];
00478 [self saveColorList];
00479 }
00480
00481 - (void)mouseUp:(CPEvent)anEvent
00482 {
00483 var point = [self convertPoint:[anEvent locationInWindow] fromView:nil];
00484
00485 if(point.x > [self bounds].size.width - 1 || point.x < 1)
00486 return NO;
00487
00488 [_colorPanel setColor: [self colorAtIndex:FLOOR(point.x / 13)] updatePicker: YES];
00489 }
00490
00491 - (void)mouseDragged:(CPEvent)anEvent
00492 {
00493 var point = [self convertPoint:[anEvent locationInWindow] fromView:nil];
00494
00495 if(point.x > [self bounds].size.width - 1 || point.x < 1)
00496 return NO;
00497
00498 [[CPPasteboard pasteboardWithName:CPDragPboard] declareTypes:[CPArray arrayWithObject:CPColorDragType] owner:self];
00499
00500 var swatch = _swatches[FLOOR(point.x / 13)];
00501
00502
00503 _dragColor = [[swatch subviews][0] backgroundColor];
00504
00505 var bounds = CPRectCreateCopy([swatch bounds]);
00506
00507
00508 var dragView = [[CPView alloc] initWithFrame: bounds];
00509 dragFillView = [[CPView alloc] initWithFrame:CGRectInset(bounds, 1.0, 1.0)];
00510
00511 [dragView setBackgroundColor:[CPColor blackColor]];
00512 [dragFillView setBackgroundColor:_dragColor];
00513
00514 [dragView addSubview:dragFillView];
00515
00516 [self dragView: dragView
00517 at: CPPointMake(point.x - bounds.size.width / 2.0, point.y - bounds.size.height / 2.0)
00518 offset: CPPointMake(0.0, 0.0)
00519 event: anEvent
00520 pasteboard: nil
00521 source: self
00522 slideBack: YES];
00523 }
00524
00525 - (void)pasteboard:(CPPasteboard)aPasteboard provideDataForType:(CPString)aType
00526 {
00527 if(aType == CPColorDragType)
00528 [aPasteboard setData:_dragColor forType:aType];
00529 }
00530
00531 - (void)performDragOperation:(id <CPDraggingInfo>)aSender
00532 {
00533 var location = [self convertPoint:[aSender draggingLocation] fromView:nil],
00534 pasteboard = [aSender draggingPasteboard],
00535 swatch = nil;
00536
00537 if(![pasteboard availableTypeFromArray:[CPColorDragType]] || location.x > [self bounds].size.width - 1 || location.x < 1)
00538 return NO;
00539
00540 [self setColor:[pasteboard dataForType:CPColorDragType] atIndex: FLOOR(location.x / 13)];
00541 }
00542
00543 @end
00544
00545
00546 @implementation _CPColorPanelPreview : CPView
00547 {
00548 CPColorPanel _colorPanel;
00549 }
00550
00551 - (id)initWithFrame:(CPRect)aFrame
00552 {
00553 self = [super initWithFrame:aFrame];
00554
00555 [self registerForDraggedTypes:[CPArray arrayWithObjects:CPColorDragType]];
00556
00557 return self;
00558 }
00559
00560 - (void)setColorPanel:(CPColorPanel)aPanel
00561 {
00562 _colorPanel = aPanel;
00563 }
00564
00565 - (CPColorPanel)colorPanel
00566 {
00567 return _colorPanel;
00568 }
00569
00570 - (void)performDragOperation:(id <CPDraggingInfo>)aSender
00571 {
00572 var pasteboard = [aSender draggingPasteboard];
00573
00574 if(![pasteboard availableTypeFromArray:[CPColorDragType]])
00575 return NO;
00576
00577 var color = [pasteboard dataForType:CPColorDragType];
00578 [_colorPanel setColor: color updatePicker: YES];
00579 }
00580
00581 - (BOOL)isOpaque
00582 {
00583 return YES;
00584 }
00585
00586 - (void)mouseDragged:(CPEvent)anEvent
00587 {
00588 var point = [self convertPoint:[anEvent locationInWindow] fromView:nil];
00589
00590 [[CPPasteboard pasteboardWithName:CPDragPboard] declareTypes:[CPArray arrayWithObject:CPColorDragType] owner:self];
00591
00592 var bounds = CPRectMake(0, 0, 15, 15);
00593
00594
00595 var dragView = [[CPView alloc] initWithFrame: bounds];
00596 dragFillView = [[CPView alloc] initWithFrame:CGRectInset(bounds, 1.0, 1.0)];
00597
00598 [dragView setBackgroundColor:[CPColor blackColor]];
00599 [dragFillView setBackgroundColor:[self backgroundColor]];
00600
00601 [dragView addSubview:dragFillView];
00602
00603 [self dragView: dragView
00604 at: CPPointMake(point.x - bounds.size.width / 2.0, point.y - bounds.size.height / 2.0)
00605 offset: CPPointMake(0.0, 0.0)
00606 event: anEvent
00607 pasteboard: nil
00608 source: self
00609 slideBack: YES];
00610 }
00611
00612 - (void)pasteboard:(CPPasteboard)aPasteboard provideDataForType:(CPString)aType
00613 {
00614 if(aType == CPColorDragType)
00615 [aPasteboard setData:[self backgroundColor] forType:aType];
00616 }
00617
00618 @end
00619
00620 @import "CPColorPicker.j"
00621 @import "CPSliderColorPicker.j"
00622
00623 [CPColorPanel provideColorPickerClass:CPColorWheelColorPicker];
00624 [CPColorPanel provideColorPickerClass:CPSliderColorPicker];