Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / editeng / AccessibleParaManager.hxx
blobaf4687581f064da6ed2d51649ad73492e1a7afba
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_EDITENG_ACCESSIBLEPARAMANAGER_HXX
21 #define INCLUDED_EDITENG_ACCESSIBLEPARAMANAGER_HXX
23 #include <vector>
24 #include <algorithm>
25 #include <functional>
26 #include <utility>
27 #include <tools/gen.hxx>
28 #include <com/sun/star/awt/Rectangle.hpp>
29 #include <com/sun/star/uno/Reference.hxx>
30 #include <cppuhelper/weakref.hxx>
31 #include <com/sun/star/accessibility/XAccessibleContext.hpp>
32 #include <editeng/editengdllapi.h>
34 class SvxEditSourceAdapter;
36 namespace accessibility
38 class AccessibleEditableTextPara;
40 /** Helper class for weak object references plus implementation
42 This class combines a weak reference (to facilitate automatic
43 object disposal if user drops last reference) and hard
44 reference to the c++ class (for fast access and bypassing of
45 the UNO interface)
47 template < class UnoType, class CppType > class WeakCppRef
49 public:
51 typedef UnoType UnoInterfaceType;
52 typedef CppType InterfaceType;
54 WeakCppRef() : maWeakRef(), maUnsafeRef( nullptr ) {}
56 WeakCppRef(rtl::Reference<InterfaceType> const & rImpl):
57 maWeakRef(rImpl.get()),
58 maUnsafeRef(rImpl.get())
62 // get object with c++ object and hard reference (which
63 // prevents the c++ object from destruction during use)
64 rtl::Reference<InterfaceType> get() const {
65 css::uno::Reference<UnoInterfaceType> ref(maWeakRef);
66 return ref.is() ? maUnsafeRef : rtl::Reference<InterfaceType>();
69 // default copy constructor and assignment will do
70 // WeakCppRef( const WeakCppRef& );
71 // WeakCppRef& operator= ( const WeakCppRef& );
73 private:
75 // the interface, hold weakly
76 css::uno::WeakReference< UnoInterfaceType > maWeakRef;
78 // hard ref to c++ class, _only_ valid if maWeakRef.is() is true
79 InterfaceType* maUnsafeRef;
83 /** This class manages the paragraphs of an AccessibleTextHelper
85 To facilitate automatic deletion of paragraphs no longer used,
86 this class uses the WeakCppRef helper to hold the objects weakly.
88 class EDITENG_DLLPUBLIC AccessibleParaManager
90 public:
91 typedef WeakCppRef < css::accessibility::XAccessible, AccessibleEditableTextPara > WeakPara;
92 typedef ::std::pair< WeakPara, css::awt::Rectangle > WeakChild;
93 typedef ::std::pair< css::uno::Reference<
94 css::accessibility::XAccessible > , css::awt::Rectangle > Child;
95 typedef ::std::vector< WeakChild > VectorOfChildren;
96 typedef ::std::vector< sal_Int16 > VectorOfStates;
98 AccessibleParaManager();
99 ~AccessibleParaManager();
101 /** Sets a vector of additional accessible states.
103 The states are passed to every created child object
104 (text paragraph). The state values are defined in
105 css::accessibility::AccessibleStateType.
107 void SetAdditionalChildStates( const VectorOfStates& rChildStates );
109 /** Set the number of paragraphs
111 @param nNumPara
112 The total number of paragraphs the EditEngine currently
113 has (_not_ the number of currently visible children)
115 void SetNum( sal_Int32 nNumParas );
117 /** Get the number of paragraphs currently possible */
118 sal_Int32 GetNum() const;
120 // iterators
121 VectorOfChildren::iterator begin();
122 VectorOfChildren::iterator end();
124 /// Set focus to given child
125 void SetFocus( sal_Int32 nChild );
127 void FireEvent( sal_Int32 nPara,
128 const sal_Int16 nEventId ) const;
130 static bool IsReferencable(rtl::Reference<AccessibleEditableTextPara> const & aChild);
131 bool IsReferencable( sal_Int32 nChild ) const;
132 static void ShutdownPara( const WeakChild& rChild );
134 Child CreateChild( sal_Int32 nChild,
135 const css::uno::Reference< css::accessibility::XAccessible >& xFrontEnd,
136 SvxEditSourceAdapter& rEditSource,
137 sal_Int32 nParagraphIndex );
139 WeakChild GetChild( sal_Int32 nParagraphIndex ) const;
141 // forwarder to all paragraphs
142 /// Make all children active and editable (or off)
143 void SetActive( bool bActive = true );
144 /// Set state of all children
145 void SetState( const sal_Int16 nStateId );
146 /// Unset state of all children
147 void UnSetState( const sal_Int16 nStateId );
148 /// Set offset to edit engine for all children
149 void SetEEOffset ( const Point& rOffset );
150 /// Dispose all living children
151 void Dispose ();
153 // forwarder to given paragraphs
155 /** Release the given range of paragraphs
157 All ranges have the meaning [start,end), similar to STL
159 @param nStartPara
160 Index of paragraph to start with releasing
162 @param nEndPara
163 Index of first paragraph to stop with releasing
165 void Release( sal_Int32 nStartPara, sal_Int32 nEndPara );
167 /** Fire event for the given range of paragraphs
169 All ranges have the meaning [start,end), similar to STL
171 @param nStartPara
172 Index of paragraph to start with event firing
174 @param nEndPara
175 Index of first paragraph to stop with event firing
177 void FireEvent( sal_Int32 nStartPara,
178 sal_Int32 nEndPara,
179 const sal_Int16 nEventId,
180 const css::uno::Any& rNewValue = css::uno::Any(),
181 const css::uno::Any& rOldValue = css::uno::Any() ) const;
183 /** Functor adapter for ForEach template
185 Adapts giving functor such that only the paragraph objects
186 are accessed and the fact that our children are held
187 weakly is hidden
189 The functor must provide the following method:
190 void operator() ( AccessibleEditablePara& )
193 template < typename Functor > class WeakChildAdapter : public ::std::unary_function< const WeakChild&, void >
195 public:
196 WeakChildAdapter( Functor& rFunctor ) : mrFunctor(rFunctor) {}
197 void operator()( const WeakChild& rPara )
199 // retrieve hard reference from weak one
200 auto aHardRef( rPara.first.get() );
202 if( aHardRef.is() )
203 mrFunctor( *aHardRef );
206 private:
207 Functor& mrFunctor;
210 /** Adapter for unary member functions
212 Since STL's binder don't work with const& arguments (and
213 BOOST's neither, at least on MSVC), have to provide our
214 own adapter for unary member functions.
216 Create with pointer to member function of
217 AccessibleEditableTextPara and the corresponding argument.
219 template < typename Argument > class MemFunAdapter : public ::std::unary_function< const WeakChild&, void >
221 public:
222 typedef void (::accessibility::AccessibleEditableTextPara::*FunctionPointer)( Argument );
224 MemFunAdapter( FunctionPointer aFunPtr, Argument aArg ) : maFunPtr(aFunPtr), maArg(aArg) {}
225 void operator()( const WeakChild& rPara )
227 // retrieve hard reference from weak one
228 auto aHardRef( rPara.first.get() );
230 if( aHardRef.is() )
231 (*aHardRef.*maFunPtr)( maArg );
234 private:
235 FunctionPointer maFunPtr;
236 Argument maArg;
239 private:
240 /// Set state on given child
241 void SetState( sal_Int32 nChild, const sal_Int16 nStateId );
242 /// Unset state on given child
243 void UnSetState( sal_Int32 nChild, const sal_Int16 nStateId );
244 /// Init child with default state (as stored in previous SetFocus and SetActive calls)
245 void InitChild( AccessibleEditableTextPara& rChild,
246 SvxEditSourceAdapter& rEditSource,
247 sal_Int32 nChild,
248 sal_Int32 nParagraphIndex ) const;
250 // vector the size of the paragraph number of the underlying EditEngine
251 VectorOfChildren maChildren;
253 /// Additional states that will be set at every created child object.
254 VectorOfStates maChildStates;
256 // cache EE offset for child creation
257 Point maEEOffset;
259 // which child currently has the focus (-1 for none)
260 sal_Int32 mnFocusedChild;
262 // whether children are active and editable
263 bool mbActive;
266 } // end of namespace accessibility
268 #endif
270 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */