Remove an unneeded argument from FrameLoaderClient::download
[WebKit-https.git] / Source / WebKit / qt / WebCoreSupport / PageClientQt.cpp
1 /*
2  * Copyright (C) 2010 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 library 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 library; 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 "config.h"
22 #include "PageClientQt.h"
23
24 #include <QGraphicsScene>
25 #include <QGraphicsView>
26 #if defined(Q_WS_X11)
27 #include <QX11Info>
28 #endif
29
30 #if USE(ACCELERATED_COMPOSITING) && USE(TEXTURE_MAPPER)
31 #include "TextureMapperQt.h"
32 #include "texmap/TextureMapperNode.h"
33
34 #if USE(TEXTURE_MAPPER_GL)
35 #include "opengl/TextureMapperGL.h"
36 #endif
37 #endif
38
39 namespace WebCore {
40
41 #if USE(ACCELERATED_COMPOSITING) && USE(TEXTURE_MAPPER)
42 TextureMapperNodeClientQt::TextureMapperNodeClientQt(QWebFrame* frame, GraphicsLayer* layer)
43     : m_frame(frame)
44     , m_rootGraphicsLayer(GraphicsLayer::create(0))
45 {
46     m_frame->d->rootTextureMapperNode = rootNode();
47     m_rootGraphicsLayer->addChild(layer);
48     m_rootGraphicsLayer->setDrawsContent(false);
49     m_rootGraphicsLayer->setMasksToBounds(false);
50     m_rootGraphicsLayer->setSize(IntSize(1, 1));
51 }
52
53 void TextureMapperNodeClientQt::setTextureMapper(const PassOwnPtr<TextureMapper>& textureMapper)
54 {
55     m_frame->d->textureMapper = textureMapper;
56     m_frame->d->rootTextureMapperNode->setTextureMapper(m_frame->d->textureMapper.get());
57 }
58
59 TextureMapperNodeClientQt::~TextureMapperNodeClientQt()
60 {
61     m_frame->d->rootTextureMapperNode = 0;
62 }
63
64 void TextureMapperNodeClientQt::syncRootLayer()
65 {
66     m_rootGraphicsLayer->syncCompositingStateForThisLayerOnly();
67 }
68
69 TextureMapperNode* TextureMapperNodeClientQt::rootNode()
70 {
71     return toTextureMapperNode(m_rootGraphicsLayer.get());
72 }
73
74
75 void PageClientQWidget::setRootGraphicsLayer(GraphicsLayer* layer)
76 {
77     if (layer) {
78         textureMapperNodeClient = adoptPtr(new TextureMapperNodeClientQt(page->mainFrame(), layer));
79         textureMapperNodeClient->setTextureMapper(adoptPtr(new TextureMapperQt));
80         textureMapperNodeClient->syncRootLayer();
81         return;
82     }
83     textureMapperNodeClient.clear();
84 }
85
86 void PageClientQWidget::markForSync(bool scheduleSync)
87 {
88     if (syncTimer.isActive())
89         return;
90     syncTimer.startOneShot(0);
91 }
92
93 void PageClientQWidget::syncLayers(Timer<PageClientQWidget>*)
94 {
95     if (textureMapperNodeClient)
96         textureMapperNodeClient->syncRootLayer();
97     QWebFramePrivate::core(page->mainFrame())->view()->syncCompositingStateIncludingSubframes();
98     if (!textureMapperNodeClient)
99         return;
100     if (textureMapperNodeClient->rootNode()->descendantsOrSelfHaveRunningAnimations() && !syncTimer.isActive())
101         syncTimer.startOneShot(1.0 / 60.0);
102     update(view->rect());
103 }
104 #endif
105
106 void PageClientQWidget::scroll(int dx, int dy, const QRect& rectToScroll)
107 {
108     view->scroll(qreal(dx), qreal(dy), rectToScroll);
109 }
110
111 void PageClientQWidget::update(const QRect & dirtyRect)
112 {
113     view->update(dirtyRect);
114 }
115
116 void PageClientQWidget::setInputMethodEnabled(bool enable)
117 {
118     view->setAttribute(Qt::WA_InputMethodEnabled, enable);
119 }
120
121 bool PageClientQWidget::inputMethodEnabled() const
122 {
123     return view->testAttribute(Qt::WA_InputMethodEnabled);
124 }
125
126 void PageClientQWidget::setInputMethodHints(Qt::InputMethodHints hints)
127 {
128     view->setInputMethodHints(hints);
129 }
130
131 PageClientQWidget::~PageClientQWidget()
132 {
133 }
134
135 #ifndef QT_NO_CURSOR
136 QCursor PageClientQWidget::cursor() const
137 {
138     return view->cursor();
139 }
140
141 void PageClientQWidget::updateCursor(const QCursor& cursor)
142 {
143     view->setCursor(cursor);
144 }
145 #endif
146
147 QPalette PageClientQWidget::palette() const
148 {
149     return view->palette();
150 }
151
152 int PageClientQWidget::screenNumber() const
153 {
154 #if defined(Q_WS_X11)
155     return view->x11Info().screen();
156 #endif
157     return 0;
158 }
159
160 QWidget* PageClientQWidget::ownerWidget() const
161 {
162     return view;
163 }
164
165 QRect PageClientQWidget::geometryRelativeToOwnerWidget() const
166 {
167     return view->geometry();
168 }
169
170 QObject* PageClientQWidget::pluginParent() const
171 {
172     return view;
173 }
174
175 QStyle* PageClientQWidget::style() const
176 {
177     return view->style();
178 }
179
180 QRectF PageClientQWidget::windowRect() const
181 {
182     return QRectF(view->window()->geometry());
183 }
184
185 #if !defined(QT_NO_GRAPHICSVIEW)
186 PageClientQGraphicsWidget::~PageClientQGraphicsWidget()
187 {
188     delete overlay;
189 #if USE(ACCELERATED_COMPOSITING) && !USE(TEXTURE_MAPPER)
190     if (!rootGraphicsLayer)
191         return;
192     // we don't need to delete the root graphics layer. The lifecycle is managed in GraphicsLayerQt.cpp.
193     rootGraphicsLayer.data()->setParentItem(0);
194     view->scene()->removeItem(rootGraphicsLayer.data());
195 #endif
196 }
197
198 void PageClientQGraphicsWidget::scroll(int dx, int dy, const QRect& rectToScroll)
199 {
200     view->scroll(qreal(dx), qreal(dy), rectToScroll);
201 }
202
203 void PageClientQGraphicsWidget::update(const QRect& dirtyRect)
204 {
205     view->update(dirtyRect);
206
207     createOrDeleteOverlay();
208     if (overlay)
209         overlay->update(QRectF(dirtyRect));
210 #if USE(ACCELERATED_COMPOSITING) && !USE(TEXTURE_MAPPER)
211     syncLayers();
212 #endif
213 }
214
215 void PageClientQGraphicsWidget::createOrDeleteOverlay()
216 {
217     // We don't use an overlay with TextureMapper. Instead, the overlay is drawn inside QWebFrame.
218 #if !USE(TEXTURE_MAPPER)
219     bool useOverlay = false;
220     if (!viewResizesToContents) {
221 #if USE(ACCELERATED_COMPOSITING)
222         useOverlay = useOverlay || rootGraphicsLayer;
223 #endif
224 #if USE(TILED_BACKING_STORE)
225         useOverlay = useOverlay || QWebFramePrivate::core(page->mainFrame())->tiledBackingStore();
226 #endif
227     }
228     if (useOverlay == !!overlay)
229         return;
230
231     if (useOverlay) {
232         overlay = new QGraphicsItemOverlay(view, page);
233         overlay->setZValue(OverlayZValue);
234     } else {
235         // Changing the overlay might be done inside paint events.
236         overlay->deleteLater();
237         overlay = 0;
238     }
239 #endif // !USE(TEXTURE_MAPPER)
240 }
241
242 #if USE(ACCELERATED_COMPOSITING)
243 void PageClientQGraphicsWidget::syncLayers()
244 {
245 #if USE(TEXTURE_MAPPER)
246     if (textureMapperNodeClient)
247         textureMapperNodeClient->syncRootLayer();
248 #endif
249
250     QWebFramePrivate::core(page->mainFrame())->view()->syncCompositingStateIncludingSubframes();
251
252 #if USE(TEXTURE_MAPPER)
253     if (!textureMapperNodeClient)
254         return;
255
256     if (textureMapperNodeClient->rootNode()->descendantsOrSelfHaveRunningAnimations() && !syncTimer.isActive())
257         syncTimer.startOneShot(1.0 / 60.0);
258     update(view->boundingRect().toAlignedRect());
259 #endif
260 }
261
262 #if USE(TEXTURE_MAPPER)
263 void PageClientQGraphicsWidget::setRootGraphicsLayer(GraphicsLayer* layer)
264 {
265     if (layer) {
266         textureMapperNodeClient = adoptPtr(new TextureMapperNodeClientQt(page->mainFrame(), layer));
267 #if USE(TEXTURE_MAPPER_GL)
268         QGraphicsView* graphicsView = view->scene()->views()[0];
269         if (graphicsView && graphicsView->viewport() && graphicsView->viewport()->inherits("QGLWidget")) {
270             textureMapperNodeClient->setTextureMapper(TextureMapperGL::create());
271             return;
272         }
273 #endif
274         textureMapperNodeClient->setTextureMapper(TextureMapperQt::create());
275         return;
276     }
277     textureMapperNodeClient.clear();
278 }
279 #else
280 void PageClientQGraphicsWidget::setRootGraphicsLayer(GraphicsLayer* layer)
281 {
282     if (rootGraphicsLayer) {
283         rootGraphicsLayer.data()->setParentItem(0);
284         view->scene()->removeItem(rootGraphicsLayer.data());
285         QWebFramePrivate::core(page->mainFrame())->view()->syncCompositingStateIncludingSubframes();
286     }
287
288     rootGraphicsLayer = layer ? layer->platformLayer() : 0;
289
290     if (rootGraphicsLayer) {
291         rootGraphicsLayer.data()->setParentItem(view);
292         rootGraphicsLayer.data()->setZValue(RootGraphicsLayerZValue);
293     }
294     createOrDeleteOverlay();
295 }
296 #endif
297
298 void PageClientQGraphicsWidget::markForSync(bool scheduleSync)
299 {
300     if (syncTimer.isActive())
301         return;
302     syncTimer.startOneShot(0);
303 }
304
305 #endif
306
307 #if USE(TILED_BACKING_STORE)
308 void PageClientQGraphicsWidget::updateTiledBackingStoreScale()
309 {
310     WebCore::TiledBackingStore* backingStore = QWebFramePrivate::core(page->mainFrame())->tiledBackingStore();
311     if (!backingStore)
312         return;
313     backingStore->setContentsScale(view->scale());
314 }
315 #endif
316
317 void PageClientQGraphicsWidget::setInputMethodEnabled(bool enable)
318 {
319     view->setFlag(QGraphicsItem::ItemAcceptsInputMethod, enable);
320 }
321
322 bool PageClientQGraphicsWidget::inputMethodEnabled() const
323 {
324     return view->flags() & QGraphicsItem::ItemAcceptsInputMethod;
325 }
326
327 void PageClientQGraphicsWidget::setInputMethodHints(Qt::InputMethodHints hints)
328 {
329     view->setInputMethodHints(hints);
330 }
331
332 #ifndef QT_NO_CURSOR
333 QCursor PageClientQGraphicsWidget::cursor() const
334 {
335     return view->cursor();
336 }
337
338 void PageClientQGraphicsWidget::updateCursor(const QCursor& cursor)
339 {
340     view->setCursor(cursor);
341 }
342 #endif
343
344 QPalette PageClientQGraphicsWidget::palette() const
345 {
346     return view->palette();
347 }
348
349 int PageClientQGraphicsWidget::screenNumber() const
350 {
351 #if defined(Q_WS_X11)
352     if (QGraphicsScene* scene = view->scene()) {
353         const QList<QGraphicsView*> views = scene->views();
354
355         if (!views.isEmpty())
356             return views.at(0)->x11Info().screen();
357     }
358 #endif
359
360     return 0;
361 }
362
363 QWidget* PageClientQGraphicsWidget::ownerWidget() const
364 {
365     if (QGraphicsScene* scene = view->scene()) {
366         const QList<QGraphicsView*> views = scene->views();
367         return views.value(0);
368     }
369     return 0;
370 }
371
372 QRect PageClientQGraphicsWidget::geometryRelativeToOwnerWidget() const
373 {
374     if (!view->scene())
375         return QRect();
376
377     QList<QGraphicsView*> views = view->scene()->views();
378     if (views.isEmpty())
379         return QRect();
380
381     QGraphicsView* graphicsView = views.at(0);
382     return graphicsView->mapFromScene(view->boundingRect()).boundingRect();
383 }
384
385 #if USE(TILED_BACKING_STORE)
386 QRectF PageClientQGraphicsWidget::graphicsItemVisibleRect() const
387 {
388     if (!view->scene())
389         return QRectF();
390
391     QList<QGraphicsView*> views = view->scene()->views();
392     if (views.isEmpty())
393         return QRectF();
394
395     QGraphicsView* graphicsView = views.at(0);
396     int xOffset = graphicsView->horizontalScrollBar()->value();
397     int yOffset = graphicsView->verticalScrollBar()->value();
398     return view->mapRectFromScene(QRectF(QPointF(xOffset, yOffset), graphicsView->viewport()->size()));
399 }
400 #endif
401
402 QObject* PageClientQGraphicsWidget::pluginParent() const
403 {
404     return view;
405 }
406
407 QStyle* PageClientQGraphicsWidget::style() const
408 {
409     return view->style();
410 }
411
412 QRectF PageClientQGraphicsWidget::windowRect() const
413 {
414     if (!view->scene())
415         return QRectF();
416
417     // The sceneRect is a good approximation of the size of the application, independent of the view.
418     return view->scene()->sceneRect();
419 }
420 #endif // QT_NO_GRAPHICSVIEW
421
422 } // namespace WebCore