Debian Codegraph @

chart2/source/controller/accessibility/AccessibleBase.cxx

raw
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 <AccessibleBase.hxx>
21 #include "AccessibleChartShape.hxx"
22 #include <ObjectHierarchy.hxx>
23 #include <ObjectIdentifier.hxx>
24 #include <ChartView.hxx>
25 #include <ChartController.hxx>
26
27 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
28 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
29 #include <com/sun/star/accessibility/AccessibleRole.hpp>
30 #include <com/sun/star/drawing/LineStyle.hpp>
31 #include <com/sun/star/drawing/FillStyle.hpp>
32 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
33 #include <sal/log.hxx>
34 #include <utility>
35 #include <vcl/svapp.hxx>
36 #include <i18nlangtag/languagetag.hxx>
37 #include <vcl/window.hxx>
38 #include <vcl/settings.hxx>
39 #include <o3tl/functional.hxx>
40 #include <o3tl/safeint.hxx>
41 #include <comphelper/diagnose_ex.hxx>
42
43 #include <algorithm>
44 #include <iterator>
45
46 #include "ChartElementFactory.hxx"
47
48 using namespace ::com::sun::star;
49 using namespace ::com::sun::star::accessibility;
50
51 using ::com::sun::star::uno::UNO_QUERY;
52 using ::com::sun::star::uno::Reference;
53 using ::osl::MutexGuard;
54 using ::osl::ClearableMutexGuard;
55 using ::com::sun::star::uno::Any;
56
57 namespace chart
58 {
59
60 /** @param bMayHaveChildren is false per default
61 */
62 AccessibleBase::AccessibleBase(
63 AccessibleElementInfo aAccInfo,
64 bool bMayHaveChildren,
65 bool bAlwaysTransparent /* default: false */ ) :
66 m_bMayHaveChildren( bMayHaveChildren ),
67 m_bChildrenInitialized( false ),
68 m_nStateSet( 0 ),
69 m_aAccInfo(std::move( aAccInfo )),
70 m_bAlwaysTransparent( bAlwaysTransparent ),
71 m_bStateSetInitialized( false )
72 {
73 // initialize some states
74 m_nStateSet |= AccessibleStateType::ENABLED;
75 m_nStateSet |= AccessibleStateType::SHOWING;
76 m_nStateSet |= AccessibleStateType::VISIBLE;
77 m_nStateSet |= AccessibleStateType::SELECTABLE;
78 m_nStateSet |= AccessibleStateType::FOCUSABLE;
79 }
80
81 AccessibleBase::~AccessibleBase()
82 {
83 OSL_ASSERT(!isAlive());
84 }
85
86 bool AccessibleBase::NotifyEvent( EventType eEventType, const AccessibleUniqueId & rId )
87 {
88 if( GetId() == rId )
89 {
90 // event is addressed to this object
91
92 css::uno::Any aEmpty;
93 css::uno::Any aSelected;
94 aSelected <<= AccessibleStateType::SELECTED;
95 switch( eEventType )
96 {
97 case EventType::GOT_SELECTION:
98 {
99 AddState( AccessibleStateType::SELECTED );
100 NotifyAccessibleEvent(AccessibleEventId::STATE_CHANGED, aEmpty, aSelected);
101
102 AddState( AccessibleStateType::FOCUSED );
103 aSelected <<= AccessibleStateType::FOCUSED;
104 NotifyAccessibleEvent(AccessibleEventId::STATE_CHANGED, aEmpty, aSelected);
105
106 SAL_INFO("chart2.accessibility", "Selection acquired by: " << getAccessibleName());
107 }
108 break;
109
110 case EventType::LOST_SELECTION:
111 {
112 RemoveState( AccessibleStateType::SELECTED );
113 NotifyAccessibleEvent(AccessibleEventId::STATE_CHANGED, aSelected, aEmpty);
114
115 AddState( AccessibleStateType::FOCUSED );
116 aSelected <<= AccessibleStateType::FOCUSED;
117 NotifyAccessibleEvent(AccessibleEventId::STATE_CHANGED, aSelected, aEmpty);
118 SAL_INFO("chart2.accessibility", "Selection lost by: " << getAccessibleName());
119 }
120 break;
121 }
122 return true;
123 }
124 else if( m_bMayHaveChildren )
125 {
126 bool bStop = false;
127
128 ClearableMutexGuard aGuard( m_aMutex );
129 // make local copy for notification
130 std::vector<rtl::Reference<AccessibleBase>> aLocalChildList(m_aChildList);
131 aGuard.clear();
132
133 for (auto const& localChild : aLocalChildList)
134 {
135 bStop = localChild->NotifyEvent(eEventType, rId);
136 if (bStop)
137 break;
138 }
139 return bStop;
140 }
141
142 return false;
143 }
144
145 void AccessibleBase::AddState( sal_Int64 aState )
146 {
147 ensureAlive();
148 m_nStateSet |= aState;
149 }
150
151 void AccessibleBase::RemoveState( sal_Int64 aState )
152 {
153 ensureAlive();
154 m_nStateSet &= ~aState;
155 }
156
157 bool AccessibleBase::UpdateChildren()
158 {
159 bool bMustUpdateChildren = false;
160 {
161 MutexGuard aGuard( m_aMutex );
162 if (!m_bMayHaveChildren || !isAlive())
163 return false;
164
165 bMustUpdateChildren = ( m_bMayHaveChildren &&
166 ! m_bChildrenInitialized );
167 }
168
169 // update unguarded
170 if( bMustUpdateChildren )
171 m_bChildrenInitialized = ImplUpdateChildren();
172
173 return m_bChildrenInitialized;
174 }
175
176 bool AccessibleBase::ImplUpdateChildren()
177 {
178 bool bResult = false;
179
180 if( m_aAccInfo.m_spObjectHierarchy )
181 {
182 ObjectHierarchy::tChildContainer aModelChildren(
183 m_aAccInfo.m_spObjectHierarchy->getChildren( GetId() ));
184 std::vector< ChildOIDMap::key_type > aAccChildren;
185 aAccChildren.reserve( aModelChildren.size());
186 std::transform( m_aChildOIDMap.begin(), m_aChildOIDMap.end(),
187 std::back_inserter( aAccChildren ),
188 ::o3tl::select1st< ChildOIDMap::value_type >() );
189
190 std::sort( aModelChildren.begin(), aModelChildren.end());
191
192 std::vector< ObjectIdentifier > aChildrenToRemove, aChildrenToAdd;
193 std::set_difference( aModelChildren.begin(), aModelChildren.end(),
194 aAccChildren.begin(), aAccChildren.end(),
195 std::back_inserter( aChildrenToAdd ));
196 std::set_difference( aAccChildren.begin(), aAccChildren.end(),
197 aModelChildren.begin(), aModelChildren.end(),
198 std::back_inserter( aChildrenToRemove ));
199
200 for (auto const& childToRemove : aChildrenToRemove)
201 {
202 RemoveChildByOId(childToRemove);
203 }
204
205 AccessibleElementInfo aAccInfo( GetInfo());
206 aAccInfo.m_pParent = this;
207
208 for (auto const& childToAdd : aChildrenToAdd)
209 {
210 aAccInfo.m_aOID = childToAdd;
211 if ( childToAdd.isAutoGeneratedObject() )
212 {
213 AddChild( ChartElementFactory::CreateChartElement( aAccInfo ).get() );
214 }
215 else if ( childToAdd.isAdditionalShape() )
216 {
217 AddChild( new AccessibleChartShape( aAccInfo ) );
218 }
219 }
220 bResult = true;
221 }
222
223 return bResult;
224 }
225
226 void AccessibleBase::AddChild( AccessibleBase * pChild )
227 {
228 OSL_ENSURE( pChild != nullptr, "Invalid Child" );
229 if( !pChild )
230 return;
231
232 ClearableMutexGuard aGuard( m_aMutex );
233
234 rtl::Reference<AccessibleBase> xChild(pChild);
235 m_aChildList.push_back( xChild );
236
237 m_aChildOIDMap[ pChild->GetId() ] = xChild;
238
239 // inform listeners of new child
240 if( m_bChildrenInitialized )
241 {
242 Any aEmpty, aNew;
243 aNew <<= uno::Reference<XAccessible>(xChild);
244
245 aGuard.clear();
246 NotifyAccessibleEvent(AccessibleEventId::CHILD, aEmpty, aNew);
247 }
248 }
249
250 void AccessibleBase::RemoveChildByOId( const ObjectIdentifier& rOId )
251 {
252 ClearableMutexGuard aGuard( m_aMutex );
253
254 ChildOIDMap::iterator aIt( m_aChildOIDMap.find( rOId ));
255 if( aIt == m_aChildOIDMap.end())
256 return;
257
258 rtl::Reference<AccessibleBase> xChild(aIt->second);
259
260 // remove from map
261 m_aChildOIDMap.erase( aIt );
262
263 // search child in vector
264 auto aVecIter = std::find(m_aChildList.begin(), m_aChildList.end(), xChild);
265
266 OSL_ENSURE( aVecIter != m_aChildList.end(),
267 "Inconsistent ChildMap" );
268
269 // remove child from vector
270 m_aChildList.erase( aVecIter );
271 bool bInitialized = m_bChildrenInitialized;
272
273 // call listeners unguarded
274 aGuard.clear();
275
276 // inform listeners of removed child
277 if( bInitialized )
278 {
279 Any aEmpty, aOld;
280 aOld <<= uno::Reference<XAccessible>(xChild);
281
282 NotifyAccessibleEvent(AccessibleEventId::CHILD, aOld, aEmpty);
283 }
284
285 // dispose the child
286 if (xChild.is())
287 xChild->dispose();
288 }
289
290 awt::Point AccessibleBase::GetUpperLeftOnScreen() const
291 {
292 awt::Point aResult;
293 if( m_aAccInfo.m_pParent )
294 {
295 ClearableMutexGuard aGuard( m_aMutex );
296 AccessibleBase * pParent = m_aAccInfo.m_pParent;
297 aGuard.clear();
298
299 if( pParent )
300 {
301 aResult = pParent->GetUpperLeftOnScreen();
302 }
303 else
304 OSL_FAIL( "Default position used is probably incorrect." );
305 }
306
307 return aResult;
308 }
309
310 void AccessibleBase::KillAllChildren()
311 {
312 ClearableMutexGuard aGuard( m_aMutex );
313
314 // make local copy for notification, and remove all children
315 std::vector<rtl::Reference<AccessibleBase>> aLocalChildList;
316 aLocalChildList.swap( m_aChildList );
317 m_aChildOIDMap.clear();
318
319 aGuard.clear();
320
321 // call dispose for all children
322 // and notify listeners
323 Any aEmpty, aOld;
324 for (auto const& localChild : aLocalChildList)
325 {
326 aOld <<= uno::Reference<XAccessible>(localChild);
327 NotifyAccessibleEvent(AccessibleEventId::CHILD, aOld, aEmpty);
328
329 if (localChild.is())
330 localChild->dispose();
331 }
332 m_bChildrenInitialized = false;
333 }
334
335 void AccessibleBase::SetInfo( const AccessibleElementInfo & rNewInfo )
336 {
337 m_aAccInfo = rNewInfo;
338 if( m_bMayHaveChildren )
339 {
340 KillAllChildren();
341 }
342 NotifyAccessibleEvent(AccessibleEventId::INVALIDATE_ALL_CHILDREN, uno::Any(), uno::Any());
343 }
344
345 // ________ (XComponent::dispose) ________
346 void SAL_CALL AccessibleBase::disposing()
347 {
348 {
349 MutexGuard aGuard(m_aMutex);
350 OSL_ENSURE(isAlive(), "dispose() called twice");
351
352 OAccessible::disposing();
353
354 // reset pointers
355 m_aAccInfo.m_pWindow.reset();
356 m_aAccInfo.m_pParent = nullptr;
357
358 m_nStateSet = AccessibleStateType::DEFUNC;
359
360 }
361 // call listeners unguarded
362
363 if( m_bMayHaveChildren )
364 {
365 KillAllChildren();
366 }
367 else
368 OSL_ENSURE( m_aChildList.empty(), "Child list should be empty" );
369 }
370
371 // ________ AccessibleBase::XAccessibleContext ________
372 sal_Int64 SAL_CALL AccessibleBase::getAccessibleChildCount()
373 {
374 ClearableMutexGuard aGuard( m_aMutex );
375 if (!m_bMayHaveChildren || !isAlive())
376 return 0;
377
378 bool bMustUpdateChildren = ( m_bMayHaveChildren &&
379 ! m_bChildrenInitialized );
380
381 aGuard.clear();
382
383 // update unguarded
384 if( bMustUpdateChildren )
385 UpdateChildren();
386
387 return ImplGetAccessibleChildCount();
388 }
389
390 sal_Int64 AccessibleBase::ImplGetAccessibleChildCount() const
391 {
392 return m_aChildList.size();
393 }
394
395 Reference< XAccessible > SAL_CALL AccessibleBase::getAccessibleChild( sal_Int64 i )
396 {
397 ensureAlive();
398 Reference< XAccessible > xResult;
399
400 ClearableMutexGuard aGuard( m_aMutex );
401 bool bMustUpdateChildren = ( m_bMayHaveChildren &&
402 ! m_bChildrenInitialized );
403
404 aGuard.clear();
405
406 if( bMustUpdateChildren )
407 UpdateChildren();
408
409 xResult.set( ImplGetAccessibleChildById( i ));
410
411 return xResult;
412 }
413
414 Reference< XAccessible > AccessibleBase::ImplGetAccessibleChildById( sal_Int64 i ) const
415 {
416 rtl::Reference<AccessibleBase> xResult;
417
418 MutexGuard aGuard( m_aMutex);
419 if( ! m_bMayHaveChildren ||
420 i < 0 ||
421 o3tl::make_unsigned( i ) >= m_aChildList.size() )
422 {
423 OUString aBuf = "Index " + OUString::number( i ) + " is invalid for range [ 0, " +
424 OUString::number( m_aChildList.size() - 1 ) +
425 " ]";
426 lang::IndexOutOfBoundsException aEx( aBuf,
427 const_cast< ::cppu::OWeakObject * >(
428 static_cast< const ::cppu::OWeakObject * >( this )));
429 throw aEx;
430 }
431 else
432 xResult = m_aChildList[i];
433
434 return xResult;
435 }
436
437 Reference< XAccessible > SAL_CALL AccessibleBase::getAccessibleParent()
438 {
439 ensureAlive();
440 Reference< XAccessible > aResult;
441 if( m_aAccInfo.m_pParent )
442 aResult.set( m_aAccInfo.m_pParent );
443
444 return aResult;
445 }
446
447 sal_Int64 SAL_CALL AccessibleBase::getAccessibleIndexInParent()
448 {
449 ensureAlive();
450
451 if( m_aAccInfo.m_spObjectHierarchy )
452 return m_aAccInfo.m_spObjectHierarchy->getIndexInParent( GetId() );
453 return -1;
454 }
455
456 sal_Int16 SAL_CALL AccessibleBase::getAccessibleRole()
457 {
458 return AccessibleRole::SHAPE;
459 }
460
461 Reference< XAccessibleRelationSet > SAL_CALL AccessibleBase::getAccessibleRelationSet()
462 {
463 Reference< XAccessibleRelationSet > aResult;
464 return aResult;
465 }
466
467 sal_Int64 SAL_CALL AccessibleBase::getAccessibleStateSet()
468 {
469 if( ! m_bStateSetInitialized )
470 {
471 rtl::Reference< ::chart::ChartController > xSelSupp( GetInfo().m_xChartController );
472 if ( xSelSupp.is() )
473 {
474 ObjectIdentifier aOID( xSelSupp->getSelection() );
475 if ( aOID.isValid() && GetId() == aOID )
476 {
477 AddState( AccessibleStateType::SELECTED );
478 AddState( AccessibleStateType::FOCUSED );
479 }
480 }
481 m_bStateSetInitialized = true;
482 }
483
484 return m_nStateSet;
485 }
486
487 lang::Locale SAL_CALL AccessibleBase::getLocale()
488 {
489 ensureAlive();
490
491 return Application::GetSettings().GetLanguageTag().getLocale();
492 }
493
494 // ________ AccessibleBase::XAccessibleComponent ________
495
496 Reference< XAccessible > SAL_CALL AccessibleBase::getAccessibleAtPoint( const awt::Point& aPoint )
497 {
498 ensureAlive();
499 rtl::Reference< AccessibleBase > aResult;
500 awt::Rectangle aRect( implGetBounds());
501
502 // children are positioned relative to this object, so translate bound rect
503 aRect.X = 0;
504 aRect.Y = 0;
505
506 // children must be inside the own bound rect
507 if( ( aRect.X <= aPoint.X && aPoint.X <= (aRect.X + aRect.Width) ) &&
508 ( aRect.Y <= aPoint.Y && aPoint.Y <= (aRect.Y + aRect.Height)))
509 {
510 ClearableMutexGuard aGuard( m_aMutex );
511 std::vector<rtl::Reference<AccessibleBase>> aLocalChildList(m_aChildList);
512 aGuard.clear();
513
514 for (const rtl::Reference<AccessibleBase>& xLocalChild : aLocalChildList)
515 {
516 if (xLocalChild.is())
517 {
518 aRect = xLocalChild->implGetBounds();
519 if( ( aRect.X <= aPoint.X && aPoint.X <= (aRect.X + aRect.Width) ) &&
520 ( aRect.Y <= aPoint.Y && aPoint.Y <= (aRect.Y + aRect.Height)))
521 {
522 aResult = xLocalChild;
523 break;
524 }
525 }
526 }
527 }
528
529 return aResult;
530 }
531
532 css::awt::Rectangle AccessibleBase::implGetBounds()
533 {
534 rtl::Reference<ChartView> pChartView = m_aAccInfo.m_xView.get();
535 if( pChartView )
536 {
537 VclPtr<vcl::Window> pWindow = m_aAccInfo.m_pWindow;
538 awt::Rectangle aLogicRect( pChartView->getRectangleOfObject( m_aAccInfo.m_aOID.getObjectCID() ));
539 if( pWindow )
540 {
541 tools::Rectangle aRect( aLogicRect.X, aLogicRect.Y,
542 aLogicRect.X + aLogicRect.Width,
543 aLogicRect.Y + aLogicRect.Height );
544 SolarMutexGuard aSolarGuard;
545 aRect = pWindow->LogicToPixel( aRect );
546
547 // aLogicRect is relative to the page, but we need a value relative
548 // to the parent object
549 awt::Point aParentLocOnScreen;
550 uno::Reference< XAccessibleComponent > xParent( getAccessibleParent(), uno::UNO_QUERY );
551 if( xParent.is() )
552 aParentLocOnScreen = xParent->getLocationOnScreen();
553
554 awt::Point aULOnScreen = GetUpperLeftOnScreen();
555 awt::Point aOffset( aParentLocOnScreen.X - aULOnScreen.X,
556 aParentLocOnScreen.Y - aULOnScreen.Y );
557
558 return awt::Rectangle( aRect.Left() - aOffset.X, aRect.Top() - aOffset.Y,
559 aRect.getOpenWidth(), aRect.getOpenHeight());
560 }
561 }
562
563 return awt::Rectangle();
564 }
565
566 void SAL_CALL AccessibleBase::grabFocus()
567 {
568 ensureAlive();
569
570 rtl::Reference< ::chart::ChartController > xSelSupp( GetInfo().m_xChartController );
571 if ( xSelSupp.is() )
572 {
573 xSelSupp->select( GetId().getAny() );
574 }
575 }
576
577 sal_Int32 SAL_CALL AccessibleBase::getForeground()
578 {
579 return sal_Int32(getColor( ACC_BASE_FOREGROUND ));
580 }
581
582 sal_Int32 SAL_CALL AccessibleBase::getBackground()
583 {
584 return sal_Int32(getColor( ACC_BASE_BACKGROUND ));
585 }
586
587 Color AccessibleBase::getColor( eColorType eColType )
588 {
589 Color nResult = COL_TRANSPARENT;
590 if( m_bAlwaysTransparent )
591 return nResult;
592
593 ObjectIdentifier aOID( m_aAccInfo.m_aOID );
594 ObjectType eType( aOID.getObjectType() );
595 Reference< beans::XPropertySet > xObjProp;
596 OUString aObjectCID = aOID.getObjectCID();
597 if( eType == OBJECTTYPE_LEGEND_ENTRY )
598 {
599 // for colors get the data series/point properties
600 std::u16string_view aParentParticle( ObjectIdentifier::getFullParentParticle( aObjectCID ));
601 aObjectCID = ObjectIdentifier::createClassifiedIdentifierForParticle( aParentParticle );
602 }
603
604 xObjProp =
605 ObjectIdentifier::getObjectPropertySet(
606 aObjectCID, m_aAccInfo.m_xChartDocument );
607 if( xObjProp.is())
608 {
609 try
610 {
611 OUString aPropName;
612 OUString aStylePropName;
613
614 switch( eType )
615 {
616 case OBJECTTYPE_LEGEND_ENTRY:
617 case OBJECTTYPE_DATA_SERIES:
618 case OBJECTTYPE_DATA_POINT:
619 if( eColType == ACC_BASE_FOREGROUND )
620 {
621 aPropName = "BorderColor";
622 aStylePropName = "BorderTransparency";
623 }
624 else
625 {
626 aPropName = "Color";
627 aStylePropName = "Transparency";
628 }
629 break;
630 default:
631 if( eColType == ACC_BASE_FOREGROUND )
632 {
633 aPropName = "LineColor";
634 aStylePropName = "LineTransparence";
635 }
636 else
637 {
638 aPropName = "FillColor";
639 aStylePropName = "FillTransparence";
640 }
641 break;
642 }
643
644 bool bTransparent = m_bAlwaysTransparent;
645 Reference< beans::XPropertySetInfo > xInfo = xObjProp->getPropertySetInfo();
646 if( xInfo.is() &&
647 xInfo->hasPropertyByName( aStylePropName ))
648 {
649 if( eColType == ACC_BASE_FOREGROUND )
650 {
651 drawing::LineStyle aLStyle = drawing::LineStyle_SOLID;
652 if( xObjProp->getPropertyValue( aStylePropName ) >>= aLStyle )
653 bTransparent = (aLStyle == drawing::LineStyle_NONE);
654 }
655 else
656 {
657 drawing::FillStyle aFStyle = drawing::FillStyle_SOLID;
658 if( xObjProp->getPropertyValue( aStylePropName ) >>= aFStyle )
659 bTransparent = (aFStyle == drawing::FillStyle_NONE);
660 }
661 }
662
663 if( !bTransparent &&
664 xInfo.is() &&
665 xInfo->hasPropertyByName( aPropName ))
666 {
667 xObjProp->getPropertyValue( aPropName ) >>= nResult;
668 }
669 }
670 catch( const uno::Exception & )
671 {
672 DBG_UNHANDLED_EXCEPTION("chart2");
673 }
674 }
675
676 return nResult;
677 }
678
679 } // namespace chart
680
681 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */