webengine/osswebengine/WebKit/DefaultDelegates/WebDefaultContextMenuDelegate.mm
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     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 
       
    29 #import "WebDefaultContextMenuDelegate.h"
       
    30 
       
    31 #import "WebDOMOperations.h"
       
    32 #import "WebDataSourcePrivate.h"
       
    33 #import "WebDefaultUIDelegate.h"
       
    34 #import "WebFrameBridge.h"
       
    35 #import "WebFrameInternal.h"
       
    36 #import "WebFrameView.h"
       
    37 #import "WebHTMLViewPrivate.h"
       
    38 #import "WebLocalizableStrings.h"
       
    39 #import "WebNSPasteboardExtras.h"
       
    40 #import "WebNSURLRequestExtras.h"
       
    41 #import "WebPolicyDelegate.h"
       
    42 #import "WebUIDelegate.h"
       
    43 #import "WebUIDelegatePrivate.h"
       
    44 #import "WebViewInternal.h"
       
    45 #import <Foundation/NSURLConnection.h>
       
    46 #import <Foundation/NSURLRequest.h>
       
    47 #import <JavaScriptCore/Assertions.h>
       
    48 #import <WebCore/Editor.h>
       
    49 #import <WebCore/Frame.h>
       
    50 #import <WebCore/FrameLoader.h>
       
    51 #import <WebCore/KURL.h>
       
    52 #import <WebCore/WebCoreFrameBridge.h>
       
    53 #import <WebKit/DOM.h>
       
    54 #import <WebKit/DOMPrivate.h>
       
    55 
       
    56 @implementation WebDefaultUIDelegate (WebContextMenu)
       
    57 
       
    58 - (NSMenuItem *)menuItemWithTag:(int)tag target:(id)target representedObject:(id)representedObject
       
    59 {
       
    60     NSMenuItem *menuItem = [[[NSMenuItem alloc] init] autorelease];
       
    61     [menuItem setTag:tag];
       
    62     [menuItem setTarget:target]; // can be nil
       
    63     [menuItem setRepresentedObject:representedObject];
       
    64     
       
    65     NSString *title = nil;
       
    66     SEL action = NULL;
       
    67     
       
    68     switch(tag) {
       
    69         case WebMenuItemTagCopy:
       
    70             title = UI_STRING("Copy", "Copy context menu item");
       
    71             action = @selector(copy:);
       
    72             break;
       
    73         case WebMenuItemTagGoBack:
       
    74             title = UI_STRING("Back", "Back context menu item");
       
    75             action = @selector(goBack:);
       
    76             break;
       
    77         case WebMenuItemTagGoForward:
       
    78             title = UI_STRING("Forward", "Forward context menu item");
       
    79             action = @selector(goForward:);
       
    80             break;
       
    81         case WebMenuItemTagStop:
       
    82             title = UI_STRING("Stop", "Stop context menu item");
       
    83             action = @selector(stopLoading:);
       
    84             break;
       
    85         case WebMenuItemTagReload:
       
    86             title = UI_STRING("Reload", "Reload context menu item");
       
    87             action = @selector(reload:);
       
    88             break;
       
    89         case WebMenuItemTagSearchInSpotlight:
       
    90             title = UI_STRING("Search in Spotlight", "Search in Spotlight context menu item");
       
    91             action = @selector(_searchWithSpotlightFromMenu:);
       
    92             break;
       
    93         case WebMenuItemTagSearchWeb:
       
    94             title = UI_STRING("Search in Google", "Search in Google context menu item");
       
    95             action = @selector(_searchWithGoogleFromMenu:);
       
    96             break;
       
    97         case WebMenuItemTagLookUpInDictionary:
       
    98             title = UI_STRING("Look Up in Dictionary", "Look Up in Dictionary context menu item");
       
    99             action = @selector(_lookUpInDictionaryFromMenu:);
       
   100             break;
       
   101         case WebMenuItemTagOpenFrameInNewWindow:
       
   102             title = UI_STRING("Open Frame in New Window", "Open Frame in New Window context menu item");
       
   103             action = @selector(_openFrameInNewWindowFromMenu:);
       
   104             break;
       
   105         default:
       
   106             ASSERT_NOT_REACHED();
       
   107             return nil;
       
   108     }
       
   109 
       
   110     if (title)
       
   111         [menuItem setTitle:title];
       
   112 
       
   113     [menuItem setAction:action];
       
   114     
       
   115     return menuItem;
       
   116 }
       
   117 
       
   118 - (void)appendDefaultItems:(NSArray *)defaultItems toArray:(NSMutableArray *)menuItems
       
   119 {
       
   120     ASSERT_ARG(menuItems, menuItems != nil);
       
   121     if ([defaultItems count] > 0) {
       
   122         ASSERT(![[menuItems lastObject] isSeparatorItem]);
       
   123         if (![[defaultItems objectAtIndex:0] isSeparatorItem]) {
       
   124             [menuItems addObject:[NSMenuItem separatorItem]];
       
   125             
       
   126             NSEnumerator *e = [defaultItems objectEnumerator];
       
   127             NSMenuItem *item;
       
   128             while ((item = [e nextObject]) != nil) {
       
   129                 [menuItems addObject:item];
       
   130             }
       
   131         }
       
   132     }
       
   133 }
       
   134 
       
   135 - (NSArray *)webView:(WebView *)wv contextMenuItemsForElement:(NSDictionary *)element  defaultMenuItems:(NSArray *)defaultMenuItems
       
   136 {
       
   137     // The defaultMenuItems here are ones supplied by the WebDocumentView protocol implementation. WebPDFView is
       
   138     // one case that has non-nil default items here.
       
   139     NSMutableArray *menuItems = [NSMutableArray array];
       
   140 
       
   141     WebFrame *webFrame = [element objectForKey:WebElementFrameKey];
       
   142     
       
   143     if ([[element objectForKey:WebElementIsSelectedKey] boolValue]) {
       
   144         // The Spotlight and Google items are implemented in WebView, and require that the
       
   145         // current document view conforms to WebDocumentText
       
   146         ASSERT([[[webFrame frameView] documentView] conformsToProtocol:@protocol(WebDocumentText)]);
       
   147         [menuItems addObject:[self menuItemWithTag:WebMenuItemTagSearchInSpotlight target:nil representedObject:element]];
       
   148         [menuItems addObject:[self menuItemWithTag:WebMenuItemTagSearchWeb target:nil representedObject:element]];
       
   149         [menuItems addObject:[NSMenuItem separatorItem]];
       
   150 
       
   151         // FIXME 4184640: The Look Up in Dictionary item is only implemented in WebHTMLView, and so is present but
       
   152         // dimmed for other cases where WebElementIsSelectedKey is present. It would probably 
       
   153         // be better not to include it in the menu if the documentView isn't a WebHTMLView, but that could break 
       
   154         // existing clients that have code that relies on it being present (unlikely for clients outside of Apple, 
       
   155         // but Safari has such code).
       
   156         [menuItems addObject:[self menuItemWithTag:WebMenuItemTagLookUpInDictionary target:nil representedObject:element]];            
       
   157         [menuItems addObject:[NSMenuItem separatorItem]];
       
   158         [menuItems addObject:[self menuItemWithTag:WebMenuItemTagCopy target:nil representedObject:element]];
       
   159     } else {
       
   160         WebView *wv = [webFrame webView];
       
   161         if ([wv canGoBack]) {
       
   162             [menuItems addObject:[self menuItemWithTag:WebMenuItemTagGoBack target:wv representedObject:element]];
       
   163         }
       
   164         if ([wv canGoForward]) {
       
   165             [menuItems addObject:[self menuItemWithTag:WebMenuItemTagGoForward target:wv representedObject:element]];
       
   166         }
       
   167         if ([wv isLoading]) {
       
   168             [menuItems addObject:[self menuItemWithTag:WebMenuItemTagStop target:wv representedObject:element]];
       
   169         } else {
       
   170             [menuItems addObject:[self menuItemWithTag:WebMenuItemTagReload target:wv representedObject:element]];
       
   171         }
       
   172 
       
   173         if (webFrame != [wv mainFrame]) {
       
   174             [menuItems addObject:[self menuItemWithTag:WebMenuItemTagOpenFrameInNewWindow target:wv representedObject:element]];
       
   175         }
       
   176     }
       
   177     
       
   178     // Add the default items at the end, if any, after a separator
       
   179     [self appendDefaultItems:defaultMenuItems toArray:menuItems];
       
   180 
       
   181     return menuItems;
       
   182 }
       
   183 
       
   184 @end