NSImageCells in NSMatrix - Retaining focus ring while redrawing a window
NSImageCells in NSMatrix - Retaining focus ring while redrawing a window
- Subject: NSImageCells in NSMatrix - Retaining focus ring while redrawing a window
- From: Nikita Zhuk <email@hidden>
- Date: Sat, 4 Oct 2003 12:41:09 +0300
Hi all,
I have instances of subclass of NSImageCell inside a subclass of
NSMatrix, and I would like to allow user to click a cell to activate
it, and draw a focus on the selected cell. Also, when user clicks
somewhere else (on other cell or on empty area of the matrix), focus
should be removed. I have done the focus-draw-on-click, but I'm having
some problems with focus ring. It dissapears when NSMatrix is redrawn
(for example, when window resizes or user minimizes and then maximizes
it). Also I don't want to allow user to click emtpy cells ( cells,
where [cell image] returns nil), but I can't figure it out how to do
this (I have tried all modes of NSMatrix, and NSRadioModeMatrix is
pretty close, but it stills highlights empty cells of the NSMatrix if
user clicks them). My subclass of NSMatrix is called MTMediaMatrix, and
subclass of NSImageCell is called MTMediaObject.
The focus in cell is drawn via overrided action method. Here is the
relevant code of MTMediaObject:
- (SEL)action
{
[self drawFocus];
return action;
}
- (void)drawFocus
{
NSRect cellRect;
int row, col;
if ([(MTMediaMatrix *)[self controlView] getRow:&row column: &col
ofCell:self])
{
// get the rect of self
cellRect = [(MTMediaMatrix *)[self controlView]
cellFrameAtRow:row column:col];
if ([self showsFirstResponder])
{
NSRect focusRingFrame = cellRect;
focusRingFrame.size.height -= 2.0;
[NSGraphicsContext saveGraphicsState];
NSSetFocusRingStyle(NSFocusRingOnly);
[[NSBezierPath bezierPathWithRect:
NSInsetRect(focusRingFrame,4,4)] fill];
[NSGraphicsContext restoreGraphicsState];
}
}
}
- (BOOL)becomeFirstResponder
{
// we can become active if image is not nil
return ([self image]) ? YES : NO;
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (BOOL)needsPanelToBecomeKey
{
return NO;
}
MTMediaMatrix is created in -(id)initWithFrame:(NSRect)frameRect:
- (id)initWithFrame:(NSRect)frameRect
{
// create the cell prototype and style it
id cell = [[MTMediaObject alloc] init];
[cell setImageAlignment:NSImageAlignCenter];
[cell setImageFrameStyle: NSImageFrameNone];
[cell setImageScaling:NSScaleProportionally];
[cell setEnabled:YES];
// create the actual matrix of NSImageCells
[super initWithFrame:frameRect
mode:NSRadioModeMatrix
prototype:cell
numberOfRows:0
numberOfColumns:5];
// put a white background behind cells
[self setDrawsBackground:YES];
[self setAutosizesCells:YES];
[self setAllowsEmptySelection:NO];
[self setBackgroundColor:[NSColor whiteColor]];
[self setCellSize:NSMakeSize(80,60)];
[self setIntercellSpacing:NSMakeSize(2,4)];
[cell release];
return self;
}
Any help would be highly appreciated.
--
Nikita Zhuk
Mac OS & Web Developer
http://www.maczsoftware.com/blogs/nzhuk/
email@hidden
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.