Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/editeng/source/accessibility/AccessibleParaManager.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 <cassert>
21
22
// Global header
23
#include <com/sun/star/uno/Any.hxx>
24
#include <com/sun/star/uno/Reference.hxx>
25
#include <o3tl/safeint.hxx>
26
#include <sal/log.hxx>
27
#include <tools/debug.hxx>
28
#include <com/sun/star/accessibility/AccessibleStateType.hpp>
29
30
// Project-local header
31
#include <editeng/AccessibleParaManager.hxx>
32
#include <editeng/AccessibleEditableTextPara.hxx>
33
34
35
using namespace ::com::sun::star;
36
using namespace ::com::sun::star::accessibility;
37
38
39
namespace accessibility
40
{
41
42
AccessibleParaManager::AccessibleParaManager() :
43
0
    maChildren(1),
44
0
    mnChildStates( 0 ),
45
0
    maEEOffset( 0, 0 ),
46
0
    mnFocusedChild( -1 ),
47
0
    mbActive( false )
48
0
{
49
0
}
50
51
AccessibleParaManager::~AccessibleParaManager()
52
0
{
53
    // owner is responsible for possible child death
54
0
}
55
56
void AccessibleParaManager::SetAdditionalChildStates( sal_Int64 nChildStates )
57
0
{
58
0
    mnChildStates = nChildStates;
59
0
}
60
61
void AccessibleParaManager::SetNum( sal_Int32 nNumParas )
62
0
{
63
0
    if( o3tl::make_unsigned(nNumParas) < maChildren.size() )
64
0
        Release( nNumParas, maChildren.size() );
65
66
0
    maChildren.resize( nNumParas );
67
68
0
    if( mnFocusedChild >= nNumParas )
69
0
        mnFocusedChild = -1;
70
0
}
71
72
sal_Int32 AccessibleParaManager::GetNum() const
73
0
{
74
0
    size_t nSize = maChildren.size();
75
0
    if (nSize > SAL_MAX_INT32)
76
0
    {
77
0
        SAL_WARN( "editeng", "AccessibleParaManager::GetNum - overflow " << nSize);
78
0
        return SAL_MAX_INT32;
79
0
    }
80
0
    return static_cast<sal_Int32>(nSize);
81
0
}
82
83
AccessibleParaManager::VectorOfChildren::iterator AccessibleParaManager::begin()
84
0
{
85
0
    return maChildren.begin();
86
0
}
87
88
AccessibleParaManager::VectorOfChildren::iterator AccessibleParaManager::end()
89
0
{
90
0
    return maChildren.end();
91
0
}
92
93
void AccessibleParaManager::FireEvent( sal_Int32 nPara,
94
                                       const sal_Int16 nEventId ) const
95
0
{
96
0
    DBG_ASSERT( 0 <= nPara && maChildren.size() > o3tl::make_unsigned(nPara),
97
0
            "AccessibleParaManager::FireEvent: invalid index" );
98
99
0
    if( 0 <= nPara && maChildren.size() > o3tl::make_unsigned(nPara) )
100
0
    {
101
0
        auto aChild( GetChild( nPara ).first.get() );
102
0
        if( aChild.is() )
103
0
            aChild->FireEvent( nEventId );
104
0
    }
105
0
}
106
107
bool AccessibleParaManager::IsReferencable( sal_Int32 nChild ) const
108
0
{
109
0
    assert(0 <= nChild && maChildren.size() > o3tl::make_unsigned(nChild)
110
0
           && "AccessibleParaManager::IsReferencable: invalid index");
111
112
0
    if( 0 <= nChild && maChildren.size() > o3tl::make_unsigned(nChild) )
113
0
    {
114
        // retrieve hard reference from weak one
115
0
        rtl::Reference<AccessibleEditableTextPara> pChild = GetChild(nChild).first.get();
116
0
        return pChild.is();
117
0
    }
118
0
    else
119
0
    {
120
0
        return false;
121
0
    }
122
0
}
123
124
AccessibleParaManager::WeakChild AccessibleParaManager::GetChild( sal_Int32 nParagraphIndex ) const
125
0
{
126
0
    DBG_ASSERT( 0 <= nParagraphIndex && maChildren.size() > o3tl::make_unsigned(nParagraphIndex),
127
0
            "AccessibleParaManager::GetChild: invalid index" );
128
129
0
    if( 0 <= nParagraphIndex && maChildren.size() > o3tl::make_unsigned(nParagraphIndex) )
130
0
    {
131
0
        return maChildren[ nParagraphIndex ];
132
0
    }
133
0
    else
134
0
    {
135
0
        return WeakChild();
136
0
    }
137
0
}
138
139
bool AccessibleParaManager::HasCreatedChild( sal_Int32 nParagraphIndex ) const
140
0
{
141
0
    if( 0 <= nParagraphIndex && maChildren.size() > o3tl::make_unsigned(nParagraphIndex) )
142
0
    {
143
0
        auto const & rChild = maChildren[ nParagraphIndex ];
144
0
        return rChild.second.Width != 0 || rChild.second.Height != 0;
145
0
    }
146
0
    else
147
0
        return false;
148
0
}
149
150
rtl::Reference<comphelper::OAccessible>
151
AccessibleParaManager::CreateChild(sal_Int32 nChild,
152
                                   const rtl::Reference<comphelper::OAccessible>& pFrontEnd,
153
                                   SvxEditSourceAdapter& rEditSource, sal_Int32 nParagraphIndex)
154
0
{
155
0
    DBG_ASSERT( 0 <= nParagraphIndex && maChildren.size() > o3tl::make_unsigned(nParagraphIndex),
156
0
            "AccessibleParaManager::CreateChild: invalid index" );
157
158
0
    if( 0 <= nParagraphIndex && maChildren.size() > o3tl::make_unsigned(nParagraphIndex) )
159
0
    {
160
        // retrieve hard reference from weak one
161
0
        rtl::Reference<AccessibleEditableTextPara> xChild(GetChild(nParagraphIndex).first.get());
162
163
0
        if( !IsReferencable( nParagraphIndex ) )
164
0
        {
165
            // there is no hard reference available, create object then
166
            // #i27138#
167
0
            xChild = new AccessibleEditableTextPara(pFrontEnd, this);
168
169
0
            InitChild(*xChild, rEditSource, nChild, nParagraphIndex);
170
171
0
            maChildren[nParagraphIndex] = WeakChild(xChild, xChild->getBounds());
172
0
        }
173
174
0
        return xChild;
175
0
    }
176
0
    else
177
0
    {
178
0
        return nullptr;
179
0
    }
180
0
}
181
182
void AccessibleParaManager::SetEEOffset( const Point& rOffset )
183
0
{
184
0
    maEEOffset = rOffset;
185
186
0
    MemFunAdapter< const Point& > aAdapter( &::accessibility::AccessibleEditableTextPara::SetEEOffset, rOffset );
187
0
    std::for_each( begin(), end(), aAdapter );
188
0
}
189
190
void AccessibleParaManager::SetActive( bool bActive )
191
0
{
192
0
    mbActive = bActive;
193
194
0
    if( bActive )
195
0
    {
196
0
        SetState( AccessibleStateType::ACTIVE );
197
0
        SetState( AccessibleStateType::EDITABLE );
198
0
    }
199
0
    else
200
0
    {
201
0
        UnSetState( AccessibleStateType::ACTIVE );
202
0
        UnSetState( AccessibleStateType::EDITABLE );
203
0
    }
204
0
}
205
206
void AccessibleParaManager::SetFocus( sal_Int32 nChild )
207
0
{
208
0
    if( mnFocusedChild != -1 )
209
0
        UnSetState( mnFocusedChild, AccessibleStateType::FOCUSED );
210
211
0
    mnFocusedChild = nChild;
212
213
0
    if( mnFocusedChild != -1 )
214
0
        SetState( mnFocusedChild, AccessibleStateType::FOCUSED );
215
0
}
216
217
void AccessibleParaManager::InitChild( AccessibleEditableTextPara&  rChild,
218
                                       SvxEditSourceAdapter&        rEditSource,
219
                                       sal_Int32                    nChild,
220
                                       sal_Int32                    nParagraphIndex ) const
221
0
{
222
0
    rChild.SetEditSource( &rEditSource );
223
0
    rChild.SetIndexInParent( nChild );
224
0
    rChild.SetParagraphIndex( nParagraphIndex );
225
226
0
    rChild.SetEEOffset( maEEOffset );
227
228
0
    if( mbActive )
229
0
    {
230
0
        rChild.SetState( AccessibleStateType::ACTIVE );
231
0
        rChild.SetState( AccessibleStateType::EDITABLE );
232
0
    }
233
234
0
    if( mnFocusedChild == nParagraphIndex )
235
0
        rChild.SetState( AccessibleStateType::FOCUSED );
236
237
    // add states passed from outside
238
0
    for (int i=0; i<63; i++)
239
0
    {
240
0
        sal_Int64 nState = sal_Int64(1) << i;
241
0
        if ( nState & mnChildStates )
242
0
            rChild.SetState( nState );
243
0
    }
244
0
}
245
246
void AccessibleParaManager::SetState( sal_Int32 nChild, const sal_Int64 nStateId )
247
0
{
248
0
    MemFunAdapter< const sal_Int64 > aFunc( &AccessibleEditableTextPara::SetState,
249
0
                                            nStateId );
250
0
    aFunc( GetChild(nChild) );
251
0
}
252
253
void AccessibleParaManager::SetState( const sal_Int64 nStateId )
254
0
{
255
0
    std::for_each( begin(), end(),
256
0
                     MemFunAdapter< const sal_Int64 >( &AccessibleEditableTextPara::SetState,
257
0
                                                       nStateId ) );
258
0
}
259
260
void AccessibleParaManager::UnSetState( sal_Int32 nChild, const sal_Int64 nStateId )
261
0
{
262
0
    MemFunAdapter< const sal_Int64 > aFunc( &AccessibleEditableTextPara::UnSetState,
263
0
                                            nStateId );
264
0
    aFunc( GetChild(nChild) );
265
0
}
266
267
void AccessibleParaManager::UnSetState( const sal_Int64 nStateId )
268
0
{
269
0
    std::for_each( begin(), end(),
270
0
                     MemFunAdapter< const sal_Int64 >( &AccessibleEditableTextPara::UnSetState,
271
0
                                                       nStateId ) );
272
0
}
273
274
namespace {
275
276
// not generic yet, no arguments...
277
class AccessibleParaManager_DisposeChildren
278
{
279
public:
280
0
    AccessibleParaManager_DisposeChildren() {}
281
    void operator()( ::accessibility::AccessibleEditableTextPara& rPara )
282
0
    {
283
0
        rPara.dispose();
284
0
    }
285
};
286
287
}
288
289
void AccessibleParaManager::Dispose()
290
0
{
291
0
    AccessibleParaManager_DisposeChildren aFunctor;
292
293
0
    std::for_each( begin(), end(),
294
0
                     WeakChildAdapter< AccessibleParaManager_DisposeChildren > (aFunctor) );
295
0
}
296
297
namespace {
298
299
// not generic yet, too many method arguments...
300
class StateChangeEvent
301
{
302
public:
303
    StateChangeEvent( const sal_Int16 nEventId,
304
                      const uno::Any& rNewValue,
305
                      const uno::Any& rOldValue ) :
306
0
        mnEventId( nEventId ),
307
0
        mrNewValue( rNewValue ),
308
0
        mrOldValue( rOldValue ) {}
309
    void operator()(::accessibility::AccessibleEditableTextPara& rPara)
310
0
    {
311
0
        rPara.FireEvent( mnEventId, mrNewValue, mrOldValue );
312
0
    }
313
314
private:
315
    const sal_Int16 mnEventId;
316
    const uno::Any& mrNewValue;
317
    const uno::Any& mrOldValue;
318
};
319
320
}
321
322
void AccessibleParaManager::FireEvent( sal_Int32 nStartPara,
323
                                       sal_Int32 nEndPara,
324
                                       const sal_Int16 nEventId,
325
                                       const uno::Any& rNewValue,
326
                                       const uno::Any& rOldValue ) const
327
0
{
328
0
    DBG_ASSERT( 0 <= nStartPara && 0 <= nEndPara &&
329
0
                maChildren.size() > o3tl::make_unsigned(nStartPara) &&
330
0
                maChildren.size() >= o3tl::make_unsigned(nEndPara) &&
331
0
                nEndPara >= nStartPara, "AccessibleParaManager::FireEvent: invalid index" );
332
333
334
0
    if( 0 <= nStartPara && 0 <= nEndPara &&
335
0
            maChildren.size() > o3tl::make_unsigned(nStartPara) &&
336
0
            maChildren.size() >= o3tl::make_unsigned(nEndPara) &&
337
0
            nEndPara >= nStartPara )
338
0
    {
339
0
        VectorOfChildren::const_iterator front = maChildren.begin();
340
0
        VectorOfChildren::const_iterator back = front;
341
342
0
        std::advance( front, nStartPara );
343
0
        std::advance( back, nEndPara );
344
345
0
        StateChangeEvent aFunctor( nEventId, rNewValue, rOldValue );
346
347
0
        std::for_each( front, back, AccessibleParaManager::WeakChildAdapter< StateChangeEvent >( aFunctor ) );
348
0
    }
349
0
}
350
351
void AccessibleParaManager::Release( sal_Int32 nStartPara, sal_Int32 nEndPara )
352
0
{
353
0
    DBG_ASSERT( 0 <= nStartPara && 0 <= nEndPara &&
354
0
                maChildren.size() > o3tl::make_unsigned(nStartPara) &&
355
0
                maChildren.size() >= o3tl::make_unsigned(nEndPara),
356
0
                "AccessibleParaManager::Release: invalid index" );
357
358
0
    if( 0 <= nStartPara && 0 <= nEndPara &&
359
0
            maChildren.size() > o3tl::make_unsigned(nStartPara) &&
360
0
            maChildren.size() >= o3tl::make_unsigned(nEndPara) )
361
0
    {
362
0
        VectorOfChildren::iterator front = maChildren.begin();
363
0
        VectorOfChildren::iterator back = front;
364
365
0
        std::advance( front, nStartPara );
366
0
        std::advance( back, nEndPara );
367
368
0
        std::transform(front, back, front,
369
0
                       [](const AccessibleParaManager::WeakChild& rPara)
370
0
                       {
371
0
                           rtl::Reference<AccessibleEditableTextPara> pChild = rPara.first.get();
372
0
                           if (pChild.is())
373
0
                           {
374
0
                               pChild->SetEditSource(nullptr);
375
0
                               pChild->dispose();
376
0
                           }
377
378
                           // clear reference
379
0
                           return AccessibleParaManager::WeakChild();
380
0
                       });
381
0
    }
382
0
}
383
384
}
385
386
387
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */