WebContextMenuClient.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  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.
  12. *
  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.
  24. */
  25. #include "config.h"
  26. #include "WebContextMenuClient.h"
  27. #include "UserGestureIndicator.h"
  28. #include "WebElementPropertyBag.h"
  29. #include "WebLocalizableStrings.h"
  30. #include "WebView.h"
  31. #include <WebCore/ContextMenu.h>
  32. #include <WebCore/ContextMenuController.h>
  33. #include <WebCore/Editor.h>
  34. #include <WebCore/Event.h>
  35. #include <WebCore/Frame.h>
  36. #include <WebCore/FrameLoader.h>
  37. #include <WebCore/FrameLoadRequest.h>
  38. #include <WebCore/Page.h>
  39. #include <WebCore/ResourceRequest.h>
  40. #include <WebCore/NotImplemented.h>
  41. using namespace WebCore;
  42. WebContextMenuClient::WebContextMenuClient(WebView* webView)
  43. : m_webView(webView)
  44. {
  45. }
  46. void WebContextMenuClient::contextMenuDestroyed()
  47. {
  48. delete this;
  49. }
  50. PassOwnPtr<ContextMenu> WebContextMenuClient::customizeMenu(PassOwnPtr<ContextMenu> popMenu)
  51. {
  52. OwnPtr<ContextMenu> menu = popMenu;
  53. COMPtr<IWebUIDelegate> uiDelegate;
  54. if (FAILED(m_webView->uiDelegate(&uiDelegate)))
  55. return menu.release();
  56. ASSERT(uiDelegate);
  57. HMENU nativeMenu = menu->platformContextMenu();
  58. COMPtr<WebElementPropertyBag> propertyBag;
  59. propertyBag.adoptRef(WebElementPropertyBag::createInstance(m_webView->page()->contextMenuController()->hitTestResult()));
  60. // FIXME: We need to decide whether to do the default before calling this delegate method
  61. if (FAILED(uiDelegate->contextMenuItemsForElement(m_webView, propertyBag.get(), (OLE_HANDLE)(ULONG64)nativeMenu, (OLE_HANDLE*)&nativeMenu))) {
  62. ::DestroyMenu(nativeMenu);
  63. return menu.release();
  64. }
  65. OwnPtr<ContextMenu> customizedMenu = adoptPtr(new ContextMenu(nativeMenu));
  66. ::DestroyMenu(nativeMenu);
  67. return customizedMenu.release();
  68. }
  69. void WebContextMenuClient::contextMenuItemSelected(ContextMenuItem* item, const ContextMenu* parentMenu)
  70. {
  71. ASSERT(item->type() == ActionType || item->type() == CheckableActionType);
  72. COMPtr<IWebUIDelegate> uiDelegate;
  73. if (FAILED(m_webView->uiDelegate(&uiDelegate)))
  74. return;
  75. ASSERT(uiDelegate);
  76. COMPtr<WebElementPropertyBag> propertyBag;
  77. propertyBag.adoptRef(WebElementPropertyBag::createInstance(m_webView->page()->contextMenuController()->hitTestResult()));
  78. // This call would leak the MENUITEMINFO's subMenu if it had one, but on Windows, subMenus can't be selected, so there is
  79. // no way we would get to this point. Also, it can't be a separator, because separators cannot be selected.
  80. ASSERT(item->type() != SubmenuType);
  81. ASSERT(item->type() != SeparatorType);
  82. // ContextMenuItem::platformContextMenuItem doesn't set the dwTypeData of the MENUITEMINFO, but no WebKit clients
  83. // use the title in IWebUIDelegate::contextMenuItemSelected, so we don't need to populate it here.
  84. MENUITEMINFO selectedItem = item->platformContextMenuItem();
  85. uiDelegate->contextMenuItemSelected(m_webView, &selectedItem, propertyBag.get());
  86. }
  87. void WebContextMenuClient::downloadURL(const KURL& url)
  88. {
  89. m_webView->downloadURL(url);
  90. }
  91. void WebContextMenuClient::searchWithGoogle(const Frame* frame)
  92. {
  93. String searchString = frame->editor().selectedText();
  94. searchString.stripWhiteSpace();
  95. String encoded = encodeWithURLEscapeSequences(searchString);
  96. encoded.replace("%20", "+");
  97. String url("http://www.google.com/search?q=");
  98. url.append(encoded);
  99. url.append("&ie=UTF-8&oe=UTF-8");
  100. if (Page* page = frame->page()) {
  101. UserGestureIndicator indicator(DefinitelyProcessingNewUserGesture);
  102. page->mainFrame()->loader()->urlSelected(KURL(ParsedURLString, url), String(), 0, false, false, MaybeSendReferrer);
  103. }
  104. }
  105. void WebContextMenuClient::lookUpInDictionary(Frame*)
  106. {
  107. notImplemented();
  108. }
  109. void WebContextMenuClient::speak(const String&)
  110. {
  111. notImplemented();
  112. }
  113. void WebContextMenuClient::stopSpeaking()
  114. {
  115. notImplemented();
  116. }
  117. bool WebContextMenuClient::isSpeaking()
  118. {
  119. notImplemented();
  120. return false;
  121. }