Coverage Report

Created: 2026-06-30 11:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/vcl/source/accessibility/vclxaccessiblebox.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/vclxaccessiblebox.hxx>
21
#include <accessibility/vclxaccessibletextfield.hxx>
22
#include <accessibility/vclxaccessibleedit.hxx>
23
#include <accessibility/vclxaccessiblelist.hxx>
24
25
#include <com/sun/star/accessibility/AccessibleStateType.hpp>
26
#include <com/sun/star/accessibility/AccessibleEventId.hpp>
27
#include <com/sun/star/accessibility/AccessibleRole.hpp>
28
#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
29
30
#include <vcl/accessibility/strings.hxx>
31
#include <vcl/vclevent.hxx>
32
#include <vcl/svapp.hxx>
33
#include <vcl/toolkit/combobox.hxx>
34
35
using namespace ::com::sun::star;
36
using namespace ::com::sun::star::uno;
37
using namespace ::com::sun::star::lang;
38
using namespace ::com::sun::star::beans;
39
using namespace ::com::sun::star::accessibility;
40
41
VCLXAccessibleBox::VCLXAccessibleBox(vcl::Window* pBox, BoxType aType, bool bIsDropDownBox)
42
0
    : ImplInheritanceHelper(pBox),
43
0
      m_aBoxType (aType),
44
0
      m_bIsDropDownBox (bIsDropDownBox)
