xref: /aoo41x/main/vcl/source/gdi/mapmod.cxx (revision 9f62ea84)
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_vcl.hxx"
26 #include <tools/stream.hxx>
27 #include <tools/vcompat.hxx>
28 #include <tools/debug.hxx>
29 #include <vcl/mapmod.hxx>
30 
31 // =======================================================================
32 
DBG_NAME(MapMode)33 DBG_NAME( MapMode )
34 
35 // -----------------------------------------------------------------------
36 
37 ImplMapMode::ImplMapMode() :
38     maOrigin( 0, 0 ),
39     maScaleX( 1, 1 ),
40     maScaleY( 1, 1 )
41 {
42     mnRefCount  = 1;
43     meUnit      = MAP_PIXEL;
44     mbSimple    = sal_False;
45 }
46 
47 // -----------------------------------------------------------------------
48 
ImplMapMode(const ImplMapMode & rImplMapMode)49 ImplMapMode::ImplMapMode( const ImplMapMode& rImplMapMode ) :
50     maOrigin( rImplMapMode.maOrigin ),
51     maScaleX( rImplMapMode.maScaleX ),
52     maScaleY( rImplMapMode.maScaleY )
53 {
54     mnRefCount      = 1;
55     meUnit          = rImplMapMode.meUnit;
56     mbSimple        = sal_False;
57 }
58 
59 // -----------------------------------------------------------------------
60 
operator >>(SvStream & rIStm,ImplMapMode & rImplMapMode)61 SvStream& operator>>( SvStream& rIStm, ImplMapMode& rImplMapMode )
62 {
63     VersionCompat	aCompat( rIStm, STREAM_READ );
64     sal_uInt16			nTmp16;
65 
66     rIStm >> nTmp16; rImplMapMode.meUnit = (MapUnit) nTmp16;
67     rIStm >> rImplMapMode.maOrigin >> rImplMapMode.maScaleX >>
68              rImplMapMode.maScaleY >> rImplMapMode.mbSimple;
69 
70     return rIStm;
71 }
72 
73 // -----------------------------------------------------------------------
74 
operator <<(SvStream & rOStm,const ImplMapMode & rImplMapMode)75 SvStream& operator<<( SvStream& rOStm, const ImplMapMode& rImplMapMode )
76 {
77     VersionCompat aCompat( rOStm, STREAM_WRITE, 1 );
78 
79     rOStm << (sal_uInt16) rImplMapMode.meUnit <<
80              rImplMapMode.maOrigin <<
81              rImplMapMode.maScaleX <<
82              rImplMapMode.maScaleY <<
83              rImplMapMode.mbSimple;
84 
85     return rOStm;
86 }
87 
88 // -----------------------------------------------------------------------
89 
ImplGetStaticMapMode(MapUnit eUnit)90 ImplMapMode* ImplMapMode::ImplGetStaticMapMode( MapUnit eUnit )
91 {
92     static long aStaticImplMapModeAry[(MAP_LASTENUMDUMMY)*sizeof(ImplMapMode)/sizeof(long)];
93 
94     // #i19496 check for out-of-bounds
95      if( eUnit >= MAP_LASTENUMDUMMY )
96         return (ImplMapMode*)aStaticImplMapModeAry;
97 
98     ImplMapMode* pImplMapMode = ((ImplMapMode*)aStaticImplMapModeAry)+eUnit;
99     if ( !pImplMapMode->mbSimple )
100     {
101         Fraction aDefFraction( 1, 1 );
102         pImplMapMode->maScaleX  = aDefFraction;
103         pImplMapMode->maScaleY  = aDefFraction;
104         pImplMapMode->meUnit    = eUnit;
105         pImplMapMode->mbSimple  = sal_True;
106     }
107 
108     return pImplMapMode;
109 }
110 
111 // -----------------------------------------------------------------------
112 
ImplMakeUnique()113 inline void MapMode::ImplMakeUnique()
114 {
115     // Falls noch andere Referenzen bestehen, dann kopieren
116     if ( mpImplMapMode->mnRefCount != 1 )
117     {
118         if ( mpImplMapMode->mnRefCount )
119             mpImplMapMode->mnRefCount--;
120         mpImplMapMode = new ImplMapMode( *mpImplMapMode );
121     }
122 }
123 
124 // -----------------------------------------------------------------------
125 
MapMode()126 MapMode::MapMode()
127 {
128     DBG_CTOR( MapMode, NULL );
129 
130     mpImplMapMode = ImplMapMode::ImplGetStaticMapMode( MAP_PIXEL );
131 }
132 
133 // -----------------------------------------------------------------------
134 
MapMode(const MapMode & rMapMode)135 MapMode::MapMode( const MapMode& rMapMode )
136 {
137     DBG_CTOR( MapMode, NULL );
138     DBG_CHKOBJ( &rMapMode, MapMode, NULL );
139     DBG_ASSERT( rMapMode.mpImplMapMode->mnRefCount < 0xFFFFFFFE, "MapMode: RefCount overflow" );
140 
141     // shared Instance Daten uebernehmen und Referenzcounter erhoehen
142     mpImplMapMode = rMapMode.mpImplMapMode;
143     // RefCount == 0 fuer statische Objekte
144     if ( mpImplMapMode->mnRefCount )
145         mpImplMapMode->mnRefCount++;
146 }
147 
148 // -----------------------------------------------------------------------
149 
MapMode(MapUnit eUnit)150 MapMode::MapMode( MapUnit eUnit )
151 {
152     DBG_CTOR( MapMode, NULL );
153 
154     mpImplMapMode = ImplMapMode::ImplGetStaticMapMode( eUnit );
155 }
156 
157 // -----------------------------------------------------------------------
158 
MapMode(MapUnit eUnit,const Point & rLogicOrg,const Fraction & rScaleX,const Fraction & rScaleY)159 MapMode::MapMode( MapUnit eUnit, const Point& rLogicOrg,
160                   const Fraction& rScaleX, const Fraction& rScaleY )
161 {
162     DBG_CTOR( MapMode, NULL );
163 
164     mpImplMapMode           = new ImplMapMode;
165     mpImplMapMode->meUnit   = eUnit;
166     mpImplMapMode->maOrigin = rLogicOrg;
167     mpImplMapMode->maScaleX = rScaleX;
168     mpImplMapMode->maScaleY = rScaleY;
169 }
170 
171 // -----------------------------------------------------------------------
172 
~MapMode()173 MapMode::~MapMode()
174 {
175     DBG_DTOR( MapMode, NULL );
176 
177     // Wenn es keine statischen ImpDaten sind, dann loeschen, wenn es
178     // die letzte Referenz ist, sonst Referenzcounter decrementieren
179     if ( mpImplMapMode->mnRefCount )
180     {
181         if ( mpImplMapMode->mnRefCount == 1 )
182             delete mpImplMapMode;
183         else
184             mpImplMapMode->mnRefCount--;
185     }
186 }
187 
188 // -----------------------------------------------------------------------
189 
SetMapUnit(MapUnit eUnit)190 void MapMode::SetMapUnit( MapUnit eUnit )
191 {
192     DBG_CHKTHIS( MapMode, NULL );
193 
194     ImplMakeUnique();
195     mpImplMapMode->meUnit = eUnit;
196 }
197 
198 // -----------------------------------------------------------------------
199 
SetOrigin(const Point & rLogicOrg)200 void MapMode::SetOrigin( const Point& rLogicOrg )
201 {
202     DBG_CHKTHIS( MapMode, NULL );
203 
204     ImplMakeUnique();
205     mpImplMapMode->maOrigin = rLogicOrg;
206 }
207 
208 // -----------------------------------------------------------------------
209 
SetScaleX(const Fraction & rScaleX)210 void MapMode::SetScaleX( const Fraction& rScaleX )
211 {
212     DBG_CHKTHIS( MapMode, NULL );
213 
214     ImplMakeUnique();
215     mpImplMapMode->maScaleX = rScaleX;
216 }
217 
218 // -----------------------------------------------------------------------
219 
SetScaleY(const Fraction & rScaleY)220 void MapMode::SetScaleY( const Fraction& rScaleY )
221 {
222     DBG_CHKTHIS( MapMode, NULL );
223 
224     ImplMakeUnique();
225     mpImplMapMode->maScaleY = rScaleY;
226 }
227 
228 // -----------------------------------------------------------------------
229 
operator =(const MapMode & rMapMode)230 MapMode& MapMode::operator=( const MapMode& rMapMode )
231 {
232     DBG_CHKTHIS( MapMode, NULL );
233     DBG_CHKOBJ( &rMapMode, MapMode, NULL );
234     DBG_ASSERT( rMapMode.mpImplMapMode->mnRefCount < 0xFFFFFFFE, "MapMode: RefCount overflow" );
235 
236     // Zuerst Referenzcounter erhoehen, damit man sich selbst zuweisen kann
237     // RefCount == 0 fuer statische Objekte
238     if ( rMapMode.mpImplMapMode->mnRefCount )
239         rMapMode.mpImplMapMode->mnRefCount++;
240 
241     // Wenn es keine statischen ImpDaten sind, dann loeschen, wenn es
242     // die letzte Referenz ist, sonst Referenzcounter decrementieren
243     if ( mpImplMapMode->mnRefCount )
244     {
245         if ( mpImplMapMode->mnRefCount == 1 )
246             delete mpImplMapMode;
247         else
248             mpImplMapMode->mnRefCount--;
249     }
250 
251     mpImplMapMode = rMapMode.mpImplMapMode;
252 
253     return *this;
254 }
255 
256 // -----------------------------------------------------------------------
257 
operator ==(const MapMode & rMapMode) const258 sal_Bool MapMode::operator==( const MapMode& rMapMode ) const
259 {
260     DBG_CHKTHIS( MapMode, NULL );
261     DBG_CHKOBJ( &rMapMode, MapMode, NULL );
262 
263     if ( mpImplMapMode == rMapMode.mpImplMapMode )
264         return sal_True;
265 
266     if ( (mpImplMapMode->meUnit   == rMapMode.mpImplMapMode->meUnit)   &&
267          (mpImplMapMode->maOrigin == rMapMode.mpImplMapMode->maOrigin) &&
268          (mpImplMapMode->maScaleX == rMapMode.mpImplMapMode->maScaleX) &&
269          (mpImplMapMode->maScaleY == rMapMode.mpImplMapMode->maScaleY) )
270         return sal_True;
271     else
272         return sal_False;
273 }
274 
275 // -----------------------------------------------------------------------
276 
IsDefault() const277 sal_Bool MapMode::IsDefault() const
278 {
279     DBG_CHKTHIS( MapMode, NULL );
280 
281     ImplMapMode* pDefMapMode = ImplMapMode::ImplGetStaticMapMode( MAP_PIXEL );
282     if ( mpImplMapMode == pDefMapMode )
283         return sal_True;
284 
285     if ( (mpImplMapMode->meUnit   == pDefMapMode->meUnit)   &&
286          (mpImplMapMode->maOrigin == pDefMapMode->maOrigin) &&
287          (mpImplMapMode->maScaleX == pDefMapMode->maScaleX) &&
288          (mpImplMapMode->maScaleY == pDefMapMode->maScaleY) )
289         return sal_True;
290     else
291         return sal_False;
292 }
293 
294 // -----------------------------------------------------------------------
295 
operator >>(SvStream & rIStm,MapMode & rMapMode)296 SvStream& operator>>( SvStream& rIStm, MapMode& rMapMode )
297 {
298     rMapMode.ImplMakeUnique();
299     return (rIStm >> *rMapMode.mpImplMapMode);
300 }
301 
302 // -----------------------------------------------------------------------
303 
operator <<(SvStream & rOStm,const MapMode & rMapMode)304 SvStream& operator<<( SvStream& rOStm, const MapMode& rMapMode )
305 {
306     return (rOStm << *rMapMode.mpImplMapMode);
307 }
308