NSImageCell and NSImageWell

Hello,

I have a question about NSImageCell. It can display a NSImage, but there is no way to edit it. Other cells allows modification of their contents (you can edit text, check controls and so on) but you cannot edit (copy, paste) a NSImageCell.

If you drag and drop an image on a NSImageWell, everything is fine. Why can’t it be the same with NSImageCell?

Of course my column is editable, my cell is editable too in IB. What am I missing?

Regards,

I believe you need to play around with Core Graphics, as NSImage is immutable. NSIMageWell are just one way to display an image, and not an image editor, as far as I know.

There is some nice things in core graphics about image manipulation, but don’t expect Photoshop-level capabilities.

Cheers

Hi leonsimard,

Thank you for lending me these abilities (or at least these projects) ! I am still very far from playing with Core Graphics, I would just be able to click the cell and paste the image, or drag and drop. In fact, to do with my cell with the same thing as with a NSImageWell.

So what? Create a subclass of NSActionCell? I do not want to turn my tableView into a view-based one.

Er. When something is not implemented in cocoa, this means usually that I want something weird. Is is?

Regards,

No, not something weird, something custom. Quite different!

This is why you can subclass classes. To make them do something it doesn’t do ordinarily.

But if I understand correctly you want to have a nscell inside a table view that you can drag and drop images in and will place it inside the nscell?

Why don’t you use a drag and drop and use the replace option and then you get the URL of the image that was dropped onto the row and replace the nscell’s contents?

What have you tried so far?

That’it!

I felt a bit discouraged after trying every combination of settings in IB – and after reading a lot about drag and drop :confused:

For the moment I’m wrestling with a custom NSFormatter (for another part of the app) and feeling even more discouraged :smiley:

I don’t think you can achieve what I was talking about in IB. You need to do this programmatically.

ASOC or cocoa?

Cocoa. And by the way, what wrong with this *** formatter? It is supposed to accept only a single word in a TextField.

What is this thing called NSCFString subtly disguised as an obj, I cannot get anything useful from it: return obj, NSString stringWithString: obj, [obj stringValue]. only errors or a wiped out text field.
@end

Don’t see anything wrong with your custom formatter, but this is not one of my strenghts i must admit.

BTW, you don’t need to alloc init a new array for the following code, it already returns an NSarra:

{
    NSArray *arr = [[NSArray alloc]initWithArray:[*partialString componentsSeparatedByString:@" "]];
   if([arr count] >1) return NO;
    return YES;
}

I would phrase it like this instead:

{
    NSArray *arr = [partialString componentsSeparatedByString:@" "];
    if ( [arr count] > 1 ) { return NO };
    return YES;
}

As for the drag and drop part, here’s a starter:

In the applicationWillFinishLaunching part:

    [theTableView setDataSource:(id<NSTableViewDataSource>) self];
    [theTableView registerForDraggedTypes:[NSArray arrayWithObject:@"public.file-url"]];
    [theTableView setDraggingDestinationFeedbackStyle:NSTableViewDraggingDestinationFeedbackStyleNone];

Replace “NSTableViewDraggingDestinationFeedbackStyleNone” with “NSTableViewDraggingDestinationFeedbackStyleSourceList” to get the “replace” feedback to the user.

And for the dragging part:

- (NSDragOperation)tableView:(NSTableView *)aTableView validateDrop:(id < NSDraggingInfo >)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)operation
{
    NSPasteboard* pasteBoardContent = [info draggingPasteboard];
    NSArray* acceptedTypes = [NSArray arrayWithObject:(NSString*)kUTTypeFolder];
    NSArray* pasteboardURLs = [pasteBoardContent readObjectsForClasses:[NSArray arrayWithObject:[NSURL class]]
                                                                                           options:[NSDictionary dictionaryWithObject:acceptedTypes 
                                                                                            forKey:NSPasteboardURLReadingContentsConformToTypesKey]];

    //This returns the mouse cursor you want to give as feedback to the user when they drag something on the table view.
    if (pasteboardURLs.count > 0) {
        return NSDragOperationCopy;
    } else {
        return NSDragOperationNone;
    }
}

- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id < NSDraggingInfo >)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation
{
    NSPasteboard* pasteBoardContent = [info draggingPasteboard];
    NSArray* acceptedTypes = [NSArray arrayWithObject:(NSString*)kUTTypeFolder];
    NSArray* pasteboardURLs = [pasteBoardContent readObjectsForClasses:[NSArray arrayWithObject:[NSURL class]]
                                                                                           options:[NSDictionary dictionaryWithObject:acceptedTypes 
                                                                                            forKey:NSPasteboardURLReadingContentsConformToTypesKey]];

    if (pasteboardURLs.count > 0) {
        //Do something with the URLS in pasteboardURLs
        //not forgetting the row value to know which row to replace in your array controller's arranged objects.
    }
    return YES;
}

You can replace NSDragOperationCopy by any other kind of cursor, just check the docs for “NSDraggingInfo Protocol Reference”. Also, you can replace kUTTypeFolder by any other types, just check the docs for “UTType Reference”.

Helps?

It scares.

You mean, this is what a NSImageWell does for us? Well, drag and drop is really not intuitive.

Thank you for the code! I can’t just copy/paste it without understanding it.

Well, i cant promise it’ll work as is, you’ll have to do some adjustments to fit your needs. But this is what I is. It’s fairly logical, just read thru the whole thing slowly, you’ll see the logic of it.

Good luck!