Back to index

lightning-sunbird  0.9+nobinonly
nsAccessible.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   John Gaunt (jgaunt@netscape.com)
00024  *   Aaron Leventhal (aaronl@netscape.com)
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsAccessible.h"
00041 #include "nsIAccessibleDocument.h"
00042 #include "nsIDocument.h"
00043 #include "nsIDOMNSDocument.h"
00044 #include "nsIImageDocument.h"
00045 #include "nsIPresShell.h"
00046 #include "nsPresContext.h"
00047 #include "nsIContent.h"
00048 #include "nsIFrame.h"
00049 #include "nsIScrollableView.h"
00050 #include "nsIViewManager.h"
00051 #include "nsIWidget.h"
00052 #include "nsIDOMDocumentView.h"
00053 #include "nsIDOMAbstractView.h"
00054 #include "nsIDOM3Node.h"
00055 #include "nsIDOMWindowInternal.h"
00056 #include "nsPIDOMWindow.h"
00057 #include "nsIDOMElement.h"
00058 #include "nsHTMLLinkAccessible.h"
00059 #include "nsISelection.h"
00060 #include "nsISelectionController.h"
00061 #include "nsIServiceManager.h"
00062 #include "nsXPIDLString.h"
00063 
00064 #include "nsIDOMComment.h"
00065 #include "nsITextContent.h"
00066 #include "nsIDOMHTMLImageElement.h"
00067 #include "nsIDOMHTMLInputElement.h"
00068 #include "nsIDOMHTMLBRElement.h"
00069 #include "nsIAtom.h"
00070 #include "nsGUIEvent.h"
00071 #include "nsIDocShellTreeItem.h"
00072 
00073 #include "nsIDOMHTMLInputElement.h"
00074 #include "nsIDOMXULSelectCntrlEl.h"
00075 #include "nsIDOMXULSelectCntrlItemEl.h"
00076 #include "nsIDOMHTMLObjectElement.h"
00077 #include "nsIDOMXULButtonElement.h"
00078 #include "nsIDOMXULCheckboxElement.h"
00079 #include "nsIDOMDocument.h"
00080 #include "nsIDOMDocumentXBL.h"
00081 #include "nsIDOMHTMLDocument.h"
00082 #include "nsIDOMXULDocument.h"
00083 #include "nsIDOMXULElement.h"
00084 #include "nsIDOMXULLabelElement.h"
00085 #include "nsIForm.h"
00086 #include "nsIFormControl.h"
00087 #include "nsIPrefService.h"
00088 #include "nsIPrefBranch.h"
00089 #include "nsIScriptGlobalObject.h"
00090 #include "nsIFocusController.h"
00091 #include "nsAccessibleTreeWalker.h"
00092 #include "nsIURI.h"
00093 #include "nsIImageLoadingContent.h"
00094 #include "nsITimer.h"
00095 #include "nsIDOMHTMLDocument.h"
00096 #include "nsArray.h"
00097 
00098 #ifdef NS_DEBUG
00099 #include "nsIFrameDebug.h"
00100 #include "nsIDOMCharacterData.h"
00101 #endif
00102 
00103 /*
00104  * Class nsAccessible
00105  */
00106 
00107 //-----------------------------------------------------
00108 // construction 
00109 //-----------------------------------------------------
00110 NS_IMPL_ADDREF_INHERITED(nsAccessible, nsAccessNode)
00111 NS_IMPL_RELEASE_INHERITED(nsAccessible, nsAccessNode)
00112 
00113 nsresult nsAccessible::QueryInterface(REFNSIID aIID, void** aInstancePtr)
00114 {
00115   // Custom-built QueryInterface() knows when we support nsIAccessibleSelectable
00116   // based on role attribute and waistate:multiselect
00117   *aInstancePtr = nsnull;
00118   
00119   if (aIID.Equals(NS_GET_IID(nsIAccessible))) {
00120     *aInstancePtr = NS_STATIC_CAST(nsIAccessible*, this);
00121     NS_ADDREF_THIS();
00122     return NS_OK;
00123   }
00124 
00125   if(aIID.Equals(NS_GET_IID(nsPIAccessible))) {
00126     *aInstancePtr = NS_STATIC_CAST(nsPIAccessible*, this);
00127     NS_ADDREF_THIS();
00128     return NS_OK;
00129   }
00130 
00131   if (aIID.Equals(NS_GET_IID(nsIAccessibleSelectable))) {
00132     nsCOMPtr<nsIContent> content(do_QueryInterface(mDOMNode));
00133     if (!content) {
00134       return NS_ERROR_FAILURE; // This accessible has been shut down
00135     }
00136     if (HasRoleAttribute(content)) {
00137       // If we have an XHTML role attribute present and the
00138       // waistate multiselect attribute not empty or false, then we need
00139       // to support nsIAccessibleSelectable
00140       // If either attribute (role or multiselect) change, then we'll
00141       // destroy this accessible so that we can follow COM identity rules.
00142       nsAutoString multiSelect;
00143       content->GetAttr(kNameSpaceID_WAIProperties,
00144                        nsAccessibilityAtoms::multiselect,
00145                        multiSelect);
00146       if (!multiSelect.IsEmpty() && !multiSelect.EqualsLiteral("false")) {
00147         *aInstancePtr = NS_STATIC_CAST(nsIAccessibleSelectable*, this);
00148         NS_ADDREF_THIS();
00149       }
00150     }
00151   }
00152 
00153   return nsAccessNode::QueryInterface(aIID, aInstancePtr);
00154 }
00155 
00156 nsAccessible::nsAccessible(nsIDOMNode* aNode, nsIWeakReference* aShell): nsAccessNodeWrap(aNode, aShell), 
00157   mParent(nsnull), mFirstChild(nsnull), mNextSibling(nsnull), mRoleMapEntry(nsnull),
00158   mAccChildCount(eChildCountUninitialized)
00159 {
00160 #ifdef NS_DEBUG_X
00161    {
00162      nsCOMPtr<nsIPresShell> shell(do_QueryReferent(aShell));
00163      printf(">>> %p Created Acc - Con: %p  Acc: %p  PS: %p", 
00164              (nsIAccessible*)this, aContent, aAccessible, shell.get());
00165      if (shell && aContent != nsnull) {
00166        nsIFrame* frame;
00167        shell->GetPrimaryFrameFor(aContent, &frame);
00168        char * name;
00169        if (GetNameForFrame(frame, &name)) {
00170          printf(" Name:[%s]", name);
00171          nsMemory::Free(name);
00172        }
00173      }
00174      printf("\n");
00175    }
00176 #endif
00177 }
00178 
00179 //-----------------------------------------------------
00180 // destruction
00181 //-----------------------------------------------------
00182 nsAccessible::~nsAccessible()
00183 {
00184 }
00185 
00186 NS_IMETHODIMP nsAccessible::GetName(nsAString& aName)
00187 {
00188   aName.Truncate();
00189   nsCOMPtr<nsIContent> content(do_QueryInterface(mDOMNode));
00190   if (!content) {
00191     return NS_ERROR_FAILURE;  // Node shut down
00192   }
00193 
00194   PRBool canAggregateName = mRoleMapEntry &&
00195                             mRoleMapEntry->nameRule == eNameOkFromChildren;
00196 
00197   if (content->IsContentOfType(nsIContent::eHTML)) {
00198     return GetHTMLName(aName, canAggregateName);
00199   }
00200 
00201   if (content->IsContentOfType(nsIContent::eXUL)) {
00202     return GetXULName(aName, canAggregateName);
00203   }
00204 
00205   return NS_OK;
00206 }
00207 
00208 NS_IMETHODIMP nsAccessible::GetDescription(nsAString& aDescription)
00209 {
00210   // There are 4 conditions that make an accessible have no accDescription:
00211   // 1. it's a text node; or
00212   // 2. It has no DHTML describedby property
00213   // 3. it doesn't have an accName; or
00214   // 4. its title attribute already equals to its accName nsAutoString name; 
00215   nsCOMPtr<nsIContent> content(do_QueryInterface(mDOMNode));
00216   if (!content) {
00217     return NS_ERROR_FAILURE;  // Node shut down
00218   }
00219   if (!content->IsContentOfType(nsIContent::eTEXT)) {
00220     nsAutoString description;
00221     nsresult rv = GetTextFromRelationID(nsAccessibilityAtoms::describedby, description);
00222     if (NS_FAILED(rv)) {
00223       PRBool isXUL = content->IsContentOfType(nsIContent::eXUL);
00224       if (isXUL) {
00225         // Try XUL <description control="[id]">description text</description>
00226         nsIContent *descriptionContent =
00227           GetXULLabelContent(content, nsAccessibilityAtoms::description);
00228         if (descriptionContent) {
00229           // We have a description content node
00230           AppendFlatStringFromSubtree(descriptionContent, &description);
00231         }
00232       }
00233       if (description.IsEmpty()) {
00234         nsIAtom *descAtom = isXUL ? nsAccessibilityAtoms::tooltiptext :
00235                                     nsAccessibilityAtoms::title;
00236         if (NS_CONTENT_ATTR_HAS_VALUE ==
00237             content->GetAttr(kNameSpaceID_None, descAtom, description)) {
00238           nsAutoString name;
00239           GetName(name);
00240           if (name.IsEmpty() || description == name) {
00241             // Don't use tooltip for a description if this object
00242             // has no name or the tooltip is the same as the name
00243             description.Truncate();
00244           }
00245         }
00246       }
00247     }
00248     description.CompressWhitespace();
00249     aDescription = description;
00250   }
00251 
00252   return NS_OK;
00253 }
00254 
00255 // mask values for ui.key.chromeAccess and ui.key.contentAccess
00256 #define NS_MODIFIER_SHIFT    1
00257 #define NS_MODIFIER_CONTROL  2
00258 #define NS_MODIFIER_ALT      4
00259 #define NS_MODIFIER_META     8
00260 
00261 // returns the accesskey modifier mask used in the given node's context
00262 // (i.e. chrome or content), or 0 if an error occurs
00263 static PRInt32
00264 GetAccessModifierMask(nsIDOMElement* aDOMNode)
00265 {
00266   nsCOMPtr<nsIPrefBranch> prefBranch =
00267     do_GetService(NS_PREFSERVICE_CONTRACTID);
00268   if (!prefBranch)
00269     return 0;
00270 
00271   // use ui.key.generalAccessKey (unless it is -1)
00272   PRInt32 accessKey;
00273   nsresult rv = prefBranch->GetIntPref("ui.key.generalAccessKey", &accessKey);
00274   if (NS_SUCCEEDED(rv) && accessKey != -1) {
00275     switch (accessKey) {
00276       case nsIDOMKeyEvent::DOM_VK_SHIFT:   return NS_MODIFIER_SHIFT;
00277       case nsIDOMKeyEvent::DOM_VK_CONTROL: return NS_MODIFIER_CONTROL;
00278       case nsIDOMKeyEvent::DOM_VK_ALT:     return NS_MODIFIER_ALT;
00279       case nsIDOMKeyEvent::DOM_VK_META:    return NS_MODIFIER_META;
00280       default:                             return 0;
00281     }
00282   }
00283 
00284   // get the docShell to this DOMNode, return 0 on failure
00285   nsCOMPtr<nsIContent> content(do_QueryInterface(aDOMNode));
00286   nsCOMPtr<nsIDocument> document = content->GetCurrentDoc();
00287   if (!document)
00288     return 0;
00289   nsCOMPtr<nsISupports> container = document->GetContainer();
00290   if (!container)
00291     return 0;
00292   nsCOMPtr<nsIDocShellTreeItem> treeItem(do_QueryInterface(container));
00293   if (!treeItem)
00294     return 0;
00295 
00296   // determine the access modifier used in this context
00297   PRInt32 itemType, accessModifierMask = 0;
00298   treeItem->GetItemType(&itemType);
00299   switch (itemType) {
00300 
00301   case nsIDocShellTreeItem::typeChrome:
00302     rv = prefBranch->GetIntPref("ui.key.chromeAccess", &accessModifierMask);
00303     break;
00304 
00305   case nsIDocShellTreeItem::typeContent:
00306     rv = prefBranch->GetIntPref("ui.key.contentAccess", &accessModifierMask);
00307     break;
00308   }
00309 
00310   return NS_SUCCEEDED(rv) ? accessModifierMask : 0;
00311 }
00312 
00313 NS_IMETHODIMP nsAccessible::GetKeyboardShortcut(nsAString& _retval)
00314 {
00315   nsCOMPtr<nsIDOMElement> elt(do_QueryInterface(mDOMNode));
00316   if (elt) {
00317     nsAutoString accesskey;
00318     elt->GetAttribute(NS_LITERAL_STRING("accesskey"), accesskey);
00319     if (accesskey.IsEmpty()) {
00320       nsCOMPtr<nsIContent> content = do_QueryInterface(elt);
00321       nsIContent *labelContent = GetLabelContent(content);
00322       if (labelContent) {
00323         labelContent->GetAttr(kNameSpaceID_None, nsAccessibilityAtoms::accesskey, accesskey);
00324       }
00325       if (accesskey.IsEmpty()) {
00326         return NS_ERROR_FAILURE;
00327       }
00328     }
00329 
00330     // append the modifiers in reverse order
00331     // (result: Control+Alt+Shift+Meta+<key>)
00332     nsAutoString propertyKey;
00333     PRInt32 modifierMask = GetAccessModifierMask(elt);
00334     if (modifierMask & NS_MODIFIER_META) {
00335       propertyKey.AssignLiteral("VK_META");
00336       nsAccessible::GetFullKeyName(propertyKey, accesskey, accesskey);
00337     }
00338     if (modifierMask & NS_MODIFIER_SHIFT) {
00339       propertyKey.AssignLiteral("VK_SHIFT");
00340       nsAccessible::GetFullKeyName(propertyKey, accesskey, accesskey);
00341     }
00342     if (modifierMask & NS_MODIFIER_ALT) {
00343       propertyKey.AssignLiteral("VK_ALT");
00344       nsAccessible::GetFullKeyName(propertyKey, accesskey, accesskey);
00345     }
00346     if (modifierMask & NS_MODIFIER_CONTROL) {
00347       propertyKey.AssignLiteral("VK_CONTROL");
00348       nsAccessible::GetFullKeyName(propertyKey, accesskey, accesskey);
00349     }
00350     _retval= accesskey;
00351     return NS_OK;
00352   }
00353   return NS_ERROR_FAILURE;
00354 }
00355 
00356 NS_IMETHODIMP nsAccessible::SetParent(nsIAccessible *aParent)
00357 {
00358   mParent = aParent;
00359   return NS_OK;
00360 }
00361 
00362 NS_IMETHODIMP nsAccessible::SetFirstChild(nsIAccessible *aFirstChild)
00363 {
00364   mFirstChild = aFirstChild;
00365   return NS_OK;
00366 }
00367 
00368 NS_IMETHODIMP nsAccessible::SetNextSibling(nsIAccessible *aNextSibling)
00369 {
00370   mNextSibling = aNextSibling? aNextSibling: DEAD_END_ACCESSIBLE;
00371   return NS_OK;
00372 }
00373 
00374 NS_IMETHODIMP nsAccessible::Init()
00375 {
00376   nsIContent *content = GetRoleContent(mDOMNode);
00377   nsAutoString roleString;
00378   if (content && GetRoleAttribute(content, roleString)) {
00379     // QI to nsIDOM3Node causes some overhead. Unfortunately we need to do this each
00380     // time there is a role attribute, because the prefixe to namespace mappings
00381     // can change within any subtree via the xmlns attribute
00382     nsCOMPtr<nsIDOM3Node> dom3Node(do_QueryInterface(content));
00383     if (dom3Node) {
00384       nsAutoString prefix;
00385       NS_NAMED_LITERAL_STRING(kWAIRoles_Namespace, "http://www.w3.org/2005/01/wai-rdf/GUIRoleTaxonomy#");
00386       dom3Node->LookupPrefix(kWAIRoles_Namespace, prefix);
00387       if (prefix.IsEmpty()) {
00388         // In HTML we are hardcoded to allow the exact prefix "wairole:" to 
00389         // always indicate that we are using the WAI roles. This allows DHTML accessibility
00390         // to be used within HTML
00391         nsCOMPtr<nsIDOMNSDocument> doc(do_QueryInterface(content->GetDocument()));
00392         if (doc) {
00393           nsAutoString mimeType;
00394           doc->GetContentType(mimeType);
00395           if (mimeType.EqualsLiteral("text/html")) {
00396             prefix = NS_LITERAL_STRING("wairole");
00397           }
00398         }
00399       }
00400       prefix += ':';
00401       PRUint32 length = prefix.Length();
00402       if (length > 1 && StringBeginsWith(roleString, prefix)) {
00403         roleString.Cut(0, length);
00404         nsCString utf8Role = NS_ConvertUCS2toUTF8(roleString); // For easy comparison
00405         ToLowerCase(utf8Role);
00406         PRUint32 index;
00407         for (index = 0; gWAIRoleMap[index].roleString; index ++) {
00408           if (utf8Role.Equals(gWAIRoleMap[index].roleString)) {
00409             break; // The dynamic role attribute maps to an entry in our table
00410           }
00411         }
00412         // Always use some entry if there is a role string
00413         // If no match, we use the last entry which maps to ROLE_NOTHING
00414         mRoleMapEntry = &gWAIRoleMap[index];
00415       }
00416     }
00417   }
00418 
00419   return nsAccessNodeWrap::Init();
00420 }
00421 
00422 nsIContent *nsAccessible::GetRoleContent(nsIDOMNode *aDOMNode)
00423 {
00424   // Given the DOM node for an acessible, return content node that
00425   // we should look at role string from
00426   // For non-document accessibles, this is the associated content node.
00427   // For doc accessibles, first try the <body> if it's HTML and there is
00428   // a role attribute used there.
00429   // For any other doc accessible , this is the document element.
00430   nsCOMPtr<nsIContent> content(do_QueryInterface(aDOMNode));
00431   if (!content) {
00432     nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(aDOMNode));
00433     if (domDoc) {
00434       nsCOMPtr<nsIDOMHTMLDocument> htmlDoc(do_QueryInterface(aDOMNode));
00435       if (htmlDoc) {
00436         nsCOMPtr<nsIDOMHTMLElement> bodyElement;
00437         htmlDoc->GetBody(getter_AddRefs(bodyElement));
00438         content = do_QueryInterface(bodyElement);
00439       }
00440       if (!content || !HasRoleAttribute(content)) {
00441         nsCOMPtr<nsIDOMElement> docElement;
00442         domDoc->GetDocumentElement(getter_AddRefs(docElement));
00443         content = do_QueryInterface(docElement);
00444       }
00445     }
00446   }
00447   return content;
00448 }
00449 
00450 NS_IMETHODIMP nsAccessible::Shutdown()
00451 {
00452   mNextSibling = nsnull;
00453   // Make sure none of it's children point to this parent
00454   if (mFirstChild) {
00455     nsCOMPtr<nsIAccessible> current(mFirstChild), next;
00456     while (current) {
00457       nsCOMPtr<nsPIAccessible> privateAcc(do_QueryInterface(current));
00458       current->GetNextSibling(getter_AddRefs(next));
00459       privateAcc->SetParent(nsnull);
00460       current = next;
00461     }
00462   }
00463   // Now invalidate the child count and pointers to other accessibles
00464   InvalidateChildren();
00465   if (mParent) {
00466     nsCOMPtr<nsPIAccessible> privateParent(do_QueryInterface(mParent));
00467     privateParent->InvalidateChildren();
00468     mParent = nsnull;
00469   }
00470 
00471   return nsAccessNodeWrap::Shutdown();
00472 }
00473 
00474 NS_IMETHODIMP nsAccessible::InvalidateChildren()
00475 {
00476   // Document has transformed, reset our invalid children and child count
00477   mAccChildCount = -1;
00478   mFirstChild = nsnull;
00479   return NS_OK;
00480 }
00481 
00482 NS_IMETHODIMP nsAccessible::GetParent(nsIAccessible **  aParent)
00483 {
00484   if (!mWeakShell) {
00485     // This node has been shut down
00486     *aParent = nsnull;
00487     return NS_ERROR_FAILURE;
00488   }
00489   if (mParent) {
00490     *aParent = mParent;
00491     NS_ADDREF(*aParent);
00492     return NS_OK;
00493   }
00494 
00495   *aParent = nsnull;
00496   // Last argument of PR_TRUE indicates to walk anonymous content
00497   nsAccessibleTreeWalker walker(mWeakShell, mDOMNode, PR_TRUE); 
00498   if (NS_SUCCEEDED(walker.GetParent())) {
00499     *aParent = walker.mState.accessible;
00500     SetParent(*aParent); // Cache it, unless perhaps accessible class overrides SetParent
00501     NS_ADDREF(*aParent);
00502   }
00503 
00504   return NS_OK;
00505 }
00506 
00507   /* readonly attribute nsIAccessible nextSibling; */
00508 NS_IMETHODIMP nsAccessible::GetNextSibling(nsIAccessible * *aNextSibling) 
00509 { 
00510   *aNextSibling = nsnull; 
00511   if (!mWeakShell) {
00512     // This node has been shut down
00513     return NS_ERROR_FAILURE;
00514   }
00515   if (!mParent) {
00516     nsCOMPtr<nsIAccessible> parent;
00517     GetParent(getter_AddRefs(parent));
00518     if (parent) {
00519       PRInt32 numChildren;
00520       parent->GetChildCount(&numChildren);  // Make sure we cache all of the children
00521     }
00522   }
00523 
00524   if (mNextSibling || !mParent) {
00525     // If no parent, don't try to calculate a new sibling
00526     // It either means we're at the root or shutting down the parent
00527     if (mNextSibling != DEAD_END_ACCESSIBLE) {
00528       NS_IF_ADDREF(*aNextSibling = mNextSibling);
00529     }
00530     return NS_OK;
00531   }
00532 
00533   return NS_ERROR_FAILURE;
00534 }
00535 
00536   /* readonly attribute nsIAccessible previousSibling; */
00537 NS_IMETHODIMP nsAccessible::GetPreviousSibling(nsIAccessible * *aPreviousSibling) 
00538 {
00539   *aPreviousSibling = nsnull;
00540 
00541   if (!mWeakShell) {
00542     // This node has been shut down
00543     return NS_ERROR_FAILURE;
00544   }
00545 
00546   nsCOMPtr<nsIAccessible> parent;
00547   if (NS_FAILED(GetParent(getter_AddRefs(parent)))) {
00548     return NS_ERROR_FAILURE;
00549   }
00550 
00551   nsCOMPtr<nsIAccessible> testAccessible, prevSibling;
00552   parent->GetFirstChild(getter_AddRefs(testAccessible));
00553   while (testAccessible && this != testAccessible) {
00554     prevSibling = testAccessible;
00555     prevSibling->GetNextSibling(getter_AddRefs(testAccessible));
00556   }
00557 
00558   if (!prevSibling) {
00559     return NS_ERROR_FAILURE;
00560   }
00561 
00562   NS_ADDREF(*aPreviousSibling = prevSibling);
00563   return NS_OK;
00564 }
00565 
00566   /* readonly attribute nsIAccessible firstChild; */
00567 NS_IMETHODIMP nsAccessible::GetFirstChild(nsIAccessible * *aFirstChild) 
00568 {  
00569   if (gIsCacheDisabled) {
00570     InvalidateChildren();
00571   }
00572   PRInt32 numChildren;
00573   GetChildCount(&numChildren);  // Make sure we cache all of the children
00574 
00575   NS_IF_ADDREF(*aFirstChild = mFirstChild);
00576 
00577   return NS_OK;  
00578 }
00579 
00580   /* readonly attribute nsIAccessible lastChild; */
00581 NS_IMETHODIMP nsAccessible::GetLastChild(nsIAccessible * *aLastChild)
00582 {  
00583   GetChildAt(-1, aLastChild);
00584   return NS_OK;
00585 }
00586 
00587 NS_IMETHODIMP nsAccessible::GetChildAt(PRInt32 aChildNum, nsIAccessible **aChild)
00588 {
00589   // aChildNum is a zero-based index
00590 
00591   PRInt32 numChildren;
00592   GetChildCount(&numChildren);
00593 
00594   // If no children or aChildNum is larger than numChildren, return null
00595   if (aChildNum >= numChildren || numChildren == 0 || !mWeakShell) {
00596     *aChild = nsnull;
00597     return NS_ERROR_FAILURE;
00598   // If aChildNum is less than zero, set aChild to last index
00599   } else if (aChildNum < 0) {
00600     aChildNum = numChildren - 1;
00601   }
00602 
00603   nsCOMPtr<nsIAccessible> current(mFirstChild), nextSibling;
00604   PRInt32 index = 0;
00605 
00606   while (current) {
00607     nextSibling = current;
00608     if (++index > aChildNum) {
00609       break;
00610     }
00611     nextSibling->GetNextSibling(getter_AddRefs(current));
00612   }
00613 
00614   NS_IF_ADDREF(*aChild = nextSibling);
00615 
00616   return NS_OK;
00617 }
00618 
00619 void nsAccessible::CacheChildren(PRBool aWalkAnonContent)
00620 {
00621   if (!mWeakShell) {
00622     // This node has been shut down
00623     mAccChildCount = -1;
00624     return;
00625   }
00626 
00627   if (mAccChildCount == eChildCountUninitialized) {
00628     nsAccessibleTreeWalker walker(mWeakShell, mDOMNode, aWalkAnonContent);
00629     // Seed the frame hint early while we're still on a container node.
00630     // This is better than doing the GetPrimaryFrameFor() later on
00631     // a text node, because text nodes aren't in the frame map.
00632     walker.mState.frame = GetFrame();
00633 
00634     nsCOMPtr<nsPIAccessible> privatePrevAccessible;
00635     mAccChildCount = 0;
00636     walker.GetFirstChild();
00637     SetFirstChild(walker.mState.accessible);
00638 
00639     while (walker.mState.accessible) {
00640       ++mAccChildCount;
00641       privatePrevAccessible = do_QueryInterface(walker.mState.accessible);
00642       privatePrevAccessible->SetParent(this);
00643       walker.GetNextSibling();
00644       privatePrevAccessible->SetNextSibling(walker.mState.accessible);
00645     }
00646   }
00647 }
00648 
00649 /* readonly attribute long childCount; */
00650 NS_IMETHODIMP nsAccessible::GetChildCount(PRInt32 *aAccChildCount) 
00651 {
00652   CacheChildren(PR_TRUE);
00653   *aAccChildCount = mAccChildCount;
00654   return NS_OK;  
00655 }
00656 
00657 /* readonly attribute long indexInParent; */
00658 NS_IMETHODIMP nsAccessible::GetIndexInParent(PRInt32 *aIndexInParent)
00659 {
00660   *aIndexInParent = -1;
00661   if (!mWeakShell) {
00662     return NS_ERROR_FAILURE;
00663   }
00664 
00665   nsCOMPtr<nsIAccessible> parent;
00666   GetParent(getter_AddRefs(parent));
00667   if (!parent) {
00668     return NS_ERROR_FAILURE;
00669   }
00670 
00671   nsCOMPtr<nsIAccessible> sibling;
00672   parent->GetFirstChild(getter_AddRefs(sibling));
00673   if (!sibling) {
00674     return NS_ERROR_FAILURE;
00675   }
00676 
00677   *aIndexInParent = 0;
00678   while (sibling != this) {
00679     NS_ASSERTION(sibling, "Never ran into the same child that we started from");
00680 
00681     if (!sibling)
00682       return NS_ERROR_FAILURE;
00683 
00684     ++*aIndexInParent;
00685     nsCOMPtr<nsIAccessible> tempAccessible;
00686     sibling->GetNextSibling(getter_AddRefs(tempAccessible));
00687     sibling = tempAccessible;
00688   }
00689 
00690   return NS_OK;
00691 }
00692 
00693 nsresult nsAccessible::GetTranslatedString(const nsAString& aKey, nsAString& aStringOut)
00694 {
00695   nsXPIDLString xsValue;
00696 
00697   if (!gStringBundle || 
00698     NS_FAILED(gStringBundle->GetStringFromName(PromiseFlatString(aKey).get(), getter_Copies(xsValue)))) 
00699     return NS_ERROR_FAILURE;
00700 
00701   aStringOut.Assign(xsValue);
00702   return NS_OK;
00703 }
00704 
00705 nsresult nsAccessible::GetFullKeyName(const nsAString& aModifierName, const nsAString& aKeyName, nsAString& aStringOut)
00706 {
00707   nsXPIDLString modifierName, separator;
00708 
00709   if (!gKeyStringBundle ||
00710       NS_FAILED(gKeyStringBundle->GetStringFromName(PromiseFlatString(aModifierName).get(), 
00711                                                     getter_Copies(modifierName))) ||
00712       NS_FAILED(gKeyStringBundle->GetStringFromName(PromiseFlatString(NS_LITERAL_STRING("MODIFIER_SEPARATOR")).get(), 
00713                                                     getter_Copies(separator)))) {
00714     return NS_ERROR_FAILURE;
00715   }
00716 
00717   aStringOut = modifierName + separator + aKeyName; 
00718   return NS_OK;
00719 }
00720 
00721 PRBool nsAccessible::IsPartiallyVisible(PRBool *aIsOffscreen) 
00722 {
00723   // We need to know if at least a kMinPixels around the object is visible
00724   // Otherwise it will be marked STATE_OFFSCREEN and STATE_INVISIBLE
00725   
00726   *aIsOffscreen = PR_FALSE;
00727 
00728   const PRUint16 kMinPixels  = 12;
00729    // Set up the variables we need, return false if we can't get at them all
00730   nsCOMPtr<nsIPresShell> shell(GetPresShell());
00731   if (!shell) 
00732     return PR_FALSE;
00733 
00734   nsIViewManager* viewManager = shell->GetViewManager();
00735   if (!viewManager)
00736     return PR_FALSE;
00737 
00738   nsIFrame *frame = GetFrame();
00739   if (!frame) {
00740     return PR_FALSE;
00741   }
00742 
00743   // If visibility:hidden or visibility:collapsed then mark with STATE_INVISIBLE
00744   if (!frame->GetStyleVisibility()->IsVisible())
00745   {
00746       return PR_FALSE;
00747   }
00748 
00749   nsPresContext *presContext = shell->GetPresContext();
00750   if (!presContext)
00751     return PR_FALSE;
00752 
00753   // Get the bounds of the current frame, relative to the current view.
00754   // We don't use the more accurate GetBoundsRect, because that is more expensive 
00755   // and the STATE_OFFSCREEN flag that this is used for only needs to be a rough indicator
00756 
00757   nsRect relFrameRect = frame->GetRect();
00758   nsPoint frameOffset;
00759   nsIView *containingView = frame->GetViewExternal();
00760   if (!containingView) {
00761     frame->GetOffsetFromView(frameOffset, &containingView);
00762     if (!containingView)
00763       return PR_FALSE;  // no view -- not visible
00764     relFrameRect.x = frameOffset.x;
00765     relFrameRect.y = frameOffset.y;
00766   }
00767 
00768   float p2t;
00769   p2t = presContext->PixelsToTwips();
00770   nsRectVisibility rectVisibility;
00771   viewManager->GetRectVisibility(containingView, relFrameRect, 
00772                                  NS_STATIC_CAST(PRUint16, (kMinPixels * p2t)), 
00773                                  &rectVisibility);
00774 
00775   if (rectVisibility == nsRectVisibility_kVisible ||
00776       (rectVisibility == nsRectVisibility_kZeroAreaRect && frame->GetNextInFlow())) {
00777     // This view says it is visible, but we need to check the parent view chain :(
00778     // Note: zero area rects can occur in the first frame of a multi-frame text flow,
00779     //       in which case the next frame exists because the text flow is visible
00780     while ((containingView = containingView->GetParent()) != nsnull) {
00781       if (containingView->GetVisibility() == nsViewVisibility_kHide) {
00782         return PR_FALSE;
00783       }
00784     }
00785     return PR_TRUE;
00786   }
00787 
00788   *aIsOffscreen = rectVisibility != nsRectVisibility_kZeroAreaRect;
00789   return PR_FALSE;
00790 }
00791 
00792 /* readonly attribute wstring state; */
00793 NS_IMETHODIMP nsAccessible::GetState(PRUint32 *aState) 
00794 { 
00795   *aState = 0;
00796 
00797   nsCOMPtr<nsIContent> content(do_QueryInterface(mDOMNode));
00798   if (!content) {
00799     return NS_ERROR_FAILURE;  // Node shut down
00800   }
00801 
00802   // Set STATE_UNAVAILABLE state based on disabled attribute
00803   // The disabled attribute is mostly used in XUL elements and HTML forms, but
00804   // if someone sets it on another attribute, 
00805   // it seems reasonable to consider it unavailable
00806   PRBool isDisabled;
00807   if (content->IsContentOfType(nsIContent::eHTML)) {
00808     // In HTML, just the presence of the disabled attribute means it is disabled,
00809     // therefore disabled="false" indicates disabled!
00810     isDisabled = content->HasAttr(kNameSpaceID_None, nsAccessibilityAtoms::disabled);
00811   }
00812   else {
00813     nsAutoString disabled;
00814     content->GetAttr(kNameSpaceID_None, nsAccessibilityAtoms::disabled, disabled);
00815     isDisabled = disabled.EqualsLiteral("true");
00816   }
00817   if (isDisabled) {
00818     *aState |= STATE_UNAVAILABLE;
00819   }
00820   else if (content->IsContentOfType(nsIContent::eELEMENT)) {
00821     if (!mRoleMapEntry) {
00822       // Default state for element accessible is focusable unless role is manually set
00823       // Subclasses of nsAccessible will clear focusable state if necessary
00824       *aState |= STATE_FOCUSABLE;
00825     }
00826     else {
00827       nsIFrame *frame = GetFrame();
00828       if (frame && frame->IsFocusable()) {
00829         *aState |= STATE_FOCUSABLE;
00830       }
00831     }
00832 
00833     if (gLastFocusedNode == mDOMNode) {
00834       *aState |= STATE_FOCUSED;
00835     }
00836   }
00837 
00838   // Check if STATE_OFFSCREEN bitflag should be turned on for this object
00839   PRBool isOffscreen;
00840   if (!IsPartiallyVisible(&isOffscreen)) {
00841     *aState |= STATE_INVISIBLE;
00842     if (isOffscreen)
00843       *aState |= STATE_OFFSCREEN;
00844   }
00845 
00846   return NS_OK;
00847 }
00848 
00849   /* readonly attribute boolean focusedChild; */
00850 NS_IMETHODIMP nsAccessible::GetFocusedChild(nsIAccessible **aFocusedChild) 
00851 { 
00852   nsCOMPtr<nsIAccessible> focusedChild;
00853   if (gLastFocusedNode == mDOMNode) {
00854     focusedChild = this;
00855   }
00856   else if (gLastFocusedNode) {
00857     nsCOMPtr<nsIAccessibilityService> accService =
00858       do_GetService("@mozilla.org/accessibilityService;1");
00859     NS_ENSURE_TRUE(accService, NS_ERROR_FAILURE);
00860     accService->GetAccessibleInWeakShell(gLastFocusedNode, mWeakShell, 
00861                                          getter_AddRefs(focusedChild));
00862     if (focusedChild) {
00863       nsCOMPtr<nsIAccessible> focusedParentAccessible;
00864       focusedChild->GetParent(getter_AddRefs(focusedParentAccessible));
00865       if (focusedParentAccessible != this) {
00866         focusedChild = nsnull;
00867       }
00868     }
00869   }
00870 
00871   NS_IF_ADDREF(*aFocusedChild = focusedChild);
00872   return NS_OK;
00873 }
00874 
00875   /* nsIAccessible getChildAtPoint (in long x, in long y); */
00876 NS_IMETHODIMP nsAccessible::GetChildAtPoint(PRInt32 tx, PRInt32 ty, nsIAccessible **aAccessible)
00877 {
00878   *aAccessible = nsnull;
00879   PRInt32 numChildren; // Make sure all children cached first
00880   GetChildCount(&numChildren);
00881 
00882   nsCOMPtr<nsIAccessible> child;
00883   GetFirstChild(getter_AddRefs(child));
00884 
00885   PRInt32 x, y, w, h;
00886   PRUint32 state;
00887 
00888   while (child) {
00889     child->GetBounds(&x, &y, &w, &h);
00890     if (tx >= x && tx < x + w && ty >= y && ty < y + h) {
00891       child->GetFinalState(&state);
00892       if ((state & (STATE_OFFSCREEN|STATE_INVISIBLE)) == 0) {   // Don't walk into offscreen items
00893         NS_ADDREF(*aAccessible = child);
00894         return NS_OK;
00895       }
00896     }
00897     nsCOMPtr<nsIAccessible> next;
00898     child->GetNextSibling(getter_AddRefs(next));
00899     child = next;
00900   }
00901 
00902   GetState(&state);
00903   GetBounds(&x, &y, &w, &h);
00904   if ((state & (STATE_OFFSCREEN|STATE_INVISIBLE)) == 0 &&
00905       tx >= x && tx < x + w && ty >= y && ty < y + h) {
00906     *aAccessible = this;
00907     NS_ADDREF_THIS();
00908     return NS_OK;
00909   }
00910   return NS_ERROR_FAILURE;
00911 }
00912 
00913 void nsAccessible::GetScreenOrigin(nsPresContext *aPresContext, nsIFrame *aFrame, nsRect *aRect)
00914 {
00915   aRect->x = aRect->y = 0;
00916 
00917   if (!aPresContext) {
00918     return;
00919   }
00920 
00921   nsPoint origin(0,0);
00922   nsIView *view = aFrame->GetViewExternal();
00923   if (!view) {
00924     aFrame->GetOffsetFromView(origin, &view);
00925     NS_ASSERTION(view, "Frame has no view");
00926   }
00927 
00928   nsPoint viewOrigin(0,0);
00929   nsIWidget *widget = view->GetNearestWidget(&viewOrigin);
00930   origin += viewOrigin;
00931 
00932   // Get the scale from that Presentation Context
00933   float t2p = aPresContext->TwipsToPixels();
00934 
00935   // Convert to pixels using that scale
00936   origin.x = NSTwipsToIntPixels(origin.x, t2p);
00937   origin.y = NSTwipsToIntPixels(origin.y, t2p);
00938   
00939   // Add the widget's screen coordinates to the offset we've counted
00940   NS_ASSERTION(widget, "No widget for top view");
00941   widget->WidgetToScreen(nsRect(origin.x, origin.y, 1, 1), *aRect);
00942 }
00943 
00944 void nsAccessible::GetBoundsRect(nsRect& aTotalBounds, nsIFrame** aBoundingFrame)
00945 {
00946 /*
00947  * This method is used to determine the bounds of a content node.
00948  * Because HTML wraps and links are not always rectangular, this
00949  * method uses the following algorithm:
00950  *
00951  * 1) Start with an empty rectangle
00952  * 2) Add the rect for the primary frame from for the DOM node.
00953  * 3) For each next frame at the same depth with the same DOM node, add that rect to total
00954  * 4) If that frame is an inline frame, search deeper at that point in the tree, adding all rects
00955  */
00956 
00957   // Initialization area
00958   *aBoundingFrame = nsnull;
00959   nsIFrame *firstFrame = GetBoundsFrame();
00960   if (!firstFrame)
00961     return;
00962 
00963   // Find common relative parent
00964   // This is an ancestor frame that will incompass all frames for this content node.
00965   // We need the relative parent so we can get absolute screen coordinates
00966   nsIFrame *ancestorFrame = firstFrame;
00967 
00968   while (ancestorFrame) {  
00969     *aBoundingFrame = ancestorFrame;
00970     // If any other frame type, we only need to deal with the primary frame
00971     // Otherwise, there may be more frames attached to the same content node
00972     if (!IsCorrectFrameType(ancestorFrame, nsAccessibilityAtoms::inlineFrame) &&
00973         !IsCorrectFrameType(ancestorFrame, nsAccessibilityAtoms::textFrame))
00974       break;
00975     ancestorFrame = ancestorFrame->GetParent();
00976   }
00977 
00978   nsIFrame *iterFrame = firstFrame;
00979   nsCOMPtr<nsIContent> firstContent(do_QueryInterface(mDOMNode));
00980   nsIContent* iterContent = firstContent;
00981   PRInt32 depth = 0;
00982 
00983   // Look only at frames below this depth, or at this depth (if we're still on the content node we started with)
00984   while (iterContent == firstContent || depth > 0) {
00985     // Coordinates will come back relative to parent frame
00986     nsRect currFrameBounds = iterFrame->GetRect();
00987     
00988     // Make this frame's bounds relative to common parent frame
00989     currFrameBounds +=
00990       iterFrame->GetParent()->GetOffsetToExternal(*aBoundingFrame);
00991 
00992     // Add this frame's bounds to total
00993     aTotalBounds.UnionRect(aTotalBounds, currFrameBounds);
00994 
00995     nsIFrame *iterNextFrame = nsnull;
00996 
00997     if (IsCorrectFrameType(iterFrame, nsAccessibilityAtoms::inlineFrame)) {
00998       // Only do deeper bounds search if we're on an inline frame
00999       // Inline frames can contain larger frames inside of them
01000       iterNextFrame = iterFrame->GetFirstChild(nsnull);
01001     }
01002 
01003     if (iterNextFrame) 
01004       ++depth;  // Child was found in code above this: We are going deeper in this iteration of the loop
01005     else {  
01006       // Use next sibling if it exists, or go back up the tree to get the first next-in-flow or next-sibling 
01007       // within our search
01008       while (iterFrame) {
01009         iterNextFrame = iterFrame->GetNextInFlow();
01010         if (!iterNextFrame)
01011           iterNextFrame = iterFrame->GetNextSibling();
01012         if (iterNextFrame || --depth < 0) 
01013           break;
01014         iterFrame = iterFrame->GetParent();
01015       }
01016     }
01017 
01018     // Get ready for the next round of our loop
01019     iterFrame = iterNextFrame;
01020     if (iterFrame == nsnull)
01021       break;
01022     iterContent = nsnull;
01023     if (depth == 0)
01024       iterContent = iterFrame->GetContent();
01025   }
01026 }
01027 
01028 
01029 /* void getBounds (out long x, out long y, out long width, out long height); */
01030 NS_IMETHODIMP nsAccessible::GetBounds(PRInt32 *x, PRInt32 *y, PRInt32 *width, PRInt32 *height)
01031 {
01032   // This routine will get the entire rectange for all the frames in this node
01033   // -------------------------------------------------------------------------
01034   //      Primary Frame for node
01035   //  Another frame, same node                <- Example
01036   //  Another frame, same node
01037 
01038   nsPresContext *presContext = GetPresContext();
01039   if (!presContext)
01040   {
01041     *x = *y = *width = *height = 0;
01042     return NS_ERROR_FAILURE;
01043   }
01044 
01045   float t2p;
01046   t2p = presContext->TwipsToPixels();   // Get pixels to twips conversion factor
01047 
01048   nsRect unionRectTwips;
01049   nsIFrame* aBoundingFrame = nsnull;
01050   GetBoundsRect(unionRectTwips, &aBoundingFrame);   // Unions up all primary frames for this node and all siblings after it
01051   if (!aBoundingFrame) {
01052     *x = *y = *width = *height = 0;
01053     return NS_ERROR_FAILURE;
01054   }
01055 
01056   *x      = NSTwipsToIntPixels(unionRectTwips.x, t2p); 
01057   *y      = NSTwipsToIntPixels(unionRectTwips.y, t2p);
01058   *width  = NSTwipsToIntPixels(unionRectTwips.width, t2p);
01059   *height = NSTwipsToIntPixels(unionRectTwips.height, t2p);
01060 
01061   // We have the union of the rectangle, now we need to put it in absolute screen coords
01062 
01063   nsRect orgRectPixels, pageRectPixels;
01064   GetScreenOrigin(presContext, aBoundingFrame, &orgRectPixels);
01065   *x += orgRectPixels.x;
01066   *y += orgRectPixels.y;
01067 
01068   return NS_OK;
01069 }
01070 
01071 // helpers
01072 
01078 PRBool nsAccessible::IsCorrectFrameType( nsIFrame* aFrame, nsIAtom* aAtom ) 
01079 {
01080   NS_ASSERTION(aFrame != nsnull, "aFrame is null in call to IsCorrectFrameType!");
01081   NS_ASSERTION(aAtom != nsnull, "aAtom is null in call to IsCorrectFrameType!");
01082 
01083   return aFrame->GetType() == aAtom;
01084 }
01085 
01086 
01087 nsIFrame* nsAccessible::GetBoundsFrame()
01088 {
01089   return GetFrame();
01090 }
01091 
01092 already_AddRefed<nsIAccessible>
01093 nsAccessible::GetMultiSelectFor(nsIDOMNode *aNode)
01094 {
01095   NS_ENSURE_TRUE(aNode, nsnull);
01096   nsCOMPtr<nsIAccessibilityService> accService =
01097     do_GetService("@mozilla.org/accessibilityService;1");
01098   NS_ENSURE_TRUE(accService, nsnull);
01099   nsCOMPtr<nsIAccessible> accessible;
01100   accService->GetAccessibleFor(aNode, getter_AddRefs(accessible));
01101   if (!accessible) {
01102     return nsnull;
01103   }
01104 
01105   PRUint32 state;
01106   accessible->GetFinalState(&state);
01107   if (0 == (state & STATE_SELECTABLE)) {
01108     return nsnull;
01109   }
01110 
01111   PRUint32 containerRole;
01112   while (0 == (state & STATE_MULTISELECTABLE)) {
01113     nsIAccessible *current = accessible;
01114     current->GetParent(getter_AddRefs(accessible));
01115     if (!accessible || (NS_SUCCEEDED(accessible->GetFinalRole(&containerRole)) &&
01116                         containerRole == ROLE_PANE)) {
01117       return nsnull;
01118     }
01119     accessible->GetFinalState(&state);
01120   }
01121   nsIAccessible *returnAccessible = nsnull;
01122   accessible.swap(returnAccessible);
01123   return returnAccessible;
01124 }
01125 
01126 nsresult nsAccessible::SetNonTextSelection(PRBool aSelect)
01127 {
01128   nsCOMPtr<nsIAccessible> multiSelect = GetMultiSelectFor(mDOMNode);
01129   if (!multiSelect) {
01130     return aSelect ? TakeFocus() : NS_ERROR_FAILURE;
01131   }
01132   nsCOMPtr<nsIContent> content(do_QueryInterface(mDOMNode));
01133   NS_ASSERTION(content, "Called for dead accessible");
01134 
01135   // For DHTML widgets use WAI namespace
01136   PRUint32 nameSpaceID = mRoleMapEntry ? kNameSpaceID_WAIProperties : kNameSpaceID_None;
01137   if (aSelect) {
01138     return content->SetAttr(nameSpaceID, nsAccessibilityAtoms::selected, NS_LITERAL_STRING("true"), PR_TRUE);
01139   }
01140   return content->UnsetAttr(nameSpaceID, nsAccessibilityAtoms::selected, PR_TRUE);
01141 }
01142 
01143 /* void removeSelection (); */
01144 NS_IMETHODIMP nsAccessible::RemoveSelection()
01145 {
01146   if (!mDOMNode) {
01147     return NS_ERROR_FAILURE;
01148   }
01149 
01150   PRUint32 state;
01151   GetFinalState(&state);
01152   if (state & STATE_SELECTABLE) {
01153     return SetNonTextSelection(PR_TRUE);
01154   }
01155 
01156   nsCOMPtr<nsISelectionController> control(do_QueryReferent(mWeakShell));
01157   if (!control) {
01158     return NS_ERROR_FAILURE;  
01159   }
01160 
01161   nsCOMPtr<nsISelection> selection;
01162   nsresult rv = control->GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(selection));
01163   if (NS_FAILED(rv))
01164     return rv;
01165 
01166   nsCOMPtr<nsIDOMNode> parent;
01167   rv = mDOMNode->GetParentNode(getter_AddRefs(parent));
01168   if (NS_FAILED(rv))
01169     return rv;
01170 
01171   rv = selection->Collapse(parent, 0);
01172   if (NS_FAILED(rv))
01173     return rv;
01174 
01175   return NS_OK;
01176 }
01177 
01178 /* void takeSelection (); */
01179 NS_IMETHODIMP nsAccessible::TakeSelection()
01180 {
01181   if (!mDOMNode) {
01182     return NS_ERROR_FAILURE;
01183   }
01184 
01185   PRUint32 state;
01186   GetFinalState(&state);
01187   if (state & STATE_SELECTABLE) {
01188     return SetNonTextSelection(PR_TRUE);
01189   }
01190 
01191   nsCOMPtr<nsISelectionController> control(do_QueryReferent(mWeakShell));
01192   if (!control) {
01193     return NS_ERROR_FAILURE;  
01194   }
01195  
01196   nsCOMPtr<nsISelection> selection;
01197   nsresult rv = control->GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(selection));
01198   if (NS_FAILED(rv))
01199     return rv;
01200 
01201   nsCOMPtr<nsIDOMNode> parent;
01202   rv = mDOMNode->GetParentNode(getter_AddRefs(parent));
01203   if (NS_FAILED(rv))
01204     return rv;
01205 
01206   PRInt32 offsetInParent = 0;
01207   nsCOMPtr<nsIDOMNode> child;
01208   rv = parent->GetFirstChild(getter_AddRefs(child));
01209   if (NS_FAILED(rv))
01210     return rv;
01211 
01212   nsCOMPtr<nsIDOMNode> next; 
01213 
01214   while(child)
01215   {
01216     if (child == mDOMNode) {
01217       // Collapse selection to just before desired element,
01218       rv = selection->Collapse(parent, offsetInParent);
01219       if (NS_FAILED(rv))
01220         return rv;
01221 
01222       // then extend it to just after
01223       rv = selection->Extend(parent, offsetInParent+1);
01224       return rv;
01225     }
01226 
01227      child->GetNextSibling(getter_AddRefs(next));
01228      child = next;
01229      offsetInParent++;
01230   }
01231 
01232   // didn't find a child
01233   return NS_ERROR_FAILURE;
01234 }
01235 
01236 /* void takeFocus (); */
01237 NS_IMETHODIMP nsAccessible::TakeFocus()
01238 { 
01239   nsCOMPtr<nsIContent> content(do_QueryInterface(mDOMNode));
01240   if (!content) {
01241     return NS_ERROR_FAILURE;
01242   }
01243   content->SetFocus(GetPresContext());
01244 
01245   return NS_OK;
01246 }
01247 
01248 nsresult nsAccessible::AppendStringWithSpaces(nsAString *aFlatString, const nsAString& textEquivalent)
01249 {
01250   // Insert spaces to insure that words from controls aren't jammed together
01251   if (!textEquivalent.IsEmpty()) {
01252     if (!aFlatString->IsEmpty())
01253       aFlatString->Append(PRUnichar(' '));
01254     aFlatString->Append(textEquivalent);
01255     aFlatString->Append(PRUnichar(' '));
01256   }
01257   return NS_OK;
01258 }
01259 
01260 nsresult nsAccessible::AppendNameFromAccessibleFor(nsIContent *aContent,
01261                                                    nsAString *aFlatString,
01262                                                    PRBool aFromValue)
01263 {
01264   nsAutoString textEquivalent, value;
01265 
01266   nsCOMPtr<nsIDOMNode> domNode(do_QueryInterface(aContent));
01267   nsCOMPtr<nsIAccessible> accessible;
01268   if (domNode == mDOMNode) {
01269     accessible = this;
01270   }
01271   else {
01272     nsCOMPtr<nsIAccessibilityService> accService =
01273       do_GetService("@mozilla.org/accessibilityService;1");
01274     NS_ENSURE_TRUE(accService, NS_ERROR_FAILURE);
01275     accService->GetAccessibleInWeakShell(domNode, mWeakShell, getter_AddRefs(accessible));
01276   }
01277   if (accessible) {
01278     if (aFromValue) {
01279       accessible->GetFinalValue(textEquivalent);
01280     }
01281     else {
01282       accessible->GetName(textEquivalent);
01283     }
01284   }
01285 
01286   textEquivalent.CompressWhitespace();
01287   return AppendStringWithSpaces(aFlatString, textEquivalent);
01288 }
01289 
01290 /*
01291  * AppendFlatStringFromContentNode and AppendFlatStringFromSubtree
01292  *
01293  * This method will glean useful text, in whatever form it exists, from any content node given to it.
01294  * It is used by any decendant of nsAccessible that needs to get text from a single node, as
01295  * well as by nsAccessible::AppendFlatStringFromSubtree, which gleans and concatenates text from any node and
01296  * that node's decendants.
01297  */
01298 
01299 nsresult nsAccessible::AppendFlatStringFromContentNode(nsIContent *aContent, nsAString *aFlatString)
01300 {
01301   if (aContent->IsContentOfType(nsIContent::eTEXT)) {
01302     nsCOMPtr<nsITextContent> textContent(do_QueryInterface(aContent));
01303     NS_ASSERTION(textContent, "No text content for text content type");
01304     // If it's a text node, append the text
01305     PRBool isHTMLBlock = PR_FALSE;
01306     nsCOMPtr<nsIPresShell> shell = GetPresShell();
01307     if (!shell) {
01308       return NS_ERROR_FAILURE;  
01309     }
01310 
01311     nsIContent *parentContent = aContent->GetParent();
01312     nsCOMPtr<nsIContent> appendedSubtreeStart(do_QueryInterface(mDOMNode));
01313     if (parentContent && parentContent != appendedSubtreeStart) {
01314       nsIFrame *frame;
01315       shell->GetPrimaryFrameFor(parentContent, &frame);
01316       if (frame) {
01317         // If this text is inside a block level frame (as opposed to span level), we need to add spaces around that 
01318         // block's text, so we don't get words jammed together in final name
01319         // Extra spaces will be trimmed out later
01320         const nsStyleDisplay* display = frame->GetStyleDisplay();
01321         if (display->IsBlockLevel() ||
01322           display->mDisplay == NS_STYLE_DISPLAY_TABLE_CELL) {
01323           isHTMLBlock = PR_TRUE;
01324           if (!aFlatString->IsEmpty()) {
01325             aFlatString->Append(PRUnichar(' '));
01326           }
01327         }
01328       }
01329     }
01330     if (textContent->TextLength() > 0) {
01331       nsAutoString text;
01332       textContent->AppendTextTo(text);
01333       if (!text.IsEmpty())
01334         aFlatString->Append(text);
01335       if (isHTMLBlock && !aFlatString->IsEmpty())
01336         aFlatString->Append(PRUnichar(' '));
01337     }
01338     return NS_OK;
01339   }
01340 
01341   nsAutoString textEquivalent;
01342   if (!aContent->IsContentOfType(nsIContent::eHTML)) {
01343     if (aContent->IsContentOfType(nsIContent::eXUL)) {
01344       nsCOMPtr<nsIPresShell> shell = GetPresShell();
01345       if (!shell) {
01346         return NS_ERROR_FAILURE;  
01347       }
01348       nsIFrame *frame;
01349       shell->GetPrimaryFrameFor(aContent, &frame);
01350       if (!frame || !frame->GetStyleVisibility()->IsVisible()) {
01351         return NS_OK;
01352       }
01353  
01354       nsCOMPtr<nsIDOMXULLabeledControlElement> labeledEl(do_QueryInterface(aContent));
01355       if (labeledEl) {
01356         labeledEl->GetLabel(textEquivalent);
01357       }
01358       else {
01359         aContent->GetAttr(kNameSpaceID_None,
01360                           nsAccessibilityAtoms::tooltiptext, textEquivalent);
01361       }
01362       AppendNameFromAccessibleFor(aContent, &textEquivalent, PR_TRUE /* use value */);
01363 
01364       return AppendStringWithSpaces(aFlatString, textEquivalent);
01365     }
01366     return NS_OK; // Not HTML and not XUL -- we don't handle it yet
01367   }
01368 
01369   nsCOMPtr<nsIAtom> tag = aContent->Tag();
01370   if (tag == nsAccessibilityAtoms::img) {
01371     return AppendNameFromAccessibleFor(aContent, aFlatString);
01372   }
01373 
01374   if (tag == nsAccessibilityAtoms::input) {
01375     nsAutoString inputType;
01376     aContent->GetAttr(kNameSpaceID_None, nsAccessibilityAtoms::type, inputType);
01377     if (inputType.LowerCaseEqualsLiteral("button") ||
01378         inputType.LowerCaseEqualsLiteral("submit") ||
01379         inputType.LowerCaseEqualsLiteral("reset") ||
01380         inputType.LowerCaseEqualsLiteral("image")) {
01381       return AppendNameFromAccessibleFor(aContent, aFlatString);
01382     }
01383   }
01384 
01385   if (tag == nsAccessibilityAtoms::object && !aContent->GetChildCount()) {
01386     // If object has no alternative content children, try title
01387     aContent->GetAttr(kNameSpaceID_None, nsAccessibilityAtoms::title, textEquivalent);
01388   }
01389   else if (tag == nsAccessibilityAtoms::br) {
01390     // If it's a line break, insert a space so that words aren't jammed together
01391     aFlatString->AppendLiteral("\r\n");
01392     return NS_OK;
01393   }
01394   else if (tag != nsAccessibilityAtoms::a && tag != nsAccessibilityAtoms::area) { 
01395     AppendNameFromAccessibleFor(aContent, aFlatString, PR_TRUE /* use value */);
01396   }
01397 
01398   textEquivalent.CompressWhitespace();
01399   return AppendStringWithSpaces(aFlatString, textEquivalent);
01400 }
01401 
01402 
01403 nsresult nsAccessible::AppendFlatStringFromSubtree(nsIContent *aContent, nsAString *aFlatString)
01404 {
01405   nsresult rv = AppendFlatStringFromSubtreeRecurse(aContent, aFlatString);
01406   if (NS_SUCCEEDED(rv) && !aFlatString->IsEmpty()) {
01407     nsAString::const_iterator start, end;
01408     aFlatString->BeginReading(start);
01409     aFlatString->EndReading(end);
01410 
01411     PRInt32 spacesToTruncate = 0;
01412     while (-- end != start && *end == ' ')
01413       ++ spacesToTruncate;
01414 
01415     if (spacesToTruncate > 0)
01416       aFlatString->Truncate(aFlatString->Length() - spacesToTruncate);
01417   }
01418 
01419   return rv;
01420 }
01421 
01422 nsresult nsAccessible::AppendFlatStringFromSubtreeRecurse(nsIContent *aContent, nsAString *aFlatString)
01423 {
01424   // Depth first search for all text nodes that are decendants of content node.
01425   // Append all the text into one flat string
01426   PRUint32 numChildren = 0;
01427   nsCOMPtr<nsIDOMXULSelectControlElement> selectControlEl(do_QueryInterface(aContent));
01428   if (!selectControlEl) {  // Don't walk children of elements with options, just get label directly
01429     numChildren = aContent->GetChildCount();
01430   }
01431 
01432   if (numChildren == 0) {
01433     // There are no children or they are irrelvant: get the text from the current node
01434     AppendFlatStringFromContentNode(aContent, aFlatString);
01435     return NS_OK;
01436   }
01437 
01438   // There are relevant children: use them to get the text.
01439   PRUint32 index;
01440   for (index = 0; index < numChildren; index++) {
01441     AppendFlatStringFromSubtreeRecurse(aContent->GetChildAt(index), aFlatString);
01442   }
01443   return NS_OK;
01444 }
01445 
01446 nsIContent *nsAccessible::GetLabelContent(nsIContent *aForNode)
01447 {
01448   return aForNode->IsContentOfType(nsIContent::eXUL) ? GetXULLabelContent(aForNode) :
01449                                                        GetHTMLLabelContent(aForNode);
01450 }
01451  
01452 nsIContent* nsAccessible::GetXULLabelContent(nsIContent *aForNode, nsIAtom *aLabelType)
01453 {
01454   nsAutoString controlID;
01455   nsIContent *labelContent = GetContentPointingTo(&controlID, aForNode, nsnull,
01456                                                   kNameSpaceID_None, aLabelType);
01457   if (labelContent) {
01458     return labelContent;
01459   }
01460 
01461   // If we're in anonymous content, determine whether we should use
01462   // the binding parent based on where the id for this control is
01463   aForNode->GetAttr(kNameSpaceID_None, nsAccessibilityAtoms::id, controlID);
01464   if (controlID.IsEmpty()) {
01465     // If no control ID and we're anonymous content
01466     // get ID from parent that inserted us.
01467     aForNode = aForNode->GetBindingParent();
01468     if (aForNode) {
01469       aForNode->GetAttr(kNameSpaceID_None, nsAccessibilityAtoms::id, controlID);
01470     }
01471     if (controlID.IsEmpty()) {
01472       return nsnull;
01473     }
01474   }
01475   
01476   // Look for label in subtrees of nearby ancestors
01477   static const PRUint32 kAncestorLevelsToSearch = 5;
01478   PRUint32 count = 0;
01479   while (!labelContent && ++count <= kAncestorLevelsToSearch && 
01480          (aForNode = aForNode->GetParent()) != nsnull) {
01481     labelContent = GetContentPointingTo(&controlID, aForNode,
01482                                         nsAccessibilityAtoms::control,
01483                                         kNameSpaceID_None, aLabelType);
01484   }
01485 
01486   return labelContent;
01487 }
01488 
01489 nsIContent* nsAccessible::GetHTMLLabelContent(nsIContent *aForNode)
01490 {
01491   nsIContent *walkUpContent = aForNode;
01492 
01493   // go up tree get name of ancestor label if there is one. Don't go up farther than form element
01494   while ((walkUpContent = walkUpContent->GetParent()) != nsnull) {
01495     nsIAtom *tag = walkUpContent->Tag();
01496     if (tag == nsAccessibilityAtoms::label) {
01497       return walkUpContent;
01498     }
01499     if (tag == nsAccessibilityAtoms::form ||
01500         tag == nsAccessibilityAtoms::body) {
01501       // Reached top ancestor in form
01502       // There can be a label targeted at this control using the 
01503       // for="control_id" attribute. To save computing time, only 
01504       // look for those inside of a form element
01505       nsAutoString forId;
01506       aForNode->GetAttr(kNameSpaceID_None, nsAccessibilityAtoms::id, forId);
01507       // Actually we'll be walking down the content this time, with a depth first search
01508       if (forId.IsEmpty()) {
01509         break;
01510       }
01511       return GetContentPointingTo(&forId, walkUpContent, nsAccessibilityAtoms::_for); 
01512     }
01513   }
01514 
01515   return nsnull;
01516 }
01517 
01518 nsresult nsAccessible::GetTextFromRelationID(nsIAtom *aIDAttrib, nsString &aName)
01519 {
01520   // Get DHTML name from content subtree pointed to by ID attribute
01521   aName.Truncate();
01522   nsCOMPtr<nsIContent> content(do_QueryInterface(mDOMNode));
01523   NS_ASSERTION(content, "Called from shutdown accessible");
01524 
01525   nsAutoString ids;
01526   if (NS_CONTENT_ATTR_NOT_THERE ==
01527       content->GetAttr(kNameSpaceID_WAIProperties, aIDAttrib, ids)) {
01528     return NS_ERROR_FAILURE;
01529   }
01530   ids.CompressWhitespace(PR_TRUE, PR_TRUE);
01531 
01532   nsCOMPtr<nsIDOMDocument> domDoc;
01533   mDOMNode->GetOwnerDocument(getter_AddRefs(domDoc));
01534   NS_ENSURE_TRUE(domDoc, NS_ERROR_FAILURE);
01535   
01536   nsresult rv = NS_ERROR_FAILURE;
01537 
01538   // Support idlist as in aaa::labelledby="id1 id2 id3"
01539   while (PR_TRUE) {
01540     nsAutoString id;
01541     PRInt32 idLength = ids.FindChar(' ');  // -1 means end of string, not space found
01542     NS_ASSERTION(idLength != 0, "Should not be 0 because of CompressWhitespace() call above");
01543     id = Substring(ids, 0, idLength);
01544     if (idLength == -1) {
01545       ids.Truncate();
01546     }
01547     else {
01548       ids.Cut(0, idLength + 1);
01549     }
01550     if (!id.IsEmpty() && id.Last() == ' ') {
01551       id.Truncate(idLength - 1);  // Eliminate trailing space if it's there
01552     }
01553 
01554     if (id.IsEmpty()) {
01555       break;
01556     }
01557     if (!aName.IsEmpty()) {
01558       aName += ' '; // Need whitespace between multiple labels or descriptions
01559     }
01560     nsCOMPtr<nsIDOMElement> labelElement;
01561     domDoc->GetElementById(id, getter_AddRefs(labelElement));
01562     content = do_QueryInterface(labelElement);
01563     if (!content) {
01564       return NS_OK;
01565     }
01566     // We have a label content
01567     rv = AppendFlatStringFromSubtree(content, &aName);
01568     if (NS_SUCCEEDED(rv)) {
01569       aName.CompressWhitespace();
01570     }
01571   }
01572   
01573   return rv;
01574 }
01575 
01576 // Pass in aForAttrib == nsnull if any <label> will do
01577 nsIContent *nsAccessible::GetContentPointingTo(const nsAString *aId,
01578                                                nsIContent *aLookContent,
01579                                                nsIAtom *aForAttrib,
01580                                                PRUint32 aForAttribNameSpace,
01581                                                nsIAtom *aTagType)
01582 {
01583   if (!aTagType || aLookContent->Tag() == aTagType) {
01584     if (aForAttrib) {
01585       nsAutoString labelIsFor;
01586       aLookContent->GetAttr(aForAttribNameSpace, aForAttrib, labelIsFor);
01587       if (labelIsFor.Equals(*aId)) {
01588         return aLookContent;
01589       }
01590     }
01591     if (aTagType) {
01592       return nsnull;
01593     }
01594   }
01595 
01596   // Recursively search descendents for labels
01597   PRUint32 count  = 0;
01598   nsIContent *child;
01599 
01600   while ((child = aLookContent->GetChildAt(count++)) != nsnull) {
01601     nsIContent *labelContent = GetContentPointingTo(aId, child, aForAttrib,
01602                                                     aForAttribNameSpace, aTagType);
01603     if (labelContent) {
01604       return labelContent;
01605     }
01606   }
01607   return nsnull;
01608 }
01609 
01615 nsresult nsAccessible::GetHTMLName(nsAString& aLabel, PRBool aCanAggregateSubtree)
01616 {
01617   nsCOMPtr<nsIContent> content(do_QueryInterface(mDOMNode));
01618   if (!content) {
01619     return NS_ERROR_FAILURE;   // Node shut down
01620   }
01621 
01622   // Check for DHTML accessibility labelledby relationship property
01623   nsAutoString label;
01624   nsresult rv = GetTextFromRelationID(nsAccessibilityAtoms::labelledby, label);
01625   if (NS_SUCCEEDED(rv)) {
01626     aLabel = label;
01627     return rv;
01628   }
01629 
01630   nsIContent *labelContent = GetHTMLLabelContent(content);
01631   if (labelContent) {
01632     AppendFlatStringFromSubtree(labelContent, &label);
01633     label.CompressWhitespace();
01634     if (!label.IsEmpty()) {
01635       aLabel = label;
01636       return NS_OK;
01637     }
01638   }
01639 
01640   if (aCanAggregateSubtree) {
01641     // Don't use AppendFlatStringFromSubtree for container widgets like menulist
01642     nsresult rv = AppendFlatStringFromSubtree(content, &aLabel);
01643     if (NS_SUCCEEDED(rv)) {
01644       return NS_OK;
01645     }
01646   }
01647 
01648   // Still try the title as as fallback method in that case.
01649   if (NS_CONTENT_ATTR_NOT_THERE ==
01650       content->GetAttr(kNameSpaceID_None, nsAccessibilityAtoms::title, aLabel)) {
01651     aLabel.SetIsVoid(PR_TRUE);
01652   }
01653   return NS_OK;
01654 }
01655 
01668 nsresult nsAccessible::GetXULName(nsAString& aLabel, PRBool aCanAggregateSubtree)
01669 {
01670   nsCOMPtr<nsIContent> content(do_QueryInterface(mDOMNode));
01671   NS_ASSERTION(content, "No nsIContent for DOM node");
01672 
01673   // First check for label override via accessibility labelledby relationship
01674   nsAutoString label;
01675   nsresult rv = GetTextFromRelationID(nsAccessibilityAtoms::labelledby, label);
01676   if (NS_SUCCEEDED(rv)) {
01677     aLabel = label;
01678     return rv;
01679   }
01680 
01681   // CASE #1 (via label attribute) -- great majority of the cases
01682   nsCOMPtr<nsIDOMXULLabeledControlElement> labeledEl(do_QueryInterface(mDOMNode));
01683   if (labeledEl) {
01684     rv = labeledEl->GetLabel(label);
01685   }
01686   else {
01687     nsCOMPtr<nsIDOMXULSelectControlItemElement> itemEl(do_QueryInterface(mDOMNode));
01688     if (itemEl) {
01689       rv = itemEl->GetLabel(label);
01690     }
01691     else {
01692       nsCOMPtr<nsIDOMXULSelectControlElement> select(do_QueryInterface(mDOMNode));
01693       // Use label if this is not a select control element which 
01694       // uses label attribute to indicate which option is selected
01695       if (!select) {
01696         nsCOMPtr<nsIDOMXULElement> xulEl(do_QueryInterface(mDOMNode));
01697         if (xulEl) {
01698           rv = xulEl->GetAttribute(NS_LITERAL_STRING("label"), label);
01699         }
01700       }
01701     }
01702   }
01703 
01704   // CASES #2 and #3 ------ label as a child or <label control="id" ... > </label>
01705   if (NS_FAILED(rv) || label.IsEmpty()) {
01706     label.Truncate();
01707     nsIContent *labelContent = GetXULLabelContent(content);
01708     nsCOMPtr<nsIDOMXULLabelElement> xulLabel(do_QueryInterface(labelContent));
01709     // Check if label's value attribute is used
01710     if (xulLabel && NS_SUCCEEDED(xulLabel->GetValue(label)) && label.IsEmpty()) {
01711       // If no value attribute, a non-empty label must contain
01712       // children that define it's text -- possibly using HTML
01713       AppendFlatStringFromSubtree(labelContent, &label);
01714     }
01715   }
01716 
01717   // XXX If CompressWhiteSpace worked on nsAString we could avoid a copy
01718   label.CompressWhitespace();
01719   if (!label.IsEmpty()) {
01720     aLabel = label;
01721     return NS_OK;
01722   }
01723 
01724   content->GetAttr(kNameSpaceID_None, nsAccessibilityAtoms::tooltiptext, label);
01725   label.CompressWhitespace();
01726   if (!label.IsEmpty()) {
01727     aLabel = label;
01728     return NS_OK;
01729   }
01730 
01731   // Can get text from title of <toolbaritem> if we're a child of a <toolbaritem>
01732   nsIContent *bindingParent = content->GetBindingParent();
01733   nsIContent *parent = bindingParent? bindingParent->GetParent() :
01734                                       content->GetParent();
01735   while (parent) {
01736     if (parent->Tag() == nsAccessibilityAtoms::toolbaritem &&
01737         parent->HasAttr(kNameSpaceID_None, nsAccessibilityAtoms::title)) {
01738       parent->GetAttr(kNameSpaceID_None, nsAccessibilityAtoms::title, label);
01739       label.CompressWhitespace();
01740       aLabel = label;
01741       return NS_OK;
01742     }
01743     parent = parent->GetParent();
01744   }
01745 
01746   // Don't use AppendFlatStringFromSubtree for container widgets like menulist
01747   return aCanAggregateSubtree? AppendFlatStringFromSubtree(content, &aLabel) : NS_OK;
01748 }
01749 
01750 NS_IMETHODIMP nsAccessible::FireToolkitEvent(PRUint32 aEvent, nsIAccessible *aTarget, void * aData)
01751 {
01752   if (!mWeakShell)
01753     return NS_ERROR_FAILURE; // Don't fire event for accessible that has been shut down
01754   nsCOMPtr<nsIAccessibleDocument> docAccessible(GetDocAccessible());
01755   nsCOMPtr<nsPIAccessible> eventHandlingAccessible(do_QueryInterface(docAccessible));
01756   if (eventHandlingAccessible) {
01757     return eventHandlingAccessible->FireToolkitEvent(aEvent, aTarget, aData);
01758   }
01759 
01760   return NS_ERROR_FAILURE;
01761 }
01762 
01763 nsRoleMapEntry nsAccessible::gWAIRoleMap[] = 
01764 {
01765   // This list of WAI-defined roles are currently hardcoded.
01766   // Eventually we will most likely be loading an RDF resource that contains this information
01767   // Using RDF will also allow for role extensibility. See bug 280138.
01768   // XXX Should we store attribute names in this table as atoms instead of strings?
01769   // Definition of nsRoleMapEntry and nsStateMapEntry contains comments explaining this table.
01770   {"alert", ROLE_ALERT, eNameOkFromChildren, eNoValue, eNoReqStates, END_ENTRY},
01771   {"alertdialog", ROLE_ALERT, eNameOkFromChildren, eNoValue, eNoReqStates, END_ENTRY},
01772   {"application", ROLE_APPLICATION, eNameLabelOrTitle, eNoValue, eNoReqStates, END_ENTRY},
01773   {"button", ROLE_PUSHBUTTON, eNameOkFromChildren, eNoValue, eNoReqStates,
01774             {"pressed", BOOL_STATE, STATE_PRESSED},
01775             {"haspopup", BOOL_STATE, STATE_HASPOPUP}, END_ENTRY},
01776   {"buttonsubmit", ROLE_PUSHBUTTON, eNameOkFromChildren, eNoValue, STATE_DEFAULT, END_ENTRY},
01777   {"buttoncancel", ROLE_PUSHBUTTON, eNameOkFromChildren, eNoValue, eNoReqStates, END_ENTRY},
01778   {"checkbox", ROLE_CHECKBUTTON, eNameOkFromChildren, eNoValue, STATE_CHECKABLE,
01779             {"checked", BOOL_STATE, STATE_CHECKED},
01780             {"readonly", BOOL_STATE, STATE_READONLY}, END_ENTRY},
01781   {"checkboxtristate", ROLE_CHECKBUTTON, eNameOkFromChildren, eNoValue, STATE_CHECKABLE,
01782             {"checked", BOOL_STATE, STATE_CHECKED},
01783             {"checked", "mixed", STATE_MIXED},
01784             {"readonly", BOOL_STATE, STATE_READONLY}, END_ENTRY},
01785   {"columnheader", ROLE_COLUMNHEADER, eNameOkFromChildren, eNoValue, eNoReqStates,
01786             {"selected", BOOL_STATE, STATE_SELECTED | STATE_SELECTABLE},
01787             {"selected", "false", STATE_SELECTABLE},
01788             {"readonly", BOOL_STATE, STATE_READONLY}, END_ENTRY},
01789   {"combobox", ROLE_COMBOBOX, eNameLabelOrTitle, eHasValueMinMax, eNoReqStates,
01790             {"readonly", BOOL_STATE, STATE_READONLY},
01791             {"multiselect", BOOL_STATE, STATE_MULTISELECTABLE | STATE_EXTSELECTABLE}, END_ENTRY},
01792   {"description", ROLE_STATICTEXT, eNameOkFromChildren, eNoValue, eNoReqStates, END_ENTRY},
01793   {"dialog", ROLE_DIALOG, eNameLabelOrTitle, eNoValue, eNoReqStates, END_ENTRY},
01794   {"document", ROLE_DOCUMENT, eNameLabelOrTitle, eNoValue, eNoReqStates, END_ENTRY},
01795   {"label", ROLE_STATICTEXT, eNameOkFromChildren, eNoValue, eNoReqStates, END_ENTRY},
01796   {"list", ROLE_LIST, eNameLabelOrTitle, eNoValue, eNoReqStates,
01797             {"readonly", BOOL_STATE, STATE_READONLY},
01798             {"multiselect", BOOL_STATE, STATE_MULTISELECTABLE | STATE_EXTSELECTABLE}, END_ENTRY},
01799   {"listbox", ROLE_LIST, eNameLabelOrTitle, eNoValue, eNoReqStates,
01800             {"readonly", BOOL_STATE, STATE_READONLY},
01801             {"multiselect", BOOL_STATE, STATE_MULTISELECTABLE | STATE_EXTSELECTABLE}, END_ENTRY},
01802   {"listitem", ROLE_LISTITEM, eNameOkFromChildren, eNoValue, eNoReqStates,
01803             {"selected", BOOL_STATE, STATE_SELECTED | STATE_SELECTABLE},
01804             {"selected", "false", STATE_SELECTABLE},
01805             {"checked", BOOL_STATE, STATE_CHECKED | STATE_CHECKABLE},
01806             {"checked", "false", STATE_CHECKABLE}, END_ENTRY},
01807   {"menu", ROLE_MENUPOPUP, eNameLabelOrTitle, eNoValue, eNoReqStates, END_ENTRY},
01808   {"menubar", ROLE_MENUBAR, eNameLabelOrTitle, eNoValue, eNoReqStates, END_ENTRY},
01809   {"menuitem", ROLE_MENUITEM, eNameOkFromChildren, eNoValue, eNoReqStates,
01810             {"haspopup", BOOL_STATE, STATE_HASPOPUP},
01811             {"checked", BOOL_STATE, STATE_CHECKED | STATE_CHECKABLE},
01812             {"checked", "mixed", STATE_MIXED},
01813             {"checked", "false", STATE_CHECKABLE}, END_ENTRY},
01814   {"menuitemcheckbox", ROLE_MENUITEM, eNameOkFromChildren, eNoValue, STATE_CHECKABLE,
01815             {"checked", BOOL_STATE, STATE_CHECKED }, END_ENTRY},
01816   {"menuitemradio", ROLE_MENUITEM, eNameOkFromChildren, eNoValue, STATE_CHECKABLE,
01817             {"checked", BOOL_STATE, STATE_CHECKED }, END_ENTRY},
01818   {"grid", ROLE_TABLE, eNameLabelOrTitle, eNoValue, STATE_FOCUSABLE,
01819             {"multiselectable", BOOL_STATE, STATE_MULTISELECTABLE | STATE_EXTSELECTABLE},
01820             {"readonly", BOOL_STATE, STATE_READONLY}, END_ENTRY},
01821   {"gridcell", ROLE_CELL, eNameOkFromChildren, eNoValue, eNoReqStates,
01822             {"selected", BOOL_STATE, STATE_SELECTED | STATE_SELECTABLE},
01823             {"selected", "false", STATE_SELECTABLE},
01824             {"readonly", BOOL_STATE, STATE_READONLY}, END_ENTRY},
01825   {"group", ROLE_GROUPING, eNameLabelOrTitle, eNoValue, eNoReqStates, END_ENTRY},
01826   {"link", ROLE_LINK, eNameLabelOrTitle, eNoValue, STATE_LINKED, END_ENTRY},
01827   {"option", ROLE_LISTITEM, eNameOkFromChildren, eNoValue, eNoReqStates,
01828             {"selected", BOOL_STATE, STATE_SELECTED | STATE_SELECTABLE},
01829             {"selected", "false", STATE_SELECTABLE},
01830             {"checked", BOOL_STATE, STATE_CHECKED | STATE_CHECKABLE},
01831             {"checked", "false", STATE_CHECKABLE}, END_ENTRY},
01832   {"progressbar", ROLE_PROGRESSBAR, eNameLabelOrTitle, eHasValueMinMax, STATE_READONLY,
01833             {"valuenow", "unknown", STATE_MIXED}, END_ENTRY},
01834   {"radio", ROLE_RADIOBUTTON, eNameOkFromChildren, eNoValue, eNoReqStates,
01835             {"checked", BOOL_STATE, STATE_CHECKED}, END_ENTRY},
01836   {"radiogroup", ROLE_GROUPING, eNameLabelOrTitle, eNoValue, eNoReqStates, END_ENTRY},
01837   {"rowheader", ROLE_ROWHEADER, eNameOkFromChildren, eNoValue, eNoReqStates,
01838             {"selected", BOOL_STATE, STATE_SELECTED | STATE_SELECTABLE},
01839             {"selected", "false", STATE_SELECTABLE},
01840             {"readonly", BOOL_STATE, STATE_READONLY}, END_ENTRY},
01841   {"secret", ROLE_PASSWORD_TEXT, eNameLabelOrTitle, eNoValue, STATE_PROTECTED,
01842              END_ENTRY},  // EXT_STATE_SINGLE_LINE manually supported in code
01843   {"separator", ROLE_SEPARATOR, eNameLabelOrTitle, eNoValue, eNoReqStates, END_ENTRY},
01844   {"slider", ROLE_SLIDER, eNameLabelOrTitle, eHasValueMinMax, eNoReqStates,
01845             {"readonly", BOOL_STATE, STATE_READONLY}, END_ENTRY},
01846   {"spinbutton", ROLE_SPINBUTTON, eNameLabelOrTitle, eHasValueMinMax, eNoReqStates,
01847             {"readonly", BOOL_STATE, STATE_READONLY}, END_ENTRY},
01848   {"spreadsheet", ROLE_TABLE, eNameLabelOrTitle, eNoValue, STATE_MULTISELECTABLE | STATE_EXTSELECTABLE | STATE_FOCUSABLE,
01849             {"readonly", BOOL_STATE, STATE_READONLY}, END_ENTRY},  // Still supported, but deprecated in favor of grid
01850   {"table", ROLE_TABLE, eNameLabelOrTitle, eNoValue, eNoReqStates, END_ENTRY},
01851   {"td", ROLE_CELL, eNameLabelOrTitle, eNoValue, eNoReqStates, END_ENTRY},
01852   {"th", ROLE_CELL, eNameLabelOrTitle, eNoValue, eNoReqStates, END_ENTRY},
01853   {"tab", ROLE_PAGETAB, eNameOkFromChildren, eNoValue, eNoReqStates, END_ENTRY},
01854   {"tablist", ROLE_PAGETABLIST, eNameLabelOrTitle, eNoValue, eNoReqStates, END_ENTRY},
01855   {"tabpanel", ROLE_PROPERTYPAGE, eNameLabelOrTitle, eNoValue, eNoReqStates, END_ENTRY},
01856   {"textarea", ROLE_TEXT, eNameLabelOrTitle, eHasValueMinMax, eNoReqStates,
01857             {"readonly", BOOL_STATE, STATE_READONLY}, END_ENTRY}, // XXX EXT_STATE_MULTI_LINE supported in code
01858   {"textfield", ROLE_TEXT, eNameLabelOrTitle, eHasValueMinMax, eNoReqStates,
01859             {"readonly", BOOL_STATE, STATE_READONLY}, 
01860             {"haspopup", BOOL_STATE, STATE_HASPOPUP}, END_ENTRY}, // XXX EXT_STATE_SINGLE_LINE supported in code
01861   {"toolbar", ROLE_TOOLBAR, eNameLabelOrTitle, eNoValue, eNoReqStates, END_ENTRY},
01862   {"tree", ROLE_OUTLINE, eNameLabelOrTitle, eNoValue, eNoReqStates,
01863             {"readonly", BOOL_STATE, STATE_READONLY},
01864             {"multiselectable", BOOL_STATE, STATE_MULTISELECTABLE | STATE_EXTSELECTABLE}, END_ENTRY},
01865   {"treeitem", ROLE_OUTLINEITEM, eNameOkFromChildren, eNoValue, eNoReqStates,
01866             {"selected", BOOL_STATE, STATE_SELECTED | STATE_SELECTABLE},
01867             {"selected", "false", STATE_SELECTABLE},
01868             {"expanded", BOOL_STATE, STATE_EXPANDED},
01869             {"expanded", "false", STATE_COLLAPSED},
01870             {"checked", BOOL_STATE, STATE_CHECKED | STATE_CHECKABLE},
01871             {"checked", "mixed", STATE_MIXED},
01872             {"checked", "false", STATE_CHECKABLE},},
01873   {nsnull, ROLE_NOTHING, eNameLabelOrTitle, eNoValue, eNoReqStates, END_ENTRY} // Last item
01874 };
01875 
01876 // XHTML 2 roles
01877 // These don't need a mapping - they are exposed either through DOM or via MSAA role string
01878 // banner, contentinfo, main, navigation, note, search, secondary, seealso
01879 
01880 nsStateMapEntry nsAccessible::gUnivStateMap[] = {
01881   {"disabled", BOOL_STATE, STATE_UNAVAILABLE},
01882   {"required", BOOL_STATE, STATE_REQUIRED},
01883   {"invalid", BOOL_STATE, STATE_INVALID}
01884 };
01885 
01886 NS_IMETHODIMP nsAccessible::GetFinalRole(PRUint32 *aRole)
01887 {
01888   if (mRoleMapEntry) {
01889     *aRole = mRoleMapEntry->role;
01890     if (*aRole != ROLE_NOTHING) {
01891       return NS_OK;
01892     }
01893   }
01894   return mDOMNode ? GetRole(aRole) : NS_ERROR_FAILURE;  // Node already shut down
01895 }
01896 
01897 PRBool nsAccessible::MappedAttrState(nsIContent *aContent, PRUint32 *aStateInOut,
01898                                      nsStateMapEntry *aStateMapEntry)
01899 {
01900   // Return true if we should continue
01901   if (!aStateMapEntry->attributeName) {
01902     return PR_FALSE;  // Stop looking -- no more states
01903   }
01904 
01905   nsAutoString attribValue;
01906   nsCOMPtr<nsIAtom> attribAtom = do_GetAtom(aStateMapEntry->attributeName); // XXX put atoms directly in entry
01907   if (NS_CONTENT_ATTR_HAS_VALUE == aContent->GetAttr(kNameSpaceID_WAIProperties,
01908                                                      attribAtom,
01909                                                      attribValue)) {
01910     if (aStateMapEntry->attributeValue == BOOL_STATE) {
01911       // No attribute value map specified in state map entry indicates state cleared
01912       if (attribValue.EqualsLiteral("false")) {
01913         return *aStateInOut &= ~aStateMapEntry->state;
01914       }
01915       return *aStateInOut |= aStateMapEntry->state;
01916     }
01917     if (NS_ConvertUTF16toUTF8(attribValue).Equals(aStateMapEntry->attributeValue)) {
01918       return *aStateInOut |= aStateMapEntry->state;
01919     }
01920   }
01921 
01922   return PR_TRUE;
01923 }
01924 
01925 NS_IMETHODIMP nsAccessible::GetFinalState(PRUint32 *aState)
01926 {
01927   *aState = 0;
01928   if (!mDOMNode) {
01929     return NS_ERROR_FAILURE;  // Node already shut down
01930   }
01931   nsresult rv = GetState(aState);
01932   if (NS_FAILED(rv)) {
01933     return rv;
01934   }
01935 
01936   // Test for universal states first
01937   nsIContent *content = GetRoleContent(mDOMNode);
01938   if (!content) {
01939     return NS_ERROR_FAILURE;  // Node already shut down
01940   }
01941   for (PRInt32 index = 0; index < NS_ARRAY_LENGTH(gUnivStateMap); index ++) {
01942     MappedAttrState(content, aState, &gUnivStateMap[index]);
01943   }
01944   if (!mRoleMapEntry) {
01945     return rv;
01946   }
01947 
01948   PRUint32 finalState = *aState;
01949   finalState &= ~STATE_READONLY;  // Once DHTML role is used, we're only readonly if DHTML readonly used
01950 
01951   if (finalState & STATE_UNAVAILABLE) {
01952     // Disabled elements are not selectable or focusable, even if disabled
01953     // via DHTML accessibility disabled property
01954     finalState &= ~(STATE_SELECTABLE | STATE_FOCUSABLE);
01955   }
01956 
01957   finalState |= mRoleMapEntry->state;
01958   if (MappedAttrState(content, &finalState, &mRoleMapEntry->attributeMap1) &&
01959       MappedAttrState(content, &finalState, &mRoleMapEntry->attributeMap2) &&
01960       MappedAttrState(content, &finalState, &mRoleMapEntry->attributeMap3) &&
01961       MappedAttrState(content, &finalState, &mRoleMapEntry->attributeMap4) &&
01962       MappedAttrState(content, &finalState, &mRoleMapEntry->attributeMap5) &&
01963       MappedAttrState(content, &finalState, &mRoleMapEntry->attributeMap6)) {
01964     MappedAttrState(content, &finalState, &mRoleMapEntry->attributeMap7);
01965   }
01966 
01967   *aState = finalState;
01968   return rv;
01969 }
01970 
01971 NS_IMETHODIMP nsAccessible::GetFinalValue(nsAString& aValue)
01972 {
01973   if (!mDOMNode) {
01974     return NS_ERROR_FAILURE;  // Node already shut down
01975   }
01976   if (mRoleMapEntry) {
01977     if (mRoleMapEntry->valueRule == eNoValue) {
01978       return NS_OK;
01979     }
01980     nsCOMPtr<nsIContent> content(do_QueryInterface(mDOMNode));
01981     if (content &&
01982         NS_CONTENT_ATTR_HAS_VALUE == content->GetAttr(kNameSpaceID_WAIProperties,
01983                                                       nsAccessibilityAtoms::valuenow,
01984                                                       aValue)) {
01985       return NS_OK;
01986     }
01987   }
01988   return GetValue(aValue);
01989 }
01990 
01991 // Not implemented by this class
01992 
01993 /* DOMString getValue (); */
01994 NS_IMETHODIMP nsAccessible::GetValue(nsAString& _retval)
01995 {
01996   return NS_ERROR_NOT_IMPLEMENTED;
01997 }
01998 
01999 /* void setName (in DOMString name); */
02000 NS_IMETHODIMP nsAccessible::SetName(const nsAString& name)
02001 {
02002   return NS_ERROR_NOT_IMPLEMENTED;
02003 }
02004 
02005 /* DOMString getKeyBinding (); */
02006 NS_IMETHODIMP nsAccessible::GetKeyBinding(nsAString& _retval)
02007 {
02008   return NS_ERROR_NOT_IMPLEMENTED;
02009 }
02010 
02011 /* unsigned long getRole (); */
02012 NS_IMETHODIMP nsAccessible::GetRole(PRUint32 *aRole)
02013 {
02014   *aRole = ROLE_NOTHING;
02015   return NS_OK;
02016 }
02017 
02018 /* PRUint8 getAccNumActions (); */
02019 NS_IMETHODIMP nsAccessible::GetNumActions(PRUint8 *aNumActions)
02020 {
02021   *aNumActions = 0;
02022   return NS_OK;
02023 }
02024 
02025 /* DOMString getAccActionName (in PRUint8 index); */
02026 NS_IMETHODIMP nsAccessible::GetActionName(PRUint8 index, nsAString& aName)
02027 {
02028   return NS_ERROR_FAILURE;
02029 }
02030 
02031 /* void doAction (in PRUint8 index); */
02032 NS_IMETHODIMP nsAccessible::DoAction(PRUint8 index)
02033 {
02034   return NS_ERROR_FAILURE;
02035 }
02036 
02037 /* DOMString getHelp (); */
02038 NS_IMETHODIMP nsAccessible::GetHelp(nsAString& _retval)
02039 {
02040   return NS_ERROR_NOT_IMPLEMENTED;
02041 }
02042 
02043 /* nsIAccessible getAccessibleToRight(); */
02044 NS_IMETHODIMP nsAccessible::GetAccessibleToRight(nsIAccessible **_retval)
02045 {
02046   return NS_ERROR_NOT_IMPLEMENTED;
02047 }
02048 
02049 /* nsIAccessible getAccessibleToLeft(); */
02050 NS_IMETHODIMP nsAccessible::GetAccessibleToLeft(nsIAccessible **_retval)
02051 {
02052   return NS_ERROR_NOT_IMPLEMENTED;
02053 }
02054 
02055 /* nsIAccessible getAccessibleAbove(); */
02056 NS_IMETHODIMP nsAccessible::GetAccessibleAbove(nsIAccessible **_retval)
02057 {
02058   return NS_ERROR_NOT_IMPLEMENTED;
02059 }
02060 
02061 /* nsIAccessible getAccessibleBelow(); */
02062 NS_IMETHODIMP nsAccessible::GetAccessibleBelow(nsIAccessible **_retval)
02063 {
02064   return NS_ERROR_NOT_IMPLEMENTED;
02065 }
02066 
02067 already_AddRefed<nsIDOMNode> nsAccessible::GetInverseRelatedNode(nsIAtom *aRelationAttr,
02068                                                                  PRUint32 aAncestorLevelsToSearch)
02069 {
02070   // aAncestorLevelsToSearch is an optimization used for label and description searches.
02071   // We expect the control to be relatively near the label/description in the DOM tree,
02072   // so to optimize we don't search the entire DOM tree.
02073   nsIContent *content = GetRoleContent(mDOMNode);
02074   if (!content) {
02075     return nsnull; // Node shut down
02076   }
02077   nsAutoString controlID;
02078   content->GetAttr(kNameSpaceID_None, nsAccessibilityAtoms::id, controlID);
02079   if (controlID.IsEmpty()) {
02080     return nsnull;
02081   }
02082   nsIDOMNode *relatedNode = nsnull;
02083   // Something might be pointing to us
02084   PRUint32 count = 0;
02085   nsIContent *start = content;
02086   while ((!aAncestorLevelsToSearch || ++count <= aAncestorLevelsToSearch) && 
02087         (start = start->GetParent()) != nsnull) {
02088     nsIContent *description = GetContentPointingTo(&controlID, start,
02089                                                    aRelationAttr,
02090                                                    kNameSpaceID_WAIProperties,
02091                                                    nsnull);
02092     if (description) {
02093       nsIDOMNode *relatedNode;
02094       CallQueryInterface(description, &relatedNode);
02095       return relatedNode;
02096     }
02097   }
02098   return nsnull;
02099 }
02100 
02101 
02102 /* nsIAccessible getAccessibleRelated(); */
02103 NS_IMETHODIMP nsAccessible::GetAccessibleRelated(PRUint32 aRelationType, nsIAccessible **aRelated)
02104 {
02105   *aRelated = nsnull;
02106 
02107   // Relationships are defined on the same content node
02108   // that the role would be defined on
02109   nsIContent *content = GetRoleContent(mDOMNode);
02110   if (!content) {
02111     return NS_ERROR_FAILURE;  // Node already shut down
02112   }
02113 
02114   nsCOMPtr<nsIDOMNode> relatedNode;
02115   nsAutoString relatedID;
02116 
02117   // Search for the related DOM node according to the specified "relation type"
02118   switch (aRelationType)
02119   {
02120   case RELATION_LABEL_FOR:
02121     {
02122       if (content->Tag() == nsAccessibilityAtoms::label) {
02123         nsIAtom *relatedIDAttr = content->IsContentOfType(nsIContent::eHTML) ?
02124           nsAccessibilityAtoms::_for : nsAccessibilityAtoms::control;
02125         content->GetAttr(kNameSpaceID_None, relatedIDAttr, relatedID);
02126       }
02127       if (relatedID.IsEmpty()) {
02128         const PRUint32 kAncestorLevelsToSearch = 3;
02129         relatedNode = GetInverseRelatedNode(nsAccessibilityAtoms::labelledby, kAncestorLevelsToSearch);
02130       }
02131       break;
02132     }
02133   case RELATION_LABELLED_BY:
02134     {
02135       content->GetAttr(kNameSpaceID_WAIProperties,
02136                        nsAccessibilityAtoms::labelledby, relatedID);
02137       if (relatedID.IsEmpty()) {
02138         relatedNode = do_QueryInterface(GetLabelContent(content));
02139       }
02140       break;
02141     }
02142   case RELATION_DESCRIBED_BY:
02143     {
02144       content->GetAttr(kNameSpaceID_WAIProperties,
02145                        nsAccessibilityAtoms::describedby, relatedID);
02146       if (relatedID.IsEmpty()) {
02147         nsIContent *description =
02148           GetXULLabelContent(content, nsAccessibilityAtoms::description);
02149         relatedNode = do_QueryInterface(description);
02150       }
02151       break;
02152     }
02153   case RELATION_DESCRIPTION_FOR:
02154     {
02155       const PRUint32 kAncestorLevelsToSearch = 3;
02156       relatedNode = GetInverseRelatedNode(nsAccessibilityAtoms::describedby, kAncestorLevelsToSearch);
02157       if (!relatedNode && content->Tag() == nsAccessibilityAtoms::description &&
02158           content->IsContentOfType(nsIContent::eXUL)) {
02159         // This affectively adds an optional control attribute to xul:description,
02160         // which only affects accessibility, by allowing the description to be
02161         // tied to a control.
02162         content->GetAttr(kNameSpaceID_None,
02163                          nsAccessibilityAtoms::control, relatedID);
02164       }
02165       break;
02166     }
02167   case RELATION_CONTROLLED_BY: 
02168     {
02169       relatedNode = GetInverseRelatedNode(nsAccessibilityAtoms::controls);
02170       break;
02171     }
02172   case RELATION_CONTROLLER_FOR:
02173     {
02174       content->GetAttr(kNameSpaceID_WAIProperties,
02175                        nsAccessibilityAtoms::controls, relatedID);
02176       break;
02177     }
02178   case RELATION_FLOWS_TO:
02179     {
02180       content->GetAttr(kNameSpaceID_WAIProperties,
02181                        nsAccessibilityAtoms::flowto, relatedID);
02182       break;
02183     }
02184   case RELATION_FLOWS_FROM:
02185     {
02186       relatedNode = GetInverseRelatedNode(nsAccessibilityAtoms::flowto);
02187       break;
02188     }
02189     
02190   case RELATION_DEFAULT_BUTTON:
02191     {
02192       if (content->IsContentOfType(nsIContent::eHTML)) {
02193         nsCOMPtr<nsIForm> form;
02194         while ((form = do_QueryInterface(content)) == nsnull &&
02195                (content = content->GetParent()) != nsnull) /* nothing */ ;
02196 
02197         if (form) {
02198           // We have a <form> element, so iterate through and try to find a submit button
02199           nsCOMPtr<nsISimpleEnumerator> formControls;
02200           form->GetControlEnumerator(getter_AddRefs(formControls));
02201           nsCOMPtr<nsISupports> controlSupports;
02202           PRBool hasMoreElements;
02203           while (NS_SUCCEEDED(formControls->HasMoreElements(&hasMoreElements)) &&
02204                 hasMoreElements) {
02205             nsresult rv = formControls->GetNext(getter_AddRefs(controlSupports));
02206             nsCOMPtr<nsIFormControl> control = do_QueryInterface(controlSupports);    
02207             if (control) {
02208               PRInt32 type = control->GetType();
02209               if (type == NS_FORM_INPUT_SUBMIT || type == NS_FORM_BUTTON_SUBMIT ||
02210                   type == NS_FORM_INPUT_IMAGE) {
02211                 relatedNode = do_QueryInterface(control);
02212                 break;
02213               }
02214             }
02215           }
02216         }
02217       }
02218       else {
02219         // In XUL, use first <button default="true" .../> in the document
02220         nsCOMPtr<nsIDOMXULDocument> xulDoc = do_QueryInterface(content->GetDocument());
02221         nsCOMPtr<nsIDOMXULButtonElement> buttonEl;
02222         if (xulDoc) {
02223           nsCOMPtr<nsIDOMNodeList> possibleDefaultButtons;
02224           xulDoc->GetElementsByAttribute(NS_LITERAL_STRING("default"),
02225                                          NS_LITERAL_STRING("true"),
02226                                          getter_AddRefs(possibleDefaultButtons));
02227           if (possibleDefaultButtons) {
02228             PRUint32 length;
02229             possibleDefaultButtons->GetLength(&length);
02230             nsCOMPtr<nsIDOMNode> possibleButton;
02231             // Check for button in list of default="true" elements
02232             for (PRUint32 count = 0; count < length && !buttonEl; count ++) {
02233               possibleDefaultButtons->Item(count, getter_AddRefs(possibleButton));
02234               buttonEl = do_QueryInterface(possibleButton);
02235             }
02236           }
02237           if (!buttonEl) { // Check for anonymous accept button in <dialog>
02238             nsCOMPtr<nsIDOMDocumentXBL> xblDoc(do_QueryInterface(xulDoc));
02239             if (xblDoc) {
02240               nsCOMPtr<nsIDOMDocument> domDoc = do_QueryInterface(xulDoc);
02241               NS_ASSERTION(domDoc, "No DOM document");
02242               nsCOMPtr<nsIDOMElement> rootEl;
02243               domDoc->GetDocumentElement(getter_AddRefs(rootEl));
02244               if (rootEl) {
02245                 nsCOMPtr<nsIDOMElement> possibleButtonEl;
02246                 xblDoc->GetAnonymousElementByAttribute(rootEl,
02247                                                       NS_LITERAL_STRING("default"),
02248                                                       NS_LITERAL_STRING("true"),
02249                                                       getter_AddRefs(possibleButtonEl));
02250                 buttonEl = do_QueryInterface(possibleButtonEl);
02251               }
02252             }
02253           }
02254           relatedNode = do_QueryInterface(buttonEl);
02255         }
02256       }
02257       break;
02258     }
02259   default:
02260     return NS_ERROR_NOT_IMPLEMENTED;
02261   }
02262 
02263   if (!relatedID.IsEmpty()) {
02264     // In some cases we need to get the relatedNode from an ID-style attribute
02265     nsCOMPtr<nsIDOMDocument> domDoc;
02266     mDOMNode->GetOwnerDocument(getter_AddRefs(domDoc));
02267     NS_ENSURE_TRUE(domDoc, NS_ERROR_FAILURE);
02268     nsCOMPtr<nsIDOMElement> relatedEl;
02269     domDoc->GetElementById(relatedID, getter_AddRefs(relatedEl));
02270     relatedNode = do_QueryInterface(relatedEl);
02271   }
02272 
02273   // Return the corresponding accessible if the related DOM node is found
02274   if (relatedNode) {
02275     nsCOMPtr<nsIAccessibilityService> accService =
02276       do_GetService("@mozilla.org/accessibilityService;1");
02277     NS_ENSURE_TRUE(accService, NS_ERROR_FAILURE);
02278     return accService->GetAccessibleInWeakShell(relatedNode, mWeakShell, aRelated);
02279   }
02280   return NS_ERROR_FAILURE;
02281 }
02282 
02283 /* void addSelection (); */
02284 NS_IMETHODIMP nsAccessible::AddSelection()
02285 {
02286   return NS_ERROR_NOT_IMPLEMENTED;
02287 }
02288 
02289 /* void extendSelection (); */
02290 NS_IMETHODIMP nsAccessible::ExtendSelection()
02291 {
02292   return NS_ERROR_NOT_IMPLEMENTED;
02293 }
02294 
02295 /* unsigned long getExtState (); */
02296 NS_IMETHODIMP nsAccessible::GetExtState(PRUint32 *aExtState)
02297 {
02298   if (!mDOMNode) {
02299     return NS_ERROR_FAILURE; // Node shut down
02300   }
02301   *aExtState = 0;
02302   // XXX We can remove this hack once we support RDF-based role & state maps
02303   if (mRoleMapEntry && mRoleMapEntry->role == ROLE_TEXT) {
02304     *aExtState = NS_LITERAL_CSTRING("textarea").Equals(mRoleMapEntry->roleString) ? 
02305        EXT_STATE_MULTI_LINE : EXT_STATE_SINGLE_LINE;
02306   }
02307   return NS_OK;
02308 }
02309 
02310 /* [noscript] void getNativeInterface(out voidPtr aOutAccessible); */
02311 NS_IMETHODIMP nsAccessible::GetNativeInterface(void **aOutAccessible)
02312 {
02313   return NS_ERROR_NOT_IMPLEMENTED;
02314 }
02315 
02316 void nsAccessible::DoCommandCallback(nsITimer *aTimer, void *aClosure)
02317 {
02318   NS_ASSERTION(gDoCommandTimer, "How did we get here if there was no gDoCommandTimer?");
02319   NS_RELEASE(gDoCommandTimer);
02320 
02321   nsIContent *content = NS_REINTERPRET_CAST(nsIContent*, aClosure);
02322   nsCOMPtr<nsIDOMXULElement> xulElement(do_QueryInterface(content));
02323   if (xulElement) {
02324     xulElement->Click();
02325   }
02326   else {
02327     nsIDocument *doc = content->GetDocument();
02328     if (!doc) {
02329       return;
02330     }
02331     nsCOMPtr<nsIPresShell> presShell = doc->GetShellAt(0);
02332     nsPIDOMWindow *outerWindow = doc->GetWindow();
02333     if (presShell && outerWindow) {
02334       nsAutoPopupStatePusher popupStatePusher(outerWindow, openAllowed);
02335 
02336       nsMouseEvent downEvent(PR_TRUE, NS_MOUSE_LEFT_BUTTON_DOWN, nsnull,
02337                               nsMouseEvent::eSynthesized);
02338       nsMouseEvent upEvent(PR_TRUE, NS_MOUSE_LEFT_BUTTON_UP, nsnull,
02339                               nsMouseEvent::eSynthesized);
02340       nsMouseEvent clickEvent(PR_TRUE, NS_MOUSE_LEFT_CLICK, nsnull,
02341                               nsMouseEvent::eSynthesized);
02342 
02343       nsEventStatus eventStatus = nsEventStatus_eIgnore;
02344       presShell->HandleDOMEventWithTarget(content, &downEvent, &eventStatus);
02345       presShell->HandleDOMEventWithTarget(content, &upEvent, &eventStatus);
02346       presShell->HandleDOMEventWithTarget(content, &clickEvent, &eventStatus);
02347     }
02348   }
02349 }
02350 
02351 /*
02352  * Use Timer to execute "Click" command of XUL/HTML element (e.g. menuitem, button...).
02353  *
02354  * When "Click" is to open a "modal" dialog/window, it won't return untill the
02355  * dialog/window is closed. If executing "Click" command directly in
02356  * nsXXXAccessible::DoAction, it will block AT-Tools(e.g. GOK) that invoke
02357  * "action" of mozilla accessibles direclty.
02358  */
02359 nsresult nsAccessible::DoCommand(nsIContent *aContent)
02360 {
02361   nsCOMPtr<nsIContent> content = aContent;
02362   if (!content) {
02363     content = do_QueryInterface(mDOMNode);
02364   }
02365   if (gDoCommandTimer) {
02366     // Already have timer going for another command
02367     NS_WARNING("Doubling up on do command timers doesn't work. This wasn't expected.");
02368     return NS_ERROR_FAILURE;
02369   }
02370 
02371   nsCOMPtr<nsITimer> timer = do_CreateInstance("@mozilla.org/timer;1");
02372   if (!timer) {
02373     return NS_ERROR_OUT_OF_MEMORY;
02374   } 
02375 
02376   NS_ADDREF(gDoCommandTimer = timer);
02377   return gDoCommandTimer->InitWithFuncCallback(DoCommandCallback,
02378                                                (void*)content, 0,
02379                                                nsITimer::TYPE_ONE_SHOT);
02380 }
02381 
02382 already_AddRefed<nsIAccessible>
02383 nsAccessible::GetNextWithState(nsIAccessible *aStart, PRUint32 matchState)
02384 {
02385   // Return the next descendant that matches one of the states in matchState
02386   // Uses depth first search
02387   NS_ASSERTION(matchState, "GetNextWithState() not called with a state to match");
02388   NS_ASSERTION(aStart, "GetNextWithState() not called with an accessible to start with");
02389   nsCOMPtr<nsIAccessible> look, current = aStart;
02390   PRUint32 state = 0;
02391   while (0 == (state & matchState)) {
02392     current->GetFirstChild(getter_AddRefs(look));
02393     while (!look) {
02394       if (current == this) {
02395         return nsnull; // At top of subtree
02396       }
02397       current->GetNextSibling(getter_AddRefs(look));
02398       if (!look) {
02399         current->GetParent(getter_AddRefs(look));
02400         current.swap(look);
02401         continue;
02402       }
02403     }
02404     current.swap(look);
02405     current->GetFinalState(&state);
02406   }
02407 
02408   nsIAccessible *returnAccessible = nsnull;
02409   current.swap(returnAccessible);
02410 
02411   return returnAccessible;
02412 }
02413 
02414 // nsIAccessibleSelectable
02415 NS_IMETHODIMP nsAccessible::GetSelectedChildren(nsIArray **aSelectedAccessibles)
02416 {
02417   *aSelectedAccessibles = nsnull;
02418 
02419   nsCOMPtr<nsIMutableArray> selectedAccessibles;
02420   NS_NewArray(getter_AddRefs(selectedAccessibles));
02421   if (!selectedAccessibles)
02422     return NS_ERROR_OUT_OF_MEMORY;
02423 
02424   nsCOMPtr<nsIAccessible> selected = this;
02425   while ((selected = GetNextWithState(selected, STATE_SELECTED)) != nsnull) {
02426     selectedAccessibles->AppendElement(selected, PR_FALSE);
02427   }
02428 
02429   PRUint32 length = 0;
02430   selectedAccessibles->GetLength(&length); 
02431   if (length) { // length of nsIArray containing selected options
02432     *aSelectedAccessibles = selectedAccessibles;
02433     NS_ADDREF(*aSelectedAccessibles);
02434   }
02435 
02436   return NS_OK;
02437 }
02438 
02439 // return the nth selected descendant nsIAccessible object
02440 NS_IMETHODIMP nsAccessible::RefSelection(PRInt32 aIndex, nsIAccessible **aSelected)
02441 {
02442   *aSelected = nsnull;
02443   if (aIndex < 0) {
02444     return NS_ERROR_FAILURE;
02445   }
02446   nsCOMPtr<nsIAccessible> selected = this;
02447   PRInt32 count = 0;
02448   while (count ++ <= aIndex) {
02449     selected = GetNextWithState(selected, STATE_SELECTED);
02450     if (!selected) {
02451       return NS_ERROR_FAILURE; // aIndex out of range
02452     }
02453   }
02454   NS_IF_ADDREF(*aSelected = selected);
02455   return NS_OK;
02456 }
02457 
02458 NS_IMETHODIMP nsAccessible::GetSelectionCount(PRInt32 *aSelectionCount)
02459 {
02460   *aSelectionCount = 0;
02461   nsCOMPtr<nsIAccessible> selected = this;
02462   while ((selected = GetNextWithState(selected, STATE_SELECTED)) != nsnull) {
02463     ++ *aSelectionCount;
02464   }
02465 
02466   return NS_OK;
02467 }
02468 
02469 NS_IMETHODIMP nsAccessible::AddChildToSelection(PRInt32 aIndex)
02470 {
02471   // Tree views and other container widgets which may have grandchildren should
02472   // implement a selection methods for their specific interfaces, because being
02473   // able to deal with selection on a per-child basis would not be enough.
02474 
02475   NS_ENSURE_TRUE(aIndex >= 0, NS_ERROR_FAILURE);
02476 
02477   nsCOMPtr<nsIAccessible> child;
02478   GetChildAt(aIndex, getter_AddRefs(child));
02479 
02480   PRUint32 state;
02481   nsresult rv = child->GetFinalState(&state);
02482   NS_ENSURE_SUCCESS(rv, rv);
02483 
02484   if (!(state & STATE_SELECTABLE)) {
02485     return NS_OK;
02486   }
02487 
02488   return child->TakeSelection();
02489 }
02490 
02491 NS_IMETHODIMP nsAccessible::RemoveChildFromSelection(PRInt32 aIndex)
02492 {
02493   // Tree views and other container widgets which may have grandchildren should
02494   // implement a selection methods for their specific interfaces, because being
02495   // able to deal with selection on a per-child basis would not be enough.
02496 
02497   NS_ENSURE_TRUE(aIndex >= 0, NS_ERROR_FAILURE);
02498 
02499   nsCOMPtr<nsIAccessible> child;
02500   GetChildAt(aIndex, getter_AddRefs(child));
02501 
02502   PRUint32 state;
02503   nsresult rv = child->GetFinalState(&state);
02504   NS_ENSURE_SUCCESS(rv, rv);
02505 
02506   if (!(state & STATE_SELECTED)) {
02507     return NS_OK;
02508   }
02509 
02510   return child->RemoveSelection();
02511 }
02512 
02513 NS_IMETHODIMP nsAccessible::IsChildSelected(PRInt32 aIndex, PRBool *aIsSelected)
02514 {
02515   // Tree views and other container widgets which may have grandchildren should
02516   // implement a selection methods for their specific interfaces, because being
02517   // able to deal with selection on a per-child basis would not be enough.
02518 
02519   *aIsSelected = PR_FALSE;
02520   NS_ENSURE_TRUE(aIndex >= 0, NS_ERROR_FAILURE);
02521 
02522   nsCOMPtr<nsIAccessible> child;
02523   GetChildAt(aIndex, getter_AddRefs(child));
02524 
02525   PRUint32 state;
02526   nsresult rv = child->GetFinalState(&state);
02527   NS_ENSURE_SUCCESS(rv, rv);
02528 
02529   if (state & STATE_SELECTED) {
02530     *aIsSelected = PR_TRUE;
02531   }
02532   return NS_OK;
02533 }
02534 
02535 NS_IMETHODIMP nsAccessible::ClearSelection()
02536 {
02537   nsCOMPtr<nsIAccessible> selected = this;
02538   while ((selected = GetNextWithState(selected, STATE_SELECTED)) != nsnull) {
02539     selected->RemoveSelection();
02540   }
02541   return NS_OK;
02542 }
02543 
02544 NS_IMETHODIMP nsAccessible::SelectAllSelection(PRBool *_retval)
02545 {
02546   nsCOMPtr<nsIAccessible> selectable = this;
02547   while ((selectable = GetNextWithState(selectable, STATE_SELECTED)) != nsnull) {
02548     selectable->TakeSelection();
02549   }
02550   return NS_OK;
02551 }
02552 
02553 #ifdef MOZ_ACCESSIBILITY_ATK
02554 // static helper functions
02555 nsresult nsAccessible::GetParentBlockNode(nsIPresShell *aPresShell, nsIDOMNode *aCurrentNode, nsIDOMNode **aBlockNode)
02556 {
02557   *aBlockNode = nsnull;
02558 
02559   nsCOMPtr<nsIContent> content(do_QueryInterface(aCurrentNode));
02560   if (! content)
02561     return NS_ERROR_FAILURE;
02562 
02563   nsIFrame *frame = nsnull;
02564   aPresShell->GetPrimaryFrameFor(content, &frame);
02565   if (! frame)
02566     return NS_ERROR_FAILURE;
02567 
02568   nsIFrame *parentFrame = GetParentBlockFrame(frame);
02569   if (! parentFrame)
02570     return NS_ERROR_FAILURE;
02571 
02572   nsPresContext *presContext = aPresShell->GetPresContext();
02573   nsIAtom* frameType = nsnull;
02574   while (frame && (frameType = frame->GetType()) != nsAccessibilityAtoms::textFrame) {
02575     frame = frame->GetFirstChild(nsnull);
02576   }
02577   if (! frame || frameType != nsAccessibilityAtoms::textFrame)
02578     return NS_ERROR_FAILURE;
02579 
02580   PRInt32 index = 0;
02581   nsIFrame *firstTextFrame = nsnull;
02582   FindTextFrame(index, presContext, parentFrame->GetFirstChild(nsnull),
02583                 &firstTextFrame, frame);
02584   if (firstTextFrame) {
02585     nsIContent *content = firstTextFrame->GetContent();
02586 
02587     if (content) {
02588       CallQueryInterface(content, aBlockNode);
02589     }
02590 
02591     return NS_OK;
02592   }
02593   else {
02594     //XXX, why?
02595   }
02596   return NS_ERROR_FAILURE;
02597 }
02598 
02599 nsIFrame* nsAccessible::GetParentBlockFrame(nsIFrame *aFrame)
02600 {
02601   if (! aFrame)
02602     return nsnull;
02603 
02604   nsIFrame* frame = aFrame;
02605   while (frame && frame->GetType() != nsAccessibilityAtoms::blockFrame) {
02606     nsIFrame* parentFrame = frame->GetParent();
02607     frame = parentFrame;
02608   }
02609   return frame;
02610 }
02611 
02612 PRBool nsAccessible::FindTextFrame(PRInt32 &index, nsPresContext *aPresContext, nsIFrame *aCurFrame, 
02613                                    nsIFrame **aFirstTextFrame, const nsIFrame *aTextFrame)
02614 // Do a depth-first traversal to find the given text frame(aTextFrame)'s index of the block frame(aCurFrame)
02615 //   it belongs to, also return the first text frame within the same block.
02616 // Parameters:
02617 //   index        [in/out] - the current index - finally, it will be the aTextFrame's index in its block;
02618 //   aCurFrame        [in] - the current frame - its initial value is the first child frame of the block frame;
02619 //   aFirstTextFrame [out] - the first text frame which is within the same block with aTextFrame;
02620 //   aTextFrame       [in] - the text frame we are looking for;
02621 // Return:
02622 //   PR_TRUE  - the aTextFrame was found in its block frame;
02623 //   PR_FALSE - the aTextFrame was NOT found in its block frame;
02624 {
02625   while (aCurFrame) {
02626 
02627     if (aCurFrame == aTextFrame) {
02628       if (index == 0)
02629         *aFirstTextFrame = aCurFrame;
02630       // we got it, stop traversing
02631       return PR_TRUE;
02632     }
02633 
02634     nsIAtom* frameType = aCurFrame->GetType();
02635     if (frameType == nsAccessibilityAtoms::blockFrame) {
02636       // every block frame will reset the index
02637       index = 0;
02638     }
02639     else {
02640       if (frameType == nsAccessibilityAtoms::textFrame) {
02641         nsRect frameRect = aCurFrame->GetRect();
02642        // skip the empty frame
02643         if (! frameRect.IsEmpty()) {
02644           if (index == 0)
02645             *aFirstTextFrame = aCurFrame;
02646           index++;
02647         }
02648       }
02649 
02650       // we won't expand the tree under a block frame.
02651       if (FindTextFrame(index, aPresContext, aCurFrame->GetFirstChild(nsnull),
02652                         aFirstTextFrame, aTextFrame))
02653         return PR_TRUE;
02654     }
02655 
02656     nsIFrame* siblingFrame = aCurFrame->GetNextSibling();
02657     aCurFrame = siblingFrame;
02658   }
02659   return PR_FALSE;
02660 }
02661 #endif //MOZ_ACCESSIBILITY_ATK