Branch libreoffice-5-0-4
[LibreOffice.git] / accessibility / source / standard / vclxaccessiblemenuitem.cxx
blob05c2bb0b58537e12066a977e75aef49a6a7e695d
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/vclxaccessiblemenuitem.hxx>
21 #include <accessibility/helper/accresmgr.hxx>
22 #include <accessibility/helper/accessiblestrings.hrc>
23 #include <toolkit/helper/convert.hxx>
24 #include <accessibility/helper/characterattributeshelper.hxx>
25 #include <comphelper/accessiblekeybindinghelper.hxx>
26 #include <com/sun/star/awt/KeyModifier.hpp>
28 #include <com/sun/star/accessibility/AccessibleRole.hpp>
29 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
30 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
31 #include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
32 #include <unotools/accessiblestatesethelper.hxx>
33 #include <comphelper/sequence.hxx>
34 #include <vcl/svapp.hxx>
35 #include <vcl/window.hxx>
36 #include <vcl/menu.hxx>
37 #include <vcl/unohelp2.hxx>
38 #include <vcl/settings.hxx>
40 using namespace ::com::sun::star::accessibility;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::beans;
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star;
45 using namespace ::comphelper;
49 // class VCLXAccessibleMenuItem
52 VCLXAccessibleMenuItem::VCLXAccessibleMenuItem( Menu* pParent, sal_uInt16 nItemPos, Menu* pMenu )
53 :OAccessibleMenuItemComponent( pParent, nItemPos, pMenu )
59 VCLXAccessibleMenuItem::~VCLXAccessibleMenuItem()
65 bool VCLXAccessibleMenuItem::IsFocused()
67 return IsHighlighted();
72 bool VCLXAccessibleMenuItem::IsSelected()
74 return IsHighlighted();
79 bool VCLXAccessibleMenuItem::IsChecked()
81 bool bChecked = false;
83 if ( m_pParent )
85 sal_uInt16 nItemId = m_pParent->GetItemId( m_nItemPos );
86 if ( m_pParent->IsItemChecked( nItemId ) )
87 bChecked = true;
90 return bChecked;
95 bool VCLXAccessibleMenuItem::IsHighlighted()
97 bool bHighlighted = false;
99 if ( m_pParent && m_pParent->IsHighlighted( m_nItemPos ) )
100 bHighlighted = true;
102 return bHighlighted;
107 void VCLXAccessibleMenuItem::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
109 OAccessibleMenuItemComponent::FillAccessibleStateSet( rStateSet );
111 rStateSet.AddState( AccessibleStateType::FOCUSABLE );
113 if ( IsFocused() )
114 rStateSet.AddState( AccessibleStateType::FOCUSED );
116 rStateSet.AddState( AccessibleStateType::SELECTABLE );
118 if ( IsSelected() )
119 rStateSet.AddState( AccessibleStateType::SELECTED );
121 if ( IsChecked() )
122 rStateSet.AddState( AccessibleStateType::CHECKED );
126 // OCommonAccessibleText
129 OUString VCLXAccessibleMenuItem::implGetText()
131 return m_sItemText;
136 Locale VCLXAccessibleMenuItem::implGetLocale()
138 return Application::GetSettings().GetLanguageTag().getLocale();
143 void VCLXAccessibleMenuItem::implGetSelection( sal_Int32& nStartIndex, sal_Int32& nEndIndex )
145 nStartIndex = 0;
146 nEndIndex = 0;
150 // XInterface
153 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleMenuItem, OAccessibleMenuItemComponent, VCLXAccessibleMenuItem_BASE )
156 // XTypeProvider
159 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleMenuItem, OAccessibleMenuItemComponent, VCLXAccessibleMenuItem_BASE )
162 // XServiceInfo
165 OUString VCLXAccessibleMenuItem::getImplementationName() throw (RuntimeException, std::exception)
167 return OUString( "com.sun.star.comp.toolkit.AccessibleMenuItem" );
172 Sequence< OUString > VCLXAccessibleMenuItem::getSupportedServiceNames() throw (RuntimeException, std::exception)
174 Sequence< OUString > aNames(1);
175 aNames[0] = "com.sun.star.awt.AccessibleMenuItem";
176 return aNames;
180 // XAccessibleContext
183 sal_Int16 VCLXAccessibleMenuItem::getAccessibleRole( ) throw (RuntimeException, std::exception)
185 OExternalLockGuard aGuard( this );
186 // IA2 CWS. MT: We had the additional roles in UAA for ever, but never used them anywhere.
187 // Looks reasonable, but need to verify in Orca and VoiceOver.
188 sal_Int16 nRole = AccessibleRole::MENU_ITEM;
189 if ( m_pParent )
191 sal_uInt16 nItemId = m_pParent->GetItemId( m_nItemPos );
192 MenuItemBits nItemBits = m_pParent->GetItemBits(nItemId);
193 if( nItemBits & MenuItemBits::RADIOCHECK)
194 nRole = AccessibleRole::RADIO_MENU_ITEM;
195 else if( nItemBits & MenuItemBits::CHECKABLE)
196 nRole = AccessibleRole::CHECK_MENU_ITEM;
198 return nRole;
202 // XAccessibleText
205 sal_Int32 VCLXAccessibleMenuItem::getCaretPosition() throw (RuntimeException, std::exception)
207 OExternalLockGuard aGuard( this );
209 return -1;
214 sal_Bool VCLXAccessibleMenuItem::setCaretPosition( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
217 OExternalLockGuard aGuard( this );
219 if ( !implIsValidRange( nIndex, nIndex, implGetText().getLength() ) )
220 throw IndexOutOfBoundsException();
222 return false;
227 sal_Unicode VCLXAccessibleMenuItem::getCharacter( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
229 OExternalLockGuard aGuard( this );
231 return OCommonAccessibleText::getCharacter( nIndex );
236 Sequence< PropertyValue > VCLXAccessibleMenuItem::getCharacterAttributes( sal_Int32 nIndex, const Sequence< OUString >& aRequestedAttributes ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
238 OExternalLockGuard aGuard( this );
240 OUString sText( implGetText() );
242 if ( !implIsValidIndex( nIndex, sText.getLength() ) )
243 throw IndexOutOfBoundsException();
245 vcl::Font aFont = Application::GetSettings().GetStyleSettings().GetMenuFont();
246 sal_Int32 nBackColor = getBackground();
247 sal_Int32 nColor = getForeground();
248 return CharacterAttributesHelper( aFont, nBackColor, nColor )
249 .GetCharacterAttributes( aRequestedAttributes );
254 awt::Rectangle VCLXAccessibleMenuItem::getCharacterBounds( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
256 OExternalLockGuard aGuard( this );
258 if ( !implIsValidIndex( nIndex, implGetText().getLength() ) )
259 throw IndexOutOfBoundsException();
261 awt::Rectangle aBounds( 0, 0, 0, 0 );
262 if ( m_pParent )
264 sal_uInt16 nItemId = m_pParent->GetItemId( m_nItemPos );
265 Rectangle aItemRect = m_pParent->GetBoundingRectangle( m_nItemPos );
266 Rectangle aCharRect = m_pParent->GetCharacterBounds( nItemId, nIndex );
267 aCharRect.Move( -aItemRect.Left(), -aItemRect.Top() );
268 aBounds = AWTRectangle( aCharRect );
271 return aBounds;
276 sal_Int32 VCLXAccessibleMenuItem::getCharacterCount() throw (RuntimeException, std::exception)
278 OExternalLockGuard aGuard( this );
280 return OCommonAccessibleText::getCharacterCount();
285 sal_Int32 VCLXAccessibleMenuItem::getIndexAtPoint( const awt::Point& aPoint ) throw (RuntimeException, std::exception)
287 OExternalLockGuard aGuard( this );
289 sal_Int32 nIndex = -1;
290 if ( m_pParent )
292 sal_uInt16 nItemId = 0;
293 Rectangle aItemRect = m_pParent->GetBoundingRectangle( m_nItemPos );
294 Point aPnt( VCLPoint( aPoint ) );
295 aPnt += aItemRect.TopLeft();
296 sal_Int32 nI = m_pParent->GetIndexForPoint( aPnt, nItemId );
297 if ( nI != -1 && m_pParent->GetItemId( m_nItemPos ) == nItemId )
298 nIndex = nI;
301 return nIndex;
306 OUString VCLXAccessibleMenuItem::getSelectedText() throw (RuntimeException, std::exception)
308 OExternalLockGuard aGuard( this );
310 return OCommonAccessibleText::getSelectedText();
315 sal_Int32 VCLXAccessibleMenuItem::getSelectionStart() throw (RuntimeException, std::exception)
317 OExternalLockGuard aGuard( this );
319 return OCommonAccessibleText::getSelectionStart();
324 sal_Int32 VCLXAccessibleMenuItem::getSelectionEnd() throw (RuntimeException, std::exception)
326 OExternalLockGuard aGuard( this );
328 return OCommonAccessibleText::getSelectionEnd();
333 sal_Bool VCLXAccessibleMenuItem::setSelection( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
335 OExternalLockGuard aGuard( this );
337 if ( !implIsValidRange( nStartIndex, nEndIndex, implGetText().getLength() ) )
338 throw IndexOutOfBoundsException();
340 return false;
345 OUString VCLXAccessibleMenuItem::getText() throw (RuntimeException, std::exception)
347 OExternalLockGuard aGuard( this );
349 return OCommonAccessibleText::getText();
354 OUString VCLXAccessibleMenuItem::getTextRange( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
356 OExternalLockGuard aGuard( this );
358 return OCommonAccessibleText::getTextRange( nStartIndex, nEndIndex );
363 ::com::sun::star::accessibility::TextSegment VCLXAccessibleMenuItem::getTextAtIndex( sal_Int32 nIndex, sal_Int16 aTextType ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException, std::exception)
365 OExternalLockGuard aGuard( this );
367 return OCommonAccessibleText::getTextAtIndex( nIndex, aTextType );
372 ::com::sun::star::accessibility::TextSegment VCLXAccessibleMenuItem::getTextBeforeIndex( sal_Int32 nIndex, sal_Int16 aTextType ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException, std::exception)
374 OExternalLockGuard aGuard( this );
376 return OCommonAccessibleText::getTextBeforeIndex( nIndex, aTextType );
381 ::com::sun::star::accessibility::TextSegment VCLXAccessibleMenuItem::getTextBehindIndex( sal_Int32 nIndex, sal_Int16 aTextType ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException, std::exception)
383 OExternalLockGuard aGuard( this );
385 return OCommonAccessibleText::getTextBehindIndex( nIndex, aTextType );
390 sal_Bool VCLXAccessibleMenuItem::copyText( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
392 OExternalLockGuard aGuard( this );
394 bool bReturn = false;
396 if ( m_pParent )
398 vcl::Window* pWindow = m_pParent->GetWindow();
399 if ( pWindow )
401 Reference< datatransfer::clipboard::XClipboard > xClipboard = pWindow->GetClipboard();
402 if ( xClipboard.is() )
404 OUString sText( getTextRange( nStartIndex, nEndIndex ) );
406 vcl::unohelper::TextDataObject* pDataObj = new vcl::unohelper::TextDataObject( sText );
408 SolarMutexReleaser aReleaser;
409 xClipboard->setContents( pDataObj, NULL );
410 Reference< datatransfer::clipboard::XFlushableClipboard > xFlushableClipboard( xClipboard, uno::UNO_QUERY );
411 if( xFlushableClipboard.is() )
412 xFlushableClipboard->flushClipboard();
414 bReturn = true;
419 return bReturn;
423 // XAccessibleAction
426 sal_Int32 VCLXAccessibleMenuItem::getAccessibleActionCount( ) throw (RuntimeException, std::exception)
428 OExternalLockGuard aGuard( this );
430 return 1;
435 sal_Bool VCLXAccessibleMenuItem::doAccessibleAction ( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
437 OExternalLockGuard aGuard( this );
439 if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
440 throw IndexOutOfBoundsException();
442 Click();
444 return true;
449 OUString VCLXAccessibleMenuItem::getAccessibleActionDescription ( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
451 OExternalLockGuard aGuard( this );
453 if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
454 throw IndexOutOfBoundsException();
456 return TK_RES_STRING( RID_STR_ACC_ACTION_SELECT );
461 Reference< XAccessibleKeyBinding > VCLXAccessibleMenuItem::getAccessibleActionKeyBinding( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
463 OExternalLockGuard aGuard( this );
465 if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
466 throw IndexOutOfBoundsException();
468 OAccessibleKeyBindingHelper* pKeyBindingHelper = new OAccessibleKeyBindingHelper();
469 Reference< XAccessibleKeyBinding > xKeyBinding = pKeyBindingHelper;
471 if ( m_pParent )
473 // create auto mnemonics
474 if ( Application::GetSettings().GetStyleSettings().GetAutoMnemonic() && !( m_pParent->GetMenuFlags() & MenuFlags::NoAutoMnemonics ) )
475 m_pParent->CreateAutoMnemonics();
477 // activation key
478 KeyEvent aKeyEvent = m_pParent->GetActivationKey( m_pParent->GetItemId( m_nItemPos ) );
479 vcl::KeyCode aKeyCode = aKeyEvent.GetKeyCode();
480 Sequence< awt::KeyStroke > aSeq1(1);
481 aSeq1[0].Modifiers = 0;
482 Reference< XAccessible > xParent( getAccessibleParent() );
483 if ( xParent.is() )
485 Reference< XAccessibleContext > xParentContext( xParent->getAccessibleContext() );
486 if ( xParentContext.is() && xParentContext->getAccessibleRole() == AccessibleRole::MENU_BAR )
487 aSeq1[0].Modifiers |= awt::KeyModifier::MOD2;
489 aSeq1[0].KeyCode = aKeyCode.GetCode();
490 aSeq1[0].KeyChar = aKeyEvent.GetCharCode();
491 aSeq1[0].KeyFunc = static_cast< sal_Int16 >( aKeyCode.GetFunction() );
492 pKeyBindingHelper->AddKeyBinding( aSeq1 );
494 // complete menu activation key sequence
495 Sequence< awt::KeyStroke > aSeq;
496 if ( xParent.is() )
498 Reference< XAccessibleContext > xParentContext( xParent->getAccessibleContext() );
499 if ( xParentContext.is() && xParentContext->getAccessibleRole() == AccessibleRole::MENU )
501 Reference< XAccessibleAction > xAction( xParentContext, UNO_QUERY );
502 if ( xAction.is() && xAction->getAccessibleActionCount() > 0 )
504 Reference< XAccessibleKeyBinding > xKeyB( xAction->getAccessibleActionKeyBinding( 0 ) );
505 if ( xKeyB.is() && xKeyB->getAccessibleKeyBindingCount() > 1 )
506 aSeq = xKeyB->getAccessibleKeyBinding( 1 );
510 Sequence< awt::KeyStroke > aSeq2 = ::comphelper::concatSequences( aSeq, aSeq1 );
511 pKeyBindingHelper->AddKeyBinding( aSeq2 );
513 // accelerator key
514 vcl::KeyCode aAccelKeyCode = m_pParent->GetAccelKey( m_pParent->GetItemId( m_nItemPos ) );
515 if ( aAccelKeyCode.GetCode() != 0 )
517 Sequence< awt::KeyStroke > aSeq3(1);
518 aSeq3[0].Modifiers = 0;
519 if ( aAccelKeyCode.IsShift() )
520 aSeq3[0].Modifiers |= awt::KeyModifier::SHIFT;
521 if ( aAccelKeyCode.IsMod1() )
522 aSeq3[0].Modifiers |= awt::KeyModifier::MOD1;
523 if ( aAccelKeyCode.IsMod2() )
524 aSeq3[0].Modifiers |= awt::KeyModifier::MOD2;
525 if ( aAccelKeyCode.IsMod3() )
526 aSeq3[0].Modifiers |= awt::KeyModifier::MOD3;
527 aSeq3[0].KeyCode = aAccelKeyCode.GetCode();
528 aSeq3[0].KeyFunc = static_cast< sal_Int16 >( aAccelKeyCode.GetFunction() );
529 pKeyBindingHelper->AddKeyBinding( aSeq3 );
533 return xKeyBinding;
537 // XAccessibleValue
540 Any VCLXAccessibleMenuItem::getCurrentValue( ) throw (RuntimeException, std::exception)
542 OExternalLockGuard aGuard( this );
544 Any aValue;
545 if ( IsSelected() )
546 aValue <<= (sal_Int32) 1;
547 else
548 aValue <<= (sal_Int32) 0;
550 return aValue;
555 sal_Bool VCLXAccessibleMenuItem::setCurrentValue( const Any& aNumber ) throw (RuntimeException, std::exception)
557 OExternalLockGuard aGuard( this );
559 bool bReturn = false;
560 sal_Int32 nValue = 0;
561 OSL_VERIFY( aNumber >>= nValue );
563 if ( nValue <= 0 )
565 DeSelect();
566 bReturn = true;
568 else if ( nValue >= 1 )
570 Select();
571 bReturn = true;
574 return bReturn;
579 Any VCLXAccessibleMenuItem::getMaximumValue( ) throw (RuntimeException, std::exception)
581 OExternalLockGuard aGuard( this );
583 Any aValue;
584 aValue <<= (sal_Int32) 1;
586 return aValue;
591 Any VCLXAccessibleMenuItem::getMinimumValue( ) throw (RuntimeException, std::exception)
593 OExternalLockGuard aGuard( this );
595 Any aValue;
596 aValue <<= (sal_Int32) 0;
598 return aValue;
603 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */