1c82f2877SAndrew Rist /**************************************************************
2*15466770Smseidel  *
3c82f2877SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4c82f2877SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5c82f2877SAndrew Rist  * distributed with this work for additional information
6c82f2877SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7c82f2877SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8c82f2877SAndrew Rist  * "License"); you may not use this file except in compliance
9c82f2877SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*15466770Smseidel  *
11c82f2877SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*15466770Smseidel  *
13c82f2877SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14c82f2877SAndrew Rist  * software distributed under the License is distributed on an
15c82f2877SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16c82f2877SAndrew Rist  * KIND, either express or implied.  See the License for the
17c82f2877SAndrew Rist  * specific language governing permissions and limitations
18c82f2877SAndrew Rist  * under the License.
19*15466770Smseidel  *
20c82f2877SAndrew Rist  *************************************************************/
21c82f2877SAndrew Rist 
22c82f2877SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_accessibility.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir // includes --------------------------------------------------------------
28cdf0e10cSrcweir #include <accessibility/standard/vclxaccessiblebutton.hxx>
29cdf0e10cSrcweir #include <accessibility/helper/accresmgr.hxx>
30cdf0e10cSrcweir #include <accessibility/helper/accessiblestrings.hrc>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <unotools/accessiblestatesethelper.hxx>
33cdf0e10cSrcweir #include <comphelper/accessiblekeybindinghelper.hxx>
34cdf0e10cSrcweir #include <com/sun/star/awt/KeyModifier.hpp>
35cdf0e10cSrcweir #include <com/sun/star/accessibility/AccessibleStateType.hpp>
36cdf0e10cSrcweir #include <com/sun/star/accessibility/AccessibleEventId.hpp>
37cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>
38cdf0e10cSrcweir #include <comphelper/sequence.hxx>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include <vcl/button.hxx>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir using namespace ::com::sun::star;
43cdf0e10cSrcweir using namespace ::com::sun::star::uno;
44cdf0e10cSrcweir using namespace ::com::sun::star::lang;
45cdf0e10cSrcweir using namespace ::com::sun::star::beans;
46cdf0e10cSrcweir using namespace ::com::sun::star::accessibility;
47cdf0e10cSrcweir using namespace ::comphelper;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 
50cdf0e10cSrcweir // -----------------------------------------------------------------------------
51cdf0e10cSrcweir // VCLXAccessibleButton
52cdf0e10cSrcweir // -----------------------------------------------------------------------------
53cdf0e10cSrcweir 
VCLXAccessibleButton(VCLXWindow * pVCLWindow)54cdf0e10cSrcweir VCLXAccessibleButton::VCLXAccessibleButton( VCLXWindow* pVCLWindow )
55cdf0e10cSrcweir 	:VCLXAccessibleTextComponent( pVCLWindow )
56cdf0e10cSrcweir {
57cdf0e10cSrcweir }
58cdf0e10cSrcweir 
59cdf0e10cSrcweir // -----------------------------------------------------------------------------
60cdf0e10cSrcweir 
~VCLXAccessibleButton()61cdf0e10cSrcweir VCLXAccessibleButton::~VCLXAccessibleButton()
62cdf0e10cSrcweir {
63cdf0e10cSrcweir }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir // -----------------------------------------------------------------------------
66cdf0e10cSrcweir 
ProcessWindowEvent(const VclWindowEvent & rVclWindowEvent)67cdf0e10cSrcweir void VCLXAccessibleButton::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
68cdf0e10cSrcweir {
69*15466770Smseidel 	switch ( rVclWindowEvent.GetId() )
70*15466770Smseidel 	{
71cdf0e10cSrcweir 		case VCLEVENT_PUSHBUTTON_TOGGLE:
72*15466770Smseidel 		{
73cdf0e10cSrcweir 			Any aOldValue;
74cdf0e10cSrcweir 			Any aNewValue;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 			PushButton* pButton = (PushButton*) GetWindow();
77cdf0e10cSrcweir 			if ( pButton && pButton->GetState() == STATE_CHECK )
78cdf0e10cSrcweir 				aNewValue <<= AccessibleStateType::CHECKED;
79cdf0e10cSrcweir 			else
80cdf0e10cSrcweir 				aOldValue <<= AccessibleStateType::CHECKED;
81cdf0e10cSrcweir 
82*15466770Smseidel 			NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
83*15466770Smseidel 		}
84*15466770Smseidel 		break;
85cdf0e10cSrcweir 		default:
86cdf0e10cSrcweir 			VCLXAccessibleTextComponent::ProcessWindowEvent( rVclWindowEvent );
87*15466770Smseidel 	}
88cdf0e10cSrcweir }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir // -----------------------------------------------------------------------------
91cdf0e10cSrcweir 
FillAccessibleStateSet(utl::AccessibleStateSetHelper & rStateSet)92cdf0e10cSrcweir void VCLXAccessibleButton::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
93cdf0e10cSrcweir {
94cdf0e10cSrcweir 	VCLXAccessibleTextComponent::FillAccessibleStateSet( rStateSet );
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 	PushButton* pButton = (PushButton*) GetWindow();
97cdf0e10cSrcweir 	if ( pButton )
98cdf0e10cSrcweir 	{
99*15466770Smseidel 		rStateSet.AddState( AccessibleStateType::FOCUSABLE );
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 		if ( pButton->GetState() == STATE_CHECK )
102*15466770Smseidel 			rStateSet.AddState( AccessibleStateType::CHECKED );
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 		if ( pButton->IsPressed() )
105*15466770Smseidel 			rStateSet.AddState( AccessibleStateType::PRESSED );
10621075d77SSteve Yin 
107*15466770Smseidel 		// IA2 CWS: If the button has a poppup menu,it should has the state EXPANDABLE
108*15466770Smseidel 		if( pButton->GetType() == WINDOW_MENUBUTTON )
109*15466770Smseidel 		{
110*15466770Smseidel 			rStateSet.AddState( AccessibleStateType::EXPANDABLE );
111*15466770Smseidel 		}
11221075d77SSteve Yin 		if( pButton->GetStyle() & WB_DEFBUTTON )
11321075d77SSteve Yin 		{
114*15466770Smseidel 			rStateSet.AddState( AccessibleStateType::DEFAULT );
11521075d77SSteve Yin 		}
116cdf0e10cSrcweir 	}
117cdf0e10cSrcweir }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir // -----------------------------------------------------------------------------
120cdf0e10cSrcweir // XInterface
121cdf0e10cSrcweir // -----------------------------------------------------------------------------
122cdf0e10cSrcweir 
IMPLEMENT_FORWARD_XINTERFACE2(VCLXAccessibleButton,VCLXAccessibleTextComponent,VCLXAccessibleButton_BASE)123cdf0e10cSrcweir IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleButton, VCLXAccessibleTextComponent, VCLXAccessibleButton_BASE )
124cdf0e10cSrcweir 
125cdf0e10cSrcweir // -----------------------------------------------------------------------------
126cdf0e10cSrcweir // XTypeProvider
127cdf0e10cSrcweir // -----------------------------------------------------------------------------
128cdf0e10cSrcweir 
129cdf0e10cSrcweir IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleButton, VCLXAccessibleTextComponent, VCLXAccessibleButton_BASE )
130cdf0e10cSrcweir 
131cdf0e10cSrcweir // -----------------------------------------------------------------------------
132cdf0e10cSrcweir // XServiceInfo
133cdf0e10cSrcweir // -----------------------------------------------------------------------------
134cdf0e10cSrcweir 
135cdf0e10cSrcweir ::rtl::OUString VCLXAccessibleButton::getImplementationName() throw (RuntimeException)
136cdf0e10cSrcweir {
137cdf0e10cSrcweir 	return ::rtl::OUString::createFromAscii( "com.sun.star.comp.toolkit.AccessibleButton" );
138cdf0e10cSrcweir }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir // -----------------------------------------------------------------------------
141cdf0e10cSrcweir 
getSupportedServiceNames()142cdf0e10cSrcweir Sequence< ::rtl::OUString > VCLXAccessibleButton::getSupportedServiceNames() throw (RuntimeException)
143cdf0e10cSrcweir {
144cdf0e10cSrcweir 	Sequence< ::rtl::OUString > aNames(1);
145cdf0e10cSrcweir 	aNames[0] = ::rtl::OUString::createFromAscii( "com.sun.star.awt.AccessibleButton" );
146cdf0e10cSrcweir 	return aNames;
147cdf0e10cSrcweir }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir // -----------------------------------------------------------------------------
150cdf0e10cSrcweir // XAccessibleContext
151cdf0e10cSrcweir // -----------------------------------------------------------------------------
152cdf0e10cSrcweir 
getAccessibleName()153cdf0e10cSrcweir ::rtl::OUString VCLXAccessibleButton::getAccessibleName(  ) throw (RuntimeException)
154cdf0e10cSrcweir {
155cdf0e10cSrcweir 	OExternalLockGuard aGuard( this );
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 	::rtl::OUString aName( VCLXAccessibleTextComponent::getAccessibleName() );
15821075d77SSteve Yin 
159*15466770Smseidel 	// IA2 CWS: Removed special handling for browse/more buttons.
160*15466770Smseidel 	//          Comment was "the '...' or '<<' or '>>' should be kept per the requirements from AT"
161*15466770Smseidel 	// MT: We did introduce this special handling by intention.
16221075d77SSteve Yin 	//     As the original text is still what you get via XAccessibleText,
16321075d77SSteve Yin 	//     I think for the accessible name the stuff below is correct.
16421075d77SSteve Yin 
165cdf0e10cSrcweir 	sal_Int32 nLength = aName.getLength();
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 	if ( nLength >= 3 && aName.matchAsciiL( RTL_CONSTASCII_STRINGPARAM("..."), nLength - 3 ) )
168cdf0e10cSrcweir 	{
169cdf0e10cSrcweir 		if ( nLength == 3 )
170cdf0e10cSrcweir 		{
171cdf0e10cSrcweir 			// it's a browse button
172*15466770Smseidel 			aName = ::rtl::OUString( TK_RES_STRING( RID_STR_ACC_NAME_BROWSEBUTTON ) );
173cdf0e10cSrcweir 		}
174cdf0e10cSrcweir 		else
175cdf0e10cSrcweir 		{
176cdf0e10cSrcweir 			// remove the three trailing dots
177cdf0e10cSrcweir 			aName = aName.copy( 0, nLength - 3 );
178cdf0e10cSrcweir 		}
179cdf0e10cSrcweir 	}
180cdf0e10cSrcweir 	else if ( nLength >= 3 && aName.matchAsciiL( RTL_CONSTASCII_STRINGPARAM("<< "), 0 ) )
181cdf0e10cSrcweir 	{
182cdf0e10cSrcweir 		// remove the leading symbols
183cdf0e10cSrcweir 		aName = aName.copy( 3, nLength - 3 );
184cdf0e10cSrcweir 	}
185cdf0e10cSrcweir 	else if ( nLength >= 3 && aName.matchAsciiL( RTL_CONSTASCII_STRINGPARAM(" >>"), nLength - 3 ) )
186cdf0e10cSrcweir 	{
187cdf0e10cSrcweir 		// remove the trailing symbols
188cdf0e10cSrcweir 		aName = aName.copy( 0, nLength - 3 );
189cdf0e10cSrcweir 	}
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 	return aName;
192cdf0e10cSrcweir }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir // -----------------------------------------------------------------------------
195cdf0e10cSrcweir // XAccessibleAction
196cdf0e10cSrcweir // -----------------------------------------------------------------------------
197cdf0e10cSrcweir 
getAccessibleActionCount()198cdf0e10cSrcweir sal_Int32 VCLXAccessibleButton::getAccessibleActionCount( ) throw (RuntimeException)
199cdf0e10cSrcweir {
200cdf0e10cSrcweir 	OExternalLockGuard aGuard( this );
201cdf0e10cSrcweir 
202cdf0e10cSrcweir 	return 1;
203cdf0e10cSrcweir }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir // -----------------------------------------------------------------------------
206cdf0e10cSrcweir 
doAccessibleAction(sal_Int32 nIndex)207cdf0e10cSrcweir sal_Bool VCLXAccessibleButton::doAccessibleAction ( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
208cdf0e10cSrcweir {
209cdf0e10cSrcweir 	OExternalLockGuard aGuard( this );
210cdf0e10cSrcweir 
211cdf0e10cSrcweir 	if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
212*15466770Smseidel 		throw IndexOutOfBoundsException();
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 	PushButton* pButton = (PushButton*) GetWindow();
215cdf0e10cSrcweir 	if ( pButton )
216cdf0e10cSrcweir 		pButton->Click();
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 	return sal_True;
219cdf0e10cSrcweir }
220cdf0e10cSrcweir 
221cdf0e10cSrcweir // -----------------------------------------------------------------------------
222cdf0e10cSrcweir 
getAccessibleActionDescription(sal_Int32 nIndex)223cdf0e10cSrcweir ::rtl::OUString VCLXAccessibleButton::getAccessibleActionDescription ( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
224cdf0e10cSrcweir {
225cdf0e10cSrcweir 	OExternalLockGuard aGuard( this );
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 	if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
228*15466770Smseidel 		throw IndexOutOfBoundsException();
229cdf0e10cSrcweir 
230cdf0e10cSrcweir 	return ::rtl::OUString( TK_RES_STRING( RID_STR_ACC_ACTION_CLICK ) );
231cdf0e10cSrcweir }
232cdf0e10cSrcweir 
233cdf0e10cSrcweir // -----------------------------------------------------------------------------
234cdf0e10cSrcweir 
getAccessibleActionKeyBinding(sal_Int32 nIndex)235cdf0e10cSrcweir Reference< XAccessibleKeyBinding > VCLXAccessibleButton::getAccessibleActionKeyBinding( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
236cdf0e10cSrcweir {
237*15466770Smseidel 	OExternalLockGuard aGuard( this );
238*15466770Smseidel 
239*15466770Smseidel 	if ( nIndex < 0 || nIndex >= getAccessibleActionCount() )
240*15466770Smseidel 		throw IndexOutOfBoundsException();
241*15466770Smseidel 
242*15466770Smseidel 	OAccessibleKeyBindingHelper* pKeyBindingHelper = new OAccessibleKeyBindingHelper();
243*15466770Smseidel 	Reference< XAccessibleKeyBinding > xKeyBinding = pKeyBindingHelper;
244*15466770Smseidel 
245*15466770Smseidel 	Window* pWindow = GetWindow();
246*15466770Smseidel 	if ( pWindow )
247*15466770Smseidel 	{
248*15466770Smseidel 		KeyEvent aKeyEvent = pWindow->GetActivationKey();
249*15466770Smseidel 		KeyCode aKeyCode = aKeyEvent.GetKeyCode();
250*15466770Smseidel 		if ( aKeyCode.GetCode() != 0 )
251*15466770Smseidel 		{
252*15466770Smseidel 			awt::KeyStroke aKeyStroke;
253*15466770Smseidel 			aKeyStroke.Modifiers = 0;
254*15466770Smseidel 			if ( aKeyCode.IsShift() )
255*15466770Smseidel 				aKeyStroke.Modifiers |= awt::KeyModifier::SHIFT;
256*15466770Smseidel 			if ( aKeyCode.IsMod1() )
257*15466770Smseidel 				aKeyStroke.Modifiers |= awt::KeyModifier::MOD1;
258*15466770Smseidel 			if ( aKeyCode.IsMod2() )
259*15466770Smseidel 				aKeyStroke.Modifiers |= awt::KeyModifier::MOD2;
260*15466770Smseidel 			if ( aKeyCode.IsMod3() )
261*15466770Smseidel 				aKeyStroke.Modifiers |= awt::KeyModifier::MOD3;
262*15466770Smseidel 			aKeyStroke.KeyCode = aKeyCode.GetCode();
263*15466770Smseidel 			aKeyStroke.KeyChar = aKeyEvent.GetCharCode();
264*15466770Smseidel 			aKeyStroke.KeyFunc = static_cast< sal_Int16 >( aKeyCode.GetFunction() );
265*15466770Smseidel 			pKeyBindingHelper->AddKeyBinding( aKeyStroke );
266*15466770Smseidel 		}
267*15466770Smseidel 	}
268*15466770Smseidel 
269*15466770Smseidel 	return xKeyBinding;
270cdf0e10cSrcweir }
271cdf0e10cSrcweir 
272cdf0e10cSrcweir // -----------------------------------------------------------------------------
273cdf0e10cSrcweir // XAccessibleValue
274cdf0e10cSrcweir // -----------------------------------------------------------------------------
275cdf0e10cSrcweir 
getCurrentValue()276cdf0e10cSrcweir Any VCLXAccessibleButton::getCurrentValue(  ) throw (RuntimeException)
277cdf0e10cSrcweir {
278cdf0e10cSrcweir 	OExternalLockGuard aGuard( this );
279cdf0e10cSrcweir 
280cdf0e10cSrcweir 	Any aValue;
281cdf0e10cSrcweir 
282cdf0e10cSrcweir 	PushButton* pButton = (PushButton*) GetWindow();
283cdf0e10cSrcweir 	if ( pButton )
284cdf0e10cSrcweir 		aValue <<= (sal_Int32) pButton->IsPressed();
285cdf0e10cSrcweir 
286cdf0e10cSrcweir 	return aValue;
287cdf0e10cSrcweir }
288cdf0e10cSrcweir 
289cdf0e10cSrcweir // -----------------------------------------------------------------------------
290cdf0e10cSrcweir 
setCurrentValue(const Any & aNumber)291cdf0e10cSrcweir sal_Bool VCLXAccessibleButton::setCurrentValue( const Any& aNumber ) throw (RuntimeException)
292cdf0e10cSrcweir {
293cdf0e10cSrcweir 	OExternalLockGuard aGuard( this );
294cdf0e10cSrcweir 
295cdf0e10cSrcweir 	sal_Bool bReturn = sal_False;
296cdf0e10cSrcweir 
297cdf0e10cSrcweir 	PushButton* pButton = (PushButton*) GetWindow();
298cdf0e10cSrcweir 	if ( pButton )
299cdf0e10cSrcweir 	{
300cdf0e10cSrcweir 		sal_Int32 nValue = 0;
301cdf0e10cSrcweir 		OSL_VERIFY( aNumber >>= nValue );
302cdf0e10cSrcweir 
303cdf0e10cSrcweir 		if ( nValue < 0 )
304cdf0e10cSrcweir 			nValue = 0;
305cdf0e10cSrcweir 		else if ( nValue > 1 )
306cdf0e10cSrcweir 			nValue = 1;
307cdf0e10cSrcweir 
308cdf0e10cSrcweir 		pButton->SetPressed( (sal_Bool) nValue );
309cdf0e10cSrcweir 		bReturn = sal_True;
310cdf0e10cSrcweir 	}
311*15466770Smseidel 
312cdf0e10cSrcweir 	return bReturn;
313cdf0e10cSrcweir }
314cdf0e10cSrcweir 
315cdf0e10cSrcweir // -----------------------------------------------------------------------------
316cdf0e10cSrcweir 
getMaximumValue()317cdf0e10cSrcweir Any VCLXAccessibleButton::getMaximumValue(  ) throw (RuntimeException)
318cdf0e10cSrcweir {
319cdf0e10cSrcweir 	OExternalLockGuard aGuard( this );
320cdf0e10cSrcweir 
321cdf0e10cSrcweir 	Any aValue;
322cdf0e10cSrcweir 	aValue <<= (sal_Int32) 1;
323*15466770Smseidel 
324cdf0e10cSrcweir 	return aValue;
325cdf0e10cSrcweir }
326cdf0e10cSrcweir 
327cdf0e10cSrcweir // -----------------------------------------------------------------------------
328cdf0e10cSrcweir 
getMinimumValue()329cdf0e10cSrcweir Any VCLXAccessibleButton::getMinimumValue(  ) throw (RuntimeException)
330cdf0e10cSrcweir {
331cdf0e10cSrcweir 	OExternalLockGuard aGuard( this );
332cdf0e10cSrcweir 
333cdf0e10cSrcweir 	Any aValue;
334cdf0e10cSrcweir 	aValue <<= (sal_Int32) 0;
335*15466770Smseidel 
336cdf0e10cSrcweir 	return aValue;
337cdf0e10cSrcweir }
338cdf0e10cSrcweir 
339cdf0e10cSrcweir // -----------------------------------------------------------------------------
340