1 2 3 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 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 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
/*
* Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
*
* 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"
#if ENABLE(CONTEXT_MENUS)
#include "Chrome.h"
#include "ContextMenu.h"
#include "ContextMenuClient.h"
#include "ContextMenuProvider.h"
#include "Document.h"
#include "DocumentFragment.h"
#include "DocumentLoader.h"
#include "Editor.h"
#include "EditorClient.h"
#include "Event.h"
#include "EventHandler.h"
#include "EventNames.h"
#include "FormState.h"
#include "Frame.h"
#include "FrameLoadRequest.h"
#include "FrameLoader.h"
#include "HTMLFormElement.h"
#include "HitTestRequest.h"
#include "HitTestResult.h"
#include "InspectorController.h"
#include "MouseEvent.h"
#include "Node.h"
#include "Page.h"
#include "RenderLayer.h"
#include "RenderObject.h"
#include "ReplaceSelectionCommand.h"
#include "ResourceRequest.h"
#include "SelectionController.h"
#include "Settings.h"
#include "TextIterator.h"
#include "WindowFeatures.h"
#include "markup.h"
namespace WebCore {
ContextMenuController::ContextMenuController(Page* page, ContextMenuClient* client)
: m_page(page)
, m_client(client)
, m_contextMenu(0)
{
ASSERT_ARG(page, page);
ASSERT_ARG(client, client);
}
ContextMenuController::~ContextMenuController()
{
m_client->contextMenuDestroyed();
}
void ContextMenuController::clearContextMenu()
{
m_contextMenu.set(0);
if (m_menuProvider)
m_menuProvider->contextMenuCleared();
m_menuProvider = 0;
}
void ContextMenuController::handleContextMenuEvent(Event* event)
{
m_contextMenu.set(createContextMenu(event));
if (!m_contextMenu)
return;
m_contextMenu->populate();
showContextMenu(event);
}
void ContextMenuController::showContextMenu(Event* event, PassRefPtr<ContextMenuProvider> menuProvider)
{
m_menuProvider = menuProvider;
m_contextMenu.set(createContextMenu(event));
if (!m_contextMenu) {
clearContextMenu();
return;
}
m_menuProvider->populateContextMenu(m_contextMenu.get());
showContextMenu(event);
}
ContextMenu* ContextMenuController::createContextMenu(Event* event)
{
if (!event->isMouseEvent())
return 0;
MouseEvent* mouseEvent = static_cast<MouseEvent*>(event);
HitTestResult result(mouseEvent->absoluteLocation());
if (Frame* frame = event->target()->toNode()->document()->frame())
result = frame->eventHandler()->hitTestResultAtPoint(mouseEvent->absoluteLocation(), false);
if (!result.innerNonSharedNode())
return 0;
return new ContextMenu(result);
}
void ContextMenuController::showContextMenu(Event* event)
{
#if ENABLE(INSPECTOR)
if (m_page->inspectorController()->enabled())
m_contextMenu->addInspectElementItem();
#endif
PlatformMenuDescription customMenu = m_client->getCustomMenuFromDefaultItems(m_contextMenu.get());
m_contextMenu->setPlatformDescription(customMenu);
event->setDefaultHandled();
}
static void openNewWindow(const KURL& urlToLoad, Frame* frame)
{
if (Page* oldPage = frame->page()) {
WindowFeatures features;
if (Page* newPage = oldPage->chrome()->createWindow(frame, FrameLoadRequest(ResourceRequest(urlToLoad, frame->loader()->outgoingReferrer())), features))
newPage->chrome()->show();
}
}
void ContextMenuController::contextMenuItemSelected(ContextMenuItem* item)
{
ASSERT(item->type() == ActionType || item->type() == CheckableActionType);
if (item->action() >= ContextMenuItemBaseApplicationTag) {
m_client->contextMenuItemSelected(item, m_contextMenu.get());
return;
}
if (item->action() >= ContextMenuItemBaseCustomTag) {
ASSERT(m_menuProvider);
m_menuProvider->contextMenuItemSelected(item);
return;
}
HitTestResult result = m_contextMenu->hitTestResult();
Frame* frame = result.innerNonSharedNode()->document()->frame();
if (!frame)
return;
switch (item->action()) {
case ContextMenuItemTagOpenLinkInNewWindow:
openNewWindow(result.absoluteLinkURL(), frame);
break;
case ContextMenuItemTagDownloadLinkToDisk:
// FIXME: Some day we should be able to do this from within WebCore.
m_client->downloadURL(result.absoluteLinkURL());
break;
case ContextMenuItemTagCopyLinkToClipboard:
frame->editor()->copyURL(result.absoluteLinkURL(), result.textContent());
break;
case ContextMenuItemTagOpenImageInNewWindow:
openNewWindow(result.absoluteImageURL(), frame);
break;
case ContextMenuItemTagDownloadImageToDisk:
// FIXME: Some day we should be able to do this from within WebCore.
m_client->downloadURL(result.absoluteImageURL());
break;
case ContextMenuItemTagCopyImageToClipboard:
// FIXME: The Pasteboard class is not written yet
// For now, call into the client. This is temporary!
frame->editor()->copyImage(result);
break;
case ContextMenuItemTagOpenFrameInNewWindow: {
DocumentLoader* loader = frame->loader()->documentLoader();
if (!loader->unreachableURL().isEmpty())
openNewWindow(loader->unreachableURL(), frame);
else
openNewWindow(loader->url(), frame);
break;
}
case ContextMenuItemTagCopy:
frame->editor()->copy();
break;
case ContextMenuItemTagGoBack:
if (Page* page = frame->page())
page->goBackOrForward(-1);
break;
case ContextMenuItemTagGoForward:
if (Page* page = frame->page())
page->goBackOrForward(1);
break;
case ContextMenuItemTagStop:
frame->loader()->stop();
break;
case ContextMenuItemTagReload:
frame->loader()->reload();
break;
case ContextMenuItemTagCut:
frame->editor()->cut();
break;
case ContextMenuItemTagPaste:
frame->editor()->paste();
break;
#if PLATFORM(GTK)
case ContextMenuItemTagDelete:
frame->editor()->performDelete();
break;
case ContextMenuItemTagSelectAll:
frame->editor()->command("SelectAll").execute();
break;
#endif
case ContextMenuItemTagSpellingGuess:
ASSERT(frame->selectedText().length());
if (frame->editor()->shouldInsertText(item->title(), frame->selection()->toNormalizedRange().get(), EditorInsertActionPasted)) {
Document* document = frame->document();
RefPtr<ReplaceSelectionCommand> command = ReplaceSelectionCommand::create(document, createFragmentFromMarkup(document, item->title(), ""), true, false, true);
applyCommand(command);
frame->revealSelection(ScrollAlignment::alignToEdgeIfNeeded);
}
break;
case ContextMenuItemTagIgnoreSpelling:
frame->editor()->ignoreSpelling();
break;
case ContextMenuItemTagLearnSpelling:
frame->editor()->learnSpelling();
break;
case ContextMenuItemTagSearchWeb:
m_client->searchWithGoogle(frame);
break;
case ContextMenuItemTagLookUpInDictionary:
// FIXME: Some day we may be able to do this from within WebCore.
m_client->lookUpInDictionary(frame);
break;
case ContextMenuItemTagOpenLink:
if (Frame* targetFrame = result.targetFrame())
targetFrame->loader()->loadFrameRequest(FrameLoadRequest(ResourceRequest(result.absoluteLinkURL(), frame->loader()->outgoingReferrer())), false, false, 0, 0, SendReferrer);
else
openNewWindow(result.absoluteLinkURL(), frame);
break;
case ContextMenuItemTagBold:
frame->editor()->command("ToggleBold").execute();
break;
case ContextMenuItemTagItalic:
frame->editor()->command("ToggleItalic").execute();
break;
case ContextMenuItemTagUnderline:
frame->editor()->toggleUnderline();
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: {
ExceptionCode ec;
RefPtr<Range> selectedRange = frame->selection()->toNormalizedRange();
if (!selectedRange || selectedRange->collapsed(ec)) {
Document* document = result.innerNonSharedNode()->document();
selectedRange = document->createRange();
selectedRange->selectNode(document->documentElement(), ec);
}
m_client->speak(plainText(selectedRange.get()));
break;
}
case ContextMenuItemTagStopSpeaking:
m_client->stopSpeaking();
break;
case ContextMenuItemTagDefaultDirection:
frame->editor()->setBaseWritingDirection(NaturalWritingDirection);
break;
case ContextMenuItemTagLeftToRight:
frame->editor()->setBaseWritingDirection(LeftToRightWritingDirection);
break;
case ContextMenuItemTagRightToLeft:
frame->editor()->setBaseWritingDirection(RightToLeftWritingDirection);
break;
case ContextMenuItemTagTextDirectionDefault:
frame->editor()->command("MakeTextWritingDirectionNatural").execute();
break;
case ContextMenuItemTagTextDirectionLeftToRight:
frame->editor()->command("MakeTextWritingDirectionLeftToRight").execute();
break;
case ContextMenuItemTagTextDirectionRightToLeft:
frame->editor()->command("MakeTextWritingDirectionRightToLeft").execute();
break;
#if PLATFORM(MAC)
case ContextMenuItemTagSearchInSpotlight:
m_client->searchWithSpotlight();
break;
#endif
case ContextMenuItemTagShowSpellingPanel:
frame->editor()->showSpellingGuessPanel();
break;
case ContextMenuItemTagCheckSpelling:
frame->editor()->advanceToNextMisspelling();
break;
case ContextMenuItemTagCheckSpellingWhileTyping:
frame->editor()->toggleContinuousSpellChecking();
break;
#ifndef BUILDING_ON_TIGER
case ContextMenuItemTagCheckGrammarWithSpelling:
frame->editor()->toggleGrammarChecking();
break;
#endif
#if PLATFORM(MAC)
case ContextMenuItemTagShowFonts:
frame->editor()->showFontPanel();
break;
case ContextMenuItemTagStyles:
frame->editor()->showStylesPanel();
break;
case ContextMenuItemTagShowColors:
frame->editor()->showColorPanel();
break;
#endif
#if PLATFORM(MAC) && !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD)
case ContextMenuItemTagMakeUpperCase:
frame->editor()->uppercaseWord();
break;
case ContextMenuItemTagMakeLowerCase:
frame->editor()->lowercaseWord();
break;
case ContextMenuItemTagCapitalize:
frame->editor()->capitalizeWord();
break;
case ContextMenuItemTagShowSubstitutions:
frame->editor()->showSubstitutionsPanel();
break;
case ContextMenuItemTagSmartCopyPaste:
frame->editor()->toggleSmartInsertDelete();
break;
case ContextMenuItemTagSmartQuotes:
frame->editor()->toggleAutomaticQuoteSubstitution();
break;
case ContextMenuItemTagSmartDashes:
frame->editor()->toggleAutomaticDashSubstitution();
break;
case ContextMenuItemTagSmartLinks:
frame->editor()->toggleAutomaticLinkDetection();
break;
case ContextMenuItemTagTextReplacement:
frame->editor()->toggleAutomaticTextReplacement();
break;
case ContextMenuItemTagCorrectSpellingAutomatically:
frame->editor()->toggleAutomaticSpellingCorrection();
break;
case ContextMenuItemTagChangeBack:
frame->editor()->changeBackToReplacedString(result.replacedString());
break;
#endif
#if ENABLE(INSPECTOR)
case ContextMenuItemTagInspectElement:
if (Page* page = frame->page())
page->inspectorController()->inspect(result.innerNonSharedNode());
break;
#endif
default:
break;
}
}
} // namespace WebCore
#endif // ENABLE(CONTEXT_MENUS)
|