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/vclxaccessiblebutton.hxx>
33 #include <accessibility/helper/accresmgr.hxx>
34 #include <accessibility/helper/accessiblestrings.hrc>
35 
36 #include <unotools/accessiblestatesethelper.hxx>
37 #include <comphelper/accessiblekeybindinghelper.hxx>
38 #include <com/sun/star/awt/KeyModifier.hpp>
39 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
40 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
41 #include <cppuhelper/typeprovider.hxx>
42 #include <comphelper/sequence.hxx>
43 
44 #include <vcl/button.hxx>
45 
46 using namespace ::com::sun::star;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::lang;
49 using namespace ::com::sun::star::beans;
50 using namespace ::com::sun::star::accessibility;
51 using namespace ::comphelper;
52 
53 
54 // -----------------------------------------------------------------------------
55 // VCLXAccessibleButton
56 // -----------------------------------------------------------------------------
57 
58 VCLXAccessibleButton::VCLXAccessibleButton( VCLXWindow* pVCLWindow )
59 	:VCLXAccessibleTextComponent( pVCLWindow )
60 {
61 }
62 
63 // -----------------------------------------------------------------------------
64 
65 VCLXAccessibleButton::~VCLXAccessibleButton()
66 {
67 }
68 
69 // -----------------------------------------------------------------------------
70 
71 void VCLXAccessibleButton::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
72 {
73     switch ( rVclWindowEvent.GetId() )
74     {
75 		case VCLEVENT_PUSHBUTTON_TOGGLE:
76         {
77 			Any aOldValue;
78 			Any aNewValue;
79 
80 			PushButton* pButton = (PushButton*) GetWindow();
81 			if ( pButton && pButton->GetState() == STATE_CHECK )
82 				aNewValue <<= AccessibleStateType::CHECKED;
83 			else
84 				aOldValue <<= AccessibleStateType::CHECKED;
85 
86             NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
87         }
88         break;
89 		default:
90 			VCLXAccessibleTextComponent::ProcessWindowEvent( rVclWindowEvent );
91    }
92 }
93 
94 // -----------------------------------------------------------------------------
95 
96 void VCLXAccessibleButton::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
97 {
98 	VCLXAccessibleTextComponent::FillAccessibleStateSet( rStateSet );
99 
100 	PushButton* pButton = (PushButton*) GetWindow();
101 	if ( pButton )
102 	{
103         rStateSet.AddState( AccessibleStateType::FOCUSABLE );
104 
105 		if ( pButton->GetState() == STATE_CHECK )
106             rStateSet.AddState( AccessibleStateType::CHECKED );
107 
108 		if ( pButton->IsPressed() )
109             rStateSet.AddState( AccessibleStateType::PRESSED );
110 	}
111 }
112 
113 // -----------------------------------------------------------------------------
114 // XInterface
115 // -----------------------------------------------------------------------------
116 
117 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleButton, VCLXAccessibleTextComponent, VCLXAccessibleButton_BASE )
118 
119 // -----------------------------------------------------------------------------
120 // XTypeProvider
121 // -----------------------------------------------------------------------------
122 
123 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleButton, VCLXAccessibleTextComponent, VCLXAccessibleButton_BASE )
124 
125 // -----------------------------------------------------------------------------
126 // XServiceInfo
127 // -----------------------------------------------------------------------------
128 
129 ::rtl::OUString VCLXAccessibleButton::getImplementationName() throw (RuntimeException)
130 {
131 	return ::rtl::OUString::createFromAscii( "com.sun.star.comp.toolkit.AccessibleButton" );
132 }
133 
134 // -----------------------------------------------------------------------------
135 
136 Sequence< ::rtl::OUString > VCLXAccessibleButton::getSupportedServiceNames() throw (RuntimeException)
137 {
138 	Sequence< ::rtl::OUString > aNames(1);
139 	aNames[0] = ::rtl::OUString::createFromAscii( "com.sun.star.awt.AccessibleButton" );
140 	return aNames;
141 }
142 
143 // -----------------------------------------------------------------------------
144 // XAccessibleContext
145 // -----------------------------------------------------------------------------
146 
147 ::rtl::OUString VCLXAccessibleButton::getAccessibleName(  ) throw (RuntimeException)
148 {
149 	OExternalLockGuard aGuard( this );
150 
151 	::rtl::OUString aName( VCLXAccessibleTextComponent::getAccessibleName() );
152 	sal_Int32 nLength = aName.getLength();
153 
154 	if ( nLength >= 3 && aName.matchAsciiL( RTL_CONSTASCII_STRINGPARAM("..."), nLength - 3 ) )
155 	{
156 		if ( nLength == 3 )
157 		{
158 			// it's a browse button
159 	        aName = ::rtl::OUString( TK_RES_STRING( RID_STR_ACC_NAME_BROWSEBUTTON ) );
160 		}
161 		else
162 		{
163 			// remove the three trailing dots
164 			aName = aName.copy( 0, nLength - 3 );
165 		}
166 	}
167 	else if ( nLength >= 3 && aName.matchAsciiL( RTL_CONSTASCII_STRINGPARAM("<< "), 0 ) )
168 	{
169 		// remove the leading symbols
170 		aName = aName.copy( 3, nLength - 3 );
171 	}
172 	else if ( nLength >= 3 && aName.matchAsciiL( RTL_CONSTASCII_STRINGPARAM(" >>"), nLength - 3 ) )
173 	{
174 		// remove the trailing symbols
175 		aName = aName.copy( 0, nLength - 3 );
176 	}
177 
178 	return aName;
179 }
180 
181 // -----------------------------------------------------------------------------
182 // XAccessibleAction
183 // -----------------------------------------------------------------------------
184 
185 sal_Int32 VCLXAccessibleButton::getAccessibleActionCount( ) throw (RuntimeException)
186 {
187 	OExternalLockGuard aGuard( this );
188 
189 	return 1;
190 }
191 
192 // -----------------------------------------------------------------------------
193 
194 sal_Bool VCLXAccessibleButton::doAccessibleAction ( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
195 {
196 	OExternalLockGuard aGuard( this );
197 
198 	if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
199         throw IndexOutOfBoundsException();
200 
201 	PushButton* pButton = (PushButton*) GetWindow();
202 	if ( pButton )
203 		pButton->Click();
204 
205 	return sal_True;
206 }
207 
208 // -----------------------------------------------------------------------------
209 
210 ::rtl::OUString VCLXAccessibleButton::getAccessibleActionDescription ( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
211 {
212 	OExternalLockGuard aGuard( this );
213 
214 	if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
215         throw IndexOutOfBoundsException();
216 
217 	return ::rtl::OUString( TK_RES_STRING( RID_STR_ACC_ACTION_CLICK ) );
218 }
219 
220 // -----------------------------------------------------------------------------
221 
222 Reference< XAccessibleKeyBinding > VCLXAccessibleButton::getAccessibleActionKeyBinding( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
223 {
224     OExternalLockGuard aGuard( this );
225 
226     if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
227         throw IndexOutOfBoundsException();
228 
229     OAccessibleKeyBindingHelper* pKeyBindingHelper = new OAccessibleKeyBindingHelper();
230     Reference< XAccessibleKeyBinding > xKeyBinding = pKeyBindingHelper;
231 
232     Window* pWindow = GetWindow();
233     if ( pWindow )
234     {
235         KeyEvent aKeyEvent = pWindow->GetActivationKey();
236         KeyCode aKeyCode = aKeyEvent.GetKeyCode();
237         if ( aKeyCode.GetCode() != 0 )
238         {
239             awt::KeyStroke aKeyStroke;
240             aKeyStroke.Modifiers = 0;
241             if ( aKeyCode.IsShift() )
242                 aKeyStroke.Modifiers |= awt::KeyModifier::SHIFT;
243             if ( aKeyCode.IsMod1() )
244                 aKeyStroke.Modifiers |= awt::KeyModifier::MOD1;
245             if ( aKeyCode.IsMod2() )
246                 aKeyStroke.Modifiers |= awt::KeyModifier::MOD2;
247             if ( aKeyCode.IsMod3() )
248                 aKeyStroke.Modifiers |= awt::KeyModifier::MOD3;
249             aKeyStroke.KeyCode = aKeyCode.GetCode();
250             aKeyStroke.KeyChar = aKeyEvent.GetCharCode();
251             aKeyStroke.KeyFunc = static_cast< sal_Int16 >( aKeyCode.GetFunction() );
252             pKeyBindingHelper->AddKeyBinding( aKeyStroke );
253         }
254     }
255 
256     return xKeyBinding;
257 }
258 
259 // -----------------------------------------------------------------------------
260 // XAccessibleValue
261 // -----------------------------------------------------------------------------
262 
263 Any VCLXAccessibleButton::getCurrentValue(  ) throw (RuntimeException)
264 {
265 	OExternalLockGuard aGuard( this );
266 
267 	Any aValue;
268 
269 	PushButton* pButton = (PushButton*) GetWindow();
270 	if ( pButton )
271 		aValue <<= (sal_Int32) pButton->IsPressed();
272 
273 	return aValue;
274 }
275 
276 // -----------------------------------------------------------------------------
277 
278 sal_Bool VCLXAccessibleButton::setCurrentValue( const Any& aNumber ) throw (RuntimeException)
279 {
280 	OExternalLockGuard aGuard( this );
281 
282 	sal_Bool bReturn = sal_False;
283 
284 	PushButton* pButton = (PushButton*) GetWindow();
285 	if ( pButton )
286 	{
287 		sal_Int32 nValue = 0;
288 		OSL_VERIFY( aNumber >>= nValue );
289 
290 		if ( nValue < 0 )
291 			nValue = 0;
292 		else if ( nValue > 1 )
293 			nValue = 1;
294 
295 		pButton->SetPressed( (sal_Bool) nValue );
296 		bReturn = sal_True;
297 	}
298 
299 	return bReturn;
300 }
301 
302 // -----------------------------------------------------------------------------
303 
304 Any VCLXAccessibleButton::getMaximumValue(  ) throw (RuntimeException)
305 {
306 	OExternalLockGuard aGuard( this );
307 
308 	Any aValue;
309 	aValue <<= (sal_Int32) 1;
310 
311 	return aValue;
312 }
313 
314 // -----------------------------------------------------------------------------
315 
316 Any VCLXAccessibleButton::getMinimumValue(  ) throw (RuntimeException)
317 {
318 	OExternalLockGuard aGuard( this );
319 
320 	Any aValue;
321 	aValue <<= (sal_Int32) 0;
322 
323 	return aValue;
324 }
325 
326 // -----------------------------------------------------------------------------
327