1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
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>
34 
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>
44 
45 #include <memory>
46 #include <vector>
47 
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;
54 
55 
56 //	----------------------------------------------------
57 //	class VCLXAccessibleTextComponent
58 //	----------------------------------------------------
59 
60 VCLXAccessibleTextComponent::VCLXAccessibleTextComponent( VCLXWindow* pVCLXWindow )
61 	:VCLXAccessibleComponent( pVCLXWindow )
62 {
63     if ( GetWindow() )
64 		m_sText = OutputDevice::GetNonMnemonicString( GetWindow()->GetText() );
65 }
66 
67 // -----------------------------------------------------------------------------
68 
69 VCLXAccessibleTextComponent::~VCLXAccessibleTextComponent()
70 {
71 }
72 
73 // -----------------------------------------------------------------------------
74 
75 void VCLXAccessibleTextComponent::SetText( const ::rtl::OUString& sText )
76 {
77 	Any aOldValue, aNewValue;
78     if ( implInitTextChangedEvent( m_sText, sText, aOldValue, aNewValue ) )
79     {
80         m_sText = sText;
81 		NotifyAccessibleEvent( AccessibleEventId::TEXT_CHANGED, aOldValue, aNewValue );
82     }
83 }
84 
85 // -----------------------------------------------------------------------------
86 
87 void VCLXAccessibleTextComponent::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
88 {
89     switch ( rVclWindowEvent.GetId() )
90     {
91         case VCLEVENT_WINDOW_FRAMETITLECHANGED:
92         {
93 			VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
94             SetText( implGetText() );
95         }
96         break;
97 		default:
98 			VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
99    }
100 }
101 
102 // -----------------------------------------------------------------------------
103 // OCommonAccessibleText
104 // -----------------------------------------------------------------------------
105 
106 ::rtl::OUString VCLXAccessibleTextComponent::implGetText()
107 {
108     ::rtl::OUString aText;
109     if ( GetWindow() )
110 		aText = OutputDevice::GetNonMnemonicString( GetWindow()->GetText() );
111 
112     return aText;
113 }
114 
115 // -----------------------------------------------------------------------------
116 
117 lang::Locale VCLXAccessibleTextComponent::implGetLocale()
118 {
119 	return Application::GetSettings().GetLocale();
120 }
121 
122 // -----------------------------------------------------------------------------
123 
124 void VCLXAccessibleTextComponent::implGetSelection( sal_Int32& nStartIndex, sal_Int32& nEndIndex )
125 {
126 	nStartIndex = 0;
127 	nEndIndex = 0;
128 }
129 
130 // -----------------------------------------------------------------------------
131 // XComponent
132 // -----------------------------------------------------------------------------
133 
134 void VCLXAccessibleTextComponent::disposing()
135 {
136 	VCLXAccessibleComponent::disposing();
137 
138 	m_sText = ::rtl::OUString();
139 }
140 
141 // -----------------------------------------------------------------------------
142 // XInterface
143 // -----------------------------------------------------------------------------
144 
145 IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleTextComponent, VCLXAccessibleComponent, VCLXAccessibleTextComponent_BASE )
146 
147 // -----------------------------------------------------------------------------
148 // XTypeProvider
149 // -----------------------------------------------------------------------------
150 
151 IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleTextComponent, VCLXAccessibleComponent, VCLXAccessibleTextComponent_BASE )
152 
153 // -----------------------------------------------------------------------------
154 // XAccessibleText
155 // -----------------------------------------------------------------------------
156 
157 sal_Int32 VCLXAccessibleTextComponent::getCaretPosition() throw (RuntimeException)
158 {
159 	OExternalLockGuard aGuard( this );
160 
161 	return -1;
162 }
163 
164 // -----------------------------------------------------------------------------
165 
166 sal_Bool VCLXAccessibleTextComponent::setCaretPosition( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
167 {
168 	OExternalLockGuard aGuard( this );
169 
170 	return setSelection( nIndex, nIndex );
171 }
172 
173 // -----------------------------------------------------------------------------
174 
175 sal_Unicode VCLXAccessibleTextComponent::getCharacter( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
176 {
177 	OExternalLockGuard aGuard( this );
178 
179 	return OCommonAccessibleText::getCharacter( nIndex );
180 }
181 
182 // -----------------------------------------------------------------------------
183 
184 Sequence< PropertyValue > VCLXAccessibleTextComponent::getCharacterAttributes( sal_Int32 nIndex, const Sequence< ::rtl::OUString >& aRequestedAttributes ) throw (IndexOutOfBoundsException, RuntimeException)
185 {
186 	OExternalLockGuard aGuard( this );
187 
188 	Sequence< PropertyValue > aValues;
189 	::rtl::OUString sText( implGetText() );
190 
191     if ( !implIsValidIndex( nIndex, sText.getLength() ) )
192         throw IndexOutOfBoundsException();
193 
194     if ( GetWindow() )
195     {
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 );
201     }
202 
203     return aValues;
204 }
205 
206 // -----------------------------------------------------------------------------
207 
208 awt::Rectangle VCLXAccessibleTextComponent::getCharacterBounds( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
209 {
210 	OExternalLockGuard aGuard( this );
211 
212     if ( !implIsValidIndex( nIndex, implGetText().getLength() ) )
213         throw IndexOutOfBoundsException();
214 
215 	awt::Rectangle aRect;
216 	Control* pControl = static_cast< Control* >( GetWindow() );
217 	if ( pControl )
218 		aRect = AWTRectangle( pControl->GetCharacterBounds( nIndex ) );
219 
220     return aRect;
221 }
222 
223 // -----------------------------------------------------------------------------
224 
225 sal_Int32 VCLXAccessibleTextComponent::getCharacterCount() throw (RuntimeException)
226 {
227 	OExternalLockGuard aGuard( this );
228 
229 	return OCommonAccessibleText::getCharacterCount();
230 }
231 
232 // -----------------------------------------------------------------------------
233 
234 sal_Int32 VCLXAccessibleTextComponent::getIndexAtPoint( const awt::Point& aPoint ) throw (RuntimeException)
235 {
236 	OExternalLockGuard aGuard( this );
237 
238 	sal_Int32 nIndex = -1;
239 	Control* pControl = static_cast< Control* >( GetWindow() );
240 	if ( pControl )
241 		nIndex = pControl->GetIndexForPoint( VCLPoint( aPoint ) );
242 
243     return nIndex;
244 }
245 
246 // -----------------------------------------------------------------------------
247 
248 ::rtl::OUString VCLXAccessibleTextComponent::getSelectedText() throw (RuntimeException)
249 {
250 	OExternalLockGuard aGuard( this );
251 
252 	return OCommonAccessibleText::getSelectedText();
253 }
254 
255 // -----------------------------------------------------------------------------
256 
257 sal_Int32 VCLXAccessibleTextComponent::getSelectionStart() throw (RuntimeException)
258 {
259 	OExternalLockGuard aGuard( this );
260 
261 	return OCommonAccessibleText::getSelectionStart();
262 }
263 
264 // -----------------------------------------------------------------------------
265 
266 sal_Int32 VCLXAccessibleTextComponent::getSelectionEnd() throw (RuntimeException)
267 {
268 	OExternalLockGuard aGuard( this );
269 
270 	return OCommonAccessibleText::getSelectionEnd();
271 }
272 
273 // -----------------------------------------------------------------------------
274 
275 sal_Bool VCLXAccessibleTextComponent::setSelection( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException)
276 {
277 	OExternalLockGuard aGuard( this );
278 
279     if ( !implIsValidRange( nStartIndex, nEndIndex, implGetText().getLength() ) )
280         throw IndexOutOfBoundsException();
281 
282 	return sal_False;
283 }
284 
285 // -----------------------------------------------------------------------------
286 
287 ::rtl::OUString VCLXAccessibleTextComponent::getText() throw (RuntimeException)
288 {
289 	OExternalLockGuard aGuard( this );
290 
291 	return OCommonAccessibleText::getText();
292 }
293 
294 // -----------------------------------------------------------------------------
295 
296 ::rtl::OUString VCLXAccessibleTextComponent::getTextRange( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException)
297 {
298 	OExternalLockGuard aGuard( this );
299 
300 	return OCommonAccessibleText::getTextRange( nStartIndex, nEndIndex );
301 }
302 
303 // -----------------------------------------------------------------------------
304 
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)
306 {
307 	OExternalLockGuard aGuard( this );
308 
309 	return OCommonAccessibleText::getTextAtIndex( nIndex, aTextType );
310 }
311 
312 // -----------------------------------------------------------------------------
313 
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)
315 {
316 	OExternalLockGuard aGuard( this );
317 
318 	return OCommonAccessibleText::getTextBeforeIndex( nIndex, aTextType );
319 }
320 
321 // -----------------------------------------------------------------------------
322 
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)
324 {
325 	OExternalLockGuard aGuard( this );
326 
327 	return OCommonAccessibleText::getTextBehindIndex( nIndex, aTextType );
328 }
329 
330 // -----------------------------------------------------------------------------
331 
332 sal_Bool VCLXAccessibleTextComponent::copyText( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException)
333 {
334 	OExternalLockGuard aGuard( this );
335 
336 	sal_Bool bReturn = sal_False;
337 
338 	if ( GetWindow() )
339 	{
340 		Reference< datatransfer::clipboard::XClipboard > xClipboard = GetWindow()->GetClipboard();
341 		if ( xClipboard.is() )
342 		{
343 			::rtl::OUString sText( getTextRange( nStartIndex, nEndIndex ) );
344 
345 			::vcl::unohelper::TextDataObject* pDataObj = new ::vcl::unohelper::TextDataObject( sText );
346 			const sal_uInt32 nRef = Application::ReleaseSolarMutex();
347 			xClipboard->setContents( pDataObj, NULL );
348 
349 			Reference< datatransfer::clipboard::XFlushableClipboard > xFlushableClipboard( xClipboard, uno::UNO_QUERY );
350 			if( xFlushableClipboard.is() )
351 				xFlushableClipboard->flushClipboard();
352 
353 			Application::AcquireSolarMutex( nRef );
354 
355 			bReturn = sal_True;
356 		}
357 	}
358 
359     return bReturn;
360 }
361 
362 // -----------------------------------------------------------------------------
363