xref: /AOO41X/main/sd/source/core/shapelist.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sd.hxx"
30 #include <tools/debug.hxx>
31 #include <svx/svdobj.hxx>
32 #include "shapelist.hxx"
33 
34 #include <algorithm>
35 
36 using namespace sd;
37 
38 ShapeList::ShapeList()
39 {
40 	maIter = maShapeList.end();
41 }
42 
43 ShapeList::~ShapeList()
44 {
45 	clear();
46 }
47 
48 /** adds the given shape to this list */
49 void ShapeList::addShape( SdrObject& rObject )
50 {
51 	ListImpl::iterator aIter( std::find( maShapeList.begin(), maShapeList.end(), &rObject ) );
52 	if( aIter == maShapeList.end() )
53 	{
54 		maShapeList.push_back(&rObject);
55 		rObject.AddObjectUser( *this );
56 	}
57 	else
58 	{
59 		DBG_ERROR("sd::ShapeList::addShape(), given shape already part of list!");
60 	}
61 }
62 
63 /** removes the given shape from this list */
64 SdrObject* ShapeList::removeShape( SdrObject& rObject )
65 {
66 	ListImpl::iterator aIter( std::find( maShapeList.begin(), maShapeList.end(), &rObject ) );
67 	if( aIter != maShapeList.end() )
68 	{
69 		bool bIterErased = aIter == maIter;
70 
71 		(*aIter)->RemoveObjectUser(*this);
72 		aIter = maShapeList.erase( aIter );
73 
74 		if( bIterErased )
75 			maIter = aIter;
76 
77 		if( aIter != maShapeList.end() )
78 			return (*aIter);
79 	}
80 	else
81 	{
82 		DBG_ERROR("sd::ShapeList::removeShape(), given shape not part of list!");
83 	}
84 	return 0;
85 }
86 
87 /** removes all shapes from this list
88 	NOTE: iterators will become invalid */
89 void ShapeList::clear()
90 {
91 	ListImpl aShapeList;
92 	aShapeList.swap( maShapeList );
93 
94 	ListImpl::iterator aIter( aShapeList.begin() );
95 	while( aIter != aShapeList.end() )
96 		(*aIter++)->RemoveObjectUser(*this);
97 
98 	maIter = aShapeList.end();
99 }
100 
101 /** returns true if this list is empty */
102 bool ShapeList::isEmpty() const
103 {
104 	return maShapeList.empty();
105 }
106 
107 /** returns true if given shape is part of this list */
108 bool ShapeList::hasShape( SdrObject& rObject ) const
109 {
110 	return std::find( maShapeList.begin(), maShapeList.end(), &rObject )  != maShapeList.end();
111 }
112 
113 SdrObject* ShapeList::getNextShape(SdrObject* pObj) const
114 {
115 	if( pObj )
116 	{
117 		ListImpl::const_iterator aIter( std::find( maShapeList.begin(), maShapeList.end(), pObj ) );
118 		if( aIter != maShapeList.end() )
119 		{
120 			aIter++;
121 			if( aIter != maShapeList.end() )
122 			{
123 				return (*aIter);
124 			}
125 		}
126 	}
127 	else if( !maShapeList.empty() )
128 	{
129 		return (*maShapeList.begin());
130 	}
131 
132 	return 0;
133 }
134 
135 void ShapeList::ObjectInDestruction(const SdrObject& rObject)
136 {
137 	ListImpl::iterator aIter( std::find( maShapeList.begin(), maShapeList.end(), &rObject ) );
138 	if( aIter != maShapeList.end() )
139 	{
140 		bool bIterErased = aIter == maIter;
141 
142 		aIter = maShapeList.erase( aIter );
143 
144 		if( bIterErased )
145 			maIter = aIter;
146 	}
147 	else
148 	{
149 		DBG_ERROR("sd::ShapeList::ObjectInDestruction(), got a call from an unknown friend!");
150 	}
151 }
152 
153 SdrObject* ShapeList::getNextShape()
154 {
155 	if( maIter != maShapeList.end() )
156 	{
157 		return (*maIter++);
158 	}
159 	else
160 	{
161 		return 0;
162 	}
163 }
164 
165 void ShapeList::seekShape( sal_uInt32 nIndex )
166 {
167 	maIter = maShapeList.begin();
168 	while( nIndex-- && (maIter != maShapeList.end()) )
169 		maIter++;
170 }
171 
172 bool ShapeList::hasMore() const
173 {
174 	return maIter != maShapeList.end();
175 }
176