Created
August 14, 2010 19:19
-
-
Save davidcann/524620 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want the drag view to actually be moved to the drop view, then you can make these changes: