Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 668 lines (515 sloc) 18.608 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
/*
* CPColorPanel.j
* AppKit
*
* Created by Ross Boucher.
* Copyright 2008, 280 North, Inc.
*
* 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
* 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-1301 USA
*/

@import "CPApplication.j"
@import "CPButton.j"
@import "CPCookie.j"
@import "CPPanel.j"
@import "CPView.j"


CPColorPanelColorDidChangeNotification = @"CPColorPanelColorDidChangeNotification";

var PREVIEW_HEIGHT = 20.0,
    TOOLBAR_HEIGHT = 32.0,
    SWATCH_HEIGHT = 14.0,
    ICON_WIDTH = 32.0,
    ICON_PADDING = 12.0;

var SharedColorPanel = nil,
    ColorPickerClasses = [];

/*
A color wheel
@global
@group CPColorPanelMode
*/
CPWheelColorPickerMode = 1;
/*
Slider based picker
@global
@group CPColorPanelMode
*/
CPSliderColorPickerMode = 2;

CPColorPickerViewWidth = 265;
CPColorPickerViewHeight = 370;

/*!
@ingroup appkit
@class CPColorPanel

CPColorPanel provides a reusable panel that can be used
displayed on screen to prompt the user for a color selection. To
obtain the panel, call the \c +sharedColorPanel method.
*/
@implementation CPColorPanel : CPPanel
{
    _CPColorPanelToolbar _toolbar;
    _CPColorPanelSwatches _swatchView;
    _CPColorPanelPreview _previewView;

    CPSlider _opacitySlider;

    CPArray _colorPickers;
    CPView _currentView;
    id _activePicker;

    CPColor _color;

    id _target;
    SEL _action;

    int _mode;
}

/*!
A list of color pickers is collected here, and any color panel created will contain
any picker in this list up to this point. In other words, call before creating a color panel.
*/
+ (void)provideColorPickerClass:(Class)aColorPickerSubclass
{
    ColorPickerClasses.push(aColorPickerSubclass);
}

/*!
Returns (and if necessary, creates) the shared color panel.
*/
+ (CPColorPanel)sharedColorPanel
{
    if (!SharedColorPanel)
        SharedColorPanel = [[CPColorPanel alloc] init];

    return SharedColorPanel;
}

/*!
Sets the mode for the shared color panel.
@param mode the mode to which the color panel will be set
*/
+ (void)setPickerMode:(CPColorPanelMode)mode
{
    var panel = [CPColorPanel sharedColorPanel];
    [panel setMode:mode];
}

/*
To obtain the color panel, use \c +sharedColorPanel.
@ignore
*/
- (id)init
{
    self = [super initWithContentRect:CGRectMake(500.0, 50.0, 219.0, 370.0)
                            styleMask:(CPTitledWindowMask | CPClosableWindowMask | CPResizableWindowMask)];

    if (self)
    {
        [[self contentView] setBackgroundColor:[CPColor colorWithWhite:0.95 alpha:1.0]];

        [self setTitle:@"Color Panel"];
        [self setLevel:CPFloatingWindowLevel];

        [self setFloatingPanel:YES];
        [self setBecomesKeyOnlyIfNeeded:YES];

        [self setMinSize:CGSizeMake(219.0, 363.0)];
        [self setMaxSize:CGSizeMake(323.0, 537.0)];
    }

    return self;
}

/*!
Sets the color of the panel, and updates the picker. Also posts a \c CPColorPanelDidChangeNotification.
*/
- (void)setColor:(CPColor)aColor
{
    _color = aColor;
    [_previewView setBackgroundColor:_color];

    [CPApp sendAction:@selector(changeColor:) to:nil from:self];

    if (_target && _action)
        [CPApp sendAction:_action to:_target from:self];

    [[CPNotificationCenter defaultCenter]
        postNotificationName:CPColorPanelColorDidChangeNotification
                      object:self];

    [_activePicker setColor:_color];
    [_opacitySlider setFloatValue:[_color alphaComponent]];
}

/*!
Sets the selected color of the panel and optionally updates the picker.
@param bool whether or not to update the picker
@ignore
*/
- (void)setColor:(CPColor)aColor updatePicker:(BOOL)bool
{
    [self setColor:aColor];

    if (bool)
        [_activePicker setColor:_color];
}

/*!
Returns the panel's currently selected color.
*/
- (CPColor)color
{
    return _color;
}

- (float)opacity
{
    return [_opacitySlider floatValue];
}

/*!
Sets the target for the color panel. Messages are sent
to the target when colors are selected in the panel.
*/
- (void)setTarget:(id)aTarget
{
    _target = aTarget;
}

/*!
Returns the current target. The target receives messages
when colors are selected in the panel.
*/
- (id)target
{
    return _target;
}

/*!
Sets the action that gets sent to the target.
This action is sent whenever a color is selected in the panel.
@param anAction the action that will be sent
*/
- (void)setAction:(selector)anAction
{
    _action = anAction;
}

/*!
Returns the current target action.
*/
- (selector)action
{
    return _action;
}

/*!
Sets the mode (look) of the color panel.
@param mode the mode in which to display the color panel
*/
- (void)setMode:(CPColorPanelMode)mode
{
    _mode = mode;
}

- (void)_setPicker:(id)sender
{
    var picker = _colorPickers[[sender tag]],
        view = [picker provideNewView:NO];

    if (!view)
        view = [picker provideNewView:YES];

    if (view == _currentView)
        return;

    if (_currentView)
        [view setFrame:[_currentView frame]];
    else
    {
        var height = (TOOLBAR_HEIGHT + 10 + PREVIEW_HEIGHT + 5 + SWATCH_HEIGHT + 32),
            bounds = [[self contentView] bounds];

        [view setFrameSize:CPSizeMake(bounds.size.width - 10, bounds.size.height - height)];
        [view setFrameOrigin:CPPointMake(5, height)];
    }

    [_currentView removeFromSuperview];
    [[self contentView] addSubview:view];

    _currentView = view;
    _activePicker = picker;

    [picker setColor:[self color]];
}

/*!
Returns the color panel's current display mode.
*/
- (CPColorPanelMode)mode
{
    return _mode;
}

- (void)orderFront:(id)aSender
{
    [self _loadContentsIfNecessary];
    [super orderFront:aSender];
}

