1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_accessibility.hxx"
30 
31 // includes --------------------------------------------------------------
32 #include <accessibility/standard/vclxaccessibleradiobutton.hxx>
33 
34 #include <toolkit/awt/vclxwindows.hxx>
35 #include <accessibility/helper/accresmgr.hxx>
36 #include <accessibility/helper/accessiblestrings.hrc>
37 
38 #include <unotools/accessiblerelationsethelper.hxx>
39 #include <unotools/accessiblestatesethelper.hxx>
40 #include <comphelper/accessiblekeybindinghelper.hxx>
41 #include <com/sun/star/awt/KeyModifier.hpp>
42 #include <com/sun/star/accessibility/AccessibleRelationType.hpp>
43 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
44 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
45 #include <cppuhelper/typeprovider.hxx>
46 #include <comphelper/sequence.hxx>
47 #include <vcl/window.hxx>
48 #include <vcl/button.hxx>
49 
50 
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;
57 
58 
59 // -----------------------------------------------------------------------------
60 // VCLXAccessibleRadioButton
61 // -----------------------------------------------------------------------------
62 
63 VCLXAccessibleRadioButton::VCLXAccessibleRadioButton( VCLXWindow* pVCLWindow )
64 	:VCLXAccessibleTextComponent( pVCLWindow )
65 {
66 }
67 
68 // -----------------------------------------------------------------------------
69 
70 VCLXAccessibleRadioButton::~VCLXAccessibleRadioButton()
71 {
72 }
73 
74 // -----------------------------------------------------------------------------
75 
76 void VCLXAccessibleRadioButton::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
77 {
78     switch ( rVclWindowEvent.GetId() )
79     {
80 		case VCLEVENT_RADIOBUTTON_TOGGLE:
81         {
82 			Any aOldValue;
83 			Any aNewValue;
84 
85 			VCLXRadioButton* pVCLXRadioButton = static_cast< VCLXRadioButton* >( GetVCLXWindow() );
86 			if ( pVCLXRadioButton && pVCLXRadioButton->getState() )
87 				aNewValue <<= AccessibleStateType::CHECKED;
88 			else
89 				aOldValue <<= AccessibleStateType::CHECKED;
90 
91             NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
92         }
93         break;
94 		default:
95 			VCLXAccessibleTextComponent::ProcessWindowEvent( rVclWindowEvent );
96    }
97 }
98 
99 // -----------------------------------------------------------------------------
100 
101 void VCLXAccessibleRadioButton::FillAccessibleRelationSet( utl::AccessibleRelationSetHelper& rRelationSet )
102 {
103     VCLXAccessibleTextComponent::FillAccessibleRelationSet( rRelationSet );
104 
105     RadioButton* pRadioButton = dynamic_cast< RadioButton* >( GetWindow() );
106     if ( pRadioButton )
107     {
108         ::std::vector< RadioButton* > aGroup;
109         pRadioButton->GetRadioButtonGroup( aGroup, true );
110         if ( !aGroup.empty() )
111         {
112             sal_Int32 i = 0;
113             Sequence< Reference< XInterface > > aSequence( static_cast< sal_Int32 >( aGroup.size() ) );
114             ::std::vector< RadioButton* >::const_iterator aEndItr = aGroup.end();
115             for ( ::std::vector< RadioButton* >::const_iterator aItr = aGroup.begin(); aItr < aEndItr; ++aItr )
116             {
117                 aSequence[i++] = (*aItr)->GetAccessible();
118             }
119             rRelationSet.AddRelation( AccessibleRelation( AccessibleRelationType::MEMBER_OF, aSequence ) );
120         }
121     }
122 }
123 
124 // -----------------------------------------------------------------------------
125 
126 void VCLXAccessibleRadioButton::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
127 {
128 	VCLXAccessibleTextComponent::FillAccessibleStateSet( rStateSet );
129 
130 	VCLXRadioButton* pVCLXRadioButton = static_cast< VCLXRadioButton* >( GetVCLXWindow() );
131 	if ( pVCLXRadioButton )
132 	{
133         rStateSet.AddState( AccessibleStateType::FOCUSABLE );
134 		if ( pVCLXRadioButton->getState() )
135             rStateSet.AddState( AccessibleStateType::CHECKED );
136 	}
137 }
138 
139 // -----------------------------------------------------------------------------
140 // XInterface
141 // -----------------------------------------------------------------------------
142 
143 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleRadioButton, VCLXAccessibleTextComponent, VCLXAccessibleRadioButton_BASE )
144 
145 // -----------------------------------------------------------------------------
146 // XTypeProvider
147 // -----------------------------------------------------------------------------
148 
149 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleRadioButton, VCLXAccessibleTextComponent, VCLXAccessibleRadioButton_BASE )
150 
151 // -----------------------------------------------------------------------------
152 // XServiceInfo
153 // -----------------------------------------------------------------------------
154 
155 ::rtl::OUString VCLXAccessibleRadioButton::getImplementationName() throw (RuntimeException)
156 {
157 	return ::rtl::OUString::createFromAscii( "com.sun.star.comp.toolkit.AccessibleRadioButton" );
158 }
159 
160 // -----------------------------------------------------------------------------
161 
162 Sequence< ::rtl::OUString > VCLXAccessibleRadioButton::getSupportedServiceNames() throw (RuntimeException)
163 {
164 	Sequence< ::rtl::OUString > aNames(1);
165 	aNames[0] = ::rtl::OUString::createFromAscii( "com.sun.star.awt.AccessibleRadioButton" );
166 	return aNames;
167 }
168 
169 // -----------------------------------------------------------------------------
170 // XAccessibleAction
171 // -----------------------------------------------------------------------------
172 
173 sal_Int32 VCLXAccessibleRadioButton::getAccessibleActionCount( ) throw (RuntimeException)
174 {
175 	OExternalLockGuard aGuard( this );
176 
177 	return 1;
178 }
179 
180 // -----------------------------------------------------------------------------
181 
182 sal_Bool VCLXAccessibleRadioButton::doAccessibleAction ( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
183 {
184 	OExternalLockGuard aGuard( this );
185 
186 	if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
187         throw IndexOutOfBoundsException();
188 
189 	VCLXRadioButton* pVCLXRadioButton = static_cast< VCLXRadioButton* >( GetVCLXWindow() );
190 	if ( pVCLXRadioButton && !pVCLXRadioButton->getState() )
191 		pVCLXRadioButton->setState( sal_True );
192 
193 	return sal_True;
194 }
195 
196 // -----------------------------------------------------------------------------
197 
198 ::rtl::OUString VCLXAccessibleRadioButton::getAccessibleActionDescription ( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
199 {
200 	OExternalLockGuard aGuard( this );
201 
202 	if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
203         throw IndexOutOfBoundsException();
204 
205 	return ::rtl::OUString( TK_RES_STRING( RID_STR_ACC_ACTION_CLICK ) );
206 }
207 
208 // -----------------------------------------------------------------------------
209 
210 Reference< XAccessibleKeyBinding > VCLXAccessibleRadioButton::getAccessibleActionKeyBinding( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
211 {
212     OExternalLockGuard aGuard( this );
213 
214     if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
215         throw IndexOutOfBoundsException();
216 
217     OAccessibleKeyBindingHelper* pKeyBindingHelper = new OAccessibleKeyBindingHelper();
218     Reference< XAccessibleKeyBinding > xKeyBinding = pKeyBindingHelper;
219 
220     Window* pWindow = GetWindow();
221     if ( pWindow )
222     {
223         KeyEvent aKeyEvent = pWindow->GetActivationKey();
224         KeyCode aKeyCode = aKeyEvent.GetKeyCode();
225         if ( aKeyCode.GetCode() != 0 )
226         {
227             awt::KeyStroke aKeyStroke;
228             aKeyStroke.Modifiers = 0;
229             if ( aKeyCode.IsShift() )
230                 aKeyStroke.Modifiers |= awt::KeyModifier::SHIFT;
231             if ( aKeyCode.IsMod1() )
232                 aKeyStroke.Modifiers |= awt::KeyModifier::MOD1;
233             if ( aKeyCode.IsMod2() )
234                 aKeyStroke.Modifiers |= awt::KeyModifier::MOD2;
235             if ( aKeyCode.IsMod3() )
236                 aKeyStroke.Modifiers |= awt::KeyModifier::MOD3;
237             aKeyStroke.KeyCode = aKeyCode.GetCode();
238             aKeyStroke.KeyChar = aKeyEvent.GetCharCode();
239             aKeyStroke.KeyFunc = static_cast< sal_Int16 >( aKeyCode.GetFunction() );
240             pKeyBindingHelper->AddKeyBinding( aKeyStroke );
241         }
242     }
243 
244     return xKeyBinding;
245 }
246 
247 // -----------------------------------------------------------------------------
248 // XAccessibleValue
249 // -----------------------------------------------------------------------------
250 
251 Any VCLXAccessibleRadioButton::getCurrentValue(  ) throw (RuntimeException)
252 {
253 	OExternalLockGuard aGuard( this );
254 
255 	Any aValue;
256 
257 	VCLXRadioButton* pVCLXRadioButton = static_cast< VCLXRadioButton* >( GetVCLXWindow() );
258 	if ( pVCLXRadioButton )
259 		aValue <<= (sal_Int32) pVCLXRadioButton->getState();
260 
261 	return aValue;
262 }
263 
264 // -----------------------------------------------------------------------------
265 
266 sal_Bool VCLXAccessibleRadioButton::setCurrentValue( const Any& aNumber ) throw (RuntimeException)
267 {
268 	OExternalLockGuard aGuard( this );
269 
270 	sal_Bool bReturn = sal_False;
271 
272 	VCLXRadioButton* pVCLXRadioButton = static_cast< VCLXRadioButton* >( GetVCLXWindow() );
273 	if ( pVCLXRadioButton )
274 	{
275 		sal_Int32 nValue = 0;
276 		OSL_VERIFY( aNumber >>= nValue );
277 
278 		if ( nValue < 0 )
279 			nValue = 0;
280 		else if ( nValue > 1 )
281 			nValue = 1;
282 
283 		pVCLXRadioButton->setState( (sal_Bool) nValue );
284 		bReturn = sal_True;
285 	}
286 
287 	return bReturn;
288 }
289 
290 // -----------------------------------------------------------------------------
291 
292 Any VCLXAccessibleRadioButton::getMaximumValue(  ) throw (RuntimeException)
293 {
294 	OExternalLockGuard aGuard( this );
295 
296 	Any aValue;
297 	aValue <<= (sal_Int32) 1;
298 
299 	return aValue;
300 }
301 
302 // -----------------------------------------------------------------------------
303 
304 Any VCLXAccessibleRadioButton::getMinimumValue(  ) throw (RuntimeException)
305 {
306 	OExternalLockGuard aGuard( this );
307 
308 	Any aValue;
309 	aValue <<= (sal_Int32) 0;
310 
311 	return aValue;
312 }
313 
314 // -----------------------------------------------------------------------------
315