views:

21

answers:

2

Hi guys, I've sort of accomplised implementing a custom slider cell that can draw over using images for the scroll bar and knob. The only obstacle that is in the way now is this, when I drag the knob quickly, the images get messed up. i've posted a screen shot. http://img818.imageshack.us/img818/1730/duhm.png

Here is the code:

#import "customSliderCell.h"
@implementation customSliderCell
- (void)drawKnob:(NSRect)knobRect {

    NSImage * knob = knobImage;
 [[self controlView] lockFocus];
 [knob
 compositeToPoint:NSMakePoint(knobRect.origin.x,knobRect.origin.y+knobRect.size.height)
 operation:NSCompositeSourceOver];

[[self controlView] unlockFocus];
}
- (void)drawBarInside:(NSRect)rect flipped:(BOOL)flipped {
rect.size.height = 8;

    NSRect leftRect = rect;
    leftRect.origin.x=0;
    leftRect.origin.y=2;
    leftRect.size.width = knobrect.origin.x + (knobrect.size.width);
    [leftBarImage setSize:leftRect.size];
    [leftBarImage drawInRect:leftRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction:1];

    NSRect rightRect = rect;
    rightRect.origin.x=0;
    rightRect.origin.y=2;
    rightRect.origin.x = knobrect.origin.x;
    [rightBarImage setSize:rightRect.size];
    [rightBarImage drawInRect:rightRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction:1];
}

ah i'm so close. any help regarding as to why this happens and how to solve it will be greatly appreciated, thanks!

+1  A: 

Remove all the -lockFocus and -unlockFocus messages. The framework will take care of setting up the drawing context for you before -drawBarInside:flipped: or -drawKnob: are ever sent.

Also, you shouldn't be creating any objects within a draw method.

NSResponder
Hey thanks NSResponder, i've removed the lock and unlocked and also removed most of the creating object code. however the problem still persists. i'm currently drawing 3 images, left bar, right bar and the knob. It only happens when I move the mouse quickly. If i drag it slow then it draws ok.
han
Even when I use the private method (which I don't really like) and commented out the left drawing bar. it seems that the same problem still persists if i drag the knob from right to left quickly. And when i release the mouse, then the missing spaces disappear and a nice image is drawn over it.
han
+1  A: 

Ha, it's another story. No, NSResponder is right and you should remove all 'lockFocus' stuff, however, this issue is a result of the default slider bar drawn by the NSSliderCell somewhere outside of the drawBarInside:flipped:flipped. I have faced this issue not so far ago as well.

Here is discussion and solution: http://www.cocoabuilder.com/archive/cocoa/177288-preventing-nsslider-bar-from-drawing.html , in short, you can override whole drawCell:inView: or use a "dirty hack trick" with overriding a private method. I personally don't like hacks, but in this case I did

- (BOOL)_usesCustomTrackImage {
return YES;
}

And it solved the problem for me

Gobra
Hey Gobra, yea i've seen your post before. I've tried that method but it still doesnt work unfortunately :( btw the left side is an image, not the default bar. It was a random image that I grabbed off my computer for testing purposes.
han
Oh yea, and even if the bar is there, my images seem to be drawn over it and it doesnt really show the bar.
han