tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / accessibility / source / standard / vclxaccessiblebutton.cxx
blob56f30c3d21488e1d526a2402fd6b3745f7ab8941
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 <standard/vclxaccessiblebutton.hxx>
21 #include <helper/accresmgr.hxx>
22 #include <strings.hrc>
24 #include <comphelper/accessiblecontexthelper.hxx>
25 #include <comphelper/accessiblekeybindinghelper.hxx>
26 #include <com/sun/star/awt/KeyModifier.hpp>
27 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
28 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
29 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
31 #include <vcl/accessibility/strings.hxx>
32 #include <vcl/toolkit/button.hxx>
33 #include <vcl/event.hxx>
34 #include <vcl/vclevent.hxx>
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::lang;
39 using namespace ::com::sun::star::accessibility;
40 using namespace ::comphelper;
43 // VCLXAccessibleButton
46 void VCLXAccessibleButton::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
48 switch ( rVclWindowEvent.GetId() )
50 case VclEventId::PushbuttonToggle:
52 Any aOldValue;
53 Any aNewValue;
55 VclPtr< PushButton > pButton = GetAs< PushButton >();
56 if ( pButton && pButton->GetState() == TRISTATE_TRUE )
57 aNewValue <<= AccessibleStateType::CHECKED;
58 else
59 aOldValue <<= AccessibleStateType::CHECKED;
61 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
63 break;
64 default:
65 VCLXAccessibleTextComponent::ProcessWindowEvent( rVclWindowEvent );
70 void VCLXAccessibleButton::FillAccessibleStateSet( sal_Int64& rStateSet )
72 VCLXAccessibleTextComponent::FillAccessibleStateSet( rStateSet );
74 VclPtr< PushButton > pButton = GetAs< PushButton >();
75 if ( !pButton )
76 return;
78 rStateSet |= AccessibleStateType::FOCUSABLE;
80 if (pButton->isToggleButton())
81 rStateSet |= AccessibleStateType::CHECKABLE;
83 if ( pButton->GetState() == TRISTATE_TRUE )
84 rStateSet |= AccessibleStateType::CHECKED;
86 if ( pButton->IsPressed() )
87 rStateSet |= AccessibleStateType::PRESSED;
89 // IA2 CWS: if the button has a popup menu, it should has the state EXPANDABLE
90 if( pButton->GetType() == WindowType::MENUBUTTON )
92 rStateSet |= AccessibleStateType::EXPANDABLE;
94 if( pButton->GetStyle() & WB_DEFBUTTON )
96 rStateSet |= AccessibleStateType::DEFAULT;
101 // XServiceInfo
104 OUString VCLXAccessibleButton::getImplementationName()
106 return u"com.sun.star.comp.toolkit.AccessibleButton"_ustr;
110 Sequence< OUString > VCLXAccessibleButton::getSupportedServiceNames()
112 return { u"com.sun.star.awt.AccessibleButton"_ustr };
116 // XAccessibleContext
119 OUString VCLXAccessibleButton::getAccessibleName( )
121 OUString aName( VCLXAccessibleTextComponent::getAccessibleName() );
122 sal_Int32 nLength = aName.getLength();
124 if ( nLength >= 3 && aName.match( "...", nLength - 3 ) )
126 if ( nLength == 3 )
128 // it's a browse button
129 aName = AccResId( RID_STR_ACC_NAME_BROWSEBUTTON );
131 else
133 // remove the three trailing dots
134 aName = aName.copy( 0, nLength - 3 );
137 else if ( nLength >= 3 && aName.match( "<< ", 0 ) )
139 // remove the leading symbols
140 aName = aName.copy( 3, nLength - 3 );
142 else if ( nLength >= 3 && aName.match( " >>", nLength - 3 ) )
144 // remove the trailing symbols
145 aName = aName.copy( 0, nLength - 3 );
148 return aName;
152 // XAccessibleAction
155 sal_Int32 VCLXAccessibleButton::getAccessibleActionCount( )
157 OExternalLockGuard aGuard( this );
159 return 1;
163 sal_Bool VCLXAccessibleButton::doAccessibleAction ( sal_Int32 nIndex )
165 OExternalLockGuard aGuard( this );
167 if ( nIndex != 0 )
168 throw IndexOutOfBoundsException();
170 VclPtr< PushButton > pButton = GetAs< PushButton >();
171 if ( pButton )
173 if (pButton->isToggleButton())
175 // PushButton::Click doesn't toggle when it's a toggle button
176 pButton->Check(!pButton->IsChecked());
177 pButton->Toggle();
179 else
181 pButton->Click();
185 return true;
189 OUString VCLXAccessibleButton::getAccessibleActionDescription ( sal_Int32 nIndex )
191 OExternalLockGuard aGuard( this );
193 if ( nIndex != 0 )
194 throw IndexOutOfBoundsException();
196 return RID_STR_ACC_ACTION_CLICK;
200 Reference< XAccessibleKeyBinding > VCLXAccessibleButton::getAccessibleActionKeyBinding( sal_Int32 nIndex )
202 OExternalLockGuard aGuard( this );
204 if ( nIndex != 0 )
205 throw IndexOutOfBoundsException();
207 rtl::Reference<OAccessibleKeyBindingHelper> pKeyBindingHelper = new OAccessibleKeyBindingHelper();
209 VclPtr<vcl::Window> pWindow = GetWindow();
210 if ( pWindow )
212 KeyEvent aKeyEvent = pWindow->GetActivationKey();
213 vcl::KeyCode aKeyCode = aKeyEvent.GetKeyCode();
214 if ( aKeyCode.GetCode() != 0 )
216 awt::KeyStroke aKeyStroke;
217 aKeyStroke.Modifiers = 0;
218 if ( aKeyCode.IsShift() )
219 aKeyStroke.Modifiers |= awt::KeyModifier::SHIFT;
220 if ( aKeyCode.IsMod1() )
221 aKeyStroke.Modifiers |= awt::KeyModifier::MOD1;
222 if ( aKeyCode.IsMod2() )
223 aKeyStroke.Modifiers |= awt::KeyModifier::MOD2;
224 if ( aKeyCode.IsMod3() )
225 aKeyStroke.Modifiers |= awt::KeyModifier::MOD3;
226 aKeyStroke.KeyCode = aKeyCode.GetCode();
227 aKeyStroke.KeyChar = aKeyEvent.GetCharCode();
228 aKeyStroke.KeyFunc = static_cast< sal_Int16 >( aKeyCode.GetFunction() );
229 pKeyBindingHelper->AddKeyBinding( aKeyStroke );
233 return pKeyBindingHelper;
237 // XAccessibleValue
240 Any VCLXAccessibleButton::getCurrentValue( )
242 OExternalLockGuard aGuard( this );
244 Any aValue;
246 VclPtr< PushButton > pButton = GetAs< PushButton >();
247 if ( pButton )
248 aValue <<= static_cast<sal_Int32>(pButton->IsPressed());
250 return aValue;
254 sal_Bool VCLXAccessibleButton::setCurrentValue( const Any& aNumber )
256 OExternalLockGuard aGuard( this );
258 bool bReturn = false;
260 VclPtr< PushButton > pButton = GetAs< PushButton >();
261 if ( pButton )
263 sal_Int32 nValue = 0;
264 OSL_VERIFY( aNumber >>= nValue );
266 if ( nValue < 0 )
267 nValue = 0;
268 else if ( nValue > 1 )
269 nValue = 1;
271 pButton->SetPressed( nValue == 1 );
272 bReturn = true;
275 return bReturn;
279 Any VCLXAccessibleButton::getMaximumValue( )
281 OExternalLockGuard aGuard( this );
283 Any aValue;
284 aValue <<= sal_Int32(1);
286 return aValue;
290 Any VCLXAccessibleButton::getMinimumValue( )
292 OExternalLockGuard aGuard( this );
294 Any aValue;
295 aValue <<= sal_Int32(0);
297 return aValue;
300 Any VCLXAccessibleButton::getMinimumIncrement( )
302 OExternalLockGuard aGuard( this );
304 Any aValue;
305 aValue <<= sal_Int32(1);
307 return aValue;
311 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */