merged tag ooo/DEV300_m102
[LibreOffice.git] / accessibility / source / standard / vclxaccessibletextcomponent.cxx
blobc6bbaddf6426b582ebba927ebe933edf7678f683
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_accessibility.hxx"
30 #include <accessibility/standard/vclxaccessibletextcomponent.hxx>
31 #include <toolkit/helper/macros.hxx>
32 #include <toolkit/helper/convert.hxx>
33 #include <accessibility/helper/characterattributeshelper.hxx>
35 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
36 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
37 #include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
38 #include <cppuhelper/typeprovider.hxx>
39 #include <comphelper/sequence.hxx>
40 #include <vcl/window.hxx>
41 #include <vcl/svapp.hxx>
42 #include <vcl/unohelp2.hxx>
43 #include <vcl/ctrl.hxx>
45 #include <memory>
46 #include <vector>
48 using namespace ::com::sun::star;
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::lang;
51 using namespace ::com::sun::star::beans;
52 using namespace ::com::sun::star::accessibility;
53 using namespace ::comphelper;
56 // ----------------------------------------------------
57 // class VCLXAccessibleTextComponent
58 // ----------------------------------------------------
60 VCLXAccessibleTextComponent::VCLXAccessibleTextComponent( VCLXWindow* pVCLXWindow )
61 :VCLXAccessibleComponent( pVCLXWindow )
63 if ( GetWindow() )
64 m_sText = OutputDevice::GetNonMnemonicString( GetWindow()->GetText() );
67 // -----------------------------------------------------------------------------
69 VCLXAccessibleTextComponent::~VCLXAccessibleTextComponent()
73 // -----------------------------------------------------------------------------
75 void VCLXAccessibleTextComponent::SetText( const ::rtl::OUString& sText )
77 Any aOldValue, aNewValue;
78 if ( implInitTextChangedEvent( m_sText, sText, aOldValue, aNewValue ) )
80 m_sText = sText;
81 NotifyAccessibleEvent( AccessibleEventId::TEXT_CHANGED, aOldValue, aNewValue );
85 // -----------------------------------------------------------------------------
87 void VCLXAccessibleTextComponent::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
89 switch ( rVclWindowEvent.GetId() )
91 case VCLEVENT_WINDOW_FRAMETITLECHANGED:
93 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
94 SetText( implGetText() );
96 break;
97 default:
98 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
102 // -----------------------------------------------------------------------------
103 // OCommonAccessibleText
104 // -----------------------------------------------------------------------------
106 ::rtl::OUString VCLXAccessibleTextComponent::implGetText()
108 ::rtl::OUString aText;
109 if ( GetWindow() )
110 aText = OutputDevice::GetNonMnemonicString( GetWindow()->GetText() );
112 return aText;
115 // -----------------------------------------------------------------------------
117 lang::Locale VCLXAccessibleTextComponent::implGetLocale()
119 return Application::GetSettings().GetLocale();
122 // -----------------------------------------------------------------------------
124 void VCLXAccessibleTextComponent::implGetSelection( sal_Int32& nStartIndex, sal_Int32& nEndIndex )
126 nStartIndex = 0;
127 nEndIndex = 0;
130 // -----------------------------------------------------------------------------
131 // XComponent
132 // -----------------------------------------------------------------------------
134 void VCLXAccessibleTextComponent::disposing()
136 VCLXAccessibleComponent::disposing();
138 m_sText = ::rtl::OUString();
141 // -----------------------------------------------------------------------------
142 // XInterface
143 // -----------------------------------------------------------------------------
145 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleTextComponent, VCLXAccessibleComponent, VCLXAccessibleTextComponent_BASE )
147 // -----------------------------------------------------------------------------
148 // XTypeProvider
149 // -----------------------------------------------------------------------------
151 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleTextComponent, VCLXAccessibleComponent, VCLXAccessibleTextComponent_BASE )
153 // -----------------------------------------------------------------------------
154 // XAccessibleText
155 // -----------------------------------------------------------------------------
157 sal_Int32 VCLXAccessibleTextComponent::getCaretPosition() throw (RuntimeException)
159 OExternalLockGuard aGuard( this );
161 return -1;
164 // -----------------------------------------------------------------------------
166 sal_Bool VCLXAccessibleTextComponent::setCaretPosition( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
168 OExternalLockGuard aGuard( this );
170 return setSelection( nIndex, nIndex );
173 // -----------------------------------------------------------------------------
175 sal_Unicode VCLXAccessibleTextComponent::getCharacter( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
177 OExternalLockGuard aGuard( this );
179 return OCommonAccessibleText::getCharacter( nIndex );
182 // -----------------------------------------------------------------------------
184 Sequence< PropertyValue > VCLXAccessibleTextComponent::getCharacterAttributes( sal_Int32 nIndex, const Sequence< ::rtl::OUString >& aRequestedAttributes ) throw (IndexOutOfBoundsException, RuntimeException)
186 OExternalLockGuard aGuard( this );
188 Sequence< PropertyValue > aValues;
189 ::rtl::OUString sText( implGetText() );
191 if ( !implIsValidIndex( nIndex, sText.getLength() ) )
192 throw IndexOutOfBoundsException();
194 if ( GetWindow() )
196 Font aFont = GetWindow()->GetControlFont();
197 sal_Int32 nBackColor = GetWindow()->GetControlBackground().GetColor();
198 sal_Int32 nColor = GetWindow()->GetControlForeground().GetColor();
199 ::std::auto_ptr< CharacterAttributesHelper > pHelper( new CharacterAttributesHelper( aFont, nBackColor, nColor ) );
200 aValues = pHelper->GetCharacterAttributes( aRequestedAttributes );
203 return aValues;
206 // -----------------------------------------------------------------------------
208 awt::Rectangle VCLXAccessibleTextComponent::getCharacterBounds( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
210 OExternalLockGuard aGuard( this );
212 if ( !implIsValidIndex( nIndex, implGetText().getLength() ) )
213 throw IndexOutOfBoundsException();
215 awt::Rectangle aRect;
216 Control* pControl = static_cast< Control* >( GetWindow() );
217 if ( pControl )
218 aRect = AWTRectangle( pControl->GetCharacterBounds( nIndex ) );
220 return aRect;
223 // -----------------------------------------------------------------------------
225 sal_Int32 VCLXAccessibleTextComponent::getCharacterCount() throw (RuntimeException)
227 OExternalLockGuard aGuard( this );
229 return OCommonAccessibleText::getCharacterCount();
232 // -----------------------------------------------------------------------------
234 sal_Int32 VCLXAccessibleTextComponent::getIndexAtPoint( const awt::Point& aPoint ) throw (RuntimeException)
236 OExternalLockGuard aGuard( this );
238 sal_Int32 nIndex = -1;
239 Control* pControl = static_cast< Control* >( GetWindow() );
240 if ( pControl )
241 nIndex = pControl->GetIndexForPoint( VCLPoint( aPoint ) );
243 return nIndex;
246 // -----------------------------------------------------------------------------
248 ::rtl::OUString VCLXAccessibleTextComponent::getSelectedText() throw (RuntimeException)
250 OExternalLockGuard aGuard( this );
252 return OCommonAccessibleText::getSelectedText();
255 // -----------------------------------------------------------------------------
257 sal_Int32 VCLXAccessibleTextComponent::getSelectionStart() throw (RuntimeException)
259 OExternalLockGuard aGuard( this );
261 return OCommonAccessibleText::getSelectionStart();
264 // -----------------------------------------------------------------------------
266 sal_Int32 VCLXAccessibleTextComponent::getSelectionEnd() throw (RuntimeException)
268 OExternalLockGuard aGuard( this );
270 return OCommonAccessibleText::getSelectionEnd();
273 // -----------------------------------------------------------------------------
275 sal_Bool VCLXAccessibleTextComponent::setSelection( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException)
277 OExternalLockGuard aGuard( this );
279 if ( !implIsValidRange( nStartIndex, nEndIndex, implGetText().getLength() ) )
280 throw IndexOutOfBoundsException();
282 return sal_False;
285 // -----------------------------------------------------------------------------
287 ::rtl::OUString VCLXAccessibleTextComponent::getText() throw (RuntimeException)
289 OExternalLockGuard aGuard( this );
291 return OCommonAccessibleText::getText();
294 // -----------------------------------------------------------------------------
296 ::rtl::OUString VCLXAccessibleTextComponent::getTextRange( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException)
298 OExternalLockGuard aGuard( this );
300 return OCommonAccessibleText::getTextRange( nStartIndex, nEndIndex );
303 // -----------------------------------------------------------------------------
305 ::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)
307 OExternalLockGuard aGuard( this );
309 return OCommonAccessibleText::getTextAtIndex( nIndex, aTextType );
312 // -----------------------------------------------------------------------------
314 ::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)
316 OExternalLockGuard aGuard( this );
318 return OCommonAccessibleText::getTextBeforeIndex( nIndex, aTextType );
321 // -----------------------------------------------------------------------------
323 ::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)
325 OExternalLockGuard aGuard( this );
327 return OCommonAccessibleText::getTextBehindIndex( nIndex, aTextType );
330 // -----------------------------------------------------------------------------
332 sal_Bool VCLXAccessibleTextComponent::copyText( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException)
334 OExternalLockGuard aGuard( this );
336 sal_Bool bReturn = sal_False;
338 if ( GetWindow() )
340 Reference< datatransfer::clipboard::XClipboard > xClipboard = GetWindow()->GetClipboard();
341 if ( xClipboard.is() )
343 ::rtl::OUString sText( getTextRange( nStartIndex, nEndIndex ) );
345 ::vcl::unohelper::TextDataObject* pDataObj = new ::vcl::unohelper::TextDataObject( sText );
346 const sal_uInt32 nRef = Application::ReleaseSolarMutex();
347 xClipboard->setContents( pDataObj, NULL );
349 Reference< datatransfer::clipboard::XFlushableClipboard > xFlushableClipboard( xClipboard, uno::UNO_QUERY );
350 if( xFlushableClipboard.is() )
351 xFlushableClipboard->flushClipboard();
353 Application::AcquireSolarMutex( nRef );
355 bReturn = sal_True;
359 return bReturn;
362 // -----------------------------------------------------------------------------