如何在NSOutlineView的委托方法中获取NSButtonCell的标题

时间:2012-11-06 06:07:38

标签: objective-c cocoa nsoutlineview nsbuttoncell nscontrol

我正在使用NSButtonCell子类来显示同一单元格中的复选框,图像和标签。现在我想在双击或输入键上的outlineView中编辑标签 NSButtonCell子类:

- (void)dealloc {
    [image release];
    image = nil;
    [super dealloc];
}

-(id)copyWithZone:(NSZone *)zone {
    ImageAndTextAndCheckBox *cell = (ImageAndTextAndCheckBox *)[super copyWithZone:zone];
    cell->image = [image retain];
    return cell;
}

- (void)setImage:(NSImage *)anImage {
    if (anImage != image) {
        [image release];
        image = [anImage retain];
        [image setSize:NSMakeSize(kIconImageSize, kIconImageSize)];
    }
}

- (NSImage *)image {
    return image;
}


#define KNImageAndTextButtonCellPadding 5 // Distance between the end of the image and the start of the text.

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    // Draw the original button first, then the image.
    //shift check box to right
    NSRect newCellFrame = cellFrame;
    newCellFrame.origin.x =  newCellFrame.origin.x + 5;
    cellFrame = newCellFrame;

    [super drawWithFrame:cellFrame inView:controlView];

    if (image != nil) {

        NSRect imageFrame;

        NSSize imageSize = [image size];
        NSDivideRect(cellFrame, &imageFrame, &cellFrame, KNImageAndTextButtonCellPadding + imageSize.width, NSMinXEdge);

        imageFrame.origin.x += imageFrame.size.width;
        imageFrame.size = imageSize;

        if ([controlView isFlipped]) {
            imageFrame.origin.y += ceil((cellFrame.size.height + imageFrame.size.height) / 2);
        } else {
            imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2);
        }

        BOOL oldFlipped = [image isFlipped];
        [image setFlipped:![controlView isFlipped]];
        [image compositeToPoint:imageFrame.origin operation:NSCompositeSourceOver];
        [image setFlipped:oldFlipped];

    }   
}


-(BOOL)trackMouse:(NSEvent *)theEvent inRect:(NSRect)cellFrame ofView:(NSView *)controlView untilMouseUp:(BOOL)untilMouseUp {

    // Make sure that the mouse only gets tracked if it is clicked on the check box, otherwise 
    // the row will be checked/unchecked whenever the user clicks on any part of the cell (which is bad).
    if ([theEvent clickCount] > 1)
    {
        NSLog(@"do double-click action" );
    }
    else
    {
         NSLog(@"do single-click action" );
    }
    NSPoint event_location = [theEvent locationInWindow];
    NSPoint localPoint = [controlView convertPoint:event_location fromView:nil];
    //shift check box mouse event to right
    NSRect newCellFrame = cellFrame;
    newCellFrame.origin.x =  newCellFrame.origin.x + 5;
    cellFrame = newCellFrame;
    if (localPoint.x <= cellFrame.origin.x + 16) {
        return [super trackMouse:theEvent inRect:cellFrame ofView:controlView untilMouseUp:untilMouseUp];
    } else {
        return NO;
    }
}

-(NSRect)drawTitle:(NSAttributedString *)title withFrame:(NSRect)frame inView:(NSView *)controlView {

    // Adjust the rect so we don't interfere with the image's location

    if (image != nil) {
        //NSLog(@"orig %f %f %f %f", frame.size.width,frame.size.height,frame.origin.x,frame.origin.y);
        NSRect newFrame = NSOffsetRect(frame, [[self image] size].width + KNImageAndTextButtonCellPadding, 0);
        newFrame.size.width -= ([[self image] size].width + KNImageAndTextButtonCellPadding);
        //NSLog(@"drawTitle %f %f %f %f", newFrame.size.width,newFrame.size.height,newFrame.origin.x,newFrame.origin.y);
        return [super drawTitle:title withFrame:newFrame inView:controlView];

    } else {
        return [super drawTitle:title withFrame:frame inView:controlView];
    }

}
- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent {
    NSLog(@"%@", anObject);


//  id node = [anObject itemAtRow: [anObject selectedRow]];
//  int length = [[node relativePath] length];
    NSRect newFrame = NSOffsetRect(aRect,  KNImageAndTextButtonCellPadding+kIconImageSize+ 16, 0);
    newFrame.size.width -= (KNImageAndTextButtonCellPadding+kIconImageSize+ 16);
    [super editWithFrame:newFrame inView:controlView editor:textObj delegate:anObject event:theEvent];
}  

我总是在

中获得1(布尔值)
- (void)outlineView:(NSOutlineView *)ov     
     setObjectValue:(id)obj 
     forTableColumn:(NSTableColumn *)tableColumn 
             byItem:(id)item  

NSOutlineView委托方法。(因为NSButtonCell)但我想编辑标签。

  1. 如何编辑标签?
  2. 如何处理双击?
  3. 按下回车键前:
    enter image description here

  4. enter image description here 如何更改彩色文字?它显示白色。

    它应该像enter image description here(带复选框)

    有人可以帮帮我吗?

0 个答案:

没有答案