tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / accessibility / source / standard / vclxaccessiblecheckbox.cxx
blob34eb97635f2da26f70f28b61451c53ff0be59645
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 <standard/vclxaccessiblecheckbox.hxx>
22 #include <helper/accresmgr.hxx>
23 #include <strings.hrc>
25 #include <comphelper/accessiblecontexthelper.hxx>
26 #include <comphelper/accessiblekeybindinghelper.hxx>
27 #include <com/sun/star/awt/KeyModifier.hpp>
28 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
29 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
30 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
32 #include <vcl/event.hxx>
33 #include <vcl/vclevent.hxx>
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::uno;
37 using namespace ::com::sun::star::lang;
38 using namespace ::com::sun::star::accessibility;
39 using namespace ::comphelper;
42 // VCLXAccessibleCheckBox
45 VCLXAccessibleCheckBox::VCLXAccessibleCheckBox(CheckBox* pCheckBox)
46 :ImplInheritanceHelper(pCheckBox)
48 m_bChecked = IsChecked();
49 m_bIndeterminate = IsIndeterminate();
53 bool VCLXAccessibleCheckBox::IsChecked() const
55 VclPtr<CheckBox> pCheckBox = GetAs<CheckBox>();
56 return pCheckBox && pCheckBox->IsChecked();
60 bool VCLXAccessibleCheckBox::IsIndeterminate() const
62 VclPtr<CheckBox> pCheckBox = GetAs<CheckBox>();
63 return pCheckBox && pCheckBox->GetState() == TRISTATE_INDET;
67 void VCLXAccessibleCheckBox::SetChecked( bool bChecked )
69 if ( m_bChecked != bChecked )
71 Any aOldValue, aNewValue;
72 if ( m_bChecked )
73 aOldValue <<= AccessibleStateType::CHECKED;
74 else
75 aNewValue <<= AccessibleStateType::CHECKED;
76 m_bChecked = bChecked;
77 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
82 void VCLXAccessibleCheckBox::SetIndeterminate( bool bIndeterminate )
84 if ( m_bIndeterminate != bIndeterminate )
86 Any aOldValue, aNewValue;
87 if ( m_bIndeterminate )
88 aOldValue <<= AccessibleStateType::INDETERMINATE;
89 else
90 aNewValue <<= AccessibleStateType::INDETERMINATE;
91 m_bIndeterminate = bIndeterminate;
92 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
97 void VCLXAccessibleCheckBox::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
99 switch ( rVclWindowEvent.GetId() )
101 case VclEventId::CheckboxToggle:
103 const sal_Int32 nOldValue = m_bIndeterminate ? 2 : (m_bChecked ? 1 : 0);
105 SetChecked( IsChecked() );
106 SetIndeterminate( IsIndeterminate() );
108 const sal_Int32 nNewValue = m_bIndeterminate ? 2 : (m_bChecked ? 1 : 0);
109 if (nOldValue != nNewValue)
110 NotifyAccessibleEvent(AccessibleEventId::VALUE_CHANGED, Any(nOldValue), Any(nNewValue));
112 break;
113 default:
114 VCLXAccessibleTextComponent::ProcessWindowEvent( rVclWindowEvent );
119 void VCLXAccessibleCheckBox::FillAccessibleStateSet( sal_Int64& rStateSet )
121 VCLXAccessibleTextComponent::FillAccessibleStateSet( rStateSet );
123 rStateSet |= AccessibleStateType::CHECKABLE;
124 rStateSet |= AccessibleStateType::FOCUSABLE;
126 if ( IsChecked() )
127 rStateSet |= AccessibleStateType::CHECKED;
129 if ( IsIndeterminate() )
130 rStateSet |= AccessibleStateType::INDETERMINATE;
134 // XServiceInfo
137 OUString VCLXAccessibleCheckBox::getImplementationName()
139 return u"com.sun.star.comp.toolkit.AccessibleCheckBox"_ustr;
143 Sequence< OUString > VCLXAccessibleCheckBox::getSupportedServiceNames()
145 return { u"com.sun.star.awt.AccessibleCheckBox"_ustr };
149 // XAccessibleAction
152 sal_Int32 VCLXAccessibleCheckBox::getAccessibleActionCount( )
154 OExternalLockGuard aGuard( this );
156 return 1;
160 sal_Bool VCLXAccessibleCheckBox::doAccessibleAction ( sal_Int32 nIndex )
162 OExternalLockGuard aGuard( this );
164 if ( nIndex != 0 )
165 throw IndexOutOfBoundsException();
167 VclPtr<CheckBox> pCheckBox = GetAs<CheckBox>();
168 if (pCheckBox)
170 sal_Int32 nValueMax = sal_Int32(1);
172 if ( pCheckBox->IsTriStateEnabled() )
173 nValueMax = sal_Int32(2);
175 sal_Int32 nValue = static_cast<sal_Int32>(pCheckBox->GetState());
177 ++nValue;
179 if ( nValue > nValueMax )
180 nValue = 0;
182 pCheckBox->SetState(static_cast<TriState>(nValue));
185 return true;
189 OUString VCLXAccessibleCheckBox::getAccessibleActionDescription ( sal_Int32 nIndex )
191 OExternalLockGuard aGuard( this );
193 if ( nIndex != 0 )
194 throw IndexOutOfBoundsException();
196 if(IsChecked())
197 return AccResId( RID_STR_ACC_ACTION_UNCHECK );
198 else
199 return AccResId( RID_STR_ACC_ACTION_CHECK );
203 Reference< XAccessibleKeyBinding > VCLXAccessibleCheckBox::getAccessibleActionKeyBinding( sal_Int32 nIndex )
205 OExternalLockGuard aGuard( this );
207 if ( nIndex != 0 )
208 throw IndexOutOfBoundsException();
210 rtl::Reference<OAccessibleKeyBindingHelper> pKeyBindingHelper = new OAccessibleKeyBindingHelper();
212 VclPtr<vcl::Window> pWindow = GetWindow();
213 if ( pWindow )
215 KeyEvent aKeyEvent = pWindow->GetActivationKey();
216 vcl::KeyCode aKeyCode = aKeyEvent.GetKeyCode();
217 if ( aKeyCode.GetCode() != 0 )
219 awt::KeyStroke aKeyStroke;
220 aKeyStroke.Modifiers = 0;
221 if ( aKeyCode.IsShift() )
222 aKeyStroke.Modifiers |= awt::KeyModifier::SHIFT;
223 if ( aKeyCode.IsMod1() )
224 aKeyStroke.Modifiers |= awt::KeyModifier::MOD1;
225 if ( aKeyCode.IsMod2() )
226 aKeyStroke.Modifiers |= awt::KeyModifier::MOD2;
227 if ( aKeyCode.IsMod3() )
228 aKeyStroke.Modifiers |= awt::KeyModifier::MOD3;
229 aKeyStroke.KeyCode = aKeyCode.GetCode();
230 aKeyStroke.KeyChar = aKeyEvent.GetCharCode();
231 aKeyStroke.KeyFunc = static_cast< sal_Int16 >( aKeyCode.GetFunction() );
232 pKeyBindingHelper->AddKeyBinding( aKeyStroke );
236 return pKeyBindingHelper;
240 // XAccessibleValue
243 Any VCLXAccessibleCheckBox::getCurrentValue( )
245 OExternalLockGuard aGuard( this );
247 Any aValue;
249 VclPtr<CheckBox> pCheckBox = GetAs<CheckBox>();
250 if (pCheckBox)
251 aValue <<= static_cast<sal_Int32>(pCheckBox->GetState());
253 return aValue;
257 sal_Bool VCLXAccessibleCheckBox::setCurrentValue( const Any& aNumber )
259 OExternalLockGuard aGuard( this );
261 bool bReturn = false;
263 VclPtr<CheckBox> pCheckBox = GetAs<CheckBox>();
264 if (pCheckBox)
266 sal_Int32 nValue = 0, nValueMin = 0, nValueMax = 0;
267 OSL_VERIFY( aNumber >>= nValue );
268 nValueMax=implGetMaximumValue();
270 if ( nValue < nValueMin )
271 nValue = nValueMin;
272 else if ( nValue > nValueMax )
273 nValue = nValueMax;
275 pCheckBox->SetState(static_cast<TriState>(nValue));
276 bReturn = true;
279 return bReturn;
283 Any VCLXAccessibleCheckBox::getMaximumValue( )
285 OExternalLockGuard aGuard( this );
287 Any aValue;
288 aValue <<= implGetMaximumValue();
290 return aValue;
293 sal_Int32 VCLXAccessibleCheckBox::implGetMaximumValue( )
295 VclPtr< CheckBox > pCheckBox = GetAs< CheckBox >();
296 if ( pCheckBox && pCheckBox->IsTriStateEnabled() )
297 return 2;
299 return 1;
302 Any VCLXAccessibleCheckBox::getMinimumValue( )
304 OExternalLockGuard aGuard( this );
306 Any aValue;
307 aValue <<= sal_Int32(0);
309 return aValue;
312 Any VCLXAccessibleCheckBox::getMinimumIncrement( )
314 OExternalLockGuard aGuard( this );
316 Any aValue;
317 aValue <<= sal_Int32(1);
319 return aValue;
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */