Branch libreoffice-5-0-4
[LibreOffice.git] / accessibility / source / extended / accessibleiconchoicectrl.cxx
blob1d512aa0631c720f39f291204e838c94ca119bc1
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 "accessibility/extended/accessibleiconchoicectrl.hxx"
21 #include "accessibility/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 <unotools/accessiblestatesethelper.hxx>
27 #include <vcl/svapp.hxx>
28 #include <cppuhelper/supportsservice.hxx>
29 #include <cppuhelper/typeprovider.hxx>
32 namespace accessibility
36 // class AccessibleIconChoiceCtrl ----------------------------------------------
38 using namespace ::com::sun::star::accessibility;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::lang;
41 using namespace ::com::sun::star;
44 // Ctor() and Dtor()
46 AccessibleIconChoiceCtrl::AccessibleIconChoiceCtrl( SvtIconChoiceCtrl& _rIconCtrl, const Reference< XAccessible >& _xParent ) :
48 VCLXAccessibleComponent( _rIconCtrl.GetWindowPeer() ),
49 m_xParent ( _xParent )
53 AccessibleIconChoiceCtrl::~AccessibleIconChoiceCtrl()
57 IMPLEMENT_FORWARD_XINTERFACE2(AccessibleIconChoiceCtrl, VCLXAccessibleComponent, AccessibleIconChoiceCtrl_BASE)
58 IMPLEMENT_FORWARD_XTYPEPROVIDER2(AccessibleIconChoiceCtrl, VCLXAccessibleComponent, AccessibleIconChoiceCtrl_BASE)
60 void AccessibleIconChoiceCtrl::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
62 if ( isAlive() )
64 switch ( rVclWindowEvent.GetId() )
66 case VCLEVENT_LISTBOX_SELECT :
68 // First send an event that tells the listeners of a
69 // modified selection. The active descendant event is
70 // send after that so that the receiving AT has time to
71 // read the text or name of the active child.
72 // NotifyAccessibleEvent( AccessibleEventId::SELECTION_CHANGED, Any(), Any() );
74 if ( getCtrl() && getCtrl()->HasFocus() )
76 SvxIconChoiceCtrlEntry* pEntry = static_cast< SvxIconChoiceCtrlEntry* >( rVclWindowEvent.GetData() );
77 if ( pEntry )
79 sal_uLong nPos = getCtrl()->GetEntryListPos( pEntry );
80 Reference< XAccessible > xChild = new AccessibleIconChoiceCtrlEntry( *getCtrl(), nPos, this );
81 uno::Any aOldValue, aNewValue;
82 aNewValue <<= xChild;
83 NotifyAccessibleEvent( AccessibleEventId::ACTIVE_DESCENDANT_CHANGED, aOldValue, aNewValue );
85 NotifyAccessibleEvent( AccessibleEventId::SELECTION_CHANGED, aOldValue, aNewValue );
89 break;
91 case VCLEVENT_WINDOW_GETFOCUS :
93 SvtIconChoiceCtrl* pCtrl = getCtrl();
94 if ( pCtrl && pCtrl->HasFocus() )
96 SvxIconChoiceCtrlEntry* pEntry = static_cast< SvxIconChoiceCtrlEntry* >( rVclWindowEvent.GetData() );
97 if ( pEntry == NULL )
99 sal_uLong nPos=0;
100 pEntry = getCtrl()->GetSelectedEntry ( nPos );
102 if ( pEntry )
104 sal_uLong nPos = pCtrl->GetEntryListPos( pEntry );
105 Reference< XAccessible > xChild = new AccessibleIconChoiceCtrlEntry( *pCtrl, nPos, this );
106 uno::Any aOldValue, aNewValue;
107 aNewValue <<= xChild;
108 NotifyAccessibleEvent( AccessibleEventId::ACTIVE_DESCENDANT_CHANGED, aOldValue, aNewValue );
109 NotifyAccessibleEvent( AccessibleEventId::SELECTION_CHANGED, aOldValue, aNewValue );
112 break;
114 default:
115 VCLXAccessibleComponent::ProcessWindowChildEvent (rVclWindowEvent);
120 // XComponent
122 void SAL_CALL AccessibleIconChoiceCtrl::disposing()
124 ::osl::MutexGuard aGuard( m_aMutex );
126 m_xParent = NULL;
129 // XServiceInfo
131 OUString SAL_CALL AccessibleIconChoiceCtrl::getImplementationName() throw (RuntimeException, std::exception)
133 return getImplementationName_Static();
136 Sequence< OUString > SAL_CALL AccessibleIconChoiceCtrl::getSupportedServiceNames() throw (RuntimeException, std::exception)
138 return getSupportedServiceNames_Static();
141 sal_Bool SAL_CALL AccessibleIconChoiceCtrl::supportsService( const OUString& _rServiceName ) throw (RuntimeException, std::exception)
143 return cppu::supportsService(this, _rServiceName);
146 // XServiceInfo - static methods
148 Sequence< OUString > AccessibleIconChoiceCtrl::getSupportedServiceNames_Static() throw (RuntimeException)
150 Sequence< OUString > aSupported(3);
151 aSupported[0] = "com.sun.star.accessibility.AccessibleContext";
152 aSupported[1] = "com.sun.star.accessibility.AccessibleComponent";
153 aSupported[2] = "com.sun.star.awt.AccessibleIconChoiceControl";
154 return aSupported;
157 OUString AccessibleIconChoiceCtrl::getImplementationName_Static() throw (RuntimeException)
159 return OUString( "com.sun.star.comp.svtools.AccessibleIconChoiceControl" );
162 // XAccessible
164 Reference< XAccessibleContext > SAL_CALL AccessibleIconChoiceCtrl::getAccessibleContext( ) throw (RuntimeException, std::exception)
166 ensureAlive();
167 return this;
170 // XAccessibleContext
172 sal_Int32 SAL_CALL AccessibleIconChoiceCtrl::getAccessibleChildCount( ) throw (RuntimeException, std::exception)
174 ::comphelper::OExternalLockGuard aGuard( this );
176 ensureAlive();
177 return getCtrl()->GetEntryCount();
180 Reference< XAccessible > SAL_CALL AccessibleIconChoiceCtrl::getAccessibleChild( sal_Int32 i ) throw (RuntimeException, IndexOutOfBoundsException, std::exception)
182 ::comphelper::OExternalLockGuard aGuard( this );
184 ensureAlive();
185 SvtIconChoiceCtrl* pCtrl = getCtrl();
186 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry(i);
187 if ( !pEntry )
188 throw RuntimeException();
190 return new AccessibleIconChoiceCtrlEntry( *pCtrl, i, this );
193 Reference< XAccessible > SAL_CALL AccessibleIconChoiceCtrl::getAccessibleParent( ) throw (RuntimeException, std::exception)
195 ::osl::MutexGuard aGuard( m_aMutex );
197 ensureAlive();
198 return m_xParent;
201 sal_Int16 SAL_CALL AccessibleIconChoiceCtrl::getAccessibleRole( ) throw (RuntimeException, std::exception)
203 //return AccessibleRole::TREE;
204 return AccessibleRole::LIST;
207 OUString SAL_CALL AccessibleIconChoiceCtrl::getAccessibleDescription( ) throw (RuntimeException, std::exception)
209 ::comphelper::OExternalLockGuard aGuard( this );
211 ensureAlive();
212 return getCtrl()->GetAccessibleDescription();
215 OUString SAL_CALL AccessibleIconChoiceCtrl::getAccessibleName( ) throw (RuntimeException, std::exception)
217 ::comphelper::OExternalLockGuard aGuard( this );
219 ensureAlive();
221 OUString sName = getCtrl()->GetAccessibleName();
222 if ( sName.isEmpty() )
223 sName = "IconChoiceControl";
224 return sName;
227 // XAccessibleSelection
229 void SAL_CALL AccessibleIconChoiceCtrl::selectAccessibleChild( sal_Int32 nChildIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
231 ::comphelper::OExternalLockGuard aGuard( this );
233 ensureAlive();
235 SvtIconChoiceCtrl* pCtrl = getCtrl();
236 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( nChildIndex );
237 if ( !pEntry )
238 throw IndexOutOfBoundsException();
240 pCtrl->SetCursor( pEntry );
243 sal_Bool SAL_CALL AccessibleIconChoiceCtrl::isAccessibleChildSelected( sal_Int32 nChildIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
245 ::comphelper::OExternalLockGuard aGuard( this );
247 ensureAlive();
249 SvtIconChoiceCtrl* pCtrl = getCtrl();
250 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( nChildIndex );
251 if ( !pEntry )
252 throw IndexOutOfBoundsException();
254 return ( pCtrl->GetCursor() == pEntry );
257 void SAL_CALL AccessibleIconChoiceCtrl::clearAccessibleSelection( ) throw (RuntimeException, std::exception)
259 ::comphelper::OExternalLockGuard aGuard( this );
261 ensureAlive();
262 getCtrl()->SetNoSelection();
265 void SAL_CALL AccessibleIconChoiceCtrl::selectAllAccessibleChildren( ) throw (RuntimeException, std::exception)
267 ::comphelper::OExternalLockGuard aGuard( this );
269 ensureAlive();
271 SvtIconChoiceCtrl* pCtrl = getCtrl();
272 sal_Int32 nCount = pCtrl->GetEntryCount();
273 for ( sal_Int32 i = 0; i < nCount; ++i )
275 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( i );
276 if ( pCtrl->GetCursor() != pEntry )
277 pCtrl->SetCursor( pEntry );
281 sal_Int32 SAL_CALL AccessibleIconChoiceCtrl::getSelectedAccessibleChildCount( ) throw (RuntimeException, std::exception)
283 ::comphelper::OExternalLockGuard aGuard( this );
285 ensureAlive();
287 sal_Int32 nSelCount = 0;
288 SvtIconChoiceCtrl* pCtrl = getCtrl();
289 sal_Int32 nCount = pCtrl->GetEntryCount();
290 for ( sal_Int32 i = 0; i < nCount; ++i )
292 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( i );
293 if ( pCtrl->GetCursor() == pEntry )
294 ++nSelCount;
297 return nSelCount;
300 Reference< XAccessible > SAL_CALL AccessibleIconChoiceCtrl::getSelectedAccessibleChild( sal_Int32 nSelectedChildIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
302 ::comphelper::OExternalLockGuard aGuard( this );
304 ensureAlive();
306 if ( nSelectedChildIndex < 0 || nSelectedChildIndex >= getSelectedAccessibleChildCount() )
307 throw IndexOutOfBoundsException();
309 Reference< XAccessible > xChild;
310 sal_Int32 nSelCount = 0;
311 SvtIconChoiceCtrl* pCtrl = getCtrl();
312 sal_Int32 nCount = pCtrl->GetEntryCount();
313 for ( sal_Int32 i = 0; i < nCount; ++i )
315 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( i );
316 if ( pCtrl->GetCursor() == pEntry )
317 ++nSelCount;
319 if ( nSelCount == ( nSelectedChildIndex + 1 ) )
321 xChild = new AccessibleIconChoiceCtrlEntry( *pCtrl, i, this );
322 break;
326 return xChild;
329 void SAL_CALL AccessibleIconChoiceCtrl::deselectAccessibleChild( sal_Int32 nSelectedChildIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
331 ::comphelper::OExternalLockGuard aGuard( this );
333 ensureAlive();
335 if ( nSelectedChildIndex < 0 || nSelectedChildIndex >= getAccessibleChildCount() )
336 throw IndexOutOfBoundsException();
338 Reference< XAccessible > xChild;
339 sal_Int32 nSelCount = 0;
340 SvtIconChoiceCtrl* pCtrl = getCtrl();
341 sal_Int32 nCount = pCtrl->GetEntryCount();
342 bool bFound = false;
343 for ( sal_Int32 i = 0; i < nCount; ++i )
345 SvxIconChoiceCtrlEntry* pEntry = pCtrl->GetEntry( i );
346 if ( pEntry->IsSelected() )
348 ++nSelCount;
349 if ( i == nSelectedChildIndex )
350 bFound = true;
354 // if only one entry is selected and its index is chosen to deselect -> no selection anymore
355 if ( 1 == nSelCount && bFound )
356 pCtrl->SetNoSelection();
359 void AccessibleIconChoiceCtrl::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
361 VCLXAccessibleComponent::FillAccessibleStateSet( rStateSet );
362 if ( isAlive() )
364 rStateSet.AddState( AccessibleStateType::FOCUSABLE );
365 rStateSet.AddState( AccessibleStateType::MANAGES_DESCENDANTS );
366 rStateSet.AddState( AccessibleStateType::SELECTABLE );
370 VclPtr< SvtIconChoiceCtrl > AccessibleIconChoiceCtrl::getCtrl()
372 return GetAs<SvtIconChoiceCtrl >();
375 }// namespace accessibility
378 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */