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 COMPUTER, 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 COMPUTER, 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#import "WebArchive.h"
29#import "WebDOMOperations.h"
30#import "WebFrame.h"
31#import "WebFrameInternal.h"
32#import "WebHTMLViewInternal.h"
33#import "WebHTMLViewPrivate.h"
34#import "WebKitLogging.h"
35#import "WebKitNSStringExtras.h"
36#import "WebNSPasteboardExtras.h"
37#import "WebNSURLExtras.h"
38#import "WebStringTruncator.h"
39#import "WebUIDelegate.h"
40#import "WebUIDelegatePrivate.h"
41#import "WebViewInternal.h"
42#import <WebCore/ClipboardMac.h>
43#import <WebCore/DragData.h>
44#import <WebCore/Editor.h>
45#import <WebCore/EditorClient.h>
46#import <WebCore/EventHandler.h>
47#import <WebCore/Frame.h>
48#import <WebCore/FrameView.h>
49#import <WebCore/Image.h>
50#import <WebCore/Page.h>
51
52using namespace WebCore;
53
54WebDragClient::WebDragClient(WebView* webView)
55    : m_webView(webView)
56{
57}
58
59static WebHTMLView *getTopHTMLView(Frame* frame)
60{
61    ASSERT(frame);
62    ASSERT(frame->page());
63    return (WebHTMLView*)[[kit(frame->page()->mainFrame()) frameView] documentView];
64}
65
66WebCore::DragDestinationAction WebDragClient::actionMaskForDrag(WebCore::DragData* dragData)
67{
68    return (WebCore::DragDestinationAction)[[m_webView _UIDelegateForwarder] webView:m_webView dragDestinationActionMaskForDraggingInfo:dragData->platformData()];
69}
70
71void WebDragClient::willPerformDragDestinationAction(WebCore::DragDestinationAction action, WebCore::DragData* dragData)
72{
73    [[m_webView _UIDelegateForwarder] webView:m_webView willPerformDragDestinationAction:(WebDragDestinationAction)action forDraggingInfo:dragData->platformData()];
74}
75
76
77WebCore::DragSourceAction WebDragClient::dragSourceActionMaskForPoint(const IntPoint& windowPoint)
78{
79    NSPoint viewPoint = [m_webView convertPoint:windowPoint fromView:nil];
80    return (DragSourceAction)[[m_webView _UIDelegateForwarder] webView:m_webView dragSourceActionMaskForPoint:viewPoint];
81}
82
83void WebDragClient::willPerformDragSourceAction(WebCore::DragSourceAction action, const WebCore::IntPoint& mouseDownPoint, WebCore::Clipboard* clipboard)
84{
85    ASSERT(clipboard);
86    [[m_webView _UIDelegateForwarder] webView:m_webView willPerformDragSourceAction:(WebDragSourceAction)action fromPoint:mouseDownPoint withPasteboard:static_cast<WebCore::ClipboardMac*>(clipboard)->pasteboard()];
87}
88
89void WebDragClient::startDrag(DragImageRef dragImage, const IntPoint& at, const IntPoint& eventPos, Clipboard* clipboard, Frame* frame, bool linkDrag)
90{
91    if (!frame)
92        return;
93    ASSERT(clipboard);
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 = static_cast<ClipboardMac*>(clipboard)->pasteboard();
104
105    NSImage *dragNSImage = dragImage.get();
106    WebHTMLView *sourceHTMLView = htmlView.get();
107
108    id delegate = [m_webView UIDelegate];
109    SEL selector = @selector(webView:dragImage:at:offset:event:pasteboard:source:slideBack:forView:);
110    if ([delegate respondsToSelector:selector]) {
111        if ([m_webView _catchesDelegateExceptions]) {
112            @try {
113                [delegate webView:m_webView dragImage:dragNSImage at:at offset:NSZeroSize event:event pasteboard:pasteboard source:sourceHTMLView slideBack:YES forView:topHTMLView];
114            } @catch (id exception) {
115                ReportDiscardedDelegateException(selector, exception);
116            }
117        } else
118            [delegate webView:m_webView dragImage:dragNSImage at:at offset:NSZeroSize event:event pasteboard:pasteboard source:sourceHTMLView slideBack:YES forView:topHTMLView];
119    } else
120        [topHTMLView dragImage:dragNSImage at:at offset:NSZeroSize event:event pasteboard:pasteboard source:sourceHTMLView slideBack:YES];
121}
122
123void WebDragClient::declareAndWriteDragImage(NSPasteboard* pasteboard, DOMElement* element, NSURL* URL, NSString* title, WebCore::Frame* frame)
124{
125    ASSERT(pasteboard);
126    ASSERT(element);
127    WebHTMLView *source = getTopHTMLView(frame);
128    WebArchive *archive = [element webArchive];
129
130    [pasteboard _web_declareAndWriteDragImageForElement:element URL:URL title:title archive:archive source:source];
131}
132
133void WebDragClient::dragControllerDestroyed()
134{
135    delete this;
136}
137