update: matt neuburg responded to today's post to cocoa-dev with the solution:

[[matrix currentEditor] setFrame: [matrix cellFrameAtRow:r column:c]];

Thank you Matt


This one's been bugging me for years and I think I've thrown evey method in the book at it but obviously I've missed one or it's a bug.

Given a matrix of selectable textFieldCells and a -windowWillResize delegate, if some text is selected that cell sticks at its position within the window when you resize it. It looks like this:

resizing - the movie (1.3MB)

Here is the Archived Xcode project (1.4MB).

The main source is below the mail.

By way of description here's the mail I sent to maxosx-dev@omnigroup.com (and some time later to cocoa-dev@lists.apple.com) to describe the problem.

Hello,

I have a simple test program that displays an NSMatrix with a row of NSTextFieldCell cells. I've set the cells selectable and told the matrix to setAutosizesCells. I have a windowWillResize delegate that adjusts the matrix size with -setFrame:.

If I select the text in one of the cells and resize the window, the cell with the selected text remains in the same position - all the other cells move horizontally as I would expect. What did I forget to do that causes the selected cell to be stuck at its window location? Should I be relocating the individual cells in my windowWillResize delegate rather than just resizing the matrix?

If someone recognizes what I'm talking about I'd be overjoyed to know what I'm doing wrong.

I've placed the project (about 50 lines of code) at <http://berdom.net/code/textfielder.zip> .

Thanks


#import "MyObject.h"

#define NCOLS	(10)

@implementation MyObject
- (id)init
{
    self = [ super init];
    return self;
}

- (void)awakeFromNib
{        
    NSRect frameRect = NSMakeRect(0, 0, 480, 48);
    NSWindow *theWindow = [[[ NSWindow alloc] initWithContentRect:frameRect
                      styleMask:(NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask)
                      backing:NSBackingStoreBuffered
                      defer:YES] retain];

    NSMatrix *newMatrix = [[[ NSMatrix alloc] initWithFrame:frameRect mode:NSRadioModeMatrix cellClass:[ NSTextFieldCell class] numberOfRows:1 numberOfColumns:NCOLS] autorelease];
    [ newMatrix setAutosizesCells:YES];
    [ newMatrix setCellSize:NSMakeSize(frameRect.size.width/NCOLS, frameRect.size.height)];
    
    int i;
    NSTextFieldCell *textCell;
    for (i = 0; i < NCOLS; i++) {
        textCell = [ newMatrix cellAtRow:0 column:i];
        [ textCell setSelectable: YES];
        [ textCell setStringValue:@"test text"];
        [ textCell setRefusesFirstResponder:YES];
    }
    [[ theWindow contentView] addSubview:newMatrix];
    [ theWindow center];
    [ theWindow setDelegate:self];
    [ theWindow makeKeyAndOrderFront:self];
}

- (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)proposedFrameSize
{
	[[[[ sender contentView] subviews] objectAtIndex:0] setFrame:NSMakeRect(0, 0, proposedFrameSize.width, (proposedFrameSize.height-22))];
	return proposedFrameSize;
}

@end



Wed, Aug 18, 2004