lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / accessibility / source / extended / accessibleiconchoicectrl.cxx
blobd3abb12cac1e830b550064bbb9e2833bbae3a43e
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/accessibleiconchoicectrl.hxx>
21 #include <extended/accessibleiconchoicectrlentry.hxx>
22 #include <svtools/ivctrl.hxx>
23 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
24 #include <com/sun/star/accessibility/AccessibleRole.hpp>
25 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
26 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
27 #include <unotools/accessiblestatesethelper.hxx>
28 #include <vcl/svapp.hxx>
29 #include <cppuhelper/supportsservice.hxx>
30 #include <cppuhelper/typeprovider.hxx>
33 namespace accessibility
37 // class AccessibleIconChoiceCtrl ----------------------------------------------
39 using namespace ::com::sun::star::accessibility;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::lang;
42 using namespace ::com::sun::star;
45 // Ctor() and Dtor()
47 AccessibleIconChoiceCtrl::AccessibleIconChoiceCtrl( SvtIconChoiceCtrl const & _rIconCtrl, const Reference< XAccessible >& _xParent ) :
48 VCLXAccessibleComponent( _rIconCtrl.GetWindowPeer() ),
49 m_xParent ( _xParent )
53 IMPLEMENT_FORWARD_XINTERFACE2(AccessibleIconChoiceCtrl, VCLXAccessibleComponent, AccessibleIconChoiceCtrl_BASE)
54 IMPLEMENT_FORWARD_XTYPEPROVIDER2(AccessibleIconChoiceCtrl, VCLXAccessibleComponent, AccessibleIconChoiceCtrl_BASE)
56 void AccessibleIconChoiceCtrl::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
58 if ( isAlive() )
60 switch ( rVclWindowEvent.GetId() )
62 case VclEventId::ListboxSelect :
64 // First send an event that tells the listeners of a
65 // modified selection. The active descendant event is
66 // send after that so that the receiving AT has time to
67 // read the text or name of the active child.
68 // NotifyAccessibleEvent( AccessibleEventId::SELECTION_CHANGED, Any(), Any() );
70 if ( getCtrl() && getCtrl()->HasFocus() )
72 SvxIconChoiceCtrlEntry* pEntry = static_cast< SvxIconChoiceCtrlEntry* >( rVclWindowEvent.GetData() );
73 if ( pEntry )
75 sal_uLong nPos = getCtrl()->GetEntryListPos( pEntry );
76 Reference< XAccessible > xChild = new AccessibleIconChoiceCtrlEntry( *getCtrl(), nPos, this );
77 uno::Any aOldValue, aNewValue;
78 aNewValue <<= xChild;
79 NotifyAccessibleEvent( AccessibleEventId::ACTIVE_DESCENDANT_CHANGED, aOldValue, aNewValue );
81 NotifyAccessibleEvent( AccessibleEventId::SELECTION_CHANGED, aOldValue, aNewValue );
85 break;
87 case VclEventId::WindowGetFocus :
89 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
90 if ( pCtrl && pCtrl->HasFocus() )
92 SvxIconChoiceCtrlEntry* pEntry = static_cast< SvxIconChoiceCtrlEntry* >( rVclWindowEvent.GetData() );
93 if ( pEntry == nullptr )
95 pEntry = getCtrl()->GetSelectedEntry();
97 if ( pEntry )
99 sal_uLong nPos = pCtrl->GetEntryListPos( pEntry );
100 Reference< XAccessible > xChild = new AccessibleIconChoiceCtrlEntry( *pCtrl, nPos, this );
101 uno::Any aOldValue, aNewValue;
102 aNewValue <<= xChild;
103 NotifyAccessibleEvent( AccessibleEventId::ACTIVE_DESCENDANT_CHANGED, aOldValue, aNewValue );
104 NotifyAccessibleEvent( AccessibleEventId::SELECTION_CHANGED, aOldValue, aNewValue );
107 break;
109 default:
110 VCLXAccessibleComponent::ProcessWindowChildEvent (rVclWindowEvent);
115 // XComponent
117 void SAL_CALL AccessibleIconChoiceCtrl::disposing()
119 ::osl::MutexGuard aGuard( m_aMutex );
121 m_xParent = nullptr;
124 // XServiceInfo
126 OUString SAL_CALL AccessibleIconChoiceCtrl::getImplementationName()
128 return OUString( "com.sun.star.comp.svtools.AccessibleIconChoiceControl" );
131 Sequence< OUString > SAL_CALL AccessibleIconChoiceCtrl::getSupportedServiceNames()
133 return {"com.sun.star.accessibility.AccessibleContext",
134 "com.sun.star.accessibility.AccessibleComponent",
135 "com.sun.star.awt.AccessibleIconChoiceControl"};
138 sal_Bool SAL_CALL AccessibleIconChoiceCtrl::supportsService( const OUString& _rServiceName )
140 return cppu::supportsService(this, _rServiceName);
143 // XAccessible
145 Reference< XAccessibleContext > SAL_CALL AccessibleIconChoiceCtrl::getAccessibleContext( )
147 ensureAlive();
148 return this;
151 // XAccessibleContext
153 sal_Int32 SAL_CALL AccessibleIconChoiceCtrl::getAccessibleChildCount( )
155 ::comphelper::OExternalLockGuard aGuard( this );
157 return getCtrl()->GetEntryCount();
160 Reference< XAccessible > SAL_CALL AccessibleIconChoiceCtrl::getAccessibleChild( sal_Int32 i )
162 ::comphelper::OExternalLockGuard aGuard( this );
164 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
165 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry(i);
166 if ( !pEntry )
167 throw RuntimeException();
169 return new AccessibleIconChoiceCtrlEntry( *pCtrl, i, this );
172 Reference< XAccessible > SAL_CALL AccessibleIconChoiceCtrl::getAccessibleParent( )
174 ::osl::MutexGuard aGuard( m_aMutex );
176 ensureAlive();
177 return m_xParent;
180 sal_Int16 SAL_CALL AccessibleIconChoiceCtrl::getAccessibleRole( )
182 //return AccessibleRole::TREE;
183 return AccessibleRole::LIST;
186 OUString SAL_CALL AccessibleIconChoiceCtrl::getAccessibleDescription( )
188 ::comphelper::OExternalLockGuard aGuard( this );
190 return getCtrl()->GetAccessibleDescription();
193 OUString SAL_CALL AccessibleIconChoiceCtrl::getAccessibleName( )
195 ::comphelper::OExternalLockGuard aGuard( this );
197 OUString sName = getCtrl()->GetAccessibleName();
198 if ( sName.isEmpty() )
199 sName = "IconChoiceControl";
200 return sName;
203 // XAccessibleSelection
205 void SAL_CALL AccessibleIconChoiceCtrl::selectAccessibleChild( sal_Int32 nChildIndex )
207 ::comphelper::OExternalLockGuard aGuard( this );
209 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
210 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( nChildIndex );
211 if ( !pEntry )
212 throw IndexOutOfBoundsException();
214 pCtrl->SetCursor( pEntry );
217 sal_Bool SAL_CALL AccessibleIconChoiceCtrl::isAccessibleChildSelected( sal_Int32 nChildIndex )
219 ::comphelper::OExternalLockGuard aGuard( this );
221 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
222 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( nChildIndex );
223 if ( !pEntry )
224 throw IndexOutOfBoundsException();
226 return ( pCtrl->GetCursor() == pEntry );
229 void SAL_CALL AccessibleIconChoiceCtrl::clearAccessibleSelection( )
231 ::comphelper::OExternalLockGuard aGuard( this );
232 getCtrl()->SetNoSelection();
235 void SAL_CALL AccessibleIconChoiceCtrl::selectAllAccessibleChildren( )
237 ::comphelper::OExternalLockGuard aGuard( this );
239 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
240 sal_Int32 nCount = pCtrl->GetEntryCount();
241 for ( sal_Int32 i = 0; i < nCount; ++i )
243 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( i );
244 if ( pCtrl->GetCursor() != pEntry )
245 pCtrl->SetCursor( pEntry );
249 sal_Int32 SAL_CALL AccessibleIconChoiceCtrl::getSelectedAccessibleChildCount( )
251 ::comphelper::OExternalLockGuard aGuard( this );
253 sal_Int32 nSelCount = 0;
254 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
255 sal_Int32 nCount = pCtrl->GetEntryCount();
256 for ( sal_Int32 i = 0; i < nCount; ++i )
258 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( i );
259 if ( pCtrl->GetCursor() == pEntry )
260 ++nSelCount;
263 return nSelCount;
266 Reference< XAccessible > SAL_CALL AccessibleIconChoiceCtrl::getSelectedAccessibleChild( sal_Int32 nSelectedChildIndex )
268 ::comphelper::OExternalLockGuard aGuard( this );
270 if ( nSelectedChildIndex < 0 || nSelectedChildIndex >= getSelectedAccessibleChildCount() )
271 throw IndexOutOfBoundsException();
273 Reference< XAccessible > xChild;
274 sal_Int32 nSelCount = 0;
275 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
276 sal_Int32 nCount = pCtrl->GetEntryCount();
277 for ( sal_Int32 i = 0; i < nCount; ++i )
279 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( i );
280 if ( pCtrl->GetCursor() == pEntry )
281 ++nSelCount;
283 if ( nSelCount == ( nSelectedChildIndex + 1 ) )
285 xChild = new AccessibleIconChoiceCtrlEntry( *pCtrl, i, this );
286 break;
290 return xChild;
293 void SAL_CALL AccessibleIconChoiceCtrl::deselectAccessibleChild( sal_Int32 nSelectedChildIndex )
295 ::comphelper::OExternalLockGuard aGuard( this );
297 if ( nSelectedChildIndex < 0 || nSelectedChildIndex >= getAccessibleChildCount() )
298 throw IndexOutOfBoundsException();
300 sal_Int32 nSelCount = 0;
301 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
302 sal_Int32 nCount = pCtrl->GetEntryCount();
303 bool bFound = false;
304 for ( sal_Int32 i = 0; i < nCount; ++i )
306 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( i );
307 if ( pEntry->IsSelected() )
309 ++nSelCount;
310 if ( i == nSelectedChildIndex )
311 bFound = true;
315 // if only one entry is selected and its index is chosen to deselect -> no selection anymore
316 if ( nSelCount == 1 && bFound )
317 pCtrl->SetNoSelection();
320 void AccessibleIconChoiceCtrl::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
322 VCLXAccessibleComponent::FillAccessibleStateSet( rStateSet );
323 if ( isAlive() )
325 rStateSet.AddState( AccessibleStateType::FOCUSABLE );
326 rStateSet.AddState( AccessibleStateType::MANAGES_DESCENDANTS );
327 rStateSet.AddState( AccessibleStateType::SELECTABLE );
331 VclPtr< SvtIconChoiceCtrl > AccessibleIconChoiceCtrl::getCtrl()
333 return GetAs<SvtIconChoiceCtrl >();
336 }// namespace accessibility
339 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */