2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "ContextMenuController.h"
30 #include "ContextMenu.h"
31 #include "ContextMenuClient.h"
33 #include "DocumentFragment.h"
34 #include "DocumentLoader.h"
36 #include "EditorClient.h"
38 #include "EventHandler.h"
39 #include "EventNames.h"
41 #include "FrameLoader.h"
42 #include "FrameLoadRequest.h"
43 #include "HitTestRequest.h"
44 #include "HitTestResult.h"
45 #include "InspectorController.h"
46 #include "MouseEvent.h"
49 #include "RenderLayer.h"
50 #include "RenderObject.h"
51 #include "ReplaceSelectionCommand.h"
52 #include "ResourceRequest.h"
53 #include "SelectionController.h"
55 #include "TextIterator.h"
56 #include "WindowFeatures.h"
61 ContextMenuController::ContextMenuController(Page* page, ContextMenuClient* client)
66 ASSERT_ARG(page, page);
67 ASSERT_ARG(client, client);
70 ContextMenuController::~ContextMenuController()
72 m_client->contextMenuDestroyed();
75 void ContextMenuController::clearContextMenu()
80 void ContextMenuController::handleContextMenuEvent(Event* event)
82 ASSERT(event->type() == eventNames().contextmenuEvent);
83 if (!event->isMouseEvent())
85 MouseEvent* mouseEvent = static_cast<MouseEvent*>(event);
86 IntPoint point = IntPoint(mouseEvent->pageX(), mouseEvent->pageY());
87 HitTestResult result(point);
89 if (Frame* frame = event->target()->toNode()->document()->frame()) {
90 float zoomFactor = frame->pageZoomFactor();
91 point.setX(static_cast<int>(point.x() * zoomFactor));
92 point.setY(static_cast<int>(point.y() * zoomFactor));
93 result = frame->eventHandler()->hitTestResultAtPoint(point, false);
96 if (!result.innerNonSharedNode())
99 m_contextMenu.set(new ContextMenu(result));
100 m_contextMenu->populate();
101 if (m_page->inspectorController()->enabled())
102 m_contextMenu->addInspectElementItem();
104 PlatformMenuDescription customMenu = m_client->getCustomMenuFromDefaultItems(m_contextMenu.get());
105 m_contextMenu->setPlatformDescription(customMenu);
107 event->setDefaultHandled();
110 static void openNewWindow(const KURL& urlToLoad, Frame* frame)
112 if (Page* oldPage = frame->page()) {
113 WindowFeatures features;
114 if (Page* newPage = oldPage->chrome()->createWindow(frame,
115 FrameLoadRequest(ResourceRequest(urlToLoad, frame->loader()->outgoingReferrer())), features))
116 newPage->chrome()->show();
120 void ContextMenuController::contextMenuItemSelected(ContextMenuItem* item)
122 ASSERT(item->type() == ActionType || item->type() == CheckableActionType);
124 if (item->action() >= ContextMenuItemBaseApplicationTag) {
125 m_client->contextMenuItemSelected(item, m_contextMenu.get());
129 HitTestResult result = m_contextMenu->hitTestResult();
130 Frame* frame = result.innerNonSharedNode()->document()->frame();
134 switch (item->action()) {
135 case ContextMenuItemTagOpenLinkInNewWindow:
136 openNewWindow(result.absoluteLinkURL(), frame);
138 case ContextMenuItemTagDownloadLinkToDisk:
139 // FIXME: Some day we should be able to do this from within WebCore.
140 m_client->downloadURL(result.absoluteLinkURL());
142 case ContextMenuItemTagCopyLinkToClipboard:
143 frame->editor()->copyURL(result.absoluteLinkURL(), result.textContent());
145 case ContextMenuItemTagOpenImageInNewWindow:
146 openNewWindow(result.absoluteImageURL(), frame);
148 case ContextMenuItemTagDownloadImageToDisk:
149 // FIXME: Some day we should be able to do this from within WebCore.
150 m_client->downloadURL(result.absoluteImageURL());
152 case ContextMenuItemTagCopyImageToClipboard:
153 // FIXME: The Pasteboard class is not written yet
154 // For now, call into the client. This is temporary!
155 frame->editor()->copyImage(result);
157 case ContextMenuItemTagOpenFrameInNewWindow: {
158 DocumentLoader* loader = frame->loader()->documentLoader();
159 if (!loader->unreachableURL().isEmpty())
160 openNewWindow(loader->unreachableURL(), frame);
162 openNewWindow(loader->url(), frame);
165 case ContextMenuItemTagCopy:
166 frame->editor()->copy();
168 case ContextMenuItemTagGoBack:
169 frame->loader()->goBackOrForward(-1);
171 case ContextMenuItemTagGoForward:
172 frame->loader()->goBackOrForward(1);
174 case ContextMenuItemTagStop:
175 frame->loader()->stop();
177 case ContextMenuItemTagReload:
178 frame->loader()->reload();
180 case ContextMenuItemTagCut:
181 frame->editor()->cut();
183 case ContextMenuItemTagPaste:
184 frame->editor()->paste();
187 case ContextMenuItemTagDelete:
188 frame->editor()->performDelete();
190 case ContextMenuItemTagSelectAll:
191 frame->editor()->command("SelectAll").execute();
194 case ContextMenuItemTagSpellingGuess:
195 ASSERT(frame->selectedText().length());
196 if (frame->editor()->shouldInsertText(item->title(), frame->selection()->toRange().get(),
197 EditorInsertActionPasted)) {
198 Document* document = frame->document();
199 RefPtr<ReplaceSelectionCommand> command =
200 ReplaceSelectionCommand::create(document, createFragmentFromMarkup(document, item->title(), ""),
202 applyCommand(command);
203 frame->revealSelection(RenderLayer::gAlignToEdgeIfNeeded);
206 case ContextMenuItemTagIgnoreSpelling:
207 frame->editor()->ignoreSpelling();
209 case ContextMenuItemTagLearnSpelling:
210 frame->editor()->learnSpelling();
212 case ContextMenuItemTagSearchWeb:
213 m_client->searchWithGoogle(frame);
215 case ContextMenuItemTagLookUpInDictionary:
216 // FIXME: Some day we may be able to do this from within WebCore.
217 m_client->lookUpInDictionary(frame);
219 case ContextMenuItemTagOpenLink:
220 if (Frame* targetFrame = result.targetFrame())
221 targetFrame->loader()->loadFrameRequestWithFormAndValues(FrameLoadRequest(ResourceRequest(result.absoluteLinkURL(),
222 frame->loader()->outgoingReferrer())), false, 0, 0, HashMap<String, String>());
224 openNewWindow(result.absoluteLinkURL(), frame);
226 case ContextMenuItemTagBold:
227 frame->editor()->command("ToggleBold").execute();
229 case ContextMenuItemTagItalic:
230 frame->editor()->command("ToggleItalic").execute();
232 case ContextMenuItemTagUnderline:
233 frame->editor()->toggleUnderline();
235 case ContextMenuItemTagOutline:
236 // We actually never enable this because CSS does not have a way to specify an outline font,
237 // which may make this difficult to implement. Maybe a special case of text-shadow?
239 case ContextMenuItemTagStartSpeaking: {
241 RefPtr<Range> selectedRange = frame->selection()->toRange();
242 if (!selectedRange || selectedRange->collapsed(ec)) {
243 Document* document = result.innerNonSharedNode()->document();
244 selectedRange = document->createRange();
245 selectedRange->selectNode(document->documentElement(), ec);
247 m_client->speak(plainText(selectedRange.get()));
250 case ContextMenuItemTagStopSpeaking:
251 m_client->stopSpeaking();
253 case ContextMenuItemTagDefaultDirection:
254 frame->editor()->setBaseWritingDirection(NaturalWritingDirection);
256 case ContextMenuItemTagLeftToRight:
257 frame->editor()->setBaseWritingDirection(LeftToRightWritingDirection);
259 case ContextMenuItemTagRightToLeft:
260 frame->editor()->setBaseWritingDirection(RightToLeftWritingDirection);
262 case ContextMenuItemTagTextDirectionDefault:
263 frame->editor()->command("MakeTextWritingDirectionNatural").execute();
265 case ContextMenuItemTagTextDirectionLeftToRight:
266 frame->editor()->command("MakeTextWritingDirectionLeftToRight").execute();
268 case ContextMenuItemTagTextDirectionRightToLeft:
269 frame->editor()->command("MakeTextWritingDirectionRightToLeft").execute();
272 case ContextMenuItemTagSearchInSpotlight:
273 m_client->searchWithSpotlight();
276 case ContextMenuItemTagShowSpellingPanel:
277 frame->editor()->showSpellingGuessPanel();
279 case ContextMenuItemTagCheckSpelling:
280 frame->editor()->advanceToNextMisspelling();
282 case ContextMenuItemTagCheckSpellingWhileTyping:
283 frame->editor()->toggleContinuousSpellChecking();
285 #ifndef BUILDING_ON_TIGER
286 case ContextMenuItemTagCheckGrammarWithSpelling:
287 frame->editor()->toggleGrammarChecking();
291 case ContextMenuItemTagShowFonts:
292 frame->editor()->showFontPanel();
294 case ContextMenuItemTagStyles:
295 frame->editor()->showStylesPanel();
297 case ContextMenuItemTagShowColors:
298 frame->editor()->showColorPanel();
301 case ContextMenuItemTagInspectElement:
302 if (Page* page = frame->page())
303 page->inspectorController()->inspect(result.innerNonSharedNode());
310 } // namespace WebCore