LibreOffice Module winaccessibility (master) 1
AccEditableText.cxx
Go to the documentation of this file.
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
23#include "stdafx.h"
24#include <UAccCOM.h>
25#include "AccEditableText.h"
26
27#include <vcl/svapp.hxx>
29
30#include <com/sun/star/accessibility/XAccessible.hpp>
31#include <com/sun/star/accessibility/XAccessibleContext.hpp>
32#include <com/sun/star/accessibility/XAccessibleText.hpp>
33#include <com/sun/star/awt/FontSlant.hpp>
34#include <com/sun/star/beans/PropertyValue.hpp>
35#include <com/sun/star/style/LineSpacing.hpp>
36#include <com/sun/star/style/TabStop.hpp>
37#include <vector>
38
39using namespace com::sun::star::accessibility;
40using namespace com::sun::star::uno;
41using namespace com::sun::star::awt;
42using namespace com::sun::star::beans;
43
51COM_DECLSPEC_NOTHROW STDMETHODIMP CAccEditableText::copyText(long startOffset, long endOffset)
52{
54
55 try {
56
57 // #CHECK XInterface#
58 if(!pRXEdtTxt.is())
59 {
60 return E_FAIL;
61 }
62
63 if ( GetXInterface()->copyText( startOffset, endOffset ) )
64 return S_OK;
65
66 return E_FAIL;
67
68 } catch(...) { return E_FAIL; }
69}
70
78COM_DECLSPEC_NOTHROW STDMETHODIMP CAccEditableText::deleteText(long startOffset, long endOffset)
79{
81
82 try {
83
84 if( !pRXEdtTxt.is() )
85 return E_FAIL;
86
87 if( GetXInterface()->deleteText( startOffset, endOffset ) )
88 return S_OK;
89
90 return E_FAIL;
91
92 } catch(...) { return E_FAIL; }
93}
94
102COM_DECLSPEC_NOTHROW STDMETHODIMP CAccEditableText::insertText(long offset, BSTR * text)
103{
105
106 try {
107
108 if (text == nullptr)
109 return E_INVALIDARG;
110
111 if( !pRXEdtTxt.is() )
112 return E_FAIL;
113
114 OUString ouStr(o3tl::toU(*text));
115
116 if( GetXInterface()->insertText( ouStr, offset ) )
117 return S_OK;
118
119 return E_FAIL;
120
121 } catch(...) { return E_FAIL; }
122}
123
131COM_DECLSPEC_NOTHROW STDMETHODIMP CAccEditableText::cutText(long startOffset, long endOffset)
132{
134
135 try {
136
137 if( !pRXEdtTxt.is() )
138 return E_FAIL;
139
140 if( GetXInterface()->cutText( startOffset, endOffset ) )
141 return S_OK;
142
143 return E_FAIL;
144
145 } catch(...) { return E_FAIL; }
146}
147
154COM_DECLSPEC_NOTHROW STDMETHODIMP CAccEditableText::pasteText(long offset)
155{
157
158 try {
159
160 if( !pRXEdtTxt.is() )
161 return E_FAIL;
162
163 if( GetXInterface()->pasteText( offset ) )
164 return S_OK;
165
166 return E_FAIL;
167
168 } catch(...) { return E_FAIL; }
169}
170
179COM_DECLSPEC_NOTHROW STDMETHODIMP CAccEditableText::replaceText(long startOffset, long endOffset, BSTR * text)
180{
182
183 try {
184
185 // #CHECK#
186 if (text == nullptr)
187 return E_INVALIDARG;
188 if( !pRXEdtTxt.is() )
189 return E_FAIL;
190
191 OUString ouStr(o3tl::toU(*text));
192
193 if( GetXInterface()->replaceText( startOffset,endOffset, ouStr) )
194 return S_OK;
195 return E_FAIL;
196
197 } catch(...) { return E_FAIL; }
198}
199
208COM_DECLSPEC_NOTHROW STDMETHODIMP CAccEditableText::setAttributes(long startOffset, long endOffset, BSTR * attributes)
209{
211
212 try {
213
214 // #CHECK#
215 if (attributes == nullptr)
216 return E_INVALIDARG;
217 if( !pRXEdtTxt.is() )
218 return E_FAIL;
219
220 OUString ouStr(o3tl::toU(*attributes));
221
222 std::vector< OUString > vecAttr;
223 for (sal_Int32 nIndex {0}; nIndex >= 0; )
224 vecAttr.push_back(ouStr.getToken(0, ';', nIndex));
225
226 Sequence< PropertyValue > beanSeq(vecAttr.size());
227 auto beanSeqRange = asNonConstRange(beanSeq);
228 for(std::vector<OUString>::size_type i = 0; i < vecAttr.size(); i ++)
229 {
230 OUString attr = vecAttr[i];
231 sal_Int32 nPos = attr.indexOf(':');
232 if(nPos > -1)
233 {
234 OUString attrName = attr.copy(0, nPos);
235 OUString attrValue = attr.copy(nPos + 1);
236 beanSeqRange[i].Name = attrName;
237 get_AnyFromOLECHAR(attrName, attrValue, beanSeqRange[i].Value);
238 }
239 }
240
241 if( GetXInterface()->setAttributes( startOffset,endOffset, beanSeq) )
242 return S_OK;
243
244 return E_FAIL;
245
246 } catch(...) { return E_FAIL; }
247}
248
256void CAccEditableText::get_AnyFromOLECHAR(std::u16string_view ouName, const OUString &ouValue, Any &rAny)
257{
258 if(ouName == u"CharBackColor" ||
259 ouName == u"CharColor" ||
260 ouName == u"ParaAdjust" ||
261 ouName == u"ParaFirstLineIndent" ||
262 ouName == u"ParaLeftMargin" ||
263 ouName == u"ParaRightMargin" ||
264 ouName == u"ParaTopMargin" ||
265 ouName == u"ParaBottomMargin" ||
266 ouName == u"CharFontPitch" )
267 {
268 // Convert to int.
269 // NOTE: CharFontPitch is not implemented in java file.
270 sal_Int32 nValue = ouValue.toInt32();
271 rAny.setValue(&nValue, cppu::UnoType<sal_Int32>::get());
272 }
273 else if(ouName == u"CharShadowed" ||
274 ouName == u"CharContoured" )
275 {
276 // Convert to boolean.
277 rAny <<= ouValue.toBoolean();
278 }
279 else if(ouName == u"CharEscapement" ||
280 ouName == u"CharStrikeout" ||
281 ouName == u"CharUnderline" ||
282 ouName == u"CharFontPitch" )
283 {
284 // Convert to short.
285 short nValue = static_cast<short>(ouValue.toInt32());
286 rAny.setValue(&nValue, cppu::UnoType<short>::get());
287 }
288 else if(ouName == u"CharHeight" ||
289 ouName == u"CharWeight" )
290 {
291 // Convert to float.
292 float fValue = ouValue.toFloat();
293 rAny.setValue(&fValue, cppu::UnoType<float>::get());
294 }
295 else if(ouName == u"CharFontName" )
296 {
297 // Convert to string.
298 rAny.setValue(&ouValue, cppu::UnoType<OUString>::get());
299 }
300 else if(ouName == u"CharPosture" )
301 {
302 // Convert to FontSlant.
303 css::awt::FontSlant fontSlant = static_cast<css::awt::FontSlant>(ouValue.toInt32());
304 rAny.setValue(&fontSlant, cppu::UnoType<css::awt::FontSlant>::get());
305 }
306 else if(ouName == u"ParaTabStops" )
307 {
308
309 // Convert to the Sequence with TabStop element.
310 std::vector< css::style::TabStop > vecTabStop;
311 css::style::TabStop tabStop;
312 OUString ouSubValue;
313 sal_Int32 pos = 0, posComma = 0;
314
315 do
316 {
317 // Position.
318 pos = ouValue.indexOf("Position=", pos);
319 if(pos != -1)
320 {
321 posComma = ouValue.indexOf(',', pos + 9); // 9 = length of "Position=".
322 if(posComma != -1)
323 {
324 ouSubValue = ouValue.copy(pos + 9, posComma - pos - 9);
325 tabStop.Position = ouSubValue.toInt32();
326 pos = posComma + 1;
327
328 // TabAlign.
329 pos = ouValue.indexOf("TabAlign=", pos);
330 if(pos != -1)
331 {
332 posComma = ouValue.indexOf(',', pos + 9); // 9 = length of "TabAlign=".
333 if(posComma != -1)
334 {
335 ouSubValue = ouValue.copy(pos + 9, posComma - pos - 9);
336 tabStop.Alignment = static_cast<css::style::TabAlign>(ouSubValue.toInt32());
337 pos = posComma + 1;
338
339 // DecimalChar.
340 pos = ouValue.indexOf("DecimalChar=", pos);
341 if(pos != -1)
342 {
343 posComma = ouValue.indexOf(',', pos + 11); // 11 = length of "TabAlign=".
344 if(posComma != -1)
345 {
346 ouSubValue = ouValue.copy(pos + 11, posComma - pos - 11);
347 tabStop.DecimalChar = ouSubValue.toChar();
348 pos = posComma + 1;
349
350 // FillChar.
351 pos = ouValue.indexOf("FillChar=", pos);
352 if(pos != -1)
353 {
354 posComma = ouValue.indexOf(',', pos + 9); // 9 = length of "TabAlign=".
355 if(posComma != -1)
356 {
357 ouSubValue = ouValue.copy(pos + 9, posComma - pos - 9);
358 tabStop.DecimalChar = ouSubValue.toChar();
359 pos = posComma + 1;
360
361 // Complete TabStop element.
362 vecTabStop.push_back(tabStop);
363 }
364 else
365 break; // No match comma.
366 }
367 else
368 break; // No match FillChar.
369 }
370 else
371 break; // No match comma.
372 }
373 else
374 break; // No match DecimalChar.
375 }
376 else
377 break; // No match comma.
378 }
379 else
380 break; // No match TabAlign.
381 }
382 else
383 break; // No match comma.
384 }
385 else
386 break; // No match Position.
387 }
388 while(pos < ouValue.getLength());
389
390
391 // Dump into Sequence.
392 int iSeqLen = vecTabStop.empty() ? 1 : vecTabStop.size();
393 Sequence< css::style::TabStop > seqTabStop(iSeqLen);
394 auto pseqTabStop = seqTabStop.getArray();
395
396 if (!vecTabStop.empty())
397 {
398 // Dump every element.
399 for(int i = 0; i < iSeqLen; i ++)
400 {
401 pseqTabStop[i] = vecTabStop[i];
402 }
403 }
404 else
405 {
406 // Create default value.
407 pseqTabStop[0].Position = 0;
408 pseqTabStop[0].Alignment = css::style::TabAlign_DEFAULT;
409 pseqTabStop[0].DecimalChar = '.';
410 pseqTabStop[0].FillChar = ' ';
411 }
412
413 // Assign to Any object.
414 rAny.setValue(&seqTabStop, cppu::UnoType<Sequence< css::style::TabStop >>::get());
415 }
416 else if(ouName == u"ParaLineSpacing" )
417 {
418 // Parse value string.
419 css::style::LineSpacing lineSpacing;
420 OUString ouSubValue;
421 sal_Int32 pos = 0, posComma = 0;
422
423 pos = ouValue.indexOf("Mode=", pos);
424 if(pos != -1)
425 {
426 posComma = ouValue.indexOf(',', pos + 5); // 5 = length of "Mode=".
427 if(posComma != -1)
428 {
429 ouSubValue = ouValue.copy(pos + 5, posComma - pos - 5);
430 lineSpacing.Mode = static_cast<sal_Int16>(ouSubValue.toInt32());
431 pos = posComma + 1;
432
433 pos = ouValue.indexOf("Height=", pos);
434 if(pos != -1)
435 {
436 ouSubValue = ouValue.copy(pos + 7);
437 lineSpacing.Height = static_cast<sal_Int16>(ouSubValue.toInt32());
438 }
439 else
440 {
441 lineSpacing.Height = sal_Int16(100); // Default height.
442 }
443 }
444 else
445 {
446 lineSpacing.Height = sal_Int16(100); // Default height.
447 }
448 }
449 else
450 {
451 // Default Mode and Height.
452 lineSpacing.Mode = sal_Int16(0);
453 lineSpacing.Height = sal_Int16(100); // Default height.
454 }
455
456 // Convert to Any object.
457 rAny.setValue(&lineSpacing, cppu::UnoType<css::style::LineSpacing>::get());
458 }
459 else
460 {
461 // Do nothing.
462 sal_Int32 nDefault = 0;
463 rAny.setValue(&nDefault, cppu::UnoType<sal_Int32>::get());
464 }
465}
466
472COM_DECLSPEC_NOTHROW STDMETHODIMP CAccEditableText::put_XInterface(hyper pXInterface)
473{
474 // internal IUNOXWrapper - no mutex meeded
475
476 try {
477
478 CUNOXWrapper::put_XInterface(pXInterface);
479 //special query.
480 if(pUNOInterface == nullptr)
481 return E_FAIL;
482 Reference<XAccessibleContext> pRContext = pUNOInterface->getAccessibleContext();
483 if( !pRContext.is() )
484 {
485 return E_FAIL;
486 }
487 Reference<XAccessibleEditableText> pRXI(pRContext,UNO_QUERY);
488 if( !pRXI.is() )
489 pRXEdtTxt = nullptr;
490 else
491 pRXEdtTxt = pRXI.get();
492 return S_OK;
493
494 } catch(...) { return E_FAIL; }
495}
496
497/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
STDMETHOD() setAttributes(long startOffset, long endOffset, BSTR *attributes) override
Sets attributes of range of text.
static void get_AnyFromOLECHAR(std::u16string_view ouName, const OUString &ouValue, css::uno::Any &rAny)
Convert attributes string to Any type.
STDMETHOD() copyText(long startOffset, long endOffset) override
Copy a range of text to the clipboard.
STDMETHOD() replaceText(long startOffset, long endOffset, BSTR *text) override
Replaces range of text with new text.
STDMETHOD() put_XInterface(hyper pXInterface) override
Override of IUNOXWrapper.
css::uno::Reference< css::accessibility::XAccessibleEditableText > pRXEdtTxt
STDMETHOD() insertText(long offset, BSTR *text) override
Inserts text at a specified offset.
STDMETHOD() deleteText(long startOffset, long endOffset) override
Deletes a range of text.
STDMETHOD() pasteText(long offset) override
Pastes text from clipboard at specified offset.
STDMETHOD() cutText(long startOffset, long endOffset) override
Cuts a range of text to the clipboard.
css::accessibility::XAccessibleEditableText * GetXInterface()
STDMETHOD() put_XInterface(hyper pXInterface) override
Definition: UNOXWrapper.cxx:27
css::accessibility::XAccessible * pUNOInterface
Definition: UNOXWrapper.h:34
float u
sal_Int16 nValue
sal_Int32 nIndex
sal_uInt16 nPos
const char * attrName
def text(shape, orig_st)
Value
int i
size_t pos