Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <osl/mutex.hxx>
21 : #include <com/sun/star/accessibility/AccessibleRole.hpp>
22 : #include <com/sun/star/accessibility/AccessibleStateType.hpp>
23 : #include <com/sun/star/accessibility/AccessibleEventId.hpp>
24 : #include <unotools/accessiblestatesethelper.hxx>
25 : #include <comphelper/servicehelper.hxx>
26 : #include <cppuhelper/supportsservice.hxx>
27 : #include <vcl/svapp.hxx>
28 : #include <cellfrm.hxx>
29 : #include <tabfrm.hxx>
30 : #include <swtable.hxx>
31 : #include "crsrsh.hxx"
32 : #include "viscrs.hxx"
33 : #include <accfrmobj.hxx>
34 : #include <accfrmobjslist.hxx>
35 : #include "frmfmt.hxx"
36 : #include "cellatr.hxx"
37 : #include "accmap.hxx"
38 : #include <acccell.hxx>
39 :
40 : #include <cfloat>
41 : #include <limits.h>
42 :
43 : #include <ndtxt.hxx>
44 : #include <editeng/brushitem.hxx>
45 : #include <swatrset.hxx>
46 : #include <frmatr.hxx>
47 : #include "acctable.hxx"
48 :
49 : using namespace ::com::sun::star;
50 : using namespace ::com::sun::star::accessibility;
51 : using namespace sw::access;
52 :
53 : const sal_Char sServiceName[] = "com.sun.star.table.AccessibleCellView";
54 : const sal_Char sImplementationName[] = "com.sun.star.comp.Writer.SwAccessibleCellView";
55 :
56 39 : sal_Bool SwAccessibleCell::IsSelected()
57 : {
58 39 : sal_Bool bRet = sal_False;
59 :
60 : OSL_ENSURE( GetMap(), "no map?" );
61 39 : const SwViewShell *pVSh = GetMap()->GetShell();
62 : OSL_ENSURE( pVSh, "no shell?" );
63 39 : if( pVSh->ISA( SwCrsrShell ) )
64 : {
65 39 : const SwCrsrShell *pCSh = static_cast< const SwCrsrShell * >( pVSh );
66 39 : if( pCSh->IsTableMode() )
67 : {
68 : const SwCellFrm *pCFrm =
69 29 : static_cast< const SwCellFrm * >( GetFrm() );
70 : SwTableBox *pBox =
71 29 : const_cast< SwTableBox *>( pCFrm->GetTabBox() );
72 29 : SwSelBoxes const& rBoxes(pCSh->GetTableCrsr()->GetSelectedBoxes());
73 29 : bRet = rBoxes.find(pBox) != rBoxes.end();
74 : }
75 : }
76 :
77 39 : return bRet;
78 : }
79 :
80 4 : void SwAccessibleCell::GetStates( ::utl::AccessibleStateSetHelper& rStateSet )
81 : {
82 4 : SwAccessibleContext::GetStates( rStateSet );
83 :
84 : // SELECTABLE
85 4 : const SwViewShell *pVSh = GetMap()->GetShell();
86 : OSL_ENSURE( pVSh, "no shell?" );
87 4 : if( pVSh->ISA( SwCrsrShell ) )
88 4 : rStateSet.AddState( AccessibleStateType::SELECTABLE );
89 : //Add resizable state to table cell.
90 4 : rStateSet.AddState( AccessibleStateType::RESIZABLE );
91 :
92 : // SELECTED
93 4 : if( IsSelected() )
94 : {
95 0 : rStateSet.AddState( AccessibleStateType::SELECTED );
96 : OSL_ENSURE( bIsSelected, "bSelected out of sync" );
97 0 : ::rtl::Reference < SwAccessibleContext > xThis( this );
98 0 : GetMap()->SetCursorContext( xThis );
99 : }
100 4 : }
101 :
102 10 : SwAccessibleCell::SwAccessibleCell( SwAccessibleMap *pInitMap,
103 : const SwCellFrm *pCellFrm )
104 : : SwAccessibleContext( pInitMap, AccessibleRole::TABLE_CELL, pCellFrm )
105 : , aSelectionHelper( *this )
106 10 : , bIsSelected( sal_False )
107 : {
108 10 : SolarMutexGuard aGuard;
109 20 : OUString sBoxName( pCellFrm->GetTabBox()->GetName() );
110 10 : SetName( sBoxName );
111 :
112 10 : bIsSelected = IsSelected();
113 :
114 : css::uno::Reference<css::accessibility::XAccessible> xTableReference(
115 20 : getAccessibleParent());
116 : css::uno::Reference<css::accessibility::XAccessibleContext> xContextTable(
117 20 : xTableReference, css::uno::UNO_QUERY);
118 : SAL_WARN_IF(
119 : (!xContextTable.is()
120 : || xContextTable->getAccessibleRole() != AccessibleRole::TABLE),
121 : "sw.core", "bad accessible context");
122 20 : m_pAccTable = static_cast<SwAccessibleTable *>(xTableReference.get());
123 10 : }
124 :
125 15 : sal_Bool SwAccessibleCell::_InvalidateMyCursorPos()
126 : {
127 15 : sal_Bool bNew = IsSelected();
128 : sal_Bool bOld;
129 : {
130 15 : osl::MutexGuard aGuard( aMutex );
131 15 : bOld = bIsSelected;
132 15 : bIsSelected = bNew;
133 : }
134 15 : if( bNew )
135 : {
136 : // remember that object as the one that has the caret. This is
137 : // necessary to notify that object if the cursor leaves it.
138 13 : ::rtl::Reference < SwAccessibleContext > xThis( this );
139 13 : GetMap()->SetCursorContext( xThis );
140 : }
141 :
142 15 : sal_Bool bChanged = bOld != bNew;
143 15 : if( bChanged )
144 : {
145 5 : FireStateChangedEvent( AccessibleStateType::SELECTED, bNew );
146 5 : if (m_pAccTable.is())
147 : {
148 5 : m_pAccTable->AddSelectionCell(this,bNew);
149 : }
150 : }
151 15 : return bChanged;
152 : }
153 :
154 30 : sal_Bool SwAccessibleCell::_InvalidateChildrenCursorPos( const SwFrm *pFrm )
155 : {
156 30 : sal_Bool bChanged = sal_False;
157 :
158 30 : const SwAccessibleChildSList aVisList( GetVisArea(), *pFrm, *GetMap() );
159 30 : SwAccessibleChildSList::const_iterator aIter( aVisList.begin() );
160 120 : while( aIter != aVisList.end() )
161 : {
162 60 : const SwAccessibleChild& rLower = *aIter;
163 60 : const SwFrm *pLower = rLower.GetSwFrm();
164 60 : if( pLower )
165 : {
166 60 : if( rLower.IsAccessible( GetMap()->GetShell()->IsPreview() ) )
167 : {
168 : ::rtl::Reference< SwAccessibleContext > xAccImpl(
169 40 : GetMap()->GetContextImpl( pLower, sal_False ) );
170 40 : if( xAccImpl.is() )
171 : {
172 : OSL_ENSURE( xAccImpl->GetFrm()->IsCellFrm(),
173 : "table child is not a cell frame" );
174 : bChanged = static_cast< SwAccessibleCell *>(
175 15 : xAccImpl.get() )->_InvalidateMyCursorPos();
176 : }
177 : else
178 25 : bChanged = sal_True; // If the context is not know we
179 : // don't know whether the selection
180 : // changed or not.
181 : }
182 : else
183 : {
184 : // This is a box with sub rows.
185 20 : bChanged |= _InvalidateChildrenCursorPos( pLower );
186 : }
187 : }
188 60 : ++aIter;
189 : }
190 :
191 30 : return bChanged;
192 : }
193 :
194 10 : void SwAccessibleCell::_InvalidateCursorPos()
195 : {
196 10 : if (IsSelected())
197 : {
198 8 : const SwAccessibleChild aChild( GetChild( *(GetMap()), 0 ) );
199 8 : if( aChild.IsValid() && aChild.GetSwFrm() )
200 : {
201 8 : ::rtl::Reference < SwAccessibleContext > xChildImpl( GetMap()->GetContextImpl( aChild.GetSwFrm()) );
202 8 : if (xChildImpl.is())
203 : {
204 8 : AccessibleEventObject aEvent;
205 8 : aEvent.EventId = AccessibleEventId::STATE_CHANGED;
206 8 : aEvent.NewValue <<= AccessibleStateType::FOCUSED;
207 8 : xChildImpl->FireAccessibleEvent( aEvent );
208 8 : }
209 : }
210 : }
211 :
212 10 : const SwFrm *pParent = GetParent( SwAccessibleChild(GetFrm()), IsInPagePreview() );
213 : OSL_ENSURE( pParent->IsTabFrm(), "parent is not a tab frame" );
214 10 : const SwTabFrm *pTabFrm = static_cast< const SwTabFrm * >( pParent );
215 10 : if( pTabFrm->IsFollow() )
216 0 : pTabFrm = pTabFrm->FindMaster();
217 :
218 30 : while( pTabFrm )
219 : {
220 10 : _InvalidateChildrenCursorPos( pTabFrm );
221 10 : pTabFrm = pTabFrm->GetFollow();
222 : }
223 10 : if (m_pAccTable.is())
224 : {
225 10 : m_pAccTable->FireSelectionEvent();
226 : }
227 10 : }
228 :
229 10 : sal_Bool SwAccessibleCell::HasCursor()
230 : {
231 10 : osl::MutexGuard aGuard( aMutex );
232 10 : return bIsSelected;
233 : }
234 :
235 20 : SwAccessibleCell::~SwAccessibleCell()
236 : {
237 20 : }
238 :
239 7 : OUString SAL_CALL SwAccessibleCell::getAccessibleDescription (void)
240 : throw (uno::RuntimeException, std::exception)
241 : {
242 7 : return GetName();
243 : }
244 :
245 3 : OUString SAL_CALL SwAccessibleCell::getImplementationName()
246 : throw( uno::RuntimeException, std::exception )
247 : {
248 3 : return OUString(sImplementationName);
249 : }
250 :
251 0 : sal_Bool SAL_CALL SwAccessibleCell::supportsService(const OUString& sTestServiceName)
252 : throw (uno::RuntimeException, std::exception)
253 : {
254 0 : return cppu::supportsService(this, sTestServiceName);
255 : }
256 :
257 0 : uno::Sequence< OUString > SAL_CALL SwAccessibleCell::getSupportedServiceNames()
258 : throw( uno::RuntimeException, std::exception )
259 : {
260 0 : uno::Sequence< OUString > aRet(2);
261 0 : OUString* pArray = aRet.getArray();
262 0 : pArray[0] = OUString(sServiceName );
263 0 : pArray[1] = OUString(sAccessibleServiceName );
264 0 : return aRet;
265 : }
266 :
267 2 : void SwAccessibleCell::Dispose( sal_Bool bRecursive )
268 : {
269 2 : const SwFrm *pParent = GetParent( SwAccessibleChild(GetFrm()), IsInPagePreview() );
270 : ::rtl::Reference< SwAccessibleContext > xAccImpl(
271 2 : GetMap()->GetContextImpl( pParent, sal_False ) );
272 2 : if( xAccImpl.is() )
273 2 : xAccImpl->DisposeChild( SwAccessibleChild(GetFrm()), bRecursive );
274 2 : SwAccessibleContext::Dispose( bRecursive );
275 2 : }
276 :
277 0 : void SwAccessibleCell::InvalidatePosOrSize( const SwRect& rOldBox )
278 : {
279 0 : const SwFrm *pParent = GetParent( SwAccessibleChild(GetFrm()), IsInPagePreview() );
280 : ::rtl::Reference< SwAccessibleContext > xAccImpl(
281 0 : GetMap()->GetContextImpl( pParent, sal_False ) );
282 0 : if( xAccImpl.is() )
283 0 : xAccImpl->InvalidateChildPosOrSize( SwAccessibleChild(GetFrm()), rOldBox );
284 0 : SwAccessibleContext::InvalidatePosOrSize( rOldBox );
285 0 : }
286 :
287 : // XAccessibleInterface
288 :
289 120 : uno::Any SwAccessibleCell::queryInterface( const uno::Type& rType )
290 : throw( uno::RuntimeException, std::exception )
291 : {
292 120 : if (rType == ::getCppuType((const uno::Reference<XAccessibleExtendedAttributes>*)0))
293 : {
294 0 : uno::Any aR;
295 0 : aR <<= uno::Reference<XAccessibleExtendedAttributes>(this);
296 0 : return aR;
297 : }
298 :
299 120 : if (rType == ::getCppuType((const uno::Reference<XAccessibleSelection>*)0))
300 : {
301 9 : uno::Any aR;
302 9 : aR <<= uno::Reference<XAccessibleSelection>(this);
303 9 : return aR;
304 : }
305 111 : if ( rType == ::getCppuType( static_cast< uno::Reference< XAccessibleValue > * >( 0 ) ) )
306 : {
307 1 : uno::Reference<XAccessibleValue> xValue = this;
308 2 : uno::Any aRet;
309 1 : aRet <<= xValue;
310 2 : return aRet;
311 : }
312 : else
313 : {
314 110 : return SwAccessibleContext::queryInterface( rType );
315 : }
316 : }
317 :
318 : // XTypeProvider
319 0 : uno::Sequence< uno::Type > SAL_CALL SwAccessibleCell::getTypes()
320 : throw(uno::RuntimeException, std::exception)
321 : {
322 0 : uno::Sequence< uno::Type > aTypes( SwAccessibleContext::getTypes() );
323 :
324 0 : sal_Int32 nIndex = aTypes.getLength();
325 0 : aTypes.realloc( nIndex + 1 );
326 :
327 0 : uno::Type* pTypes = aTypes.getArray();
328 0 : pTypes[nIndex] = ::getCppuType( static_cast< uno::Reference< XAccessibleValue > * >( 0 ) );
329 :
330 0 : return aTypes;
331 : }
332 :
333 0 : uno::Sequence< sal_Int8 > SAL_CALL SwAccessibleCell::getImplementationId()
334 : throw(uno::RuntimeException, std::exception)
335 : {
336 0 : return css::uno::Sequence<sal_Int8>();
337 : }
338 :
339 : // XAccessibleValue
340 :
341 11 : SwFrmFmt* SwAccessibleCell::GetTblBoxFormat() const
342 : {
343 : OSL_ENSURE( GetFrm() != NULL, "no frame?" );
344 : OSL_ENSURE( GetFrm()->IsCellFrm(), "no cell frame?" );
345 :
346 11 : const SwCellFrm* pCellFrm = static_cast<const SwCellFrm*>( GetFrm() );
347 11 : return pCellFrm->GetTabBox()->GetFrmFmt();
348 : }
349 :
350 : //Implement TableCell currentValue
351 6 : uno::Any SwAccessibleCell::getCurrentValue( )
352 : throw( uno::RuntimeException, std::exception )
353 : {
354 6 : SolarMutexGuard aGuard;
355 6 : CHECK_FOR_DEFUNC( XAccessibleValue );
356 :
357 6 : uno::Any aAny;
358 6 : aAny <<= GetTblBoxFormat()->GetTblBoxValue().GetValue();
359 6 : return aAny;
360 : }
361 :
362 5 : sal_Bool SwAccessibleCell::setCurrentValue( const uno::Any& aNumber )
363 : throw( uno::RuntimeException, std::exception )
364 : {
365 5 : SolarMutexGuard aGuard;
366 5 : CHECK_FOR_DEFUNC( XAccessibleValue );
367 :
368 5 : double fValue = 0;
369 5 : sal_Bool bValid = (aNumber >>= fValue);
370 5 : if( bValid )
371 : {
372 5 : SwTblBoxValue aValue( fValue );
373 5 : GetTblBoxFormat()->SetFmtAttr( aValue );
374 : }
375 5 : return bValid;
376 : }
377 :
378 1 : uno::Any SwAccessibleCell::getMaximumValue( )
379 : throw( uno::RuntimeException, std::exception )
380 : {
381 1 : uno::Any aAny;
382 1 : aAny <<= DBL_MAX;
383 1 : return aAny;
384 : }
385 :
386 1 : uno::Any SwAccessibleCell::getMinimumValue( )
387 : throw( uno::RuntimeException, std::exception )
388 : {
389 1 : uno::Any aAny;
390 1 : aAny <<= -DBL_MAX;
391 1 : return aAny;
392 : }
393 :
394 0 : static OUString ReplaceOneChar(const OUString& oldOUString, const OUString& replacedChar, const OUString& replaceStr)
395 : {
396 0 : int iReplace = oldOUString.lastIndexOf(replacedChar);
397 0 : OUString aRet = oldOUString;
398 0 : while(iReplace > -1)
399 : {
400 0 : aRet = aRet.replaceAt(iReplace,1, replaceStr);
401 0 : iReplace = aRet.lastIndexOf(replacedChar,iReplace);
402 : }
403 0 : return aRet;
404 : }
405 :
406 0 : static OUString ReplaceFourChar(const OUString& oldOUString)
407 : {
408 0 : OUString aRet = ReplaceOneChar(oldOUString,OUString("\\"),OUString("\\\\"));
409 0 : aRet = ReplaceOneChar(aRet,OUString(";"),OUString("\\;"));
410 0 : aRet = ReplaceOneChar(aRet,OUString("="),OUString("\\="));
411 0 : aRet = ReplaceOneChar(aRet,OUString(","),OUString("\\,"));
412 0 : aRet = ReplaceOneChar(aRet,OUString(":"),OUString("\\:"));
413 0 : return aRet;
414 : }
415 :
416 0 : ::com::sun::star::uno::Any SAL_CALL SwAccessibleCell::getExtendedAttributes()
417 : throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException, std::exception)
418 : {
419 0 : SolarMutexGuard g;
420 :
421 0 : ::com::sun::star::uno::Any strRet;
422 0 : SwFrmFmt *pFrmFmt = GetTblBoxFormat();
423 : DBG_ASSERT(pFrmFmt,"Must be Valid");
424 :
425 0 : const SwTblBoxFormula& tbl_formula = pFrmFmt->GetTblBoxFormula();
426 :
427 0 : OUString strFormula = ReplaceFourChar(tbl_formula.GetFormula());
428 0 : OUString strFor("Formula:");
429 0 : strFor += strFormula;
430 0 : strFor += ";" ;
431 0 : strRet <<= strFor;
432 :
433 0 : return strRet;
434 : }
435 :
436 1 : sal_Int32 SAL_CALL SwAccessibleCell::getBackground()
437 : throw (::com::sun::star::uno::RuntimeException, std::exception)
438 : {
439 1 : SolarMutexGuard g;
440 :
441 1 : const SvxBrushItem &rBack = GetFrm()->GetAttrSet()->GetBackground();
442 1 : sal_uInt32 crBack = rBack.GetColor().GetColor();
443 :
444 1 : if (COL_AUTO == crBack)
445 : {
446 1 : uno::Reference<XAccessible> xAccDoc = getAccessibleParent();
447 1 : if (xAccDoc.is())
448 : {
449 1 : uno::Reference<XAccessibleComponent> xCompoentDoc(xAccDoc, uno::UNO_QUERY);
450 1 : if (xCompoentDoc.is())
451 : {
452 1 : crBack = (sal_uInt32)xCompoentDoc->getBackground();
453 1 : }
454 1 : }
455 : }
456 1 : return crBack;
457 : }
458 :
459 : //===== XAccessibleSelection ============================================
460 2 : void SwAccessibleCell::selectAccessibleChild(
461 : sal_Int32 nChildIndex )
462 : throw ( lang::IndexOutOfBoundsException, uno::RuntimeException, std::exception )
463 : {
464 2 : aSelectionHelper.selectAccessibleChild(nChildIndex);
465 0 : }
466 :
467 3 : sal_Bool SwAccessibleCell::isAccessibleChildSelected(
468 : sal_Int32 nChildIndex )
469 : throw ( lang::IndexOutOfBoundsException, uno::RuntimeException, std::exception )
470 : {
471 3 : return aSelectionHelper.isAccessibleChildSelected(nChildIndex);
472 : }
473 :
474 2 : void SwAccessibleCell::clearAccessibleSelection( )
475 : throw ( uno::RuntimeException, std::exception )
476 : {
477 2 : aSelectionHelper.clearAccessibleSelection();
478 2 : }
479 :
480 3 : void SwAccessibleCell::selectAllAccessibleChildren( )
481 : throw ( uno::RuntimeException, std::exception )
482 : {
483 3 : aSelectionHelper.selectAllAccessibleChildren();
484 3 : }
485 :
486 5 : sal_Int32 SwAccessibleCell::getSelectedAccessibleChildCount( )
487 : throw ( uno::RuntimeException, std::exception )
488 : {
489 5 : return aSelectionHelper.getSelectedAccessibleChildCount();
490 : }
491 :
492 2 : uno::Reference<XAccessible> SwAccessibleCell::getSelectedAccessibleChild(
493 : sal_Int32 nSelectedChildIndex )
494 : throw ( lang::IndexOutOfBoundsException, uno::RuntimeException, std::exception)
495 : {
496 2 : return aSelectionHelper.getSelectedAccessibleChild(nSelectedChildIndex);
497 : }
498 :
499 2 : void SwAccessibleCell::deselectAccessibleChild(
500 : sal_Int32 nSelectedChildIndex )
501 : throw ( lang::IndexOutOfBoundsException, uno::RuntimeException, std::exception )
502 : {
503 2 : aSelectionHelper.deselectAccessibleChild(nSelectedChildIndex);
504 0 : }
505 :
506 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|