// Copyright 1997-2000 Omni Development, Inc.  All rights reserved.
//
// This software may only be used and reproduced according to the
// terms in the file OmniSourceLicense.html, which should be
// distributed with this project and can also be found at
// http://www.omnigroup.com/DeveloperResources/OmniSourceLicense.html.

#import "OWAddress-OHDragging.h"

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <OmniBase/OmniBase.h>
#import <OmniFoundation/OmniFoundation.h>
#import <OmniAppKit/OmniAppKit.h>
#import <OWF/OWF.h>
#import <OIF/OIF.h>

#import "OWAddress-OHActions.h"
#import "OWAddress-OHImage.h"
#import "OWAddress-OHPasteboard.h"

RCS_ID("$Header: /Network/Source/CVS/OmniGroup/Frameworks/OmniHTML/Support.subproj/OWAddress-OHDragging.m,v 1.4 2000/01/20 00:27:59 kc Exp $")

@implementation OWAddress (Dragging)

static BOOL DraggingImagesSetup = NO;
static NSImage *NewURLWindowImage = nil;

+ (void)setupDraggingImages;
{
    if (DraggingImagesSetup)
	return;
    NewURLWindowImage = [NSImage imageNamed:@"TinyWindow"];
    DraggingImagesSetup = YES;
}

- (void)startDragFromView:(NSView *)view image:(NSImage *)image atPoint:(NSPoint)location event:(NSEvent *)event pasteboardHelper:(OAPasteboardHelper *)pasteboardHelper;
{
    NSString *localFilename = nil;
    BOOL dragNewWindow;
    NSPoint zero;

    zero = NSMakePoint(0.0, 0.0);

    if ((localFilename = [self localFilename])) {
        [pasteboardHelper addTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
	pasteboardFilename = [localFilename retain];
    } 

    dragNewWindow = NO;
    if ([event modifierFlags] & NSAlternateKeyMask) {
	NSSize imageSize;
	NSString *OWNewWindowURIType;
	
	if (!DraggingImagesSetup)
	    [isa setupDraggingImages];
	image = NewURLWindowImage;
	imageSize = [image size];
	location.x -= (int)(imageSize.width / 2.0);
	location.y += (int)(imageSize.height / 2.0);

	dragNewWindow = YES;
	OWNewWindowURIType = [isa OWNewWindowURIType];
	[pasteboardHelper declareTypes:[NSArray arrayWithObject:OWNewWindowURIType] owner:self];
    } else if ([self representsFile] && ([event modifierFlags] & NSCommandKeyMask)) {
#warning Should change the drag image to indicate a download vs. normal drag
	[pasteboardHelper declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
#ifdef CREATE_URL_FILES_WHEN_DRAGGING_LINKS
    } else if (!(localFilename || [[[pasteboardHelper pasteboard] types] containsObject:NSFilenamesPboardType])) {
        NSData *urlFileData;
        NSString *filename, *path;
        // Defaults to making a .url file.

        path = [[[[OFUserDefaults sharedUserDefaults] stringForKey:@"OHDownloadDirectory"] stringByExpandingTildeInPath] stringByAppendingString:@"/"];
        filename = [[path stringByAppendingString:[self bestKnownTitle]] stringByAppendingString:@".url"];
        urlFileData = [[NSData alloc] initWithData:[[self addressString] dataUsingEncoding:NSASCIIStringEncoding]];
        
        if ([urlFileData writeToFile:filename atomically:YES]) {
            [pasteboardHelper addTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:self];
            pasteboardFilename = [filename retain];
        }

        [urlFileData release];
#endif
    }

    [[OADragController sharedDragController] startDragFromView:view image:image atPoint:location offset:zero event:event slideBack:!dragNewWindow pasteboardHelper:pasteboardHelper delegate:self];
}

- (void)startDragFromView:(NSView *)view atPoint:(NSPoint)location event:(NSEvent *)event;
{
    OAPasteboardHelper *pasteboardHelper;

    pasteboardHelper = [OAPasteboardHelper helperWithPasteboardNamed:NSDragPboard];
    [OWAddress declareTypesForEntry:self pasteboardHelper:pasteboardHelper];
    [self startDragFromView:view image:[self dragImage] atPoint:location event:event pasteboardHelper:pasteboardHelper];
}

- (void)startDragOmniImage:(OIImage *)omniImage imageAddress:(OWAddress *)imageAddress fromView:(NSView *)aView event:(NSEvent *)event;
{
    NSImage *dragImage;
    NSSize dragImageSize;
    OAPasteboardHelper *pasteboardHelper;
    NSPoint where;

    where = [aView convertPoint:[event locationInWindow] fromView:nil];

    dragImage = [omniImage image];
    dragImageSize = [dragImage size];
    if (!dragImage || (dragImageSize.width > 64 && dragImageSize.height > 64)) {
	dragImage = [NSImage imageNamed:@"NotFetched"];
	dragImageSize = [dragImage size];
    }
    where.x -= dragImageSize.width / (int)2;
    where.y += dragImageSize.height / (int)2;

    pasteboardHelper = [OAPasteboardHelper helperWithPasteboardNamed:NSDragPboard];
    [OWAddress declareTypesForEntry:self pasteboardHelper:pasteboardHelper];

    if (omniImage)
        [pasteboardHelper addTypes:[NSArray arrayWithObject:NSTIFFPboardType] owner:omniImage];
    [pasteboardHelper addTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:imageAddress];

    [self startDragFromView:aView image:dragImage atPoint:where event:event pasteboardHelper:pasteboardHelper];
}

// OADragController delegate

- (void)draggedImage:(NSImage *)image endedAt:(NSPoint)screenPoint deposited:(BOOL)didDeposit;
{
    if (!DraggingImagesSetup)
	[isa setupDraggingImages];

    if (!didDeposit && image == NewURLWindowImage)
        [self openDocumentInNewWindowAtScreenPoint:screenPoint];
}

@end
