Skip to content

Instantly share code, notes, and snippets.

@RonchettiAssociati
Created November 29, 2009 14:56
Show Gist options
  • Save RonchettiAssociati/244935 to your computer and use it in GitHub Desktop.
Save RonchettiAssociati/244935 to your computer and use it in GitHub Desktop.
/*
* AppController.j
* WktTest51
*
* Created by Bruno Ronchetti on November 29, 2009.
* Copyright 2009, Ronchetti & Associati All rights reserved.
*/
CPDraggedDragType = "CPDraggedDragType";
CPStringDragType = "CPStringDragType";
@implementation AppController : CPObject
{
CPView contentView;
CPWindow theWindow;
}
- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
var theWindow = [[CPWindow alloc] initWithContentRect:CGRectMakeZero() styleMask:CPBorderlessBridgeWindowMask];
contentView = [theWindow contentView];
CPLogRegister(CPLogPopup);
CPLog(@"partiti");
[theWindow setAcceptsMouseMovedEvents:YES]
var inputField1 = [[DnDTextField alloc] initWithFrame:CGRectMake(200,20,200,30)];
[inputField1 setTag:"inputField1"];
[inputField1 setEditable:YES];
[inputField1 setBezeled:YES];
[inputField1 registerForDraggedTypes:[CPArray arrayWithObjects:CPStringDragType]];
[contentView addSubview:inputField1];
var inputField2 = [[DnDTextField alloc] initWithFrame:CGRectMake(200,80,200,30)];
[inputField2 setTag:"inputField2"];
[inputField2 setEditable:YES];
[inputField2 setBezeled:YES];
[inputField2 registerForDraggedTypes:[CPArray arrayWithObjects:CPStringDragType]];
[contentView addSubview:inputField2];
var dragged = [[DnDView alloc] initWithFrame:CGRectMake(100,200,20,20)];
[dragged setBackgroundColor:[CPColor redColor]];
[contentView addSubview:dragged];
var dropped = [[DnDView alloc] initWithFrame:CGRectMake(200,200,200,200)];
[dropped setBackgroundColor:[CPColor greenColor]];
[dropped registerForDraggedTypes:[CPArray arrayWithObjects:CPDraggedDragType]];
[contentView addSubview:dropped];
[theWindow orderFront:self];
}
@end
@implementation DnDView : CPView
{
}
- (void)mouseDragged:(CPEvent)anEvent
{
CPLog("entro in dragged della view");
pasteboard = [CPPasteboard pasteboardWithName:CPDragPboard];
[pasteboard declareTypes:[CPArray arrayWithObject:CPDraggedDragType] owner:self];
[pasteboard addTypes:[CPArray arrayWithObject:CPStringDragType] owner:self];
var point = [self convertPoint:[anEvent locationInWindow] fromView:nil];
var bounds = CGRectMake(0, 0, 15, 15);
var dragView = [[CPTextField alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
[dragView setStringValue:"sto copiando"];
[dragView sizeToFit];
[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
{
CPLog("entro in pasteboard della view");
if(aType == CPDraggedDragType)
{
pasteData = [[self backgroundColor] hexString];
[aPasteboard setString:pasteData forType:aType];
}
if(aType == CPStringDragType)
{
pasteData = "copiato";
[aPasteboard setString:pasteData forType:aType];
}
}
- (BOOL)performDragOperation:(id <CPDraggingInfo>)aSender
{
CPLog("entro in perfrom della view");
pasteboard = [aSender draggingPasteboard];
if([pasteboard availableTypeFromArray:[CPDraggedDragType]])
{
passedString = [pasteboard stringForType:CPDraggedDragType];
color = [CPColor colorWithHexString:passedString];
[self setBackgroundColor:color];
}
if([pasteboard availableTypeFromArray:[CPStringDragType]])
{
passedString = [pasteboard stringForType:CPStringDragType];
alert([self tag]);
[self setStringValue:passedString];
}
}
@end
@implementation DnDTextField : CPTextField
{
}
- (void)mouseDragged:(CPEvent)anEvent
{
[[[self window] platformWindow] _propagateCurrentDOMEvent:YES];
CPLog("entro in dragged del textfield");
pasteboard = [CPPasteboard pasteboardWithName:CPDragPboard];
[pasteboard declareTypes:[CPArray arrayWithObject:CPDraggedDragType] owner:self];
[pasteboard addTypes:[@"CPStringDragType"] owner:self];
CPLog("pasteboard costruito in textfield" + [pasteboard types].toString());
var point = [self convertPoint:[anEvent locationInWindow] fromView:nil];
var bounds = CGRectMake(0, 0, 15, 15);
CPLog("point nel textField "+point.x+" "+point.y);
var dragView = [[CPTextField alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
[dragView setStringValue:"sto copiando"];
[dragView sizeToFit];
[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
{
CPLog("entro in pasteboard del field");
if(aType == CPDraggedDragType)
{
pasteData = [[self backgroundColor] hexString];
[aPasteboard setString:pasteData forType:aType];
}
if(aType == CPStringDragType)
{
pasteData = "copiato";
[aPasteboard setString:pasteData forType:aType];
}
}
- (BOOL)performDragOperation:(id <CPDraggingInfo>)aSender
{
CPLog("entro in perform "+[self tag]+" "+[pasteboard stringForType:CPStringDragType]+" "+[pasteboard stringForType:CPDraggedDragType]);
pasteboard = [aSender draggingPasteboard];
if([pasteboard availableTypeFromArray:[CPDraggedDragType]])
{
passedString = [pasteboard stringForType:CPDraggedDragType];
color = [CPColor colorWithHexString:passedString];
[self setBackgroundColor:color];
}
if([pasteboard availableTypeFromArray:[CPStringDragType]])
{
passedString = [pasteboard stringForType:CPStringDragType];
alert([self tag]);
[self setStringValue:passedString];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment