Qt build fix: Get globalExec() from the right place.
[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 "kjs/JSGlobalObject.h"
63
64 #include <qdebug.h>
65 #include <qevent.h>
66 #include <qpainter.h>
67
68 using namespace WebCore;
69
70 void QWebFramePrivate::init(QWebFrame *qframe, WebCore::Page *webcorePage, QWebFrameData *frameData)
71 {
72     q = qframe;
73
74     frameLoaderClient = new FrameLoaderClientQt();
75     frame = new Frame(webcorePage, frameData->ownerElement, frameLoaderClient);
76     frameLoaderClient->setFrame(qframe, frame.get());
77
78     frameView = new FrameView(frame.get());
79     frameView->deref();
80     frameView->setQWebFrame(qframe);
81     if (!frameData->allowsScrolling)
82         frameView->setScrollbarsMode(ScrollbarAlwaysOff);
83     if (frameData->marginWidth != -1)
84         frameView->setMarginWidth(frameData->marginWidth);
85     if (frameData->marginHeight != -1)
86         frameView->setMarginHeight(frameData->marginHeight);
87
88     frame->setView(frameView.get());
89     frame->init();
90     eventHandler = frame->eventHandler();
91
92     QObject::connect(q, SIGNAL(hoveringOverLink(const QString&, const QString&, const QString&)),
93                      page, SIGNAL(hoveringOverLink(const QString&, const QString&, const QString&)));
94 }
95
96 QWebFrame *QWebFramePrivate::parentFrame()
97 {
98     return qobject_cast<QWebFrame*>(q->parent());
99 }
100
101 WebCore::PlatformScrollbar *QWebFramePrivate::horizontalScrollBar() const
102 {
103     Q_ASSERT(frameView);
104     return frameView->horizontalScrollBar();
105 }
106
107 WebCore::PlatformScrollbar *QWebFramePrivate::verticalScrollBar() const
108 {
109     Q_ASSERT(frameView);
110     return frameView->verticalScrollBar();
111 }
112
113 QWebFrame::QWebFrame(QWebPage *parent, QWebFrameData *frameData)
114     : QObject(parent)
115     , d(new QWebFramePrivate)
116 {
117     d->page = parent;
118     d->init(this, parent->d->page, frameData);
119
120     if (!frameData->url.isEmpty()) {
121         ResourceRequest request(frameData->url, frameData->referrer);
122         d->frame->loader()->load(request, frameData->name);
123     }
124 }
125
126 QWebFrame::QWebFrame(QWebFrame *parent, QWebFrameData *frameData)
127     : QObject(parent)
128     , d(new QWebFramePrivate)
129 {
130     d->page = parent->d->page;
131     d->init(this, parent->d->page->d->page, frameData);
132 }
133
134 QWebFrame::~QWebFrame()
135 {
136     Q_ASSERT(d->frame == 0);
137     Q_ASSERT(d->frameView == 0);
138     delete d;
139 }
140
141 void QWebFrame::addToJSWindowObject(const QByteArray &name, QObject *object)
142 {
143       KJS::JSLock lock;
144       KJS::Window *window = KJS::Window::retrieveWindow(d->frame.get());
145       KJS::Bindings::RootObject *root = d->frame->bindingRootObject();
146       if (!window) {
147           qDebug() << "Warning: couldn't get window object";
148           return;
149       }
150
151       KJS::JSObject *runtimeObject =
152         KJS::Bindings::Instance::createRuntimeObject(KJS::Bindings::Instance::QtLanguage,
153                                                      object, root);
154
155       window->put(window->globalExec(), KJS::Identifier(name.constData()), runtimeObject);
156 }
157
158
159 QString QWebFrame::markup() const
160 {
161     if (!d->frame->document())
162         return QString();
163     return createMarkup(d->frame->document());
164 }
165
166 QString QWebFrame::innerText() const
167 {
168     if (d->frameView->layoutPending())
169         d->frameView->layout();
170
171     Element *documentElement = d->frame->document()->documentElement();
172     return documentElement->innerText();
173 }
174
175 QString QWebFrame::renderTreeDump() const
176 {
177     if (d->frameView->layoutPending())
178         d->frameView->layout();
179
180     return externalRepresentation(d->frame->renderer());
181 }
182
183 QString QWebFrame::title() const
184 {
185     if (d->frame->document())
186         return d->frame->document()->title();
187     else return QString();
188 }
189
190 QString QWebFrame::name() const
191 {
192     return d->frame->tree()->name();
193 }
194
195 QWebPage * QWebFrame::page() const
196 {
197     return d->page;
198 }
199
200 QList<QWebFrame*> QWebFrame::childFrames() const
201 {
202     QList<QWebFrame*> rc;
203     if (d->frame) {
204         FrameTree *tree = d->frame->tree();
205         for (Frame *child = tree->firstChild(); child; child = child->tree()->nextSibling()) {
206             FrameLoader *loader = child->loader();
207             FrameLoaderClientQt *client = static_cast<FrameLoaderClientQt*>(loader->client());
208             if (client)
209                 rc.append(client->webFrame());
210         }
211
212     }
213     return rc;
214 }
215
216
217 Qt::ScrollBarPolicy QWebFrame::verticalScrollBarPolicy() const
218 {
219     return (Qt::ScrollBarPolicy) d->frameView->vScrollbarMode();
220 }
221
222 void QWebFrame::setVerticalScrollBarPolicy(Qt::ScrollBarPolicy policy)
223 {
224     Q_ASSERT(ScrollbarAuto == Qt::ScrollBarAsNeeded);
225     Q_ASSERT(ScrollbarAlwaysOff == Qt::ScrollBarAlwaysOff);
226     Q_ASSERT(ScrollbarAlwaysOn == Qt::ScrollBarAlwaysOn);
227     d->frameView->setVScrollbarMode((ScrollbarMode)policy);
228 }
229
230 Qt::ScrollBarPolicy QWebFrame::horizontalScrollBarPolicy() const
231 {
232     return (Qt::ScrollBarPolicy) d->frameView->hScrollbarMode();
233 }
234
235 void QWebFrame::setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy policy)
236 {
237     d->frameView->setHScrollbarMode((ScrollbarMode)policy);
238 }
239
240 void QWebFrame::render(QPainter *painter, const QRect &source)
241 {
242     if (!d->frameView || !d->frame->renderer())
243         return;
244
245     layout();
246
247     GraphicsContext ctx(painter);
248     d->frameView->paint(&ctx, source);
249 }
250
251 void QWebFrame::layout()
252 {
253     if (d->frameView->needsLayout()) {
254         d->frameView->layout();
255     }
256
257     foreach (QWebFrame *child, childFrames()) {
258         child->layout();
259     }
260 }
261
262 QPoint QWebFrame::pos() const
263 {
264     Q_ASSERT(d->frameView);
265     return d->pos();
266 }
267
268 QRect QWebFrame::geometry() const
269 {
270     Q_ASSERT(d->frameView);
271     return d->frameView->frameGeometry();
272 }
273
274 QString QWebFrame::evaluateJavaScript(const QString& scriptSource)
275 {
276     KJSProxy *proxy = d->frame->scriptProxy();
277     QString rc;
278     if (proxy) {
279         KJS::JSValue *v = proxy->evaluate(String(), 0, scriptSource);
280         if (v) {
281             rc = String(v->toString(proxy->globalObject()->globalExec()));
282         }
283     }
284     return rc;
285 }
286