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