2 Copyright (C) 2007 Trolltech ASA
3 Copyright (C) 2007 Staikos Computing Services Inc.
4 Copyright (C) 2007 Apple Inc.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
21 This class provides all functionality needed for loading images, style sheets and html
22 pages from the web. It has a memory cache for these objects.
26 #include "qwebframe.h"
27 #include "qwebpage_p.h"
28 #include "qwebframe_p.h"
29 #include "qwebnetworkinterface.h"
30 #include "qwebpagehistory.h"
31 #include "qwebpagehistory_p.h"
32 #include "qwebsettings.h"
35 #include "ChromeClientQt.h"
36 #include "ContextMenu.h"
37 #include "ContextMenuClientQt.h"
38 #include "DragClientQt.h"
39 #include "DragController.h"
41 #include "EditorClientQt.h"
44 #include "FrameLoader.h"
45 #include "FrameLoadRequest.h"
48 #include "IconDatabase.h"
49 #include "InspectorClientQt.h"
50 #include "FocusController.h"
52 #include "PlatformScrollBar.h"
53 #include "PlatformKeyboardEvent.h"
54 #include "PlatformWheelEvent.h"
55 #include "ProgressTracker.h"
58 #include "HitTestResult.h"
59 #include "WindowFeatures.h"
60 #include "LocalizedStrings.h"
63 #include <QDragEnterEvent>
64 #include <QDragLeaveEvent>
65 #include <QDragMoveEvent>
67 #include <QFileDialog>
68 #include <QHttpRequestHeader>
69 #include <QInputDialog>
70 #include <QMessageBox>
71 #include <QNetworkProxy>
76 using namespace WebCore;
78 QWebPagePrivate::QWebPagePrivate(QWebPage *qq)
82 q->setMouseTracking(true);
83 q->setFocusPolicy(Qt::ClickFocus);
84 chromeClient = new ChromeClientQt(q);
85 contextMenuClient = new ContextMenuClientQt();
86 editorClient = new EditorClientQt(q);
87 page = new Page(chromeClient, contextMenuClient, editorClient,
88 new DragClientQt(q), new InspectorClientQt());
90 // ### should be configurable
91 page->settings()->setDefaultTextEncodingName("iso-8859-1");
93 settings = new QWebSettings(page->settings());
98 insideOpenCall = false;
100 history.d = new QWebPageHistoryPrivate(page->backForwardList());
101 memset(actions, 0, sizeof(actions));
104 QWebPagePrivate::~QWebPagePrivate()
111 QWebPage::NavigationRequestResponse QWebPagePrivate::navigationRequested(QWebFrame *frame, const QWebNetworkRequest &request, QWebPage::NavigationType type)
114 && frame == mainFrame)
115 return QWebPage::AcceptNavigationRequest;
116 return q->navigationRequested(frame, request, type);
119 void QWebPagePrivate::createMainFrame()
122 QWebFrameData frameData;
123 frameData.ownerElement = 0;
124 frameData.allowsScrolling = true;
125 frameData.marginWidth = 0;
126 frameData.marginHeight = 0;
127 mainFrame = new QWebFrame(q, &frameData);
128 QObject::connect(mainFrame, SIGNAL(titleChanged(const QString&)),
129 q, SIGNAL(titleChanged(const QString&)));
130 QObject::connect(mainFrame, SIGNAL(hoveringOverLink(const QString&, const QString&, const QString&)),
131 q, SIGNAL(hoveringOverLink(const QString&, const QString&, const QString&)));
133 mainFrame->d->frameView->setFrameGeometry(q->geometry());
135 emit q->frameCreated(mainFrame);
139 static QWebPage::WebAction webActionForContextMenuAction(WebCore::ContextMenuAction action)
142 case WebCore::ContextMenuItemTagOpenLink: return QWebPage::OpenLink;
143 case WebCore::ContextMenuItemTagOpenLinkInNewWindow: return QWebPage::OpenLinkInNewWindow;
144 case WebCore::ContextMenuItemTagDownloadLinkToDisk: return QWebPage::DownloadLinkToDisk;
145 case WebCore::ContextMenuItemTagCopyLinkToClipboard: return QWebPage::CopyLinkToClipboard;
146 case WebCore::ContextMenuItemTagOpenImageInNewWindow: return QWebPage::OpenImageInNewWindow;
147 case WebCore::ContextMenuItemTagDownloadImageToDisk: return QWebPage::DownloadImageToDisk;
148 case WebCore::ContextMenuItemTagCopyImageToClipboard: return QWebPage::CopyImageToClipboard;
149 case WebCore::ContextMenuItemTagOpenFrameInNewWindow: return QWebPage::OpenFrameInNewWindow;
150 case WebCore::ContextMenuItemTagCopy: return QWebPage::Copy;
151 case WebCore::ContextMenuItemTagGoBack: return QWebPage::GoBack;
152 case WebCore::ContextMenuItemTagGoForward: return QWebPage::GoForward;
153 case WebCore::ContextMenuItemTagStop: return QWebPage::Stop;
154 case WebCore::ContextMenuItemTagReload: return QWebPage::Reload;
155 case WebCore::ContextMenuItemTagCut: return QWebPage::Cut;
156 case WebCore::ContextMenuItemTagPaste: return QWebPage::Paste;
157 case WebCore::ContextMenuItemTagDefaultDirection: return QWebPage::SetTextDirectionDefault;
158 case WebCore::ContextMenuItemTagLeftToRight: return QWebPage::SetTextDirectionLeftToRight;
159 case WebCore::ContextMenuItemTagRightToLeft: return QWebPage::SetTextDirectionRightToLeft;
160 case WebCore::ContextMenuItemTagBold: return QWebPage::ToggleBold;
161 case WebCore::ContextMenuItemTagItalic: return QWebPage::ToggleItalic;
162 case WebCore::ContextMenuItemTagUnderline: return QWebPage::ToggleUnderline;
165 return QWebPage::NoWebAction;
168 QMenu *QWebPagePrivate::createContextMenu(const WebCore::ContextMenu *webcoreMenu, const QList<WebCore::ContextMenuItem> *items)
170 QMenu *menu = new QMenu;
171 for (int i = 0; i < items->count(); ++i) {
172 const ContextMenuItem &item = items->at(i);
173 switch (item.type()) {
174 case WebCore::ActionType: {
175 QWebPage::WebAction action = webActionForContextMenuAction(item.action());
176 QAction *a = q->action(action);
178 ContextMenuItem it(item);
179 webcoreMenu->checkOrEnableIfNeeded(it);
180 PlatformMenuItemDescription desc = it.releasePlatformDescription();
181 a->setEnabled(desc.enabled);
182 a->setChecked(desc.checked);
188 case WebCore::SeparatorType:
189 menu->addSeparator();
191 case WebCore::SubmenuType: {
192 QMenu *subMenu = createContextMenu(webcoreMenu, item.platformSubMenu());
193 if (!subMenu->actions().isEmpty()) {
194 subMenu->setTitle(item.title());
195 menu->addAction(subMenu->menuAction());
206 QWebFrame *QWebPagePrivate::frameAt(const QPoint &pos) const
208 QWebFrame *frame = mainFrame;
211 QList<QWebFrame*> children = frame->childFrames();
212 for (int i = 0; i < children.size(); ++i) {
213 if (children.at(i)->geometry().contains(pos)) {
214 frame = children.at(i);
218 if (frame->geometry().contains(pos))
223 void QWebPagePrivate::_q_webActionTriggered(bool checked)
225 QAction *a = qobject_cast<QAction *>(q->sender());
228 QWebPage::WebAction action = static_cast<QWebPage::WebAction>(a->data().toInt());
229 q->triggerAction(action, checked);
232 void QWebPagePrivate::updateAction(QWebPage::WebAction action)
234 QAction *a = actions[action];
235 if (!a || !mainFrame)
238 WebCore::FrameLoader *loader = mainFrame->d->frame->loader();
239 WebCore::Editor *editor = page->focusController()->focusedOrMainFrame()->editor();
241 bool enabled = a->isEnabled();
244 case QWebPage::GoBack:
245 enabled = loader->canGoBackOrForward(-1);
247 case QWebPage::GoForward:
248 enabled = loader->canGoBackOrForward(1);
251 enabled = loader->isLoading();
253 case QWebPage::Reload:
254 enabled = !loader->isLoading();
257 enabled = editor->canCut();
260 enabled = editor->canCopy();
262 case QWebPage::Paste:
263 enabled = editor->canPaste();
267 // those two are handled by QUndoStack
272 a->setEnabled(enabled);
275 void QWebPagePrivate::updateNavigationActions()
277 updateAction(QWebPage::GoBack);
278 updateAction(QWebPage::GoForward);
279 updateAction(QWebPage::Stop);
280 updateAction(QWebPage::Reload);
283 void QWebPagePrivate::updateEditorActions()
285 updateAction(QWebPage::Cut);
286 updateAction(QWebPage::Copy);
287 updateAction(QWebPage::Paste);
290 QWebPage::QWebPage(QWidget *parent)
292 , d(new QWebPagePrivate(this))
295 QPalette pal = palette();
296 pal.setBrush(QPalette::Background, Qt::white);
298 setAttribute(Qt::WA_OpaquePaintEvent);
301 setAcceptDrops(true);
302 connect(this, SIGNAL(loadProgressChanged(int)), this, SLOT(_q_onLoadProgressChanged(int)));
305 QWebPage::~QWebPage()
307 FrameLoader *loader = d->mainFrame->d->frame->loader();
309 loader->detachFromParent();
313 void QWebPage::open(const QUrl &url)
315 open(QWebNetworkRequest(url));
318 void QWebPage::open(const QWebNetworkRequest &req)
320 d->insideOpenCall = true;
322 QUrl url = req.url();
323 QHttpRequestHeader httpHeader = req.httpHeader();
324 QByteArray postData = req.postData();
326 WebCore::ResourceRequest request(KURL(url.toString()));
328 QString method = httpHeader.method();
329 if (!method.isEmpty())
330 request.setHTTPMethod(method);
332 QList<QPair<QString, QString> > values = httpHeader.values();
333 for (int i = 0; i < values.size(); ++i) {
334 const QPair<QString, QString> &val = values.at(i);
335 request.addHTTPHeaderField(val.first, val.second);
338 if (!postData.isEmpty()) {
339 WTF::RefPtr<WebCore::FormData> formData = new WebCore::FormData(postData.constData(), postData.size());
340 request.setHTTPBody(formData);
343 mainFrame()->d->frame->loader()->load(request);
344 d->insideOpenCall = false;
347 QUrl QWebPage::url() const
349 return QUrl((QString)mainFrame()->d->frame->loader()->url().url());
352 QString QWebPage::title() const
354 return mainFrame()->title();
357 QWebFrame *QWebPage::mainFrame() const
359 d->createMainFrame();
363 QSize QWebPage::sizeHint() const
365 return QSize(800, 600);
368 void QWebPage::stop()
373 QWebPageHistory *QWebPage::history() const
378 void QWebPage::javaScriptConsoleMessage(const QString& message, unsigned int lineNumber, const QString& sourceID)
382 void QWebPage::javaScriptAlert(QWebFrame *frame, const QString& msg)
385 QMessageBox::information(this, title(), msg, QMessageBox::Ok);
388 bool QWebPage::javaScriptConfirm(QWebFrame *frame, const QString& msg)
391 return 0 == QMessageBox::information(this, title(), msg, QMessageBox::Yes, QMessageBox::No);
394 bool QWebPage::javaScriptPrompt(QWebFrame *frame, const QString& msg, const QString& defaultValue, QString* result)
398 #ifndef QT_NO_INPUTDIALOG
399 QString x = QInputDialog::getText(this, title(), msg, QLineEdit::Normal, defaultValue, &ok);
407 QWebPage *QWebPage::createWindow()
412 QWebPage *QWebPage::createModalDialog()
417 QObject *QWebPage::createPlugin(const QString &classid, const QUrl &url, const QStringList ¶mNames, const QStringList ¶mValues)
422 Q_UNUSED(paramValues)
426 static WebCore::FrameLoadRequest frameLoadRequest(const QUrl &url, WebCore::Frame *frame)
428 WebCore::ResourceRequest rr(WebCore::KURL(url.toString()),
429 frame->loader()->outgoingReferrer());
430 return WebCore::FrameLoadRequest(rr);
433 static void openNewWindow(const QUrl& url, WebCore::Frame* frame)
435 if (Page* oldPage = frame->page()) {
436 WindowFeatures features;
437 if (Page* newPage = oldPage->chrome()->createWindow(frame,
438 frameLoadRequest(url, frame), features))
439 newPage->chrome()->show();
443 void QWebPage::triggerAction(WebAction action, bool checked)
445 WebCore::Frame *frame = d->page->focusController()->focusedOrMainFrame();
446 WebCore::Editor *editor = frame->editor();
447 const char *command = 0;
451 if (QWebFrame *targetFrame = d->currentContext.targetFrame()) {
452 WTF::RefPtr<WebCore::Frame> wcFrame = targetFrame->d->frame;
453 targetFrame->d->frame->loader()->load(frameLoadRequest(d->currentContext.linkUrl(), wcFrame.get()),
454 /*lockHistory*/ false,
455 /*userGesture*/ true,
457 /*HTMLFormElement*/ 0,
459 WTF::HashMap<String, String>());
464 case OpenLinkInNewWindow:
465 openNewWindow(d->currentContext.linkUrl(), frame);
467 case OpenFrameInNewWindow:
469 case DownloadLinkToDisk:
470 case CopyLinkToClipboard:
471 editor->copyURL(WebCore::KURL(d->currentContext.linkUrl().toString()), d->currentContext.text());
473 case OpenImageInNewWindow:
474 openNewWindow(d->currentContext.imageUrl(), frame);
476 case DownloadImageToDisk:
477 case CopyImageToClipboard:
483 d->page->goForward();
486 mainFrame()->d->frame->loader()->stopForUserCancel();
489 mainFrame()->d->frame->loader()->reload();
509 command = "MoveForward";
511 case MoveToPreviousChar:
512 command = "MoveBackward";
515 command = "MoveWordForward";
517 case MoveToPreviousWord:
518 command = "MoveWordBackward";
521 command = "MoveDown";
523 case MoveToPreviousLine:
526 case MoveToStartOfLine:
527 command = "MoveToBeginningOfLine";
529 case MoveToEndOfLine:
530 command = "MoveToEndOfLine";
532 case MoveToStartOfBlock:
533 command = "MoveToBeginningOfParagraph";
535 case MoveToEndOfBlock:
536 command = "MoveToEndOfParagraph";
538 case MoveToStartOfDocument:
539 command = "MoveToBeginningOfDocument";
541 case MoveToEndOfDocument:
542 command = "MoveToEndOfDocument";
545 command = "MoveForwardAndModifySelection";
547 case SelectPreviousChar:
548 command = "MoveBackwardAndModifySelection";
551 command = "MoveWordForwardAndModifySelection";
553 case SelectPreviousWord:
554 command = "MoveWordBackwardAndModifySelection";
557 command = "MoveDownAndModifySelection";
559 case SelectPreviousLine:
560 command = "MoveUpAndModifySelection";
562 case SelectStartOfLine:
563 command = "MoveToBeginningOfLineAndModifySelection";
565 case SelectEndOfLine:
566 command = "MoveToEndOfLineAndModifySelection";
568 case SelectStartOfBlock:
569 command = "MoveToBeginningOfParagraphAndModifySelection";
571 case SelectEndOfBlock:
572 command = "MoveToEndOfParagraphAndModifySelection";
574 case SelectStartOfDocument:
575 command = "MoveToBeginningOfDocumentAndModifySelection";
577 case SelectEndOfDocument:
578 command = "MoveToEndOfDocumentAndModifySelection";
580 case DeleteStartOfWord:
581 command = "DeleteWordBackward";
583 case DeleteEndOfWord:
584 command = "DeleteWordForward";
587 case SetTextDirectionDefault:
588 editor->setBaseWritingDirection("inherit");
590 case SetTextDirectionLeftToRight:
591 editor->setBaseWritingDirection("ltr");
593 case SetTextDirectionRightToLeft:
594 editor->setBaseWritingDirection("rtl");
598 command = "ToggleBold";
601 command = "ToggleItalic";
603 case ToggleUnderline:
604 editor->toggleUnderline();
611 editor->execCommand(command);
614 QWebPage::NavigationRequestResponse QWebPage::navigationRequested(QWebFrame *frame, const QWebNetworkRequest &request, QWebPage::NavigationType type)
617 return AcceptNavigationRequest;
620 QString QWebPage::selectedText() const
622 return d->page->focusController()->focusedOrMainFrame()->selectedText();
625 QAction *QWebPage::action(WebAction action) const
627 if (action == QWebPage::NoWebAction) return 0;
628 if (d->actions[action])
629 return d->actions[action];
632 bool checkable = false;
636 text = contextMenuItemTagOpenLink();
638 case OpenLinkInNewWindow:
639 text = contextMenuItemTagOpenLinkInNewWindow();
641 case OpenFrameInNewWindow:
642 text = contextMenuItemTagOpenFrameInNewWindow();
645 case DownloadLinkToDisk:
646 text = contextMenuItemTagDownloadLinkToDisk();
648 case CopyLinkToClipboard:
649 text = contextMenuItemTagCopyLinkToClipboard();
652 case OpenImageInNewWindow:
653 text = contextMenuItemTagOpenImageInNewWindow();
655 case DownloadImageToDisk:
656 text = contextMenuItemTagDownloadImageToDisk();
658 case CopyImageToClipboard:
659 text = contextMenuItemTagCopyImageToClipboard();
663 text = contextMenuItemTagGoBack();
666 text = contextMenuItemTagGoForward();
669 text = contextMenuItemTagStop();
672 text = contextMenuItemTagReload();
676 text = contextMenuItemTagCut();
679 text = contextMenuItemTagCopy();
682 text = contextMenuItemTagPaste();
686 QAction *a = undoStack()->createUndoAction(d->q);
687 d->actions[action] = a;
691 QAction *a = undoStack()->createRedoAction(d->q);
692 d->actions[action] = a;
696 case MoveToPreviousChar:
698 case MoveToPreviousWord:
700 case MoveToPreviousLine:
701 case MoveToStartOfLine:
702 case MoveToEndOfLine:
703 case MoveToStartOfBlock:
704 case MoveToEndOfBlock:
705 case MoveToStartOfDocument:
706 case MoveToEndOfDocument:
708 case SelectPreviousChar:
710 case SelectPreviousWord:
712 case SelectPreviousLine:
713 case SelectStartOfLine:
714 case SelectEndOfLine:
715 case SelectStartOfBlock:
716 case SelectEndOfBlock:
717 case SelectStartOfDocument:
718 case SelectEndOfDocument:
719 case DeleteStartOfWord:
720 case DeleteEndOfWord:
723 case SetTextDirectionDefault:
724 text = contextMenuItemTagDefaultDirection();
726 case SetTextDirectionLeftToRight:
727 text = contextMenuItemTagLeftToRight();
730 case SetTextDirectionRightToLeft:
731 text = contextMenuItemTagRightToLeft();
736 text = contextMenuItemTagBold();
740 text = contextMenuItemTagItalic();
743 case ToggleUnderline:
744 text = contextMenuItemTagUnderline();
755 QAction *a = new QAction(d->q);
758 a->setCheckable(checkable);
760 connect(a, SIGNAL(triggered(bool)),
761 this, SLOT(_q_webActionTriggered(bool)));
763 d->actions[action] = a;
764 d->updateAction(action);
769 Returns true if the page contains unsubmitted form data.
771 bool QWebPage::isModified() const
777 QUndoStack *QWebPage::undoStack() const
780 d->undoStack = new QUndoStack(const_cast<QWebPage *>(this));
785 static inline DragOperation dropActionToDragOp(Qt::DropActions actions)
788 if (actions & Qt::CopyAction)
789 result |= DragOperationCopy;
790 if (actions & Qt::MoveAction)
791 result |= DragOperationMove;
792 if (actions & Qt::LinkAction)
793 result |= DragOperationLink;
794 return (DragOperation)result;
797 static inline Qt::DropAction dragOpToDropAction(unsigned actions)
799 Qt::DropAction result = Qt::IgnoreAction;
800 if (actions & DragOperationCopy)
801 result = Qt::CopyAction;
802 else if (actions & DragOperationMove)
803 result = Qt::MoveAction;
804 else if (actions & DragOperationLink)
805 result = Qt::LinkAction;
809 void QWebPage::resizeEvent(QResizeEvent *e)
811 QWidget::resizeEvent(e);
812 if (mainFrame()->d->frame && mainFrame()->d->frameView) {
813 mainFrame()->d->frameView->setFrameGeometry(rect());
814 mainFrame()->d->frame->forceLayout();
815 mainFrame()->d->frame->view()->adjustViewSize();
819 void QWebPage::paintEvent(QPaintEvent *ev)
821 #ifdef QWEBKIT_TIME_RENDERING
828 QVector<QRect> vector = ev->region().rects();
829 if (!vector.isEmpty()) {
830 for (int i = 0; i < vector.size(); ++i) {
831 mainFrame()->render(&p, vector.at(i));
834 mainFrame()->render(&p, ev->rect());
837 #ifdef QWEBKIT_TIME_RENDERING
838 int elapsed = time.elapsed();
839 qDebug()<<"paint event on "<<ev->region()<<", took to render = "<<elapsed;
843 void QWebPage::mouseMoveEvent(QMouseEvent *ev)
845 QWebFrame *f = d->currentFrame(ev->pos());
849 QWebFramePrivate *frame = f->d;
850 if (!frame->frameView)
853 frame->eventHandler->handleMouseMoveEvent(PlatformMouseEvent(ev, 0));
855 frame->horizontalScrollBar() ? frame->horizontalScrollBar()->value() : 0;
857 frame->verticalScrollBar() ? frame->verticalScrollBar()->value() : 0;
858 IntPoint pt(ev->x() + xOffset, ev->y() + yOffset);
859 WebCore::HitTestResult result = frame->eventHandler->hitTestResultAtPoint(pt, false);
860 WebCore::Element *link = result.URLElement();
861 if (link != frame->lastHoverElement) {
862 frame->lastHoverElement = link;
863 emit hoveringOverLink(result.absoluteLinkURL().prettyURL(), result.title(), result.textContent());
867 void QWebPage::mousePressEvent(QMouseEvent *ev)
869 d->frameUnderMouse = d->frameAt(ev->pos());
870 if (!d->frameUnderMouse)
873 QWebFramePrivate *frame = d->frameUnderMouse->d;
874 if (!frame->eventHandler)
877 frame->eventHandler->handleMousePressEvent(PlatformMouseEvent(ev, 1));
879 //FIXME need to keep track of subframe focus for key events!
880 frame->page->setFocus();
883 void QWebPage::mouseDoubleClickEvent(QMouseEvent *ev)
885 QWebFrame *f = d->currentFrame(ev->pos());
889 QWebFramePrivate *frame = f->d;
890 if (!frame->eventHandler)
893 frame->eventHandler->handleMousePressEvent(PlatformMouseEvent(ev, 2));
895 //FIXME need to keep track of subframe focus for key events!
896 frame->page->setFocus();
899 void QWebPage::mouseReleaseEvent(QMouseEvent *ev)
901 QWebFrame *f = d->currentFrame(ev->pos());
905 QWebFramePrivate *frame = f->d;
906 if (!frame->frameView)
909 frame->eventHandler->handleMouseReleaseEvent(PlatformMouseEvent(ev, 0));
911 //FIXME need to keep track of subframe focus for key events!
912 frame->page->setFocus();
913 d->frameUnderMouse = 0;
916 void QWebPage::contextMenuEvent(QContextMenuEvent *ev)
918 QWebFrame *f = d->currentFrame(ev->pos());
922 QWebFramePrivate *frame = f->d;
923 if (!frame->eventHandler)
926 d->page->contextMenuController()->clearContextMenu();
927 frame->eventHandler->sendContextMenuEvent(PlatformMouseEvent(ev, 1));
928 ContextMenu *menu = d->page->contextMenuController()->contextMenu();
930 QWebPageContext oldContext = d->currentContext;
931 d->currentContext = QWebPageContext(menu->hitTestResult());
933 const QList<ContextMenuItem> *items = menu->platformDescription();
934 QMenu *qmenu = d->createContextMenu(menu, items);
936 qmenu->exec(ev->globalPos());
939 d->currentContext = oldContext;
942 void QWebPage::wheelEvent(QWheelEvent *ev)
944 QWebFramePrivate *frame = d->currentFrame(ev->pos())->d;
946 bool accepted = false;
947 if (frame->eventHandler) {
948 WebCore::PlatformWheelEvent pev(ev);
949 accepted = frame->eventHandler->handleWheelEvent(pev);
952 ev->setAccepted(accepted);
954 //FIXME need to keep track of subframe focus for key events!
955 frame->page->setFocus();
957 if (!ev->isAccepted())
958 QWidget::wheelEvent(ev);
961 void QWebPage::keyPressEvent(QKeyEvent *ev)
963 if (!mainFrame()->d->eventHandler)
966 bool handled = false;
967 QWebFrame *frame = mainFrame();
968 WebCore::Editor *editor = frame->d->frame->editor();
969 if (editor->canEdit()) {
970 if (ev == QKeySequence::Cut) {
973 } else if (ev == QKeySequence::Copy) {
976 } else if (ev == QKeySequence::Paste) {
977 triggerAction(Paste);
979 } else if (ev == QKeySequence::Undo) {
982 } else if (ev == QKeySequence::Redo) {
985 } else if(ev == QKeySequence::MoveToNextChar) {
986 triggerAction(MoveToNextChar);
988 } else if(ev == QKeySequence::MoveToPreviousChar) {
989 triggerAction(MoveToPreviousChar);
991 } else if(ev == QKeySequence::MoveToNextWord) {
992 triggerAction(MoveToNextWord);
994 } else if(ev == QKeySequence::MoveToPreviousWord) {
995 triggerAction(MoveToPreviousWord);
997 } else if(ev == QKeySequence::MoveToNextLine) {
998 triggerAction(MoveToNextLine);
1000 } else if(ev == QKeySequence::MoveToPreviousLine) {
1001 triggerAction(MoveToPreviousLine);
1003 // } else if(ev == QKeySequence::MoveToNextPage) {
1004 // } else if(ev == QKeySequence::MoveToPreviousPage) {
1005 } else if(ev == QKeySequence::MoveToStartOfLine) {
1006 triggerAction(MoveToStartOfLine);
1008 } else if(ev == QKeySequence::MoveToEndOfLine) {
1009 triggerAction(MoveToEndOfLine);
1011 } else if(ev == QKeySequence::MoveToStartOfBlock) {
1012 triggerAction(MoveToStartOfBlock);
1014 } else if(ev == QKeySequence::MoveToEndOfBlock) {
1015 triggerAction(MoveToEndOfBlock);
1017 } else if(ev == QKeySequence::MoveToStartOfDocument) {
1018 triggerAction(MoveToStartOfDocument);
1020 } else if(ev == QKeySequence::MoveToEndOfDocument) {
1021 triggerAction(MoveToEndOfDocument);
1023 } else if(ev == QKeySequence::SelectNextChar) {
1024 triggerAction(SelectNextChar);
1026 } else if(ev == QKeySequence::SelectPreviousChar) {
1027 triggerAction(SelectPreviousChar);
1029 } else if(ev == QKeySequence::SelectNextWord) {
1030 triggerAction(SelectNextWord);
1032 } else if(ev == QKeySequence::SelectPreviousWord) {
1033 triggerAction(SelectPreviousWord);
1035 } else if(ev == QKeySequence::SelectNextLine) {
1036 triggerAction(SelectNextLine);
1038 } else if(ev == QKeySequence::SelectPreviousLine) {
1039 triggerAction(SelectPreviousLine);
1041 // } else if(ev == QKeySequence::SelectNextPage) {
1042 // } else if(ev == QKeySequence::SelectPreviousPage) {
1043 } else if(ev == QKeySequence::SelectStartOfLine) {
1044 triggerAction(SelectStartOfLine);
1046 } else if(ev == QKeySequence::SelectEndOfLine) {
1047 triggerAction(SelectEndOfLine);
1049 } else if(ev == QKeySequence::SelectStartOfBlock) {
1050 triggerAction(SelectStartOfBlock);
1052 } else if(ev == QKeySequence::SelectEndOfBlock) {
1053 triggerAction(SelectEndOfBlock);
1055 } else if(ev == QKeySequence::SelectStartOfDocument) {
1056 triggerAction(SelectStartOfDocument);
1058 } else if(ev == QKeySequence::SelectEndOfDocument) {
1059 triggerAction(SelectEndOfDocument);
1061 } else if(ev == QKeySequence::DeleteStartOfWord) {
1062 triggerAction(DeleteStartOfWord);
1064 } else if(ev == QKeySequence::DeleteEndOfWord) {
1065 triggerAction(DeleteEndOfWord);
1067 // } else if(ev == QKeySequence::DeleteEndOfLine) {
1071 handled = frame->d->eventHandler->keyEvent(ev);
1074 PlatformScrollbar *h, *v;
1075 h = mainFrame()->d->horizontalScrollBar();
1076 v = mainFrame()->d->verticalScrollBar();
1078 if (ev == QKeySequence::MoveToNextPage) {
1080 v->setValue(v->value() + height());
1081 } else if (ev == QKeySequence::MoveToPreviousPage) {
1083 v->setValue(v->value() - height());
1085 switch (ev->key()) {
1088 v->setValue(v->value() - 10);
1092 v->setValue(v->value() + 10);
1096 h->setValue(h->value() - 10);
1100 h->setValue(h->value() + 10);
1109 ev->setAccepted(handled);
1112 void QWebPage::keyReleaseEvent(QKeyEvent *ev)
1114 if (ev->isAutoRepeat()) {
1115 ev->setAccepted(true);
1119 if (!mainFrame()->d->eventHandler)
1122 bool handled = mainFrame()->d->eventHandler->keyEvent(ev);
1123 ev->setAccepted(handled);
1126 void QWebPage::focusInEvent(QFocusEvent *ev)
1128 if (ev->reason() != Qt::PopupFocusReason)
1129 mainFrame()->d->frame->page()->focusController()->setFocusedFrame(mainFrame()->d->frame);
1130 QWidget::focusInEvent(ev);
1133 void QWebPage::focusOutEvent(QFocusEvent *ev)
1135 QWidget::focusOutEvent(ev);
1136 if (ev->reason() != Qt::PopupFocusReason) {
1137 mainFrame()->d->frame->selectionController()->clear();
1138 mainFrame()->d->frame->setIsActive(false);
1142 bool QWebPage::focusNextPrevChild(bool next)
1148 void QWebPage::dragEnterEvent(QDragEnterEvent *ev)
1150 #ifndef QT_NO_DRAGANDDROP
1151 DragData dragData(ev->mimeData(), ev->pos(), QCursor::pos(),
1152 dropActionToDragOp(ev->possibleActions()));
1153 Qt::DropAction action = dragOpToDropAction(d->page->dragController()->dragEntered(&dragData));
1154 ev->setDropAction(action);
1159 void QWebPage::dragLeaveEvent(QDragLeaveEvent *ev)
1161 #ifndef QT_NO_DRAGANDDROP
1162 DragData dragData(0, IntPoint(), QCursor::pos(), DragOperationNone);
1163 d->page->dragController()->dragExited(&dragData);
1168 void QWebPage::dragMoveEvent(QDragMoveEvent *ev)
1170 #ifndef QT_NO_DRAGANDDROP
1171 DragData dragData(ev->mimeData(), ev->pos(), QCursor::pos(),
1172 dropActionToDragOp(ev->possibleActions()));
1173 Qt::DropAction action = dragOpToDropAction(d->page->dragController()->dragUpdated(&dragData));
1174 ev->setDropAction(action);
1179 void QWebPage::dropEvent(QDropEvent *ev)
1181 #ifndef QT_NO_DRAGANDDROP
1182 DragData dragData(ev->mimeData(), ev->pos(), QCursor::pos(),
1183 dropActionToDragOp(ev->possibleActions()));
1184 Qt::DropAction action = dragOpToDropAction(d->page->dragController()->performDrag(&dragData));
1189 void QWebPage::setNetworkInterface(QWebNetworkInterface *interface)
1191 d->networkInterface = interface;
1194 QWebNetworkInterface *QWebPage::networkInterface() const
1196 if (d->networkInterface)
1197 return d->networkInterface;
1199 return QWebNetworkInterface::defaultInterface();
1202 QPixmap QWebPage::icon() const
1204 Image* image = iconDatabase()->iconForPageURL(url().toString(), IntSize(16, 16));
1205 if (!image || image->isNull()) {
1206 image = iconDatabase()->defaultIcon(IntSize(16, 16));
1213 QPixmap *icon = image->getPixmap();
1220 QWebSettings *QWebPage::settings()
1225 QString QWebPage::chooseFile(QWebFrame *parentFrame, const QString& oldFile)
1227 //FIXME frame pos...
1228 #ifndef QT_NO_FILEDIALOG
1229 return QFileDialog::getOpenFileName(this, QString::null, oldFile);
1231 return QString::null;
1235 #ifndef QT_NO_NETWORKPROXY
1236 void QWebPage::setNetworkProxy(const QNetworkProxy& proxy)
1238 d->networkProxy = proxy;
1241 QNetworkProxy QWebPage::networkProxy() const
1243 return d->networkProxy;
1247 QString QWebPage::userAgentFor(const QUrl& url) const {
1249 return QLatin1String("Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en) AppleWebKit/418.9.1 (KHTML, like Gecko) Safari/419.3 Qt");
1253 void QWebPagePrivate::_q_onLoadProgressChanged(int) {
1254 m_totalBytes = page->progress()->totalPageAndResourceBytesToLoad();
1255 m_bytesReceived = page->progress()->totalBytesReceived();
1259 quint64 QWebPage::totalBytes() const {
1260 return d->m_bytesReceived;
1264 quint64 QWebPage::bytesReceived() const {
1265 return d->m_totalBytes;
1268 QWebPageContext::QWebPageContext(const WebCore::HitTestResult &hitTest)
1269 : d(new QWebPageContextPrivate)
1271 d->pos = hitTest.point();
1272 d->text = hitTest.textContent();
1273 d->linkUrl = hitTest.absoluteLinkURL().url();
1274 d->imageUrl = hitTest.absoluteImageURL().url();
1275 WebCore::Image *img = hitTest.image();
1277 QPixmap *pix = img->getPixmap();
1281 WebCore::Frame *frame = hitTest.targetFrame();
1283 d->targetFrame = frame->view()->qwebframe();
1286 QWebPageContext::QWebPageContext()
1291 QWebPageContext::QWebPageContext(const QWebPageContext &other)
1295 d = new QWebPageContextPrivate(*other.d);
1298 QWebPageContext &QWebPageContext::operator=(const QWebPageContext &other)
1300 if (this != &other) {
1303 d = new QWebPageContextPrivate;
1313 QWebPageContext::~QWebPageContext()
1318 QPoint QWebPageContext::pos() const
1325 QString QWebPageContext::text() const
1332 QUrl QWebPageContext::linkUrl() const
1339 QUrl QWebPageContext::imageUrl() const
1346 QPixmap QWebPageContext::image() const
1353 QWebFrame *QWebPageContext::targetFrame() const
1357 return d->targetFrame;
1360 #include "moc_qwebpage.cpp"