nstextfield subclass in nsslider subclass won't allow editing
nstextfield subclass in nsslider subclass won't allow editing
- Subject: nstextfield subclass in nsslider subclass won't allow editing
- From: Mike Hanna <email@hidden>
- Date: Sun, 30 Sep 2007 00:28:30 -0700
I've subclassed nsslider and I've added an NSTextField subclass(let's
call it MyTextField) drawn to the right end of the slider.
MyTextField will display the value of the slider, and the user can
enter a value to adjust the model and subsequently the slider knob
position.
The problem I'm having is that [MyTextField -setEditable] and
[[MyTextField cell] -setEditable] seem to have no effect. When I
click on the textfield, no insertion bar appears. Also, it doesn't
ever become firstresponder(tabbing from another nstextfield in the
same window has no effect).
This is how I'm initializing the textfield and cell:
MySlider.m:
#define LABEL_CELL_W (34.0f) // default label cell width
#define VALUE_CELL_W (55.0f) // default label cell width
#define FONT_SIZE (8.5f)
@implementation MySlider
[....]
- (id)initWithCoder:(NSCoder *)decoder {
self=[super initWithCoder: decoder];
int ticks=[self numberOfTickMarks];
double max=[self maxValue];
double min=[self minValue];
BOOL allowsTickMarkValuesOnly=[self allowsTickMarkValuesOnly];
[self setup];
// value field settings
[self setHasValueField:YES];
[self setValueFieldWidth:VALUE_CELL_W];
// default valueField settings
[[valueField cell] setAlignment:NSRightTextAlignment];
[[valueField cell] setBordered:NO];
[[valueField cell] setBezeled:NO];
[[valueField cell] setEditable:YES];
[[valueField cell] setBackgroundColor:[NSColor blackColor]];
[[valueField cell] setFont:[NSFont systemFontOfSize:9.0f]];
[[valueField cell] setTextColor:[NSColor whiteColor]];
[valueField setStringValue:@"foo"];
NSText * fieldEditor = [[self window] fieldEditor:YES
forObject:valueField];
NSLog(@"fieldEditor %@", fieldEditor);
[[self window] setNextResponder:valueField];
NSLog(@"[valueField isEditable] %d", [valueField isEditable]);
NSLog(@"[[valueField cell] isEditable] %d", [[valueField cell]
isEditable]);
return self;
}
- (void) setup {
[self setCell: [[self cellClass] new]];
minCell = [[NSTextFieldCell alloc] init];
maxCell = [[NSTextFieldCell alloc] init];
valueField = [[InspectorTextField alloc] initWithFrame:NSMakeRect
([self frame].size.width - VALUE_CELL_W, 6.0f, 35.0f, [self
bounds].size.height)]; // FIXME: temporary, use more general draw
code at some point
[valueField setEditable:YES];
scrollTime = 0.0f;
}
-(void)drawRect:(NSRect)rect
{
[....]
NSRect valueFieldCellRect = NSMakeRect([self frame].size.width -
[self valueFieldWidth], 6.0f, 35.0f, [self bounds].size.height);
[[valueField cell] drawWithFrame:valueFieldCellRect inView:self];
// draw the slider
NSRect sliderRect = NSMakeRect( ([self bounds].origin.x), [self
bounds].origin.y, [self bounds].size.width - [self valueFieldWidth],
[self bounds].size.height);
[[self cell] drawWithFrame:sliderRect inView:self];
[.....]
}
OUTPUT:
2007-09-29 23:13:31.669 Prog[6109] fieldEditor (null)
2007-09-29 23:13:31.669 Prog[6109] [valueField isEditable] 1
2007-09-29 23:13:31.669 Prog[6109] [[valueField cell] isEditable] 1
the textfield draws the word "foo" as expected.
mh
--
Mike Hanna
email@hidden
iChat: michaelkhanna(AIM)
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden