Skip to content

Instantly share code, notes, and snippets.

@nicoclavier
Created November 24, 2009 09:30
Show Gist options
  • Select an option

  • Save nicoclavier/241760 to your computer and use it in GitHub Desktop.

Select an option

Save nicoclavier/241760 to your computer and use it in GitHub Desktop.
/*
* AppController.j
* DragDrop
*
* Created by You on November 23, 2009.
* Copyright 2009, Your Company All rights reserved.
*/
@import <Foundation/CPObject.j>
@import <AppKit/CPView.j>
CPDraggedDragType = "CPDraggedDragType";
@implementation AppController : CPObject
{
@outlet CPWindow theWindow; //this "outlet" is connected automatically by the Cib
}
- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
}
- (void)awakeFromCib
{
var dragged = [[Dragged alloc] initWithFrame:CGRectMake(100,100,20,20)];
[dragged setBackgroundColor:[CPColor redColor]];
[[theWindow contentView] addSubview:dragged];
var dropped = [[Dropped alloc] initWithFrame:CGRectMake(200,100,200,200)];
[dropped setBackgroundColor:[CPColor greenColor]];
[[theWindow contentView] addSubview:dropped];
}
@end
/* The dragged source */
@implementation Dragged : CPView
{
}
- (void)mouseDragged:(CPEvent)anEvent
{
var pasteboard = [CPPasteboard pasteboardWithName:CPDragPboard];
[pasteboard declareTypes:[CPArray arrayWithObject:CPDraggedDragType] owner:self];
var point = [self convertPoint:[anEvent locationInWindow] fromView:nil];
var bounds = CGRectMake(0, 0, 15, 15);
var dragView = [[CPView alloc] initWithFrame:bounds];
[dragView setBackgroundColor:[self backgroundColor]];
[self dragView: dragView
at: CPPointMake(point.x - bounds.size.width / 2.0, point.y - bounds.size.height / 2.0)
offset: CPPointMake(0.0, 0.0)
event: anEvent
pasteboard: nil
source: self
slideBack: YES];
}
- (void)pasteboard:(CPPasteboard)aPasteboard provideDataForType: (CPString)aType
{
if(aType == CPDraggedDragType)
{
/* PASS A COLOR OBJECT */
[aPasteboard setData:[self backgroundColor] forType:aType];
/* PASS A STRING OBJECT */
// [aPasteboard setString:[[self backgroundColor] hexString] forType:aType];
}
}
@end
/* The dragged Destination */
@implementation Dropped : CPView
{
}
-(id)initWithFrame:(CGRect)aFrame
{
self = [super initWithFrame:aFrame];
if(self)
{
[self registerForDraggedTypes:[CPArray arrayWithObjects:CPDraggedDragType]];
}
return self;
}
- (BOOL)performDragOperation:(id <CPDraggingInfo>)aSender
{
var pasteboard = [aSender draggingPasteboard];
if(![pasteboard availableTypeFromArray:[CPDraggedDragType]])
return NO;
/* USE A COLOR OBJECT */
var color = [pasteboard dataForType:CPDraggedDragType];
/* USE A STRING OBJECT */
// var color = [CPColor colorWithHexString:[pasteboard stringForType:CPDraggedDragType]];
[self setBackgroundColor:color];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment