1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: prcntfld.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
34 // include ---------------------------------------------------------------
37 #include "prcntfld.hxx"
39 // STATIC DATA -----------------------------------------------------------
41 /*--------------------------------------------------------------------
43 --------------------------------------------------------------------*/
45 PercentField::PercentField( Window
* pWin
, const ResId
& rResId
) :
46 MetricField ( pWin
, rResId
),
52 eOldUnit (FUNIT_NONE
),
53 bLockAutoCalculation(sal_False
)
56 nOldSpinSize
= GetSpinSize();
57 nRefValue
= DenormalizePercent(MetricField::GetMax(FUNIT_TWIP
));
58 nOldDigits
= GetDecimalDigits();
59 SetCustomUnitText('%');
62 /*--------------------------------------------------------------------
64 --------------------------------------------------------------------*/
66 void PercentField::SetRefValue(sal_Int64 nValue
)
68 sal_Int64 nRealValue
= GetRealValue(eOldUnit
);
72 if (!bLockAutoCalculation
&& (GetUnit() == FUNIT_CUSTOM
))
73 SetPrcntValue(nRealValue
, eOldUnit
);
76 /*--------------------------------------------------------------------
78 --------------------------------------------------------------------*/
80 void PercentField::ShowPercent(BOOL bPercent
)
82 if ((bPercent
&& GetUnit() == FUNIT_CUSTOM
) ||
83 (!bPercent
&& GetUnit() != FUNIT_CUSTOM
))
90 sal_Int64 nAktWidth
, nPercent
;
92 nOldValue
= GetValue();
95 nOldDigits
= GetDecimalDigits();
98 nOldSpinSize
= GetSpinSize();
99 nOldBaseValue
= GetBaseValue();
100 SetUnit(FUNIT_CUSTOM
);
101 SetDecimalDigits( 0 );
103 nAktWidth
= ConvertValue(nOldMin
, 0, nOldDigits
, eOldUnit
, FUNIT_TWIP
);
104 // Um 0.5 Prozent aufrunden
105 nPercent
= ((nAktWidth
* 10) / nRefValue
+ 5) / 10;
107 MetricField::SetMin(Max(static_cast< sal_Int64
>(1), nPercent
));
108 MetricField::SetMax(100);
110 MetricField::SetBaseValue(0);
111 if (nOldValue
!= nLastValue
)
113 nAktWidth
= ConvertValue(nOldValue
, 0, nOldDigits
, eOldUnit
, FUNIT_TWIP
);
114 nPercent
= ((nAktWidth
* 10) / nRefValue
+ 5) / 10;
115 MetricFormatter::SetValue(nPercent
);
116 nLastPercent
= nPercent
;
117 nLastValue
= nOldValue
;
120 MetricFormatter::SetValue(nLastPercent
);
121 // SetValue(100, FUNIT_CUSTOM);
125 sal_Int64 nOldPercent
= GetValue(FUNIT_CUSTOM
);
127 nOldValue
= Convert(GetValue(), GetUnit(), eOldUnit
);
130 SetDecimalDigits(nOldDigits
);
131 MetricField::SetMin(nOldMin
);
132 MetricField::SetMax(nOldMax
);
133 SetSpinSize(nOldSpinSize
);
134 MetricField::SetBaseValue(nOldBaseValue
);
136 if (nOldPercent
!= nLastPercent
)
138 SetPrcntValue(nOldValue
, eOldUnit
);
139 nLastPercent
= nOldPercent
;
140 nLastValue
= nOldValue
;
143 SetPrcntValue(nLastValue
, eOldUnit
);
147 /*--------------------------------------------------------------------
149 --------------------------------------------------------------------*/
150 void PercentField::SetValue(sal_Int64 nNewValue
, FieldUnit eInUnit
)
152 MetricFormatter::SetValue(nNewValue
, eInUnit
);
154 /*--------------------------------------------------------------------
156 --------------------------------------------------------------------*/
157 void PercentField::SetPrcntValue(sal_Int64 nNewValue
, FieldUnit eInUnit
)
159 if (GetUnit() != FUNIT_CUSTOM
|| eInUnit
== FUNIT_CUSTOM
)
160 MetricFormatter::SetValue(Convert(nNewValue
, eInUnit
, GetUnit()));
164 // Ausgangswert ueberschreiben, nicht spaeter restaurieren
165 sal_Int64 nPercent
, nAktWidth
;
166 if(eInUnit
== FUNIT_TWIP
)
168 nAktWidth
= ConvertValue(nNewValue
, 0, nOldDigits
, FUNIT_TWIP
, FUNIT_TWIP
);
172 sal_Int64 nValue
= Convert(nNewValue
, eInUnit
, eOldUnit
);
173 nAktWidth
= ConvertValue(nValue
, 0, nOldDigits
, eOldUnit
, FUNIT_TWIP
);
175 nPercent
= ((nAktWidth
* 10) / nRefValue
+ 5) / 10;
176 MetricFormatter::SetValue(nPercent
);
180 /*--------------------------------------------------------------------
182 --------------------------------------------------------------------*/
184 void PercentField::SetUserValue( sal_Int64 nNewValue
, FieldUnit eInUnit
)
186 if (GetUnit() != FUNIT_CUSTOM
|| eInUnit
== FUNIT_CUSTOM
)
187 MetricField::SetUserValue(Convert(nNewValue
, eInUnit
, GetUnit()),FUNIT_NONE
);
191 // Ausgangswert ueberschreiben, nicht spaeter restaurieren
192 sal_Int64 nPercent
, nAktWidth
;
193 if(eInUnit
== FUNIT_TWIP
)
195 nAktWidth
= ConvertValue(nNewValue
, 0, nOldDigits
, FUNIT_TWIP
, FUNIT_TWIP
);
199 sal_Int64 nValue
= Convert(nNewValue
, eInUnit
, eOldUnit
);
200 nAktWidth
= ConvertValue(nValue
, 0, nOldDigits
, eOldUnit
, FUNIT_TWIP
);
202 nPercent
= ((nAktWidth
* 10) / nRefValue
+ 5) / 10;
203 MetricField::SetUserValue(nPercent
,FUNIT_NONE
);
208 /*--------------------------------------------------------------------
210 --------------------------------------------------------------------*/
212 void PercentField::SetBaseValue(sal_Int64 nNewValue
, FieldUnit eInUnit
)
214 if (GetUnit() == FUNIT_CUSTOM
)
215 nOldBaseValue
= ConvertValue(nNewValue
, 0, nOldDigits
, eInUnit
, eOldUnit
);
217 MetricField::SetBaseValue(nNewValue
, eInUnit
);
220 /*--------------------------------------------------------------------
222 --------------------------------------------------------------------*/
224 sal_Int64
PercentField::GetValue( FieldUnit eOutUnit
)
226 return Convert(MetricField::GetValue(), GetUnit(), eOutUnit
);
229 /*--------------------------------------------------------------------
231 --------------------------------------------------------------------*/
233 void PercentField::SetMin(sal_Int64 nNewMin
, FieldUnit eInUnit
)
235 if (GetUnit() != FUNIT_CUSTOM
)
236 MetricField::SetMin(nNewMin
, eInUnit
);
239 if (eInUnit
== FUNIT_NONE
)
241 nOldMin
= Convert(nNewMin
, eInUnit
, eOldUnit
);
243 sal_Int64 nPercent
= Convert(nNewMin
, eInUnit
, FUNIT_CUSTOM
);
244 MetricField::SetMin(Max( static_cast< sal_Int64
>(1), nPercent
));
248 /*--------------------------------------------------------------------
250 --------------------------------------------------------------------*/
252 void PercentField::SetMax(sal_Int64 nNewMax
, FieldUnit eInUnit
)
254 if (GetUnit() != FUNIT_CUSTOM
)
255 MetricField::SetMax(nNewMax
, eInUnit
);
258 if (eInUnit
== FUNIT_NONE
)
260 // SetRefValue(Convert(nNewMax, eInUnit, FUNIT_TWIP));
264 /*--------------------------------------------------------------------
266 --------------------------------------------------------------------*/
268 sal_Int64
PercentField::NormalizePercent(sal_Int64 nValue
)
270 if (GetUnit() != FUNIT_CUSTOM
)
271 nValue
= MetricField::Normalize(nValue
);
273 nValue
= nValue
* ImpPower10(nOldDigits
);
278 /*--------------------------------------------------------------------
280 --------------------------------------------------------------------*/
282 sal_Int64
PercentField::DenormalizePercent(sal_Int64 nValue
)
284 if (GetUnit() != FUNIT_CUSTOM
)
285 nValue
= MetricField::Denormalize(nValue
);
288 sal_Int64 nFactor
= ImpPower10(nOldDigits
);
289 nValue
= ((nValue
+(nFactor
/2)) / nFactor
);
295 /*--------------------------------------------------------------------
297 --------------------------------------------------------------------*/
299 BOOL
PercentField::IsValueModified()
301 if (GetUnit() == FUNIT_CUSTOM
)
304 return MetricField::IsValueModified();
307 /*--------------------------------------------------------------------
309 --------------------------------------------------------------------*/
311 sal_Int64
PercentField::ImpPower10( USHORT n
)
314 sal_Int64 nValue
= 1;
316 for ( i
=0; i
< n
; i
++ )
322 /*--------------------------------------------------------------------
324 --------------------------------------------------------------------*/
326 sal_Int64
PercentField::GetRealValue(FieldUnit eOutUnit
)
328 if (GetUnit() != FUNIT_CUSTOM
)
329 return GetValue(eOutUnit
);
331 return Convert(GetValue(), GetUnit(), eOutUnit
);
334 /*--------------------------------------------------------------------
336 --------------------------------------------------------------------*/
338 sal_Int64
PercentField::Convert(sal_Int64 nValue
, FieldUnit eInUnit
, FieldUnit eOutUnit
)
340 if (eInUnit
== eOutUnit
||
341 (eInUnit
== FUNIT_NONE
&& eOutUnit
== GetUnit()) ||
342 (eOutUnit
== FUNIT_NONE
&& eInUnit
== GetUnit()))
345 if (eInUnit
== FUNIT_CUSTOM
)
347 // Umrechnen in Metrik
348 sal_Int64 nTwipValue
= (nRefValue
* nValue
+ 50) / 100;
350 if (eOutUnit
== FUNIT_TWIP
) // Nur wandeln, wenn unbedingt notwendig
351 return NormalizePercent(nTwipValue
);
353 return ConvertValue(NormalizePercent(nTwipValue
), 0, nOldDigits
, FUNIT_TWIP
, eOutUnit
);
356 if (eOutUnit
== FUNIT_CUSTOM
)
358 // Umrechnen in Prozent
360 nValue
= DenormalizePercent(nValue
);
362 if (eInUnit
== FUNIT_TWIP
) // Nur wandeln, wenn unbedingt notwendig
365 nAktWidth
= ConvertValue(nValue
, 0, nOldDigits
, eInUnit
, FUNIT_TWIP
);
366 // Um 0.5 Prozent runden
367 return ((nAktWidth
* 1000) / nRefValue
+ 5) / 10;
370 return ConvertValue(nValue
, 0, nOldDigits
, eInUnit
, eOutUnit
);