/* @ignore */
- (void)_loadContentsIfNecessary
{
    if (_toolbar)
        return;

    if (!_color)
        _color = [CPColor whiteColor];

    _colorPickers = [];

    var count = [ColorPickerClasses count];
    for (var i = 0; i < count; i++)
    {
        var currentPickerClass = ColorPickerClasses[i],
            currentPicker = [[currentPickerClass alloc] initWithPickerMask:0 colorPanel:self];

        _colorPickers.push(currentPicker);
    }

    var contentView = [self contentView],
        bounds = [contentView bounds];

    _toolbar = [[CPView alloc] initWithFrame:CGRectMake(0, 6, CGRectGetWidth(bounds), TOOLBAR_HEIGHT)];
    [_toolbar setAutoresizingMask:CPViewWidthSizable];

    var totalToolbarWidth = count * ICON_WIDTH + (count - 1) * ICON_PADDING,
        leftOffset = (CGRectGetWidth(bounds) - totalToolbarWidth) / 2.0,
        buttonForLater = nil;

    for (var i = 0; i < count; i++)
    {
        var image = [_colorPickers[i] provideNewButtonImage],
            highlightImage = [_colorPickers[i] provideNewAlternateButtonImage],
            button = [[CPButton alloc] initWithFrame:CGRectMake(leftOffset + i * (ICON_WIDTH + ICON_PADDING), 0, ICON_WIDTH, ICON_WIDTH)];

        [button setTag:i];
        [button setTarget:self];
        [button setAction:@selector(_setPicker:)];
        [button setBordered:NO];
        [button setAutoresizingMask:CPViewMinXMargin | CPViewMaxXMargin];

        [button setImage:image];
        [button setAlternateImage:highlightImage];

        [_toolbar addSubview:button];

        if (!buttonForLater)
            buttonForLater = button;
    }

    // FIXME: http://280north.lighthouseapp.com/projects/13294-cappuccino/tickets/25-implement-cpbox
    var previewBox = [[CPView alloc] initWithFrame:CGRectMake(76, TOOLBAR_HEIGHT + 10, CGRectGetWidth(bounds) - 86, PREVIEW_HEIGHT)];

    _previewView = [[_CPColorPanelPreview alloc] initWithFrame:CGRectInset([previewBox bounds], 2.0, 2.0)];

    [_previewView setColorPanel:self];
    [_previewView setAutoresizingMask:CPViewWidthSizable];

    [previewBox setBackgroundColor:[CPColor colorWithWhite:0.8 alpha:1.0]];
    [previewBox setAutoresizingMask:CPViewWidthSizable];

    [previewBox addSubview:_previewView];

    var _previewLabel = [[CPTextField alloc] initWithFrame:CGRectMake(10, TOOLBAR_HEIGHT + 10, 60, 15)];
    [_previewLabel setStringValue:"Preview:"];
    [_previewLabel setTextColor:[CPColor blackColor]];
    [_previewLabel setAlignment:CPRightTextAlignment];

    // FIXME: http://280north.lighthouseapp.com/projects/13294-cappuccino/tickets/25-implement-cpbox
    var swatchBox = [[CPView alloc] initWithFrame:CGRectMake(76, TOOLBAR_HEIGHT + 10 + PREVIEW_HEIGHT + 5, CGRectGetWidth(bounds) - 86, SWATCH_HEIGHT + 2.0)];

    [swatchBox setBackgroundColor:[CPColor colorWithWhite:0.8 alpha:1.0]];
    [swatchBox setAutoresizingMask:CPViewWidthSizable];

    _swatchView = [[_CPColorPanelSwatches alloc] initWithFrame:CGRectInset([swatchBox bounds], 1.0, 1.0)];

    [_swatchView setColorPanel:self];
    [_swatchView setAutoresizingMask:CPViewWidthSizable];

    [swatchBox addSubview:_swatchView];

    var _swatchLabel = [[CPTextField alloc] initWithFrame:CGRectMake(10, TOOLBAR_HEIGHT + 8 + PREVIEW_HEIGHT + 6, 60, 15)];
    [_swatchLabel setStringValue:"Swatches:"];
    [_swatchLabel setTextColor:[CPColor blackColor]];
    [_swatchLabel setAlignment:CPRightTextAlignment];


    var opacityLabel = [[CPTextField alloc] initWithFrame:CGRectMake(10, TOOLBAR_HEIGHT + PREVIEW_HEIGHT + 35, 60, 20)];
    [opacityLabel setStringValue:"Opacity:"];
    [opacityLabel setTextColor:[CPColor blackColor]];
    [opacityLabel setAlignment:CPRightTextAlignment];

    _opacitySlider = [[CPSlider alloc] initWithFrame:CGRectMake(76, TOOLBAR_HEIGHT + PREVIEW_HEIGHT + 34, CGRectGetWidth(bounds) - 86, 20.0)];

    [_opacitySlider setMinValue:0.0];
    [_opacitySlider setMaxValue:1.0];
    [_opacitySlider setAutoresizingMask:CPViewWidthSizable];

    [_opacitySlider setTarget:self];
    [_opacitySlider setAction:@selector(setOpacity:)];

    [contentView addSubview:_toolbar];
    [contentView addSubview:previewBox];
    [contentView addSubview:_previewLabel];
    [contentView addSubview:swatchBox];
    [contentView addSubview:_swatchLabel];
    [contentView addSubview:opacityLabel];
    [contentView addSubview:_opacitySlider];

    _target = nil;
    _action = nil;
    _activePicker = nil;

    [_previewView setBackgroundColor:_color];

    if (buttonForLater)
        [self _setPicker:buttonForLater];
}

- (void)setOpacity:(id)sender
{
    var components = [[self color] components],
        alpha = [sender floatValue];

    [self setColor:[_color colorWithAlphaComponent:alpha] updatePicker:YES];
}

@end


CPColorDragType = "CPColorDragType";

var CPColorPanelSwatchesCookie = "CPColorPanelSwatchesCookie";

/* @ignore */
@implementation _CPColorPanelSwatches : CPView
{
    CPArray _swatches;
    CPColor _dragColor;
    CPColorPanel _colorPanel;
    CPCookie _swatchCookie;
}

