qwebpage.cpp 39.1 KB
Newer Older
1
2
/*
    Copyright (C) 2007 Trolltech ASA
3
    Copyright (C) 2007 Staikos Computing Services Inc.
4
    Copyright (C) 2007 Apple Inc.
5
6
7
8
9
10
11
12
13
14
15
16
17

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
ddkilzer's avatar
ddkilzer committed
18
19
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
20
21
22
23

    This class provides all functionality needed for loading images, style sheets and html
    pages from the web. It has a memory cache for these objects.
*/
24
#include "config.h"
25
26
27
28
#include "qwebpage.h"
#include "qwebframe.h"
#include "qwebpage_p.h"
#include "qwebframe_p.h"
lars's avatar
lars committed
29
#include "qwebnetworkinterface.h"
zack's avatar
zack committed
30
31
#include "qwebpagehistory.h"
#include "qwebpagehistory_p.h"
32
#include "qwebsettings.h"
33

mjs's avatar
mjs committed
34
#include "Frame.h"
35
#include "ChromeClientQt.h"
36
#include "ContextMenu.h"
37
#include "ContextMenuClientQt.h"
38
#include "DragClientQt.h"
39
40
#include "DragController.h"
#include "DragData.h"
41
#include "EditorClientQt.h"
lars's avatar
lars committed
42
#include "Settings.h"
43
44
#include "Page.h"
#include "FrameLoader.h"
45
#include "FrameLoadRequest.h"
46
#include "KURL.h"
47
48
#include "Image.h"
#include "IconDatabase.h"
thatcher's avatar
thatcher committed
49
#include "InspectorClientQt.h"
50
51
#include "FocusController.h"
#include "Editor.h"
52
53
#include "PlatformScrollBar.h"
#include "PlatformKeyboardEvent.h"
54
#include "PlatformWheelEvent.h"
55
#include "ProgressTracker.h"
56
57
#include "RefPtr.h"
#include "HashMap.h"
58
#include "HitTestResult.h"
hausmann's avatar
hausmann committed
59
#include "WindowFeatures.h"
60
#include "LocalizedStrings.h"
61

62
#include <QDebug>
63
64
65
66
#include <QDragEnterEvent>
#include <QDragLeaveEvent>
#include <QDragMoveEvent>
#include <QDropEvent>
67
68
69
70
#include <QFileDialog>
#include <QHttpRequestHeader>
#include <QInputDialog>
#include <QMessageBox>
71
#include <QNetworkProxy>
72
#include <QUndoStack>
staikos's avatar
staikos committed
73
#include <QUrl>
74
#include <QPainter>
75

76
77
78
using namespace WebCore;

QWebPagePrivate::QWebPagePrivate(QWebPage *qq)
79
80
    : q(qq)
    , modified(false)
81
{
82
    q->setMouseTracking(true);
83
    q->setFocusPolicy(Qt::ClickFocus);
84
85
    chromeClient = new ChromeClientQt(q);
    contextMenuClient = new ContextMenuClientQt();
zack's avatar
zack committed
86
    editorClient = new EditorClientQt(q);
87
    page = new Page(chromeClient, contextMenuClient, editorClient,
thatcher's avatar
thatcher committed
88
                    new DragClientQt(q), new InspectorClientQt());
89

hausmann's avatar
hausmann committed
90
91
92
93
94
    // ### should be configurable
    page->settings()->setDefaultTextEncodingName("iso-8859-1");

    settings = new QWebSettings(page->settings());

95
    undoStack = 0;
96
    mainFrame = 0;
97
    networkInterface = 0;
98
    insideOpenCall = false;
99
100

    history.d = new QWebPageHistoryPrivate(page->backForwardList());
101
    memset(actions, 0, sizeof(actions));
102
103
104
105
}

QWebPagePrivate::~QWebPagePrivate()
{
106
    delete undoStack;
hausmann's avatar
hausmann committed
107
    delete settings;
108
109
110
    delete page;
}

111
QWebPage::NavigationRequestResponse QWebPagePrivate::navigationRequested(QWebFrame *frame, const QWebNetworkRequest &request, QWebPage::NavigationType type)
112
113
114
115
{
    if (insideOpenCall
        && frame == mainFrame)
        return QWebPage::AcceptNavigationRequest;
116
    return q->navigationRequested(frame, request, type);
117
118
}

119
120
121
void QWebPagePrivate::createMainFrame()
{
    if (!mainFrame) {
lars's avatar
lars committed
122
123
124
        QWebFrameData frameData;
        frameData.ownerElement = 0;
        frameData.allowsScrolling = true;
lars's avatar
lars committed
125
126
        frameData.marginWidth = 0;
        frameData.marginHeight = 0;
hausmann's avatar
hausmann committed
127
        mainFrame = new QWebFrame(q, &frameData);
128
129
130
131
132
        QObject::connect(mainFrame, SIGNAL(titleChanged(const QString&)),
                q, SIGNAL(titleChanged(const QString&)));
        QObject::connect(mainFrame, SIGNAL(hoveringOverLink(const QString&, const QString&)),
                q, SIGNAL(hoveringOverLink(const QString&, const QString&)));
        
133
        mainFrame->d->frameView->setFrameGeometry(q->geometry());
134
135

        emit q->frameCreated(mainFrame);
136
137
138
    }
}

