1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef SD_OUTLINER_ITERATOR_IMPL_HXX
29 #define SD_OUTLINER_ITERATOR_IMPL_HXX
30 
31 #include <svx/svdobj.hxx>
32 #include "OutlinerIterator.hxx"
33 #include <boost/weak_ptr.hpp>
34 
35 class SdDrawDocument;
36 class SdPage;
37 class SdrObjListIter;
38 
39 namespace sd {
40 
41 class ViewShell;
42 
43 namespace outliner {
44 
45 class IteratorImplBase;
46 
47 /** Base class for the polymorphic implementation class of the
48     <type>Iterator</type> class.  The iterators based on this class are
49     basically uni directional iterators.  Their direction can, however, be
50     reversed at any point of their life time.
51 */
52 class IteratorImplBase
53 {
54 public:
55     /** The constructor stores the given arguments to be used by the derived
56         classes.
57         @param pDocument
58             The document provides the information to be iterated on.
59         @param pViewShellWeak
60             Some information has to be taken from the view shell.
61         @param bDirectionIsForward
62             This flag defines the iteration direction.  When <TRUE/> then
63             the direction is forwards otherwise it is backwards.
64     */
65     IteratorImplBase (SdDrawDocument* pDocument,
66         const ::boost::weak_ptr<ViewShell>& rpViewShellWeak,
67         bool bDirectionIsForward);
68     IteratorImplBase (SdDrawDocument* pDocument,
69         const ::boost::weak_ptr<ViewShell>& rpViewShellWeak,
70         bool bDirectionIsForward, PageKind ePageKind, EditMode eEditMode);
71     virtual ~IteratorImplBase (void);
72 
73     /** Advance to the next text of the current object or to the next object.
74 	    This takes the iteration direction into
75         account.  The new object pointed to can be retrieved (among other
76         information) by calling the <member>GetPosition</member> method.
77     */
78     virtual void GotoNextText (void) = 0;
79     /** Return an object that describes the current object.
80         @return
81             The returned object describes the current object pointed to by
82             the iterator.  See the description of
83             <type>IteratorPosition</type> for details on the available
84             information.
85     */
86     virtual const IteratorPosition& GetPosition (void);
87     /** Create an exact copy of this object.  No argument should be
88         specified when called from the outside.  It then creates an object
89         first and passes that to the inherited <member>Clone()</member>
90         methods to fill in class specific information.
91         @return
92             Returns a copy of this object.  When this method is called with
93             an argument then this value will be returned.
94     */
95     virtual IteratorImplBase* Clone (IteratorImplBase* pObject=NULL) const;
96     /** Test the equality of the this object and the given iterator.  Two
97         iterators are taken to be equal when they point to the same object.
98         Iteration direction is not taken into account.
99         @param rIterator
100             The iterator to compare to.
101         @return
102             When both iterators ar equal <TRUE/> is returned, <FALSE/> otherwise.
103     */
104     virtual bool operator== (const IteratorImplBase& rIterator) const;
105     /** This method is used by the equality operator.  Additionaly to the
106         iterator it takes a type information which is taken into account on
107         comparison.  It is part of a "multimethod" pattern.
108         @param rIterator
109             The iterator to compare to.
110         @param aType
111             The type of the iterator.
112         @return
113             Returns <TRUE/> when both iterators point to the same object.
114     */
115     virtual bool IsEqual (const IteratorImplBase& rIterator, IteratorType aType) const;
116     /** Reverse the direction of iteration.  The current object stays the same.
117     */
118     virtual void Reverse (void);
119 
120 protected:
121     /// The current position as returned by <member>GetPosition()</member>.
122     IteratorPosition maPosition;
123     /// The document on whose data the iterator operates.
124     SdDrawDocument* mpDocument;
125     /// Necessary secondary source of information.
126     ::boost::weak_ptr<ViewShell> mpViewShellWeak;
127     /// Specifies the search direction.
128     bool mbDirectionIsForward;
129 };
130 
131 
132 
133 
134 /** Iterator all objects that belong to the current mark list
135     a.k.a. selection.  It is assumed that all marked objects belong to the
136     same page.  It is further assumed that the mark list does not change
137     while an iterator is alive.  It is therefore the responsibility of an
138     iterator's owner to handle the case of a changed mark list.
139 
140     <p>For documentation of the methods please refere to the base class
141     <type>IteratorImplBase</type>.</p>
142 */
143 class SelectionIteratorImpl
144     : public IteratorImplBase
145 {
146 public:
147     SelectionIteratorImpl (
148         const ::std::vector< SdrObjectWeakRef >& rObjectList,
149         sal_Int32 nObjectIndex,
150         SdDrawDocument* pDocument,
151         const ::boost::weak_ptr<ViewShell>& rpViewShellWeak,
152         bool bDirectionIsForward);
153     SelectionIteratorImpl (const SelectionIteratorImpl& rObject);
154     virtual ~SelectionIteratorImpl (void);
155 
156     virtual void GotoNextText (void);
157     virtual const IteratorPosition& GetPosition (void);
158     virtual IteratorImplBase* Clone (IteratorImplBase* pObject) const;
159     virtual bool operator== (const IteratorImplBase& rIterator) const;
160 
161 private:
162     const ::std::vector<SdrObjectWeakRef>& mrObjectList;
163     sal_Int32 mnObjectIndex;
164 
165     /** Compare the given iterator with this object.  This method handles
166         only the case that the given iterator is an instance of this class.
167         @param rIterator
168             The iterator to compare to.
169         @param aType
170             The type of the iterator.
171         @return
172             Returns <TRUE/> when both iterators point to the same object.
173     */
174     virtual bool IsEqual (const IteratorImplBase& rIterator, IteratorType aType) const;
175 
176     IteratorImplBase& operator= (const IteratorImplBase& rIterator);
177 };
178 
179 
180 /** Iterator for iteration over all objects in a single view.  On reaching
181     the last object on the last page (or the first object on the first page)
182     the view is *not* switched.  Further calls to the
183     <member>GotoNextObject()</member> method will be ignored.
184 
185     <p>For documentation of the methods please refere to the base class
186     <type>IteratorImplBase</type>.</p>
187 */
188 class ViewIteratorImpl : public IteratorImplBase
189 {
190 public:
191     ViewIteratorImpl (
192         sal_Int32 nPageIndex,
193         SdDrawDocument* pDocument,
194         const ::boost::weak_ptr<ViewShell>& rpViewShellWeak,
195         bool bDirectionIsForward);
196     ViewIteratorImpl (
197         sal_Int32 nPageIndex,
198         SdDrawDocument* pDocument,
199         const ::boost::weak_ptr<ViewShell>& rpViewShellWeak,
200         bool bDirectionIsForward,
201         PageKind ePageKind,
202         EditMode eEditMode);
203     virtual ~ViewIteratorImpl (void);
204 
205     virtual void GotoNextText (void);
206     virtual IteratorImplBase* Clone (IteratorImplBase* pObject) const;
207     virtual void Reverse (void);
208 
209 protected:
210     /// Number of pages in the view that is specified by <member>maPosition</member>.
211     sal_Int32 mnPageCount;
212 
213     /** Initialize this iterator with respect to the given location.  After
214         this call the object looks like newly constructed.
215     */
216     void Init (IteratorLocation aLocation);
217 
218     /** Set up page pointer and object list iterator for the specified
219         page.
220         @param nPageIndex
221             Index of the new page.  It may lie outside the valid range for
222             page indices.
223     */
224     void SetPage (sal_Int32 nPageIndex);
225 
226 private:
227     /// Indicates whether a page changed occured on switching to current page.
228     bool mbPageChangeOccured;
229     /// Pointer to the page associated with the current page index. May be NULL.
230     SdPage* mpPage;
231     /// Iterator of all objects on the current page.
232     SdrObjListIter* mpObjectIterator;
233 
234     // Don't use this operator.
235     ViewIteratorImpl& operator= (const ViewIteratorImpl&){return *this;};
236 };
237 
238 
239 
240 
241 /** Iterator for iteration over all objects in all views.  It automatically
242     switches views when reaching the end/beginning of a view.
243 
244     <p>For documentation of the methods please refere to the base class
245     <type>IteratorImplBase</type>.</p>
246 */
247 class DocumentIteratorImpl : public ViewIteratorImpl
248 {
249 public:
250     DocumentIteratorImpl (
251         sal_Int32 nPageIndex,
252         PageKind ePageKind,
253         EditMode eEditMode,
254         SdDrawDocument* pDocument,
255         const ::boost::weak_ptr<ViewShell>& rpViewShellWeak,
256         bool bDirectionIsForward);
257     virtual ~DocumentIteratorImpl (void);
258 
259     virtual void GotoNextText (void);
260     virtual IteratorImplBase* Clone (IteratorImplBase* pObject) const;
261 
262 private:
263     sal_Int32 mnPageCount;
264 
265     // Don't use this operator.
266     DocumentIteratorImpl& operator= (const DocumentIteratorImpl& ){return *this;};
267 };
268 
269 
270 } } // end of namespace ::sd::outliner
271 
272 #endif
273