#import <appkit/appkit.h>
#import "GUI_view.h"
#import "GUI.h"

extern NXEvent GUI_lastmousedown; // for dragging

@implementation GUI_view
- drawSelf: (const NXRect *)re: (int)co
{
	[myGUIObject doDrawSelf:self:re:&bounds:name];
	return self;
}

- mouseDown: (NXEvent *)te
{
	if(dragsource)
		GUI_lastmousedown=*te;
	[myGUIObject doMouseDown:self:te:name];
	return self;
}

- setTheObject: (id)to
{
	myGUIObject=to;
	return self;
}

- setVarName:(char *)myname
{
	name=(char *)malloc(sizeof(char)*(strlen(myname)+1));
	strcpy(name, myname);
	return self;
}

- setDragging:(int)flag
{
dragging=flag; 
if(dragging) {
const char *mytype[2] = {NXAsciiPboardType, NXFilenamePboardType};
[self registerForDraggedTypes:mytype count:2];
} else [self unregisterDraggedTypes];
return self;
}

- setDragSource:(int)flag
{ dragsource=flag; return self; }

- (NXDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal
{
	return NX_DragOperationCopy;
}

- (BOOL)prepareForDragOperation:(id <NXDraggingInfo>)sender
{  if(dragging) return YES; else return NO; }

- (BOOL)performDragOperation:(id <NXDraggingInfo>)sender
{
	Pasteboard *pboard;
	NXPoint loc;
	
	if(dragging) {
	pboard=[sender draggingPasteboard];
	loc=[sender draggedImageLocation];
	[self convertPoint: &loc fromView: nil];
	[myGUIObject takeDrag: pboard: &loc: name];
	return YES;
	} else
		return NO;
}

- (NXDragOperation)draggingEntered:(id <NXDraggingInfo>)sender
{
	if(dragging)
		return NX_DragOperationCopy|NX_DragOperationGeneric;
	else
		return NX_DragOperationNone;
}

- free
{
	if(name) free(name);
	return [super free];
}
@end