139
140
141
static QWebPage::WebAction webActionForContextMenuAction(WebCore::ContextMenuAction action)
{
    switch (action) {
142
        case WebCore::ContextMenuItemTagOpenLink: return QWebPage::OpenLink;
143
144
145
146
147
148
149
150
151
152
153
154
155
156
        case WebCore::ContextMenuItemTagOpenLinkInNewWindow: return QWebPage::OpenLinkInNewWindow;
        case WebCore::ContextMenuItemTagDownloadLinkToDisk: return QWebPage::DownloadLinkToDisk;
        case WebCore::ContextMenuItemTagCopyLinkToClipboard: return QWebPage::CopyLinkToClipboard;
        case WebCore::ContextMenuItemTagOpenImageInNewWindow: return QWebPage::OpenImageInNewWindow;
        case WebCore::ContextMenuItemTagDownloadImageToDisk: return QWebPage::DownloadImageToDisk;
        case WebCore::ContextMenuItemTagCopyImageToClipboard: return QWebPage::CopyImageToClipboard;
        case WebCore::ContextMenuItemTagOpenFrameInNewWindow: return QWebPage::OpenFrameInNewWindow;
        case WebCore::ContextMenuItemTagCopy: return QWebPage::Copy;
        case WebCore::ContextMenuItemTagGoBack: return QWebPage::GoBack;
        case WebCore::ContextMenuItemTagGoForward: return QWebPage::GoForward;
        case WebCore::ContextMenuItemTagStop: return QWebPage::Stop;
        case WebCore::ContextMenuItemTagReload: return QWebPage::Reload;
        case WebCore::ContextMenuItemTagCut: return QWebPage::Cut;
        case WebCore::ContextMenuItemTagPaste: return QWebPage::Paste;
157
158
159
        case WebCore::ContextMenuItemTagDefaultDirection: return QWebPage::SetTextDirectionDefault;
        case WebCore::ContextMenuItemTagLeftToRight: return QWebPage::SetTextDirectionLeftToRight;
        case WebCore::ContextMenuItemTagRightToLeft: return QWebPage::SetTextDirectionRightToLeft;
160
161
162
        case WebCore::ContextMenuItemTagBold: return QWebPage::ToggleBold;
        case WebCore::ContextMenuItemTagItalic: return QWebPage::ToggleItalic;
        case WebCore::ContextMenuItemTagUnderline: return QWebPage::ToggleUnderline;
163
164
165
166
167
        default: break;
    }
    return QWebPage::NoWebAction;
}

168
QMenu *QWebPagePrivate::createContextMenu(const WebCore::ContextMenu *webcoreMenu, const QList<WebCore::ContextMenuItem> *items)
169
170
171
172
173
{
    QMenu *menu = new QMenu;
    for (int i = 0; i < items->count(); ++i) {
        const ContextMenuItem &item = items->at(i);
        switch (item.type()) {
174
175
            case WebCore::ActionType: {
                QWebPage::WebAction action = webActionForContextMenuAction(item.action());
176
                QAction *a = q->action(action);
177
178
179
180
181
182
183
                if (a) {
                    ContextMenuItem it(item);
                    webcoreMenu->checkOrEnableIfNeeded(it);
                    PlatformMenuItemDescription desc = it.releasePlatformDescription();
                    a->setEnabled(desc.enabled);
                    a->setChecked(desc.checked);

184
                    menu->addAction(a);
185
                }
186
                break;
187
            }
188
189
190
191
            case WebCore::SeparatorType:
                menu->addSeparator();
                break;
            case WebCore::SubmenuType: {
192
                QMenu *subMenu = createContextMenu(webcoreMenu, item.platformSubMenu());
193
194
195
196
197
198
                if (!subMenu->actions().isEmpty()) {
                    subMenu->setTitle(item.title());
                    menu->addAction(subMenu->menuAction());
                } else {
                    delete subMenu;
                }
199
200
201
202
203
204
205
                break;
            }
        }
    }
    return menu;
}

206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
QWebFrame *QWebPagePrivate::frameAt(const QPoint &pos) const
{
    QWebFrame *frame = mainFrame;

redo:
    QList<QWebFrame*> children = frame->childFrames();
    for (int i = 0; i < children.size(); ++i) {
        if (children.at(i)->geometry().contains(pos)) {
            frame = children.at(i);
            goto redo;
        }
    }
    if (frame->geometry().contains(pos))
        return frame;
    return 0;
}
222

223
224
225
226
227
228
void QWebPagePrivate::_q_webActionTriggered(bool checked)
{
    QAction *a = qobject_cast<QAction *>(q->sender());
    if (!a)
        return;
    QWebPage::WebAction action = static_cast<QWebPage::WebAction>(a->data().toInt());
229
    q->triggerAction(action, checked);
230
231
}

232
233
234
235
236
237
238
void QWebPagePrivate::updateAction(QWebPage::WebAction action)
{
    QAction *a = actions[action];
    if (!a || !mainFrame)
        return;

    WebCore::FrameLoader *loader = mainFrame->d->frame->loader();
239
    WebCore::Editor *editor = page->focusController()->focusedOrMainFrame()->editor();
240
241
242
243
244
245
246
247
248
249
250
251
252

    bool enabled = a->isEnabled();

    switch (action) {
        case QWebPage::GoBack:
            enabled = loader->canGoBackOrForward(-1);
            break;
        case QWebPage::GoForward:
            enabled = loader->canGoBackOrForward(1);
            break;
        case QWebPage::Stop:
            enabled = loader->isLoading();
            break;
253
254
255
        case QWebPage::Reload:
            enabled = !loader->isLoading();
            break;
256
257
258
259
260
261
262
263
264
        case QWebPage::Cut:
            enabled = editor->canCut();
            break;
        case QWebPage::Copy:
            enabled = editor->canCopy();
            break;
        case QWebPage::Paste:
            enabled = editor->canPaste();
            break;
265
266
267
268
        case QWebPage::Undo:
        case QWebPage::Redo:
            // those two are handled by QUndoStack
            break;
269
270
271
272
273
274
275
276
277
278
279
        default: break;
    }

    a->setEnabled(enabled);
}

void QWebPagePrivate::updateNavigationActions()
{
    updateAction(QWebPage::GoBack);
    updateAction(QWebPage::GoForward);
    updateAction(QWebPage::Stop);
280
    updateAction(QWebPage::Reload);
281
282
}

283
284
285
286
287
288
289
void QWebPagePrivate::updateEditorActions()
{
    updateAction(QWebPage::Cut);
    updateAction(QWebPage::Copy);
    updateAction(QWebPage::Paste);
}

290
291
292
293
QWebPage::QWebPage(QWidget *parent)
    : QWidget(parent)
    , d(new QWebPagePrivate(this))
{
hausmann's avatar
hausmann committed
294

295
296
    QPalette pal = palette();
    pal.setBrush(QPalette::Background, Qt::white);
297
298
299

    setAttribute(Qt::WA_OpaquePaintEvent);

300
    setPalette(pal);
301
    setAcceptDrops(true);
302
    connect(this, SIGNAL(loadProgressChanged(int)), this, SLOT(_q_onLoadProgressChanged(int)));
303
304
305
306
}

QWebPage::~QWebPage()
{
lars's avatar
lars committed
307
308
309
    FrameLoader *loader = d->mainFrame->d->frame->loader();
    if (loader)
        loader->detachFromParent();
310
311
312
313
    delete d;
}

void QWebPage::open(const QUrl &url)
314
{
315
    open(QWebNetworkRequest(url));
316
317
}

318
void QWebPage::open(const QWebNetworkRequest &req)
319
{
320
    d->insideOpenCall = true;
321
322
323
324
325

    QUrl url = req.url();
    QHttpRequestHeader httpHeader = req.httpHeader();
    QByteArray postData = req.postData();

326
327
    WebCore::ResourceRequest request(KURL(url.toString()));

328
329
330
331
    QString method = httpHeader.method();
    if (!method.isEmpty())
        request.setHTTPMethod(method);

332
333
334
335
336
    QList<QPair<QString, QString> > values = httpHeader.values();
    for (int i = 0; i < values.size(); ++i) {
        const QPair<QString, QString> &val = values.at(i);
        request.addHTTPHeaderField(val.first, val.second);
    }
337
338
339
340

    if (!postData.isEmpty()) {
        WTF::RefPtr<WebCore::FormData> formData = new WebCore::FormData(postData.constData(), postData.size());
        request.setHTTPBody(formData);
341
342
343
    }

    mainFrame()->d->frame->loader()->load(request);
344
    d->insideOpenCall = false;
345
346
347
348
349
350
}

QUrl QWebPage::url() const
{
    return QUrl((QString)mainFrame()->d->frame->loader()->url().url());
}
351

352
353
354
QString QWebPage::title() const
{
    return mainFrame()->title();
355
356
357
358
359
360
361
362
363
364
365
366
367
}

QWebFrame *QWebPage::mainFrame() const
{
    d->createMainFrame();
    return d->mainFrame;
}

QSize QWebPage::sizeHint() const
{
    return QSize(800, 600);
}

368
369
void QWebPage::stop()
{
370
    triggerAction(Stop);
371
372
}

373
QWebPageHistory *QWebPage::history() const
zack's avatar
zack committed
374
{
375
    return &d->history;
zack's avatar
zack committed
376
377
}

378
379
380
381
void QWebPage::javaScriptConsoleMessage(const QString& message, unsigned int lineNumber, const QString& sourceID)
{
}

382
383
void QWebPage::javaScriptAlert(QWebFrame *frame, const QString& msg)
{
384
385
    //FIXME frame pos...
    QMessageBox::information(this, title(), msg, QMessageBox::Ok);
386
387
388
}

bool QWebPage::javaScriptConfirm(QWebFrame *frame, const QString& msg)
389
{
390
391
    //FIXME frame pos...
    return 0 == QMessageBox::information(this, title(), msg, QMessageBox::Yes, QMessageBox::No);
392
393
}

394
bool QWebPage::javaScriptPrompt(QWebFrame *frame, const QString& msg, const QString& defaultValue, QString* result)
395
{
396
    //FIXME frame pos...
397
    bool ok = false;
398
#ifndef QT_NO_INPUTDIALOG
399
    QString x = QInputDialog::getText(this, title(), msg, QLineEdit::Normal, defaultValue, &ok);
400
401
402
    if (ok && result) {
        *result = x;
    }
403
#endif
404
    return ok;
405
406
}

staikos's avatar
staikos committed
407
408
409
410
QWebPage *QWebPage::createWindow()
{
    return 0;
}
411

treat's avatar
treat committed
412
413
414
415
416
QWebPage *QWebPage::createModalDialog()
{
    return 0;
}

417
QObject *QWebPage::createPlugin(const QString &classid, const QUrl &url, const QStringList &paramNames, const QStringList &paramValues)
418
{
419
    Q_UNUSED(classid)
420
421
422
423
424
425
    Q_UNUSED(url)
    Q_UNUSED(paramNames)
    Q_UNUSED(paramValues)
    return 0;
}

426
427
428
429
430
431
432
433
434
static WebCore::FrameLoadRequest frameLoadRequest(const QUrl &url, WebCore::Frame *frame)
{
    WebCore::ResourceRequest rr(WebCore::KURL(url.toString()),
                                frame->loader()->outgoingReferrer());
    return WebCore::FrameLoadRequest(rr);
}

static void openNewWindow(const QUrl& url, WebCore::Frame* frame)
{
hausmann's avatar
hausmann committed
435
436
    if (Page* oldPage = frame->page()) {
        WindowFeatures features;
437
        if (Page* newPage = oldPage->chrome()->createWindow(frame,
hausmann's avatar
hausmann committed
438
                frameLoadRequest(url, frame), features))
439
            newPage->chrome()->show();
hausmann's avatar
hausmann committed
440
    }
441
442
}

443
void QWebPage::triggerAction(WebAction action, bool checked)
444
{
445
446
    WebCore::Frame *frame = d->page->focusController()->focusedOrMainFrame();
    WebCore::Editor *editor = frame->editor();
447
448
    const char *command = 0;

449
    switch (action) {
450
451
452
        case OpenLink:
            if (QWebFrame *targetFrame = d->currentContext.targetFrame()) {
                WTF::RefPtr<WebCore::Frame> wcFrame = targetFrame->d->frame;
453
                targetFrame->d->frame->loader()->load(frameLoadRequest(d->currentContext.linkUrl(), wcFrame.get()),
454
455
456
457
458
459
460
461
462
463
                                                      /*lockHistory*/ false,
                                                      /*userGesture*/ true,
                                                      /*event*/ 0,
                                                      /*HTMLFormElement*/ 0,
                                                      /*formValues*/
                                                      WTF::HashMap<String, String>());
                break;
            } else {
            }
            // fall through
464
        case OpenLinkInNewWindow:
465
            openNewWindow(d->currentContext.linkUrl(), frame);
466
            break;
467
        case OpenFrameInNewWindow:
468
            break;
469
470
        case DownloadLinkToDisk:
        case CopyLinkToClipboard:
471
472
            editor->copyURL(WebCore::KURL(d->currentContext.linkUrl().toString()), d->currentContext.text());
            break;
473
        case OpenImageInNewWindow:
474
            openNewWindow(d->currentContext.imageUrl(), frame);
475
            break;
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
        case DownloadImageToDisk:
        case CopyImageToClipboard:
            break;
        case GoBack:
            d->page->goBack();
            break;
        case GoForward:
            d->page->goForward();
            break;
        case Stop:
            mainFrame()->d->frame->loader()->stopForUserCancel();
            break;
        case Reload:
            mainFrame()->d->frame->loader()->reload();
            break;
        case Cut:
492
            editor->cut();
493
494
            break;
        case Copy:
495
            editor->copy();
496
497
            break;
        case Paste:
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
            editor->paste();
            break;

        case Undo:
            editor->undo();
            break;
        case Redo:
            editor->redo();
            break;

        case MoveToNextChar:
            command = "MoveForward";
            break;
        case MoveToPreviousChar:
            command = "MoveBackward";
            break;
        case MoveToNextWord:
            command = "MoveWordForward";
            break;
        case MoveToPreviousWord:
            command = "MoveWordBackward";
            break;
        case MoveToNextLine:
            command = "MoveDown";
            break;
        case MoveToPreviousLine:
            command = "MoveUp";
            break;
        case MoveToStartOfLine:
            command = "MoveToBeginningOfLine";
            break;
        case MoveToEndOfLine:
            command = "MoveToEndOfLine";
            break;
        case MoveToStartOfBlock:
            command = "MoveToBeginningOfParagraph";
            break;
        case MoveToEndOfBlock:
            command = "MoveToEndOfParagraph";
            break;
        case MoveToStartOfDocument:
            command = "MoveToBeginningOfDocument";
            break;
        case MoveToEndOfDocument:
            command = "MoveToEndOfDocument";
            break;
        case SelectNextChar:
            command = "MoveForwardAndModifySelection";
            break;
        case SelectPreviousChar:
            command = "MoveBackwardAndModifySelection";
            break;
        case SelectNextWord:
            command = "MoveWordForwardAndModifySelection";
            break;
        case SelectPreviousWord:
            command = "MoveWordBackwardAndModifySelection";
            break;
        case SelectNextLine:
            command = "MoveDownAndModifySelection";
            break;
        case SelectPreviousLine:
            command = "MoveUpAndModifySelection";
            break;
        case SelectStartOfLine:
            command = "MoveToBeginningOfLineAndModifySelection";
            break;
        case SelectEndOfLine:
            command = "MoveToEndOfLineAndModifySelection";
            break;
        case SelectStartOfBlock:
            command = "MoveToBeginningOfParagraphAndModifySelection";
            break;
        case SelectEndOfBlock:
            command = "MoveToEndOfParagraphAndModifySelection";
            break;
        case SelectStartOfDocument:
            command = "MoveToBeginningOfDocumentAndModifySelection";
            break;
        case SelectEndOfDocument:
            command = "MoveToEndOfDocumentAndModifySelection";
            break;
        case DeleteStartOfWord:
            command = "DeleteWordBackward";
            break;
        case DeleteEndOfWord:
            command = "DeleteWordForward";
585
            break;
586

587
588
589
590
591
592
593
594
595
596
        case SetTextDirectionDefault:
            editor->setBaseWritingDirection("inherit");
            break;
        case SetTextDirectionLeftToRight:
            editor->setBaseWritingDirection("ltr");
            break;
        case SetTextDirectionRightToLeft:
            editor->setBaseWritingDirection("rtl");
            break;

597
598
599
600
601
602
603
604
605
606
        case ToggleBold:
            command = "ToggleBold";
            break;
        case ToggleItalic:
            command = "ToggleItalic";
            break;
        case ToggleUnderline:
            editor->toggleUnderline();
            break;

607
608
        default: break;
    }
609
610
611

    if (command)
        editor->execCommand(command);
612
613
}