- (id)initWithFrame:(CPRect)aFrame
{
    self = [super initWithFrame:aFrame];

    [self setBackgroundColor:[CPColor grayColor]];

    [self registerForDraggedTypes:[CPArray arrayWithObjects:CPColorDragType]];

    var whiteColor = [CPColor whiteColor];

    _swatchCookie = [[CPCookie alloc] initWithName:CPColorPanelSwatchesCookie];
    var colorList = [self startingColorList];

    _swatches = [];

    for (var i = 0; i < 50; i++)
    {
        // FIXME: http://280north.lighthouseapp.com/projects/13294-cappuccino/tickets/25-implement-cpbox
        var view = [[CPView alloc] initWithFrame:CPRectMake(13 * i + 1, 1, 12, 12)],
            fillView = [[CPView alloc] initWithFrame:CGRectInset([view bounds], 1.0, 1.0)];

        [view setBackgroundColor:whiteColor];
        [fillView setBackgroundColor:(i < colorList.length) ? colorList[i] : whiteColor];

        [view addSubview:fillView];

        [self addSubview:view];

        _swatches.push(view);
    }

    return self;
}

- (BOOL)isOpaque
{
    return YES;
}

- (CPArray)startingColorList
{
    var cookieValue = [_swatchCookie value];

    if (!cookieValue)
    {
        return [
            [CPColor blackColor],
            [CPColor darkGrayColor],
            [CPColor grayColor],
            [CPColor lightGrayColor],
            [CPColor whiteColor],
            [CPColor redColor],
            [CPColor greenColor],
            [CPColor blueColor],
            [CPColor yellowColor]
        ];
    }

    var cookieValue = eval(cookieValue),
        result = [];

    for (var i = 0; i < cookieValue.length; i++)
        result.push([CPColor colorWithHexString:cookieValue[i]]);

    return result;
}

- (CPArray)saveColorList
{
    var result = [];
    // FIXME: http://280north.lighthouseapp.com/projects/13294-cappuccino/tickets/25-implement-cpbox
    for (var i = 0; i < _swatches.length; i++)
        result.push([[[_swatches[i] subviews][0] backgroundColor] hexString]);

    var future = new Date();
    future.setYear(2019);

    [_swatchCookie setValue:JSON.stringify(result) expires:future domain:nil];
}

- (void)setColorPanel:(CPColorPanel)panel
{
    _colorPanel = panel;
}

- (CPColorPanel)colorPanel
{
    return _colorPanel;
}

- (CPColor)colorAtIndex:(int)index
{
    return [[_swatches[index] subviews][0] backgroundColor];
}

- (void)setColor:(CPColor)aColor atIndex:(int)index
{
    // FIXME: http://280north.lighthouseapp.com/projects/13294-cappuccino/tickets/25-implement-cpbox
    [[_swatches[index] subviews][0] setBackgroundColor:aColor];
    [self saveColorList];
}

- (void)mouseUp:(CPEvent)anEvent
{
    var point = [self convertPoint:[anEvent locationInWindow] fromView:nil],
        bounds = [self bounds];

    if (!CGRectContainsPoint(bounds, point) || point.x > [self bounds].size.width - 1 || point.x < 1)
        return NO;

    [_colorPanel setColor:[self colorAtIndex:FLOOR(point.x / 13)] updatePicker:YES];
}

- (void)mouseDragged:(CPEvent)anEvent
{
    var point = [self convertPoint:[anEvent locationInWindow] fromView:nil];

     if (point.x > [self bounds].size.width - 1 || point.x < 1)
        return NO;

    [[CPPasteboard pasteboardWithName:CPDragPboard] declareTypes:[CPArray arrayWithObject:CPColorDragType] owner:self];

    var swatch = _swatches[FLOOR(point.x / 13)];

    // FIXME: http://280north.lighthouseapp.com/projects/13294-cappuccino/tickets/25-implement-cpbox
    _dragColor = [[swatch subviews][0] backgroundColor];

    var bounds = CGRectMakeCopy([swatch bounds]);

    // FIXME: http://280north.lighthouseapp.com/projects/13294-cappuccino/tickets/25-implement-cpbox
    var dragView = [[CPView alloc] initWithFrame:bounds],
        dragFillView = [[CPView alloc] initWithFrame:CGRectInset(bounds, 1.0, 1.0)];

    [dragView setBackgroundColor:[CPColor blackColor]];
    [dragFillView setBackgroundColor:_dragColor];

    [dragView addSubview:dragFillView];

    [self dragView:dragView
                at:CGPointMake(point.x - bounds.size.width / 2.0, point.y - bounds.size.height / 2.0)
            offset:CGPointMake(0.0, 0.0)
             event:anEvent
        pasteboard:nil
            source:self
         slideBack:YES];
}

- (void)pasteboard:(CPPasteboard)aPasteboard provideDataForType:(CPString)aType
{
    if (aType == CPColorDragType)
        [aPasteboard setData:[CPKeyedArchiver archivedDataWithRootObject:_dragColor] forType:aType];
}

- (void)performDragOperation:(id <CPDraggingInfo>)aSender
{
    var location = [self convertPoint:[aSender draggingLocation] fromView:nil],
        pasteboard = [aSender draggingPasteboard],
        swatch = nil;

    if (![pasteboard availableTypeFromArray:[CPColorDragType]] || location.x > [self bounds].size.width - 1 || location.x < 1)
        return NO;

    [self setColor:[CPKeyedUnarchiver unarchiveObjectWithData:[pasteboard dataForType:CPColorDragType]] atIndex:FLOOR(location.x / 13)];
}

@end

/* @ignore */
@implementation _CPColorPanelPreview : CPView
{
    CPColorPanel _colorPanel;
}

- (id)initWithFrame:(CPRect)aFrame
{
    self = [super initWithFrame:aFrame];

    [self registerForDraggedTypes:[CPArray arrayWithObjects:CPColorDragType]];

    return self;
}

- (void)setColorPanel:(CPColorPanel)aPanel
{
    _colorPanel = aPanel;
}

- (CPColorPanel)colorPanel
{
    return _colorPanel;
}

- (void)performDragOperation:(id <CPDraggingInfo>)aSender
{
    var pasteboard = [aSender draggingPasteboard];

    if (![pasteboard availableTypeFromArray:[CPColorDragType]])
        return NO;

    var color = [CPKeyedUnarchiver unarchiveObjectWithData:[pasteboard dataForType:CPColorDragType]];
    [_colorPanel setColor:color updatePicker:YES];
}

- (BOOL)isOpaque
{
    return YES;
}

- (void)mouseDragged:(CPEvent)anEvent
{
    var point = [self convertPoint:[anEvent locationInWindow] fromView:nil];

    [[CPPasteboard pasteboardWithName:CPDragPboard] declareTypes:[CPColorDragType] owner:self];

    var bounds = CPRectMake(0, 0, 15, 15);

    // FIXME: http://280north.lighthouseapp.com/projects/13294-cappuccino/tickets/25-implement-cpbox
    var dragView = [[CPView alloc] initWithFrame:bounds],
        dragFillView = [[CPView alloc] initWithFrame:CGRectInset(bounds, 1.0, 1.0)];

    [dragView setBackgroundColor:[CPColor blackColor]];
    [dragFillView setBackgroundColor:[self backgroundColor]];

    [dragView addSubview:dragFillView];

    [self dragView:dragView
                at:CPPointMake(point.x - bounds.size.width / 2.0, point.y - bounds.size.height / 2.0)
            offset:CPPointMake(0.0, 0.0)
             event:anEvent
        pasteboard:nil
            source:self
         slideBack:YES];
}

- (void)pasteboard:(CPPasteboard)aPasteboard provideDataForType:(CPString)aType
{
    if (aType == CPColorDragType)
        [aPasteboard setData:[CPKeyedArchiver archivedDataWithRootObject:[self backgroundColor]] forType:aType];
}

@end

@import "CPColorPicker.j"
@import "CPSliderColorPicker.j"

[CPColorPanel provideColorPickerClass:CPColorWheelColorPicker];
[CPColorPanel provideColorPickerClass:CPSliderColorPicker];
Something went wrong with that request. Please try again.