1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is mozilla.org code.
15 : *
16 : * The Initial Developer of the Original Code is
17 : * Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2010
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Alexander Surkov <surkov.alexander@gmail.com> (original author)
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "AccCollector.h"
39 :
40 : #include "nsAccessible.h"
41 :
42 : ////////////////////////////////////////////////////////////////////////////////
43 : // nsAccCollector
44 : ////////////////////////////////////////////////////////////////////////////////
45 :
46 0 : AccCollector::
47 : AccCollector(nsAccessible* aRoot, filters::FilterFuncPtr aFilterFunc) :
48 0 : mFilterFunc(aFilterFunc), mRoot(aRoot), mRootChildIdx(0)
49 : {
50 0 : }
51 :
52 0 : AccCollector::~AccCollector()
53 : {
54 0 : }
55 :
56 : PRUint32
57 0 : AccCollector::Count()
58 : {
59 0 : EnsureNGetIndex(nsnull);
60 0 : return mObjects.Length();
61 : }
62 :
63 : nsAccessible*
64 0 : AccCollector::GetAccessibleAt(PRUint32 aIndex)
65 : {
66 0 : nsAccessible *accessible = mObjects.SafeElementAt(aIndex, nsnull);
67 0 : if (accessible)
68 0 : return accessible;
69 :
70 0 : return EnsureNGetObject(aIndex);
71 : }
72 :
73 : PRInt32
74 0 : AccCollector::GetIndexAt(nsAccessible *aAccessible)
75 : {
76 0 : PRInt32 index = mObjects.IndexOf(aAccessible);
77 0 : if (index != -1)
78 0 : return index;
79 :
80 0 : return EnsureNGetIndex(aAccessible);
81 : }
82 :
83 : ////////////////////////////////////////////////////////////////////////////////
84 : // nsAccCollector protected
85 :
86 : nsAccessible*
87 0 : AccCollector::EnsureNGetObject(PRUint32 aIndex)
88 : {
89 0 : PRInt32 childCount = mRoot->GetChildCount();
90 0 : while (mRootChildIdx < childCount) {
91 0 : nsAccessible* child = mRoot->GetChildAt(mRootChildIdx++);
92 0 : if (!mFilterFunc(child))
93 0 : continue;
94 :
95 0 : AppendObject(child);
96 0 : if (mObjects.Length() - 1 == aIndex)
97 0 : return mObjects[aIndex];
98 : }
99 :
100 0 : return nsnull;
101 : }
102 :
103 : PRInt32
104 0 : AccCollector::EnsureNGetIndex(nsAccessible* aAccessible)
105 : {
106 0 : PRInt32 childCount = mRoot->GetChildCount();
107 0 : while (mRootChildIdx < childCount) {
108 0 : nsAccessible* child = mRoot->GetChildAt(mRootChildIdx++);
109 0 : if (!mFilterFunc(child))
110 0 : continue;
111 :
112 0 : AppendObject(child);
113 0 : if (child == aAccessible)
114 0 : return mObjects.Length() - 1;
115 : }
116 :
117 0 : return -1;
118 : }
119 :
120 : void
121 0 : AccCollector::AppendObject(nsAccessible* aAccessible)
122 : {
123 0 : mObjects.AppendElement(aAccessible);
124 0 : }
125 :
126 : ////////////////////////////////////////////////////////////////////////////////
127 : // EmbeddedObjCollector
128 : ////////////////////////////////////////////////////////////////////////////////
129 :
130 : PRInt32
131 0 : EmbeddedObjCollector::GetIndexAt(nsAccessible *aAccessible)
132 : {
133 0 : if (aAccessible->mParent != mRoot)
134 0 : return -1;
135 :
136 0 : if (aAccessible->mIndexOfEmbeddedChild != -1)
137 0 : return aAccessible->mIndexOfEmbeddedChild;
138 :
139 0 : return mFilterFunc(aAccessible) ? EnsureNGetIndex(aAccessible) : -1;
140 : }
141 :
142 : void
143 0 : EmbeddedObjCollector::AppendObject(nsAccessible* aAccessible)
144 : {
145 0 : aAccessible->mIndexOfEmbeddedChild = mObjects.Length();
146 0 : mObjects.AppendElement(aAccessible);
147 0 : }
|