1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_accessibility.hxx"
30 
31 
32 #include "accessibility/extended/AccessibleGridControlTableBase.hxx"
33 #include <svtools/accessibletable.hxx>
34 #include <tools/multisel.hxx>
35 #include <comphelper/sequence.hxx>
36 
37 // ============================================================================
38 
39 using ::rtl::OUString;
40 
41 using ::com::sun::star::uno::Reference;
42 using ::com::sun::star::uno::Sequence;
43 using ::com::sun::star::uno::Any;
44 
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::accessibility;
47 using namespace ::svt;
48 using namespace ::svt::table;
49 
50 // ============================================================================
51 
52 namespace accessibility {
53 
54 // ============================================================================
55 
56 DBG_NAME( AccessibleGridControlTableBase )
57 
58 AccessibleGridControlTableBase::AccessibleGridControlTableBase(
59         const Reference< XAccessible >& rxParent,
60         IAccessibleTable& rTable,
61         AccessibleTableControlObjType eObjType ) :
62     GridControlAccessibleElement( rxParent, rTable, eObjType )
63 {
64 }
65 
66 AccessibleGridControlTableBase::~AccessibleGridControlTableBase()
67 {
68 }
69 
70 // XAccessibleContext ---------------------------------------------------------
71 
72 sal_Int32 SAL_CALL AccessibleGridControlTableBase::getAccessibleChildCount()
73     throw ( uno::RuntimeException )
74 {
75     TCSolarGuard aSolarGuard;
76     ::osl::MutexGuard aGuard( getOslMutex() );
77     ensureIsAlive();
78     sal_Int32 nChildren = 0;
79     if(m_eObjType == TCTYPE_ROWHEADERBAR)
80 	    nChildren = m_aTable.GetRowCount();
81     else if(m_eObjType == TCTYPE_TABLE)
82 	    nChildren = m_aTable.GetRowCount()*m_aTable.GetColumnCount();
83     else if(m_eObjType == TCTYPE_COLUMNHEADERBAR)
84 	    nChildren = m_aTable.GetColumnCount();
85     return nChildren;
86 }
87 
88 sal_Int16 SAL_CALL AccessibleGridControlTableBase::getAccessibleRole()
89     throw ( uno::RuntimeException )
90 {
91     ensureIsAlive();
92     return AccessibleRole::TABLE;
93 }
94 
95 // XAccessibleTable -----------------------------------------------------------
96 
97 sal_Int32 SAL_CALL AccessibleGridControlTableBase::getAccessibleRowCount()
98     throw ( uno::RuntimeException )
99 {
100     TCSolarGuard aSolarGuard;
101     ::osl::MutexGuard aGuard( getOslMutex() );
102     ensureIsAlive();
103     return  m_aTable.GetRowCount();
104 }
105 
106 sal_Int32 SAL_CALL AccessibleGridControlTableBase::getAccessibleColumnCount()
107     throw ( uno::RuntimeException )
108 {
109     TCSolarGuard aSolarGuard;
110     ::osl::MutexGuard aGuard( getOslMutex() );
111     ensureIsAlive();
112     return m_aTable.GetColumnCount();
113 }
114 
115 sal_Int32 SAL_CALL AccessibleGridControlTableBase::getAccessibleRowExtentAt(
116         sal_Int32 nRow, sal_Int32 nColumn )
117     throw ( lang::IndexOutOfBoundsException, uno::RuntimeException )
118 {
119     TCSolarGuard aSolarGuard;
120     ::osl::MutexGuard aGuard( getOslMutex() );
121     ensureIsAlive();
122     ensureIsValidAddress( nRow, nColumn );
123     return 1;   // merged cells not supported
124 }
125 
126 sal_Int32 SAL_CALL AccessibleGridControlTableBase::getAccessibleColumnExtentAt(
127         sal_Int32 nRow, sal_Int32 nColumn )
128     throw ( lang::IndexOutOfBoundsException, uno::RuntimeException )
129 {
130     TCSolarGuard aSolarGuard;
131     ::osl::MutexGuard aGuard( getOslMutex() );
132     ensureIsAlive();
133     ensureIsValidAddress( nRow, nColumn );
134     return 1;   // merged cells not supported
135 }
136 
137 Reference< XAccessible > SAL_CALL AccessibleGridControlTableBase::getAccessibleCaption()
138     throw ( uno::RuntimeException )
139 {
140     ensureIsAlive();
141     return NULL;    // not supported
142 }
143 
144 Reference< XAccessible > SAL_CALL AccessibleGridControlTableBase::getAccessibleSummary()
145     throw ( uno::RuntimeException )
146 {
147     ensureIsAlive();
148     return NULL;    // not supported
149 }
150 
151 sal_Int32 SAL_CALL AccessibleGridControlTableBase::getAccessibleIndex(
152         sal_Int32 nRow, sal_Int32 nColumn )
153     throw ( lang::IndexOutOfBoundsException, uno::RuntimeException )
154 {
155     TCSolarGuard aSolarGuard;
156     ::osl::MutexGuard aGuard( getOslMutex() );
157     ensureIsAlive();
158     ensureIsValidAddress( nRow, nColumn );
159     return implGetChildIndex( nRow, nColumn );
160 }
161 
162 sal_Int32 SAL_CALL AccessibleGridControlTableBase::getAccessibleRow( sal_Int32 nChildIndex )
163     throw ( lang::IndexOutOfBoundsException, uno::RuntimeException )
164 {
165     TCSolarGuard aSolarGuard;
166     ::osl::MutexGuard aGuard( getOslMutex() );
167     ensureIsAlive();
168     ensureIsValidIndex( nChildIndex );
169     return implGetRow( nChildIndex );
170 }
171 
172 sal_Int32 SAL_CALL AccessibleGridControlTableBase::getAccessibleColumn( sal_Int32 nChildIndex )
173     throw ( lang::IndexOutOfBoundsException, uno::RuntimeException )
174 {
175     TCSolarGuard aSolarGuard;
176     ::osl::MutexGuard aGuard( getOslMutex() );
177     ensureIsAlive();
178     ensureIsValidIndex( nChildIndex );
179     return implGetColumn( nChildIndex );
180 }
181 
182 // XInterface -----------------------------------------------------------------
183 
184 Any SAL_CALL AccessibleGridControlTableBase::queryInterface( const uno::Type& rType )
185     throw ( uno::RuntimeException )
186 {
187     Any aAny( GridControlAccessibleElement::queryInterface( rType ) );
188     return aAny.hasValue() ?
189         aAny : AccessibleGridControlTableImplHelper::queryInterface( rType );
190 }
191 
192 void SAL_CALL AccessibleGridControlTableBase::acquire() throw ()
193 {
194     GridControlAccessibleElement::acquire();
195 }
196 
197 void SAL_CALL AccessibleGridControlTableBase::release() throw ()
198 {
199     GridControlAccessibleElement::release();
200 }
201 
202 // XTypeProvider --------------------------------------------------------------
203 
204 Sequence< uno::Type > SAL_CALL AccessibleGridControlTableBase::getTypes()
205     throw ( uno::RuntimeException )
206 {
207     return ::comphelper::concatSequences(
208         GridControlAccessibleElement::getTypes(),
209         AccessibleGridControlTableImplHelper::getTypes() );
210 }
211 
212 Sequence< sal_Int8 > SAL_CALL AccessibleGridControlTableBase::getImplementationId()
213     throw ( uno::RuntimeException )
214 {
215     ::osl::MutexGuard aGuard( getOslGlobalMutex() );
216     static Sequence< sal_Int8 > aId;
217     implCreateUuid( aId );
218     return aId;
219 }
220 
221 // internal helper methods ----------------------------------------------------
222 
223 sal_Int32 AccessibleGridControlTableBase::implGetChildCount() const
224 {
225     return m_aTable.GetRowCount()*m_aTable.GetColumnCount();
226 }
227 
228 sal_Int32 AccessibleGridControlTableBase::implGetRow( sal_Int32 nChildIndex ) const
229 {
230     sal_Int32 nColumns = m_aTable.GetColumnCount();
231     return nColumns ? (nChildIndex / nColumns) : 0;
232 }
233 
234 sal_Int32 AccessibleGridControlTableBase::implGetColumn( sal_Int32 nChildIndex ) const
235 {
236     sal_Int32 nColumns = m_aTable.GetColumnCount();
237     return nColumns ? (nChildIndex % nColumns) : 0;
238 }
239 
240 sal_Int32 AccessibleGridControlTableBase::implGetChildIndex(
241         sal_Int32 nRow, sal_Int32 nColumn ) const
242 {
243     return nRow * m_aTable.GetColumnCount() + nColumn;
244 }
245 
246 void AccessibleGridControlTableBase::implGetSelectedRows( Sequence< sal_Int32 >& rSeq )
247 {
248     sal_Int32 const selectionCount( m_aTable.GetSelectedRowCount() );
249     rSeq.realloc( selectionCount );
250     for ( sal_Int32 i=0; i<selectionCount; ++i )
251         rSeq[i] = m_aTable.GetSelectedRowIndex(i);
252 }
253 
254 void AccessibleGridControlTableBase::ensureIsValidRow( sal_Int32 nRow )
255     throw ( lang::IndexOutOfBoundsException )
256 {
257     if( nRow >= m_aTable.GetRowCount() )
258         throw lang::IndexOutOfBoundsException(
259             OUString( RTL_CONSTASCII_USTRINGPARAM( "row index is invalid" ) ), *this );
260 }
261 
262 void AccessibleGridControlTableBase::ensureIsValidColumn( sal_Int32 nColumn )
263     throw ( lang::IndexOutOfBoundsException )
264 {
265     if( nColumn >= m_aTable.GetColumnCount() )
266         throw lang::IndexOutOfBoundsException(
267             OUString( RTL_CONSTASCII_USTRINGPARAM("column index is invalid") ), *this );
268 }
269 
270 void AccessibleGridControlTableBase::ensureIsValidAddress(
271         sal_Int32 nRow, sal_Int32 nColumn )
272     throw ( lang::IndexOutOfBoundsException )
273 {
274     ensureIsValidRow( nRow );
275     ensureIsValidColumn( nColumn );
276 }
277 
278 void AccessibleGridControlTableBase::ensureIsValidIndex( sal_Int32 nChildIndex )
279     throw ( lang::IndexOutOfBoundsException )
280 {
281     if( nChildIndex >= implGetChildCount() )
282         throw lang::IndexOutOfBoundsException(
283             OUString( RTL_CONSTASCII_USTRINGPARAM("child index is invalid") ), *this );
284 }
285 
286 // ============================================================================
287 
288 } // namespace accessibility
289 
290 // ============================================================================
291 
292