LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / accessibility / source / extended / accessiblelistboxentry.cxx
blobb96bb96165a8c30697c8a11f7e7bb3c863ee09ef
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <extended/accessiblelistboxentry.hxx>
21 #include <extended/accessiblelistbox.hxx>
22 #include <vcl/toolkit/treelistbox.hxx>
23 #include <svtools/stringtransfer.hxx>
24 #include <vcl/toolkit/svlbitm.hxx>
25 #include <com/sun/star/awt/Rectangle.hpp>
26 #include <com/sun/star/accessibility/AccessibleRelationType.hpp>
27 #include <com/sun/star/accessibility/AccessibleRole.hpp>
28 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
29 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
30 #include <i18nlangtag/languagetag.hxx>
31 #include <vcl/svapp.hxx>
32 #include <vcl/settings.hxx>
33 #include <toolkit/helper/convert.hxx>
34 #include <unotools/accessiblestatesethelper.hxx>
35 #include <unotools/accessiblerelationsethelper.hxx>
36 #include <cppuhelper/supportsservice.hxx>
37 #include <comphelper/accessibleeventnotifier.hxx>
38 #include <helper/accresmgr.hxx>
39 #include <strings.hrc>
41 #define ACCESSIBLE_ACTION_COUNT 1
43 namespace
45 /// @throws css::lang::IndexOutOfBoundsException
46 void checkActionIndex_Impl( sal_Int32 _nIndex )
48 if ( _nIndex < 0 || _nIndex >= ACCESSIBLE_ACTION_COUNT )
49 // only three actions
50 throw css::lang::IndexOutOfBoundsException();
55 namespace accessibility
57 // class AccessibleListBoxEntry -----------------------------------------------------
59 using namespace ::com::sun::star::accessibility;
60 using namespace ::com::sun::star::uno;
61 using namespace ::com::sun::star::lang;
62 using namespace ::com::sun::star;
63 using namespace ::comphelper;
66 // Ctor() and Dtor()
68 AccessibleListBoxEntry::AccessibleListBoxEntry( SvTreeListBox& _rListBox,
69 SvTreeListEntry& rEntry,
70 AccessibleListBox & rListBox)
71 : AccessibleListBoxEntry_BASE( m_aMutex )
73 , m_pTreeListBox( &_rListBox )
74 , m_pSvLBoxEntry(&rEntry)
75 , m_nClientId( 0 )
76 , m_wListBox(&rListBox)
77 , m_rListBox(rListBox)
79 m_pTreeListBox->AddEventListener( LINK( this, AccessibleListBoxEntry, WindowEventListener ) );
80 _rListBox.FillEntryPath( m_pSvLBoxEntry, m_aEntryPath );
83 AccessibleListBoxEntry::~AccessibleListBoxEntry()
85 if ( IsAlive_Impl() )
87 // increment ref count to prevent double call of Dtor
88 osl_atomic_increment( &m_refCount );
89 dispose();
93 IMPL_LINK( AccessibleListBoxEntry, WindowEventListener, VclWindowEvent&, rEvent, void )
95 OSL_ENSURE( rEvent.GetWindow() , "AccessibleListBoxEntry::WindowEventListener: no event window!" );
96 OSL_ENSURE( rEvent.GetWindow() == m_pTreeListBox, "AccessibleListBoxEntry::WindowEventListener: where did this come from?" );
98 if ( m_pTreeListBox == nullptr )
99 return;
101 switch ( rEvent.GetId() )
103 case VclEventId::ObjectDying :
105 if ( m_pTreeListBox )
106 m_pTreeListBox->RemoveEventListener( LINK( this, AccessibleListBoxEntry, WindowEventListener ) );
107 m_pTreeListBox = nullptr;
108 dispose();
109 break;
111 default: break;
115 void AccessibleListBoxEntry::NotifyAccessibleEvent( sal_Int16 _nEventId,
116 const css::uno::Any& _aOldValue,
117 const css::uno::Any& _aNewValue )
119 Reference< uno::XInterface > xSource( *this );
120 AccessibleEventObject aEventObj( xSource, _nEventId, _aNewValue, _aOldValue );
122 if (m_nClientId)
123 comphelper::AccessibleEventNotifier::addEvent( m_nClientId, aEventObj );
127 tools::Rectangle AccessibleListBoxEntry::GetBoundingBox_Impl() const
129 tools::Rectangle aRect;
130 SvTreeListEntry* pEntry = m_pTreeListBox->GetEntryFromPath( m_aEntryPath );
131 if ( pEntry )
133 aRect = m_pTreeListBox->GetBoundingRect( pEntry );
134 SvTreeListEntry* pParent = m_pTreeListBox->GetParent( pEntry );
135 if ( pParent )
137 // position relative to parent entry
138 Point aTopLeft = aRect.TopLeft();
139 aTopLeft -= m_pTreeListBox->GetBoundingRect( pParent ).TopLeft();
140 aRect = tools::Rectangle( aTopLeft, aRect.GetSize() );
144 return aRect;
147 tools::Rectangle AccessibleListBoxEntry::GetBoundingBoxOnScreen_Impl() const
149 tools::Rectangle aRect;
150 SvTreeListEntry* pEntry = m_pTreeListBox->GetEntryFromPath( m_aEntryPath );
151 if ( pEntry )
153 aRect = m_pTreeListBox->GetBoundingRect( pEntry );
154 Point aTopLeft = aRect.TopLeft();
155 aTopLeft += m_pTreeListBox->GetWindowExtentsRelative( nullptr ).TopLeft();
156 aRect = tools::Rectangle( aTopLeft, aRect.GetSize() );
159 return aRect;
162 bool AccessibleListBoxEntry::IsAlive_Impl() const
164 return !rBHelper.bDisposed && !rBHelper.bInDispose && (m_pTreeListBox != nullptr);
167 bool AccessibleListBoxEntry::IsShowing_Impl() const
169 Reference< XAccessible > xParent = implGetParentAccessible( );
171 bool bShowing = false;
172 Reference< XAccessibleContext > xParentContext =
173 xParent.is() ? xParent->getAccessibleContext() : Reference< XAccessibleContext >();
174 if( xParentContext.is() )
176 Reference< XAccessibleComponent > xParentComp( xParentContext, uno::UNO_QUERY );
177 if( xParentComp.is() )
178 bShowing = GetBoundingBox_Impl().Overlaps( VCLRectangle( xParentComp->getBounds() ) );
181 return bShowing;
184 tools::Rectangle AccessibleListBoxEntry::GetBoundingBox()
186 SolarMutexGuard aSolarGuard;
187 ::osl::MutexGuard aGuard( m_aMutex );
189 EnsureIsAlive();
190 return GetBoundingBox_Impl();
193 tools::Rectangle AccessibleListBoxEntry::GetBoundingBoxOnScreen()
195 SolarMutexGuard aSolarGuard;
196 ::osl::MutexGuard aGuard( m_aMutex );
198 EnsureIsAlive();
199 return GetBoundingBoxOnScreen_Impl();
202 void AccessibleListBoxEntry::EnsureIsAlive() const
204 if ( !IsAlive_Impl() )
205 throw lang::DisposedException();
208 OUString AccessibleListBoxEntry::implGetText()
210 OUString sRet;
211 SvTreeListEntry* pEntry = m_pTreeListBox->GetEntryFromPath( m_aEntryPath );
212 if ( pEntry )
213 sRet = SvTreeListBox::SearchEntryTextWithHeadTitle( pEntry );
214 return sRet;
217 Locale AccessibleListBoxEntry::implGetLocale()
219 return Application::GetSettings().GetUILanguageTag().getLocale();
221 void AccessibleListBoxEntry::implGetSelection( sal_Int32& nStartIndex, sal_Int32& nEndIndex )
223 nStartIndex = 0;
224 nEndIndex = 0;
227 // XTypeProvider
230 Sequence< sal_Int8 > AccessibleListBoxEntry::getImplementationId()
232 return css::uno::Sequence<sal_Int8>();
236 // XComponent
238 void SAL_CALL AccessibleListBoxEntry::disposing()
240 SolarMutexGuard aSolarGuard;
241 ::osl::MutexGuard aGuard( m_aMutex );
243 Reference< XAccessible > xKeepAlive( this );
245 // Send a disposing to all listeners.
246 if ( m_nClientId )
248 ::comphelper::AccessibleEventNotifier::TClientId nId = m_nClientId;
249 m_nClientId = 0;
250 ::comphelper::AccessibleEventNotifier::revokeClientNotifyDisposing( nId, *this );
253 // clean up
254 m_wListBox.clear();
256 if ( m_pTreeListBox )
257 m_pTreeListBox->RemoveEventListener( LINK( this, AccessibleListBoxEntry, WindowEventListener ) );
258 m_pTreeListBox = nullptr;
261 // XServiceInfo
263 OUString SAL_CALL AccessibleListBoxEntry::getImplementationName()
265 return "com.sun.star.comp.svtools.AccessibleTreeListBoxEntry";
268 Sequence< OUString > SAL_CALL AccessibleListBoxEntry::getSupportedServiceNames()
270 return {"com.sun.star.accessibility.AccessibleContext",
271 "com.sun.star.accessibility.AccessibleComponent",
272 "com.sun.star.awt.AccessibleTreeListBoxEntry"};
275 sal_Bool SAL_CALL AccessibleListBoxEntry::supportsService( const OUString& _rServiceName )
277 return cppu::supportsService(this, _rServiceName);
280 // XAccessible
282 Reference< XAccessibleContext > SAL_CALL AccessibleListBoxEntry::getAccessibleContext( )
284 EnsureIsAlive();
285 return this;
288 // XAccessibleContext
290 sal_Int32 SAL_CALL AccessibleListBoxEntry::getAccessibleChildCount( )
292 SolarMutexGuard aSolarGuard;
293 ::osl::MutexGuard aGuard( m_aMutex );
295 EnsureIsAlive();
296 SvTreeListEntry* pEntry = m_pTreeListBox->GetEntryFromPath( m_aEntryPath );
297 sal_Int32 nCount = 0;
298 if ( pEntry )
299 nCount = m_pTreeListBox->GetLevelChildCount( pEntry );
301 return nCount;
304 Reference< XAccessible > SAL_CALL AccessibleListBoxEntry::getAccessibleChild( sal_Int32 i )
306 SolarMutexGuard aSolarGuard;
307 ::osl::MutexGuard aGuard( m_aMutex );
308 EnsureIsAlive();
310 SvTreeListEntry* pEntry = GetRealChild(i);
311 if ( !pEntry )
312 throw IndexOutOfBoundsException();
314 uno::Reference<XAccessible> xListBox(m_wListBox);
315 assert(xListBox.is());
317 return m_rListBox.implGetAccessible(*pEntry);
320 Reference< XAccessible > AccessibleListBoxEntry::implGetParentAccessible( ) const
322 Reference< XAccessible > xParent;
323 if ( !xParent.is() )
325 OSL_ENSURE( m_aEntryPath.size(), "AccessibleListBoxEntry::getAccessibleParent: invalid path!" );
326 if ( m_aEntryPath.size() == 1 )
327 { // we're a top level entry
328 // -> our parent is the tree listbox itself
329 if ( m_pTreeListBox )
330 xParent = m_pTreeListBox->GetAccessible( );
332 else
333 { // we have an entry as parent -> get its accessible
335 // shorten our access path by one
336 std::deque< sal_Int32 > aParentPath( m_aEntryPath );
337 aParentPath.pop_back();
339 // get the entry for this shortened access path
340 SvTreeListEntry* pParentEntry = m_pTreeListBox->GetEntryFromPath( aParentPath );
341 OSL_ENSURE( pParentEntry, "AccessibleListBoxEntry::implGetParentAccessible: could not obtain a parent entry!" );
343 if ( pParentEntry )
344 pParentEntry = m_pTreeListBox->GetParent(pParentEntry);
345 if ( pParentEntry )
347 uno::Reference<XAccessible> xListBox(m_wListBox);
348 assert(xListBox.is());
349 return m_rListBox.implGetAccessible(*pParentEntry);
350 // the AccessibleListBoxEntry class will create its parent
351 // when needed
356 return xParent;
360 Reference< XAccessible > SAL_CALL AccessibleListBoxEntry::getAccessibleParent( )
362 SolarMutexGuard aSolarGuard;
363 ::osl::MutexGuard aGuard( m_aMutex );
364 EnsureIsAlive();
366 return implGetParentAccessible( );
369 sal_Int32 SAL_CALL AccessibleListBoxEntry::getAccessibleIndexInParent( )
371 ::osl::MutexGuard aGuard( m_aMutex );
373 OSL_ENSURE( !m_aEntryPath.empty(), "empty path" );
374 return m_aEntryPath.empty() ? -1 : m_aEntryPath.back();
377 sal_Int32 AccessibleListBoxEntry::GetRoleType() const
379 sal_Int32 nCase = 0;
380 SvTreeListEntry* pEntry = m_pTreeListBox->GetEntry(0);
381 if ( pEntry )
383 if( pEntry->HasChildrenOnDemand() || m_pTreeListBox->GetChildCount(pEntry) > 0 )
385 nCase = 1;
386 return nCase;
390 bool bHasButtons = (m_pTreeListBox->GetStyle() & WB_HASBUTTONS)!=0;
391 if( !(m_pTreeListBox->GetTreeFlags() & SvTreeFlags::CHKBTN) )
393 if( bHasButtons )
394 nCase = 1;
396 else
398 if( bHasButtons )
399 nCase = 2;
400 else
401 nCase = 3;
403 return nCase;
406 sal_Int16 SAL_CALL AccessibleListBoxEntry::getAccessibleRole( )
408 SolarMutexGuard aSolarGuard;
409 ::osl::MutexGuard aGuard( m_aMutex );
411 SvTreeListBox* pBox = m_pTreeListBox;
412 if(pBox)
414 SvTreeFlags treeFlag = pBox->GetTreeFlags();
415 if(treeFlag & SvTreeFlags::CHKBTN )
417 SvTreeListEntry* pEntry = pBox->GetEntryFromPath( m_aEntryPath );
418 SvButtonState eState = pBox->GetCheckButtonState( pEntry );
419 switch( eState )
421 case SvButtonState::Checked:
422 case SvButtonState::Unchecked:
423 return AccessibleRole::CHECK_BOX;
424 case SvButtonState::Tristate:
425 default:
426 return AccessibleRole::LABEL;
429 if (GetRoleType() == 0)
430 return AccessibleRole::LIST_ITEM;
431 else
432 //o is: return AccessibleRole::LABEL;
433 return AccessibleRole::TREE_ITEM;
435 return AccessibleRole::UNKNOWN;
438 OUString SAL_CALL AccessibleListBoxEntry::getAccessibleDescription( )
440 SolarMutexGuard aSolarGuard;
441 ::osl::MutexGuard aGuard( m_aMutex );
443 if( getAccessibleRole() == AccessibleRole::TREE_ITEM )
445 return OUString();
447 return m_pTreeListBox->GetEntryAccessibleDescription(
448 m_pTreeListBox->GetEntryFromPath(m_aEntryPath));
451 OUString SAL_CALL AccessibleListBoxEntry::getAccessibleName( )
453 ::osl::MutexGuard aGuard( m_aMutex );
455 EnsureIsAlive();
457 return implGetText();
460 Reference< XAccessibleRelationSet > SAL_CALL AccessibleListBoxEntry::getAccessibleRelationSet( )
462 Reference< XAccessibleRelationSet > xRelSet;
463 Reference< XAccessible > xParent;
464 if ( m_aEntryPath.size() > 1 ) // not a root entry
465 xParent = implGetParentAccessible();
466 if ( xParent.is() )
468 rtl::Reference<utl::AccessibleRelationSetHelper> pRelationSetHelper = new utl::AccessibleRelationSetHelper;
469 Sequence< Reference< XInterface > > aSequence { xParent };
470 pRelationSetHelper->AddRelation(
471 AccessibleRelation( AccessibleRelationType::NODE_CHILD_OF, aSequence ) );
472 xRelSet = pRelationSetHelper;
474 return xRelSet;
477 Reference< XAccessibleStateSet > SAL_CALL AccessibleListBoxEntry::getAccessibleStateSet( )
479 ::osl::MutexGuard aGuard( m_aMutex );
481 rtl::Reference<utl::AccessibleStateSetHelper> pStateSetHelper = new utl::AccessibleStateSetHelper;
483 if ( IsAlive_Impl() )
485 switch(getAccessibleRole())
487 case AccessibleRole::LABEL:
488 pStateSetHelper->AddState( AccessibleStateType::TRANSIENT );
489 pStateSetHelper->AddState( AccessibleStateType::SELECTABLE );
490 pStateSetHelper->AddState( AccessibleStateType::ENABLED );
491 if (m_pTreeListBox->IsInplaceEditingEnabled())
492 pStateSetHelper->AddState( AccessibleStateType::EDITABLE );
493 if (IsShowing_Impl())
494 pStateSetHelper->AddState( AccessibleStateType::SHOWING );
495 break;
496 case AccessibleRole::CHECK_BOX:
497 pStateSetHelper->AddState( AccessibleStateType::TRANSIENT );
498 pStateSetHelper->AddState( AccessibleStateType::SELECTABLE );
499 pStateSetHelper->AddState( AccessibleStateType::ENABLED );
500 if (IsShowing_Impl())
501 pStateSetHelper->AddState( AccessibleStateType::SHOWING );
502 break;
504 SvTreeListEntry *pEntry = m_pTreeListBox->GetEntryFromPath(m_aEntryPath);
505 if (pEntry)
506 m_pTreeListBox->FillAccessibleEntryStateSet(pEntry, *pStateSetHelper);
508 else
509 pStateSetHelper->AddState( AccessibleStateType::DEFUNC );
511 return pStateSetHelper;
514 Locale SAL_CALL AccessibleListBoxEntry::getLocale( )
516 SolarMutexGuard aSolarGuard;
517 ::osl::MutexGuard aGuard( m_aMutex );
519 return implGetLocale();
522 // XAccessibleComponent
524 sal_Bool SAL_CALL AccessibleListBoxEntry::containsPoint( const awt::Point& rPoint )
526 return tools::Rectangle( Point(), GetBoundingBox().GetSize() ).Contains( VCLPoint( rPoint ) );
529 Reference< XAccessible > SAL_CALL AccessibleListBoxEntry::getAccessibleAtPoint( const awt::Point& _aPoint )
531 SolarMutexGuard aSolarGuard;
532 ::osl::MutexGuard aGuard( m_aMutex );
534 EnsureIsAlive();
535 SvTreeListEntry* pEntry = m_pTreeListBox->GetEntry( VCLPoint( _aPoint ) );
536 if ( !pEntry )
537 throw RuntimeException();
539 Reference< XAccessible > xAcc;
540 uno::Reference<XAccessible> xListBox(m_wListBox);
541 assert(xListBox.is());
542 auto pAccEntry = m_rListBox.implGetAccessible(*pEntry);
543 tools::Rectangle aRect = pAccEntry->GetBoundingBox_Impl();
544 if ( aRect.Contains( VCLPoint( _aPoint ) ) )
545 xAcc = pAccEntry.get();
546 return xAcc;
549 awt::Rectangle SAL_CALL AccessibleListBoxEntry::getBounds( )
551 return AWTRectangle( GetBoundingBox() );
554 awt::Point SAL_CALL AccessibleListBoxEntry::getLocation( )
556 return AWTPoint( GetBoundingBox().TopLeft() );
559 awt::Point SAL_CALL AccessibleListBoxEntry::getLocationOnScreen( )
561 return AWTPoint( GetBoundingBoxOnScreen().TopLeft() );
564 awt::Size SAL_CALL AccessibleListBoxEntry::getSize( )
566 return AWTSize( GetBoundingBox().GetSize() );
569 void SAL_CALL AccessibleListBoxEntry::grabFocus( )
571 // do nothing, because no focus for each item
574 sal_Int32 AccessibleListBoxEntry::getForeground( )
576 SolarMutexGuard aSolarGuard;
577 ::osl::MutexGuard aGuard( m_aMutex );
579 sal_Int32 nColor = 0;
580 Reference< XAccessible > xParent = getAccessibleParent();
581 if ( xParent.is() )
583 Reference< XAccessibleComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
584 if ( xParentComp.is() )
585 nColor = xParentComp->getForeground();
588 return nColor;
591 sal_Int32 AccessibleListBoxEntry::getBackground( )
593 SolarMutexGuard aSolarGuard;
594 ::osl::MutexGuard aGuard( m_aMutex );
596 sal_Int32 nColor = 0;
597 Reference< XAccessible > xParent = getAccessibleParent();
598 if ( xParent.is() )
600 Reference< XAccessibleComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
601 if ( xParentComp.is() )
602 nColor = xParentComp->getBackground();
605 return nColor;
608 // XAccessibleText
611 awt::Rectangle SAL_CALL AccessibleListBoxEntry::getCharacterBounds( sal_Int32 nIndex )
613 SolarMutexGuard aSolarGuard;
614 ::osl::MutexGuard aGuard( m_aMutex );
616 EnsureIsAlive();
618 if ( !implIsValidIndex( nIndex, implGetText().getLength() ) )
619 throw IndexOutOfBoundsException();
621 awt::Rectangle aBounds( 0, 0, 0, 0 );
622 SvTreeListEntry* pEntry = m_pTreeListBox->GetEntryFromPath( m_aEntryPath );
623 if ( pEntry )
625 vcl::ControlLayoutData aLayoutData;
626 tools::Rectangle aItemRect = GetBoundingBox();
627 m_pTreeListBox->RecordLayoutData( &aLayoutData, aItemRect );
628 tools::Rectangle aCharRect = aLayoutData.GetCharacterBounds( nIndex );
629 aCharRect.Move( -aItemRect.Left(), -aItemRect.Top() );
630 aBounds = AWTRectangle( aCharRect );
633 return aBounds;
636 sal_Int32 SAL_CALL AccessibleListBoxEntry::getIndexAtPoint( const awt::Point& aPoint )
638 SolarMutexGuard aSolarGuard;
639 ::osl::MutexGuard aGuard( m_aMutex );
640 EnsureIsAlive();
641 if(aPoint.X==0 && aPoint.Y==0) return 0;
643 sal_Int32 nIndex = -1;
644 SvTreeListEntry* pEntry = m_pTreeListBox->GetEntryFromPath( m_aEntryPath );
645 if ( pEntry )
647 vcl::ControlLayoutData aLayoutData;
648 tools::Rectangle aItemRect = GetBoundingBox();
649 m_pTreeListBox->RecordLayoutData( &aLayoutData, aItemRect );
650 Point aPnt( VCLPoint( aPoint ) );
651 aPnt += aItemRect.TopLeft();
652 nIndex = aLayoutData.GetIndexForPoint( aPnt );
655 return nIndex;
658 sal_Bool SAL_CALL AccessibleListBoxEntry::copyText( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
660 SolarMutexGuard aSolarGuard;
661 ::osl::MutexGuard aGuard( m_aMutex );
662 EnsureIsAlive();
664 OUString sText = implGetText();
665 if ( ( 0 > nStartIndex ) || ( sText.getLength() <= nStartIndex )
666 || ( 0 > nEndIndex ) || ( sText.getLength() <= nEndIndex ) )
667 throw IndexOutOfBoundsException();
669 sal_Int32 nLen = nEndIndex - nStartIndex + 1;
670 ::svt::OStringTransfer::CopyString( sText.copy( nStartIndex, nLen ), m_pTreeListBox );
672 return true;
675 sal_Bool SAL_CALL AccessibleListBoxEntry::scrollSubstringTo( sal_Int32, sal_Int32, AccessibleScrollType )
677 return false;
680 // XAccessibleEventBroadcaster
682 void SAL_CALL AccessibleListBoxEntry::addAccessibleEventListener( const Reference< XAccessibleEventListener >& xListener )
684 if (xListener.is())
686 ::osl::MutexGuard aGuard( m_aMutex );
687 if (!m_nClientId)
688 m_nClientId = comphelper::AccessibleEventNotifier::registerClient( );
689 comphelper::AccessibleEventNotifier::addEventListener( m_nClientId, xListener );
693 void SAL_CALL AccessibleListBoxEntry::removeAccessibleEventListener( const Reference< XAccessibleEventListener >& xListener )
695 if (!xListener.is())
696 return;
698 ::osl::MutexGuard aGuard( m_aMutex );
700 sal_Int32 nListenerCount = comphelper::AccessibleEventNotifier::removeEventListener( m_nClientId, xListener );
701 if ( !nListenerCount )
703 // no listeners anymore
704 // -> revoke ourself. This may lead to the notifier thread dying (if we were the last client),
705 // and at least to us not firing any events anymore, in case somebody calls
706 // NotifyAccessibleEvent, again
707 sal_Int32 nId = m_nClientId;
708 m_nClientId = 0;
709 comphelper::AccessibleEventNotifier::revokeClient( nId );
714 // XAccessibleAction
716 sal_Int32 SAL_CALL AccessibleListBoxEntry::getAccessibleActionCount( )
718 ::osl::MutexGuard aGuard( m_aMutex );
720 // three actions supported
721 SvTreeFlags treeFlag = m_pTreeListBox->GetTreeFlags();
722 bool bHasButtons = (m_pTreeListBox->GetStyle() & WB_HASBUTTONS)!=0;
723 if( (treeFlag & SvTreeFlags::CHKBTN) && !bHasButtons)
725 sal_Int16 role = getAccessibleRole();
726 if ( role == AccessibleRole::CHECK_BOX )
727 return 2;
728 else if ( role == AccessibleRole::LABEL )
729 return 0;
731 else
732 return ACCESSIBLE_ACTION_COUNT;
733 return 0;
736 sal_Bool SAL_CALL AccessibleListBoxEntry::doAccessibleAction( sal_Int32 nIndex )
738 SolarMutexGuard aSolarGuard;
739 ::osl::MutexGuard aGuard( m_aMutex );
741 bool bRet = false;
742 checkActionIndex_Impl( nIndex );
743 EnsureIsAlive();
745 SvTreeFlags treeFlag = m_pTreeListBox->GetTreeFlags();
746 if( nIndex == 0 && (treeFlag & SvTreeFlags::CHKBTN) )
748 if(getAccessibleRole() == AccessibleRole::CHECK_BOX)
750 SvTreeListEntry* pEntry = m_pTreeListBox->GetEntryFromPath( m_aEntryPath );
751 SvButtonState state = m_pTreeListBox->GetCheckButtonState( pEntry );
752 if ( state == SvButtonState::Checked )
753 m_pTreeListBox->SetCheckButtonState(pEntry, SvButtonState::Unchecked);
754 else if (state == SvButtonState::Unchecked)
755 m_pTreeListBox->SetCheckButtonState(pEntry, SvButtonState::Checked);
758 else if( (nIndex == 1 && (treeFlag & SvTreeFlags::CHKBTN) ) || (nIndex == 0) )
760 SvTreeListEntry* pEntry = m_pTreeListBox->GetEntryFromPath( m_aEntryPath );
761 if ( pEntry )
763 if ( m_pTreeListBox->IsExpanded( pEntry ) )
764 m_pTreeListBox->Collapse( pEntry );
765 else
766 m_pTreeListBox->Expand( pEntry );
767 bRet = true;
771 return bRet;
774 OUString SAL_CALL AccessibleListBoxEntry::getAccessibleActionDescription( sal_Int32 nIndex )
776 SolarMutexGuard aSolarGuard;
777 ::osl::MutexGuard aGuard( m_aMutex );
779 checkActionIndex_Impl( nIndex );
780 EnsureIsAlive();
782 SvTreeListEntry* pEntry = m_pTreeListBox->GetEntryFromPath( m_aEntryPath );
783 SvButtonState state = m_pTreeListBox->GetCheckButtonState( pEntry );
784 SvTreeFlags treeFlag = m_pTreeListBox->GetTreeFlags();
785 if(nIndex == 0 && (treeFlag & SvTreeFlags::CHKBTN))
787 if(getAccessibleRole() == AccessibleRole::CHECK_BOX)
789 if ( state == SvButtonState::Checked )
790 return "UnCheck";
791 else if (state == SvButtonState::Unchecked)
792 return "Check";
794 else
796 //Sometimes, a List or Tree may have both checkbox and label at the same time
797 return OUString();
799 }else if( (nIndex == 1 && (treeFlag & SvTreeFlags::CHKBTN)) || nIndex == 0 )
801 if( pEntry->HasChildren() || pEntry->HasChildrenOnDemand() )
802 return m_pTreeListBox->IsExpanded( pEntry ) ?
803 AccResId(STR_SVT_ACC_ACTION_COLLAPSE) :
804 AccResId(STR_SVT_ACC_ACTION_EXPAND);
805 return OUString();
808 throw IndexOutOfBoundsException();
811 Reference< XAccessibleKeyBinding > AccessibleListBoxEntry::getAccessibleActionKeyBinding( sal_Int32 nIndex )
813 ::osl::MutexGuard aGuard( m_aMutex );
815 Reference< XAccessibleKeyBinding > xRet;
816 checkActionIndex_Impl( nIndex );
817 // ... which key?
818 return xRet;
821 // XAccessibleSelection
823 void SAL_CALL AccessibleListBoxEntry::selectAccessibleChild( sal_Int32 nChildIndex )
825 SolarMutexGuard aSolarGuard;
826 ::osl::MutexGuard aGuard( m_aMutex );
828 EnsureIsAlive();
830 SvTreeListEntry* pEntry = GetRealChild(nChildIndex);
831 if ( !pEntry )
832 throw IndexOutOfBoundsException();
834 m_pTreeListBox->Select( pEntry );
837 sal_Bool SAL_CALL AccessibleListBoxEntry::isAccessibleChildSelected( sal_Int32 nChildIndex )
839 SolarMutexGuard aSolarGuard;
840 ::osl::MutexGuard aGuard( m_aMutex );
842 EnsureIsAlive();
844 SvTreeListEntry* pParent = m_pTreeListBox->GetEntryFromPath( m_aEntryPath );
845 SvTreeListEntry* pEntry = m_pTreeListBox->GetEntry( pParent, nChildIndex );
846 if ( !pEntry )
847 throw IndexOutOfBoundsException();
849 return m_pTreeListBox->IsSelected( pEntry );
852 void SAL_CALL AccessibleListBoxEntry::clearAccessibleSelection( )
854 SolarMutexGuard aSolarGuard;
855 ::osl::MutexGuard aGuard( m_aMutex );
857 EnsureIsAlive();
859 SvTreeListEntry* pParent = m_pTreeListBox->GetEntryFromPath( m_aEntryPath );
860 if ( !pParent )
861 throw RuntimeException();
862 sal_Int32 nCount = m_pTreeListBox->GetLevelChildCount( pParent );
863 for ( sal_Int32 i = 0; i < nCount; ++i )
865 SvTreeListEntry* pEntry = m_pTreeListBox->GetEntry( pParent, i );
866 if ( m_pTreeListBox->IsSelected( pEntry ) )
867 m_pTreeListBox->Select( pEntry, false );
871 void SAL_CALL AccessibleListBoxEntry::selectAllAccessibleChildren( )
873 SolarMutexGuard aSolarGuard;
874 ::osl::MutexGuard aGuard( m_aMutex );
876 EnsureIsAlive();
878 SvTreeListEntry* pParent = m_pTreeListBox->GetEntryFromPath( m_aEntryPath );
879 if ( !pParent )
880 throw RuntimeException();
881 sal_Int32 nCount = m_pTreeListBox->GetLevelChildCount( pParent );
882 for ( sal_Int32 i = 0; i < nCount; ++i )
884 SvTreeListEntry* pEntry = m_pTreeListBox->GetEntry( pParent, i );
885 if ( !m_pTreeListBox->IsSelected( pEntry ) )
886 m_pTreeListBox->Select( pEntry );
890 sal_Int32 SAL_CALL AccessibleListBoxEntry::getSelectedAccessibleChildCount( )
892 SolarMutexGuard aSolarGuard;
893 ::osl::MutexGuard aGuard( m_aMutex );
895 EnsureIsAlive();
897 sal_Int32 i, nSelCount = 0, nCount = 0;
899 SvTreeListEntry* pParent = m_pTreeListBox->GetEntryFromPath( m_aEntryPath );
900 if ( !pParent )
901 throw RuntimeException();
902 nCount = m_pTreeListBox->GetLevelChildCount( pParent );
903 for ( i = 0; i < nCount; ++i )
905 SvTreeListEntry* pEntry = m_pTreeListBox->GetEntry( pParent, i );
906 if ( m_pTreeListBox->IsSelected( pEntry ) )
907 ++nSelCount;
910 return nSelCount;
913 Reference< XAccessible > SAL_CALL AccessibleListBoxEntry::getSelectedAccessibleChild( sal_Int32 nSelectedChildIndex )
915 SolarMutexGuard aSolarGuard;
916 ::osl::MutexGuard aGuard( m_aMutex );
918 EnsureIsAlive();
920 if ( nSelectedChildIndex < 0 || nSelectedChildIndex >= getSelectedAccessibleChildCount() )
921 throw IndexOutOfBoundsException();
923 Reference< XAccessible > xChild;
924 sal_Int32 i, nSelCount = 0, nCount = 0;
926 SvTreeListEntry* pParent = m_pTreeListBox->GetEntryFromPath( m_aEntryPath );
927 if ( !pParent )
928 throw RuntimeException();
929 nCount = m_pTreeListBox->GetLevelChildCount( pParent );
930 for ( i = 0; i < nCount; ++i )
932 SvTreeListEntry* pEntry = m_pTreeListBox->GetEntry( pParent, i );
933 if ( m_pTreeListBox->IsSelected( pEntry ) )
934 ++nSelCount;
936 if ( nSelCount == ( nSelectedChildIndex + 1 ) )
938 uno::Reference<XAccessible> xListBox(m_wListBox);
939 assert(xListBox.is());
940 xChild = m_rListBox.implGetAccessible(*pEntry).get();
941 break;
945 return xChild;
948 void SAL_CALL AccessibleListBoxEntry::deselectAccessibleChild( sal_Int32 nSelectedChildIndex )
950 SolarMutexGuard aSolarGuard;
951 ::osl::MutexGuard aGuard( m_aMutex );
953 EnsureIsAlive();
955 SvTreeListEntry* pParent = m_pTreeListBox->GetEntryFromPath( m_aEntryPath );
956 SvTreeListEntry* pEntry = m_pTreeListBox->GetEntry( pParent, nSelectedChildIndex );
957 if ( !pEntry )
958 throw IndexOutOfBoundsException();
960 m_pTreeListBox->Select( pEntry, false );
962 sal_Int32 SAL_CALL AccessibleListBoxEntry::getCaretPosition( )
964 return -1;
966 sal_Bool SAL_CALL AccessibleListBoxEntry::setCaretPosition ( sal_Int32 nIndex )
968 SolarMutexGuard aSolarGuard;
969 ::osl::MutexGuard aGuard( m_aMutex );
970 EnsureIsAlive();
972 if ( !implIsValidRange( nIndex, nIndex, implGetText().getLength() ) )
973 throw IndexOutOfBoundsException();
975 return false;
977 sal_Unicode SAL_CALL AccessibleListBoxEntry::getCharacter( sal_Int32 nIndex )
979 SolarMutexGuard aSolarGuard;
980 ::osl::MutexGuard aGuard( m_aMutex );
981 EnsureIsAlive();
982 return OCommonAccessibleText::implGetCharacter( implGetText(), nIndex );
984 css::uno::Sequence< css::beans::PropertyValue > SAL_CALL AccessibleListBoxEntry::getCharacterAttributes( sal_Int32 nIndex, const css::uno::Sequence< OUString >& )
986 SolarMutexGuard aSolarGuard;
987 ::osl::MutexGuard aGuard( m_aMutex );
988 EnsureIsAlive();
990 OUString sText( implGetText() );
992 if ( !implIsValidIndex( nIndex, sText.getLength() ) )
993 throw IndexOutOfBoundsException();
995 return css::uno::Sequence< css::beans::PropertyValue >();
997 sal_Int32 SAL_CALL AccessibleListBoxEntry::getCharacterCount( )
999 SolarMutexGuard aSolarGuard;
1000 ::osl::MutexGuard aGuard( m_aMutex );
1001 EnsureIsAlive();
1002 return implGetText().getLength();
1005 OUString SAL_CALL AccessibleListBoxEntry::getSelectedText( )
1007 SolarMutexGuard aSolarGuard;
1008 ::osl::MutexGuard aGuard( m_aMutex );
1009 EnsureIsAlive();
1010 return OUString();
1012 sal_Int32 SAL_CALL AccessibleListBoxEntry::getSelectionStart( )
1014 SolarMutexGuard aSolarGuard;
1015 ::osl::MutexGuard aGuard( m_aMutex );
1016 EnsureIsAlive();
1017 return 0;
1019 sal_Int32 SAL_CALL AccessibleListBoxEntry::getSelectionEnd( )
1021 SolarMutexGuard aSolarGuard;
1022 ::osl::MutexGuard aGuard( m_aMutex );
1023 EnsureIsAlive();
1024 return 0;
1026 sal_Bool SAL_CALL AccessibleListBoxEntry::setSelection( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
1028 SolarMutexGuard aSolarGuard;
1029 ::osl::MutexGuard aGuard( m_aMutex );
1030 EnsureIsAlive();
1032 if ( !implIsValidRange( nStartIndex, nEndIndex, implGetText().getLength() ) )
1033 throw IndexOutOfBoundsException();
1035 return false;
1037 OUString SAL_CALL AccessibleListBoxEntry::getText( )
1039 SolarMutexGuard aSolarGuard;
1040 ::osl::MutexGuard aGuard( m_aMutex );
1041 EnsureIsAlive();
1042 return implGetText( );
1044 OUString SAL_CALL AccessibleListBoxEntry::getTextRange( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
1046 SolarMutexGuard aSolarGuard;
1047 ::osl::MutexGuard aGuard( m_aMutex );
1048 EnsureIsAlive();
1049 return OCommonAccessibleText::implGetTextRange( implGetText(), nStartIndex, nEndIndex );
1051 css::accessibility::TextSegment SAL_CALL AccessibleListBoxEntry::getTextAtIndex( sal_Int32 nIndex, sal_Int16 aTextType )
1053 SolarMutexGuard aSolarGuard;
1054 ::osl::MutexGuard aGuard( m_aMutex );
1055 EnsureIsAlive();
1056 return OCommonAccessibleText::getTextAtIndex( nIndex ,aTextType);
1058 css::accessibility::TextSegment SAL_CALL AccessibleListBoxEntry::getTextBeforeIndex( sal_Int32 nIndex, sal_Int16 aTextType )
1060 SolarMutexGuard aSolarGuard;
1061 ::osl::MutexGuard aGuard( m_aMutex );
1062 EnsureIsAlive();
1063 return OCommonAccessibleText::getTextBeforeIndex( nIndex ,aTextType);
1065 css::accessibility::TextSegment SAL_CALL AccessibleListBoxEntry::getTextBehindIndex( sal_Int32 nIndex, sal_Int16 aTextType )
1067 SolarMutexGuard aSolarGuard;
1068 ::osl::MutexGuard aGuard( m_aMutex );
1069 EnsureIsAlive();
1071 return OCommonAccessibleText::getTextBehindIndex( nIndex ,aTextType);
1074 // XAccessibleValue
1077 Any AccessibleListBoxEntry::getCurrentValue( )
1079 ::osl::MutexGuard aGuard( m_aMutex );
1080 Any aValue;
1081 sal_Int32 level = static_cast<sal_Int32>(m_aEntryPath.size()) - 1;
1082 level = level < 0 ? 0: level;
1083 aValue <<= level;
1084 return aValue;
1088 sal_Bool AccessibleListBoxEntry::setCurrentValue( const Any& aNumber )
1090 ::osl::MutexGuard aGuard( m_aMutex );
1093 bool bReturn = false;
1094 SvTreeListBox* pBox = m_pTreeListBox;
1095 if(getAccessibleRole() == AccessibleRole::CHECK_BOX)
1097 SvTreeListEntry* pEntry = pBox->GetEntryFromPath( m_aEntryPath );
1098 if ( pEntry )
1100 sal_Int32 nValue(0), nValueMin(0), nValueMax(0);
1101 aNumber >>= nValue;
1102 getMinimumValue() >>= nValueMin;
1103 getMaximumValue() >>= nValueMax;
1105 if ( nValue < nValueMin )
1106 nValue = nValueMin;
1107 else if ( nValue > nValueMax )
1108 nValue = nValueMax;
1110 pBox->SetCheckButtonState(pEntry, static_cast<SvButtonState>(nValue) );
1111 bReturn = true;
1115 return bReturn;
1119 Any AccessibleListBoxEntry::getMaximumValue( )
1121 ::osl::MutexGuard aGuard( m_aMutex );
1123 Any aValue;
1124 // SvTreeListBox* pBox = m_pTreeListBox;
1125 switch(getAccessibleRole())
1127 case AccessibleRole::CHECK_BOX:
1128 aValue <<= sal_Int32(1);
1129 break;
1130 case AccessibleRole::LABEL:
1131 default:
1132 break;
1135 return aValue;
1139 Any AccessibleListBoxEntry::getMinimumValue( )
1141 ::osl::MutexGuard aGuard( m_aMutex );
1143 Any aValue;
1144 // SvTreeListBox* pBox = m_pTreeListBox;
1145 switch(getAccessibleRole())
1147 case AccessibleRole::CHECK_BOX:
1148 aValue <<= sal_Int32(0);
1149 break;
1150 case AccessibleRole::LABEL:
1151 default:
1152 break;
1155 return aValue;
1158 Any AccessibleListBoxEntry::getMinimumIncrement( )
1160 ::osl::MutexGuard aGuard( m_aMutex );
1162 Any aValue;
1163 switch(getAccessibleRole())
1165 case AccessibleRole::CHECK_BOX:
1166 aValue <<= sal_Int32(1);
1167 break;
1168 case AccessibleRole::LABEL:
1169 default:
1170 break;
1173 return aValue;
1176 SvTreeListEntry* AccessibleListBoxEntry::GetRealChild(sal_Int32 nIndex)
1178 SvTreeListEntry* pEntry = nullptr;
1179 SvTreeListEntry* pParent = m_pTreeListBox->GetEntryFromPath( m_aEntryPath );
1180 if (pParent)
1182 pEntry = m_pTreeListBox->GetEntry( pParent, nIndex );
1183 if ( !pEntry && getAccessibleChildCount() > 0 )
1185 m_pTreeListBox->RequestingChildren(pParent);
1186 pEntry = m_pTreeListBox->GetEntry( pParent, nIndex );
1189 return pEntry;
1192 }// namespace accessibility
1195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */