2 * Copyright (C) 2010, 2011 Nokia Corporation and/or its subsidiary(-ies)
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.
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.
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.
21 #include "qdesktopwebview_p.h"
22 #include "qdesktopwebpageproxy.h"
23 #include "DrawingAreaProxyImpl.h"
24 #include "NativeWebMouseEvent.h"
25 #include "NativeWebWheelEvent.h"
26 #include <QApplication>
28 #include <QGraphicsSceneDragDropEvent>
29 #include <QGraphicsWidget>
30 #include <WebCore/DragData.h>
31 #include <WebCore/Region.h>
33 using namespace WebCore;
34 using namespace WebKit;
36 static inline Qt::DropAction dragOperationToDropAction(unsigned dragOperation)
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;
50 QDesktopWebPageProxy::QDesktopWebPageProxy(QDesktopWebViewPrivate* desktopWebView, WKContextRef context, WKPageGroupRef pageGroup)
51 : QtWebPageProxy(desktopWebView, desktopWebView, context, pageGroup)
56 PassOwnPtr<DrawingAreaProxy> QDesktopWebPageProxy::createDrawingAreaProxy()
58 return DrawingAreaProxyImpl::create(m_webPageProxy.get());
61 void QDesktopWebPageProxy::paintContent(QPainter* painter, const QRect& area)
63 // FIXME: Do something with the unpainted region?
64 WebCore::Region unpaintedRegion;
65 static_cast<DrawingAreaProxyImpl*>(m_webPageProxy->drawingArea())->paint(painter, area, unpaintedRegion);
68 void QDesktopWebPageProxy::setViewportArguments(const WebCore::ViewportArguments&)
70 // We ignore the viewport definition on the Desktop.
73 #if ENABLE(TOUCH_EVENTS)
74 void QDesktopWebPageProxy::doneWithTouchEvent(const NativeWebTouchEvent&, bool wasEventHandled)
76 // We do not handle touch on Desktop for now, the events are not supposed to be forwarded to the WebProcess.
81 bool QDesktopWebPageProxy::handleEvent(QEvent* ev)
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));
105 return QtWebPageProxy::handleEvent(ev);
108 bool QDesktopWebPageProxy::handleMouseMoveEvent(QGraphicsSceneMouseEvent* ev)
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
115 static QPointF lastPos = QPointF();
116 if (lastPos == ev->pos())
117 return ev->isAccepted();
120 m_webPageProxy->handleMouseEvent(NativeWebMouseEvent(ev, /*eventClickCount=*/0));
122 return ev->isAccepted();
125 bool QDesktopWebPageProxy::handleMousePressEvent(QGraphicsSceneMouseEvent* ev)
127 if (m_tripleClickTimer.isActive() && (ev->pos() - m_tripleClick).manhattanLength() < QApplication::startDragDistance()) {
128 m_webPageProxy->handleMouseEvent(NativeWebMouseEvent(ev, /*eventClickCount=*/3));
129 return ev->isAccepted();
132 m_webPageProxy->handleMouseEvent(NativeWebMouseEvent(ev, /*eventClickCount=*/1));
133 return ev->isAccepted();
136 bool QDesktopWebPageProxy::handleMouseReleaseEvent(QGraphicsSceneMouseEvent* ev)
138 m_webPageProxy->handleMouseEvent(NativeWebMouseEvent(ev, /*eventClickCount=*/0));
139 return ev->isAccepted();
142 bool QDesktopWebPageProxy::handleMouseDoubleClickEvent(QGraphicsSceneMouseEvent* ev)
144 m_webPageProxy->handleMouseEvent(NativeWebMouseEvent(ev, /*eventClickCount=*/2));
146 m_tripleClickTimer.start(QApplication::doubleClickInterval(), this);
147 m_tripleClick = ev->pos().toPoint();
148 return ev->isAccepted();
151 bool QDesktopWebPageProxy::handleWheelEvent(QGraphicsSceneWheelEvent* ev)
153 m_webPageProxy->handleWheelEvent(NativeWebWheelEvent(ev));
154 return ev->isAccepted();
157 bool QDesktopWebPageProxy::handleHoverMoveEvent(QHoverEvent* ev)
159 QGraphicsSceneMouseEvent me(QEvent::GraphicsSceneMouseMove);
160 me.setPos(ev->pos());
161 me.setAccepted(ev->isAccepted());
163 return handleMouseMoveEvent(&me);
166 bool QDesktopWebPageProxy::handleDragEnterEvent(QGraphicsSceneDragDropEvent* ev)
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();
176 bool QDesktopWebPageProxy::handleDragLeaveEvent(QGraphicsSceneDragDropEvent* ev)
178 bool accepted = ev->isAccepted();
180 // FIXME: Should not use QCursor::pos()
181 DragData dragData(0, IntPoint(), QCursor::pos(), DragOperationNone);
182 m_webPageProxy->dragExited(&dragData);
183 m_webPageProxy->resetDragOperation();
185 ev->setAccepted(accepted);
189 bool QDesktopWebPageProxy::handleDragMoveEvent(QGraphicsSceneDragDropEvent* ev)
191 bool accepted = ev->isAccepted();
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)
200 ev->setAccepted(accepted);
204 bool QDesktopWebPageProxy::handleDropEvent(QGraphicsSceneDragDropEvent* ev)
206 bool accepted = ev->isAccepted();
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()));
215 ev->setAccepted(accepted);
219 void QDesktopWebPageProxy::timerEvent(QTimerEvent* ev)
221 int timerId = ev->timerId();
222 if (timerId == m_tripleClickTimer.timerId())
223 m_tripleClickTimer.stop();
225 QObject::timerEvent(ev);