2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-27 17:25:04 -07:00
|
|
|
#ifdef MOZ_LOGGING
|
|
|
|
#define FORCE_PR_LOG
|
|
|
|
#endif
|
|
|
|
#include "prlog.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsDragService.h"
|
2008-02-19 12:58:01 -08:00
|
|
|
#include "nsObjCExceptions.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsITransferable.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsClipboard.h"
|
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsPrimitiveHelpers.h"
|
|
|
|
#include "nsLinebreakConverter.h"
|
|
|
|
#include "nsIMacUtils.h"
|
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsRect.h"
|
|
|
|
#include "nsPoint.h"
|
|
|
|
#include "nsIIOService.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIContent.h"
|
2013-01-03 05:23:11 -08:00
|
|
|
#include "nsView.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "gfxASurface.h"
|
|
|
|
#include "gfxContext.h"
|
2012-09-29 04:36:09 -07:00
|
|
|
#include "nsCocoaUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-09-04 23:58:16 -07:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
extern PRLogModuleInfo* sCocoaLog;
|
|
|
|
#endif
|
|
|
|
|
2011-07-05 20:41:27 -07:00
|
|
|
extern void EnsureLogInitialized();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
extern NSPasteboard* globalDragPboard;
|
2007-09-23 17:01:04 -07:00
|
|
|
extern NSView* gLastDragView;
|
2009-11-06 02:21:41 -08:00
|
|
|
extern NSEvent* gLastDragMouseDownEvent;
|
2011-09-28 23:19:26 -07:00
|
|
|
extern bool gUserCancelledDrag;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-16 19:24:05 -07:00
|
|
|
// This global makes the transferable array available to Cocoa's promised
|
|
|
|
// file destination callback.
|
2012-07-30 07:20:58 -07:00
|
|
|
nsISupportsArray *gDraggedTransferables = nullptr;
|
2007-07-16 19:24:05 -07:00
|
|
|
|
2007-05-28 22:06:12 -07:00
|
|
|
NSString* const kWildcardPboardType = @"MozillaWildcard";
|
2007-09-24 15:41:10 -07:00
|
|
|
NSString* const kCorePboardType_url = @"CorePasteboardFlavorType 0x75726C20"; // 'url ' url
|
2007-09-27 18:50:09 -07:00
|
|
|
NSString* const kCorePboardType_urld = @"CorePasteboardFlavorType 0x75726C64"; // 'urld' desc
|
2007-09-24 15:41:10 -07:00
|
|
|
NSString* const kCorePboardType_urln = @"CorePasteboardFlavorType 0x75726C6E"; // 'urln' title
|
2007-05-28 22:06:12 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsDragService::nsDragService()
|
|
|
|
{
|
2007-09-23 17:01:04 -07:00
|
|
|
mNativeDragView = nil;
|
|
|
|
mNativeDragEvent = nil;
|
2011-07-05 20:41:27 -07:00
|
|
|
|
|
|
|
EnsureLogInitialized();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsDragService::~nsDragService()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult SetUpDragClipboard(nsISupportsArray* aTransferableArray)
|
|
|
|
{
|
2008-02-19 12:58:01 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!aTransferableArray)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
aTransferableArray->Count(&count);
|
|
|
|
|
2007-07-03 00:16:19 -07:00
|
|
|
NSPasteboard* dragPBoard = [NSPasteboard pasteboardWithName:NSDragPboard];
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsISupports> currentTransferableSupports;
|
|
|
|
aTransferableArray->GetElementAt(i, getter_AddRefs(currentTransferableSupports));
|
|
|
|
if (!currentTransferableSupports)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsITransferable> currentTransferable(do_QueryInterface(currentTransferableSupports));
|
|
|
|
if (!currentTransferable)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2007-06-26 22:26:18 -07:00
|
|
|
// Transform the transferable to an NSDictionary
|
|
|
|
NSDictionary* pasteboardOutputDict = nsClipboard::PasteboardDictFromTransferable(currentTransferable);
|
|
|
|
if (!pasteboardOutputDict)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// write everything out to the general pasteboard
|
2007-07-03 16:13:28 -07:00
|
|
|
unsigned int typeCount = [pasteboardOutputDict count];
|
|
|
|
NSMutableArray* types = [NSMutableArray arrayWithCapacity:typeCount + 1];
|
|
|
|
[types addObjectsFromArray:[pasteboardOutputDict allKeys]];
|
|
|
|
// Gecko is initiating this drag so we always want its own views to consider
|
|
|
|
// it. Add our wildcard type to the pasteboard to accomplish this.
|
|
|
|
[types addObject:kWildcardPboardType]; // we don't increase the count for the loop below on purpose
|
|
|
|
[dragPBoard declareTypes:types owner:nil];
|
|
|
|
for (unsigned int i = 0; i < typeCount; i++) {
|
|
|
|
NSString* currentKey = [types objectAtIndex:i];
|
2007-03-22 10:30:00 -07:00
|
|
|
id currentValue = [pasteboardOutputDict valueForKey:currentKey];
|
2007-09-24 13:43:01 -07:00
|
|
|
if (currentKey == NSStringPboardType ||
|
2007-09-24 15:41:10 -07:00
|
|
|
currentKey == kCorePboardType_url ||
|
2007-09-27 18:50:09 -07:00
|
|
|
currentKey == kCorePboardType_urld ||
|
2007-09-24 15:41:10 -07:00
|
|
|
currentKey == kCorePboardType_urln) {
|
2007-07-03 00:16:19 -07:00
|
|
|
[dragPBoard setString:currentValue forType:currentKey];
|
2007-06-28 12:20:12 -07:00
|
|
|
}
|
2009-01-01 06:26:16 -08:00
|
|
|
else if (currentKey == NSHTMLPboardType) {
|
|
|
|
[dragPBoard setString:(nsClipboard::WrapHtmlForSystemPasteboard(currentValue))
|
|
|
|
forType:currentKey];
|
|
|
|
}
|
2007-06-28 12:20:12 -07:00
|
|
|
else if (currentKey == NSTIFFPboardType) {
|
2007-07-03 00:16:19 -07:00
|
|
|
[dragPBoard setData:currentValue forType:currentKey];
|
2007-05-28 22:06:12 -07:00
|
|
|
}
|
2013-08-01 18:21:31 -07:00
|
|
|
else if (currentKey == NSFilesPromisePboardType ||
|
|
|
|
currentKey == NSFilenamesPboardType) {
|
2007-07-19 11:09:43 -07:00
|
|
|
[dragPBoard setPropertyList:currentValue forType:currentKey];
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-03 00:16:19 -07:00
|
|
|
return NS_OK;
|
2008-02-19 12:58:01 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NSImage*
|
|
|
|
nsDragService::ConstructDragImage(nsIDOMNode* aDOMNode,
|
2009-01-14 19:27:09 -08:00
|
|
|
nsIntRect* aDragRect,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIScriptableRegion* aRegion)
|
|
|
|
{
|
2008-02-19 12:58:01 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
|
|
|
|
|
2012-09-29 04:36:09 -07:00
|
|
|
NSPoint screenPoint =
|
|
|
|
[[gLastDragView window] convertBaseToScreen:
|
|
|
|
[gLastDragMouseDownEvent locationInWindow]];
|
2007-03-22 10:30:00 -07:00
|
|
|
// Y coordinates are bottom to top, so reverse this
|
2012-09-29 04:36:09 -07:00
|
|
|
screenPoint.y = nsCocoaUtils::FlippedScreenY(screenPoint.y);
|
|
|
|
|
|
|
|
CGFloat scaleFactor = nsCocoaUtils::GetBackingScaleFactor(gLastDragView);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsRefPtr<gfxASurface> surface;
|
2008-01-08 14:03:53 -08:00
|
|
|
nsPresContext* pc;
|
2012-12-14 12:25:51 -08:00
|
|
|
nsresult rv = DrawDrag(aDOMNode, aRegion,
|
|
|
|
NSToIntRound(screenPoint.x),
|
|
|
|
NSToIntRound(screenPoint.y),
|
2008-01-08 14:03:53 -08:00
|
|
|
aDragRect, getter_AddRefs(surface), &pc);
|
2007-03-25 20:27:53 -07:00
|
|
|
if (!aDragRect->width || !aDragRect->height) {
|
|
|
|
// just use some suitable defaults
|
2012-09-29 04:36:09 -07:00
|
|
|
int32_t size = nsCocoaUtils::CocoaPointsToDevPixels(20, scaleFactor);
|
2012-12-14 12:25:51 -08:00
|
|
|
aDragRect->SetRect(nsCocoaUtils::CocoaPointsToDevPixels(screenPoint.x, scaleFactor),
|
|
|
|
nsCocoaUtils::CocoaPointsToDevPixels(screenPoint.y, scaleFactor),
|
|
|
|
size, size);
|
2007-03-25 20:27:53 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv) || !surface)
|
2007-08-22 09:11:59 -07:00
|
|
|
return nil;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t width = aDragRect->width;
|
|
|
|
uint32_t height = aDragRect->height;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsRefPtr<gfxImageSurface> imgSurface = new gfxImageSurface(
|
2013-09-24 13:45:13 -07:00
|
|
|
gfxIntSize(width, height), gfxImageFormatARGB32);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!imgSurface)
|
2007-08-22 09:11:59 -07:00
|
|
|
return nil;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsRefPtr<gfxContext> context = new gfxContext(imgSurface);
|
|
|
|
if (!context)
|
2007-08-22 09:11:59 -07:00
|
|
|
return nil;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
context->SetOperator(gfxContext::OPERATOR_SOURCE);
|
|
|
|
context->SetSource(surface);
|
|
|
|
context->Paint();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t* imageData = (uint32_t*)imgSurface->Data();
|
|
|
|
int32_t stride = imgSurface->Stride();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-09-29 04:36:09 -07:00
|
|
|
NSBitmapImageRep* imageRep =
|
|
|
|
[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
|
|
|
|
pixelsWide:width
|
|
|
|
pixelsHigh:height
|
|
|
|
bitsPerSample:8
|
|
|
|
samplesPerPixel:4
|
|
|
|
hasAlpha:YES
|
|
|
|
isPlanar:NO
|
|
|
|
colorSpaceName:NSDeviceRGBColorSpace
|
|
|
|
bytesPerRow:width * 4
|
|
|
|
bitsPerPixel:32];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t* dest = [imageRep bitmapData];
|
|
|
|
for (uint32_t i = 0; i < height; ++i) {
|
|
|
|
uint8_t* src = (uint8_t *)imageData + i * stride;
|
|
|
|
for (uint32_t j = 0; j < width; ++j) {
|
2007-08-22 09:11:59 -07:00
|
|
|
// Reduce transparency overall by multipying by a factor. Remember, Alpha
|
|
|
|
// is premultipled here. Also, Quartz likes RGBA, so do that translation as well.
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef IS_BIG_ENDIAN
|
2012-08-22 08:56:38 -07:00
|
|
|
dest[0] = uint8_t(src[1] * DRAG_TRANSLUCENCY);
|
|
|
|
dest[1] = uint8_t(src[2] * DRAG_TRANSLUCENCY);
|
|
|
|
dest[2] = uint8_t(src[3] * DRAG_TRANSLUCENCY);
|
|
|
|
dest[3] = uint8_t(src[0] * DRAG_TRANSLUCENCY);
|
2007-03-22 10:30:00 -07:00
|
|
|
#else
|
2012-08-22 08:56:38 -07:00
|
|
|
dest[0] = uint8_t(src[2] * DRAG_TRANSLUCENCY);
|
|
|
|
dest[1] = uint8_t(src[1] * DRAG_TRANSLUCENCY);
|
|
|
|
dest[2] = uint8_t(src[0] * DRAG_TRANSLUCENCY);
|
|
|
|
dest[3] = uint8_t(src[3] * DRAG_TRANSLUCENCY);
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
src += 4;
|
|
|
|
dest += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-29 04:36:09 -07:00
|
|
|
NSImage* image =
|
|
|
|
[[NSImage alloc] initWithSize:NSMakeSize(width / scaleFactor,
|
|
|
|
height / scaleFactor)];
|
2007-03-22 10:30:00 -07:00
|
|
|
[image addRepresentation:imageRep];
|
2007-08-22 09:26:23 -07:00
|
|
|
[imageRep release];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return [image autorelease];
|
2008-02-19 12:58:01 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// We can only invoke NSView's 'dragImage:at:offset:event:pasteboard:source:slideBack:' from
|
|
|
|
// within NSView's 'mouseDown:' or 'mouseDragged:'. Luckily 'mouseDragged' is always on the
|
|
|
|
// stack when InvokeDragSession gets called.
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDragService::InvokeDragSession(nsIDOMNode* aDOMNode, nsISupportsArray* aTransferableArray,
|
2012-08-22 08:56:38 -07:00
|
|
|
nsIScriptableRegion* aDragRgn, uint32_t aActionType)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-02-19 12:58:01 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2008-03-19 13:18:41 -07:00
|
|
|
nsresult rv = nsBaseDragService::InvokeDragSession(aDOMNode,
|
|
|
|
aTransferableArray,
|
|
|
|
aDragRgn, aActionType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-06-28 12:20:12 -07:00
|
|
|
mDataItems = aTransferableArray;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// put data on the clipboard
|
|
|
|
if (NS_FAILED(SetUpDragClipboard(aTransferableArray)))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2009-01-14 19:27:09 -08:00
|
|
|
nsIntRect dragRect(0, 0, 20, 20);
|
2007-03-22 10:30:00 -07:00
|
|
|
NSImage* image = ConstructDragImage(aDOMNode, &dragRect, aDragRgn);
|
|
|
|
if (!image) {
|
|
|
|
// if no image was returned, just draw a rectangle
|
|
|
|
NSSize size;
|
|
|
|
size.width = dragRect.width;
|
|
|
|
size.height = dragRect.height;
|
|
|
|
image = [[NSImage alloc] initWithSize:size];
|
|
|
|
[image lockFocus];
|
|
|
|
[[NSColor grayColor] set];
|
|
|
|
NSBezierPath* path = [NSBezierPath bezierPath];
|
|
|
|
[path setLineWidth:2.0];
|
|
|
|
[path moveToPoint:NSMakePoint(0, 0)];
|
|
|
|
[path lineToPoint:NSMakePoint(0, size.height)];
|
|
|
|
[path lineToPoint:NSMakePoint(size.width, size.height)];
|
|
|
|
[path lineToPoint:NSMakePoint(size.width, 0)];
|
|
|
|
[path lineToPoint:NSMakePoint(0, 0)];
|
|
|
|
[path stroke];
|
|
|
|
[image unlockFocus];
|
|
|
|
}
|
|
|
|
|
2012-09-29 04:36:09 -07:00
|
|
|
nsIntPoint pt(dragRect.x, dragRect.YMost());
|
|
|
|
CGFloat scaleFactor = nsCocoaUtils::GetBackingScaleFactor(gLastDragView);
|
|
|
|
NSPoint point = nsCocoaUtils::DevPixelsToCocoaPoints(pt, scaleFactor);
|
|
|
|
point.y = nsCocoaUtils::FlippedScreenY(point.y);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-09-23 17:01:04 -07:00
|
|
|
point = [[gLastDragView window] convertScreenToBase: point];
|
|
|
|
NSPoint localPoint = [gLastDragView convertPoint:point fromView:nil];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-16 19:24:05 -07:00
|
|
|
// Save the transferables away in case a promised file callback is invoked.
|
2007-07-17 16:02:40 -07:00
|
|
|
gDraggedTransferables = aTransferableArray;
|
2007-07-16 19:24:05 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsBaseDragService::StartDragSession();
|
2011-04-25 18:37:20 -07:00
|
|
|
nsBaseDragService::OpenDragPopup();
|
2007-07-16 19:24:05 -07:00
|
|
|
|
2007-09-23 17:01:04 -07:00
|
|
|
// We need to retain the view and the event during the drag in case either gets destroyed.
|
|
|
|
mNativeDragView = [gLastDragView retain];
|
2009-11-06 02:21:41 -08:00
|
|
|
mNativeDragEvent = [gLastDragMouseDownEvent retain];
|
2007-09-23 17:01:04 -07:00
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
gUserCancelledDrag = false;
|
2007-09-23 17:01:04 -07:00
|
|
|
[mNativeDragView dragImage:image
|
|
|
|
at:localPoint
|
2009-11-06 02:21:41 -08:00
|
|
|
offset:NSZeroSize
|
2007-09-23 17:01:04 -07:00
|
|
|
event:mNativeDragEvent
|
|
|
|
pasteboard:[NSPasteboard pasteboardWithName:NSDragPboard]
|
|
|
|
source:mNativeDragView
|
|
|
|
slideBack:YES];
|
2011-09-30 17:20:33 -07:00
|
|
|
gUserCancelledDrag = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-10-27 21:44:29 -07:00
|
|
|
if (mDoingDrag)
|
2011-09-30 17:20:33 -07:00
|
|
|
nsBaseDragService::EndDragSession(false);
|
2008-10-27 21:44:29 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2008-02-19 12:58:01 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsDragService::GetData(nsITransferable* aTransferable, uint32_t aItemIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-02-19 12:58:01 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-06-28 12:20:12 -07:00
|
|
|
if (!aTransferable)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// get flavor list that includes all acceptable flavors (including ones obtained through conversion)
|
|
|
|
nsCOMPtr<nsISupportsArray> flavorList;
|
|
|
|
nsresult rv = aTransferable->FlavorsTransferableCanImport(getter_AddRefs(flavorList));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t acceptableFlavorCount;
|
2007-06-28 12:20:12 -07:00
|
|
|
flavorList->Count(&acceptableFlavorCount);
|
|
|
|
|
|
|
|
// if this drag originated within Mozilla we should just use the cached data from
|
|
|
|
// when the drag started if possible
|
|
|
|
if (mDataItems) {
|
|
|
|
nsCOMPtr<nsISupports> currentTransferableSupports;
|
|
|
|
mDataItems->GetElementAt(aItemIndex, getter_AddRefs(currentTransferableSupports));
|
|
|
|
if (currentTransferableSupports) {
|
|
|
|
nsCOMPtr<nsITransferable> currentTransferable(do_QueryInterface(currentTransferableSupports));
|
|
|
|
if (currentTransferable) {
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < acceptableFlavorCount; i++) {
|
2007-06-28 12:20:12 -07:00
|
|
|
nsCOMPtr<nsISupports> genericFlavor;
|
|
|
|
flavorList->GetElementAt(i, getter_AddRefs(genericFlavor));
|
|
|
|
nsCOMPtr<nsISupportsCString> currentFlavor(do_QueryInterface(genericFlavor));
|
|
|
|
if (!currentFlavor)
|
|
|
|
continue;
|
|
|
|
nsXPIDLCString flavorStr;
|
|
|
|
currentFlavor->ToString(getter_Copies(flavorStr));
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> dataSupports;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t dataSize = 0;
|
2007-06-28 12:20:12 -07:00
|
|
|
rv = currentTransferable->GetTransferData(flavorStr, getter_AddRefs(dataSupports), &dataSize);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
aTransferable->SetTransferData(flavorStr, dataSupports, dataSize);
|
|
|
|
return NS_OK; // maybe try to fill in more types? Is there a point?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now check the actual clipboard for data
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < acceptableFlavorCount; i++) {
|
2007-06-28 12:20:12 -07:00
|
|
|
nsCOMPtr<nsISupports> genericFlavor;
|
|
|
|
flavorList->GetElementAt(i, getter_AddRefs(genericFlavor));
|
|
|
|
nsCOMPtr<nsISupportsCString> currentFlavor(do_QueryInterface(genericFlavor));
|
|
|
|
|
|
|
|
if (!currentFlavor)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nsXPIDLCString flavorStr;
|
|
|
|
currentFlavor->ToString(getter_Copies(flavorStr));
|
|
|
|
|
2007-07-16 19:24:05 -07:00
|
|
|
PR_LOG(sCocoaLog, PR_LOG_ALWAYS, ("nsDragService::GetData: looking for clipboard data of type %s\n", flavorStr.get()));
|
2007-06-28 12:20:12 -07:00
|
|
|
|
2007-07-03 00:16:19 -07:00
|
|
|
if (flavorStr.EqualsLiteral(kFileMime)) {
|
2007-06-28 12:20:12 -07:00
|
|
|
NSArray* pFiles = [globalDragPboard propertyListForType:NSFilenamesPboardType];
|
|
|
|
if (!pFiles || [pFiles count] < (aItemIndex + 1))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
NSString* filePath = [pFiles objectAtIndex:aItemIndex];
|
|
|
|
if (!filePath)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
unsigned int stringLength = [filePath length];
|
|
|
|
unsigned int dataLength = (stringLength + 1) * sizeof(PRUnichar); // in bytes
|
|
|
|
PRUnichar* clipboardDataPtr = (PRUnichar*)malloc(dataLength);
|
|
|
|
if (!clipboardDataPtr)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2013-10-10 09:59:40 -07:00
|
|
|
[filePath getCharacters:reinterpret_cast<unichar*>(clipboardDataPtr)];
|
2007-06-28 12:20:12 -07:00
|
|
|
clipboardDataPtr[stringLength] = 0; // null terminate
|
|
|
|
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> file;
|
2011-09-30 17:20:33 -07:00
|
|
|
nsresult rv = NS_NewLocalFile(nsDependentString(clipboardDataPtr), true, getter_AddRefs(file));
|
2007-06-28 12:20:12 -07:00
|
|
|
free(clipboardDataPtr);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
continue;
|
|
|
|
|
2013-08-01 18:21:31 -07:00
|
|
|
aTransferable->SetTransferData(flavorStr, file, dataLength);
|
2007-06-28 12:20:12 -07:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-11-17 13:46:39 -08:00
|
|
|
NSString *pboardType = NSStringPboardType;
|
2008-11-06 17:08:35 -08:00
|
|
|
|
|
|
|
if (nsClipboard::IsStringType(flavorStr, &pboardType) ||
|
2007-11-06 23:28:36 -08:00
|
|
|
flavorStr.EqualsLiteral(kURLMime) ||
|
|
|
|
flavorStr.EqualsLiteral(kURLDataMime) ||
|
|
|
|
flavorStr.EqualsLiteral(kURLDescriptionMime)) {
|
2008-11-06 17:08:35 -08:00
|
|
|
NSString* pString = [globalDragPboard stringForType:pboardType];
|
2007-06-28 12:20:12 -07:00
|
|
|
if (!pString)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
NSData* stringData = [pString dataUsingEncoding:NSUnicodeStringEncoding];
|
|
|
|
unsigned int dataLength = [stringData length];
|
|
|
|
void* clipboardDataPtr = malloc(dataLength);
|
|
|
|
if (!clipboardDataPtr)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
[stringData getBytes:clipboardDataPtr];
|
|
|
|
|
|
|
|
// The DOM only wants LF, so convert from MacOS line endings to DOM line endings.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t signedDataLength = dataLength;
|
2007-07-03 00:16:19 -07:00
|
|
|
nsLinebreakHelpers::ConvertPlatformToDOMLinebreaks(flavorStr, &clipboardDataPtr, &signedDataLength);
|
2007-06-28 12:20:12 -07:00
|
|
|
dataLength = signedDataLength;
|
|
|
|
|
|
|
|
// skip BOM (Byte Order Mark to distinguish little or big endian)
|
|
|
|
PRUnichar* clipboardDataPtrNoBOM = (PRUnichar*)clipboardDataPtr;
|
|
|
|
if ((dataLength > 2) &&
|
|
|
|
((clipboardDataPtrNoBOM[0] == 0xFEFF) ||
|
|
|
|
(clipboardDataPtrNoBOM[0] == 0xFFFE))) {
|
|
|
|
dataLength -= sizeof(PRUnichar);
|
|
|
|
clipboardDataPtrNoBOM += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> genericDataWrapper;
|
|
|
|
nsPrimitiveHelpers::CreatePrimitiveForData(flavorStr, clipboardDataPtrNoBOM, dataLength,
|
|
|
|
getter_AddRefs(genericDataWrapper));
|
|
|
|
aTransferable->SetTransferData(flavorStr, genericDataWrapper, dataLength);
|
|
|
|
free(clipboardDataPtr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have never supported this on Mac OS X, we should someday. Normally dragging images
|
|
|
|
// in is accomplished with a file path drag instead of the image data itself.
|
|
|
|
/*
|
2007-07-03 00:16:19 -07:00
|
|
|
if (flavorStr.EqualsLiteral(kPNGImageMime) || flavorStr.EqualsLiteral(kJPEGImageMime) ||
|
2012-06-10 16:44:50 -07:00
|
|
|
flavorStr.EqualsLiteral(kJPGImageMime) || flavorStr.EqualsLiteral(kGIFImageMime)) {
|
2007-06-28 12:20:12 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2008-02-19 12:58:01 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsDragService::IsDataFlavorSupported(const char *aDataFlavor, bool *_retval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-02-19 12:58:01 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
*_retval = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!globalDragPboard)
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-07-03 00:16:19 -07:00
|
|
|
|
|
|
|
nsDependentCString dataFlavor(aDataFlavor);
|
|
|
|
|
2007-07-03 16:13:28 -07:00
|
|
|
// first see if we have data for this in our cached transferable
|
|
|
|
if (mDataItems) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t dataItemsCount;
|
2007-07-03 16:13:28 -07:00
|
|
|
mDataItems->Count(&dataItemsCount);
|
|
|
|
for (unsigned int i = 0; i < dataItemsCount; i++) {
|
|
|
|
nsCOMPtr<nsISupports> currentTransferableSupports;
|
|
|
|
mDataItems->GetElementAt(i, getter_AddRefs(currentTransferableSupports));
|
|
|
|
if (!currentTransferableSupports)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nsCOMPtr<nsITransferable> currentTransferable(do_QueryInterface(currentTransferableSupports));
|
|
|
|
if (!currentTransferable)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupportsArray> flavorList;
|
|
|
|
nsresult rv = currentTransferable->FlavorsTransferableCanImport(getter_AddRefs(flavorList));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
continue;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t flavorCount;
|
2007-07-03 16:13:28 -07:00
|
|
|
flavorList->Count(&flavorCount);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < flavorCount; j++) {
|
2007-07-03 16:13:28 -07:00
|
|
|
nsCOMPtr<nsISupports> genericFlavor;
|
|
|
|
flavorList->GetElementAt(j, getter_AddRefs(genericFlavor));
|
|
|
|
nsCOMPtr<nsISupportsCString> currentFlavor(do_QueryInterface(genericFlavor));
|
|
|
|
if (!currentFlavor)
|
|
|
|
continue;
|
|
|
|
nsXPIDLCString flavorStr;
|
|
|
|
currentFlavor->ToString(getter_Copies(flavorStr));
|
|
|
|
if (dataFlavor.Equals(flavorStr)) {
|
2011-09-30 17:20:33 -07:00
|
|
|
*_retval = true;
|
2007-07-03 16:13:28 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-17 13:46:39 -08:00
|
|
|
NSString *pboardType = nil;
|
2008-11-06 17:08:35 -08:00
|
|
|
|
2007-07-03 00:16:19 -07:00
|
|
|
if (dataFlavor.EqualsLiteral(kFileMime)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
NSString* availableType = [globalDragPboard availableTypeFromArray:[NSArray arrayWithObject:NSFilenamesPboardType]];
|
|
|
|
if (availableType && [availableType isEqualToString:NSFilenamesPboardType])
|
2011-09-30 17:20:33 -07:00
|
|
|
*_retval = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-09-25 07:30:20 -07:00
|
|
|
else if (dataFlavor.EqualsLiteral(kURLMime)) {
|
|
|
|
NSString* availableType = [globalDragPboard availableTypeFromArray:[NSArray arrayWithObject:kCorePboardType_url]];
|
|
|
|
if (availableType && [availableType isEqualToString:kCorePboardType_url])
|
2011-09-30 17:20:33 -07:00
|
|
|
*_retval = true;
|
2008-09-25 07:30:20 -07:00
|
|
|
}
|
2008-11-06 17:08:35 -08:00
|
|
|
else if (nsClipboard::IsStringType(dataFlavor, &pboardType)) {
|
|
|
|
NSString* availableType = [globalDragPboard availableTypeFromArray:[NSArray arrayWithObject:pboardType]];
|
|
|
|
if (availableType && [availableType isEqualToString:pboardType])
|
2011-09-30 17:20:33 -07:00
|
|
|
*_retval = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2008-02-19 12:58:01 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsDragService::GetNumDropItems(uint32_t* aNumItems)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-02-19 12:58:01 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
*aNumItems = 0;
|
2007-06-28 12:20:12 -07:00
|
|
|
|
|
|
|
// first check to see if we have a number of items cached
|
|
|
|
if (mDataItems) {
|
|
|
|
mDataItems->Count(aNumItems);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// if there is a clipboard and there is something on it, then there is at least 1 item
|
|
|
|
NSArray* clipboardTypes = [globalDragPboard types];
|
|
|
|
if (globalDragPboard && [clipboardTypes count] > 0)
|
|
|
|
*aNumItems = 1;
|
|
|
|
else
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// if there is a list of files, send the number of files in that list
|
|
|
|
NSArray* fileNames = [globalDragPboard propertyListForType:NSFilenamesPboardType];
|
|
|
|
if (fileNames)
|
|
|
|
*aNumItems = [fileNames count];
|
|
|
|
|
|
|
|
return NS_OK;
|
2008-02-19 12:58:01 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-06-28 12:20:12 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsDragService::EndDragSession(bool aDoneDrag)
|
2007-06-28 12:20:12 -07:00
|
|
|
{
|
2008-02-19 12:58:01 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-09-23 17:01:04 -07:00
|
|
|
if (mNativeDragView) {
|
|
|
|
[mNativeDragView release];
|
|
|
|
mNativeDragView = nil;
|
|
|
|
}
|
|
|
|
if (mNativeDragEvent) {
|
|
|
|
[mNativeDragEvent release];
|
|
|
|
mNativeDragEvent = nil;
|
|
|
|
}
|
|
|
|
|
2009-02-17 07:51:12 -08:00
|
|
|
mUserCancelled = gUserCancelledDrag;
|
|
|
|
|
2008-08-27 05:07:27 -07:00
|
|
|
nsresult rv = nsBaseDragService::EndDragSession(aDoneDrag);
|
2012-07-30 07:20:58 -07:00
|
|
|
mDataItems = nullptr;
|
2008-08-27 05:07:27 -07:00
|
|
|
return rv;
|
2008-02-19 12:58:01 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-06-28 12:20:12 -07:00
|
|
|
}
|