// // DKSliderCell.m // DarkKit // // Created by Chad Weider on 4/7/07. // Copyright (c) 2008 Chad Weider // Some rights reserved: // // This software is provided 'as-is', without any express or implied warranty. // In no event will the authors be held liable for any damages arising from the // use of this software. // // Permission is granted to anyone to use this software for any purpose, // including commercial applications, and to alter it and redistribute it // freely, subject to the following restrictions: // // 1. The origin of this software must not be misrepresented; you must not // claim that you wrote the original software. If you use this software in a // product, an acknowledgment in the product documentation would be appreciated // but is not required. // // 2. Altered source versions must be plainly marked as such, and must not be // misrepresented as being the original software. // // 3. This notice may not be removed or altered from any source distribution. // #import "DKSliderCell.h" #import "NSBezierPath+PXRoundedRectangleAdditions.h" #import "CTGradient.h" @implementation DKSliderCell - (void)drawKnob:(NSRect)knobRect { // [[NSColor greenColor] set]; // NSRectFill(knobRect); NSBezierPath *knobPath = [[NSBezierPath alloc] init]; if([self numberOfTickMarks] == 0) { float knobPad; if(NSWidth(knobRect) > NSHeight(knobRect)) knobRect.size.height = knobRect.size.width; else knobRect.size.width = knobRect.size.height; switch([self controlSize]) { case NSRegularControlSize: knobPad = 3.5; break; case NSSmallControlSize: knobPad = 1.5; break; case NSMiniControlSize: knobPad = 1.5; break; default: knobPad = 1; } knobRect.origin.x = floor(knobRect.origin.x); knobRect.origin.y = floor(knobRect.origin.y); [knobPath appendBezierPathWithOvalInRect:NSInsetRect(knobRect, knobPad, knobPad)]; } else { float curveRadius; NSSize knobSize; NSPoint knobOffset; switch([self controlSize]) { case NSRegularControlSize: knobSize.width = 14; knobSize.height = 9+14/2; knobOffset.x = 0; knobOffset.y = -1; curveRadius = 2.5; break; case NSSmallControlSize: knobSize.width = 10; knobSize.height = 7+10/2; knobOffset.x = 0; knobOffset.y = -1; curveRadius = 2; break; case NSMiniControlSize: knobSize.width = 8; knobSize.height = 6+8/2; knobOffset.x = 0; knobOffset.y = 0; curveRadius = 1.5; break; } //center knobsize in knobRect knobRect = NSOffsetRect(knobRect, (NSWidth(knobRect)-knobSize.width)/2, (NSHeight(knobRect)-knobSize.height)/2); knobRect.size = knobSize; knobRect.origin.x = floor(knobRect.origin.x)+0.5; knobRect.origin.y = floor(knobRect.origin.y)+0.5; //do offset if([self isVertical]) { if([self tickMarkPosition] == NSTickMarkRight) knobRect = NSOffsetRect(knobRect, +knobOffset.y, +knobOffset.x); else knobRect = NSOffsetRect(knobRect, -knobOffset.y, -knobOffset.x); } else { if([self tickMarkPosition] == NSTickMarkBelow) knobRect = NSOffsetRect(knobRect, +knobOffset.x, +knobOffset.y); else knobRect = NSOffsetRect(knobRect, -knobOffset.x, -knobOffset.y); } /* Draw as if ticks below - we'll transform for the different cases */ //steps: +, --, \, |, /, \, | / [knobPath moveToPoint: NSMakePoint(NSMinX(knobRect)+curveRadius, NSMinY(knobRect))]; [knobPath lineToPoint: NSMakePoint(NSMaxX(knobRect)-curveRadius, NSMinY(knobRect))]; [knobPath appendBezierPathWithArcFromPoint:NSMakePoint(NSMaxX(knobRect), NSMinY(knobRect)) toPoint:NSMakePoint(NSMaxX(knobRect), NSMinY(knobRect)+curveRadius) radius:curveRadius]; [knobPath lineToPoint: NSMakePoint(NSMaxX(knobRect), NSMaxY(knobRect)-(NSWidth(knobRect)/2))]; [knobPath lineToPoint: NSMakePoint(NSMidX(knobRect), NSMaxY(knobRect))]; [knobPath lineToPoint: NSMakePoint(NSMinX(knobRect), NSMaxY(knobRect)-(NSWidth(knobRect)/2))]; [knobPath lineToPoint: NSMakePoint(NSMinX(knobRect), NSMinY(knobRect)+curveRadius)]; [knobPath appendBezierPathWithArcFromPoint:NSMakePoint(NSMinX(knobRect), NSMinY(knobRect)) toPoint:NSMakePoint(NSMinX(knobRect)+curveRadius, NSMinY(knobRect)) radius:curveRadius]; NSAffineTransform *transform = [NSAffineTransform transform]; if([self isVertical]) { if([self tickMarkPosition] == NSTickMarkRight) { [transform translateXBy:+NSMidX(knobRect) yBy:+NSMidY(knobRect)]; [transform rotateByDegrees:270]; [transform translateXBy:-NSMidX(knobRect) yBy:-NSMidY(knobRect)]; } else { [transform translateXBy:+NSMidX(knobRect) yBy:+NSMidY(knobRect)]; [transform rotateByDegrees:90]; [transform translateXBy:-NSMidX(knobRect) yBy:-NSMidY(knobRect)]; } } else { if([self tickMarkPosition] == NSTickMarkBelow) ; else { [transform translateXBy:+NSMidX(knobRect) yBy:+NSMidY(knobRect)]; [transform rotateByDegrees:180]; [transform translateXBy:-NSMidX(knobRect) yBy:-NSMidY(knobRect)]; } } [knobPath transformUsingAffineTransform:transform]; } NSColor *startColor = [NSColor colorWithCalibratedWhite:([self isHighlighted] ? .4 : .3) alpha:([self isEnabled] ? 1 : .5)]; NSColor *endColor = [NSColor colorWithCalibratedWhite:([self isHighlighted] ? .2: 0) alpha:([self isEnabled] ? 1 : .5)]; CTGradient *backgroundGradient = [CTGradient gradientWithBeginningColor:startColor endingColor:endColor]; [backgroundGradient fillBezierPath:knobPath angle:90.0]; // and stroke [[NSColor colorWithCalibratedWhite:1 alpha:([self isEnabled] ? 1 : .5)] set]; [knobPath setLineWidth:1]; [knobPath setLineJoinStyle:NSRoundLineJoinStyle]; [knobPath stroke]; [knobPath release]; } - (void)drawBarInside:(NSRect)frame flipped:(BOOL)flipped { float barHeight; float cornerRadius; switch([self controlSize]) { case NSRegularControlSize: barHeight = 5; cornerRadius = 2.5; break; case NSSmallControlSize: barHeight = 4; cornerRadius = 1.5; break; case NSMiniControlSize: barHeight = 3; cornerRadius = 1; break; } float edgePad = 1.5; NSRect barRect = [self isVertical] ? NSInsetRect(frame, (NSWidth(frame)-barHeight)/2, edgePad) : NSInsetRect(frame, edgePad, (NSHeight(frame)-barHeight)/2); if([self numberOfTickMarks] != 0) { if([self isVertical]) barRect.origin.x -= ([self tickMarkPosition] == NSTickMarkBelow) ? barHeight : -barHeight; else barRect.origin.y -= ([self tickMarkPosition] == NSTickMarkRight) ? barHeight : -barHeight; } barRect.origin.y = floorf(barRect.origin.y); barRect.origin.y -= flipped ? -.5 : .5; barRect.origin.x = floorf(barRect.origin.x); barRect.origin.x -= flipped ? -.5 : .5; NSBezierPath *barPath = [NSBezierPath bezierPathWithRoundedRect:barRect cornerRadius:cornerRadius]; [[NSColor colorWithCalibratedWhite:0.35 alpha:1.0] set]; [barPath fill]; [barPath setLineWidth:1]; [[NSColor whiteColor] set]; [barPath stroke]; } - (BOOL)_usesCustomTrackImage { return YES; } @end