Re: NSMatrix selections
Re: NSMatrix selections
- Subject: Re: NSMatrix selections
- From: John Pannell <email@hidden>
- Date: Sat, 16 Aug 2003 13:18:43 -0600
Hi Dave-
I have made such an application, and below are the pertinent code
sections. You can check out the app in action -
http://www.positivespinmedia.com/shareware/NetScrape
My subclass of NSImageCell (subclassed to add target/action and tag
features)
@interface PSMActionImageCell : NSImageCell {
int tag;
id target;
SEL action;
BOOL enabled;
}
- (id)target;
- (void)setTarget:(id)anObject;
- (SEL)action;
- (void)setAction:(SEL)aSelector;
- (int)tag;
- (void)setTag:(int)value;
@end
@implementation PSMActionImageCell
- (id)target
{
return target;
}
- (void)setTarget:(id)anObject
{
[anObject retain];
[target release];
target = anObject;
}
- (SEL)action
{
return action;
}
- (void)setAction:(SEL)aSelector
{
action = aSelector;
}
- (int)tag
{
return tag;
}
- (void)setTag:(int)value
{
tag = value;
}
- (void)dealloc
{
[self setAction:nil];
[self setTarget:nil];
[super dealloc];
}
@end
My subclass of NSMatrix (to handle resizing and style it correctly)
@interface ThumbnailMatrix : NSMatrix
{
}
// override so that I can resize the cells myself
- (void)resizeWithOldSuperviewSize:(NSSize)oldFrameSize;
- (void)viewDidEndLiveResize;
@end
@implementation ThumbnailMatrix
- (id)initWithFrame:(NSRect)frameRect
{
// create the cell prototype and style it
id cell = [[PSMActionImageCell alloc] init];
[cell setImageAlignment:NSImageAlignCenter];
[cell setImageFrameStyle:NSImageFrameGrayBezel];
[cell setImageScaling:NSScaleProportionally];
[cell setEnabled:YES];
// create the actual matrix of buttons
[super initWithFrame:frameRect
mode:NSListModeMatrix
prototype:cell
numberOfRows:0
numberOfColumns:4];
// put a white background behind cells
[self setDrawsBackground:YES];
[self setBackgroundColor:[NSColor whiteColor]];
// configure the size and spacing of buttons
// use the width of the superview (the scroller)
[self setCellSize:NSMakeSize(100,100)];
[self setIntercellSpacing:NSMakeSize(0,0)];
[cell release];
return self;
}
- (void)resizeWithOldSuperviewSize:(NSSize)oldFrameSize
{
// this gets called when the superview is being resized
// I want the matrix to grow and shrink based on the width of the
superview,
// with no regard to the height.
/*
int cellWidth = (int)(oldFrameSize.width / 4.0);
[self setCellSize:NSMakeSize(cellWidth,cellWidth)];
[self sizeToCells];
[self setNeedsDisplay];
*/
// this was too slow in real life...
}
- (void)viewDidEndLiveResize
{
// called when the resize is over
// want to set width to 1/4 of the parent view
int cellWidth = (int)([[self superview] frame].size.width / 4.0);
[self setCellSize:NSMakeSize(cellWidth,cellWidth)];
[self sizeToCells];
[self setNeedsDisplay];
}
@end
Your code probably looks a lot like this; from your description it
sounds like things *should* be working properly. I hope this helps
somehow...
John
On Saturday, Aug 16, 2003, at 12:12 US/Mountain, Dave Riggle wrote:
>
I'm a complete Cocoa newbie. I'm building an iPhoto like app. From
>
reading the archives, it sounds like other people have been doing the
>
same thing.
>
>
I have my own subclass of NSImageCell inside an NSMatrix inside an
>
NSScrollView inside an NSWindow. I have a controller object that is a
>
delegate for the window, and it "renews" the matrix when the window
>
size changes. That part is working nicely. In Interface Builder I
>
instantiated my controller, linked the window delegate, scroll view
>
and matrix to to my controller. I also set a few options for my cells
>
and matrix. That's all I did in IB.
>
>
My problem is: I can't get selection to work. I'm using the
>
NSImageFrameGrayBezel frame style with List selection mode. The
>
matrix always has the first cell selected when I load it. If I click
>
cells that are partially shown, the matrix scrolls up to show them, so
>
I know clicks are getting through, but the cells don't get selected.
>
If I hit the page down key, a random cell gets selected and then the
>
matrix scrolls down a page. What's going on?
>
>
Any tips or code snippets from other people building apps like mine
>
would be greatly appreciated!
>
>
Dave
>
_______________________________________________
>
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.
_______________________________________________
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.