614
QWebPage::NavigationRequestResponse QWebPage::navigationRequested(QWebFrame *frame, const QWebNetworkRequest &request, QWebPage::NavigationType type)
615
616
617
618
619
{
    Q_UNUSED(request)
    return AcceptNavigationRequest;
}

620
621
622
623
624
QString QWebPage::selectedText() const
{
    return d->page->focusController()->focusedOrMainFrame()->selectedText();
}

625
QAction *QWebPage::action(WebAction action) const
626
627
628
629
630
631
{
    if (action == QWebPage::NoWebAction) return 0;
    if (d->actions[action])
        return d->actions[action];

    QString text;
632
    bool checkable = false;
633
634

    switch (action) {
635
636
637
        case OpenLink:
            text = contextMenuItemTagOpenLink();
            break;
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
        case OpenLinkInNewWindow:
            text = contextMenuItemTagOpenLinkInNewWindow();
            break;
        case OpenFrameInNewWindow:
            text = contextMenuItemTagOpenFrameInNewWindow();
            break;

        case DownloadLinkToDisk:
            text = contextMenuItemTagDownloadLinkToDisk();
            break;
        case CopyLinkToClipboard:
            text = contextMenuItemTagCopyLinkToClipboard();
            break;

        case OpenImageInNewWindow:
            text = contextMenuItemTagOpenImageInNewWindow();
            break;
        case DownloadImageToDisk:
            text = contextMenuItemTagDownloadImageToDisk();
            break;
        case CopyImageToClipboard:
            text = contextMenuItemTagCopyImageToClipboard();
            break;

        case GoBack:
            text = contextMenuItemTagGoBack();
            break;
        case GoForward:
            text = contextMenuItemTagGoForward();
            break;
        case Stop:
            text = contextMenuItemTagStop();
            break;
        case Reload:
            text = contextMenuItemTagReload();
            break;

        case Cut:
            text = contextMenuItemTagCut();
            break;
        case Copy:
            text = contextMenuItemTagCopy();
            break;
        case Paste:
            text = contextMenuItemTagPaste();
            break;

685
686
687
688
689
690
691
692
693
694
        case Undo: {
            QAction *a = undoStack()->createUndoAction(d->q);
            d->actions[action] = a;
            return a;
        }
        case Redo: {
            QAction *a = undoStack()->createRedoAction(d->q);
            d->actions[action] = a;
            return a;
        }
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
        case MoveToNextChar:
        case MoveToPreviousChar:
        case MoveToNextWord:
        case MoveToPreviousWord:
        case MoveToNextLine:
        case MoveToPreviousLine:
        case MoveToStartOfLine:
        case MoveToEndOfLine:
        case MoveToStartOfBlock:
        case MoveToEndOfBlock:
        case MoveToStartOfDocument:
        case MoveToEndOfDocument:
        case SelectNextChar:
        case SelectPreviousChar:
        case SelectNextWord:
        case SelectPreviousWord:
        case SelectNextLine:
        case SelectPreviousLine:
        case SelectStartOfLine:
        case SelectEndOfLine:
        case SelectStartOfBlock:
        case SelectEndOfBlock:
        case SelectStartOfDocument:
        case SelectEndOfDocument:
        case DeleteStartOfWord:
        case DeleteEndOfWord:
            break; // ####

723
724
725
726
727
        case SetTextDirectionDefault:
            text = contextMenuItemTagDefaultDirection();
            break;
        case SetTextDirectionLeftToRight:
            text = contextMenuItemTagLeftToRight();
728
            checkable = true;
729
730
731
            break;
        case SetTextDirectionRightToLeft:
            text = contextMenuItemTagRightToLeft();
732
            checkable = true;
733
734
            break;

735
736
737
738
739
740
741
742
743
744
745
746
747
        case ToggleBold:
            text = contextMenuItemTagBold();
            checkable = true;
            break;
        case ToggleItalic:
            text = contextMenuItemTagItalic();
            checkable = true;
            break;
        case ToggleUnderline:
            text = contextMenuItemTagUnderline();
            checkable = true;
            break;

748
749
750
751
752
753
754
755
756
757
        case NoWebAction:
            return 0;
    }

    if (text.isEmpty())
        return 0;

    QAction *a = new QAction(d->q);
    a->setText(text);
    a->setData(action);
758
    a->setCheckable(checkable);
759
760
761
762
763

    connect(a, SIGNAL(triggered(bool)),
            this, SLOT(_q_webActionTriggered(bool)));

    d->actions[action] = a;
764
    d->updateAction(action);
765
766
767
    return a;
}

768
769
770
771
772
773
774
775
/*!
  Returns true if the page contains unsubmitted form data.
*/
bool QWebPage::isModified() const
{
    return d->modified;
}

776

777
QUndoStack *QWebPage::undoStack() const
778
779
{
    if (!d->undoStack)
780
        d->undoStack = new QUndoStack(const_cast<QWebPage *>(this));
781
782
783
784

    return d->undoStack;
}

785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
static inline DragOperation dropActionToDragOp(Qt::DropActions actions)
{
    unsigned result = 0;
    if (actions & Qt::CopyAction)
        result |= DragOperationCopy;
    if (actions & Qt::MoveAction)
        result |= DragOperationMove;
    if (actions & Qt::LinkAction)
        result |= DragOperationLink;
    return (DragOperation)result;    
}

static inline Qt::DropAction dragOpToDropAction(unsigned actions)
{
    Qt::DropAction result = Qt::IgnoreAction;
    if (actions & DragOperationCopy)
        result = Qt::CopyAction;
    else if (actions & DragOperationMove)
        result = Qt::MoveAction;
    else if (actions & DragOperationLink)
        result = Qt::LinkAction;
    return result;    
}

809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
void QWebPage::resizeEvent(QResizeEvent *e)
{
    QWidget::resizeEvent(e);
    if (mainFrame()->d->frame && mainFrame()->d->frameView) {
        mainFrame()->d->frameView->setFrameGeometry(rect());
        mainFrame()->d->frame->forceLayout();
        mainFrame()->d->frame->view()->adjustViewSize();
    }
}

void QWebPage::paintEvent(QPaintEvent *ev)
{
#ifdef QWEBKIT_TIME_RENDERING
    QTime time;
    time.start();
#endif

    QPainter p(this);

828
829
830
831
832
833
834
835
    QVector<QRect> vector = ev->region().rects();
    if (!vector.isEmpty()) {
        for (int i = 0; i < vector.size(); ++i) {
            mainFrame()->render(&p, vector.at(i));
        }
    } else {
        mainFrame()->render(&p, ev->rect());
    }
836
837
838

#ifdef    QWEBKIT_TIME_RENDERING
    int elapsed = time.elapsed();
839
    qDebug()<<"paint event on "<<ev->region()<<", took to render =  "<<elapsed;
840
841
842
843
844
#endif
}

void QWebPage::mouseMoveEvent(QMouseEvent *ev)
{
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
    QWebFramePrivate *frame = d->currentFrame(ev->pos())->d;
    if (!frame->frameView)
        return;

    frame->eventHandler->handleMouseMoveEvent(PlatformMouseEvent(ev, 0));
    const int xOffset =
        frame->horizontalScrollBar() ? frame->horizontalScrollBar()->value() : 0;
    const int yOffset =
        frame->verticalScrollBar() ? frame->verticalScrollBar()->value() : 0;
    IntPoint pt(ev->x() + xOffset, ev->y() + yOffset);
    WebCore::HitTestResult result = frame->eventHandler->hitTestResultAtPoint(pt, false);
    WebCore::Element *link = result.URLElement();
    if (link != frame->lastHoverElement) {
        frame->lastHoverElement = link;
        emit hoveringOverLink(result.absoluteLinkURL().prettyURL(), result.title());
    }
861
862
863
864
}

void QWebPage::mousePressEvent(QMouseEvent *ev)
{
865
866
867
868
869
    d->frameUnderMouse = d->frameAt(ev->pos());
    QWebFramePrivate *frame = d->frameUnderMouse->d;
    if (!frame->eventHandler)
        return;

870
    frame->eventHandler->handleMousePressEvent(PlatformMouseEvent(ev, 1));
871
872
873

    //FIXME need to keep track of subframe focus for key events!
    frame->page->setFocus();
874
875
876
877
}

void QWebPage::mouseDoubleClickEvent(QMouseEvent *ev)
{
878
879
880
881
882
883
884
885
    QWebFramePrivate *frame = d->currentFrame(ev->pos())->d;
    if (!frame->eventHandler)
        return;

    frame->eventHandler->handleMousePressEvent(PlatformMouseEvent(ev, 2));

    //FIXME need to keep track of subframe focus for key events!
    frame->page->setFocus();
886
887
888
889
}

void QWebPage::mouseReleaseEvent(QMouseEvent *ev)
{
890
891
892
893
894
895
896
897
    QWebFramePrivate *frame = d->currentFrame(ev->pos())->d;
    if (frame->frameView) {
        frame->eventHandler->handleMouseReleaseEvent(PlatformMouseEvent(ev, 0));

        //FIXME need to keep track of subframe focus for key events!
        frame->page->setFocus();
    }
    d->frameUnderMouse = 0;
898
899
}

900
901
902
void QWebPage::contextMenuEvent(QContextMenuEvent *ev)
{
    QWebFramePrivate *frame = d->currentFrame(ev->pos())->d;
903
904
905
906
907
    if (!frame->eventHandler)
        return;
    d->page->contextMenuController()->clearContextMenu();
    frame->eventHandler->sendContextMenuEvent(PlatformMouseEvent(ev, 1));
    ContextMenu *menu = d->page->contextMenuController()->contextMenu();
908

909
910
911
    QWebPageContext oldContext = d->currentContext;
    d->currentContext = QWebPageContext(menu->hitTestResult());

912
    const QList<ContextMenuItem> *items = menu->platformDescription();
913
    QMenu *qmenu = d->createContextMenu(menu, items);
914
915
916
917
    if (qmenu) {
        qmenu->exec(ev->globalPos());
        delete qmenu;
    }
918
    d->currentContext = oldContext;
919
920
}

921
922
void QWebPage::wheelEvent(QWheelEvent *ev)
{
923
924
925
926
927
928
929
930
931
932
933
934
935
    QWebFramePrivate *frame = d->currentFrame(ev->pos())->d;

    bool accepted = false;
    if (frame->eventHandler) {
        WebCore::PlatformWheelEvent pev(ev);
        accepted = frame->eventHandler->handleWheelEvent(pev);
    }

    ev->setAccepted(accepted);

    //FIXME need to keep track of subframe focus for key events!
    frame->page->setFocus();

936
937
938
939
940
941
942
943
944
    if (!ev->isAccepted())
        QWidget::wheelEvent(ev);
}

void QWebPage::keyPressEvent(QKeyEvent *ev)
{
    if (!mainFrame()->d->eventHandler)
        return;

945
946
947
948
949
    bool handled = false;
    QWebFrame *frame = mainFrame();
    WebCore::Editor *editor = frame->d->frame->editor();
    if (editor->canEdit()) {
        if (ev == QKeySequence::Cut) {
950
            triggerAction(Cut);
951
952
            handled = true;
        } else if (ev == QKeySequence::Copy) {
953
            triggerAction(Copy);
954
955
            handled = true;
        } else if (ev == QKeySequence::Paste) {
956
            triggerAction(Paste);
957
958
            handled = true;
        } else if (ev == QKeySequence::Undo) {
959
            triggerAction(Undo);
960
961
            handled = true;
        } else if (ev == QKeySequence::Redo) {
962
            triggerAction(Redo);
963
964
            handled = true;
        } else if(ev == QKeySequence::MoveToNextChar) {
965
            triggerAction(MoveToNextChar);
966
            handled = true;
967
        } else if(ev == QKeySequence::MoveToPreviousChar) {
968
            triggerAction(MoveToPreviousChar);
969
            handled = true;
970
        } else if(ev == QKeySequence::MoveToNextWord) {
971
            triggerAction(MoveToNextWord);
972
            handled = true;
973
        } else if(ev == QKeySequence::MoveToPreviousWord) {
974
            triggerAction(MoveToPreviousWord);
975
            handled = true;
976
        } else if(ev == QKeySequence::MoveToNextLine) {
977
            triggerAction(MoveToNextLine);
978
            handled = true;
979
        } else if(ev == QKeySequence::MoveToPreviousLine) {
980
            triggerAction(MoveToPreviousLine);
981
            handled = true;
982
983
984
//             } else if(ev == QKeySequence::MoveToNextPage) {
//             } else if(ev == QKeySequence::MoveToPreviousPage) {
        } else if(ev == QKeySequence::MoveToStartOfLine) {
985
            triggerAction(MoveToStartOfLine);
986
            handled = true;
987
        } else if(ev == QKeySequence::MoveToEndOfLine) {
988
            triggerAction(MoveToEndOfLine);
989
            handled = true;
990
        } else if(ev == QKeySequence::MoveToStartOfBlock) {
991
            triggerAction(MoveToStartOfBlock);
992
            handled = true;
993
        } else if(ev == QKeySequence::MoveToEndOfBlock) {
994
            triggerAction(MoveToEndOfBlock);
995
            handled = true;
996
        } else if(ev == QKeySequence::MoveToStartOfDocument) {
997
            triggerAction(MoveToStartOfDocument);
998
            handled = true;
999
        } else if(ev == QKeySequence::MoveToEndOfDocument) {
1000
            triggerAction(MoveToEndOfDocument);
1001
            handled = true;
1002
        } else if(ev == QKeySequence::SelectNextChar) {
1003
            triggerAction(SelectNextChar);
1004
            handled = true;
1005
        } else if(ev == QKeySequence::SelectPreviousChar) {
1006
            triggerAction(SelectPreviousChar);
1007
            handled = true;
1008
        } else if(ev == QKeySequence::SelectNextWord) {
1009
            triggerAction(SelectNextWord);
1010
            handled = true;
1011
        } else if(ev == QKeySequence::SelectPreviousWord) {
1012
            triggerAction(SelectPreviousWord);
1013
            handled = true;
1014
        } else if(ev == QKeySequence::SelectNextLine) {
1015
            triggerAction(SelectNextLine);
1016
            handled = true;
1017
        } else if(ev == QKeySequence::SelectPreviousLine) {
1018
            triggerAction(SelectPreviousLine);
1019
            handled = true;
1020
1021
1022
//             } else if(ev == QKeySequence::SelectNextPage) {
//             } else if(ev == QKeySequence::SelectPreviousPage) {
        } else if(ev == QKeySequence::SelectStartOfLine) {
1023
            triggerAction(SelectStartOfLine);
1024
            handled = true;
1025
        } else if(ev == QKeySequence::SelectEndOfLine) {
1026
            triggerAction(SelectEndOfLine);
1027
            handled = true;
1028
        } else if(ev == QKeySequence::SelectStartOfBlock) {
1029
            triggerAction(SelectStartOfBlock);
1030
            handled = true;
1031
        } else if(ev == QKeySequence::SelectEndOfBlock) {
1032
            triggerAction(SelectEndOfBlock);
1033
            handled = true;
1034
        } else if(ev == QKeySequence::SelectStartOfDocument) {
1035
            triggerAction(SelectStartOfDocument);
1036
            handled = true;
1037
        } else if(ev == QKeySequence::SelectEndOfDocument) {
1038
            triggerAction(SelectEndOfDocument);
1039
            handled = true;
1040
        } else if(ev == QKeySequence::DeleteStartOfWord) {
1041
            triggerAction(DeleteStartOfWord);
1042
            handled = true;
1043
        } else if(ev == QKeySequence::DeleteEndOfWord) {
1044
            triggerAction(DeleteEndOfWord);
1045
            handled = true;
1046
//             } else if(ev == QKeySequence::DeleteEndOfLine) {
1047
1048
1049
        }
    }
    if (!handled) 
1050
        handled = frame->d->eventHandler->keyEvent(ev);
1051
    if (!handled) {
1052
1053
1054
1055
1056
        handled = true;
        PlatformScrollbar *h, *v;
        h = mainFrame()->d->horizontalScrollBar();
        v = mainFrame()->d->verticalScrollBar();

1057
1058
1059
1060
1061
1062
1063
1064
        if (ev == QKeySequence::MoveToNextPage) {
            if (v)
                v->setValue(v->value() + height());
        } else if (ev == QKeySequence::MoveToPreviousPage) {
            if (v)
                v->setValue(v->value() - height());
        } else {
            switch (ev->key()) {
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
            case Qt::Key_Up:
                if (v)
                    v->setValue(v->value() - 10);
                break;
            case Qt::Key_Down:
                if (v)
                    v->setValue(v->value() + 10);
                break;
            case Qt::Key_Left:
                if (h)
                    h->setValue(h->value() - 10);
                break;
            case Qt::Key_Right:
                if (h)
                    h->setValue(h->value() + 10);
                break;
            default:
                handled = false;
                break;
1084
            }
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
        }
    }

    ev->setAccepted(handled);
}

void QWebPage::keyReleaseEvent(QKeyEvent *ev)
{
    if (ev->isAutoRepeat()) {
        ev->setAccepted(true);
        return;
    }

    if (!mainFrame()->d->eventHandler)
        return;

1101
    bool handled = mainFrame()->d->eventHandler->keyEvent(ev);
1102
1103
1104
1105
1106
    ev->setAccepted(handled);
}

void QWebPage::focusInEvent(QFocusEvent *ev)
{
1107
    if (ev->reason() != Qt::PopupFocusReason) 
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
        mainFrame()->d->frame->page()->focusController()->setFocusedFrame(mainFrame()->d->frame);
    QWidget::focusInEvent(ev);
}

void QWebPage::focusOutEvent(QFocusEvent *ev)
{
    QWidget::focusOutEvent(ev);
    if (ev->reason() != Qt::PopupFocusReason) {
        mainFrame()->d->frame->selectionController()->clear();
        mainFrame()->d->frame->setIsActive(false);
    }
}

bool QWebPage::focusNextPrevChild(bool next)
{
    Q_UNUSED(next)
    return false;
}

1127
1128
void QWebPage::dragEnterEvent(QDragEnterEvent *ev)
{
1129
#ifndef QT_NO_DRAGANDDROP
1130
1131
1132
1133
1134
    DragData dragData(ev->mimeData(), ev->pos(), QCursor::pos(), 
                      dropActionToDragOp(ev->possibleActions()));
    Qt::DropAction action = dragOpToDropAction(d->page->dragController()->dragEntered(&dragData));
    ev->setDropAction(action);
    ev->accept();
1135
#endif
1136
1137
1138
1139
}

void QWebPage::dragLeaveEvent(QDragLeaveEvent *ev)
{