1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/vclxaccessiblebutton.hxx>
21 #include <accessibility/helper/accresmgr.hxx>
22 #include <accessibility/helper/accessiblestrings.hrc>
24 #include <unotools/accessiblestatesethelper.hxx>
25 #include <comphelper/accessiblekeybindinghelper.hxx>
26 #include <com/sun/star/awt/KeyModifier.hpp>
27 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
28 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
29 #include <cppuhelper/typeprovider.hxx>
30 #include <comphelper/sequence.hxx>
32 #include <vcl/button.hxx>
34 using namespace ::com::sun::star
;
35 using namespace ::com::sun::star::uno
;
36 using namespace ::com::sun::star::lang
;
37 using namespace ::com::sun::star::beans
;
38 using namespace ::com::sun::star::accessibility
;
39 using namespace ::comphelper
;
42 // -----------------------------------------------------------------------------
43 // VCLXAccessibleButton
44 // -----------------------------------------------------------------------------
46 VCLXAccessibleButton::VCLXAccessibleButton( VCLXWindow
* pVCLWindow
)
47 :VCLXAccessibleTextComponent( pVCLWindow
)
51 // -----------------------------------------------------------------------------
53 VCLXAccessibleButton::~VCLXAccessibleButton()
57 // -----------------------------------------------------------------------------
59 void VCLXAccessibleButton::ProcessWindowEvent( const VclWindowEvent
& rVclWindowEvent
)
61 switch ( rVclWindowEvent
.GetId() )
63 case VCLEVENT_PUSHBUTTON_TOGGLE
:
68 PushButton
* pButton
= (PushButton
*) GetWindow();
69 if ( pButton
&& pButton
->GetState() == STATE_CHECK
)
70 aNewValue
<<= AccessibleStateType::CHECKED
;
72 aOldValue
<<= AccessibleStateType::CHECKED
;
74 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED
, aOldValue
, aNewValue
);
78 VCLXAccessibleTextComponent::ProcessWindowEvent( rVclWindowEvent
);
82 // -----------------------------------------------------------------------------
84 void VCLXAccessibleButton::FillAccessibleStateSet( utl::AccessibleStateSetHelper
& rStateSet
)
86 VCLXAccessibleTextComponent::FillAccessibleStateSet( rStateSet
);
88 PushButton
* pButton
= (PushButton
*) GetWindow();
91 rStateSet
.AddState( AccessibleStateType::FOCUSABLE
);
93 if ( pButton
->GetState() == STATE_CHECK
)
94 rStateSet
.AddState( AccessibleStateType::CHECKED
);
96 if ( pButton
->IsPressed() )
97 rStateSet
.AddState( AccessibleStateType::PRESSED
);
101 // -----------------------------------------------------------------------------
103 // -----------------------------------------------------------------------------
105 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleButton
, VCLXAccessibleTextComponent
, VCLXAccessibleButton_BASE
)
107 // -----------------------------------------------------------------------------
109 // -----------------------------------------------------------------------------
111 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleButton
, VCLXAccessibleTextComponent
, VCLXAccessibleButton_BASE
)
113 // -----------------------------------------------------------------------------
115 // -----------------------------------------------------------------------------
117 OUString
VCLXAccessibleButton::getImplementationName() throw (RuntimeException
)
119 return OUString( "com.sun.star.comp.toolkit.AccessibleButton" );
122 // -----------------------------------------------------------------------------
124 Sequence
< OUString
> VCLXAccessibleButton::getSupportedServiceNames() throw (RuntimeException
)
126 Sequence
< OUString
> aNames(1);
127 aNames
[0] = "com.sun.star.awt.AccessibleButton";
131 // -----------------------------------------------------------------------------
132 // XAccessibleContext
133 // -----------------------------------------------------------------------------
135 OUString
VCLXAccessibleButton::getAccessibleName( ) throw (RuntimeException
)
137 OExternalLockGuard
aGuard( this );
139 OUString
aName( VCLXAccessibleTextComponent::getAccessibleName() );
140 sal_Int32 nLength
= aName
.getLength();
142 if ( nLength
>= 3 && aName
.matchAsciiL( RTL_CONSTASCII_STRINGPARAM("..."), nLength
- 3 ) )
146 // it's a browse button
147 aName
= OUString( TK_RES_STRING( RID_STR_ACC_NAME_BROWSEBUTTON
) );
151 // remove the three trailing dots
152 aName
= aName
.copy( 0, nLength
- 3 );
155 else if ( nLength
>= 3 && aName
.matchAsciiL( RTL_CONSTASCII_STRINGPARAM("<< "), 0 ) )
157 // remove the leading symbols
158 aName
= aName
.copy( 3, nLength
- 3 );
160 else if ( nLength
>= 3 && aName
.matchAsciiL( RTL_CONSTASCII_STRINGPARAM(" >>"), nLength
- 3 ) )
162 // remove the trailing symbols
163 aName
= aName
.copy( 0, nLength
- 3 );
169 // -----------------------------------------------------------------------------
171 // -----------------------------------------------------------------------------
173 sal_Int32
VCLXAccessibleButton::getAccessibleActionCount( ) throw (RuntimeException
)
175 OExternalLockGuard
aGuard( this );
180 // -----------------------------------------------------------------------------
182 sal_Bool
VCLXAccessibleButton::doAccessibleAction ( sal_Int32 nIndex
) throw (IndexOutOfBoundsException
, RuntimeException
)
184 OExternalLockGuard
aGuard( this );
186 if ( nIndex
< 0 || nIndex
>= getAccessibleActionCount() )
187 throw IndexOutOfBoundsException();
189 PushButton
* pButton
= (PushButton
*) GetWindow();
196 // -----------------------------------------------------------------------------
198 OUString
VCLXAccessibleButton::getAccessibleActionDescription ( sal_Int32 nIndex
) throw (IndexOutOfBoundsException
, RuntimeException
)
200 OExternalLockGuard
aGuard( this );
202 if ( nIndex
< 0 || nIndex
>= getAccessibleActionCount() )
203 throw IndexOutOfBoundsException();
205 return OUString( TK_RES_STRING( RID_STR_ACC_ACTION_CLICK
) );
208 // -----------------------------------------------------------------------------
210 Reference
< XAccessibleKeyBinding
> VCLXAccessibleButton::getAccessibleActionKeyBinding( sal_Int32 nIndex
) throw (IndexOutOfBoundsException
, RuntimeException
)
212 OExternalLockGuard
aGuard( this );
214 if ( nIndex
< 0 || nIndex
>= getAccessibleActionCount() )
215 throw IndexOutOfBoundsException();
217 OAccessibleKeyBindingHelper
* pKeyBindingHelper
= new OAccessibleKeyBindingHelper();
218 Reference
< XAccessibleKeyBinding
> xKeyBinding
= pKeyBindingHelper
;
220 Window
* pWindow
= GetWindow();
223 KeyEvent aKeyEvent
= pWindow
->GetActivationKey();
224 KeyCode aKeyCode
= aKeyEvent
.GetKeyCode();
225 if ( aKeyCode
.GetCode() != 0 )
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
);
247 // -----------------------------------------------------------------------------
249 // -----------------------------------------------------------------------------
251 Any
VCLXAccessibleButton::getCurrentValue( ) throw (RuntimeException
)
253 OExternalLockGuard
aGuard( this );
257 PushButton
* pButton
= (PushButton
*) GetWindow();
259 aValue
<<= (sal_Int32
) pButton
->IsPressed();
264 // -----------------------------------------------------------------------------
266 sal_Bool
VCLXAccessibleButton::setCurrentValue( const Any
& aNumber
) throw (RuntimeException
)
268 OExternalLockGuard
aGuard( this );
270 sal_Bool bReturn
= sal_False
;
272 PushButton
* pButton
= (PushButton
*) GetWindow();
275 sal_Int32 nValue
= 0;
276 OSL_VERIFY( aNumber
>>= nValue
);
280 else if ( nValue
> 1 )
283 pButton
->SetPressed( (sal_Bool
) nValue
);
290 // -----------------------------------------------------------------------------
292 Any
VCLXAccessibleButton::getMaximumValue( ) throw (RuntimeException
)
294 OExternalLockGuard
aGuard( this );
297 aValue
<<= (sal_Int32
) 1;
302 // -----------------------------------------------------------------------------
304 Any
VCLXAccessibleButton::getMinimumValue( ) throw (RuntimeException
)
306 OExternalLockGuard
aGuard( this );
309 aValue
<<= (sal_Int32
) 0;
314 // -----------------------------------------------------------------------------
316 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */