Branch libreoffice-5-0-4
[LibreOffice.git] / accessibility / source / standard / vclxaccessibleradiobutton.cxx
blob23d8a92b268b8ecc67efb783da0d10dd310e9071
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 <accessibility/standard/vclxaccessibleradiobutton.hxx>
22 #include <toolkit/awt/vclxwindows.hxx>
23 #include <accessibility/helper/accresmgr.hxx>
24 #include <accessibility/helper/accessiblestrings.hrc>
26 #include <unotools/accessiblerelationsethelper.hxx>
27 #include <unotools/accessiblestatesethelper.hxx>
28 #include <comphelper/accessiblekeybindinghelper.hxx>
29 #include <com/sun/star/awt/KeyModifier.hpp>
30 #include <com/sun/star/accessibility/AccessibleRelationType.hpp>
31 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
32 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
33 #include <cppuhelper/typeprovider.hxx>
34 #include <comphelper/sequence.hxx>
35 #include <vcl/window.hxx>
36 #include <vcl/button.hxx>
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::lang;
42 using namespace ::com::sun::star::beans;
43 using namespace ::com::sun::star::accessibility;
44 using namespace ::comphelper;
48 // VCLXAccessibleRadioButton
51 VCLXAccessibleRadioButton::VCLXAccessibleRadioButton( VCLXWindow* pVCLWindow )
52 :VCLXAccessibleTextComponent( pVCLWindow )
58 VCLXAccessibleRadioButton::~VCLXAccessibleRadioButton()
64 void VCLXAccessibleRadioButton::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
66 switch ( rVclWindowEvent.GetId() )
68 case VCLEVENT_RADIOBUTTON_TOGGLE:
70 Any aOldValue;
71 Any aNewValue;
73 VCLXRadioButton* pVCLXRadioButton = static_cast< VCLXRadioButton* >( GetVCLXWindow() );
74 if ( pVCLXRadioButton && pVCLXRadioButton->getState() )
75 aNewValue <<= AccessibleStateType::CHECKED;
76 else
77 aOldValue <<= AccessibleStateType::CHECKED;
79 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
81 break;
82 default:
83 VCLXAccessibleTextComponent::ProcessWindowEvent( rVclWindowEvent );
89 void VCLXAccessibleRadioButton::FillAccessibleRelationSet( utl::AccessibleRelationSetHelper& rRelationSet )
91 VCLXAccessibleTextComponent::FillAccessibleRelationSet( rRelationSet );
93 VclPtr< RadioButton > pRadioButton = GetAsDynamic< RadioButton >();
94 if ( pRadioButton )
96 ::std::vector< VclPtr<RadioButton> > aGroup(pRadioButton->GetRadioButtonGroup(true));
97 if (!aGroup.empty())
99 sal_Int32 i = 0;
100 Sequence< Reference< XInterface > > aSequence( static_cast< sal_Int32 >( aGroup.size() ) );
101 auto aEndItr = aGroup.end();
102 for ( auto aItr = aGroup.begin(); aItr < aEndItr; ++aItr )
104 aSequence[i++] = (*aItr)->GetAccessible();
106 rRelationSet.AddRelation( AccessibleRelation( AccessibleRelationType::MEMBER_OF, aSequence ) );
113 void VCLXAccessibleRadioButton::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
115 VCLXAccessibleTextComponent::FillAccessibleStateSet( rStateSet );
117 VCLXRadioButton* pVCLXRadioButton = static_cast< VCLXRadioButton* >( GetVCLXWindow() );
118 if ( pVCLXRadioButton )
120 rStateSet.AddState( AccessibleStateType::FOCUSABLE );
121 if ( pVCLXRadioButton->getState() )
122 rStateSet.AddState( AccessibleStateType::CHECKED );
127 // XInterface
130 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleRadioButton, VCLXAccessibleTextComponent, VCLXAccessibleRadioButton_BASE )
133 // XTypeProvider
136 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleRadioButton, VCLXAccessibleTextComponent, VCLXAccessibleRadioButton_BASE )
139 // XServiceInfo
142 OUString VCLXAccessibleRadioButton::getImplementationName() throw (RuntimeException, std::exception)
144 return OUString( "com.sun.star.comp.toolkit.AccessibleRadioButton" );
149 Sequence< OUString > VCLXAccessibleRadioButton::getSupportedServiceNames() throw (RuntimeException, std::exception)
151 Sequence< OUString > aNames(1);
152 aNames[0] = "com.sun.star.awt.AccessibleRadioButton";
153 return aNames;
157 // XAccessibleAction
160 sal_Int32 VCLXAccessibleRadioButton::getAccessibleActionCount( ) throw (RuntimeException, std::exception)
162 OExternalLockGuard aGuard( this );
164 return 1;
169 sal_Bool VCLXAccessibleRadioButton::doAccessibleAction ( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
171 OExternalLockGuard aGuard( this );
173 if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
174 throw IndexOutOfBoundsException();
176 VCLXRadioButton* pVCLXRadioButton = static_cast< VCLXRadioButton* >( GetVCLXWindow() );
177 if ( pVCLXRadioButton && !pVCLXRadioButton->getState() )
178 pVCLXRadioButton->setState( true );
180 return true;
185 OUString VCLXAccessibleRadioButton::getAccessibleActionDescription ( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
187 OExternalLockGuard aGuard( this );
189 if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
190 throw IndexOutOfBoundsException();
192 return TK_RES_STRING( RID_STR_ACC_ACTION_SELECT );
197 Reference< XAccessibleKeyBinding > VCLXAccessibleRadioButton::getAccessibleActionKeyBinding( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
199 OExternalLockGuard aGuard( this );
201 if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
202 throw IndexOutOfBoundsException();
204 OAccessibleKeyBindingHelper* pKeyBindingHelper = new OAccessibleKeyBindingHelper();
205 Reference< XAccessibleKeyBinding > xKeyBinding = pKeyBindingHelper;
207 vcl::Window* pWindow = GetWindow();
208 if ( pWindow )
210 KeyEvent aKeyEvent = pWindow->GetActivationKey();
211 vcl::KeyCode aKeyCode = aKeyEvent.GetKeyCode();
212 if ( aKeyCode.GetCode() != 0 )
214 awt::KeyStroke aKeyStroke;
215 aKeyStroke.Modifiers = 0;
216 if ( aKeyCode.IsShift() )
217 aKeyStroke.Modifiers |= awt::KeyModifier::SHIFT;
218 if ( aKeyCode.IsMod1() )
219 aKeyStroke.Modifiers |= awt::KeyModifier::MOD1;
220 if ( aKeyCode.IsMod2() )
221 aKeyStroke.Modifiers |= awt::KeyModifier::MOD2;
222 if ( aKeyCode.IsMod3() )
223 aKeyStroke.Modifiers |= awt::KeyModifier::MOD3;
224 aKeyStroke.KeyCode = aKeyCode.GetCode();
225 aKeyStroke.KeyChar = aKeyEvent.GetCharCode();
226 aKeyStroke.KeyFunc = static_cast< sal_Int16 >( aKeyCode.GetFunction() );
227 pKeyBindingHelper->AddKeyBinding( aKeyStroke );
231 return xKeyBinding;
235 // XAccessibleValue
238 Any VCLXAccessibleRadioButton::getCurrentValue( ) throw (RuntimeException, std::exception)
240 OExternalLockGuard aGuard( this );
242 Any aValue;
244 VCLXRadioButton* pVCLXRadioButton = static_cast< VCLXRadioButton* >( GetVCLXWindow() );
245 if ( pVCLXRadioButton )
246 aValue <<= (sal_Int32) pVCLXRadioButton->getState();
248 return aValue;
253 sal_Bool VCLXAccessibleRadioButton::setCurrentValue( const Any& aNumber ) throw (RuntimeException, std::exception)
255 OExternalLockGuard aGuard( this );
257 bool bReturn = false;
259 VCLXRadioButton* pVCLXRadioButton = static_cast< VCLXRadioButton* >( GetVCLXWindow() );
260 if ( pVCLXRadioButton )
262 sal_Int32 nValue = 0;
263 OSL_VERIFY( aNumber >>= nValue );
265 if ( nValue < 0 )
266 nValue = 0;
267 else if ( nValue > 1 )
268 nValue = 1;
270 pVCLXRadioButton->setState( nValue == 1 );
271 bReturn = true;
274 return bReturn;
279 Any VCLXAccessibleRadioButton::getMaximumValue( ) throw (RuntimeException, std::exception)
281 OExternalLockGuard aGuard( this );
283 Any aValue;
284 aValue <<= (sal_Int32) 1;
286 return aValue;
291 Any VCLXAccessibleRadioButton::getMinimumValue( ) throw (RuntimeException, std::exception)
293 OExternalLockGuard aGuard( this );
295 Any aValue;
296 aValue <<= (sal_Int32) 0;
298 return aValue;
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */