merge the formfield patch from ooo-build
[ooovba.git] / accessibility / source / standard / accessiblemenuitemcomponent.cxx
blobb4b31cc262b43d796c85db83ccdc88374db6d417
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: accessiblemenuitemcomponent.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_accessibility.hxx"
33 #include <accessibility/standard/accessiblemenuitemcomponent.hxx>
36 #include <accessibility/helper/accresmgr.hxx>
37 #include <accessibility/helper/accessiblestrings.hrc>
38 #include <toolkit/awt/vclxwindows.hxx>
39 #include <toolkit/helper/externallock.hxx>
40 #include <toolkit/helper/convert.hxx>
42 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
43 #include <com/sun/star/accessibility/AccessibleRole.hpp>
44 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
45 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
46 #include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
48 #include <unotools/accessiblestatesethelper.hxx>
49 #include <unotools/accessiblerelationsethelper.hxx>
50 #include <cppuhelper/typeprovider.hxx>
51 #include <comphelper/sequence.hxx>
52 #include <comphelper/accessibletexthelper.hxx>
53 #include <vcl/svapp.hxx>
54 #include <vcl/window.hxx>
55 #include <vcl/menu.hxx>
56 #include <vcl/unohelp2.hxx>
59 using namespace ::com::sun::star::accessibility;
60 using namespace ::com::sun::star::uno;
61 using namespace ::com::sun::star::beans;
62 using namespace ::com::sun::star::lang;
63 using namespace ::com::sun::star;
64 using namespace ::comphelper;
67 // -----------------------------------------------------------------------------
68 // class OAccessibleMenuItemComponent
69 // -----------------------------------------------------------------------------
71 OAccessibleMenuItemComponent::OAccessibleMenuItemComponent( Menu* pParent, sal_uInt16 nItemPos, Menu* pMenu )
72 :OAccessibleMenuBaseComponent( pMenu )
73 ,m_pParent( pParent )
74 ,m_nItemPos( nItemPos )
76 m_sAccessibleName = GetAccessibleName();
77 m_sItemText = GetItemText();
80 // -----------------------------------------------------------------------------
82 OAccessibleMenuItemComponent::~OAccessibleMenuItemComponent()
86 // -----------------------------------------------------------------------------
88 sal_Bool OAccessibleMenuItemComponent::IsEnabled()
90 OExternalLockGuard aGuard( this );
92 sal_Bool bEnabled = sal_False;
93 if ( m_pParent )
94 bEnabled = m_pParent->IsItemEnabled( m_pParent->GetItemId( m_nItemPos ) );
96 return bEnabled;
99 // -----------------------------------------------------------------------------
101 sal_Bool OAccessibleMenuItemComponent::IsVisible()
103 sal_Bool bVisible = sal_False;
105 if ( m_pParent )
106 bVisible = m_pParent->IsItemPosVisible( m_nItemPos );
108 return bVisible;
111 // -----------------------------------------------------------------------------
113 void OAccessibleMenuItemComponent::Select()
115 // open the parent menu
116 Reference< XAccessible > xParent( getAccessibleParent() );
117 if ( xParent.is() )
119 OAccessibleMenuBaseComponent* pComp = static_cast< OAccessibleMenuBaseComponent* >( xParent.get() );
120 if ( pComp && pComp->getAccessibleRole() == AccessibleRole::MENU && !pComp->IsPopupMenuOpen() )
121 pComp->Click();
124 // highlight the menu item
125 if ( m_pParent )
126 m_pParent->HighlightItem( m_nItemPos );
129 // -----------------------------------------------------------------------------
131 void OAccessibleMenuItemComponent::DeSelect()
133 if ( m_pParent && IsSelected() )
134 m_pParent->DeHighlight();
137 // -----------------------------------------------------------------------------
139 void OAccessibleMenuItemComponent::Click()
141 // open the parent menu
142 Reference< XAccessible > xParent( getAccessibleParent() );
143 if ( xParent.is() )
145 OAccessibleMenuBaseComponent* pComp = static_cast< OAccessibleMenuBaseComponent* >( xParent.get() );
146 if ( pComp && pComp->getAccessibleRole() == AccessibleRole::MENU && !pComp->IsPopupMenuOpen() )
147 pComp->Click();
150 // click the menu item
151 if ( m_pParent )
153 Window* pWindow = m_pParent->GetWindow();
154 if ( pWindow )
156 // #102438# Menu items are not selectable
157 // Popup menus are executed asynchronously, triggered by a timer.
158 // As Menu::SelectItem only works, if the corresponding menu window is
159 // already created, we have to set the menu delay to 0, so
160 // that the popup menus are executed synchronously.
161 AllSettings aSettings = pWindow->GetSettings();
162 MouseSettings aMouseSettings = aSettings.GetMouseSettings();
163 ULONG nDelay = aMouseSettings.GetMenuDelay();
164 aMouseSettings.SetMenuDelay( 0 );
165 aSettings.SetMouseSettings( aMouseSettings );
166 pWindow->SetSettings( aSettings );
168 m_pParent->SelectItem( m_pParent->GetItemId( m_nItemPos ) );
170 // meanwhile the window pointer may be invalid
171 pWindow = m_pParent->GetWindow();
172 if ( pWindow )
174 // set the menu delay back to the old value
175 aSettings = pWindow->GetSettings();
176 aMouseSettings = aSettings.GetMouseSettings();
177 aMouseSettings.SetMenuDelay( nDelay );
178 aSettings.SetMouseSettings( aMouseSettings );
179 pWindow->SetSettings( aSettings );
185 // -----------------------------------------------------------------------------
187 void OAccessibleMenuItemComponent::SetItemPos( sal_uInt16 nItemPos )
189 m_nItemPos = nItemPos;
192 // -----------------------------------------------------------------------------
194 void OAccessibleMenuItemComponent::SetAccessibleName( const ::rtl::OUString& sAccessibleName )
196 if ( !m_sAccessibleName.equals( sAccessibleName ) )
198 Any aOldValue, aNewValue;
199 aOldValue <<= m_sAccessibleName;
200 aNewValue <<= sAccessibleName;
201 m_sAccessibleName = sAccessibleName;
202 NotifyAccessibleEvent( AccessibleEventId::NAME_CHANGED, aOldValue, aNewValue );
206 // -----------------------------------------------------------------------------
208 ::rtl::OUString OAccessibleMenuItemComponent::GetAccessibleName()
210 ::rtl::OUString sName;
211 if ( m_pParent )
213 sal_uInt16 nItemId = m_pParent->GetItemId( m_nItemPos );
214 sName = m_pParent->GetAccessibleName( nItemId );
215 if ( sName.getLength() == 0 )
216 sName = m_pParent->GetItemText( nItemId );
217 sName = OutputDevice::GetNonMnemonicString( sName );
220 return sName;
223 // -----------------------------------------------------------------------------
225 void OAccessibleMenuItemComponent::SetItemText( const ::rtl::OUString& sItemText )
227 Any aOldValue, aNewValue;
228 if ( OCommonAccessibleText::implInitTextChangedEvent( m_sItemText, sItemText, aOldValue, aNewValue ) )
230 m_sItemText = sItemText;
231 NotifyAccessibleEvent( AccessibleEventId::TEXT_CHANGED, aOldValue, aNewValue );
235 // -----------------------------------------------------------------------------
237 ::rtl::OUString OAccessibleMenuItemComponent::GetItemText()
239 ::rtl::OUString sText;
240 if ( m_pParent )
241 sText = OutputDevice::GetNonMnemonicString( m_pParent->GetItemText( m_pParent->GetItemId( m_nItemPos ) ) );
243 return sText;
246 // -----------------------------------------------------------------------------
248 void OAccessibleMenuItemComponent::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
250 if ( IsEnabled() )
252 rStateSet.AddState( AccessibleStateType::ENABLED );
253 rStateSet.AddState( AccessibleStateType::SENSITIVE );
256 if ( IsVisible() )
258 rStateSet.AddState( AccessibleStateType::VISIBLE );
259 rStateSet.AddState( AccessibleStateType::SHOWING );
262 rStateSet.AddState( AccessibleStateType::OPAQUE );
265 // -----------------------------------------------------------------------------
266 // OCommonAccessibleComponent
267 // -----------------------------------------------------------------------------
269 awt::Rectangle OAccessibleMenuItemComponent::implGetBounds() throw (RuntimeException)
271 awt::Rectangle aBounds( 0, 0, 0, 0 );
273 if ( m_pParent )
275 // get bounding rectangle of the item relative to the containing window
276 aBounds = AWTRectangle( m_pParent->GetBoundingRectangle( m_nItemPos ) );
278 // get position of containing window in screen coordinates
279 Window* pWindow = m_pParent->GetWindow();
280 if ( pWindow )
282 Rectangle aRect = pWindow->GetWindowExtentsRelative( NULL );
283 awt::Point aWindowScreenLoc = AWTPoint( aRect.TopLeft() );
285 // get position of accessible parent in screen coordinates
286 Reference< XAccessible > xParent = getAccessibleParent();
287 if ( xParent.is() )
289 Reference< XAccessibleComponent > xParentComponent( xParent->getAccessibleContext(), UNO_QUERY );
290 if ( xParentComponent.is() )
292 awt::Point aParentScreenLoc = xParentComponent->getLocationOnScreen();
294 // calculate bounding rectangle of the item relative to the accessible parent
295 aBounds.X += aWindowScreenLoc.X - aParentScreenLoc.X;
296 aBounds.Y += aWindowScreenLoc.Y - aParentScreenLoc.Y;
302 return aBounds;
305 // -----------------------------------------------------------------------------
306 // XComponent
307 // -----------------------------------------------------------------------------
309 void SAL_CALL OAccessibleMenuItemComponent::disposing()
311 OAccessibleMenuBaseComponent::disposing();
313 m_pParent = NULL;
314 m_sAccessibleName = ::rtl::OUString();
315 m_sItemText = ::rtl::OUString();
318 // -----------------------------------------------------------------------------
319 // XAccessibleContext
320 // -----------------------------------------------------------------------------
322 sal_Int32 OAccessibleMenuItemComponent::getAccessibleChildCount() throw (RuntimeException)
324 OExternalLockGuard aGuard( this );
326 return 0;
329 // -----------------------------------------------------------------------------
331 Reference< XAccessible > OAccessibleMenuItemComponent::getAccessibleChild( sal_Int32 i ) throw (IndexOutOfBoundsException, RuntimeException)
333 OExternalLockGuard aGuard( this );
335 if ( i < 0 || i >= getAccessibleChildCount() )
336 throw IndexOutOfBoundsException();
338 return Reference< XAccessible >();
341 // -----------------------------------------------------------------------------
343 Reference< XAccessible > OAccessibleMenuItemComponent::getAccessibleParent( ) throw (RuntimeException)
345 OExternalLockGuard aGuard( this );
347 return m_pParent->GetAccessible();
350 // -----------------------------------------------------------------------------
352 sal_Int32 OAccessibleMenuItemComponent::getAccessibleIndexInParent( ) throw (RuntimeException)
354 OExternalLockGuard aGuard( this );
356 return m_nItemPos;
359 // -----------------------------------------------------------------------------
361 sal_Int16 OAccessibleMenuItemComponent::getAccessibleRole( ) throw (RuntimeException)
363 OExternalLockGuard aGuard( this );
365 return AccessibleRole::UNKNOWN;
368 // -----------------------------------------------------------------------------
370 ::rtl::OUString OAccessibleMenuItemComponent::getAccessibleDescription( ) throw (RuntimeException)
372 OExternalLockGuard aGuard( this );
374 ::rtl::OUString sDescription;
375 if ( m_pParent )
376 sDescription = m_pParent->GetHelpText( m_pParent->GetItemId( m_nItemPos ) );
378 return sDescription;
381 // -----------------------------------------------------------------------------
383 ::rtl::OUString OAccessibleMenuItemComponent::getAccessibleName( ) throw (RuntimeException)
385 OExternalLockGuard aGuard( this );
387 return m_sAccessibleName;
390 // -----------------------------------------------------------------------------
392 Reference< XAccessibleRelationSet > OAccessibleMenuItemComponent::getAccessibleRelationSet( ) throw (RuntimeException)
394 OExternalLockGuard aGuard( this );
396 utl::AccessibleRelationSetHelper* pRelationSetHelper = new utl::AccessibleRelationSetHelper;
397 Reference< XAccessibleRelationSet > xSet = pRelationSetHelper;
398 return xSet;
401 // -----------------------------------------------------------------------------
403 Locale OAccessibleMenuItemComponent::getLocale( ) throw (IllegalAccessibleComponentStateException, RuntimeException)
405 OExternalLockGuard aGuard( this );
407 return Application::GetSettings().GetLocale();
410 // -----------------------------------------------------------------------------
411 // XAccessibleComponent
412 // -----------------------------------------------------------------------------
414 Reference< XAccessible > OAccessibleMenuItemComponent::getAccessibleAtPoint( const awt::Point& ) throw (RuntimeException)
416 OExternalLockGuard aGuard( this );
418 return Reference< XAccessible >();
421 // -----------------------------------------------------------------------------
423 void OAccessibleMenuItemComponent::grabFocus( ) throw (RuntimeException)
425 // no focus for items
428 // -----------------------------------------------------------------------------
430 sal_Int32 OAccessibleMenuItemComponent::getForeground( ) throw (RuntimeException)
432 OExternalLockGuard aGuard( this );
434 sal_Int32 nColor = 0;
435 Reference< XAccessible > xParent = getAccessibleParent();
436 if ( xParent.is() )
438 Reference< XAccessibleComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
439 if ( xParentComp.is() )
440 nColor = xParentComp->getForeground();
443 return nColor;
446 // -----------------------------------------------------------------------------
448 sal_Int32 OAccessibleMenuItemComponent::getBackground( ) throw (RuntimeException)
450 OExternalLockGuard aGuard( this );
452 sal_Int32 nColor = 0;
453 Reference< XAccessible > xParent = getAccessibleParent();
454 if ( xParent.is() )
456 Reference< XAccessibleComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
457 if ( xParentComp.is() )
458 nColor = xParentComp->getBackground();
461 return nColor;
464 // -----------------------------------------------------------------------------
465 // XAccessibleExtendedComponent
466 // -----------------------------------------------------------------------------
468 Reference< awt::XFont > OAccessibleMenuItemComponent::getFont( ) throw (RuntimeException)
470 OExternalLockGuard aGuard( this );
472 Reference< awt::XFont > xFont;
473 Reference< XAccessible > xParent = getAccessibleParent();
474 if ( xParent.is() )
476 Reference< XAccessibleExtendedComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
477 if ( xParentComp.is() )
478 xFont = xParentComp->getFont();
481 return xFont;
484 // -----------------------------------------------------------------------------
486 ::rtl::OUString OAccessibleMenuItemComponent::getTitledBorderText( ) throw (RuntimeException)
488 OExternalLockGuard aGuard( this );
490 return ::rtl::OUString();
493 // -----------------------------------------------------------------------------
495 ::rtl::OUString OAccessibleMenuItemComponent::getToolTipText( ) throw (RuntimeException)
497 OExternalLockGuard aGuard( this );
499 ::rtl::OUString sRet;
500 if ( m_pParent )
501 sRet = m_pParent->GetTipHelpText( m_pParent->GetItemId( m_nItemPos ) );
503 return sRet;
506 // -----------------------------------------------------------------------------