Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / winaccessibility / source / UAccCOM / AccEditableText.cxx
blobdc9078fe1d284ee9c6599669c03b8829b9666a82
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 /**
21 * AccEditableText.cpp : Implementation of CUAccCOMApp and DLL registration.
23 #include "stdafx.h"
24 #include "AccEditableText.h"
26 #if defined __clang__
27 #pragma clang diagnostic push
28 #pragma clang diagnostic ignored "-Wnon-virtual-dtor"
29 #endif
30 #include "UAccCOM.h"
31 #if defined __clang__
32 #pragma clang diagnostic pop
33 #endif
35 #include <vcl/svapp.hxx>
37 #include <com/sun/star/accessibility/XAccessible.hpp>
38 #include <com/sun/star/accessibility/XAccessibleContext.hpp>
39 #include <com/sun/star/accessibility/XAccessibleText.hpp>
40 #include <com/sun/star/awt/FontSlant.hpp>
41 #include <com/sun/star/beans/PropertyValue.hpp>
42 #include <com/sun/star/style/LineSpacing.hpp>
43 #include <com/sun/star/style/TabStop.hpp>
44 #include <vector>
46 using namespace com::sun::star::accessibility;
47 using namespace com::sun::star::uno;
48 using namespace com::sun::star::awt;
49 using namespace com::sun::star::beans;
50 using namespace std;
52 /**
53 * Copy a range of text to the clipboard.
55 * @param startOffset the start offset of copying.
56 * @param endOffset the end offset of copying.
57 * @param success the boolean result to be returned.
59 STDMETHODIMP CAccEditableText::copyText(long startOffset, long endOffset)
61 SolarMutexGuard g;
63 ENTER_PROTECTED_BLOCK
65 // #CHECK XInterface#
66 if(!pRXEdtTxt.is())
68 return E_FAIL;
71 if ( GetXInterface()->copyText( startOffset, endOffset ) )
72 return S_OK;
74 return E_FAIL;
76 LEAVE_PROTECTED_BLOCK
79 /**
80 * Deletes a range of text.
82 * @param startOffset the start offset of deleting.
83 * @param endOffset the end offset of deleting.
84 * @param success the boolean result to be returned.
86 STDMETHODIMP CAccEditableText::deleteText(long startOffset, long endOffset)
88 SolarMutexGuard g;
90 ENTER_PROTECTED_BLOCK
92 if( !pRXEdtTxt.is() )
93 return E_FAIL;
95 if( GetXInterface()->deleteText( startOffset, endOffset ) )
96 return S_OK;
98 return E_FAIL;
100 LEAVE_PROTECTED_BLOCK
104 * Inserts text at a specified offset.
106 * @param offset the offset of inserting.
107 * @param text the text to be inserted.
108 * @param success the boolean result to be returned.
110 STDMETHODIMP CAccEditableText::insertText(long offset, BSTR * text)
112 SolarMutexGuard g;
114 ENTER_PROTECTED_BLOCK
116 if (text == NULL)
117 return E_INVALIDARG;
119 if( !pRXEdtTxt.is() )
120 return E_FAIL;
122 ::rtl::OUString ouStr(*text);
124 if( GetXInterface()->insertText( ouStr, offset ) )
125 return S_OK;
127 return E_FAIL;
129 LEAVE_PROTECTED_BLOCK
133 * Cuts a range of text to the clipboard.
135 * @param startOffset the start offset of cutting.
136 * @param endOffset the end offset of cutting.
137 * @param success the boolean result to be returned.
139 STDMETHODIMP CAccEditableText::cutText(long startOffset, long endOffset)
141 SolarMutexGuard g;
143 ENTER_PROTECTED_BLOCK
145 if( !pRXEdtTxt.is() )
146 return E_FAIL;
148 if( GetXInterface()->cutText( startOffset, endOffset ) )
149 return S_OK;
151 return E_FAIL;
153 LEAVE_PROTECTED_BLOCK
157 * Pastes text from clipboard at specified offset.
159 * @param offset the offset of pasting.
160 * @param success the boolean result to be returned.
162 STDMETHODIMP CAccEditableText::pasteText(long offset)
164 SolarMutexGuard g;
166 ENTER_PROTECTED_BLOCK
168 if( !pRXEdtTxt.is() )
169 return E_FAIL;
171 if( GetXInterface()->pasteText( offset ) )
172 return S_OK;
174 return E_FAIL;
176 LEAVE_PROTECTED_BLOCK
180 * Replaces range of text with new text.
182 * @param startOffset the start offset of replacing.
183 * @param endOffset the end offset of replacing.
184 * @param text the replacing text.
185 * @param success the boolean result to be returned.
187 STDMETHODIMP CAccEditableText::replaceText(long startOffset, long endOffset, BSTR * text)
189 SolarMutexGuard g;
191 ENTER_PROTECTED_BLOCK
193 // #CHECK#
194 if (text == NULL)
195 return E_INVALIDARG;
196 if( !pRXEdtTxt.is() )
197 return E_FAIL;
199 ::rtl::OUString ouStr(*text);
201 if( GetXInterface()->replaceText( startOffset,endOffset, ouStr) )
202 return S_OK;
203 return E_FAIL;
205 LEAVE_PROTECTED_BLOCK
209 * Sets attributes of range of text.
211 * @param startOffset the start offset.
212 * @param endOffset the end offset.
213 * @param attributes the attribute text.
214 * @param success the boolean result to be returned.
216 STDMETHODIMP CAccEditableText::setAttributes(long startOffset, long endOffset, BSTR * attributes)
218 SolarMutexGuard g;
220 ENTER_PROTECTED_BLOCK
222 // #CHECK#
223 if (attributes == NULL)
224 return E_INVALIDARG;
225 if( !pRXEdtTxt.is() )
226 return E_FAIL;
228 ::rtl::OUString ouStr(*attributes);
230 sal_Int32 nIndex = 0;
231 sal_Unicode cTok = ';';
232 vector< ::rtl::OUString > vecAttr;
235 ::rtl::OUString ouToken = ouStr.getToken(0, cTok, nIndex);
236 vecAttr.push_back(ouToken);
238 while(nIndex >= 0);
240 Sequence< PropertyValue > beanSeq(vecAttr.size());
241 for(unsigned int i = 0; i < vecAttr.size(); i ++)
243 ::rtl::OUString attr = vecAttr[i];
244 sal_Int32 nPos = attr.indexOf(':');
245 if(nPos > -1)
247 ::rtl::OUString attrName = attr.copy(0, nPos);
248 ::rtl::OUString attrValue = attr.copy(nPos + 1, attr.getLength() - nPos - 1);
249 beanSeq[i].Name = attrName;
250 get_AnyFromOLECHAR(attrName, attrValue, beanSeq[i].Value);
254 if( GetXInterface()->setAttributes( startOffset,endOffset, beanSeq) )
255 return S_OK;
257 return E_FAIL;
259 LEAVE_PROTECTED_BLOCK
263 * Convert attributes string to Any type.
265 * @param ouName the string of attribute name.
266 * @param ouValue the string of attribute value.
267 * @param rAny the Any object to be returned.
269 void CAccEditableText::get_AnyFromOLECHAR(const ::rtl::OUString &ouName, const ::rtl::OUString &ouValue, Any &rAny)
271 if(ouName.equals(L"CharBackColor") ||
272 ouName.equals(L"CharColor") ||
273 ouName.equals(L"ParaAdjust") ||
274 ouName.equals(L"ParaFirstLineIndent") ||
275 ouName.equals(L"ParaLeftMargin") ||
276 ouName.equals(L"ParaRightMargin") ||
277 ouName.equals(L"ParaTopMargin") ||
278 ouName.equals(L"ParaBottomMargin") ||
279 ouName.equals(L"CharFontPitch") )
281 // Convert to int.
282 // NOTE: CharFontPitch is not implemented in java file.
283 sal_Int32 nValue = ouValue.toInt32();
284 rAny.setValue(&nValue, cppu::UnoType<sal_Int32>::get());
286 else if(ouName.equals(L"CharShadowed") ||
287 ouName.equals(L"CharContoured") )
289 // Convert to boolean.
290 rAny <<= ouValue.toBoolean();
292 else if(ouName.equals(L"CharEscapement") ||
293 ouName.equals(L"CharStrikeout") ||
294 ouName.equals(L"CharUnderline") ||
295 ouName.equals(L"CharFontPitch") )
297 // Convert to short.
298 short nValue = (short)ouValue.toInt32();
299 rAny.setValue(&nValue, cppu::UnoType<short>::get());
301 else if(ouName.equals(L"CharHeight") ||
302 ouName.equals(L"CharWeight") )
304 // Convert to float.
305 float fValue = ouValue.toFloat();
306 rAny.setValue(&fValue, cppu::UnoType<float>::get());
308 else if(ouName.equals(L"CharFontName") )
310 // Convert to string.
311 rAny.setValue(&ouValue, cppu::UnoType<rtl::OUString>::get());
313 else if(ouName.equals(L"CharPosture") )
315 // Convert to FontSlant.
316 css::awt::FontSlant fontSlant = (css::awt::FontSlant)ouValue.toInt32();
317 rAny.setValue(&fontSlant, cppu::UnoType<css::awt::FontSlant>::get());
319 else if(ouName.equals(L"ParaTabStops") )
322 // Convert to the Sequence with TabStop element.
323 vector< css::style::TabStop > vecTabStop;
324 css::style::TabStop tabStop;
325 ::rtl::OUString ouSubValue;
326 sal_Int32 pos = 0, posComma = 0;
330 // Position.
331 pos = ouValue.indexOf(L"Position=", pos);
332 if(pos != -1)
334 posComma = ouValue.indexOf(',', pos + 9); // 9 = length of "Position=".
335 if(posComma != -1)
337 ouSubValue = ouValue.copy(pos + 9, posComma - pos - 9);
338 tabStop.Position = ouSubValue.toInt32();
339 pos = posComma + 1;
341 // TabAlign.
342 pos = ouValue.indexOf(L"TabAlign=", pos);
343 if(pos != -1)
345 posComma = ouValue.indexOf(',', pos + 9); // 9 = length of "TabAlign=".
346 if(posComma != -1)
348 ouSubValue = ouValue.copy(pos + 9, posComma - pos - 9);
349 tabStop.Alignment = (css::style::TabAlign)ouSubValue.toInt32();
350 pos = posComma + 1;
352 // DecimalChar.
353 pos = ouValue.indexOf(L"DecimalChar=", pos);
354 if(pos != -1)
356 posComma = ouValue.indexOf(',', pos + 11); // 11 = length of "TabAlign=".
357 if(posComma != -1)
359 ouSubValue = ouValue.copy(pos + 11, posComma - pos - 11);
360 tabStop.DecimalChar = (sal_Unicode)ouSubValue.toChar();
361 pos = posComma + 1;
363 // FillChar.
364 pos = ouValue.indexOf(L"FillChar=", pos);
365 if(pos != -1)
367 posComma = ouValue.indexOf(',', pos + 9); // 9 = length of "TabAlign=".
368 if(posComma != -1)
370 ouSubValue = ouValue.copy(pos + 9, posComma - pos - 9);
371 tabStop.DecimalChar = (sal_Unicode)ouSubValue.toChar();
372 pos = posComma + 1;
374 // Complete TabStop element.
375 vecTabStop.push_back(tabStop);
377 else
378 break; // No match comma.
380 else
381 break; // No match FillChar.
383 else
384 break; // No match comma.
386 else
387 break; // No match DecimalChar.
389 else
390 break; // No match comma.
392 else
393 break; // No match TabAlign.
395 else
396 break; // No match comma.
398 else
399 break; // No match Position.
401 while(pos < ouValue.getLength());
404 // Dump into Sequence.
405 int iSeqLen = (vecTabStop.size() == 0) ? 1 : vecTabStop.size();
406 Sequence< css::style::TabStop > seqTabStop(iSeqLen);
408 if(vecTabStop.size() != 0)
410 // Dump every element.
411 for(int i = 0; i < iSeqLen; i ++)
413 seqTabStop[i] = vecTabStop[i];
416 else
418 // Create default value.
419 seqTabStop[0].Position = 0;
420 seqTabStop[0].Alignment = css::style::TabAlign_DEFAULT;
421 seqTabStop[0].DecimalChar = '.';
422 seqTabStop[0].FillChar = ' ';
425 // Assign to Any object.
426 rAny.setValue(&seqTabStop, cppu::UnoType<Sequence< css::style::TabStop >>::get());
428 else if(ouName.equals(L"ParaLineSpacing") )
430 // Parse value string.
431 css::style::LineSpacing lineSpacing;
432 ::rtl::OUString ouSubValue;
433 sal_Int32 pos = 0, posComma = 0;
435 pos = ouValue.indexOf(L"Mode=", pos);
436 if(pos != -1)
438 posComma = ouValue.indexOf(',', pos + 5); // 5 = length of "Mode=".
439 if(posComma != -1)
441 ouSubValue = ouValue.copy(pos + 5, posComma - pos - 5);
442 lineSpacing.Mode = (sal_Int16)ouSubValue.toInt32();
443 pos = posComma + 1;
445 pos = ouValue.indexOf(L"Height=", pos);
446 if(pos != -1)
448 ouSubValue = ouValue.copy(pos + 7, ouValue.getLength() - pos - 7);
449 lineSpacing.Height = (sal_Int16)ouSubValue.toInt32();
451 else
453 lineSpacing.Height = (sal_Int16)100; // Default height.
456 else
458 lineSpacing.Height = (sal_Int16)100; // Default height.
461 else
463 // Default Mode and Height.
464 lineSpacing.Mode = (sal_Int16)0;
465 lineSpacing.Height = (sal_Int16)100; // Default height.
468 // Convert to Any object.
469 rAny.setValue(&lineSpacing, cppu::UnoType<css::style::LineSpacing>::get());
471 else
473 // Do nothing.
474 sal_Int32 nDefault = 0;
475 rAny.setValue(&nDefault, cppu::UnoType<sal_Int32>::get());
480 * Override of IUNOXWrapper.
482 * @param pXInterface the pointer of UNO interface.
484 STDMETHODIMP CAccEditableText::put_XInterface(hyper pXInterface)
486 // internal IUNOXWrapper - no mutex meeded
488 ENTER_PROTECTED_BLOCK
490 CUNOXWrapper::put_XInterface(pXInterface);
491 //special query.
492 if(pUNOInterface == NULL)
493 return E_FAIL;
494 Reference<XAccessibleContext> pRContext = pUNOInterface->getAccessibleContext();
495 if( !pRContext.is() )
497 return E_FAIL;
499 Reference<XAccessibleEditableText> pRXI(pRContext,UNO_QUERY);
500 if( !pRXI.is() )
501 pRXEdtTxt = NULL;
502 else
503 pRXEdtTxt = pRXI.get();
504 return S_OK;
506 LEAVE_PROTECTED_BLOCK
509 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */