handle cmd-enter in urlbar and search field to open new tab or new window

depending on pref. refactors load logic a little better (bug 247238)
This commit is contained in:
pinkerton%aol.net 2004-09-28 21:05:58 +00:00
Родитель 2f5825e73e
Коммит 50e371fb68
3 изменённых файлов: 164 добавлений и 69 удалений

Просмотреть файл

@ -81,6 +81,19 @@ static const int kEscapeKeyCode = 53;
mSuppressMakeKeyFront = inSuppress; mSuppressMakeKeyFront = inSuppress;
} }
// pass command-return off to the controller so that locations/searches may be opened in a new tab
- (BOOL)performKeyEquivalent:(NSEvent *)theEvent
{
BrowserWindowController* windowController = (BrowserWindowController*)[self delegate];
NSString* keyString = [theEvent charactersIgnoringModifiers];
unichar keyChar = [keyString characterAtIndex:0];
BOOL handled = NO;
if (keyChar == NSCarriageReturnCharacter) {
handled = [windowController handleCommandReturn];
}
return handled ? handled : [super performKeyEquivalent:theEvent];
}
// accessor for the 'URL' Apple Event attribute // accessor for the 'URL' Apple Event attribute
- (NSString*)getURL - (NSString*)getURL
{ {

Просмотреть файл

@ -363,4 +363,7 @@ typedef enum
// not hold onto this for longer than the current call unless they addref it. // not hold onto this for longer than the current call unless they addref it.
- (nsIWebNavigation*) currentWebNavigation; - (nsIWebNavigation*) currentWebNavigation;
// handle command-return in location or search field
- (BOOL) handleCommandReturn;
@end @end

Просмотреть файл

@ -128,6 +128,12 @@ static NSArray* sToolbarDefaults = nil;
#define kMaxBrowserWindowTabs 16 #define kMaxBrowserWindowTabs 16
enum BWCOpenDest {
kDestinationNewWindow = 0,
kDestinationNewTab,
kDestinationCurrentView
};
////////////////////////////////////// //////////////////////////////////////
@interface AutoCompleteTextFieldEditor : NSTextView @interface AutoCompleteTextFieldEditor : NSTextView
{ {
@ -349,6 +355,8 @@ static NSArray* sToolbarDefaults = nil;
-(void)openNewWindowWithDescriptor:(nsISupports*)aDesc displayType:(PRUint32)aDisplayType loadInBackground:(BOOL)aLoadInBG; -(void)openNewWindowWithDescriptor:(nsISupports*)aDesc displayType:(PRUint32)aDisplayType loadInBackground:(BOOL)aLoadInBG;
-(void)openNewTabWithDescriptor:(nsISupports*)aDesc displayType:(PRUint32)aDisplayType loadInBackground:(BOOL)aLoadInBG; -(void)openNewTabWithDescriptor:(nsISupports*)aDesc displayType:(PRUint32)aDisplayType loadInBackground:(BOOL)aLoadInBG;
- (BOOL)isPageTextFieldFocused; - (BOOL)isPageTextFieldFocused;
-(void)performSearch:(SearchTextField *)inSearchField inView:(BWCOpenDest)inDest inBackground:(BOOL)inLoadInBG;
-(void)goToLocationFromToolbarURLField:(AutoCompleteTextField *)inURLField inView:(BWCOpenDest)inDest inBackground:(BOOL)inLoadInBG;
// create back/forward session history menus on toolbar button // create back/forward session history menus on toolbar button
- (IBAction)backMenu:(id)inSender; - (IBAction)backMenu:(id)inSender;
@ -1439,22 +1447,35 @@ static NSArray* sToolbarDefaults = nil;
} }
- (IBAction)goToLocationFromToolbarURLField:(id)sender - (IBAction)goToLocationFromToolbarURLField:(id)sender
{
if ([sender isKindOfClass:[AutoCompleteTextField class]])
[self goToLocationFromToolbarURLField:(AutoCompleteTextField *)sender
inView:kDestinationCurrentView inBackground:NO];
}
- (void)goToLocationFromToolbarURLField:(AutoCompleteTextField *)inURLField
inView:(BWCOpenDest)inDest inBackground:(BOOL)inLoadInBG
{ {
// trim off any whitespace around url // trim off any whitespace around url
NSString *theURL = [[sender stringValue] stringByTrimmingWhitespace]; NSString *theURL = [[inURLField stringValue] stringByTrimmingWhitespace];
// look for bookmarks keywords match // look for bookmarks keywords match
NSArray *resolvedURLs = [[BookmarkManager sharedBookmarkManager] resolveBookmarksKeyword:theURL]; NSArray *resolvedURLs = [[BookmarkManager sharedBookmarkManager] resolveBookmarksKeyword:theURL];
NSString* resolvedURL = nil; NSString* resolvedURL = nil;
if ([resolvedURLs count] == 1) if ([resolvedURLs count] == 1) {
{
resolvedURL = [resolvedURLs lastObject]; resolvedURL = [resolvedURLs lastObject];
[self loadURL:resolvedURL referrer:nil activate:YES]; if (inDest == kDestinationNewTab)
} [self openNewTabWithURL:resolvedURL referrer:nil loadInBackground:inLoadInBG];
else else if (inDest == kDestinationNewWindow)
{ [self openNewWindowWithURL:resolvedURL referrer:nil loadInBackground:inLoadInBG];
[self openURLArray:resolvedURLs replaceExistingTabs:YES]; else // if it's not a new window or a new tab, load into the current view
[self loadURL:resolvedURL referrer:nil activate:YES];
} else {
if (inDest == kDestinationNewTab || inDest == kDestinationNewWindow)
[self openURLArray:resolvedURLs replaceExistingTabs:NO];
else
[self openURLArray:resolvedURLs replaceExistingTabs:YES];
} }
// global history needs to know the user typed this url so it can present it // global history needs to know the user typed this url so it can present it
@ -1472,7 +1493,6 @@ static NSArray* sToolbarDefaults = nil;
mGlobalHistory->MarkPageAsTyped(fixedURI); mGlobalHistory->MarkPageAsTyped(fixedURI);
} }
} }
- (void)saveDocument:(BOOL)focusedFrame filterView:(NSView*)aFilterView - (void)saveDocument:(BOOL)focusedFrame filterView:(NSView*)aFilterView
{ {
[[mBrowserView getBrowserView] saveDocument:focusedFrame filterView:aFilterView]; [[mBrowserView getBrowserView] saveDocument:focusedFrame filterView:aFilterView];
@ -1543,70 +1563,91 @@ static NSArray* sToolbarDefaults = nil;
- (IBAction)performSearch:(id)aSender - (IBAction)performSearch:(id)aSender
{ {
// If we have a valid SearchTextField, perform a search using its contents // If we have a valid SearchTextField, perform a search using its contents
if ([aSender isKindOfClass:[SearchTextField class]]) { if ([aSender isKindOfClass:[SearchTextField class]])
// Get the search URL from our dictionary of sites and search urls [self performSearch:(SearchTextField *)aSender inView:kDestinationCurrentView inBackground:NO];
NSMutableString *searchURL = [NSMutableString stringWithString: }
[[BrowserWindowController searchURLDictionary] objectForKey:
[aSender titleOfSelectedPopUpItem]]];
NSString *currentURL = [[self getBrowserWrapper] getCurrentURLSpec];
NSString *searchString = [aSender stringValue];
const char *aURLSpec = [currentURL lossyCString]; //
NSString *aDomain = @""; // - performSearch:inView:inBackground
nsIURI *aURI = nil; //
// performs a search using searchField and opens either in the current view, a new tab, or a new
// window. If it's a new tab or window, loadInBG determines whether the window/tab is opened in the background
//
-(void)performSearch:(SearchTextField *)inSearchField inView:(BWCOpenDest)inDest inBackground:(BOOL)inLoadInBG
{
// Get the search URL from our dictionary of sites and search urls
NSMutableString *searchURL = [NSMutableString stringWithString:
[[BrowserWindowController searchURLDictionary] objectForKey:
[inSearchField titleOfSelectedPopUpItem]]];
NSString *currentURL = [[self getBrowserWrapper] getCurrentURLSpec];
NSString *searchString = [inSearchField stringValue];
// If we have an about: type URL, remove " site:%d" from the search string const char *aURLSpec = [currentURL lossyCString];
// This is a fix to deal with Google's Search this Site feature NSString *aDomain = @"";
// If other sites use %d to search the site, we'll have to have specific rules nsIURI *aURI = nil;
// for those sites.
if ([currentURL hasPrefix:@"about:"]) { // If we have an about: type URL, remove " site:%d" from the search string
NSRange domainStringRange = [searchURL rangeOfString:@" site:%d" // This is a fix to deal with Google's Search this Site feature
options:NSBackwardsSearch]; // If other sites use %d to search the site, we'll have to have specific rules
// for those sites.
NSRange notFoundRange = NSMakeRange(NSNotFound, 0); if ([currentURL hasPrefix:@"about:"]) {
if (NSEqualRanges(domainStringRange, notFoundRange) == NO) NSRange domainStringRange = [searchURL rangeOfString:@" site:%d"
[searchURL deleteCharactersInRange:domainStringRange]; options:NSBackwardsSearch];
}
// If they didn't type anything in the search field, visit the domain of NSRange notFoundRange = NSMakeRange(NSNotFound, 0);
// the search site, i.e. www.google.com for the Google site if (NSEqualRanges(domainStringRange, notFoundRange) == NO)
if ([[aSender stringValue] isEqualToString:@""]) { [searchURL deleteCharactersInRange:domainStringRange];
aURLSpec = [searchURL lossyCString]; }
if (NS_NewURI(&aURI, aURLSpec, nsnull, nsnull) == NS_OK) { // If they didn't type anything in the search field, visit the domain of
nsCAutoString spec; // the search site, i.e. www.google.com for the Google site
aURI->GetHost(spec); if ([[inSearchField stringValue] isEqualToString:@""]) {
aURLSpec = [searchURL lossyCString];
aDomain = [NSString stringWithUTF8String:spec.get()]; if (NS_NewURI(&aURI, aURLSpec, nsnull, nsnull) == NS_OK) {
nsCAutoString spec;
aURI->GetHost(spec);
aDomain = [NSString stringWithUTF8String:spec.get()];
if (inDest == kDestinationNewTab)
[self openNewTabWithURL:aDomain referrer:nil loadInBackground:inLoadInBG];
else if (inDest == kDestinationNewWindow)
[self openNewWindowWithURL:aDomain referrer:nil loadInBackground:inLoadInBG];
else // if it's not a new window or a new tab, load into the current view
[self loadURL:aDomain referrer:nil activate:NO]; [self loadURL:aDomain referrer:nil activate:NO];
}
} }
else { } else {
aURLSpec = [[[self getBrowserWrapper] getCurrentURLSpec] lossyCString]; aURLSpec = [[[self getBrowserWrapper] getCurrentURLSpec] lossyCString];
// Get the domain so that we can replace %d in our searchURL // Get the domain so that we can replace %d in our searchURL
if (NS_NewURI(&aURI, aURLSpec, nsnull, nsnull) == NS_OK) { if (NS_NewURI(&aURI, aURLSpec, nsnull, nsnull) == NS_OK) {
nsCAutoString spec; nsCAutoString spec;
aURI->GetHost(spec); aURI->GetHost(spec);
aDomain = [NSString stringWithUTF8String:spec.get()]; aDomain = [NSString stringWithUTF8String:spec.get()];
} }
// Escape the search string so the user can search for strings with // Escape the search string so the user can search for strings with
// special characters ("&", "+", etc.) List from RFC2396. // special characters ("&", "+", etc.) List from RFC2396.
NSString *escapedSearchString = (NSString *) CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)searchString, NULL, CFSTR(";/?:@&=+$,"), kCFStringEncodingUTF8); NSString *escapedSearchString = (NSString *) CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)searchString, NULL, CFSTR(";/?:@&=+$,"), kCFStringEncodingUTF8);
// replace the conversion specifiers (%d, %s) in the search string // replace the conversion specifiers (%d, %s) in the search string
[self transformFormatString:searchURL domain:aDomain search:escapedSearchString]; [self transformFormatString:searchURL domain:aDomain search:escapedSearchString];
[escapedSearchString release]; [escapedSearchString release];
if (inDest == kDestinationNewTab)
[self openNewTabWithURL:searchURL referrer:nil loadInBackground:inLoadInBG];
else if (inDest == kDestinationNewWindow)
[self openNewWindowWithURL:searchURL referrer:nil loadInBackground:inLoadInBG];
else // if it's not a new window or a new tab, load into the current view
[self loadURL:searchURL referrer:nil activate:NO]; [self loadURL:searchURL referrer:nil activate:NO];
}
} }
} }
// //
// - transformFormatString:domain:search // - transformFormatString:domain:search
// //
@ -3170,6 +3211,44 @@ static NSArray* sToolbarDefaults = nil;
[self toggleBookmarkManager:self]; [self toggleBookmarkManager:self];
} }
//
// - handleCommandReturn:
//
// handle command-return in location or search field, opening a new tab or window as appropriate
//
- (BOOL) handleCommandReturn
{
BOOL handled = NO;
// determine whether to load in background
PRBool loadInBG;
nsCOMPtr<nsIPrefBranch> pref(do_GetService("@mozilla.org/preferences-service;1"));
pref->GetBoolPref("browser.tabs.loadInBackground", &loadInBG);
// determine whether to load in tab or window
PRBool loadInTab;
pref->GetBoolPref("browser.tabs.opentabfor.middleclick",&loadInTab);
BWCOpenDest destination = loadInTab ? kDestinationNewTab : kDestinationNewWindow;
// see if command-return came in the url bar
if ([mURLBar fieldEditor] && [[self window] firstResponder] == [mURLBar fieldEditor]) {
handled = YES;
[self goToLocationFromToolbarURLField:mURLBar inView:destination inBackground:loadInBG];
// kill any autocomplete that was in progress
[mURLBar revertText];
// set the text in the URL bar back to the current URL
[self updateLocationFields:[mBrowserView getCurrentURLSpec]];
// see if command-return came in the search field
} else if ([mSearchBar isFirstResponder]) {
handled = YES;
[self performSearch:mSearchBar inView:destination inBackground:loadInBG];
}
return handled;
}
@end @end
#pragma mark - #pragma mark -