Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / accessibility / source / standard / vclxaccessibletabcontrol.cxx
blob83a67b6a35714f083f5b9737ff09c176105dc668
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 <standard/vclxaccessibletabcontrol.hxx>
21 #include <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 <com/sun/star/lang/IndexOutOfBoundsException.hpp>
27 #include <unotools/accessiblestatesethelper.hxx>
28 #include <vcl/tabctrl.hxx>
29 #include <vcl/tabpage.hxx>
31 #include <vector>
33 using namespace ::com::sun::star;
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::lang;
36 using namespace ::com::sun::star::accessibility;
37 using namespace ::comphelper;
40 // class VCLXAccessibleTabControl
43 VCLXAccessibleTabControl::VCLXAccessibleTabControl( VCLXWindow* pVCLXWindow )
44 :VCLXAccessibleComponent( pVCLXWindow )
46 m_pTabControl = static_cast< TabControl* >( GetWindow().get() );
47 if (!m_pTabControl)
48 return;
49 if (m_pTabControl->isDisposed())
51 m_pTabControl.clear();
52 return;
54 m_aAccessibleChildren.assign( m_pTabControl->GetPageCount(), Reference< XAccessible >() );
58 void VCLXAccessibleTabControl::UpdateFocused()
60 for (const Reference<XAccessible>& xChild : m_aAccessibleChildren)
62 if ( xChild.is() )
64 VCLXAccessibleTabPage* pVCLXAccessibleTabPage = static_cast< VCLXAccessibleTabPage* >( xChild.get() );
65 if ( pVCLXAccessibleTabPage )
66 pVCLXAccessibleTabPage->SetFocused( pVCLXAccessibleTabPage->IsFocused() );
72 void VCLXAccessibleTabControl::UpdateSelected( sal_Int32 i, bool bSelected )
74 if ( i >= 0 && i < static_cast<sal_Int32>(m_aAccessibleChildren.size()) )
76 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
77 if ( xChild.is() )
79 VCLXAccessibleTabPage* pVCLXAccessibleTabPage = static_cast< VCLXAccessibleTabPage* >( xChild.get() );
80 if ( pVCLXAccessibleTabPage )
81 pVCLXAccessibleTabPage->SetSelected( bSelected );
87 void VCLXAccessibleTabControl::UpdatePageText( sal_Int32 i )
89 if ( i >= 0 && i < static_cast<sal_Int32>(m_aAccessibleChildren.size()) )
91 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
92 if ( xChild.is() )
94 VCLXAccessibleTabPage* pVCLXAccessibleTabPage = static_cast< VCLXAccessibleTabPage* >( xChild.get() );
95 if ( pVCLXAccessibleTabPage )
96 pVCLXAccessibleTabPage->SetPageText( pVCLXAccessibleTabPage->GetPageText() );
102 void VCLXAccessibleTabControl::UpdateTabPage( sal_Int32 i, bool bNew )
104 if ( i >= 0 && i < static_cast<sal_Int32>(m_aAccessibleChildren.size()) )
106 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
107 if ( xChild.is() )
109 VCLXAccessibleTabPage* pVCLXAccessibleTabPage = static_cast< VCLXAccessibleTabPage* >( xChild.get() );
110 if ( pVCLXAccessibleTabPage )
111 pVCLXAccessibleTabPage->Update( bNew );
117 void VCLXAccessibleTabControl::InsertChild( sal_Int32 i )
119 if ( i >= 0 && i <= static_cast<sal_Int32>(m_aAccessibleChildren.size()) )
121 // insert entry in child list
122 m_aAccessibleChildren.insert( m_aAccessibleChildren.begin() + i, Reference< XAccessible >() );
124 // send accessible child event
125 Reference< XAccessible > xChild( getAccessibleChild( i ) );
126 if ( xChild.is() )
128 Any aOldValue, aNewValue;
129 aNewValue <<= xChild;
130 NotifyAccessibleEvent( AccessibleEventId::CHILD, aOldValue, aNewValue );
136 void VCLXAccessibleTabControl::RemoveChild( sal_Int32 i )
138 if ( i >= 0 && i < static_cast<sal_Int32>(m_aAccessibleChildren.size()) )
140 // get the accessible of the removed page
141 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
143 // remove entry in child list
144 m_aAccessibleChildren.erase( m_aAccessibleChildren.begin() + i );
146 // send accessible child event
147 if ( xChild.is() )
149 Any aOldValue, aNewValue;
150 aOldValue <<= xChild;
151 NotifyAccessibleEvent( AccessibleEventId::CHILD, aOldValue, aNewValue );
153 Reference< XComponent > xComponent( xChild, UNO_QUERY );
154 if ( xComponent.is() )
155 xComponent->dispose();
161 void VCLXAccessibleTabControl::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
163 switch ( rVclWindowEvent.GetId() )
165 case VclEventId::TabpageActivate:
166 case VclEventId::TabpageDeactivate:
168 if ( m_pTabControl )
170 sal_uInt16 nPageId = static_cast<sal_uInt16>(reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData()));
171 sal_uInt16 nPagePos = m_pTabControl->GetPagePos( nPageId );
172 UpdateFocused();
173 UpdateSelected( nPagePos, rVclWindowEvent.GetId() == VclEventId::TabpageActivate );
176 break;
177 case VclEventId::TabpagePageTextChanged:
179 if ( m_pTabControl )
181 sal_uInt16 nPageId = static_cast<sal_uInt16>(reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData()));
182 sal_uInt16 nPagePos = m_pTabControl->GetPagePos( nPageId );
183 UpdatePageText( nPagePos );
186 break;
187 case VclEventId::TabpageInserted:
189 if ( m_pTabControl )
191 sal_uInt16 nPageId = static_cast<sal_uInt16>(reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData()));
192 sal_uInt16 nPagePos = m_pTabControl->GetPagePos( nPageId );
193 InsertChild( nPagePos );
196 break;
197 case VclEventId::TabpageRemoved:
199 if ( m_pTabControl )
201 sal_uInt16 nPageId = static_cast<sal_uInt16>(reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData()));
202 for ( sal_Int32 i = 0, nCount = getAccessibleChildCount(); i < nCount; ++i )
204 Reference< XAccessible > xChild( getAccessibleChild( i ) );
205 if ( xChild.is() )
207 VCLXAccessibleTabPage* pVCLXAccessibleTabPage = static_cast< VCLXAccessibleTabPage* >( xChild.get() );
208 if ( pVCLXAccessibleTabPage && pVCLXAccessibleTabPage->GetPageId() == nPageId )
210 RemoveChild( i );
211 break;
217 break;
218 case VclEventId::TabpageRemovedAll:
220 for ( sal_Int32 i = m_aAccessibleChildren.size() - 1; i >= 0; --i )
221 RemoveChild( i );
223 break;
224 case VclEventId::WindowGetFocus:
225 case VclEventId::WindowLoseFocus:
227 UpdateFocused();
229 break;
230 case VclEventId::ObjectDying:
232 if ( m_pTabControl )
234 m_pTabControl = nullptr;
236 // dispose all tab pages
237 for (const Reference<XAccessible>& i : m_aAccessibleChildren)
239 Reference< XComponent > xComponent( i, UNO_QUERY );
240 if ( xComponent.is() )
241 xComponent->dispose();
243 m_aAccessibleChildren.clear();
246 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
248 break;
249 default:
250 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
255 void VCLXAccessibleTabControl::ProcessWindowChildEvent( const VclWindowEvent& rVclWindowEvent )
257 switch ( rVclWindowEvent.GetId() )
259 case VclEventId::WindowShow:
260 case VclEventId::WindowHide:
262 if ( m_pTabControl )
264 vcl::Window* pChild = static_cast< vcl::Window* >( rVclWindowEvent.GetData() );
265 if ( pChild && pChild->GetType() == WindowType::TABPAGE )
267 for ( sal_Int32 i = 0, nCount = m_pTabControl->GetPageCount(); i < nCount; ++i )
269 sal_uInt16 nPageId = m_pTabControl->GetPageId( static_cast<sal_uInt16>(i) );
270 TabPage* pTabPage = m_pTabControl->GetTabPage( nPageId );
271 if ( pTabPage == static_cast<TabPage*>(pChild) )
272 UpdateTabPage( i, rVclWindowEvent.GetId() == VclEventId::WindowShow );
277 break;
278 default:
279 VCLXAccessibleComponent::ProcessWindowChildEvent( rVclWindowEvent );
284 void VCLXAccessibleTabControl::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
286 VCLXAccessibleComponent::FillAccessibleStateSet( rStateSet );
288 if ( m_pTabControl )
289 rStateSet.AddState( AccessibleStateType::FOCUSABLE );
293 // XInterface
296 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleTabControl, VCLXAccessibleComponent, VCLXAccessibleTabControl_BASE )
299 // XTypeProvider
302 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleTabControl, VCLXAccessibleComponent, VCLXAccessibleTabControl_BASE )
305 // XComponent
308 void VCLXAccessibleTabControl::disposing()
310 VCLXAccessibleComponent::disposing();
312 if ( m_pTabControl )
314 m_pTabControl = nullptr;
316 // dispose all tab pages
317 for (const Reference<XAccessible>& i : m_aAccessibleChildren)
319 Reference< XComponent > xComponent( i, UNO_QUERY );
320 if ( xComponent.is() )
321 xComponent->dispose();
323 m_aAccessibleChildren.clear();
328 // XServiceInfo
331 OUString VCLXAccessibleTabControl::getImplementationName()
333 return "com.sun.star.comp.toolkit.AccessibleTabControl";
337 Sequence< OUString > VCLXAccessibleTabControl::getSupportedServiceNames()
339 return { "com.sun.star.awt.AccessibleTabControl" };
343 // XAccessibleContext
346 sal_Int32 VCLXAccessibleTabControl::getAccessibleChildCount()
348 OExternalLockGuard aGuard( this );
350 return m_aAccessibleChildren.size();
354 Reference< XAccessible > VCLXAccessibleTabControl::getAccessibleChild( sal_Int32 i )
356 OExternalLockGuard aGuard( this );
358 if ( i < 0 || static_cast<std::size_t>(i) >= m_aAccessibleChildren.size() )
359 throw IndexOutOfBoundsException();
361 return implGetAccessibleChild( i );
364 Reference< XAccessible > VCLXAccessibleTabControl::implGetAccessibleChild( sal_Int32 i )
366 Reference< XAccessible > xChild = m_aAccessibleChildren[i];
367 if ( !xChild.is() )
369 sal_uInt16 nPageId = m_pTabControl ? m_pTabControl->GetPageId(static_cast<sal_uInt16>(i)) : 0;
370 if (nPageId)
372 xChild = new VCLXAccessibleTabPage( m_pTabControl, nPageId );
374 // insert into tab page list
375 m_aAccessibleChildren[i] = xChild;
379 return xChild;
383 sal_Int16 VCLXAccessibleTabControl::getAccessibleRole( )
385 OExternalLockGuard aGuard( this );
387 return AccessibleRole::PAGE_TAB_LIST;
391 OUString VCLXAccessibleTabControl::getAccessibleName( )
393 OExternalLockGuard aGuard( this );
395 return OUString();
399 // XAccessibleSelection
402 void VCLXAccessibleTabControl::selectAccessibleChild( sal_Int32 nChildIndex )
404 OExternalLockGuard aGuard( this );
406 if ( nChildIndex < 0 || static_cast<std::size_t>(nChildIndex) >= m_aAccessibleChildren.size() )
407 throw IndexOutOfBoundsException();
409 if ( m_pTabControl )
410 m_pTabControl->SelectTabPage( m_pTabControl->GetPageId( static_cast<sal_uInt16>(nChildIndex) ) );
414 sal_Bool VCLXAccessibleTabControl::isAccessibleChildSelected( sal_Int32 nChildIndex )
416 OExternalLockGuard aGuard( this );
418 if ( nChildIndex < 0 || static_cast<std::size_t>(nChildIndex) >= m_aAccessibleChildren.size() )
419 throw IndexOutOfBoundsException();
421 return implIsAccessibleChildSelected( nChildIndex );
424 bool VCLXAccessibleTabControl::implIsAccessibleChildSelected( sal_Int32 nChildIndex )
426 bool bSelected = false;
427 if ( m_pTabControl && m_pTabControl->GetCurPageId() == m_pTabControl->GetPageId( static_cast<sal_uInt16>(nChildIndex) ) )
428 bSelected = true;
430 return bSelected;
434 void VCLXAccessibleTabControl::clearAccessibleSelection( )
436 // This method makes no sense in a tab control, and so does nothing.
440 void VCLXAccessibleTabControl::selectAllAccessibleChildren( )
442 selectAccessibleChild( 0 );
446 sal_Int32 VCLXAccessibleTabControl::getSelectedAccessibleChildCount( )
448 OExternalLockGuard aGuard( this );
450 return 1;
454 Reference< XAccessible > VCLXAccessibleTabControl::getSelectedAccessibleChild( sal_Int32 nSelectedChildIndex )
456 OExternalLockGuard aGuard( this );
458 if ( nSelectedChildIndex != 0 )
459 throw IndexOutOfBoundsException();
461 Reference< XAccessible > xChild;
463 for ( std::size_t i = 0, nCount = m_aAccessibleChildren.size(); i < nCount; i++ )
465 if ( implIsAccessibleChildSelected( i ) )
467 xChild = implGetAccessibleChild( i );
468 break;
472 return xChild;
476 void VCLXAccessibleTabControl::deselectAccessibleChild( sal_Int32 nChildIndex )
478 OExternalLockGuard aGuard( this );
480 if ( nChildIndex < 0 || static_cast<std::size_t>(nChildIndex) >= m_aAccessibleChildren.size() )
481 throw IndexOutOfBoundsException();
483 // This method makes no sense in a tab control, and so does nothing.
487 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */