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 <editeng/AccessibleContextBase.hxx>
21 :
22 : #include <com/sun/star/accessibility/AccessibleRole.hpp>
23 : #include <com/sun/star/beans/PropertyChangeEvent.hpp>
24 : #include <com/sun/star/accessibility/XAccessibleEventListener.hpp>
25 : #include <com/sun/star/accessibility/AccessibleStateType.hpp>
26 : #include <com/sun/star/accessibility/AccessibleRelationType.hpp>
27 :
28 : #include <unotools/accessiblestatesethelper.hxx>
29 : #include <unotools/accessiblerelationsethelper.hxx>
30 : #include <comphelper/accessibleeventnotifier.hxx>
31 : #include <comphelper/servicehelper.hxx>
32 : #include <cppuhelper/supportsservice.hxx>
33 : #include <osl/mutex.hxx>
34 :
35 : #include <utility>
36 :
37 : using namespace ::com::sun::star;
38 : using namespace ::com::sun::star::accessibility;
39 : using ::com::sun::star::uno::Reference;
40 :
41 : namespace accessibility {
42 :
43 : //===== internal ============================================================
44 :
45 : // Define a shortcut for the somewhot longish base class name.
46 : typedef ::cppu::PartialWeakComponentImplHelper4<
47 : ::com::sun::star::accessibility::XAccessible,
48 : ::com::sun::star::accessibility::XAccessibleContext,
49 : ::com::sun::star::accessibility::XAccessibleEventBroadcaster,
50 : ::com::sun::star::lang::XServiceInfo> BaseClass;
51 :
52 0 : AccessibleContextBase::AccessibleContextBase (
53 : const uno::Reference<XAccessible>& rxParent,
54 : const sal_Int16 aRole)
55 : : BaseClass (MutexOwner::maMutex),
56 : mxStateSet (NULL),
57 : mxRelationSet (NULL),
58 : mxParent(rxParent),
59 : msDescription(),
60 : meDescriptionOrigin(NotSet),
61 : msName(),
62 : meNameOrigin(NotSet),
63 : mnClientId(0),
64 0 : maRole(aRole)
65 : {
66 : // Create the state set.
67 0 : ::utl::AccessibleStateSetHelper* pStateSet = new ::utl::AccessibleStateSetHelper ();
68 0 : mxStateSet = pStateSet;
69 :
70 : // Set some states. Don't use the SetState method because no events
71 : // shall be broadcastet (that is not yet initialized anyway).
72 0 : if (pStateSet != NULL)
73 : {
74 0 : pStateSet->AddState (AccessibleStateType::ENABLED);
75 0 : pStateSet->AddState (AccessibleStateType::SENSITIVE);
76 0 : pStateSet->AddState (AccessibleStateType::SHOWING);
77 0 : pStateSet->AddState (AccessibleStateType::VISIBLE);
78 0 : pStateSet->AddState (AccessibleStateType::FOCUSABLE);
79 0 : pStateSet->AddState (AccessibleStateType::SELECTABLE);
80 : }
81 :
82 : // Create the relation set.
83 0 : ::utl::AccessibleRelationSetHelper* pRelationSet = new ::utl::AccessibleRelationSetHelper ();
84 0 : mxRelationSet = pRelationSet;
85 0 : }
86 :
87 :
88 :
89 :
90 0 : AccessibleContextBase::~AccessibleContextBase(void)
91 : {
92 0 : }
93 :
94 :
95 :
96 :
97 0 : bool AccessibleContextBase::SetState (sal_Int16 aState)
98 : {
99 0 : ::osl::ClearableMutexGuard aGuard (maMutex);
100 : ::utl::AccessibleStateSetHelper* pStateSet =
101 0 : static_cast< ::utl::AccessibleStateSetHelper*>(mxStateSet.get());
102 0 : if ((pStateSet != NULL) && !pStateSet->contains(aState))
103 : {
104 0 : pStateSet->AddState (aState);
105 : // Clear the mutex guard so that it is not locked during calls to
106 : // listeners.
107 0 : aGuard.clear();
108 :
109 : // Send event for all states except the DEFUNC state.
110 0 : if (aState != AccessibleStateType::DEFUNC)
111 : {
112 0 : uno::Any aNewValue;
113 0 : aNewValue <<= aState;
114 : CommitChange(
115 : AccessibleEventId::STATE_CHANGED,
116 : aNewValue,
117 0 : uno::Any());
118 : }
119 0 : return true;
120 : }
121 : else
122 0 : return false;
123 : }
124 :
125 :
126 :
127 :
128 0 : bool AccessibleContextBase::ResetState (sal_Int16 aState)
129 : {
130 0 : ::osl::ClearableMutexGuard aGuard (maMutex);
131 : ::utl::AccessibleStateSetHelper* pStateSet =
132 0 : static_cast< ::utl::AccessibleStateSetHelper*>(mxStateSet.get());
133 0 : if ((pStateSet != NULL) && pStateSet->contains(aState))
134 : {
135 0 : pStateSet->RemoveState (aState);
136 : // Clear the mutex guard so that it is not locked during calls to listeners.
137 0 : aGuard.clear();
138 :
139 0 : uno::Any aOldValue;
140 0 : aOldValue <<= aState;
141 : CommitChange(
142 : AccessibleEventId::STATE_CHANGED,
143 : uno::Any(),
144 0 : aOldValue);
145 0 : return true;
146 : }
147 : else
148 0 : return false;
149 : }
150 :
151 :
152 :
153 :
154 0 : bool AccessibleContextBase::GetState (sal_Int16 aState)
155 : {
156 0 : ::osl::MutexGuard aGuard (maMutex);
157 : ::utl::AccessibleStateSetHelper* pStateSet =
158 0 : static_cast< ::utl::AccessibleStateSetHelper*>(mxStateSet.get());
159 0 : if (pStateSet != NULL)
160 0 : return pStateSet->contains(aState);
161 : else
162 : // If there is no state set then return false as a default value.
163 0 : return false;
164 : }
165 :
166 :
167 :
168 :
169 0 : void AccessibleContextBase::SetRelationSet (
170 : const uno::Reference<XAccessibleRelationSet>& rxNewRelationSet)
171 : throw (::com::sun::star::uno::RuntimeException)
172 : {
173 : OSL_TRACE ("setting relation set");
174 :
175 : // Try to emit some meaningfull events indicating differing relations in
176 : // both sets.
177 : typedef std::pair<short int,short int> RD;
178 : const RD aRelationDescriptors[] = {
179 : RD(AccessibleRelationType::CONTROLLED_BY, AccessibleEventId::CONTROLLED_BY_RELATION_CHANGED),
180 : RD(AccessibleRelationType::CONTROLLER_FOR, AccessibleEventId::CONTROLLER_FOR_RELATION_CHANGED),
181 : RD(AccessibleRelationType::LABELED_BY, AccessibleEventId::LABELED_BY_RELATION_CHANGED),
182 : RD(AccessibleRelationType::LABEL_FOR, AccessibleEventId::LABEL_FOR_RELATION_CHANGED),
183 : RD(AccessibleRelationType::MEMBER_OF, AccessibleEventId::MEMBER_OF_RELATION_CHANGED),
184 : RD(AccessibleRelationType::INVALID, -1),
185 0 : };
186 0 : for (int i=0; aRelationDescriptors[i].first!=AccessibleRelationType::INVALID; i++)
187 0 : if (mxRelationSet->containsRelation(aRelationDescriptors[i].first)
188 0 : != rxNewRelationSet->containsRelation(aRelationDescriptors[i].first))
189 0 : CommitChange (aRelationDescriptors[i].second, uno::Any(), uno::Any());
190 :
191 0 : mxRelationSet = rxNewRelationSet;
192 0 : }
193 :
194 :
195 :
196 :
197 : //===== XAccessible =========================================================
198 :
199 : uno::Reference< XAccessibleContext> SAL_CALL
200 0 : AccessibleContextBase::getAccessibleContext (void)
201 : throw (uno::RuntimeException, std::exception)
202 : {
203 0 : ThrowIfDisposed ();
204 0 : return this;
205 : }
206 :
207 :
208 :
209 :
210 : //===== XAccessibleContext ==================================================
211 :
212 : /** No children.
213 : */
214 : sal_Int32 SAL_CALL
215 0 : AccessibleContextBase::getAccessibleChildCount (void)
216 : throw (uno::RuntimeException, std::exception)
217 : {
218 0 : ThrowIfDisposed ();
219 0 : return 0;
220 : }
221 :
222 :
223 :
224 :
225 : /** Forward the request to the shape. Return the requested shape or throw
226 : an exception for a wrong index.
227 : */
228 : uno::Reference<XAccessible> SAL_CALL
229 0 : AccessibleContextBase::getAccessibleChild (sal_Int32 nIndex)
230 : throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::uno::RuntimeException, std::exception)
231 : {
232 0 : ThrowIfDisposed ();
233 : throw lang::IndexOutOfBoundsException (
234 0 : "no child with index " + OUString(nIndex),
235 0 : NULL);
236 : }
237 :
238 :
239 :
240 :
241 : uno::Reference<XAccessible> SAL_CALL
242 0 : AccessibleContextBase::getAccessibleParent (void)
243 : throw (::com::sun::star::uno::RuntimeException, std::exception)
244 : {
245 0 : ThrowIfDisposed ();
246 0 : return mxParent;
247 : }
248 :
249 :
250 :
251 :
252 : sal_Int32 SAL_CALL
253 0 : AccessibleContextBase::getAccessibleIndexInParent (void)
254 : throw (::com::sun::star::uno::RuntimeException, std::exception)
255 : {
256 0 : ThrowIfDisposed ();
257 : // Use a simple but slow solution for now. Optimize later.
258 :
259 : // Iterate over all the parent's children and search for this object.
260 0 : if (mxParent.is())
261 : {
262 : uno::Reference<XAccessibleContext> xParentContext (
263 0 : mxParent->getAccessibleContext());
264 0 : if (xParentContext.is())
265 : {
266 0 : sal_Int32 nChildCount = xParentContext->getAccessibleChildCount();
267 0 : for (sal_Int32 i=0; i<nChildCount; i++)
268 : {
269 0 : uno::Reference<XAccessible> xChild (xParentContext->getAccessibleChild (i));
270 0 : if (xChild.is())
271 : {
272 0 : uno::Reference<XAccessibleContext> xChildContext = xChild->getAccessibleContext();
273 0 : if (xChildContext == (XAccessibleContext*)this)
274 0 : return i;
275 : }
276 0 : }
277 0 : }
278 : }
279 :
280 : // Return -1 to indicate that this object's parent does not know about the
281 : // object.
282 0 : return -1;
283 : }
284 :
285 :
286 :
287 :
288 : sal_Int16 SAL_CALL
289 0 : AccessibleContextBase::getAccessibleRole (void)
290 : throw (::com::sun::star::uno::RuntimeException, std::exception)
291 : {
292 0 : ThrowIfDisposed ();
293 0 : return maRole;
294 : }
295 :
296 :
297 :
298 :
299 : OUString SAL_CALL
300 0 : AccessibleContextBase::getAccessibleDescription (void)
301 : throw (::com::sun::star::uno::RuntimeException, std::exception)
302 : {
303 0 : ThrowIfDisposed ();
304 :
305 0 : return msDescription;
306 : }
307 :
308 :
309 :
310 :
311 : OUString SAL_CALL
312 0 : AccessibleContextBase::getAccessibleName (void)
313 : throw (::com::sun::star::uno::RuntimeException, std::exception)
314 : {
315 0 : ThrowIfDisposed ();
316 :
317 0 : if (meNameOrigin == NotSet)
318 : {
319 : // Do not send an event because this is the first time it has been
320 : // requested.
321 0 : msName = CreateAccessibleName();
322 0 : meNameOrigin = AutomaticallyCreated;
323 : }
324 :
325 0 : return msName;
326 : }
327 :
328 :
329 :
330 :
331 : /** Return a copy of the relation set.
332 : */
333 : uno::Reference<XAccessibleRelationSet> SAL_CALL
334 0 : AccessibleContextBase::getAccessibleRelationSet (void)
335 : throw (::com::sun::star::uno::RuntimeException, std::exception)
336 : {
337 0 : ThrowIfDisposed ();
338 :
339 : // Create a copy of the relation set and return it.
340 : ::utl::AccessibleRelationSetHelper* pRelationSet =
341 0 : static_cast< ::utl::AccessibleRelationSetHelper*>(mxRelationSet.get());
342 0 : if (pRelationSet != NULL)
343 : {
344 : return uno::Reference<XAccessibleRelationSet> (
345 0 : new ::utl::AccessibleRelationSetHelper (*pRelationSet));
346 : }
347 : else
348 0 : return uno::Reference<XAccessibleRelationSet>(NULL);
349 : }
350 :
351 :
352 :
353 :
354 : /** Return a copy of the state set.
355 : Possible states are:
356 : ENABLED
357 : SHOWING
358 : VISIBLE
359 : */
360 : uno::Reference<XAccessibleStateSet> SAL_CALL
361 0 : AccessibleContextBase::getAccessibleStateSet (void)
362 : throw (::com::sun::star::uno::RuntimeException, std::exception)
363 : {
364 0 : ::utl::AccessibleStateSetHelper* pStateSet = NULL;
365 :
366 0 : if (rBHelper.bDisposed)
367 : {
368 : // We are already disposed!
369 : // Create a new state set that has only set the DEFUNC state.
370 0 : pStateSet = new ::utl::AccessibleStateSetHelper ();
371 0 : if (pStateSet != NULL)
372 0 : pStateSet->AddState (AccessibleStateType::DEFUNC);
373 : }
374 : else
375 : {
376 : // Create a copy of the state set and return it.
377 0 : pStateSet = static_cast< ::utl::AccessibleStateSetHelper*>(mxStateSet.get());
378 :
379 : // Merge current focused state from edit engine.
380 : #if 0
381 : if (aState == AccessibleStateType::FOCUSED
382 : && pStateSet != NULL
383 : && mpText != NULL)
384 : {
385 : if (mpText->GetFocusedState ())
386 : pStateSet->AddState (aState);
387 : else
388 : pStateSet->RemoveState (aState);
389 : }
390 : #endif
391 0 : if (pStateSet != NULL)
392 0 : pStateSet = new ::utl::AccessibleStateSetHelper (*pStateSet);
393 : }
394 :
395 0 : return uno::Reference<XAccessibleStateSet>(pStateSet);
396 : }
397 :
398 :
399 :
400 :
401 : lang::Locale SAL_CALL
402 0 : AccessibleContextBase::getLocale (void)
403 : throw (IllegalAccessibleComponentStateException,
404 : ::com::sun::star::uno::RuntimeException, std::exception)
405 : {
406 0 : ThrowIfDisposed ();
407 : // Delegate request to parent.
408 0 : if (mxParent.is())
409 : {
410 : uno::Reference<XAccessibleContext> xParentContext (
411 0 : mxParent->getAccessibleContext());
412 0 : if (xParentContext.is())
413 0 : return xParentContext->getLocale ();
414 : }
415 :
416 : // No locale and no parent. Therefore throw exception to indicate this
417 : // cluelessness.
418 0 : throw IllegalAccessibleComponentStateException ();
419 : }
420 :
421 :
422 :
423 :
424 : //===== XAccessibleEventListener ============================================
425 :
426 0 : void SAL_CALL AccessibleContextBase::addAccessibleEventListener (
427 : const uno::Reference<XAccessibleEventListener >& rxListener)
428 : throw (uno::RuntimeException, std::exception)
429 : {
430 0 : if (rxListener.is())
431 : {
432 0 : if (rBHelper.bDisposed || rBHelper.bInDispose)
433 : {
434 0 : uno::Reference<uno::XInterface> x ((lang::XComponent *)this, uno::UNO_QUERY);
435 0 : rxListener->disposing (lang::EventObject (x));
436 : }
437 : else
438 : {
439 0 : if (!mnClientId)
440 0 : mnClientId = comphelper::AccessibleEventNotifier::registerClient( );
441 0 : comphelper::AccessibleEventNotifier::addEventListener( mnClientId, rxListener );
442 : }
443 : }
444 0 : }
445 :
446 :
447 :
448 :
449 0 : void SAL_CALL AccessibleContextBase::removeAccessibleEventListener (
450 : const uno::Reference<XAccessibleEventListener >& rxListener )
451 : throw (uno::RuntimeException, std::exception)
452 : {
453 0 : ThrowIfDisposed ();
454 0 : if (rxListener.is())
455 : {
456 0 : sal_Int32 nListenerCount = comphelper::AccessibleEventNotifier::removeEventListener( mnClientId, rxListener );
457 0 : if ( !nListenerCount )
458 : {
459 : // no listeners anymore
460 : // -> revoke ourself. This may lead to the notifier thread dying (if we were the last client),
461 : // and at least to us not firing any events anymore, in case somebody calls
462 : // NotifyAccessibleEvent, again
463 0 : comphelper::AccessibleEventNotifier::revokeClient( mnClientId );
464 0 : mnClientId = 0;
465 : }
466 : }
467 0 : }
468 :
469 : //===== XServiceInfo ========================================================
470 0 : OUString SAL_CALL AccessibleContextBase::getImplementationName (void)
471 : throw (::com::sun::star::uno::RuntimeException, std::exception)
472 : {
473 0 : ThrowIfDisposed ();
474 0 : return OUString("AccessibleContextBase");
475 : }
476 :
477 0 : sal_Bool SAL_CALL AccessibleContextBase::supportsService (const OUString& sServiceName)
478 : throw (::com::sun::star::uno::RuntimeException, std::exception)
479 : {
480 0 : return cppu::supportsService(this, sServiceName);
481 : }
482 :
483 : uno::Sequence< OUString > SAL_CALL
484 0 : AccessibleContextBase::getSupportedServiceNames (void)
485 : throw (::com::sun::star::uno::RuntimeException, std::exception)
486 : {
487 0 : ThrowIfDisposed ();
488 : static const OUString sServiceNames[2] = {
489 : OUString("com.sun.star.accessibility.Accessible"),
490 : OUString("com.sun.star.accessibility.AccessibleContext")
491 0 : };
492 0 : return uno::Sequence<OUString> (sServiceNames, 2);
493 : }
494 :
495 :
496 :
497 :
498 : //===== XTypeProvider =======================================================
499 :
500 : uno::Sequence< ::com::sun::star::uno::Type>
501 0 : AccessibleContextBase::getTypes (void)
502 : throw (::com::sun::star::uno::RuntimeException, std::exception)
503 : {
504 0 : ThrowIfDisposed ();
505 :
506 : // This class supports no interfaces on its own. Just return those
507 : // supported by the base class.
508 0 : return BaseClass::getTypes();
509 : }
510 :
511 : uno::Sequence<sal_Int8> SAL_CALL
512 0 : AccessibleContextBase::getImplementationId (void)
513 : throw (::com::sun::star::uno::RuntimeException, std::exception)
514 : {
515 0 : return css::uno::Sequence<sal_Int8>();
516 : }
517 :
518 : //===== internal ============================================================
519 :
520 0 : void SAL_CALL AccessibleContextBase::disposing (void)
521 : {
522 0 : SetState (AccessibleStateType::DEFUNC);
523 :
524 0 : ::osl::MutexGuard aGuard (maMutex);
525 :
526 : // Send a disposing to all listeners.
527 0 : if ( mnClientId )
528 : {
529 0 : comphelper::AccessibleEventNotifier::revokeClientNotifyDisposing( mnClientId, *this );
530 0 : mnClientId = 0;
531 0 : }
532 0 : }
533 :
534 :
535 :
536 :
537 0 : void AccessibleContextBase::SetAccessibleDescription (
538 : const OUString& rDescription,
539 : StringOrigin eDescriptionOrigin)
540 : throw (uno::RuntimeException)
541 : {
542 0 : if (eDescriptionOrigin < meDescriptionOrigin
543 0 : || (eDescriptionOrigin == meDescriptionOrigin && msDescription != rDescription))
544 : {
545 0 : uno::Any aOldValue, aNewValue;
546 0 : aOldValue <<= msDescription;
547 0 : aNewValue <<= rDescription;
548 :
549 0 : msDescription = rDescription;
550 0 : meDescriptionOrigin = eDescriptionOrigin;
551 :
552 : CommitChange(
553 : AccessibleEventId::DESCRIPTION_CHANGED,
554 : aNewValue,
555 0 : aOldValue);
556 : }
557 0 : }
558 :
559 :
560 :
561 :
562 0 : void AccessibleContextBase::SetAccessibleName (
563 : const OUString& rName,
564 : StringOrigin eNameOrigin)
565 : throw (uno::RuntimeException)
566 : {
567 0 : if (eNameOrigin < meNameOrigin
568 0 : || (eNameOrigin == meNameOrigin && msName != rName))
569 : {
570 0 : uno::Any aOldValue, aNewValue;
571 0 : aOldValue <<= msName;
572 0 : aNewValue <<= rName;
573 :
574 0 : msName = rName;
575 0 : meNameOrigin = eNameOrigin;
576 :
577 : CommitChange(
578 : AccessibleEventId::NAME_CHANGED,
579 : aNewValue,
580 0 : aOldValue);
581 : }
582 0 : }
583 :
584 :
585 :
586 :
587 0 : OUString AccessibleContextBase::CreateAccessibleDescription (void)
588 : throw (::com::sun::star::uno::RuntimeException)
589 : {
590 0 : return OUString("Empty Description");
591 : }
592 :
593 :
594 :
595 :
596 0 : OUString AccessibleContextBase::CreateAccessibleName (void)
597 : throw (::com::sun::star::uno::RuntimeException)
598 : {
599 0 : return OUString("Empty Name");
600 : }
601 :
602 :
603 :
604 :
605 0 : void AccessibleContextBase::CommitChange (
606 : sal_Int16 nEventId,
607 : const uno::Any& rNewValue,
608 : const uno::Any& rOldValue)
609 : {
610 : // Do not call FireEvent and do not even create the event object when no
611 : // listener has been registered yet. Creating the event object can
612 : // otherwise lead to a crash. See issue 93419 for details.
613 0 : if (mnClientId != 0)
614 : {
615 : AccessibleEventObject aEvent (
616 : static_cast<XAccessibleContext*>(this),
617 : nEventId,
618 : rNewValue,
619 0 : rOldValue);
620 :
621 0 : FireEvent (aEvent);
622 : }
623 0 : }
624 :
625 :
626 :
627 :
628 0 : void AccessibleContextBase::FireEvent (const AccessibleEventObject& aEvent)
629 : {
630 0 : if (mnClientId)
631 0 : comphelper::AccessibleEventNotifier::addEvent( mnClientId, aEvent );
632 0 : }
633 :
634 :
635 :
636 :
637 0 : void AccessibleContextBase::ThrowIfDisposed (void)
638 : throw (::com::sun::star::lang::DisposedException)
639 : {
640 0 : if (rBHelper.bDisposed || rBHelper.bInDispose)
641 : {
642 : OSL_TRACE ("Calling disposed object. Throwing exception:");
643 : throw lang::DisposedException ("object has been already disposed",
644 0 : static_cast<uno::XWeak*>(this));
645 : }
646 0 : }
647 :
648 :
649 :
650 0 : bool AccessibleContextBase::IsDisposed (void)
651 : {
652 0 : return (rBHelper.bDisposed || rBHelper.bInDispose);
653 : }
654 :
655 :
656 :
657 0 : void AccessibleContextBase::SetAccessibleRole( sal_Int16 _nRole )
658 : {
659 0 : maRole = _nRole;
660 0 : }
661 :
662 :
663 : } // end of namespace accessibility
664 :
665 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|