Back to index

lightning-sunbird  0.9+nobinonly
nsXULTreeAccessibleWrap.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: NPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public
00006  * License Version 1.1 (the "License"); you may not use this file
00007  * except in compliance with the License. You may obtain a copy of
00008  * the License at http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS
00011  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
00012  * implied. See the License for the specific language governing
00013  * rights and limitations under the License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is Netscape Corp.
00018  * Portions created by Netscape Corp.are Copyright (C) 2003 Netscape
00019  * Corp. All Rights Reserved.
00020  *
00021  * Original Author: Aaron Leventhal
00022  *
00023  * Contributor(s):
00024  *
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * 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 NPL, 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 NPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsXULTreeAccessibleWrap.h"
00041 #include "nsTextFormatter.h"
00042 #include "nsIFrame.h"
00043 
00044 // --------------------------------------------------------
00045 // nsXULTreeAccessibleWrap
00046 // --------------------------------------------------------
00047 
00048 nsXULTreeAccessibleWrap::nsXULTreeAccessibleWrap(nsIDOMNode *aDOMNode, nsIWeakReference *aShell):
00049 nsXULTreeAccessible(aDOMNode, aShell)
00050 {
00051 }
00052 
00053 NS_IMETHODIMP nsXULTreeAccessibleWrap::GetRole(PRUint32 *aRole)
00054 {
00055   NS_ASSERTION(mTree, "No tree view");
00056 
00057   nsCOMPtr<nsITreeColumns> cols;
00058   mTree->GetColumns(getter_AddRefs(cols));
00059   nsCOMPtr<nsITreeColumn> primaryCol;
00060   if (cols) {
00061     cols->GetPrimaryColumn(getter_AddRefs(primaryCol));
00062   }
00063   // No primary column means we're in a list
00064   // In fact, history and mail turn off the primary flag when switching to a flat view
00065   *aRole = primaryCol ? ROLE_OUTLINE : ROLE_LIST;
00066 
00067   return NS_OK;
00068 }
00069 
00070 // --------------------------------------------------------
00071 // nsXULTreeitemAccessibleWrap Accessible
00072 // --------------------------------------------------------
00073 
00074 nsXULTreeitemAccessibleWrap::nsXULTreeitemAccessibleWrap(nsIAccessible *aParent, 
00075                                                          nsIDOMNode *aDOMNode, 
00076                                                          nsIWeakReference *aShell, 
00077                                                          PRInt32 aRow, 
00078                                                          nsITreeColumn* aColumn) :
00079 nsXULTreeitemAccessible(aParent, aDOMNode, aShell, aRow, aColumn)
00080 {
00081 }
00082 
00083 NS_IMETHODIMP nsXULTreeitemAccessibleWrap::GetRole(PRUint32 *aRole)
00084 {
00085   // No primary column means we're in a list
00086   // In fact, history and mail turn off the primary flag when switching to a flat view
00087   NS_ASSERTION(mColumn, "mColumn is null");
00088   PRBool isPrimary = PR_FALSE;
00089   mColumn->GetPrimary(&isPrimary);
00090   *aRole = isPrimary ? ROLE_OUTLINEITEM : ROLE_LISTITEM;
00091   return NS_OK;
00092 }
00093 
00094 NS_IMETHODIMP nsXULTreeitemAccessibleWrap::GetBounds(PRInt32 *x, PRInt32 *y, PRInt32 *width, PRInt32 *height)
00095 {
00096   nsresult rv = nsXULTreeitemAccessible::GetBounds(x, y, width, height);
00097   if (NS_FAILED(rv)) {
00098     return rv;
00099   }
00100   nsIFrame *frame = GetFrame();
00101   if (frame) {
00102     // Will subtract first cell's start x from total width
00103     PRInt32 cellStartX, cellStartY;
00104     mTree->GetCoordsForCellItem(mRow, mColumn, EmptyCString(), &cellStartX, &cellStartY, width, height);
00105     // Use entire row width, not just key column's width
00106     float t2p = GetPresContext()->TwipsToPixels();
00107     *width = NSTwipsToIntPixels(frame->GetRect().width, t2p) - cellStartX;
00108   }
00109   return NS_OK;
00110 }
00111 
00112 NS_IMETHODIMP nsXULTreeitemAccessibleWrap::GetName(nsAString& aName)
00113 {
00114   nsCOMPtr<nsITreeColumns> cols;
00115   mTree->GetColumns(getter_AddRefs(cols));
00116   if (!cols) {
00117     return NS_OK;
00118   }
00119   nsCOMPtr<nsITreeColumn> column;
00120   cols->GetFirstColumn(getter_AddRefs(column));
00121   while (column) {
00122     nsAutoString colText;
00123     mTreeView->GetCellText(mRow, column, colText);
00124     aName += colText + NS_LITERAL_STRING("  ");
00125     nsCOMPtr<nsITreeColumn> nextColumn;
00126     column->GetNext(getter_AddRefs(nextColumn));
00127     column = nextColumn;
00128   }
00129   return NS_OK;
00130 }
00131 
00132 NS_IMETHODIMP nsXULTreeitemAccessibleWrap::GetDescription(nsAString& aDescription)
00133 {
00134   if (!mParent || !mWeakShell || !mTreeView) {
00135     return NS_ERROR_FAILURE;
00136   }
00137 
00138   PRUint32 itemRole;
00139   GetRole(&itemRole);
00140   if (itemRole == ROLE_LISTITEM) {
00141     return nsAccessibleWrap::GetDescription(aDescription);
00142   }
00143 
00144   aDescription.Truncate();
00145 
00146   PRInt32 level;
00147   if (NS_FAILED(mTreeView->GetLevel(mRow, &level))) {
00148     return NS_OK;
00149   }
00150 
00151   PRInt32 testRow = -1;
00152   if (level > 0) {
00153     mTreeView->GetParentIndex(mRow, &testRow);
00154   }
00155 
00156   PRInt32 numRows;
00157   mTreeView->GetRowCount(&numRows);
00158 
00159   PRInt32 indexInParent = 0, numSiblings = 0;
00160 
00161   // Get the index in parent and number of siblings
00162   while (++ testRow < numRows) {
00163     PRInt32 testLevel = 0;
00164     mTreeView->GetLevel(testRow, &testLevel);
00165     if (testLevel == level) {
00166       if (testRow <= mRow) {
00167         ++indexInParent;
00168       }
00169       ++numSiblings;
00170     }
00171     else if (testLevel < level) {
00172       break;
00173     }
00174   }
00175 
00176   // Count the number of children
00177   testRow = mRow;
00178   PRInt32 numChildren = 0;
00179   while (++ testRow < numRows) {
00180     PRInt32 testLevel = 0;
00181     mTreeView->GetLevel(testRow, &testLevel);
00182     if (testLevel <= level) {
00183       break;
00184     }
00185     else if (testLevel == level + 1) {
00186       ++ numChildren;
00187     }
00188   }
00189 
00190   // Don't localize "of" or "with" -- that is just the format of
00191   // the string, and are parsed out by the AT
00192   nsTextFormatter::ssprintf(aDescription, NS_LITERAL_STRING("L%d, %d of %d with %d").get(),
00193                             level + 1, indexInParent, numSiblings, numChildren);
00194 
00195   return NS_OK;
00196 }
00197