Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / accessibility / source / standard / vclxaccessiblebutton.cxx
blob52153a0c22b931c2232ab8c7a07832c7e5a0145e
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>
30 #include <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::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( sal_Int64& rStateSet )
73 VCLXAccessibleTextComponent::FillAccessibleStateSet( rStateSet );
75 VclPtr< PushButton > pButton = GetAs< PushButton >();
76 if ( !pButton )
77 return;
79 rStateSet |= AccessibleStateType::FOCUSABLE;
81 if ( pButton->GetState() == TRISTATE_TRUE )
82 rStateSet |= AccessibleStateType::CHECKED;
84 if ( pButton->IsPressed() )
85 rStateSet |= AccessibleStateType::PRESSED;
87 // IA2 CWS: if the button has a popup menu, it should has the state EXPANDABLE
88 if( pButton->GetType() == WindowType::MENUBUTTON )
90 rStateSet |= AccessibleStateType::EXPANDABLE;
92 if( pButton->GetStyle() & WB_DEFBUTTON )
94 rStateSet |= AccessibleStateType::DEFAULT;
99 // XServiceInfo
102 OUString VCLXAccessibleButton::getImplementationName()
104 return "com.sun.star.comp.toolkit.AccessibleButton";
108 Sequence< OUString > VCLXAccessibleButton::getSupportedServiceNames()
110 return { "com.sun.star.awt.AccessibleButton" };
114 // XAccessibleContext
117 OUString VCLXAccessibleButton::getAccessibleName( )
119 OUString aName( VCLXAccessibleTextComponent::getAccessibleName() );
120 sal_Int32 nLength = aName.getLength();
122 if ( nLength >= 3 && aName.match( "...", nLength - 3 ) )
124 if ( nLength == 3 )
126 // it's a browse button
127 aName = AccResId( RID_STR_ACC_NAME_BROWSEBUTTON );
129 else
131 // remove the three trailing dots
132 aName = aName.copy( 0, nLength - 3 );
135 else if ( nLength >= 3 && aName.match( "<< ", 0 ) )
137 // remove the leading symbols
138 aName = aName.copy( 3, nLength - 3 );
140 else if ( nLength >= 3 && aName.match( " >>", nLength - 3 ) )
142 // remove the trailing symbols
143 aName = aName.copy( 0, nLength - 3 );
146 return aName;
150 // XAccessibleAction
153 sal_Int32 VCLXAccessibleButton::getAccessibleActionCount( )
155 OExternalLockGuard aGuard( this );
157 return 1;
161 sal_Bool VCLXAccessibleButton::doAccessibleAction ( sal_Int32 nIndex )
163 OExternalLockGuard aGuard( this );
165 if ( nIndex != 0 )
166 throw IndexOutOfBoundsException();
168 VclPtr< PushButton > pButton = GetAs< PushButton >();
169 if ( pButton )
170 pButton->Click();
172 return true;
176 OUString VCLXAccessibleButton::getAccessibleActionDescription ( sal_Int32 nIndex )
178 OExternalLockGuard aGuard( this );
180 if ( nIndex != 0 )
181 throw IndexOutOfBoundsException();
183 return RID_STR_ACC_ACTION_CLICK;
187 Reference< XAccessibleKeyBinding > VCLXAccessibleButton::getAccessibleActionKeyBinding( sal_Int32 nIndex )
189 OExternalLockGuard aGuard( this );
191 if ( nIndex != 0 )
192 throw IndexOutOfBoundsException();
194 rtl::Reference<OAccessibleKeyBindingHelper> pKeyBindingHelper = new OAccessibleKeyBindingHelper();
196 VclPtr<vcl::Window> pWindow = GetWindow();
197 if ( pWindow )
199 KeyEvent aKeyEvent = pWindow->GetActivationKey();
200 vcl::KeyCode aKeyCode = aKeyEvent.GetKeyCode();
201 if ( aKeyCode.GetCode() != 0 )
203 awt::KeyStroke aKeyStroke;
204 aKeyStroke.Modifiers = 0;
205 if ( aKeyCode.IsShift() )
206 aKeyStroke.Modifiers |= awt::KeyModifier::SHIFT;
207 if ( aKeyCode.IsMod1() )
208 aKeyStroke.Modifiers |= awt::KeyModifier::MOD1;
209 if ( aKeyCode.IsMod2() )
210 aKeyStroke.Modifiers |= awt::KeyModifier::MOD2;
211 if ( aKeyCode.IsMod3() )
212 aKeyStroke.Modifiers |= awt::KeyModifier::MOD3;
213 aKeyStroke.KeyCode = aKeyCode.GetCode();
214 aKeyStroke.KeyChar = aKeyEvent.GetCharCode();
215 aKeyStroke.KeyFunc = static_cast< sal_Int16 >( aKeyCode.GetFunction() );
216 pKeyBindingHelper->AddKeyBinding( aKeyStroke );
220 return pKeyBindingHelper;
224 // XAccessibleValue
227 Any VCLXAccessibleButton::getCurrentValue( )
229 OExternalLockGuard aGuard( this );
231 Any aValue;
233 VclPtr< PushButton > pButton = GetAs< PushButton >();
234 if ( pButton )
235 aValue <<= static_cast<sal_Int32>(pButton->IsPressed());
237 return aValue;
241 sal_Bool VCLXAccessibleButton::setCurrentValue( const Any& aNumber )
243 OExternalLockGuard aGuard( this );
245 bool bReturn = false;
247 VclPtr< PushButton > pButton = GetAs< PushButton >();
248 if ( pButton )
250 sal_Int32 nValue = 0;
251 OSL_VERIFY( aNumber >>= nValue );
253 if ( nValue < 0 )
254 nValue = 0;
255 else if ( nValue > 1 )
256 nValue = 1;
258 pButton->SetPressed( nValue == 1 );
259 bReturn = true;
262 return bReturn;
266 Any VCLXAccessibleButton::getMaximumValue( )
268 OExternalLockGuard aGuard( this );
270 Any aValue;
271 aValue <<= sal_Int32(1);
273 return aValue;
277 Any VCLXAccessibleButton::getMinimumValue( )
279 OExternalLockGuard aGuard( this );
281 Any aValue;
282 aValue <<= sal_Int32(0);
284 return aValue;
287 Any VCLXAccessibleButton::getMinimumIncrement( )
289 OExternalLockGuard aGuard( this );
291 Any aValue;
292 aValue <<= sal_Int32(1);
294 return aValue;
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */