Branch libreoffice-5-0-4
[LibreOffice.git] / accessibility / source / extended / AccessibleToolPanelDeck.cxx
blobe1fb62f9f71d2e1dbc8337ec77e539c25582792c
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/AccessibleToolPanelDeck.hxx"
22 #include <com/sun/star/accessibility/AccessibleRole.hpp>
23 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
24 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
25 #include <com/sun/star/lang/DisposedException.hpp>
27 #include <svtools/toolpanel/toolpaneldeck.hxx>
28 #include <toolkit/awt/vclxwindow.hxx>
29 #include <toolkit/helper/vclunohelper.hxx>
30 #include <vcl/svapp.hxx>
31 #include <osl/mutex.hxx>
32 #include <unotools/accessiblestatesethelper.hxx>
33 #include <tools/diagnose_ex.h>
35 #include <boost/noncopyable.hpp>
38 namespace accessibility
40 using ::com::sun::star::uno::Reference;
41 using ::com::sun::star::uno::XInterface;
42 using ::com::sun::star::uno::UNO_QUERY;
43 using ::com::sun::star::uno::UNO_QUERY_THROW;
44 using ::com::sun::star::uno::UNO_SET_THROW;
45 using ::com::sun::star::uno::Exception;
46 using ::com::sun::star::uno::RuntimeException;
47 using ::com::sun::star::uno::Any;
48 using ::com::sun::star::uno::makeAny;
49 using ::com::sun::star::uno::Sequence;
50 using ::com::sun::star::uno::Type;
51 using ::com::sun::star::accessibility::XAccessible;
52 using ::com::sun::star::accessibility::XAccessibleContext;
53 using ::com::sun::star::lang::DisposedException;
54 using ::com::sun::star::lang::IndexOutOfBoundsException;
55 using ::com::sun::star::lang::Locale;
56 using ::com::sun::star::accessibility::XAccessibleRelationSet;
57 using ::com::sun::star::accessibility::XAccessibleStateSet;
58 using ::com::sun::star::accessibility::IllegalAccessibleComponentStateException;
59 using ::com::sun::star::awt::XFont;
61 namespace AccessibleRole = ::com::sun::star::accessibility::AccessibleRole;
62 namespace AccessibleEventId = ::com::sun::star::accessibility::AccessibleEventId;
63 namespace AccessibleStateType = ::com::sun::star::accessibility::AccessibleStateType;
65 typedef ::com::sun::star::awt::Point UnoPoint;
67 // AccessibleToolPanelDeck_Impl - declaration
68 class AccessibleToolPanelDeck_Impl :public ::boost::noncopyable
69 ,public ::svt::IToolPanelDeckListener
71 public:
72 AccessibleToolPanelDeck_Impl(
73 AccessibleToolPanelDeck& i_rAntiImpl,
74 const Reference< XAccessible >& i_rAccessibleParent,
75 ::svt::ToolPanelDeck& i_rPanelDeck
78 void checkDisposed();
79 bool isDisposed() const { return m_pPanelDeck == nullptr; }
80 void dispose();
82 virtual ~AccessibleToolPanelDeck_Impl();
84 Reference< XAccessible > getOwnAccessible() const;
85 Reference< XAccessible > getActivePanelAccessible();
87 protected:
88 // IToolPanelDeckListener
89 virtual void PanelInserted( const ::svt::PToolPanel& i_pPanel, const size_t i_nPosition ) SAL_OVERRIDE;
90 virtual void PanelRemoved( const size_t i_nPosition ) SAL_OVERRIDE;
91 virtual void ActivePanelChanged( const ::boost::optional< size_t >& i_rOldActive, const ::boost::optional< size_t >& i_rNewActive ) SAL_OVERRIDE;
92 virtual void LayouterChanged( const ::svt::PDeckLayouter& i_rNewLayouter ) SAL_OVERRIDE;
93 virtual void Dying() SAL_OVERRIDE;
95 public:
96 AccessibleToolPanelDeck& m_rAntiImpl;
97 Reference< XAccessible > m_xAccessibleParent;
98 VclPtr< ::svt::ToolPanelDeck > m_pPanelDeck;
100 Reference< XAccessible > m_xActivePanelAccessible;
103 // MethodGuard
104 namespace
106 class MethodGuard
108 public:
109 explicit MethodGuard( AccessibleToolPanelDeck_Impl& i_rImpl )
110 :m_aGuard()
112 i_rImpl.checkDisposed();
114 ~MethodGuard()
118 private:
119 SolarMutexGuard m_aGuard;
123 // AccessibleToolPanelDeck_Impl - implementation
124 AccessibleToolPanelDeck_Impl::AccessibleToolPanelDeck_Impl( AccessibleToolPanelDeck& i_rAntiImpl, const Reference< XAccessible >& i_rAccessibleParent,
125 ::svt::ToolPanelDeck& i_rPanelDeck )
126 :m_rAntiImpl( i_rAntiImpl )
127 ,m_xAccessibleParent( i_rAccessibleParent )
128 ,m_pPanelDeck( &i_rPanelDeck )
129 ,m_xActivePanelAccessible()
131 m_pPanelDeck->AddListener( *this );
134 AccessibleToolPanelDeck_Impl::~AccessibleToolPanelDeck_Impl()
136 if ( !isDisposed() )
137 dispose();
140 void AccessibleToolPanelDeck_Impl::dispose()
142 ENSURE_OR_RETURN_VOID( !isDisposed(), "disposed twice" );
143 m_pPanelDeck->RemoveListener( *this );
144 m_pPanelDeck.clear();
145 m_xAccessibleParent.clear();
148 void AccessibleToolPanelDeck_Impl::checkDisposed()
150 if ( isDisposed() )
151 throw DisposedException( OUString(), *&m_rAntiImpl );
154 Reference< XAccessible > AccessibleToolPanelDeck_Impl::getOwnAccessible() const
156 Reference< XAccessible > xOwnAccessible( static_cast< XAccessible* >( m_rAntiImpl.GetVCLXWindow() ) );
157 OSL_ENSURE( xOwnAccessible->getAccessibleContext() == Reference< XAccessibleContext >( &m_rAntiImpl ),
158 "AccessibleToolPanelDeck_Impl::getOwnAccessible: could not retrieve proper XAccessible for /myself!" );
159 return xOwnAccessible;
162 Reference< XAccessible > AccessibleToolPanelDeck_Impl::getActivePanelAccessible()
164 ENSURE_OR_RETURN( !isDisposed(), "AccessibleToolPanelDeck_Impl::getActivePanelAccessible: already disposed!", NULL );
166 if ( !m_xActivePanelAccessible.is() )
168 ::boost::optional< size_t > aActivePanel( m_pPanelDeck->GetActivePanel() );
169 ENSURE_OR_RETURN( !!aActivePanel, "AccessibleToolPanelDeck_Impl::getActivePanelAccessible: this should not be called without an active panel!", NULL );
170 ::svt::PToolPanel pActivePanel( m_pPanelDeck->GetPanel( *aActivePanel ) );
171 ENSURE_OR_RETURN( pActivePanel.get() != NULL, "AccessibleToolPanelDeck_Impl::getActivePanelAccessible: no active panel!", NULL );
172 m_xActivePanelAccessible = pActivePanel->CreatePanelAccessible( getOwnAccessible() );
173 OSL_ENSURE( m_xActivePanelAccessible.is(), "AccessibleToolPanelDeck_Impl::getActivePanelAccessible: illegal accessible returned by the panel!" );
176 return m_xActivePanelAccessible;
179 void AccessibleToolPanelDeck_Impl::PanelInserted( const ::svt::PToolPanel& i_pPanel, const size_t i_nPosition )
181 (void)i_pPanel;
182 (void)i_nPosition;
185 void AccessibleToolPanelDeck_Impl::PanelRemoved( const size_t i_nPosition )
187 (void)i_nPosition;
190 void AccessibleToolPanelDeck_Impl::ActivePanelChanged( const ::boost::optional< size_t >& i_rOldActive, const ::boost::optional< size_t >& i_rNewActive )
192 if ( !!i_rOldActive )
194 if ( !m_xActivePanelAccessible.is() )
196 // again, this might in theory happen if the XAccessible for the active panel has never before been requested.
197 // In this case, just say that all our children are invalid, so they all must be re-requested.
198 m_rAntiImpl.NotifyAccessibleEvent( AccessibleEventId::INVALIDATE_ALL_CHILDREN, Any(), Any() );
200 else
202 m_rAntiImpl.NotifyAccessibleEvent( AccessibleEventId::CHILD, makeAny( m_xActivePanelAccessible ), Any() );
206 m_xActivePanelAccessible.clear();
208 if ( !!i_rNewActive )
210 m_rAntiImpl.NotifyAccessibleEvent( AccessibleEventId::CHILD, Any(), makeAny( getActivePanelAccessible() ) );
214 void AccessibleToolPanelDeck_Impl::LayouterChanged( const ::svt::PDeckLayouter& i_rNewLayouter )
216 MethodGuard aGuard( *this );
218 (void)i_rNewLayouter;
219 m_rAntiImpl.NotifyAccessibleEvent( AccessibleEventId::INVALIDATE_ALL_CHILDREN, Any(), Any() );
222 void AccessibleToolPanelDeck_Impl::Dying()
224 // the tool panel deck is dying, so dispose ourself
225 m_rAntiImpl.dispose();
228 // AccessibleToolPanelDeck
229 AccessibleToolPanelDeck::AccessibleToolPanelDeck( const Reference< XAccessible >& i_rAccessibleParent,
230 ::svt::ToolPanelDeck& i_rPanelDeck )
231 :AccessibleToolPanelDeck_Base( i_rPanelDeck.GetWindowPeer() )
232 ,m_xImpl( new AccessibleToolPanelDeck_Impl( *this, i_rAccessibleParent, i_rPanelDeck ) )
236 AccessibleToolPanelDeck::~AccessibleToolPanelDeck()
240 sal_Int32 SAL_CALL AccessibleToolPanelDeck::getAccessibleChildCount( ) throw (RuntimeException, std::exception)
242 MethodGuard aGuard( *m_xImpl );
244 sal_Int32 nChildCount( m_xImpl->m_pPanelDeck->GetLayouter()->GetAccessibleChildCount() );
246 ::boost::optional< size_t > aActivePanel( m_xImpl->m_pPanelDeck->GetActivePanel() );
247 if ( !!aActivePanel )
248 return ++nChildCount;
250 return nChildCount;
253 Reference< XAccessible > SAL_CALL AccessibleToolPanelDeck::getAccessibleChild( sal_Int32 i_nIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
255 MethodGuard aGuard( *m_xImpl );
257 const sal_Int32 nChildCount( getAccessibleChildCount() );
258 if ( ( i_nIndex < 0 ) || ( i_nIndex >= nChildCount ) )
259 throw IndexOutOfBoundsException( OUString(), *this );
261 // first "n" children are provided by the layouter
262 const size_t nLayouterCount( m_xImpl->m_pPanelDeck->GetLayouter()->GetAccessibleChildCount() );
263 if ( size_t( i_nIndex ) < nLayouterCount )
264 return m_xImpl->m_pPanelDeck->GetLayouter()->GetAccessibleChild(
265 size_t( i_nIndex ),
266 m_xImpl->getOwnAccessible()
269 // the last child is the XAccessible of the active panel
270 return m_xImpl->getActivePanelAccessible();
273 Reference< XAccessible > SAL_CALL AccessibleToolPanelDeck::getAccessibleParent( ) throw (RuntimeException, std::exception)
275 MethodGuard aGuard( *m_xImpl );
276 const Reference< XAccessible > xParent = implGetForeignControlledParent();
277 if ( xParent.is() )
278 return xParent;
279 return m_xImpl->m_xAccessibleParent;
282 sal_Int16 SAL_CALL AccessibleToolPanelDeck::getAccessibleRole( ) throw (RuntimeException, std::exception)
284 MethodGuard aGuard( *m_xImpl );
285 return AccessibleRole::PANEL;
288 Reference< XAccessible > SAL_CALL AccessibleToolPanelDeck::getAccessibleAtPoint( const UnoPoint& i_rPoint ) throw (RuntimeException, std::exception)
290 MethodGuard aGuard( *m_xImpl );
292 const ::Point aRequestedPoint( VCLUnoHelper::ConvertToVCLPoint( i_rPoint ) );
293 // check the panel window itself
294 const vcl::Window& rActivePanelAnchor( m_xImpl->m_pPanelDeck->GetPanelWindowAnchor() );
295 const Rectangle aPanelAnchorArea( rActivePanelAnchor.GetPosPixel(), rActivePanelAnchor.GetOutputSizePixel() );
296 if ( aPanelAnchorArea.IsInside( aRequestedPoint ) )
297 // note that this assumes that the Window which actually implements the concrete panel covers
298 // the complete area of its "anchor" Window. But this is ensured by the ToolPanelDeck implementation.
299 return m_xImpl->getActivePanelAccessible();
301 // check the XAccessible instances provided by the layouter
304 const ::svt::PDeckLayouter pLayouter( m_xImpl->m_pPanelDeck->GetLayouter() );
305 ENSURE_OR_THROW( pLayouter.get() != NULL, "invalid layouter" );
307 const size_t nLayouterChildren = pLayouter->GetAccessibleChildCount();
308 for ( size_t i=0; i<nLayouterChildren; ++i )
310 const Reference< XAccessible > xLayoutItemAccessible( pLayouter->GetAccessibleChild( i, m_xImpl->getOwnAccessible() ), UNO_SET_THROW );
311 const Reference< XAccessibleComponent > xLayoutItemComponent( xLayoutItemAccessible->getAccessibleContext(), UNO_QUERY_THROW );
312 const ::Rectangle aLayoutItemBounds( VCLUnoHelper::ConvertToVCLRect( xLayoutItemComponent->getBounds() ) );
313 if ( aLayoutItemBounds.IsInside( aRequestedPoint ) )
314 return xLayoutItemAccessible;
317 catch( const Exception& )
319 DBG_UNHANDLED_EXCEPTION();
322 return NULL;
325 void SAL_CALL AccessibleToolPanelDeck::grabFocus( ) throw (RuntimeException, std::exception)
327 MethodGuard aGuard( *m_xImpl );
328 m_xImpl->m_pPanelDeck->GrabFocus();
331 void SAL_CALL AccessibleToolPanelDeck::disposing()
333 AccessibleToolPanelDeck_Base::disposing();
334 m_xImpl->dispose();
337 Reference< XAccessible > AccessibleToolPanelDeck::GetChildAccessible( const VclWindowEvent& i_rVclWindowEvent )
339 // don't let the base class generate any A11Y events from VclWindowEvent, we completely manage those
340 // A11Y events ourself
341 (void)i_rVclWindowEvent;
342 return NULL;
345 void AccessibleToolPanelDeck::FillAccessibleStateSet( ::utl::AccessibleStateSetHelper& i_rStateSet )
347 AccessibleToolPanelDeck_Base::FillAccessibleStateSet( i_rStateSet );
348 if ( m_xImpl->isDisposed() )
350 i_rStateSet.AddState( AccessibleStateType::DEFUNC );
352 else
354 i_rStateSet.AddState( AccessibleStateType::FOCUSABLE );
357 } // namespace accessibility
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */