Branch libreoffice-5-0-4
[LibreOffice.git] / winaccessibility / source / UAccCOM / AccEditableText.cxx
blobf5d0d8e3dad0a7da86e7fadd6f21274079a48462
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 "UAccCOM.h"
25 #include "AccEditableText.h"
27 #include <vcl/svapp.hxx>
29 #include <com/sun/star/accessibility/XAccessible.hpp>
30 #include <com/sun/star/accessibility/XAccessibleContext.hpp>
31 #include <com/sun/star/accessibility/XAccessibleText.hpp>
32 #include <com/sun/star/awt/FontSlant.hpp>
33 #include <com/sun/star/beans/PropertyValue.hpp>
34 #include <com/sun/star/style/LineSpacing.hpp>
35 #include <com/sun/star/style/TabStop.hpp>
36 #include <vector>
38 using namespace com::sun::star::accessibility;
39 using namespace com::sun::star::uno;
40 using namespace com::sun::star::awt;
41 using namespace com::sun::star::beans;
42 using namespace std;
44 /**
45 * Copys a range of text to the clipboard.
47 * @param startOffset the start offset of copying.
48 * @param endOffset the end offset of copying.
49 * @param success the boolean result to be returned.
51 STDMETHODIMP CAccEditableText::copyText(long startOffset, long endOffset)
53 SolarMutexGuard g;
55 ENTER_PROTECTED_BLOCK
57 // #CHECK XInterface#
58 if(!pRXEdtTxt.is())
60 return E_FAIL;
63 if ( GetXInterface()->copyText( startOffset, endOffset ) )
64 return S_OK;
66 return E_FAIL;
68 LEAVE_PROTECTED_BLOCK
71 /**
72 * Deletes a range of text.
74 * @param startOffset the start offset of deleting.
75 * @param endOffset the end offset of deleting.
76 * @param success the boolean result to be returned.
78 STDMETHODIMP CAccEditableText::deleteText(long startOffset, long endOffset)
80 SolarMutexGuard g;
82 ENTER_PROTECTED_BLOCK
84 if( !pRXEdtTxt.is() )
85 return E_FAIL;
87 if( GetXInterface()->deleteText( startOffset, endOffset ) )
88 return S_OK;
90 return E_FAIL;
92 LEAVE_PROTECTED_BLOCK
95 /**
96 * Inserts text at a specified offset.
98 * @param offset the offset of inserting.
99 * @param text the text to be inserted.
100 * @param success the boolean result to be returned.
102 STDMETHODIMP CAccEditableText::insertText(long offset, BSTR * text)
104 SolarMutexGuard g;
106 ENTER_PROTECTED_BLOCK
108 if (text == NULL)
109 return E_INVALIDARG;
111 if( !pRXEdtTxt.is() )
112 return E_FAIL;
114 ::rtl::OUString ouStr(*text);
116 if( GetXInterface()->insertText( ouStr, offset ) )
117 return S_OK;
119 return E_FAIL;
121 LEAVE_PROTECTED_BLOCK
125 * Cuts a range of text to the clipboard.
127 * @param startOffset the start offset of cuting.
128 * @param endOffset the end offset of cuting.
129 * @param success the boolean result to be returned.
131 STDMETHODIMP CAccEditableText::cutText(long startOffset, long endOffset)
133 SolarMutexGuard g;
135 ENTER_PROTECTED_BLOCK
137 if( !pRXEdtTxt.is() )
138 return E_FAIL;
140 if( GetXInterface()->cutText( startOffset, endOffset ) )
141 return S_OK;
143 return E_FAIL;
145 LEAVE_PROTECTED_BLOCK
149 * Pastes text from clipboard at specified offset.
151 * @param offset the offset of pasting.
152 * @param success the boolean result to be returned.
154 STDMETHODIMP CAccEditableText::pasteText(long offset)
156 SolarMutexGuard g;
158 ENTER_PROTECTED_BLOCK
160 if( !pRXEdtTxt.is() )
161 return E_FAIL;
163 if( GetXInterface()->pasteText( offset ) )
164 return S_OK;
166 return E_FAIL;
168 LEAVE_PROTECTED_BLOCK
172 * Replaces range of text with new text.
174 * @param startOffset the start offset of replacing.
175 * @param endOffset the end offset of replacing.
176 * @param text the replacing text.
177 * @param success the boolean result to be returned.
179 STDMETHODIMP CAccEditableText::replaceText(long startOffset, long endOffset, BSTR * text)
181 SolarMutexGuard g;
183 ENTER_PROTECTED_BLOCK
185 // #CHECK#
186 if (text == NULL)
187 return E_INVALIDARG;
188 if( !pRXEdtTxt.is() )
189 return E_FAIL;
191 ::rtl::OUString ouStr(*text);
193 if( GetXInterface()->replaceText( startOffset,endOffset, ouStr) )
194 return S_OK;
195 return E_FAIL;
197 LEAVE_PROTECTED_BLOCK
201 * Sets attributes of range of text.
203 * @param startOffset the start offset.
204 * @param endOffset the end offset.
205 * @param attributes the attribute text.
206 * @param success the boolean result to be returned.
208 STDMETHODIMP CAccEditableText::setAttributes(long startOffset, long endOffset, BSTR * attributes)
210 SolarMutexGuard g;
212 ENTER_PROTECTED_BLOCK
214 // #CHECK#
215 if (attributes == NULL)
216 return E_INVALIDARG;
217 if( !pRXEdtTxt.is() )
218 return E_FAIL;
220 ::rtl::OUString ouStr(*attributes);
222 sal_Int32 nIndex = 0;
223 sal_Unicode cTok = ';';
224 vector< ::rtl::OUString > vecAttr;
227 ::rtl::OUString ouToken = ouStr.getToken(0, cTok, nIndex);
228 vecAttr.push_back(ouToken);
230 while(nIndex >= 0);
232 Sequence< PropertyValue > beanSeq(vecAttr.size());
233 for(unsigned int i = 0; i < vecAttr.size(); i ++)
235 ::rtl::OUString attr = vecAttr[i];
236 sal_Int32 nPos = attr.indexOf(':');
237 if(nPos > -1)
239 ::rtl::OUString attrName = attr.copy(0, nPos);
240 ::rtl::OUString attrValue = attr.copy(nPos + 1, attr.getLength() - nPos - 1);
241 beanSeq[i].Name = attrName;
242 get_AnyFromOLECHAR(attrName, attrValue, beanSeq[i].Value);
246 if( GetXInterface()->setAttributes( startOffset,endOffset, beanSeq) )
247 return S_OK;
249 return E_FAIL;
251 LEAVE_PROTECTED_BLOCK
255 * Convert attributes string to Any type.
256 * Reference to infra\accessibility\bridge\org\openoffice\java\accessibility\AccessibleTextImpl.java
258 * @param ouName the string of attribute name.
259 * @param ouValue the string of attribute value.
260 * @param rAny the Any object to be returned.
262 void CAccEditableText::get_AnyFromOLECHAR(const ::rtl::OUString &ouName, const ::rtl::OUString &ouValue, Any &rAny)
264 if(ouName.equals(L"CharBackColor") ||
265 ouName.equals(L"CharColor") ||
266 ouName.equals(L"ParaAdjust") ||
267 ouName.equals(L"ParaFirstLineIndent") ||
268 ouName.equals(L"ParaLeftMargin") ||
269 ouName.equals(L"ParaRightMargin") ||
270 ouName.equals(L"ParaTopMargin") ||
271 ouName.equals(L"ParaBottomMargin") ||
272 ouName.equals(L"CharFontPitch") )
274 // Convert to int.
275 // NOTE: CharFontPitch is not implemented in java file.
276 sal_Int32 nValue = ouValue.toInt32();
277 rAny.setValue(&nValue, cppu::UnoType<sal_Int32>::get());
279 else if(ouName.equals(L"CharShadowed") ||
280 ouName.equals(L"CharContoured") )
282 // Convert to boolean.
283 boolean nValue = (boolean)ouValue.toBoolean();
284 rAny.setValue(&nValue, cppu::UnoType<sal_Bool>::get() );
286 else if(ouName.equals(L"CharEscapement") ||
287 ouName.equals(L"CharStrikeout") ||
288 ouName.equals(L"CharUnderline") ||
289 ouName.equals(L"CharFontPitch") )
291 // Convert to short.
292 short nValue = (short)ouValue.toInt32();
293 rAny.setValue(&nValue, cppu::UnoType<short>::get());
295 else if(ouName.equals(L"CharHeight") ||
296 ouName.equals(L"CharWeight") )
298 // Convert to float.
299 float fValue = ouValue.toFloat();
300 rAny.setValue(&fValue, cppu::UnoType<float>::get());
302 else if(ouName.equals(L"CharFontName") )
304 // Convert to string.
305 rAny.setValue(&ouValue, cppu::UnoType<rtl::OUString>::get());
307 else if(ouName.equals(L"CharPosture") )
309 // Convert to FontSlant.
310 ::com::sun::star::awt::FontSlant fontSlant = (::com::sun::star::awt::FontSlant)ouValue.toInt32();
311 rAny.setValue(&fontSlant, cppu::UnoType<com::sun::star::awt::FontSlant>::get());
313 else if(ouName.equals(L"ParaTabStops") )
316 // Convert to the Sequence with TabStop element.
317 vector< ::com::sun::star::style::TabStop > vecTabStop;
318 ::com::sun::star::style::TabStop tabStop;
319 ::rtl::OUString ouSubValue;
320 sal_Int32 pos = 0, posComma = 0;
324 // Position.
325 pos = ouValue.indexOf(L"Position=", pos);
326 if(pos != -1)
328 posComma = ouValue.indexOf(',', pos + 9); // 9 = length of "Position=".
329 if(posComma != -1)
331 ouSubValue = ouValue.copy(pos + 9, posComma - pos - 9);
332 tabStop.Position = ouSubValue.toInt32();
333 pos = posComma + 1;
335 // TabAlign.
336 pos = ouValue.indexOf(L"TabAlign=", pos);
337 if(pos != -1)
339 posComma = ouValue.indexOf(',', pos + 9); // 9 = length of "TabAlign=".
340 if(posComma != -1)
342 ouSubValue = ouValue.copy(pos + 9, posComma - pos - 9);
343 tabStop.Alignment = (::com::sun::star::style::TabAlign)ouSubValue.toInt32();
344 pos = posComma + 1;
346 // DecimalChar.
347 pos = ouValue.indexOf(L"DecimalChar=", pos);
348 if(pos != -1)
350 posComma = ouValue.indexOf(',', pos + 11); // 11 = length of "TabAlign=".
351 if(posComma != -1)
353 ouSubValue = ouValue.copy(pos + 11, posComma - pos - 11);
354 tabStop.DecimalChar = (sal_Unicode)ouSubValue.toChar();
355 pos = posComma + 1;
357 // FillChar.
358 pos = ouValue.indexOf(L"FillChar=", pos);
359 if(pos != -1)
361 posComma = ouValue.indexOf(',', pos + 9); // 9 = length of "TabAlign=".
362 if(posComma != -1)
364 ouSubValue = ouValue.copy(pos + 9, posComma - pos - 9);
365 tabStop.DecimalChar = (sal_Unicode)ouSubValue.toChar();
366 pos = posComma + 1;
368 // Complete TabStop element.
369 vecTabStop.push_back(tabStop);
371 else
372 break; // No match comma.
374 else
375 break; // No match FillChar.
377 else
378 break; // No match comma.
380 else
381 break; // No match DecimalChar.
383 else
384 break; // No match comma.
386 else
387 break; // No match TabAlign.
389 else
390 break; // No match comma.
392 else
393 break; // No match Position.
395 while(pos < ouValue.getLength());
398 // Dump into Sequence.
399 int iSeqLen = (vecTabStop.size() == 0) ? 1 : vecTabStop.size();
400 Sequence< ::com::sun::star::style::TabStop > seqTabStop(iSeqLen);
402 if(vecTabStop.size() != 0)
404 // Dump every element.
405 for(int i = 0; i < iSeqLen; i ++)
407 seqTabStop[i] = vecTabStop[i];
410 else
412 // Create default value.
413 seqTabStop[0].Position = 0;
414 seqTabStop[0].Alignment = ::com::sun::star::style::TabAlign_DEFAULT;
415 seqTabStop[0].DecimalChar = '.';
416 seqTabStop[0].FillChar = ' ';
419 // Assign to Any object.
420 rAny.setValue(&seqTabStop, cppu::UnoType<Sequence< ::com::sun::star::style::TabStop >>::get());
422 else if(ouName.equals(L"ParaLineSpacing") )
424 // Parse value string.
425 ::com::sun::star::style::LineSpacing lineSpacing;
426 ::rtl::OUString ouSubValue;
427 sal_Int32 pos = 0, posComma = 0;
429 pos = ouValue.indexOf(L"Mode=", pos);
430 if(pos != -1)
432 posComma = ouValue.indexOf(',', pos + 5); // 5 = length of "Mode=".
433 if(posComma != -1)
435 ouSubValue = ouValue.copy(pos + 5, posComma - pos - 5);
436 lineSpacing.Mode = (sal_Int16)ouSubValue.toInt32();
437 pos = posComma + 1;
439 pos = ouValue.indexOf(L"Height=", pos);
440 if(pos != -1)
442 ouSubValue = ouValue.copy(pos + 7, ouValue.getLength() - pos - 7);
443 lineSpacing.Height = (sal_Int16)ouSubValue.toInt32();
445 else
447 lineSpacing.Height = (sal_Int16)100; // Default height.
450 else
452 lineSpacing.Height = (sal_Int16)100; // Default height.
455 else
457 // Default Mode and Height.
458 lineSpacing.Mode = (sal_Int16)0;
459 lineSpacing.Height = (sal_Int16)100; // Default height.
462 // Convert to Any object.
463 rAny.setValue(&lineSpacing, cppu::UnoType<com::sun::star::style::LineSpacing>::get());
465 else
467 // Do nothing.
468 sal_Int32 nDefault = 0;
469 rAny.setValue(&nDefault, cppu::UnoType<sal_Int32>::get());
474 * Override of IUNOXWrapper.
476 * @param pXInterface the pointer of UNO interface.
478 STDMETHODIMP CAccEditableText::put_XInterface(hyper pXInterface)
480 // internal IUNOXWrapper - no mutex meeded
482 ENTER_PROTECTED_BLOCK
484 CUNOXWrapper::put_XInterface(pXInterface);
485 //special query.
486 if(pUNOInterface == NULL)
487 return E_FAIL;
488 Reference<XAccessibleContext> pRContext = pUNOInterface->getAccessibleContext();
489 if( !pRContext.is() )
491 return E_FAIL;
493 Reference<XAccessibleEditableText> pRXI(pRContext,UNO_QUERY);
494 if( !pRXI.is() )
495 pRXEdtTxt = NULL;
496 else
497 pRXEdtTxt = pRXI.get();
498 return S_OK;
500 LEAVE_PROTECTED_BLOCK
503 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */