Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / accessibility / source / extended / accessibleiconchoicectrl.cxx
blobbf299903472214783e19811321137b1c74e3307b
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 <com/sun/star/accessibility/AccessibleEventId.hpp>
23 #include <com/sun/star/accessibility/AccessibleRole.hpp>
24 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
25 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
26 #include <unotools/accessiblestatesethelper.hxx>
27 #include <vcl/ivctrl.hxx>
28 #include <cppuhelper/supportsservice.hxx>
31 namespace accessibility
35 // class AccessibleIconChoiceCtrl ----------------------------------------------
37 using namespace ::com::sun::star::accessibility;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star;
43 // Ctor() and Dtor()
45 AccessibleIconChoiceCtrl::AccessibleIconChoiceCtrl( SvtIconChoiceCtrl const & _rIconCtrl, const Reference< XAccessible >& _xParent ) :
46 VCLXAccessibleComponent( _rIconCtrl.GetWindowPeer() ),
47 m_xParent ( _xParent )
51 IMPLEMENT_FORWARD_XINTERFACE2(AccessibleIconChoiceCtrl, VCLXAccessibleComponent, AccessibleIconChoiceCtrl_BASE)
52 IMPLEMENT_FORWARD_XTYPEPROVIDER2(AccessibleIconChoiceCtrl, VCLXAccessibleComponent, AccessibleIconChoiceCtrl_BASE)
54 void AccessibleIconChoiceCtrl::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
56 if ( isAlive() )
58 switch ( rVclWindowEvent.GetId() )
60 case VclEventId::ListboxSelect :
62 // First send an event that tells the listeners of a
63 // modified selection. The active descendant event is
64 // send after that so that the receiving AT has time to
65 // read the text or name of the active child.
66 // NotifyAccessibleEvent( AccessibleEventId::SELECTION_CHANGED, Any(), Any() );
68 if ( getCtrl() && getCtrl()->HasFocus() )
70 SvxIconChoiceCtrlEntry* pEntry = static_cast< SvxIconChoiceCtrlEntry* >( rVclWindowEvent.GetData() );
71 if ( pEntry )
73 sal_Int32 nPos = getCtrl()->GetEntryListPos( pEntry );
74 Reference< XAccessible > xChild = new AccessibleIconChoiceCtrlEntry( *getCtrl(), nPos, this );
75 uno::Any aOldValue, aNewValue;
76 aNewValue <<= xChild;
77 NotifyAccessibleEvent( AccessibleEventId::ACTIVE_DESCENDANT_CHANGED, aOldValue, aNewValue );
79 NotifyAccessibleEvent( AccessibleEventId::SELECTION_CHANGED, aOldValue, aNewValue );
83 break;
85 case VclEventId::WindowGetFocus :
87 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
88 if ( pCtrl && pCtrl->HasFocus() )
90 SvxIconChoiceCtrlEntry* pEntry = static_cast< SvxIconChoiceCtrlEntry* >( rVclWindowEvent.GetData() );
91 if ( pEntry == nullptr )
93 pEntry = getCtrl()->GetSelectedEntry();
95 if ( pEntry )
97 sal_Int32 nPos = pCtrl->GetEntryListPos( pEntry );
98 Reference< XAccessible > xChild = new AccessibleIconChoiceCtrlEntry( *pCtrl, nPos, this );
99 uno::Any aOldValue, aNewValue;
100 aNewValue <<= xChild;
101 NotifyAccessibleEvent( AccessibleEventId::ACTIVE_DESCENDANT_CHANGED, aOldValue, aNewValue );
102 NotifyAccessibleEvent( AccessibleEventId::SELECTION_CHANGED, aOldValue, aNewValue );
105 break;
107 default:
108 VCLXAccessibleComponent::ProcessWindowChildEvent (rVclWindowEvent);
113 // XComponent
115 void SAL_CALL AccessibleIconChoiceCtrl::disposing()
117 ::osl::MutexGuard aGuard( m_aMutex );
119 m_xParent = nullptr;
122 // XServiceInfo
124 OUString SAL_CALL AccessibleIconChoiceCtrl::getImplementationName()
126 return "com.sun.star.comp.svtools.AccessibleIconChoiceControl";
129 Sequence< OUString > SAL_CALL AccessibleIconChoiceCtrl::getSupportedServiceNames()
131 return {"com.sun.star.accessibility.AccessibleContext",
132 "com.sun.star.accessibility.AccessibleComponent",
133 "com.sun.star.awt.AccessibleIconChoiceControl"};
136 sal_Bool SAL_CALL AccessibleIconChoiceCtrl::supportsService( const OUString& _rServiceName )
138 return cppu::supportsService(this, _rServiceName);
141 // XAccessible
143 Reference< XAccessibleContext > SAL_CALL AccessibleIconChoiceCtrl::getAccessibleContext( )
145 ensureAlive();
146 return this;
149 // XAccessibleContext
151 sal_Int32 SAL_CALL AccessibleIconChoiceCtrl::getAccessibleChildCount( )
153 ::comphelper::OExternalLockGuard aGuard( this );
155 return getCtrl()->GetEntryCount();
158 Reference< XAccessible > SAL_CALL AccessibleIconChoiceCtrl::getAccessibleChild( sal_Int32 i )
160 ::comphelper::OExternalLockGuard aGuard( this );
162 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
163 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry(i);
164 if ( !pEntry )
165 throw RuntimeException();
167 return new AccessibleIconChoiceCtrlEntry( *pCtrl, i, this );
170 Reference< XAccessible > SAL_CALL AccessibleIconChoiceCtrl::getAccessibleParent( )
172 ::osl::MutexGuard aGuard( m_aMutex );
174 ensureAlive();
175 return m_xParent;
178 sal_Int16 SAL_CALL AccessibleIconChoiceCtrl::getAccessibleRole( )
180 //return AccessibleRole::TREE;
181 return AccessibleRole::LIST;
184 OUString SAL_CALL AccessibleIconChoiceCtrl::getAccessibleDescription( )
186 ::comphelper::OExternalLockGuard aGuard( this );
188 return getCtrl()->GetAccessibleDescription();
191 OUString SAL_CALL AccessibleIconChoiceCtrl::getAccessibleName( )
193 ::comphelper::OExternalLockGuard aGuard( this );
195 OUString sName = getCtrl()->GetAccessibleName();
196 if ( sName.isEmpty() )
197 sName = "IconChoiceControl";
198 return sName;
201 // XAccessibleSelection
203 void SAL_CALL AccessibleIconChoiceCtrl::selectAccessibleChild( sal_Int32 nChildIndex )
205 ::comphelper::OExternalLockGuard aGuard( this );
207 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
208 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( nChildIndex );
209 if ( !pEntry )
210 throw IndexOutOfBoundsException();
212 pCtrl->SetCursor( pEntry );
215 sal_Bool SAL_CALL AccessibleIconChoiceCtrl::isAccessibleChildSelected( sal_Int32 nChildIndex )
217 ::comphelper::OExternalLockGuard aGuard( this );
219 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
220 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( nChildIndex );
221 if ( !pEntry )
222 throw IndexOutOfBoundsException();
224 return ( pCtrl->GetCursor() == pEntry );
227 void SAL_CALL AccessibleIconChoiceCtrl::clearAccessibleSelection( )
229 ::comphelper::OExternalLockGuard aGuard( this );
230 getCtrl()->SetNoSelection();
233 void SAL_CALL AccessibleIconChoiceCtrl::selectAllAccessibleChildren( )
235 ::comphelper::OExternalLockGuard aGuard( this );
237 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
238 sal_Int32 nCount = pCtrl->GetEntryCount();
239 for ( sal_Int32 i = 0; i < nCount; ++i )
241 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( i );
242 if ( pCtrl->GetCursor() != pEntry )
243 pCtrl->SetCursor( pEntry );
247 sal_Int32 SAL_CALL AccessibleIconChoiceCtrl::getSelectedAccessibleChildCount( )
249 ::comphelper::OExternalLockGuard aGuard( this );
251 sal_Int32 nSelCount = 0;
252 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
253 sal_Int32 nCount = pCtrl->GetEntryCount();
254 for ( sal_Int32 i = 0; i < nCount; ++i )
256 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( i );
257 if ( pCtrl->GetCursor() == pEntry )
258 ++nSelCount;
261 return nSelCount;
264 Reference< XAccessible > SAL_CALL AccessibleIconChoiceCtrl::getSelectedAccessibleChild( sal_Int32 nSelectedChildIndex )
266 ::comphelper::OExternalLockGuard aGuard( this );
268 if ( nSelectedChildIndex < 0 || nSelectedChildIndex >= getSelectedAccessibleChildCount() )
269 throw IndexOutOfBoundsException();
271 Reference< XAccessible > xChild;
272 sal_Int32 nSelCount = 0;
273 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
274 sal_Int32 nCount = pCtrl->GetEntryCount();
275 for ( sal_Int32 i = 0; i < nCount; ++i )
277 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( i );
278 if ( pCtrl->GetCursor() == pEntry )
279 ++nSelCount;
281 if ( nSelCount == ( nSelectedChildIndex + 1 ) )
283 xChild = new AccessibleIconChoiceCtrlEntry( *pCtrl, i, this );
284 break;
288 return xChild;
291 void SAL_CALL AccessibleIconChoiceCtrl::deselectAccessibleChild( sal_Int32 nSelectedChildIndex )
293 ::comphelper::OExternalLockGuard aGuard( this );
295 if ( nSelectedChildIndex < 0 || nSelectedChildIndex >= getAccessibleChildCount() )
296 throw IndexOutOfBoundsException();
298 sal_Int32 nSelCount = 0;
299 VclPtr<SvtIconChoiceCtrl> pCtrl = getCtrl();
300 sal_Int32 nCount = pCtrl->GetEntryCount();
301 bool bFound = false;
302 for ( sal_Int32 i = 0; i < nCount; ++i )
304 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( i );
305 if ( pEntry->IsSelected() )
307 ++nSelCount;
308 if ( i == nSelectedChildIndex )
309 bFound = true;
313 // if only one entry is selected and its index is chosen to deselect -> no selection anymore
314 if ( nSelCount == 1 && bFound )
315 pCtrl->SetNoSelection();
318 void AccessibleIconChoiceCtrl::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
320 VCLXAccessibleComponent::FillAccessibleStateSet( rStateSet );
321 if ( isAlive() )
323 rStateSet.AddState( AccessibleStateType::FOCUSABLE );
324 rStateSet.AddState( AccessibleStateType::MANAGES_DESCENDANTS );
325 rStateSet.AddState( AccessibleStateType::SELECTABLE );
329 VclPtr< SvtIconChoiceCtrl > AccessibleIconChoiceCtrl::getCtrl() const
331 return GetAs<SvtIconChoiceCtrl >();
334 }// namespace accessibility
337 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */