Git init
[framework/web/webkit-efl.git] / Source / WebKit / qt / WebCoreSupport / PageClientQt.h
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 "FrameView.h"
25 #include "GraphicsContext.h"
26 #include "IntRect.h"
27 #include "QWebPageClient.h"
28 #include "TiledBackingStore.h"
29 #include "qgraphicswebview.h"
30 #include "qwebframe.h"
31 #include "qwebframe_p.h"
32 #include "qwebpage.h"
33 #include "qwebpage_p.h"
34 #include <qgraphicsscene.h>
35 #include <qgraphicsview.h>
36 #include <qgraphicswidget.h>
37 #include <qscrollbar.h>
38 #include <qstyleoption.h>
39 #include <qwidget.h>
40 #include <QtCore/qmetaobject.h>
41
42 #include <Settings.h>
43
44 namespace WebCore {
45
46 #if USE(ACCELERATED_COMPOSITING) && USE(TEXTURE_MAPPER)
47 class TextureMapperNodeClientQt {
48 public:
49     TextureMapperNodeClientQt(QWebFrame*, GraphicsLayer*);
50     virtual ~TextureMapperNodeClientQt();
51     void setTextureMapper(const PassOwnPtr<TextureMapper>&);
52     void syncRootLayer();
53     TextureMapperNode* rootNode();
54
55 private:
56     QWebFrame* m_frame;
57     OwnPtr<GraphicsLayer> m_rootGraphicsLayer;
58 };
59 #endif
60
61 class PageClientQWidget : public QWebPageClient {
62 public:
63     PageClientQWidget(QWidget* newView, QWebPage* newPage)
64         : view(newView)
65         , page(newPage)
66 #if USE(ACCELERATED_COMPOSITING) && USE(TEXTURE_MAPPER)
67         , syncTimer(this, &PageClientQWidget::syncLayers)
68 #endif
69     {
70         Q_ASSERT(view);
71     }
72     virtual ~PageClientQWidget();
73
74     virtual bool isQWidgetClient() const { return true; }
75
76     virtual void scroll(int dx, int dy, const QRect&);
77     virtual void update(const QRect& dirtyRect);
78     virtual void setInputMethodEnabled(bool enable);
79     virtual bool inputMethodEnabled() const;
80     virtual void setInputMethodHints(Qt::InputMethodHints hints);
81
82 #ifndef QT_NO_CURSOR
83     virtual QCursor cursor() const;
84     virtual void updateCursor(const QCursor& cursor);
85 #endif
86
87     virtual QPalette palette() const;
88     virtual int screenNumber() const;
89     virtual QWidget* ownerWidget() const;
90     virtual QRect geometryRelativeToOwnerWidget() const;
91
92     virtual QObject* pluginParent() const;
93
94     virtual QStyle* style() const;
95
96     virtual bool viewResizesToContentsEnabled() const { return false; }
97
98     virtual QRectF windowRect() const;
99
100     QWidget* view;
101     QWebPage* page;
102
103 #if USE(ACCELERATED_COMPOSITING) && USE(TEXTURE_MAPPER)
104     virtual void setRootGraphicsLayer(GraphicsLayer*);
105     virtual void markForSync(bool scheduleSync);
106     void syncLayers(Timer<PageClientQWidget>*);
107 #endif
108
109 #if USE(ACCELERATED_COMPOSITING) && USE(TEXTURE_MAPPER)
110     virtual bool allowsAcceleratedCompositing() const { return true; }
111 #else
112     virtual bool allowsAcceleratedCompositing() const { return false; }
113 #endif
114
115 #if USE(ACCELERATED_COMPOSITING) && USE(TEXTURE_MAPPER)
116     Timer<PageClientQWidget> syncTimer;
117     OwnPtr<TextureMapperNodeClientQt> textureMapperNodeClient;
118 #endif
119 };
120
121 #if !defined(QT_NO_GRAPHICSVIEW)
122 // the overlay is here for one reason only: to have the scroll-bars and other
123 // extra UI elements appear on top of any QGraphicsItems created by CSS compositing layers
124 class QGraphicsItemOverlay : public QGraphicsObject {
125     public:
126     QGraphicsItemOverlay(QGraphicsWidget* view, QWebPage* p)
127             :QGraphicsObject(view)
128             , q(view)
129             , page(p)
130     {
131         setPos(0, 0);
132         setFlag(QGraphicsItem::ItemUsesExtendedStyleOption, true);
133         setCacheMode(QGraphicsItem::DeviceCoordinateCache);
134     }
135
136     QRectF boundingRect() const
137     {
138         return q->boundingRect();
139     }
140
141     void paint(QPainter* painter, const QStyleOptionGraphicsItem* options, QWidget*)
142     {
143         page->mainFrame()->render(painter, static_cast<QWebFrame::RenderLayer>(QWebFrame::AllLayers&(~QWebFrame::ContentsLayer)), options->exposedRect.toRect());
144     }
145
146     void prepareGraphicsItemGeometryChange()
147     {
148         prepareGeometryChange();
149     }
150
151     QGraphicsWidget* q;
152     QWebPage* page;
153 };
154
155
156 class PageClientQGraphicsWidget : public QWebPageClient {
157 public:
158     PageClientQGraphicsWidget(QGraphicsWebView* newView, QWebPage* newPage)
159         : view(newView)
160         , page(newPage)
161         , viewResizesToContents(false)
162 #if USE(ACCELERATED_COMPOSITING)
163         , syncTimer(this, &PageClientQGraphicsWidget::syncLayersTimeout)
164         , shouldSync(false)
165 #endif
166         , overlay(0)
167     {
168        Q_ASSERT(view);
169 #if USE(ACCELERATED_COMPOSITING)
170         // the overlay and stays alive for the lifetime of
171         // this QGraphicsWebView as the scrollbars are needed when there's no compositing
172         view->setFlag(QGraphicsItem::ItemUsesExtendedStyleOption);
173 #endif
174     }
175
176     virtual ~PageClientQGraphicsWidget();
177
178     virtual bool isQWidgetClient() const { return false; }
179
180     virtual void scroll(int dx, int dy, const QRect&);
181     virtual void update(const QRect& dirtyRect);
182     virtual void setInputMethodEnabled(bool enable);
183     virtual bool inputMethodEnabled() const;
184     virtual void setInputMethodHints(Qt::InputMethodHints hints);
185
186 #ifndef QT_NO_CURSOR
187     virtual QCursor cursor() const;
188     virtual void updateCursor(const QCursor& cursor);
189 #endif
190
191     virtual QPalette palette() const;
192     virtual int screenNumber() const;
193     virtual QWidget* ownerWidget() const;
194     virtual QRect geometryRelativeToOwnerWidget() const;
195
196     virtual QObject* pluginParent() const;
197
198     virtual QStyle* style() const;
199
200     virtual bool viewResizesToContentsEnabled() const { return viewResizesToContents; }
201
202     void createOrDeleteOverlay();
203
204 #if USE(TILED_BACKING_STORE)
205     void updateTiledBackingStoreScale();
206     virtual QRectF graphicsItemVisibleRect() const;
207 #endif
208
209 #if USE(ACCELERATED_COMPOSITING)
210     virtual void setRootGraphicsLayer(GraphicsLayer*);
211     virtual void markForSync(bool scheduleSync);
212     void syncLayers();
213     void syncLayersTimeout(Timer<PageClientQGraphicsWidget>*) { syncLayers(); }
214
215     // QGraphicsWebView can render composited layers
216     virtual bool allowsAcceleratedCompositing() const { return true; }
217 #endif
218
219     virtual QRectF windowRect() const;
220
221     QGraphicsWebView* view;
222     QWebPage* page;
223     bool viewResizesToContents;
224
225 #if USE(ACCELERATED_COMPOSITING)
226 #if USE(TEXTURE_MAPPER)
227     OwnPtr<TextureMapperNodeClientQt> textureMapperNodeClient;
228 #else
229     QWeakPointer<QGraphicsObject> rootGraphicsLayer;
230 #endif
231     // we have to flush quite often, so we use a meta-method instead of QTimer::singleShot for putting the event in the queue
232     Timer<PageClientQGraphicsWidget> syncTimer;
233
234     // we need to sync the layers if we get a special call from the WebCore
235     // compositor telling us to do so. We'll get that call from ChromeClientQt
236     bool shouldSync;
237 #endif
238     // the overlay gets instantiated when the root layer is attached, and get deleted when it's detached
239     QGraphicsItemOverlay* overlay;
240
241     // we need to put the root graphics layer behind the overlay (which contains the scrollbar)
242     enum { RootGraphicsLayerZValue, OverlayZValue };
243 };
244 #endif // QT_NO_GRAPHICSVIEW
245
246 }
247 #endif // PageClientQt