Strange NSCell Editing (was Re: Why does this leak memory?)
Strange NSCell Editing (was Re: Why does this leak memory?)
- Subject: Strange NSCell Editing (was Re: Why does this leak memory?)
- From: Matt Ball <email@hidden>
- Date: Mon, 11 Jul 2005 15:52:19 -0400
I have a custom NSTextFieldCell subclass which displays an image and
some text. Up until now, the editing has worked as expected; when I
double click the cell, the Field Editor appears and is resized and
relocated so that it does not overlap the image.
However, when I went through my code to stop all my memory leaks, I
implemented the cell's copyWithZone: method, and now my cell's editing
behavior is all screwed up. When I double click on the cell, the
cell's background turns white (and if I click into another window, I
get the black outline that signifies that it is a field editor, not
just some color problem), and the text highlights. Additionally, I can
not edit the text or change which text is highlighted. Interestingly,
the white background is behind the image, so the field editor seems to
be getting partially repositioned. If any has any ideas of how to fix
this, I would be extremely grateful. Here is some of my custom
NSCell's code:
- (id)copyWithZone:(NSZone *)zone
{
LayerCell *copy = [[[self class] alloc] init];
[copy setImage: [self image]];
return copy;
}
// Reset the text color to account for the cell being selected
- (NSText *)setUpFieldEditorAttributes:(NSText *)textObj
{
[self setTextColor:[NSColor textColor]];
return textObj;
}
// Modify the editing frame to compensate for the thumbnail image
- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView
editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent
*)theEvent
{
if([self image] != nil) {
NSRect textFrame, imageFrame;
NSDivideRect (aRect, &imageFrame, &textFrame, 10 + [[self image]
size].width, NSMinXEdge);
textFrame.origin.y = NSMinY(textFrame) + textFrame.size.height/4 + 2;
textFrame.size.height = 18;
[super editWithFrame:textFrame inView:controlView editor:textObj
delegate:anObject event:theEvent];
}
else {
[super editWithFrame:aRect inView:controlView editor:textObj
delegate:anObject event:theEvent];
}
}
- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView
editor:(NSText *)textObj delegate:(id)anObject start:(int)selStart
length:(int)selLength {
if([self image] != nil) {
NSRect textFrame, imageFrame;
NSDivideRect (aRect, &imageFrame, &textFrame, 10 + [[self image]
size].width, NSMinXEdge);
textFrame.origin.y = NSMinY(textFrame) + textFrame.size.height/4 + 2;
textFrame.size.height = 18;
[super selectWithFrame:textFrame inView:controlView editor:textObj
delegate:anObject start:selStart length:selLength];
}
else {
[super selectWithFrame:aRect inView:controlView editor:textObj
delegate:anObject start:selStart length:selLength];
}
}
// Draw the cell
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
// Draw the thumbnail image
if (image != nil) {
NSSize imageSize;
NSRect imageFrame;
imageSize = [[self image] size];
NSDivideRect(cellFrame, &imageFrame, &cellFrame, 10 +
imageSize.width, NSMinXEdge);
if ([self drawsBackground]) {
[[self backgroundColor] set];
NSRectFill(imageFrame);
}
imageFrame.origin.x += 3;
imageFrame.size = imageSize;
imageFrame.origin.y = NSMinY(cellFrame) + (cellFrame.size.height -
[[self image] size].height)/2;
[image drawInRect:imageFrame fromRect:NSMakeRect(0, 0, [[self image]
size].width, [[self image] size].height)
operation:NSCompositeSourceOver fraction:1.0];
}
// Modify the cell frame to compensate for the image
cellFrame.origin.y = NSMinY(cellFrame) + cellFrame.size.height/4 + 2;
if([self isHighlighted])
[self setTextColor:[NSColor alternateSelectedControlTextColor]];
else
[self setTextColor:[NSColor textColor]];
// Draw the cell's text normally
[super drawWithFrame:cellFrame inView:controlView];
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden