Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / accessibility / source / standard / accessiblemenuitemcomponent.cxx
blobe458e95de2eeee385ccbe3bb92ef678680c550f0
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/accessiblemenuitemcomponent.hxx>
23 #include <accessibility/helper/accresmgr.hxx>
24 #include <accessibility/helper/accessiblestrings.hrc>
25 #include <toolkit/awt/vclxwindows.hxx>
26 #include <toolkit/helper/externallock.hxx>
27 #include <toolkit/helper/convert.hxx>
29 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
30 #include <com/sun/star/accessibility/AccessibleRole.hpp>
31 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
32 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
33 #include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
35 #include <unotools/accessiblestatesethelper.hxx>
36 #include <unotools/accessiblerelationsethelper.hxx>
37 #include <cppuhelper/typeprovider.hxx>
38 #include <comphelper/sequence.hxx>
39 #include <comphelper/accessibletexthelper.hxx>
40 #include <vcl/svapp.hxx>
41 #include <vcl/window.hxx>
42 #include <vcl/menu.hxx>
43 #include <vcl/unohelp2.hxx>
46 using namespace ::com::sun::star::accessibility;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::beans;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star;
51 using namespace ::comphelper;
54 // -----------------------------------------------------------------------------
55 // class OAccessibleMenuItemComponent
56 // -----------------------------------------------------------------------------
58 OAccessibleMenuItemComponent::OAccessibleMenuItemComponent( Menu* pParent, sal_uInt16 nItemPos, Menu* pMenu )
59 :OAccessibleMenuBaseComponent( pMenu )
60 ,m_pParent( pParent )
61 ,m_nItemPos( nItemPos )
63 m_sAccessibleName = GetAccessibleName();
64 m_sItemText = GetItemText();
67 // -----------------------------------------------------------------------------
69 OAccessibleMenuItemComponent::~OAccessibleMenuItemComponent()
73 // -----------------------------------------------------------------------------
75 sal_Bool OAccessibleMenuItemComponent::IsEnabled()
77 OExternalLockGuard aGuard( this );
79 sal_Bool bEnabled = sal_False;
80 if ( m_pParent )
81 bEnabled = m_pParent->IsItemEnabled( m_pParent->GetItemId( m_nItemPos ) );
83 return bEnabled;
86 // -----------------------------------------------------------------------------
88 sal_Bool OAccessibleMenuItemComponent::IsVisible()
90 sal_Bool bVisible = sal_False;
92 if ( m_pParent )
93 bVisible = m_pParent->IsItemPosVisible( m_nItemPos );
95 return bVisible;
98 // -----------------------------------------------------------------------------
100 void OAccessibleMenuItemComponent::Select()
102 // open the parent menu
103 Reference< XAccessible > xParent( getAccessibleParent() );
104 if ( xParent.is() )
106 OAccessibleMenuBaseComponent* pComp = static_cast< OAccessibleMenuBaseComponent* >( xParent.get() );
107 if ( pComp && pComp->getAccessibleRole() == AccessibleRole::MENU && !pComp->IsPopupMenuOpen() )
108 pComp->Click();
111 // highlight the menu item
112 if ( m_pParent )
113 m_pParent->HighlightItem( m_nItemPos );
116 // -----------------------------------------------------------------------------
118 void OAccessibleMenuItemComponent::DeSelect()
120 if ( m_pParent && IsSelected() )
121 m_pParent->DeHighlight();
124 // -----------------------------------------------------------------------------
126 void OAccessibleMenuItemComponent::Click()
128 // open the parent menu
129 Reference< XAccessible > xParent( getAccessibleParent() );
130 if ( xParent.is() )
132 OAccessibleMenuBaseComponent* pComp = static_cast< OAccessibleMenuBaseComponent* >( xParent.get() );
133 if ( pComp && pComp->getAccessibleRole() == AccessibleRole::MENU && !pComp->IsPopupMenuOpen() )
134 pComp->Click();
137 // click the menu item
138 if ( m_pParent )
140 Window* pWindow = m_pParent->GetWindow();
141 if ( pWindow )
143 // #102438# Menu items are not selectable
144 // Popup menus are executed asynchronously, triggered by a timer.
145 // As Menu::SelectItem only works, if the corresponding menu window is
146 // already created, we have to set the menu delay to 0, so
147 // that the popup menus are executed synchronously.
148 AllSettings aSettings = pWindow->GetSettings();
149 MouseSettings aMouseSettings = aSettings.GetMouseSettings();
150 sal_uLong nDelay = aMouseSettings.GetMenuDelay();
151 aMouseSettings.SetMenuDelay( 0 );
152 aSettings.SetMouseSettings( aMouseSettings );
153 pWindow->SetSettings( aSettings );
155 m_pParent->SelectItem( m_pParent->GetItemId( m_nItemPos ) );
157 // meanwhile the window pointer may be invalid
158 pWindow = m_pParent->GetWindow();
159 if ( pWindow )
161 // set the menu delay back to the old value
162 aSettings = pWindow->GetSettings();
163 aMouseSettings = aSettings.GetMouseSettings();
164 aMouseSettings.SetMenuDelay( nDelay );
165 aSettings.SetMouseSettings( aMouseSettings );
166 pWindow->SetSettings( aSettings );
172 // -----------------------------------------------------------------------------
174 void OAccessibleMenuItemComponent::SetItemPos( sal_uInt16 nItemPos )
176 m_nItemPos = nItemPos;
179 // -----------------------------------------------------------------------------
181 void OAccessibleMenuItemComponent::SetAccessibleName( const OUString& sAccessibleName )
183 if ( !m_sAccessibleName.equals( sAccessibleName ) )
185 Any aOldValue, aNewValue;
186 aOldValue <<= m_sAccessibleName;
187 aNewValue <<= sAccessibleName;
188 m_sAccessibleName = sAccessibleName;
189 NotifyAccessibleEvent( AccessibleEventId::NAME_CHANGED, aOldValue, aNewValue );
193 // -----------------------------------------------------------------------------
195 OUString OAccessibleMenuItemComponent::GetAccessibleName()
197 OUString sName;
198 if ( m_pParent )
200 sal_uInt16 nItemId = m_pParent->GetItemId( m_nItemPos );
201 sName = m_pParent->GetAccessibleName( nItemId );
202 if ( sName.isEmpty() )
203 sName = m_pParent->GetItemText( nItemId );
204 sName = OutputDevice::GetNonMnemonicString( sName );
207 return sName;
210 // -----------------------------------------------------------------------------
212 void OAccessibleMenuItemComponent::SetItemText( const OUString& sItemText )
214 Any aOldValue, aNewValue;
215 if ( OCommonAccessibleText::implInitTextChangedEvent( m_sItemText, sItemText, aOldValue, aNewValue ) )
217 m_sItemText = sItemText;
218 NotifyAccessibleEvent( AccessibleEventId::TEXT_CHANGED, aOldValue, aNewValue );
222 // -----------------------------------------------------------------------------
224 OUString OAccessibleMenuItemComponent::GetItemText()
226 OUString sText;
227 if ( m_pParent )
228 sText = OutputDevice::GetNonMnemonicString( m_pParent->GetItemText( m_pParent->GetItemId( m_nItemPos ) ) );
230 return sText;
233 // -----------------------------------------------------------------------------
235 void OAccessibleMenuItemComponent::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
237 if ( IsEnabled() )
239 rStateSet.AddState( AccessibleStateType::ENABLED );
240 rStateSet.AddState( AccessibleStateType::SENSITIVE );
243 if ( IsVisible() )
245 rStateSet.AddState( AccessibleStateType::VISIBLE );
246 rStateSet.AddState( AccessibleStateType::SHOWING );
249 rStateSet.AddState( AccessibleStateType::OPAQUE );
252 // -----------------------------------------------------------------------------
253 // OCommonAccessibleComponent
254 // -----------------------------------------------------------------------------
256 awt::Rectangle OAccessibleMenuItemComponent::implGetBounds() throw (RuntimeException)
258 awt::Rectangle aBounds( 0, 0, 0, 0 );
260 if ( m_pParent )
262 // get bounding rectangle of the item relative to the containing window
263 aBounds = AWTRectangle( m_pParent->GetBoundingRectangle( m_nItemPos ) );
265 // get position of containing window in screen coordinates
266 Window* pWindow = m_pParent->GetWindow();
267 if ( pWindow )
269 Rectangle aRect = pWindow->GetWindowExtentsRelative( NULL );
270 awt::Point aWindowScreenLoc = AWTPoint( aRect.TopLeft() );
272 // get position of accessible parent in screen coordinates
273 Reference< XAccessible > xParent = getAccessibleParent();
274 if ( xParent.is() )
276 Reference< XAccessibleComponent > xParentComponent( xParent->getAccessibleContext(), UNO_QUERY );
277 if ( xParentComponent.is() )
279 awt::Point aParentScreenLoc = xParentComponent->getLocationOnScreen();
281 // calculate bounding rectangle of the item relative to the accessible parent
282 aBounds.X += aWindowScreenLoc.X - aParentScreenLoc.X;
283 aBounds.Y += aWindowScreenLoc.Y - aParentScreenLoc.Y;
289 return aBounds;
292 // -----------------------------------------------------------------------------
293 // XComponent
294 // -----------------------------------------------------------------------------
296 void SAL_CALL OAccessibleMenuItemComponent::disposing()
298 OAccessibleMenuBaseComponent::disposing();
300 m_pParent = NULL;
301 m_sAccessibleName = OUString();
302 m_sItemText = OUString();
305 // -----------------------------------------------------------------------------
306 // XAccessibleContext
307 // -----------------------------------------------------------------------------
309 sal_Int32 OAccessibleMenuItemComponent::getAccessibleChildCount() throw (RuntimeException)
311 OExternalLockGuard aGuard( this );
313 return 0;
316 // -----------------------------------------------------------------------------
318 Reference< XAccessible > OAccessibleMenuItemComponent::getAccessibleChild( sal_Int32 i ) throw (IndexOutOfBoundsException, RuntimeException)
320 OExternalLockGuard aGuard( this );
322 if ( i < 0 || i >= getAccessibleChildCount() )
323 throw IndexOutOfBoundsException();
325 return Reference< XAccessible >();
328 // -----------------------------------------------------------------------------
330 Reference< XAccessible > OAccessibleMenuItemComponent::getAccessibleParent( ) throw (RuntimeException)
332 OExternalLockGuard aGuard( this );
334 return m_pParent->GetAccessible();
337 // -----------------------------------------------------------------------------
339 sal_Int32 OAccessibleMenuItemComponent::getAccessibleIndexInParent( ) throw (RuntimeException)
341 OExternalLockGuard aGuard( this );
343 return m_nItemPos;
346 // -----------------------------------------------------------------------------
348 sal_Int16 OAccessibleMenuItemComponent::getAccessibleRole( ) throw (RuntimeException)
350 OExternalLockGuard aGuard( this );
352 return AccessibleRole::UNKNOWN;
355 // -----------------------------------------------------------------------------
357 OUString OAccessibleMenuItemComponent::getAccessibleDescription( ) throw (RuntimeException)
359 OExternalLockGuard aGuard( this );
361 OUString sDescription;
362 if ( m_pParent )
363 sDescription = m_pParent->GetHelpText( m_pParent->GetItemId( m_nItemPos ) );
365 return sDescription;
368 // -----------------------------------------------------------------------------
370 OUString OAccessibleMenuItemComponent::getAccessibleName( ) throw (RuntimeException)
372 OExternalLockGuard aGuard( this );
374 return m_sAccessibleName;
377 // -----------------------------------------------------------------------------
379 Reference< XAccessibleRelationSet > OAccessibleMenuItemComponent::getAccessibleRelationSet( ) throw (RuntimeException)
381 OExternalLockGuard aGuard( this );
383 utl::AccessibleRelationSetHelper* pRelationSetHelper = new utl::AccessibleRelationSetHelper;
384 Reference< XAccessibleRelationSet > xSet = pRelationSetHelper;
385 return xSet;
388 // -----------------------------------------------------------------------------
390 Locale OAccessibleMenuItemComponent::getLocale( ) throw (IllegalAccessibleComponentStateException, RuntimeException)
392 OExternalLockGuard aGuard( this );
394 return Application::GetSettings().GetLanguageTag().getLocale();
397 // -----------------------------------------------------------------------------
398 // XAccessibleComponent
399 // -----------------------------------------------------------------------------
401 Reference< XAccessible > OAccessibleMenuItemComponent::getAccessibleAtPoint( const awt::Point& ) throw (RuntimeException)
403 OExternalLockGuard aGuard( this );
405 return Reference< XAccessible >();
408 // -----------------------------------------------------------------------------
410 void OAccessibleMenuItemComponent::grabFocus( ) throw (RuntimeException)
412 // no focus for items
415 // -----------------------------------------------------------------------------
417 sal_Int32 OAccessibleMenuItemComponent::getForeground( ) throw (RuntimeException)
419 OExternalLockGuard aGuard( this );
421 sal_Int32 nColor = 0;
422 Reference< XAccessible > xParent = getAccessibleParent();
423 if ( xParent.is() )
425 Reference< XAccessibleComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
426 if ( xParentComp.is() )
427 nColor = xParentComp->getForeground();
430 return nColor;
433 // -----------------------------------------------------------------------------
435 sal_Int32 OAccessibleMenuItemComponent::getBackground( ) throw (RuntimeException)
437 OExternalLockGuard aGuard( this );
439 sal_Int32 nColor = 0;
440 Reference< XAccessible > xParent = getAccessibleParent();
441 if ( xParent.is() )
443 Reference< XAccessibleComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
444 if ( xParentComp.is() )
445 nColor = xParentComp->getBackground();
448 return nColor;
451 // -----------------------------------------------------------------------------
452 // XAccessibleExtendedComponent
453 // -----------------------------------------------------------------------------
455 Reference< awt::XFont > OAccessibleMenuItemComponent::getFont( ) throw (RuntimeException)
457 OExternalLockGuard aGuard( this );
459 Reference< awt::XFont > xFont;
460 Reference< XAccessible > xParent = getAccessibleParent();
461 if ( xParent.is() )
463 Reference< XAccessibleExtendedComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
464 if ( xParentComp.is() )
465 xFont = xParentComp->getFont();
468 return xFont;
471 // -----------------------------------------------------------------------------
473 OUString OAccessibleMenuItemComponent::getTitledBorderText( ) throw (RuntimeException)
475 OExternalLockGuard aGuard( this );
477 return OUString();
480 // -----------------------------------------------------------------------------
482 OUString OAccessibleMenuItemComponent::getToolTipText( ) throw (RuntimeException)
484 OExternalLockGuard aGuard( this );
486 OUString sRet;
487 if ( m_pParent )
488 sRet = m_pParent->GetTipHelpText( m_pParent->GetItemId( m_nItemPos ) );
490 return sRet;
493 // -----------------------------------------------------------------------------
495 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */