Re: IB and image drag and drop
Re: IB and image drag and drop
- Subject: Re: IB and image drag and drop
- From: Vince DeMarco <email@hidden>
- Date: Thu, 6 Sep 2001 14:31:58 -0700
On Thursday, September 6, 2001, at 01:57 pm, Robert Miller wrote:
Hello,
Can anyone tell me how to create an IB palette widget that accepts a
dropped image (similar to a control) and then addopts the image and
displays it within IB such that when actually instantiated the image is
displayed on the screen when running ? Basically the behavior of a
control except in a custom subclass of NSView that contains an NSImage
or NSMovie ?
Look at the busy palette example (which will be updated with this stuff
soon)
You register an object with NSView as a resourcedraggingdelegate
like this
[NSView registerViewResourceDraggingDelegate:self];
Here is a sample implementation of the view resource dragging code
/*
* Implementation functions for IBViewResourceDraggingDelegates protocol
*/
/*
* This method will be called by InterfaceBuilder to determine which
drag
* types our drag delegate is responsible for.
*/
- (NSArray *)viewResourcePasteboardTypes
{
return [NSArray arrayWithObject:BPCellPboardType];
}
/*
* InterfaceBuilder will call this method to determine if object
* should accept a drag at the current point. In this example,
* our drag delegate knows how to deposit cells on NSTableColumns.
*/
- (BOOL)acceptsViewResourceFromPasteboard:(NSPasteboard *)pasteboard
forObject:(id)object atPoint:(NSPoint)point
{
return [object isKindOfClass: [NSTableHeaderView class]];
}
/*
* We have accepted the drag, so now our delegate must do the work of
* depositing the cell on the object.
*/
- (void)depositViewResourceFromPasteboard:(NSPasteboard *)pasteboard
onObject:(id)object atPoint:(NSPoint)point
{
id <IBDocuments> document;
NSArray* pasteboardObjects;
document = [NSApp documentForObject:object];
pasteboardObjects = [document pasteType:BPCellPboardType
fromPasteboard:pasteboard parent:object];
if ([pasteboardObjects count] > 0) {
NSCell* cell;
cell = [pasteboardObjects objectAtIndex:0];
if ([object respondsToSelector: @selector(setDataCell:)]) {
[object setDataCell: cell];
}
}
}
/*
* If we can accept the drag, should a drop target ring be
* drawn around the view? In our case, yes.
*/
- (BOOL)shouldDrawConnectionFrame
{
return YES;
}
//
The pasteboard type for images and sounds is just the standard file name
pasteboard type.
The only funky part is that you will have to handle the case were the
image is removed from PB, we have code in IB that converts the image that
is displayed to a broken image. So the easiest thing for you to do is if
you can't find that image anymore, just clear it out.
Keep an eye on the IB examples in /Developer/Examples/InterfaceBuilder
BusyPalette
and ProgressView.
Both are being updated every release to show how to do more and more in
your own palettes.
And no its not a black art, There should be enough examples to cover
almost everything you could want to do in a palette.
vince