Branch libreoffice-5-0-4
[LibreOffice.git] / accessibility / source / standard / vclxaccessiblebutton.cxx
blobae3698d6b298d4693283438def602ddec2c3849b
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/vclxaccessiblebutton.hxx>
21 #include <accessibility/helper/accresmgr.hxx>
22 #include <accessibility/helper/accessiblestrings.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 <cppuhelper/typeprovider.hxx>
30 #include <comphelper/sequence.hxx>
32 #include <vcl/button.hxx>
34 using namespace ::com::sun::star;
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::lang;
37 using namespace ::com::sun::star::beans;
38 using namespace ::com::sun::star::accessibility;
39 using namespace ::comphelper;
43 // VCLXAccessibleButton
46 VCLXAccessibleButton::VCLXAccessibleButton( VCLXWindow* pVCLWindow )
47 :VCLXAccessibleTextComponent( pVCLWindow )
53 VCLXAccessibleButton::~VCLXAccessibleButton()
59 void VCLXAccessibleButton::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
61 switch ( rVclWindowEvent.GetId() )
63 case VCLEVENT_PUSHBUTTON_TOGGLE:
65 Any aOldValue;
66 Any aNewValue;
68 VclPtr< PushButton > pButton = GetAs< PushButton >();
69 if ( pButton && pButton->GetState() == TRISTATE_TRUE )
70 aNewValue <<= AccessibleStateType::CHECKED;
71 else
72 aOldValue <<= AccessibleStateType::CHECKED;
74 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
76 break;
77 default:
78 VCLXAccessibleTextComponent::ProcessWindowEvent( rVclWindowEvent );
84 void VCLXAccessibleButton::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
86 VCLXAccessibleTextComponent::FillAccessibleStateSet( rStateSet );
88 VclPtr< PushButton > pButton = GetAs< PushButton >();
89 if ( pButton )
91 rStateSet.AddState( AccessibleStateType::FOCUSABLE );
93 if ( pButton->GetState() == TRISTATE_TRUE )
94 rStateSet.AddState( AccessibleStateType::CHECKED );
96 if ( pButton->IsPressed() )
97 rStateSet.AddState( AccessibleStateType::PRESSED );
99 // IA2 CWS: If the button has a poppup menu,it should has the state EXPANDABLE
100 if( pButton->GetType() == WINDOW_MENUBUTTON )
102 rStateSet.AddState( AccessibleStateType::EXPANDABLE );
104 if( pButton->GetStyle() & WB_DEFBUTTON )
106 rStateSet.AddState( AccessibleStateType::DEFAULT );
112 // XInterface
115 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleButton, VCLXAccessibleTextComponent, VCLXAccessibleButton_BASE )
118 // XTypeProvider
121 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleButton, VCLXAccessibleTextComponent, VCLXAccessibleButton_BASE )
124 // XServiceInfo
127 OUString VCLXAccessibleButton::getImplementationName() throw (RuntimeException, std::exception)
129 return OUString( "com.sun.star.comp.toolkit.AccessibleButton" );
134 Sequence< OUString > VCLXAccessibleButton::getSupportedServiceNames() throw (RuntimeException, std::exception)
136 Sequence< OUString > aNames(1);
137 aNames[0] = "com.sun.star.awt.AccessibleButton";
138 return aNames;
142 // XAccessibleContext
145 OUString VCLXAccessibleButton::getAccessibleName( ) throw (RuntimeException, std::exception)
147 OExternalLockGuard aGuard( this );
149 OUString aName( VCLXAccessibleTextComponent::getAccessibleName() );
150 sal_Int32 nLength = aName.getLength();
152 if ( nLength >= 3 && aName.match( "...", nLength - 3 ) )
154 if ( nLength == 3 )
156 // it's a browse button
157 aName = OUString( TK_RES_STRING( RID_STR_ACC_NAME_BROWSEBUTTON ) );
159 else
161 // remove the three trailing dots
162 aName = aName.copy( 0, nLength - 3 );
165 else if ( nLength >= 3 && aName.match( "<< ", 0 ) )
167 // remove the leading symbols
168 aName = aName.copy( 3, nLength - 3 );
170 else if ( nLength >= 3 && aName.match( " >>", nLength - 3 ) )
172 // remove the trailing symbols
173 aName = aName.copy( 0, nLength - 3 );
176 return aName;
180 // XAccessibleAction
183 sal_Int32 VCLXAccessibleButton::getAccessibleActionCount( ) throw (RuntimeException, std::exception)
185 OExternalLockGuard aGuard( this );
187 return 1;
192 sal_Bool VCLXAccessibleButton::doAccessibleAction ( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
194 OExternalLockGuard aGuard( this );
196 if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
197 throw IndexOutOfBoundsException();
199 VclPtr< PushButton > pButton = GetAs< PushButton >();
200 if ( pButton )
201 pButton->Click();
203 return true;
208 OUString VCLXAccessibleButton::getAccessibleActionDescription ( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
210 OExternalLockGuard aGuard( this );
212 if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
213 throw IndexOutOfBoundsException();
215 return OUString( TK_RES_STRING( RID_STR_ACC_ACTION_CLICK ) );
220 Reference< XAccessibleKeyBinding > VCLXAccessibleButton::getAccessibleActionKeyBinding( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
222 OExternalLockGuard aGuard( this );
224 if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
225 throw IndexOutOfBoundsException();
227 OAccessibleKeyBindingHelper* pKeyBindingHelper = new OAccessibleKeyBindingHelper();
228 Reference< XAccessibleKeyBinding > xKeyBinding = pKeyBindingHelper;
230 vcl::Window* pWindow = GetWindow();
231 if ( pWindow )
233 KeyEvent aKeyEvent = pWindow->GetActivationKey();
234 vcl::KeyCode aKeyCode = aKeyEvent.GetKeyCode();
235 if ( aKeyCode.GetCode() != 0 )
237 awt::KeyStroke aKeyStroke;
238 aKeyStroke.Modifiers = 0;
239 if ( aKeyCode.IsShift() )
240 aKeyStroke.Modifiers |= awt::KeyModifier::SHIFT;
241 if ( aKeyCode.IsMod1() )
242 aKeyStroke.Modifiers |= awt::KeyModifier::MOD1;
243 if ( aKeyCode.IsMod2() )
244 aKeyStroke.Modifiers |= awt::KeyModifier::MOD2;
245 if ( aKeyCode.IsMod3() )
246 aKeyStroke.Modifiers |= awt::KeyModifier::MOD3;
247 aKeyStroke.KeyCode = aKeyCode.GetCode();
248 aKeyStroke.KeyChar = aKeyEvent.GetCharCode();
249 aKeyStroke.KeyFunc = static_cast< sal_Int16 >( aKeyCode.GetFunction() );
250 pKeyBindingHelper->AddKeyBinding( aKeyStroke );
254 return xKeyBinding;
258 // XAccessibleValue
261 Any VCLXAccessibleButton::getCurrentValue( ) throw (RuntimeException, std::exception)
263 OExternalLockGuard aGuard( this );
265 Any aValue;
267 VclPtr< PushButton > pButton = GetAs< PushButton >();
268 if ( pButton )
269 aValue <<= (sal_Int32) pButton->IsPressed();
271 return aValue;
276 sal_Bool VCLXAccessibleButton::setCurrentValue( const Any& aNumber ) throw (RuntimeException, std::exception)
278 OExternalLockGuard aGuard( this );
280 bool bReturn = false;
282 VclPtr< PushButton > pButton = GetAs< PushButton >();
283 if ( pButton )
285 sal_Int32 nValue = 0;
286 OSL_VERIFY( aNumber >>= nValue );
288 if ( nValue < 0 )
289 nValue = 0;
290 else if ( nValue > 1 )
291 nValue = 1;
293 pButton->SetPressed( nValue == 1 );
294 bReturn = true;
297 return bReturn;
302 Any VCLXAccessibleButton::getMaximumValue( ) throw (RuntimeException, std::exception)
304 OExternalLockGuard aGuard( this );
306 Any aValue;
307 aValue <<= (sal_Int32) 1;
309 return aValue;
314 Any VCLXAccessibleButton::getMinimumValue( ) throw (RuntimeException, std::exception)
316 OExternalLockGuard aGuard( this );
318 Any aValue;
319 aValue <<= (sal_Int32) 0;
321 return aValue;
326 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */