1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: AccessibleContextBase.cxx,v $
10 * $Revision: 1.28.144.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
35 #include <svx/AccessibleContextBase.hxx>
36 #include <com/sun/star/accessibility/AccessibleRole.hpp>
39 #include <com/sun/star/beans/PropertyChangeEvent.hpp>
40 #include <com/sun/star/accessibility/XAccessibleEventListener.hpp>
41 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
42 #include <com/sun/star/accessibility/AccessibleRelationType.hpp>
43 #include <unotools/accessiblestatesethelper.hxx>
44 #include <unotools/accessiblerelationsethelper.hxx>
45 #include <comphelper/accessibleeventnotifier.hxx>
48 #include <vos/mutex.hxx>
49 #include <vcl/svapp.hxx>
53 using namespace ::rtl
;
54 using namespace ::com::sun::star
;
55 using namespace ::com::sun::star::accessibility
;
56 using ::com::sun::star::uno::Reference
;
58 namespace accessibility
{
60 //===== internal ============================================================
62 // Define a shortcut for the somewhot longish base class name.
63 typedef ::cppu::WeakComponentImplHelper4
<
64 ::com::sun::star::accessibility::XAccessible
,
65 ::com::sun::star::accessibility::XAccessibleContext
,
66 ::com::sun::star::accessibility::XAccessibleEventBroadcaster
,
67 ::com::sun::star::lang::XServiceInfo
> BaseClass
;
69 AccessibleContextBase::AccessibleContextBase (
70 const uno::Reference
<XAccessible
>& rxParent
,
71 const sal_Int16 aRole
)
72 : BaseClass (MutexOwner::maMutex
),
77 meDescriptionOrigin(NotSet
),
83 // Create the state set.
84 ::utl::AccessibleStateSetHelper
* pStateSet
= new ::utl::AccessibleStateSetHelper ();
85 mxStateSet
= pStateSet
;
87 // Set some states. Don't use the SetState method because no events
88 // shall be broadcastet (that is not yet initialized anyway).
89 if (pStateSet
!= NULL
)
91 pStateSet
->AddState (AccessibleStateType::ENABLED
);
92 pStateSet
->AddState (AccessibleStateType::SENSITIVE
);
93 pStateSet
->AddState (AccessibleStateType::SHOWING
);
94 pStateSet
->AddState (AccessibleStateType::VISIBLE
);
95 pStateSet
->AddState (AccessibleStateType::FOCUSABLE
);
96 pStateSet
->AddState (AccessibleStateType::SELECTABLE
);
99 // Create the relation set.
100 ::utl::AccessibleRelationSetHelper
* pRelationSet
= new ::utl::AccessibleRelationSetHelper ();
101 mxRelationSet
= pRelationSet
;
107 AccessibleContextBase::~AccessibleContextBase(void)
114 sal_Bool
AccessibleContextBase::SetState (sal_Int16 aState
)
116 ::osl::ClearableMutexGuard
aGuard (maMutex
);
117 ::utl::AccessibleStateSetHelper
* pStateSet
=
118 static_cast< ::utl::AccessibleStateSetHelper
*>(mxStateSet
.get());
119 if ((pStateSet
!= NULL
) && !pStateSet
->contains(aState
))
121 pStateSet
->AddState (aState
);
122 // Clear the mutex guard so that it is not locked during calls to
126 // Send event for all states except the DEFUNC state.
127 if (aState
!= AccessibleStateType::DEFUNC
)
130 aNewValue
<<= aState
;
132 AccessibleEventId::STATE_CHANGED
,
145 sal_Bool
AccessibleContextBase::ResetState (sal_Int16 aState
)
147 ::osl::ClearableMutexGuard
aGuard (maMutex
);
148 ::utl::AccessibleStateSetHelper
* pStateSet
=
149 static_cast< ::utl::AccessibleStateSetHelper
*>(mxStateSet
.get());
150 if ((pStateSet
!= NULL
) && pStateSet
->contains(aState
))
152 pStateSet
->RemoveState (aState
);
153 // Clear the mutex guard so that it is not locked during calls to listeners.
157 aOldValue
<<= aState
;
159 AccessibleEventId::STATE_CHANGED
,
171 sal_Bool
AccessibleContextBase::GetState (sal_Int16 aState
)
173 ::osl::MutexGuard
aGuard (maMutex
);
174 ::utl::AccessibleStateSetHelper
* pStateSet
=
175 static_cast< ::utl::AccessibleStateSetHelper
*>(mxStateSet
.get());
176 if (pStateSet
!= NULL
)
177 return pStateSet
->contains(aState
);
179 // If there is no state set then return false as a default value.
186 void AccessibleContextBase::SetRelationSet (
187 const uno::Reference
<XAccessibleRelationSet
>& rxNewRelationSet
)
188 throw (::com::sun::star::uno::RuntimeException
)
190 OSL_TRACE ("setting relation set");
192 // Try to emit some meaningfull events indicating differing relations in
194 typedef std::pair
<short int,short int> RD
;
195 const RD aRelationDescriptors
[] = {
196 RD(AccessibleRelationType::CONTROLLED_BY
, AccessibleEventId::CONTROLLED_BY_RELATION_CHANGED
),
197 RD(AccessibleRelationType::CONTROLLER_FOR
, AccessibleEventId::CONTROLLER_FOR_RELATION_CHANGED
),
198 RD(AccessibleRelationType::LABELED_BY
, AccessibleEventId::LABELED_BY_RELATION_CHANGED
),
199 RD(AccessibleRelationType::LABEL_FOR
, AccessibleEventId::LABEL_FOR_RELATION_CHANGED
),
200 RD(AccessibleRelationType::MEMBER_OF
, AccessibleEventId::MEMBER_OF_RELATION_CHANGED
),
201 RD(AccessibleRelationType::INVALID
, -1),
203 for (int i
=0; aRelationDescriptors
[i
].first
!=AccessibleRelationType::INVALID
; i
++)
204 if (mxRelationSet
->containsRelation(aRelationDescriptors
[i
].first
)
205 != rxNewRelationSet
->containsRelation(aRelationDescriptors
[i
].first
))
206 CommitChange (aRelationDescriptors
[i
].second
, uno::Any(), uno::Any());
208 mxRelationSet
= rxNewRelationSet
;
214 //===== XAccessible =========================================================
216 uno::Reference
< XAccessibleContext
> SAL_CALL
217 AccessibleContextBase::getAccessibleContext (void)
218 throw (uno::RuntimeException
)
227 //===== XAccessibleContext ==================================================
232 AccessibleContextBase::getAccessibleChildCount (void)
233 throw (uno::RuntimeException
)
242 /** Forward the request to the shape. Return the requested shape or throw
243 an exception for a wrong index.
245 uno::Reference
<XAccessible
> SAL_CALL
246 AccessibleContextBase::getAccessibleChild (sal_Int32 nIndex
)
247 throw (::com::sun::star::lang::IndexOutOfBoundsException
, ::com::sun::star::uno::RuntimeException
)
250 throw lang::IndexOutOfBoundsException (
251 ::rtl::OUString::createFromAscii ("no child with index " + nIndex
),
258 uno::Reference
<XAccessible
> SAL_CALL
259 AccessibleContextBase::getAccessibleParent (void)
260 throw (::com::sun::star::uno::RuntimeException
)
270 AccessibleContextBase::getAccessibleIndexInParent (void)
271 throw (::com::sun::star::uno::RuntimeException
)
274 // Use a simple but slow solution for now. Optimize later.
276 // Iterate over all the parent's children and search for this object.
279 uno::Reference
<XAccessibleContext
> xParentContext (
280 mxParent
->getAccessibleContext());
281 if (xParentContext
.is())
283 sal_Int32 nChildCount
= xParentContext
->getAccessibleChildCount();
284 for (sal_Int32 i
=0; i
<nChildCount
; i
++)
286 uno::Reference
<XAccessible
> xChild (xParentContext
->getAccessibleChild (i
));
289 uno::Reference
<XAccessibleContext
> xChildContext
= xChild
->getAccessibleContext();
290 if (xChildContext
== (XAccessibleContext
*)this)
297 // Return -1 to indicate that this object's parent does not know about the
306 AccessibleContextBase::getAccessibleRole (void)
307 throw (::com::sun::star::uno::RuntimeException
)
316 ::rtl::OUString SAL_CALL
317 AccessibleContextBase::getAccessibleDescription (void)
318 throw (::com::sun::star::uno::RuntimeException
)
322 return msDescription
;
329 AccessibleContextBase::getAccessibleName (void)
330 throw (::com::sun::star::uno::RuntimeException
)
334 if (meNameOrigin
== NotSet
)
336 // Do not send an event because this is the first time it has been
338 msName
= CreateAccessibleName();
339 meNameOrigin
= AutomaticallyCreated
;
348 /** Return a copy of the relation set.
350 uno::Reference
<XAccessibleRelationSet
> SAL_CALL
351 AccessibleContextBase::getAccessibleRelationSet (void)
352 throw (::com::sun::star::uno::RuntimeException
)
356 // Create a copy of the relation set and return it.
357 ::utl::AccessibleRelationSetHelper
* pRelationSet
=
358 static_cast< ::utl::AccessibleRelationSetHelper
*>(mxRelationSet
.get());
359 if (pRelationSet
!= NULL
)
361 return uno::Reference
<XAccessibleRelationSet
> (
362 new ::utl::AccessibleRelationSetHelper (*pRelationSet
));
365 return uno::Reference
<XAccessibleRelationSet
>(NULL
);
371 /** Return a copy of the state set.
377 uno::Reference
<XAccessibleStateSet
> SAL_CALL
378 AccessibleContextBase::getAccessibleStateSet (void)
379 throw (::com::sun::star::uno::RuntimeException
)
381 ::utl::AccessibleStateSetHelper
* pStateSet
= NULL
;
383 if (rBHelper
.bDisposed
)
385 // We are already disposed!
386 // Create a new state set that has only set the DEFUNC state.
387 pStateSet
= new ::utl::AccessibleStateSetHelper ();
388 if (pStateSet
!= NULL
)
389 pStateSet
->AddState (AccessibleStateType::DEFUNC
);
393 // Create a copy of the state set and return it.
394 pStateSet
= static_cast< ::utl::AccessibleStateSetHelper
*>(mxStateSet
.get());
396 // Merge current focused state from edit engine.
398 if (aState
== AccessibleStateType::FOCUSED
402 if (mpText
->GetFocusedState ())
403 pStateSet
->AddState (aState
);
405 pStateSet
->RemoveState (aState
);
408 if (pStateSet
!= NULL
)
409 pStateSet
= new ::utl::AccessibleStateSetHelper (*pStateSet
);
412 return uno::Reference
<XAccessibleStateSet
>(pStateSet
);
418 lang::Locale SAL_CALL
419 AccessibleContextBase::getLocale (void)
420 throw (IllegalAccessibleComponentStateException
,
421 ::com::sun::star::uno::RuntimeException
)
424 // Delegate request to parent.
427 uno::Reference
<XAccessibleContext
> xParentContext (
428 mxParent
->getAccessibleContext());
429 if (xParentContext
.is())
430 return xParentContext
->getLocale ();
433 // No locale and no parent. Therefore throw exception to indicate this
435 throw IllegalAccessibleComponentStateException ();
441 //===== XAccessibleEventListener ============================================
444 AccessibleContextBase::addEventListener (
445 const uno::Reference
<XAccessibleEventListener
>& rxListener
)
446 throw (uno::RuntimeException
)
450 if (rBHelper
.bDisposed
|| rBHelper
.bInDispose
)
452 uno::Reference
<uno::XInterface
> x ((lang::XComponent
*)this, uno::UNO_QUERY
);
453 rxListener
->disposing (lang::EventObject (x
));
458 mnClientId
= comphelper::AccessibleEventNotifier::registerClient( );
459 comphelper::AccessibleEventNotifier::addEventListener( mnClientId
, rxListener
);
468 AccessibleContextBase::removeEventListener (
469 const uno::Reference
<XAccessibleEventListener
>& rxListener
)
470 throw (uno::RuntimeException
)
475 sal_Int32 nListenerCount
= comphelper::AccessibleEventNotifier::removeEventListener( mnClientId
, rxListener
);
476 if ( !nListenerCount
)
478 // no listeners anymore
479 // -> revoke ourself. This may lead to the notifier thread dying (if we were the last client),
480 // and at least to us not firing any events anymore, in case somebody calls
481 // NotifyAccessibleEvent, again
482 comphelper::AccessibleEventNotifier::revokeClient( mnClientId
);
491 //===== XServiceInfo ========================================================
493 ::rtl::OUString SAL_CALL
494 AccessibleContextBase::getImplementationName (void)
495 throw (::com::sun::star::uno::RuntimeException
)
498 return OUString(RTL_CONSTASCII_USTRINGPARAM("AccessibleContextBase"));
505 AccessibleContextBase::supportsService (const OUString
& sServiceName
)
506 throw (::com::sun::star::uno::RuntimeException
)
509 // Iterate over all supported service names and return true if on of them
510 // matches the given name.
511 uno::Sequence
< ::rtl::OUString
> aSupportedServices (
512 getSupportedServiceNames ());
513 for (int i
=0; i
<aSupportedServices
.getLength(); i
++)
514 if (sServiceName
== aSupportedServices
[i
])
522 uno::Sequence
< ::rtl::OUString
> SAL_CALL
523 AccessibleContextBase::getSupportedServiceNames (void)
524 throw (::com::sun::star::uno::RuntimeException
)
527 static const OUString sServiceNames
[2] = {
528 OUString(RTL_CONSTASCII_USTRINGPARAM(
529 "com.sun.star.accessibility.Accessible")),
530 OUString(RTL_CONSTASCII_USTRINGPARAM(
531 "com.sun.star.accessibility.AccessibleContext"))
533 return uno::Sequence
<OUString
> (sServiceNames
, 2);
539 //===== XTypeProvider =======================================================
541 uno::Sequence
< ::com::sun::star::uno::Type
>
542 AccessibleContextBase::getTypes (void)
543 throw (::com::sun::star::uno::RuntimeException
)
547 // This class supports no interfaces on its own. Just return those
548 // supported by the base class.
549 return BaseClass::getTypes();
555 uno::Sequence
<sal_Int8
> SAL_CALL
556 AccessibleContextBase::getImplementationId (void)
557 throw (::com::sun::star::uno::RuntimeException
)
560 static uno::Sequence
<sal_Int8
> aId
;
561 if (aId
.getLength() == 0)
563 ::osl::MutexGuard
aGuard (maMutex
);
565 rtl_createUuid ((sal_uInt8
*)aId
.getArray(), 0, sal_True
);
573 //===== internal ============================================================
575 void SAL_CALL
AccessibleContextBase::disposing (void)
577 SetState (AccessibleStateType::DEFUNC
);
579 ::osl::MutexGuard
aGuard (maMutex
);
581 // Send a disposing to all listeners.
584 comphelper::AccessibleEventNotifier::revokeClientNotifyDisposing( mnClientId
, *this );
592 void AccessibleContextBase::SetAccessibleDescription (
593 const ::rtl::OUString
& rDescription
,
594 StringOrigin eDescriptionOrigin
)
595 throw (uno::RuntimeException
)
597 if (eDescriptionOrigin
< meDescriptionOrigin
598 || (eDescriptionOrigin
== meDescriptionOrigin
&& msDescription
!= rDescription
))
600 uno::Any aOldValue
, aNewValue
;
601 aOldValue
<<= msDescription
;
602 aNewValue
<<= rDescription
;
604 msDescription
= rDescription
;
605 meDescriptionOrigin
= eDescriptionOrigin
;
608 AccessibleEventId::DESCRIPTION_CHANGED
,
617 void AccessibleContextBase::SetAccessibleName (
618 const ::rtl::OUString
& rName
,
619 StringOrigin eNameOrigin
)
620 throw (uno::RuntimeException
)
622 if (eNameOrigin
< meNameOrigin
623 || (eNameOrigin
== meNameOrigin
&& msName
!= rName
))
625 uno::Any aOldValue
, aNewValue
;
626 aOldValue
<<= msName
;
630 meNameOrigin
= eNameOrigin
;
633 AccessibleEventId::NAME_CHANGED
,
642 ::rtl::OUString
AccessibleContextBase::CreateAccessibleDescription (void)
643 throw (::com::sun::star::uno::RuntimeException
)
645 return ::rtl::OUString::createFromAscii ("Empty Description");
651 ::rtl::OUString
AccessibleContextBase::CreateAccessibleName (void)
652 throw (::com::sun::star::uno::RuntimeException
)
654 return ::rtl::OUString::createFromAscii ("Empty Name");
660 void AccessibleContextBase::CommitChange (
662 const uno::Any
& rNewValue
,
663 const uno::Any
& rOldValue
)
665 // Do not call FireEvent and do not even create the event object when no
666 // listener has been registered yet. Creating the event object can
667 // otherwise lead to a crash. See issue 93419 for details.
670 AccessibleEventObject
aEvent (
671 static_cast<XAccessibleContext
*>(this),
683 void AccessibleContextBase::FireEvent (const AccessibleEventObject
& aEvent
)
686 comphelper::AccessibleEventNotifier::addEvent( mnClientId
, aEvent
);
692 void AccessibleContextBase::ThrowIfDisposed (void)
693 throw (::com::sun::star::lang::DisposedException
)
695 if (rBHelper
.bDisposed
|| rBHelper
.bInDispose
)
697 OSL_TRACE ("Calling disposed object. Throwing exception:");
698 throw lang::DisposedException (
699 OUString(RTL_CONSTASCII_USTRINGPARAM("object has been already disposed")),
700 static_cast<uno::XWeak
*>(this));
706 sal_Bool
AccessibleContextBase::IsDisposed (void)
708 return (rBHelper
.bDisposed
|| rBHelper
.bInDispose
);
713 void AccessibleContextBase::SetAccessibleRole( sal_Int16 _nRole
)
719 } // end of namespace accessibility