1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sd.hxx"
26 #include <tools/debug.hxx>
27 #include <svx/svdobj.hxx>
28 #include "shapelist.hxx"
29
30 #include <algorithm>
31
32 using namespace sd;
33
ShapeList()34 ShapeList::ShapeList()
35 {
36 maIter = maShapeList.end();
37 }
38
~ShapeList()39 ShapeList::~ShapeList()
40 {
41 clear();
42 }
43
44 /** adds the given shape to this list */
addShape(SdrObject & rObject)45 void ShapeList::addShape( SdrObject& rObject )
46 {
47 ListImpl::iterator aIter( std::find( maShapeList.begin(), maShapeList.end(), &rObject ) );
48 if( aIter == maShapeList.end() )
49 {
50 maShapeList.push_back(&rObject);
51 rObject.AddObjectUser( *this );
52 }
53 else
54 {
55 DBG_ERROR("sd::ShapeList::addShape(), given shape already part of list!");
56 }
57 }
58
59 /** removes the given shape from this list */
removeShape(SdrObject & rObject)60 SdrObject* ShapeList::removeShape( SdrObject& rObject )
61 {
62 ListImpl::iterator aIter( std::find( maShapeList.begin(), maShapeList.end(), &rObject ) );
63 if( aIter != maShapeList.end() )
64 {
65 bool bIterErased = aIter == maIter;
66
67 (*aIter)->RemoveObjectUser(*this);
68 aIter = maShapeList.erase( aIter );
69
70 if( bIterErased )
71 maIter = aIter;
72
73 if( aIter != maShapeList.end() )
74 return (*aIter);
75 }
76 else
77 {
78 DBG_ERROR("sd::ShapeList::removeShape(), given shape not part of list!");
79 }
80 return 0;
81 }
82
83 /** removes all shapes from this list
84 NOTE: iterators will become invalid */
clear()85 void ShapeList::clear()
86 {
87 ListImpl aShapeList;
88 aShapeList.swap( maShapeList );
89
90 ListImpl::iterator aIter( aShapeList.begin() );
91 while( aIter != aShapeList.end() )
92 (*aIter++)->RemoveObjectUser(*this);
93
94 maIter = aShapeList.end();
95 }
96
97 /** returns true if this list is empty */
isEmpty() const98 bool ShapeList::isEmpty() const
99 {
100 return maShapeList.empty();
101 }
102
103 /** returns true if given shape is part of this list */
hasShape(SdrObject & rObject) const104 bool ShapeList::hasShape( SdrObject& rObject ) const
105 {
106 return std::find( maShapeList.begin(), maShapeList.end(), &rObject ) != maShapeList.end();
107 }
108
getNextShape(SdrObject * pObj) const109 SdrObject* ShapeList::getNextShape(SdrObject* pObj) const
110 {
111 if( pObj )
112 {
113 ListImpl::const_iterator aIter( std::find( maShapeList.begin(), maShapeList.end(), pObj ) );
114 if( aIter != maShapeList.end() )
115 {
116 aIter++;
117 if( aIter != maShapeList.end() )
118 {
119 return (*aIter);
120 }
121 }
122 }
123 else if( !maShapeList.empty() )
124 {
125 return (*maShapeList.begin());
126 }
127
128 return 0;
129 }
130
ObjectInDestruction(const SdrObject & rObject)131 void ShapeList::ObjectInDestruction(const SdrObject& rObject)
132 {
133 ListImpl::iterator aIter( std::find( maShapeList.begin(), maShapeList.end(), &rObject ) );
134 if( aIter != maShapeList.end() )
135 {
136 bool bIterErased = aIter == maIter;
137
138 aIter = maShapeList.erase( aIter );
139
140 if( bIterErased )
141 maIter = aIter;
142 }
143 else
144 {
145 DBG_ERROR("sd::ShapeList::ObjectInDestruction(), got a call from an unknown friend!");
146 }
147 }
148
getNextShape()149 SdrObject* ShapeList::getNextShape()
150 {
151 if( maIter != maShapeList.end() )
152 {
153 return (*maIter++);
154 }
155 else
156 {
157 return 0;
158 }
159 }
160
seekShape(sal_uInt32 nIndex)161 void ShapeList::seekShape( sal_uInt32 nIndex )
162 {
163 maIter = maShapeList.begin();
164 while( nIndex-- && (maIter != maShapeList.end()) )
165 maIter++;
166 }
167
hasMore() const168 bool ShapeList::hasMore() const
169 {
170 return maIter != maShapeList.end();
171 }
172