I need to create a couple of radio buttons programatically.

So I create an NSMatrix whose cell is an NSButtonCell. Everything works fine except that when the window is drawn, the system seems to think that the cell is only 54 points wide when in fact its size.width is > 54. Thus, the title of the radio button is clipped at its right edge.

The superview of the NSMatrix is a custom subview which is 250 points wide. I have verified this by overriding its -drawRect: to fill the rect with yellow and indeed I get a yellow box which is 250 wide.

By similarly overriding -drawWithFrame:inView: of the button cell I verify that that the cell's size.width is 71 points, but when this method is invoked, the rect argument it gets from Cocoa never extends beyond x=54.

Why won't it draw the remainder of my cell?

*** TEST CODE:

@interface KookyButtonCell : NSButtonCell
@end

@implementation KookyButtonCell

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    NSLog(@"cellFrame is %@", NSStringFromSize([self cellSize])) ;
NSLog(@"drawing cell '%@' in rect %@", [self title], NSStringFromRect(cellFrame)) ; NSLog(@"controlView: %@ %@", controlView, NSStringFromRect([controlView frame])) ;
    NSLog(@"control's superview: %@ %@",
          [controlView superview],
          NSStringFromRect([[controlView superview] frame])) ;
    ...
}

@end

*** CONSOLE OUTPUT FROM TEST CODE:

[67003:10b] cellFrame is {71, 18}
[67003:10b] drawing cell 'New File' in rect {{0, 0}, {54, 18}}
[67003:10b] controlView: <NSMatrix: 0x165fa910> {{0, 0}, {250, 18}}
[67003:10b] control's superview: <SSYLabelledRadioButtons: 0x1700be40> {{112, 196}, {250, 32}}

The above is repeated whenever the window is redrawn, sometimes more than once, but always with the same numbers.


*** CODE THAT CREATES THE MATRIX:

NSButtonCell* cell = [[KookyButtonCell alloc] initTextCell:@"foo"] ;
[cell setButtonType:NSRadioButton] ;
NSMatrix* matrix_ ;

matrix_ = [[NSMatrix alloc] initWithFrame:NSZeroRect
                                     mode:NSRadioModeMatrix
                                prototype:cell
                             numberOfRows:[choices count]
                          numberOfColumns:1] ;
[cell release] ;
[matrix_ setLeftEdge:0.0] ;
[matrix_ setBottom:0.0] ;
NSInteger i = 0 ;
CGFloat width = 0 ;
for (NSString* title in choices) {
    NSButtonCell* cell = [matrix_ cellAtRow:i
                                     column:0] ;
    [cell setTitle:title] ;
    // I added the following in order to try and fix
    // this problem, but it doesn't help
    width = MAX(width, [cell cellSize].width) ;
    i++ ;
}
[matrix_ sizeToCells] ;

// The following lines are in case -sizeToCells doesn't work.
// They have no effect.
NSRect frame = [matrix_ frame] ;
frame.size.width = width ;
NSLog(@"1688: frame = %@", NSStringFromRect(frame)) ;
[matrix_ setFrame:frame] ;

[self setMatrix:matrix_] ;
[self addSubview:matrix_] ;

// More logging
NSLog(@"2789: matrix frame = %@", NSStringFromRect([[self matrix] frame])) ;
NSCell* cell = [[self matrix] cellAtRow:0 column:0];
NSLog(@" Title: '%@' %@", [cell title], NSStringFromSize([cell cellSize])) ;

[matrix_ release] ;

*** CONSOLE OUTPUT FROM CREATING THE MATRIX

[66817:10b] 1688: frame = {{0, 0}, {71, 18}}
[66817:10b] 2789: matrix frame = {{0, 0}, {71, 18}}
[66817:10b]   Title: 'New File' {71, 18}
[66817:10b]   (null) {0, 0}

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to