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_svx.hxx"
30 #include <svx/sdr/properties/defaultproperties.hxx>
31 #include <svx/sdr/properties/itemsettools.hxx>
32 #include <svl/itemset.hxx>
33 #include <svl/whiter.hxx>
34 
35 #include <vector>
36 #include <svx/svdobj.hxx>
37 #include <svx/svddef.hxx>
38 #include <svx/svdpool.hxx>
39 #include <editeng/eeitem.hxx>
40 
41 //////////////////////////////////////////////////////////////////////////////
42 
43 namespace sdr
44 {
45 	namespace properties
46 	{
47 		SfxItemSet& DefaultProperties::CreateObjectSpecificItemSet(SfxItemPool& rPool)
48 		{
49 			// Basic implementation; Basic object has NO attributes
50 			return *(new SfxItemSet(rPool));
51 		}
52 
53 		DefaultProperties::DefaultProperties(SdrObject& rObj)
54 		:	BaseProperties(rObj),
55 			mpItemSet(0L)
56 		{
57 		}
58 
59 		DefaultProperties::DefaultProperties(const DefaultProperties& rProps, SdrObject& rObj)
60 		:	BaseProperties(rObj),
61 			mpItemSet(0L)
62 		{
63 			if(rProps.mpItemSet)
64 			{
65 				mpItemSet = rProps.mpItemSet->Clone(sal_True);
66 
67 				// do not keep parent info, this may be changed by later construrtors.
68 				// This class just copies the ItemSet, ignore parent.
69 				if(mpItemSet && mpItemSet->GetParent())
70 				{
71 					mpItemSet->SetParent(0L);
72 				}
73 			}
74 		}
75 
76 		BaseProperties& DefaultProperties::Clone(SdrObject& rObj) const
77 		{
78 			return *(new DefaultProperties(*this, rObj));
79 		}
80 
81 		DefaultProperties::~DefaultProperties()
82 		{
83 			if(mpItemSet)
84 			{
85 				delete mpItemSet;
86 				mpItemSet = 0L;
87 			}
88 		}
89 
90 		const SfxItemSet& DefaultProperties::GetObjectItemSet() const
91 		{
92 			if(!mpItemSet)
93 			{
94 				((DefaultProperties*)this)->mpItemSet = &(((DefaultProperties*)this)->CreateObjectSpecificItemSet(*GetSdrObject().GetObjectItemPool()));
95 				((DefaultProperties*)this)->ForceDefaultAttributes();
96 			}
97 
98 			DBG_ASSERT(mpItemSet, "Could not create an SfxItemSet(!)");
99 
100 			return *mpItemSet;
101 		}
102 
103 		void DefaultProperties::SetObjectItem(const SfxPoolItem& rItem)
104 		{
105 			const sal_uInt16 nWhichID(rItem.Which());
106 
107 			if(AllowItemChange(nWhichID, &rItem))
108 			{
109 				ItemChange(nWhichID, &rItem);
110 				PostItemChange(nWhichID);
111 
112 				SfxItemSet aSet(*GetSdrObject().GetObjectItemPool(), nWhichID, nWhichID);
113 				aSet.Put(rItem);
114 				ItemSetChanged(aSet);
115 			}
116 		}
117 
118 		void DefaultProperties::SetObjectItemDirect(const SfxPoolItem& rItem)
119 		{
120 			const sal_uInt16 nWhichID(rItem.Which());
121 
122 			if(AllowItemChange(nWhichID, &rItem))
123 			{
124 				ItemChange(nWhichID, &rItem);
125 			}
126 		}
127 
128 		void DefaultProperties::ClearObjectItem(const sal_uInt16 nWhich)
129 		{
130 			if(AllowItemChange(nWhich))
131 			{
132 				ItemChange(nWhich);
133 				PostItemChange(nWhich);
134 
135 				if(nWhich)
136 				{
137 					SfxItemSet aSet(*GetSdrObject().GetObjectItemPool(), nWhich, nWhich, 0, 0);
138 					ItemSetChanged(aSet);
139 				}
140 			}
141 		}
142 
143 		void DefaultProperties::ClearObjectItemDirect(const sal_uInt16 nWhich)
144 		{
145 			if(AllowItemChange(nWhich))
146 			{
147 				ItemChange(nWhich);
148 			}
149 		}
150 
151 		void DefaultProperties::SetObjectItemSet(const SfxItemSet& rSet)
152 		{
153 			SfxWhichIter aWhichIter(rSet);
154 			sal_uInt16 nWhich(aWhichIter.FirstWhich());
155 			const SfxPoolItem *pPoolItem;
156 			std::vector< sal_uInt16 > aPostItemChangeList;
157 			sal_Bool bDidChange(sal_False);
158 			SfxItemSet aSet(*GetSdrObject().GetObjectItemPool(), SDRATTR_START, EE_ITEMS_END, 0, 0);
159 
160 			// give a hint to STL_Vector
161 			aPostItemChangeList.reserve(rSet.Count());
162 
163 			while(nWhich)
164 			{
165 				if(SFX_ITEM_SET == rSet.GetItemState(nWhich, sal_False, &pPoolItem))
166 				{
167 					if(AllowItemChange(nWhich, pPoolItem))
168 					{
169 						bDidChange = sal_True;
170 						ItemChange(nWhich, pPoolItem);
171 						aPostItemChangeList.push_back( nWhich );
172 						aSet.Put(*pPoolItem);
173 					}
174 				}
175 
176 				nWhich = aWhichIter.NextWhich();
177 			}
178 
179 			if(bDidChange)
180 			{
181 				std::vector< sal_uInt16 >::iterator aIter = aPostItemChangeList.begin();
182 				const std::vector< sal_uInt16 >::iterator aEnd = aPostItemChangeList.end();
183 
184 				while(aIter != aEnd)
185 				{
186 					PostItemChange(*aIter);
187 					aIter++;
188 				}
189 
190 				ItemSetChanged(aSet);
191 			}
192 		}
193 
194 		void DefaultProperties::ItemSetChanged(const SfxItemSet& /*rSet*/)
195 		{
196 		}
197 
198 		sal_Bool DefaultProperties::AllowItemChange(const sal_uInt16 /*nWhich*/, const SfxPoolItem* /*pNewItem*/) const
199 		{
200 			return sal_True;
201 		}
202 
203 		void DefaultProperties::ItemChange(const sal_uInt16 /*nWhich*/, const SfxPoolItem* /*pNewItem*/)
204 		{
205 		}
206 
207 		void DefaultProperties::PostItemChange(const sal_uInt16 nWhich )
208 		{
209 			if( (nWhich == XATTR_FILLSTYLE) && (mpItemSet != NULL) )
210 				CleanupFillProperties(*mpItemSet);
211 		}
212 
213 		void DefaultProperties::SetStyleSheet(SfxStyleSheet* /*pNewStyleSheet*/, sal_Bool /*bDontRemoveHardAttr*/)
214 		{
215 			// no StyleSheet in DefaultProperties
216 		}
217 
218 		SfxStyleSheet* DefaultProperties::GetStyleSheet() const
219 		{
220 			// no StyleSheet in DefaultProperties
221 			return 0L;
222 		}
223 
224 		void DefaultProperties::ForceDefaultAttributes()
225 		{
226 		}
227 
228 		void DefaultProperties::Scale(const Fraction& rScale)
229 		{
230 			if(mpItemSet)
231 			{
232 				ScaleItemSet(*mpItemSet, rScale);
233 			}
234 		}
235 	} // end of namespace properties
236 } // end of namespace sdr
237 
238 //////////////////////////////////////////////////////////////////////////////
239 // eof
240