1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vclxaccessibletextcomponent.cxx,v $
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>
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
)
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
) )
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() );
101 VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent
);
105 // -----------------------------------------------------------------------------
106 // OCommonAccessibleText
107 // -----------------------------------------------------------------------------
109 ::rtl::OUString
VCLXAccessibleTextComponent::implGetText()
111 ::rtl::OUString aText
;
113 aText
= OutputDevice::GetNonMnemonicString( GetWindow()->GetText() );
118 // -----------------------------------------------------------------------------
120 lang::Locale
VCLXAccessibleTextComponent::implGetLocale()
122 return Application::GetSettings().GetLocale();
125 // -----------------------------------------------------------------------------
127 void VCLXAccessibleTextComponent::implGetSelection( sal_Int32
& nStartIndex
, sal_Int32
& nEndIndex
)
133 // -----------------------------------------------------------------------------
135 // -----------------------------------------------------------------------------
137 void VCLXAccessibleTextComponent::disposing()
139 VCLXAccessibleComponent::disposing();
141 m_sText
= ::rtl::OUString();
144 // -----------------------------------------------------------------------------
146 // -----------------------------------------------------------------------------
148 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleTextComponent
, VCLXAccessibleComponent
, VCLXAccessibleTextComponent_BASE
)
150 // -----------------------------------------------------------------------------
152 // -----------------------------------------------------------------------------
154 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleTextComponent
, VCLXAccessibleComponent
, VCLXAccessibleTextComponent_BASE
)
156 // -----------------------------------------------------------------------------
158 // -----------------------------------------------------------------------------
160 sal_Int32
VCLXAccessibleTextComponent::getCaretPosition() throw (RuntimeException
)
162 OExternalLockGuard
aGuard( this );
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();
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
);
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() );
221 aRect
= AWTRectangle( pControl
->GetCharacterBounds( nIndex
) );
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() );
244 nIndex
= pControl
->GetIndexForPoint( VCLPoint( aPoint
) );
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();
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
;
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
);
365 // -----------------------------------------------------------------------------