NSPopUpButtonCell within NSTextFieldCell: disappearing text
Christiaan Hofman
cmhofman at gmail.com
Mon Aug 6 05:38:57 PDT 2007
My guess is actually that you forgot to override selectWithFrame:...
and editWithFrame:... This does everything you asked for:
@implementation MyPopUpTextFieldCell
- (id)initTextCell:(NSString *)aString {
if (self = [super initTextCell:aString]) {
popUpCell = [[NSPopUpButtonCell alloc] initTextCell:@""
pullsDown:NO];
[popUpCell setBordered:NO];
[popUpCell addItemsWithTitles:[NSArray
arrayWithObjects:@"One", @"Two", @"Three", nil]];
[popUpCell selectItemAtIndex:0];
}
return self;
}
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
if (popUpCell == nil) {
popUpCell = [[NSPopUpButtonCell alloc] initTextCell:@""
pullsDown:NO];
[popUpCell setBordered:NO];
[popUpCell addItemsWithTitles:[NSArray
arrayWithObjects:@"One", @"Two", @"Three", nil]];
[popUpCell selectItemAtIndex:0];
}
}
return self;
}
- (id)copyWithZone:(NSZone *)aZone {
MyPopUpTextFieldCell *copy = [super copyWithZone:aZone];
copy->popUpCell = [popUpCell copyWithZone:aZone];
return copy;
}
- (void)dealloc {
[popUpCell release];
[super dealloc];
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSRect popUpRect, textRect;
NSDivideRect(cellFrame, &popUpRect, &textRect, POPUP_WIDTH,
NSMinXEdge);
[super drawWithFrame:textRect inView:controlView];
[popUpCell drawWithFrame:popUpRect inView:controlView];
}
- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView
editor:(NSText *)textObj delegate:(id)anObject start:(int)selStart
length:(int)selLength {
NSRect popUpRect, textRect;
NSDivideRect(aRect, &popUpRect, &textRect, POPUP_WIDTH,
NSMinXEdge);
[super selectWithFrame:textRect inView:controlView
editor:textObj delegate:anObject start:selStart length:selLength];
}
- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView
editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)
theEvent {
NSRect popUpRect, textRect;
NSDivideRect(aRect, &popUpRect, &textRect, POPUP_WIDTH,
NSMinXEdge);
[super editWithFrame:textRect inView:controlView editor:textObj
delegate:anObject event:theEvent];
}
- (BOOL)trackMouse:(NSEvent *)theEvent inRect:(NSRect)cellFrame
ofView:(NSView *)controlView untilMouseUp:(BOOL)untilMouseUp {
NSPoint point = [controlView convertPoint:[theEvent
locationInWindow] fromView:nil];
NSRect popUpRect, textRect;
NSDivideRect(cellFrame, &popUpRect, &textRect, POPUP_WIDTH,
NSMinXEdge);
if (NSPointInRect(point, popUpRect))
return [popUpCell trackMouse:theEvent inRect:popUpRect
ofView:controlView untilMouseUp:untilMouseUp];
return NO;
}
@end
Christiaan
On 6 Aug 2007, at 1:19 PM, Michael Dupuis wrote:
>
> --- Alan Smith <alanrogersmith at gmail.com> wrote:
>
>> I suggest not subclassing NSTextFieldCell but
>> NSCell. That way you
>> don't have to worry about overriding some of
>> NSTextFieldCell's
>> behavior. If you override NSCell just create a
>> NSTextFieldCell and
>> NSPopUpButtonCell as instance variables and draw
>> them in
>> drawWithFrame::
>
> Thanks Alan, that's pretty much what I wound up doing.
> I was hoping to use NSTextFieldCell to get a bunch of
> stuff "for free" but ultimately, it didn't work. I'd
> still be interested in the "why?" of it not working
> though.
>
> Michael
>
>
>
> ______________________________________________________________________
> ______________
> Got a little couch potato?
> Check out fun summer activities for kids.
> http://search.yahoo.com/search?fr=oni_on_mail&p=summer+activities
> +for+kids&cs=bz
> _______________________________________________
> MacOSX-dev mailing list
> MacOSX-dev at omnigroup.com
> http://www.omnigroup.com/mailman/listinfo/macosx-dev
More information about the MacOSX-dev
mailing list