Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / accessibility / source / standard / vclxaccessibletextcomponent.cxx
blobef3cbed4b1eca7e1597410cf60299f4369416a13
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 #include <standard/vclxaccessibletextcomponent.hxx>
21 #include <toolkit/helper/convert.hxx>
22 #include <helper/characterattributeshelper.hxx>
24 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
25 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
26 #include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
27 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
28 #include <comphelper/accessiblecontexthelper.hxx>
29 #include <vcl/window.hxx>
30 #include <vcl/mnemonic.hxx>
31 #include <vcl/svapp.hxx>
32 #include <vcl/unohelp2.hxx>
33 #include <vcl/ctrl.hxx>
34 #include <vcl/settings.hxx>
35 #include <i18nlangtag/languagetag.hxx>
37 using namespace ::com::sun::star;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::accessibility;
42 using namespace ::comphelper;
47 VCLXAccessibleTextComponent::VCLXAccessibleTextComponent( VCLXWindow* pVCLXWindow )
48 :ImplInheritanceHelper( pVCLXWindow )
50 VclPtr<vcl::Window> pWindow = GetWindow();
51 if ( pWindow )
52 m_sText = removeMnemonicFromString( pWindow->GetText() );
56 void VCLXAccessibleTextComponent::SetText( const OUString& sText )
58 Any aOldValue, aNewValue;
59 if ( implInitTextChangedEvent( m_sText, sText, aOldValue, aNewValue ) )
61 m_sText = sText;
62 NotifyAccessibleEvent( AccessibleEventId::TEXT_CHANGED, aOldValue, aNewValue );
67 void VCLXAccessibleTextComponent::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
69 switch ( rVclWindowEvent.GetId() )
71 case VclEventId::WindowFrameTitleChanged:
73 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
74 SetText( implGetText() );
76 break;
77 default:
78 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
83 // OCommonAccessibleText
86 OUString VCLXAccessibleTextComponent::implGetText()
88 OUString aText;
89 VclPtr<vcl::Window> pWindow = GetWindow();
90 if ( pWindow )
91 aText = removeMnemonicFromString( pWindow->GetText() );
93 return aText;
97 lang::Locale VCLXAccessibleTextComponent::implGetLocale()
99 return Application::GetSettings().GetLanguageTag().getLocale();
103 void VCLXAccessibleTextComponent::implGetSelection( sal_Int32& nStartIndex, sal_Int32& nEndIndex )
105 nStartIndex = 0;
106 nEndIndex = 0;
110 // XComponent
113 void VCLXAccessibleTextComponent::disposing()
115 VCLXAccessibleComponent::disposing();
117 m_sText.clear();
121 // XAccessibleText
124 sal_Int32 VCLXAccessibleTextComponent::getCaretPosition()
126 return -1;
130 sal_Bool VCLXAccessibleTextComponent::setCaretPosition( sal_Int32 nIndex )
132 return setSelection( nIndex, nIndex );
136 sal_Unicode VCLXAccessibleTextComponent::getCharacter( sal_Int32 nIndex )
138 OExternalLockGuard aGuard( this );
140 return OCommonAccessibleText::implGetCharacter( implGetText(), nIndex );
144 Sequence< PropertyValue > VCLXAccessibleTextComponent::getCharacterAttributes( sal_Int32 nIndex, const Sequence< OUString >& aRequestedAttributes )
146 OExternalLockGuard aGuard( this );
148 Sequence< PropertyValue > aValues;
149 OUString sText( implGetText() );
151 if ( !implIsValidIndex( nIndex, sText.getLength() ) )
152 throw IndexOutOfBoundsException();
154 VclPtr<vcl::Window> pWindow = GetWindow();
155 if ( pWindow )
157 vcl::Font aFont = pWindow->GetControlFont();
159 Color nBackColor = pWindow->GetControlBackground();
160 Color nColor = pWindow->GetControlForeground();
162 // MT: Code with default font was introduced with the IA2 CWS, but I am not convinced that this is the correct font...
163 // Decide what to do when we have a concrete issue.
165 Font aDefaultVCLFont;
166 OutputDevice* pDev = Application::GetDefaultDevice();
167 if ( pDev )
169 aDefaultVCLFont = pDev->GetSettings().GetStyleSettings().GetAppFont();
170 if ( !aFont.GetName().Len() )
172 String aDefaultName = aDefaultVCLFont.GetName();
173 aFont.SetName( aDefaultName );
175 if ( !aFont.GetHeight() )
177 aFont.SetHeight( aDefaultVCLFont.GetHeight() );
179 if ( aFont.GetWeight() == WEIGHT_DONTKNOW )
181 aFont.SetWeight( aDefaultVCLFont.GetWeight() );
184 //if nColor is -1, it may indicate that the default color black is using.
185 if ( nColor == -1)
187 nColor = aDefaultVCLFont.GetColor().GetColor();
192 // MT: Adjustment stuff was introduced with the IA2 CWS, but adjustment is not a character attribute...
193 // In case we reintroduce it, use adjustment as extra parameter for the CharacterAttributesHelper...
195 WinBits aBits = GetWindow()->GetStyle();
196 sal_Int16 nAdjust = -1;
197 if ( aBits & WB_LEFT )
199 nAdjust = style::ParagraphAdjust_LEFT;
201 else if ( aBits & WB_RIGHT )
203 nAdjust = style::ParagraphAdjust_RIGHT;
205 else if ( aBits & WB_CENTER )
207 nAdjust = style::ParagraphAdjust_CENTER;
211 aValues = CharacterAttributesHelper( aFont, sal_Int32(nBackColor), sal_Int32(nColor) )
212 .GetCharacterAttributes( aRequestedAttributes );
215 return aValues;
219 awt::Rectangle VCLXAccessibleTextComponent::getCharacterBounds( sal_Int32 nIndex )
221 OExternalLockGuard aGuard( this );
223 if ( !implIsValidIndex( nIndex, implGetText().getLength() ) )
224 throw IndexOutOfBoundsException();
226 awt::Rectangle aRect;
227 VclPtr< Control > pControl = GetAs< Control >();
228 if ( pControl )
229 aRect = AWTRectangle( pControl->GetCharacterBounds( nIndex ) );
231 return aRect;
235 sal_Int32 VCLXAccessibleTextComponent::getCharacterCount()
237 OExternalLockGuard aGuard( this );
239 return implGetText().getLength();
243 sal_Int32 VCLXAccessibleTextComponent::getIndexAtPoint( const awt::Point& aPoint )
245 OExternalLockGuard aGuard( this );
247 sal_Int32 nIndex = -1;
248 VclPtr< Control > pControl = GetAs< Control >();
249 if ( pControl )
250 nIndex = pControl->GetIndexForPoint( VCLPoint( aPoint ) );
252 return nIndex;
256 OUString VCLXAccessibleTextComponent::getSelectedText()
258 OExternalLockGuard aGuard( this );
260 return OCommonAccessibleText::getSelectedText();
264 sal_Int32 VCLXAccessibleTextComponent::getSelectionStart()
266 OExternalLockGuard aGuard( this );
268 return OCommonAccessibleText::getSelectionStart();
272 sal_Int32 VCLXAccessibleTextComponent::getSelectionEnd()
274 OExternalLockGuard aGuard( this );
276 return OCommonAccessibleText::getSelectionEnd();
280 sal_Bool VCLXAccessibleTextComponent::setSelection( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
282 OExternalLockGuard aGuard( this );
284 if ( !implIsValidRange( nStartIndex, nEndIndex, implGetText().getLength() ) )
285 throw IndexOutOfBoundsException();
287 return false;
291 OUString VCLXAccessibleTextComponent::getText()
293 OExternalLockGuard aGuard( this );
295 return implGetText();
299 OUString VCLXAccessibleTextComponent::getTextRange( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
301 OExternalLockGuard aGuard( this );
303 return OCommonAccessibleText::implGetTextRange( implGetText(), nStartIndex, nEndIndex );
307 css::accessibility::TextSegment VCLXAccessibleTextComponent::getTextAtIndex( sal_Int32 nIndex, sal_Int16 aTextType )
309 OExternalLockGuard aGuard( this );
311 return OCommonAccessibleText::getTextAtIndex( nIndex, aTextType );
315 css::accessibility::TextSegment VCLXAccessibleTextComponent::getTextBeforeIndex( sal_Int32 nIndex, sal_Int16 aTextType )
317 OExternalLockGuard aGuard( this );
319 return OCommonAccessibleText::getTextBeforeIndex( nIndex, aTextType );
323 css::accessibility::TextSegment VCLXAccessibleTextComponent::getTextBehindIndex( sal_Int32 nIndex, sal_Int16 aTextType )
325 OExternalLockGuard aGuard( this );
327 return OCommonAccessibleText::getTextBehindIndex( nIndex, aTextType );
331 sal_Bool VCLXAccessibleTextComponent::copyText( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
333 OExternalLockGuard aGuard( this );
335 bool bReturn = false;
337 VclPtr<vcl::Window> pWindow = GetWindow();
338 if ( pWindow )
340 Reference< datatransfer::clipboard::XClipboard > xClipboard = pWindow->GetClipboard();
341 if ( xClipboard.is() )
343 OUString sText( OCommonAccessibleText::implGetTextRange( implGetText(), nStartIndex, nEndIndex ) );
345 rtl::Reference<vcl::unohelper::TextDataObject> pDataObj = new vcl::unohelper::TextDataObject( sText );
347 SolarMutexReleaser aReleaser;
348 xClipboard->setContents( pDataObj, nullptr );
350 Reference< datatransfer::clipboard::XFlushableClipboard > xFlushableClipboard( xClipboard, uno::UNO_QUERY );
351 if( xFlushableClipboard.is() )
352 xFlushableClipboard->flushClipboard();
354 bReturn = true;
358 return bReturn;
361 sal_Bool VCLXAccessibleTextComponent::scrollSubstringTo( sal_Int32, sal_Int32, AccessibleScrollType )
363 return false;
366 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */