02663fe654bb3303a874577d401555edabd759f4
[WebKit-https.git] / WebKit / qt / Api / qwebframe.cpp
1 /*
2     Copyright (C) 2007 Trolltech ASA
3     Copyright (C) 2007 Staikos Computing Services Inc.
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Library General Public
7     License as published by the Free Software Foundation; either
8     version 2 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     Library General Public License for more details.
14
15     You should have received a copy of the GNU Library General Public License
16     along with this library; see the file COPYING.LIB.  If not, write to
17     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18     Boston, MA 02110-1301, USA.
19
20     This class provides all functionality needed for loading images, style sheets and html
21     pages from the web. It has a memory cache for these objects.
22 */
23 #include "config.h"
24 #include "qwebframe.h"
25 #include "qwebpage.h"
26 #include "qwebpage_p.h"
27 #include "qwebframe_p.h"
28
29 #include "FocusController.h"
30 #include "FrameLoaderClientQt.h"
31 #include "Frame.h"
32 #include "FrameTree.h"
33 #include "FrameView.h"
34 #include "Page.h"
35 #include "ResourceRequest.h"
36 #include "SelectionController.h"
37 #include "PlatformScrollBar.h"
38
39 #include "markup.h"
40 #include "RenderTreeAsText.h"
41 #include "Element.h"
42 #include "Document.h"
43 #include "DragData.h"
44 #include "RenderObject.h"
45 #include "GraphicsContext.h"
46 #include "PlatformScrollBar.h"
47 #include "PlatformMouseEvent.h"
48 #include "PlatformWheelEvent.h"
49 #include "GraphicsContext.h"
50 #include "HitTestResult.h"
51
52 #include "bindings/runtime.h"
53 #include "bindings/runtime_root.h"
54 #include "kjs_proxy.h"
55 #include "kjs_window.h"
56 #include "kjs_binding.h"
57 #include "ExecState.h"
58 #include "object.h"
59
60 #include "wtf/HashMap.h"
61
62 #include <qdebug.h>
63 #include <qevent.h>
64 #include <qpainter.h>
65
66 using namespace WebCore;
67
68 void QWebFramePrivate::init(QWebFrame *qframe, WebCore::Page *webcorePage, QWebFrameData *frameData)
69 {
70     q = qframe;
71
72     frameLoaderClient = new FrameLoaderClientQt();
73     frame = new Frame(webcorePage, frameData->ownerElement, frameLoaderClient);
74     frameLoaderClient->setFrame(qframe, frame.get());
75
76     frameView = new FrameView(frame.get());
77     frameView->deref();
78     frameView->setQWebFrame(qframe);
79     if (!frameData->allowsScrolling)
80         frameView->setScrollbarsMode(ScrollbarAlwaysOff);
81     if (frameData->marginWidth != -1)
82         frameView->setMarginWidth(frameData->marginWidth);
83     if (frameData->marginHeight != -1)
84         frameView->setMarginHeight(frameData->marginHeight);
85
86     frame->setView(frameView.get());
87     frame->init();
88     eventHandler = frame->eventHandler();
89
90     QObject::connect(q, SIGNAL(hoveringOverLink(const QString&, const QString&, const QString&)),
91                      page, SIGNAL(hoveringOverLink(const QString&, const QString&, const QString&)));
92 }
93
94 QWebFrame *QWebFramePrivate::parentFrame()
95 {
96     return qobject_cast<QWebFrame*>(q->parent());
97 }
98
99 WebCore::PlatformScrollbar *QWebFramePrivate::horizontalScrollBar() const
100 {
101     Q_ASSERT(frameView);
102     return frameView->horizontalScrollBar();
103 }
104
105 WebCore::PlatformScrollbar *QWebFramePrivate::verticalScrollBar() const
106 {
107     Q_ASSERT(frameView);
108     return frameView->verticalScrollBar();
109 }
110
111 QWebFrame::QWebFrame(QWebPage *parent, QWebFrameData *frameData)
112     : QObject(parent)
113     , d(new QWebFramePrivate)
114 {
115     d->page = parent;
116     d->init(this, parent->d->page, frameData);
117
118     if (!frameData->url.isEmpty()) {
119         ResourceRequest request(frameData->url, frameData->referrer);
120         d->frame->loader()->load(request, frameData->name);
121     }
122 }
123
124 QWebFrame::QWebFrame(QWebFrame *parent, QWebFrameData *frameData)
125     : QObject(parent)
126     , d(new QWebFramePrivate)
127 {
128     d->page = parent->d->page;
129     d->init(this, parent->d->page->d->page, frameData);
130 }
131
132 QWebFrame::~QWebFrame()
133 {
134     Q_ASSERT(d->frame == 0);
135     Q_ASSERT(d->frameView == 0);
136     delete d;
137 }
138
139 void QWebFrame::addToJSWindowObject(const QByteArray &name, QObject *object)
140 {
141       KJS::JSLock lock;
142       KJS::Window *window = KJS::Window::retrieveWindow(d->frame.get());
143       KJS::Bindings::RootObject *root = d->frame->bindingRootObject();
144       if (!window) {
145           qDebug() << "Warning: couldn't get window object";
146           return;
147       }
148
149       KJS::JSObject *runtimeObject =
150         KJS::Bindings::Instance::createRuntimeObject(KJS::Bindings::Instance::QtLanguage,
151                                                      object, root);
152
153       window->put(window->interpreter()->globalExec(), KJS::Identifier(name.constData()), runtimeObject);
154 }
155
156
157 QString QWebFrame::markup() const
158 {
159     if (!d->frame->document())
160         return QString();
161     return createMarkup(d->frame->document());
162 }
163
164 QString QWebFrame::innerText() const
165 {
166     if (d->frameView->layoutPending())
167         d->frameView->layout();
168
169     Element *documentElement = d->frame->document()->documentElement();
170     return documentElement->innerText();
171 }
172
173 QString QWebFrame::renderTreeDump() const
174 {
175     if (d->frameView->layoutPending())
176         d->frameView->layout();
177
178     return externalRepresentation(d->frame->renderer());
179 }
180
181 QString QWebFrame::title() const
182 {
183     if (d->frame->document())
184         return d->frame->document()->title();
185     else return QString();
186 }
187
188 QString QWebFrame::name() const
189 {
190     return d->frame->tree()->name();
191 }
192
193 QWebPage * QWebFrame::page() const
194 {
195     return d->page;
196 }
197
198 QList<QWebFrame*> QWebFrame::childFrames() const
199 {
200     QList<QWebFrame*> rc;
201     if (d->frame) {
202         FrameTree *tree = d->frame->tree();
203         for (Frame *child = tree->firstChild(); child; child = child->tree()->nextSibling()) {
204             FrameLoader *loader = child->loader();
205             FrameLoaderClientQt *client = static_cast<FrameLoaderClientQt*>(loader->client());
206             if (client)
207                 rc.append(client->webFrame());
208         }
209
210     }
211     return rc;
212 }
213
214
215 Qt::ScrollBarPolicy QWebFrame::verticalScrollBarPolicy() const
216 {
217     return (Qt::ScrollBarPolicy) d->frameView->vScrollbarMode();
218 }
219
220 void QWebFrame::setVerticalScrollBarPolicy(Qt::ScrollBarPolicy policy)
221 {
222     Q_ASSERT(ScrollbarAuto == Qt::ScrollBarAsNeeded);
223     Q_ASSERT(ScrollbarAlwaysOff == Qt::ScrollBarAlwaysOff);
224     Q_ASSERT(ScrollbarAlwaysOn == Qt::ScrollBarAlwaysOn);
225     d->frameView->setVScrollbarMode((ScrollbarMode)policy);
226 }
227
228 Qt::ScrollBarPolicy QWebFrame::horizontalScrollBarPolicy() const
229 {
230     return (Qt::ScrollBarPolicy) d->frameView->hScrollbarMode();
231 }
232
233 void QWebFrame::setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy policy)
234 {
235     d->frameView->setHScrollbarMode((ScrollbarMode)policy);
236 }
237
238 void QWebFrame::render(QPainter *painter, const QRect &source)
239 {
240     if (!d->frameView || !d->frame->renderer())
241         return;
242
243     layout();
244
245     GraphicsContext ctx(painter);
246     d->frameView->paint(&ctx, source);
247 }
248
249 void QWebFrame::layout()
250 {
251     if (d->frameView->needsLayout()) {
252         d->frameView->layout();
253     }
254
255     foreach (QWebFrame *child, childFrames()) {
256         child->layout();
257     }
258 }
259
260 QPoint QWebFrame::pos() const
261 {
262     Q_ASSERT(d->frameView);
263     return d->pos();
264 }
265
266 QRect QWebFrame::geometry() const
267 {
268     Q_ASSERT(d->frameView);
269     return d->frameView->frameGeometry();
270 }
271
272 QString QWebFrame::evaluateJavaScript(const QString& scriptSource)
273 {
274     KJSProxy *proxy = d->frame->scriptProxy();
275     QString rc;
276     if (proxy) {
277         KJS::JSValue *v = proxy->evaluate(String(), 0, scriptSource);
278         if (v) {
279             rc = String(v->toString(proxy->interpreter()->globalExec()));
280         }
281     }
282     return rc;
283 }
284