45
0
{
46
    // Set up the flags that indicate which children this object has.
47
0
    m_bHasListChild = true;
48
49
    // A text field is not present for non drop down list boxes.
50
0
    if ((m_aBoxType==LISTBOX) && ! m_bIsDropDownBox)
51
0
        m_bHasTextChild = false;
52
0
    else
53
0
        m_bHasTextChild = true;
54
0
}
55
56
0
VCLXAccessibleBox::~VCLXAccessibleBox() {}
57
58
bool VCLXAccessibleBox::IsValid() const
59
0
{
60
0
    return GetWindow();
61
0
}
62
63
void VCLXAccessibleBox::ProcessWindowChildEvent( const VclWindowEvent& rVclWindowEvent )
64
0
{
65
0
    uno::Any aOldValue, aNewValue;
66
67
0
    switch ( rVclWindowEvent.GetId() )
68
0
    {
69
0
        case VclEventId::WindowShow:
70
0
        case VclEventId::WindowHide:
71
0
        {
72
0
            vcl::Window* pChildWindow = static_cast<vcl::Window *>(rVclWindowEvent.GetData());
73
            // Just compare to the combo box text field.  All other children
74
            // are identical to this object in which case this object will
75
            // be removed in a short time.
76
0
            if (m_aBoxType==COMBOBOX)
77
0
            {
78
0
                VclPtr< ComboBox > pComboBox = GetAs< ComboBox >();
79
0
                if (pComboBox && pChildWindow && pChildWindow == pComboBox->GetSubEdit()
80
0
                    && m_xText.is())
81
0
                {
82
0
                    if (rVclWindowEvent.GetId() == VclEventId::WindowShow)
83
0
                    {
84
                        // Instantiate text field.
85
0
                        getAccessibleChild (0);
86
0
                        aNewValue <<= m_xText;
87
0
                    }
88
0
                    else
89
0
                    {
90
                        // Release text field.
91
0
                        aOldValue <<= m_xText;
92
0
                        m_xText = nullptr;
93
0
                    }
94
                    // Tell the listeners about the new/removed child.
95
0
                    NotifyAccessibleEvent (
96
0
                        AccessibleEventId::CHILD,
97
0
                        aOldValue, aNewValue);
98
0
                }
99
100
0
            }
101
0
        }
102
0
        break;
103
104
0
        default:
105
0
            VCLXAccessibleComponent::ProcessWindowChildEvent (rVclWindowEvent);
106
0
    }
107
0
}
108
109
void VCLXAccessibleBox::ProcessWindowEvent (const VclWindowEvent& rVclWindowEvent)
110
0
{
111
0
    switch ( rVclWindowEvent.GetId() )
112
0
    {
113
0
        case VclEventId::DropdownSelect:
114
0
        case VclEventId::ListboxSelect:
115
0
        {
116
            // Forward the call to the list child.
117
0
            if (!m_xList.is())
118
0
            {
119
0
                getAccessibleChild ( m_bHasTextChild ? 1 : 0 );
120
0
            }
121
0
            if (m_xList.is())
122
0
            {
123
0
                m_xList->ProcessWindowEvent(rVclWindowEvent, m_bIsDropDownBox);
124
#if defined(_WIN32)
125
                if (m_bIsDropDownBox)
126
                {
127
                    NotifyAccessibleEvent(AccessibleEventId::VALUE_CHANGED, Any(), Any());
128
                }
129
#endif
130
0
            }
131
0
            break;
132
0
        }
133
0
        case VclEventId::DropdownOpen:
134
0
        {
135
0
            if (!m_xList.is())
136
0
            {
137
0
                getAccessibleChild ( m_bHasTextChild ? 1 : 0 );
138
0
            }
139
0
            if (m_xList.is())
140
0
            {
141
0
                m_xList->ProcessWindowEvent(rVclWindowEvent);
142
0
                m_xList->HandleDropOpen();
143
0
            }
144
0
            break;
145
0
        }
146
0
        case VclEventId::DropdownClose:
147
0
        {
148
0
            if (!m_xList.is())
149
0
            {
150
0
                getAccessibleChild ( m_bHasTextChild ? 1 : 0 );
151
0
            }
152
0
            if (m_xList.is())
153
0
            {
154
0
                m_xList->ProcessWindowEvent(rVclWindowEvent);
155
0
            }
156
0
            VclPtr<vcl::Window> pWindow = GetWindow();
157
0
            if( pWindow && (pWindow->HasFocus() || pWindow->HasChildPathFocus()) )
158
0
            {
159
0
                Any aOldValue, aNewValue;
160
0
                aNewValue <<= AccessibleStateType::FOCUSED;
161
0
                NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED, aOldValue, aNewValue );
162
0
            }
163
0
            break;
164
0
        }
165
0
        case VclEventId::ComboboxSelect:
166
0
        {
167
0
            if (m_xList.is() && m_xText.is())
168
0
            {
169
0
                Reference<XAccessibleText> xText (m_xText->getAccessibleContext(), UNO_QUERY);
170
0
                if ( xText.is() )
171
0
                {
172
0
                    OUString sText = xText->getSelectedText();
173
0
                    if ( sText.isEmpty() )
174
0
                        sText = xText->getText();
175
0
                    m_xList->UpdateSelection_Acc(sText, m_bIsDropDownBox);
176
#if defined(_WIN32)
177
                    if (m_bIsDropDownBox || m_aBoxType==COMBOBOX)
178
                        NotifyAccessibleEvent(AccessibleEventId::VALUE_CHANGED, Any(), Any());
179
#endif
180
0
                }
181
0
            }
182
0
            break;
183
0
        }
184
        //case VclEventId::DropdownOpen:
185
        //case VclEventId::DropdownClose:
186
0
        case VclEventId::ListboxDoubleClick:
187
0
        case VclEventId::ListboxScrolled:
188
        //case VclEventId::ListboxSelect:
189
0
        case VclEventId::ListboxItemAdded:
190
0
        case VclEventId::ListboxItemRemoved:
191
0
        case VclEventId::ComboboxItemAdded:
192
0
        case VclEventId::ComboboxItemRemoved:
193
0
        {
194
            // Forward the call to the list child.
195
0
            if (!m_xList.is())
196
0
            {
197
0
                getAccessibleChild ( m_bHasTextChild ? 1 : 0 );
198
0
            }
199
0
            if (m_xList.is())
200
0
                m_xList->ProcessWindowEvent(rVclWindowEvent);
201
0
            break;
202
0
        }
203
204
        //case VclEventId::ComboboxSelect:
205
0
        case VclEventId::ComboboxDeselect:
206
0
        {
207
            // Selection is handled by VCLXAccessibleList which operates on
208
            // the same VCL object as this box does.  In case of the
209
            // combobox, however, we have to help by providing the list with
210
            // the text of the currently selected item.
211
0
            if (m_xList.is() && m_xText.is())
212
0
            {
213
0
                Reference<XAccessibleText> xText (m_xText->getAccessibleContext(), UNO_QUERY);
214
0
                if ( xText.is() )
215
0
                {
216
0
                    OUString sText = xText->getSelectedText();
217
0
                    if ( sText.isEmpty() )
218
0
                        sText = xText->getText();
219
0
                    m_xList->UpdateSelection(sText);
220
0
                }
221
0
            }
222
0
            break;
223
0
        }
224
225
0
        case VclEventId::EditModify:
226
0
        case VclEventId::EditSelectionChanged:
227
0
        case VclEventId::EditCaretChanged:
228
            // Modify/Selection events are handled by the combo box instead of
229
            // directly by the edit field (Why?).  Therefore, delegate this
230
            // call to the edit field.
231
0
            if (m_aBoxType==COMBOBOX)
232
0
            {
233
0
                if (m_xText.is())
234
0
                {
235
0
                    Reference<XAccessibleContext> xContext = m_xText->getAccessibleContext();
236
0
                    VCLXAccessibleEdit* pEdit = static_cast<VCLXAccessibleEdit*>(xContext.get());
237
0
                    if (pEdit != nullptr)
238
0
                        pEdit->ProcessWindowEvent (rVclWindowEvent);
239
0
                }
240
0
            }
241
0
            break;
242
0
        default:
243
0
            VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
244
0
    }
245
0
}
246
247
//=====  XAccessibleContext  ==================================================
248
249
sal_Int64 VCLXAccessibleBox::getAccessibleChildCount()
250
0
{
251
0
    SolarMutexGuard aSolarGuard;
252
0
    ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
253
254
0
    return implGetAccessibleChildCount();
255
0
}
256
257
sal_Int64 VCLXAccessibleBox::implGetAccessibleChildCount()
258
0
{
259
    // Usually a box has a text field and a list of items as its children.
260
    // Non drop down list boxes have no text field.  Additionally check
261
    // whether the object is valid.
262
0
    sal_Int64 nCount = 0;
263
0
    if (IsValid())
264
0
        nCount += (m_bHasTextChild?1:0) + (m_bHasListChild?1:0);
265
0
    else
266
0
    {
267
        // Object not valid anymore.  Release references to children.
268
0
        m_bHasTextChild = false;
269
0
        m_xText = nullptr;
270
0
        m_bHasListChild = false;
271
0
        m_xList = nullptr;
272
0
    }
273
274
0
    return nCount;
275
0
}
276
277
Reference<XAccessible> SAL_CALL VCLXAccessibleBox::getAccessibleChild (sal_Int64 i)
278
0
{
279
0
    SolarMutexGuard aSolarGuard;
280
0
    ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
281
282
0
    if (i<0 || i>=implGetAccessibleChildCount())
283
0
        throw IndexOutOfBoundsException();
284
285
0
    if (!IsValid())
286
0
        return nullptr;
287
288
0
    if (i==1 || ! m_bHasTextChild)
289
0
    {
290
        // List.
291
0
        if ( ! m_xList.is())
292
0
        {
293
0
            m_xList = new VCLXAccessibleList(GetWindow(),
294
0
                (m_aBoxType == LISTBOX ? VCLXAccessibleList::LISTBOX : VCLXAccessibleList::COMBOBOX),
295
0
                                                                this);
296
0
            m_xList->SetIndexInParent(i);
297
0
        }
298
0
        return m_xList;
299
0
    }
300
0
    else
301
0
    {
302
        // Text Field.
303
0
        if ( ! m_xText.is())
304
0
        {
305
0
            if (m_aBoxType==COMBOBOX)
306
0
            {
307
0
                VclPtr< ComboBox > pComboBox = GetAs< ComboBox >();
308
0
                if (pComboBox && pComboBox->GetSubEdit())
309
0
                    m_xText = pComboBox->GetSubEdit()->GetAccessible();
310
0
            }
311
0
            else if (m_bIsDropDownBox)
312
0
                m_xText = new VCLXAccessibleTextField(GetAs<ListBox>(), this);
313
0
        }
314
0
        return m_xText;
315
0
    }
316
0
}
317
318
sal_Int16 SAL_CALL VCLXAccessibleBox::getAccessibleRole()
319
0
{
320
0
    ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
321
322
    // Return the role <const>COMBO_BOX</const> for both VCL combo boxes and
323
    // VCL list boxes in DropDown-Mode else <const>PANEL</const>.
324
    // This way the Java bridge has not to handle both independently.
325
    //return m_bIsDropDownBox ? AccessibleRole::COMBO_BOX : AccessibleRole::PANEL;
326
0
    if (m_bIsDropDownBox || (m_aBoxType == COMBOBOX))
327
0
        return AccessibleRole::COMBO_BOX;
328
0
    else
329
0
        return AccessibleRole::PANEL;
330
0
}
331
332
//=====  XAccessibleAction  ===================================================
333
334
sal_Int32 SAL_CALL VCLXAccessibleBox::getAccessibleActionCount()
335
0
{
336
0
    ::osl::Guard< ::osl::Mutex> aGuard (GetMutex());
337
338
    // There is one action for drop down boxes (toggle popup) and none for
339
    // the other boxes.
340
0
    return m_bIsDropDownBox ? 1 : 0;
341
0
}
342
343
sal_Bool SAL_CALL VCLXAccessibleBox::doAccessibleAction (sal_Int32 nIndex)
344
0
{
345
0
    bool bNotify = false;
346
347
0
    {
348
0
        SolarMutexGuard aSolarGuard;
349
0
        ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
350
351
0
        if (nIndex!=0 || !m_bIsDropDownBox)
352
0
            throw css::lang::IndexOutOfBoundsException(
353
0
                ("VCLXAccessibleBox::doAccessibleAction: index "
354
0
                 + OUString::number(nIndex) + " not among 0.."
355
0
                 + OUString::number(getAccessibleActionCount())),
356
0
                getXWeak());
357
358
0
        if (m_aBoxType == COMBOBOX)
359
0
        {
360
0
            VclPtr< ComboBox > pComboBox = GetAs< ComboBox >();
361
0
            if (pComboBox != nullptr)
362
0
            {
363
0
                pComboBox->ToggleDropDown();
364
0
                bNotify = true;
365
0
            }
366
0
        }
367
0
        else if (m_aBoxType == LISTBOX)
368
0
        {
369
0
            VclPtr< ListBox > pListBox = GetAs< ListBox >();
370
0
            if (pListBox != nullptr)
371
0
            {
372
0
                pListBox->ToggleDropDown();
373
0
                bNotify = true;
374
0
            }
375
0
        }
376
0
    }
377
378
0
    if (bNotify)
379
0
        NotifyAccessibleEvent (AccessibleEventId::ACTION_CHANGED, Any(), Any());
380
381
0
    return bNotify;
382
0
}
383
384
OUString SAL_CALL VCLXAccessibleBox::getAccessibleActionDescription (sal_Int32 nIndex)
385
0
{
386
0
    ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
387
0
    if (nIndex!=0 || !m_bIsDropDownBox)
388
0
        throw css::lang::IndexOutOfBoundsException();
389
390
0
    return RID_STR_ACC_ACTION_TOGGLEPOPUP;
391
0
}
392
393
Reference< XAccessibleKeyBinding > VCLXAccessibleBox::getAccessibleActionKeyBinding( sal_Int32 nIndex )
394
0
{
395
0
    ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
396
397
0
    Reference< XAccessibleKeyBinding > xRet;
398
399
0
    if (nIndex<0 || nIndex>=getAccessibleActionCount())
400
0
        throw css::lang::IndexOutOfBoundsException();
401
402
    // ... which key?
403
0
    return xRet;
404
0
}
405
406
// =====  XAccessibleValue  ===============================================
407
Any VCLXAccessibleBox::getCurrentValue( )
408
0
{
409
0
    SolarMutexGuard aSolarGuard;
410
0
    ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
411
412
0
    Any aAny;
413
0
    if( m_xList.is() && m_xText.is())
414
0
    {
415
        // VCLXAccessibleList* pList = static_cast<VCLXAccessibleList*>(m_xList.get());
416
0
        Reference<XAccessibleText> xText (m_xText->getAccessibleContext(), UNO_QUERY);
417
0
        if ( xText.is() )
418
0
        {
419
0
            OUString sText = xText->getText();
420
0
            aAny <<= sText;
421
0
        }
422
0
    }
423
0
    if (m_aBoxType == LISTBOX && m_bIsDropDownBox  && m_xList.is() )
424
0
    {
425
0
        if (m_xList->IsInDropDown())
426
0
        {
427
0
            if (m_xList->getSelectedAccessibleChildCount() > 0)
428
0
            {
429
0
                Reference<XAccessibleContext> xName (m_xList->getSelectedAccessibleChild(sal_Int64(0)), UNO_QUERY);
430
0
                if(xName.is())
431
0
                {
432
0
                    aAny <<= xName->getAccessibleName();
433
0
                }
434
0
            }
435
0
        }
436
0
    }
437
438
0
    return aAny;
439
0
}
440
441
sal_Bool VCLXAccessibleBox::setCurrentValue( const Any& aNumber )
442
0
{
443
0
    SolarMutexGuard aSolarGuard;
444
0
    ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
445
446
0
    OUString  fValue;
447
0
    bool bValid = (aNumber >>= fValue);
448
0
    return bValid;
449
450
0
}
451
452
Any VCLXAccessibleBox::getMaximumValue( )
453
0
{
454
0
    Any aAny;
455
0
    return aAny;
456
0
}
457
458
Any VCLXAccessibleBox::getMinimumValue(  )
459
0
{
460
0
    Any aAny;
461
0
    return aAny;
462
0
}
463
464
Any VCLXAccessibleBox::getMinimumIncrement(  )
465
0
{
466
0
    return Any();
467
0
}
468
469
// Set the INDETERMINATE state when there is no selected item for combobox
470
void VCLXAccessibleBox::FillAccessibleStateSet( sal_Int64& rStateSet )
471
0
{
472
0
    VCLXAccessibleComponent::FillAccessibleStateSet(rStateSet);
473
0
    if (m_aBoxType == COMBOBOX )
474
0
    {
475
0
        OUString sText;
476
0
        sal_Int32 nEntryCount = 0;
477
0
        VclPtr< ComboBox > pComboBox = GetAs< ComboBox >();
478
0
        if (pComboBox != nullptr)
479
0
        {
480
0
            Edit* pSubEdit = pComboBox->GetSubEdit();
481
0
            if ( pSubEdit)
482
0
                sText = pSubEdit->GetText();
483
0
            nEntryCount = pComboBox->GetEntryCount();
484
0
        }
485
0
        if ( sText.isEmpty() && nEntryCount > 0 )
486
0
            rStateSet |= AccessibleStateType::INDETERMINATE;
487
0
    }
488
0
    else if (m_aBoxType == LISTBOX && m_bIsDropDownBox)
489
0
    {
490
0
        VclPtr< ListBox > pListBox = GetAs< ListBox >();
491
0
        if (pListBox != nullptr && pListBox->GetEntryCount() > 0)
492
0
        {
493
0
            sal_Int32 nSelectedEntryCount = pListBox->GetSelectedEntryCount();
494
0
            if ( nSelectedEntryCount == 0)
495
0
                rStateSet |= AccessibleStateType::INDETERMINATE;
496
0
        }
497
0
    }
498
0
}
499
500
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */