ChromeClientQt.cpp revision 8e35f3cfc7fba1d1c829dc557ebad6409cbe16a2
1/*
2 * Copyright (C) 2006 Zack Rusin <zack@kde.org>
3 * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28#include "config.h"
29#include "ChromeClientQt.h"
30
31#include "FileChooser.h"
32#include "Frame.h"
33#include "FrameLoadRequest.h"
34#include "FrameLoader.h"
35#include "FrameLoaderClientQt.h"
36#include "FrameView.h"
37#include "HitTestResult.h"
38#include "NotImplemented.h"
39#include "WindowFeatures.h"
40
41#include "qwebpage.h"
42#include "qwebpage_p.h"
43#include "qwebframe_p.h"
44
45#include <qtooltip.h>
46
47namespace WebCore
48{
49
50
51ChromeClientQt::ChromeClientQt(QWebPage* webPage)
52    : m_webPage(webPage)
53{
54    toolBarsVisible = statusBarVisible = menuBarVisible = true;
55}
56
57ChromeClientQt::~ChromeClientQt()
58{
59
60}
61
62void ChromeClientQt::setWindowRect(const FloatRect& rect)
63{
64    if (!m_webPage)
65        return;
66    emit m_webPage->geometryChangeRequested(QRect(qRound(rect.x()), qRound(rect.y()),
67                            qRound(rect.width()), qRound(rect.height())));
68}
69
70
71FloatRect ChromeClientQt::windowRect()
72{
73    if (!m_webPage)
74        return FloatRect();
75
76    QWidget* view = m_webPage->view();
77    if (!view)
78        return FloatRect();
79    return IntRect(view->topLevelWidget()->geometry());
80}
81
82
83FloatRect ChromeClientQt::pageRect()
84{
85    if (!m_webPage)
86        return FloatRect();
87    return FloatRect(QRectF(QPointF(0,0), m_webPage->viewportSize()));
88}
89
90
91float ChromeClientQt::scaleFactor()
92{
93    notImplemented();
94    return 1;
95}
96
97
98void ChromeClientQt::focus()
99{
100    if (!m_webPage)
101        return;
102    QWidget* view = m_webPage->view();
103    if (!view)
104        return;
105
106    view->setFocus();
107}
108
109
110void ChromeClientQt::unfocus()
111{
112    if (!m_webPage)
113        return;
114    QWidget* view = m_webPage->view();
115    if (!view)
116        return;
117    view->clearFocus();
118}
119
120bool ChromeClientQt::canTakeFocus(FocusDirection)
121{
122    // This is called when cycling through links/focusable objects and we
123    // reach the last focusable object. Then we want to claim that we can
124    // take the focus to avoid wrapping.
125    return true;
126}
127
128void ChromeClientQt::takeFocus(FocusDirection)
129{
130    // don't do anything. This is only called when cycling to links/focusable objects,
131    // which in turn is called from focusNextPrevChild. We let focusNextPrevChild
132    // call QWidget::focusNextPrevChild accordingly, so there is no need to do anything
133    // here.
134}
135
136
137Page* ChromeClientQt::createWindow(Frame*, const FrameLoadRequest& request, const WindowFeatures& features)
138{
139    QWebPage *newPage = m_webPage->createWindow(features.dialog ? QWebPage::WebModalDialog : QWebPage::WebBrowserWindow);
140    if (!newPage)
141        return 0;
142    newPage->mainFrame()->load(request.resourceRequest().url());
143    return newPage->d->page;
144}
145
146void ChromeClientQt::show()
147{
148    if (!m_webPage)
149        return;
150    QWidget* view = m_webPage->view();
151    if (!view)
152        return;
153    view->topLevelWidget()->show();
154}
155
156
157bool ChromeClientQt::canRunModal()
158{
159    notImplemented();
160    return false;
161}
162
163
164void ChromeClientQt::runModal()
165{
166    notImplemented();
167}
168
169
170void ChromeClientQt::setToolbarsVisible(bool visible)
171{
172    toolBarsVisible = visible;
173    emit m_webPage->toolBarVisibilityChangeRequested(visible);
174}
175
176
177bool ChromeClientQt::toolbarsVisible()
178{
179    return toolBarsVisible;
180}
181
182
183void ChromeClientQt::setStatusbarVisible(bool visible)
184{
185    emit m_webPage->statusBarVisibilityChangeRequested(visible);
186    statusBarVisible = visible;
187}
188
189
190bool ChromeClientQt::statusbarVisible()
191{
192    return statusBarVisible;
193    return false;
194}
195
196
197void ChromeClientQt::setScrollbarsVisible(bool)
198{
199    notImplemented();
200}
201
202
203bool ChromeClientQt::scrollbarsVisible()
204{
205    notImplemented();
206    return true;
207}
208
209
210void ChromeClientQt::setMenubarVisible(bool visible)
211{
212    menuBarVisible = visible;
213    emit m_webPage->menuBarVisibilityChangeRequested(visible);
214}
215
216bool ChromeClientQt::menubarVisible()
217{
218    return menuBarVisible;
219}
220
221void ChromeClientQt::setResizable(bool)
222{
223    notImplemented();
224}
225
226void ChromeClientQt::addMessageToConsole(const String& message, unsigned int lineNumber,
227                                         const String& sourceID)
228{
229    QString x = message;
230    QString y = sourceID;
231    m_webPage->javaScriptConsoleMessage(x, lineNumber, y);
232}
233
234void ChromeClientQt::chromeDestroyed()
235{
236    delete this;
237}
238
239bool ChromeClientQt::canRunBeforeUnloadConfirmPanel()
240{
241    return true;
242}
243
244bool ChromeClientQt::runBeforeUnloadConfirmPanel(const String& message, Frame* frame)
245{
246    return runJavaScriptConfirm(frame, message);
247}
248
249void ChromeClientQt::closeWindowSoon()
250{
251    m_webPage->mainFrame()->d->frame->loader()->stopAllLoaders();
252    emit m_webPage->windowCloseRequested();
253}
254
255void ChromeClientQt::runJavaScriptAlert(Frame* f, const String& msg)
256{
257    QString x = msg;
258    FrameLoaderClientQt *fl = static_cast<FrameLoaderClientQt*>(f->loader()->client());
259    m_webPage->javaScriptAlert(fl->webFrame(), x);
260}
261
262bool ChromeClientQt::runJavaScriptConfirm(Frame* f, const String& msg)
263{
264    QString x = msg;
265    FrameLoaderClientQt *fl = static_cast<FrameLoaderClientQt*>(f->loader()->client());
266    return m_webPage->javaScriptConfirm(fl->webFrame(), x);
267}
268
269bool ChromeClientQt::runJavaScriptPrompt(Frame* f, const String& message, const String& defaultValue, String& result)
270{
271    QString x = result;
272    FrameLoaderClientQt *fl = static_cast<FrameLoaderClientQt*>(f->loader()->client());
273    bool rc = m_webPage->javaScriptPrompt(fl->webFrame(), (QString)message, (QString)defaultValue, &x);
274    result = x;
275    return rc;
276}
277
278void ChromeClientQt::setStatusbarText(const String& msg)
279{
280    QString x = msg;
281    emit m_webPage->statusBarMessage(x);
282}
283
284bool ChromeClientQt::shouldInterruptJavaScript()
285{
286    notImplemented();
287    return false;
288}
289
290bool ChromeClientQt::tabsToLinks() const
291{
292    return m_webPage->settings()->testAttribute(QWebSettings::LinksIncludedInFocusChain);
293}
294
295IntRect ChromeClientQt::windowResizerRect() const
296{
297    return IntRect();
298}
299
300void ChromeClientQt::repaint(const IntRect& windowRect, bool contentChanged, bool immediate, bool repaintContentOnly)
301{
302    // No double buffer, so only update the QWidget if content changed.
303    if (contentChanged) {
304        QWidget* view = m_webPage->view();
305        if (view) {
306            QRect rect(windowRect);
307            rect = rect.intersected(QRect(QPoint(0, 0), m_webPage->viewportSize()));
308            if (!windowRect.isEmpty())
309                view->update(windowRect);
310        }
311        emit m_webPage->repaintRequested(windowRect);
312    }
313
314    // FIXME: There is no "immediate" support for window painting.  This should be done always whenever the flag
315    // is set.
316}
317
318void ChromeClientQt::scroll(const IntSize& delta, const IntRect& scrollViewRect, const IntRect&)
319{
320    QWidget* view = m_webPage->view();
321    if (view)
322        view->scroll(delta.width(), delta.height(), scrollViewRect);
323    emit m_webPage->scrollRequested(delta.width(), delta.height(), scrollViewRect);
324}
325
326IntRect ChromeClientQt::windowToScreen(const IntRect& rect) const
327{
328    notImplemented();
329    return rect;
330}
331
332IntPoint ChromeClientQt::screenToWindow(const IntPoint& point) const
333{
334    notImplemented();
335    return point;
336}
337
338PlatformWidget ChromeClientQt::platformWindow() const
339{
340    return m_webPage->view();
341}
342
343void ChromeClientQt::mouseDidMoveOverElement(const HitTestResult& result, unsigned modifierFlags)
344{
345    if (result.absoluteLinkURL() != lastHoverURL
346        || result.title() != lastHoverTitle
347        || result.textContent() != lastHoverContent) {
348        lastHoverURL = result.absoluteLinkURL();
349        lastHoverTitle = result.title();
350        lastHoverContent = result.textContent();
351        emit m_webPage->linkHovered(lastHoverURL.prettyURL(),
352                lastHoverTitle, lastHoverContent);
353    }
354}
355
356void ChromeClientQt::setToolTip(const String &tip)
357{
358#ifndef QT_NO_TOOLTIP
359    QWidget* view = m_webPage->view();
360    if (!view)
361        return;
362
363    if (tip.isEmpty()) {
364        view->setToolTip(QString());
365        QToolTip::hideText();
366    } else {
367        QString dtip = QLatin1String("<p>") + tip + QLatin1String("</p>");
368        view->setToolTip(dtip);
369    }
370#else
371    Q_UNUSED(tip);
372#endif
373}
374
375void ChromeClientQt::print(Frame *frame)
376{
377    emit m_webPage->printRequested(QWebFramePrivate::kit(frame));
378}
379
380void ChromeClientQt::exceededDatabaseQuota(Frame*, const String&)
381{
382    notImplemented();
383}
384
385void ChromeClientQt::runOpenPanel(Frame* frame, PassRefPtr<FileChooser> prpFileChooser)
386{
387    // FIXME: Support multiple files.
388
389    RefPtr<FileChooser> fileChooser = prpFileChooser;
390    QString suggestedFile = fileChooser->filenames()[0];
391    QString file = m_webPage->chooseFile(QWebFramePrivate::kit(frame), suggestedFile);
392    if (!file.isEmpty())
393        fileChooser->chooseFile(file);
394}
395
396}
397