nstextfield subclass in nsslider subclass won't allow editing

  • 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
    <mhanna...>
    iChat: michaelkhanna(AIM)
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today