Branch libreoffice-5-0-4
[LibreOffice.git] / accessibility / source / standard / vclxaccessibletabcontrol.cxx
blobd1ea190aab15fe3526f5c2fe2e2c1a29126ff834
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/standard/vclxaccessibletabcontrol.hxx>
21 #include <accessibility/standard/vclxaccessibletabpage.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/tabctrl.hxx>
28 #include <vcl/tabpage.hxx>
30 #include <vector>
32 using namespace ::com::sun::star;
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::lang;
35 using namespace ::com::sun::star::accessibility;
36 using namespace ::comphelper;
40 // class VCLXAccessibleTabControl
43 VCLXAccessibleTabControl::VCLXAccessibleTabControl( VCLXWindow* pVCLXWindow )
44 :VCLXAccessibleComponent( pVCLXWindow )
46 m_pTabControl = static_cast< TabControl* >( GetWindow().get() );
48 if ( m_pTabControl )
49 m_aAccessibleChildren.assign( m_pTabControl->GetPageCount(), Reference< XAccessible >() );
54 VCLXAccessibleTabControl::~VCLXAccessibleTabControl()
60 void VCLXAccessibleTabControl::UpdateFocused()
62 for ( sal_uInt32 i = 0; i < m_aAccessibleChildren.size(); ++i )
64 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
65 if ( xChild.is() )
67 VCLXAccessibleTabPage* pVCLXAccessibleTabPage = static_cast< VCLXAccessibleTabPage* >( xChild.get() );
68 if ( pVCLXAccessibleTabPage )
69 pVCLXAccessibleTabPage->SetFocused( pVCLXAccessibleTabPage->IsFocused() );
76 void VCLXAccessibleTabControl::UpdateSelected( sal_Int32 i, bool bSelected )
78 if ( i >= 0 && i < (sal_Int32)m_aAccessibleChildren.size() )
80 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
81 if ( xChild.is() )
83 VCLXAccessibleTabPage* pVCLXAccessibleTabPage = static_cast< VCLXAccessibleTabPage* >( xChild.get() );
84 if ( pVCLXAccessibleTabPage )
85 pVCLXAccessibleTabPage->SetSelected( bSelected );
92 void VCLXAccessibleTabControl::UpdatePageText( sal_Int32 i )
94 if ( i >= 0 && i < (sal_Int32)m_aAccessibleChildren.size() )
96 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
97 if ( xChild.is() )
99 VCLXAccessibleTabPage* pVCLXAccessibleTabPage = static_cast< VCLXAccessibleTabPage* >( xChild.get() );
100 if ( pVCLXAccessibleTabPage )
101 pVCLXAccessibleTabPage->SetPageText( pVCLXAccessibleTabPage->GetPageText() );
108 void VCLXAccessibleTabControl::UpdateTabPage( sal_Int32 i, bool bNew )
110 if ( i >= 0 && i < (sal_Int32)m_aAccessibleChildren.size() )
112 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
113 if ( xChild.is() )
115 VCLXAccessibleTabPage* pVCLXAccessibleTabPage = static_cast< VCLXAccessibleTabPage* >( xChild.get() );
116 if ( pVCLXAccessibleTabPage )
117 pVCLXAccessibleTabPage->Update( bNew );
124 void VCLXAccessibleTabControl::InsertChild( sal_Int32 i )
126 if ( i >= 0 && i <= (sal_Int32)m_aAccessibleChildren.size() )
128 // insert entry in child list
129 m_aAccessibleChildren.insert( m_aAccessibleChildren.begin() + i, Reference< XAccessible >() );
131 // send accessible child event
132 Reference< XAccessible > xChild( getAccessibleChild( i ) );
133 if ( xChild.is() )
135 Any aOldValue, aNewValue;
136 aNewValue <<= xChild;
137 NotifyAccessibleEvent( AccessibleEventId::CHILD, aOldValue, aNewValue );
144 void VCLXAccessibleTabControl::RemoveChild( sal_Int32 i )
146 if ( i >= 0 && i < (sal_Int32)m_aAccessibleChildren.size() )
148 // get the accessible of the removed page
149 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
151 // remove entry in child list
152 m_aAccessibleChildren.erase( m_aAccessibleChildren.begin() + i );
154 // send accessible child event
155 if ( xChild.is() )
157 Any aOldValue, aNewValue;
158 aOldValue <<= xChild;
159 NotifyAccessibleEvent( AccessibleEventId::CHILD, aOldValue, aNewValue );
161 Reference< XComponent > xComponent( xChild, UNO_QUERY );
162 if ( xComponent.is() )
163 xComponent->dispose();
170 void VCLXAccessibleTabControl::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
172 switch ( rVclWindowEvent.GetId() )
174 case VCLEVENT_TABPAGE_ACTIVATE:
175 case VCLEVENT_TABPAGE_DEACTIVATE:
177 if ( m_pTabControl )
179 sal_uInt16 nPageId = (sal_uInt16)reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData());
180 sal_uInt16 nPagePos = m_pTabControl->GetPagePos( nPageId );
181 UpdateFocused();
182 UpdateSelected( nPagePos, rVclWindowEvent.GetId() == VCLEVENT_TABPAGE_ACTIVATE );
185 break;
186 case VCLEVENT_TABPAGE_PAGETEXTCHANGED:
188 if ( m_pTabControl )
190 sal_uInt16 nPageId = (sal_uInt16)reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData());
191 sal_uInt16 nPagePos = m_pTabControl->GetPagePos( nPageId );
192 UpdatePageText( nPagePos );
195 break;
196 case VCLEVENT_TABPAGE_INSERTED:
198 if ( m_pTabControl )
200 sal_uInt16 nPageId = (sal_uInt16)reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData());
201 sal_uInt16 nPagePos = m_pTabControl->GetPagePos( nPageId );
202 InsertChild( nPagePos );
205 break;
206 case VCLEVENT_TABPAGE_REMOVED:
208 if ( m_pTabControl )
210 sal_uInt16 nPageId = (sal_uInt16)reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData());
211 for ( sal_Int32 i = 0, nCount = getAccessibleChildCount(); i < nCount; ++i )
213 Reference< XAccessible > xChild( getAccessibleChild( i ) );
214 if ( xChild.is() )
216 VCLXAccessibleTabPage* pVCLXAccessibleTabPage = static_cast< VCLXAccessibleTabPage* >( xChild.get() );
217 if ( pVCLXAccessibleTabPage && pVCLXAccessibleTabPage->GetPageId() == nPageId )
219 RemoveChild( i );
220 break;
226 break;
227 case VCLEVENT_TABPAGE_REMOVEDALL:
229 for ( sal_Int32 i = m_aAccessibleChildren.size() - 1; i >= 0; --i )
230 RemoveChild( i );
232 break;
233 case VCLEVENT_WINDOW_GETFOCUS:
234 case VCLEVENT_WINDOW_LOSEFOCUS:
236 UpdateFocused();
238 break;
239 case VCLEVENT_OBJECT_DYING:
241 if ( m_pTabControl )
243 m_pTabControl = NULL;
245 // dispose all tab pages
246 for ( sal_uInt32 i = 0; i < m_aAccessibleChildren.size(); ++i )
248 Reference< XComponent > xComponent( m_aAccessibleChildren[i], UNO_QUERY );
249 if ( xComponent.is() )
250 xComponent->dispose();
252 m_aAccessibleChildren.clear();
255 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
257 break;
258 default:
259 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
265 void VCLXAccessibleTabControl::ProcessWindowChildEvent( const VclWindowEvent& rVclWindowEvent )
267 switch ( rVclWindowEvent.GetId() )
269 case VCLEVENT_WINDOW_SHOW:
270 case VCLEVENT_WINDOW_HIDE:
272 if ( m_pTabControl )
274 vcl::Window* pChild = static_cast< vcl::Window* >( rVclWindowEvent.GetData() );
275 if ( pChild && pChild->GetType() == WINDOW_TABPAGE )
277 for ( sal_Int32 i = 0, nCount = m_pTabControl->GetPageCount(); i < nCount; ++i )
279 sal_uInt16 nPageId = m_pTabControl->GetPageId( (sal_uInt16)i );
280 TabPage* pTabPage = m_pTabControl->GetTabPage( nPageId );
281 if ( pTabPage == static_cast<TabPage*>(pChild) )
282 UpdateTabPage( i, rVclWindowEvent.GetId() == VCLEVENT_WINDOW_SHOW );
287 break;
288 default:
289 VCLXAccessibleComponent::ProcessWindowChildEvent( rVclWindowEvent );
296 void VCLXAccessibleTabControl::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
298 VCLXAccessibleComponent::FillAccessibleStateSet( rStateSet );
300 if ( m_pTabControl )
301 rStateSet.AddState( AccessibleStateType::FOCUSABLE );
305 // XInterface
308 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleTabControl, VCLXAccessibleComponent, VCLXAccessibleTabControl_BASE )
311 // XTypeProvider
314 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleTabControl, VCLXAccessibleComponent, VCLXAccessibleTabControl_BASE )
317 // XComponent
320 void VCLXAccessibleTabControl::disposing()
322 VCLXAccessibleComponent::disposing();
324 if ( m_pTabControl )
326 m_pTabControl = NULL;
328 // dispose all tab pages
329 for ( sal_uInt32 i = 0; i < m_aAccessibleChildren.size(); ++i )
331 Reference< XComponent > xComponent( m_aAccessibleChildren[i], UNO_QUERY );
332 if ( xComponent.is() )
333 xComponent->dispose();
335 m_aAccessibleChildren.clear();
340 // XServiceInfo
343 OUString VCLXAccessibleTabControl::getImplementationName() throw (RuntimeException, std::exception)
345 return OUString( "com.sun.star.comp.toolkit.AccessibleTabControl" );
350 Sequence< OUString > VCLXAccessibleTabControl::getSupportedServiceNames() throw (RuntimeException, std::exception)
352 Sequence< OUString > aNames(1);
353 aNames[0] = "com.sun.star.awt.AccessibleTabControl";
354 return aNames;
358 // XAccessibleContext
361 sal_Int32 VCLXAccessibleTabControl::getAccessibleChildCount() throw (RuntimeException, std::exception)
363 OExternalLockGuard aGuard( this );
365 return m_aAccessibleChildren.size();
370 Reference< XAccessible > VCLXAccessibleTabControl::getAccessibleChild( sal_Int32 i ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
372 OExternalLockGuard aGuard( this );
374 if ( i < 0 || i >= getAccessibleChildCount() )
375 throw IndexOutOfBoundsException();
377 Reference< XAccessible > xChild = m_aAccessibleChildren[i];
378 if ( !xChild.is() )
380 sal_uInt16 nPageId = m_pTabControl ? m_pTabControl->GetPageId((sal_uInt16)i) : 0;
381 if (nPageId)
383 xChild = new VCLXAccessibleTabPage( m_pTabControl, nPageId );
385 // insert into tab page list
386 m_aAccessibleChildren[i] = xChild;
390 return xChild;
395 sal_Int16 VCLXAccessibleTabControl::getAccessibleRole( ) throw (RuntimeException, std::exception)
397 OExternalLockGuard aGuard( this );
399 return AccessibleRole::PAGE_TAB_LIST;
404 OUString VCLXAccessibleTabControl::getAccessibleName( ) throw (RuntimeException, std::exception)
406 OExternalLockGuard aGuard( this );
408 return OUString();
412 // XAccessibleSelection
415 void VCLXAccessibleTabControl::selectAccessibleChild( sal_Int32 nChildIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
417 OExternalLockGuard aGuard( this );
419 if ( nChildIndex < 0 || nChildIndex >= getAccessibleChildCount() )
420 throw IndexOutOfBoundsException();
422 if ( m_pTabControl )
423 m_pTabControl->SelectTabPage( m_pTabControl->GetPageId( (sal_uInt16)nChildIndex ) );
428 sal_Bool VCLXAccessibleTabControl::isAccessibleChildSelected( sal_Int32 nChildIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
430 OExternalLockGuard aGuard( this );
432 if ( nChildIndex < 0 || nChildIndex >= getAccessibleChildCount() )
433 throw IndexOutOfBoundsException();
435 bool bSelected = false;
436 if ( m_pTabControl && m_pTabControl->GetCurPageId() == m_pTabControl->GetPageId( (sal_uInt16)nChildIndex ) )
437 bSelected = true;
439 return bSelected;
444 void VCLXAccessibleTabControl::clearAccessibleSelection( ) throw (RuntimeException, std::exception)
446 // This method makes no sense in a tab control, and so does nothing.
451 void VCLXAccessibleTabControl::selectAllAccessibleChildren( ) throw (RuntimeException, std::exception)
453 OExternalLockGuard aGuard( this );
455 selectAccessibleChild( 0 );
460 sal_Int32 VCLXAccessibleTabControl::getSelectedAccessibleChildCount( ) throw (RuntimeException, std::exception)
462 OExternalLockGuard aGuard( this );
464 return 1;
469 Reference< XAccessible > VCLXAccessibleTabControl::getSelectedAccessibleChild( sal_Int32 nSelectedChildIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
471 OExternalLockGuard aGuard( this );
473 if ( nSelectedChildIndex < 0 || nSelectedChildIndex >= getSelectedAccessibleChildCount() )
474 throw IndexOutOfBoundsException();
476 Reference< XAccessible > xChild;
478 for ( sal_Int32 i = 0, j = 0, nCount = getAccessibleChildCount(); i < nCount; i++ )
480 if ( isAccessibleChildSelected( i ) && ( j++ == nSelectedChildIndex ) )
482 xChild = getAccessibleChild( i );
483 break;
487 return xChild;
492 void VCLXAccessibleTabControl::deselectAccessibleChild( sal_Int32 nChildIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
494 OExternalLockGuard aGuard( this );
496 if ( nChildIndex < 0 || nChildIndex >= getAccessibleChildCount() )
497 throw IndexOutOfBoundsException();
499 // This method makes no sense in a tab control, and so does nothing.
504 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */