1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 * AccEditableText.cpp : Implementation of CUAccCOMApp and DLL registration.
24 #include "AccEditableText.h"
27 #pragma clang diagnostic push
28 #pragma clang diagnostic ignored "-Wnon-virtual-dtor"
32 #pragma clang diagnostic pop
35 #include <vcl/svapp.hxx>
36 #include <o3tl/char16_t2wchar_t.hxx>
38 #include <com/sun/star/accessibility/XAccessible.hpp>
39 #include <com/sun/star/accessibility/XAccessibleContext.hpp>
40 #include <com/sun/star/accessibility/XAccessibleText.hpp>
41 #include <com/sun/star/awt/FontSlant.hpp>
42 #include <com/sun/star/beans/PropertyValue.hpp>
43 #include <com/sun/star/style/LineSpacing.hpp>
44 #include <com/sun/star/style/TabStop.hpp>
47 using namespace com::sun::star::accessibility
;
48 using namespace com::sun::star::uno
;
49 using namespace com::sun::star::awt
;
50 using namespace com::sun::star::beans
;
54 * Copy a range of text to the clipboard.
56 * @param startOffset the start offset of copying.
57 * @param endOffset the end offset of copying.
58 * @param success the boolean result to be returned.
60 COM_DECLSPEC_NOTHROW STDMETHODIMP
CAccEditableText::copyText(long startOffset
, long endOffset
)
72 if ( GetXInterface()->copyText( startOffset
, endOffset
) )
81 * Deletes a range of text.
83 * @param startOffset the start offset of deleting.
84 * @param endOffset the end offset of deleting.
85 * @param success the boolean result to be returned.
87 COM_DECLSPEC_NOTHROW STDMETHODIMP
CAccEditableText::deleteText(long startOffset
, long endOffset
)
96 if( GetXInterface()->deleteText( startOffset
, endOffset
) )
101 LEAVE_PROTECTED_BLOCK
105 * Inserts text at a specified offset.
107 * @param offset the offset of inserting.
108 * @param text the text to be inserted.
109 * @param success the boolean result to be returned.
111 COM_DECLSPEC_NOTHROW STDMETHODIMP
CAccEditableText::insertText(long offset
, BSTR
* text
)
115 ENTER_PROTECTED_BLOCK
120 if( !pRXEdtTxt
.is() )
123 OUString
ouStr(o3tl::toU(*text
));
125 if( GetXInterface()->insertText( ouStr
, offset
) )
130 LEAVE_PROTECTED_BLOCK
134 * Cuts a range of text to the clipboard.
136 * @param startOffset the start offset of cutting.
137 * @param endOffset the end offset of cutting.
138 * @param success the boolean result to be returned.
140 COM_DECLSPEC_NOTHROW STDMETHODIMP
CAccEditableText::cutText(long startOffset
, long endOffset
)
144 ENTER_PROTECTED_BLOCK
146 if( !pRXEdtTxt
.is() )
149 if( GetXInterface()->cutText( startOffset
, endOffset
) )
154 LEAVE_PROTECTED_BLOCK
158 * Pastes text from clipboard at specified offset.
160 * @param offset the offset of pasting.
161 * @param success the boolean result to be returned.
163 COM_DECLSPEC_NOTHROW STDMETHODIMP
CAccEditableText::pasteText(long offset
)
167 ENTER_PROTECTED_BLOCK
169 if( !pRXEdtTxt
.is() )
172 if( GetXInterface()->pasteText( offset
) )
177 LEAVE_PROTECTED_BLOCK
181 * Replaces range of text with new text.
183 * @param startOffset the start offset of replacing.
184 * @param endOffset the end offset of replacing.
185 * @param text the replacing text.
186 * @param success the boolean result to be returned.
188 COM_DECLSPEC_NOTHROW STDMETHODIMP
CAccEditableText::replaceText(long startOffset
, long endOffset
, BSTR
* text
)
192 ENTER_PROTECTED_BLOCK
197 if( !pRXEdtTxt
.is() )
200 OUString
ouStr(o3tl::toU(*text
));
202 if( GetXInterface()->replaceText( startOffset
,endOffset
, ouStr
) )
206 LEAVE_PROTECTED_BLOCK
210 * Sets attributes of range of text.
212 * @param startOffset the start offset.
213 * @param endOffset the end offset.
214 * @param attributes the attribute text.
215 * @param success the boolean result to be returned.
217 COM_DECLSPEC_NOTHROW STDMETHODIMP
CAccEditableText::setAttributes(long startOffset
, long endOffset
, BSTR
* attributes
)
221 ENTER_PROTECTED_BLOCK
224 if (attributes
== nullptr)
226 if( !pRXEdtTxt
.is() )
229 OUString
ouStr(o3tl::toU(*attributes
));
231 vector
< OUString
> vecAttr
;
232 for (sal_Int32 nIndex
{0}; nIndex
>= 0; )
233 vecAttr
.push_back(ouStr
.getToken(0, ';', nIndex
));
235 Sequence
< PropertyValue
> beanSeq(vecAttr
.size());
236 for(std::vector
<OUString
>::size_type i
= 0; i
< vecAttr
.size(); i
++)
238 OUString attr
= vecAttr
[i
];
239 sal_Int32 nPos
= attr
.indexOf(':');
242 OUString attrName
= attr
.copy(0, nPos
);
243 OUString attrValue
= attr
.copy(nPos
+ 1);
244 beanSeq
[i
].Name
= attrName
;
245 get_AnyFromOLECHAR(attrName
, attrValue
, beanSeq
[i
].Value
);
249 if( GetXInterface()->setAttributes( startOffset
,endOffset
, beanSeq
) )
254 LEAVE_PROTECTED_BLOCK
258 * Convert attributes string to Any type.
260 * @param ouName the string of attribute name.
261 * @param ouValue the string of attribute value.
262 * @param rAny the Any object to be returned.
264 void CAccEditableText::get_AnyFromOLECHAR(const OUString
&ouName
, const OUString
&ouValue
, Any
&rAny
)
266 if(ouName
== "CharBackColor" ||
267 ouName
== "CharColor" ||
268 ouName
== "ParaAdjust" ||
269 ouName
== "ParaFirstLineIndent" ||
270 ouName
== "ParaLeftMargin" ||
271 ouName
== "ParaRightMargin" ||
272 ouName
== "ParaTopMargin" ||
273 ouName
== "ParaBottomMargin" ||
274 ouName
== "CharFontPitch" )
277 // NOTE: CharFontPitch is not implemented in java file.
278 sal_Int32 nValue
= ouValue
.toInt32();
279 rAny
.setValue(&nValue
, cppu::UnoType
<sal_Int32
>::get());
281 else if(ouName
== "CharShadowed" ||
282 ouName
== "CharContoured" )
284 // Convert to boolean.
285 rAny
<<= ouValue
.toBoolean();
287 else if(ouName
== "CharEscapement" ||
288 ouName
== "CharStrikeout" ||
289 ouName
== "CharUnderline" ||
290 ouName
== "CharFontPitch" )
293 short nValue
= static_cast<short>(ouValue
.toInt32());
294 rAny
.setValue(&nValue
, cppu::UnoType
<short>::get());
296 else if(ouName
== "CharHeight" ||
297 ouName
== "CharWeight" )
300 float fValue
= ouValue
.toFloat();
301 rAny
.setValue(&fValue
, cppu::UnoType
<float>::get());
303 else if(ouName
== "CharFontName" )
305 // Convert to string.
306 rAny
.setValue(&ouValue
, cppu::UnoType
<OUString
>::get());
308 else if(ouName
== "CharPosture" )
310 // Convert to FontSlant.
311 css::awt::FontSlant fontSlant
= static_cast<css::awt::FontSlant
>(ouValue
.toInt32());
312 rAny
.setValue(&fontSlant
, cppu::UnoType
<css::awt::FontSlant
>::get());
314 else if(ouName
== "ParaTabStops" )
317 // Convert to the Sequence with TabStop element.
318 vector
< css::style::TabStop
> vecTabStop
;
319 css::style::TabStop tabStop
;
321 sal_Int32 pos
= 0, posComma
= 0;
326 pos
= ouValue
.indexOf("Position=", pos
);
329 posComma
= ouValue
.indexOf(',', pos
+ 9); // 9 = length of "Position=".
332 ouSubValue
= ouValue
.copy(pos
+ 9, posComma
- pos
- 9);
333 tabStop
.Position
= ouSubValue
.toInt32();
337 pos
= ouValue
.indexOf("TabAlign=", pos
);
340 posComma
= ouValue
.indexOf(',', pos
+ 9); // 9 = length of "TabAlign=".
343 ouSubValue
= ouValue
.copy(pos
+ 9, posComma
- pos
- 9);
344 tabStop
.Alignment
= static_cast<css::style::TabAlign
>(ouSubValue
.toInt32());
348 pos
= ouValue
.indexOf("DecimalChar=", pos
);
351 posComma
= ouValue
.indexOf(',', pos
+ 11); // 11 = length of "TabAlign=".
354 ouSubValue
= ouValue
.copy(pos
+ 11, posComma
- pos
- 11);
355 tabStop
.DecimalChar
= ouSubValue
.toChar();
359 pos
= ouValue
.indexOf("FillChar=", pos
);
362 posComma
= ouValue
.indexOf(',', pos
+ 9); // 9 = length of "TabAlign=".
365 ouSubValue
= ouValue
.copy(pos
+ 9, posComma
- pos
- 9);
366 tabStop
.DecimalChar
= ouSubValue
.toChar();
369 // Complete TabStop element.
370 vecTabStop
.push_back(tabStop
);
373 break; // No match comma.
376 break; // No match FillChar.
379 break; // No match comma.
382 break; // No match DecimalChar.
385 break; // No match comma.
388 break; // No match TabAlign.
391 break; // No match comma.
394 break; // No match Position.
396 while(pos
< ouValue
.getLength());
399 // Dump into Sequence.
400 int iSeqLen
= (vecTabStop
.size() == 0) ? 1 : vecTabStop
.size();
401 Sequence
< css::style::TabStop
> seqTabStop(iSeqLen
);
403 if(vecTabStop
.size() != 0)
405 // Dump every element.
406 for(int i
= 0; i
< iSeqLen
; i
++)
408 seqTabStop
[i
] = vecTabStop
[i
];
413 // Create default value.
414 seqTabStop
[0].Position
= 0;
415 seqTabStop
[0].Alignment
= css::style::TabAlign_DEFAULT
;
416 seqTabStop
[0].DecimalChar
= '.';
417 seqTabStop
[0].FillChar
= ' ';
420 // Assign to Any object.
421 rAny
.setValue(&seqTabStop
, cppu::UnoType
<Sequence
< css::style::TabStop
>>::get());
423 else if(ouName
== "ParaLineSpacing" )
425 // Parse value string.
426 css::style::LineSpacing lineSpacing
;
428 sal_Int32 pos
= 0, posComma
= 0;
430 pos
= ouValue
.indexOf("Mode=", pos
);
433 posComma
= ouValue
.indexOf(',', pos
+ 5); // 5 = length of "Mode=".
436 ouSubValue
= ouValue
.copy(pos
+ 5, posComma
- pos
- 5);
437 lineSpacing
.Mode
= static_cast<sal_Int16
>(ouSubValue
.toInt32());
440 pos
= ouValue
.indexOf("Height=", pos
);
443 ouSubValue
= ouValue
.copy(pos
+ 7);
444 lineSpacing
.Height
= static_cast<sal_Int16
>(ouSubValue
.toInt32());
448 lineSpacing
.Height
= sal_Int16(100); // Default height.
453 lineSpacing
.Height
= sal_Int16(100); // Default height.
458 // Default Mode and Height.
459 lineSpacing
.Mode
= sal_Int16(0);
460 lineSpacing
.Height
= sal_Int16(100); // Default height.
463 // Convert to Any object.
464 rAny
.setValue(&lineSpacing
, cppu::UnoType
<css::style::LineSpacing
>::get());
469 sal_Int32 nDefault
= 0;
470 rAny
.setValue(&nDefault
, cppu::UnoType
<sal_Int32
>::get());
475 * Override of IUNOXWrapper.
477 * @param pXInterface the pointer of UNO interface.
479 COM_DECLSPEC_NOTHROW STDMETHODIMP
CAccEditableText::put_XInterface(hyper pXInterface
)
481 // internal IUNOXWrapper - no mutex meeded
483 ENTER_PROTECTED_BLOCK
485 CUNOXWrapper::put_XInterface(pXInterface
);
487 if(pUNOInterface
== nullptr)
489 Reference
<XAccessibleContext
> pRContext
= pUNOInterface
->getAccessibleContext();
490 if( !pRContext
.is() )
494 Reference
<XAccessibleEditableText
> pRXI(pRContext
,UNO_QUERY
);
498 pRXEdtTxt
= pRXI
.get();
501 LEAVE_PROTECTED_BLOCK
504 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */