1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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() );
49 if (m_pTabControl
->isDisposed())
51 m_pTabControl
.clear();
54 m_aAccessibleChildren
.assign( m_pTabControl
->GetPageCount(), Reference
< XAccessible
>() );
58 void VCLXAccessibleTabControl::UpdateFocused()
60 for (const Reference
<XAccessible
>& xChild
: m_aAccessibleChildren
)
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
] );
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
] );
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
] );
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
) );
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
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
:
170 sal_uInt16 nPageId
= static_cast<sal_uInt16
>(reinterpret_cast<sal_IntPtr
>(rVclWindowEvent
.GetData()));
171 sal_uInt16 nPagePos
= m_pTabControl
->GetPagePos( nPageId
);
173 UpdateSelected( nPagePos
, rVclWindowEvent
.GetId() == VclEventId::TabpageActivate
);
177 case VclEventId::TabpagePageTextChanged
:
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
);
187 case VclEventId::TabpageInserted
:
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
);
197 case VclEventId::TabpageRemoved
:
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
) );
207 VCLXAccessibleTabPage
* pVCLXAccessibleTabPage
= static_cast< VCLXAccessibleTabPage
* >( xChild
.get() );
208 if ( pVCLXAccessibleTabPage
&& pVCLXAccessibleTabPage
->GetPageId() == nPageId
)
218 case VclEventId::TabpageRemovedAll
:
220 for ( sal_Int32 i
= m_aAccessibleChildren
.size() - 1; i
>= 0; --i
)
224 case VclEventId::WindowGetFocus
:
225 case VclEventId::WindowLoseFocus
:
230 case VclEventId::ObjectDying
:
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
);
250 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent
);
255 void VCLXAccessibleTabControl::ProcessWindowChildEvent( const VclWindowEvent
& rVclWindowEvent
)
257 switch ( rVclWindowEvent
.GetId() )
259 case VclEventId::WindowShow
:
260 case VclEventId::WindowHide
:
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
);
279 VCLXAccessibleComponent::ProcessWindowChildEvent( rVclWindowEvent
);
284 void VCLXAccessibleTabControl::FillAccessibleStateSet( utl::AccessibleStateSetHelper
& rStateSet
)
286 VCLXAccessibleComponent::FillAccessibleStateSet( rStateSet
);
289 rStateSet
.AddState( AccessibleStateType::FOCUSABLE
);
296 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleTabControl
, VCLXAccessibleComponent
, VCLXAccessibleTabControl_BASE
)
302 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleTabControl
, VCLXAccessibleComponent
, VCLXAccessibleTabControl_BASE
)
308 void VCLXAccessibleTabControl::disposing()
310 VCLXAccessibleComponent::disposing();
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();
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
];
369 sal_uInt16 nPageId
= m_pTabControl
? m_pTabControl
->GetPageId(static_cast<sal_uInt16
>(i
)) : 0;
372 xChild
= new VCLXAccessibleTabPage( m_pTabControl
, nPageId
);
374 // insert into tab page list
375 m_aAccessibleChildren
[i
] = xChild
;
383 sal_Int16
VCLXAccessibleTabControl::getAccessibleRole( )
385 OExternalLockGuard
aGuard( this );
387 return AccessibleRole::PAGE_TAB_LIST
;
391 OUString
VCLXAccessibleTabControl::getAccessibleName( )
393 OExternalLockGuard
aGuard( this );
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();
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
) ) )
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 );
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
);
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: */