Branch libreoffice-5-0-4
[LibreOffice.git] / accessibility / source / extended / AccessibleToolPanelDeckTabBar.cxx
blob11dc7300e339e28b3b926d2f342a6d0b28e44d63
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/AccessibleToolPanelDeckTabBar.hxx"
21 #include "accessibility/extended/AccessibleToolPanelDeckTabBarItem.hxx"
22 #include "accessibility/helper/accresmgr.hxx"
23 #include "accessibility/helper/accessiblestrings.hrc"
25 #include <com/sun/star/accessibility/AccessibleRole.hpp>
26 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
27 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
28 #include <com/sun/star/lang/DisposedException.hpp>
30 #include <svtools/toolpanel/toolpaneldeck.hxx>
31 #include <svtools/toolpanel/paneltabbar.hxx>
32 #include <unotools/accessiblestatesethelper.hxx>
33 #include <toolkit/awt/vclxwindow.hxx>
34 #include <toolkit/helper/vclunohelper.hxx>
35 #include <vcl/svapp.hxx>
36 #include <vcl/button.hxx>
37 #include <osl/mutex.hxx>
38 #include <tools/diagnose_ex.h>
40 #include <vector>
42 namespace accessibility
44 using ::com::sun::star::uno::Reference;
45 using ::com::sun::star::uno::XInterface;
46 using ::com::sun::star::uno::UNO_QUERY;
47 using ::com::sun::star::uno::UNO_QUERY_THROW;
48 using ::com::sun::star::uno::UNO_SET_THROW;
49 using ::com::sun::star::uno::Exception;
50 using ::com::sun::star::uno::RuntimeException;
51 using ::com::sun::star::uno::Any;
52 using ::com::sun::star::uno::makeAny;
53 using ::com::sun::star::uno::Sequence;
54 using ::com::sun::star::uno::Type;
55 using ::com::sun::star::accessibility::XAccessible;
56 using ::com::sun::star::lang::DisposedException;
57 using ::com::sun::star::lang::IndexOutOfBoundsException;
58 using ::com::sun::star::accessibility::XAccessibleContext;
60 namespace AccessibleRole = ::com::sun::star::accessibility::AccessibleRole;
61 namespace AccessibleEventId = ::com::sun::star::accessibility::AccessibleEventId;
62 namespace AccessibleStateType = ::com::sun::star::accessibility::AccessibleStateType;
64 typedef ::com::sun::star::awt::Point UnoPoint;
66 // AccessibleWrapper
67 typedef ::cppu::WeakImplHelper1< XAccessible > AccessibleWrapper_Base;
68 class AccessibleWrapper : public AccessibleWrapper_Base
70 public:
71 explicit AccessibleWrapper( const Reference< XAccessibleContext >& i_rContext )
72 :m_xContext( i_rContext )
76 // XAccessible
77 virtual Reference< XAccessibleContext > SAL_CALL getAccessibleContext( ) throw (RuntimeException, std::exception) SAL_OVERRIDE
79 return m_xContext;
82 private:
83 const Reference< XAccessibleContext > m_xContext;
86 // AccessibleToolPanelTabBar_Impl
87 class AccessibleToolPanelTabBar_Impl :public ::boost::noncopyable
88 ,public ::svt::IToolPanelDeckListener
90 public:
91 AccessibleToolPanelTabBar_Impl(
92 AccessibleToolPanelTabBar& i_rAntiImpl,
93 const Reference< XAccessible >& i_rAccessibleParent,
94 ::svt::IToolPanelDeck& i_rPanelDeck,
95 ::svt::PanelTabBar& i_rTabBar
97 virtual ~AccessibleToolPanelTabBar_Impl();
99 void checkDisposed();
100 bool isDisposed() const { return m_pPanelDeck == NULL; }
101 void dispose();
103 ::svt::IToolPanelDeck* getPanelDeck() const { return m_pPanelDeck; }
104 ::svt::PanelTabBar* getTabBar() const { return m_pTabBar; }
105 const Reference< XAccessible >& getAccessibleParent() const { return m_xAccessibleParent; }
106 Reference< XAccessible > getAccessiblePanelItem( size_t i_nPosition );
107 Reference< XAccessible > getOwnAccessible() const;
109 protected:
110 // IToolPanelDeckListener
111 virtual void PanelInserted( const ::svt::PToolPanel& i_pPanel, const size_t i_nPosition ) SAL_OVERRIDE;
112 virtual void PanelRemoved( const size_t i_nPosition ) SAL_OVERRIDE;
113 virtual void ActivePanelChanged( const ::boost::optional< size_t >& i_rOldActive, const ::boost::optional< size_t >& i_rNewActive ) SAL_OVERRIDE;
114 virtual void LayouterChanged( const ::svt::PDeckLayouter& i_rNewLayouter ) SAL_OVERRIDE;
115 virtual void Dying() SAL_OVERRIDE;
117 DECL_LINK( OnWindowEvent, const VclSimpleEvent* );
119 private:
120 AccessibleToolPanelTabBar& m_rAntiImpl;
121 Reference< XAccessible > m_xAccessibleParent;
122 ::svt::IToolPanelDeck* m_pPanelDeck;
123 VclPtr< ::svt::PanelTabBar > m_pTabBar;
124 ::std::vector< Reference< XAccessible > > m_aChildren;
127 AccessibleToolPanelTabBar_Impl::AccessibleToolPanelTabBar_Impl( AccessibleToolPanelTabBar& i_rAntiImpl,
128 const Reference< XAccessible >& i_rAccessibleParent, ::svt::IToolPanelDeck& i_rPanelDeck, ::svt::PanelTabBar& i_rTabBar )
129 :m_rAntiImpl( i_rAntiImpl )
130 ,m_xAccessibleParent( i_rAccessibleParent )
131 ,m_pPanelDeck( &i_rPanelDeck )
132 ,m_pTabBar( &i_rTabBar )
133 ,m_aChildren()
135 m_pPanelDeck->AddListener( *this );
136 m_aChildren.resize( m_pPanelDeck->GetPanelCount() );
138 const OUString sAccessibleDescription( TK_RES_STRING( RID_STR_ACC_DESC_PANELDECL_TABBAR ) );
139 i_rTabBar.SetAccessibleName( sAccessibleDescription );
140 i_rTabBar.SetAccessibleDescription( sAccessibleDescription );
142 i_rTabBar.GetScrollButton( true ).AddEventListener( LINK( this, AccessibleToolPanelTabBar_Impl, OnWindowEvent ) );
143 i_rTabBar.GetScrollButton( false ).AddEventListener( LINK( this, AccessibleToolPanelTabBar_Impl, OnWindowEvent ) );
146 void AccessibleToolPanelTabBar_Impl::checkDisposed()
148 if ( isDisposed() )
149 throw DisposedException( OUString(), *&m_rAntiImpl );
152 AccessibleToolPanelTabBar_Impl::~AccessibleToolPanelTabBar_Impl()
154 if ( !isDisposed() )
155 dispose();
158 void AccessibleToolPanelTabBar_Impl::dispose()
160 ENSURE_OR_RETURN_VOID( !isDisposed(), "disposed twice" );
161 m_pPanelDeck->RemoveListener( *this );
162 m_pPanelDeck = NULL;
164 m_pTabBar->GetScrollButton( true ).RemoveEventListener( LINK( this, AccessibleToolPanelTabBar_Impl, OnWindowEvent ) );
165 m_pTabBar->GetScrollButton( false ).RemoveEventListener( LINK( this, AccessibleToolPanelTabBar_Impl, OnWindowEvent ) );
166 m_pTabBar.clear();
168 m_xAccessibleParent.clear();
171 Reference< XAccessible > AccessibleToolPanelTabBar_Impl::getAccessiblePanelItem( size_t i_nPosition )
173 ENSURE_OR_RETURN( !isDisposed(), "AccessibleToolPanelTabBar_Impl::getAccessiblePanelItem: already disposed!", NULL );
174 ENSURE_OR_RETURN( i_nPosition < m_aChildren.size(), "AccessibleToolPanelTabBar_Impl::getAccessiblePanelItem: invalid index!", NULL );
176 Reference< XAccessible >& rAccessibleChild( m_aChildren[ i_nPosition ] );
177 if ( !rAccessibleChild.is() )
179 ::rtl::Reference< AccessibleToolPanelDeckTabBarItem > pAccesibleItemContext( new AccessibleToolPanelDeckTabBarItem(
180 getOwnAccessible(), *m_pPanelDeck, *m_pTabBar, i_nPosition ) );
181 rAccessibleChild.set( new AccessibleWrapper( pAccesibleItemContext.get() ) );
182 pAccesibleItemContext->lateInit( rAccessibleChild );
184 return rAccessibleChild;
187 Reference< XAccessible > AccessibleToolPanelTabBar_Impl::getOwnAccessible() const
189 Reference< XAccessible > xOwnAccessible( static_cast< XAccessible* >( m_rAntiImpl.GetVCLXWindow() ) );
190 OSL_ENSURE( xOwnAccessible->getAccessibleContext() == Reference< XAccessibleContext >( &m_rAntiImpl ),
191 "AccessibleToolPanelTabBar_Impl::getOwnAccessible: could not retrieve proper XAccessible for /myself!" );
192 return xOwnAccessible;
195 void AccessibleToolPanelTabBar_Impl::PanelInserted( const ::svt::PToolPanel& i_pPanel, const size_t i_nPosition )
197 ENSURE_OR_RETURN_VOID( i_nPosition <= m_aChildren.size(), "AccessibleToolPanelTabBar_Impl::PanelInserted: illegal position (or invalid cache!)" );
198 (void)i_pPanel;
199 m_aChildren.insert( m_aChildren.begin() + i_nPosition, Reference< XAccessible >() );
200 m_rAntiImpl.NotifyAccessibleEvent( AccessibleEventId::CHILD, Any(), makeAny( getAccessiblePanelItem( i_nPosition ) ) );
203 void AccessibleToolPanelTabBar_Impl::PanelRemoved( const size_t i_nPosition )
205 ENSURE_OR_RETURN_VOID( i_nPosition < m_aChildren.size(), "AccessibleToolPanelTabBar_Impl::PanelInserted: illegal position (or invalid cache!)" );
207 const Reference< XAccessible > xOldChild( getAccessiblePanelItem( i_nPosition ) );
208 m_aChildren.erase( m_aChildren.begin() + i_nPosition );
209 m_rAntiImpl.NotifyAccessibleEvent( AccessibleEventId::CHILD, makeAny( xOldChild ), Any() );
212 void AccessibleToolPanelTabBar_Impl::ActivePanelChanged( const ::boost::optional< size_t >& i_rOldActive, const ::boost::optional< size_t >& i_rNewActive )
214 (void)i_rOldActive;
215 (void)i_rNewActive;
218 void AccessibleToolPanelTabBar_Impl::LayouterChanged( const ::svt::PDeckLayouter& i_rNewLayouter )
220 (void)i_rNewLayouter;
221 m_rAntiImpl.dispose();
224 void AccessibleToolPanelTabBar_Impl::Dying()
226 m_rAntiImpl.dispose();
229 IMPL_LINK( AccessibleToolPanelTabBar_Impl, OnWindowEvent, const VclSimpleEvent*, i_pEvent )
231 ENSURE_OR_RETURN( !isDisposed(), "AccessibleToolPanelTabBar_Impl::OnWindowEvent: already disposed!", 0L );
233 const VclWindowEvent* pWindowEvent( dynamic_cast< const VclWindowEvent* >( i_pEvent ) );
234 if ( !pWindowEvent )
235 return 0L;
237 const bool bForwardButton = ( pWindowEvent->GetWindow() == &m_pTabBar->GetScrollButton( true ) );
238 const bool bBackwardButton = ( pWindowEvent->GetWindow() == &m_pTabBar->GetScrollButton( false ) );
239 ENSURE_OR_RETURN( bForwardButton || bBackwardButton, "AccessibleToolPanelTabBar_Impl::OnWindowEvent: where does this come from?", 0L );
241 const bool bShow = ( i_pEvent->GetId() == VCLEVENT_WINDOW_SHOW );
242 const bool bHide = ( i_pEvent->GetId() == VCLEVENT_WINDOW_HIDE );
243 if ( !bShow && !bHide )
244 // not interested in events other than visibility changes
245 return 0L;
247 const Reference< XAccessible > xButtonAccessible( m_pTabBar->GetScrollButton( bForwardButton ).GetAccessible() );
248 const Any aOldChild( bHide ? xButtonAccessible : Reference< XAccessible >() );
249 const Any aNewChild( bShow ? xButtonAccessible : Reference< XAccessible >() );
250 m_rAntiImpl.NotifyAccessibleEvent( AccessibleEventId::CHILD, aOldChild, aNewChild );
252 return 1L;
255 // MethodGuard
256 namespace
258 class MethodGuard
260 public:
261 explicit MethodGuard( AccessibleToolPanelTabBar_Impl& i_rImpl )
262 :m_aGuard()
264 i_rImpl.checkDisposed();
266 ~MethodGuard()
270 private:
271 SolarMutexGuard m_aGuard;
275 // AccessibleToolPanelTabBar
276 AccessibleToolPanelTabBar::AccessibleToolPanelTabBar( const Reference< XAccessible >& i_rAccessibleParent,
277 ::svt::IToolPanelDeck& i_rPanelDeck, ::svt::PanelTabBar& i_rTabBar )
278 :AccessibleToolPanelTabBar_Base( i_rTabBar.GetWindowPeer() )
279 ,m_xImpl( new AccessibleToolPanelTabBar_Impl( *this, i_rAccessibleParent, i_rPanelDeck, i_rTabBar ) )
283 AccessibleToolPanelTabBar::~AccessibleToolPanelTabBar()
287 sal_Int32 SAL_CALL AccessibleToolPanelTabBar::getAccessibleChildCount( ) throw (RuntimeException, std::exception)
289 MethodGuard aGuard( *m_xImpl );
291 const bool bHasScrollBack = m_xImpl->getTabBar()->GetScrollButton( false ).IsVisible();
292 const bool bHasScrollForward = m_xImpl->getTabBar()->GetScrollButton( true ).IsVisible();
294 return m_xImpl->getPanelDeck()->GetPanelCount()
295 + ( bHasScrollBack ? 1 : 0 )
296 + ( bHasScrollForward ? 1 : 0 );
299 Reference< XAccessible > SAL_CALL AccessibleToolPanelTabBar::getAccessibleChild( sal_Int32 i_nIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
301 MethodGuard aGuard( *m_xImpl );
303 const bool bHasScrollBack = m_xImpl->getTabBar()->GetScrollButton( false ).IsVisible();
304 const bool bHasScrollForward = m_xImpl->getTabBar()->GetScrollButton( true ).IsVisible();
306 const bool bScrollBackRequested = ( bHasScrollBack && ( i_nIndex == 0 ) );
307 const bool bScrollForwardRequested = ( bHasScrollForward && ( i_nIndex == getAccessibleChildCount() - 1 ) );
308 OSL_ENSURE( !( bScrollBackRequested && bScrollForwardRequested ), "AccessibleToolPanelTabBar::getAccessibleChild: ouch!" );
310 if ( bScrollBackRequested || bScrollForwardRequested )
312 Reference< XAccessible > xScrollButtonAccessible( m_xImpl->getTabBar()->GetScrollButton( bScrollForwardRequested ).GetAccessible() );
313 ENSURE_OR_RETURN( xScrollButtonAccessible.is(), "AccessibleToolPanelTabBar::getAccessibleChild: invalid button accessible!", NULL );
314 #if OSL_DEBUG_LEVEL > 0
315 Reference< XAccessibleContext > xScrollButtonContext( xScrollButtonAccessible->getAccessibleContext() );
316 ENSURE_OR_RETURN( xScrollButtonContext.is(), "AccessibleToolPanelTabBar::getAccessibleChild: invalid button accessible context!", xScrollButtonAccessible );
317 OSL_ENSURE( xScrollButtonContext->getAccessibleParent() == m_xImpl->getOwnAccessible(),
318 "AccessibleToolPanelTabBar::getAccessibleChild: wrong parent at the button's accessible!" );
319 #endif
320 return xScrollButtonAccessible;
323 return m_xImpl->getAccessiblePanelItem( i_nIndex - ( bHasScrollBack ? 1 : 0 ) );
326 Reference< XAccessible > SAL_CALL AccessibleToolPanelTabBar::getAccessibleParent( ) throw (RuntimeException, std::exception)
328 MethodGuard aGuard( *m_xImpl );
329 return m_xImpl->getAccessibleParent();
332 sal_Int16 SAL_CALL AccessibleToolPanelTabBar::getAccessibleRole( ) throw (RuntimeException, std::exception)
334 MethodGuard aGuard( *m_xImpl );
335 return AccessibleRole::PAGE_TAB_LIST;
338 namespace
340 bool lcl_covers( const vcl::Window& i_rWindow, const ::Point& i_rPoint )
342 const Rectangle aWindowBounds( i_rWindow.GetWindowExtentsRelative( i_rWindow.GetParent() ) );
343 return aWindowBounds.IsInside( i_rPoint );
347 Reference< XAccessible > SAL_CALL AccessibleToolPanelTabBar::getAccessibleAtPoint( const UnoPoint& i_rPoint ) throw (RuntimeException, std::exception)
349 MethodGuard aGuard( *m_xImpl );
351 // check the tab items
352 const UnoPoint aOwnScreenPos( getLocationOnScreen() );
353 const ::Point aRequestedScreenPoint( i_rPoint.X + aOwnScreenPos.X, i_rPoint.Y + aOwnScreenPos.Y );
355 for ( size_t i=0; i<m_xImpl->getPanelDeck()->GetPanelCount(); ++i )
357 const ::Rectangle aItemScreenRect( m_xImpl->getTabBar()->GetItemScreenRect(i) );
358 if ( aItemScreenRect.IsInside( aRequestedScreenPoint ) )
359 return m_xImpl->getAccessiblePanelItem(i);
362 // check the scroll buttons
363 const ::Point aRequestedClientPoint( VCLUnoHelper::ConvertToVCLPoint( i_rPoint ) );
365 const bool bHasScrollBack = m_xImpl->getTabBar()->GetScrollButton( false ).IsVisible();
366 if ( bHasScrollBack && lcl_covers( m_xImpl->getTabBar()->GetScrollButton( false ), aRequestedClientPoint ) )
367 return m_xImpl->getTabBar()->GetScrollButton( false ).GetAccessible();
369 const bool bHasScrollForward = m_xImpl->getTabBar()->GetScrollButton( true ).IsVisible();
370 if ( bHasScrollForward && lcl_covers( m_xImpl->getTabBar()->GetScrollButton( true ), aRequestedClientPoint ) )
371 return m_xImpl->getTabBar()->GetScrollButton( true ).GetAccessible();
373 // no hit
374 return NULL;
377 void SAL_CALL AccessibleToolPanelTabBar::disposing()
379 AccessibleToolPanelTabBar_Base::disposing();
380 m_xImpl->dispose();
383 Reference< XAccessible > AccessibleToolPanelTabBar::GetChildAccessible( const VclWindowEvent& i_rVclWindowEvent )
385 // don't let the base class generate any A11Y events from VclWindowEvent, we completely manage those
386 // A11Y events ourself
387 (void)i_rVclWindowEvent;
388 return NULL;
391 void AccessibleToolPanelTabBar::FillAccessibleStateSet( ::utl::AccessibleStateSetHelper& i_rStateSet )
393 AccessibleToolPanelTabBar_Base::FillAccessibleStateSet( i_rStateSet );
394 i_rStateSet.AddState( AccessibleStateType::FOCUSABLE );
396 ENSURE_OR_RETURN_VOID( !m_xImpl->isDisposed(), "AccessibleToolPanelTabBar::FillAccessibleStateSet: already disposed!" );
397 if ( m_xImpl->getTabBar()->IsVertical() )
398 i_rStateSet.AddState( AccessibleStateType::VERTICAL );
399 else
400 i_rStateSet.AddState( AccessibleStateType::HORIZONTAL );
402 } // namespace accessibility
404 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */