8ea8db53000d54906de4d18b3fa3208c3eb36f95
[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 *page, QWebFrameData *frameData)
69 {
70     q = qframe;
71
72     frameLoaderClient = new FrameLoaderClientQt();
73     frame = new Frame(page, 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
91 QWebFrame *QWebFramePrivate::parentFrame()
92 {
93     return qobject_cast<QWebFrame*>(q->parent());
94 }
95
96 WebCore::PlatformScrollbar *QWebFramePrivate::horizontalScrollBar() const
97 {
98     Q_ASSERT(frameView);
99     return frameView->horizontalScrollBar();
100 }
101
102 WebCore::PlatformScrollbar *QWebFramePrivate::verticalScrollBar() const
103 {
104     Q_ASSERT(frameView);
105     return frameView->verticalScrollBar();
106 }
107
108 QWebFrame::QWebFrame(QWebPage *parent, QWebFrameData *frameData)
109     : QObject(parent)
110     , d(new QWebFramePrivate)
111 {
112     d->page = parent;
113     d->init(this, parent->d->page, frameData);
114
115     if (!frameData->url.isEmpty()) {
116         ResourceRequest request(frameData->url, frameData->referrer);
117         d->frame->loader()->load(request, frameData->name);
118     }
119 }
120
121 QWebFrame::QWebFrame(QWebFrame *parent, QWebFrameData *frameData)
122     : QObject(parent)
123     , d(new QWebFramePrivate)
124 {
125     d->page = parent->d->page;
126     d->init(this, parent->d->page->d->page, frameData);
127 }
128
129 QWebFrame::~QWebFrame()
130 {
131     Q_ASSERT(d->frame == 0);
132     Q_ASSERT(d->frameView == 0);
133     delete d;
134 }
135
136 void QWebFrame::addToJSWindowObject(const QByteArray &name, QObject *object)
137 {
138       KJS::JSLock lock;
139       KJS::Window *window = KJS::Window::retrieveWindow(d->frame.get());
140       KJS::Bindings::RootObject *root = d->frame->bindingRootObject();
141       if (!window) {
142           qDebug() << "Warning: couldn't get window object";
143           return;
144       }
145
146       KJS::JSObject *runtimeObject =
147         KJS::Bindings::Instance::createRuntimeObject(KJS::Bindings::Instance::QtLanguage,
148                                                      object, root);
149
150       window->put(window->interpreter()->globalExec(), KJS::Identifier(name.constData()), runtimeObject);
151 }
152
153
154 QString QWebFrame::markup() const
155 {
156     if (!d->frame->document())
157         return QString();
158     return createMarkup(d->frame->document());
159 }
160
161 QString QWebFrame::innerText() const
162 {
163     if (d->frameView->layoutPending())
164         d->frameView->layout();
165
166     Element *documentElement = d->frame->document()->documentElement();
167     return documentElement->innerText();
168 }
169
170 QString QWebFrame::renderTreeDump() const
171 {
172     if (d->frameView->layoutPending())
173         d->frameView->layout();
174
175     return externalRepresentation(d->frame->renderer());
176 }
177
178 QString QWebFrame::title() const
179 {
180     if (d->frame->document())
181         return d->frame->document()->title();
182     else return QString();
183 }
184
185 QString QWebFrame::name() const
186 {
187     return d->frame->tree()->name();
188 }
189
190 QWebPage * QWebFrame::page() const
191 {
192     return d->page;
193 }
194
195 QString QWebFrame::selectedText() const
196 {
197     return d->frame->selectedText();
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->frameView->frameGeometry().topLeft();
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->interpreter()->globalExec()));
282         }
283     }
284     return rc;
285 }
286
287 void QWebFrame::mouseMoveEvent(QMouseEvent *ev)
288 {
289     if (!d->frameView)
290         return;
291
292     d->eventHandler->handleMouseMoveEvent(PlatformMouseEvent(ev, 0));
293     const int xOffset =
294         d->horizontalScrollBar() ? d->horizontalScrollBar()->value() : 0;
295     const int yOffset =
296         d->verticalScrollBar() ? d->verticalScrollBar()->value() : 0;
297     IntPoint pt(ev->x() + xOffset, ev->y() + yOffset);
298     WebCore::HitTestResult result = d->eventHandler->hitTestResultAtPoint(pt, false);
299     WebCore::Element *link = result.URLElement();
300     if (link != d->lastHoverElement) {
301         d->lastHoverElement = link;
302         emit hoveringOverLink(result.absoluteLinkURL().prettyURL(), result.title());
303     }
304 }
305
306 void QWebFrame::mousePressEvent(QMouseEvent *ev)
307 {
308     if (!d->eventHandler)
309         return;
310
311     if (ev->button() == Qt::RightButton)
312         d->eventHandler->sendContextMenuEvent(PlatformMouseEvent(ev, 1));
313     else
314         d->eventHandler->handleMousePressEvent(PlatformMouseEvent(ev, 1));
315
316     //FIXME need to keep track of subframe focus for key events!
317     d->page->setFocus();
318 }
319
320 void QWebFrame::mouseDoubleClickEvent(QMouseEvent *ev)
321 {
322     if (!d->eventHandler)
323         return;
324
325     d->eventHandler->handleMousePressEvent(PlatformMouseEvent(ev, 2));
326
327     //FIXME need to keep track of subframe focus for key events!
328     d->page->setFocus();
329 }
330
331 void QWebFrame::mouseReleaseEvent(QMouseEvent *ev)
332 {
333     if (!d->frameView)
334         return;
335
336     d->eventHandler->handleMouseReleaseEvent(PlatformMouseEvent(ev, 0));
337
338     //FIXME need to keep track of subframe focus for key events!
339     d->page->setFocus();
340 }
341
342 void QWebFrame::wheelEvent(QWheelEvent *ev)
343 {
344     PlatformWheelEvent wkEvent(ev);
345     bool accepted = false;
346     if (d->eventHandler)
347         accepted = d->eventHandler->handleWheelEvent(wkEvent);
348
349     ev->setAccepted(accepted);
350
351     //FIXME need to keep track of subframe focus for key events!
352     d->page->setFocus();
353 }
354