BWToolkit / BWTexturedSliderCell.m

//
//  BWTexturedSliderCell.m
//  BWToolkit
//
//  Created by Brandon Walkin (www.brandonwalkin.com)
//  All code is provided under the New BSD license.
//

#import "BWTexturedSliderCell.h"

@implementation BWTexturedSliderCell
@synthesize trackHeight;

static NSImage *trackLeftImage, *trackFillImage, *trackRightImage, *thumbPImage, *thumbNImage;

+ (void)initialize 
{
	if([BWTexturedSliderCell class] == [self class])
	{
		NSBundle *bundle = [NSBundle bundleForClass:[BWTexturedSliderCell class]];

		trackLeftImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"TexturedSliderTrackLeft.tiff"]];
		trackFillImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"TexturedSliderTrackFill.tiff"]];
		trackRightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"TexturedSliderTrackRight.tiff"]];
		thumbPImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"TexturedSliderThumbP.tiff"]];
		thumbNImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"TexturedSliderThumbN.tiff"]];
	}
}

- (id)initWithCoder:(NSCoder *)decoder;
{
    if ((self = [super initWithCoder:decoder]) != nil)
	{
		[self setTrackHeight:[decoder decodeBoolForKey:@"BWTSTrackHeight"]];
		[self setControlSize:NSSmallControlSize];
		isPressed = NO;
	}
	return self;
}

- (void)encodeWithCoder:(NSCoder*)coder
{
    [super encodeWithCoder:coder];
	
	[coder encodeBool:[self trackHeight] forKey:@"BWTSTrackHeight"];
}
	
- (NSControlSize)controlSize
{
	return NSRegularControlSize;
}

- (void)setControlSize:(NSControlSize)size
{
	
}

- (NSInteger)numberOfTickMarks
{
	return 0;
}

- (void)setNumberOfTickMarks:(NSInteger)numberOfTickMarks
{
	
}

- (void)drawBarInside:(NSRect)cellFrame flipped:(BOOL)flipped
{	
	NSRect slideRect = cellFrame;
	
	if (trackHeight == 0)
		slideRect.size.height = trackFillImage.size.height;
	else
		slideRect.size.height = trackFillImage.size.height + 1;
	
	slideRect.origin.y += roundf((cellFrame.size.height - slideRect.size.height) / 2);

	// Inset the bar so the knob goes all the way to both ends
	slideRect.size.width -= 9;
	slideRect.origin.x += 5;

	if ([self isEnabled])
		NSDrawThreePartImage(slideRect, trackLeftImage, trackFillImage, trackRightImage, NO, NSCompositeSourceOver, 1, flipped);
	else
		NSDrawThreePartImage(slideRect, trackLeftImage, trackFillImage, trackRightImage, NO, NSCompositeSourceOver, 0.5, flipped);
}

- (void)drawKnob:(NSRect)rect
{
	NSImage *drawImage;
	
	if (isPressed)
		drawImage = thumbPImage;
	else
		drawImage = thumbNImage;
	
	NSPoint drawPoint;
	drawPoint.x = rect.origin.x + roundf((rect.size.width - drawImage.size.width) / 2);
	drawPoint.y = NSMaxY(rect) - roundf((rect.size.height - drawImage.size.height) / 2);
	
	if (trackHeight == 0)
		drawPoint.y++;
	
	[drawImage compositeToPoint:drawPoint operation:NSCompositeSourceOver];
}

- (BOOL)_usesCustomTrackImage
{
	return YES;
}

- (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView
{
	isPressed = YES;
	return [super startTrackingAt:startPoint inView:controlView];	
}

- (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:(NSView *)controlView mouseIsUp:(BOOL)flag
{
	isPressed = NO;
	[super stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:(NSView *)controlView mouseIsUp:(BOOL)flag];
}

@end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.