Skip to content

Instantly share code, notes, and snippets.

@davidcann
Created August 14, 2010 19:19
Show Gist options
  • Save davidcann/524620 to your computer and use it in GitHub Desktop.
Save davidcann/524620 to your computer and use it in GitHub Desktop.
// example of result (including comment changes): http://davidcann.com/cappuccino-cpview-drag-drop/
// ****************** AppController.j *********************
@import <Foundation/CPObject.j>
@import "DCDragView.j"
@import "DCDropView.j"
@implementation AppController : CPObject {
}
- (void)applicationDidFinishLaunching:(CPNotification)aNotification {
var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask],
contentView = [theWindow contentView];
var dragView = [[DCDragView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[dragView setBackgroundColor:[CPColor blueColor]];
[contentView addSubview:dragView];
var dropView = [[DCDropView alloc] initWithFrame:CGRectMake(400, 100, 100, 100)];
[dropView setBackgroundColor:[CPColor redColor]];
[dropView registerForDraggedTypes:["FileDragType"]];
[contentView addSubview:dropView];
[theWindow orderFront:self];
}
@end
// ***************** Starting the Drag ********************
@implementation DCDragView : CPView {
CPEvent mouseDownEvent;
}
- (void)mouseDown:(CPEvent)anEvent {
mouseDownEvent = anEvent;
}
- (void)mouseDragged:(CPEvent)anEvent {
[[CPPasteboard pasteboardWithName:CPDragPboard] declareTypes:["FileDragType"] owner:self];
var view = [[CPView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[view setBackgroundColor:[CPColor greenColor]];
[view setAlphaValue:0.5];
// there's also a dragImage:... method. See the CPView documentation for details.
[self dragView:view
at:CGPointMake(0, 0)
offset:CGSizeMakeZero()
event:mouseDownEvent
pasteboard:nil
source:self
slideBack:YES];
}
- (void)pasteboard:(CPPasteboard)aPasteboard provideDataForType:(CPString)aType {
if (aType == "FileDragType") {
var myData = [[CPData alloc] initWithString:"Hello, this is my data. It could be an image, URL, CPObject, etc. as long as it's encoded as a CPData object."];
[aPasteboard setData:myData forType:aType];
}
}
@end
// ***************** Receiving the Drop *******************
@implementation DCDropView : CPView {
CPEvent mouseDownEvent;
}
- (void)performDragOperation:(CPDraggingInfo)aSender {
var data = [[aSender draggingPasteboard] dataForType:"FileDragType"];
alert("dropped data = "+ [data rawString]);
}
- (void)draggingEntered:(CPDraggingInfo)aSender {
// change the view's style
}
- (void)draggingExited:(CPDraggingInfo)aSender {
// change the view's style back to normal
}
@end
@davidcann
Copy link
Author

If you want the drag view to actually be moved to the drop view, then you can make these changes:


// DCDragView.j

- (void)mouseDragged:(CPEvent)anEvent {
    [[CPPasteboard pasteboardWithName:CPDragPboard] declareTypes:["FileDragType"] owner:self];

    var viewData = [CPKeyedArchiver archivedDataWithRootObject:self];
    var view = [CPKeyedUnarchiver unarchiveObjectWithData:viewData];

    [self dragView:view
        at:CGPointMake(0, 0)
        offset:CGSizeMakeZero()
        event:mouseDownEvent
        pasteboard:nil
        source:self
        slideBack:YES];

    [self setHidden:YES];
}

- (void)pasteboard:(CPPasteboard)aPasteboard provideDataForType:(CPString)aType {
    if (aType == "FileDragType") {
        var myData = [CPKeyedArchiver archivedDataWithRootObject:self];
        [aPasteboard setData:myData forType:aType];
    }
}

- (void)draggedView:(CPImage)aView endedAt:(CGPoint)aLocation operation:(CPDragOperation)anOperation {
    if (anOperation == CPDragOperationNone) {
        // didn't drop on a target
        [self setHidden:NO];
    } else {
        // dropped on a target
        //[self removeFromSuperview];
    }
}

// DCDropView.j

- (void)performDragOperation:(CPDraggingInfo)aSender {
    var data = [[aSender draggingPasteboard] dataForType:"FileDragType"];
    var myView = [CPKeyedUnarchiver unarchiveObjectWithData:data];
    [myView setFrame:CGRectMake(
        ([self frame].size.width / 2) - 25, 
        ([self frame].size.height / 2) - 25,
        50, 50)];
    [myView setHidden:NO];

    alert("dropped data = "+ myView);

    [self addSubview:myView];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment