Coverage Report

Created: 2025-07-07 10:01

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