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 :
21 :
22 :
23 : // Global header
24 :
25 :
26 : #include <com/sun/star/uno/Any.hxx>
27 : #include <com/sun/star/uno/Reference.hxx>
28 : #include <cppuhelper/weakref.hxx>
29 : #include <com/sun/star/accessibility/XAccessible.hpp>
30 : #include <com/sun/star/accessibility/AccessibleStateType.hpp>
31 :
32 :
33 :
34 : // Project-local header
35 :
36 :
37 :
38 : #include <editeng/unoedhlp.hxx>
39 : #include <editeng/unopracc.hxx>
40 : #include <editeng/unoedsrc.hxx>
41 : #include "editeng/AccessibleParaManager.hxx"
42 : #include "editeng/AccessibleEditableTextPara.hxx"
43 :
44 :
45 : using namespace ::com::sun::star;
46 : using namespace ::com::sun::star::accessibility;
47 :
48 :
49 :
50 : namespace accessibility
51 : {
52 62 : AccessibleParaManager::AccessibleParaManager() :
53 : maChildren(1),
54 : maEEOffset( 0, 0 ),
55 : mnFocusedChild( -1 ),
56 62 : mbActive( false )
57 : {
58 62 : }
59 :
60 46 : AccessibleParaManager::~AccessibleParaManager()
61 : {
62 : // owner is responsible for possible child defuncs
63 46 : }
64 :
65 14 : void AccessibleParaManager::SetAdditionalChildStates( const VectorOfStates& rChildStates )
66 : {
67 14 : maChildStates = rChildStates;
68 14 : }
69 :
70 128 : void AccessibleParaManager::SetNum( sal_Int32 nNumParas )
71 : {
72 128 : if( (size_t)nNumParas < maChildren.size() )
73 66 : Release( nNumParas, maChildren.size() );
74 :
75 128 : maChildren.resize( nNumParas );
76 :
77 128 : if( mnFocusedChild >= nNumParas )
78 0 : mnFocusedChild = -1;
79 128 : }
80 :
81 14 : sal_Int32 AccessibleParaManager::GetNum() const
82 : {
83 14 : size_t nSize = maChildren.size();
84 14 : if (nSize > SAL_MAX_INT32)
85 : {
86 : SAL_WARN( "editeng", "AccessibleParaManager::GetNum - overflow " << nSize);
87 0 : return SAL_MAX_INT32;
88 : }
89 14 : return static_cast<sal_Int32>(nSize);
90 : }
91 :
92 208 : AccessibleParaManager::VectorOfChildren::iterator AccessibleParaManager::begin()
93 : {
94 208 : return maChildren.begin();
95 : }
96 :
97 166 : AccessibleParaManager::VectorOfChildren::iterator AccessibleParaManager::end()
98 : {
99 166 : return maChildren.end();
100 : }
101 :
102 0 : AccessibleParaManager::VectorOfChildren::const_iterator AccessibleParaManager::begin() const
103 : {
104 0 : return maChildren.begin();
105 : }
106 :
107 0 : AccessibleParaManager::VectorOfChildren::const_iterator AccessibleParaManager::end() const
108 : {
109 0 : return maChildren.end();
110 : }
111 :
112 0 : void AccessibleParaManager::FireEvent( sal_Int32 nPara,
113 : const sal_Int16 nEventId,
114 : const uno::Any& rNewValue,
115 : const uno::Any& rOldValue ) const
116 : {
117 : DBG_ASSERT( 0 <= nPara && maChildren.size() > static_cast<size_t>(nPara),
118 : "AccessibleParaManager::FireEvent: invalid index" );
119 :
120 0 : if( 0 <= nPara && maChildren.size() > static_cast<size_t>(nPara) )
121 : {
122 0 : WeakPara::HardRefType maChild( GetChild( nPara ).first.get() );
123 0 : if( maChild.is() )
124 0 : maChild->FireEvent( nEventId, rNewValue, rOldValue );
125 : }
126 0 : }
127 :
128 140 : bool AccessibleParaManager::IsReferencable( WeakPara::HardRefType aChild )
129 : {
130 140 : return aChild.is();
131 : }
132 :
133 74 : bool AccessibleParaManager::IsReferencable( sal_Int32 nChild ) const
134 : {
135 : DBG_ASSERT( 0 <= nChild && maChildren.size() > static_cast<size_t>(nChild),
136 : "AccessibleParaManager::IsReferencable: invalid index" );
137 :
138 74 : if( 0 <= nChild && maChildren.size() > static_cast<size_t>(nChild) )
139 : {
140 : // retrieve hard reference from weak one
141 74 : return IsReferencable( GetChild( nChild ).first.get() );
142 : }
143 : else
144 : {
145 0 : return false;
146 : }
147 : }
148 :
149 326 : AccessibleParaManager::WeakChild AccessibleParaManager::GetChild( sal_Int32 nParagraphIndex ) const
150 : {
151 : DBG_ASSERT( 0 <= nParagraphIndex && maChildren.size() > static_cast<size_t>(nParagraphIndex),
152 : "AccessibleParaManager::GetChild: invalid index" );
153 :
154 326 : if( 0 <= nParagraphIndex && maChildren.size() > static_cast<size_t>(nParagraphIndex) )
155 : {
156 326 : return maChildren[ nParagraphIndex ];
157 : }
158 : else
159 : {
160 0 : return WeakChild();
161 : }
162 : }
163 :
164 74 : AccessibleParaManager::Child AccessibleParaManager::CreateChild( sal_Int32 nChild,
165 : const uno::Reference< XAccessible >& xFrontEnd,
166 : SvxEditSourceAdapter& rEditSource,
167 : sal_Int32 nParagraphIndex )
168 : {
169 : DBG_ASSERT( 0 <= nParagraphIndex && maChildren.size() > static_cast<size_t>(nParagraphIndex),
170 : "AccessibleParaManager::CreateChild: invalid index" );
171 :
172 74 : if( 0 <= nParagraphIndex && maChildren.size() > static_cast<size_t>(nParagraphIndex) )
173 : {
174 : // retrieve hard reference from weak one
175 74 : WeakPara::HardRefType aChild( GetChild( nParagraphIndex ).first.get() );
176 :
177 74 : if( !IsReferencable( nParagraphIndex ) )
178 : {
179 : // there is no hard reference available, create object then
180 : // #i27138#
181 52 : AccessibleEditableTextPara* pChild = new AccessibleEditableTextPara( xFrontEnd, this );
182 52 : uno::Reference< XAccessible > xChild( static_cast< ::cppu::OWeakObject* > (pChild), uno::UNO_QUERY );
183 :
184 52 : if( !xChild.is() )
185 0 : throw uno::RuntimeException("Child creation failed", xFrontEnd);
186 :
187 52 : aChild = WeakPara::HardRefType( xChild, pChild );
188 :
189 52 : InitChild( *aChild, rEditSource, nChild, nParagraphIndex );
190 :
191 52 : maChildren[ nParagraphIndex ] = WeakChild( aChild, pChild->getBounds() );
192 : }
193 :
194 74 : return Child( aChild.getRef(), GetChild( nParagraphIndex ).second );
195 : }
196 : else
197 : {
198 0 : return Child();
199 : }
200 : }
201 :
202 0 : void AccessibleParaManager::SetEEOffset( const Point& rOffset )
203 : {
204 0 : maEEOffset = rOffset;
205 :
206 0 : MemFunAdapter< const Point& > aAdapter( &::accessibility::AccessibleEditableTextPara::SetEEOffset, rOffset );
207 0 : ::std::for_each( begin(), end(), aAdapter );
208 0 : }
209 :
210 0 : void AccessibleParaManager::SetActive( bool bActive )
211 : {
212 0 : mbActive = bActive;
213 :
214 0 : if( bActive )
215 : {
216 0 : SetState( AccessibleStateType::ACTIVE );
217 0 : SetState( AccessibleStateType::EDITABLE );
218 : }
219 : else
220 : {
221 0 : UnSetState( AccessibleStateType::ACTIVE );
222 0 : UnSetState( AccessibleStateType::EDITABLE );
223 : }
224 0 : }
225 :
226 0 : void AccessibleParaManager::SetFocus( sal_Int32 nChild )
227 : {
228 0 : if( mnFocusedChild != -1 )
229 0 : UnSetState( mnFocusedChild, AccessibleStateType::FOCUSED );
230 :
231 0 : mnFocusedChild = nChild;
232 :
233 0 : if( mnFocusedChild != -1 )
234 0 : SetState( mnFocusedChild, AccessibleStateType::FOCUSED );
235 0 : }
236 :
237 52 : void AccessibleParaManager::InitChild( AccessibleEditableTextPara& rChild,
238 : SvxEditSourceAdapter& rEditSource,
239 : sal_Int32 nChild,
240 : sal_Int32 nParagraphIndex ) const
241 : {
242 52 : rChild.SetEditSource( &rEditSource );
243 52 : rChild.SetIndexInParent( nChild );
244 52 : rChild.SetParagraphIndex( nParagraphIndex );
245 :
246 52 : rChild.SetEEOffset( maEEOffset );
247 :
248 52 : if( mbActive )
249 : {
250 0 : rChild.SetState( AccessibleStateType::ACTIVE );
251 0 : rChild.SetState( AccessibleStateType::EDITABLE );
252 : }
253 :
254 52 : if( mnFocusedChild == nParagraphIndex )
255 0 : rChild.SetState( AccessibleStateType::FOCUSED );
256 :
257 : // add states passed from outside
258 64 : for( VectorOfStates::const_iterator aIt = maChildStates.begin(), aEnd = maChildStates.end(); aIt != aEnd; ++aIt )
259 12 : rChild.SetState( *aIt );
260 52 : }
261 :
262 0 : void AccessibleParaManager::SetState( sal_Int32 nChild, const sal_Int16 nStateId )
263 : {
264 : MemFunAdapter< const sal_Int16 > aFunc( &AccessibleEditableTextPara::SetState,
265 0 : nStateId );
266 0 : aFunc( GetChild(nChild) );
267 0 : }
268 :
269 0 : void AccessibleParaManager::SetState( const sal_Int16 nStateId )
270 : {
271 : ::std::for_each( begin(), end(),
272 : MemFunAdapter< const sal_Int16 >( &AccessibleEditableTextPara::SetState,
273 0 : nStateId ) );
274 0 : }
275 :
276 0 : void AccessibleParaManager::UnSetState( sal_Int32 nChild, const sal_Int16 nStateId )
277 : {
278 : MemFunAdapter< const sal_Int16 > aFunc( &AccessibleEditableTextPara::UnSetState,
279 0 : nStateId );
280 0 : aFunc( GetChild(nChild) );
281 0 : }
282 :
283 0 : void AccessibleParaManager::UnSetState( const sal_Int16 nStateId )
284 : {
285 : ::std::for_each( begin(), end(),
286 : MemFunAdapter< const sal_Int16 >( &AccessibleEditableTextPara::UnSetState,
287 0 : nStateId ) );
288 0 : }
289 :
290 : // not generic yet, no arguments...
291 : class AccessibleParaManager_DisposeChildren : public ::std::unary_function< ::accessibility::AccessibleEditableTextPara&, void >
292 : {
293 : public:
294 124 : AccessibleParaManager_DisposeChildren() {}
295 37 : void operator()( ::accessibility::AccessibleEditableTextPara& rPara )
296 : {
297 37 : rPara.Dispose();
298 37 : }
299 : };
300 :
301 124 : void AccessibleParaManager::Dispose()
302 : {
303 124 : AccessibleParaManager_DisposeChildren aFunctor;
304 :
305 : ::std::for_each( begin(), end(),
306 124 : WeakChildAdapter< AccessibleParaManager_DisposeChildren > (aFunctor) );
307 124 : }
308 :
309 : // not generic yet, too many method arguments...
310 : class StateChangeEvent : public ::std::unary_function< ::accessibility::AccessibleEditableTextPara&, void >
311 : {
312 : public:
313 : typedef void return_type;
314 0 : StateChangeEvent( const sal_Int16 nEventId,
315 : const uno::Any& rNewValue,
316 : const uno::Any& rOldValue ) :
317 : mnEventId( nEventId ),
318 : mrNewValue( rNewValue ),
319 0 : mrOldValue( rOldValue ) {}
320 0 : void operator()( ::accessibility::AccessibleEditableTextPara& rPara )
321 : {
322 0 : rPara.FireEvent( mnEventId, mrNewValue, mrOldValue );
323 0 : }
324 :
325 : private:
326 : const sal_Int16 mnEventId;
327 : const uno::Any& mrNewValue;
328 : const uno::Any& mrOldValue;
329 : };
330 :
331 0 : void AccessibleParaManager::FireEvent( sal_Int32 nStartPara,
332 : sal_Int32 nEndPara,
333 : const sal_Int16 nEventId,
334 : const uno::Any& rNewValue,
335 : const uno::Any& rOldValue ) const
336 : {
337 : DBG_ASSERT( 0 <= nStartPara && 0 <= nEndPara &&
338 : maChildren.size() > static_cast<size_t>(nStartPara) &&
339 : maChildren.size() >= static_cast<size_t>(nEndPara) &&
340 : nEndPara >= nStartPara, "AccessibleParaManager::FireEvent: invalid index" );
341 :
342 :
343 :
344 0 : if( 0 <= nStartPara && 0 <= nEndPara &&
345 0 : maChildren.size() > static_cast<size_t>(nStartPara) &&
346 0 : maChildren.size() >= static_cast<size_t>(nEndPara) &&
347 : nEndPara >= nStartPara )
348 : {
349 0 : VectorOfChildren::const_iterator front = maChildren.begin();
350 0 : VectorOfChildren::const_iterator back = front;
351 :
352 0 : ::std::advance( front, nStartPara );
353 0 : ::std::advance( back, nEndPara );
354 :
355 0 : StateChangeEvent aFunctor( nEventId, rNewValue, rOldValue );
356 :
357 0 : ::std::for_each( front, back, AccessibleParaManager::WeakChildAdapter< StateChangeEvent >( aFunctor ) );
358 : }
359 0 : }
360 :
361 : class ReleaseChild : public ::std::unary_function< const AccessibleParaManager::WeakChild&, AccessibleParaManager::WeakChild >
362 : {
363 : public:
364 66 : AccessibleParaManager::WeakChild operator()( const AccessibleParaManager::WeakChild& rPara )
365 : {
366 66 : AccessibleParaManager::ShutdownPara( rPara );
367 :
368 : // clear reference
369 66 : return AccessibleParaManager::WeakChild();
370 : }
371 : };
372 :
373 66 : void AccessibleParaManager::Release( sal_Int32 nStartPara, sal_Int32 nEndPara )
374 : {
375 : DBG_ASSERT( 0 <= nStartPara && 0 <= nEndPara &&
376 : maChildren.size() > static_cast<size_t>(nStartPara) &&
377 : maChildren.size() >= static_cast<size_t>(nEndPara),
378 : "AccessibleParaManager::Release: invalid index" );
379 :
380 198 : if( 0 <= nStartPara && 0 <= nEndPara &&
381 198 : maChildren.size() > static_cast<size_t>(nStartPara) &&
382 66 : maChildren.size() >= static_cast<size_t>(nEndPara) )
383 : {
384 66 : VectorOfChildren::iterator front = maChildren.begin();
385 66 : VectorOfChildren::iterator back = front;
386 :
387 66 : ::std::advance( front, nStartPara );
388 66 : ::std::advance( back, nEndPara );
389 :
390 66 : ::std::transform( front, back, front, ReleaseChild() );
391 : }
392 66 : }
393 :
394 66 : void AccessibleParaManager::ShutdownPara( const WeakChild& rChild )
395 : {
396 66 : WeakPara::HardRefType aChild( rChild.first.get() );
397 :
398 66 : if( IsReferencable( aChild ) )
399 4 : aChild->SetEditSource( NULL );
400 66 : }
401 :
402 669 : }
403 :
404 :
405 :
406 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|