2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #import "WebDragClient.h"
29 #import "PasteboardTypes.h"
30 #import "ShareableBitmap.h"
31 #import "WebCoreArgumentCoders.h"
33 #import "WebPageProxyMessages.h"
34 #import <WebCore/CachedImage.h>
35 #import <WebCore/DOMPrivate.h>
36 #import <WebCore/DOMElementInternal.h>
37 #import <WebCore/FrameView.h>
38 #import <WebCore/GraphicsContext.h>
39 #import <WebCore/LegacyWebArchive.h>
40 #import <WebCore/RenderImage.h>
41 #import <WebCore/StringTruncator.h>
42 #import <wtf/StdLibExtras.h>
43 #import <WebKit/WebKitNSStringExtras.h>
44 #import <WebKit/WebNSURLExtras.h>
46 using namespace WebCore;
50 using namespace WebCore;
52 void WebDragClient::startDrag(DragImageRef dragImage, const IntPoint& at, const IntPoint& eventPos, Clipboard* clipboard, Frame* frame, bool linkDrag)
58 NSImage *dragNSImage = dragImage.get();
59 RefPtr<ShareableBitmap> dragShareableImage = ShareableBitmap::createShareable(IntSize([dragNSImage size]));
60 OwnPtr<GraphicsContext> graphicsContext = dragShareableImage->createGraphicsContext();
62 [NSGraphicsContext saveGraphicsState];
63 NSGraphicsContext* bitmapContext = [NSGraphicsContext graphicsContextWithGraphicsPort:graphicsContext->platformContext() flipped:YES];
64 [NSGraphicsContext setCurrentContext: bitmapContext];
66 [dragNSImage drawInRect:NSMakeRect(0, 0, [dragNSImage size].width , [dragNSImage size].height) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1 respectFlipped:YES hints:nil];
67 [NSGraphicsContext restoreGraphicsState];
69 SharedMemory::Handle handle;
70 if (!dragShareableImage->createHandle(handle))
72 IntPoint clientPoint(at);
73 m_page->send(Messages::WebPageProxy::SetDragImage(clientPoint, IntSize([dragNSImage size]), handle, linkDrag));
76 static void writeURL(NSPasteboard* pasteboard, NSURL* URL, NSString* title, NSArray* types)
80 if (![title length]) {
81 title = [[URL path] lastPathComponent];
83 title = [URL _web_userVisibleString];
86 if ([types containsObject:NSURLPboardType])
87 [URL writeToPasteboard:pasteboard];
88 if ([types containsObject:PasteboardTypes::WebURLPboardType])
89 [pasteboard setString:[URL _web_originalDataAsString] forType:PasteboardTypes::WebURLPboardType];
90 if ([types containsObject:PasteboardTypes::WebURLNamePboardType])
91 [pasteboard setString:title forType:PasteboardTypes::WebURLNamePboardType];
92 if ([types containsObject:NSStringPboardType])
93 [pasteboard setString:[URL _web_userVisibleString] forType:NSStringPboardType];
94 if ([types containsObject:PasteboardTypes::WebURLsWithTitlesPboardType]) {
95 NSArray* URLs = [NSArray arrayWithObject:URL];
96 unsigned count = [URLs count];
98 if (!count || [pasteboard availableTypeFromArray:[NSArray arrayWithObject:PasteboardTypes::WebURLsWithTitlesPboardType]] == nil)
101 NSArray* titles = [NSArray arrayWithObject:title];
103 if (count != [titles count])
106 NSMutableArray* URLStrings = [NSMutableArray arrayWithCapacity:count];
107 NSMutableArray* titlesOrEmptyStrings = [NSMutableArray arrayWithCapacity:count];
108 for (unsigned index = 0; index < count; ++index) {
109 [URLStrings addObject:[[URLs objectAtIndex:index] _web_originalDataAsString]];
110 [titlesOrEmptyStrings addObject:(titles == nil) ? @"" : [[titles objectAtIndex:index] _webkit_stringByTrimmingWhitespace]];
113 [pasteboard setPropertyList:[NSArray arrayWithObjects:URLStrings, titlesOrEmptyStrings, nil]
114 forType:PasteboardTypes::WebURLsWithTitlesPboardType];
118 static void writeImage(NSPasteboard* pasteboard, NSImage *image, DOMElement* element, NSURL* URL, NSString* title, LegacyWebArchive* archive, NSArray* types)
120 ASSERT(image || element);
123 writeURL(pasteboard, URL, title, types);
125 if ([types containsObject:NSTIFFPboardType]) {
126 // FIXME: we should add handling of promised types.
128 [pasteboard setData:[image TIFFRepresentation] forType:NSTIFFPboardType];
130 [pasteboard setData:[element _imageTIFFRepresentation] forType:NSTIFFPboardType];
133 if (archive && [types containsObject:PasteboardTypes::WebArchivePboardType])
134 [pasteboard setData:[[(NSData *)archive->rawDataRepresentation().get() retain] autorelease] forType:PasteboardTypes::WebArchivePboardType];
137 void WebDragClient::declareAndWriteDragImage(NSPasteboard* pasteboard, DOMElement* element, NSURL* URL, NSString* title, WebCore::Frame*)
140 ASSERT(pasteboard && pasteboard == [NSPasteboard pasteboardWithName:NSDragPboard]);
142 NSString *extension = @"";
143 if (RenderObject* renderer = core(element)->renderer()) {
144 if (renderer->isImage()) {
145 if (CachedImage* image = toRenderImage(renderer)->cachedImage()) {
146 extension = image->image()->filenameExtension();
147 if (![extension length])
153 RefPtr<LegacyWebArchive> archive = LegacyWebArchive::create(core(element));
154 NSMutableArray *types = [[NSMutableArray alloc] initWithObjects:NSFilesPromisePboardType, nil];
155 [types addObjectsFromArray:(archive) ? PasteboardTypes::forImagesWithArchive() : PasteboardTypes::forImages()];
156 [pasteboard declareTypes:types owner:nil];
157 writeImage(pasteboard, nil, element, URL, title, archive.get(), types);
160 NSArray *extensions = [[NSArray alloc] initWithObjects:extension, nil];
161 [pasteboard setPropertyList:extensions forType:NSFilesPromisePboardType];
162 [extensions release];
165 } // namespace WebKit