Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef _SVX_ACCESSIBLE_PARA_MANAGER_HXX
30 : : #define _SVX_ACCESSIBLE_PARA_MANAGER_HXX
31 : :
32 : : #include <vector>
33 : : #include <algorithm>
34 : : #include <functional>
35 : : #include <utility>
36 : : #include <tools/gen.hxx>
37 : : #include <com/sun/star/awt/Rectangle.hpp>
38 : : #include <com/sun/star/uno/Reference.hxx>
39 : : #include <cppuhelper/weakref.hxx>
40 : : #include <com/sun/star/accessibility/XAccessibleContext.hpp>
41 : : #include "editeng/editengdllapi.h"
42 : :
43 : : class SvxEditSourceAdapter;
44 : :
45 : : namespace accessibility
46 : : {
47 : : class AccessibleEditableTextPara;
48 : :
49 : : /** Helper class for WeakCppRef
50 : :
51 : : This class is returned by WeakChild::get() and contains a hard
52 : : reference and a reference to the c++ object. This combination
53 : : prevents the c++ object from destruction during usage. Hold
54 : : this object only as long as absolutely necessary, prevents
55 : : referenced object from vanishing otherwise
56 : : */
57 : 709 : template < class UnoType, class CppType > class HardCppRef
58 : : {
59 : : public:
60 : :
61 : : typedef UnoType UnoInterfaceType;
62 : : typedef CppType InterfaceType;
63 : :
64 : 542 : HardCppRef( const ::com::sun::star::uno::WeakReference< UnoInterfaceType >& xRef, InterfaceType* rImpl ) :
65 : : mxRef( xRef ),
66 : 542 : mpImpl( rImpl )
67 : : {
68 : 542 : }
69 : :
70 : : /** Query whether the reference is still valid.
71 : :
72 : : Hands off also from the implementation pointer if this
73 : : returns sal_False!
74 : : */
75 : 436 : sal_Bool is() const { return mxRef.is(); }
76 : 64 : InterfaceType* operator->() const { return mpImpl; }
77 : 79 : InterfaceType& operator*() const { return *mpImpl; }
78 : 106 : ::com::sun::star::uno::Reference< UnoInterfaceType >& getRef() { return mxRef; }
79 : : const ::com::sun::star::uno::Reference< UnoInterfaceType >& getRef() const { return mxRef; }
80 : :
81 : : // default copy constructor and assignment will do
82 : : // HardCppRef( const HardCppRef& );
83 : : // HardCppRef& operator= ( const HardCppRef& );
84 : :
85 : : private:
86 : :
87 : : // the interface, hard reference to prevent object from vanishing
88 : : ::com::sun::star::uno::Reference< UnoInterfaceType > mxRef;
89 : :
90 : : // the c++ object, for our internal stuff
91 : : InterfaceType* mpImpl;
92 : :
93 : : };
94 : :
95 : : /** Helper class for weak object references plus implementation
96 : :
97 : : This class combines a weak reference (to facilitate automatic
98 : : object disposal if user drops last reference) and hard
99 : : reference to the c++ class (for fast access and bypassing of
100 : : the UNO interface)
101 : : */
102 : 945 : template < class UnoType, class CppType > class WeakCppRef
103 : : {
104 : : public:
105 : :
106 : : typedef UnoType UnoInterfaceType;
107 : : typedef CppType InterfaceType;
108 : : typedef HardCppRef< UnoInterfaceType, InterfaceType > HardRefType;
109 : :
110 : 229 : WeakCppRef() : maWeakRef(), maUnsafeRef( NULL ) {}
111 : : WeakCppRef( InterfaceType& rImpl ) :
112 : : maWeakRef( ::com::sun::star::uno::Reference< UnoInterfaceType >( rImpl, ::com::sun::star::uno::UNO_QUERY ) ),
113 : : maUnsafeRef( &rImpl )
114 : : {
115 : : }
116 : :
117 : 45 : WeakCppRef( HardRefType& rImpl ) :
118 : : maWeakRef( rImpl.getRef() ),
119 : 45 : maUnsafeRef( rImpl.operator->() )
120 : : {
121 : 45 : }
122 : :
123 : : // get object with c++ object and hard reference (which
124 : : // prevents the c++ object from destruction during use)
125 : 497 : HardRefType get() const { return HardRefType( maWeakRef, maUnsafeRef ); }
126 : :
127 : : // default copy constructor and assignment will do
128 : : // WeakCppRef( const WeakCppRef& );
129 : : // WeakCppRef& operator= ( const WeakCppRef& );
130 : :
131 : : private:
132 : :
133 : : // the interface, hold weakly
134 : : ::com::sun::star::uno::WeakReference< UnoInterfaceType > maWeakRef;
135 : :
136 : : // hard ref to c++ class, _only_ valid if maWeakRef.is() is true
137 : : InterfaceType* maUnsafeRef;
138 : : };
139 : :
140 : :
141 : : /** This class manages the paragraphs of an AccessibleTextHelper
142 : :
143 : : To facilitate automatic deletion of paragraphs no longer used,
144 : : this class uses the WeakCppRef helper to hold the objects weakly.
145 : : */
146 : : class EDITENG_DLLPUBLIC AccessibleParaManager
147 : : {
148 : : public:
149 : : typedef WeakCppRef < ::com::sun::star::accessibility::XAccessible, AccessibleEditableTextPara > WeakPara;
150 : : typedef ::std::pair< WeakPara, ::com::sun::star::awt::Rectangle > WeakChild;
151 : : typedef ::std::pair< ::com::sun::star::uno::Reference<
152 : : ::com::sun::star::accessibility::XAccessible > , ::com::sun::star::awt::Rectangle > Child;
153 : : typedef ::std::vector< WeakChild > VectorOfChildren;
154 : : typedef ::std::vector< sal_Int16 > VectorOfStates;
155 : :
156 : : AccessibleParaManager();
157 : : ~AccessibleParaManager();
158 : :
159 : : /** Sets a vector of additional accessible states.
160 : :
161 : : The states are passed to every created child object
162 : : (text paragraph). The state values are defined in
163 : : com::sun::star::accessibility::AccessibleStateType.
164 : : */
165 : : void SetAdditionalChildStates( const VectorOfStates& rChildStates );
166 : :
167 : : /** Set the number of paragraphs
168 : :
169 : : @param nNumPara
170 : : The total number of paragraphs the EditEngine currently
171 : : has (_not_ the number of currently visible children)
172 : : */
173 : : void SetNum( sal_Int32 nNumParas );
174 : :
175 : : /** Get the number of paragraphs currently possible */
176 : : sal_uInt32 GetNum() const;
177 : :
178 : : // iterators
179 : : VectorOfChildren::iterator begin();
180 : : VectorOfChildren::iterator end();
181 : : VectorOfChildren::const_iterator begin() const;
182 : : VectorOfChildren::const_iterator end() const;
183 : :
184 : : // dealing with single paragraphs (release reference, return reference etc)
185 : : void Release( sal_uInt32 nPara );
186 : : /// Set focus to given child
187 : : void SetFocus( sal_Int32 nChild );
188 : :
189 : : void FireEvent( sal_uInt32 nPara,
190 : : const sal_Int16 nEventId,
191 : : const ::com::sun::star::uno::Any& rNewValue = ::com::sun::star::uno::Any(),
192 : : const ::com::sun::star::uno::Any& rOldValue = ::com::sun::star::uno::Any() ) const;
193 : :
194 : : static sal_Bool IsReferencable( WeakPara::HardRefType aChild );
195 : : sal_Bool IsReferencable( sal_uInt32 nChild ) const;
196 : : static void ShutdownPara( const WeakChild& rChild );
197 : :
198 : : Child CreateChild( sal_Int32 nChild,
199 : : const ::com::sun::star::uno::Reference< ::com::sun::star::accessibility::XAccessible >& xFrontEnd,
200 : : SvxEditSourceAdapter& rEditSource,
201 : : sal_uInt32 nParagraphIndex );
202 : :
203 : : WeakChild GetChild( sal_uInt32 nParagraphIndex ) const;
204 : :
205 : : // forwarder to all paragraphs
206 : : /// Make all children active and editable (or off)
207 : : void SetActive( sal_Bool bActive = sal_True );
208 : : /// Set state of all children
209 : : void SetState( const sal_Int16 nStateId );
210 : : /// Unset state of all children
211 : : void UnSetState( const sal_Int16 nStateId );
212 : : /// Set offset to edit engine for all children
213 : : void SetEEOffset ( const Point& rOffset );
214 : : /// Dispose all living children
215 : : void Dispose ();
216 : :
217 : : // forwarder to given paragraphs
218 : : //------------------------------------------------------------------------
219 : : /** Release the given range of paragraphs
220 : :
221 : : All ranges have the meaning [start,end), similar to STL
222 : :
223 : : @param nStartPara
224 : : Index of paragraph to start with releasing
225 : :
226 : : @param nEndPara
227 : : Index of first paragraph to stop with releasing
228 : : */
229 : : void Release( sal_uInt32 nStartPara, sal_uInt32 nEndPara );
230 : :
231 : : /** Fire event for the given range of paragraphs
232 : :
233 : : All ranges have the meaning [start,end), similar to STL
234 : :
235 : : @param nStartPara
236 : : Index of paragraph to start with event firing
237 : :
238 : : @param nEndPara
239 : : Index of first paragraph to stop with event firing
240 : : */
241 : : void FireEvent( sal_uInt32 nStartPara,
242 : : sal_uInt32 nEndPara,
243 : : const sal_Int16 nEventId,
244 : : const ::com::sun::star::uno::Any& rNewValue = ::com::sun::star::uno::Any(),
245 : : const ::com::sun::star::uno::Any& rOldValue = ::com::sun::star::uno::Any() ) const;
246 : :
247 : : /** Functor adapter for ForEach template
248 : :
249 : : Adapts giving functor such that only the paragraph objects
250 : : are accessed and the fact that our children are held
251 : : weakly is hidden
252 : :
253 : : The functor must provide the following method:
254 : : void operator() ( AccessibleEditablePara& )
255 : :
256 : : */
257 : : template < typename Functor > class WeakChildAdapter : public ::std::unary_function< const WeakChild&, void >
258 : : {
259 : : public:
260 : 117 : WeakChildAdapter( Functor& rFunctor ) : mrFunctor(rFunctor) {}
261 : 109 : void operator()( const WeakChild& rPara )
262 : : {
263 : : // retrieve hard reference from weak one
264 [ # # ][ + - ]: 109 : WeakPara::HardRefType aHardRef( rPara.first.get() );
265 : :
266 [ # # ][ + + ]: 109 : if( aHardRef.is() )
267 [ # # ][ # # ]: 109 : mrFunctor( *aHardRef );
[ + - ][ + - ]
268 : 109 : }
269 : :
270 : : private:
271 : : Functor& mrFunctor;
272 : : };
273 : :
274 : : /** Adapter for unary member functions
275 : :
276 : : Since STL's binder don't work with const& arguments (and
277 : : BOOST's neither, at least on MSVC), have to provide our
278 : : own adapter for unary member functions.
279 : :
280 : : Create with pointer to member function of
281 : : AccessibleEditableTextPara and the corresponding argument.
282 : : */
283 : : template < typename Argument > class MemFunAdapter : public ::std::unary_function< const WeakChild&, void >
284 : : {
285 : : public:
286 : : typedef void (::accessibility::AccessibleEditableTextPara::*FunctionPointer)( Argument );
287 : :
288 : 0 : MemFunAdapter( FunctionPointer aFunPtr, Argument aArg ) : maFunPtr(aFunPtr), maArg(aArg) {}
289 : 0 : void operator()( const WeakChild& rPara )
290 : : {
291 : : // retrieve hard reference from weak one
292 [ # # ][ # # ]: 0 : WeakPara::HardRefType aHardRef( rPara.first.get() );
293 : :
294 [ # # ][ # # ]: 0 : if( aHardRef.is() )
295 [ # # ][ # # ]: 0 : (*aHardRef.*maFunPtr)( maArg );
[ # # ][ # # ]
[ # # ][ # # ]
296 : 0 : }
297 : :
298 : : private:
299 : : FunctionPointer maFunPtr;
300 : : Argument maArg;
301 : : };
302 : :
303 : : /** Generic algorithm on given paragraphs
304 : :
305 : : Convenience method, that already adapts the given functor with WeakChildAdapter
306 : : */
307 : : template < typename Functor > void ForEach( Functor& rFunctor )
308 : : {
309 : : ::std::for_each( begin(), end(), WeakChildAdapter< Functor >(rFunctor) );
310 : : }
311 : :
312 : : private:
313 : : /// Set state on given child
314 : : void SetState( sal_Int32 nChild, const sal_Int16 nStateId );
315 : : /// Unset state on given child
316 : : void UnSetState( sal_Int32 nChild, const sal_Int16 nStateId );
317 : : /// Init child with default state (as stored in previous SetFocus and SetActive calls)
318 : : void InitChild( AccessibleEditableTextPara& rChild,
319 : : SvxEditSourceAdapter& rEditSource,
320 : : sal_Int32 nChild,
321 : : sal_uInt32 nParagraphIndex ) const;
322 : :
323 : : // vector the size of the paragraph number of the underlying EditEngine
324 : : VectorOfChildren maChildren;
325 : :
326 : : /// Additional states that will be set at every created child object.
327 : : VectorOfStates maChildStates;
328 : :
329 : : // cache EE offset for child creation
330 : : Point maEEOffset;
331 : :
332 : : // which child currently has the focus (-1 for none)
333 : : sal_Int32 mnFocusedChild;
334 : :
335 : : // whether children are active and editable
336 : : sal_Bool mbActive;
337 : : };
338 : :
339 : : } // end of namespace accessibility
340 : :
341 : : #endif
342 : :
343 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|