Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/vcl/source/accessibility/vclxaccessibletextcomponent.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/vclxaccessibletextcomponent.hxx>
21
22
#include <com/sun/star/accessibility/AccessibleEventId.hpp>
23
#include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
24
#include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
25
#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
26
#include <comphelper/accessiblecontexthelper.hxx>
27
#include <vcl/accessibility/characterattributeshelper.hxx>
28
#include <vcl/window.hxx>
29
#include <vcl/mnemonic.hxx>
30
#include <vcl/svapp.hxx>
31
#include <vcl/unohelp2.hxx>
32
#include <vcl/ctrl.hxx>
33
#include <vcl/settings.hxx>
34
#include <vcl/unohelp.hxx>
35
#include <vcl/vclevent.hxx>
36
#include <i18nlangtag/languagetag.hxx>
37
38
using namespace ::com::sun::star;
39
using namespace ::com::sun::star::uno;
40
using namespace ::com::sun::star::lang;
41
using namespace ::com::sun::star::beans;
42
using namespace ::com::sun::star::accessibility;
43
using namespace ::comphelper;
44
45
46
47
48
VCLXAccessibleTextComponent::VCLXAccessibleTextComponent(vcl::Window* pWindow)
49
0
    : ImplInheritanceHelper(pWindow)
50
0
{
51
0
    if ( pWindow )
52
0
        m_sText = removeMnemonicFromString( pWindow->GetText() );
53
0
}
54
55
56
void VCLXAccessibleTextComponent::SetText( const OUString& sText )
57
0
{
58
0
    Any aOldValue, aNewValue;
59
0
    bool bChanged = false;
60
0
    if (!PreferFullTextInTextChangedEvent())
61
0
    {
62
0
        bChanged = implInitTextChangedEvent(m_sText, sText, aOldValue, aNewValue);
63
64
0
    }
65
0
    else if (sText != m_sText)
66
0
    {
67
        // use the full old/new text as old/new value
68
0
        TextSegment aDeletedText(m_sText, 0, m_sText.getLength());
69
0
        aOldValue <<= aDeletedText;
70
0
        TextSegment aInsertedText(sText, 0, sText.getLength());
71
0
        aNewValue <<= aInsertedText;
72
0
        bChanged = true;
73
0
    }
74
75
0
    if (bChanged)
76
0
    {
77
0
        m_sText = sText;
78
0
        NotifyAccessibleEvent( AccessibleEventId::TEXT_CHANGED, aOldValue, aNewValue );
79
0
    }
80
81
    // check whether accessible name has also changed, since text is (often) used as name as well
82
0
    const OUString sName = getAccessibleName();
83
0
    if (sName != m_sOldName)
84
0
    {
85
0
        NotifyAccessibleEvent(AccessibleEventId::NAME_CHANGED, Any(m_sOldName), Any(sName));
86
0
        m_sOldName = sName;
87
0
    }
88
0
}
89
90
91
void VCLXAccessibleTextComponent::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
92
0
{
93
0
    switch ( rVclWindowEvent.GetId() )
94
0
    {
95
0
        case VclEventId::WindowFrameTitleChanged:
96
0
        {
97
0
            VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
98
0
            SetText( implGetText() );
99
0
        }
100
0
        break;
101
0
        default:
102
0
            VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
103
0
   }
104
0
}
105
106
107
// OCommonAccessibleText
108
109
110
OUString VCLXAccessibleTextComponent::implGetText()
111
0
{
112
0
    OUString aText;
113
0
    VclPtr<vcl::Window> pWindow = GetWindow();
114
0
    if ( pWindow )
115
0
        aText = removeMnemonicFromString( pWindow->GetText() );
116
117
0
    return aText;
118
0
}
119
120
121
lang::Locale VCLXAccessibleTextComponent::implGetLocale()
122
0
{
123
0
    return Application::GetSettings().GetLanguageTag().getLocale();
124
0
}
125
126
127
void VCLXAccessibleTextComponent::implGetSelection( sal_Int32& nStartIndex, sal_Int32& nEndIndex )
128
0
{
129
0
    nStartIndex = 0;
130
0
    nEndIndex = 0;
131
0
}
132
133
134
// XComponent
135
136
137
void VCLXAccessibleTextComponent::disposing()
138
0
{
139
0
    VCLXAccessibleComponent::disposing();
140
141
0
    m_sText.clear();
142
0
}
143
144
145
// XAccessibleText
146
147
148
sal_Int32 VCLXAccessibleTextComponent::getCaretPosition()
149
0
{
150
0
    return -1;
151
0
}
152
153
154
sal_Bool VCLXAccessibleTextComponent::setCaretPosition( sal_Int32 nIndex )
155
0
{
156
0
    return setSelection( nIndex, nIndex );
157
0
}
158
159
160
sal_Unicode VCLXAccessibleTextComponent::getCharacter( sal_Int32 nIndex )
161
0
{
162
0
    OExternalLockGuard aGuard( this );
163
164
0
    return OCommonAccessibleText::implGetCharacter( implGetText(), nIndex );
165
0
}
166
167
168
Sequence< PropertyValue > VCLXAccessibleTextComponent::getCharacterAttributes( sal_Int32 nIndex, const Sequence< OUString >& aRequestedAttributes )
169
0
{
170
0
    OExternalLockGuard aGuard( this );
171
172
0
    Sequence< PropertyValue > aValues;
173
0
    OUString sText( implGetText() );
174
175
0
    if ( !implIsValidIndex( nIndex, sText.getLength() ) )
176
0
        throw IndexOutOfBoundsException();
177
178
0
    VclPtr<vcl::Window> pWindow = GetWindow();
179
0
    if ( pWindow )
180
0
    {
181
0
        vcl::Font aFont = pWindow->GetControlFont();
182
183
0
        Color nBackColor = pWindow->GetControlBackground();
184
0
        Color nColor = pWindow->GetControlForeground();
185
186
        // MT: Code with default font was introduced with the IA2 CWS, but I am not convinced that this is the correct font...
187
        // Decide what to do when we have a concrete issue.
188
        /*
189
        Font aDefaultVCLFont;
190
        OutputDevice* pDev = Application::GetDefaultDevice();
191
        if ( pDev )
192
        {
193
            aDefaultVCLFont = pDev->GetSettings().GetStyleSettings().GetAppFont();
194
            if ( !aFont.GetName().Len() )
195
            {
196
                String aDefaultName = aDefaultVCLFont.GetName();
197
                aFont.SetName( aDefaultName );
198
            }
199
            if ( !aFont.GetHeight() )
200
            {
201
                aFont.SetHeight( aDefaultVCLFont.GetHeight() );
202
            }
203
            if ( aFont.GetWeight() == WEIGHT_DONTKNOW )
204
            {
205
                aFont.SetWeight( aDefaultVCLFont.GetWeight() );
206
            }
207
208
            //if nColor is -1, it may indicate that the default color black is using.
209
            if ( nColor == -1)
210
            {
211
                nColor = aDefaultVCLFont.GetColor().GetColor();
212
            }
213
        }
214
        */
215
216
        // MT: Adjustment stuff was introduced with the IA2 CWS, but adjustment is not a character attribute...
217
        // In case we reintroduce it, use adjustment as extra parameter for the CharacterAttributesHelper...
218
        /*
219
        WinBits aBits = GetWindow()->GetStyle();
220
        sal_Int16 nAdjust = -1;
221
        if ( aBits & WB_LEFT )
222
        {
223
            nAdjust = style::ParagraphAdjust_LEFT;
224
        }
225
        else if ( aBits & WB_RIGHT )
226
        {
227
            nAdjust = style::ParagraphAdjust_RIGHT;
228
        }
229
        else if ( aBits & WB_CENTER )
230
        {
231
            nAdjust = style::ParagraphAdjust_CENTER;
232
        }
233
        */
234
235
0
        aValues = CharacterAttributesHelper( aFont, sal_Int32(nBackColor), sal_Int32(nColor) )
236
0
            .GetCharacterAttributes( aRequestedAttributes );
237
0
    }
238
239
0
    return aValues;
240
0
}
241
242
243
awt::Rectangle VCLXAccessibleTextComponent::getCharacterBounds( sal_Int32 nIndex )
244
0
{
245
0
    OExternalLockGuard aGuard( this );
246
247
0
    if ( !implIsValidIndex( nIndex, implGetText().getLength() ) )
248
0
        throw IndexOutOfBoundsException();
249
250
0
    awt::Rectangle aRect;
251
0
    VclPtr< Control > pControl = GetAs< Control >();
252
0
    if ( pControl )
253
0
        aRect = vcl::unohelper::ConvertToAWTRect(pControl->GetCharacterBounds(nIndex));
254
255
0
    return aRect;
256
0
}
257
258
259
sal_Int32 VCLXAccessibleTextComponent::getCharacterCount()
260
0
{
261
0
    OExternalLockGuard aGuard( this );
262
263
0
    return implGetText().getLength();
264
0
}
265
266
267
sal_Int32 VCLXAccessibleTextComponent::getIndexAtPoint( const awt::Point& aPoint )
268
0
{
269
0
    OExternalLockGuard aGuard( this );
270
271
0
    sal_Int32 nIndex = -1;
272
0
    VclPtr< Control > pControl = GetAs< Control >();
273
0
    if ( pControl )
274
0
        nIndex = pControl->GetIndexForPoint(vcl::unohelper::ConvertToVCLPoint(aPoint));
275
276
0
    return nIndex;
277
0
}
278
279
280
OUString VCLXAccessibleTextComponent::getSelectedText()
281
0
{
282
0
    OExternalLockGuard aGuard( this );
283
284
0
    return OCommonAccessibleText::getSelectedText();
285
0
}
286
287
288
sal_Int32 VCLXAccessibleTextComponent::getSelectionStart()
289
0
{
290
0
    OExternalLockGuard aGuard( this );
291
292
0
    return OCommonAccessibleText::getSelectionStart();
293
0
}
294
295
296
sal_Int32 VCLXAccessibleTextComponent::getSelectionEnd()
297
0
{
298
0
    OExternalLockGuard aGuard( this );
299
300
0
    return OCommonAccessibleText::getSelectionEnd();
301
0
}
302
303
304
sal_Bool VCLXAccessibleTextComponent::setSelection( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
305
0
{
306
0
    OExternalLockGuard aGuard( this );
307
308
0
    if ( !implIsValidRange( nStartIndex, nEndIndex, implGetText().getLength() ) )
309
0
        throw IndexOutOfBoundsException();
310
311
0
    return false;
312
0
}
313
314
315
OUString VCLXAccessibleTextComponent::getText()
316
0
{
317
0
    OExternalLockGuard aGuard( this );
318
319
0
    return implGetText();
320
0
}
321
322
323
OUString VCLXAccessibleTextComponent::getTextRange( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
324
0
{
325
0
    OExternalLockGuard aGuard( this );
326
327
0
    return OCommonAccessibleText::implGetTextRange( implGetText(), nStartIndex, nEndIndex );
328
0
}
329
330
331
css::accessibility::TextSegment VCLXAccessibleTextComponent::getTextAtIndex( sal_Int32 nIndex, sal_Int16 aTextType )
332
0
{
333
0
    OExternalLockGuard aGuard( this );
334
335
0
    return OCommonAccessibleText::getTextAtIndex( nIndex, aTextType );
336
0
}
337
338
339
css::accessibility::TextSegment VCLXAccessibleTextComponent::getTextBeforeIndex( sal_Int32 nIndex, sal_Int16 aTextType )
340
0
{
341
0
    OExternalLockGuard aGuard( this );
342
343
0
    return OCommonAccessibleText::getTextBeforeIndex( nIndex, aTextType );
344
0
}
345
346
347
css::accessibility::TextSegment VCLXAccessibleTextComponent::getTextBehindIndex( sal_Int32 nIndex, sal_Int16 aTextType )
348
0
{
349
0
    OExternalLockGuard aGuard( this );
350
351
0
    return OCommonAccessibleText::getTextBehindIndex( nIndex, aTextType );
352
0
}
353
354
355
sal_Bool VCLXAccessibleTextComponent::copyText( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
356
0
{
357
0
    OExternalLockGuard aGuard( this );
358
359
0
    bool bReturn = false;
360
361
0
    VclPtr<vcl::Window> pWindow = GetWindow();
362
0
    if ( pWindow )
363
0
    {
364
0
        Reference< datatransfer::clipboard::XClipboard > xClipboard = pWindow->GetClipboard();
365
0
        if ( xClipboard.is() )
366
0
        {
367
0
            OUString sText( OCommonAccessibleText::implGetTextRange( implGetText(), nStartIndex, nEndIndex ) );
368
369
0
            rtl::Reference<vcl::unohelper::TextDataObject> pDataObj = new vcl::unohelper::TextDataObject( sText );
370
371
0
            SolarMutexReleaser aReleaser;
372
0
            xClipboard->setContents( pDataObj, nullptr );
373
374
0
            Reference< datatransfer::clipboard::XFlushableClipboard > xFlushableClipboard( xClipboard, uno::UNO_QUERY );
375
0
            if( xFlushableClipboard.is() )
376
0
                xFlushableClipboard->flushClipboard();
377
378
0
            bReturn = true;
379
0
        }
380
0
    }
381
382
0
    return bReturn;
383
0
}
384
385
sal_Bool VCLXAccessibleTextComponent::scrollSubstringTo( sal_Int32, sal_Int32, AccessibleScrollType )
386
0
{
387
0
    return false;
388
0
}
389
390
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */