1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 _SVX_ACCESSIBILITY_CHILDREN_MANAGER_IMPL_HXX
22 #include <svx/IAccessibleViewForwarderListener.hxx>
23 #include <svx/IAccessibleParent.hxx>
24 #include <svx/AccessibleShapeTreeInfo.hxx>
25 #include <editeng/AccessibleContextBase.hxx>
26 #include <cppuhelper/compbase2.hxx>
27 #include <osl/mutex.hxx>
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 send 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>
74 class ChildrenManagerImpl
76 public cppu::WeakComponentImplHelper2
<
77 ::com::sun::star::document::XEventListener
,
78 ::com::sun::star::view::XSelectionChangeListener
>,
79 public IAccessibleViewForwarderListener
,
80 public IAccessibleParent
83 /** Create a children manager, which manages the children of the given
84 parent. The parent is used for creating accessible objects. The
85 list of shapes for which to create those objects is not derived from
86 the parent and has to be provided separately by calling one of the
89 The parent of the accessible objects which will be created
90 on demand at some point of time in the future.
92 List of UNO shapes to manage.
94 Bundel of information passed down the shape tree.
96 An accessible context object that is called for fireing events
97 for new and deleted children, i.e. that holds a list of
98 listeners to be informed.
100 ChildrenManagerImpl (const ::com::sun::star::uno::Reference
<
101 ::com::sun::star::accessibility::XAccessible
>& rxParent
,
102 const ::com::sun::star::uno::Reference
<
103 ::com::sun::star::drawing::XShapes
>& rxShapeList
,
104 const AccessibleShapeTreeInfo
& rShapeTreeInfo
,
105 AccessibleContextBase
& rContext
);
107 /** If there still are managed children these are disposed and
110 ~ChildrenManagerImpl (void);
112 /** Do that part of the initialization that you can not or should not do
113 in the constructor like registering at broadcasters.
117 /** Return the number of currently visible accessible children.
119 If there are no children a 0 is returned.
121 long GetChildCount (void) const throw ();
123 /** Return the requested accessible child or throw and
124 IndexOutOfBoundsException if the given index is invalid.
126 Index of the requested child. Call getChildCount for obtaining
127 the number of children.
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
134 Throws an IndexOutOfBoundsException if the index is not valid.
136 ::com::sun::star::uno::Reference
<
137 ::com::sun::star::accessibility::XAccessible
>
138 GetChild (long nIndex
)
139 throw (::com::sun::star::uno::RuntimeException
,
140 ::com::sun::star::lang::IndexOutOfBoundsException
);
142 /** Return the requested accessible child.
143 @param aChildDescriptor
144 This object contains references to the original shape and its
145 associated accessible object.
147 The index which will be used in getAccessibleIndexInParent of the accessible shape.
149 Returns a reference to the requested accessible child. This
150 reference is empty if it has not been possible to create the
151 accessible object of the corresponding shape.
153 ::com::sun::star::uno::Reference
<
154 ::com::sun::star::accessibility::XAccessible
>
155 GetChild (ChildDescriptor
& aChildDescriptor
,sal_Int32 _nIndex
)
156 throw (::com::sun::star::uno::RuntimeException
);
158 /** Update the child manager. Take care of a modified set of children
159 and modified visible area. This method can optimize the update
160 process with respect separate updates of a modified children list
162 @param bCreateNewObjectsOnDemand
163 If </true> then accessible objects associated with the visible
164 shapes are created only when asked for. No event is sent on
165 creation. If </false> then the accessible objects are created
166 before this method returns and events are sent to inform the
167 listeners of the new object.
169 void Update (bool bCreateNewObjectsOnDemand
= true);
171 /** Set the list of UNO shapes to the given list. This removes the old
172 list and does not add to it. The list of accessible shapes that is
173 build up by calls to <member>AddAccessibleShape</member> is not
174 modified. Neither is the list of visible children. Accessible
175 objects are created on demand.
177 The list of UNO shapes that replaces the old list.
179 void SetShapeList (const ::com::sun::star::uno::Reference
<
180 ::com::sun::star::drawing::XShapes
>& xShapeList
);
182 /** Add a accessible shape. This does not modify the list of UNO shapes
183 or the list of visible shapes. Accessible shapes are, at the
184 moment, not tested against the visible area but are always appended
185 to the list of visible children.
187 The new shape that is added to the list of accessible shapes.
189 void AddAccessibleShape (std::auto_ptr
<AccessibleShape
> pShape
);
191 /** Clear the lists of accessible shapes and that of visible accessible
192 shapes. The list of UNO shapes is not modified.
194 void ClearAccessibleShapeList (void);
196 /** Set a new event shape tree info. Call this method to inform the
197 children manager of a change of the info bundle.
198 @param rShapeTreeInfo
199 The new info that replaces the current one.
201 void SetInfo (const AccessibleShapeTreeInfo
& rShapeTreeInfo
);
203 /** Update the SELECTED and FOCUSED states of all visible children
204 according to the given selection. This includes setting
205 <em>and</em> resetting the states.
207 void UpdateSelection (void);
209 /** Return whether one of the shapes managed by this object has
212 Returns <true/> when there is a shape that has the focus and
213 <false/> when there is no such shape.
215 bool HasFocus (void);
217 /** When there is a shape that currently has the focus,
218 i.e. <member>HasFocus()</member> returns <true/> then remove the
219 focus from that shape. Otherwise nothing changes.
221 void RemoveFocus (void);
223 //===== lang::XEventListener ============================================
225 virtual void SAL_CALL
226 disposing (const ::com::sun::star::lang::EventObject
& rEventObject
)
227 throw (::com::sun::star::uno::RuntimeException
);
230 //===== document::XEventListener ========================================
232 virtual void SAL_CALL
233 notifyEvent (const ::com::sun::star::document::EventObject
& rEventObject
)
234 throw (::com::sun::star::uno::RuntimeException
);
237 //===== view::XSelectionChangeListener ==================================
239 virtual void SAL_CALL
240 selectionChanged (const ::com::sun::star::lang::EventObject
& rEvent
)
241 throw (::com::sun::star::uno::RuntimeException
);
244 //===== IAccessibleViewForwarderListener ================================
246 /** Informs this children manager and its children about a change of one
247 (or more) aspect of the view forwarder.
249 A change type of <const>VISIBLE_AREA</const> leads to a call to
250 the <member>Update</member> which creates accessible objects of
251 new shapes immediately. Other change types are passed to the
252 visible accessible children without calling
253 <member>Update</member>.
254 @param pViewForwarder
255 The modified view forwarder. Use this one from now on.
257 virtual void ViewForwarderChanged (ChangeType aChangeType
,
258 const IAccessibleViewForwarder
* pViewForwarder
);
260 //===== IAccessibleParent ===============================================
262 /** Replace the specified child with a replacement.
264 This child is to be replaced.
266 The replacement for the current child.
268 The returned value indicates whether the replacement has been
269 finished successfully.
271 virtual sal_Bool
ReplaceChild (
272 AccessibleShape
* pCurrentChild
,
273 const ::com::sun::star::uno::Reference
< ::com::sun::star::drawing::XShape
>& _rxShape
,
275 const AccessibleShapeTreeInfo
& _rShapeTreeInfo
276 ) throw (::com::sun::star::uno::RuntimeException
);
280 /** This list holds the descriptors of all currently visible shapes and
281 associated accessible object.
283 <p>With the descriptors it maintains a mapping of shapes to
284 accessible objects. It acts as a cache in that accessible objects
285 are only created on demand and released with every update (where the
286 latter may be optimized by the update methods).<p>
288 <p>The list is realized as a vector because it remains unchanged
289 between updates (i.e. complete rebuilds of the list) and allows a
290 fast (constant time) access to its elements for given indices.</p>
292 ChildDescriptorListType maVisibleChildren
;
294 /** The original list of UNO shapes. The visible shapes are inserted
295 into the list of visible children
296 <member>maVisibleChildren</member>.
298 ::com::sun::star::uno::Reference
<
299 ::com::sun::star::drawing::XShapes
> mxShapeList
;
301 /** This list of additional accessible shapes that can or shall not be
302 created by the shape factory.
304 typedef std::vector
< ::com::sun::star::uno::Reference
<
305 ::com::sun::star::accessibility::XAccessible
> > AccessibleShapeList
;
306 AccessibleShapeList maAccessibleShapes
;
308 /** Rectangle that describes the visible area in which a shape has to lie
309 at least partly, to be accessible through this class. Used to
310 detect changes of the visible area after changes of the view forwarder.
312 Rectangle maVisibleArea
;
314 /** The parent of the shapes. It is used for creating accessible
315 objects for given shapes.
317 ::com::sun::star::uno::Reference
<
318 ::com::sun::star::accessibility::XAccessible
> mxParent
;
320 /** Bundel of information passed down the shape tree.
322 AccessibleShapeTreeInfo maShapeTreeInfo
;
324 /** Reference to an accessible context object that is used to inform its
325 listeners of new and remved children.
327 AccessibleContextBase
& mrContext
;
329 /** This method is called from the component helper base class while
332 virtual void SAL_CALL
disposing (void);
334 void impl_dispose (void);
337 /** Names of new accessible objects are disambiguated with this index.
338 It gets increased every time a new object is created and (at the
341 sal_Int32 mnNewNameIndex
;
343 // Don't use the copy constructor or the assignment operator. They are
344 // not implemented (and are not intended to be).
345 ChildrenManagerImpl (const ChildrenManagerImpl
&);
346 ChildrenManagerImpl
& operator= (const ChildrenManagerImpl
&);
348 /** This member points to the currently focused shape. It is NULL when
349 there is no focused shape.
351 AccessibleShape
* mpFocusedShape
;
353 /** Three helper functions for the <member>Update</member> method.
356 /** Create a list of visible shapes from the list of UNO shapes
357 <member>maShapeList</member> and the list of accessible objects.
359 For every visible shape from the two sources mentioned above one
360 descriptor is added to this list.
362 void CreateListOfVisibleShapes (ChildDescriptorListType
& raChildList
);
364 /** From the old list of (former) visible shapes remove those that
365 are not member of the new list. Send appropriate events for every
367 @param raNewChildList
368 The new list of visible children against which the old one
370 @param raOldChildList
371 The old list of visible children against which the new one
374 void RemoveNonVisibleChildren (
375 const ChildDescriptorListType
& raNewChildList
,
376 ChildDescriptorListType
& raOldChildList
);
378 /** Merge the information that is already known about the visible shapes
379 from the current list into the new list.
381 Information is merged from the current list of visible children
384 void MergeAccessibilityInformation (ChildDescriptorListType
& raChildList
);
386 /** If the visible area has changed then send events that signal a
387 change of their bounding boxes for all shapes that are members of
388 both the current and the new list of visible shapes.
390 Events are sent to all entries of this list that already contain
391 an accessible object.
393 void SendVisibleAreaEvents (ChildDescriptorListType
& raChildList
);
395 /** If children have to be created immediately and not on demand the
396 create the missing accessible objects now.
397 @param raDescriptorList
398 Create an accessible object for every member of this list where
399 that obejct does not already exist.
401 void CreateAccessibilityObjects (ChildDescriptorListType
& raChildList
);
403 /** Add a single shape. Update all relevant data structures
404 accordingly. Use this method instead of <member>Update()</member>
405 when only a single shape has been added.
407 void AddShape (const ::com::sun::star::uno::Reference
<
408 ::com::sun::star::drawing::XShape
>& xShape
);
410 /** Remove a single shape. Update all relevant data structures
411 accordingly. Use this method instead of <member>Update()</member>
412 when only a single shape has been removed.
414 void RemoveShape (const ::com::sun::star::uno::Reference
<
415 ::com::sun::star::drawing::XShape
>& xShape
);
417 /** Add the children manager as dispose listener at the given shape so
418 that the associated accessible object can be disposed when the shape
421 Register at this shape as dispose listener.
423 void RegisterAsDisposeListener (const ::com::sun::star::uno::Reference
<
424 ::com::sun::star::drawing::XShape
>& xShape
);
426 /** Remove the children manager as dispose listener at the given shape
428 Unregister at this shape as dispose listener.
430 void UnregisterAsDisposeListener (const ::com::sun::star::uno::Reference
<
431 ::com::sun::star::drawing::XShape
>& xShape
);
437 /** A child descriptor holds a reference to a UNO shape and the
438 corresponding accessible object. There are two use cases:
439 <ol><li>The accessible object is only created on demand and is then
440 initially empty.</li>
441 <li>There is no UNO shape. The accessible object is given as argument
442 to the constructor.</li>
444 In both cases the child descriptor assumes ownership over the accessible
447 class ChildDescriptor
450 /** Reference to a (partially) visible shape.
452 ::com::sun::star::uno::Reference
<
453 ::com::sun::star::drawing::XShape
> mxShape
;
455 /** The corresponding accessible object. This reference is initially
456 empty and only replaced by a reference to a new object when that is
457 requested from the outside.
459 ::com::sun::star::uno::Reference
<
460 ::com::sun::star::accessibility::XAccessible
> mxAccessibleShape
;
462 /** Return a pointer to the implementation object of the accessible
463 shape of this descriptor.
465 The result is NULL if either the UNO reference to the accessible
466 shape is empty or it can not be transformed into a pointer to
469 AccessibleShape
* GetAccessibleShape (void) const;
471 /** set the index _nIndex at the accessible shape
473 The new index in parent.
475 void setIndexAtAccessibleShape(sal_Int32 _nIndex
);
477 /** This flag is set during the visibility calculation and indicates
478 that at one time in this process an event is sent that informs the
479 listners of the creation of a new accessible object. This flags is
480 not reset afterwards. Don't use it unless you know exactly what you
483 bool mbCreateEventPending
;
485 /** Create a new descriptor for the specified shape with empty reference
486 to accessible object.
488 explicit ChildDescriptor (const ::com::sun::star::uno::Reference
<
489 ::com::sun::star::drawing::XShape
>& xShape
);
491 /** Create a new descriptor for the specified shape with empty reference
492 to the original shape.
494 explicit ChildDescriptor (const ::com::sun::star::uno::Reference
<
495 ::com::sun::star::accessibility::XAccessible
>& rxAccessibleShape
);
497 ~ChildDescriptor (void);
499 /** Dispose the accessible object of this descriptor. If that object
500 does not exist then do nothing.
502 The parent of the accessible object to dispose. A child event
505 void disposeAccessibleObject (AccessibleContextBase
& rParent
);
507 /** Compare two child descriptors. Take into account that a child
508 descriptor may be based on a UNO shape or, already, on an accessible
511 inline bool operator == (const ChildDescriptor
& aDescriptor
) const
514 this == &aDescriptor
||
516 (mxShape
.get() == aDescriptor
.mxShape
.get() ) &&
517 (mxShape
.is() || mxAccessibleShape
.get() == aDescriptor
.mxAccessibleShape
.get())
522 /** The ordering defined by this operator is only used in order to be able
523 to put child descriptors in some STL containers. The ordering itself is
524 not so important, its 'features' are not used.
526 inline bool operator < (const ChildDescriptor
& aDescriptor
) const
528 return (mxShape
.get() < aDescriptor
.mxShape
.get());
535 } // end of namespace accessibility
539 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */