• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "WebNSPasteboardExtras.h"
36#import "WebNSURLExtras.h"
37#import "WebUIDelegate.h"
38#import "WebUIDelegatePrivate.h"
39#import "WebViewInternal.h"
40#import <WebCore/ClipboardMac.h>
41#import <WebCore/DragData.h>
42#import <WebCore/EventHandler.h>
43#import <WebCore/Frame.h>
44#import <WebCore/FrameView.h>
45#import <WebCore/Image.h>
46#import <WebCore/Page.h>
47
48using namespace WebCore;
49
50WebDragClient::WebDragClient(WebView* webView)
51    : m_webView(webView)
52{
53}
54
55static WebHTMLView *getTopHTMLView(Frame* frame)
56{
57    ASSERT(frame);
58    ASSERT(frame->page());
59    return (WebHTMLView*)[[kit(frame->page()->mainFrame()) frameView] documentView];
60}
61
62WebCore::DragDestinationAction WebDragClient::actionMaskForDrag(WebCore::DragData* dragData)
63{
64    return (WebCore::DragDestinationAction)[[m_webView _UIDelegateForwarder] webView:m_webView dragDestinationActionMaskForDraggingInfo:dragData->platformData()];
65}
66
67void WebDragClient::willPerformDragDestinationAction(WebCore::DragDestinationAction action, WebCore::DragData* dragData)
68{
69    [[m_webView _UIDelegateForwarder] webView:m_webView willPerformDragDestinationAction:(WebDragDestinationAction)action forDraggingInfo:dragData->platformData()];
70}
71
72
73WebCore::DragSourceAction WebDragClient::dragSourceActionMaskForPoint(const IntPoint& windowPoint)
74{
75    NSPoint viewPoint = [m_webView convertPoint:windowPoint fromView:nil];
76    return (DragSourceAction)[[m_webView _UIDelegateForwarder] webView:m_webView dragSourceActionMaskForPoint:viewPoint];
77}
78
79void WebDragClient::willPerformDragSourceAction(WebCore::DragSourceAction action, const WebCore::IntPoint& mouseDownPoint, WebCore::Clipboard* clipboard)
80{
81    ASSERT(clipboard);
82    [[m_webView _UIDelegateForwarder] webView:m_webView willPerformDragSourceAction:(WebDragSourceAction)action fromPoint:mouseDownPoint withPasteboard:static_cast<WebCore::ClipboardMac*>(clipboard)->pasteboard()];
83}
84
85void WebDragClient::startDrag(DragImageRef dragImage, const IntPoint& at, const IntPoint& eventPos, Clipboard* clipboard, Frame* frame, bool linkDrag)
86{
87    if (!frame)
88        return;
89    ASSERT(clipboard);
90    RetainPtr<WebHTMLView> htmlView = (WebHTMLView*)[[kit(frame) frameView] documentView];
91    if (![htmlView.get() isKindOfClass:[WebHTMLView class]])
92        return;
93
94    NSEvent *event = linkDrag ? frame->eventHandler()->currentNSEvent() : [htmlView.get() _mouseDownEvent];
95    WebHTMLView* topHTMLView = getTopHTMLView(frame);
96    RetainPtr<WebHTMLView> topViewProtector = topHTMLView;
97
98    [topHTMLView _stopAutoscrollTimer];
99    NSPasteboard *pasteboard = static_cast<ClipboardMac*>(clipboard)->pasteboard();
100
101    NSImage *dragNSImage = dragImage.get();
102    WebHTMLView *sourceHTMLView = htmlView.get();
103
104    id delegate = [m_webView UIDelegate];
105    SEL selector = @selector(webView:dragImage:at:offset:event:pasteboard:source:slideBack:forView:);
106    if ([delegate respondsToSelector:selector]) {
107        if ([m_webView _catchesDelegateExceptions]) {
108            @try {
109                [delegate webView:m_webView dragImage:dragNSImage at:at offset:NSZeroSize event:event pasteboard:pasteboard source:sourceHTMLView slideBack:YES forView:topHTMLView];
110            } @catch (id exception) {
111                ReportDiscardedDelegateException(selector, exception);
112            }
113        } else
114            [delegate webView:m_webView dragImage:dragNSImage at:at offset:NSZeroSize event:event pasteboard:pasteboard source:sourceHTMLView slideBack:YES forView:topHTMLView];
115    } else
116        [topHTMLView dragImage:dragNSImage at:at offset:NSZeroSize event:event pasteboard:pasteboard source:sourceHTMLView slideBack:YES];
117}
118
119DragImageRef WebDragClient::createDragImageForLink(KURL& url, const String& title, Frame* frame)
120{
121    if (!frame)
122        return nil;
123    WebHTMLView *htmlView = (WebHTMLView *)[[kit(frame) frameView] documentView];
124    NSString *label = 0;
125    if (!title.isEmpty())
126        label = title;
127    NSURL *cocoaURL = url;
128    return [htmlView _dragImageForURL:[cocoaURL _web_userVisibleString] withLabel:label];
129}
130
131
132void WebDragClient::declareAndWriteDragImage(NSPasteboard* pasteboard, DOMElement* element, NSURL* URL, NSString* title, WebCore::Frame* frame)
133{
134    ASSERT(pasteboard);
135    ASSERT(element);
136    WebHTMLView *source = getTopHTMLView(frame);
137    WebArchive *archive = [element webArchive];
138
139    [pasteboard _web_declareAndWriteDragImageForElement:element URL:URL title:title archive:archive source:source];
140}
141
142void WebDragClient::dragControllerDestroyed()
143{
144    delete this;
145}
146