LibreOffice Module accessibility (master) 1
vclxaccessibleedit.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
21
22#include <toolkit/awt/vclxwindows.hxx>
24
25#include <com/sun/star/accessibility/AccessibleStateType.hpp>
26#include <com/sun/star/accessibility/AccessibleEventId.hpp>
27#include <com/sun/star/accessibility/AccessibleRole.hpp>
28#include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
29#include <com/sun/star/accessibility/AccessibleTextType.hpp>
30#include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
32#include <comphelper/string.hxx>
33#include <vcl/svapp.hxx>
34#include <vcl/window.hxx>
35#include <vcl/mnemonic.hxx>
36#include <vcl/settings.hxx>
37#include <vcl/toolkit/edit.hxx>
39#include <vcl/textdata.hxx>
40#include <vcl/txtattr.hxx>
41#include <vcl/xtextedt.hxx>
42#include <sot/exchange.hxx>
43#include <sot/formats.hxx>
44
45#include <algorithm>
46
47using namespace ::com::sun::star;
48using namespace ::com::sun::star::uno;
49using namespace ::com::sun::star::lang;
50using namespace ::com::sun::star::beans;
51using namespace ::com::sun::star::accessibility;
52using namespace ::comphelper;
53
54
55// VCLXAccessibleEdit
56
57
59 :ImplInheritanceHelper( pVCLWindow )
60{
62}
63
64
66{
67 switch ( rVclWindowEvent.GetId() )
68 {
69 case VclEventId::EditModify:
70 {
72 }
73 break;
74 case VclEventId::EditCaretChanged:
75 {
76 sal_Int32 nOldCaretPosition = m_nCaretPosition;
78
79 VclPtr<vcl::Window> pWindow = GetWindow();
80 if (pWindow && pWindow->HasChildPathFocus())
81 {
82 if (m_nCaretPosition != nOldCaretPosition)
83 {
84 Any aOldValue, aNewValue;
85 aOldValue <<= nOldCaretPosition;
86 aNewValue <<= m_nCaretPosition;
87 NotifyAccessibleEvent( AccessibleEventId::CARET_CHANGED, aOldValue, aNewValue );
88 }
89 }
90 }
91 break;
92 case VclEventId::EditSelectionChanged:
93 {
94 VclPtr<vcl::Window> pWindow = GetWindow();
95 if (pWindow && pWindow->HasChildPathFocus())
96 {
97 NotifyAccessibleEvent( AccessibleEventId::TEXT_SELECTION_CHANGED, Any(), Any() );
98 }
99 }
100 break;
101 default:
103 }
104}
105
106
108{
109 VCLXAccessibleTextComponent::FillAccessibleStateSet( rStateSet );
110
111 VCLXEdit* pVCLXEdit = static_cast< VCLXEdit* >( GetVCLXWindow() );
112 if ( pVCLXEdit )
113 {
114 rStateSet |= AccessibleStateType::FOCUSABLE;
115 rStateSet |= AccessibleStateType::SINGLE_LINE;
116 if ( pVCLXEdit->isEditable() )
117 rStateSet |= AccessibleStateType::EDITABLE;
118 }
119}
120
121
122// OCommonAccessibleText
123
124
126{
127 OUString aText;
128
129 VclPtr< Edit > pEdit = GetAs< Edit >();
130 if ( pEdit )
131 {
132 aText = removeMnemonicFromString( pEdit->GetText() );
133
134 if ( implGetAccessibleRole() == AccessibleRole::PASSWORD_TEXT )
135 {
136 sal_Unicode cEchoChar = pEdit->GetEchoChar();
137 if ( !cEchoChar )
138 cEchoChar = '*';
139 OUStringBuffer sTmp(aText.getLength());
140 aText = comphelper::string::padToLength(sTmp, aText.getLength(),
141 cEchoChar).makeStringAndClear();
142 }
143 }
144
145 return aText;
146}
147
148
149void VCLXAccessibleEdit::implGetSelection( sal_Int32& nStartIndex, sal_Int32& nEndIndex )
150{
151 awt::Selection aSelection;
152 VCLXEdit* pVCLXEdit = static_cast< VCLXEdit* >( GetVCLXWindow() );
153 if ( pVCLXEdit )
154 aSelection = pVCLXEdit->getSelection();
155
156 nStartIndex = aSelection.Min;
157 nEndIndex = aSelection.Max;
158}
159
160
161// XServiceInfo
162
163
165{
166 return "com.sun.star.comp.toolkit.AccessibleEdit";
167}
168
169
171{
172 return { "com.sun.star.awt.AccessibleEdit" };
173}
174
175
176// XAccessibleContext
177
178
180{
181 OExternalLockGuard aGuard( this );
182
183 return 0;
184}
185
186
188{
189 throw IndexOutOfBoundsException();
190}
191
192
194{
195 OExternalLockGuard aGuard( this );
196
197 return implGetAccessibleRole();
198}
199
201{
202 sal_Int16 nRole;
203 VclPtr< Edit > pEdit = GetAs< Edit >();
204 if ( pEdit && ( pEdit->IsPassword() || pEdit->GetEchoChar() ) )
205 nRole = AccessibleRole::PASSWORD_TEXT;
206 else if ( pEdit && ( pEdit->GetStyle() & WB_READONLY ) )
207 nRole = AccessibleRole::STATIC;
208 else
209 nRole = AccessibleRole::TEXT;
210
211 return nRole;
212}
213
214
215// XAccessibleAction
216
217
219{
220 OExternalLockGuard aGuard( this );
221
222 // There is one action: activate
223 return 1;
224}
225
226
228{
229 OExternalLockGuard aGuard( this );
230
231 if ( nIndex != 0 )
232 throw IndexOutOfBoundsException();
233
234 bool bDoAction = false;
235 VclPtr<vcl::Window> pWindow = GetWindow();
236 if ( pWindow )
237 {
238 pWindow->GrabFocus();
239 bDoAction = true;
240 }
241
242 return bDoAction;
243}
244
245
247{
248 OExternalLockGuard aGuard( this );
249
250 if ( nIndex != 0)
251 throw IndexOutOfBoundsException();
252
253 return "activate";
254}
255
256
257Reference< XAccessibleKeyBinding > VCLXAccessibleEdit::getAccessibleActionKeyBinding( sal_Int32 nIndex )
258{
259 OExternalLockGuard aGuard( this );
260
261 if ( nIndex != 0 )
262 throw IndexOutOfBoundsException();
263
264 return Reference< XAccessibleKeyBinding >();
265}
266
267
268// XAccessibleText
269
270
272{
273 return getSelectionEnd();
274}
275
276
278{
279 return setSelection( nIndex, nIndex );
280}
281
282
284{
286}
287
288
289Sequence< PropertyValue > VCLXAccessibleEdit::getCharacterAttributes( sal_Int32 nIndex, const Sequence< OUString >& aRequestedAttributes )
290{
291 OExternalLockGuard aGuard( this );
292 Sequence< PropertyValue > aProperties = VCLXAccessibleTextComponent::getCharacterAttributes( nIndex, aRequestedAttributes );
293 auto aNonConstRange = asNonConstRange(aProperties);
294
295 // Handle multiline edit character properties
296 VclPtr<VclMultiLineEdit> pMulitLineEdit = GetAsDynamic< VclMultiLineEdit >();
297 if ( pMulitLineEdit )
298 {
299 ExtTextEngine* pTextEngine = pMulitLineEdit->GetTextEngine();
300 TextPaM aCursor( 0, nIndex );
301 const TextAttribFontColor* pFontColor = static_cast<const TextAttribFontColor* >(pTextEngine->FindAttrib( aCursor, TEXTATTR_FONTCOLOR ));
302 if ( pFontColor )
303 {
304 for (PropertyValue& aValue : aNonConstRange )
305 {
306 if (aValue.Name == "CharColor")
307 {
308 aValue.Value <<= pFontColor->GetColor().GetRGBColor();
309 break;
310 }
311 }
312 }
313 }
314
315 // Set default character color if it is not set yet to a valid value
316 for (PropertyValue& aValue : aNonConstRange )
317 {
318 if (aValue.Name == "CharColor")
319 {
320 if ( aValue.Value == sal_Int32(-1) )
321 {
323 if ( pDev )
324 {
325 aValue.Value <<= pDev->GetSettings().GetStyleSettings().GetFieldTextColor();
326 }
327 }
328 break;
329 }
330 }
331
332 return aProperties;
333}
334
335
336awt::Rectangle VCLXAccessibleEdit::getCharacterBounds( sal_Int32 nIndex )
337{
338 OExternalLockGuard aGuard( this );
339
340 awt::Rectangle aBounds( 0, 0, 0, 0 );
341 sal_Int32 nLength = implGetText().getLength();
342
343 if ( !implIsValidRange( nIndex, nIndex, nLength ) )
344 throw IndexOutOfBoundsException();
345
346 VclPtr< Control > pControl = GetAs< Control >();
347 if ( pControl )
348 {
349 if ( nIndex == nLength )
350 {
351 // #108914# calculate virtual bounding rectangle
352 for ( sal_Int32 i = 0; i < nLength; ++i )
353 {
354 tools::Rectangle aRect = pControl->GetCharacterBounds( i );
355 sal_Int32 nHeight = aRect.GetHeight();
356 if ( aBounds.Height < nHeight )
357 {
358 aBounds.Y = aRect.Top();
359 aBounds.Height = nHeight;
360 }
361 if ( i == nLength - 1 )
362 {
363 aBounds.X = aRect.Right() + 1;
364 aBounds.Width = 1;
365 }
366 }
367 }
368 else
369 {
370 aBounds = AWTRectangle( pControl->GetCharacterBounds( nIndex ) );
371 }
372 }
373
374 return aBounds;
375}
376
377
379{
381}
382
383
384sal_Int32 VCLXAccessibleEdit::getIndexAtPoint( const awt::Point& aPoint )
385{
387}
388
389
391{
393}
394
395
397{
399}
400
401
403{
405}
406
407
408sal_Bool VCLXAccessibleEdit::setSelection( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
409{
410 OExternalLockGuard aGuard( this );
411
412 bool bReturn = false;
413 OUString sText( implGetText() );
414
415 if ( !implIsValidRange( nStartIndex, nEndIndex, sText.getLength() ) )
416 throw IndexOutOfBoundsException();
417
418 VCLXEdit* pVCLXEdit = static_cast< VCLXEdit* >( GetVCLXWindow() );
419 VclPtr< Edit > pEdit = GetAs< Edit >();
420 if ( pVCLXEdit && pEdit && pEdit->IsEnabled() )
421 {
422 pVCLXEdit->setSelection( awt::Selection( nStartIndex, nEndIndex ) );
423 bReturn = true;
424 }
425
426 return bReturn;
427}
428
429
431{
433}
434
435
436OUString VCLXAccessibleEdit::getTextRange( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
437{
438 return VCLXAccessibleTextComponent::getTextRange( nStartIndex, nEndIndex );
439}
440
441
442css::accessibility::TextSegment VCLXAccessibleEdit::getTextAtIndex( sal_Int32 nIndex, sal_Int16 aTextType )
443{
444 OExternalLockGuard aGuard( this );
445 // Override general text component behavior: MultiLineEdit can have more text portions
446 if ( aTextType == AccessibleTextType::ATTRIBUTE_RUN )
447 {
448 VclPtr<VclMultiLineEdit> pMulitLineEdit = GetAsDynamic< VclMultiLineEdit >();
449 if ( pMulitLineEdit )
450 {
451 ExtTextEngine* pTextEngine = pMulitLineEdit->GetTextEngine();
452 TextPaM aCursor( 0, nIndex );
453 TextSegment aResult;
454 pTextEngine->GetTextPortionRange( aCursor, aResult.SegmentStart, aResult.SegmentEnd );
455 return aResult;
456 }
457 }
458
460}
461
462
463css::accessibility::TextSegment VCLXAccessibleEdit::getTextBeforeIndex( sal_Int32 nIndex, sal_Int16 aTextType )
464{
466}
467
468
469css::accessibility::TextSegment VCLXAccessibleEdit::getTextBehindIndex( sal_Int32 nIndex, sal_Int16 aTextType )
470{
472}
473
474
475sal_Bool VCLXAccessibleEdit::copyText( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
476{
477 return VCLXAccessibleTextComponent::copyText( nStartIndex, nEndIndex );
478}
479
480sal_Bool VCLXAccessibleEdit::scrollSubstringTo( sal_Int32, sal_Int32, AccessibleScrollType )
481{
482 return false;
483}
484
485// XAccessibleEditableText
486
487
488sal_Bool VCLXAccessibleEdit::cutText( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
489{
490 return copyText( nStartIndex, nEndIndex ) && deleteText( nStartIndex, nEndIndex );
491}
492
493
495{
496 OExternalLockGuard aGuard( this );
497
498 bool bReturn = false;
499
500 if ( GetWindow() )
501 {
502 Reference< datatransfer::clipboard::XClipboard > xClipboard = GetWindow()->GetClipboard();
503 if ( xClipboard.is() )
504 {
505 Reference< datatransfer::XTransferable > xDataObj;
506 {
507 SolarMutexReleaser aReleaser;
508 xDataObj = xClipboard->getContents();
509 }
510 if ( xDataObj.is() )
511 {
512 datatransfer::DataFlavor aFlavor;
513 SotExchange::GetFormatDataFlavor( SotClipboardFormatId::STRING, aFlavor );
514 if ( xDataObj->isDataFlavorSupported( aFlavor ) )
515 {
516 Any aData = xDataObj->getTransferData( aFlavor );
517 OUString sText;
518 aData >>= sText;
519 bReturn = replaceText( nIndex, nIndex, sText );
520 }
521 }
522 }
523 }
524
525 return bReturn;
526}
527
528
529sal_Bool VCLXAccessibleEdit::deleteText( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
530{
531 return replaceText( nStartIndex, nEndIndex, OUString() );
532}
533
534
535sal_Bool VCLXAccessibleEdit::insertText( const OUString& sText, sal_Int32 nIndex )
536{
537 return replaceText( nIndex, nIndex, sText );
538}
539
540
541sal_Bool VCLXAccessibleEdit::replaceText( sal_Int32 nStartIndex, sal_Int32 nEndIndex, const OUString& sReplacement )
542{
543 OExternalLockGuard aGuard( this );
544
545 bool bReturn = false;
546 OUString sText( implGetText() );
547
548 if ( !implIsValidRange( nStartIndex, nEndIndex, sText.getLength() ) )
549 throw IndexOutOfBoundsException();
550
551 sal_Int32 nMinIndex = std::min( nStartIndex, nEndIndex );
552 sal_Int32 nMaxIndex = std::max( nStartIndex, nEndIndex );
553
554 VCLXEdit* pVCLXEdit = static_cast< VCLXEdit* >( GetVCLXWindow() );
555 if ( pVCLXEdit && pVCLXEdit->isEditable() )
556 {
557 pVCLXEdit->setText( sText.replaceAt( nMinIndex, nMaxIndex - nMinIndex, sReplacement ) );
558 sal_Int32 nIndex = nMinIndex + sReplacement.getLength();
560 bReturn = true;
561 }
562
563 return bReturn;
564}
565
566
567sal_Bool VCLXAccessibleEdit::setAttributes( sal_Int32 nStartIndex, sal_Int32 nEndIndex, const Sequence<PropertyValue>& )
568{
569 OExternalLockGuard aGuard( this );
570
571 if ( !implIsValidRange( nStartIndex, nEndIndex, implGetText().getLength() ) )
572 throw IndexOutOfBoundsException();
573
574 return false; // attributes cannot be set for an edit
575}
576
577
578sal_Bool VCLXAccessibleEdit::setText( const OUString& sText )
579{
580 OExternalLockGuard aGuard( this );
581
582 bool bReturn = false;
583
584 VCLXEdit* pVCLXEdit = static_cast< VCLXEdit* >( GetVCLXWindow() );
585 if ( pVCLXEdit && pVCLXEdit->isEditable() )
586 {
587 pVCLXEdit->setText( sText );
588 sal_Int32 nSize = sText.getLength();
589 pVCLXEdit->setSelection( awt::Selection( nSize, nSize ) );
590 bReturn = true;
591 }
592
593 return bReturn;
594}
595
596
597/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
PropertiesInfo aProperties
const StyleSettings & GetStyleSettings() const
static OutputDevice * GetDefaultDevice()
Color GetRGBColor() const
const AllSettings & GetSettings() const
static bool GetFormatDataFlavor(SotClipboardFormatId nFormat, css::datatransfer::DataFlavor &rFlavor)
const Color & GetFieldTextColor() const
const Color & GetColor() const
void GetTextPortionRange(const TextPaM &rPaM, sal_Int32 &nStart, sal_Int32 &nEnd)
const TextAttrib * FindAttrib(const TextPaM &rPaM, sal_uInt16 nWhich) const
virtual OUString SAL_CALL getText() override
virtual sal_Bool SAL_CALL setAttributes(sal_Int32 nStartIndex, sal_Int32 nEndIndex, const css::uno::Sequence< css::beans::PropertyValue > &aAttributeSet) override
virtual sal_Bool SAL_CALL setCaretPosition(sal_Int32 nIndex) override
virtual css::accessibility::TextSegment SAL_CALL getTextBehindIndex(sal_Int32 nIndex, sal_Int16 aTextType) override
virtual sal_Int32 SAL_CALL getCharacterCount() override
virtual sal_Bool SAL_CALL setSelection(sal_Int32 nStartIndex, sal_Int32 nEndIndex) override
virtual sal_Int32 SAL_CALL getSelectionStart() override
sal_Int16 implGetAccessibleRole()
virtual css::uno::Reference< css::accessibility::XAccessible > SAL_CALL getAccessibleChild(sal_Int64 i) override
virtual void implGetSelection(sal_Int32 &nStartIndex, sal_Int32 &nEndIndex) override
virtual sal_Bool SAL_CALL setText(const OUString &sText) override
virtual sal_Int16 SAL_CALL getAccessibleRole() override
virtual sal_Bool SAL_CALL doAccessibleAction(sal_Int32 nIndex) override
virtual sal_Int32 SAL_CALL getIndexAtPoint(const css::awt::Point &aPoint) override
virtual sal_Int32 SAL_CALL getSelectionEnd() override
virtual sal_Bool SAL_CALL pasteText(sal_Int32 nIndex) override
virtual sal_Bool SAL_CALL cutText(sal_Int32 nStartIndex, sal_Int32 nEndIndex) override
virtual sal_Int32 SAL_CALL getCaretPosition() override
virtual void FillAccessibleStateSet(sal_Int64 &rStateSet) override
virtual OUString SAL_CALL getSelectedText() override
virtual sal_Int32 SAL_CALL getAccessibleActionCount() override
virtual OUString implGetText() override
VCLXAccessibleEdit(VCLXWindow *pVCLXindow)
virtual OUString SAL_CALL getAccessibleActionDescription(sal_Int32 nIndex) override
virtual sal_Bool SAL_CALL scrollSubstringTo(sal_Int32 nStartIndex, sal_Int32 nEndIndex, css::accessibility::AccessibleScrollType aScrollType) override
virtual css::awt::Rectangle SAL_CALL getCharacterBounds(sal_Int32 nIndex) override
virtual void ProcessWindowEvent(const VclWindowEvent &rVclWindowEvent) override
virtual css::accessibility::TextSegment SAL_CALL getTextAtIndex(sal_Int32 nIndex, sal_Int16 aTextType) override
virtual css::uno::Sequence< css::beans::PropertyValue > SAL_CALL getCharacterAttributes(sal_Int32 nIndex, const css::uno::Sequence< OUString > &aRequestedAttributes) override
virtual sal_Bool SAL_CALL replaceText(sal_Int32 nStartIndex, sal_Int32 nEndIndex, const OUString &sReplacement) override
virtual OUString SAL_CALL getTextRange(sal_Int32 nStartIndex, sal_Int32 nEndIndex) override
virtual OUString SAL_CALL getImplementationName() override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual css::accessibility::TextSegment SAL_CALL getTextBeforeIndex(sal_Int32 nIndex, sal_Int16 aTextType) override
virtual sal_Bool SAL_CALL copyText(sal_Int32 nStartIndex, sal_Int32 nEndIndex) override
virtual sal_Bool SAL_CALL deleteText(sal_Int32 nStartIndex, sal_Int32 nEndIndex) override
virtual sal_Bool SAL_CALL insertText(const OUString &sText, sal_Int32 nIndex) override
virtual css::uno::Reference< css::accessibility::XAccessibleKeyBinding > SAL_CALL getAccessibleActionKeyBinding(sal_Int32 nIndex) override
virtual sal_Unicode SAL_CALL getCharacter(sal_Int32 nIndex) override
virtual sal_Int64 SAL_CALL getAccessibleChildCount() override
virtual css::accessibility::TextSegment SAL_CALL getTextBehindIndex(sal_Int32 nIndex, sal_Int16 aTextType) override
virtual sal_Int32 SAL_CALL getIndexAtPoint(const css::awt::Point &aPoint) override
virtual OUString SAL_CALL getTextRange(sal_Int32 nStartIndex, sal_Int32 nEndIndex) override
virtual sal_Unicode SAL_CALL getCharacter(sal_Int32 nIndex) override
virtual OUString SAL_CALL getSelectedText() override
virtual void ProcessWindowEvent(const VclWindowEvent &rVclWindowEvent) override
virtual sal_Int32 SAL_CALL getSelectionStart() override
virtual css::accessibility::TextSegment SAL_CALL getTextBeforeIndex(sal_Int32 nIndex, sal_Int16 aTextType) override
virtual sal_Int32 SAL_CALL getSelectionEnd() override
virtual css::uno::Sequence< css::beans::PropertyValue > SAL_CALL getCharacterAttributes(sal_Int32 nIndex, const css::uno::Sequence< OUString > &aRequestedAttributes) override
virtual css::accessibility::TextSegment SAL_CALL getTextAtIndex(sal_Int32 nIndex, sal_Int16 aTextType) override
virtual sal_Bool SAL_CALL copyText(sal_Int32 nStartIndex, sal_Int32 nEndIndex) override
virtual OUString SAL_CALL getText() override
virtual sal_Int32 SAL_CALL getCharacterCount() override
void SAL_CALL setText(const OUString &aText) override
void SAL_CALL setSelection(const css::awt::Selection &aSelection) override
css::awt::Selection SAL_CALL getSelection() override
sal_Bool SAL_CALL isEditable() override
VclEventId GetId() const
constexpr tools::Long Top() const
constexpr tools::Long Right() const
constexpr tools::Long GetHeight() const
css::awt::Rectangle AWTRectangle(const ::tools::Rectangle &rVCLRect)
virtual void SetText(const OUString &rStr) override
sal_Int32 nIndex
VCL_DLLPUBLIC OUString removeMnemonicFromString(OUString const &rStr, sal_Int32 &rMnemonicPos)
constexpr OUStringLiteral aData
double getLength(const B2DPolygon &rCandidate)
OStringBuffer & padToLength(OStringBuffer &rBuffer, sal_Int32 nLength, char cFill='\0')
int i
#define TEXTATTR_FONTCOLOR
unsigned char sal_Bool
sal_uInt16 sal_Unicode
WinBits const WB_READONLY
sal_Int32 nLength