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