merge the formfield patch from ooo-build
[ooovba.git] / accessibility / source / standard / vclxaccessiblebutton.cxx
blobf0f4dda7119a929707c860df1c0424274e80f9e9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vclxaccessiblebutton.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_accessibility.hxx"
34 // includes --------------------------------------------------------------
35 #include <accessibility/standard/vclxaccessiblebutton.hxx>
36 #include <accessibility/helper/accresmgr.hxx>
37 #include <accessibility/helper/accessiblestrings.hrc>
39 #include <unotools/accessiblestatesethelper.hxx>
40 #include <comphelper/accessiblekeybindinghelper.hxx>
41 #include <com/sun/star/awt/KeyModifier.hpp>
42 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
43 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
44 #include <cppuhelper/typeprovider.hxx>
45 #include <comphelper/sequence.hxx>
47 #include <vcl/button.hxx>
49 using namespace ::com::sun::star;
50 using namespace ::com::sun::star::uno;
51 using namespace ::com::sun::star::lang;
52 using namespace ::com::sun::star::beans;
53 using namespace ::com::sun::star::accessibility;
54 using namespace ::comphelper;
57 // -----------------------------------------------------------------------------
58 // VCLXAccessibleButton
59 // -----------------------------------------------------------------------------
61 VCLXAccessibleButton::VCLXAccessibleButton( VCLXWindow* pVCLWindow )
62 :VCLXAccessibleTextComponent( pVCLWindow )
66 // -----------------------------------------------------------------------------
68 VCLXAccessibleButton::~VCLXAccessibleButton()
72 // -----------------------------------------------------------------------------
74 void VCLXAccessibleButton::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
76 switch ( rVclWindowEvent.GetId() )
78 case VCLEVENT_PUSHBUTTON_TOGGLE:
80 Any aOldValue;
81 Any aNewValue;
83 PushButton* pButton = (PushButton*) GetWindow();
84 if ( pButton && pButton->GetState() == STATE_CHECK )
85 aNewValue <<= AccessibleStateType::CHECKED;
86 else
87 aOldValue <<= AccessibleStateType::CHECKED;
89 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
91 break;
92 default:
93 VCLXAccessibleTextComponent::ProcessWindowEvent( rVclWindowEvent );
97 // -----------------------------------------------------------------------------
99 void VCLXAccessibleButton::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
101 VCLXAccessibleTextComponent::FillAccessibleStateSet( rStateSet );
103 PushButton* pButton = (PushButton*) GetWindow();
104 if ( pButton )
106 rStateSet.AddState( AccessibleStateType::FOCUSABLE );
108 if ( pButton->GetState() == STATE_CHECK )
109 rStateSet.AddState( AccessibleStateType::CHECKED );
111 if ( pButton->IsPressed() )
112 rStateSet.AddState( AccessibleStateType::PRESSED );
116 // -----------------------------------------------------------------------------
117 // XInterface
118 // -----------------------------------------------------------------------------
120 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleButton, VCLXAccessibleTextComponent, VCLXAccessibleButton_BASE )
122 // -----------------------------------------------------------------------------
123 // XTypeProvider
124 // -----------------------------------------------------------------------------
126 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleButton, VCLXAccessibleTextComponent, VCLXAccessibleButton_BASE )
128 // -----------------------------------------------------------------------------
129 // XServiceInfo
130 // -----------------------------------------------------------------------------
132 ::rtl::OUString VCLXAccessibleButton::getImplementationName() throw (RuntimeException)
134 return ::rtl::OUString::createFromAscii( "com.sun.star.comp.toolkit.AccessibleButton" );
137 // -----------------------------------------------------------------------------
139 Sequence< ::rtl::OUString > VCLXAccessibleButton::getSupportedServiceNames() throw (RuntimeException)
141 Sequence< ::rtl::OUString > aNames(1);
142 aNames[0] = ::rtl::OUString::createFromAscii( "com.sun.star.awt.AccessibleButton" );
143 return aNames;
146 // -----------------------------------------------------------------------------
147 // XAccessibleContext
148 // -----------------------------------------------------------------------------
150 ::rtl::OUString VCLXAccessibleButton::getAccessibleName( ) throw (RuntimeException)
152 OExternalLockGuard aGuard( this );
154 ::rtl::OUString aName( VCLXAccessibleTextComponent::getAccessibleName() );
155 sal_Int32 nLength = aName.getLength();
157 if ( nLength >= 3 && aName.matchAsciiL( RTL_CONSTASCII_STRINGPARAM("..."), nLength - 3 ) )
159 if ( nLength == 3 )
161 // it's a browse button
162 aName = ::rtl::OUString( TK_RES_STRING( RID_STR_ACC_NAME_BROWSEBUTTON ) );
164 else
166 // remove the three trailing dots
167 aName = aName.copy( 0, nLength - 3 );
170 else if ( nLength >= 3 && aName.matchAsciiL( RTL_CONSTASCII_STRINGPARAM("<< "), 0 ) )
172 // remove the leading symbols
173 aName = aName.copy( 3, nLength - 3 );
175 else if ( nLength >= 3 && aName.matchAsciiL( RTL_CONSTASCII_STRINGPARAM(" >>"), nLength - 3 ) )
177 // remove the trailing symbols
178 aName = aName.copy( 0, nLength - 3 );
181 return aName;
184 // -----------------------------------------------------------------------------
185 // XAccessibleAction
186 // -----------------------------------------------------------------------------
188 sal_Int32 VCLXAccessibleButton::getAccessibleActionCount( ) throw (RuntimeException)
190 OExternalLockGuard aGuard( this );
192 return 1;
195 // -----------------------------------------------------------------------------
197 sal_Bool VCLXAccessibleButton::doAccessibleAction ( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
199 OExternalLockGuard aGuard( this );
201 if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
202 throw IndexOutOfBoundsException();
204 PushButton* pButton = (PushButton*) GetWindow();
205 if ( pButton )
206 pButton->Click();
208 return sal_True;
211 // -----------------------------------------------------------------------------
213 ::rtl::OUString VCLXAccessibleButton::getAccessibleActionDescription ( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
215 OExternalLockGuard aGuard( this );
217 if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
218 throw IndexOutOfBoundsException();
220 return ::rtl::OUString( TK_RES_STRING( RID_STR_ACC_ACTION_CLICK ) );
223 // -----------------------------------------------------------------------------
225 Reference< XAccessibleKeyBinding > VCLXAccessibleButton::getAccessibleActionKeyBinding( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
227 OExternalLockGuard aGuard( this );
229 if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
230 throw IndexOutOfBoundsException();
232 OAccessibleKeyBindingHelper* pKeyBindingHelper = new OAccessibleKeyBindingHelper();
233 Reference< XAccessibleKeyBinding > xKeyBinding = pKeyBindingHelper;
235 Window* pWindow = GetWindow();
236 if ( pWindow )
238 KeyEvent aKeyEvent = pWindow->GetActivationKey();
239 KeyCode aKeyCode = aKeyEvent.GetKeyCode();
240 if ( aKeyCode.GetCode() != 0 )
242 awt::KeyStroke aKeyStroke;
243 aKeyStroke.Modifiers = 0;
244 if ( aKeyCode.IsShift() )
245 aKeyStroke.Modifiers |= awt::KeyModifier::SHIFT;
246 if ( aKeyCode.IsMod1() )
247 aKeyStroke.Modifiers |= awt::KeyModifier::MOD1;
248 if ( aKeyCode.IsMod2() )
249 aKeyStroke.Modifiers |= awt::KeyModifier::MOD2;
250 if ( aKeyCode.IsMod3() )
251 aKeyStroke.Modifiers |= awt::KeyModifier::MOD3;
252 aKeyStroke.KeyCode = aKeyCode.GetCode();
253 aKeyStroke.KeyChar = aKeyEvent.GetCharCode();
254 aKeyStroke.KeyFunc = static_cast< sal_Int16 >( aKeyCode.GetFunction() );
255 pKeyBindingHelper->AddKeyBinding( aKeyStroke );
259 return xKeyBinding;
262 // -----------------------------------------------------------------------------
263 // XAccessibleValue
264 // -----------------------------------------------------------------------------
266 Any VCLXAccessibleButton::getCurrentValue( ) throw (RuntimeException)
268 OExternalLockGuard aGuard( this );
270 Any aValue;
272 PushButton* pButton = (PushButton*) GetWindow();
273 if ( pButton )
274 aValue <<= (sal_Int32) pButton->IsPressed();
276 return aValue;
279 // -----------------------------------------------------------------------------
281 sal_Bool VCLXAccessibleButton::setCurrentValue( const Any& aNumber ) throw (RuntimeException)
283 OExternalLockGuard aGuard( this );
285 sal_Bool bReturn = sal_False;
287 PushButton* pButton = (PushButton*) GetWindow();
288 if ( pButton )
290 sal_Int32 nValue = 0;
291 OSL_VERIFY( aNumber >>= nValue );
293 if ( nValue < 0 )
294 nValue = 0;
295 else if ( nValue > 1 )
296 nValue = 1;
298 pButton->SetPressed( (BOOL) nValue );
299 bReturn = sal_True;
302 return bReturn;
305 // -----------------------------------------------------------------------------
307 Any VCLXAccessibleButton::getMaximumValue( ) throw (RuntimeException)
309 OExternalLockGuard aGuard( this );
311 Any aValue;
312 aValue <<= (sal_Int32) 1;
314 return aValue;
317 // -----------------------------------------------------------------------------
319 Any VCLXAccessibleButton::getMinimumValue( ) throw (RuntimeException)
321 OExternalLockGuard aGuard( this );
323 Any aValue;
324 aValue <<= (sal_Int32) 0;
326 return aValue;
329 // -----------------------------------------------------------------------------