update dev300-m58
[ooovba.git] / accessibility / source / standard / vclxaccessibletextcomponent.cxx
blobc94eb2d05a11af6130f6d08b3d08035e8795e4a0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vclxaccessibletextcomponent.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_accessibility.hxx"
33 #include <accessibility/standard/vclxaccessibletextcomponent.hxx>
34 #include <toolkit/helper/macros.hxx>
35 #include <toolkit/helper/convert.hxx>
36 #include <accessibility/helper/characterattributeshelper.hxx>
38 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
39 #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
40 #include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
41 #include <cppuhelper/typeprovider.hxx>
42 #include <comphelper/sequence.hxx>
43 #include <vcl/window.hxx>
44 #include <vcl/svapp.hxx>
45 #include <vcl/unohelp2.hxx>
46 #include <vcl/ctrl.hxx>
48 #include <memory>
49 #include <vector>
51 using namespace ::com::sun::star;
52 using namespace ::com::sun::star::uno;
53 using namespace ::com::sun::star::lang;
54 using namespace ::com::sun::star::beans;
55 using namespace ::com::sun::star::accessibility;
56 using namespace ::comphelper;
59 // ----------------------------------------------------
60 // class VCLXAccessibleTextComponent
61 // ----------------------------------------------------
63 VCLXAccessibleTextComponent::VCLXAccessibleTextComponent( VCLXWindow* pVCLXWindow )
64 :VCLXAccessibleComponent( pVCLXWindow )
66 if ( GetWindow() )
67 m_sText = OutputDevice::GetNonMnemonicString( GetWindow()->GetText() );
70 // -----------------------------------------------------------------------------
72 VCLXAccessibleTextComponent::~VCLXAccessibleTextComponent()
76 // -----------------------------------------------------------------------------
78 void VCLXAccessibleTextComponent::SetText( const ::rtl::OUString& sText )
80 Any aOldValue, aNewValue;
81 if ( implInitTextChangedEvent( m_sText, sText, aOldValue, aNewValue ) )
83 m_sText = sText;
84 NotifyAccessibleEvent( AccessibleEventId::TEXT_CHANGED, aOldValue, aNewValue );
88 // -----------------------------------------------------------------------------
90 void VCLXAccessibleTextComponent::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
92 switch ( rVclWindowEvent.GetId() )
94 case VCLEVENT_WINDOW_FRAMETITLECHANGED:
96 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
97 SetText( implGetText() );
99 break;
100 default:
101 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
105 // -----------------------------------------------------------------------------
106 // OCommonAccessibleText
107 // -----------------------------------------------------------------------------
109 ::rtl::OUString VCLXAccessibleTextComponent::implGetText()
111 ::rtl::OUString aText;
112 if ( GetWindow() )
113 aText = OutputDevice::GetNonMnemonicString( GetWindow()->GetText() );
115 return aText;
118 // -----------------------------------------------------------------------------
120 lang::Locale VCLXAccessibleTextComponent::implGetLocale()
122 return Application::GetSettings().GetLocale();
125 // -----------------------------------------------------------------------------
127 void VCLXAccessibleTextComponent::implGetSelection( sal_Int32& nStartIndex, sal_Int32& nEndIndex )
129 nStartIndex = 0;
130 nEndIndex = 0;
133 // -----------------------------------------------------------------------------
134 // XComponent
135 // -----------------------------------------------------------------------------
137 void VCLXAccessibleTextComponent::disposing()
139 VCLXAccessibleComponent::disposing();
141 m_sText = ::rtl::OUString();
144 // -----------------------------------------------------------------------------
145 // XInterface
146 // -----------------------------------------------------------------------------
148 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleTextComponent, VCLXAccessibleComponent, VCLXAccessibleTextComponent_BASE )
150 // -----------------------------------------------------------------------------
151 // XTypeProvider
152 // -----------------------------------------------------------------------------
154 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleTextComponent, VCLXAccessibleComponent, VCLXAccessibleTextComponent_BASE )
156 // -----------------------------------------------------------------------------
157 // XAccessibleText
158 // -----------------------------------------------------------------------------
160 sal_Int32 VCLXAccessibleTextComponent::getCaretPosition() throw (RuntimeException)
162 OExternalLockGuard aGuard( this );
164 return -1;
167 // -----------------------------------------------------------------------------
169 sal_Bool VCLXAccessibleTextComponent::setCaretPosition( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
171 OExternalLockGuard aGuard( this );
173 return setSelection( nIndex, nIndex );
176 // -----------------------------------------------------------------------------
178 sal_Unicode VCLXAccessibleTextComponent::getCharacter( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
180 OExternalLockGuard aGuard( this );
182 return OCommonAccessibleText::getCharacter( nIndex );
185 // -----------------------------------------------------------------------------
187 Sequence< PropertyValue > VCLXAccessibleTextComponent::getCharacterAttributes( sal_Int32 nIndex, const Sequence< ::rtl::OUString >& aRequestedAttributes ) throw (IndexOutOfBoundsException, RuntimeException)
189 OExternalLockGuard aGuard( this );
191 Sequence< PropertyValue > aValues;
192 ::rtl::OUString sText( implGetText() );
194 if ( !implIsValidIndex( nIndex, sText.getLength() ) )
195 throw IndexOutOfBoundsException();
197 if ( GetWindow() )
199 Font aFont = GetWindow()->GetControlFont();
200 sal_Int32 nBackColor = GetWindow()->GetControlBackground().GetColor();
201 sal_Int32 nColor = GetWindow()->GetControlForeground().GetColor();
202 ::std::auto_ptr< CharacterAttributesHelper > pHelper( new CharacterAttributesHelper( aFont, nBackColor, nColor ) );
203 aValues = pHelper->GetCharacterAttributes( aRequestedAttributes );
206 return aValues;
209 // -----------------------------------------------------------------------------
211 awt::Rectangle VCLXAccessibleTextComponent::getCharacterBounds( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
213 OExternalLockGuard aGuard( this );
215 if ( !implIsValidIndex( nIndex, implGetText().getLength() ) )
216 throw IndexOutOfBoundsException();
218 awt::Rectangle aRect;
219 Control* pControl = static_cast< Control* >( GetWindow() );
220 if ( pControl )
221 aRect = AWTRectangle( pControl->GetCharacterBounds( nIndex ) );
223 return aRect;
226 // -----------------------------------------------------------------------------
228 sal_Int32 VCLXAccessibleTextComponent::getCharacterCount() throw (RuntimeException)
230 OExternalLockGuard aGuard( this );
232 return OCommonAccessibleText::getCharacterCount();
235 // -----------------------------------------------------------------------------
237 sal_Int32 VCLXAccessibleTextComponent::getIndexAtPoint( const awt::Point& aPoint ) throw (RuntimeException)
239 OExternalLockGuard aGuard( this );
241 sal_Int32 nIndex = -1;
242 Control* pControl = static_cast< Control* >( GetWindow() );
243 if ( pControl )
244 nIndex = pControl->GetIndexForPoint( VCLPoint( aPoint ) );
246 return nIndex;
249 // -----------------------------------------------------------------------------
251 ::rtl::OUString VCLXAccessibleTextComponent::getSelectedText() throw (RuntimeException)
253 OExternalLockGuard aGuard( this );
255 return OCommonAccessibleText::getSelectedText();
258 // -----------------------------------------------------------------------------
260 sal_Int32 VCLXAccessibleTextComponent::getSelectionStart() throw (RuntimeException)
262 OExternalLockGuard aGuard( this );
264 return OCommonAccessibleText::getSelectionStart();
267 // -----------------------------------------------------------------------------
269 sal_Int32 VCLXAccessibleTextComponent::getSelectionEnd() throw (RuntimeException)
271 OExternalLockGuard aGuard( this );
273 return OCommonAccessibleText::getSelectionEnd();
276 // -----------------------------------------------------------------------------
278 sal_Bool VCLXAccessibleTextComponent::setSelection( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException)
280 OExternalLockGuard aGuard( this );
282 if ( !implIsValidRange( nStartIndex, nEndIndex, implGetText().getLength() ) )
283 throw IndexOutOfBoundsException();
285 return sal_False;
288 // -----------------------------------------------------------------------------
290 ::rtl::OUString VCLXAccessibleTextComponent::getText() throw (RuntimeException)
292 OExternalLockGuard aGuard( this );
294 return OCommonAccessibleText::getText();
297 // -----------------------------------------------------------------------------
299 ::rtl::OUString VCLXAccessibleTextComponent::getTextRange( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException)
301 OExternalLockGuard aGuard( this );
303 return OCommonAccessibleText::getTextRange( nStartIndex, nEndIndex );
306 // -----------------------------------------------------------------------------
308 ::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)
310 OExternalLockGuard aGuard( this );
312 return OCommonAccessibleText::getTextAtIndex( nIndex, aTextType );
315 // -----------------------------------------------------------------------------
317 ::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)
319 OExternalLockGuard aGuard( this );
321 return OCommonAccessibleText::getTextBeforeIndex( nIndex, aTextType );
324 // -----------------------------------------------------------------------------
326 ::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)
328 OExternalLockGuard aGuard( this );
330 return OCommonAccessibleText::getTextBehindIndex( nIndex, aTextType );
333 // -----------------------------------------------------------------------------
335 sal_Bool VCLXAccessibleTextComponent::copyText( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException)
337 OExternalLockGuard aGuard( this );
339 sal_Bool bReturn = sal_False;
341 if ( GetWindow() )
343 Reference< datatransfer::clipboard::XClipboard > xClipboard = GetWindow()->GetClipboard();
344 if ( xClipboard.is() )
346 ::rtl::OUString sText( getTextRange( nStartIndex, nEndIndex ) );
348 ::vcl::unohelper::TextDataObject* pDataObj = new ::vcl::unohelper::TextDataObject( sText );
349 const sal_uInt32 nRef = Application::ReleaseSolarMutex();
350 xClipboard->setContents( pDataObj, NULL );
352 Reference< datatransfer::clipboard::XFlushableClipboard > xFlushableClipboard( xClipboard, uno::UNO_QUERY );
353 if( xFlushableClipboard.is() )
354 xFlushableClipboard->flushClipboard();
356 Application::AcquireSolarMutex( nRef );
358 bReturn = sal_True;
362 return bReturn;
365 // -----------------------------------------------------------------------------