Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <accessibility/standard/vclxaccessibletextcomponent.hxx>
21 : #include <toolkit/helper/macros.hxx>
22 : #include <toolkit/helper/convert.hxx>
23 : #include <accessibility/helper/characterattributeshelper.hxx>
24 :
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 :
35 : #include <memory>
36 : #include <vector>
37 :
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;
44 :
45 :
46 : // ----------------------------------------------------
47 : // class VCLXAccessibleTextComponent
48 : // ----------------------------------------------------
49 :
50 0 : VCLXAccessibleTextComponent::VCLXAccessibleTextComponent( VCLXWindow* pVCLXWindow )
51 0 : :VCLXAccessibleComponent( pVCLXWindow )
52 : {
53 0 : if ( GetWindow() )
54 0 : m_sText = OutputDevice::GetNonMnemonicString( GetWindow()->GetText() );
55 0 : }
56 :
57 : // -----------------------------------------------------------------------------
58 :
59 0 : VCLXAccessibleTextComponent::~VCLXAccessibleTextComponent()
60 : {
61 0 : }
62 :
63 : // -----------------------------------------------------------------------------
64 :
65 0 : void VCLXAccessibleTextComponent::SetText( const OUString& sText )
66 : {
67 0 : Any aOldValue, aNewValue;
68 0 : if ( implInitTextChangedEvent( m_sText, sText, aOldValue, aNewValue ) )
69 : {
70 0 : m_sText = sText;
71 0 : NotifyAccessibleEvent( AccessibleEventId::TEXT_CHANGED, aOldValue, aNewValue );
72 0 : }
73 0 : }
74 :
75 : // -----------------------------------------------------------------------------
76 :
77 0 : void VCLXAccessibleTextComponent::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
78 : {
79 0 : switch ( rVclWindowEvent.GetId() )
80 : {
81 : case VCLEVENT_WINDOW_FRAMETITLECHANGED:
82 : {
83 0 : VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
84 0 : SetText( implGetText() );
85 : }
86 0 : break;
87 : default:
88 0 : VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
89 : }
90 0 : }
91 :
92 : // -----------------------------------------------------------------------------
93 : // OCommonAccessibleText
94 : // -----------------------------------------------------------------------------
95 :
96 0 : OUString VCLXAccessibleTextComponent::implGetText()
97 : {
98 0 : OUString aText;
99 0 : if ( GetWindow() )
100 0 : aText = OutputDevice::GetNonMnemonicString( GetWindow()->GetText() );
101 :
102 0 : return aText;
103 : }
104 :
105 : // -----------------------------------------------------------------------------
106 :
107 0 : lang::Locale VCLXAccessibleTextComponent::implGetLocale()
108 : {
109 0 : return Application::GetSettings().GetLanguageTag().getLocale();
110 : }
111 :
112 : // -----------------------------------------------------------------------------
113 :
114 0 : void VCLXAccessibleTextComponent::implGetSelection( sal_Int32& nStartIndex, sal_Int32& nEndIndex )
115 : {
116 0 : nStartIndex = 0;
117 0 : nEndIndex = 0;
118 0 : }
119 :
120 : // -----------------------------------------------------------------------------
121 : // XComponent
122 : // -----------------------------------------------------------------------------
123 :
124 0 : void VCLXAccessibleTextComponent::disposing()
125 : {
126 0 : VCLXAccessibleComponent::disposing();
127 :
128 0 : m_sText = OUString();
129 0 : }
130 :
131 : // -----------------------------------------------------------------------------
132 : // XInterface
133 : // -----------------------------------------------------------------------------
134 :
135 0 : IMPLEMENT_FORWARD_XINTERFACE2( VCLXAccessibleTextComponent, VCLXAccessibleComponent, VCLXAccessibleTextComponent_BASE )
136 :
137 : // -----------------------------------------------------------------------------
138 : // XTypeProvider
139 : // -----------------------------------------------------------------------------
140 :
141 0 : IMPLEMENT_FORWARD_XTYPEPROVIDER2( VCLXAccessibleTextComponent, VCLXAccessibleComponent, VCLXAccessibleTextComponent_BASE )
142 :
143 : // -----------------------------------------------------------------------------
144 : // XAccessibleText
145 : // -----------------------------------------------------------------------------
146 :
147 0 : sal_Int32 VCLXAccessibleTextComponent::getCaretPosition() throw (RuntimeException)
148 : {
149 0 : OExternalLockGuard aGuard( this );
150 :
151 0 : return -1;
152 : }
153 :
154 : // -----------------------------------------------------------------------------
155 :
156 0 : sal_Bool VCLXAccessibleTextComponent::setCaretPosition( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
157 : {
158 0 : OExternalLockGuard aGuard( this );
159 :
160 0 : return setSelection( nIndex, nIndex );
161 : }
162 :
163 : // -----------------------------------------------------------------------------
164 :
165 0 : sal_Unicode VCLXAccessibleTextComponent::getCharacter( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
166 : {
167 0 : OExternalLockGuard aGuard( this );
168 :
169 0 : return OCommonAccessibleText::getCharacter( nIndex );
170 : }
171 :
172 : // -----------------------------------------------------------------------------
173 :
174 0 : Sequence< PropertyValue > VCLXAccessibleTextComponent::getCharacterAttributes( sal_Int32 nIndex, const Sequence< OUString >& aRequestedAttributes ) throw (IndexOutOfBoundsException, RuntimeException)
175 : {
176 0 : OExternalLockGuard aGuard( this );
177 :
178 0 : Sequence< PropertyValue > aValues;
179 0 : OUString sText( implGetText() );
180 :
181 0 : if ( !implIsValidIndex( nIndex, sText.getLength() ) )
182 0 : throw IndexOutOfBoundsException();
183 :
184 0 : if ( GetWindow() )
185 : {
186 0 : Font aFont = GetWindow()->GetControlFont();
187 0 : sal_Int32 nBackColor = GetWindow()->GetControlBackground().GetColor();
188 0 : sal_Int32 nColor = GetWindow()->GetControlForeground().GetColor();
189 0 : ::std::auto_ptr< CharacterAttributesHelper > pHelper( new CharacterAttributesHelper( aFont, nBackColor, nColor ) );
190 0 : aValues = pHelper->GetCharacterAttributes( aRequestedAttributes );
191 : }
192 :
193 0 : return aValues;
194 : }
195 :
196 : // -----------------------------------------------------------------------------
197 :
198 0 : awt::Rectangle VCLXAccessibleTextComponent::getCharacterBounds( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
199 : {
200 0 : OExternalLockGuard aGuard( this );
201 :
202 0 : if ( !implIsValidIndex( nIndex, implGetText().getLength() ) )
203 0 : throw IndexOutOfBoundsException();
204 :
205 0 : awt::Rectangle aRect;
206 0 : Control* pControl = static_cast< Control* >( GetWindow() );
207 0 : if ( pControl )
208 0 : aRect = AWTRectangle( pControl->GetCharacterBounds( nIndex ) );
209 :
210 0 : return aRect;
211 : }
212 :
213 : // -----------------------------------------------------------------------------
214 :
215 0 : sal_Int32 VCLXAccessibleTextComponent::getCharacterCount() throw (RuntimeException)
216 : {
217 0 : OExternalLockGuard aGuard( this );
218 :
219 0 : return OCommonAccessibleText::getCharacterCount();
220 : }
221 :
222 : // -----------------------------------------------------------------------------
223 :
224 0 : sal_Int32 VCLXAccessibleTextComponent::getIndexAtPoint( const awt::Point& aPoint ) throw (RuntimeException)
225 : {
226 0 : OExternalLockGuard aGuard( this );
227 :
228 0 : sal_Int32 nIndex = -1;
229 0 : Control* pControl = static_cast< Control* >( GetWindow() );
230 0 : if ( pControl )
231 0 : nIndex = pControl->GetIndexForPoint( VCLPoint( aPoint ) );
232 :
233 0 : return nIndex;
234 : }
235 :
236 : // -----------------------------------------------------------------------------
237 :
238 0 : OUString VCLXAccessibleTextComponent::getSelectedText() throw (RuntimeException)
239 : {
240 0 : OExternalLockGuard aGuard( this );
241 :
242 0 : return OCommonAccessibleText::getSelectedText();
243 : }
244 :
245 : // -----------------------------------------------------------------------------
246 :
247 0 : sal_Int32 VCLXAccessibleTextComponent::getSelectionStart() throw (RuntimeException)
248 : {
249 0 : OExternalLockGuard aGuard( this );
250 :
251 0 : return OCommonAccessibleText::getSelectionStart();
252 : }
253 :
254 : // -----------------------------------------------------------------------------
255 :
256 0 : sal_Int32 VCLXAccessibleTextComponent::getSelectionEnd() throw (RuntimeException)
257 : {
258 0 : OExternalLockGuard aGuard( this );
259 :
260 0 : return OCommonAccessibleText::getSelectionEnd();
261 : }
262 :
263 : // -----------------------------------------------------------------------------
264 :
265 0 : sal_Bool VCLXAccessibleTextComponent::setSelection( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException)
266 : {
267 0 : OExternalLockGuard aGuard( this );
268 :
269 0 : if ( !implIsValidRange( nStartIndex, nEndIndex, implGetText().getLength() ) )
270 0 : throw IndexOutOfBoundsException();
271 :
272 0 : return sal_False;
273 : }
274 :
275 : // -----------------------------------------------------------------------------
276 :
277 0 : OUString VCLXAccessibleTextComponent::getText() throw (RuntimeException)
278 : {
279 0 : OExternalLockGuard aGuard( this );
280 :
281 0 : return OCommonAccessibleText::getText();
282 : }
283 :
284 : // -----------------------------------------------------------------------------
285 :
286 0 : OUString VCLXAccessibleTextComponent::getTextRange( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException)
287 : {
288 0 : OExternalLockGuard aGuard( this );
289 :
290 0 : return OCommonAccessibleText::getTextRange( nStartIndex, nEndIndex );
291 : }
292 :
293 : // -----------------------------------------------------------------------------
294 :
295 0 : ::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)
296 : {
297 0 : OExternalLockGuard aGuard( this );
298 :
299 0 : return OCommonAccessibleText::getTextAtIndex( nIndex, aTextType );
300 : }
301 :
302 : // -----------------------------------------------------------------------------
303 :
304 0 : ::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)
305 : {
306 0 : OExternalLockGuard aGuard( this );
307 :
308 0 : return OCommonAccessibleText::getTextBeforeIndex( nIndex, aTextType );
309 : }
310 :
311 : // -----------------------------------------------------------------------------
312 :
313 0 : ::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)
314 : {
315 0 : OExternalLockGuard aGuard( this );
316 :
317 0 : return OCommonAccessibleText::getTextBehindIndex( nIndex, aTextType );
318 : }
319 :
320 : // -----------------------------------------------------------------------------
321 :
322 0 : sal_Bool VCLXAccessibleTextComponent::copyText( sal_Int32 nStartIndex, sal_Int32 nEndIndex ) throw (IndexOutOfBoundsException, RuntimeException)
323 : {
324 0 : OExternalLockGuard aGuard( this );
325 :
326 0 : sal_Bool bReturn = sal_False;
327 :
328 0 : if ( GetWindow() )
329 : {
330 0 : Reference< datatransfer::clipboard::XClipboard > xClipboard = GetWindow()->GetClipboard();
331 0 : if ( xClipboard.is() )
332 : {
333 0 : OUString sText( getTextRange( nStartIndex, nEndIndex ) );
334 :
335 0 : ::vcl::unohelper::TextDataObject* pDataObj = new ::vcl::unohelper::TextDataObject( sText );
336 0 : const sal_uInt32 nRef = Application::ReleaseSolarMutex();
337 0 : xClipboard->setContents( pDataObj, NULL );
338 :
339 0 : Reference< datatransfer::clipboard::XFlushableClipboard > xFlushableClipboard( xClipboard, uno::UNO_QUERY );
340 0 : if( xFlushableClipboard.is() )
341 0 : xFlushableClipboard->flushClipboard();
342 :
343 0 : Application::AcquireSolarMutex( nRef );
344 :
345 0 : bReturn = sal_True;
346 0 : }
347 : }
348 :
349 0 : return bReturn;
350 : }
351 :
352 : // -----------------------------------------------------------------------------
353 :
354 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|