Bug 363214. TakeFocus/grabFocus not working on document accessible. r=mats
authoraaronleventhal@moonset.net
Fri, 27 Apr 2007 08:15:19 -0700
changeset 883 0c29ef2fac3ae960c6043e8b088a9baf5e51714a
parent 882 eff37fef3035cc12ec94dce24c7f71bc98c70b26
child 884 63b74dd475a7a3b3bb19188ed0365af11d787548
push idunknown
push userunknown
push dateunknown
reviewersmats
bugs363214
milestone1.9a5pre
Bug 363214. TakeFocus/grabFocus not working on document accessible. r=mats
accessible/src/base/nsDocAccessible.cpp
--- a/accessible/src/base/nsDocAccessible.cpp
+++ b/accessible/src/base/nsDocAccessible.cpp
@@ -249,28 +249,37 @@ NS_IMETHODIMP nsDocAccessible::GetFocuse
   // be contained within the current document.
   nsCOMPtr<nsIAccessibilityService> accService =
     do_GetService("@mozilla.org/accessibilityService;1");
   return accService->GetAccessibleFor(gLastFocusedNode, aFocusedChild);
 }
 
 NS_IMETHODIMP nsDocAccessible::TakeFocus()
 {
-  nsCOMPtr<nsIDOMWindow> domWin;
-  GetWindow(getter_AddRefs(domWin));
-  nsCOMPtr<nsPIDOMWindow> privateDOMWindow(do_QueryInterface(domWin));
-  NS_ENSURE_TRUE(privateDOMWindow, NS_ERROR_FAILURE);
-  nsIFocusController *focusController =
-    privateDOMWindow->GetRootFocusController();
-  if (focusController) {
-    nsCOMPtr<nsIDOMElement> ele(do_QueryInterface(mDOMNode));
-    focusController->SetFocusedElement(ele);
-    return NS_OK;
+  NS_ENSURE_TRUE(mDocument, NS_ERROR_FAILURE);
+  PRUint32 state;
+  GetState(&state, nsnull);
+  if (0 == (state & nsIAccessibleStates::STATE_FOCUSABLE)) {
+    return NS_ERROR_FAILURE; // Not focusable
   }
-  return NS_ERROR_FAILURE;
+
+  nsCOMPtr<nsIDocShellTreeItem> treeItem = GetDocShellTreeItemFor(mDOMNode);
+  nsCOMPtr<nsIDocShell> docShell = do_QueryInterface(treeItem);
+  NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE);
+
+  nsCOMPtr<nsIPresShell> shell(GetPresShell());
+  nsIEventStateManager *esm = shell->GetPresContext()->EventStateManager();
+  NS_ENSURE_TRUE(esm, NS_ERROR_FAILURE);
+
+  // Focus the document
+  nsresult rv = docShell->SetHasFocus(PR_TRUE);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  // Clear out any existing focus state
+  return esm->SetContentState(nsnull, NS_EVENT_STATE_FOCUS);
 }
 
 // ------- nsIAccessibleDocument Methods (5) ---------------
 
 NS_IMETHODIMP nsDocAccessible::GetURL(nsAString& aURL)
 {
   if (!mDocument) {
     return NS_ERROR_FAILURE; // Document has been shut down