Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / accessibility / ChildrenManagerImpl.hxx
blob2de34e10da165f9261ebf2121bd6b7b99af859b1
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_SVX_SOURCE_ACCESSIBILITY_CHILDRENMANAGERIMPL_HXX
21 #define INCLUDED_SVX_SOURCE_ACCESSIBILITY_CHILDRENMANAGERIMPL_HXX
23 #include <svx/IAccessibleViewForwarderListener.hxx>
24 #include <svx/IAccessibleParent.hxx>
25 #include <svx/AccessibleShapeTreeInfo.hxx>
26 #include <editeng/AccessibleContextBase.hxx>
27 #include <comphelper/compbase.hxx>
28 #include <tools/gen.hxx>
29 #include <vector>
30 #include <com/sun/star/drawing/XShape.hpp>
31 #include <com/sun/star/drawing/XShapes.hpp>
32 #include <com/sun/star/document/XEventListener.hpp>
33 #include <com/sun/star/view/XSelectionChangeListener.hpp>
34 #include <com/sun/star/accessibility/XAccessible.hpp>
36 namespace accessibility {
38 class AccessibleShape;
40 class ChildDescriptor; // See below for declaration.
41 typedef ::std::vector<ChildDescriptor> ChildDescriptorListType;
43 // Re-using MutexOwner class defined in AccessibleContextBase.hxx
45 /** This class contains the actual implementation of the children manager.
47 <p>It maintains a set of visible accessible shapes in
48 <member>maVisibleChildren</member>. The objects in this list stem from
49 two sources. The first is a list of UNO shapes like the list of shapes
50 in a draw page. A reference to this list is held in
51 <member>maShapeList</member>. Accessible objects for these shapes are
52 created on demand. The list can be replaced by calls to the
53 <member>SetShapeList</member> method. The second source is a list of
54 already accessible objects. It can be modified by calls to the
55 <member>AddAccessibleShape</member> and
56 <member>ClearAccessibleShapeList</member> methods.</p>
58 <p>Each call of the <member>Update</member> method leads to a
59 re-calculation of the visible shapes which then can be queried with the
60 <member>GetChildCount</member> and <member>GetChild</member> methods.
61 Events are sent informing all listeners about the removed shapes which are
62 not visible anymore and about the added shapes.</p>
64 <p> The visible area which is used to determine the visibility of the
65 shapes is taken from the view forwarder. Thus, to signal a change of
66 the visible area call <member>ViewForwarderChanged</member>.</p>
68 <p>The children manager adds itself as disposing() listener at every UNO
69 shape it creates an accessible object for so that when the UNO shape
70 passes away it can dispose() the associated accessible object.</p>
72 @see ChildrenManager
74 class ChildrenManagerImpl final
75 : public comphelper::WeakComponentImplHelper<
76 css::document::XEventListener,
77 css::view::XSelectionChangeListener>,
78 public IAccessibleViewForwarderListener,
79 public IAccessibleParent
81 public:
82 /** Create a children manager, which manages the children of the given
83 parent. The parent is used for creating accessible objects. The
84 list of shapes for which to create those objects is not derived from
85 the parent and has to be provided separately by calling one of the
86 update methods.
87 @param rxParent
88 The parent of the accessible objects which will be created
89 on demand at some point of time in the future.
90 @param rxShapeList
91 List of UNO shapes to manage.
92 @param rShapeTreeInfo
93 Bundle of information passed down the shape tree.
94 @param rContext
95 An accessible context object that is called for firing events
96 for new and deleted children, i.e. that holds a list of
97 listeners to be informed.
99 ChildrenManagerImpl (css::uno::Reference<css::accessibility::XAccessible> xParent,
100 css::uno::Reference<css::drawing::XShapes> xShapeList,
101 const AccessibleShapeTreeInfo& rShapeTreeInfo,
102 AccessibleContextBase& rContext);
104 /** If there still are managed children these are disposed and
105 released.
107 virtual ~ChildrenManagerImpl() override;
109 /** Do that part of the initialization that you can not or should not do
110 in the constructor like registering at broadcasters.
112 void Init();
114 /** Return the number of currently visible accessible children.
115 @return
116 If there are no children a 0 is returned.
118 sal_Int64 GetChildCount() const noexcept;
120 /// @throws css::uno::RuntimeException
121 /// @throws css::lang::IndexOutOfBoundsException
122 const css::uno::Reference<css::drawing::XShape>& GetChildShape(sal_Int64 nIndex);
123 /** Return the requested accessible child or throw and
124 IndexOutOfBoundsException if the given index is invalid.
125 @param nIndex
126 Index of the requested child. Call getChildCount for obtaining
127 the number of children.
128 @return
129 In case of a valid index this method returns a reference to the
130 requested accessible child. This reference is empty if it has
131 not been possible to create the accessible object of the
132 corresponding shape.
133 @throws
134 Throws an IndexOutOfBoundsException if the index is not valid.
136 css::uno::Reference<css::accessibility::XAccessible>
137 GetChild (sal_Int64 nIndex);
139 /** Return the requested accessible child.
140 @param aChildDescriptor
141 This object contains references to the original shape and its
142 associated accessible object.
143 @param _nIndex
144 The index which will be used in getAccessibleIndexInParent of the accessible shape.
145 @return
146 Returns a reference to the requested accessible child. This
147 reference is empty if it has not been possible to create the
148 accessible object of the corresponding shape.
149 @throws css::uno::RuntimeException
151 css::uno::Reference<css::accessibility::XAccessible>
152 GetChild (ChildDescriptor& aChildDescriptor,sal_Int32 _nIndex);
154 /** Update the child manager. Take care of a modified set of children
155 and modified visible area. This method can optimize the update
156 process with respect separate updates of a modified children list
157 and visible area.
158 @param bCreateNewObjectsOnDemand
159 If </true> then accessible objects associated with the visible
160 shapes are created only when asked for. No event is sent on
161 creation. If </false> then the accessible objects are created
162 before this method returns and events are sent to inform the
163 listeners of the new object.
165 void Update (bool bCreateNewObjectsOnDemand);
167 /** Set the list of UNO shapes to the given list. This removes the old
168 list and does not add to it. The list of accessible shapes that is
169 build up by calls to <member>AddAccessibleShape</member> is not
170 modified. Neither is the list of visible children. Accessible
171 objects are created on demand.
172 @param xShapeList
173 The list of UNO shapes that replaces the old list.
175 void SetShapeList (const css::uno::Reference<css::drawing::XShapes>& xShapeList);
177 /** Add an accessible shape. This does not modify the list of UNO shapes
178 or the list of visible shapes. Accessible shapes are, at the
179 moment, not tested against the visible area but are always appended
180 to the list of visible children.
181 @param shape
182 The new shape that is added to the list of accessible shapes; must
183 be non-null.
185 void AddAccessibleShape (rtl::Reference<AccessibleShape> const & shape);
187 /** Clear the lists of accessible shapes and that of visible accessible
188 shapes. The list of UNO shapes is not modified.
190 void ClearAccessibleShapeList();
192 /** Set a new event shape tree info. Call this method to inform the
193 children manager of a change of the info bundle.
194 @param rShapeTreeInfo
195 The new info that replaces the current one.
197 void SetInfo (const AccessibleShapeTreeInfo& rShapeTreeInfo);
199 /** Update the SELECTED and FOCUSED states of all visible children
200 according to the given selection. This includes setting
201 <em>and</em> resetting the states.
203 void UpdateSelection();
205 /** Return whether one of the shapes managed by this object has
206 currently the focus.
207 @return
208 Returns <true/> when there is a shape that has the focus and
209 <false/> when there is no such shape.
211 bool HasFocus() const;
213 /** When there is a shape that currently has the focus,
214 i.e. <member>HasFocus()</member> returns <true/> then remove the
215 focus from that shape. Otherwise nothing changes.
217 void RemoveFocus();
219 // lang::XEventListener
220 virtual void SAL_CALL
221 disposing (const css::lang::EventObject& rEventObject) override;
223 // document::XEventListener
224 virtual void SAL_CALL
225 notifyEvent (const css::document::EventObject& rEventObject) override;
227 // view::XSelectionChangeListener
228 virtual void SAL_CALL
229 selectionChanged (const css::lang::EventObject& rEvent) override;
231 // IAccessibleViewForwarderListener
232 /** Informs this children manager and its children about a change of one
233 (or more) aspect of the view forwarder.
234 @param aChangeType
235 A change type of <const>VISIBLE_AREA</const> leads to a call to
236 the <member>Update</member> which creates accessible objects of
237 new shapes immediately. Other change types are passed to the
238 visible accessible children without calling
239 <member>Update</member>.
240 @param pViewForwarder
241 The modified view forwarder. Use this one from now on.
243 virtual void ViewForwarderChanged() override;
245 // IAccessibleParent
246 /** Replace the specified child with a replacement.
247 @param pCurrentChild
248 This child is to be replaced.
249 @param pReplacement
250 The replacement for the current child.
251 @return
252 The returned value indicates whether the replacement has been
253 finished successfully.
255 virtual bool ReplaceChild (
256 AccessibleShape* pCurrentChild,
257 const css::uno::Reference< css::drawing::XShape >& _rxShape,
258 const tools::Long _nIndex,
259 const AccessibleShapeTreeInfo& _rShapeTreeInfo
260 ) override;
262 // Add the impl method for IAccessibleParent interface
263 virtual AccessibleControlShape* GetAccControlShapeFromModel
264 (css::beans::XPropertySet* pSet) override;
265 virtual css::uno::Reference<css::accessibility::XAccessible>
266 GetAccessibleCaption (const css::uno::Reference<css::drawing::XShape>& xShape) override;
268 private:
269 /** This list holds the descriptors of all currently visible shapes and
270 associated accessible object.
272 <p>With the descriptors it maintains a mapping of shapes to
273 accessible objects. It acts as a cache in that accessible objects
274 are only created on demand and released with every update (where the
275 latter may be optimized by the update methods).<p>
277 <p>The list is realized as a vector because it remains unchanged
278 between updates (i.e. complete rebuilds of the list) and allows a
279 fast (constant time) access to its elements for given indices.</p>
281 ChildDescriptorListType maVisibleChildren;
283 /** The original list of UNO shapes. The visible shapes are inserted
284 into the list of visible children
285 <member>maVisibleChildren</member>.
287 css::uno::Reference<css::drawing::XShapes> mxShapeList;
289 /** This list of additional accessible shapes that can or shall not be
290 created by the shape factory.
292 typedef std::vector< rtl::Reference< AccessibleShape> > AccessibleShapeList;
293 AccessibleShapeList maAccessibleShapes;
295 /** Rectangle that describes the visible area in which a shape has to lie
296 at least partly, to be accessible through this class. Used to
297 detect changes of the visible area after changes of the view forwarder.
299 tools::Rectangle maVisibleArea;
301 /** The parent of the shapes. It is used for creating accessible
302 objects for given shapes.
304 css::uno::Reference<css::accessibility::XAccessible> mxParent;
306 /** Bundle of information passed down the shape tree.
308 AccessibleShapeTreeInfo maShapeTreeInfo;
310 /** Reference to an accessible context object that is used to inform its
311 listeners of new and removed children.
313 AccessibleContextBase& mrContext;
315 /** This method is called from the component helper base class while
316 disposing.
318 virtual void disposing(std::unique_lock<std::mutex>&) override;
320 void impl_dispose();
322 ChildrenManagerImpl (const ChildrenManagerImpl&) = delete;
323 ChildrenManagerImpl& operator= (const ChildrenManagerImpl&) = delete;
325 /** This member points to the currently focused shape. It is NULL when
326 there is no focused shape.
328 AccessibleShape* mpFocusedShape;
330 /** Three helper functions for the <member>Update</member> method.
333 /** Create a list of visible shapes from the list of UNO shapes
334 <member>maShapeList</member> and the list of accessible objects.
335 @param raChildList
336 For every visible shape from the two sources mentioned above one
337 descriptor is added to this list.
339 void CreateListOfVisibleShapes (ChildDescriptorListType& raChildList);
341 /** From the old list of (former) visible shapes remove those that
342 are not member of the new list. Send appropriate events for every
343 such shape.
344 @param raNewChildList
345 The new list of visible children against which the old one
346 is compared.
347 @param raOldChildList
348 The old list of visible children against which the new one
349 is compared.
351 void RemoveNonVisibleChildren (
352 const std::vector<ChildDescriptor*>& rNonVisibleChildren);
354 /** Merge the information that is already known about the visible shapes
355 from the old list into the current list, and return a list of
356 children that are in the old list, but not the current one.
357 @param raChildList
358 Information is merged to the current list of visible children
359 from this list. The old list can get reordered.
360 @return
361 Vector of children that are in the old list, but not the current
362 one.
364 std::vector<ChildDescriptor*> MergeAccessibilityInformation (ChildDescriptorListType& raChildList);
366 /** If the visible area has changed then send events that signal a
367 change of their bounding boxes for all shapes that are members of
368 both the current and the new list of visible shapes.
369 @param raChildList
370 Events are sent to all entries of this list that already contain
371 an accessible object.
373 static void SendVisibleAreaEvents (ChildDescriptorListType& raChildList);
375 /** If children have to be created immediately and not on demand the
376 create the missing accessible objects now.
377 @param raDescriptorList
378 Create an accessible object for every member of this list where
379 that object does not already exist.
381 void CreateAccessibilityObjects (ChildDescriptorListType& raChildList);
383 /** Add a single shape. Update all relevant data structures
384 accordingly. Use this method instead of <member>Update()</member>
385 when only a single shape has been added.
387 void AddShape (const css::uno::Reference<css::drawing::XShape>& xShape);
389 /** Remove a single shape. Update all relevant data structures
390 accordingly. Use this method instead of <member>Update()</member>
391 when only a single shape has been removed.
393 void RemoveShape (const css::uno::Reference<css::drawing::XShape>& xShape);
395 /** Add the children manager as dispose listener at the given shape so
396 that the associated accessible object can be disposed when the shape
397 is disposed.
398 @param xShape
399 Register at this shape as dispose listener.
401 void RegisterAsDisposeListener (const css::uno::Reference<css::drawing::XShape>& xShape);
403 /** Remove the children manager as dispose listener at the given shape
404 @param xShape
405 Unregister at this shape as dispose listener.
407 void UnregisterAsDisposeListener (const css::uno::Reference<css::drawing::XShape>& xShape);
411 /** A child descriptor holds a reference to a UNO shape and the
412 corresponding accessible object. There are two use cases:
413 <ol><li>The accessible object is only created on demand and is then
414 initially empty.</li>
415 <li>There is no UNO shape. The accessible object is given as argument
416 to the constructor.</li>
417 </ol>
418 In both cases the child descriptor assumes ownership over the accessible
419 object.
421 class ChildDescriptor
423 public:
424 /** Reference to a (partially) visible shape.
426 css::uno::Reference<css::drawing::XShape> mxShape;
428 /** The corresponding accessible object. This reference is initially
429 empty and only replaced by a reference to a new object when that is
430 requested from the outside.
432 rtl::Reference<AccessibleShape> mxAccessibleShape;
434 /** Return a pointer to the implementation object of the accessible
435 shape of this descriptor.
436 @return
437 The result is NULL if either the UNO reference to the accessible
438 shape is empty or it can not be transformed into a pointer to
439 the desired class.
441 AccessibleShape* GetAccessibleShape() const { return mxAccessibleShape.get(); }
443 /** set the index _nIndex at the accessible shape
444 @param _nIndex
445 The new index in parent.
447 void setIndexAtAccessibleShape(sal_Int32 _nIndex);
449 /** This flag is set during the visibility calculation and indicates
450 that at one time in this process an event is sent that informs the
451 listeners of the creation of a new accessible object. This flags is
452 not reset afterwards. Don't use it unless you know exactly what you
453 are doing.
455 bool mbCreateEventPending;
457 /** Create a new descriptor for the specified shape with empty reference
458 to accessible object.
460 explicit ChildDescriptor (const css::uno::Reference<css::drawing::XShape>& xShape);
462 /** Create a new descriptor for the specified shape with empty reference
463 to the original shape.
465 explicit ChildDescriptor (const rtl::Reference<AccessibleShape>& rxAccessibleShape);
467 /** Dispose the accessible object of this descriptor. If that object
468 does not exist then do nothing.
469 @param rParent
470 The parent of the accessible object to dispose. A child event
471 is sent in its name.
473 void disposeAccessibleObject (AccessibleContextBase& rParent);
475 /** Compare two child descriptors. Take into account that a child
476 descriptor may be based on a UNO shape or, already, on an accessible
477 shape.
479 bool operator == (const ChildDescriptor& aDescriptor) const
481 return (
482 this == &aDescriptor ||
484 (mxShape.get() == aDescriptor.mxShape.get() ) &&
485 (mxShape.is() || mxAccessibleShape.get() == aDescriptor.mxAccessibleShape.get())
493 } // end of namespace accessibility
495 #endif
497 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */