Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/vcl/source/accessibility/vclxaccessiblebutton.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
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/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
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 .
18
 */
19
20
#include <accessibility/vclxaccessiblebutton.hxx>
21
#include <svdata.hxx>
22
#include <strings.hrc>
23
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>
30
31
#include <vcl/accessibility/strings.hxx>
32
#include <vcl/toolkit/button.hxx>
33
#include <vcl/event.hxx>
34
#include <vcl/vclevent.hxx>
35
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;
41
42
43
// VCLXAccessibleButton
44
45
46
void VCLXAccessibleButton::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
47
0
{
48
0
    switch ( rVclWindowEvent.GetId() )
49
0
    {
50
0
        case VclEventId::PushbuttonToggle:
51
0
        {
52
0
            Any aOldValue;
53
0
            Any aNewValue;
54
55
0
            VclPtr< PushButton > pButton = GetAs< PushButton >();
56
0
            if ( pButton && pButton->GetState() == TRISTATE_TRUE )
57
0
                aNewValue <<= AccessibleStateType::CHECKED;
58
0
            else
59
0
                aOldValue <<= AccessibleStateType::CHECKED;
60
61
0
            NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
62
0
        }
63
0
        break;
64
0
        default:
65
0
            VCLXAccessibleTextComponent::ProcessWindowEvent( rVclWindowEvent );
66
0
   }
67
0
}
68
69
70
void VCLXAccessibleButton::FillAccessibleStateSet( sal_Int64& rStateSet )
71
0
{
72
0
    VCLXAccessibleTextComponent::FillAccessibleStateSet( rStateSet );
73
74
0
    VclPtr< PushButton > pButton = GetAs< PushButton >();
75
0
    if ( !pButton )
76
0
        return;
77
78
0
    rStateSet |= AccessibleStateType::FOCUSABLE;
79
80
0
    if (pButton->isToggleButton())
81
0
        rStateSet |= AccessibleStateType::CHECKABLE;
82
83
0
    if ( pButton->GetState() == TRISTATE_TRUE )
84
0
        rStateSet |= AccessibleStateType::CHECKED;
85
86
0
    if ( pButton->IsPressed() )
87
0
        rStateSet |= AccessibleStateType::PRESSED;
88
89
    // IA2 CWS: if the button has a popup menu, it should has the state EXPANDABLE
90
0
    if( pButton->GetType() == WindowType::MENUBUTTON )
91
0
    {
92
0
        rStateSet |= AccessibleStateType::EXPANDABLE;
93
0
    }
94
0
    if( pButton->GetStyle() & WB_DEFBUTTON )
95
0
    {
96
0
        rStateSet |= AccessibleStateType::DEFAULT;
97
0
    }
98
0
}
99
100
101
// XServiceInfo
102
103
104
OUString VCLXAccessibleButton::getImplementationName()
105
0
{
106
0
    return u"com.sun.star.comp.toolkit.AccessibleButton"_ustr;
107
0
}
108
109
110
Sequence< OUString > VCLXAccessibleButton::getSupportedServiceNames()
111
0
{
112
0
    return { u"com.sun.star.awt.AccessibleButton"_ustr };
113
0
}
114
115
// XAccessibleContext
116
117
118
OUString VCLXAccessibleButton::getAccessibleName(  )
119
0
{
120
0
    OUString aName( VCLXAccessibleTextComponent::getAccessibleName() );
121
0
    sal_Int32 nLength = aName.getLength();
122
123
0
    if ( nLength >= 3 && aName.match( "...", nLength - 3 ) )
124
0
    {
125
0
        if ( nLength == 3 )
126
0
        {
127
            // it's a browse button
128
0
            aName = VclResId( RID_STR_ACC_NAME_BROWSEBUTTON );
129
0
        }
130
0
        else
131
0
        {
132
            // remove the three trailing dots
133
0
            aName = aName.copy( 0, nLength - 3 );
134
0
        }
135
0
    }
136
0
    else if ( nLength >= 3 && aName.match( "<< ", 0 ) )
137
0
    {
138
        // remove the leading symbols
139
0
        aName = aName.copy( 3, nLength - 3 );
140
0
    }
141
0
    else if ( nLength >= 3 && aName.match( " >>", nLength - 3 ) )
142
0
    {
143
        // remove the trailing symbols
144
0
        aName = aName.copy( 0, nLength - 3 );
145
0
    }
146
147
0
    return aName;
148
0
}
149
150
151
// XAccessibleAction
152
153
154
sal_Int32 VCLXAccessibleButton::getAccessibleActionCount( )
155
0
{
156
0
    OExternalLockGuard aGuard( this );
157
158
0
    return 1;
159
0
}
160
161
162
sal_Bool VCLXAccessibleButton::doAccessibleAction ( sal_Int32 nIndex )
163
0
{
164
0
    OExternalLockGuard aGuard( this );
165
166
0
    if ( nIndex != 0 )
167
0
        throw IndexOutOfBoundsException();
168
169
0
    VclPtr< PushButton > pButton = GetAs< PushButton >();
170
0
    if ( pButton )
171
0
    {
172
0
        if (pButton->isToggleButton())
173
0
        {
174
            // PushButton::Click doesn't toggle when it's a toggle button
175
0
            pButton->Check(!pButton->IsChecked());
176
0
            pButton->Toggle();
177
0
        }
178
0
        else
179
0
        {
180
0
            pButton->Click();
181
0
        }
182
0
    }
183
184
0
    return true;
185
0
}
186
187
188
OUString VCLXAccessibleButton::getAccessibleActionDescription ( sal_Int32 nIndex )
189
0
{
190
0
    OExternalLockGuard aGuard( this );
191
192
0
    if ( nIndex != 0 )
193
0
        throw IndexOutOfBoundsException();
194
195
0
    return RID_STR_ACC_ACTION_CLICK;
196
0
}
197
198
199
Reference< XAccessibleKeyBinding > VCLXAccessibleButton::getAccessibleActionKeyBinding( sal_Int32 nIndex )
200
0
{
201
0
    OExternalLockGuard aGuard( this );
202
203
0
    if ( nIndex != 0 )
204
0
        throw IndexOutOfBoundsException();
205
206
0
    rtl::Reference<OAccessibleKeyBindingHelper> pKeyBindingHelper = new OAccessibleKeyBindingHelper();
207
208
0
    VclPtr<vcl::Window> pWindow = GetWindow();
209
0
    if ( pWindow )
210
0
    {
211
0
        KeyEvent aKeyEvent = pWindow->GetActivationKey();
212
0
        vcl::KeyCode aKeyCode = aKeyEvent.GetKeyCode();
213
0
        if ( aKeyCode.GetCode() != 0 )
214
0
        {
215
0
            awt::KeyStroke aKeyStroke;
216
0
            aKeyStroke.Modifiers = 0;
217
0
            if ( aKeyCode.IsShift() )
218
0
                aKeyStroke.Modifiers |= awt::KeyModifier::SHIFT;
219
0
            if ( aKeyCode.IsMod1() )
220
0
                aKeyStroke.Modifiers |= awt::KeyModifier::MOD1;
221
0
            if ( aKeyCode.IsMod2() )
222
0
                aKeyStroke.Modifiers |= awt::KeyModifier::MOD2;
223
0
            if ( aKeyCode.IsMod3() )
224
0
                aKeyStroke.Modifiers |= awt::KeyModifier::MOD3;
225
0
            aKeyStroke.KeyCode = aKeyCode.GetCode();
226
0
            aKeyStroke.KeyChar = aKeyEvent.GetCharCode();
227
0
            aKeyStroke.KeyFunc = static_cast< sal_Int16 >( aKeyCode.GetFunction() );
228
0
            pKeyBindingHelper->AddKeyBinding( aKeyStroke );
229
0
        }
230
0
    }
231
232
0
    return pKeyBindingHelper;
233
0
}
234
235
236
// XAccessibleValue
237
238
239
Any VCLXAccessibleButton::getCurrentValue(  )
240
0
{
241
0
    OExternalLockGuard aGuard( this );
242
243
0
    Any aValue;
244
245
0
    VclPtr< PushButton > pButton = GetAs< PushButton >();
246
0
    if ( pButton )
247
0
        aValue <<= static_cast<sal_Int32>(pButton->IsPressed());
248
249
0
    return aValue;
250
0
}
251
252
253
sal_Bool VCLXAccessibleButton::setCurrentValue( const Any& aNumber )
254
0
{
255
0
    OExternalLockGuard aGuard( this );
256
257
0
    bool bReturn = false;
258
259
0
    VclPtr< PushButton > pButton = GetAs< PushButton >();
260
0
    if ( pButton )
261
0
    {
262
0
        sal_Int32 nValue = 0;
263
0
        OSL_VERIFY( aNumber >>= nValue );
264
265
0
        if ( nValue < 0 )
266
0
            nValue = 0;
267
0
        else if ( nValue > 1 )
268
0
            nValue = 1;
269
270
0
        pButton->SetPressed( nValue == 1 );
271
0
        bReturn = true;
272
0
    }
273
274
0
    return bReturn;
275
0
}
276
277
278
Any VCLXAccessibleButton::getMaximumValue(  )
279
0
{
280
0
    OExternalLockGuard aGuard( this );
281
282
0
    Any aValue;
283
0
    aValue <<= sal_Int32(1);
284
285
0
    return aValue;
286
0
}
287
288
289
Any VCLXAccessibleButton::getMinimumValue(  )
290
0
{
291
0
    OExternalLockGuard aGuard( this );
292
293
0
    Any aValue;
294
0
    aValue <<= sal_Int32(0);
295
296
0
    return aValue;
297
0
}
298
299
Any VCLXAccessibleButton::getMinimumIncrement(  )
300
0
{
301
0
    OExternalLockGuard aGuard( this );
302
303
0
    Any aValue;
304
0
    aValue <<= sal_Int32(1);
305
306
0
    return aValue;
307
0
}
308
309
310
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */