tizen beta release
[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 #endif
165         , overlay(0)
166     {
167        Q_ASSERT(view);
168 #if USE(ACCELERATED_COMPOSITING)
169         // the overlay and stays alive for the lifetime of
170         // this QGraphicsWebView as the scrollbars are needed when there's no compositing
171         view->setFlag(QGraphicsItem::ItemUsesExtendedStyleOption);
172 #endif
173     }
174
175     virtual ~PageClientQGraphicsWidget();
176
177     virtual bool isQWidgetClient() const { return false; }
178
179     virtual void scroll(int dx, int dy, const QRect&);
180     virtual void update(const QRect& dirtyRect);
181     virtual void setInputMethodEnabled(bool enable);
182     virtual bool inputMethodEnabled() const;
183     virtual void setInputMethodHints(Qt::InputMethodHints hints);
184
185 #ifndef QT_NO_CURSOR
186     virtual QCursor cursor() const;
187     virtual void updateCursor(const QCursor& cursor);
188 #endif
189
190     virtual QPalette palette() const;
191     virtual int screenNumber() const;
192     virtual QWidget* ownerWidget() const;
193     virtual QRect geometryRelativeToOwnerWidget() const;
194
195     virtual QObject* pluginParent() const;
196
197     virtual QStyle* style() const;
198
199     virtual bool viewResizesToContentsEnabled() const { return viewResizesToContents; }
200
201     void createOrDeleteOverlay();
202
203 #if USE(TILED_BACKING_STORE)
204     void updateTiledBackingStoreScale();
205     virtual QRectF graphicsItemVisibleRect() const;
206 #endif
207
208 #if USE(ACCELERATED_COMPOSITING)
209     virtual void setRootGraphicsLayer(GraphicsLayer*);
210     virtual void markForSync(bool scheduleSync);
211     void syncLayers();
212     void syncLayersTimeout(Timer<PageClientQGraphicsWidget>*) { syncLayers(); }
213
214     // QGraphicsWebView can render composited layers
215     virtual bool allowsAcceleratedCompositing() const { return true; }
216 #endif
217
218     virtual QRectF windowRect() const;
219
220     QGraphicsWebView* view;
221     QWebPage* page;
222     bool viewResizesToContents;
223
224 #if USE(ACCELERATED_COMPOSITING)
225 #if USE(TEXTURE_MAPPER)
226     OwnPtr<TextureMapperNodeClientQt> textureMapperNodeClient;
227 #else
228     QWeakPointer<QGraphicsObject> rootGraphicsLayer;
229 #endif
230     // we have to flush quite often, so we use a meta-method instead of QTimer::singleShot for putting the event in the queue
231     Timer<PageClientQGraphicsWidget> syncTimer;
232 #endif
233     // the overlay gets instantiated when the root layer is attached, and get deleted when it's detached
234     QGraphicsItemOverlay* overlay;
235
236     // we need to put the root graphics layer behind the overlay (which contains the scrollbar)
237     enum { RootGraphicsLayerZValue, OverlayZValue };
238 };
239 #endif // QT_NO_GRAPHICSVIEW
240
241 }
242 #endif // PageClientQt