// // JSRunColorwell.m // JSRun // // Created by Jonathan Saggau on 12/6/06. // Copyright 2006 Jonathan Saggau. All rights reserved. // #import "JSRunColorwell.h" // use CTGradient to make the colorwell look a little like a button? // (at the expense of some alpha accuracy) //#define MAKE_GRADIENT_BUTTON 1 #ifdef MAKE_GRADIENT_BUTTON #import "CTGradient.h" #endif @implementation JSRunColorwell - (void)activate:(BOOL)exclusive { //allow alpha //http://www.allocinit.net/blog/show/30 [[NSColorPanel sharedColorPanel] setShowsAlpha:YES]; [super activate: exclusive]; } // Gemmell's code for somethin else somewhat messed with - (void)drawRect:(NSRect)aRect { //float borderWidth = ([self isEnabled]) ? 2.0 : 0.0; float borderWidth = 1.0; float borderAlpha = ([self isEnabled]) ? 1.0 : 0.0; float radius = 4.0; NSColor *borderColor; if(([self isEnabled]) && (![self isActive])) { //white iff we're enabled and not active //acts like the default color well this way borderColor = [NSColor colorWithCalibratedWhite:.8 alpha:borderAlpha]; } else { borderColor = [NSColor colorWithCalibratedWhite:.5 alpha:borderAlpha]; } // Construct rounded rect path NSRect boxRect = aRect; NSRect bgRect = boxRect; bgRect = NSInsetRect(boxRect, borderWidth / 2.0, borderWidth / 2.0); bgRect = NSIntegralRect(bgRect); bgRect.origin.x += 0.5; bgRect.origin.y += 0.5; int minX = NSMinX(bgRect); int midX = NSMidX(bgRect); int maxX = NSMaxX(bgRect); int minY = NSMinY(bgRect); int midY = NSMidY(bgRect); int maxY = NSMaxY(bgRect); NSBezierPath *bgPath = [NSBezierPath bezierPath]; // Bottom edge and bottom-right curve [bgPath moveToPoint:NSMakePoint(midX, minY)]; [bgPath appendBezierPathWithArcFromPoint:NSMakePoint(maxX, minY) toPoint:NSMakePoint(maxX, midY) radius:radius]; // Right edge and top-right curve [bgPath appendBezierPathWithArcFromPoint:NSMakePoint(maxX, maxY) toPoint:NSMakePoint(midX, maxY) radius:radius]; // Top edge and top-left curve [bgPath appendBezierPathWithArcFromPoint:NSMakePoint(minX, maxY) toPoint:NSMakePoint(minX, midY) radius:radius]; // Left edge and bottom-left curve [bgPath appendBezierPathWithArcFromPoint:NSMakePoint(minX, minY) toPoint:NSMakePoint(midX, minY) radius:radius]; [bgPath closePath]; // Draw background // Draw background #ifdef MAKE_GRADIENT_BUTTON // Draw gradient background //NSLog(@"DrawRect"); NSGraphicsContext *nsContext = [NSGraphicsContext currentContext]; [nsContext saveGraphicsState]; [bgPath addClip]; float r, g, b, a; NSColor *convertedColor = [[self color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace]; [convertedColor getRed:&r green:&g blue:&b alpha:&a]; CTGradient *gradient = [CTGradient gradientWithBeginningColor:[self color] endingColor:[NSColor colorWithCalibratedRed:r green:g blue:b alpha:a*.4]]; NSRect gradientRect = [bgPath bounds]; [gradient fillRect:gradientRect angle:270.0]; [nsContext restoreGraphicsState]; #else // Draw solid color background [[self color] set]; [bgPath fill]; #endif [borderColor set]; // Draw rounded rect around entire box if (borderWidth > 0.0) { [bgPath setLineWidth:borderWidth]; [bgPath stroke]; } } - (void)drawWellInside:(NSRect)insideRect { // [self drawRect:insideRect]; } @end