merge the formfield patch from ooo-build
[ooovba.git] / accessibility / source / standard / vclxaccessiblecheckbox.cxx
blobe62c9b9d7b8dd22e6f5139397cfbc726774f2ffd
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: vclxaccessiblecheckbox.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/vclxaccessiblecheckbox.hxx>
37 #include <toolkit/awt/vclxwindows.hxx>
38 #include <accessibility/helper/accresmgr.hxx>
39 #include <accessibility/helper/accessiblestrings.hrc>
41 #include <unotools/accessiblestatesethelper.hxx>
42 #include <comphelper/accessiblekeybindinghelper.hxx>
43 #include <com/sun/star/awt/KeyModifier.hpp>
44 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
45 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
46 #include <cppuhelper/typeprovider.hxx>
47 #include <comphelper/sequence.hxx>
49 #include <vcl/button.hxx>
51 using namespace ::com::sun::star;
52 using namespace ::com::sun::star::uno;
53 using namespace ::com::sun::star::lang;
54 using namespace ::com::sun::star::beans;
55 using namespace ::com::sun::star::accessibility;
56 using namespace ::comphelper;
59 // -----------------------------------------------------------------------------
60 // VCLXAccessibleCheckBox
61 // -----------------------------------------------------------------------------
63 VCLXAccessibleCheckBox::VCLXAccessibleCheckBox( VCLXWindow* pVCLWindow )
64 :VCLXAccessibleTextComponent( pVCLWindow )
66 m_bChecked = IsChecked();
67 m_bIndeterminate = IsIndeterminate();
70 // -----------------------------------------------------------------------------
72 VCLXAccessibleCheckBox::~VCLXAccessibleCheckBox()
76 // -----------------------------------------------------------------------------
78 bool VCLXAccessibleCheckBox::IsChecked()
80 bool bChecked = false;
82 VCLXCheckBox* pVCLXCheckBox = static_cast< VCLXCheckBox* >( GetVCLXWindow() );
83 if ( pVCLXCheckBox && pVCLXCheckBox->getState() == (sal_Int16) 1 )
84 bChecked = true;
86 return bChecked;
89 // -----------------------------------------------------------------------------
91 bool VCLXAccessibleCheckBox::IsIndeterminate()
93 bool bIndeterminate = false;
95 VCLXCheckBox* pVCLXCheckBox = static_cast< VCLXCheckBox* >( GetVCLXWindow() );
96 if ( pVCLXCheckBox && pVCLXCheckBox->getState() == (sal_Int16) 2 )
97 bIndeterminate = true;
99 return bIndeterminate;
102 // -----------------------------------------------------------------------------
104 void VCLXAccessibleCheckBox::SetChecked( bool bChecked )
106 if ( m_bChecked != bChecked )
108 Any aOldValue, aNewValue;
109 if ( m_bChecked )
110 aOldValue <<= AccessibleStateType::CHECKED;
111 else
112 aNewValue <<= AccessibleStateType::CHECKED;
113 m_bChecked = bChecked;
114 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
118 // -----------------------------------------------------------------------------
120 void VCLXAccessibleCheckBox::SetIndeterminate( bool bIndeterminate )
122 if ( m_bIndeterminate != bIndeterminate )
124 Any aOldValue, aNewValue;
125 if ( m_bIndeterminate )
126 aOldValue <<= AccessibleStateType::INDETERMINATE;
127 else
128 aNewValue <<= AccessibleStateType::INDETERMINATE;
129 m_bIndeterminate = bIndeterminate;
130 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
134 // -----------------------------------------------------------------------------
136 void VCLXAccessibleCheckBox::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
138 switch ( rVclWindowEvent.GetId() )
140 case VCLEVENT_CHECKBOX_TOGGLE:
142 SetChecked( IsChecked() );
143 SetIndeterminate( IsIndeterminate() );
145 break;
146 default:
147 VCLXAccessibleTextComponent::ProcessWindowEvent( rVclWindowEvent );
151 // -----------------------------------------------------------------------------
153 void VCLXAccessibleCheckBox::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
155 VCLXAccessibleTextComponent::FillAccessibleStateSet( rStateSet );
157 rStateSet.AddState( AccessibleStateType::FOCUSABLE );
159 if ( IsChecked() )
160 rStateSet.AddState( AccessibleStateType::CHECKED );
162 if ( IsIndeterminate() )
163 rStateSet.AddState( AccessibleStateType::INDETERMINATE );
166 // -----------------------------------------------------------------------------
167 // XInterface
168 // -----------------------------------------------------------------------------
170 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleCheckBox, VCLXAccessibleTextComponent, VCLXAccessibleCheckBox_BASE )
172 // -----------------------------------------------------------------------------
173 // XTypeProvider
174 // -----------------------------------------------------------------------------
176 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleCheckBox, VCLXAccessibleTextComponent, VCLXAccessibleCheckBox_BASE )
178 // -----------------------------------------------------------------------------
179 // XServiceInfo
180 // -----------------------------------------------------------------------------
182 ::rtl::OUString VCLXAccessibleCheckBox::getImplementationName() throw (RuntimeException)
184 return ::rtl::OUString::createFromAscii( "com.sun.star.comp.toolkit.AccessibleCheckBox" );
187 // -----------------------------------------------------------------------------
189 Sequence< ::rtl::OUString > VCLXAccessibleCheckBox::getSupportedServiceNames() throw (RuntimeException)
191 Sequence< ::rtl::OUString > aNames(1);
192 aNames[0] = ::rtl::OUString::createFromAscii( "com.sun.star.awt.AccessibleCheckBox" );
193 return aNames;
196 // -----------------------------------------------------------------------------
197 // XAccessibleAction
198 // -----------------------------------------------------------------------------
200 sal_Int32 VCLXAccessibleCheckBox::getAccessibleActionCount( ) throw (RuntimeException)
202 OExternalLockGuard aGuard( this );
204 return 1;
207 // -----------------------------------------------------------------------------
209 sal_Bool VCLXAccessibleCheckBox::doAccessibleAction ( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
211 OExternalLockGuard aGuard( this );
213 if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
214 throw IndexOutOfBoundsException();
216 CheckBox* pCheckBox = (CheckBox*) GetWindow();
217 VCLXCheckBox* pVCLXCheckBox = static_cast< VCLXCheckBox* >( GetVCLXWindow() );
218 if ( pCheckBox && pVCLXCheckBox )
220 sal_Int32 nValueMin = (sal_Int32) 0;
221 sal_Int32 nValueMax = (sal_Int32) 1;
223 if ( pCheckBox->IsTriStateEnabled() )
224 nValueMax = (sal_Int32) 2;
226 sal_Int32 nValue = (sal_Int32) pVCLXCheckBox->getState();
228 ++nValue;
230 if ( nValue > nValueMax )
231 nValue = nValueMin;
233 pVCLXCheckBox->setState( (sal_Int16) nValue );
236 return sal_True;
239 // -----------------------------------------------------------------------------
241 ::rtl::OUString VCLXAccessibleCheckBox::getAccessibleActionDescription ( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
243 OExternalLockGuard aGuard( this );
245 if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
246 throw IndexOutOfBoundsException();
248 return ::rtl::OUString( TK_RES_STRING( RID_STR_ACC_ACTION_CLICK ) );
251 // -----------------------------------------------------------------------------
253 Reference< XAccessibleKeyBinding > VCLXAccessibleCheckBox::getAccessibleActionKeyBinding( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
255 OExternalLockGuard aGuard( this );
257 if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
258 throw IndexOutOfBoundsException();
260 OAccessibleKeyBindingHelper* pKeyBindingHelper = new OAccessibleKeyBindingHelper();
261 Reference< XAccessibleKeyBinding > xKeyBinding = pKeyBindingHelper;
263 Window* pWindow = GetWindow();
264 if ( pWindow )
266 KeyEvent aKeyEvent = pWindow->GetActivationKey();
267 KeyCode aKeyCode = aKeyEvent.GetKeyCode();
268 if ( aKeyCode.GetCode() != 0 )
270 awt::KeyStroke aKeyStroke;
271 aKeyStroke.Modifiers = 0;
272 if ( aKeyCode.IsShift() )
273 aKeyStroke.Modifiers |= awt::KeyModifier::SHIFT;
274 if ( aKeyCode.IsMod1() )
275 aKeyStroke.Modifiers |= awt::KeyModifier::MOD1;
276 if ( aKeyCode.IsMod2() )
277 aKeyStroke.Modifiers |= awt::KeyModifier::MOD2;
278 if ( aKeyCode.IsMod3() )
279 aKeyStroke.Modifiers |= awt::KeyModifier::MOD3;
280 aKeyStroke.KeyCode = aKeyCode.GetCode();
281 aKeyStroke.KeyChar = aKeyEvent.GetCharCode();
282 aKeyStroke.KeyFunc = static_cast< sal_Int16 >( aKeyCode.GetFunction() );
283 pKeyBindingHelper->AddKeyBinding( aKeyStroke );
287 return xKeyBinding;
290 // -----------------------------------------------------------------------------
291 // XAccessibleValue
292 // -----------------------------------------------------------------------------
294 Any VCLXAccessibleCheckBox::getCurrentValue( ) throw (RuntimeException)
296 OExternalLockGuard aGuard( this );
298 Any aValue;
300 VCLXCheckBox* pVCLXCheckBox = static_cast< VCLXCheckBox* >( GetVCLXWindow() );
301 if ( pVCLXCheckBox )
302 aValue <<= (sal_Int32) pVCLXCheckBox->getState();
304 return aValue;
307 // -----------------------------------------------------------------------------
309 sal_Bool VCLXAccessibleCheckBox::setCurrentValue( const Any& aNumber ) throw (RuntimeException)
311 OExternalLockGuard aGuard( this );
313 sal_Bool bReturn = sal_False;
315 VCLXCheckBox* pVCLXCheckBox = static_cast< VCLXCheckBox* >( GetVCLXWindow() );
316 if ( pVCLXCheckBox )
318 sal_Int32 nValue = 0, nValueMin = 0, nValueMax = 0;
319 OSL_VERIFY( aNumber >>= nValue );
320 OSL_VERIFY( getMinimumValue() >>= nValueMin );
321 OSL_VERIFY( getMaximumValue() >>= nValueMax );
323 if ( nValue < nValueMin )
324 nValue = nValueMin;
325 else if ( nValue > nValueMax )
326 nValue = nValueMax;
328 pVCLXCheckBox->setState( (sal_Int16) nValue );
329 bReturn = sal_True;
332 return bReturn;
335 // -----------------------------------------------------------------------------
337 Any VCLXAccessibleCheckBox::getMaximumValue( ) throw (RuntimeException)
339 OExternalLockGuard aGuard( this );
341 Any aValue;
343 CheckBox* pCheckBox = (CheckBox*) GetWindow();
344 if ( pCheckBox && pCheckBox->IsTriStateEnabled() )
345 aValue <<= (sal_Int32) 2;
346 else
347 aValue <<= (sal_Int32) 1;
349 return aValue;
352 // -----------------------------------------------------------------------------
354 Any VCLXAccessibleCheckBox::getMinimumValue( ) throw (RuntimeException)
356 OExternalLockGuard aGuard( this );
358 Any aValue;
359 aValue <<= (sal_Int32) 0;
361 return aValue;
364 // -----------------------------------------------------------------------------