diff --git a/mozilla/toolkit/components/typeaheadfind/content/findBar.js b/mozilla/toolkit/components/typeaheadfind/content/findBar.js index f486cef5d61..62d05937c3d 100755 --- a/mozilla/toolkit/components/typeaheadfind/content/findBar.js +++ b/mozilla/toolkit/components/typeaheadfind/content/findBar.js @@ -49,6 +49,7 @@ const CHAR_CODE_APOSTROPHE = "'".charCodeAt(0); // Global find variables var gFindMode = FIND_NORMAL; var gFoundLink = null; +var gCurrentWindow = null; var gTmpOutline = null; var gTmpOutlineOffset = "0"; var gDrawOutline = false; @@ -392,10 +393,20 @@ function setFoundLink(foundLink) function finishFAYT(aKeypressEvent) { - if (!gFoundLink) + try { + if (gFoundLink) + gFoundLink.focus(); + else if (gCurrentWindow) + gCurrentWindow.focus(); + else + return false; + } + catch(e) { return false; + } - gFoundLink.focus(); // In this function, gFoundLink is set null. + // NOTE: In this time, gFoundLink and gCurrentWindow are set null. + // Because find toolbar lost focus. if (aKeypressEvent) aKeypressEvent.preventDefault(); @@ -416,22 +427,34 @@ function delayedCloseFindBar() if (focusedElement && focusedElement.parentNode && (focusedElement.parentNode == findToolbar || focusedElement.parentNode.parentNode == findField)) { - if (gFoundLink) { - // block scrolling on focus since find already scrolls, further - // scrolling is due to user action, so don't override this - var suppressedScroll = document.commandDispatcher.suppressFocusScroll; - document.commandDispatcher.suppressFocusScroll = true; - gFoundLink.focus(); - document.commandDispatcher.suppressFocusScroll = suppressedScroll; + // block scrolling on focus since find already scrolls, further + // scrolling is due to user action, so don't override this + var suppressedScroll = document.commandDispatcher.suppressFocusScroll; + document.commandDispatcher.suppressFocusScroll = true; + // We MUST reset suppressFocusScroll. + try { + if (gFoundLink) + gFoundLink.focus(); + else if (gCurrentWindow) + gCurrentWindow.focus(); + else + window.content.focus(); } - else - window.content.focus(); + catch(e) { + // Retry to set focus. + try { + window.content.focus(); + } + catch(e) { /* We lose focused element! */ } + } + document.commandDispatcher.suppressFocusScroll = suppressedScroll; } } findToolbar.hidden = true; setFindMode(FIND_NORMAL); setFoundLink(null); + gCurrentWindow = null; changeSelectionColor(false); if (gQuickFindTimeout) { clearTimeout(gQuickFindTimeout); @@ -479,6 +502,7 @@ function onFindBarBlur() { changeSelectionColor(false); setFoundLink(null); + gCurrentWindow = null; } function onBrowserMouseUp(evt) @@ -568,8 +592,7 @@ function onFindBarKeyPress(evt) } else if (evt.keyCode == KeyEvent.DOM_VK_TAB) { var shouldHandle = !evt.altKey && !evt.ctrlKey && !evt.metaKey; - if (shouldHandle && gFindMode != FIND_NORMAL && - gFoundLink && finishFAYT(evt)) { + if (shouldHandle && gFindMode != FIND_NORMAL && finishFAYT(evt)) { if (evt.shiftKey) document.commandDispatcher.rewindFocus(); else @@ -610,10 +633,13 @@ function enableFindButtons(aEnable) function updateFoundLink(res) { var val = document.getElementById("find-field").value; - if (res == Components.interfaces.nsITypeAheadFind.FIND_NOTFOUND || !val) + if (res == Components.interfaces.nsITypeAheadFind.FIND_NOTFOUND || !val) { setFoundLink(null); - else + gCurrentWindow = null; + } else { setFoundLink(getBrowser().fastFind.foundLink); + gCurrentWindow = getBrowser().fastFind.currentWindow; + } } function find(val) diff --git a/mozilla/toolkit/components/typeaheadfind/public/nsITypeAheadFind.idl b/mozilla/toolkit/components/typeaheadfind/public/nsITypeAheadFind.idl index d1b5f3b3902..046219db031 100755 --- a/mozilla/toolkit/components/typeaheadfind/public/nsITypeAheadFind.idl +++ b/mozilla/toolkit/components/typeaheadfind/public/nsITypeAheadFind.idl @@ -20,7 +20,8 @@ * the Initial Developer. All Rights Reserved. * * Contributor(s): - * Original Author: Blake Ross (blake@cs.stanford.edu) + * Blake Ross (Original Author) + * Masayuki Nakano * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or @@ -42,7 +43,7 @@ interface nsIDocShell; -[scriptable, uuid(09ccaa1d-422c-48a8-93fe-9c6e03dc4746)] +[scriptable, uuid(376da416-e6b3-4bac-98f3-6aa408742751)] interface nsITypeAheadFind : nsISupports { void init(in nsIDocShell aDocShell); @@ -61,5 +62,6 @@ interface nsITypeAheadFind : nsISupports attribute boolean focusLinks; attribute boolean caseSensitive; readonly attribute nsIDOMElement foundLink; + readonly attribute nsIDOMWindow currentWindow; }; diff --git a/mozilla/toolkit/components/typeaheadfind/src/nsTypeAheadFind.cpp b/mozilla/toolkit/components/typeaheadfind/src/nsTypeAheadFind.cpp index 1d6d2c6665c..627451d0225 100755 --- a/mozilla/toolkit/components/typeaheadfind/src/nsTypeAheadFind.cpp +++ b/mozilla/toolkit/components/typeaheadfind/src/nsTypeAheadFind.cpp @@ -22,6 +22,7 @@ * Contributor(s): * Aaron Leventhal (aaronl@netscape.com) * Blake Ross (blake@cs.stanford.edu) + * Masayuki Nakano (masayuki@d-toybox.com) * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or @@ -83,7 +84,6 @@ #include "nsICaret.h" #include "nsIScriptGlobalObject.h" -#include "nsPIDOMWindow.h" #include "nsIDocShellTreeItem.h" #include "nsIWebNavigation.h" #include "nsIInterfaceRequestor.h" @@ -278,6 +278,7 @@ nsTypeAheadFind::FindItNow(nsIPresShell *aPresShell, { *aResult = FIND_NOTFOUND; mFoundLink = nsnull; + mCurrentWindow = nsnull; nsCOMPtr selection; nsCOMPtr selectionController; nsCOMPtr startingPresShell (GetPresShell()); @@ -439,6 +440,10 @@ nsTypeAheadFind::FindItNow(nsIPresShell *aPresShell, lastFocusedElement(do_QueryInterface(lastFocusedContent)); mFoundLink = lastFocusedElement; } + nsCOMPtr doc = + do_QueryInterface(presShell->GetDocument()); + NS_ASSERTION(doc, "Wow, presShell doesn't have document!"); + mCurrentWindow = do_QueryInterface(doc->GetScriptGlobalObject()); } if (hasWrapped) @@ -531,6 +536,15 @@ nsTypeAheadFind::GetFoundLink(nsIDOMElement** aFoundLink) return NS_OK; } +NS_IMETHODIMP +nsTypeAheadFind::GetCurrentWindow(nsIDOMWindow** aCurrentWindow) +{ + NS_ENSURE_ARG_POINTER(aCurrentWindow); + *aCurrentWindow = mCurrentWindow; + NS_IF_ADDREF(*aCurrentWindow); + return NS_OK; +} + NS_IMETHODIMP nsTypeAheadFind::GetFocusLinks(PRBool* aFocusLinks) { diff --git a/mozilla/toolkit/components/typeaheadfind/src/nsTypeAheadFind.h b/mozilla/toolkit/components/typeaheadfind/src/nsTypeAheadFind.h index 48c216d6fb8..736fecaed17 100755 --- a/mozilla/toolkit/components/typeaheadfind/src/nsTypeAheadFind.h +++ b/mozilla/toolkit/components/typeaheadfind/src/nsTypeAheadFind.h @@ -22,6 +22,7 @@ * Contributor(s): * Aaron Leventhal (aaronl@netscape.com) * Blake Ross (blake@cs.stanford.edu) + * Masayuki Nakano (masayuki@d-toybox.com) * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or @@ -125,6 +126,7 @@ protected: PRBool mCaretBrowsingOn; PRBool mFocusLinks; nsCOMPtr mFoundLink; + nsCOMPtr mCurrentWindow; PRPackedBool mLiteralTextSearchOnly; PRPackedBool mDontTryExactMatch; // mAllTheSame Char starts out PR_TRUE, becomes false when