xref: /AOO41X/main/vcl/source/gdi/mapmod.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_vcl.hxx"
30*cdf0e10cSrcweir #include <tools/stream.hxx>
31*cdf0e10cSrcweir #include <tools/vcompat.hxx>
32*cdf0e10cSrcweir #include <tools/debug.hxx>
33*cdf0e10cSrcweir #include <vcl/mapmod.hxx>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir // =======================================================================
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir DBG_NAME( MapMode )
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir // -----------------------------------------------------------------------
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir ImplMapMode::ImplMapMode() :
42*cdf0e10cSrcweir     maOrigin( 0, 0 ),
43*cdf0e10cSrcweir     maScaleX( 1, 1 ),
44*cdf0e10cSrcweir     maScaleY( 1, 1 )
45*cdf0e10cSrcweir {
46*cdf0e10cSrcweir     mnRefCount  = 1;
47*cdf0e10cSrcweir     meUnit      = MAP_PIXEL;
48*cdf0e10cSrcweir     mbSimple    = sal_False;
49*cdf0e10cSrcweir }
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir // -----------------------------------------------------------------------
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir ImplMapMode::ImplMapMode( const ImplMapMode& rImplMapMode ) :
54*cdf0e10cSrcweir     maOrigin( rImplMapMode.maOrigin ),
55*cdf0e10cSrcweir     maScaleX( rImplMapMode.maScaleX ),
56*cdf0e10cSrcweir     maScaleY( rImplMapMode.maScaleY )
57*cdf0e10cSrcweir {
58*cdf0e10cSrcweir     mnRefCount      = 1;
59*cdf0e10cSrcweir     meUnit          = rImplMapMode.meUnit;
60*cdf0e10cSrcweir     mbSimple        = sal_False;
61*cdf0e10cSrcweir }
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir // -----------------------------------------------------------------------
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir SvStream& operator>>( SvStream& rIStm, ImplMapMode& rImplMapMode )
66*cdf0e10cSrcweir {
67*cdf0e10cSrcweir     VersionCompat	aCompat( rIStm, STREAM_READ );
68*cdf0e10cSrcweir     sal_uInt16			nTmp16;
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir     rIStm >> nTmp16; rImplMapMode.meUnit = (MapUnit) nTmp16;
71*cdf0e10cSrcweir     rIStm >> rImplMapMode.maOrigin >> rImplMapMode.maScaleX >>
72*cdf0e10cSrcweir              rImplMapMode.maScaleY >> rImplMapMode.mbSimple;
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir     return rIStm;
75*cdf0e10cSrcweir }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir // -----------------------------------------------------------------------
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir SvStream& operator<<( SvStream& rOStm, const ImplMapMode& rImplMapMode )
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir     VersionCompat aCompat( rOStm, STREAM_WRITE, 1 );
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir     rOStm << (sal_uInt16) rImplMapMode.meUnit <<
84*cdf0e10cSrcweir              rImplMapMode.maOrigin <<
85*cdf0e10cSrcweir              rImplMapMode.maScaleX <<
86*cdf0e10cSrcweir              rImplMapMode.maScaleY <<
87*cdf0e10cSrcweir              rImplMapMode.mbSimple;
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir     return rOStm;
90*cdf0e10cSrcweir }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir // -----------------------------------------------------------------------
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir ImplMapMode* ImplMapMode::ImplGetStaticMapMode( MapUnit eUnit )
95*cdf0e10cSrcweir {
96*cdf0e10cSrcweir     static long aStaticImplMapModeAry[(MAP_LASTENUMDUMMY)*sizeof(ImplMapMode)/sizeof(long)];
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir     // #i19496 check for out-of-bounds
99*cdf0e10cSrcweir      if( eUnit >= MAP_LASTENUMDUMMY )
100*cdf0e10cSrcweir         return (ImplMapMode*)aStaticImplMapModeAry;
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir     ImplMapMode* pImplMapMode = ((ImplMapMode*)aStaticImplMapModeAry)+eUnit;
103*cdf0e10cSrcweir     if ( !pImplMapMode->mbSimple )
104*cdf0e10cSrcweir     {
105*cdf0e10cSrcweir         Fraction aDefFraction( 1, 1 );
106*cdf0e10cSrcweir         pImplMapMode->maScaleX  = aDefFraction;
107*cdf0e10cSrcweir         pImplMapMode->maScaleY  = aDefFraction;
108*cdf0e10cSrcweir         pImplMapMode->meUnit    = eUnit;
109*cdf0e10cSrcweir         pImplMapMode->mbSimple  = sal_True;
110*cdf0e10cSrcweir     }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir     return pImplMapMode;
113*cdf0e10cSrcweir }
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir // -----------------------------------------------------------------------
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir inline void MapMode::ImplMakeUnique()
118*cdf0e10cSrcweir {
119*cdf0e10cSrcweir     // Falls noch andere Referenzen bestehen, dann kopieren
120*cdf0e10cSrcweir     if ( mpImplMapMode->mnRefCount != 1 )
121*cdf0e10cSrcweir     {
122*cdf0e10cSrcweir         if ( mpImplMapMode->mnRefCount )
123*cdf0e10cSrcweir             mpImplMapMode->mnRefCount--;
124*cdf0e10cSrcweir         mpImplMapMode = new ImplMapMode( *mpImplMapMode );
125*cdf0e10cSrcweir     }
126*cdf0e10cSrcweir }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir // -----------------------------------------------------------------------
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir MapMode::MapMode()
131*cdf0e10cSrcweir {
132*cdf0e10cSrcweir     DBG_CTOR( MapMode, NULL );
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir     mpImplMapMode = ImplMapMode::ImplGetStaticMapMode( MAP_PIXEL );
135*cdf0e10cSrcweir }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir // -----------------------------------------------------------------------
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir MapMode::MapMode( const MapMode& rMapMode )
140*cdf0e10cSrcweir {
141*cdf0e10cSrcweir     DBG_CTOR( MapMode, NULL );
142*cdf0e10cSrcweir     DBG_CHKOBJ( &rMapMode, MapMode, NULL );
143*cdf0e10cSrcweir     DBG_ASSERT( rMapMode.mpImplMapMode->mnRefCount < 0xFFFFFFFE, "MapMode: RefCount overflow" );
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir     // shared Instance Daten uebernehmen und Referenzcounter erhoehen
146*cdf0e10cSrcweir     mpImplMapMode = rMapMode.mpImplMapMode;
147*cdf0e10cSrcweir     // RefCount == 0 fuer statische Objekte
148*cdf0e10cSrcweir     if ( mpImplMapMode->mnRefCount )
149*cdf0e10cSrcweir         mpImplMapMode->mnRefCount++;
150*cdf0e10cSrcweir }
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir // -----------------------------------------------------------------------
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir MapMode::MapMode( MapUnit eUnit )
155*cdf0e10cSrcweir {
156*cdf0e10cSrcweir     DBG_CTOR( MapMode, NULL );
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir     mpImplMapMode = ImplMapMode::ImplGetStaticMapMode( eUnit );
159*cdf0e10cSrcweir }
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir // -----------------------------------------------------------------------
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir MapMode::MapMode( MapUnit eUnit, const Point& rLogicOrg,
164*cdf0e10cSrcweir                   const Fraction& rScaleX, const Fraction& rScaleY )
165*cdf0e10cSrcweir {
166*cdf0e10cSrcweir     DBG_CTOR( MapMode, NULL );
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir     mpImplMapMode           = new ImplMapMode;
169*cdf0e10cSrcweir     mpImplMapMode->meUnit   = eUnit;
170*cdf0e10cSrcweir     mpImplMapMode->maOrigin = rLogicOrg;
171*cdf0e10cSrcweir     mpImplMapMode->maScaleX = rScaleX;
172*cdf0e10cSrcweir     mpImplMapMode->maScaleY = rScaleY;
173*cdf0e10cSrcweir }
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir // -----------------------------------------------------------------------
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir MapMode::~MapMode()
178*cdf0e10cSrcweir {
179*cdf0e10cSrcweir     DBG_DTOR( MapMode, NULL );
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir     // Wenn es keine statischen ImpDaten sind, dann loeschen, wenn es
182*cdf0e10cSrcweir     // die letzte Referenz ist, sonst Referenzcounter decrementieren
183*cdf0e10cSrcweir     if ( mpImplMapMode->mnRefCount )
184*cdf0e10cSrcweir     {
185*cdf0e10cSrcweir         if ( mpImplMapMode->mnRefCount == 1 )
186*cdf0e10cSrcweir             delete mpImplMapMode;
187*cdf0e10cSrcweir         else
188*cdf0e10cSrcweir             mpImplMapMode->mnRefCount--;
189*cdf0e10cSrcweir     }
190*cdf0e10cSrcweir }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir // -----------------------------------------------------------------------
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir void MapMode::SetMapUnit( MapUnit eUnit )
195*cdf0e10cSrcweir {
196*cdf0e10cSrcweir     DBG_CHKTHIS( MapMode, NULL );
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir     ImplMakeUnique();
199*cdf0e10cSrcweir     mpImplMapMode->meUnit = eUnit;
200*cdf0e10cSrcweir }
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir // -----------------------------------------------------------------------
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir void MapMode::SetOrigin( const Point& rLogicOrg )
205*cdf0e10cSrcweir {
206*cdf0e10cSrcweir     DBG_CHKTHIS( MapMode, NULL );
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir     ImplMakeUnique();
209*cdf0e10cSrcweir     mpImplMapMode->maOrigin = rLogicOrg;
210*cdf0e10cSrcweir }
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir // -----------------------------------------------------------------------
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir void MapMode::SetScaleX( const Fraction& rScaleX )
215*cdf0e10cSrcweir {
216*cdf0e10cSrcweir     DBG_CHKTHIS( MapMode, NULL );
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir     ImplMakeUnique();
219*cdf0e10cSrcweir     mpImplMapMode->maScaleX = rScaleX;
220*cdf0e10cSrcweir }
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir // -----------------------------------------------------------------------
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir void MapMode::SetScaleY( const Fraction& rScaleY )
225*cdf0e10cSrcweir {
226*cdf0e10cSrcweir     DBG_CHKTHIS( MapMode, NULL );
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir     ImplMakeUnique();
229*cdf0e10cSrcweir     mpImplMapMode->maScaleY = rScaleY;
230*cdf0e10cSrcweir }
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir // -----------------------------------------------------------------------
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir MapMode& MapMode::operator=( const MapMode& rMapMode )
235*cdf0e10cSrcweir {
236*cdf0e10cSrcweir     DBG_CHKTHIS( MapMode, NULL );
237*cdf0e10cSrcweir     DBG_CHKOBJ( &rMapMode, MapMode, NULL );
238*cdf0e10cSrcweir     DBG_ASSERT( rMapMode.mpImplMapMode->mnRefCount < 0xFFFFFFFE, "MapMode: RefCount overflow" );
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir     // Zuerst Referenzcounter erhoehen, damit man sich selbst zuweisen kann
241*cdf0e10cSrcweir     // RefCount == 0 fuer statische Objekte
242*cdf0e10cSrcweir     if ( rMapMode.mpImplMapMode->mnRefCount )
243*cdf0e10cSrcweir         rMapMode.mpImplMapMode->mnRefCount++;
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir     // Wenn es keine statischen ImpDaten sind, dann loeschen, wenn es
246*cdf0e10cSrcweir     // die letzte Referenz ist, sonst Referenzcounter decrementieren
247*cdf0e10cSrcweir     if ( mpImplMapMode->mnRefCount )
248*cdf0e10cSrcweir     {
249*cdf0e10cSrcweir         if ( mpImplMapMode->mnRefCount == 1 )
250*cdf0e10cSrcweir             delete mpImplMapMode;
251*cdf0e10cSrcweir         else
252*cdf0e10cSrcweir             mpImplMapMode->mnRefCount--;
253*cdf0e10cSrcweir     }
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir     mpImplMapMode = rMapMode.mpImplMapMode;
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir     return *this;
258*cdf0e10cSrcweir }
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir // -----------------------------------------------------------------------
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir sal_Bool MapMode::operator==( const MapMode& rMapMode ) const
263*cdf0e10cSrcweir {
264*cdf0e10cSrcweir     DBG_CHKTHIS( MapMode, NULL );
265*cdf0e10cSrcweir     DBG_CHKOBJ( &rMapMode, MapMode, NULL );
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir     if ( mpImplMapMode == rMapMode.mpImplMapMode )
268*cdf0e10cSrcweir         return sal_True;
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir     if ( (mpImplMapMode->meUnit   == rMapMode.mpImplMapMode->meUnit)   &&
271*cdf0e10cSrcweir          (mpImplMapMode->maOrigin == rMapMode.mpImplMapMode->maOrigin) &&
272*cdf0e10cSrcweir          (mpImplMapMode->maScaleX == rMapMode.mpImplMapMode->maScaleX) &&
273*cdf0e10cSrcweir          (mpImplMapMode->maScaleY == rMapMode.mpImplMapMode->maScaleY) )
274*cdf0e10cSrcweir         return sal_True;
275*cdf0e10cSrcweir     else
276*cdf0e10cSrcweir         return sal_False;
277*cdf0e10cSrcweir }
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir // -----------------------------------------------------------------------
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir sal_Bool MapMode::IsDefault() const
282*cdf0e10cSrcweir {
283*cdf0e10cSrcweir     DBG_CHKTHIS( MapMode, NULL );
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir     ImplMapMode* pDefMapMode = ImplMapMode::ImplGetStaticMapMode( MAP_PIXEL );
286*cdf0e10cSrcweir     if ( mpImplMapMode == pDefMapMode )
287*cdf0e10cSrcweir         return sal_True;
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir     if ( (mpImplMapMode->meUnit   == pDefMapMode->meUnit)   &&
290*cdf0e10cSrcweir          (mpImplMapMode->maOrigin == pDefMapMode->maOrigin) &&
291*cdf0e10cSrcweir          (mpImplMapMode->maScaleX == pDefMapMode->maScaleX) &&
292*cdf0e10cSrcweir          (mpImplMapMode->maScaleY == pDefMapMode->maScaleY) )
293*cdf0e10cSrcweir         return sal_True;
294*cdf0e10cSrcweir     else
295*cdf0e10cSrcweir         return sal_False;
296*cdf0e10cSrcweir }
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir // -----------------------------------------------------------------------
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir SvStream& operator>>( SvStream& rIStm, MapMode& rMapMode )
301*cdf0e10cSrcweir {
302*cdf0e10cSrcweir     rMapMode.ImplMakeUnique();
303*cdf0e10cSrcweir     return (rIStm >> *rMapMode.mpImplMapMode);
304*cdf0e10cSrcweir }
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir // -----------------------------------------------------------------------
307*cdf0e10cSrcweir 
308*cdf0e10cSrcweir SvStream& operator<<( SvStream& rOStm, const MapMode& rMapMode )
309*cdf0e10cSrcweir {
310*cdf0e10cSrcweir     return (rOStm << *rMapMode.mpImplMapMode);
311*cdf0e10cSrcweir }
312