summaryrefslogtreecommitdiffstats
path: root/winaccessibility/source/UAccCOM/AccEditableText.cxx
blob: 212546e12ee08dd284d87fff3f7712be2d82a48c (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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/* -*- 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 .
 */

/**
 * AccEditableText.cpp : Implementation of CUAccCOMApp and DLL registration.
 */
#include "stdafx.h"
#include  <UAccCOM.h>
#include "AccEditableText.h"

#include <vcl/svapp.hxx>
#include <o3tl/char16_t2wchar_t.hxx>

#include <com/sun/star/accessibility/XAccessible.hpp>
#include <com/sun/star/accessibility/XAccessibleContext.hpp>
#include <com/sun/star/accessibility/XAccessibleText.hpp>
#include <com/sun/star/awt/FontSlant.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/style/LineSpacing.hpp>
#include <com/sun/star/style/TabStop.hpp>
#include <vector>

using namespace com::sun::star::accessibility;
using namespace com::sun::star::uno;
using namespace com::sun::star::awt;
using namespace com::sun::star::beans;

/**
 * Copy a range of text to the clipboard.
 *
 * @param    startOffset    the start offset of copying.
 * @param    endOffset      the end offset of copying.
 * @param    success        the boolean result to be returned.
 */
COM_DECLSPEC_NOTHROW STDMETHODIMP CAccEditableText::copyText(long startOffset, long endOffset)
{
    SolarMutexGuard g;

    try {

    if(!pRXEdtTxt.is())
    {
        return E_FAIL;
    }

    if ( GetXInterface()->copyText( startOffset, endOffset ) )
        return S_OK;

    return E_FAIL;

    } catch(...) { return E_FAIL; }
}

/**
 * Deletes a range of text.
 *
 * @param    startOffset    the start offset of deleting.
 * @param    endOffset      the end offset of deleting.
 * @param    success        the boolean result to be returned.
 */
COM_DECLSPEC_NOTHROW STDMETHODIMP CAccEditableText::deleteText(long startOffset, long endOffset)
{
    SolarMutexGuard g;

    try {

    if( !pRXEdtTxt.is() )
        return E_FAIL;

    if( GetXInterface()->deleteText( startOffset, endOffset ) )
        return S_OK;

    return E_FAIL;

    } catch(...) { return E_FAIL; }
}

/**
 * Inserts text at a specified offset.
 *
 * @param    offset    the offset of inserting.
 * @param    text      the text to be inserted.
 * @param    success   the boolean result to be returned.
 */
COM_DECLSPEC_NOTHROW STDMETHODIMP CAccEditableText::insertText(long offset, BSTR * text)
{
    SolarMutexGuard g;

    try {

    if (text == nullptr)
        return E_INVALIDARG;

    if( !pRXEdtTxt.is() )
        return E_FAIL;

    OUString ouStr(o3tl::toU(*text));

    if( GetXInterface()->insertText( ouStr, offset ) )
        return S_OK;

    return E_FAIL;

    } catch(...) { return E_FAIL; }
}

/**
 * Cuts a range of text to the clipboard.
 *
 * @param    startOffset    the start offset of cutting.
 * @param    endOffset      the end offset of cutting.
 * @param    success        the boolean result to be returned.
 */
COM_DECLSPEC_NOTHROW STDMETHODIMP CAccEditableText::cutText(long startOffset, long endOffset)
{
    SolarMutexGuard g;

    try {

    if( !pRXEdtTxt.is() )
        return E_FAIL;

    if( GetXInterface()->cutText( startOffset, endOffset ) )
        return S_OK;

    return E_FAIL;

    } catch(...) { return E_FAIL; }
}

/**
 * Pastes text from clipboard at specified offset.
 *
 * @param    offset    the offset of pasting.
 * @param    success   the boolean result to be returned.
 */
COM_DECLSPEC_NOTHROW STDMETHODIMP CAccEditableText::pasteText(long offset)
{
    SolarMutexGuard g;

    try {

    if( !pRXEdtTxt.is() )
        return E_FAIL;

    if( GetXInterface()->pasteText( offset ) )
        return S_OK;

    return E_FAIL;

    } catch(...) { return E_FAIL; }
}

/**
 * Replaces range of text with new text.
 *
 * @param    startOffset    the start offset of replacing.
 * @param    endOffset      the end offset of replacing.
 * @param    text           the replacing text.
 * @param    success        the boolean result to be returned.
 */
COM_DECLSPEC_NOTHROW STDMETHODIMP CAccEditableText::replaceText(long startOffset, long endOffset, BSTR * text)
{
    SolarMutexGuard g;

    try {

    if (text == nullptr)
        return E_INVALIDARG;
    if( !pRXEdtTxt.is() )
        return E_FAIL;

    OUString ouStr(o3tl::toU(*text));

    if( GetXInterface()->replaceText( startOffset,endOffset, ouStr) )
        return S_OK;
    return E_FAIL;

    } catch(...) { return E_FAIL; }
}

/**
 * Sets attributes of range of text.
 *
 * @param    startOffset    the start offset.
 * @param    endOffset      the end offset.
 * @param    attributes     the attribute text.
 * @param    success        the boolean result to be returned.
 */
COM_DECLSPEC_NOTHROW STDMETHODIMP CAccEditableText::setAttributes(long startOffset, long endOffset, BSTR * attributes)
{
    SolarMutexGuard g;

    try {

    if (attributes == nullptr)
        return E_INVALIDARG;
    if( !pRXEdtTxt.is() )
        return E_FAIL;

    OUString ouStr(o3tl::toU(*attributes));

    std::vector< OUString > vecAttr;
    for (sal_Int32 nIndex {0}; nIndex >= 0; )
        vecAttr.push_back(ouStr.getToken(0, ';', nIndex));

    Sequence< PropertyValue > beanSeq(vecAttr.size());
    auto beanSeqRange = asNonConstRange(beanSeq);
    for(std::vector<OUString>::size_type i = 0; i < vecAttr.size(); i ++)
    {
        OUString attr = vecAttr[i];
        sal_Int32 nPos = attr.indexOf(':');
        if(nPos > -1)
        {
            OUString attrName = attr.copy(0, nPos);
            OUString attrValue = attr.copy(nPos + 1);
            beanSeqRange[i].Name = attrName;
            get_AnyFromOLECHAR(attrName, attrValue, beanSeqRange[i].Value);
        }
    }

    if( GetXInterface()->setAttributes( startOffset,endOffset, beanSeq) )
        return S_OK;

    return E_FAIL;

    } catch(...) { return E_FAIL; }
}

/**
 * Convert attributes string to Any type.
 *
 * @param   ouName      the string of attribute name.
 * @param   ouValue     the string of attribute value.
 * @param   rAny        the Any object to be returned.
 */
void CAccEditableText::get_AnyFromOLECHAR(std::u16string_view ouName, const OUString &ouValue, Any &rAny)
{
    if(ouName == u"CharBackColor" ||
            ouName == u"CharColor" ||
            ouName == u"ParaAdjust" ||
            ouName == u"ParaFirstLineIndent" ||
            ouName == u"ParaLeftMargin" ||
            ouName == u"ParaRightMargin" ||
            ouName == u"ParaTopMargin" ||
            ouName == u"ParaBottomMargin" ||
            ouName == u"CharFontPitch" )
    {
        // Convert to int.
        // NOTE: CharFontPitch is not implemented in java file.
        sal_Int32 nValue = ouValue.toInt32();
        rAny.setValue(&nValue, cppu::UnoType<sal_Int32>::get());
    }
    else if(ouName == u"CharShadowed" ||
            ouName == u"CharContoured" )
    {
        // Convert to boolean.
        rAny <<= ouValue.toBoolean();
    }
    else if(ouName == u"CharEscapement" ||
            ouName == u"CharStrikeout"  ||
            ouName == u"CharUnderline" ||
            ouName == u"CharFontPitch" )
    {
        // Convert to short.
        short nValue = static_cast<short>(ouValue.toInt32());
        rAny.setValue(&nValue, cppu::UnoType<short>::get());
    }
    else if(ouName == u"CharHeight" ||
            ouName == u"CharWeight" )
    {
        // Convert to float.
        float fValue = ouValue.toFloat();
        rAny.setValue(&fValue, cppu::UnoType<float>::get());
    }
    else if(ouName == u"CharFontName" )
    {
        // Convert to string.
        rAny.setValue(&ouValue, cppu::UnoType<OUString>::get());
    }
    else if(ouName == u"CharPosture" )
    {
        // Convert to FontSlant.
        css::awt::FontSlant fontSlant = static_cast<css::awt::FontSlant>(ouValue.toInt32());
        rAny.setValue(&fontSlant, cppu::UnoType<css::awt::FontSlant>::get());
    }
    else if(ouName == u"ParaTabStops" )
    {

        // Convert to the Sequence with TabStop element.
        std::vector< css::style::TabStop > vecTabStop;
        css::style::TabStop tabStop;
        OUString ouSubValue;
        sal_Int32 pos = 0, posComma = 0;

        do
        {
            // Position.
            pos = ouValue.indexOf("Position=", pos);
            if(pos != -1)
            {
                posComma = ouValue.indexOf(',', pos + 9); // 9 = length of "Position=".
                if(posComma != -1)
                {
                    ouSubValue = ouValue.copy(pos + 9, posComma - pos - 9);
                    tabStop.Position = ouSubValue.toInt32();
                    pos = posComma + 1;

                    // TabAlign.
                    pos = ouValue.indexOf("TabAlign=", pos);
                    if(pos != -1)
                    {
                        posComma = ouValue.indexOf(',', pos + 9); // 9 = length of "TabAlign=".
                        if(posComma != -1)
                        {
                            ouSubValue = ouValue.copy(pos + 9, posComma - pos - 9);
                            tabStop.Alignment = static_cast<css::style::TabAlign>(ouSubValue.toInt32());
                            pos = posComma + 1;

                            // DecimalChar.
                            pos = ouValue.indexOf("DecimalChar=", pos);
                            if(pos != -1)
                            {
                                posComma = ouValue.indexOf(',', pos + 11); // 11 = length of "TabAlign=".
                                if(posComma != -1)
                                {
                                    ouSubValue = ouValue.copy(pos + 11, posComma - pos - 11);
                                    tabStop.DecimalChar = ouSubValue.toChar();
                                    pos = posComma + 1;

                                    // FillChar.
                                    pos = ouValue.indexOf("FillChar=", pos);
                                    if(pos != -1)
                                    {
                                        posComma = ouValue.indexOf(',', pos + 9); // 9 = length of "TabAlign=".
                                        if(posComma != -1)
                                        {
                                            ouSubValue = ouValue.copy(pos + 9, posComma - pos - 9);
                                            tabStop.DecimalChar = ouSubValue.toChar();
                                            pos = posComma + 1;

                                            // Complete TabStop element.
                                            vecTabStop.push_back(tabStop);
                                        }
                                        else
                                            break;  // No match comma.
                                    }
                                    else
                                        break;  // No match FillChar.
                                }
                                else
                                    break;  // No match comma.
                            }
                            else
                                break;  // No match DecimalChar.
                        }
                        else
                            break;  // No match comma.
                    }
                    else
                        break;  // No match TabAlign.
                }
                else
                    break;  // No match comma.
            }
            else
                break;  // No match Position.
        }
        while(pos < ouValue.getLength());


        // Dump into Sequence.
        int iSeqLen = vecTabStop.empty() ? 1 : vecTabStop.size();
        Sequence< css::style::TabStop > seqTabStop(iSeqLen);
        auto pseqTabStop = seqTabStop.getArray();

        if (!vecTabStop.empty())
        {
            // Dump every element.
            for(int i = 0; i < iSeqLen; i ++)
            {
                pseqTabStop[i] = vecTabStop[i];
            }
        }
        else
        {
            // Create default value.
            pseqTabStop[0].Position = 0;
            pseqTabStop[0].Alignment = css::style::TabAlign_DEFAULT;
            pseqTabStop[0].DecimalChar = '.';
            pseqTabStop[0].FillChar = ' ';
        }

        // Assign to Any object.
        rAny.setValue(&seqTabStop, cppu::UnoType<Sequence< css::style::TabStop >>::get());
    }
    else if(ouName == u"ParaLineSpacing" )
    {
        // Parse value string.
        css::style::LineSpacing lineSpacing;
        OUString ouSubValue;
        sal_Int32 pos = 0, posComma = 0;

        pos = ouValue.indexOf("Mode=", pos);
        if(pos != -1)
        {
            posComma = ouValue.indexOf(',', pos + 5); // 5 = length of "Mode=".
            if(posComma != -1)
            {
                ouSubValue = ouValue.copy(pos + 5, posComma - pos - 5);
                lineSpacing.Mode = static_cast<sal_Int16>(ouSubValue.toInt32());
                pos = posComma + 1;

                pos = ouValue.indexOf("Height=", pos);
                if(pos != -1)
                {
                    ouSubValue = ouValue.copy(pos + 7);
                    lineSpacing.Height = static_cast<sal_Int16>(ouSubValue.toInt32());
                }
                else
                {
                    lineSpacing.Height = sal_Int16(100);    // Default height.
                }
            }
            else
            {
                lineSpacing.Height = sal_Int16(100);    // Default height.
            }
        }
        else
        {
            // Default Mode and Height.
            lineSpacing.Mode = sal_Int16(0);
            lineSpacing.Height = sal_Int16(100);    // Default height.
        }

        // Convert to Any object.
        rAny.setValue(&lineSpacing, cppu::UnoType<css::style::LineSpacing>::get());
    }
    else
    {
        // Do nothing.
        sal_Int32 nDefault = 0;
        rAny.setValue(&nDefault, cppu::UnoType<sal_Int32>::get());
    }
}

/**
 * Override of IUNOXWrapper.
 *
 * @param    pXInterface    the pointer of UNO interface.
 */
COM_DECLSPEC_NOTHROW STDMETHODIMP CAccEditableText::put_XInterface(hyper pXInterface)
{
    // internal IUNOXWrapper - no mutex meeded

    try {

    CUNOXWrapper::put_XInterface(pXInterface);
    //special query.
    if(pUNOInterface == nullptr)
        return E_FAIL;
    Reference<XAccessibleContext> pRContext = pUNOInterface->getAccessibleContext();
    if( !pRContext.is() )
    {
        return E_FAIL;
    }
    Reference<XAccessibleEditableText> pRXI(pRContext,UNO_QUERY);
    if( !pRXI.is() )
        pRXEdtTxt = nullptr;
    else
        pRXEdtTxt = pRXI.get();
    return S_OK;

    } catch(...) { return E_FAIL; }
}

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