Re: Setting origins for NSMatrix cells
Re: Setting origins for NSMatrix cells
- Subject: Re: Setting origins for NSMatrix cells
- From: P Teeson <email@hidden>
- Date: Sat, 8 Apr 2006 12:18:05 -0400
On 7-Apr-06, at 11:59 PM, John C. Randolph wrote:
On Apr 7, 2006, at 8:06 PM, P Teeson wrote:
Using Mac OS X 10.4.5, XCode 2.2.1.
In my app I have an NSMatrix subclass with overridden methods
initWithFrame and drawRect.
The cell class is a subclass of NSButtonCell
It's overridden method is drawWithFrame (working with flipped co-
ordinates although I never set
that in any graphics context).
There are 9 cells in a 3 by 3 array in the matrix view.
My problem is that the cellFrames passed in to this method have
origins that are not what I want.
For example the first cell is at 0,0 when I really want it to be
inset from the controlView frame by
a few pixels - e.g. at 4,4.
Now of course I can write code to adjust the cellFrame rects to
meet my needs.
But this seems excessive to me when some method somewhere is
already computing the cellFrame.
My guess is that this is happening somewhere in NSMatrix or it's
ancestors but so far
I have not been able to track it down.
Any suggestions would be gratefully received.
Subclass NSMatrix, and override -cellFrameAtRow:colum: to return
whatever rect you want. You probably also want to override -
getRow:column:forPoint:
As I mentioned in the first sentence I am already subclassing NSMatrix.
Following your suggestion (thanks btw) it seems cellFrameAtRow:column
is invoked only if a cell is clicked on by the mouse.
Zooming or resizing the window does not invoke it.
FWIW here is the code I have written for my overrides 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];
NSRectFill(aRect);
[theContext restoreGraphicsState];
[super drawRect:aRect];
}
and
NSButtonCell drawWithFrame:inView
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
[super drawWithFrame:cellFrame inView:controlView];
// NSGraphicsContext* theContext = [NSGraphicsContext currentContext];
// [theContext saveGraphicsState];
// NSBezierPath* thePath = [NSBezierPath bezierPathWithRect:cellFrame];
// [thePath setLineWidth:1.0];
// [thePath stroke];
// [theContext restoreGraphicsState];
}
respect...
Peter
_______________________________________________
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