summaryrefslogtreecommitdiff
path: root/vcl/source/accessibility/vclxaccessibletextcomponent.cxx
blob: 2bc40dc312c8ff1d3ac4f503970858fddbfc5327 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <accessibility/vclxaccessibletextcomponent.hxx>

#include <com/sun/star/accessibility/AccessibleEventId.hpp>
#include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
#include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
#include <comphelper/accessiblecontexthelper.hxx>
#include <vcl/accessibility/characterattributeshelper.hxx>
#include <vcl/window.hxx>
#include <vcl/mnemonic.hxx>
#include <vcl/svapp.hxx>
#include <vcl/unohelp2.hxx>
#include <vcl/ctrl.hxx>
#include <vcl/settings.hxx>
#include <vcl/unohelp.hxx>
#include <i18nlangtag/languagetag.hxx>

using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::accessibility;
using namespace ::comphelper;




VCLXAccessibleTextComponent::VCLXAccessibleTextComponent(vcl::Window* pWindow)
    : ImplInheritanceHelper(pWindow)
{
    if ( pWindow )
        m_sText = removeMnemonicFromString( pWindow->GetText() );
}


void VCLXAccessibleTextComponent::SetText( const OUString& sText )
{
    Any aOldValue, aNewValue;
    bool bChanged = false;
    if (!PreferFullTextInTextChangedEvent())
    {
        bChanged = implInitTextChangedEvent(m_sText, sText, aOldValue, aNewValue);

    }
    else if (sText != m_sText)
    {
        // use the full old/new text as old/new value
        TextSegment aDeletedText(m_sText, 0, m_sText.getLength());
        aOldValue <<= aDeletedText;
        TextSegment aInsertedText(sText, 0, sText.getLength());
        aNewValue <<= aInsertedText;
        bChanged = true;
    }

    if (bChanged)
    {
        m_sText = sText;
        NotifyAccessibleEvent( AccessibleEventId::TEXT_CHANGED, aOldValue, aNewValue );
    }

    // check whether accessible name has also changed, since text is (often) used as name as well
    const OUString sName = getAccessibleName();
    if (sName != m_sOldName)
    {
        NotifyAccessibleEvent(AccessibleEventId::NAME_CHANGED, Any(m_sOldName), Any(sName));
        m_sOldName = sName;
    }
}


void VCLXAccessibleTextComponent::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
{
    switch ( rVclWindowEvent.GetId() )
    {
        case VclEventId::WindowFrameTitleChanged:
        {
            VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
            SetText( implGetText() );
        }
        break;
        default:
            VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
   }
}


// OCommonAccessibleText


OUString VCLXAccessibleTextComponent::implGetText()
{
    OUString aText;
    VclPtr<vcl::Window> pWindow = GetWindow();
    if ( pWindow )
        aText = removeMnemonicFromString( pWindow->GetText() );

    return aText;
}


lang::Locale VCLXAccessibleTextComponent::implGetLocale()
{
    return Application::GetSettings().GetLanguageTag().getLocale();
}


void VCLXAccessibleTextComponent::implGetSelection( sal_Int32& nStartIndex, sal_Int32& nEndIndex )
{
    nStartIndex = 0;
    nEndIndex = 0;
}


// XComponent


void VCLXAccessibleTextComponent::disposing()
{
    VCLXAccessibleComponent::disposing();

    m_sText.clear();
}


// XAccessibleText


sal_Int32 VCLXAccessibleTextComponent::getCaretPosition()
{
    return -1;
}


sal_Bool VCLXAccessibleTextComponent::setCaretPosition( sal_Int32 nIndex )
{
    return setSelection( nIndex, nIndex );
}


sal_Unicode VCLXAccessibleTextComponent::getCharacter( sal_Int32 nIndex )
{
    OExternalLockGuard aGuard( this );

    return OCommonAccessibleText::implGetCharacter( implGetText(), nIndex );
}


Sequence< PropertyValue > VCLXAccessibleTextComponent::getCharacterAttributes( sal_Int32 nIndex, const Sequence< OUString >& aRequestedAttributes )
{
    OExternalLockGuard aGuard( this );

    Sequence< PropertyValue > aValues;
    OUString sText( implGetText() );

    if ( !implIsValidIndex( nIndex, sText.getLength() ) )
        throw IndexOutOfBoundsException();

    VclPtr<vcl::Window> pWindow = GetWindow();
    if ( pWindow )
    {
        vcl::Font aFont = pWindow->GetControlFont();

        Color nBackColor = pWindow->GetControlBackground();
        Color nColor = pWindow->GetControlForeground();

        // MT: Code with default font was introduced with the IA2 CWS, but I am not convinced that this is the correct font...
        // Decide what to do when we have a concrete issue.
        /*
        Font aDefaultVCLFont;
        OutputDevice* pDev = Application::GetDefaultDevice();
        if ( pDev )
        {
            aDefaultVCLFont = pDev->GetSettings().GetStyleSettings().GetAppFont();
            if ( !aFont.GetName().Len() )
            {
                String aDefaultName = aDefaultVCLFont.GetName();
                aFont.SetName( aDefaultName );
            }
            if ( !aFont.GetHeight() )
            {
                aFont.SetHeight( aDefaultVCLFont.GetHeight() );
            }
            if ( aFont.GetWeight() == WEIGHT_DONTKNOW )
            {
                aFont.SetWeight( aDefaultVCLFont.GetWeight() );
            }

            //if nColor is -1, it may indicate that the default color black is using.
            if ( nColor == -1)
            {
                nColor = aDefaultVCLFont.GetColor().GetColor();
            }
        }
        */

        // MT: Adjustment stuff was introduced with the IA2 CWS, but adjustment is not a character attribute...
        // In case we reintroduce it, use adjustment as extra parameter for the CharacterAttributesHelper...
        /*
        WinBits aBits = GetWindow()->GetStyle();
        sal_Int16 nAdjust = -1;
        if ( aBits & WB_LEFT )
        {
            nAdjust = style::ParagraphAdjust_LEFT;
        }
        else if ( aBits & WB_RIGHT )
        {
            nAdjust = style::ParagraphAdjust_RIGHT;
        }
        else if ( aBits & WB_CENTER )
        {
            nAdjust = style::ParagraphAdjust_CENTER;
        }
        */

        aValues = CharacterAttributesHelper( aFont, sal_Int32(nBackColor), sal_Int32(nColor) )
            .GetCharacterAttributes( aRequestedAttributes );
    }

    return aValues;
}


awt::Rectangle VCLXAccessibleTextComponent::getCharacterBounds( sal_Int32 nIndex )
{
    OExternalLockGuard aGuard( this );

    if ( !implIsValidIndex( nIndex, implGetText().getLength() ) )
        throw IndexOutOfBoundsException();

    awt::Rectangle aRect;
    VclPtr< Control > pControl = GetAs< Control >();
    if ( pControl )
        aRect = vcl::unohelper::ConvertToAWTRect(pControl->GetCharacterBounds(nIndex));

    return aRect;
}


sal_Int32 VCLXAccessibleTextComponent::getCharacterCount()
{
    OExternalLockGuard aGuard( this );

    return implGetText().getLength();
}


sal_Int32 VCLXAccessibleTextComponent::getIndexAtPoint( const awt::Point& aPoint )
{
    OExternalLockGuard aGuard( this );

    sal_Int32 nIndex = -1;
    VclPtr< Control > pControl = GetAs< Control >();
    if ( pControl )
        nIndex = pControl->GetIndexForPoint(vcl::unohelper::ConvertToVCLPoint(aPoint));

    return nIndex;
}


OUString VCLXAccessibleTextComponent::getSelectedText()
{
    OExternalLockGuard aGuard( this );

    return OCommonAccessibleText::getSelectedText();
}


sal_Int32 VCLXAccessibleTextComponent::getSelectionStart()
{
    OExternalLockGuard aGuard( this );

    return OCommonAccessibleText::getSelectionStart();
}


sal_Int32 VCLXAccessibleTextComponent::getSelectionEnd()
{
    OExternalLockGuard aGuard( this );

    return OCommonAccessibleText::getSelectionEnd();
}


sal_Bool VCLXAccessibleTextComponent::setSelection( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
{
    OExternalLockGuard aGuard( this );

    if ( !implIsValidRange( nStartIndex, nEndIndex, implGetText().getLength() ) )
        throw IndexOutOfBoundsException();

    return false;
}


OUString VCLXAccessibleTextComponent::getText()
{
    OExternalLockGuard aGuard( this );

    return implGetText();
}


OUString VCLXAccessibleTextComponent::getTextRange( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
{
    OExternalLockGuard aGuard( this );

    return OCommonAccessibleText::implGetTextRange( implGetText(), nStartIndex, nEndIndex );
}


css::accessibility::TextSegment VCLXAccessibleTextComponent::getTextAtIndex( sal_Int32 nIndex, sal_Int16 aTextType )
{
    OExternalLockGuard aGuard( this );

    return OCommonAccessibleText::getTextAtIndex( nIndex, aTextType );
}


css::accessibility::TextSegment VCLXAccessibleTextComponent::getTextBeforeIndex( sal_Int32 nIndex, sal_Int16 aTextType )
{
    OExternalLockGuard aGuard( this );

    return OCommonAccessibleText::getTextBeforeIndex( nIndex, aTextType );
}


css::accessibility::TextSegment VCLXAccessibleTextComponent::getTextBehindIndex( sal_Int32 nIndex, sal_Int16 aTextType )
{
    OExternalLockGuard aGuard( this );

    return OCommonAccessibleText::getTextBehindIndex( nIndex, aTextType );
}


sal_Bool VCLXAccessibleTextComponent::copyText( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
{
    OExternalLockGuard aGuard( this );

    bool bReturn = false;

    VclPtr<vcl::Window> pWindow = GetWindow();
    if ( pWindow )
    {
        Reference< datatransfer::clipboard::XClipboard > xClipboard = pWindow->GetClipboard();
        if ( xClipboard.is() )
        {
            OUString sText( OCommonAccessibleText::implGetTextRange( implGetText(), nStartIndex, nEndIndex ) );

            rtl::Reference<vcl::unohelper::TextDataObject> pDataObj = new vcl::unohelper::TextDataObject( sText );

            SolarMutexReleaser aReleaser;
            xClipboard->setContents( pDataObj, nullptr );

            Reference< datatransfer::clipboard::XFlushableClipboard > xFlushableClipboard( xClipboard, uno::UNO_QUERY );
            if( xFlushableClipboard.is() )
                xFlushableClipboard->flushClipboard();

            bReturn = true;
        }
    }

    return bReturn;
}

sal_Bool VCLXAccessibleTextComponent::scrollSubstringTo( sal_Int32, sal_Int32, AccessibleScrollType )
{
    return false;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */