Branch libreoffice-5-0-4
[LibreOffice.git] / accessibility / source / standard / accessiblemenucomponent.cxx
blob1590d99510e83746bf5af584541e241cc56dcaab
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/standard/accessiblemenucomponent.hxx>
22 #include <toolkit/awt/vclxfont.hxx>
23 #include <toolkit/helper/convert.hxx>
25 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
26 #include <com/sun/star/accessibility/AccessibleRole.hpp>
27 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
29 #include <unotools/accessiblestatesethelper.hxx>
30 #include <unotools/accessiblerelationsethelper.hxx>
31 #include <cppuhelper/typeprovider.hxx>
32 #include <comphelper/sequence.hxx>
33 #include <vcl/svapp.hxx>
34 #include <vcl/window.hxx>
35 #include <vcl/menu.hxx>
36 #include <vcl/unohelp2.hxx>
37 #include <vcl/settings.hxx>
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;
47 // class OAccessibleMenuComponent
50 OAccessibleMenuComponent::OAccessibleMenuComponent( Menu* pMenu )
51 :OAccessibleMenuBaseComponent( pMenu )
57 OAccessibleMenuComponent::~OAccessibleMenuComponent()
63 bool OAccessibleMenuComponent::IsEnabled()
65 return true;
70 bool OAccessibleMenuComponent::IsVisible()
72 bool bVisible = false;
74 if ( m_pMenu )
75 bVisible = m_pMenu->IsMenuVisible();
77 return bVisible;
82 void OAccessibleMenuComponent::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
84 if ( IsEnabled() )
86 rStateSet.AddState( AccessibleStateType::ENABLED );
87 rStateSet.AddState( AccessibleStateType::SENSITIVE );
90 rStateSet.AddState( AccessibleStateType::FOCUSABLE );
92 if ( IsFocused() )
93 rStateSet.AddState( AccessibleStateType::FOCUSED );
95 if ( IsVisible() )
97 rStateSet.AddState( AccessibleStateType::VISIBLE );
98 rStateSet.AddState( AccessibleStateType::SHOWING );
101 rStateSet.AddState( AccessibleStateType::OPAQUE );
105 // OCommonAccessibleComponent
108 awt::Rectangle OAccessibleMenuComponent::implGetBounds() throw (RuntimeException)
110 awt::Rectangle aBounds( 0, 0, 0, 0 );
112 if ( m_pMenu )
114 vcl::Window* pWindow = m_pMenu->GetWindow();
115 if ( pWindow )
117 // get bounding rectangle of the window in screen coordinates
118 Rectangle aRect = pWindow->GetWindowExtentsRelative( NULL );
119 aBounds = AWTRectangle( aRect );
121 // get position of the accessible parent in screen coordinates
122 Reference< XAccessible > xParent = getAccessibleParent();
123 if ( xParent.is() )
125 Reference< XAccessibleComponent > xParentComponent( xParent->getAccessibleContext(), UNO_QUERY );
126 if ( xParentComponent.is() )
128 awt::Point aParentScreenLoc = xParentComponent->getLocationOnScreen();
130 // calculate position of the window relative to the accessible parent
131 aBounds.X -= aParentScreenLoc.X;
132 aBounds.Y -= aParentScreenLoc.Y;
138 return aBounds;
142 // XInterface
145 IMPLEMENT_FORWARD_XINTERFACE2( OAccessibleMenuComponent, OAccessibleMenuBaseComponent, OAccessibleMenuComponent_BASE )
148 // XTypeProvider
151 IMPLEMENT_FORWARD_XTYPEPROVIDER2( OAccessibleMenuComponent, OAccessibleMenuBaseComponent, OAccessibleMenuComponent_BASE )
154 // XAccessibleContext
157 sal_Int32 OAccessibleMenuComponent::getAccessibleChildCount() throw (RuntimeException, std::exception)
159 OExternalLockGuard aGuard( this );
161 return GetChildCount();
166 Reference< XAccessible > OAccessibleMenuComponent::getAccessibleChild( sal_Int32 i ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
168 OExternalLockGuard aGuard( this );
170 if ( i < 0 || i >= GetChildCount() )
171 throw IndexOutOfBoundsException();
173 return GetChild( i );
178 Reference< XAccessible > OAccessibleMenuComponent::getAccessibleParent( ) throw (RuntimeException, std::exception)
180 OExternalLockGuard aGuard( this );
182 Reference< XAccessible > xParent;
184 if ( m_pMenu )
186 vcl::Window* pWindow = m_pMenu->GetWindow();
187 if ( pWindow )
189 vcl::Window* pParent = pWindow->GetAccessibleParentWindow();
190 if ( pParent )
191 xParent = pParent->GetAccessible();
195 return xParent;
200 sal_Int16 OAccessibleMenuComponent::getAccessibleRole( ) throw (RuntimeException, std::exception)
202 OExternalLockGuard aGuard( this );
204 return AccessibleRole::UNKNOWN;
209 OUString OAccessibleMenuComponent::getAccessibleDescription( ) throw (RuntimeException, std::exception)
211 OExternalLockGuard aGuard( this );
213 OUString sDescription;
214 if ( m_pMenu )
216 vcl::Window* pWindow = m_pMenu->GetWindow();
217 if ( pWindow )
218 sDescription = pWindow->GetAccessibleDescription();
221 return sDescription;
226 OUString OAccessibleMenuComponent::getAccessibleName( ) throw (RuntimeException, std::exception)
228 OExternalLockGuard aGuard( this );
230 return OUString();
235 Reference< XAccessibleRelationSet > OAccessibleMenuComponent::getAccessibleRelationSet( ) throw (RuntimeException, std::exception)
237 OExternalLockGuard aGuard( this );
239 utl::AccessibleRelationSetHelper* pRelationSetHelper = new utl::AccessibleRelationSetHelper;
240 Reference< XAccessibleRelationSet > xSet = pRelationSetHelper;
241 return xSet;
246 Locale OAccessibleMenuComponent::getLocale( ) throw (IllegalAccessibleComponentStateException, RuntimeException, std::exception)
248 OExternalLockGuard aGuard( this );
250 return Application::GetSettings().GetLanguageTag().getLocale();
254 // XAccessibleComponent
257 Reference< XAccessible > OAccessibleMenuComponent::getAccessibleAtPoint( const awt::Point& rPoint ) throw (RuntimeException, std::exception)
259 OExternalLockGuard aGuard( this );
261 return GetChildAt( rPoint );
266 awt::Point OAccessibleMenuComponent::getLocationOnScreen( ) throw (RuntimeException, std::exception)
268 OExternalLockGuard aGuard( this );
270 awt::Point aPos;
272 if ( m_pMenu )
274 vcl::Window* pWindow = m_pMenu->GetWindow();
275 if ( pWindow )
277 Rectangle aRect = pWindow->GetWindowExtentsRelative( NULL );
278 aPos = AWTPoint( aRect.TopLeft() );
282 return aPos;
287 void OAccessibleMenuComponent::grabFocus( ) throw (RuntimeException, std::exception)
289 OExternalLockGuard aGuard( this );
291 if ( m_pMenu )
293 vcl::Window* pWindow = m_pMenu->GetWindow();
294 if ( pWindow )
295 pWindow->GrabFocus();
301 sal_Int32 OAccessibleMenuComponent::getForeground( ) throw (RuntimeException, std::exception)
303 OExternalLockGuard aGuard( this );
305 const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
306 sal_Int32 nColor = rStyleSettings.GetMenuTextColor().GetColor();
308 return nColor;
313 sal_Int32 OAccessibleMenuComponent::getBackground( ) throw (RuntimeException, std::exception)
315 OExternalLockGuard aGuard( this );
317 return 0;
321 // XAccessibleExtendedComponent
324 Reference< awt::XFont > OAccessibleMenuComponent::getFont( ) throw (RuntimeException, std::exception)
326 OExternalLockGuard aGuard( this );
328 Reference< awt::XFont > xFont;
330 if ( m_pMenu )
332 vcl::Window* pWindow = m_pMenu->GetWindow();
333 if ( pWindow )
335 Reference< awt::XDevice > xDev( pWindow->GetComponentInterface(), UNO_QUERY );
336 if ( xDev.is() )
338 const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
339 VCLXFont* pVCLXFont = new VCLXFont;
340 pVCLXFont->Init( *xDev.get(), rStyleSettings.GetMenuFont() );
341 xFont = pVCLXFont;
346 return xFont;
351 OUString OAccessibleMenuComponent::getTitledBorderText( ) throw (RuntimeException, std::exception)
353 OExternalLockGuard aGuard( this );
355 return OUString();
360 OUString OAccessibleMenuComponent::getToolTipText( ) throw (RuntimeException, std::exception)
362 OExternalLockGuard aGuard( this );
364 return OUString();
368 // XAccessibleSelection
371 void OAccessibleMenuComponent::selectAccessibleChild( sal_Int32 nChildIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
373 OExternalLockGuard aGuard( this );
375 if ( nChildIndex < 0 || nChildIndex >= GetChildCount() )
376 throw IndexOutOfBoundsException();
378 SelectChild( nChildIndex );
383 sal_Bool OAccessibleMenuComponent::isAccessibleChildSelected( sal_Int32 nChildIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
385 OExternalLockGuard aGuard( this );
387 if ( nChildIndex < 0 || nChildIndex >= GetChildCount() )
388 throw IndexOutOfBoundsException();
390 return IsChildSelected( nChildIndex );
395 void OAccessibleMenuComponent::clearAccessibleSelection( ) throw (RuntimeException, std::exception)
397 OExternalLockGuard aGuard( this );
399 DeSelectAll();
404 void OAccessibleMenuComponent::selectAllAccessibleChildren( ) throw (RuntimeException, std::exception)
406 // This method makes no sense in a menu, and so does nothing.
411 sal_Int32 OAccessibleMenuComponent::getSelectedAccessibleChildCount( ) throw (RuntimeException, std::exception)
413 OExternalLockGuard aGuard( this );
415 sal_Int32 nRet = 0;
417 for ( sal_Int32 i = 0, nCount = GetChildCount(); i < nCount; i++ )
419 if ( IsChildSelected( i ) )
420 ++nRet;
423 return nRet;
428 Reference< XAccessible > OAccessibleMenuComponent::getSelectedAccessibleChild( sal_Int32 nSelectedChildIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
430 OExternalLockGuard aGuard( this );
432 if ( nSelectedChildIndex < 0 || nSelectedChildIndex >= getSelectedAccessibleChildCount() )
433 throw IndexOutOfBoundsException();
435 Reference< XAccessible > xChild;
437 for ( sal_Int32 i = 0, j = 0, nCount = GetChildCount(); i < nCount; i++ )
439 if ( IsChildSelected( i ) && ( j++ == nSelectedChildIndex ) )
441 xChild = GetChild( i );
442 break;
446 return xChild;
451 void OAccessibleMenuComponent::deselectAccessibleChild( sal_Int32 nChildIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
453 OExternalLockGuard aGuard( this );
455 if ( nChildIndex < 0 || nChildIndex >= GetChildCount() )
456 throw IndexOutOfBoundsException();
458 DeSelectAll();
463 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */