Created
November 11, 2008 05:56
-
-
Save boucher/23760 to your computer and use it in GitHub Desktop.
Color Well Pop Up Thing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var FormatColorWellBackgroundColor = nil, | |
| FormatColorWellHighlightedBackgroundColor = nil; | |
| var FormatColorWellMenu = nil; | |
| @implementation FormatColorWell : CPColorWell | |
| { | |
| BOOL _hasNilColor; | |
| BOOL _showsNilColor; | |
| } | |
| + (void)initialize | |
| { | |
| if (self != [FormatColorWell class]) | |
| return; | |
| FormatColorWellBackgroundColor = [CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices: | |
| [FormatBarButtonDividerImage, FormatBarButtonCenterImage, FormatBarButtonDividerImage] isVertical:NO]]; | |
| FormatColorWellHighlightedBackgroundColor = [CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices: | |
| [FormatBarButtonDividerImage, FormatBarButtonHighlightedCenterImage, FormatBarButtonDividerImage] isVertical:NO]]; | |
| FormatColorWellColors = []; | |
| var i = 0; | |
| for(; i < 8; ++i) | |
| FormatColorWellColors.push([CPColor colorWithCalibratedWhite:(i / 7) alpha:1.0]); | |
| for(i = 0; i < 16; ++i) | |
| FormatColorWellColors.push([CPColor colorWithHue:((i%8)/8)*360 saturation:100 brightness:((FLOOR(i/8) +1)/3)*100]); | |
| for(i = 0; i < 24; ++i) | |
| FormatColorWellColors.push([CPColor colorWithHue:((i%8)/8)*360 saturation:(100 - (FLOOR(i/8)/3)*100) brightness:100]); | |
| FormatColorWellMenu = [[CPMenu alloc] initWithTitle:@""]; | |
| [FormatColorWellMenu setShowsStateColumn:NO]; | |
| var menuItem = [[CPMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:nil], | |
| menuView = [[FormatColorView alloc] initWithColors:FormatColorWellColors]; | |
| [menuItem setView:menuView]; | |
| [FormatColorWellMenu addItem:menuItem]; | |
| [FormatColorWellMenu addItemWithTitle:@"No Color" action:@selector(setNilColor) keyEquivalent:nil]; | |
| [FormatColorWellMenu addItemWithTitle:@"More Choices..." action:@selector(showColorPanel:) keyEquivalent:nil]; | |
| } | |
| - (void)setShowsNilColor:(BOOL)shouldShowNilColor | |
| { | |
| _showsNilColor = shouldShowNilColor; | |
| } | |
| - (void)drawBezelWithHighlight:(BOOL)shouldHighlight | |
| { | |
| [self setBackgroundColor:shouldHighlight ? FormatColorWellHighlightedBackgroundColor : FormatColorWellBackgroundColor]; | |
| } | |
| - (void)drawWellInside:(CGRect)aRect | |
| { | |
| var bounds = CGRectMakeCopy([self bounds]); | |
| bounds.size.height -= 1.0; | |
| [super drawWellInside:CGRectInset(bounds, 3.0, 3.0)]; | |
| } | |
| - (void)mouseUp:(CPEvent)anEvent | |
| { | |
| return; | |
| } | |
| - (void)mouseDown:(CPEvent)anEvent | |
| { | |
| if (![self isEnabled]) | |
| return; | |
| anEvent = [CPEvent mouseEventWithType:CPLeftMouseDown location:[self convertPoint:CGPointMake(0.0, CGRectGetHeight([self bounds])) toView:nil] | |
| modifierFlags:[anEvent modifierFlags] timestamp:[anEvent timestamp] windowNumber:[[anEvent window] windowNumber] context:nil | |
| eventNumber:0 clickCount:[anEvent clickCount] pressure:[anEvent pressure]] | |
| [FormatColorWellMenu setDelegate:self]; | |
| var items = [FormatColorWellMenu itemArray]; | |
| [[items[0] view] setColorWell:self]; | |
| [items[1] setTarget:self]; | |
| [items[1] setHidden:!_showsNilColor]; | |
| [items[2] setTarget:self]; | |
| [CPMenu popUpContextMenu:FormatColorWellMenu withEvent:anEvent forView:self withFont:[CPFont systemFontOfSize:12.0]]; | |
| } | |
| - (void)menuWillOpen:(CPMenu)aMenu | |
| { | |
| [self drawBezelWithHighlight:YES]; | |
| } | |
| - (void)menuDidClose:(CPMenu)aMenu | |
| { | |
| [self drawBezelWithHighlight:NO]; | |
| } | |
| - (void)showColorPanel:(id)aSender | |
| { | |
| [self activate:YES]; | |
| var colorPanel = [CPColorPanel sharedColorPanel]; | |
| [colorPanel setColor:[self color]]; | |
| [colorPanel orderFront:self]; | |
| } | |
| - (void)setNilColor | |
| { | |
| [self setColorOrNil:nil]; | |
| [self sendAction:[self action] to:[self target]]; | |
| } | |
| - (void)setColor:(CPColor)aColor | |
| { | |
| _hasNilColor = NO; | |
| [super setColor:aColor]; | |
| } | |
| - (void)setColorOrNil:(CPColor)aColor | |
| { | |
| if (!aColor) | |
| { | |
| [self setColor:[CPColor whiteColor]]; | |
| _hasNilColor = YES; | |
| } | |
| else | |
| { | |
| _hasNilColor = NO; | |
| [self setColor:aColor]; | |
| } | |
| } | |
| - (CPColor)colorOrNil | |
| { | |
| if (_hasNilColor) | |
| return nil; | |
| return [super color]; | |
| } | |
| @end | |
| var COLOR_COLUMNS = 8, | |
| COLOR_MARGIN = 5.0, | |
| COLOR_WIDTH = 14.0, | |
| COLOR_HEIGHT = 14.0; | |
| @implementation FormatColorView : CPView | |
| { | |
| CPArray _colors; | |
| CPView _selectionView; | |
| CPColorWell _colorWell; | |
| } | |
| - (id)initWithColors:(CPArray)colors | |
| { | |
| var count = [colors count], | |
| rows = CEIL(count / COLOR_COLUMNS); | |
| self = [super initWithFrame:CGRectMake(0.0, 0.0, COLOR_COLUMNS * (COLOR_WIDTH + COLOR_MARGIN) + COLOR_MARGIN, rows * (COLOR_WIDTH + COLOR_MARGIN) + COLOR_MARGIN)]; | |
| if (self) | |
| { | |
| _colors = colors; | |
| _selectionView = [[CPView alloc] initWithFrame:CGRectMake(0.0, 0.0, COLOR_WIDTH + COLOR_MARGIN * 2 - 4, COLOR_HEIGHT + COLOR_MARGIN * 2 -4)]; | |
| [_selectionView setHidden:YES]; | |
| [_selectionView setHitTests:NO]; | |
| [_selectionView setBackgroundColor:[CPColor colorWithCalibratedRed:81.0 / 255.0 green:83.0 / 255.0 blue:109.0 / 255.0 alpha:1.0]]; | |
| [self addSubview:_selectionView]; | |
| var i = 0, | |
| x = COLOR_MARGIN, | |
| y = COLOR_MARGIN; | |
| for (; i < count; ++i, x+= COLOR_WIDTH + COLOR_MARGIN) | |
| { | |
| if (i && i % 8 == 0) | |
| { | |
| x = COLOR_MARGIN; | |
| y += COLOR_HEIGHT + COLOR_MARGIN; | |
| } | |
| var view = [[CPView alloc] initWithFrame:CGRectMake(x, y, COLOR_WIDTH, COLOR_HEIGHT)], | |
| shadowView = [[CPShadowView alloc] initWithFrame:CGRectMakeZero()]; | |
| [view setHitTests:NO]; | |
| [shadowView setFrameForContentFrame:[view frame]]; | |
| [view setBackgroundColor:colors[i]]; | |
| [self addSubview:view]; | |
| [self addSubview:shadowView]; | |
| } | |
| } | |
| return self; | |
| } | |
| - (void)setColorWell:(CPColorWell)aColorWell | |
| { | |
| _colorWell = aColorWell; | |
| } | |
| - (unsigned)colorIndexAtPoint:(CGPoint)aPoint | |
| { | |
| var row = FLOOR((aPoint.y - CEIL(COLOR_MARGIN / 2.0)) / (COLOR_MARGIN + COLOR_HEIGHT)), | |
| column = FLOOR((aPoint.x - CEIL(COLOR_MARGIN / 2.0)) / (COLOR_MARGIN + COLOR_WIDTH)); | |
| if (column < 0 || column >= COLOR_COLUMNS) | |
| return CPNotFound; | |
| var index = (row * COLOR_COLUMNS) + column; | |
| if (index < 0 || index >= [_colors count]) | |
| return CPNotFound; | |
| return index; | |
| } | |
| - (void)highlightColorAtIndex:(unsigned)anIndex | |
| { | |
| if (anIndex == CPNotFound) | |
| [_selectionView setHidden:YES]; | |
| else | |
| { | |
| var row = FLOOR(anIndex / COLOR_COLUMNS), | |
| column = anIndex % COLOR_COLUMNS; | |
| [_selectionView setFrameOrigin:CGPointMake(column * (COLOR_WIDTH + COLOR_MARGIN) + 2, row * (COLOR_HEIGHT + COLOR_MARGIN) + 2)]; | |
| [_selectionView setHidden:NO]; | |
| } | |
| } | |
| - (void)mouseMoved:(CPEvent)anEvent | |
| { | |
| [self highlightColorAtIndex:[self colorIndexAtPoint:[self convertPoint:[anEvent locationInWindow] fromView:nil]]]; | |
| } | |
| - (void)mouseExited:(CPEvent)anEvent | |
| { | |
| [self highlightColorAtIndex:CPNotFound]; | |
| } | |
| - (void)mouseDragged:(CPEvent)anEvent | |
| { | |
| [self highlightColorAtIndex:[self colorIndexAtPoint:[self convertPoint:[anEvent locationInWindow] fromView:nil]]]; | |
| } | |
| - (void)mouseUp:(CPEvent)anEvent | |
| { | |
| var index = [self colorIndexAtPoint:[self convertPoint:[anEvent locationInWindow] fromView:nil]]; | |
| [self highlightColorAtIndex:CPNotFound]; | |
| if (index != CPNotFound) | |
| { | |
| [_colorWell setColor:[_colors objectAtIndex:index]]; | |
| [_colorWell sendAction:[_colorWell action] to:[_colorWell target]]; | |
| } | |
| [[[self enclosingMenuItem] menu] cancelTracking]; | |
| } | |
| @end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment