merge the formfield patch from ooo-build
[ooovba.git] / svx / source / accessibility / AccessibleContextBase.cxx
blob1721f8dccdbe9df41afa3bc4c5a0efea990dfab3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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>
46 #include <rtl/uuid.h>
48 #include <vos/mutex.hxx>
49 #include <vcl/svapp.hxx>
51 #include <utility>
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),
73 mxStateSet (NULL),
74 mxRelationSet (NULL),
75 mxParent(rxParent),
76 msDescription(),
77 meDescriptionOrigin(NotSet),
78 msName(),
79 meNameOrigin(NotSet),
80 mnClientId(0),
81 maRole(aRole)
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
123 // listeners.
124 aGuard.clear();
126 // Send event for all states except the DEFUNC state.
127 if (aState != AccessibleStateType::DEFUNC)
129 uno::Any aNewValue;
130 aNewValue <<= aState;
131 CommitChange(
132 AccessibleEventId::STATE_CHANGED,
133 aNewValue,
134 uno::Any());
136 return sal_True;
138 else
139 return sal_False;
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.
154 aGuard.clear();
156 uno::Any aOldValue;
157 aOldValue <<= aState;
158 CommitChange(
159 AccessibleEventId::STATE_CHANGED,
160 uno::Any(),
161 aOldValue);
162 return sal_True;
164 else
165 return sal_False;
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);
178 else
179 // If there is no state set then return false as a default value.
180 return sal_False;
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
193 // both sets.
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)
220 ThrowIfDisposed ();
221 return this;
227 //===== XAccessibleContext ==================================================
229 /** No children.
231 sal_Int32 SAL_CALL
232 AccessibleContextBase::getAccessibleChildCount (void)
233 throw (uno::RuntimeException)
235 ThrowIfDisposed ();
236 return 0;
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)
249 ThrowIfDisposed ();
250 throw lang::IndexOutOfBoundsException (
251 ::rtl::OUString::createFromAscii ("no child with index " + nIndex),
252 NULL);
258 uno::Reference<XAccessible> SAL_CALL
259 AccessibleContextBase::getAccessibleParent (void)
260 throw (::com::sun::star::uno::RuntimeException)
262 ThrowIfDisposed ();
263 return mxParent;
269 sal_Int32 SAL_CALL
270 AccessibleContextBase::getAccessibleIndexInParent (void)
271 throw (::com::sun::star::uno::RuntimeException)
273 ThrowIfDisposed ();
274 // Use a simple but slow solution for now. Optimize later.
276 // Iterate over all the parent's children and search for this object.
277 if (mxParent.is())
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));
287 if (xChild.is())
289 uno::Reference<XAccessibleContext> xChildContext = xChild->getAccessibleContext();
290 if (xChildContext == (XAccessibleContext*)this)
291 return i;
297 // Return -1 to indicate that this object's parent does not know about the
298 // object.
299 return -1;
305 sal_Int16 SAL_CALL
306 AccessibleContextBase::getAccessibleRole (void)
307 throw (::com::sun::star::uno::RuntimeException)
309 ThrowIfDisposed ();
310 return maRole;
316 ::rtl::OUString SAL_CALL
317 AccessibleContextBase::getAccessibleDescription (void)
318 throw (::com::sun::star::uno::RuntimeException)
320 ThrowIfDisposed ();
322 return msDescription;
328 OUString SAL_CALL
329 AccessibleContextBase::getAccessibleName (void)
330 throw (::com::sun::star::uno::RuntimeException)
332 ThrowIfDisposed ();
334 if (meNameOrigin == NotSet)
336 // Do not send an event because this is the first time it has been
337 // requested.
338 msName = CreateAccessibleName();
339 meNameOrigin = AutomaticallyCreated;
342 return msName;
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)
354 ThrowIfDisposed ();
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));
364 else
365 return uno::Reference<XAccessibleRelationSet>(NULL);
371 /** Return a copy of the state set.
372 Possible states are:
373 ENABLED
374 SHOWING
375 VISIBLE
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);
391 else
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.
397 #if 0
398 if (aState == AccessibleStateType::FOCUSED
399 && pStateSet != NULL
400 && mpText != NULL)
402 if (mpText->GetFocusedState ())
403 pStateSet->AddState (aState);
404 else
405 pStateSet->RemoveState (aState);
407 #endif
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)
423 ThrowIfDisposed ();
424 // Delegate request to parent.
425 if (mxParent.is())
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
434 // cluelessness.
435 throw IllegalAccessibleComponentStateException ();
441 //===== XAccessibleEventListener ============================================
443 void SAL_CALL
444 AccessibleContextBase::addEventListener (
445 const uno::Reference<XAccessibleEventListener >& rxListener)
446 throw (uno::RuntimeException)
448 if (rxListener.is())
450 if (rBHelper.bDisposed || rBHelper.bInDispose)
452 uno::Reference<uno::XInterface> x ((lang::XComponent *)this, uno::UNO_QUERY);
453 rxListener->disposing (lang::EventObject (x));
455 else
457 if (!mnClientId)
458 mnClientId = comphelper::AccessibleEventNotifier::registerClient( );
459 comphelper::AccessibleEventNotifier::addEventListener( mnClientId, rxListener );
467 void SAL_CALL
468 AccessibleContextBase::removeEventListener (
469 const uno::Reference<XAccessibleEventListener >& rxListener )
470 throw (uno::RuntimeException)
472 ThrowIfDisposed ();
473 if (rxListener.is())
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 );
483 mnClientId = 0;
491 //===== XServiceInfo ========================================================
493 ::rtl::OUString SAL_CALL
494 AccessibleContextBase::getImplementationName (void)
495 throw (::com::sun::star::uno::RuntimeException)
497 ThrowIfDisposed ();
498 return OUString(RTL_CONSTASCII_USTRINGPARAM("AccessibleContextBase"));
504 sal_Bool SAL_CALL
505 AccessibleContextBase::supportsService (const OUString& sServiceName)
506 throw (::com::sun::star::uno::RuntimeException)
508 ThrowIfDisposed ();
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])
515 return sal_True;
516 return sal_False;
522 uno::Sequence< ::rtl::OUString> SAL_CALL
523 AccessibleContextBase::getSupportedServiceNames (void)
524 throw (::com::sun::star::uno::RuntimeException)
526 ThrowIfDisposed ();
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)
545 ThrowIfDisposed ();
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)
559 ThrowIfDisposed ();
560 static uno::Sequence<sal_Int8> aId;
561 if (aId.getLength() == 0)
563 ::osl::MutexGuard aGuard (maMutex);
564 aId.realloc (16);
565 rtl_createUuid ((sal_uInt8 *)aId.getArray(), 0, sal_True);
567 return aId;
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.
582 if ( mnClientId )
584 comphelper::AccessibleEventNotifier::revokeClientNotifyDisposing( mnClientId, *this );
585 mnClientId = 0;
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;
607 CommitChange(
608 AccessibleEventId::DESCRIPTION_CHANGED,
609 aNewValue,
610 aOldValue);
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;
627 aNewValue <<= rName;
629 msName = rName;
630 meNameOrigin = eNameOrigin;
632 CommitChange(
633 AccessibleEventId::NAME_CHANGED,
634 aNewValue,
635 aOldValue);
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 (
661 sal_Int16 nEventId,
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.
668 if (mnClientId != 0)
670 AccessibleEventObject aEvent (
671 static_cast<XAccessibleContext*>(this),
672 nEventId,
673 rNewValue,
674 rOldValue);
676 FireEvent (aEvent);
683 void AccessibleContextBase::FireEvent (const AccessibleEventObject& aEvent)
685 if (mnClientId)
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 )
715 maRole = _nRole;
719 } // end of namespace accessibility