ContextMenuController.cpp 59 KB
Newer Older
aroben's avatar
aroben committed
1
/*
darin's avatar
darin committed
2
 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3
 * Copyright (C) 2010 Igalia S.L
aroben's avatar
aroben committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#include "config.h"
#include "ContextMenuController.h"

30 31
#if ENABLE(CONTEXT_MENUS)

darin@apple.com's avatar
darin@apple.com committed
32
#include "BackForwardController.h"
aroben's avatar
aroben committed
33
#include "Chrome.h"
aroben's avatar
aroben committed
34 35
#include "ContextMenu.h"
#include "ContextMenuClient.h"
36
#include "ContextMenuItem.h"
37
#include "ContextMenuProvider.h"
bdakin's avatar
bdakin committed
38 39 40 41 42
#include "Document.h"
#include "DocumentFragment.h"
#include "DocumentLoader.h"
#include "Editor.h"
#include "EditorClient.h"
aroben's avatar
aroben committed
43
#include "Event.h"
bdakin's avatar
bdakin committed
44
#include "EventHandler.h"
aroben's avatar
aroben committed
45
#include "EventNames.h"
46
#include "ExceptionCodePlaceholder.h"
darin@apple.com's avatar
darin@apple.com committed
47
#include "FormState.h"
bdakin's avatar
bdakin committed
48
#include "FrameLoadRequest.h"
49
#include "FrameLoader.h"
50
#include "FrameLoaderClient.h"
51
#include "FrameSelection.h"
52
#include "HTMLFormElement.h"
aroben's avatar
aroben committed
53 54
#include "HitTestRequest.h"
#include "HitTestResult.h"
55
#include "InspectorController.h"
56
#include "LocalizedStrings.h"
darin@apple.com's avatar
darin@apple.com committed
57
#include "MainFrame.h"
aroben's avatar
aroben committed
58
#include "MouseEvent.h"
59
#include "NavigationAction.h"
aroben's avatar
aroben committed
60
#include "Node.h"
bdakin's avatar
bdakin committed
61
#include "Page.h"
62
#include "PlatformEvent.h"
bdakin's avatar
bdakin committed
63 64
#include "ReplaceSelectionCommand.h"
#include "ResourceRequest.h"
65
#include "Settings.h"
darin's avatar
darin committed
66
#include "TextIterator.h"
67
#include "TypingCommand.h"
68
#include "UserTypingGestureIndicator.h"
tristan's avatar
qt:  
tristan committed
69
#include "WindowFeatures.h"
bdakin's avatar
bdakin committed
70
#include "markup.h"
71
#include <wtf/unicode/CharacterNames.h>
72 73 74 75
#include <wtf/unicode/Unicode.h>

using namespace WTF;
using namespace Unicode;
aroben's avatar
aroben committed
76 77 78

namespace WebCore {

79
ContextMenuController::ContextMenuController(Page& page, ContextMenuClient& client)
80 81
    : m_page(page)
    , m_client(client)
aroben's avatar
aroben committed
82 83 84 85 86
{
}

ContextMenuController::~ContextMenuController()
{
87
    m_client.contextMenuDestroyed();
88 89
}

bdakin's avatar
bdakin committed
90 91
void ContextMenuController::clearContextMenu()
{
92
    m_contextMenu.clear();
93 94 95
    if (m_menuProvider)
        m_menuProvider->contextMenuCleared();
    m_menuProvider = 0;
bdakin's avatar
bdakin committed
96 97
}

aroben's avatar
aroben committed
98 99
void ContextMenuController::handleContextMenuEvent(Event* event)
{
100
    m_contextMenu = createContextMenu(event);
101
    if (!m_contextMenu)
adele's avatar
adele committed
102
        return;
103 104 105

    populate();

106 107 108
    showContextMenu(event);
}

109 110 111 112 113
static PassOwnPtr<ContextMenuItem> separatorItem()
{
    return adoptPtr(new ContextMenuItem(SeparatorType, ContextMenuItemTagNoAction, String()));
}

114
void ContextMenuController::showContextMenu(Event* event, PassRefPtr<ContextMenuProvider> menuProvider)
115
{
116
    m_menuProvider = menuProvider;
117

118
    m_contextMenu = createContextMenu(event);
119 120 121 122
    if (!m_contextMenu) {
        clearContextMenu();
        return;
    }
123 124

    m_menuProvider->populateContextMenu(m_contextMenu.get());
125 126 127 128
    if (m_hitTestResult.isSelected()) {
        appendItem(*separatorItem(), m_contextMenu.get());
        populate();
    }
129 130 131
    showContextMenu(event);
}

132
PassOwnPtr<ContextMenu> ContextMenuController::createContextMenu(Event* event)
133
{
134 135
    ASSERT(event);
    
136
    if (!event->isMouseEvent())
137
        return nullptr;
138

aroben's avatar
aroben committed
139
    MouseEvent* mouseEvent = static_cast<MouseEvent*>(event);
140
    HitTestResult result(mouseEvent->absoluteLocation());
aroben's avatar
aroben committed
141

142
    if (Frame* frame = event->target()->toNode()->document().frame())
143
        result = frame->eventHandler().hitTestResultAtPoint(mouseEvent->absoluteLocation());
144

bdakin's avatar
bdakin committed
145
    if (!result.innerNonSharedNode())
146
        return nullptr;
147 148 149

    m_hitTestResult = result;

150
    return adoptPtr(new ContextMenu);
151
}
aroben's avatar
aroben committed
152

153 154
void ContextMenuController::showContextMenu(Event* event)
{
bolsinga@apple.com's avatar
bolsinga@apple.com committed
155
#if ENABLE(INSPECTOR)
156
    if (m_page.inspectorController().enabled())
157
        addInspectElementItem();
bolsinga@apple.com's avatar
bolsinga@apple.com committed
158
#endif
159 160

#if USE(CROSS_PLATFORM_CONTEXT_MENUS)
161
    m_contextMenu = m_client.customizeMenu(m_contextMenu.release());
162
#else
163
    PlatformMenuDescription customMenu = m_client.getCustomMenuFromDefaultItems(m_contextMenu.get());
bdakin's avatar
bdakin committed
164
    m_contextMenu->setPlatformDescription(customMenu);
165
#endif
aroben's avatar
aroben committed
166
    event->setDefaultHandled();
aroben's avatar
aroben committed
167 168
}

darin@apple.com's avatar
darin@apple.com committed
169
static void openNewWindow(const URL& urlToLoad, Frame* frame)
bdakin's avatar
bdakin committed
170
{
171 172 173 174 175 176 177 178 179 180 181 182
    Page* oldPage = frame->page();
    if (!oldPage)
        return;
    
    FrameLoadRequest request(frame->document()->securityOrigin(), ResourceRequest(urlToLoad, frame->loader().outgoingReferrer()));
    Page* newPage = oldPage;

    newPage = oldPage->chrome().createWindow(frame, request, WindowFeatures(), NavigationAction(request.resourceRequest()));
    if (!newPage)
        return;
    newPage->chrome().show();
    newPage->mainFrame().loader().loadFrameRequest(request, false, false, 0, 0, MaybeSendReferrer);
bdakin's avatar
bdakin committed
183 184
}

185 186 187 188
#if PLATFORM(GTK)
static void insertUnicodeCharacter(UChar character, Frame* frame)
{
    String text(&character, 1);
189
    if (!frame->editor().shouldInsertText(text, frame->selection().toNormalizedRange().get(), EditorInsertActionTyped))
190 191
        return;

192 193
    ASSERT(frame->document());
    TypingCommand::insertText(*frame->document(), text, 0, TypingCommand::TextCompositionNone);
194 195 196
}
#endif

aroben's avatar
aroben committed
197
void ContextMenuController::contextMenuItemSelected(ContextMenuItem* item)
bdakin's avatar
bdakin committed
198
{
199
    ASSERT(item->type() == ActionType || item->type() == CheckableActionType);
aroben's avatar
aroben committed
200 201

    if (item->action() >= ContextMenuItemBaseApplicationTag) {
202
        m_client.contextMenuItemSelected(item, m_contextMenu.get());
aroben's avatar
aroben committed
203 204 205
        return;
    }

206
    if (item->action() >= ContextMenuItemBaseCustomTag) {
207 208
        ASSERT(m_menuProvider);
        m_menuProvider->contextMenuItemSelected(item);
209 210 211
        return;
    }

212
    Frame* frame = m_hitTestResult.innerNonSharedNode()->document().frame();
bdakin's avatar
bdakin committed
213 214
    if (!frame)
        return;
215

aroben's avatar
aroben committed
216
    switch (item->action()) {
217
    case ContextMenuItemTagOpenLinkInNewWindow:
218
        openNewWindow(m_hitTestResult.absoluteLinkURL(), frame);
219 220
        break;
    case ContextMenuItemTagDownloadLinkToDisk:
221
        // FIXME: Some day we should be able to do this from within WebCore. (Bug 117709)
222
        m_client.downloadURL(m_hitTestResult.absoluteLinkURL());
223 224
        break;
    case ContextMenuItemTagCopyLinkToClipboard:
225
        frame->editor().copyURL(m_hitTestResult.absoluteLinkURL(), m_hitTestResult.textContent());
226 227
        break;
    case ContextMenuItemTagOpenImageInNewWindow:
228
        openNewWindow(m_hitTestResult.absoluteImageURL(), frame);
229 230
        break;
    case ContextMenuItemTagDownloadImageToDisk:
231
        // FIXME: Some day we should be able to do this from within WebCore. (Bug 117709)
232
        m_client.downloadURL(m_hitTestResult.absoluteImageURL());
233 234 235 236
        break;
    case ContextMenuItemTagCopyImageToClipboard:
        // FIXME: The Pasteboard class is not written yet
        // For now, call into the client. This is temporary!
237
        frame->editor().copyImage(m_hitTestResult);
238
        break;
239
#if PLATFORM(GTK) || PLATFORM(EFL)
240
    case ContextMenuItemTagCopyImageUrlToClipboard:
241
        frame->editor().copyURL(m_hitTestResult.absoluteImageURL(), m_hitTestResult.textContent());
242 243
        break;
#endif
244
    case ContextMenuItemTagOpenMediaInNewWindow:
245
        openNewWindow(m_hitTestResult.absoluteMediaURL(), frame);
246
        break;
247 248
    case ContextMenuItemTagDownloadMediaToDisk:
        // FIXME: Some day we should be able to do this from within WebCore. (Bug 117709)
249
        m_client.downloadURL(m_hitTestResult.absoluteMediaURL());
250
        break;
251
    case ContextMenuItemTagCopyMediaLinkToClipboard:
252
        frame->editor().copyURL(m_hitTestResult.absoluteMediaURL(), m_hitTestResult.textContent());
253 254
        break;
    case ContextMenuItemTagToggleMediaControls:
255
        m_hitTestResult.toggleMediaControlsDisplay();
256 257
        break;
    case ContextMenuItemTagToggleMediaLoop:
258
        m_hitTestResult.toggleMediaLoopPlayback();
259
        break;
260 261 262
    case ContextMenuItemTagToggleVideoFullscreen:
        m_hitTestResult.toggleMediaFullscreenState();
        break;
263
    case ContextMenuItemTagEnterVideoFullscreen:
264
        m_hitTestResult.enterFullscreenForVideo();
265 266
        break;
    case ContextMenuItemTagMediaPlayPause:
267
        m_hitTestResult.toggleMediaPlayState();
268 269
        break;
    case ContextMenuItemTagMediaMute:
270
        m_hitTestResult.toggleMediaMuteState();
271
        break;
272
    case ContextMenuItemTagOpenFrameInNewWindow: {
273
        DocumentLoader* loader = frame->loader().documentLoader();
274 275 276 277 278 279 280
        if (!loader->unreachableURL().isEmpty())
            openNewWindow(loader->unreachableURL(), frame);
        else
            openNewWindow(loader->url(), frame);
        break;
    }
    case ContextMenuItemTagCopy:
281
        frame->editor().copy();
282 283
        break;
    case ContextMenuItemTagGoBack:
284
        if (Page* page = frame->page())
285
            page->backForward().goBackOrForward(-1);
286 287
        break;
    case ContextMenuItemTagGoForward:
288
        if (Page* page = frame->page())
289
            page->backForward().goBackOrForward(1);
290 291
        break;
    case ContextMenuItemTagStop:
292
        frame->loader().stop();
293 294
        break;
    case ContextMenuItemTagReload:
295
        frame->loader().reload();
296 297
        break;
    case ContextMenuItemTagCut:
298
        frame->editor().command("Cut").execute();
299 300
        break;
    case ContextMenuItemTagPaste:
301
        frame->editor().command("Paste").execute();
302
        break;
303
#if PLATFORM(GTK)
304
    case ContextMenuItemTagDelete:
305
        frame->editor().performDelete();
306
        break;
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    case ContextMenuItemTagUnicodeInsertLRMMark:
        insertUnicodeCharacter(leftToRightMark, frame);
        break;
    case ContextMenuItemTagUnicodeInsertRLMMark:
        insertUnicodeCharacter(rightToLeftMark, frame);
        break;
    case ContextMenuItemTagUnicodeInsertLREMark:
        insertUnicodeCharacter(leftToRightEmbed, frame);
        break;
    case ContextMenuItemTagUnicodeInsertRLEMark:
        insertUnicodeCharacter(rightToLeftEmbed, frame);
        break;
    case ContextMenuItemTagUnicodeInsertLROMark:
        insertUnicodeCharacter(leftToRightOverride, frame);
        break;
    case ContextMenuItemTagUnicodeInsertRLOMark:
        insertUnicodeCharacter(rightToLeftOverride, frame);
        break;
    case ContextMenuItemTagUnicodeInsertPDFMark:
        insertUnicodeCharacter(popDirectionalFormatting, frame);
        break;
    case ContextMenuItemTagUnicodeInsertZWSMark:
        insertUnicodeCharacter(zeroWidthSpace, frame);
        break;
    case ContextMenuItemTagUnicodeInsertZWJMark:
        insertUnicodeCharacter(zeroWidthJoiner, frame);
        break;
    case ContextMenuItemTagUnicodeInsertZWNJMark:
        insertUnicodeCharacter(zeroWidthNonJoiner, frame);
        break;
337
#endif
338
#if PLATFORM(GTK) || PLATFORM(EFL)
339
    case ContextMenuItemTagSelectAll:
340
        frame->editor().command("SelectAll").execute();
341
        break;
342
#endif
343
    case ContextMenuItemTagSpellingGuess: {
344 345
        FrameSelection& frameSelection = frame->selection();
        if (frame->editor().shouldInsertText(item->title(), frameSelection.toNormalizedRange().get(), EditorInsertActionPasted)) {
346 347
            ReplaceSelectionCommand::CommandOptions replaceOptions = ReplaceSelectionCommand::MatchStyle | ReplaceSelectionCommand::PreventNesting;

348
            if (frame->editor().behavior().shouldAllowSpellingSuggestionsWithoutSelection()) {
349 350
                ASSERT(frameSelection.isCaretOrRange());
                VisibleSelection wordSelection(frameSelection.base());
351
                wordSelection.expandUsingGranularity(WordGranularity);
352
                frameSelection.setSelection(wordSelection);
353
            } else {
354
                ASSERT(frame->editor().selectedText().length());
355 356 357
                replaceOptions |= ReplaceSelectionCommand::SelectReplacement;
            }

358 359
            Document* document = frame->document();
            ASSERT(document);
360
            RefPtr<ReplaceSelectionCommand> command = ReplaceSelectionCommand::create(*document, createFragmentFromMarkup(*document, item->title(), ""), replaceOptions);
361
            applyCommand(command);
362
            frameSelection.revealSelection(ScrollAlignment::alignToEdgeIfNeeded);
bdakin's avatar
bdakin committed
363
        }
364
        break;
365
    }
366
    case ContextMenuItemTagIgnoreSpelling:
367
        frame->editor().ignoreSpelling();
368 369
        break;
    case ContextMenuItemTagLearnSpelling:
370
        frame->editor().learnSpelling();
371 372
        break;
    case ContextMenuItemTagSearchWeb:
373
        m_client.searchWithGoogle(frame);
374 375 376
        break;
    case ContextMenuItemTagLookUpInDictionary:
        // FIXME: Some day we may be able to do this from within WebCore.
377
        m_client.lookUpInDictionary(frame);
378 379
        break;
    case ContextMenuItemTagOpenLink:
380
        if (Frame* targetFrame = m_hitTestResult.targetFrame())
381
            targetFrame->loader().loadFrameRequest(FrameLoadRequest(frame->document()->securityOrigin(), ResourceRequest(m_hitTestResult.absoluteLinkURL(), frame->loader().outgoingReferrer())), false, false, 0, 0, MaybeSendReferrer);
382
        else
383
            openNewWindow(m_hitTestResult.absoluteLinkURL(), frame);
384
        break;
385
    case ContextMenuItemTagOpenLinkInThisWindow:
386
        frame->loader().loadFrameRequest(FrameLoadRequest(frame->document()->securityOrigin(), ResourceRequest(m_hitTestResult.absoluteLinkURL(), frame->loader().outgoingReferrer())), false, false, 0, 0, MaybeSendReferrer);
387
        break;
388
    case ContextMenuItemTagBold:
389
        frame->editor().command("ToggleBold").execute();
390 391
        break;
    case ContextMenuItemTagItalic:
392
        frame->editor().command("ToggleItalic").execute();
393 394
        break;
    case ContextMenuItemTagUnderline:
395
        frame->editor().toggleUnderline();
396 397 398 399 400 401
        break;
    case ContextMenuItemTagOutline:
        // We actually never enable this because CSS does not have a way to specify an outline font,
        // which may make this difficult to implement. Maybe a special case of text-shadow?
        break;
    case ContextMenuItemTagStartSpeaking: {
402
        RefPtr<Range> selectedRange = frame->selection().toNormalizedRange();
403
        if (!selectedRange || selectedRange->collapsed(IGNORE_EXCEPTION)) {
404 405 406
            Document& document = m_hitTestResult.innerNonSharedNode()->document();
            selectedRange = document.createRange();
            selectedRange->selectNode(document.documentElement(), IGNORE_EXCEPTION);
407
        }
408
        m_client.speak(plainText(selectedRange.get()));
409 410 411
        break;
    }
    case ContextMenuItemTagStopSpeaking:
412
        m_client.stopSpeaking();
413 414
        break;
    case ContextMenuItemTagDefaultDirection:
415
        frame->editor().setBaseWritingDirection(NaturalWritingDirection);
416 417
        break;
    case ContextMenuItemTagLeftToRight:
418
        frame->editor().setBaseWritingDirection(LeftToRightWritingDirection);
419 420
        break;
    case ContextMenuItemTagRightToLeft:
421
        frame->editor().setBaseWritingDirection(RightToLeftWritingDirection);
422 423
        break;
    case ContextMenuItemTagTextDirectionDefault:
424
        frame->editor().command("MakeTextWritingDirectionNatural").execute();
425 426
        break;
    case ContextMenuItemTagTextDirectionLeftToRight:
427
        frame->editor().command("MakeTextWritingDirectionLeftToRight").execute();
428 429
        break;
    case ContextMenuItemTagTextDirectionRightToLeft:
430
        frame->editor().command("MakeTextWritingDirectionRightToLeft").execute();
431
        break;
bdakin's avatar
bdakin committed
432
#if PLATFORM(MAC)
433
    case ContextMenuItemTagSearchInSpotlight:
434
        m_client.searchWithSpotlight();
435
        break;
436
#endif
437
    case ContextMenuItemTagShowSpellingPanel:
438
        frame->editor().showSpellingGuessPanel();
439 440
        break;
    case ContextMenuItemTagCheckSpelling:
441
        frame->editor().advanceToNextMisspelling();
442 443
        break;
    case ContextMenuItemTagCheckSpellingWhileTyping:
444
        frame->editor().toggleContinuousSpellChecking();
445 446
        break;
    case ContextMenuItemTagCheckGrammarWithSpelling:
447
        frame->editor().toggleGrammarChecking();
448
        break;
449
#if PLATFORM(MAC)
450
    case ContextMenuItemTagShowFonts:
451
        frame->editor().showFontPanel();
452 453
        break;
    case ContextMenuItemTagStyles:
454
        frame->editor().showStylesPanel();
455 456
        break;
    case ContextMenuItemTagShowColors:
457
        frame->editor().showColorPanel();
458
        break;
adele@apple.com's avatar
adele@apple.com committed
459
#endif
460
#if USE(APPKIT)
461
    case ContextMenuItemTagMakeUpperCase:
462
        frame->editor().uppercaseWord();
463 464
        break;
    case ContextMenuItemTagMakeLowerCase:
465
        frame->editor().lowercaseWord();
466 467
        break;
    case ContextMenuItemTagCapitalize:
468
        frame->editor().capitalizeWord();
469
        break;
470 471
#endif
#if PLATFORM(MAC)
472
    case ContextMenuItemTagChangeBack:
473
        frame->editor().changeBackToReplacedString(m_hitTestResult.replacedString());
474 475 476
        break;
#endif
#if USE(AUTOMATIC_TEXT_REPLACEMENT)
477
    case ContextMenuItemTagShowSubstitutions:
478
        frame->editor().showSubstitutionsPanel();
479 480
        break;
    case ContextMenuItemTagSmartCopyPaste:
481
        frame->editor().toggleSmartInsertDelete();
482 483
        break;
    case ContextMenuItemTagSmartQuotes:
484
        frame->editor().toggleAutomaticQuoteSubstitution();
485 486
        break;
    case ContextMenuItemTagSmartDashes:
487
        frame->editor().toggleAutomaticDashSubstitution();
488 489
        break;
    case ContextMenuItemTagSmartLinks:
490
        frame->editor().toggleAutomaticLinkDetection();
491 492
        break;
    case ContextMenuItemTagTextReplacement:
493
        frame->editor().toggleAutomaticTextReplacement();
494 495
        break;
    case ContextMenuItemTagCorrectSpellingAutomatically:
496
        frame->editor().toggleAutomaticSpellingCorrection();
497
        break;
bdakin's avatar
bdakin committed
498
#endif
bolsinga@apple.com's avatar
bolsinga@apple.com committed
499
#if ENABLE(INSPECTOR)
500 501
    case ContextMenuItemTagInspectElement:
        if (Page* page = frame->page())
502
            page->inspectorController().inspect(m_hitTestResult.innerNonSharedNode());
503
        break;
bolsinga@apple.com's avatar
bolsinga@apple.com committed
504
#endif
505
    case ContextMenuItemTagDictationAlternative:
506
        frame->editor().applyDictationAlternativelternative(item->title());
507
        break;
508 509
    default:
        break;
bdakin's avatar
bdakin committed
510
    }
aroben's avatar
aroben committed
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
void ContextMenuController::appendItem(ContextMenuItem& menuItem, ContextMenu* parentMenu)
{
    checkOrEnableIfNeeded(menuItem);
    if (parentMenu)
        parentMenu->appendItem(menuItem);
}

void ContextMenuController::createAndAppendFontSubMenu(ContextMenuItem& fontMenuItem)
{
    ContextMenu fontMenu;

#if PLATFORM(MAC)
    ContextMenuItem showFonts(ActionType, ContextMenuItemTagShowFonts, contextMenuItemTagShowFonts());
#endif
    ContextMenuItem bold(CheckableActionType, ContextMenuItemTagBold, contextMenuItemTagBold());
    ContextMenuItem italic(CheckableActionType, ContextMenuItemTagItalic, contextMenuItemTagItalic());
    ContextMenuItem underline(CheckableActionType, ContextMenuItemTagUnderline, contextMenuItemTagUnderline());
    ContextMenuItem outline(ActionType, ContextMenuItemTagOutline, contextMenuItemTagOutline());
#if PLATFORM(MAC)
    ContextMenuItem styles(ActionType, ContextMenuItemTagStyles, contextMenuItemTagStyles());
    ContextMenuItem showColors(ActionType, ContextMenuItemTagShowColors, contextMenuItemTagShowColors());
#endif

#if PLATFORM(MAC)
    appendItem(showFonts, &fontMenu);
#endif
    appendItem(bold, &fontMenu);
    appendItem(italic, &fontMenu);
    appendItem(underline, &fontMenu);
    appendItem(outline, &fontMenu);
#if PLATFORM(MAC)
    appendItem(styles, &fontMenu);
    appendItem(*separatorItem(), &fontMenu);
    appendItem(showColors, &fontMenu);
#endif

    fontMenuItem.setSubMenu(&fontMenu);
}


#if !PLATFORM(GTK)

void ContextMenuController::createAndAppendSpellingAndGrammarSubMenu(ContextMenuItem& spellingAndGrammarMenuItem)
{
    ContextMenu spellingAndGrammarMenu;

    ContextMenuItem showSpellingPanel(ActionType, ContextMenuItemTagShowSpellingPanel, 
        contextMenuItemTagShowSpellingPanel(true));
    ContextMenuItem checkSpelling(ActionType, ContextMenuItemTagCheckSpelling, 
        contextMenuItemTagCheckSpelling());
    ContextMenuItem checkAsYouType(CheckableActionType, ContextMenuItemTagCheckSpellingWhileTyping, 
        contextMenuItemTagCheckSpellingWhileTyping());
    ContextMenuItem grammarWithSpelling(CheckableActionType, ContextMenuItemTagCheckGrammarWithSpelling, 
        contextMenuItemTagCheckGrammarWithSpelling());
567
#if PLATFORM(MAC)
568 569 570 571 572 573
    ContextMenuItem correctSpelling(CheckableActionType, ContextMenuItemTagCorrectSpellingAutomatically, 
        contextMenuItemTagCorrectSpellingAutomatically());
#endif

    appendItem(showSpellingPanel, &spellingAndGrammarMenu);
    appendItem(checkSpelling, &spellingAndGrammarMenu);
574
#if PLATFORM(MAC)
575 576 577 578
    appendItem(*separatorItem(), &spellingAndGrammarMenu);
#endif
    appendItem(checkAsYouType, &spellingAndGrammarMenu);
    appendItem(grammarWithSpelling, &spellingAndGrammarMenu);
579
#if PLATFORM(MAC)
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
    appendItem(correctSpelling, &spellingAndGrammarMenu);
#endif

    spellingAndGrammarMenuItem.setSubMenu(&spellingAndGrammarMenu);
}

#endif // !PLATFORM(GTK)


#if PLATFORM(MAC)

void ContextMenuController::createAndAppendSpeechSubMenu(ContextMenuItem& speechMenuItem)
{
    ContextMenu speechMenu;

    ContextMenuItem start(ActionType, ContextMenuItemTagStartSpeaking, contextMenuItemTagStartSpeaking());
    ContextMenuItem stop(ActionType, ContextMenuItemTagStopSpeaking, contextMenuItemTagStopSpeaking());

    appendItem(start, &speechMenu);
    appendItem(stop, &speechMenu);

    speechMenuItem.setSubMenu(&speechMenu);
}

#endif
 
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
#if PLATFORM(GTK)

void ContextMenuController::createAndAppendUnicodeSubMenu(ContextMenuItem& unicodeMenuItem)
{
    ContextMenu unicodeMenu;

    ContextMenuItem leftToRightMarkMenuItem(ActionType, ContextMenuItemTagUnicodeInsertLRMMark, contextMenuItemTagUnicodeInsertLRMMark());
    ContextMenuItem rightToLeftMarkMenuItem(ActionType, ContextMenuItemTagUnicodeInsertRLMMark, contextMenuItemTagUnicodeInsertRLMMark());
    ContextMenuItem leftToRightEmbedMenuItem(ActionType, ContextMenuItemTagUnicodeInsertLREMark, contextMenuItemTagUnicodeInsertLREMark());
    ContextMenuItem rightToLeftEmbedMenuItem(ActionType, ContextMenuItemTagUnicodeInsertRLEMark, contextMenuItemTagUnicodeInsertRLEMark());
    ContextMenuItem leftToRightOverrideMenuItem(ActionType, ContextMenuItemTagUnicodeInsertLROMark, contextMenuItemTagUnicodeInsertLROMark());
    ContextMenuItem rightToLeftOverrideMenuItem(ActionType, ContextMenuItemTagUnicodeInsertRLOMark, contextMenuItemTagUnicodeInsertRLOMark());
    ContextMenuItem popDirectionalFormattingMenuItem(ActionType, ContextMenuItemTagUnicodeInsertPDFMark, contextMenuItemTagUnicodeInsertPDFMark());
    ContextMenuItem zeroWidthSpaceMenuItem(ActionType, ContextMenuItemTagUnicodeInsertZWSMark, contextMenuItemTagUnicodeInsertZWSMark());
    ContextMenuItem zeroWidthJoinerMenuItem(ActionType, ContextMenuItemTagUnicodeInsertZWJMark, contextMenuItemTagUnicodeInsertZWJMark());
    ContextMenuItem zeroWidthNonJoinerMenuItem(ActionType, ContextMenuItemTagUnicodeInsertZWNJMark, contextMenuItemTagUnicodeInsertZWNJMark());

    appendItem(leftToRightMarkMenuItem, &unicodeMenu);
    appendItem(rightToLeftMarkMenuItem, &unicodeMenu);
    appendItem(leftToRightEmbedMenuItem, &unicodeMenu);
    appendItem(rightToLeftEmbedMenuItem, &unicodeMenu);
    appendItem(leftToRightOverrideMenuItem, &unicodeMenu);
    appendItem(rightToLeftOverrideMenuItem, &unicodeMenu);
    appendItem(popDirectionalFormattingMenuItem, &unicodeMenu);
    appendItem(zeroWidthSpaceMenuItem, &unicodeMenu);
    appendItem(zeroWidthJoinerMenuItem, &unicodeMenu);
    appendItem(zeroWidthNonJoinerMenuItem, &unicodeMenu);

    unicodeMenuItem.setSubMenu(&unicodeMenu);
}

#else
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

void ContextMenuController::createAndAppendWritingDirectionSubMenu(ContextMenuItem& writingDirectionMenuItem)
{
    ContextMenu writingDirectionMenu;

    ContextMenuItem defaultItem(ActionType, ContextMenuItemTagDefaultDirection, 
        contextMenuItemTagDefaultDirection());
    ContextMenuItem ltr(CheckableActionType, ContextMenuItemTagLeftToRight, contextMenuItemTagLeftToRight());
    ContextMenuItem rtl(CheckableActionType, ContextMenuItemTagRightToLeft, contextMenuItemTagRightToLeft());

    appendItem(defaultItem, &writingDirectionMenu);
    appendItem(ltr, &writingDirectionMenu);
    appendItem(rtl, &writingDirectionMenu);

    writingDirectionMenuItem.setSubMenu(&writingDirectionMenu);
}

void ContextMenuController::createAndAppendTextDirectionSubMenu(ContextMenuItem& textDirectionMenuItem)
{
    ContextMenu textDirectionMenu;

    ContextMenuItem defaultItem(ActionType, ContextMenuItemTagTextDirectionDefault, contextMenuItemTagDefaultDirection());
    ContextMenuItem ltr(CheckableActionType, ContextMenuItemTagTextDirectionLeftToRight, contextMenuItemTagLeftToRight());
    ContextMenuItem rtl(CheckableActionType, ContextMenuItemTagTextDirectionRightToLeft, contextMenuItemTagRightToLeft());

    appendItem(defaultItem, &textDirectionMenu);
    appendItem(ltr, &textDirectionMenu);
    appendItem(rtl, &textDirectionMenu);

    textDirectionMenuItem.setSubMenu(&textDirectionMenu);
}

#endif

672
#if PLATFORM(MAC)
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715

void ContextMenuController::createAndAppendSubstitutionsSubMenu(ContextMenuItem& substitutionsMenuItem)
{
    ContextMenu substitutionsMenu;

    ContextMenuItem showSubstitutions(ActionType, ContextMenuItemTagShowSubstitutions, contextMenuItemTagShowSubstitutions(true));
    ContextMenuItem smartCopyPaste(CheckableActionType, ContextMenuItemTagSmartCopyPaste, contextMenuItemTagSmartCopyPaste());
    ContextMenuItem smartQuotes(CheckableActionType, ContextMenuItemTagSmartQuotes, contextMenuItemTagSmartQuotes());
    ContextMenuItem smartDashes(CheckableActionType, ContextMenuItemTagSmartDashes, contextMenuItemTagSmartDashes());
    ContextMenuItem smartLinks(CheckableActionType, ContextMenuItemTagSmartLinks, contextMenuItemTagSmartLinks());
    ContextMenuItem textReplacement(CheckableActionType, ContextMenuItemTagTextReplacement, contextMenuItemTagTextReplacement());

    appendItem(showSubstitutions, &substitutionsMenu);
    appendItem(*separatorItem(), &substitutionsMenu);
    appendItem(smartCopyPaste, &substitutionsMenu);
    appendItem(smartQuotes, &substitutionsMenu);
    appendItem(smartDashes, &substitutionsMenu);
    appendItem(smartLinks, &substitutionsMenu);
    appendItem(textReplacement, &substitutionsMenu);

    substitutionsMenuItem.setSubMenu(&substitutionsMenu);
}

void ContextMenuController::createAndAppendTransformationsSubMenu(ContextMenuItem& transformationsMenuItem)
{
    ContextMenu transformationsMenu;

    ContextMenuItem makeUpperCase(ActionType, ContextMenuItemTagMakeUpperCase, contextMenuItemTagMakeUpperCase());
    ContextMenuItem makeLowerCase(ActionType, ContextMenuItemTagMakeLowerCase, contextMenuItemTagMakeLowerCase());
    ContextMenuItem capitalize(ActionType, ContextMenuItemTagCapitalize, contextMenuItemTagCapitalize());

    appendItem(makeUpperCase, &transformationsMenu);
    appendItem(makeLowerCase, &transformationsMenu);
    appendItem(capitalize, &transformationsMenu);

    transformationsMenuItem.setSubMenu(&transformationsMenu);
}

#endif

static bool selectionContainsPossibleWord(Frame* frame)
{
    // Current algorithm: look for a character that's not just a separator.
716
    for (TextIterator it(frame->selection().toNormalizedRange().get()); !it.atEnd(); it.advance()) {
717
        int length = it.length();
718 719
        for (int i = 0; i < length; ++i) {
            if (!(U_GET_GC_MASK(it.characterAt(i)) & U_GC_Z_MASK))
720
                return true;
721
        }
722 723 724 725
    }
    return false;
}

726 727 728 729 730 731
#if PLATFORM(MAC)
#define SUPPORTS_TOGGLE_VIDEO_FULLSCREEN 1
#else
#define SUPPORTS_TOGGLE_VIDEO_FULLSCREEN 0
#endif

732 733 734 735 736 737
#if PLATFORM(MAC)
#define SUPPORTS_TOGGLE_SHOW_HIDE_MEDIA_CONTROLS 1
#else
#define SUPPORTS_TOGGLE_SHOW_HIDE_MEDIA_CONTROLS 0
#endif

738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
void ContextMenuController::populate()
{
    ContextMenuItem OpenLinkItem(ActionType, ContextMenuItemTagOpenLink, contextMenuItemTagOpenLink());
    ContextMenuItem OpenLinkInNewWindowItem(ActionType, ContextMenuItemTagOpenLinkInNewWindow, 
        contextMenuItemTagOpenLinkInNewWindow());
    ContextMenuItem DownloadFileItem(ActionType, ContextMenuItemTagDownloadLinkToDisk, 
        contextMenuItemTagDownloadLinkToDisk());
    ContextMenuItem CopyLinkItem(ActionType, ContextMenuItemTagCopyLinkToClipboard, 
        contextMenuItemTagCopyLinkToClipboard());
    ContextMenuItem OpenImageInNewWindowItem(ActionType, ContextMenuItemTagOpenImageInNewWindow, 
        contextMenuItemTagOpenImageInNewWindow());
    ContextMenuItem DownloadImageItem(ActionType, ContextMenuItemTagDownloadImageToDisk, 
        contextMenuItemTagDownloadImageToDisk());
    ContextMenuItem CopyImageItem(ActionType, ContextMenuItemTagCopyImageToClipboard, 
        contextMenuItemTagCopyImageToClipboard());
753
#if PLATFORM(GTK) || PLATFORM(EFL)
754 755 756
    ContextMenuItem CopyImageUrlItem(ActionType, ContextMenuItemTagCopyImageUrlToClipboard, 
        contextMenuItemTagCopyImageUrlToClipboard());
#endif
757
    ContextMenuItem OpenMediaInNewWindowItem(ActionType, ContextMenuItemTagOpenMediaInNewWindow, String());
758 759
    ContextMenuItem DownloadMediaItem(ActionType, ContextMenuItemTagDownloadMediaToDisk, String());
    ContextMenuItem CopyMediaLinkItem(ActionType, ContextMenuItemTagCopyMediaLinkToClipboard, String());
760 761 762 763
    ContextMenuItem MediaPlayPause(ActionType, ContextMenuItemTagMediaPlayPause, 
        contextMenuItemTagMediaPlay());
    ContextMenuItem MediaMute(ActionType, ContextMenuItemTagMediaMute, 
        contextMenuItemTagMediaMute());
764 765 766 767
#if SUPPORTS_TOGGLE_SHOW_HIDE_MEDIA_CONTROLS
    ContextMenuItem ToggleMediaControls(ActionType, ContextMenuItemTagToggleMediaControls,
        contextMenuItemTagHideMediaControls());
#else
768 769
    ContextMenuItem ToggleMediaControls(CheckableActionType, ContextMenuItemTagToggleMediaControls, 
        contextMenuItemTagToggleMediaControls());
770
#endif
771 772
    ContextMenuItem ToggleMediaLoop(CheckableActionType, ContextMenuItemTagToggleMediaLoop, 
        contextMenuItemTagToggleMediaLoop());
773 774 775
    ContextMenuItem EnterVideoFullscreen(ActionType, ContextMenuItemTagEnterVideoFullscreen,
        contextMenuItemTagEnterVideoFullscreen());
    ContextMenuItem ToggleVideoFullscreen(ActionType, ContextMenuItemTagToggleVideoFullscreen,
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
        contextMenuItemTagEnterVideoFullscreen());
#if PLATFORM(MAC)
    ContextMenuItem SearchSpotlightItem(ActionType, ContextMenuItemTagSearchInSpotlight, 
        contextMenuItemTagSearchInSpotlight());
#endif
#if !PLATFORM(GTK)
    ContextMenuItem SearchWebItem(ActionType, ContextMenuItemTagSearchWeb, contextMenuItemTagSearchWeb());
#endif
    ContextMenuItem CopyItem(ActionType, ContextMenuItemTagCopy, contextMenuItemTagCopy());
    ContextMenuItem BackItem(ActionType, ContextMenuItemTagGoBack, contextMenuItemTagGoBack());
    ContextMenuItem ForwardItem(ActionType, ContextMenuItemTagGoForward,  contextMenuItemTagGoForward());
    ContextMenuItem StopItem(ActionType, ContextMenuItemTagStop, contextMenuItemTagStop());
    ContextMenuItem ReloadItem(ActionType, ContextMenuItemTagReload, contextMenuItemTagReload());
    ContextMenuItem OpenFrameItem(ActionType, ContextMenuItemTagOpenFrameInNewWindow, 
        contextMenuItemTagOpenFrameInNewWindow());
    ContextMenuItem NoGuessesItem(ActionType, ContextMenuItemTagNoGuessesFound, 
        contextMenuItemTagNoGuessesFound());
    ContextMenuItem IgnoreSpellingItem(ActionType, ContextMenuItemTagIgnoreSpelling, 
        contextMenuItemTagIgnoreSpelling());
    ContextMenuItem LearnSpellingItem(ActionType, ContextMenuItemTagLearnSpelling, 
        contextMenuItemTagLearnSpelling());
    ContextMenuItem IgnoreGrammarItem(ActionType, ContextMenuItemTagIgnoreGrammar, 
        contextMenuItemTagIgnoreGrammar());
    ContextMenuItem CutItem(ActionType, ContextMenuItemTagCut, contextMenuItemTagCut());
    ContextMenuItem PasteItem(ActionType, ContextMenuItemTagPaste, contextMenuItemTagPaste());
#if PLATFORM(GTK)
    ContextMenuItem DeleteItem(ActionType, ContextMenuItemTagDelete, contextMenuItemTagDelete());
803
#endif
804
#if PLATFORM(GTK) || PLATFORM(EFL)
805 806 807 808 809 810 811
    ContextMenuItem SelectAllItem(ActionType, ContextMenuItemTagSelectAll, contextMenuItemTagSelectAll());
#endif

    Node* node = m_hitTestResult.innerNonSharedNode();
    if (!node)
        return;
#if PLATFORM(GTK)
812
    if (!m_hitTestResult.isContentEditable() && (node->isElementNode() && toElement(node)->isFormControlElement()))
813 814
        return;
#endif
815
    Frame* frame = node->document().frame();
816 817 818 819
    if (!frame)
        return;

    if (!m_hitTestResult.isContentEditable()) {
820
        FrameLoader& loader = frame->loader();
darin@apple.com's avatar
darin@apple.com committed
821
        URL linkURL = m_hitTestResult.absoluteLinkURL();
822
        if (!linkURL.isEmpty()) {
823
            if (loader.client().canHandleRequest(ResourceRequest(linkURL))) {
824 825 826 827 828 829 830
                appendItem(OpenLinkItem, m_contextMenu.get());
                appendItem(OpenLinkInNewWindowItem, m_contextMenu.get());
                appendItem(DownloadFileItem, m_contextMenu.get());
            }
            appendItem(CopyLinkItem, m_contextMenu.get());
        }

darin@apple.com's avatar
darin@apple.com committed
831
        URL imageURL = m_hitTestResult.absoluteImageURL();
832 833 834 835 836 837 838 839
        if (!imageURL.isEmpty()) {
            if (!linkURL.isEmpty())
                appendItem(*separatorItem(), m_contextMenu.get());

            appendItem(OpenImageInNewWindowItem, m_contextMenu.get());
            appendItem(DownloadImageItem, m_contextMenu.get());
            if (imageURL.isLocalFile() || m_hitTestResult.image())
                appendItem(CopyImageItem, m_contextMenu.get());
840
#if PLATFORM(GTK) || PLATFORM(EFL)
841 842
            appendItem(CopyImageUrlItem, m_contextMenu.get());
#endif
843 844
        }

darin@apple.com's avatar
darin@apple.com committed
845
        URL mediaURL = m_hitTestResult.absoluteMediaURL();
846 847 848 849 850 851 852 853
        if (!mediaURL.isEmpty()) {
            if (!linkURL.isEmpty() || !imageURL.isEmpty())
                appendItem(*separatorItem(), m_contextMenu.get());

            appendItem(MediaPlayPause, m_contextMenu.get());
            appendItem(MediaMute, m_contextMenu.get());
            appendItem(ToggleMediaControls, m_contextMenu.get());
            appendItem(ToggleMediaLoop, m_contextMenu.get());
854 855 856
#if SUPPORTS_TOGGLE_VIDEO_FULLSCREEN
            appendItem(ToggleVideoFullscreen, m_contextMenu.get());
#else
857
            appendItem(EnterVideoFullscreen, m_contextMenu.get());
858
#endif
859 860 861
            appendItem(*separatorItem(), m_contextMenu.get());
            appendItem(CopyMediaLinkItem, m_contextMenu.get());
            appendItem(OpenMediaInNewWindowItem, m_contextMenu.get());
862
            if (loader.client().canHandleRequest(ResourceRequest(mediaURL)))
863
                appendItem(DownloadMediaItem, m_contextMenu.get());
864 865 866 867 868 869
        }

        if (imageURL.isEmpty() && linkURL.isEmpty() && mediaURL.isEmpty()) {
            if (m_hitTestResult.isSelected()) {
                if (selectionContainsPossibleWord(frame)) {
#if PLATFORM(MAC)
870
                    String selectedString = frame->displayStringModifiedByEncoding(frame->editor().selectedText());
871 872 873 874 875
                    ContextMenuItem LookUpInDictionaryItem(ActionType, ContextMenuItemTagLookUpInDictionary, contextMenuItemTagLookUpInDictionary(selectedString));

                    appendItem(LookUpInDictionaryItem, m_contextMenu.get());
#endif

876 877 878 879 880
#if !PLATFORM(GTK)
                    appendItem(SearchWebItem, m_contextMenu.get());
                    appendItem(*separatorItem(), m_contextMenu.get());
#endif
                }
881

882 883 884
                appendItem(CopyItem, m_contextMenu.get());
#if PLATFORM(MAC)
                appendItem(*separatorItem(), m_contextMenu.get());
885

886 887 888 889 890 891
                ContextMenuItem SpeechMenuItem(SubmenuType, ContextMenuItemTagSpeechMenu, contextMenuItemTagSpeechMenu());
                createAndAppendSpeechSubMenu(SpeechMenuItem);
                appendItem(SpeechMenuItem, m_contextMenu.get());
#endif                
            } else {
#if ENABLE(INSPECTOR)
892
                if (!(frame->page() && (frame->page()->inspectorController().hasInspectorFrontendClient() || frame->page()->inspectorController().hasRemoteFrontend()))) {
893
#endif
894

895 896
                // In GTK+ unavailable items are not hidden but insensitive.
#if PLATFORM(GTK)
897 898 899 900 901
                appendItem(BackItem, m_contextMenu.get());
                appendItem(ForwardItem, m_contextMenu.get());
                appendItem(StopItem, m_contextMenu.get());
                appendItem(ReloadItem, m_contextMenu.get());
#else
902
                if (frame->page() && frame->page()->backForward().canGoBackOrForward(-1))
903 904
                    appendItem(BackItem, m_contextMenu.get());

905
                if (frame->page() && frame->page()->backForward().canGoBackOrForward(1))
906 907 908 909
                    appendItem(ForwardItem, m_contextMenu.get());

                // use isLoadingInAPISense rather than isLoading because Stop/Reload are
                // intended to match WebKit's API, not WebCore's internal notion of loading status
910
                if (loader.documentLoader()->isLoadingInAPISense())
911 912 913
                    appendItem(StopItem, m_contextMenu.get());
                else
                    appendItem(ReloadItem, m_contextMenu.get());
914
#endif
915 916 917 918
#if ENABLE(INSPECTOR)
                }
#endif

919
                if (frame->page() && !frame->isMainFrame())
920 921 922 923
                    appendItem(OpenFrameItem, m_contextMenu.get());
            }
        }
    } else { // Make an editing context menu
924
        bool inPasswordField = frame->selection().isInPasswordField();
925 926
        if (!inPasswordField) {
            bool haveContextMenuItemsForMisspellingOrGrammer = false;
927
            bool spellCheckingEnabled = frame->editor().isSpellCheckingEnabledFor(node);
928 929 930 931 932
            if (spellCheckingEnabled) {
                // Consider adding spelling-related or grammar-related context menu items (never both, since a single selected range
                // is never considered a misspelling and bad grammar at the same time)
                bool misspelling;
                bool badGrammar;
933
                Vector<String> guesses = frame->editor().guessesForMisspelledOrUngrammatical(misspelling, badGrammar);
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
                if (misspelling || badGrammar) {
                    size_t size = guesses.size();
                    if (!size) {
                        // If there's bad grammar but no suggestions (e.g., repeated word), just leave off the suggestions
                        // list and trailing separator rather than adding a "No Guesses Found" item (matches AppKit)
                        if (misspelling) {
                            appendItem(NoGuessesItem, m_contextMenu.get());
                            appendItem(*separatorItem(), m_contextMenu.get());
                        }
                    } else {
                        for (unsigned i = 0; i < size; i++) {
                            const String &guess = guesses[i];
                            if (!guess.isEmpty()) {
                                ContextMenuItem item(ActionType, ContextMenuItemTagSpellingGuess, guess);
                                appendItem(item, m_contextMenu.get());
                            }
                        }
951 952
                        appendItem(*separatorItem(), m_contextMenu.get());
                    }
953 954 955
                    if (misspelling) {
                        appendItem(IgnoreSpellingItem, m_contextMenu.get());
                        appendItem(LearnSpellingItem, m_contextMenu.get());
956
                    } else
957 958 959
                        appendItem(IgnoreGrammarItem, m_contextMenu.get());
                    appendItem(*separatorItem(), m_contextMenu.get());
                    haveContextMenuItemsForMisspellingOrGrammer = true;
960
#if PLATFORM(MAC)
961
                } else {
962 963 964 965 966 967 968
                    // If the string was autocorrected, generate a contextual menu item allowing it to be changed back.
                    String replacedString = m_hitTestResult.replacedString();
                    if (!replacedString.isEmpty()) {
                        ContextMenuItem item(ActionType, ContextMenuItemTagChangeBack, contextMenuItemTagChangeBack(replacedString));
                        appendItem(item, m_contextMenu.get());
                        appendItem(*separatorItem(), m_contextMenu.get());
                        haveContextMenuItemsForMisspellingOrGrammer = true;
969
                    }
970
#endif
971
                }
972 973 974 975 976 977 978 979 980 981
            }

            if (!haveContextMenuItemsForMisspellingOrGrammer) {
                // Spelling and grammar checking is mutually exclusive with dictation alternatives.
                Vector<String> dictationAlternatives = m_hitTestResult.dictationAlternatives();
                if (!dictationAlternatives.isEmpty()) {
                    for (size_t i = 0; i < dictationAlternatives.size(); ++i) {
                        ContextMenuItem item(ActionType, ContextMenuItemTagDictationAlternative, dictationAlternatives[i]);
                        appendItem(item, m_contextMenu.get());
                    }
982 983 984 985 986
                    appendItem(*separatorItem(), m_contextMenu.get());
                }
            }
        }

987
        FrameLoader& loader = frame->loader();
darin@apple.com's avatar
darin@apple.com committed
988
        URL linkURL = m_hitTestResult.absoluteLinkURL();
989
        if (!linkURL.isEmpty()) {
990
            if (loader.client().canHandleRequest(ResourceRequest(linkURL))) {
991 992 993 994 995 996 997 998 999 1000
                appendItem(OpenLinkItem, m_contextMenu.get());
                appendItem(OpenLinkInNewWindowItem, m_contextMenu.get());
                appendItem(DownloadFileItem, m_contextMenu.get());
            }
            appendItem(CopyLinkItem, m_contextMenu.get());
            appendItem(*separatorItem(), m_contextMenu.get());
        }

        if (m_hitTestResult.isSelected() && !inPasswordField && selectionContainsPossibleWord(frame)) {
#if PLATFORM(MAC)
1001
            String selectedString = frame->displayStringModifiedByEncoding(frame->editor().selectedText());
1002 1003 1004 1005 1006
            ContextMenuItem LookUpInDictionaryItem(ActionType, ContextMenuItemTagLookUpInDictionary, contextMenuItemTagLookUpInDictionary(selectedString));

            appendItem(LookUpInDictionaryItem, m_contextMenu.get());
#endif

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
#if !PLATFORM(GTK)
            appendItem(SearchWebItem, m_contextMenu.get());
            appendItem(*separatorItem(), m_contextMenu.get());
#endif
        }

        appendItem(CutItem, m_contextMenu.get());
        appendItem(CopyItem, m_contextMenu.get());
        appendItem(PasteItem, m_contextMenu.get());
#if PLATFORM(GTK)
        appendItem(DeleteItem, m_contextMenu.get());
        appendItem(*separatorItem(), m_contextMenu.get());
1019
#endif
1020
#if PLATFORM(GTK) || PLATFORM(EFL)
1021