Branch libreoffice-5-0-4
[LibreOffice.git] / accessibility / source / standard / vclxaccessibletextcomponent.cxx
blob1050cdbc747baaf8fdb86adb00998255dd393538
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 <accessibility/standard/vclxaccessibletextcomponent.hxx>
21 #include <toolkit/helper/macros.hxx>
22 #include <toolkit/helper/convert.hxx>
23 #include <accessibility/helper/characterattributeshelper.hxx>
25 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
26 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
27 #include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
28 #include <cppuhelper/typeprovider.hxx>
29 #include <comphelper/sequence.hxx>
30 #include <vcl/window.hxx>
31 #include <vcl/svapp.hxx>
32 #include <vcl/unohelp2.hxx>
33 #include <vcl/ctrl.hxx>
34 #include <vcl/settings.hxx>
36 #include <vector>
38 using namespace ::com::sun::star;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::lang;
41 using namespace ::com::sun::star::beans;
42 using namespace ::com::sun::star::accessibility;
43 using namespace ::comphelper;
47 // class VCLXAccessibleTextComponent
50 VCLXAccessibleTextComponent::VCLXAccessibleTextComponent( VCLXWindow* pVCLXWindow )
51 :VCLXAccessibleComponent( pVCLXWindow )
53 if ( GetWindow() )
54 m_sText = OutputDevice::GetNonMnemonicString( GetWindow()->GetText() );
59 VCLXAccessibleTextComponent::~VCLXAccessibleTextComponent()
65 void VCLXAccessibleTextComponent::SetText( const OUString& sText )
67 Any aOldValue, aNewValue;
68 if ( implInitTextChangedEvent( m_sText, sText, aOldValue, aNewValue ) )
70 m_sText = sText;
71 NotifyAccessibleEvent( AccessibleEventId::TEXT_CHANGED, aOldValue, aNewValue );
77 void VCLXAccessibleTextComponent::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
79 switch ( rVclWindowEvent.GetId() )
81 case VCLEVENT_WINDOW_FRAMETITLECHANGED:
83 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
84 SetText( implGetText() );
86 break;
87 default:
88 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
93 // OCommonAccessibleText
96 OUString VCLXAccessibleTextComponent::implGetText()
98 OUString aText;
99 if ( GetWindow() )
100 aText = OutputDevice::GetNonMnemonicString( GetWindow()->GetText() );
102 return aText;
107 lang::Locale VCLXAccessibleTextComponent::implGetLocale()
109 return Application::GetSettings().GetLanguageTag().getLocale();
114 void VCLXAccessibleTextComponent::implGetSelection( sal_Int32& nStartIndex, sal_Int32& nEndIndex )
116 nStartIndex = 0;
117 nEndIndex = 0;
121 // XComponent
124 void VCLXAccessibleTextComponent::disposing()
126 VCLXAccessibleComponent::disposing();
128 m_sText.clear();
132 // XInterface
135 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleTextComponent, VCLXAccessibleComponent, VCLXAccessibleTextComponent_BASE )
138 // XTypeProvider
141 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleTextComponent, VCLXAccessibleComponent, VCLXAccessibleTextComponent_BASE )
144 // XAccessibleText
147 sal_Int32 VCLXAccessibleTextComponent::getCaretPosition() throw (RuntimeException, std::exception)
149 OExternalLockGuard aGuard( this );
151 return -1;
156 sal_Bool VCLXAccessibleTextComponent::setCaretPosition( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
158 OExternalLockGuard aGuard( this );
160 return setSelection( nIndex, nIndex );
165 sal_Unicode VCLXAccessibleTextComponent::getCharacter( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
167 OExternalLockGuard aGuard( this );
169 return OCommonAccessibleText::getCharacter( nIndex );
174 Sequence< PropertyValue > VCLXAccessibleTextComponent::getCharacterAttributes( sal_Int32 nIndex, const Sequence< OUString >& aRequestedAttributes ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
176 OExternalLockGuard aGuard( this );
178 Sequence< PropertyValue > aValues;
179 OUString sText( implGetText() );
181 if ( !implIsValidIndex( nIndex, sText.getLength() ) )
182 throw IndexOutOfBoundsException();
184 if ( GetWindow() )
186 vcl::Font aFont = GetWindow()->GetControlFont();
188 sal_Int32 nBackColor = GetWindow()->GetControlBackground().GetColor();
189 sal_Int32 nColor = GetWindow()->GetControlForeground().GetColor();
191 // MT: Code with default font was introduced with the IA2 CWS, but I am not convinced that this is the correct font...
192 // Decide what to do when we have a concrete issue.
194 Font aDefaultVCLFont;
195 OutputDevice* pDev = Application::GetDefaultDevice();
196 if ( pDev )
198 aDefaultVCLFont = pDev->GetSettings().GetStyleSettings().GetAppFont();
199 if ( !aFont.GetName().Len() )
201 String aDefaultName = aDefaultVCLFont.GetName();
202 aFont.SetName( aDefaultName );
204 if ( !aFont.GetHeight() )
206 aFont.SetHeight( aDefaultVCLFont.GetHeight() );
208 if ( aFont.GetWeight() == WEIGHT_DONTKNOW )
210 aFont.SetWeight( aDefaultVCLFont.GetWeight() );
213 //if nColor is -1, it may indicate that the default color black is using.
214 if ( nColor == -1)
216 nColor = aDefaultVCLFont.GetColor().GetColor();
221 // MT: Adjustment stuff was introduced with the IA2 CWS, but adjustment is not a character attribute...
222 // In case we reintroduce it, use adjustment as extra parameter for the CharacterAttributesHelper...
224 WinBits aBits = GetWindow()->GetStyle();
225 sal_Int16 nAdjust = -1;
226 if ( aBits & WB_LEFT )
228 nAdjust = style::ParagraphAdjust_LEFT;
230 else if ( aBits & WB_RIGHT )
232 nAdjust = style::ParagraphAdjust_RIGHT;
234 else if ( aBits & WB_CENTER )
236 nAdjust = style::ParagraphAdjust_CENTER;
240 aValues = CharacterAttributesHelper( aFont, nBackColor, nColor )
241 .GetCharacterAttributes( aRequestedAttributes );
244 return aValues;
249 awt::Rectangle VCLXAccessibleTextComponent::getCharacterBounds( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
251 OExternalLockGuard aGuard( this );
253 if ( !implIsValidIndex( nIndex, implGetText().getLength() ) )
254 throw IndexOutOfBoundsException();
256 awt::Rectangle aRect;
257 VclPtr< Control > pControl = GetAs< Control >();
258 if ( pControl )
259 aRect = AWTRectangle( pControl->GetCharacterBounds( nIndex ) );
261 return aRect;
266 sal_Int32 VCLXAccessibleTextComponent::getCharacterCount() throw (RuntimeException, std::exception)
268 OExternalLockGuard aGuard( this );
270 return OCommonAccessibleText::getCharacterCount();
275 sal_Int32 VCLXAccessibleTextComponent::getIndexAtPoint( const awt::Point& aPoint ) throw (RuntimeException, std::exception)
277 OExternalLockGuard aGuard( this );
279 sal_Int32 nIndex = -1;
280 VclPtr< Control > pControl = GetAs< Control >();
281 if ( pControl )
282 nIndex = pControl->GetIndexForPoint( VCLPoint( aPoint ) );
284 return nIndex;
289 OUString VCLXAccessibleTextComponent::getSelectedText() throw (RuntimeException, std::exception)
291 OExternalLockGuard aGuard( this );
293 return OCommonAccessibleText::getSelectedText();
298 sal_Int32 VCLXAccessibleTextComponent::getSelectionStart() throw (RuntimeException, std::exception)
300 OExternalLockGuard aGuard( this );
302 return OCommonAccessibleText::getSelectionStart();
307 sal_Int32 VCLXAccessibleTextComponent::getSelectionEnd() throw (RuntimeException, std::exception)
309 OExternalLockGuard aGuard( this );
311 return OCommonAccessibleText::getSelectionEnd();
316 sal_Bool VCLXAccessibleTextComponent::setSelection( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
318 OExternalLockGuard aGuard( this );
320 if ( !implIsValidRange( nStartIndex, nEndIndex, implGetText().getLength() ) )
321 throw IndexOutOfBoundsException();
323 return false;
328 OUString VCLXAccessibleTextComponent::getText() throw (RuntimeException, std::exception)
330 OExternalLockGuard aGuard( this );
332 return OCommonAccessibleText::getText();
337 OUString VCLXAccessibleTextComponent::getTextRange( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
339 OExternalLockGuard aGuard( this );
341 return OCommonAccessibleText::getTextRange( nStartIndex, nEndIndex );
346 ::com::sun::star::accessibility::TextSegment VCLXAccessibleTextComponent::getTextAtIndex( sal_Int32 nIndex, sal_Int16 aTextType ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException, std::exception)
348 OExternalLockGuard aGuard( this );
350 return OCommonAccessibleText::getTextAtIndex( nIndex, aTextType );
355 ::com::sun::star::accessibility::TextSegment VCLXAccessibleTextComponent::getTextBeforeIndex( sal_Int32 nIndex, sal_Int16 aTextType ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException, std::exception)
357 OExternalLockGuard aGuard( this );
359 return OCommonAccessibleText::getTextBeforeIndex( nIndex, aTextType );
364 ::com::sun::star::accessibility::TextSegment VCLXAccessibleTextComponent::getTextBehindIndex( sal_Int32 nIndex, sal_Int16 aTextType ) throw (::com::sun::star::lang::IndexOutOfBoundsException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException, std::exception)
366 OExternalLockGuard aGuard( this );
368 return OCommonAccessibleText::getTextBehindIndex( nIndex, aTextType );
373 sal_Bool VCLXAccessibleTextComponent::copyText( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
375 OExternalLockGuard aGuard( this );
377 bool bReturn = false;
379 if ( GetWindow() )
381 Reference< datatransfer::clipboard::XClipboard > xClipboard = GetWindow()->GetClipboard();
382 if ( xClipboard.is() )
384 OUString sText( getTextRange( nStartIndex, nEndIndex ) );
386 vcl::unohelper::TextDataObject* pDataObj = new vcl::unohelper::TextDataObject( sText );
388 SolarMutexReleaser aReleaser;
389 xClipboard->setContents( pDataObj, NULL );
391 Reference< datatransfer::clipboard::XFlushableClipboard > xFlushableClipboard( xClipboard, uno::UNO_QUERY );
392 if( xFlushableClipboard.is() )
393 xFlushableClipboard->flushClipboard();
395 bReturn = true;
399 return bReturn;
404 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */