1/*
2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#import "WebDragClient.h"
27
28#if ENABLE(DRAG_SUPPORT)
29
30#import "DOMElementInternal.h"
31#import "WebArchive.h"
32#import "WebDOMOperations.h"
33#import "WebFrame.h"
34#import "WebFrameInternal.h"
35#import "WebHTMLViewInternal.h"
36#import "WebHTMLViewPrivate.h"
37#import "WebKitLogging.h"
38#import "WebKitNSStringExtras.h"
39#import "WebNSPasteboardExtras.h"
40#import "WebNSURLExtras.h"
41#import "WebStringTruncator.h"
42#import "WebUIDelegate.h"
43#import "WebUIDelegatePrivate.h"
44#import "WebViewInternal.h"
45#import <WebCore/DataTransfer.h>
46#import <WebCore/DragData.h>
47#import <WebCore/Editor.h>
48#import <WebCore/EditorClient.h>
49#import <WebCore/EventHandler.h>
50#import <WebCore/FrameView.h>
51#import <WebCore/Image.h>
52#import <WebCore/MainFrame.h>
53#import <WebCore/Page.h>
54#import <WebCore/Pasteboard.h>
55
56using namespace WebCore;
57
58WebDragClient::WebDragClient(WebView* webView)
59    : m_webView(webView)
60{
61}
62
63static WebHTMLView *getTopHTMLView(Frame* frame)
64{
65    ASSERT(frame);
66    ASSERT(frame->page());
67    return (WebHTMLView*)[[kit(&frame->page()->mainFrame()) frameView] documentView];
68}
69
70WebCore::DragDestinationAction WebDragClient::actionMaskForDrag(WebCore::DragData& dragData)
71{
72    return (WebCore::DragDestinationAction)[[m_webView _UIDelegateForwarder] webView:m_webView dragDestinationActionMaskForDraggingInfo:dragData.platformData()];
73}
74
75void WebDragClient::willPerformDragDestinationAction(WebCore::DragDestinationAction action, WebCore::DragData& dragData)
76{
77    [[m_webView _UIDelegateForwarder] webView:m_webView willPerformDragDestinationAction:(WebDragDestinationAction)action forDraggingInfo:dragData.platformData()];
78}
79
80
81WebCore::DragSourceAction WebDragClient::dragSourceActionMaskForPoint(const IntPoint& rootViewPoint)
82{
83    NSPoint viewPoint = [m_webView _convertPointFromRootView:rootViewPoint];
84    return (DragSourceAction)[[m_webView _UIDelegateForwarder] webView:m_webView dragSourceActionMaskForPoint:viewPoint];
85}
86
87void WebDragClient::willPerformDragSourceAction(WebCore::DragSourceAction action, const WebCore::IntPoint& mouseDownPoint, WebCore::DataTransfer& dataTransfer)
88{
89    [[m_webView _UIDelegateForwarder] webView:m_webView willPerformDragSourceAction:(WebDragSourceAction)action fromPoint:mouseDownPoint withPasteboard:[NSPasteboard pasteboardWithName:dataTransfer.pasteboard().name()]];
90}
91
92void WebDragClient::startDrag(DragImageRef dragImage, const IntPoint& at, const IntPoint& eventPos, DataTransfer& dataTransfer, Frame& frame, bool linkDrag)
93{
94    RetainPtr<WebHTMLView> htmlView = (WebHTMLView*)[[kit(&frame) frameView] documentView];
95    if (![htmlView.get() isKindOfClass:[WebHTMLView class]])
96        return;
97
98    NSEvent *event = linkDrag ? frame.eventHandler().currentNSEvent() : [htmlView.get() _mouseDownEvent];
99    WebHTMLView* topHTMLView = getTopHTMLView(&frame);
100    RetainPtr<WebHTMLView> topViewProtector = topHTMLView;
101
102    [topHTMLView _stopAutoscrollTimer];
103    NSPasteboard *pasteboard = [NSPasteboard pasteboardWithName:dataTransfer.pasteboard().name()];
104
105    NSImage *dragNSImage = dragImage.get();
106    WebHTMLView *sourceHTMLView = htmlView.get();
107
108    IntSize size([dragNSImage size]);
109    size.scale(1 / frame.page()->deviceScaleFactor());
110    [dragNSImage setSize:size];
111
112    id delegate = [m_webView UIDelegate];
113    SEL selector = @selector(webView:dragImage:at:offset:event:pasteboard:source:slideBack:forView:);
114    if ([delegate respondsToSelector:selector]) {
115        @try {
116            [delegate webView:m_webView dragImage:dragNSImage at:at offset:NSZeroSize event:event pasteboard:pasteboard source:sourceHTMLView slideBack:YES forView:topHTMLView];
117        } @catch (id exception) {
118            ReportDiscardedDelegateException(selector, exception);
119        }
120    } else
121#pragma clang diagnostic push
122#pragma clang diagnostic ignored "-Wdeprecated-declarations"
123        [topHTMLView dragImage:dragNSImage at:at offset:NSZeroSize event:event pasteboard:pasteboard source:sourceHTMLView slideBack:YES];
124#pragma clang diagnostic pop
125}
126
127void WebDragClient::declareAndWriteDragImage(const String& pasteboardName, Element& element, const URL& url, const String& title, WebCore::Frame* frame)
128{
129    ASSERT(pasteboardName);
130    WebHTMLView *source = getTopHTMLView(frame);
131    WebArchive *archive = [kit(&element) webArchive];
132
133    [[NSPasteboard pasteboardWithName:pasteboardName] _web_declareAndWriteDragImageForElement:kit(&element) URL:url title:title archive:archive source:source];
134}
135
136void WebDragClient::dragControllerDestroyed()
137{
138    delete this;
139}
140
141#endif // ENABLE(DRAG_SUPPORT)
142