Help with Obj-C tidbit

Ok, so I have a custom slider that I’m using as a progress bar. Everything is working perfectly fine. I was just wondering how I would change the slider knob image on mousedown. I’m not sure how :confused:

Heres the code

//
//  PolishedSliderCell.m
//  Play MiTunes
//
//  Created by Collin Henderson on 08/09/07.
//  Copyright 2007 Hendosoft. All rights reserved.
//

#import "PolishedSliderCell.h"

@implementation PolishedSliderCell

- (void)drawBarInside:(NSRect)cellFrame flipped:(BOOL)flipped
{
	NSImage *leftImage = [NSImage imageNamed:@"psliderleft.png"];
	NSImage *fillImage = [NSImage imageNamed:@"psliderfill.png"];
	NSImage *rightImage = [NSImage imageNamed:@"psliderright.png"];
				
	NSSize size = [leftImage size];
	float addX = size.width / 2.0;
	float y = NSMaxY(cellFrame) - (cellFrame.size.height-size.height)/2.0 ;
	float x = cellFrame.origin.x+addX;
	float fillX = x + size.width;
	float fillWidth = cellFrame.size.width - size.width - addX;
	
	[leftImage compositeToPoint:NSMakePoint(x, y) operation:NSCompositeSourceOver];

	size = [rightImage size];
	addX = (size.width / 2.0)-3;
	x = NSMaxX(cellFrame) - size.width - addX;
	fillWidth -= size.width+addX;
	
	[rightImage compositeToPoint:NSMakePoint(x, y) operation:NSCompositeSourceOver];
	
	[fillImage setScalesWhenResized:YES];
	[fillImage setSize:NSMakeSize(fillWidth, [fillImage size].height)];
	[fillImage compositeToPoint:NSMakePoint(fillX, y) operation:NSCompositeSourceOver];
}

- (void)drawKnob:(NSRect)rect
{
	NSImage *knob;
	
	if([self numberOfTickMarks] == 0)
		knob = [NSImage imageNamed:@"psliderbutton.png"];
	
	float x = (rect.origin.x + (rect.size.width - [knob size].width) / 2)+2;
	float y = NSMaxY(rect) - (rect.size.height - [knob size].height) / 2 ;
	
	[knob compositeToPoint:NSMakePoint(x, y) operation:NSCompositeSourceOver];
}

-(NSRect)knobRectFlipped:(BOOL)flipped
{
	NSRect rect = [super knobRectFlipped:flipped];
	if([self numberOfTickMarks] > 0){
		rect.size.height+=2;
		return NSOffsetRect(rect, 0, flipped ? 2 : -2);
		}
	return rect;
}

- (BOOL)_usesCustomTrackImage
{
	return YES;
}

@end

Hi Hendo,

just add this in PolishedSliderCell.h:

@interface PolishedSliderCell : NSSliderCell {
	BOOL mouseIsDown;
}
-(void)setCurrentMouseIsDown:(BOOL)state;

and in PolishedSliderCell.m:

modify:

- (void)drawKnob:(NSRect)rect
{
	NSImage *knob;
	
	if([self numberOfTickMarks] == 0) {
		if (mouseIsDown) 
			knob = [NSImage imageNamed:@"sliderbutton_down.png"];
		else
			knob = [NSImage imageNamed:@"sliderbutton.png"];
	}
...

and add this somewhere in the implementation:

-(void)setCurrentMouseIsDown:(BOOL)state{
	mouseIsDown = state;
}

then you can set the state from the cell’s control:

- (void)mouseDown:(NSEvent *)theEvent{
	[[self cell] setCurrentMouseIsDown:YES]; // mouse down
	[super mouseDown];
	[[self cell] setCurrentMouseIsDown:NO]; // mouse up
}

Thanks Dominik :smiley: