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 <standard/vclxaccessiblebutton.hxx>
21 #include <helper/accresmgr.hxx>
22 #include <strings.hrc>
24 #include <comphelper/accessiblecontexthelper.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 <com/sun/star/lang/IndexOutOfBoundsException.hpp>
31 #include <vcl/accessibility/strings.hxx>
32 #include <vcl/toolkit/button.hxx>
33 #include <vcl/event.hxx>
34 #include <vcl/vclevent.hxx>
36 using namespace ::com::sun::star
;
37 using namespace ::com::sun::star::uno
;
38 using namespace ::com::sun::star::lang
;
39 using namespace ::com::sun::star::accessibility
;
40 using namespace ::comphelper
;
43 // VCLXAccessibleButton
46 void VCLXAccessibleButton::ProcessWindowEvent( const VclWindowEvent
& rVclWindowEvent
)
48 switch ( rVclWindowEvent
.GetId() )
50 case VclEventId::PushbuttonToggle
:
55 VclPtr
< PushButton
> pButton
= GetAs
< PushButton
>();
56 if ( pButton
&& pButton
->GetState() == TRISTATE_TRUE
)
57 aNewValue
<<= AccessibleStateType::CHECKED
;
59 aOldValue
<<= AccessibleStateType::CHECKED
;
61 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED
, aOldValue
, aNewValue
);
65 VCLXAccessibleTextComponent::ProcessWindowEvent( rVclWindowEvent
);
70 void VCLXAccessibleButton::FillAccessibleStateSet( sal_Int64
& rStateSet
)
72 VCLXAccessibleTextComponent::FillAccessibleStateSet( rStateSet
);
74 VclPtr
< PushButton
> pButton
= GetAs
< PushButton
>();
78 rStateSet
|= AccessibleStateType::FOCUSABLE
;
80 if (pButton
->isToggleButton())
81 rStateSet
|= AccessibleStateType::CHECKABLE
;
83 if ( pButton
->GetState() == TRISTATE_TRUE
)
84 rStateSet
|= AccessibleStateType::CHECKED
;
86 if ( pButton
->IsPressed() )
87 rStateSet
|= AccessibleStateType::PRESSED
;
89 // IA2 CWS: if the button has a popup menu, it should has the state EXPANDABLE
90 if( pButton
->GetType() == WindowType::MENUBUTTON
)
92 rStateSet
|= AccessibleStateType::EXPANDABLE
;
94 if( pButton
->GetStyle() & WB_DEFBUTTON
)
96 rStateSet
|= AccessibleStateType::DEFAULT
;
104 OUString
VCLXAccessibleButton::getImplementationName()
106 return u
"com.sun.star.comp.toolkit.AccessibleButton"_ustr
;
110 Sequence
< OUString
> VCLXAccessibleButton::getSupportedServiceNames()
112 return { u
"com.sun.star.awt.AccessibleButton"_ustr
};
116 // XAccessibleContext
119 OUString
VCLXAccessibleButton::getAccessibleName( )
121 OUString
aName( VCLXAccessibleTextComponent::getAccessibleName() );
122 sal_Int32 nLength
= aName
.getLength();
124 if ( nLength
>= 3 && aName
.match( "...", nLength
- 3 ) )
128 // it's a browse button
129 aName
= AccResId( RID_STR_ACC_NAME_BROWSEBUTTON
);
133 // remove the three trailing dots
134 aName
= aName
.copy( 0, nLength
- 3 );
137 else if ( nLength
>= 3 && aName
.match( "<< ", 0 ) )
139 // remove the leading symbols
140 aName
= aName
.copy( 3, nLength
- 3 );
142 else if ( nLength
>= 3 && aName
.match( " >>", nLength
- 3 ) )
144 // remove the trailing symbols
145 aName
= aName
.copy( 0, nLength
- 3 );
155 sal_Int32
VCLXAccessibleButton::getAccessibleActionCount( )
157 OExternalLockGuard
aGuard( this );
163 sal_Bool
VCLXAccessibleButton::doAccessibleAction ( sal_Int32 nIndex
)
165 OExternalLockGuard
aGuard( this );
168 throw IndexOutOfBoundsException();
170 VclPtr
< PushButton
> pButton
= GetAs
< PushButton
>();
173 if (pButton
->isToggleButton())
175 // PushButton::Click doesn't toggle when it's a toggle button
176 pButton
->Check(!pButton
->IsChecked());
189 OUString
VCLXAccessibleButton::getAccessibleActionDescription ( sal_Int32 nIndex
)
191 OExternalLockGuard
aGuard( this );
194 throw IndexOutOfBoundsException();
196 return RID_STR_ACC_ACTION_CLICK
;
200 Reference
< XAccessibleKeyBinding
> VCLXAccessibleButton::getAccessibleActionKeyBinding( sal_Int32 nIndex
)
202 OExternalLockGuard
aGuard( this );
205 throw IndexOutOfBoundsException();
207 rtl::Reference
<OAccessibleKeyBindingHelper
> pKeyBindingHelper
= new OAccessibleKeyBindingHelper();
209 VclPtr
<vcl::Window
> pWindow
= GetWindow();
212 KeyEvent aKeyEvent
= pWindow
->GetActivationKey();
213 vcl::KeyCode aKeyCode
= aKeyEvent
.GetKeyCode();
214 if ( aKeyCode
.GetCode() != 0 )
216 awt::KeyStroke aKeyStroke
;
217 aKeyStroke
.Modifiers
= 0;
218 if ( aKeyCode
.IsShift() )
219 aKeyStroke
.Modifiers
|= awt::KeyModifier::SHIFT
;
220 if ( aKeyCode
.IsMod1() )
221 aKeyStroke
.Modifiers
|= awt::KeyModifier::MOD1
;
222 if ( aKeyCode
.IsMod2() )
223 aKeyStroke
.Modifiers
|= awt::KeyModifier::MOD2
;
224 if ( aKeyCode
.IsMod3() )
225 aKeyStroke
.Modifiers
|= awt::KeyModifier::MOD3
;
226 aKeyStroke
.KeyCode
= aKeyCode
.GetCode();
227 aKeyStroke
.KeyChar
= aKeyEvent
.GetCharCode();
228 aKeyStroke
.KeyFunc
= static_cast< sal_Int16
>( aKeyCode
.GetFunction() );
229 pKeyBindingHelper
->AddKeyBinding( aKeyStroke
);
233 return pKeyBindingHelper
;
240 Any
VCLXAccessibleButton::getCurrentValue( )
242 OExternalLockGuard
aGuard( this );
246 VclPtr
< PushButton
> pButton
= GetAs
< PushButton
>();
248 aValue
<<= static_cast<sal_Int32
>(pButton
->IsPressed());
254 sal_Bool
VCLXAccessibleButton::setCurrentValue( const Any
& aNumber
)
256 OExternalLockGuard
aGuard( this );
258 bool bReturn
= false;
260 VclPtr
< PushButton
> pButton
= GetAs
< PushButton
>();
263 sal_Int32 nValue
= 0;
264 OSL_VERIFY( aNumber
>>= nValue
);
268 else if ( nValue
> 1 )
271 pButton
->SetPressed( nValue
== 1 );
279 Any
VCLXAccessibleButton::getMaximumValue( )
281 OExternalLockGuard
aGuard( this );
284 aValue
<<= sal_Int32(1);
290 Any
VCLXAccessibleButton::getMinimumValue( )
292 OExternalLockGuard
aGuard( this );
295 aValue
<<= sal_Int32(0);
300 Any
VCLXAccessibleButton::getMinimumIncrement( )
302 OExternalLockGuard
aGuard( this );
305 aValue
<<= sal_Int32(1);
311 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */