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 .
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 <vcl/window.hxx>
29 #include <vcl/svapp.hxx>
30 #include <vcl/unohelp2.hxx>
31 #include <vcl/ctrl.hxx>
32 #include <vcl/settings.hxx>
33 #include <i18nlangtag/languagetag.hxx>
35 using namespace ::com::sun::star
;
36 using namespace ::com::sun::star::uno
;
37 using namespace ::com::sun::star::lang
;
38 using namespace ::com::sun::star::beans
;
39 using namespace ::com::sun::star::accessibility
;
40 using namespace ::comphelper
;
43 // class VCLXAccessibleTextComponent
46 VCLXAccessibleTextComponent::VCLXAccessibleTextComponent( VCLXWindow
* pVCLXWindow
)
47 :VCLXAccessibleComponent( pVCLXWindow
)
49 VclPtr
<vcl::Window
> pWindow
= GetWindow();
51 m_sText
= OutputDevice::GetNonMnemonicString( pWindow
->GetText() );
55 void VCLXAccessibleTextComponent::SetText( const OUString
& sText
)
57 Any aOldValue
, aNewValue
;
58 if ( implInitTextChangedEvent( m_sText
, sText
, aOldValue
, aNewValue
) )
61 NotifyAccessibleEvent( AccessibleEventId::TEXT_CHANGED
, aOldValue
, aNewValue
);
66 void VCLXAccessibleTextComponent::ProcessWindowEvent( const VclWindowEvent
& rVclWindowEvent
)
68 switch ( rVclWindowEvent
.GetId() )
70 case VclEventId::WindowFrameTitleChanged
:
72 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent
);
73 SetText( implGetText() );
77 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent
);
82 // OCommonAccessibleText
85 OUString
VCLXAccessibleTextComponent::implGetText()
88 VclPtr
<vcl::Window
> pWindow
= GetWindow();
90 aText
= OutputDevice::GetNonMnemonicString( pWindow
->GetText() );
96 lang::Locale
VCLXAccessibleTextComponent::implGetLocale()
98 return Application::GetSettings().GetLanguageTag().getLocale();
102 void VCLXAccessibleTextComponent::implGetSelection( sal_Int32
& nStartIndex
, sal_Int32
& nEndIndex
)
112 void VCLXAccessibleTextComponent::disposing()
114 VCLXAccessibleComponent::disposing();
123 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleTextComponent
, VCLXAccessibleComponent
, VCLXAccessibleTextComponent_BASE
)
129 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleTextComponent
, VCLXAccessibleComponent
, VCLXAccessibleTextComponent_BASE
)
135 sal_Int32
VCLXAccessibleTextComponent::getCaretPosition()
141 sal_Bool
VCLXAccessibleTextComponent::setCaretPosition( sal_Int32 nIndex
)
143 return setSelection( nIndex
, nIndex
);
147 sal_Unicode
VCLXAccessibleTextComponent::getCharacter( sal_Int32 nIndex
)
149 OExternalLockGuard
aGuard( this );
151 return OCommonAccessibleText::implGetCharacter( implGetText(), nIndex
);
155 Sequence
< PropertyValue
> VCLXAccessibleTextComponent::getCharacterAttributes( sal_Int32 nIndex
, const Sequence
< OUString
>& aRequestedAttributes
)
157 OExternalLockGuard
aGuard( this );
159 Sequence
< PropertyValue
> aValues
;
160 OUString
sText( implGetText() );
162 if ( !implIsValidIndex( nIndex
, sText
.getLength() ) )
163 throw IndexOutOfBoundsException();
165 VclPtr
<vcl::Window
> pWindow
= GetWindow();
168 vcl::Font aFont
= pWindow
->GetControlFont();
170 Color nBackColor
= pWindow
->GetControlBackground();
171 Color nColor
= pWindow
->GetControlForeground();
173 // MT: Code with default font was introduced with the IA2 CWS, but I am not convinced that this is the correct font...
174 // Decide what to do when we have a concrete issue.
176 Font aDefaultVCLFont;
177 OutputDevice* pDev = Application::GetDefaultDevice();
180 aDefaultVCLFont = pDev->GetSettings().GetStyleSettings().GetAppFont();
181 if ( !aFont.GetName().Len() )
183 String aDefaultName = aDefaultVCLFont.GetName();
184 aFont.SetName( aDefaultName );
186 if ( !aFont.GetHeight() )
188 aFont.SetHeight( aDefaultVCLFont.GetHeight() );
190 if ( aFont.GetWeight() == WEIGHT_DONTKNOW )
192 aFont.SetWeight( aDefaultVCLFont.GetWeight() );
195 //if nColor is -1, it may indicate that the default color black is using.
198 nColor = aDefaultVCLFont.GetColor().GetColor();
203 // MT: Adjustment stuff was introduced with the IA2 CWS, but adjustment is not a character attribute...
204 // In case we reintroduce it, use adjustment as extra parameter for the CharacterAttributesHelper...
206 WinBits aBits = GetWindow()->GetStyle();
207 sal_Int16 nAdjust = -1;
208 if ( aBits & WB_LEFT )
210 nAdjust = style::ParagraphAdjust_LEFT;
212 else if ( aBits & WB_RIGHT )
214 nAdjust = style::ParagraphAdjust_RIGHT;
216 else if ( aBits & WB_CENTER )
218 nAdjust = style::ParagraphAdjust_CENTER;
222 aValues
= CharacterAttributesHelper( aFont
, sal_Int32(nBackColor
), sal_Int32(nColor
) )
223 .GetCharacterAttributes( aRequestedAttributes
);
230 awt::Rectangle
VCLXAccessibleTextComponent::getCharacterBounds( sal_Int32 nIndex
)
232 OExternalLockGuard
aGuard( this );
234 if ( !implIsValidIndex( nIndex
, implGetText().getLength() ) )
235 throw IndexOutOfBoundsException();
237 awt::Rectangle aRect
;
238 VclPtr
< Control
> pControl
= GetAs
< Control
>();
240 aRect
= AWTRectangle( pControl
->GetCharacterBounds( nIndex
) );
246 sal_Int32
VCLXAccessibleTextComponent::getCharacterCount()
248 OExternalLockGuard
aGuard( this );
250 return implGetText().getLength();
254 sal_Int32
VCLXAccessibleTextComponent::getIndexAtPoint( const awt::Point
& aPoint
)
256 OExternalLockGuard
aGuard( this );
258 sal_Int32 nIndex
= -1;
259 VclPtr
< Control
> pControl
= GetAs
< Control
>();
261 nIndex
= pControl
->GetIndexForPoint( VCLPoint( aPoint
) );
267 OUString
VCLXAccessibleTextComponent::getSelectedText()
269 OExternalLockGuard
aGuard( this );
271 return OCommonAccessibleText::getSelectedText();
275 sal_Int32
VCLXAccessibleTextComponent::getSelectionStart()
277 OExternalLockGuard
aGuard( this );
279 return OCommonAccessibleText::getSelectionStart();
283 sal_Int32
VCLXAccessibleTextComponent::getSelectionEnd()
285 OExternalLockGuard
aGuard( this );
287 return OCommonAccessibleText::getSelectionEnd();
291 sal_Bool
VCLXAccessibleTextComponent::setSelection( sal_Int32 nStartIndex
, sal_Int32 nEndIndex
)
293 OExternalLockGuard
aGuard( this );
295 if ( !implIsValidRange( nStartIndex
, nEndIndex
, implGetText().getLength() ) )
296 throw IndexOutOfBoundsException();
302 OUString
VCLXAccessibleTextComponent::getText()
304 OExternalLockGuard
aGuard( this );
306 return implGetText();
310 OUString
VCLXAccessibleTextComponent::getTextRange( sal_Int32 nStartIndex
, sal_Int32 nEndIndex
)
312 OExternalLockGuard
aGuard( this );
314 return OCommonAccessibleText::implGetTextRange( implGetText(), nStartIndex
, nEndIndex
);
318 css::accessibility::TextSegment
VCLXAccessibleTextComponent::getTextAtIndex( sal_Int32 nIndex
, sal_Int16 aTextType
)
320 OExternalLockGuard
aGuard( this );
322 return OCommonAccessibleText::getTextAtIndex( nIndex
, aTextType
);
326 css::accessibility::TextSegment
VCLXAccessibleTextComponent::getTextBeforeIndex( sal_Int32 nIndex
, sal_Int16 aTextType
)
328 OExternalLockGuard
aGuard( this );
330 return OCommonAccessibleText::getTextBeforeIndex( nIndex
, aTextType
);
334 css::accessibility::TextSegment
VCLXAccessibleTextComponent::getTextBehindIndex( sal_Int32 nIndex
, sal_Int16 aTextType
)
336 OExternalLockGuard
aGuard( this );
338 return OCommonAccessibleText::getTextBehindIndex( nIndex
, aTextType
);
342 sal_Bool
VCLXAccessibleTextComponent::copyText( sal_Int32 nStartIndex
, sal_Int32 nEndIndex
)
344 OExternalLockGuard
aGuard( this );
346 bool bReturn
= false;
348 VclPtr
<vcl::Window
> pWindow
= GetWindow();
351 Reference
< datatransfer::clipboard::XClipboard
> xClipboard
= pWindow
->GetClipboard();
352 if ( xClipboard
.is() )
354 OUString
sText( OCommonAccessibleText::implGetTextRange( implGetText(), nStartIndex
, nEndIndex
) );
356 vcl::unohelper::TextDataObject
* pDataObj
= new vcl::unohelper::TextDataObject( sText
);
358 SolarMutexReleaser aReleaser
;
359 xClipboard
->setContents( pDataObj
, nullptr );
361 Reference
< datatransfer::clipboard::XFlushableClipboard
> xFlushableClipboard( xClipboard
, uno::UNO_QUERY
);
362 if( xFlushableClipboard
.is() )
363 xFlushableClipboard
->flushClipboard();
373 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */