Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / accessibility / source / standard / vclxaccessiblebutton.cxx
blobb5289d7ed9d88a6eb4f07f921b9b63c26f939d83
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 <unotools/accessiblestatesethelper.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>
30 #include <strings.hxx>
32 #include <vcl/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::beans;
40 using namespace ::com::sun::star::accessibility;
41 using namespace ::comphelper;
44 // VCLXAccessibleButton
47 void VCLXAccessibleButton::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
49 switch ( rVclWindowEvent.GetId() )
51 case VclEventId::PushbuttonToggle:
53 Any aOldValue;
54 Any aNewValue;
56 VclPtr< PushButton > pButton = GetAs< PushButton >();
57 if ( pButton && pButton->GetState() == TRISTATE_TRUE )
58 aNewValue <<= AccessibleStateType::CHECKED;
59 else
60 aOldValue <<= AccessibleStateType::CHECKED;
62 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
64 break;
65 default:
66 VCLXAccessibleTextComponent::ProcessWindowEvent( rVclWindowEvent );
71 void VCLXAccessibleButton::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
73 VCLXAccessibleTextComponent::FillAccessibleStateSet( rStateSet );
75 VclPtr< PushButton > pButton = GetAs< PushButton >();
76 if ( pButton )
78 rStateSet.AddState( AccessibleStateType::FOCUSABLE );
80 if ( pButton->GetState() == TRISTATE_TRUE )
81 rStateSet.AddState( AccessibleStateType::CHECKED );
83 if ( pButton->IsPressed() )
84 rStateSet.AddState( AccessibleStateType::PRESSED );
86 // IA2 CWS: if the button has a popup menu, it should has the state EXPANDABLE
87 if( pButton->GetType() == WindowType::MENUBUTTON )
89 rStateSet.AddState( AccessibleStateType::EXPANDABLE );
91 if( pButton->GetStyle() & WB_DEFBUTTON )
93 rStateSet.AddState( AccessibleStateType::DEFAULT );
99 // XInterface
102 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleButton, VCLXAccessibleTextComponent, VCLXAccessibleButton_BASE )
105 // XTypeProvider
108 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleButton, VCLXAccessibleTextComponent, VCLXAccessibleButton_BASE )
111 // XServiceInfo
114 OUString VCLXAccessibleButton::getImplementationName()
116 return "com.sun.star.comp.toolkit.AccessibleButton";
120 Sequence< OUString > VCLXAccessibleButton::getSupportedServiceNames()
122 return { "com.sun.star.awt.AccessibleButton" };
126 // XAccessibleContext
129 OUString VCLXAccessibleButton::getAccessibleName( )
131 OUString aName( VCLXAccessibleTextComponent::getAccessibleName() );
132 sal_Int32 nLength = aName.getLength();
134 if ( nLength >= 3 && aName.match( "...", nLength - 3 ) )
136 if ( nLength == 3 )
138 // it's a browse button
139 aName = AccResId( RID_STR_ACC_NAME_BROWSEBUTTON );
141 else
143 // remove the three trailing dots
144 aName = aName.copy( 0, nLength - 3 );
147 else if ( nLength >= 3 && aName.match( "<< ", 0 ) )
149 // remove the leading symbols
150 aName = aName.copy( 3, nLength - 3 );
152 else if ( nLength >= 3 && aName.match( " >>", nLength - 3 ) )
154 // remove the trailing symbols
155 aName = aName.copy( 0, nLength - 3 );
158 return aName;
162 // XAccessibleAction
165 sal_Int32 VCLXAccessibleButton::getAccessibleActionCount( )
167 OExternalLockGuard aGuard( this );
169 return 1;
173 sal_Bool VCLXAccessibleButton::doAccessibleAction ( sal_Int32 nIndex )
175 OExternalLockGuard aGuard( this );
177 if ( nIndex != 0 )
178 throw IndexOutOfBoundsException();
180 VclPtr< PushButton > pButton = GetAs< PushButton >();
181 if ( pButton )
182 pButton->Click();
184 return true;
188 OUString VCLXAccessibleButton::getAccessibleActionDescription ( sal_Int32 nIndex )
190 OExternalLockGuard aGuard( this );
192 if ( nIndex != 0 )
193 throw IndexOutOfBoundsException();
195 return RID_STR_ACC_ACTION_CLICK;
199 Reference< XAccessibleKeyBinding > VCLXAccessibleButton::getAccessibleActionKeyBinding( sal_Int32 nIndex )
201 OExternalLockGuard aGuard( this );
203 if ( nIndex != 0 )
204 throw IndexOutOfBoundsException();
206 OAccessibleKeyBindingHelper* pKeyBindingHelper = new OAccessibleKeyBindingHelper();
207 Reference< XAccessibleKeyBinding > xKeyBinding = pKeyBindingHelper;
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 xKeyBinding;
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;
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */