Back to index

lightning-sunbird  0.9+nobinonly
nsXULFormControlAccessible.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  *   Aaron Leventhal (aaronl@netscape.com)
00024  *   Kyle Yuan (kyle.yuan@sun.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 // NOTE: alphabetically ordered
00041 #include "nsXULFormControlAccessible.h"
00042 #include "nsHTMLFormControlAccessible.h"
00043 #include "nsAccessibilityAtoms.h"
00044 #include "nsAccessibleTreeWalker.h"
00045 #include "nsIDOMHTMLInputElement.h"
00046 #include "nsIDOMXULButtonElement.h"
00047 #include "nsIDOMXULCheckboxElement.h"
00048 #include "nsIDOMXULMenuListElement.h"
00049 #include "nsIDOMXULSelectCntrlItemEl.h"
00050 #include "nsIDOMXULTextboxElement.h"
00051 #include "nsINameSpaceManager.h"
00052 
00061 // Don't inherit from nsFormControlAccessible - it doesn't allow children and a button can have a dropmarker child
00062 nsXULButtonAccessible::nsXULButtonAccessible(nsIDOMNode* aNode, nsIWeakReference* aShell):
00063 nsAccessibleWrap(aNode, aShell)
00064 { 
00065 }
00066 
00070 NS_IMETHODIMP nsXULButtonAccessible::GetNumActions(PRUint8 *_retval)
00071 {
00072   *_retval = eSingle_Action;
00073   return NS_OK;;
00074 }
00075 
00079 NS_IMETHODIMP nsXULButtonAccessible::GetActionName(PRUint8 index, nsAString& _retval)
00080 {
00081   if (index == eAction_Click) {
00082     nsAccessible::GetTranslatedString(NS_LITERAL_STRING("press"), _retval); 
00083     return NS_OK;
00084   }
00085   return NS_ERROR_INVALID_ARG;
00086 }
00087 
00091 NS_IMETHODIMP nsXULButtonAccessible::DoAction(PRUint8 index)
00092 {
00093   if (index == 0) {
00094     return DoCommand();
00095   }
00096   return NS_ERROR_INVALID_ARG;
00097 }
00098 
00102 NS_IMETHODIMP nsXULButtonAccessible::GetRole(PRUint32 *_retval)
00103 {
00104   *_retval = ROLE_PUSHBUTTON;
00105   return NS_OK;
00106 }
00107 
00111 NS_IMETHODIMP nsXULButtonAccessible::GetState(PRUint32 *aState)
00112 {
00113   // get focus and disable status from base class
00114   nsAccessible::GetState(aState);
00115 
00116   PRBool disabled = PR_FALSE;
00117   nsCOMPtr<nsIDOMXULControlElement> xulFormElement(do_QueryInterface(mDOMNode));  
00118   if (xulFormElement) {
00119     xulFormElement->GetDisabled(&disabled);
00120     if (disabled)
00121       *aState |= STATE_UNAVAILABLE;
00122     else 
00123       *aState |= STATE_FOCUSABLE;
00124   }
00125 
00126   // Buttons can be checked -- they simply appear pressed in rather than checked
00127   nsCOMPtr<nsIDOMXULButtonElement> xulButtonElement(do_QueryInterface(mDOMNode));
00128   if (xulButtonElement) {
00129     PRBool checked = PR_FALSE;
00130     PRInt32 checkState = 0;
00131     xulButtonElement->GetChecked(&checked);
00132     if (checked) {
00133       *aState |= STATE_PRESSED;
00134       xulButtonElement->GetCheckState(&checkState);
00135       if (checkState == nsIDOMXULButtonElement::CHECKSTATE_MIXED)  
00136         *aState |= STATE_MIXED;
00137     }
00138   }
00139 
00140   nsCOMPtr<nsIDOMElement> element(do_QueryInterface(mDOMNode));
00141   if (element) {
00142     PRBool isDefault = PR_FALSE;
00143     element->HasAttribute(NS_LITERAL_STRING("default"), &isDefault) ;
00144     if (isDefault)
00145       *aState |= STATE_DEFAULT;
00146 
00147     nsAutoString type;
00148     element->GetAttribute(NS_LITERAL_STRING("type"), type);
00149     if (type.EqualsLiteral("menu") || type.EqualsLiteral("menu-button")) {
00150       *aState |= STATE_HASPOPUP;
00151     }
00152   }
00153 
00154   return NS_OK;
00155 }
00156 
00157 void nsXULButtonAccessible::CacheChildren(PRBool aWalkAnonContent)
00158 {
00159   // An XUL button accessible may have 1 child dropmarker accessible
00160   if (!mWeakShell) {
00161     mAccChildCount = eChildCountUninitialized;
00162     return;   // This outer doc node has been shut down
00163   }
00164   if (mAccChildCount == eChildCountUninitialized) {
00165     mAccChildCount = 0;
00166     SetFirstChild(nsnull);
00167     nsAccessibleTreeWalker walker(mWeakShell, mDOMNode, PR_TRUE);
00168     walker.GetFirstChild();
00169     nsCOMPtr<nsIAccessible> dropMarkerAccessible;
00170     while (walker.mState.accessible) {
00171       dropMarkerAccessible = walker.mState.accessible;
00172       walker.GetNextSibling();
00173     }
00174 
00175     // If the anonymous tree walker can find accessible children, 
00176     // and the last one is a push button, then use it as the only accessible 
00177     // child -- because this is the scenario where we have a dropmarker child
00178 
00179     if (dropMarkerAccessible) {    
00180       PRUint32 role;
00181       if (NS_SUCCEEDED(dropMarkerAccessible->GetRole(&role)) && role == ROLE_PUSHBUTTON) {
00182         SetFirstChild(dropMarkerAccessible);
00183         nsCOMPtr<nsPIAccessible> privChildAcc = do_QueryInterface(dropMarkerAccessible);
00184         privChildAcc->SetNextSibling(nsnull);
00185         privChildAcc->SetParent(this);
00186         mAccChildCount = 1;
00187       }
00188     }
00189   }
00190 }
00191 
00199 nsXULDropmarkerAccessible::nsXULDropmarkerAccessible(nsIDOMNode* aNode, nsIWeakReference* aShell):
00200 nsFormControlAccessible(aNode, aShell)
00201 { 
00202 }
00203 
00207 NS_IMETHODIMP nsXULDropmarkerAccessible::GetNumActions(PRUint8 *aResult)
00208 {
00209   *aResult = eSingle_Action;
00210   return NS_OK;;
00211 }
00212 
00213 PRBool nsXULDropmarkerAccessible::DropmarkerOpen(PRBool aToggleOpen)
00214 {
00215   PRBool isOpen = PR_FALSE;
00216 
00217   nsCOMPtr<nsIDOMNode> parentButtonNode;
00218   mDOMNode->GetParentNode(getter_AddRefs(parentButtonNode));
00219   nsCOMPtr<nsIDOMXULButtonElement> parentButtonElement(do_QueryInterface(parentButtonNode));
00220 
00221   if (parentButtonElement) {
00222     parentButtonElement->GetOpen(&isOpen);
00223     if (aToggleOpen)
00224       parentButtonElement->SetOpen(!isOpen);
00225   }
00226   else {
00227     nsCOMPtr<nsIDOMXULMenuListElement> parentMenuListElement(do_QueryInterface(parentButtonNode));
00228     if (parentMenuListElement) {
00229       parentMenuListElement->GetOpen(&isOpen);
00230       if (aToggleOpen)
00231         parentMenuListElement->SetOpen(!isOpen);
00232     }
00233   }
00234 
00235   return isOpen;
00236 }
00237 
00241 NS_IMETHODIMP nsXULDropmarkerAccessible::GetActionName(PRUint8 index, nsAString& aResult)
00242 {
00243   if (index == eAction_Click) {
00244     if (DropmarkerOpen(PR_FALSE))
00245       aResult.AssignLiteral("close");
00246     else
00247       aResult.AssignLiteral("open");
00248     return NS_OK;
00249   }
00250 
00251   return NS_ERROR_INVALID_ARG;
00252 }
00253 
00257 NS_IMETHODIMP nsXULDropmarkerAccessible::DoAction(PRUint8 index)
00258 {
00259   if (index == eAction_Click) {
00260     DropmarkerOpen(PR_TRUE); // Reverse the open attribute
00261     return NS_OK;
00262   }
00263   return NS_ERROR_INVALID_ARG;
00264 }
00265 
00269 NS_IMETHODIMP nsXULDropmarkerAccessible::GetRole(PRUint32 *aResult)
00270 {
00271   *aResult = ROLE_PUSHBUTTON;
00272   return NS_OK;
00273 }
00274 
00275 NS_IMETHODIMP nsXULDropmarkerAccessible::GetState(PRUint32 *aResult)
00276 {
00277   *aResult = 0;
00278   
00279   if (DropmarkerOpen(PR_FALSE))
00280     *aResult = STATE_PRESSED;
00281 
00282   return NS_OK;
00283 }
00284 
00292 nsXULCheckboxAccessible::nsXULCheckboxAccessible(nsIDOMNode* aNode, nsIWeakReference* aShell):
00293 nsFormControlAccessible(aNode, aShell)
00294 { 
00295 }
00296 
00300 NS_IMETHODIMP nsXULCheckboxAccessible::GetRole(PRUint32 *_retval)
00301 {
00302   *_retval = ROLE_CHECKBUTTON;
00303   return NS_OK;
00304 }
00305 
00309 NS_IMETHODIMP nsXULCheckboxAccessible::GetNumActions(PRUint8 *_retval)
00310 {
00311   *_retval = eSingle_Action;
00312   return NS_OK;
00313 }
00314 
00318 NS_IMETHODIMP nsXULCheckboxAccessible::GetActionName(PRUint8 index, nsAString& _retval)
00319 {
00320   if (index == eAction_Click) {
00321     // check or uncheck
00322     PRUint32 state;
00323     GetState(&state);
00324 
00325     if (state & STATE_CHECKED)
00326       _retval.AssignLiteral("uncheck");
00327     else
00328       _retval.AssignLiteral("check");
00329 
00330     return NS_OK;
00331   }
00332   return NS_ERROR_INVALID_ARG;
00333 }
00334 
00338 NS_IMETHODIMP nsXULCheckboxAccessible::DoAction(PRUint8 index)
00339 {
00340   if (index == eAction_Click) {
00341    return DoCommand();
00342   }
00343   return NS_ERROR_INVALID_ARG;
00344 }
00345 
00349 NS_IMETHODIMP nsXULCheckboxAccessible::GetState(PRUint32 *_retval)
00350 {
00351   // Get focus and disable status from base class
00352   nsFormControlAccessible::GetState(_retval);
00353 
00354   // Determine Checked state
00355   nsCOMPtr<nsIDOMXULCheckboxElement> xulCheckboxElement(do_QueryInterface(mDOMNode));
00356   if (xulCheckboxElement) {
00357     PRBool checked = PR_FALSE;
00358     xulCheckboxElement->GetChecked(&checked);
00359     if (checked) {
00360       *_retval |= STATE_CHECKED;
00361       PRInt32 checkState = 0;
00362       xulCheckboxElement->GetCheckState(&checkState);
00363       if (checkState == nsIDOMXULCheckboxElement::CHECKSTATE_MIXED)   
00364         *_retval |= STATE_MIXED;
00365     }
00366   }
00367   
00368   return NS_OK;
00369 }
00370 
00375 nsXULGroupboxAccessible::nsXULGroupboxAccessible(nsIDOMNode* aNode, nsIWeakReference* aShell):
00376 nsAccessibleWrap(aNode, aShell)
00377 { 
00378 }
00379 
00380 NS_IMETHODIMP nsXULGroupboxAccessible::GetRole(PRUint32 *_retval)
00381 {
00382   *_retval = ROLE_GROUPING;
00383   return NS_OK;
00384 }
00385 
00386 NS_IMETHODIMP nsXULGroupboxAccessible::GetState(PRUint32 *_retval)
00387 {
00388   // Groupbox doesn't support focusable state!
00389   nsAccessible::GetState(_retval);
00390   *_retval &= ~STATE_FOCUSABLE;
00391 
00392   return NS_OK;
00393 }
00394 
00395 NS_IMETHODIMP nsXULGroupboxAccessible::GetName(nsAString& aName)
00396 {
00397   aName.Truncate();  // Default name is blank 
00398 
00399   if (mRoleMapEntry) {
00400     nsAccessible::GetName(aName);
00401     if (!aName.IsEmpty()) {
00402       return NS_OK;
00403     }
00404   }
00405   nsCOMPtr<nsIDOMElement> element(do_QueryInterface(mDOMNode));
00406   if (element) {
00407     nsCOMPtr<nsIDOMNodeList> captions;
00408     nsAutoString nameSpaceURI;
00409     element->GetNamespaceURI(nameSpaceURI);
00410     element->GetElementsByTagNameNS(nameSpaceURI, NS_LITERAL_STRING("caption"), 
00411                                     getter_AddRefs(captions));
00412     if (captions) {
00413       nsCOMPtr<nsIDOMNode> captionNode;
00414       captions->Item(0, getter_AddRefs(captionNode));
00415       if (captionNode) {
00416         element = do_QueryInterface(captionNode);
00417         NS_ASSERTION(element, "No nsIDOMElement for caption node!");
00418         element->GetAttribute(NS_LITERAL_STRING("label"), aName) ;
00419       }
00420     }
00421   }
00422   return NS_OK;
00423 }
00424 
00428 NS_IMPL_ISUPPORTS_INHERITED0(nsXULProgressMeterAccessible, nsFormControlAccessible)
00429 
00430 nsXULProgressMeterAccessible::nsXULProgressMeterAccessible(nsIDOMNode* aNode, nsIWeakReference* aShell):
00431 nsFormControlAccessible(aNode, aShell)
00432 { 
00433 }
00434 
00435 NS_IMETHODIMP nsXULProgressMeterAccessible::GetRole(PRUint32 *_retval)
00436 {
00437   *_retval = ROLE_PROGRESSBAR;
00438   return NS_OK;
00439 }
00440 
00444 NS_IMETHODIMP nsXULProgressMeterAccessible::GetState(PRUint32 *aState)
00445 {
00446   nsresult rv = nsAccessible::GetState(aState);
00447   *aState &= ~STATE_FOCUSABLE;
00448   return rv;
00449 }
00450 
00451 NS_IMETHODIMP nsXULProgressMeterAccessible::GetValue(nsAString& _retval)
00452 {
00453   nsCOMPtr<nsIDOMElement> element(do_QueryInterface(mDOMNode));
00454   NS_ASSERTION(element, "No element for DOM node!");
00455   element->GetAttribute(NS_LITERAL_STRING("value"), _retval);
00456   if (!_retval.IsEmpty() && _retval.Last() != '%')
00457     _retval.AppendLiteral("%");
00458   return NS_OK;
00459 }
00460 
00466 nsXULRadioButtonAccessible::nsXULRadioButtonAccessible(nsIDOMNode* aNode, nsIWeakReference* aShell):
00467 nsRadioButtonAccessible(aNode, aShell)
00468 { 
00469 }
00470 
00472 NS_IMETHODIMP nsXULRadioButtonAccessible::DoAction(PRUint8 index)
00473 {
00474   if (index == eAction_Click) {
00475     return DoCommand();
00476   }
00477   return NS_ERROR_INVALID_ARG;
00478 }
00479 
00481 NS_IMETHODIMP nsXULRadioButtonAccessible::GetState(PRUint32 *_retval)
00482 {
00483   nsFormControlAccessible::GetState(_retval);
00484   PRBool selected = PR_FALSE;   // Radio buttons can be selected
00485 
00486   nsCOMPtr<nsIDOMXULSelectControlItemElement> radioButton(do_QueryInterface(mDOMNode));
00487   if (radioButton) {
00488     radioButton->GetSelected(&selected);
00489     if (selected) {
00490       *_retval |= STATE_CHECKED;
00491     }
00492   }
00493 
00494   return NS_OK;
00495 }
00496 
00507 nsXULRadioGroupAccessible::nsXULRadioGroupAccessible(nsIDOMNode* aNode, nsIWeakReference* aShell):
00508 nsXULSelectableAccessible(aNode, aShell)
00509 { 
00510 }
00511 
00512 NS_IMETHODIMP nsXULRadioGroupAccessible::GetRole(PRUint32 *_retval)
00513 {
00514   *_retval = ROLE_GROUPING;
00515   return NS_OK;
00516 }
00517 
00518 NS_IMETHODIMP nsXULRadioGroupAccessible::GetState(PRUint32 *_retval)
00519 {
00520   // The radio group is not focusable.
00521   // Sometimes the focus controller will report that it is focused.
00522   // That means that the actual selected radio button should be considered focused
00523   nsAccessible::GetState(_retval);
00524   *_retval &= ~(STATE_FOCUSABLE | STATE_FOCUSED);
00525   return NS_OK;
00526 }
00534 nsXULStatusBarAccessible::nsXULStatusBarAccessible(nsIDOMNode* aNode, nsIWeakReference* aShell):
00535 nsAccessibleWrap(aNode, aShell)
00536 { 
00537 }
00538 
00542 NS_IMETHODIMP nsXULStatusBarAccessible::GetRole(PRUint32 *_retval)
00543 {
00544   *_retval = ROLE_STATUSBAR;
00545   return NS_OK;
00546 }
00547 
00548 NS_IMETHODIMP nsXULStatusBarAccessible::GetState(PRUint32 *_retval)
00549 {
00550   return nsAccessible::GetState(_retval);
00551 }
00552 
00557 nsXULToolbarAccessible::nsXULToolbarAccessible(nsIDOMNode* aNode, nsIWeakReference* aShell):
00558 nsAccessibleWrap(aNode, aShell)
00559 { 
00560 }
00561 
00562 NS_IMETHODIMP nsXULToolbarAccessible::GetRole(PRUint32 *_retval)
00563 {
00564   *_retval = ROLE_TOOLBAR;
00565   return NS_OK;
00566 }
00567 
00568 NS_IMETHODIMP nsXULToolbarAccessible::GetState(PRUint32 *_retval)
00569 {
00570   nsAccessible::GetState(_retval);
00571   *_retval &= ~STATE_FOCUSABLE;  // toolbar is not focusable
00572   return NS_OK;
00573 }
00574 
00579 nsXULToolbarSeparatorAccessible::nsXULToolbarSeparatorAccessible(nsIDOMNode* aNode, nsIWeakReference* aShell):
00580 nsLeafAccessible(aNode, aShell)
00581 { 
00582 }
00583 
00584 NS_IMETHODIMP nsXULToolbarSeparatorAccessible::GetRole(PRUint32 *_retval)
00585 {
00586   *_retval = ROLE_SEPARATOR;
00587   return NS_OK;
00588 }
00589 
00590 NS_IMETHODIMP nsXULToolbarSeparatorAccessible::GetState(PRUint32 *_retval)
00591 {
00592   *_retval = 0;  // no special state flags for toolbar separator
00593   return NS_OK;
00594 }
00595 
00600 nsXULTextFieldAccessible::nsXULTextFieldAccessible(nsIDOMNode* aNode, nsIWeakReference* aShell) :
00601  nsLeafAccessible(aNode, aShell)
00602 {
00603 }
00604 
00605 NS_IMETHODIMP nsXULTextFieldAccessible::GetValue(nsAString& aValue)
00606 {
00607   PRUint32 state;
00608   GetState(&state);
00609   if (state & STATE_PROTECTED)    // Don't return password text!
00610     return NS_ERROR_FAILURE;
00611 
00612   nsCOMPtr<nsIDOMXULTextBoxElement> textBox(do_QueryInterface(mDOMNode));
00613   if (textBox) {
00614     return textBox->GetValue(aValue);
00615   }
00616   return NS_ERROR_FAILURE;
00617 }
00618 
00619 NS_IMETHODIMP nsXULTextFieldAccessible::GetExtState(PRUint32 *aExtState)
00620 {
00621   nsresult rv = nsAccessible::GetExtState(aExtState);
00622   if (NS_FAILED(rv)) {
00623     return rv;
00624   }
00625   nsCOMPtr<nsIContent> content(do_QueryInterface(mDOMNode));
00626   NS_ASSERTION(content, "Should not have gotten past nsAccessible::GetExtState() without node");
00627 
00628   PRBool isMultiLine = content->HasAttr(kNameSpaceID_None, nsAccessibilityAtoms::multiline);
00629   *aExtState |= (isMultiLine ? EXT_STATE_MULTI_LINE : EXT_STATE_SINGLE_LINE);
00630   return NS_OK;
00631 }
00632 
00633 NS_IMETHODIMP nsXULTextFieldAccessible::GetState(PRUint32 *aState)
00634 {
00635   *aState = 0;
00636   nsCOMPtr<nsIDOMXULTextBoxElement> textBox(do_QueryInterface(mDOMNode));
00637   if (!textBox) {
00638     return NS_ERROR_FAILURE;
00639   }
00640 
00641   nsCOMPtr<nsIDOMNode> inputField;
00642   textBox->GetInputField(getter_AddRefs(inputField));
00643   if (!inputField) {
00644     return NS_ERROR_FAILURE;
00645   }
00646   // Create a temporary accessible from the HTML text field
00647   // to get the accessible state from. Doesn't add to cache
00648   // because Init() is not called.
00649   nsHTMLTextFieldAccessible tempAccessible(inputField, mWeakShell);
00650   nsresult rv = tempAccessible.GetState(aState);
00651   if (gLastFocusedNode == mDOMNode) {
00652     *aState |= STATE_FOCUSED;
00653   }
00654   return rv;
00655 }
00656 
00660 NS_IMETHODIMP nsXULTextFieldAccessible::GetNumActions(PRUint8 *_retval)
00661 {
00662   *_retval = eSingle_Action;
00663   return NS_OK;;
00664 }
00665 
00669 NS_IMETHODIMP nsXULTextFieldAccessible::GetActionName(PRUint8 index, nsAString& _retval)
00670 {
00671   if (index == eAction_Click) {
00672     nsAccessible::GetTranslatedString(NS_LITERAL_STRING("activate"), _retval); 
00673     return NS_OK;
00674   }
00675   return NS_ERROR_INVALID_ARG;
00676 }
00677 
00681 NS_IMETHODIMP nsXULTextFieldAccessible::DoAction(PRUint8 index)
00682 {
00683   if (index == 0) {
00684     nsCOMPtr<nsIDOMXULElement> element(do_QueryInterface(mDOMNode));
00685     if (element)
00686     {
00687       element->Focus();
00688       return NS_OK;
00689     }
00690     return NS_ERROR_FAILURE;
00691   }
00692   return NS_ERROR_INVALID_ARG;
00693 }