merged tag ooo/DEV300_m102
[LibreOffice.git] / accessibility / source / standard / accessiblemenuitemcomponent.cxx
blob9ac93cedd2999996e3e6fe8cd7fa448c60825f75
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_accessibility.hxx"
30 #include <accessibility/standard/accessiblemenuitemcomponent.hxx>
33 #include <accessibility/helper/accresmgr.hxx>
34 #include <accessibility/helper/accessiblestrings.hrc>
35 #include <toolkit/awt/vclxwindows.hxx>
36 #include <toolkit/helper/externallock.hxx>
37 #include <toolkit/helper/convert.hxx>
39 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
40 #include <com/sun/star/accessibility/AccessibleRole.hpp>
41 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
42 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
43 #include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
45 #include <unotools/accessiblestatesethelper.hxx>
46 #include <unotools/accessiblerelationsethelper.hxx>
47 #include <cppuhelper/typeprovider.hxx>
48 #include <comphelper/sequence.hxx>
49 #include <comphelper/accessibletexthelper.hxx>
50 #include <vcl/svapp.hxx>
51 #include <vcl/window.hxx>
52 #include <vcl/menu.hxx>
53 #include <vcl/unohelp2.hxx>
56 using namespace ::com::sun::star::accessibility;
57 using namespace ::com::sun::star::uno;
58 using namespace ::com::sun::star::beans;
59 using namespace ::com::sun::star::lang;
60 using namespace ::com::sun::star;
61 using namespace ::comphelper;
64 // -----------------------------------------------------------------------------
65 // class OAccessibleMenuItemComponent
66 // -----------------------------------------------------------------------------
68 OAccessibleMenuItemComponent::OAccessibleMenuItemComponent( Menu* pParent, sal_uInt16 nItemPos, Menu* pMenu )
69 :OAccessibleMenuBaseComponent( pMenu )
70 ,m_pParent( pParent )
71 ,m_nItemPos( nItemPos )
73 m_sAccessibleName = GetAccessibleName();
74 m_sItemText = GetItemText();
77 // -----------------------------------------------------------------------------
79 OAccessibleMenuItemComponent::~OAccessibleMenuItemComponent()
83 // -----------------------------------------------------------------------------
85 sal_Bool OAccessibleMenuItemComponent::IsEnabled()
87 OExternalLockGuard aGuard( this );
89 sal_Bool bEnabled = sal_False;
90 if ( m_pParent )
91 bEnabled = m_pParent->IsItemEnabled( m_pParent->GetItemId( m_nItemPos ) );
93 return bEnabled;
96 // -----------------------------------------------------------------------------
98 sal_Bool OAccessibleMenuItemComponent::IsVisible()
100 sal_Bool bVisible = sal_False;
102 if ( m_pParent )
103 bVisible = m_pParent->IsItemPosVisible( m_nItemPos );
105 return bVisible;
108 // -----------------------------------------------------------------------------
110 void OAccessibleMenuItemComponent::Select()
112 // open the parent menu
113 Reference< XAccessible > xParent( getAccessibleParent() );
114 if ( xParent.is() )
116 OAccessibleMenuBaseComponent* pComp = static_cast< OAccessibleMenuBaseComponent* >( xParent.get() );
117 if ( pComp && pComp->getAccessibleRole() == AccessibleRole::MENU && !pComp->IsPopupMenuOpen() )
118 pComp->Click();
121 // highlight the menu item
122 if ( m_pParent )
123 m_pParent->HighlightItem( m_nItemPos );
126 // -----------------------------------------------------------------------------
128 void OAccessibleMenuItemComponent::DeSelect()
130 if ( m_pParent && IsSelected() )
131 m_pParent->DeHighlight();
134 // -----------------------------------------------------------------------------
136 void OAccessibleMenuItemComponent::Click()
138 // open the parent menu
139 Reference< XAccessible > xParent( getAccessibleParent() );
140 if ( xParent.is() )
142 OAccessibleMenuBaseComponent* pComp = static_cast< OAccessibleMenuBaseComponent* >( xParent.get() );
143 if ( pComp && pComp->getAccessibleRole() == AccessibleRole::MENU && !pComp->IsPopupMenuOpen() )
144 pComp->Click();
147 // click the menu item
148 if ( m_pParent )
150 Window* pWindow = m_pParent->GetWindow();
151 if ( pWindow )
153 // #102438# Menu items are not selectable
154 // Popup menus are executed asynchronously, triggered by a timer.
155 // As Menu::SelectItem only works, if the corresponding menu window is
156 // already created, we have to set the menu delay to 0, so
157 // that the popup menus are executed synchronously.
158 AllSettings aSettings = pWindow->GetSettings();
159 MouseSettings aMouseSettings = aSettings.GetMouseSettings();
160 sal_uLong nDelay = aMouseSettings.GetMenuDelay();
161 aMouseSettings.SetMenuDelay( 0 );
162 aSettings.SetMouseSettings( aMouseSettings );
163 pWindow->SetSettings( aSettings );
165 m_pParent->SelectItem( m_pParent->GetItemId( m_nItemPos ) );
167 // meanwhile the window pointer may be invalid
168 pWindow = m_pParent->GetWindow();
169 if ( pWindow )
171 // set the menu delay back to the old value
172 aSettings = pWindow->GetSettings();
173 aMouseSettings = aSettings.GetMouseSettings();
174 aMouseSettings.SetMenuDelay( nDelay );
175 aSettings.SetMouseSettings( aMouseSettings );
176 pWindow->SetSettings( aSettings );
182 // -----------------------------------------------------------------------------
184 void OAccessibleMenuItemComponent::SetItemPos( sal_uInt16 nItemPos )
186 m_nItemPos = nItemPos;
189 // -----------------------------------------------------------------------------
191 void OAccessibleMenuItemComponent::SetAccessibleName( const ::rtl::OUString& sAccessibleName )
193 if ( !m_sAccessibleName.equals( sAccessibleName ) )
195 Any aOldValue, aNewValue;
196 aOldValue <<= m_sAccessibleName;
197 aNewValue <<= sAccessibleName;
198 m_sAccessibleName = sAccessibleName;
199 NotifyAccessibleEvent( AccessibleEventId::NAME_CHANGED, aOldValue, aNewValue );
203 // -----------------------------------------------------------------------------
205 ::rtl::OUString OAccessibleMenuItemComponent::GetAccessibleName()
207 ::rtl::OUString sName;
208 if ( m_pParent )
210 sal_uInt16 nItemId = m_pParent->GetItemId( m_nItemPos );
211 sName = m_pParent->GetAccessibleName( nItemId );
212 if ( sName.getLength() == 0 )
213 sName = m_pParent->GetItemText( nItemId );
214 sName = OutputDevice::GetNonMnemonicString( sName );
217 return sName;
220 // -----------------------------------------------------------------------------
222 void OAccessibleMenuItemComponent::SetItemText( const ::rtl::OUString& sItemText )
224 Any aOldValue, aNewValue;
225 if ( OCommonAccessibleText::implInitTextChangedEvent( m_sItemText, sItemText, aOldValue, aNewValue ) )
227 m_sItemText = sItemText;
228 NotifyAccessibleEvent( AccessibleEventId::TEXT_CHANGED, aOldValue, aNewValue );
232 // -----------------------------------------------------------------------------
234 ::rtl::OUString OAccessibleMenuItemComponent::GetItemText()
236 ::rtl::OUString sText;
237 if ( m_pParent )
238 sText = OutputDevice::GetNonMnemonicString( m_pParent->GetItemText( m_pParent->GetItemId( m_nItemPos ) ) );
240 return sText;
243 // -----------------------------------------------------------------------------
245 void OAccessibleMenuItemComponent::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
247 if ( IsEnabled() )
249 rStateSet.AddState( AccessibleStateType::ENABLED );
250 rStateSet.AddState( AccessibleStateType::SENSITIVE );
253 if ( IsVisible() )
255 rStateSet.AddState( AccessibleStateType::VISIBLE );
256 rStateSet.AddState( AccessibleStateType::SHOWING );
259 rStateSet.AddState( AccessibleStateType::OPAQUE );
262 // -----------------------------------------------------------------------------
263 // OCommonAccessibleComponent
264 // -----------------------------------------------------------------------------
266 awt::Rectangle OAccessibleMenuItemComponent::implGetBounds() throw (RuntimeException)
268 awt::Rectangle aBounds( 0, 0, 0, 0 );
270 if ( m_pParent )
272 // get bounding rectangle of the item relative to the containing window
273 aBounds = AWTRectangle( m_pParent->GetBoundingRectangle( m_nItemPos ) );
275 // get position of containing window in screen coordinates
276 Window* pWindow = m_pParent->GetWindow();
277 if ( pWindow )
279 Rectangle aRect = pWindow->GetWindowExtentsRelative( NULL );
280 awt::Point aWindowScreenLoc = AWTPoint( aRect.TopLeft() );
282 // get position of accessible parent in screen coordinates
283 Reference< XAccessible > xParent = getAccessibleParent();
284 if ( xParent.is() )
286 Reference< XAccessibleComponent > xParentComponent( xParent->getAccessibleContext(), UNO_QUERY );
287 if ( xParentComponent.is() )
289 awt::Point aParentScreenLoc = xParentComponent->getLocationOnScreen();
291 // calculate bounding rectangle of the item relative to the accessible parent
292 aBounds.X += aWindowScreenLoc.X - aParentScreenLoc.X;
293 aBounds.Y += aWindowScreenLoc.Y - aParentScreenLoc.Y;
299 return aBounds;
302 // -----------------------------------------------------------------------------
303 // XComponent
304 // -----------------------------------------------------------------------------
306 void SAL_CALL OAccessibleMenuItemComponent::disposing()
308 OAccessibleMenuBaseComponent::disposing();
310 m_pParent = NULL;
311 m_sAccessibleName = ::rtl::OUString();
312 m_sItemText = ::rtl::OUString();
315 // -----------------------------------------------------------------------------
316 // XAccessibleContext
317 // -----------------------------------------------------------------------------
319 sal_Int32 OAccessibleMenuItemComponent::getAccessibleChildCount() throw (RuntimeException)
321 OExternalLockGuard aGuard( this );
323 return 0;
326 // -----------------------------------------------------------------------------
328 Reference< XAccessible > OAccessibleMenuItemComponent::getAccessibleChild( sal_Int32 i ) throw (IndexOutOfBoundsException, RuntimeException)
330 OExternalLockGuard aGuard( this );
332 if ( i < 0 || i >= getAccessibleChildCount() )
333 throw IndexOutOfBoundsException();
335 return Reference< XAccessible >();
338 // -----------------------------------------------------------------------------
340 Reference< XAccessible > OAccessibleMenuItemComponent::getAccessibleParent( ) throw (RuntimeException)
342 OExternalLockGuard aGuard( this );
344 return m_pParent->GetAccessible();
347 // -----------------------------------------------------------------------------
349 sal_Int32 OAccessibleMenuItemComponent::getAccessibleIndexInParent( ) throw (RuntimeException)
351 OExternalLockGuard aGuard( this );
353 return m_nItemPos;
356 // -----------------------------------------------------------------------------
358 sal_Int16 OAccessibleMenuItemComponent::getAccessibleRole( ) throw (RuntimeException)
360 OExternalLockGuard aGuard( this );
362 return AccessibleRole::UNKNOWN;
365 // -----------------------------------------------------------------------------
367 ::rtl::OUString OAccessibleMenuItemComponent::getAccessibleDescription( ) throw (RuntimeException)
369 OExternalLockGuard aGuard( this );
371 ::rtl::OUString sDescription;
372 if ( m_pParent )
373 sDescription = m_pParent->GetHelpText( m_pParent->GetItemId( m_nItemPos ) );
375 return sDescription;
378 // -----------------------------------------------------------------------------
380 ::rtl::OUString OAccessibleMenuItemComponent::getAccessibleName( ) throw (RuntimeException)
382 OExternalLockGuard aGuard( this );
384 return m_sAccessibleName;
387 // -----------------------------------------------------------------------------
389 Reference< XAccessibleRelationSet > OAccessibleMenuItemComponent::getAccessibleRelationSet( ) throw (RuntimeException)
391 OExternalLockGuard aGuard( this );
393 utl::AccessibleRelationSetHelper* pRelationSetHelper = new utl::AccessibleRelationSetHelper;
394 Reference< XAccessibleRelationSet > xSet = pRelationSetHelper;
395 return xSet;
398 // -----------------------------------------------------------------------------
400 Locale OAccessibleMenuItemComponent::getLocale( ) throw (IllegalAccessibleComponentStateException, RuntimeException)
402 OExternalLockGuard aGuard( this );
404 return Application::GetSettings().GetLocale();
407 // -----------------------------------------------------------------------------
408 // XAccessibleComponent
409 // -----------------------------------------------------------------------------
411 Reference< XAccessible > OAccessibleMenuItemComponent::getAccessibleAtPoint( const awt::Point& ) throw (RuntimeException)
413 OExternalLockGuard aGuard( this );
415 return Reference< XAccessible >();
418 // -----------------------------------------------------------------------------
420 void OAccessibleMenuItemComponent::grabFocus( ) throw (RuntimeException)
422 // no focus for items
425 // -----------------------------------------------------------------------------
427 sal_Int32 OAccessibleMenuItemComponent::getForeground( ) throw (RuntimeException)
429 OExternalLockGuard aGuard( this );
431 sal_Int32 nColor = 0;
432 Reference< XAccessible > xParent = getAccessibleParent();
433 if ( xParent.is() )
435 Reference< XAccessibleComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
436 if ( xParentComp.is() )
437 nColor = xParentComp->getForeground();
440 return nColor;
443 // -----------------------------------------------------------------------------
445 sal_Int32 OAccessibleMenuItemComponent::getBackground( ) throw (RuntimeException)
447 OExternalLockGuard aGuard( this );
449 sal_Int32 nColor = 0;
450 Reference< XAccessible > xParent = getAccessibleParent();
451 if ( xParent.is() )
453 Reference< XAccessibleComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
454 if ( xParentComp.is() )
455 nColor = xParentComp->getBackground();
458 return nColor;
461 // -----------------------------------------------------------------------------
462 // XAccessibleExtendedComponent
463 // -----------------------------------------------------------------------------
465 Reference< awt::XFont > OAccessibleMenuItemComponent::getFont( ) throw (RuntimeException)
467 OExternalLockGuard aGuard( this );
469 Reference< awt::XFont > xFont;
470 Reference< XAccessible > xParent = getAccessibleParent();
471 if ( xParent.is() )
473 Reference< XAccessibleExtendedComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
474 if ( xParentComp.is() )
475 xFont = xParentComp->getFont();
478 return xFont;
481 // -----------------------------------------------------------------------------
483 ::rtl::OUString OAccessibleMenuItemComponent::getTitledBorderText( ) throw (RuntimeException)
485 OExternalLockGuard aGuard( this );
487 return ::rtl::OUString();
490 // -----------------------------------------------------------------------------
492 ::rtl::OUString OAccessibleMenuItemComponent::getToolTipText( ) throw (RuntimeException)
494 OExternalLockGuard aGuard( this );
496 ::rtl::OUString sRet;
497 if ( m_pParent )
498 sRet = m_pParent->GetTipHelpText( m_pParent->GetItemId( m_nItemPos ) );
500 return sRet;
503 // -----------------------------------------------------------------------------