1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
)
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
;
81 bEnabled
= m_pParent
->IsItemEnabled( m_pParent
->GetItemId( m_nItemPos
) );
86 // -----------------------------------------------------------------------------
88 sal_Bool
OAccessibleMenuItemComponent::IsVisible()
90 sal_Bool bVisible
= sal_False
;
93 bVisible
= m_pParent
->IsItemPosVisible( m_nItemPos
);
98 // -----------------------------------------------------------------------------
100 void OAccessibleMenuItemComponent::Select()
102 // open the parent menu
103 Reference
< XAccessible
> xParent( getAccessibleParent() );
106 OAccessibleMenuBaseComponent
* pComp
= static_cast< OAccessibleMenuBaseComponent
* >( xParent
.get() );
107 if ( pComp
&& pComp
->getAccessibleRole() == AccessibleRole::MENU
&& !pComp
->IsPopupMenuOpen() )
111 // highlight the menu item
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() );
132 OAccessibleMenuBaseComponent
* pComp
= static_cast< OAccessibleMenuBaseComponent
* >( xParent
.get() );
133 if ( pComp
&& pComp
->getAccessibleRole() == AccessibleRole::MENU
&& !pComp
->IsPopupMenuOpen() )
137 // click the menu item
140 Window
* pWindow
= m_pParent
->GetWindow();
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();
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()
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
);
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()
228 sText
= OutputDevice::GetNonMnemonicString( m_pParent
->GetItemText( m_pParent
->GetItemId( m_nItemPos
) ) );
233 // -----------------------------------------------------------------------------
235 void OAccessibleMenuItemComponent::FillAccessibleStateSet( utl::AccessibleStateSetHelper
& rStateSet
)
239 rStateSet
.AddState( AccessibleStateType::ENABLED
);
240 rStateSet
.AddState( AccessibleStateType::SENSITIVE
);
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 );
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();
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();
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
;
292 // -----------------------------------------------------------------------------
294 // -----------------------------------------------------------------------------
296 void SAL_CALL
OAccessibleMenuItemComponent::disposing()
298 OAccessibleMenuBaseComponent::disposing();
301 m_sAccessibleName
= OUString();
302 m_sItemText
= OUString();
305 // -----------------------------------------------------------------------------
306 // XAccessibleContext
307 // -----------------------------------------------------------------------------
309 sal_Int32
OAccessibleMenuItemComponent::getAccessibleChildCount() throw (RuntimeException
)
311 OExternalLockGuard
aGuard( this );
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 );
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
;
363 sDescription
= m_pParent
->GetHelpText( m_pParent
->GetItemId( m_nItemPos
) );
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
;
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();
425 Reference
< XAccessibleComponent
> xParentComp( xParent
->getAccessibleContext(), UNO_QUERY
);
426 if ( xParentComp
.is() )
427 nColor
= xParentComp
->getForeground();
433 // -----------------------------------------------------------------------------
435 sal_Int32
OAccessibleMenuItemComponent::getBackground( ) throw (RuntimeException
)
437 OExternalLockGuard
aGuard( this );
439 sal_Int32 nColor
= 0;
440 Reference
< XAccessible
> xParent
= getAccessibleParent();
443 Reference
< XAccessibleComponent
> xParentComp( xParent
->getAccessibleContext(), UNO_QUERY
);
444 if ( xParentComp
.is() )
445 nColor
= xParentComp
->getBackground();
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();
463 Reference
< XAccessibleExtendedComponent
> xParentComp( xParent
->getAccessibleContext(), UNO_QUERY
);
464 if ( xParentComp
.is() )
465 xFont
= xParentComp
->getFont();
471 // -----------------------------------------------------------------------------
473 OUString
OAccessibleMenuItemComponent::getTitledBorderText( ) throw (RuntimeException
)
475 OExternalLockGuard
aGuard( this );
480 // -----------------------------------------------------------------------------
482 OUString
OAccessibleMenuItemComponent::getToolTipText( ) throw (RuntimeException
)
484 OExternalLockGuard
aGuard( this );
488 sRet
= m_pParent
->GetTipHelpText( m_pParent
->GetItemId( m_nItemPos
) );
493 // -----------------------------------------------------------------------------
495 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */