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#ifndef PageClientQt_h
22#define PageClientQt_h
23
24#include "QWebPageClient.h"
25#include "qgraphicswebview.h"
26#include "qwebframe.h"
27#include "qwebframe_p.h"
28#include "qwebpage.h"
29#include "qwebpage_p.h"
30#include <qgraphicsscene.h>
31#include <qgraphicsview.h>
32#include <qgraphicswidget.h>
33#include <qmetaobject.h>
34#include <qscrollbar.h>
35#include <qstyleoption.h>
36#include <qtimer.h>
37#include <qwidget.h>
38
39namespace WebCore {
40
41class PageClientQWidget final : public QWebPageClient {
42public:
43 PageClientQWidget(QWidget* newView, QWebPage* newPage)
44 : view(newView)
45 , page(newPage)
46 {
47 Q_ASSERT(view);
48 }
49 ~PageClientQWidget() final;
50
51 bool isQWidgetClient() const final { return true; }
52
53 void scroll(int dx, int dy, const QRect&) final;
54 void update(const QRect& dirtyRect) final;
55 void repaintViewport() final;
56 void setInputMethodEnabled(bool) final;
57 bool inputMethodEnabled() const final;
58 void setInputMethodHints(Qt::InputMethodHints) final;
59
60#ifndef QT_NO_CURSOR
61 QCursor cursor() const final;
62 void updateCursor(const QCursor&) final;
63#endif
64
65 QPalette palette() const final;
66 int screenNumber() const final;
67 QObject* ownerWidget() const final;
68 QRect geometryRelativeToOwnerWidget() const final;
69 QPoint mapToOwnerWindow(const QPoint&) const final;
70
71 QObject* pluginParent() const final;
72
73 QStyle* style() const final;
74
75 bool viewResizesToContentsEnabled() const final { return false; }
76
77 QRectF windowRect() const final;
78
79 void setWidgetVisible(Widget*, bool visible) final;
80 bool isViewVisible() final;
81
82 QWidget* view;
83 QWebPage* page;
84};
85
86#if !defined(QT_NO_GRAPHICSVIEW)
87// the overlay is here for one reason only: to have the scroll-bars and other
88// extra UI elements appear on top of any QGraphicsItems created by CSS compositing layers
89class QGraphicsItemOverlay final : public QGraphicsObject {
90 public:
91 QGraphicsItemOverlay(QGraphicsWidget* view, QWebPage* p)
92 : QGraphicsObject(view)
93 , q(view)
94 , page(p)
95 {
96 setPos(0, 0);
97 setFlag(QGraphicsItem::ItemUsesExtendedStyleOption, true);
98 setCacheMode(QGraphicsItem::DeviceCoordinateCache);
99 }
100
101 QRectF boundingRect() const final
102 {
103 return q->boundingRect();
104 }
105
106 void paint(QPainter* painter, const QStyleOptionGraphicsItem* options, QWidget*) final
107 {
108 page->mainFrame()->render(painter, static_cast<QWebFrame::RenderLayer>(QWebFrame::AllLayers&(~QWebFrame::ContentsLayer)), options->exposedRect.toRect());
109 }
110
111 void prepareGraphicsItemGeometryChange()
112 {
113 prepareGeometryChange();
114 }
115
116 QGraphicsWidget* q;
117 QWebPage* page;
118};
119
120
121class PageClientQGraphicsWidget final : public QWebPageClient {
122public:
123 PageClientQGraphicsWidget(QGraphicsWebView* newView, QWebPage* newPage)
124 : view(newView)
125 , page(newPage)
126 , viewResizesToContents(false)
127 , overlay(0)
128 {
129 Q_ASSERT(view);
130 // the overlay and stays alive for the lifetime of
131 // this QGraphicsWebView as the scrollbars are needed when there's no compositing
132 view->setFlag(QGraphicsItem::ItemUsesExtendedStyleOption);
133 }
134
135 ~PageClientQGraphicsWidget() final;
136
137 bool isQWidgetClient() const final { return false; }
138
139 void scroll(int dx, int dy, const QRect&) final;
140 void update(const QRect& dirtyRect) final;
141 void repaintViewport() final;
142 void setInputMethodEnabled(bool) final;
143 bool inputMethodEnabled() const final;
144 void setInputMethodHints(Qt::InputMethodHints) final;
145
146#ifndef QT_NO_CURSOR
147 QCursor cursor() const final;
148 void updateCursor(const QCursor&) final;
149#endif
150
151 QPalette palette() const final;
152 int screenNumber() const final;
153 QObject* ownerWidget() const final;
154 QRect geometryRelativeToOwnerWidget() const final;
155 QPoint mapToOwnerWindow(const QPoint&) const final;
156
157 QObject* pluginParent() const final;
158
159 QStyle* style() const final;
160
161 bool viewResizesToContentsEnabled() const final { return viewResizesToContents; }
162
163 void setWidgetVisible(Widget*, bool) final;
164 bool isViewVisible() final;
165
166#if USE(TILED_BACKING_STORE)
167 virtual QRectF graphicsItemVisibleRect() const;
168#endif
169
170 bool makeOpenGLContextCurrentIfAvailable() final;
171 QOpenGLContext* openGLContextIfAvailable() final;
172
173 QRectF windowRect() const final;
174
175 QGraphicsView* firstGraphicsView() const;
176
177 QGraphicsWebView* view;
178 QWebPage* page;
179 bool viewResizesToContents;
180
181 // the overlay gets instantiated when the root layer is attached, and get deleted when it's detached
182 QGraphicsItemOverlay* overlay;
183
184 // we need to put the root graphics layer behind the overlay (which contains the scrollbar)
185 enum { RootGraphicsLayerZValue, OverlayZValue };
186};
187#endif // QT_NO_GRAPHICSVIEW
188
189}
190#endif // PageClientQt
191