webengine/osswebengine/WebKit/win/WebContextMenuClient.cpp
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     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 
       
    26 #include "config.h"
       
    27 #include "WebContextMenuClient.h"
       
    28 
       
    29 #include "WebDownload.h"
       
    30 #include "WebElementPropertyBag.h"
       
    31 #include "WebLocalizableStrings.h"
       
    32 #include "WebView.h"
       
    33 
       
    34 #pragma warning(push, 0)
       
    35 #include <WebCore/ContextMenu.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 #pragma warning(pop)
       
    42 
       
    43 #include <tchar.h>
       
    44 
       
    45 using namespace WebCore;
       
    46 
       
    47 WebContextMenuClient::WebContextMenuClient(WebView* webView)
       
    48     : m_webView(webView)
       
    49 {
       
    50 }
       
    51 
       
    52 void WebContextMenuClient::contextMenuDestroyed()
       
    53 {
       
    54     delete this;
       
    55 }
       
    56 
       
    57 static bool isPreInspectElementTagSafari(IWebUIDelegate* uiDelegate)
       
    58 {
       
    59     if (!uiDelegate)
       
    60         return false;
       
    61 
       
    62     // We assume anyone who implements IWebUIDelegate2 also knows about the Inspect Element item.
       
    63     COMPtr<IWebUIDelegate2> uiDelegate2;
       
    64     if (SUCCEEDED(uiDelegate->QueryInterface(IID_IWebUIDelegate2, (void**)&uiDelegate2)))
       
    65         return false;
       
    66 
       
    67     TCHAR modulePath[MAX_PATH];
       
    68     DWORD length = ::GetModuleFileName(0, modulePath, _countof(modulePath));
       
    69     if (!length)
       
    70         return false;
       
    71 
       
    72     return String(modulePath, length).endsWith("Safari.exe", false);
       
    73 }
       
    74 
       
    75 static HMENU fixMenuReceivedFromOldSafari(IWebUIDelegate* uiDelegate, ContextMenu* originalMenu, HMENU menuFromClient)
       
    76 {
       
    77     ASSERT_ARG(originalMenu, originalMenu);
       
    78     if (!isPreInspectElementTagSafari(uiDelegate))
       
    79         return menuFromClient;
       
    80 
       
    81     int count = ::GetMenuItemCount(originalMenu->platformDescription());
       
    82     if (count < 1)
       
    83         return menuFromClient;
       
    84 
       
    85     if (::GetMenuItemID(originalMenu->platformDescription(), count - 1) != WebMenuItemTagInspectElement)
       
    86         return menuFromClient;
       
    87 
       
    88     count = ::GetMenuItemCount(menuFromClient);
       
    89     if (count < 1)
       
    90         return menuFromClient;
       
    91 
       
    92     if (::GetMenuItemID(menuFromClient, count - 1) == WebMenuItemTagInspectElement)
       
    93         return menuFromClient;
       
    94 
       
    95     originalMenu->setPlatformDescription(menuFromClient);
       
    96     originalMenu->addInspectElementItem();
       
    97     return originalMenu->platformDescription();
       
    98 }
       
    99 
       
   100 HMENU WebContextMenuClient::getCustomMenuFromDefaultItems(ContextMenu* menu)
       
   101 {
       
   102     COMPtr<IWebUIDelegate> uiDelegate;
       
   103     if (FAILED(m_webView->uiDelegate(&uiDelegate)))
       
   104         return menu->platformDescription();
       
   105 
       
   106     ASSERT(uiDelegate);
       
   107 
       
   108     HMENU newMenu = 0;
       
   109     COMPtr<WebElementPropertyBag> propertyBag;
       
   110     propertyBag.adoptRef(WebElementPropertyBag::createInstance(menu->hitTestResult()));
       
   111     // FIXME: We need to decide whether to do the default before calling this delegate method
       
   112     if (FAILED(uiDelegate->contextMenuItemsForElement(m_webView, propertyBag.get(), (OLE_HANDLE)(ULONG64)menu->platformDescription(), (OLE_HANDLE*)&newMenu)))
       
   113         return menu->platformDescription();
       
   114     return fixMenuReceivedFromOldSafari(uiDelegate.get(), menu, newMenu);
       
   115 }
       
   116 
       
   117 void WebContextMenuClient::contextMenuItemSelected(ContextMenuItem* item, const ContextMenu* parentMenu)
       
   118 {
       
   119     ASSERT(item->type() == ActionType);
       
   120 
       
   121     COMPtr<IWebUIDelegate> uiDelegate;
       
   122     if (FAILED(m_webView->uiDelegate(&uiDelegate)))
       
   123         return;
       
   124 
       
   125     ASSERT(uiDelegate);
       
   126 
       
   127     COMPtr<WebElementPropertyBag> propertyBag;
       
   128     propertyBag.adoptRef(WebElementPropertyBag::createInstance(parentMenu->hitTestResult()));
       
   129             
       
   130     uiDelegate->contextMenuItemSelected(m_webView, item->releasePlatformDescription(), propertyBag.get());
       
   131 }
       
   132 
       
   133 void WebContextMenuClient::downloadURL(const KURL& url)
       
   134 {
       
   135     COMPtr<IWebDownloadDelegate> downloadDelegate;
       
   136     if (FAILED(m_webView->downloadDelegate(&downloadDelegate))) {
       
   137         // If the WebView doesn't successfully provide a download delegate we'll pass a null one
       
   138         // into the WebDownload - which may or may not decide to use a DefaultDownloadDelegate
       
   139         LOG_ERROR("Failed to get downloadDelegate from WebView");
       
   140         downloadDelegate = 0;
       
   141     }
       
   142 
       
   143     // Its the delegate's job to ref the WebDownload to keep it alive - otherwise it will be destroyed
       
   144     // when this method returns
       
   145     COMPtr<WebDownload> download;
       
   146     download.adoptRef(WebDownload::createInstance(url, downloadDelegate.get()));
       
   147     download->start();
       
   148 }
       
   149 
       
   150 void WebContextMenuClient::searchWithGoogle(const Frame* frame)
       
   151 {
       
   152     String searchString = frame->selectedText();
       
   153     searchString.stripWhiteSpace();
       
   154     DeprecatedString encoded = KURL::encode_string(searchString.deprecatedString());
       
   155     encoded.replace(DeprecatedString("%20"), DeprecatedString("+"));
       
   156     
       
   157     String url("http://www.google.com/search?q=");
       
   158     url.append(String(encoded));
       
   159     url.append("&ie=UTF-8&oe=UTF-8");
       
   160 
       
   161     ResourceRequest request = ResourceRequest(url);
       
   162     if (Page* page = frame->page())
       
   163         page->mainFrame()->loader()->urlSelected(FrameLoadRequest(request), 0, false, true);
       
   164 }
       
   165 
       
   166 void WebContextMenuClient::lookUpInDictionary(Frame*)
       
   167 {
       
   168     notImplemented();
       
   169 }
       
   170 
       
   171 void WebContextMenuClient::speak(const String&)
       
   172 {
       
   173     notImplemented();
       
   174 }
       
   175 
       
   176 void WebContextMenuClient::stopSpeaking()
       
   177 {
       
   178     notImplemented();
       
   179 }