Branch libreoffice-5-0-4
[LibreOffice.git] / accessibility / source / extended / accessibletabbarpagelist.cxx
blob8ca51866948c026f0c943ba80cb12170c041f440
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/extended/accessibletabbarpagelist.hxx>
21 #include <svtools/tabbar.hxx>
22 #include <accessibility/extended/accessibletabbarpage.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 <cppuhelper/supportsservice.hxx>
27 #include <unotools/accessiblestatesethelper.hxx>
28 #include <unotools/accessiblerelationsethelper.hxx>
29 #include <vcl/svapp.hxx>
30 #include <vcl/settings.hxx>
31 #include <toolkit/helper/convert.hxx>
35 namespace accessibility
39 using namespace ::com::sun::star::accessibility;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::lang;
42 using namespace ::com::sun::star;
43 using namespace ::comphelper;
46 // class AccessibleTabBarPageList
49 AccessibleTabBarPageList::AccessibleTabBarPageList( TabBar* pTabBar, sal_Int32 nIndexInParent )
50 :AccessibleTabBarBase( pTabBar )
51 ,m_nIndexInParent( nIndexInParent )
53 if ( m_pTabBar )
54 m_aAccessibleChildren.assign( m_pTabBar->GetPageCount(), Reference< XAccessible >() );
59 AccessibleTabBarPageList::~AccessibleTabBarPageList()
65 void AccessibleTabBarPageList::UpdateEnabled( sal_Int32 i, bool bEnabled )
67 if ( i >= 0 && i < (sal_Int32)m_aAccessibleChildren.size() )
69 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
70 if ( xChild.is() )
72 AccessibleTabBarPage* pAccessibleTabBarPage = static_cast< AccessibleTabBarPage* >( xChild.get() );
73 if ( pAccessibleTabBarPage )
74 pAccessibleTabBarPage->SetEnabled( bEnabled );
81 void AccessibleTabBarPageList::UpdateShowing( bool bShowing )
83 for ( sal_uInt32 i = 0; i < m_aAccessibleChildren.size(); ++i )
85 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
86 if ( xChild.is() )
88 AccessibleTabBarPage* pAccessibleTabBarPage = static_cast< AccessibleTabBarPage* >( xChild.get() );
89 if ( pAccessibleTabBarPage )
90 pAccessibleTabBarPage->SetShowing( bShowing );
97 void AccessibleTabBarPageList::UpdateSelected( sal_Int32 i, bool bSelected )
99 NotifyAccessibleEvent( AccessibleEventId::SELECTION_CHANGED, Any(), Any() );
101 if ( i >= 0 && i < (sal_Int32)m_aAccessibleChildren.size() )
103 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
104 if ( xChild.is() )
106 AccessibleTabBarPage* pAccessibleTabBarPage = static_cast< AccessibleTabBarPage* >( xChild.get() );
107 if ( pAccessibleTabBarPage )
108 pAccessibleTabBarPage->SetSelected( bSelected );
115 void AccessibleTabBarPageList::UpdatePageText( sal_Int32 i )
117 if ( i >= 0 && i < (sal_Int32)m_aAccessibleChildren.size() )
119 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
120 if ( xChild.is() )
122 AccessibleTabBarPage* pAccessibleTabBarPage = static_cast< AccessibleTabBarPage* >( xChild.get() );
123 if ( pAccessibleTabBarPage )
125 if ( m_pTabBar )
127 OUString sPageText = m_pTabBar->GetPageText( m_pTabBar->GetPageId( (sal_uInt16)i ) );
128 pAccessibleTabBarPage->SetPageText( sPageText );
137 void AccessibleTabBarPageList::InsertChild( sal_Int32 i )
139 if ( i >= 0 && i <= (sal_Int32)m_aAccessibleChildren.size() )
141 // insert entry in child list
142 m_aAccessibleChildren.insert( m_aAccessibleChildren.begin() + i, Reference< XAccessible >() );
144 // send accessible child event
145 Reference< XAccessible > xChild( getAccessibleChild( i ) );
146 if ( xChild.is() )
148 Any aOldValue, aNewValue;
149 aNewValue <<= xChild;
150 NotifyAccessibleEvent( AccessibleEventId::CHILD, aOldValue, aNewValue );
157 void AccessibleTabBarPageList::RemoveChild( sal_Int32 i )
159 if ( i >= 0 && i < (sal_Int32)m_aAccessibleChildren.size() )
161 // get the accessible of the removed page
162 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
164 // remove entry in child list
165 m_aAccessibleChildren.erase( m_aAccessibleChildren.begin() + i );
167 // send accessible child event
168 if ( xChild.is() )
170 Any aOldValue, aNewValue;
171 aOldValue <<= xChild;
172 NotifyAccessibleEvent( AccessibleEventId::CHILD, aOldValue, aNewValue );
174 Reference< XComponent > xComponent( xChild, UNO_QUERY );
175 if ( xComponent.is() )
176 xComponent->dispose();
183 void AccessibleTabBarPageList::MoveChild( sal_Int32 i, sal_Int32 j )
185 if ( i >= 0 && i < (sal_Int32)m_aAccessibleChildren.size() &&
186 j >= 0 && j <= (sal_Int32)m_aAccessibleChildren.size() )
188 if ( i < j )
189 --j;
191 // get the accessible of the moved page
192 Reference< XAccessible > xChild( m_aAccessibleChildren[i] );
194 // remove entry in child list at old position
195 m_aAccessibleChildren.erase( m_aAccessibleChildren.begin() + i );
197 // insert entry in child list at new position
198 m_aAccessibleChildren.insert( m_aAccessibleChildren.begin() + j, xChild );
204 void AccessibleTabBarPageList::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
206 switch ( rVclWindowEvent.GetId() )
208 case VCLEVENT_WINDOW_ENABLED:
210 Any aNewValue;
211 aNewValue <<= AccessibleStateType::SENSITIVE;
212 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, Any(), aNewValue );
213 aNewValue <<= AccessibleStateType::ENABLED;
214 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, Any(), aNewValue );
216 break;
217 case VCLEVENT_WINDOW_DISABLED:
219 Any aOldValue;
220 aOldValue <<= AccessibleStateType::ENABLED;
221 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, Any() );
222 aOldValue <<= AccessibleStateType::SENSITIVE;
223 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, Any() );
225 break;
226 case VCLEVENT_WINDOW_SHOW:
228 Any aOldValue, aNewValue;
229 aNewValue <<= AccessibleStateType::SHOWING;
230 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
231 UpdateShowing( true );
233 break;
234 case VCLEVENT_WINDOW_HIDE:
236 Any aOldValue, aNewValue;
237 aOldValue <<= AccessibleStateType::SHOWING;
238 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
239 UpdateShowing( false );
241 break;
242 case VCLEVENT_TABBAR_PAGEENABLED:
244 if ( m_pTabBar )
246 sal_uInt16 nPageId = (sal_uInt16)reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData());
247 sal_uInt16 nPagePos = m_pTabBar->GetPagePos( nPageId );
248 UpdateEnabled( nPagePos, true );
251 break;
252 case VCLEVENT_TABBAR_PAGEDISABLED:
254 if ( m_pTabBar )
256 sal_uInt16 nPageId = (sal_uInt16)reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData());
257 sal_uInt16 nPagePos = m_pTabBar->GetPagePos( nPageId );
258 UpdateEnabled( nPagePos, false );
261 break;
262 case VCLEVENT_TABBAR_PAGESELECTED:
264 // do nothing
266 break;
267 case VCLEVENT_TABBAR_PAGEACTIVATED:
269 if ( m_pTabBar )
271 sal_uInt16 nPageId = (sal_uInt16)reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData());
272 sal_uInt16 nPagePos = m_pTabBar->GetPagePos( nPageId );
273 UpdateSelected( nPagePos, true );
276 break;
277 case VCLEVENT_TABBAR_PAGEDEACTIVATED:
279 if ( m_pTabBar )
281 sal_uInt16 nPageId = (sal_uInt16)reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData());
282 sal_uInt16 nPagePos = m_pTabBar->GetPagePos( nPageId );
283 UpdateSelected( nPagePos, false );
286 break;
287 case VCLEVENT_TABBAR_PAGEINSERTED:
289 if ( m_pTabBar )
291 sal_uInt16 nPageId = (sal_uInt16)reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData());
292 sal_uInt16 nPagePos = m_pTabBar->GetPagePos( nPageId );
293 InsertChild( nPagePos );
296 break;
297 case VCLEVENT_TABBAR_PAGEREMOVED:
299 if ( m_pTabBar )
301 sal_uInt16 nPageId = (sal_uInt16)reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData());
303 if ( nPageId == TabBar::PAGE_NOT_FOUND )
305 for ( sal_Int32 i = m_aAccessibleChildren.size() - 1; i >= 0; --i )
306 RemoveChild( i );
308 else
310 for ( sal_Int32 i = 0, nCount = getAccessibleChildCount(); i < nCount; ++i )
312 Reference< XAccessible > xChild( getAccessibleChild( i ) );
313 if ( xChild.is() )
315 AccessibleTabBarPage* pAccessibleTabBarPage = static_cast< AccessibleTabBarPage* >( xChild.get() );
316 if ( pAccessibleTabBarPage && pAccessibleTabBarPage->GetPageId() == nPageId )
318 RemoveChild( i );
319 break;
326 break;
327 case VCLEVENT_TABBAR_PAGEMOVED:
329 Pair* pPair = static_cast<Pair*>(rVclWindowEvent.GetData());
330 if ( pPair )
331 MoveChild( pPair->A(), pPair->B() );
333 break;
334 case VCLEVENT_TABBAR_PAGETEXTCHANGED:
336 sal_uInt16 nPageId = (sal_uInt16)reinterpret_cast<sal_IntPtr>(rVclWindowEvent.GetData());
337 sal_uInt16 nPagePos = m_pTabBar->GetPagePos( nPageId );
338 UpdatePageText( nPagePos );
340 break;
341 default:
343 AccessibleTabBarBase::ProcessWindowEvent( rVclWindowEvent );
345 break;
351 void AccessibleTabBarPageList::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
353 if ( m_pTabBar )
355 if ( m_pTabBar->IsEnabled() )
357 rStateSet.AddState( AccessibleStateType::ENABLED );
358 rStateSet.AddState( AccessibleStateType::SENSITIVE );
361 rStateSet.AddState( AccessibleStateType::VISIBLE );
363 if ( m_pTabBar->IsVisible() )
364 rStateSet.AddState( AccessibleStateType::SHOWING );
369 // OCommonAccessibleComponent
372 awt::Rectangle AccessibleTabBarPageList::implGetBounds() throw (RuntimeException)
374 awt::Rectangle aBounds;
375 if ( m_pTabBar )
376 aBounds = AWTRectangle( m_pTabBar->GetPageArea() );
378 return aBounds;
382 // XInterface
385 IMPLEMENT_FORWARD_XINTERFACE2( AccessibleTabBarPageList, AccessibleExtendedComponentHelper_BASE, AccessibleTabBarPageList_BASE )
388 // XTypeProvider
391 IMPLEMENT_FORWARD_XTYPEPROVIDER2( AccessibleTabBarPageList, AccessibleExtendedComponentHelper_BASE, AccessibleTabBarPageList_BASE )
394 // XComponent
397 void AccessibleTabBarPageList::disposing()
399 AccessibleTabBarBase::disposing();
401 // dispose all children
402 for ( sal_uInt32 i = 0; i < m_aAccessibleChildren.size(); ++i )
404 Reference< XComponent > xComponent( m_aAccessibleChildren[i], UNO_QUERY );
405 if ( xComponent.is() )
406 xComponent->dispose();
408 m_aAccessibleChildren.clear();
412 // XServiceInfo
415 OUString AccessibleTabBarPageList::getImplementationName() throw (RuntimeException, std::exception)
417 return OUString( "com.sun.star.comp.svtools.AccessibleTabBarPageList" );
422 sal_Bool AccessibleTabBarPageList::supportsService( const OUString& rServiceName ) throw (RuntimeException, std::exception)
424 return cppu::supportsService(this, rServiceName);
429 Sequence< OUString > AccessibleTabBarPageList::getSupportedServiceNames() throw (RuntimeException, std::exception)
431 Sequence< OUString > aNames(1);
432 aNames[0] = "com.sun.star.awt.AccessibleTabBarPageList";
433 return aNames;
437 // XAccessible
440 Reference< XAccessibleContext > AccessibleTabBarPageList::getAccessibleContext( ) throw (RuntimeException, std::exception)
442 OExternalLockGuard aGuard( this );
444 return this;
448 // XAccessibleContext
451 sal_Int32 AccessibleTabBarPageList::getAccessibleChildCount() throw (RuntimeException, std::exception)
453 OExternalLockGuard aGuard( this );
455 return m_aAccessibleChildren.size();
460 Reference< XAccessible > AccessibleTabBarPageList::getAccessibleChild( sal_Int32 i ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
462 OExternalLockGuard aGuard( this );
464 if ( i < 0 || i >= getAccessibleChildCount() )
465 throw IndexOutOfBoundsException();
467 Reference< XAccessible > xChild = m_aAccessibleChildren[i];
468 if ( !xChild.is() )
470 if ( m_pTabBar )
472 sal_uInt16 nPageId = m_pTabBar->GetPageId( (sal_uInt16)i );
474 xChild = new AccessibleTabBarPage( m_pTabBar, nPageId, this );
476 // insert into child list
477 m_aAccessibleChildren[i] = xChild;
481 return xChild;
486 Reference< XAccessible > AccessibleTabBarPageList::getAccessibleParent( ) throw (RuntimeException, std::exception)
488 OExternalLockGuard aGuard( this );
490 Reference< XAccessible > xParent;
491 if ( m_pTabBar )
492 xParent = m_pTabBar->GetAccessible();
494 return xParent;
499 sal_Int32 AccessibleTabBarPageList::getAccessibleIndexInParent( ) throw (RuntimeException, std::exception)
501 OExternalLockGuard aGuard( this );
503 return m_nIndexInParent;
508 sal_Int16 AccessibleTabBarPageList::getAccessibleRole( ) throw (RuntimeException, std::exception)
510 OExternalLockGuard aGuard( this );
512 return AccessibleRole::PAGE_TAB_LIST;
517 OUString AccessibleTabBarPageList::getAccessibleDescription( ) throw (RuntimeException, std::exception)
519 OExternalLockGuard aGuard( this );
521 return OUString();
526 OUString AccessibleTabBarPageList::getAccessibleName( ) throw (RuntimeException, std::exception)
528 OExternalLockGuard aGuard( this );
530 return OUString();
535 Reference< XAccessibleRelationSet > AccessibleTabBarPageList::getAccessibleRelationSet( ) throw (RuntimeException, std::exception)
537 OExternalLockGuard aGuard( this );
539 utl::AccessibleRelationSetHelper* pRelationSetHelper = new utl::AccessibleRelationSetHelper;
540 Reference< XAccessibleRelationSet > xSet = pRelationSetHelper;
541 return xSet;
546 Reference< XAccessibleStateSet > AccessibleTabBarPageList::getAccessibleStateSet( ) throw (RuntimeException, std::exception)
548 OExternalLockGuard aGuard( this );
550 utl::AccessibleStateSetHelper* pStateSetHelper = new utl::AccessibleStateSetHelper;
551 Reference< XAccessibleStateSet > xSet = pStateSetHelper;
553 if ( !rBHelper.bDisposed && !rBHelper.bInDispose )
555 FillAccessibleStateSet( *pStateSetHelper );
557 else
559 pStateSetHelper->AddState( AccessibleStateType::DEFUNC );
562 return xSet;
567 Locale AccessibleTabBarPageList::getLocale( ) throw (IllegalAccessibleComponentStateException, RuntimeException, std::exception)
569 OExternalLockGuard aGuard( this );
571 return Application::GetSettings().GetLanguageTag().getLocale();
575 // XAccessibleComponent
578 Reference< XAccessible > AccessibleTabBarPageList::getAccessibleAtPoint( const awt::Point& rPoint ) throw (RuntimeException, std::exception)
580 OExternalLockGuard aGuard( this );
582 Reference< XAccessible > xChild;
583 for ( sal_uInt32 i = 0; i < m_aAccessibleChildren.size(); ++i )
585 Reference< XAccessible > xAcc = getAccessibleChild( i );
586 if ( xAcc.is() )
588 Reference< XAccessibleComponent > xComp( xAcc->getAccessibleContext(), UNO_QUERY );
589 if ( xComp.is() )
591 Rectangle aRect = VCLRectangle( xComp->getBounds() );
592 Point aPos = VCLPoint( rPoint );
593 if ( aRect.IsInside( aPos ) )
595 xChild = xAcc;
596 break;
602 return xChild;
607 void AccessibleTabBarPageList::grabFocus( ) throw (RuntimeException, std::exception)
609 // no focus
614 sal_Int32 AccessibleTabBarPageList::getForeground( ) throw (RuntimeException, std::exception)
616 OExternalLockGuard aGuard( this );
618 sal_Int32 nColor = 0;
619 Reference< XAccessible > xParent = getAccessibleParent();
620 if ( xParent.is() )
622 Reference< XAccessibleComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
623 if ( xParentComp.is() )
624 nColor = xParentComp->getForeground();
627 return nColor;
632 sal_Int32 AccessibleTabBarPageList::getBackground( ) throw (RuntimeException, std::exception)
634 OExternalLockGuard aGuard( this );
636 sal_Int32 nColor = 0;
637 Reference< XAccessible > xParent = getAccessibleParent();
638 if ( xParent.is() )
640 Reference< XAccessibleComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
641 if ( xParentComp.is() )
642 nColor = xParentComp->getBackground();
645 return nColor;
649 // XAccessibleExtendedComponent
652 Reference< awt::XFont > AccessibleTabBarPageList::getFont( ) throw (RuntimeException, std::exception)
654 OExternalLockGuard aGuard( this );
656 Reference< awt::XFont > xFont;
657 Reference< XAccessible > xParent = getAccessibleParent();
658 if ( xParent.is() )
660 Reference< XAccessibleExtendedComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
661 if ( xParentComp.is() )
662 xFont = xParentComp->getFont();
665 return xFont;
670 OUString AccessibleTabBarPageList::getTitledBorderText( ) throw (RuntimeException, std::exception)
672 OExternalLockGuard aGuard( this );
674 return OUString();
679 OUString AccessibleTabBarPageList::getToolTipText( ) throw (RuntimeException, std::exception)
681 OExternalLockGuard aGuard( this );
683 return OUString();
687 // XAccessibleSelection
690 void AccessibleTabBarPageList::selectAccessibleChild( sal_Int32 nChildIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
692 OExternalLockGuard aGuard( this );
694 if ( nChildIndex < 0 || nChildIndex >= getAccessibleChildCount() )
695 throw IndexOutOfBoundsException();
697 if ( m_pTabBar )
699 m_pTabBar->SetCurPageId( m_pTabBar->GetPageId( (sal_uInt16)nChildIndex ) );
700 m_pTabBar->Update();
701 m_pTabBar->ActivatePage();
702 m_pTabBar->Select();
708 sal_Bool AccessibleTabBarPageList::isAccessibleChildSelected( sal_Int32 nChildIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
710 OExternalLockGuard aGuard( this );
712 if ( nChildIndex < 0 || nChildIndex >= getAccessibleChildCount() )
713 throw IndexOutOfBoundsException();
715 bool bSelected = false;
716 if ( m_pTabBar && m_pTabBar->GetCurPageId() == m_pTabBar->GetPageId( (sal_uInt16)nChildIndex ) )
717 bSelected = true;
719 return bSelected;
724 void AccessibleTabBarPageList::clearAccessibleSelection( ) throw (RuntimeException, std::exception)
726 // This method makes no sense in a TabBar, and so does nothing.
731 void AccessibleTabBarPageList::selectAllAccessibleChildren( ) throw (RuntimeException, std::exception)
733 OExternalLockGuard aGuard( this );
735 selectAccessibleChild( 0 );
740 sal_Int32 AccessibleTabBarPageList::getSelectedAccessibleChildCount( ) throw (RuntimeException, std::exception)
742 OExternalLockGuard aGuard( this );
744 return 1;
749 Reference< XAccessible > AccessibleTabBarPageList::getSelectedAccessibleChild( sal_Int32 nSelectedChildIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
751 OExternalLockGuard aGuard( this );
753 if ( nSelectedChildIndex < 0 || nSelectedChildIndex >= getSelectedAccessibleChildCount() )
754 throw IndexOutOfBoundsException();
756 Reference< XAccessible > xChild;
758 for ( sal_Int32 i = 0, j = 0, nCount = getAccessibleChildCount(); i < nCount; i++ )
760 if ( isAccessibleChildSelected( i ) && ( j++ == nSelectedChildIndex ) )
762 xChild = getAccessibleChild( i );
763 break;
767 return xChild;
772 void AccessibleTabBarPageList::deselectAccessibleChild( sal_Int32 nChildIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
774 OExternalLockGuard aGuard( this );
776 if ( nChildIndex < 0 || nChildIndex >= getAccessibleChildCount() )
777 throw IndexOutOfBoundsException();
779 // This method makes no sense in a TabBar, and so does nothing.
785 } // namespace accessibility
788 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */