// // DKScroller.m // DarkKit // // Created by Chad Weider on 4/14/08. // 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 "DKScroller.h" #import "NSBezierPath+PXRoundedRectangleAdditions.h" #include "CTGradient.h" @interface NSScroller (NSScrollerPrivate) - (struct _NSRect)_drawingRectForPart:(unsigned int)fp8; + (int)_arrowsConfig; //1 top & bottom //3 bottom + (void)_setArrowsConfig:(int)fp8; @end @implementation DKScroller + (int)_arrowsConfig; //1 top & bottom //3 bottom { return 1; } + (void)_setArrowsConfig:(int)fp8 { [super _setArrowsConfig:1]; } - (BOOL)isVertical { return NSWidth([self bounds]) < NSHeight([self bounds]); } //- (NSRect)rectForPart:(NSScrollerPart)aPart // { // float arrowLength = 0; // float slotLength; // float knobLength; // // if([self usableParts] == NSNoScrollerParts || [self arrowsPosition] == NSScrollerArrowsNone) // arrowLength = 0; // else // { // switch([self controlSize]) // { // case NSRegularControlSize: // arrowLength = 12; // break; // case NSSmallControlSize: // arrowLength = 8; // break; // case NSMiniControlSize: // arrowLength = 12; // break; // } // } // // // slotLength = ([self isVertical] ? NSHeight([self bounds]):NSWidth([self bounds]) ) - 2*arrowLength; // knobLength = slotLength*[self knobProportion]; // knobPosition = floorf(([self floatValue]*(1-knobLength))*slotLength); // // if([self isVertical]) // { // // } // else // { // // } // } - (NSRect)drawingRectForPart:(NSScrollerPart)part { // float arrowLength; // float bevelLength; // // switch([self controlSize]) // { // case NSRegularControlSize: // arrowLength = 12; // bevelLength = 8; // break; // case NSSmallControlSize: // arrowLength = 8; // bevelLength = 5; // break; // case NSMiniControlSize: // arrowLength = 12; // bevelLength = 8; // break; // } // // switch (part) // { // case NSScrollerKnob: // // break; // case NSScrollerDecrementPage: // case NSScrollerKnob: // case NSScrollerIncrementPage: // case NSScrollerDecrementLine: // case NSScrollerIncrementLine: // case NSScrollerKnobSlot: // // default: // return NSZeroRect; // } } - (void)drawKnob { NSRect knobRect = NSInsetRect([self _drawingRectForPart:NSScrollerKnob], .5, .5); NSBezierPath *knobPath = [NSBezierPath bezierPathWithRoundedRect:knobRect cornerRadius:[self isVertical] ? NSWidth(knobRect)/2 : NSHeight(knobRect)/2]; CTGradient *fillGradient; if([self isEnabled] && 0) fillGradient = [CTGradient gradientWithBeginningColor:[NSColor colorWithCalibratedWhite:.4 alpha:1.0] endingColor:[NSColor colorWithCalibratedWhite:.2 alpha:1.0]]; else fillGradient = [CTGradient gradientWithBeginningColor:[NSColor colorWithCalibratedWhite:.3 alpha:1.0] endingColor:[NSColor colorWithCalibratedWhite:.0 alpha:1.0]]; //[fillGradient fillBezierPath:knobPath angle:NSWidth(knobRect) > NSHeight(knobRect) ? -90.0 : 180]; [knobPath setLineWidth:1]; [[NSColor colorWithCalibratedWhite:1 alpha:1] set]; //[knobPath stroke]; } - (void)drawKnobSlotInRect:(NSRect)rect highlight:(BOOL)flag { CTGradient *fillGradient = [CTGradient gradientWithBeginningColor:[NSColor colorWithCalibratedWhite:.3 alpha:1.0] endingColor:[NSColor colorWithCalibratedWhite:.0 alpha:1.0]]; [fillGradient fillRect:rect angle:90]; } - (void)drawArrow:(NSScrollerArrow)arrow highlightPart:(NSScrollerPart)part { float arrowLength; float bevelLength; switch([self controlSize]) { case NSRegularControlSize: arrowLength = 12; bevelLength = 8; break; case NSSmallControlSize: arrowLength = 8; bevelLength = 5; break; case NSMiniControlSize: arrowLength = 12; bevelLength = 8; break; } [[NSColor greenColor]set]; //NSRectFill([self _drawingRectForPart:part]); [[NSColor orangeColor]set]; NSRectFill([self _drawingRectForPart:part]); //draw Inc and Dec button frame NSRect frameRect = NSMakeRect(0,0,arrowLength+bevelLength, [self isVertical] ? NSWidth([self bounds]) : NSHeight([self bounds])); frameRect = NSInsetRect(frameRect, .5, .5); float radius = ceil(NSHeight(frameRect)/2)+1.5; NSBezierPath *framePath = [NSBezierPath bezierPath]; /* Draw as if on right - we'll transform for the different cases */ //steps: +, --, |, --, ) [framePath moveToPoint: NSMakePoint(NSMinX(frameRect), NSMinY(frameRect))]; [framePath lineToPoint: NSMakePoint(NSMaxX(frameRect), NSMinY(frameRect))]; [framePath lineToPoint: NSMakePoint(NSMaxX(frameRect), NSMaxY(frameRect))]; [framePath lineToPoint: NSMakePoint(NSMinX(frameRect), NSMaxY(frameRect))]; [framePath appendBezierPathWithArcWithCenter:NSMakePoint(NSMinX(frameRect)-sqrt(radius*radius - (radius-1.5)*(radius-1.5)), NSMidY(frameRect)) radius:radius startAngle:+asin(radius/(radius+1))*360/(2*3.14159) endAngle:-asin(radius/(radius+1))*360/(2*3.14159) clockwise:YES]; [self drawKnobSlotInRect:frameRect highlight:NO]; [framePath setLineWidth:1]; [[NSColor colorWithCalibratedWhite:1 alpha:1] set]; [framePath stroke]; } //- (void)drawRect:(NSRect)rect // { // float arrowLength; // float bevelLength; // // NSLog(@"draw"); // // switch([self controlSize]) // { // case NSRegularControlSize: // arrowLength = 12; // bevelLength = 8; // break; // case NSSmallControlSize: // arrowLength = 8; // bevelLength = 5; // break; // case NSMiniControlSize: // arrowLength = 12; // bevelLength = 8; // break; // } // // // [self drawKnobSlotInRect:[self bounds]]; // // NSRect bevelRectIn = [self bounds]; // if([self isVertical]) // bevelRectIn.size.height = bevelLength; // else // bevelRectIn.size.width = bevelLength; // // NSRect bevelRectDe = NSOffsetRect(bevelRectIn, [self isVertical] ? 0 : NSWidth([self bounds])-bevelLength, [self isVertical] ? NSHeight([self bounds])-bevelLength : 0); // // // if([self usableParts] == NSNoScrollerParts || [self arrowsPosition] == NSScrollerArrowsNone) // { // [self drawArrowBevel:NSScrollerIncrementArrow inRect:bevelRectIn]; // [self drawArrowBevel:NSScrollerDecrementArrow inRect:bevelRectDe]; // } // else if([self usableParts] >= NSOnlyScrollerArrows) // { // [self drawArrowBevel:NSScrollerIncrementArrow inRect:NSInsetRect(bevelRectDe, [self isVertical] ? 0 : +arrowLength, [self isVertical] ? +arrowLength : 0)]; // [self drawArrowBevel:NSScrollerDecrementArrow inRect:NSInsetRect(bevelRectDe, [self isVertical] ? 0 : -arrowLength, [self isVertical] ? -arrowLength : 0)]; // // [self drawArrow:NSScrollerIncrementArrow inRect:NSInsetRect(bevelRectDe, [self isVertical] ? 0 : +arrowLength, [self isVertical] ? +arrowLength : 0)]; // [self drawArrow:NSScrollerDecrementArrow inRect:NSInsetRect(bevelRectDe, [self isVertical] ? 0 : -arrowLength, [self isVertical] ? -arrowLength : 0)]; // } // // if([self usableParts] == NSAllScrollerParts) // { // NSRect knobRect = [self bounds]; // // knobRect = NSInsetRect([self bounds], [self isVertical] ? 0 : bevelLength, [self isVertical] ? bevelLength : 0); // if([self arrowsPosition] != NSScrollerArrowsNone) // knobRect = NSInsetRect([self bounds], [self isVertical] ? 0 : arrowLength, [self isVertical] ? arrowLength : 0); // // // float length = [self isVertical] ? NSHeight(knobRect) : NSWidth(knobRect); // float position = floorf(([self floatValue]*(1-[self knobProportion]))*length); // // knobRect = NSOffsetRect(knobRect, [self isVertical] ? 0 : position, [self isVertical] ? position : 0); // if([self isVertical]) // knobRect.size.height = floorf(length*[self knobProportion]); // else // knobRect.size.width = floorf(length*[self knobProportion]); // // [self drawKnobInRect:knobRect]; // } // } @end