tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / accessibility / source / standard / vclxaccessibletabcontrol.cxx
blob9e9a2f7caeddf92226e49abb3e4bb301ef21539f
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(vcl::Window* pWindow)
45 : ImplInheritanceHelper(pWindow)
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(), rtl::Reference< VCLXAccessibleTabPage >() );
59 void VCLXAccessibleTabControl::UpdateFocused()
61 for (const rtl::Reference<VCLXAccessibleTabPage>& pVCLXAccessibleTabPage : m_aAccessibleChildren)
63 if ( pVCLXAccessibleTabPage )
64 pVCLXAccessibleTabPage->SetFocused( pVCLXAccessibleTabPage->IsFocused() );
69 void VCLXAccessibleTabControl::UpdateSelected( sal_Int32 i, bool bSelected )
71 if ( i >= 0 && o3tl::make_unsigned(i) < m_aAccessibleChildren.size() )
73 rtl::Reference< VCLXAccessibleTabPage > pVCLXAccessibleTabPage( m_aAccessibleChildren[i] );
74 if ( pVCLXAccessibleTabPage )
75 pVCLXAccessibleTabPage->SetSelected( bSelected );
80 void VCLXAccessibleTabControl::UpdatePageText( sal_Int32 i )
82 if ( i >= 0 && o3tl::make_unsigned(i) < m_aAccessibleChildren.size() )
84 rtl::Reference< VCLXAccessibleTabPage > pVCLXAccessibleTabPage( m_aAccessibleChildren[i] );
85 if ( pVCLXAccessibleTabPage )
86 pVCLXAccessibleTabPage->SetPageText( pVCLXAccessibleTabPage->GetPageText() );
91 void VCLXAccessibleTabControl::UpdateTabPage( sal_Int32 i, bool bNew )
93 if ( i >= 0 && o3tl::make_unsigned(i) < m_aAccessibleChildren.size() )
95 rtl::Reference< VCLXAccessibleTabPage > pVCLXAccessibleTabPage( m_aAccessibleChildren[i] );
96 if ( pVCLXAccessibleTabPage )
97 pVCLXAccessibleTabPage->Update( bNew );
102 void VCLXAccessibleTabControl::InsertChild( sal_Int32 i )
104 if ( i < 0 || o3tl::make_unsigned(i) > m_aAccessibleChildren.size() )
105 return;
107 // insert entry in child list
108 m_aAccessibleChildren.insert( m_aAccessibleChildren.begin() + i, rtl::Reference< VCLXAccessibleTabPage >() );
110 // send accessible child event
111 Reference< XAccessible > xChild( getAccessibleChild( i ) );
112 if ( xChild.is() )
114 Any aOldValue, aNewValue;
115 aNewValue <<= xChild;
116 NotifyAccessibleEvent( AccessibleEventId::CHILD, aOldValue, aNewValue );
121 void VCLXAccessibleTabControl::RemoveChild( sal_Int32 i )
123 if ( i < 0 || o3tl::make_unsigned(i) >= m_aAccessibleChildren.size() )
124 return;
126 // get the accessible of the removed page
127 rtl::Reference< VCLXAccessibleTabPage > xChild( m_aAccessibleChildren[i] );
129 // remove entry in child list
130 m_aAccessibleChildren.erase( m_aAccessibleChildren.begin() + i );
132 // send accessible child event
133 if ( xChild.is() )
135 Any aOldValue, aNewValue;
136 aOldValue <<= uno::Reference<XAccessible>(xChild);
137 NotifyAccessibleEvent( AccessibleEventId::CHILD, aOldValue, aNewValue );
139 xChild->dispose();
144 void VCLXAccessibleTabControl::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
146 switch ( rVclWindowEvent.GetId() )
148 case VclEventId::TabpageActivate:
149 case VclEventId::TabpageDeactivate:
151 if ( m_pTabControl )
153 sal_uInt16 nPageId = static_cast<sal_uInt16>(reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData()));
154 sal_uInt16 nPagePos = m_pTabControl->GetPagePos( nPageId );
155 UpdateFocused();
156 UpdateSelected( nPagePos, rVclWindowEvent.GetId() == VclEventId::TabpageActivate );
159 break;
160 case VclEventId::TabpagePageTextChanged:
162 if ( m_pTabControl )
164 sal_uInt16 nPageId = static_cast<sal_uInt16>(reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData()));
165 sal_uInt16 nPagePos = m_pTabControl->GetPagePos( nPageId );
166 UpdatePageText( nPagePos );
169 break;
170 case VclEventId::TabpageInserted:
172 if ( m_pTabControl )
174 sal_uInt16 nPageId = static_cast<sal_uInt16>(reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData()));
175 sal_uInt16 nPagePos = m_pTabControl->GetPagePos( nPageId );
176 InsertChild( nPagePos );
179 break;
180 case VclEventId::TabpageRemoved:
182 if ( m_pTabControl )
184 OExternalLockGuard aGuard( this );
185 sal_uInt16 nPageId = static_cast<sal_uInt16>(reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData()));
186 for ( sal_Int64 i = 0, nCount = m_aAccessibleChildren.size(); i < nCount; ++i )
188 if ( m_aAccessibleChildren[i] && m_aAccessibleChildren[i]->GetPageId() == nPageId )
190 RemoveChild( i );
191 break;
196 break;
197 case VclEventId::TabpageRemovedAll:
199 for ( sal_Int32 i = m_aAccessibleChildren.size() - 1; i >= 0; --i )
200 RemoveChild( i );
202 break;
203 case VclEventId::WindowGetFocus:
204 case VclEventId::WindowLoseFocus:
206 UpdateFocused();
208 break;
209 case VclEventId::ObjectDying:
211 if ( m_pTabControl )
213 m_pTabControl = nullptr;
215 // dispose all tab pages
216 for (const rtl::Reference<VCLXAccessibleTabPage>& i : m_aAccessibleChildren)
218 if (i.is())
219 i->dispose();
221 m_aAccessibleChildren.clear();
224 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
226 break;
227 default:
228 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
233 void VCLXAccessibleTabControl::ProcessWindowChildEvent( const VclWindowEvent& rVclWindowEvent )
235 switch ( rVclWindowEvent.GetId() )
237 case VclEventId::WindowShow:
238 case VclEventId::WindowHide:
240 if ( m_pTabControl )
242 vcl::Window* pChild = static_cast< vcl::Window* >( rVclWindowEvent.GetData() );
243 if ( pChild && pChild->GetType() == WindowType::TABPAGE )
245 for ( sal_Int32 i = 0, nCount = m_pTabControl->GetPageCount(); i < nCount; ++i )
247 sal_uInt16 nPageId = m_pTabControl->GetPageId( static_cast<sal_uInt16>(i) );
248 TabPage* pTabPage = m_pTabControl->GetTabPage( nPageId );
249 if ( pTabPage == static_cast<TabPage*>(pChild) )
250 UpdateTabPage( i, rVclWindowEvent.GetId() == VclEventId::WindowShow );
255 break;
256 default:
257 VCLXAccessibleComponent::ProcessWindowChildEvent( rVclWindowEvent );
262 void VCLXAccessibleTabControl::FillAccessibleStateSet( sal_Int64& rStateSet )
264 VCLXAccessibleComponent::FillAccessibleStateSet( rStateSet );
266 if ( m_pTabControl )
267 rStateSet |= AccessibleStateType::FOCUSABLE;
271 // XComponent
274 void VCLXAccessibleTabControl::disposing()
276 VCLXAccessibleComponent::disposing();
278 if ( !m_pTabControl )
279 return;
281 m_pTabControl = nullptr;
283 // dispose all tab pages
284 for (const rtl::Reference<VCLXAccessibleTabPage>& xComponent : m_aAccessibleChildren)
285 if ( xComponent.is() )
286 xComponent->dispose();
287 m_aAccessibleChildren.clear();
291 // XServiceInfo
294 OUString VCLXAccessibleTabControl::getImplementationName()
296 return u"com.sun.star.comp.toolkit.AccessibleTabControl"_ustr;
300 Sequence< OUString > VCLXAccessibleTabControl::getSupportedServiceNames()
302 return { u"com.sun.star.awt.AccessibleTabControl"_ustr };
306 // XAccessibleContext
309 sal_Int64 VCLXAccessibleTabControl::getAccessibleChildCount()
311 OExternalLockGuard aGuard( this );
313 return m_aAccessibleChildren.size();
317 Reference< XAccessible > VCLXAccessibleTabControl::getAccessibleChild( sal_Int64 i )
319 OExternalLockGuard aGuard( this );
321 if ( i < 0 || o3tl::make_unsigned(i) >= m_aAccessibleChildren.size() )
322 throw IndexOutOfBoundsException();
324 return implGetAccessibleChild( i );
327 Reference< XAccessible > VCLXAccessibleTabControl::implGetAccessibleChild( sal_Int64 i )
329 rtl::Reference< VCLXAccessibleTabPage > xChild = m_aAccessibleChildren[i];
330 if ( !xChild.is() )
332 sal_uInt16 nPageId = m_pTabControl ? m_pTabControl->GetPageId(static_cast<sal_uInt16>(i)) : 0;
333 if (nPageId)
335 xChild = new VCLXAccessibleTabPage( m_pTabControl, nPageId );
337 // insert into tab page list
338 m_aAccessibleChildren[i] = xChild;
342 return xChild;
346 sal_Int16 VCLXAccessibleTabControl::getAccessibleRole( )
348 OExternalLockGuard aGuard( this );
350 return AccessibleRole::PAGE_TAB_LIST;
354 OUString VCLXAccessibleTabControl::getAccessibleName( )
356 OExternalLockGuard aGuard( this );
358 return OUString();
362 // XAccessibleSelection
365 void VCLXAccessibleTabControl::selectAccessibleChild( sal_Int64 nChildIndex )
367 OExternalLockGuard aGuard( this );
369 if ( nChildIndex < 0 || o3tl::make_unsigned(nChildIndex) >= m_aAccessibleChildren.size() )
370 throw IndexOutOfBoundsException();
372 if ( m_pTabControl )
373 m_pTabControl->SelectTabPage( m_pTabControl->GetPageId( static_cast<sal_uInt16>(nChildIndex) ) );
377 sal_Bool VCLXAccessibleTabControl::isAccessibleChildSelected( sal_Int64 nChildIndex )
379 OExternalLockGuard aGuard( this );
381 if ( nChildIndex < 0 || o3tl::make_unsigned(nChildIndex) >= m_aAccessibleChildren.size() )
382 throw IndexOutOfBoundsException();
384 return implIsAccessibleChildSelected( nChildIndex );
387 bool VCLXAccessibleTabControl::implIsAccessibleChildSelected( sal_Int32 nChildIndex )
389 bool bSelected = false;
390 if ( m_pTabControl && m_pTabControl->GetCurPageId() == m_pTabControl->GetPageId( static_cast<sal_uInt16>(nChildIndex) ) )
391 bSelected = true;
393 return bSelected;
397 void VCLXAccessibleTabControl::clearAccessibleSelection( )
399 // This method makes no sense in a tab control, and so does nothing.
403 void VCLXAccessibleTabControl::selectAllAccessibleChildren( )
405 selectAccessibleChild( 0 );
409 sal_Int64 VCLXAccessibleTabControl::getSelectedAccessibleChildCount( )
411 OExternalLockGuard aGuard( this );
413 return 1;
417 Reference< XAccessible > VCLXAccessibleTabControl::getSelectedAccessibleChild( sal_Int64 nSelectedChildIndex )
419 OExternalLockGuard aGuard( this );
421 if ( nSelectedChildIndex != 0 )
422 throw IndexOutOfBoundsException();
424 Reference< XAccessible > xChild;
426 for ( std::size_t i = 0, nCount = m_aAccessibleChildren.size(); i < nCount; i++ )
428 if ( implIsAccessibleChildSelected( i ) )
430 xChild = implGetAccessibleChild( i );
431 break;
435 return xChild;
439 void VCLXAccessibleTabControl::deselectAccessibleChild( sal_Int64 nChildIndex )
441 OExternalLockGuard aGuard( this );
443 if ( nChildIndex < 0 || o3tl::make_unsigned(nChildIndex) >= m_aAccessibleChildren.size() )
444 throw IndexOutOfBoundsException();
446 // This method makes no sense in a tab control, and so does nothing.
450 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */