1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <svxrectctaccessiblecontext.hxx>
21 #include <com/sun/star/accessibility/AccessibleRole.hpp>
22 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
23 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
24 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
26 #include <vcl/svapp.hxx>
27 #include <osl/mutex.hxx>
28 #include <tools/debug.hxx>
29 #include <tools/gen.hxx>
30 #include <sal/log.hxx>
31 #include <vcl/settings.hxx>
32 #include <vcl/unohelp.hxx>
33 #include <svx/strings.hrc>
34 #include <svx/dlgctrl.hxx>
35 #include <svx/dialmgr.hxx>
36 #include <com/sun/star/accessibility/AccessibleRelationType.hpp>
37 #include <unotools/accessiblerelationsethelper.hxx>
39 using namespace ::cppu
;
40 using namespace ::osl
;
41 using namespace ::com::sun::star
;
42 using namespace ::com::sun::star::uno
;
43 using namespace ::com::sun::star::accessibility
;
45 using namespace ::com::sun::star::lang
;
47 #define MAX_NUM_OF_CHILDREN 9
48 #define NOCHILDSELECTED -1
53 struct ChildIndexToPointData
55 TranslateId pResIdName
;
56 TranslateId pResIdDescr
;
62 static const ChildIndexToPointData
* IndexToPoint( tools::Long nIndex
)
64 DBG_ASSERT( nIndex
< 9 && nIndex
>= 0, "-IndexToPoint(): invalid child index! You have been warned..." );
66 // corners are counted from left to right and top to bottom
67 static const ChildIndexToPointData pCornerData
[] =
69 { RID_SVXSTR_RECTCTL_ACC_CHLD_LT
, RID_SVXSTR_RECTCTL_ACC_CHLD_LT
, RectPoint::LT
}, // 0
70 { RID_SVXSTR_RECTCTL_ACC_CHLD_MT
, RID_SVXSTR_RECTCTL_ACC_CHLD_MT
, RectPoint::MT
}, // 1
71 { RID_SVXSTR_RECTCTL_ACC_CHLD_RT
, RID_SVXSTR_RECTCTL_ACC_CHLD_RT
, RectPoint::RT
}, // 2
72 { RID_SVXSTR_RECTCTL_ACC_CHLD_LM
, RID_SVXSTR_RECTCTL_ACC_CHLD_LM
, RectPoint::LM
}, // 3
73 { RID_SVXSTR_RECTCTL_ACC_CHLD_MM
, RID_SVXSTR_RECTCTL_ACC_CHLD_MM
, RectPoint::MM
}, // 4
74 { RID_SVXSTR_RECTCTL_ACC_CHLD_RM
, RID_SVXSTR_RECTCTL_ACC_CHLD_RM
, RectPoint::RM
}, // 5
75 { RID_SVXSTR_RECTCTL_ACC_CHLD_LB
, RID_SVXSTR_RECTCTL_ACC_CHLD_LB
, RectPoint::LB
}, // 6
76 { RID_SVXSTR_RECTCTL_ACC_CHLD_MB
, RID_SVXSTR_RECTCTL_ACC_CHLD_MB
, RectPoint::MB
}, // 7
77 { RID_SVXSTR_RECTCTL_ACC_CHLD_RB
, RID_SVXSTR_RECTCTL_ACC_CHLD_RB
, RectPoint::RB
} // 8
80 return pCornerData
+ nIndex
;
84 static tools::Long
PointToIndex( RectPoint ePoint
)
86 tools::Long
nRet( static_cast<tools::Long
>(ePoint
) );
88 // corners are counted from left to right and top to bottom
89 DBG_ASSERT( int(RectPoint::LT
) == 0 && int(RectPoint::MT
) == 1 && int(RectPoint::RT
) == 2 && int(RectPoint::LM
) == 3 && int(RectPoint::MM
) == 4 && int(RectPoint::RM
) == 5 &&
90 int(RectPoint::LB
) == 6 && int(RectPoint::MB
) == 7 && int(RectPoint::RB
) == 8, "*PointToIndex(): unexpected enum value!" );
92 nRet
= static_cast<tools::Long
>(ePoint
);
97 SvxRectCtlAccessibleContext::SvxRectCtlAccessibleContext(SvxRectCtl
* pRepr
)
99 , mnSelectedChild(NOCHILDSELECTED
)
102 ::SolarMutexGuard aSolarGuard
;
103 msName
= SvxResId( RID_SVXSTR_RECTCTL_ACC_CORN_NAME
);
104 msDescription
= SvxResId( RID_SVXSTR_RECTCTL_ACC_CORN_DESCR
);
107 mvChildren
.resize(MAX_NUM_OF_CHILDREN
);
110 SvxRectCtlAccessibleContext::~SvxRectCtlAccessibleContext()
115 Reference
< XAccessible
> SAL_CALL
SvxRectCtlAccessibleContext::getAccessibleAtPoint( const awt::Point
& rPoint
)
117 ::osl::MutexGuard
aGuard( m_aMutex
);
119 Reference
< XAccessible
> xRet
;
121 tools::Long nChild
= mpRepr
? PointToIndex(mpRepr
->GetApproxRPFromPixPt(rPoint
)) : NOCHILDSELECTED
;
123 if (nChild
!= NOCHILDSELECTED
)
124 xRet
= getAccessibleChild( nChild
);
129 // XAccessibleContext
130 sal_Int64 SAL_CALL
SvxRectCtlAccessibleContext::getAccessibleChildCount()
132 return SvxRectCtl::NO_CHILDREN
;
135 Reference
< XAccessible
> SAL_CALL
SvxRectCtlAccessibleContext::getAccessibleChild( sal_Int64 nIndex
)
137 checkChildIndex( nIndex
);
139 Reference
< XAccessible
> xChild(mvChildren
[ nIndex
]);
142 ::SolarMutexGuard aSolarGuard
;
144 ::osl::MutexGuard
aGuard( m_aMutex
);
146 xChild
= mvChildren
[ nIndex
].get();
148 if (!xChild
.is() && mpRepr
)
150 const ChildIndexToPointData
* p
= IndexToPoint( nIndex
);
152 tools::Rectangle
aFocusRect( mpRepr
->CalculateFocusRectangle( p
->ePoint
) );
154 rtl::Reference
<SvxRectCtlChildAccessibleContext
> pChild
= new SvxRectCtlChildAccessibleContext(this,
155 SvxResId(p
->pResIdName
), SvxResId(p
->pResIdDescr
), aFocusRect
, nIndex
);
156 mvChildren
[ nIndex
] = pChild
;
160 if( mnSelectedChild
== nIndex
)
161 pChild
->setStateChecked( true );
168 Reference
< XAccessible
> SAL_CALL
SvxRectCtlAccessibleContext::getAccessibleParent()
170 ::osl::MutexGuard
aGuard( m_aMutex
);
172 return mpRepr
->getAccessibleParent();
173 return uno::Reference
<css::accessibility::XAccessible
>();
176 sal_Int16 SAL_CALL
SvxRectCtlAccessibleContext::getAccessibleRole()
178 return AccessibleRole::PANEL
;
181 OUString SAL_CALL
SvxRectCtlAccessibleContext::getAccessibleDescription()
183 ::osl::MutexGuard
aGuard( m_aMutex
);
184 return msDescription
+ " Please use arrow key to selection.";
187 OUString SAL_CALL
SvxRectCtlAccessibleContext::getAccessibleName()
189 ::osl::MutexGuard
aGuard( m_aMutex
);
193 /** Return empty reference to indicate that the relation set is not
196 Reference
< XAccessibleRelationSet
> SAL_CALL
SvxRectCtlAccessibleContext::getAccessibleRelationSet()
198 ::osl::MutexGuard
aGuard( m_aMutex
);
200 return mpRepr
->get_accessible_relation_set();
201 return uno::Reference
<css::accessibility::XAccessibleRelationSet
>();
204 sal_Int64 SAL_CALL
SvxRectCtlAccessibleContext::getAccessibleStateSet()
206 ::osl::MutexGuard
aGuard( m_aMutex
);
207 sal_Int64 nStateSet
= 0;
211 nStateSet
|= AccessibleStateType::ENABLED
;
212 nStateSet
|= AccessibleStateType::FOCUSABLE
;
213 if( mpRepr
->HasFocus() )
214 nStateSet
|= AccessibleStateType::FOCUSED
;
215 nStateSet
|= AccessibleStateType::OPAQUE
;
217 nStateSet
|= AccessibleStateType::SHOWING
;
219 if( mpRepr
->IsVisible() )
220 nStateSet
|= AccessibleStateType::VISIBLE
;
223 nStateSet
|= AccessibleStateType::DEFUNC
;
228 void SAL_CALL
SvxRectCtlAccessibleContext::grabFocus()
230 ::SolarMutexGuard aSolarGuard
;
231 ::osl::MutexGuard
aGuard( m_aMutex
);
237 sal_Int32
SvxRectCtlAccessibleContext::getForeground()
239 ::SolarMutexGuard aSolarGuard
;
240 ::osl::MutexGuard
aGuard( m_aMutex
);
242 //see SvxRectCtl::Paint
243 const StyleSettings
& rStyles
= Application::GetSettings().GetStyleSettings();
244 return sal_Int32(rStyles
.GetLabelTextColor());
247 sal_Int32
SvxRectCtlAccessibleContext::getBackground( )
249 ::SolarMutexGuard aSolarGuard
;
250 ::osl::MutexGuard
aGuard( m_aMutex
);
252 //see SvxRectCtl::Paint
253 const StyleSettings
& rStyles
= Application::GetSettings().GetStyleSettings();
254 return sal_Int32(rStyles
.GetDialogColor());
257 // XAccessibleSelection
258 void SvxRectCtlAccessibleContext::implSelect(sal_Int64 nIndex
, bool bSelect
)
260 ::SolarMutexGuard aSolarGuard
;
262 ::osl::MutexGuard
aGuard( m_aMutex
);
264 checkChildIndex( nIndex
);
268 const ChildIndexToPointData
* pData
= IndexToPoint(nIndex
);
270 assert(pData
&& "SvxRectCtlAccessibleContext::selectAccessibleChild(): this is an impossible state! Or at least should be...");
274 // this does all what is needed, including the change of the child's state!
275 mpRepr
->SetActualRP( pData
->ePoint
);
279 SAL_WARN( "svx", "SvxRectCtlAccessibleContext::clearAccessibleSelection() is not possible!" );
284 bool SvxRectCtlAccessibleContext::implIsSelected( sal_Int64 nIndex
)
286 ::osl::MutexGuard
aGuard( m_aMutex
);
288 checkChildIndex( nIndex
);
290 return nIndex
== mnSelectedChild
;
294 void SvxRectCtlAccessibleContext::checkChildIndex( sal_Int64 nIndex
)
296 if( nIndex
< 0 || nIndex
>= getAccessibleChildCount() )
297 throw lang::IndexOutOfBoundsException();
300 void SvxRectCtlAccessibleContext::FireChildFocus( RectPoint eButton
)
302 ::osl::MutexGuard
aGuard( m_aMutex
);
303 tools::Long nNew
= PointToIndex( eButton
);
304 tools::Long nNumOfChildren
= getAccessibleChildCount();
305 if( nNew
< nNumOfChildren
)
308 mnSelectedChild
= nNew
;
309 if( nNew
!= NOCHILDSELECTED
)
311 if( mvChildren
[ nNew
].is() )
312 mvChildren
[ nNew
]->FireFocusEvent();
318 aNew
<<= AccessibleStateType::FOCUSED
;
319 NotifyAccessibleEvent(AccessibleEventId::STATE_CHANGED
, aOld
, aNew
);
323 mnSelectedChild
= NOCHILDSELECTED
;
326 void SvxRectCtlAccessibleContext::selectChild( tools::Long nNew
)
328 ::osl::MutexGuard
aGuard( m_aMutex
);
329 if( nNew
== mnSelectedChild
)
332 tools::Long nNumOfChildren
= getAccessibleChildCount();
333 if( nNew
< nNumOfChildren
)
335 if( mnSelectedChild
!= NOCHILDSELECTED
)
336 { // deselect old selected child if one is selected
337 SvxRectCtlChildAccessibleContext
* pChild
= mvChildren
[ mnSelectedChild
].get();
339 pChild
->setStateChecked( false );
343 mnSelectedChild
= nNew
;
345 if( nNew
!= NOCHILDSELECTED
)
347 if( mvChildren
[ nNew
].is() )
348 mvChildren
[ nNew
]->setStateChecked( true );
352 mnSelectedChild
= NOCHILDSELECTED
;
355 void SvxRectCtlAccessibleContext::selectChild(RectPoint eButton
)
357 // no guard -> is done in next selectChild
358 selectChild(PointToIndex( eButton
));
361 void SAL_CALL
SvxRectCtlAccessibleContext::disposing()
363 ::osl::MutexGuard
aGuard(m_aMutex
);
364 OAccessibleSelectionHelper::disposing();
365 for (auto & rxChild
: mvChildren
)
374 awt::Rectangle
SvxRectCtlAccessibleContext::implGetBounds()
376 ::SolarMutexGuard aSolarGuard
;
377 ::osl::MutexGuard
aGuard( m_aMutex
);
384 Size
aOutSize(mpRepr
->GetOutputSizePixel());
386 aRet
.X
= aOutPos
.X();
387 aRet
.Y
= aOutPos
.Y();
388 aRet
.Width
= aOutSize
.Width();
389 aRet
.Height
= aOutSize
.Height();
395 SvxRectCtlChildAccessibleContext::SvxRectCtlChildAccessibleContext(
396 const Reference
<XAccessible
>& rxParent
,
398 OUString aDescription
,
399 const tools::Rectangle
& rBoundingBox
,
400 tools::Long nIndexInParent
)
401 : msDescription(std::move( aDescription
))
402 , msName(std::move( aName
))
404 , maBoundingBox( rBoundingBox
)
405 , mnIndexInParent( nIndexInParent
)
406 , mbIsChecked( false )
410 SvxRectCtlChildAccessibleContext::~SvxRectCtlChildAccessibleContext()
415 Reference
< XAccessible
> SAL_CALL
SvxRectCtlChildAccessibleContext::getAccessibleAtPoint( const awt::Point
& /*rPoint*/ )
417 return Reference
< XAccessible
>();
420 void SAL_CALL
SvxRectCtlChildAccessibleContext::grabFocus()
424 sal_Int32
SvxRectCtlChildAccessibleContext::getForeground( )
426 ::SolarMutexGuard aSolarGuard
;
427 ::osl::MutexGuard
aGuard( m_aMutex
);
429 //see SvxRectCtl::Paint
430 const StyleSettings
& rStyles
= Application::GetSettings().GetStyleSettings();
431 return sal_Int32(rStyles
.GetLabelTextColor());
434 sal_Int32
SvxRectCtlChildAccessibleContext::getBackground( )
436 ::SolarMutexGuard aSolarGuard
;
437 ::osl::MutexGuard
aGuard( m_aMutex
);
439 //see SvxRectCtl::Paint
440 const StyleSettings
& rStyles
= Application::GetSettings().GetStyleSettings();
441 return sal_Int32(rStyles
.GetDialogColor());
444 // XAccessibleContext
445 sal_Int64 SAL_CALL
SvxRectCtlChildAccessibleContext::getAccessibleChildCount()
450 Reference
< XAccessible
> SAL_CALL
SvxRectCtlChildAccessibleContext::getAccessibleChild( sal_Int64
/*nIndex*/ )
452 throw lang::IndexOutOfBoundsException();
455 Reference
< XAccessible
> SAL_CALL
SvxRectCtlChildAccessibleContext::getAccessibleParent()
460 sal_Int16 SAL_CALL
SvxRectCtlChildAccessibleContext::getAccessibleRole()
462 return AccessibleRole::RADIO_BUTTON
;
465 OUString SAL_CALL
SvxRectCtlChildAccessibleContext::getAccessibleDescription()
467 return msDescription
;
470 OUString SAL_CALL
SvxRectCtlChildAccessibleContext::getAccessibleName()
475 /** Return empty reference to indicate that the relation set is not
478 Reference
<XAccessibleRelationSet
> SAL_CALL
SvxRectCtlChildAccessibleContext::getAccessibleRelationSet()
480 rtl::Reference
<utl::AccessibleRelationSetHelper
> pRelationSetHelper
= new utl::AccessibleRelationSetHelper
;
483 uno::Sequence
<uno::Reference
<css::accessibility::XAccessible
>> aSequence
{ mxParent
};
484 pRelationSetHelper
->AddRelation(css::accessibility::AccessibleRelation(css::accessibility::AccessibleRelationType_MEMBER_OF
, aSequence
));
487 return pRelationSetHelper
;
490 sal_Int64 SAL_CALL
SvxRectCtlChildAccessibleContext::getAccessibleStateSet()
492 ::osl::MutexGuard
aGuard( m_aMutex
);
493 sal_Int64 nStateSet
= 0;
495 if (!rBHelper
.bDisposed
)
499 nStateSet
|= AccessibleStateType::CHECKED
;
502 nStateSet
|= AccessibleStateType::ENABLED
;
503 nStateSet
|= AccessibleStateType::SENSITIVE
;
504 nStateSet
|= AccessibleStateType::OPAQUE
;
505 nStateSet
|= AccessibleStateType::SELECTABLE
;
506 nStateSet
|= AccessibleStateType::SHOWING
;
507 nStateSet
|= AccessibleStateType::VISIBLE
;
510 nStateSet
|= AccessibleStateType::DEFUNC
;
516 Any SAL_CALL
SvxRectCtlChildAccessibleContext::getCurrentValue()
519 aRet
<<= ( mbIsChecked
? 1.0 : 0.0 );
523 sal_Bool SAL_CALL
SvxRectCtlChildAccessibleContext::setCurrentValue( const Any
& /*aNumber*/ )
528 Any SAL_CALL
SvxRectCtlChildAccessibleContext::getMaximumValue()
535 Any SAL_CALL
SvxRectCtlChildAccessibleContext::getMinimumValue()
542 Any SAL_CALL
SvxRectCtlChildAccessibleContext::getMinimumIncrement()
553 sal_Int32
SvxRectCtlChildAccessibleContext::getAccessibleActionCount( )
559 sal_Bool
SvxRectCtlChildAccessibleContext::doAccessibleAction ( sal_Int32 nIndex
)
561 ::osl::MutexGuard
aGuard( m_aMutex
);
563 if ( nIndex
< 0 || nIndex
>= getAccessibleActionCount() )
564 throw IndexOutOfBoundsException();
566 Reference
<XAccessibleSelection
> xSelection( mxParent
, UNO_QUERY
);
568 xSelection
->selectAccessibleChild(mnIndexInParent
);
574 OUString
SvxRectCtlChildAccessibleContext::getAccessibleActionDescription ( sal_Int32 nIndex
)
576 ::osl::MutexGuard
aGuard( m_aMutex
);
578 if ( nIndex
< 0 || nIndex
>= getAccessibleActionCount() )
579 throw IndexOutOfBoundsException();
581 return u
"select"_ustr
;
585 Reference
< XAccessibleKeyBinding
> SvxRectCtlChildAccessibleContext::getAccessibleActionKeyBinding( sal_Int32 nIndex
)
587 ::osl::MutexGuard
aGuard( m_aMutex
);
589 if ( nIndex
< 0 || nIndex
>= getAccessibleActionCount() )
590 throw IndexOutOfBoundsException();
592 return Reference
< XAccessibleKeyBinding
>();
595 void SAL_CALL
SvxRectCtlChildAccessibleContext::disposing()
597 OAccessibleComponentHelper::disposing();
601 awt::Rectangle
SvxRectCtlChildAccessibleContext::implGetBounds( )
603 // no guard necessary, because no one changes maBoundingBox after creating it
604 return vcl::unohelper::ConvertToAWTRect(maBoundingBox
);
607 void SvxRectCtlChildAccessibleContext::setStateChecked( bool bChecked
)
609 if( mbIsChecked
== bChecked
)
612 mbIsChecked
= bChecked
;
616 Any
& rMod
= bChecked
? aNew
: aOld
;
618 //Send the STATE_CHANGED(Focused) event to accessible
619 rMod
<<= AccessibleStateType::FOCUSED
;
620 NotifyAccessibleEvent(AccessibleEventId::STATE_CHANGED
, aOld
, aNew
);
622 rMod
<<= AccessibleStateType::CHECKED
;
624 NotifyAccessibleEvent(AccessibleEventId::STATE_CHANGED
, aOld
, aNew
);
627 void SvxRectCtlChildAccessibleContext::FireFocusEvent()
631 aNew
<<= AccessibleStateType::FOCUSED
;
632 NotifyAccessibleEvent(AccessibleEventId::STATE_CHANGED
, aOld
, aNew
);
635 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */