Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / accessibility / source / standard / vclxaccessibletabcontrol.cxx
blob9963b51aef5f9aa263f2652f046ea1640dd177af
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 <comphelper/accessiblecontexthelper.hxx>
28 #include <o3tl/safeint.hxx>
29 #include <vcl/tabctrl.hxx>
30 #include <vcl/tabpage.hxx>
31 #include <vcl/vclevent.hxx>
33 #include <vector>
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::uno;
37 using namespace ::com::sun::star::lang;
38 using namespace ::com::sun::star::accessibility;
39 using namespace ::comphelper;
44 VCLXAccessibleTabControl::VCLXAccessibleTabControl( VCLXWindow* pVCLXWindow )
45 :ImplInheritanceHelper( pVCLXWindow )
47 m_pTabControl = GetAs<TabControl>();
48 if (!m_pTabControl)
49 return;
50 if (m_pTabControl->isDisposed())
52 m_pTabControl.clear();
53 return;
55 m_aAccessibleChildren.assign( m_pTabControl->GetPageCount(), Reference< XAccessible >() );
59 void VCLXAccessibleTabControl::UpdateFocused()
61 for (const Reference<XAccessible>& xChild : m_aAccessibleChildren)
63 if ( xChild.is() )
65 VCLXAccessibleTabPage* pVCLXAccessibleTabPage = static_cast< VCLXAccessibleTabPage* >( xChild.get() );
66 if ( pVCLXAccessibleTabPage )
67 pVCLXAccessibleTabPage->SetFocused( pVCLXAccessibleTabPage->IsFocused() );
73 void VCLXAccessibleTabControl::UpdateSelected( sal_Int32 i, bool bSelected )
75 if ( i >= 0 && o3tl::make_unsigned(i) < m_aAccessibleChildren.size() )
77 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
78 if ( xChild.is() )
80 VCLXAccessibleTabPage* pVCLXAccessibleTabPage = static_cast< VCLXAccessibleTabPage* >( xChild.get() );
81 if ( pVCLXAccessibleTabPage )
82 pVCLXAccessibleTabPage->SetSelected( bSelected );
88 void VCLXAccessibleTabControl::UpdatePageText( sal_Int32 i )
90 if ( i >= 0 && o3tl::make_unsigned(i) < m_aAccessibleChildren.size() )
92 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
93 if ( xChild.is() )
95 VCLXAccessibleTabPage* pVCLXAccessibleTabPage = static_cast< VCLXAccessibleTabPage* >( xChild.get() );
96 if ( pVCLXAccessibleTabPage )
97 pVCLXAccessibleTabPage->SetPageText( pVCLXAccessibleTabPage->GetPageText() );
103 void VCLXAccessibleTabControl::UpdateTabPage( sal_Int32 i, bool bNew )
105 if ( i >= 0 && o3tl::make_unsigned(i) < m_aAccessibleChildren.size() )
107 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
108 if ( xChild.is() )
110 VCLXAccessibleTabPage* pVCLXAccessibleTabPage = static_cast< VCLXAccessibleTabPage* >( xChild.get() );
111 if ( pVCLXAccessibleTabPage )
112 pVCLXAccessibleTabPage->Update( bNew );
118 void VCLXAccessibleTabControl::InsertChild( sal_Int32 i )
120 if ( i < 0 || o3tl::make_unsigned(i) > m_aAccessibleChildren.size() )
121 return;
123 // insert entry in child list
124 m_aAccessibleChildren.insert( m_aAccessibleChildren.begin() + i, Reference< XAccessible >() );
126 // send accessible child event
127 Reference< XAccessible > xChild( getAccessibleChild( i ) );
128 if ( xChild.is() )
130 Any aOldValue, aNewValue;
131 aNewValue <<= xChild;
132 NotifyAccessibleEvent( AccessibleEventId::CHILD, aOldValue, aNewValue );
137 void VCLXAccessibleTabControl::RemoveChild( sal_Int32 i )
139 if ( i < 0 || o3tl::make_unsigned(i) >= m_aAccessibleChildren.size() )
140 return;
142 // get the accessible of the removed page
143 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
145 // remove entry in child list
146 m_aAccessibleChildren.erase( m_aAccessibleChildren.begin() + i );
148 // send accessible child event
149 if ( xChild.is() )
151 Any aOldValue, aNewValue;
152 aOldValue <<= xChild;
153 NotifyAccessibleEvent( AccessibleEventId::CHILD, aOldValue, aNewValue );
155 Reference< XComponent > xComponent( xChild, UNO_QUERY );
156 if ( xComponent.is() )
157 xComponent->dispose();
162 void VCLXAccessibleTabControl::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
164 switch ( rVclWindowEvent.GetId() )
166 case VclEventId::TabpageActivate:
167 case VclEventId::TabpageDeactivate:
169 if ( m_pTabControl )
171 sal_uInt16 nPageId = static_cast<sal_uInt16>(reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData()));
172 sal_uInt16 nPagePos = m_pTabControl->GetPagePos( nPageId );
173 UpdateFocused();
174 UpdateSelected( nPagePos, rVclWindowEvent.GetId() == VclEventId::TabpageActivate );
177 break;
178 case VclEventId::TabpagePageTextChanged:
180 if ( m_pTabControl )
182 sal_uInt16 nPageId = static_cast<sal_uInt16>(reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData()));
183 sal_uInt16 nPagePos = m_pTabControl->GetPagePos( nPageId );
184 UpdatePageText( nPagePos );
187 break;
188 case VclEventId::TabpageInserted:
190 if ( m_pTabControl )
192 sal_uInt16 nPageId = static_cast<sal_uInt16>(reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData()));
193 sal_uInt16 nPagePos = m_pTabControl->GetPagePos( nPageId );
194 InsertChild( nPagePos );
197 break;
198 case VclEventId::TabpageRemoved:
200 if ( m_pTabControl )
202 sal_uInt16 nPageId = static_cast<sal_uInt16>(reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData()));
203 for ( sal_Int64 i = 0, nCount = getAccessibleChildCount(); i < nCount; ++i )
205 Reference< XAccessible > xChild( getAccessibleChild( i ) );
206 if ( xChild.is() )
208 VCLXAccessibleTabPage* pVCLXAccessibleTabPage = static_cast< VCLXAccessibleTabPage* >( xChild.get() );
209 if ( pVCLXAccessibleTabPage && pVCLXAccessibleTabPage->GetPageId() == nPageId )
211 RemoveChild( i );
212 break;
218 break;
219 case VclEventId::TabpageRemovedAll:
221 for ( sal_Int32 i = m_aAccessibleChildren.size() - 1; i >= 0; --i )
222 RemoveChild( i );
224 break;
225 case VclEventId::WindowGetFocus:
226 case VclEventId::WindowLoseFocus:
228 UpdateFocused();
230 break;
231 case VclEventId::ObjectDying:
233 if ( m_pTabControl )
235 m_pTabControl = nullptr;
237 // dispose all tab pages
238 for (const Reference<XAccessible>& i : m_aAccessibleChildren)
240 Reference< XComponent > xComponent( i, UNO_QUERY );
241 if ( xComponent.is() )
242 xComponent->dispose();
244 m_aAccessibleChildren.clear();
247 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
249 break;
250 default:
251 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
256 void VCLXAccessibleTabControl::ProcessWindowChildEvent( const VclWindowEvent& rVclWindowEvent )
258 switch ( rVclWindowEvent.GetId() )
260 case VclEventId::WindowShow:
261 case VclEventId::WindowHide:
263 if ( m_pTabControl )
265 vcl::Window* pChild = static_cast< vcl::Window* >( rVclWindowEvent.GetData() );
266 if ( pChild && pChild->GetType() == WindowType::TABPAGE )
268 for ( sal_Int32 i = 0, nCount = m_pTabControl->GetPageCount(); i < nCount; ++i )
270 sal_uInt16 nPageId = m_pTabControl->GetPageId( static_cast<sal_uInt16>(i) );
271 TabPage* pTabPage = m_pTabControl->GetTabPage( nPageId );
272 if ( pTabPage == static_cast<TabPage*>(pChild) )
273 UpdateTabPage( i, rVclWindowEvent.GetId() == VclEventId::WindowShow );
278 break;
279 default:
280 VCLXAccessibleComponent::ProcessWindowChildEvent( rVclWindowEvent );
285 void VCLXAccessibleTabControl::FillAccessibleStateSet( sal_Int64& rStateSet )
287 VCLXAccessibleComponent::FillAccessibleStateSet( rStateSet );
289 if ( m_pTabControl )
290 rStateSet |= AccessibleStateType::FOCUSABLE;
294 // XComponent
297 void VCLXAccessibleTabControl::disposing()
299 VCLXAccessibleComponent::disposing();
301 if ( !m_pTabControl )
302 return;
304 m_pTabControl = nullptr;
306 // dispose all tab pages
307 for (const Reference<XAccessible>& i : m_aAccessibleChildren)
309 Reference< XComponent > xComponent( i, UNO_QUERY );
310 if ( xComponent.is() )
311 xComponent->dispose();
313 m_aAccessibleChildren.clear();
317 // XServiceInfo
320 OUString VCLXAccessibleTabControl::getImplementationName()
322 return "com.sun.star.comp.toolkit.AccessibleTabControl";
326 Sequence< OUString > VCLXAccessibleTabControl::getSupportedServiceNames()
328 return { "com.sun.star.awt.AccessibleTabControl" };
332 // XAccessibleContext
335 sal_Int64 VCLXAccessibleTabControl::getAccessibleChildCount()
337 OExternalLockGuard aGuard( this );
339 return m_aAccessibleChildren.size();
343 Reference< XAccessible > VCLXAccessibleTabControl::getAccessibleChild( sal_Int64 i )
345 OExternalLockGuard aGuard( this );
347 if ( i < 0 || o3tl::make_unsigned(i) >= m_aAccessibleChildren.size() )
348 throw IndexOutOfBoundsException();
350 return implGetAccessibleChild( i );
353 Reference< XAccessible > VCLXAccessibleTabControl::implGetAccessibleChild( sal_Int64 i )
355 Reference< XAccessible > xChild = m_aAccessibleChildren[i];
356 if ( !xChild.is() )
358 sal_uInt16 nPageId = m_pTabControl ? m_pTabControl->GetPageId(static_cast<sal_uInt16>(i)) : 0;
359 if (nPageId)
361 xChild = new VCLXAccessibleTabPage( m_pTabControl, nPageId );
363 // insert into tab page list
364 m_aAccessibleChildren[i] = xChild;
368 return xChild;
372 sal_Int16 VCLXAccessibleTabControl::getAccessibleRole( )
374 OExternalLockGuard aGuard( this );
376 return AccessibleRole::PAGE_TAB_LIST;
380 OUString VCLXAccessibleTabControl::getAccessibleName( )
382 OExternalLockGuard aGuard( this );
384 return OUString();
388 // XAccessibleSelection
391 void VCLXAccessibleTabControl::selectAccessibleChild( sal_Int64 nChildIndex )
393 OExternalLockGuard aGuard( this );
395 if ( nChildIndex < 0 || o3tl::make_unsigned(nChildIndex) >= m_aAccessibleChildren.size() )
396 throw IndexOutOfBoundsException();
398 if ( m_pTabControl )
399 m_pTabControl->SelectTabPage( m_pTabControl->GetPageId( static_cast<sal_uInt16>(nChildIndex) ) );
403 sal_Bool VCLXAccessibleTabControl::isAccessibleChildSelected( sal_Int64 nChildIndex )
405 OExternalLockGuard aGuard( this );
407 if ( nChildIndex < 0 || o3tl::make_unsigned(nChildIndex) >= m_aAccessibleChildren.size() )
408 throw IndexOutOfBoundsException();
410 return implIsAccessibleChildSelected( nChildIndex );
413 bool VCLXAccessibleTabControl::implIsAccessibleChildSelected( sal_Int32 nChildIndex )
415 bool bSelected = false;
416 if ( m_pTabControl && m_pTabControl->GetCurPageId() == m_pTabControl->GetPageId( static_cast<sal_uInt16>(nChildIndex) ) )
417 bSelected = true;
419 return bSelected;
423 void VCLXAccessibleTabControl::clearAccessibleSelection( )
425 // This method makes no sense in a tab control, and so does nothing.
429 void VCLXAccessibleTabControl::selectAllAccessibleChildren( )
431 selectAccessibleChild( 0 );
435 sal_Int64 VCLXAccessibleTabControl::getSelectedAccessibleChildCount( )
437 OExternalLockGuard aGuard( this );
439 return 1;
443 Reference< XAccessible > VCLXAccessibleTabControl::getSelectedAccessibleChild( sal_Int64 nSelectedChildIndex )
445 OExternalLockGuard aGuard( this );
447 if ( nSelectedChildIndex != 0 )
448 throw IndexOutOfBoundsException();
450 Reference< XAccessible > xChild;
452 for ( std::size_t i = 0, nCount = m_aAccessibleChildren.size(); i < nCount; i++ )
454 if ( implIsAccessibleChildSelected( i ) )
456 xChild = implGetAccessibleChild( i );
457 break;
461 return xChild;
465 void VCLXAccessibleTabControl::deselectAccessibleChild( sal_Int64 nChildIndex )
467 OExternalLockGuard aGuard( this );
469 if ( nChildIndex < 0 || o3tl::make_unsigned(nChildIndex) >= m_aAccessibleChildren.size() )
470 throw IndexOutOfBoundsException();
472 // This method makes no sense in a tab control, and so does nothing.
476 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */