bump product version to 4.1.6.2
[LibreOffice.git] / accessibility / source / extended / AccessibleToolPanelDeckTabBar.cxx
blob8ff90ea0cf588e1287b1cbec479ac2543cca9af3
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 .
21 #include "accessibility/extended/AccessibleToolPanelDeckTabBar.hxx"
22 #include "accessibility/extended/AccessibleToolPanelDeckTabBarItem.hxx"
23 #include "accessibility/helper/accresmgr.hxx"
24 #include "accessibility/helper/accessiblestrings.hrc"
26 #include <com/sun/star/accessibility/AccessibleRole.hpp>
27 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
28 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
29 #include <com/sun/star/lang/DisposedException.hpp>
31 #include <svtools/toolpanel/toolpaneldeck.hxx>
32 #include <svtools/toolpanel/paneltabbar.hxx>
33 #include <unotools/accessiblestatesethelper.hxx>
34 #include <toolkit/awt/vclxwindow.hxx>
35 #include <toolkit/helper/vclunohelper.hxx>
36 #include <vcl/svapp.hxx>
37 #include <vcl/button.hxx>
38 #include <osl/mutex.hxx>
39 #include <tools/diagnose_ex.h>
41 #include <vector>
43 //......................................................................................................................
44 namespace accessibility
46 //......................................................................................................................
48 using ::com::sun::star::uno::Reference;
49 using ::com::sun::star::uno::XInterface;
50 using ::com::sun::star::uno::UNO_QUERY;
51 using ::com::sun::star::uno::UNO_QUERY_THROW;
52 using ::com::sun::star::uno::UNO_SET_THROW;
53 using ::com::sun::star::uno::Exception;
54 using ::com::sun::star::uno::RuntimeException;
55 using ::com::sun::star::uno::Any;
56 using ::com::sun::star::uno::makeAny;
57 using ::com::sun::star::uno::Sequence;
58 using ::com::sun::star::uno::Type;
59 using ::com::sun::star::accessibility::XAccessible;
60 using ::com::sun::star::lang::DisposedException;
61 using ::com::sun::star::lang::IndexOutOfBoundsException;
62 using ::com::sun::star::accessibility::XAccessibleContext;
64 namespace AccessibleRole = ::com::sun::star::accessibility::AccessibleRole;
65 namespace AccessibleEventId = ::com::sun::star::accessibility::AccessibleEventId;
66 namespace AccessibleStateType = ::com::sun::star::accessibility::AccessibleStateType;
68 typedef ::com::sun::star::awt::Point UnoPoint;
70 //==================================================================================================================
71 //= AccessibleWrapper
72 //==================================================================================================================
73 typedef ::cppu::WeakImplHelper1< XAccessible > AccessibleWrapper_Base;
74 class AccessibleWrapper : public AccessibleWrapper_Base
76 public:
77 AccessibleWrapper( const Reference< XAccessibleContext >& i_rContext )
78 :m_xContext( i_rContext )
82 // XAccessible
83 virtual Reference< XAccessibleContext > SAL_CALL getAccessibleContext( ) throw (RuntimeException)
85 return m_xContext;
88 private:
89 const Reference< XAccessibleContext > m_xContext;
92 //==================================================================================================================
93 //= AccessibleToolPanelTabBar_Impl
94 //==================================================================================================================
95 class AccessibleToolPanelTabBar_Impl :public ::boost::noncopyable
96 ,public ::svt::IToolPanelDeckListener
98 public:
99 AccessibleToolPanelTabBar_Impl(
100 AccessibleToolPanelTabBar& i_rAntiImpl,
101 const Reference< XAccessible >& i_rAccessibleParent,
102 ::svt::IToolPanelDeck& i_rPanelDeck,
103 ::svt::PanelTabBar& i_rTabBar
105 virtual ~AccessibleToolPanelTabBar_Impl();
107 void checkDisposed();
108 bool isDisposed() const { return m_pPanelDeck == NULL; }
109 void dispose();
111 ::svt::IToolPanelDeck* getPanelDeck() const { return m_pPanelDeck; }
112 ::svt::PanelTabBar* getTabBar() const { return m_pTabBar; }
113 const Reference< XAccessible >& getAccessibleParent() const { return m_xAccessibleParent; }
114 Reference< XAccessible > getAccessiblePanelItem( size_t i_nPosition );
115 Reference< XAccessible > getOwnAccessible() const;
117 protected:
118 // IToolPanelDeckListener
119 virtual void PanelInserted( const ::svt::PToolPanel& i_pPanel, const size_t i_nPosition );
120 virtual void PanelRemoved( const size_t i_nPosition );
121 virtual void ActivePanelChanged( const ::boost::optional< size_t >& i_rOldActive, const ::boost::optional< size_t >& i_rNewActive );
122 virtual void LayouterChanged( const ::svt::PDeckLayouter& i_rNewLayouter );
123 virtual void Dying();
125 DECL_LINK( OnWindowEvent, const VclSimpleEvent* );
127 private:
128 AccessibleToolPanelTabBar& m_rAntiImpl;
129 Reference< XAccessible > m_xAccessibleParent;
130 ::svt::IToolPanelDeck* m_pPanelDeck;
131 ::svt::PanelTabBar* m_pTabBar;
132 ::std::vector< Reference< XAccessible > > m_aChildren;
135 //------------------------------------------------------------------------------------------------------------------
136 AccessibleToolPanelTabBar_Impl::AccessibleToolPanelTabBar_Impl( AccessibleToolPanelTabBar& i_rAntiImpl,
137 const Reference< XAccessible >& i_rAccessibleParent, ::svt::IToolPanelDeck& i_rPanelDeck, ::svt::PanelTabBar& i_rTabBar )
138 :m_rAntiImpl( i_rAntiImpl )
139 ,m_xAccessibleParent( i_rAccessibleParent )
140 ,m_pPanelDeck( &i_rPanelDeck )
141 ,m_pTabBar( &i_rTabBar )
142 ,m_aChildren()
144 m_pPanelDeck->AddListener( *this );
145 m_aChildren.resize( m_pPanelDeck->GetPanelCount() );
147 const String sAccessibleDescription( TK_RES_STRING( RID_STR_ACC_DESC_PANELDECL_TABBAR ) );
148 i_rTabBar.SetAccessibleName( sAccessibleDescription );
149 i_rTabBar.SetAccessibleDescription( sAccessibleDescription );
151 i_rTabBar.GetScrollButton( true ).AddEventListener( LINK( this, AccessibleToolPanelTabBar_Impl, OnWindowEvent ) );
152 i_rTabBar.GetScrollButton( false ).AddEventListener( LINK( this, AccessibleToolPanelTabBar_Impl, OnWindowEvent ) );
155 //------------------------------------------------------------------------------------------------------------------
156 void AccessibleToolPanelTabBar_Impl::checkDisposed()
158 if ( isDisposed() )
159 throw DisposedException( OUString(), *&m_rAntiImpl );
162 //------------------------------------------------------------------------------------------------------------------
163 AccessibleToolPanelTabBar_Impl::~AccessibleToolPanelTabBar_Impl()
165 if ( !isDisposed() )
166 dispose();
169 //------------------------------------------------------------------------------------------------------------------
170 void AccessibleToolPanelTabBar_Impl::dispose()
172 ENSURE_OR_RETURN_VOID( !isDisposed(), "disposed twice" );
173 m_pPanelDeck->RemoveListener( *this );
174 m_pPanelDeck = NULL;
176 m_pTabBar->GetScrollButton( true ).RemoveEventListener( LINK( this, AccessibleToolPanelTabBar_Impl, OnWindowEvent ) );
177 m_pTabBar->GetScrollButton( false ).RemoveEventListener( LINK( this, AccessibleToolPanelTabBar_Impl, OnWindowEvent ) );
178 m_pTabBar = NULL;
180 m_xAccessibleParent.clear();
183 //------------------------------------------------------------------------------------------------------------------
184 Reference< XAccessible > AccessibleToolPanelTabBar_Impl::getAccessiblePanelItem( size_t i_nPosition )
186 ENSURE_OR_RETURN( !isDisposed(), "AccessibleToolPanelTabBar_Impl::getAccessiblePanelItem: already disposed!", NULL );
187 ENSURE_OR_RETURN( i_nPosition < m_aChildren.size(), "AccessibleToolPanelTabBar_Impl::getAccessiblePanelItem: invalid index!", NULL );
189 Reference< XAccessible >& rAccessibleChild( m_aChildren[ i_nPosition ] );
190 if ( !rAccessibleChild.is() )
192 ::rtl::Reference< AccessibleToolPanelDeckTabBarItem > pAccesibleItemContext( new AccessibleToolPanelDeckTabBarItem(
193 getOwnAccessible(), *m_pPanelDeck, *m_pTabBar, i_nPosition ) );
194 rAccessibleChild.set( new AccessibleWrapper( pAccesibleItemContext.get() ) );
195 pAccesibleItemContext->lateInit( rAccessibleChild );
197 return rAccessibleChild;
200 //------------------------------------------------------------------------------------------------------------------
201 Reference< XAccessible > AccessibleToolPanelTabBar_Impl::getOwnAccessible() const
203 Reference< XAccessible > xOwnAccessible( static_cast< XAccessible* >( m_rAntiImpl.GetVCLXWindow() ) );
204 OSL_ENSURE( xOwnAccessible->getAccessibleContext() == Reference< XAccessibleContext >( &m_rAntiImpl ),
205 "AccessibleToolPanelTabBar_Impl::getOwnAccessible: could not retrieve proper XAccessible for /myself!" );
206 return xOwnAccessible;
209 //------------------------------------------------------------------------------------------------------------------
210 void AccessibleToolPanelTabBar_Impl::PanelInserted( const ::svt::PToolPanel& i_pPanel, const size_t i_nPosition )
212 ENSURE_OR_RETURN_VOID( i_nPosition <= m_aChildren.size(), "AccessibleToolPanelTabBar_Impl::PanelInserted: illegal position (or invalid cache!)" );
213 (void)i_pPanel;
214 m_aChildren.insert( m_aChildren.begin() + i_nPosition, (Reference< XAccessible >)NULL );
215 m_rAntiImpl.NotifyAccessibleEvent( AccessibleEventId::CHILD, Any(), makeAny( getAccessiblePanelItem( i_nPosition ) ) );
218 //------------------------------------------------------------------------------------------------------------------
219 void AccessibleToolPanelTabBar_Impl::PanelRemoved( const size_t i_nPosition )
221 ENSURE_OR_RETURN_VOID( i_nPosition < m_aChildren.size(), "AccessibleToolPanelTabBar_Impl::PanelInserted: illegal position (or invalid cache!)" );
223 const Reference< XAccessible > xOldChild( getAccessiblePanelItem( i_nPosition ) );
224 m_aChildren.erase( m_aChildren.begin() + i_nPosition );
225 m_rAntiImpl.NotifyAccessibleEvent( AccessibleEventId::CHILD, makeAny( xOldChild ), Any() );
228 //------------------------------------------------------------------------------------------------------------------
229 void AccessibleToolPanelTabBar_Impl::ActivePanelChanged( const ::boost::optional< size_t >& i_rOldActive, const ::boost::optional< size_t >& i_rNewActive )
231 (void)i_rOldActive;
232 (void)i_rNewActive;
235 //------------------------------------------------------------------------------------------------------------------
236 void AccessibleToolPanelTabBar_Impl::LayouterChanged( const ::svt::PDeckLayouter& i_rNewLayouter )
238 (void)i_rNewLayouter;
239 m_rAntiImpl.dispose();
242 //------------------------------------------------------------------------------------------------------------------
243 void AccessibleToolPanelTabBar_Impl::Dying()
245 m_rAntiImpl.dispose();
248 //------------------------------------------------------------------------------------------------------------------
249 IMPL_LINK( AccessibleToolPanelTabBar_Impl, OnWindowEvent, const VclSimpleEvent*, i_pEvent )
251 ENSURE_OR_RETURN( !isDisposed(), "AccessibleToolPanelTabBar_Impl::OnWindowEvent: already disposed!", 0L );
253 const VclWindowEvent* pWindowEvent( dynamic_cast< const VclWindowEvent* >( i_pEvent ) );
254 if ( !pWindowEvent )
255 return 0L;
257 const bool bForwardButton = ( pWindowEvent->GetWindow() == &m_pTabBar->GetScrollButton( true ) );
258 const bool bBackwardButton = ( pWindowEvent->GetWindow() == &m_pTabBar->GetScrollButton( false ) );
259 ENSURE_OR_RETURN( bForwardButton || bBackwardButton, "AccessibleToolPanelTabBar_Impl::OnWindowEvent: where does this come from?", 0L );
261 const bool bShow = ( i_pEvent->GetId() == VCLEVENT_WINDOW_SHOW );
262 const bool bHide = ( i_pEvent->GetId() == VCLEVENT_WINDOW_HIDE );
263 if ( !bShow && !bHide )
264 // not interested in events other than visibility changes
265 return 0L;
267 const Reference< XAccessible > xButtonAccessible( m_pTabBar->GetScrollButton( bForwardButton ).GetAccessible() );
268 const Any aOldChild( bHide ? xButtonAccessible : Reference< XAccessible >() );
269 const Any aNewChild( bShow ? xButtonAccessible : Reference< XAccessible >() );
270 m_rAntiImpl.NotifyAccessibleEvent( AccessibleEventId::CHILD, aOldChild, aNewChild );
272 return 1L;
275 //==================================================================================================================
276 //= MethodGuard
277 //==================================================================================================================
278 namespace
280 class MethodGuard
282 public:
283 MethodGuard( AccessibleToolPanelTabBar_Impl& i_rImpl )
284 :m_aGuard()
286 i_rImpl.checkDisposed();
288 ~MethodGuard()
292 private:
293 SolarMutexGuard m_aGuard;
297 //==================================================================================================================
298 //= AccessibleToolPanelTabBar
299 //==================================================================================================================
300 //------------------------------------------------------------------------------------------------------------------
301 AccessibleToolPanelTabBar::AccessibleToolPanelTabBar( const Reference< XAccessible >& i_rAccessibleParent,
302 ::svt::IToolPanelDeck& i_rPanelDeck, ::svt::PanelTabBar& i_rTabBar )
303 :AccessibleToolPanelTabBar_Base( i_rTabBar.GetWindowPeer() )
304 ,m_pImpl( new AccessibleToolPanelTabBar_Impl( *this, i_rAccessibleParent, i_rPanelDeck, i_rTabBar ) )
308 //------------------------------------------------------------------------------------------------------------------
309 AccessibleToolPanelTabBar::~AccessibleToolPanelTabBar()
313 //------------------------------------------------------------------------------------------------------------------
314 sal_Int32 SAL_CALL AccessibleToolPanelTabBar::getAccessibleChildCount( ) throw (RuntimeException)
316 MethodGuard aGuard( *m_pImpl );
318 const bool bHasScrollBack = m_pImpl->getTabBar()->GetScrollButton( false ).IsVisible();
319 const bool bHasScrollForward = m_pImpl->getTabBar()->GetScrollButton( true ).IsVisible();
321 return m_pImpl->getPanelDeck()->GetPanelCount()
322 + ( bHasScrollBack ? 1 : 0 )
323 + ( bHasScrollForward ? 1 : 0 );
326 //------------------------------------------------------------------------------------------------------------------
327 Reference< XAccessible > SAL_CALL AccessibleToolPanelTabBar::getAccessibleChild( sal_Int32 i_nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
329 MethodGuard aGuard( *m_pImpl );
331 const bool bHasScrollBack = m_pImpl->getTabBar()->GetScrollButton( false ).IsVisible();
332 const bool bHasScrollForward = m_pImpl->getTabBar()->GetScrollButton( true ).IsVisible();
334 const bool bScrollBackRequested = ( bHasScrollBack && ( i_nIndex == 0 ) );
335 const bool bScrollForwardRequested = ( bHasScrollForward && ( i_nIndex == getAccessibleChildCount() - 1 ) );
336 OSL_ENSURE( !( bScrollBackRequested && bScrollForwardRequested ), "AccessibleToolPanelTabBar::getAccessibleChild: ouch!" );
338 if ( bScrollBackRequested || bScrollForwardRequested )
340 Reference< XAccessible > xScrollButtonAccessible( m_pImpl->getTabBar()->GetScrollButton( bScrollForwardRequested ).GetAccessible() );
341 ENSURE_OR_RETURN( xScrollButtonAccessible.is(), "AccessibleToolPanelTabBar::getAccessibleChild: invalid button accessible!", NULL );
342 #if OSL_DEBUG_LEVEL > 0
343 Reference< XAccessibleContext > xScrollButtonContext( xScrollButtonAccessible->getAccessibleContext() );
344 ENSURE_OR_RETURN( xScrollButtonContext.is(), "AccessibleToolPanelTabBar::getAccessibleChild: invalid button accessible context!", xScrollButtonAccessible );
345 OSL_ENSURE( xScrollButtonContext->getAccessibleParent() == m_pImpl->getOwnAccessible(),
346 "AccessibleToolPanelTabBar::getAccessibleChild: wrong parent at the button's accesible!" );
347 #endif
348 return xScrollButtonAccessible;
351 return m_pImpl->getAccessiblePanelItem( i_nIndex - ( bHasScrollBack ? 1 : 0 ) );
354 //------------------------------------------------------------------------------------------------------------------
355 Reference< XAccessible > SAL_CALL AccessibleToolPanelTabBar::getAccessibleParent( ) throw (RuntimeException)
357 MethodGuard aGuard( *m_pImpl );
358 return m_pImpl->getAccessibleParent();
361 //------------------------------------------------------------------------------------------------------------------
362 sal_Int16 SAL_CALL AccessibleToolPanelTabBar::getAccessibleRole( ) throw (RuntimeException)
364 MethodGuard aGuard( *m_pImpl );
365 return AccessibleRole::PAGE_TAB_LIST;
368 //------------------------------------------------------------------------------------------------------------------
369 namespace
371 bool lcl_covers( const ::Window& i_rWindow, const ::Point& i_rPoint )
373 const Rectangle aWindowBounds( i_rWindow.GetWindowExtentsRelative( i_rWindow.GetParent() ) );
374 return aWindowBounds.IsInside( i_rPoint );
378 //------------------------------------------------------------------------------------------------------------------
379 Reference< XAccessible > SAL_CALL AccessibleToolPanelTabBar::getAccessibleAtPoint( const UnoPoint& i_rPoint ) throw (RuntimeException)
381 MethodGuard aGuard( *m_pImpl );
383 // check the tab items
384 const UnoPoint aOwnScreenPos( getLocationOnScreen() );
385 const ::Point aRequestedScreenPoint( i_rPoint.X + aOwnScreenPos.X, i_rPoint.Y + aOwnScreenPos.Y );
387 for ( size_t i=0; i<m_pImpl->getPanelDeck()->GetPanelCount(); ++i )
389 const ::Rectangle aItemScreenRect( m_pImpl->getTabBar()->GetItemScreenRect(i) );
390 if ( aItemScreenRect.IsInside( aRequestedScreenPoint ) )
391 return m_pImpl->getAccessiblePanelItem(i);
394 // check the scroll buttons
395 const ::Point aRequestedClientPoint( VCLUnoHelper::ConvertToVCLPoint( i_rPoint ) );
397 const bool bHasScrollBack = m_pImpl->getTabBar()->GetScrollButton( false ).IsVisible();
398 if ( bHasScrollBack && lcl_covers( m_pImpl->getTabBar()->GetScrollButton( false ), aRequestedClientPoint ) )
399 return m_pImpl->getTabBar()->GetScrollButton( false ).GetAccessible();
401 const bool bHasScrollForward = m_pImpl->getTabBar()->GetScrollButton( true ).IsVisible();
402 if ( bHasScrollForward && lcl_covers( m_pImpl->getTabBar()->GetScrollButton( true ), aRequestedClientPoint ) )
403 return m_pImpl->getTabBar()->GetScrollButton( true ).GetAccessible();
405 // no hit
406 return NULL;
409 //------------------------------------------------------------------------------------------------------------------
410 void SAL_CALL AccessibleToolPanelTabBar::disposing()
412 AccessibleToolPanelTabBar_Base::disposing();
413 m_pImpl->dispose();
416 //------------------------------------------------------------------------------------------------------------------
417 Reference< XAccessible > AccessibleToolPanelTabBar::GetChildAccessible( const VclWindowEvent& i_rVclWindowEvent )
419 // don't let the base class generate any A11Y events from VclWindowEvent, we completely manage those
420 // A11Y events ourself
421 (void)i_rVclWindowEvent;
422 return NULL;
425 //------------------------------------------------------------------------------------------------------------------
426 void AccessibleToolPanelTabBar::FillAccessibleStateSet( ::utl::AccessibleStateSetHelper& i_rStateSet )
428 AccessibleToolPanelTabBar_Base::FillAccessibleStateSet( i_rStateSet );
429 i_rStateSet.AddState( AccessibleStateType::FOCUSABLE );
431 ENSURE_OR_RETURN_VOID( !m_pImpl->isDisposed(), "AccessibleToolPanelTabBar::FillAccessibleStateSet: already disposed!" );
432 if ( m_pImpl->getTabBar()->IsVertical() )
433 i_rStateSet.AddState( AccessibleStateType::VERTICAL );
434 else
435 i_rStateSet.AddState( AccessibleStateType::HORIZONTAL );
438 //......................................................................................................................
439 } // namespace accessibility
440 //......................................................................................................................
442 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */