tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / accessibility / source / standard / vclxaccessibletextcomponent.cxx
blobf3c82134939ab8b802e9e146e8882540fe4d4dae
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>
22 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
23 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
24 #include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
25 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
26 #include <comphelper/accessiblecontexthelper.hxx>
27 #include <vcl/accessibility/characterattributeshelper.hxx>
28 #include <vcl/window.hxx>
29 #include <vcl/mnemonic.hxx>
30 #include <vcl/svapp.hxx>
31 #include <vcl/unohelp2.hxx>
32 #include <vcl/ctrl.hxx>
33 #include <vcl/settings.hxx>
34 #include <vcl/unohelp.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(vcl::Window* pWindow)
48 : ImplInheritanceHelper(pWindow)
50 if ( pWindow )
51 m_sText = removeMnemonicFromString( pWindow->GetText() );
55 void VCLXAccessibleTextComponent::SetText( const OUString& sText )
57 Any aOldValue, aNewValue;
58 bool bChanged = false;
59 if (!PreferFullTextInTextChangedEvent())
61 bChanged = implInitTextChangedEvent(m_sText, sText, aOldValue, aNewValue);
64 else if (sText != m_sText)
66 // use the full old/new text as old/new value
67 TextSegment aDeletedText(m_sText, 0, m_sText.getLength());
68 aOldValue <<= aDeletedText;
69 TextSegment aInsertedText(sText, 0, sText.getLength());
70 aNewValue <<= aInsertedText;
71 bChanged = true;
74 if (bChanged)
76 m_sText = sText;
77 NotifyAccessibleEvent( AccessibleEventId::TEXT_CHANGED, aOldValue, aNewValue );
80 // check whether accessible name has also changed, since text is (often) used as name as well
81 const OUString sName = getAccessibleName();
82 if (sName != m_sOldName)
84 NotifyAccessibleEvent(AccessibleEventId::NAME_CHANGED, Any(m_sOldName), Any(sName));
85 m_sOldName = sName;
90 void VCLXAccessibleTextComponent::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
92 switch ( rVclWindowEvent.GetId() )
94 case VclEventId::WindowFrameTitleChanged:
96 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
97 SetText( implGetText() );
99 break;
100 default:
101 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
106 // OCommonAccessibleText
109 OUString VCLXAccessibleTextComponent::implGetText()
111 OUString aText;
112 VclPtr<vcl::Window> pWindow = GetWindow();
113 if ( pWindow )
114 aText = removeMnemonicFromString( pWindow->GetText() );
116 return aText;
120 lang::Locale VCLXAccessibleTextComponent::implGetLocale()
122 return Application::GetSettings().GetLanguageTag().getLocale();
126 void VCLXAccessibleTextComponent::implGetSelection( sal_Int32& nStartIndex, sal_Int32& nEndIndex )
128 nStartIndex = 0;
129 nEndIndex = 0;
133 // XComponent
136 void VCLXAccessibleTextComponent::disposing()
138 VCLXAccessibleComponent::disposing();
140 m_sText.clear();
144 // XAccessibleText
147 sal_Int32 VCLXAccessibleTextComponent::getCaretPosition()
149 return -1;
153 sal_Bool VCLXAccessibleTextComponent::setCaretPosition( sal_Int32 nIndex )
155 return setSelection( nIndex, nIndex );
159 sal_Unicode VCLXAccessibleTextComponent::getCharacter( sal_Int32 nIndex )
161 OExternalLockGuard aGuard( this );
163 return OCommonAccessibleText::implGetCharacter( implGetText(), nIndex );
167 Sequence< PropertyValue > VCLXAccessibleTextComponent::getCharacterAttributes( sal_Int32 nIndex, const Sequence< OUString >& aRequestedAttributes )
169 OExternalLockGuard aGuard( this );
171 Sequence< PropertyValue > aValues;
172 OUString sText( implGetText() );
174 if ( !implIsValidIndex( nIndex, sText.getLength() ) )
175 throw IndexOutOfBoundsException();
177 VclPtr<vcl::Window> pWindow = GetWindow();
178 if ( pWindow )
180 vcl::Font aFont = pWindow->GetControlFont();
182 Color nBackColor = pWindow->GetControlBackground();
183 Color nColor = pWindow->GetControlForeground();
185 // MT: Code with default font was introduced with the IA2 CWS, but I am not convinced that this is the correct font...
186 // Decide what to do when we have a concrete issue.
188 Font aDefaultVCLFont;
189 OutputDevice* pDev = Application::GetDefaultDevice();
190 if ( pDev )
192 aDefaultVCLFont = pDev->GetSettings().GetStyleSettings().GetAppFont();
193 if ( !aFont.GetName().Len() )
195 String aDefaultName = aDefaultVCLFont.GetName();
196 aFont.SetName( aDefaultName );
198 if ( !aFont.GetHeight() )
200 aFont.SetHeight( aDefaultVCLFont.GetHeight() );
202 if ( aFont.GetWeight() == WEIGHT_DONTKNOW )
204 aFont.SetWeight( aDefaultVCLFont.GetWeight() );
207 //if nColor is -1, it may indicate that the default color black is using.
208 if ( nColor == -1)
210 nColor = aDefaultVCLFont.GetColor().GetColor();
215 // MT: Adjustment stuff was introduced with the IA2 CWS, but adjustment is not a character attribute...
216 // In case we reintroduce it, use adjustment as extra parameter for the CharacterAttributesHelper...
218 WinBits aBits = GetWindow()->GetStyle();
219 sal_Int16 nAdjust = -1;
220 if ( aBits & WB_LEFT )
222 nAdjust = style::ParagraphAdjust_LEFT;
224 else if ( aBits & WB_RIGHT )
226 nAdjust = style::ParagraphAdjust_RIGHT;
228 else if ( aBits & WB_CENTER )
230 nAdjust = style::ParagraphAdjust_CENTER;
234 aValues = CharacterAttributesHelper( aFont, sal_Int32(nBackColor), sal_Int32(nColor) )
235 .GetCharacterAttributes( aRequestedAttributes );
238 return aValues;
242 awt::Rectangle VCLXAccessibleTextComponent::getCharacterBounds( sal_Int32 nIndex )
244 OExternalLockGuard aGuard( this );
246 if ( !implIsValidIndex( nIndex, implGetText().getLength() ) )
247 throw IndexOutOfBoundsException();
249 awt::Rectangle aRect;
250 VclPtr< Control > pControl = GetAs< Control >();
251 if ( pControl )
252 aRect = vcl::unohelper::ConvertToAWTRect(pControl->GetCharacterBounds(nIndex));
254 return aRect;
258 sal_Int32 VCLXAccessibleTextComponent::getCharacterCount()
260 OExternalLockGuard aGuard( this );
262 return implGetText().getLength();
266 sal_Int32 VCLXAccessibleTextComponent::getIndexAtPoint( const awt::Point& aPoint )
268 OExternalLockGuard aGuard( this );
270 sal_Int32 nIndex = -1;
271 VclPtr< Control > pControl = GetAs< Control >();
272 if ( pControl )
273 nIndex = pControl->GetIndexForPoint(vcl::unohelper::ConvertToVCLPoint(aPoint));
275 return nIndex;
279 OUString VCLXAccessibleTextComponent::getSelectedText()
281 OExternalLockGuard aGuard( this );
283 return OCommonAccessibleText::getSelectedText();
287 sal_Int32 VCLXAccessibleTextComponent::getSelectionStart()
289 OExternalLockGuard aGuard( this );
291 return OCommonAccessibleText::getSelectionStart();
295 sal_Int32 VCLXAccessibleTextComponent::getSelectionEnd()
297 OExternalLockGuard aGuard( this );
299 return OCommonAccessibleText::getSelectionEnd();
303 sal_Bool VCLXAccessibleTextComponent::setSelection( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
305 OExternalLockGuard aGuard( this );
307 if ( !implIsValidRange( nStartIndex, nEndIndex, implGetText().getLength() ) )
308 throw IndexOutOfBoundsException();
310 return false;
314 OUString VCLXAccessibleTextComponent::getText()
316 OExternalLockGuard aGuard( this );
318 return implGetText();
322 OUString VCLXAccessibleTextComponent::getTextRange( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
324 OExternalLockGuard aGuard( this );
326 return OCommonAccessibleText::implGetTextRange( implGetText(), nStartIndex, nEndIndex );
330 css::accessibility::TextSegment VCLXAccessibleTextComponent::getTextAtIndex( sal_Int32 nIndex, sal_Int16 aTextType )
332 OExternalLockGuard aGuard( this );
334 return OCommonAccessibleText::getTextAtIndex( nIndex, aTextType );
338 css::accessibility::TextSegment VCLXAccessibleTextComponent::getTextBeforeIndex( sal_Int32 nIndex, sal_Int16 aTextType )
340 OExternalLockGuard aGuard( this );
342 return OCommonAccessibleText::getTextBeforeIndex( nIndex, aTextType );
346 css::accessibility::TextSegment VCLXAccessibleTextComponent::getTextBehindIndex( sal_Int32 nIndex, sal_Int16 aTextType )
348 OExternalLockGuard aGuard( this );
350 return OCommonAccessibleText::getTextBehindIndex( nIndex, aTextType );
354 sal_Bool VCLXAccessibleTextComponent::copyText( sal_Int32 nStartIndex, sal_Int32 nEndIndex )
356 OExternalLockGuard aGuard( this );
358 bool bReturn = false;
360 VclPtr<vcl::Window> pWindow = GetWindow();
361 if ( pWindow )
363 Reference< datatransfer::clipboard::XClipboard > xClipboard = pWindow->GetClipboard();
364 if ( xClipboard.is() )
366 OUString sText( OCommonAccessibleText::implGetTextRange( implGetText(), nStartIndex, nEndIndex ) );
368 rtl::Reference<vcl::unohelper::TextDataObject> pDataObj = new vcl::unohelper::TextDataObject( sText );
370 SolarMutexReleaser aReleaser;
371 xClipboard->setContents( pDataObj, nullptr );
373 Reference< datatransfer::clipboard::XFlushableClipboard > xFlushableClipboard( xClipboard, uno::UNO_QUERY );
374 if( xFlushableClipboard.is() )
375 xFlushableClipboard->flushClipboard();
377 bReturn = true;
381 return bReturn;
384 sal_Bool VCLXAccessibleTextComponent::scrollSubstringTo( sal_Int32, sal_Int32, AccessibleScrollType )
386 return false;
389 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */