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_drawinglayer.hxx"
26 
27 #include <drawinglayer/attribute/fillgraphicattribute.hxx>
28 #include <vcl/graph.hxx>
29 
30 //////////////////////////////////////////////////////////////////////////////
31 
32 namespace drawinglayer
33 {
34 	namespace attribute
35 	{
36 		class ImpFillGraphicAttribute
37 		{
38 		public:
39 			// refcounter
40 			sal_uInt32								mnRefCount;
41 
42             // data definitions
43 			Graphic                                 maGraphic;
44 			basegfx::B2DRange						maGraphicRange;
45 
46             // tiling definitions, offsets in X/Y in percent for each 2nd row.
47             // If both are set, Y is ignored (X has precedence)
48             double                                  mfOffsetX;
49             double                                  mfOffsetY;
50 
51 			// bitfield
52 			unsigned								mbTiling : 1;
53 
ImpFillGraphicAttribute(const Graphic & rGraphic,const basegfx::B2DRange & rGraphicRange,bool bTiling,double fOffsetX,double fOffsetY)54 			ImpFillGraphicAttribute(
55                 const Graphic& rGraphic,
56                 const basegfx::B2DRange& rGraphicRange,
57                 bool bTiling,
58                 double fOffsetX,
59                 double fOffsetY)
60 			:	mnRefCount(0),
61 		    	maGraphic(rGraphic),
62 			    maGraphicRange(rGraphicRange),
63                 mfOffsetX(fOffsetX),
64                 mfOffsetY(fOffsetY),
65 			    mbTiling(bTiling)
66 		    {
67 		    }
68 
69 			// data read access
getGraphic() const70 			const Graphic& getGraphic() const { return maGraphic; }
getGraphicRange() const71 			const basegfx::B2DRange& getGraphicRange() const { return maGraphicRange; }
getTiling() const72 			bool getTiling() const { return mbTiling; }
getOffsetX() const73             double getOffsetX() const { return mfOffsetX; }
getOffsetY() const74             double getOffsetY() const { return mfOffsetY; }
75 
operator ==(const ImpFillGraphicAttribute & rCandidate) const76             bool operator==(const ImpFillGraphicAttribute& rCandidate) const
77 		    {
78 			    return (getGraphic() == rCandidate.getGraphic()
79 				    && getGraphicRange() == rCandidate.getGraphicRange()
80 				    && getTiling() == rCandidate.getTiling()
81                     && getOffsetX() == rCandidate.getOffsetX()
82                     && getOffsetY() == rCandidate.getOffsetY());
83 		    }
84 
get_global_default()85             static ImpFillGraphicAttribute* get_global_default()
86             {
87                 static ImpFillGraphicAttribute* pDefault = 0;
88 
89                 if(!pDefault)
90                 {
91                     pDefault = new ImpFillGraphicAttribute(
92                         Graphic(),
93                         basegfx::B2DRange(),
94                         false,
95                         0.0,
96                         0.0);
97 
98                     // never delete; start with RefCount 1, not 0
99     			    pDefault->mnRefCount++;
100                 }
101 
102                 return pDefault;
103             }
104 		};
105 
FillGraphicAttribute(const Graphic & rGraphic,const basegfx::B2DRange & rGraphicRange,bool bTiling,double fOffsetX,double fOffsetY)106         FillGraphicAttribute::FillGraphicAttribute(
107             const Graphic& rGraphic,
108             const basegfx::B2DRange& rGraphicRange,
109             bool bTiling,
110             double fOffsetX,
111             double fOffsetY)
112 		:	mpFillGraphicAttribute(
113                 new ImpFillGraphicAttribute(
114                     rGraphic,
115                     rGraphicRange,
116                     bTiling,
117                     basegfx::clamp(fOffsetX, 0.0, 1.0),
118                     basegfx::clamp(fOffsetY, 0.0, 1.0)))
119 		{
120 		}
121 
FillGraphicAttribute()122 		FillGraphicAttribute::FillGraphicAttribute()
123         :	mpFillGraphicAttribute(ImpFillGraphicAttribute::get_global_default())
124 		{
125 			mpFillGraphicAttribute->mnRefCount++;
126 		}
127 
FillGraphicAttribute(const FillGraphicAttribute & rCandidate)128         FillGraphicAttribute::FillGraphicAttribute(const FillGraphicAttribute& rCandidate)
129 		:	mpFillGraphicAttribute(rCandidate.mpFillGraphicAttribute)
130 		{
131 			mpFillGraphicAttribute->mnRefCount++;
132 		}
133 
~FillGraphicAttribute()134 		FillGraphicAttribute::~FillGraphicAttribute()
135 		{
136 			if(mpFillGraphicAttribute->mnRefCount)
137 			{
138 				mpFillGraphicAttribute->mnRefCount--;
139 			}
140 			else
141 			{
142 				delete mpFillGraphicAttribute;
143 			}
144 		}
145 
isDefault() const146         bool FillGraphicAttribute::isDefault() const
147         {
148             return mpFillGraphicAttribute == ImpFillGraphicAttribute::get_global_default();
149         }
150 
operator =(const FillGraphicAttribute & rCandidate)151         FillGraphicAttribute& FillGraphicAttribute::operator=(const FillGraphicAttribute& rCandidate)
152 		{
153 			if(rCandidate.mpFillGraphicAttribute != mpFillGraphicAttribute)
154 			{
155 				if(mpFillGraphicAttribute->mnRefCount)
156 				{
157 					mpFillGraphicAttribute->mnRefCount--;
158 				}
159 				else
160 				{
161 					delete mpFillGraphicAttribute;
162 				}
163 
164 				mpFillGraphicAttribute = rCandidate.mpFillGraphicAttribute;
165 				mpFillGraphicAttribute->mnRefCount++;
166 			}
167 
168 			return *this;
169 		}
170 
operator ==(const FillGraphicAttribute & rCandidate) const171 		bool FillGraphicAttribute::operator==(const FillGraphicAttribute& rCandidate) const
172 		{
173 			if(rCandidate.mpFillGraphicAttribute == mpFillGraphicAttribute)
174 			{
175 				return true;
176 			}
177 
178 			if(rCandidate.isDefault() != isDefault())
179 			{
180 				return false;
181 			}
182 
183 			return (*rCandidate.mpFillGraphicAttribute == *mpFillGraphicAttribute);
184 		}
185 
getGraphic() const186         const Graphic& FillGraphicAttribute::getGraphic() const
187         {
188             return mpFillGraphicAttribute->getGraphic();
189         }
190 
getGraphicRange() const191         const basegfx::B2DRange& FillGraphicAttribute::getGraphicRange() const
192         {
193             return mpFillGraphicAttribute->getGraphicRange();
194         }
195 
getTiling() const196         bool FillGraphicAttribute::getTiling() const
197         {
198             return mpFillGraphicAttribute->getTiling();
199         }
200 
getOffsetX() const201         double FillGraphicAttribute::getOffsetX() const
202         {
203             return mpFillGraphicAttribute->getOffsetX();
204         }
205 
getOffsetY() const206         double FillGraphicAttribute::getOffsetY() const
207         {
208             return mpFillGraphicAttribute->getOffsetY();
209         }
210 
211     } // end of namespace attribute
212 } // end of namespace drawinglayer
213 
214 //////////////////////////////////////////////////////////////////////////////
215 // eof
216