merged tag ooo/DEV300_m102
[LibreOffice.git] / accessibility / source / standard / vclxaccessiblebutton.cxx
blobe133579922b355b7595b1160a4e2b171d3d0b232
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_accessibility.hxx"
31 // includes --------------------------------------------------------------
32 #include <accessibility/standard/vclxaccessiblebutton.hxx>
33 #include <accessibility/helper/accresmgr.hxx>
34 #include <accessibility/helper/accessiblestrings.hrc>
36 #include <unotools/accessiblestatesethelper.hxx>
37 #include <comphelper/accessiblekeybindinghelper.hxx>
38 #include <com/sun/star/awt/KeyModifier.hpp>
39 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
40 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
41 #include <cppuhelper/typeprovider.hxx>
42 #include <comphelper/sequence.hxx>
44 #include <vcl/button.hxx>
46 using namespace ::com::sun::star;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::lang;
49 using namespace ::com::sun::star::beans;
50 using namespace ::com::sun::star::accessibility;
51 using namespace ::comphelper;
54 // -----------------------------------------------------------------------------
55 // VCLXAccessibleButton
56 // -----------------------------------------------------------------------------
58 VCLXAccessibleButton::VCLXAccessibleButton( VCLXWindow* pVCLWindow )
59 :VCLXAccessibleTextComponent( pVCLWindow )
63 // -----------------------------------------------------------------------------
65 VCLXAccessibleButton::~VCLXAccessibleButton()
69 // -----------------------------------------------------------------------------
71 void VCLXAccessibleButton::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
73 switch ( rVclWindowEvent.GetId() )
75 case VCLEVENT_PUSHBUTTON_TOGGLE:
77 Any aOldValue;
78 Any aNewValue;
80 PushButton* pButton = (PushButton*) GetWindow();
81 if ( pButton && pButton->GetState() == STATE_CHECK )
82 aNewValue <<= AccessibleStateType::CHECKED;
83 else
84 aOldValue <<= AccessibleStateType::CHECKED;
86 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
88 break;
89 default:
90 VCLXAccessibleTextComponent::ProcessWindowEvent( rVclWindowEvent );
94 // -----------------------------------------------------------------------------
96 void VCLXAccessibleButton::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
98 VCLXAccessibleTextComponent::FillAccessibleStateSet( rStateSet );
100 PushButton* pButton = (PushButton*) GetWindow();
101 if ( pButton )
103 rStateSet.AddState( AccessibleStateType::FOCUSABLE );
105 if ( pButton->GetState() == STATE_CHECK )
106 rStateSet.AddState( AccessibleStateType::CHECKED );
108 if ( pButton->IsPressed() )
109 rStateSet.AddState( AccessibleStateType::PRESSED );
113 // -----------------------------------------------------------------------------
114 // XInterface
115 // -----------------------------------------------------------------------------
117 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleButton, VCLXAccessibleTextComponent, VCLXAccessibleButton_BASE )
119 // -----------------------------------------------------------------------------
120 // XTypeProvider
121 // -----------------------------------------------------------------------------
123 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleButton, VCLXAccessibleTextComponent, VCLXAccessibleButton_BASE )
125 // -----------------------------------------------------------------------------
126 // XServiceInfo
127 // -----------------------------------------------------------------------------
129 ::rtl::OUString VCLXAccessibleButton::getImplementationName() throw (RuntimeException)
131 return ::rtl::OUString::createFromAscii( "com.sun.star.comp.toolkit.AccessibleButton" );
134 // -----------------------------------------------------------------------------
136 Sequence< ::rtl::OUString > VCLXAccessibleButton::getSupportedServiceNames() throw (RuntimeException)
138 Sequence< ::rtl::OUString > aNames(1);
139 aNames[0] = ::rtl::OUString::createFromAscii( "com.sun.star.awt.AccessibleButton" );
140 return aNames;
143 // -----------------------------------------------------------------------------
144 // XAccessibleContext
145 // -----------------------------------------------------------------------------
147 ::rtl::OUString VCLXAccessibleButton::getAccessibleName( ) throw (RuntimeException)
149 OExternalLockGuard aGuard( this );
151 ::rtl::OUString aName( VCLXAccessibleTextComponent::getAccessibleName() );
152 sal_Int32 nLength = aName.getLength();
154 if ( nLength >= 3 && aName.matchAsciiL( RTL_CONSTASCII_STRINGPARAM("..."), nLength - 3 ) )
156 if ( nLength == 3 )
158 // it's a browse button
159 aName = ::rtl::OUString( TK_RES_STRING( RID_STR_ACC_NAME_BROWSEBUTTON ) );
161 else
163 // remove the three trailing dots
164 aName = aName.copy( 0, nLength - 3 );
167 else if ( nLength >= 3 && aName.matchAsciiL( RTL_CONSTASCII_STRINGPARAM("<< "), 0 ) )
169 // remove the leading symbols
170 aName = aName.copy( 3, nLength - 3 );
172 else if ( nLength >= 3 && aName.matchAsciiL( RTL_CONSTASCII_STRINGPARAM(" >>"), nLength - 3 ) )
174 // remove the trailing symbols
175 aName = aName.copy( 0, nLength - 3 );
178 return aName;
181 // -----------------------------------------------------------------------------
182 // XAccessibleAction
183 // -----------------------------------------------------------------------------
185 sal_Int32 VCLXAccessibleButton::getAccessibleActionCount( ) throw (RuntimeException)
187 OExternalLockGuard aGuard( this );
189 return 1;
192 // -----------------------------------------------------------------------------
194 sal_Bool VCLXAccessibleButton::doAccessibleAction ( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
196 OExternalLockGuard aGuard( this );
198 if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
199 throw IndexOutOfBoundsException();
201 PushButton* pButton = (PushButton*) GetWindow();
202 if ( pButton )
203 pButton->Click();
205 return sal_True;
208 // -----------------------------------------------------------------------------
210 ::rtl::OUString VCLXAccessibleButton::getAccessibleActionDescription ( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
212 OExternalLockGuard aGuard( this );
214 if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
215 throw IndexOutOfBoundsException();
217 return ::rtl::OUString( TK_RES_STRING( RID_STR_ACC_ACTION_CLICK ) );
220 // -----------------------------------------------------------------------------
222 Reference< XAccessibleKeyBinding > VCLXAccessibleButton::getAccessibleActionKeyBinding( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
224 OExternalLockGuard aGuard( this );
226 if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
227 throw IndexOutOfBoundsException();
229 OAccessibleKeyBindingHelper* pKeyBindingHelper = new OAccessibleKeyBindingHelper();
230 Reference< XAccessibleKeyBinding > xKeyBinding = pKeyBindingHelper;
232 Window* pWindow = GetWindow();
233 if ( pWindow )
235 KeyEvent aKeyEvent = pWindow->GetActivationKey();
236 KeyCode aKeyCode = aKeyEvent.GetKeyCode();
237 if ( aKeyCode.GetCode() != 0 )
239 awt::KeyStroke aKeyStroke;
240 aKeyStroke.Modifiers = 0;
241 if ( aKeyCode.IsShift() )
242 aKeyStroke.Modifiers |= awt::KeyModifier::SHIFT;
243 if ( aKeyCode.IsMod1() )
244 aKeyStroke.Modifiers |= awt::KeyModifier::MOD1;
245 if ( aKeyCode.IsMod2() )
246 aKeyStroke.Modifiers |= awt::KeyModifier::MOD2;
247 if ( aKeyCode.IsMod3() )
248 aKeyStroke.Modifiers |= awt::KeyModifier::MOD3;
249 aKeyStroke.KeyCode = aKeyCode.GetCode();
250 aKeyStroke.KeyChar = aKeyEvent.GetCharCode();
251 aKeyStroke.KeyFunc = static_cast< sal_Int16 >( aKeyCode.GetFunction() );
252 pKeyBindingHelper->AddKeyBinding( aKeyStroke );
256 return xKeyBinding;
259 // -----------------------------------------------------------------------------
260 // XAccessibleValue
261 // -----------------------------------------------------------------------------
263 Any VCLXAccessibleButton::getCurrentValue( ) throw (RuntimeException)
265 OExternalLockGuard aGuard( this );
267 Any aValue;
269 PushButton* pButton = (PushButton*) GetWindow();
270 if ( pButton )
271 aValue <<= (sal_Int32) pButton->IsPressed();
273 return aValue;
276 // -----------------------------------------------------------------------------
278 sal_Bool VCLXAccessibleButton::setCurrentValue( const Any& aNumber ) throw (RuntimeException)
280 OExternalLockGuard aGuard( this );
282 sal_Bool bReturn = sal_False;
284 PushButton* pButton = (PushButton*) GetWindow();
285 if ( pButton )
287 sal_Int32 nValue = 0;
288 OSL_VERIFY( aNumber >>= nValue );
290 if ( nValue < 0 )
291 nValue = 0;
292 else if ( nValue > 1 )
293 nValue = 1;
295 pButton->SetPressed( (sal_Bool) nValue );
296 bReturn = sal_True;
299 return bReturn;
302 // -----------------------------------------------------------------------------
304 Any VCLXAccessibleButton::getMaximumValue( ) throw (RuntimeException)
306 OExternalLockGuard aGuard( this );
308 Any aValue;
309 aValue <<= (sal_Int32) 1;
311 return aValue;
314 // -----------------------------------------------------------------------------
316 Any VCLXAccessibleButton::getMinimumValue( ) throw (RuntimeException)
318 OExternalLockGuard aGuard( this );
320 Any aValue;
321 aValue <<= (sal_Int32) 0;
323 return aValue;
326 // -----------------------------------------------------------------------------