1

I am using NSButtonCell subclass to display checkbox , image and label in same cell. Now i want to edit label in outlineView on double click or on enter key.
NSButtonCell subclass:

- (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];
}  

I am getting always 1 (boolean value) in

- (void)outlineView:(NSOutlineView *)ov     
     setObjectValue:(id)obj 
     forTableColumn:(NSTableColumn *)tableColumn 
             byItem:(id)item  

NSOutlineView delegate method.(because of NSButtonCell) but i want to edit label.

  1. How can i edit label?
  2. How to handle double click ?
  3. Before pressing enter key:
    enter image description here

After
enter image description here How to change color text? its displaying white color.

it should be like enter image description here (with checkbox)

Can anyone please help me out?

Parag Bafna
  • 22,812
  • 8
  • 71
  • 144

0 Answers0