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 #include "accessibility/extended/AccessibleGridControlTableCell.hxx"
32 #include <svtools/accessibletable.hxx>
33 #include "accessibility/extended/AccessibleGridControl.hxx"
34 #include <tools/gen.hxx>
35 #include <toolkit/helper/vclunohelper.hxx>
36 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
37 
38 namespace accessibility
39 {
40 	namespace
41 	{
42 		void checkIndex_Impl( sal_Int32 _nIndex, const ::rtl::OUString& _sText ) throw (::com::sun::star::lang::IndexOutOfBoundsException)
43 		{
44 			if ( _nIndex >= _sText.getLength() )
45 				throw ::com::sun::star::lang::IndexOutOfBoundsException();
46 		}
47 
48 		sal_Int32 getIndex_Impl( sal_Int32 _nRow, sal_uInt16 _nColumn, sal_uInt16 _nColumnCount )
49 		{
50 			return _nRow * _nColumnCount + _nColumn;
51 		}
52 	}
53 	using namespace ::com::sun::star::lang;
54 	using namespace utl;
55 	using namespace comphelper;
56 	using ::rtl::OUString;
57 	using ::accessibility::AccessibleGridControl;
58 	using namespace ::com::sun::star::uno;
59 	using ::com::sun::star::accessibility::XAccessible;
60 	using namespace ::com::sun::star::accessibility;
61 	using namespace ::svt;
62 	using namespace ::svt::table;
63 
64 
65 	// =============================================================================
66 	// = AccessibleGridControlCell
67 	// =============================================================================
68 	// -----------------------------------------------------------------------------
69 	AccessibleGridControlCell::AccessibleGridControlCell(
70 			const Reference< XAccessible >& _rxParent, IAccessibleTable& _rTable,
71             sal_Int32 _nRowPos, sal_uInt16 _nColPos, AccessibleTableControlObjType _eType )
72         :AccessibleGridControlBase( _rxParent, _rTable, _eType )
73 		,m_nRowPos( _nRowPos )
74 		,m_nColPos( _nColPos )
75 	{
76 		// set accessible name here, because for that we need the position of the cell
77 		// and so the base class isn't capable of doing this
78 		::rtl::OUString aAccName;
79 		if(_eType == TCTYPE_TABLECELL)
80 			aAccName = _rTable.GetAccessibleObjectName( TCTYPE_TABLECELL, _nRowPos, _nColPos );
81 		else if(_eType == TCTYPE_ROWHEADERCELL)
82 			aAccName = _rTable.GetAccessibleObjectName( TCTYPE_ROWHEADERCELL, _nRowPos, 0 );
83 		else if(_eType == TCTYPE_COLUMNHEADERCELL)
84 			aAccName = _rTable.GetAccessibleObjectName( TCTYPE_COLUMNHEADERCELL, 0, _nRowPos );
85 		implSetName( aAccName );
86 	}
87 
88 	// -----------------------------------------------------------------------------
89 	AccessibleGridControlCell::~AccessibleGridControlCell()
90 	{
91 	}
92 
93 	// -----------------------------------------------------------------------------
94 	void SAL_CALL AccessibleGridControlCell::grabFocus() throw ( RuntimeException )
95 	{
96 		TCSolarGuard aSolarGuard;
97 		::osl::MutexGuard aGuard( getOslMutex() );
98 		m_aTable.GoToCell( m_nColPos, m_nRowPos );
99 	}
100 	//// -----------------------------------------------------------------------------
101 	// implementation of a table cell
102 	::rtl::OUString AccessibleGridControlTableCell::implGetText()
103 	{
104 		ensureIsAlive();
105 		return m_aTable.GetAccessibleCellText( getRowPos(),  getColumnPos() );
106 	}
107 
108 	::com::sun::star::lang::Locale AccessibleGridControlTableCell::implGetLocale()
109 	{
110 		ensureIsAlive();
111 		return m_aTable.GetAccessible()->getAccessibleContext()->getLocale();
112 	}
113 
114 	void AccessibleGridControlTableCell::implGetSelection( sal_Int32& nStartIndex, sal_Int32& nEndIndex )
115 	{
116 		nStartIndex = 0;
117 		nEndIndex = 0;
118 	}
119 
120 	AccessibleGridControlTableCell::AccessibleGridControlTableCell(const Reference<XAccessible >& _rxParent,
121 								IAccessibleTable& _rTable,
122 								sal_Int32 _nRowPos,
123 								sal_uInt16 _nColPos,
124 								AccessibleTableControlObjType  eObjType)
125 		:AccessibleGridControlCell( _rxParent, _rTable, _nRowPos, _nColPos, eObjType )
126 	{
127 	}
128 
129 	// XInterface -------------------------------------------------------------
130 
131 	/** Queries for a new interface. */
132 	::com::sun::star::uno::Any SAL_CALL AccessibleGridControlTableCell::queryInterface(
133 			const ::com::sun::star::uno::Type& rType )
134 		throw ( ::com::sun::star::uno::RuntimeException )
135 	{
136 		Any aRet = AccessibleGridControlCell::queryInterface(rType);
137 		if ( !aRet.hasValue() )
138 			aRet = AccessibleTextHelper_BASE::queryInterface(rType);
139 		return aRet;
140 	}
141 
142 	/** Aquires the object (calls acquire() on base class). */
143 	void SAL_CALL AccessibleGridControlTableCell::acquire() throw ()
144 	{
145 		AccessibleGridControlCell::acquire();
146 	}
147 
148 	/** Releases the object (calls release() on base class). */
149 	void SAL_CALL AccessibleGridControlTableCell::release() throw ()
150 	{
151 		AccessibleGridControlCell::release();
152 	}
153 
154 	::com::sun::star::awt::Rectangle SAL_CALL AccessibleGridControlTableCell::getCharacterBounds( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
155 	{
156 		TCSolarGuard aSolarGuard;
157 		::osl::MutexGuard aGuard( getOslMutex() );
158 
159 		ensureIsAlive();
160 		if ( !implIsValidIndex( nIndex, implGetText().getLength() ) )
161 			throw IndexOutOfBoundsException();
162 
163 		::com::sun::star::awt::Rectangle aRect;
164 
165 		if ( &m_aTable )
166 			aRect = AWTRectangle( m_aTable.GetFieldCharacterBounds( getRowPos(), getColumnPos(), nIndex ) );
167 		return aRect;
168 	}
169 
170 	sal_Int32 SAL_CALL AccessibleGridControlTableCell::getIndexAtPoint( const ::com::sun::star::awt::Point& _aPoint ) throw (RuntimeException)
171 	{
172 		TCSolarGuard aSolarGuard;
173 		::osl::MutexGuard aGuard( getOslMutex() );
174 		ensureIsAlive();
175 
176 		return m_aTable.GetFieldIndexAtPoint( getRowPos(), getColumnPos(), VCLPoint( _aPoint ) );
177 	}
178 
179 	/** @return
180 			The name of this class.
181 	*/
182 	::rtl::OUString SAL_CALL AccessibleGridControlTableCell::getImplementationName()
183 		throw ( ::com::sun::star::uno::RuntimeException )
184 	{
185 		return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.accessibility.AccessibleGridControlTableCell" ) );
186 	}
187 
188 	/** @return  The count of visible children. */
189 	sal_Int32 SAL_CALL AccessibleGridControlTableCell::getAccessibleChildCount()
190 		throw ( ::com::sun::star::uno::RuntimeException )
191 	{
192 		return 0;
193 	}
194 
195 	/** @return  The XAccessible interface of the specified child. */
196 	::com::sun::star::uno::Reference<
197 		::com::sun::star::accessibility::XAccessible > SAL_CALL
198 		AccessibleGridControlTableCell::getAccessibleChild( sal_Int32 )
199 			throw ( ::com::sun::star::lang::IndexOutOfBoundsException,
200 					::com::sun::star::uno::RuntimeException )
201 	{
202 		throw ::com::sun::star::lang::IndexOutOfBoundsException();
203 	}
204 
205 	/** Creates a new AccessibleStateSetHelper and fills it with states of the
206 		current object.
207 		@return
208 			A filled AccessibleStateSetHelper.
209 	*/
210 	::utl::AccessibleStateSetHelper* AccessibleGridControlTableCell::implCreateStateSetHelper()
211 	{
212 		TCSolarGuard aSolarGuard;
213 		::osl::MutexGuard aGuard( getOslMutex() );
214 
215 		::utl::AccessibleStateSetHelper* pStateSetHelper = new ::utl::AccessibleStateSetHelper;
216 
217 		if( isAlive() )
218 		{
219 			// SHOWING done with mxParent
220 			if( implIsShowing() )
221 				pStateSetHelper->AddState( AccessibleStateType::SHOWING );
222 
223 			m_aTable.FillAccessibleStateSetForCell( *pStateSetHelper, getRowPos(), static_cast< sal_uInt16 >( getColumnPos() ) );
224 		}
225 		else
226 			pStateSetHelper->AddState( AccessibleStateType::DEFUNC );
227 
228 		return pStateSetHelper;
229 	}
230 
231 
232 	// XAccessible ------------------------------------------------------------
233 
234 	/** @return  The XAccessibleContext interface of this object. */
235 	Reference< XAccessibleContext > SAL_CALL AccessibleGridControlTableCell::getAccessibleContext() throw ( RuntimeException )
236 	{
237 		ensureIsAlive();
238 		return this;
239 	}
240 
241 	// XAccessibleContext -----------------------------------------------------
242 
243 	sal_Int32 SAL_CALL AccessibleGridControlTableCell::getAccessibleIndexInParent()
244 			throw ( ::com::sun::star::uno::RuntimeException )
245 	{
246 		TCSolarGuard aSolarGuard;
247 		::osl::MutexGuard aGuard( getOslMutex() );
248 		ensureIsAlive();
249 
250         return ( getRowPos() * m_aTable.GetColumnCount() ) + getColumnPos();
251 	}
252 
253 	sal_Int32 SAL_CALL AccessibleGridControlTableCell::getCaretPosition(  ) throw (::com::sun::star::uno::RuntimeException)
254 	{
255 		return -1;
256 	}
257 	sal_Bool SAL_CALL AccessibleGridControlTableCell::setCaretPosition ( sal_Int32 nIndex ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException)
258 	{
259 		TCSolarGuard aSolarGuard;
260 		::osl::MutexGuard aGuard( getOslMutex() );
261 
262 		if ( !implIsValidRange( nIndex, nIndex, implGetText().getLength() ) )
263 			throw IndexOutOfBoundsException();
264 
265 		return sal_False;
266 	}
267 	sal_Unicode SAL_CALL AccessibleGridControlTableCell::getCharacter( sal_Int32 nIndex ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException)
268 	{
269 		TCSolarGuard aSolarGuard;
270 		::osl::MutexGuard aGuard( getOslMutex() );
271 		return OCommonAccessibleText::getCharacter( nIndex );
272 	}
273 	::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > SAL_CALL AccessibleGridControlTableCell::getCharacterAttributes( sal_Int32 nIndex, const ::com::sun::star::uno::Sequence< ::rtl::OUString >& ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException)
274 	{
275 		TCSolarGuard aSolarGuard;
276 		::osl::MutexGuard aGuard( getOslMutex() );
277 
278 		::rtl::OUString sText( implGetText() );
279 
280 		if ( !implIsValidIndex( nIndex, sText.getLength() ) )
281 			throw IndexOutOfBoundsException();
282 
283 		return ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >();
284 	}
285 	sal_Int32 SAL_CALL AccessibleGridControlTableCell::getCharacterCount(  ) throw (::com::sun::star::uno::RuntimeException)
286 	{
287 		TCSolarGuard aSolarGuard;
288 		::osl::MutexGuard aGuard( getOslMutex() );
289 		return OCommonAccessibleText::getCharacterCount(  );
290 	}
291 
292 	::rtl::OUString SAL_CALL AccessibleGridControlTableCell::getSelectedText(  ) throw (::com::sun::star::uno::RuntimeException)
293 	{
294 		TCSolarGuard aSolarGuard;
295 		::osl::MutexGuard aGuard( getOslMutex() );
296 		return OCommonAccessibleText::getSelectedText(  );
297 	}
298 	sal_Int32 SAL_CALL AccessibleGridControlTableCell::getSelectionStart(  ) throw (::com::sun::star::uno::RuntimeException)
299 	{
300 		TCSolarGuard aSolarGuard;
301 		::osl::MutexGuard aGuard( getOslMutex() );
302 		return OCommonAccessibleText::getSelectionStart(  );
303 	}
304 	sal_Int32 SAL_CALL AccessibleGridControlTableCell::getSelectionEnd(  ) throw (::com::sun::star::uno::RuntimeException)
305 	{
306 		TCSolarGuard aSolarGuard;
307 		::osl::MutexGuard aGuard( getOslMutex() );
308 		return OCommonAccessibleText::getSelectionEnd(  );
309 	}
310 	sal_Bool SAL_CALL AccessibleGridControlTableCell::setSelection( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException)
311 	{
312 		TCSolarGuard aSolarGuard;
313 		::osl::MutexGuard aGuard( getOslMutex() );
314 		if ( !implIsValidRange( nStartIndex, nEndIndex, implGetText().getLength() ) )
315 			throw IndexOutOfBoundsException();
316 
317 		return sal_False;
318 	}
319 	::rtl::OUString SAL_CALL AccessibleGridControlTableCell::getText(  ) throw (::com::sun::star::uno::RuntimeException)
320 	{
321 		TCSolarGuard aSolarGuard;
322 		::osl::MutexGuard aGuard( getOslMutex() );
323 		return OCommonAccessibleText::getText(  );
324 	}
325 	::rtl::OUString SAL_CALL AccessibleGridControlTableCell::getTextRange( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException)
326 	{
327 		TCSolarGuard aSolarGuard;
328 		::osl::MutexGuard aGuard( getOslMutex() );
329 		return OCommonAccessibleText::getTextRange( nStartIndex, nEndIndex );
330 	}
331 	::com::sun::star::accessibility::TextSegment SAL_CALL AccessibleGridControlTableCell::getTextAtIndex( sal_Int32 nIndex, sal_Int16 aTextType ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException)
332 	{
333 		TCSolarGuard aSolarGuard;
334 		::osl::MutexGuard aGuard( getOslMutex() );
335 		return OCommonAccessibleText::getTextAtIndex( nIndex ,aTextType);
336 	}
337 	::com::sun::star::accessibility::TextSegment SAL_CALL AccessibleGridControlTableCell::getTextBeforeIndex( sal_Int32 nIndex, sal_Int16 aTextType ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException)
338 	{
339 		TCSolarGuard aSolarGuard;
340 		::osl::MutexGuard aGuard( getOslMutex() );
341 		return OCommonAccessibleText::getTextBeforeIndex( nIndex ,aTextType);
342 	}
343 	::com::sun::star::accessibility::TextSegment SAL_CALL AccessibleGridControlTableCell::getTextBehindIndex( sal_Int32 nIndex, sal_Int16 aTextType ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException)
344 	{
345 		TCSolarGuard aSolarGuard;
346 		::osl::MutexGuard aGuard( getOslMutex() );
347 		return OCommonAccessibleText::getTextBehindIndex( nIndex ,aTextType);
348 	}
349 	sal_Bool SAL_CALL AccessibleGridControlTableCell::copyText( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException)
350 	{
351 		TCSolarGuard aSolarGuard;
352 		::osl::MutexGuard aGuard( getOslMutex() );
353 		::rtl::OUString sText = implGetText();
354 		checkIndex_Impl( nStartIndex, sText );
355 		checkIndex_Impl( nEndIndex, sText );
356 
357 		//!!! don't know how to put a string into the clipboard
358 		return sal_False;
359 	}
360 
361 	Rectangle AccessibleGridControlTableCell::implGetBoundingBox()
362 	{
363 		Window* pParent = m_aTable.GetAccessibleParentWindow();
364 		DBG_ASSERT( pParent, "implGetBoundingBox - missing parent window" );
365 		Rectangle aGridRect = m_aTable.GetWindowExtentsRelative( pParent );
366 		sal_Int32 nIndex = getAccessibleIndexInParent();
367 		Rectangle aCellRect = m_aTable.calcCellRect(nIndex%m_aTable.GetColumnCount(), nIndex/m_aTable.GetColumnCount());
368 		long nX = aGridRect.Left() + aCellRect.Left();
369 		long nY = aGridRect.Top() + aCellRect.Top();
370 		Rectangle aCell( Point( nX, nY ), aCellRect.GetSize());
371 		return aCell;
372 	}
373 	// -----------------------------------------------------------------------------
374 	Rectangle AccessibleGridControlTableCell::implGetBoundingBoxOnScreen()
375 	{
376 		Rectangle aGridRect = m_aTable.GetWindowExtentsRelative( NULL );
377 		sal_Int32 nIndex = getAccessibleIndexInParent();
378 		Rectangle aCellRect = m_aTable.calcCellRect(nIndex%m_aTable.GetColumnCount(), nIndex/m_aTable.GetColumnCount());
379 		long nX = aGridRect.Left() + aCellRect.Left();
380 		long nY = aGridRect.Top() + aCellRect.Top();
381 		Rectangle aCell( Point( nX, nY ), aCellRect.GetSize());
382 		return aCell;
383 	}
384 }
385