initial import
[vuplus_webkit] / Source / WebKit2 / UIProcess / qt / qdesktopwebpageproxy.cpp
1 /*
2  * Copyright (C) 2010, 2011 Nokia Corporation and/or its subsidiary(-ies)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this program; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19  */
20
21 #include "qdesktopwebview_p.h"
22 #include "qdesktopwebpageproxy.h"
23 #include "DrawingAreaProxyImpl.h"
24 #include "NativeWebMouseEvent.h"
25 #include "NativeWebWheelEvent.h"
26 #include <QApplication>
27 #include <QEvent>
28 #include <QGraphicsSceneDragDropEvent>
29 #include <QGraphicsWidget>
30 #include <WebCore/DragData.h>
31 #include <WebCore/Region.h>
32
33 using namespace WebCore;
34 using namespace WebKit;
35
36 static inline Qt::DropAction dragOperationToDropAction(unsigned dragOperation)
37 {
38     Qt::DropAction result = Qt::IgnoreAction;
39     if (dragOperation & DragOperationCopy)
40         result = Qt::CopyAction;
41     else if (dragOperation & DragOperationMove)
42         result = Qt::MoveAction;
43     else if (dragOperation & DragOperationGeneric)
44         result = Qt::MoveAction;
45     else if (dragOperation & DragOperationLink)
46         result = Qt::LinkAction;
47     return result;
48 }
49
50 QDesktopWebPageProxy::QDesktopWebPageProxy(QDesktopWebViewPrivate* desktopWebView, WKContextRef context, WKPageGroupRef pageGroup)
51     : QtWebPageProxy(desktopWebView, desktopWebView, context, pageGroup)
52 {
53     init();
54 }
55
56 PassOwnPtr<DrawingAreaProxy> QDesktopWebPageProxy::createDrawingAreaProxy()
57 {
58     return DrawingAreaProxyImpl::create(m_webPageProxy.get());
59 }
60
61 void QDesktopWebPageProxy::paintContent(QPainter* painter, const QRect& area)
62 {
63     // FIXME: Do something with the unpainted region?
64     WebCore::Region unpaintedRegion;
65     static_cast<DrawingAreaProxyImpl*>(m_webPageProxy->drawingArea())->paint(painter, area, unpaintedRegion);
66 }
67
68 void QDesktopWebPageProxy::setViewportArguments(const WebCore::ViewportArguments&)
69 {
70     // We ignore the viewport definition on the Desktop.
71 }
72
73 #if ENABLE(TOUCH_EVENTS)
74 void QDesktopWebPageProxy::doneWithTouchEvent(const NativeWebTouchEvent&, bool wasEventHandled)
75 {
76     // We do not handle touch on Desktop for now, the events are not supposed to be forwarded to the WebProcess.
77     ASSERT_NOT_REACHED();
78 }
79 #endif
80
81 bool QDesktopWebPageProxy::handleEvent(QEvent* ev)
82 {
83     switch (ev->type()) {
84     case QEvent::GraphicsSceneMouseMove:
85         return handleMouseMoveEvent(reinterpret_cast<QGraphicsSceneMouseEvent*>(ev));
86     case QEvent::GraphicsSceneMousePress:
87         return handleMousePressEvent(reinterpret_cast<QGraphicsSceneMouseEvent*>(ev));
88     case QEvent::GraphicsSceneMouseRelease:
89         return handleMouseReleaseEvent(reinterpret_cast<QGraphicsSceneMouseEvent*>(ev));
90     case QEvent::GraphicsSceneMouseDoubleClick:
91         return handleMouseDoubleClickEvent(reinterpret_cast<QGraphicsSceneMouseEvent*>(ev));
92     case QEvent::GraphicsSceneWheel:
93         return handleWheelEvent(reinterpret_cast<QGraphicsSceneWheelEvent*>(ev));
94     case QEvent::HoverMove:
95         return handleHoverMoveEvent(reinterpret_cast<QHoverEvent*>(ev));
96     case QEvent::GraphicsSceneDragEnter:
97         return handleDragEnterEvent(reinterpret_cast<QGraphicsSceneDragDropEvent*>(ev));
98     case QEvent::GraphicsSceneDragLeave:
99         return handleDragLeaveEvent(reinterpret_cast<QGraphicsSceneDragDropEvent*>(ev));
100     case QEvent::GraphicsSceneDragMove:
101         return handleDragMoveEvent(reinterpret_cast<QGraphicsSceneDragDropEvent*>(ev));
102     case QEvent::GraphicsSceneDrop:
103         return handleDropEvent(reinterpret_cast<QGraphicsSceneDragDropEvent*>(ev));
104     }
105     return QtWebPageProxy::handleEvent(ev);
106 }
107
108 bool QDesktopWebPageProxy::handleMouseMoveEvent(QGraphicsSceneMouseEvent* ev)
109 {
110     // For some reason mouse press results in mouse hover (which is
111     // converted to mouse move for WebKit). We ignore these hover
112     // events by comparing lastPos with newPos.
113     // NOTE: lastPos from the event always comes empty, so we work
114     // around that here.
115     static QPointF lastPos = QPointF();
116     if (lastPos == ev->pos())
117         return ev->isAccepted();
118     lastPos = ev->pos();
119
120     m_webPageProxy->handleMouseEvent(NativeWebMouseEvent(ev, /*eventClickCount=*/0));
121
122     return ev->isAccepted();
123 }
124
125 bool QDesktopWebPageProxy::handleMousePressEvent(QGraphicsSceneMouseEvent* ev)
126 {
127     if (m_tripleClickTimer.isActive() && (ev->pos() - m_tripleClick).manhattanLength() < QApplication::startDragDistance()) {
128         m_webPageProxy->handleMouseEvent(NativeWebMouseEvent(ev, /*eventClickCount=*/3));
129         return ev->isAccepted();
130     }
131
132     m_webPageProxy->handleMouseEvent(NativeWebMouseEvent(ev, /*eventClickCount=*/1));
133     return ev->isAccepted();
134 }
135
136 bool QDesktopWebPageProxy::handleMouseReleaseEvent(QGraphicsSceneMouseEvent* ev)
137 {
138     m_webPageProxy->handleMouseEvent(NativeWebMouseEvent(ev, /*eventClickCount=*/0));
139     return ev->isAccepted();
140 }
141
142 bool QDesktopWebPageProxy::handleMouseDoubleClickEvent(QGraphicsSceneMouseEvent* ev)
143 {
144     m_webPageProxy->handleMouseEvent(NativeWebMouseEvent(ev, /*eventClickCount=*/2));
145
146     m_tripleClickTimer.start(QApplication::doubleClickInterval(), this);
147     m_tripleClick = ev->pos().toPoint();
148     return ev->isAccepted();
149 }
150
151 bool QDesktopWebPageProxy::handleWheelEvent(QGraphicsSceneWheelEvent* ev)
152 {
153     m_webPageProxy->handleWheelEvent(NativeWebWheelEvent(ev));
154     return ev->isAccepted();
155 }
156
157 bool QDesktopWebPageProxy::handleHoverMoveEvent(QHoverEvent* ev)
158 {
159     QGraphicsSceneMouseEvent me(QEvent::GraphicsSceneMouseMove);
160     me.setPos(ev->pos());
161     me.setAccepted(ev->isAccepted());
162
163     return handleMouseMoveEvent(&me);
164 }
165
166 bool QDesktopWebPageProxy::handleDragEnterEvent(QGraphicsSceneDragDropEvent* ev)
167 {
168     m_webPageProxy->resetDragOperation();
169     // FIXME: Should not use QCursor::pos()
170     DragData dragData(ev->mimeData(), ev->pos().toPoint(), QCursor::pos(), dropActionToDragOperation(ev->possibleActions()));
171     m_webPageProxy->dragEntered(&dragData);
172     ev->acceptProposedAction();
173     return true;
174 }
175
176 bool QDesktopWebPageProxy::handleDragLeaveEvent(QGraphicsSceneDragDropEvent* ev)
177 {
178     bool accepted = ev->isAccepted();
179
180     // FIXME: Should not use QCursor::pos()
181     DragData dragData(0, IntPoint(), QCursor::pos(), DragOperationNone);
182     m_webPageProxy->dragExited(&dragData);
183     m_webPageProxy->resetDragOperation();
184
185     ev->setAccepted(accepted);
186     return accepted;
187 }
188
189 bool QDesktopWebPageProxy::handleDragMoveEvent(QGraphicsSceneDragDropEvent* ev)
190 {
191     bool accepted = ev->isAccepted();
192
193     // FIXME: Should not use QCursor::pos()
194     DragData dragData(ev->mimeData(), ev->pos().toPoint(), QCursor::pos(), dropActionToDragOperation(ev->possibleActions()));
195     m_webPageProxy->dragUpdated(&dragData);
196     ev->setDropAction(dragOperationToDropAction(m_webPageProxy->dragOperation()));
197     if (m_webPageProxy->dragOperation() != DragOperationNone)
198         ev->accept();
199
200     ev->setAccepted(accepted);
201     return accepted;
202 }
203
204 bool QDesktopWebPageProxy::handleDropEvent(QGraphicsSceneDragDropEvent* ev)
205 {
206     bool accepted = ev->isAccepted();
207
208     // FIXME: Should not use QCursor::pos()
209     DragData dragData(ev->mimeData(), ev->pos().toPoint(), QCursor::pos(), dropActionToDragOperation(ev->possibleActions()));
210     SandboxExtension::Handle handle;
211     m_webPageProxy->performDrag(&dragData, String(), handle);
212     ev->setDropAction(dragOperationToDropAction(m_webPageProxy->dragOperation()));
213     ev->accept();
214
215     ev->setAccepted(accepted);
216     return accepted;
217 }
218
219 void QDesktopWebPageProxy::timerEvent(QTimerEvent* ev)
220 {
221     int timerId = ev->timerId();
222     if (timerId == m_tripleClickTimer.timerId())
223         m_tripleClickTimer.stop();
224     else
225         QObject::timerEvent(ev);
226 }