Why 1-pixel shift in drawing a clicked cell?
Why 1-pixel shift in drawing a clicked cell?
- Subject: Why 1-pixel shift in drawing a clicked cell?
- From: P Teeson <email@hidden>
- Date: Thu, 13 Apr 2006 09:31:45 -0400
Using Xcode 2.2.1 and Tiger 10.4.5
I have sub-classed NSMatrix and NSButton cell in order to have more
control over drawing.
The matrix contains a 3 x 3 array of the cells.
Right now I am stroking and filling the matrix rect and it is correct
when I resize or zoom the window.
Similarly for the cells when they are NOT clicked on; that is they
stroke correctly.
The problem is that when I do click on a cell it is stroked offset by
what looks like 1 pixel.
This is from the origin I calculated; e.g. I calc 4,4 but it seems to
draw at 3,3.
Any suggestions to track this down? Below are the methods I have
overriden:
Override of NSMatrix drawRect
- (void)drawRect:(NSRect) aRect {
// Note that NSMatrix uses flipped co-ordinates
NSGraphicsContext* theContext = [NSGraphicsContext currentContext];
[theContext saveGraphicsState];
[NSBezierPath setDefaultLineWidth:2.0];
[NSBezierPath strokeRect:aRect];
// Could use NSInsetRect(aRect,2,2); but why bother
aRect.origin.x +=2;
aRect.origin.y +=2;
aRect.size.width -=4;
aRect.size.height -=4;
[[NSColor blueColor] setFill];
// Could also do NSRectFill(aRect);
[NSBezierPath fillRect:aRect];
[theContext restoreGraphicsState];
[self drawCellAtRow:1 column:1]; // for testing
// [super drawRect:aRect];
}
Override NSButtonCell drawWithFrame
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
int tag = [self tag];
NSRect controlFrame = [controlView frame];
float controlWidth = controlFrame.size.width;
float controlHeight = controlFrame.size.height;
float interCellSpacing = 4.0;
// Adjust width and height to account for inset and inter cell spacing
cellFrame.size.width = (controlWidth - 4*interCellSpacing)/3;
cellFrame.size.height = (controlHeight - 4*interCellSpacing)/3;
// Adjust origin to account for inset and inter cell spacing
switch ( tag) {
case 0:
cellFrame.origin.x =interCellSpacing;
cellFrame.origin.y =interCellSpacing;
break;
// similar code snipped
default:
break;
}
[self drawInteriorWithFrame:cellFrame inView:controlView];
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)
controlView {
NSGraphicsContext* theContext = [NSGraphicsContext currentContext];
[theContext saveGraphicsState];
[NSBezierPath setDefaultLineWidth:1.0];
[[NSColor whiteColor] setStroke];
[NSBezierPath strokeRect:cellFrame];
[theContext restoreGraphicsState];
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden