WebDefaultContextMenuDelegate.mm 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (C) 2005 Apple Computer, 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. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #import "WebDefaultContextMenuDelegate.h"
  29. #import "WebDOMOperations.h"
  30. #import "WebDataSourcePrivate.h"
  31. #import "WebDefaultUIDelegate.h"
  32. #import "WebFrameInternal.h"
  33. #import "WebFrameView.h"
  34. #import "WebHTMLViewPrivate.h"
  35. #import "WebLocalizableStringsInternal.h"
  36. #import "WebNSPasteboardExtras.h"
  37. #import "WebNSURLRequestExtras.h"
  38. #import "WebPolicyDelegate.h"
  39. #import "WebUIDelegate.h"
  40. #import "WebUIDelegatePrivate.h"
  41. #import "WebViewInternal.h"
  42. #import <Foundation/NSURLConnection.h>
  43. #import <Foundation/NSURLRequest.h>
  44. #import <WebCore/Editor.h>
  45. #import <WebCore/Frame.h>
  46. #import <WebCore/FrameLoader.h>
  47. #import <WebKit/DOM.h>
  48. #import <WebKit/DOMPrivate.h>
  49. #import <WebKitSystemInterface.h>
  50. #import <wtf/Assertions.h>
  51. @implementation WebDefaultUIDelegate (WebContextMenu)
  52. - (NSMenuItem *)menuItemWithTag:(int)tag target:(id)target representedObject:(id)representedObject
  53. {
  54. NSMenuItem *menuItem = [[[NSMenuItem alloc] init] autorelease];
  55. [menuItem setTag:tag];
  56. [menuItem setTarget:target]; // can be nil
  57. [menuItem setRepresentedObject:representedObject];
  58. NSString *title = nil;
  59. SEL action = NULL;
  60. switch(tag) {
  61. case WebMenuItemTagCopy:
  62. title = UI_STRING_INTERNAL("Copy", "Copy context menu item");
  63. action = @selector(copy:);
  64. break;
  65. case WebMenuItemTagGoBack:
  66. title = UI_STRING_INTERNAL("Back", "Back context menu item");
  67. action = @selector(goBack:);
  68. break;
  69. case WebMenuItemTagGoForward:
  70. title = UI_STRING_INTERNAL("Forward", "Forward context menu item");
  71. action = @selector(goForward:);
  72. break;
  73. case WebMenuItemTagStop:
  74. title = UI_STRING_INTERNAL("Stop", "Stop context menu item");
  75. action = @selector(stopLoading:);
  76. break;
  77. case WebMenuItemTagReload:
  78. title = UI_STRING_INTERNAL("Reload", "Reload context menu item");
  79. action = @selector(reload:);
  80. break;
  81. case WebMenuItemTagSearchInSpotlight:
  82. title = UI_STRING_INTERNAL("Search in Spotlight", "Search in Spotlight context menu item");
  83. action = @selector(_searchWithSpotlightFromMenu:);
  84. break;
  85. case WebMenuItemTagSearchWeb: {
  86. #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
  87. RetainPtr<CFStringRef> searchProviderName = adoptCF(WKCopyDefaultSearchProviderDisplayName());
  88. title = [NSString stringWithFormat:UI_STRING_INTERNAL("Search with %@", "Search with search provider context menu item with provider name inserted"), searchProviderName.get()];
  89. #else
  90. title = UI_STRING_INTERNAL("Search with Google", "Search with Google context menu item");
  91. #endif
  92. action = @selector(_searchWithGoogleFromMenu:);
  93. break;
  94. }
  95. case WebMenuItemTagLookUpInDictionary:
  96. title = UI_STRING_INTERNAL("Look Up in Dictionary", "Look Up in Dictionary context menu item");
  97. action = @selector(_lookUpInDictionaryFromMenu:);
  98. break;
  99. case WebMenuItemTagOpenFrameInNewWindow:
  100. title = UI_STRING_INTERNAL("Open Frame in New Window", "Open Frame in New Window context menu item");
  101. action = @selector(_openFrameInNewWindowFromMenu:);
  102. break;
  103. default:
  104. ASSERT_NOT_REACHED();
  105. return nil;
  106. }
  107. if (title)
  108. [menuItem setTitle:title];
  109. [menuItem setAction:action];
  110. return menuItem;
  111. }
  112. - (void)appendDefaultItems:(NSArray *)defaultItems toArray:(NSMutableArray *)menuItems
  113. {
  114. ASSERT_ARG(menuItems, menuItems != nil);
  115. if ([defaultItems count] > 0) {
  116. ASSERT(![[menuItems lastObject] isSeparatorItem]);
  117. if (![[defaultItems objectAtIndex:0] isSeparatorItem]) {
  118. [menuItems addObject:[NSMenuItem separatorItem]];
  119. NSEnumerator *e = [defaultItems objectEnumerator];
  120. NSMenuItem *item;
  121. while ((item = [e nextObject]) != nil) {
  122. [menuItems addObject:item];
  123. }
  124. }
  125. }
  126. }
  127. #if __MAC_OS_X_VERSION_MIN_REQUIRED == 1060
  128. #define INCLUDE_SPOTLIGHT_CONTEXT_MENU_ITEM 1
  129. #else
  130. #define INCLUDE_SPOTLIGHT_CONTEXT_MENU_ITEM 0
  131. #endif
  132. - (NSArray *)webView:(WebView *)wv contextMenuItemsForElement:(NSDictionary *)element defaultMenuItems:(NSArray *)defaultMenuItems
  133. {
  134. // The defaultMenuItems here are ones supplied by the WebDocumentView protocol implementation. WebPDFView is
  135. // one case that has non-nil default items here.
  136. NSMutableArray *menuItems = [NSMutableArray array];
  137. WebFrame *webFrame = [element objectForKey:WebElementFrameKey];
  138. if ([[element objectForKey:WebElementIsSelectedKey] boolValue]) {
  139. // The Spotlight and Google items are implemented in WebView, and require that the
  140. // current document view conforms to WebDocumentText
  141. ASSERT([[[webFrame frameView] documentView] conformsToProtocol:@protocol(WebDocumentText)]);
  142. // FIXME 4184640: The Look Up in Dictionary item is only implemented in WebHTMLView, and so is present but
  143. // dimmed for other cases where WebElementIsSelectedKey is present. It would probably
  144. // be better not to include it in the menu if the documentView isn't a WebHTMLView, but that could break
  145. // existing clients that have code that relies on it being present (unlikely for clients outside of Apple,
  146. // but Safari has such code).
  147. #if INCLUDE_SPOTLIGHT_CONTEXT_MENU_ITEM
  148. [menuItems addObject:[self menuItemWithTag:WebMenuItemTagSearchInSpotlight target:nil representedObject:element]];
  149. #else
  150. NSMenuItem *lookupMenuItem = [self menuItemWithTag:WebMenuItemTagLookUpInDictionary target:nil representedObject:element];
  151. NSString *selectedString = [(id <WebDocumentText>)[[webFrame frameView] documentView] selectedString];
  152. [lookupMenuItem setTitle:[NSString stringWithFormat:UI_STRING_INTERNAL("Look Up “%@”", "Look Up context menu item with selected word"), selectedString]];
  153. [menuItems addObject:lookupMenuItem];
  154. #endif
  155. [menuItems addObject:[self menuItemWithTag:WebMenuItemTagSearchWeb target:nil representedObject:element]];
  156. #if INCLUDE_SPOTLIGHT_CONTEXT_MENU_ITEM
  157. [menuItems addObject:[NSMenuItem separatorItem]];
  158. [menuItems addObject:[self menuItemWithTag:WebMenuItemTagLookUpInDictionary target:nil representedObject:element]];
  159. #endif
  160. [menuItems addObject:[NSMenuItem separatorItem]];
  161. [menuItems addObject:[self menuItemWithTag:WebMenuItemTagCopy target:nil representedObject:element]];
  162. } else {
  163. WebView *wv = [webFrame webView];
  164. if ([wv canGoBack]) {
  165. [menuItems addObject:[self menuItemWithTag:WebMenuItemTagGoBack target:wv representedObject:element]];
  166. }
  167. if ([wv canGoForward]) {
  168. [menuItems addObject:[self menuItemWithTag:WebMenuItemTagGoForward target:wv representedObject:element]];
  169. }
  170. if ([wv isLoading]) {
  171. [menuItems addObject:[self menuItemWithTag:WebMenuItemTagStop target:wv representedObject:element]];
  172. } else {
  173. [menuItems addObject:[self menuItemWithTag:WebMenuItemTagReload target:wv representedObject:element]];
  174. }
  175. if (webFrame != [wv mainFrame]) {
  176. [menuItems addObject:[self menuItemWithTag:WebMenuItemTagOpenFrameInNewWindow target:wv representedObject:element]];
  177. }
  178. }
  179. // Add the default items at the end, if any, after a separator
  180. [self appendDefaultItems:defaultMenuItems toArray:menuItems];
  181. return menuItems;
  182. }
  183. @end