1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/vclxaccessiblecheckbox.hxx>
22 #include <toolkit/awt/vclxwindows.hxx>
23 #include <accessibility/helper/accresmgr.hxx>
24 #include <accessibility/helper/accessiblestrings.hrc>
26 #include <unotools/accessiblestatesethelper.hxx>
27 #include <comphelper/accessiblekeybindinghelper.hxx>
28 #include <com/sun/star/awt/KeyModifier.hpp>
29 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
30 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
31 #include <cppuhelper/typeprovider.hxx>
32 #include <comphelper/sequence.hxx>
34 #include <vcl/button.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
;
45 // VCLXAccessibleCheckBox
48 VCLXAccessibleCheckBox::VCLXAccessibleCheckBox( VCLXWindow
* pVCLWindow
)
49 :VCLXAccessibleTextComponent( pVCLWindow
)
51 m_bChecked
= IsChecked();
52 m_bIndeterminate
= IsIndeterminate();
57 VCLXAccessibleCheckBox::~VCLXAccessibleCheckBox()
63 bool VCLXAccessibleCheckBox::IsChecked()
65 bool bChecked
= false;
67 VCLXCheckBox
* pVCLXCheckBox
= static_cast< VCLXCheckBox
* >( GetVCLXWindow() );
68 if ( pVCLXCheckBox
&& pVCLXCheckBox
->getState() == (sal_Int16
) 1 )
76 bool VCLXAccessibleCheckBox::IsIndeterminate()
78 bool bIndeterminate
= false;
80 VCLXCheckBox
* pVCLXCheckBox
= static_cast< VCLXCheckBox
* >( GetVCLXWindow() );
81 if ( pVCLXCheckBox
&& pVCLXCheckBox
->getState() == (sal_Int16
) 2 )
82 bIndeterminate
= true;
84 return bIndeterminate
;
89 void VCLXAccessibleCheckBox::SetChecked( bool bChecked
)
91 if ( m_bChecked
!= bChecked
)
93 Any aOldValue
, aNewValue
;
95 aOldValue
<<= AccessibleStateType::CHECKED
;
97 aNewValue
<<= AccessibleStateType::CHECKED
;
98 m_bChecked
= bChecked
;
99 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED
, aOldValue
, aNewValue
);
105 void VCLXAccessibleCheckBox::SetIndeterminate( bool bIndeterminate
)
107 if ( m_bIndeterminate
!= bIndeterminate
)
109 Any aOldValue
, aNewValue
;
110 if ( m_bIndeterminate
)
111 aOldValue
<<= AccessibleStateType::INDETERMINATE
;
113 aNewValue
<<= AccessibleStateType::INDETERMINATE
;
114 m_bIndeterminate
= bIndeterminate
;
115 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED
, aOldValue
, aNewValue
);
121 void VCLXAccessibleCheckBox::ProcessWindowEvent( const VclWindowEvent
& rVclWindowEvent
)
123 switch ( rVclWindowEvent
.GetId() )
125 case VCLEVENT_CHECKBOX_TOGGLE
:
127 SetChecked( IsChecked() );
128 SetIndeterminate( IsIndeterminate() );
132 VCLXAccessibleTextComponent::ProcessWindowEvent( rVclWindowEvent
);
138 void VCLXAccessibleCheckBox::FillAccessibleStateSet( utl::AccessibleStateSetHelper
& rStateSet
)
140 VCLXAccessibleTextComponent::FillAccessibleStateSet( rStateSet
);
142 rStateSet
.AddState( AccessibleStateType::FOCUSABLE
);
145 rStateSet
.AddState( AccessibleStateType::CHECKED
);
147 if ( IsIndeterminate() )
148 rStateSet
.AddState( AccessibleStateType::INDETERMINATE
);
155 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleCheckBox
, VCLXAccessibleTextComponent
, VCLXAccessibleCheckBox_BASE
)
161 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleCheckBox
, VCLXAccessibleTextComponent
, VCLXAccessibleCheckBox_BASE
)
167 OUString
VCLXAccessibleCheckBox::getImplementationName() throw (RuntimeException
, std::exception
)
169 return OUString( "com.sun.star.comp.toolkit.AccessibleCheckBox" );
174 Sequence
< OUString
> VCLXAccessibleCheckBox::getSupportedServiceNames() throw (RuntimeException
, std::exception
)
176 Sequence
< OUString
> aNames(1);
177 aNames
[0] = "com.sun.star.awt.AccessibleCheckBox";
185 sal_Int32
VCLXAccessibleCheckBox::getAccessibleActionCount( ) throw (RuntimeException
, std::exception
)
187 OExternalLockGuard
aGuard( this );
194 sal_Bool
VCLXAccessibleCheckBox::doAccessibleAction ( sal_Int32 nIndex
) throw (IndexOutOfBoundsException
, RuntimeException
, std::exception
)
196 OExternalLockGuard
aGuard( this );
198 if ( nIndex
< 0 || nIndex
>= getAccessibleActionCount() )
199 throw IndexOutOfBoundsException();
201 VclPtr
< CheckBox
> pCheckBox
= GetAs
< CheckBox
>();
202 VCLXCheckBox
* pVCLXCheckBox
= static_cast< VCLXCheckBox
* >( GetVCLXWindow() );
203 if ( pCheckBox
&& pVCLXCheckBox
)
205 sal_Int32 nValueMin
= (sal_Int32
) 0;
206 sal_Int32 nValueMax
= (sal_Int32
) 1;
208 if ( pCheckBox
->IsTriStateEnabled() )
209 nValueMax
= (sal_Int32
) 2;
211 sal_Int32 nValue
= (sal_Int32
) pVCLXCheckBox
->getState();
215 if ( nValue
> nValueMax
)
218 pVCLXCheckBox
->setState( (sal_Int16
) nValue
);
226 OUString
VCLXAccessibleCheckBox::getAccessibleActionDescription ( sal_Int32 nIndex
) throw (IndexOutOfBoundsException
, RuntimeException
, std::exception
)
228 OExternalLockGuard
aGuard( this );
230 if ( nIndex
< 0 || nIndex
>= getAccessibleActionCount() )
231 throw IndexOutOfBoundsException();
234 return TK_RES_STRING( RID_STR_ACC_ACTION_UNCHECK
);
236 return TK_RES_STRING( RID_STR_ACC_ACTION_CHECK
);
241 Reference
< XAccessibleKeyBinding
> VCLXAccessibleCheckBox::getAccessibleActionKeyBinding( sal_Int32 nIndex
) throw (IndexOutOfBoundsException
, RuntimeException
, std::exception
)
243 OExternalLockGuard
aGuard( this );
245 if ( nIndex
< 0 || nIndex
>= getAccessibleActionCount() )
246 throw IndexOutOfBoundsException();
248 OAccessibleKeyBindingHelper
* pKeyBindingHelper
= new OAccessibleKeyBindingHelper();
249 Reference
< XAccessibleKeyBinding
> xKeyBinding
= pKeyBindingHelper
;
251 vcl::Window
* pWindow
= GetWindow();
254 KeyEvent aKeyEvent
= pWindow
->GetActivationKey();
255 vcl::KeyCode aKeyCode
= aKeyEvent
.GetKeyCode();
256 if ( aKeyCode
.GetCode() != 0 )
258 awt::KeyStroke aKeyStroke
;
259 aKeyStroke
.Modifiers
= 0;
260 if ( aKeyCode
.IsShift() )
261 aKeyStroke
.Modifiers
|= awt::KeyModifier::SHIFT
;
262 if ( aKeyCode
.IsMod1() )
263 aKeyStroke
.Modifiers
|= awt::KeyModifier::MOD1
;
264 if ( aKeyCode
.IsMod2() )
265 aKeyStroke
.Modifiers
|= awt::KeyModifier::MOD2
;
266 if ( aKeyCode
.IsMod3() )
267 aKeyStroke
.Modifiers
|= awt::KeyModifier::MOD3
;
268 aKeyStroke
.KeyCode
= aKeyCode
.GetCode();
269 aKeyStroke
.KeyChar
= aKeyEvent
.GetCharCode();
270 aKeyStroke
.KeyFunc
= static_cast< sal_Int16
>( aKeyCode
.GetFunction() );
271 pKeyBindingHelper
->AddKeyBinding( aKeyStroke
);
282 Any
VCLXAccessibleCheckBox::getCurrentValue( ) throw (RuntimeException
, std::exception
)
284 OExternalLockGuard
aGuard( this );
288 VCLXCheckBox
* pVCLXCheckBox
= static_cast< VCLXCheckBox
* >( GetVCLXWindow() );
290 aValue
<<= (sal_Int32
) pVCLXCheckBox
->getState();
297 sal_Bool
VCLXAccessibleCheckBox::setCurrentValue( const Any
& aNumber
) throw (RuntimeException
, std::exception
)
299 OExternalLockGuard
aGuard( this );
301 bool bReturn
= false;
303 VCLXCheckBox
* pVCLXCheckBox
= static_cast< VCLXCheckBox
* >( GetVCLXWindow() );
306 sal_Int32 nValue
= 0, nValueMin
= 0, nValueMax
= 0;
307 OSL_VERIFY( aNumber
>>= nValue
);
308 OSL_VERIFY( getMinimumValue() >>= nValueMin
);
309 OSL_VERIFY( getMaximumValue() >>= nValueMax
);
311 if ( nValue
< nValueMin
)
313 else if ( nValue
> nValueMax
)
316 pVCLXCheckBox
->setState( (sal_Int16
) nValue
);
325 Any
VCLXAccessibleCheckBox::getMaximumValue( ) throw (RuntimeException
, std::exception
)
327 OExternalLockGuard
aGuard( this );
331 VclPtr
< CheckBox
> pCheckBox
= GetAs
< CheckBox
>();
332 if ( pCheckBox
&& pCheckBox
->IsTriStateEnabled() )
333 aValue
<<= (sal_Int32
) 2;
335 aValue
<<= (sal_Int32
) 1;
342 Any
VCLXAccessibleCheckBox::getMinimumValue( ) throw (RuntimeException
, std::exception
)
344 OExternalLockGuard
aGuard( this );
347 aValue
<<= (sal_Int32
) 0;
354 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */