merge the formfield patch from ooo-build
[ooovba.git] / sw / source / ui / utlui / prcntfld.cxx
blobf0454e4c638967a9f7caa88bf5d6c342e4a72fab
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: prcntfld.cxx,v $
10 * $Revision: 1.9 $
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 /*--------------------------------------------------------------------
42 Beschreibung:
43 --------------------------------------------------------------------*/
45 PercentField::PercentField( Window* pWin, const ResId& rResId ) :
46 MetricField ( pWin, rResId ),
48 nOldMax (0),
49 nOldMin (0),
50 nLastPercent(-1),
51 nLastValue (-1),
52 eOldUnit (FUNIT_NONE),
53 bLockAutoCalculation(sal_False)
56 nOldSpinSize = GetSpinSize();
57 nRefValue = DenormalizePercent(MetricField::GetMax(FUNIT_TWIP));
58 nOldDigits = GetDecimalDigits();
59 SetCustomUnitText('%');
62 /*--------------------------------------------------------------------
63 Beschreibung:
64 --------------------------------------------------------------------*/
66 void PercentField::SetRefValue(sal_Int64 nValue)
68 sal_Int64 nRealValue = GetRealValue(eOldUnit);
70 nRefValue = nValue;
72 if (!bLockAutoCalculation && (GetUnit() == FUNIT_CUSTOM))
73 SetPrcntValue(nRealValue, eOldUnit);
76 /*--------------------------------------------------------------------
77 Beschreibung:
78 --------------------------------------------------------------------*/
80 void PercentField::ShowPercent(BOOL bPercent)
82 if ((bPercent && GetUnit() == FUNIT_CUSTOM) ||
83 (!bPercent && GetUnit() != FUNIT_CUSTOM))
84 return;
86 sal_Int64 nOldValue;
88 if (bPercent)
90 sal_Int64 nAktWidth, nPercent;
92 nOldValue = GetValue();
94 eOldUnit = GetUnit();
95 nOldDigits = GetDecimalDigits();
96 nOldMin = GetMin();
97 nOldMax = GetMax();
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);
109 SetSpinSize(5);
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;
119 else
120 MetricFormatter::SetValue(nLastPercent);
121 // SetValue(100, FUNIT_CUSTOM);
123 else
125 sal_Int64 nOldPercent = GetValue(FUNIT_CUSTOM);
127 nOldValue = Convert(GetValue(), GetUnit(), eOldUnit);
129 SetUnit(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;
142 else
143 SetPrcntValue(nLastValue, eOldUnit);
147 /*--------------------------------------------------------------------
148 Beschreibung:
149 --------------------------------------------------------------------*/
150 void PercentField::SetValue(sal_Int64 nNewValue, FieldUnit eInUnit)
152 MetricFormatter::SetValue(nNewValue, eInUnit);
154 /*--------------------------------------------------------------------
155 Beschreibung:
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()));
162 else
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);
170 else
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 /*--------------------------------------------------------------------
181 Beschreibung:
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);
189 else
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);
197 else
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 /*--------------------------------------------------------------------
209 Beschreibung:
210 --------------------------------------------------------------------*/
212 void PercentField::SetBaseValue(sal_Int64 nNewValue, FieldUnit eInUnit)
214 if (GetUnit() == FUNIT_CUSTOM)
215 nOldBaseValue = ConvertValue(nNewValue, 0, nOldDigits, eInUnit, eOldUnit);
216 else
217 MetricField::SetBaseValue(nNewValue, eInUnit);
220 /*--------------------------------------------------------------------
221 Beschreibung:
222 --------------------------------------------------------------------*/
224 sal_Int64 PercentField::GetValue( FieldUnit eOutUnit )
226 return Convert(MetricField::GetValue(), GetUnit(), eOutUnit);
229 /*--------------------------------------------------------------------
230 Beschreibung:
231 --------------------------------------------------------------------*/
233 void PercentField::SetMin(sal_Int64 nNewMin, FieldUnit eInUnit)
235 if (GetUnit() != FUNIT_CUSTOM)
236 MetricField::SetMin(nNewMin, eInUnit);
237 else
239 if (eInUnit == FUNIT_NONE)
240 eInUnit = eOldUnit;
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 /*--------------------------------------------------------------------
249 Beschreibung:
250 --------------------------------------------------------------------*/
252 void PercentField::SetMax(sal_Int64 nNewMax, FieldUnit eInUnit)
254 if (GetUnit() != FUNIT_CUSTOM)
255 MetricField::SetMax(nNewMax, eInUnit);
256 else
258 if (eInUnit == FUNIT_NONE)
259 eInUnit = eOldUnit;
260 // SetRefValue(Convert(nNewMax, eInUnit, FUNIT_TWIP));
264 /*--------------------------------------------------------------------
265 Beschreibung:
266 --------------------------------------------------------------------*/
268 sal_Int64 PercentField::NormalizePercent(sal_Int64 nValue)
270 if (GetUnit() != FUNIT_CUSTOM)
271 nValue = MetricField::Normalize(nValue);
272 else
273 nValue = nValue * ImpPower10(nOldDigits);
275 return nValue;
278 /*--------------------------------------------------------------------
279 Beschreibung:
280 --------------------------------------------------------------------*/
282 sal_Int64 PercentField::DenormalizePercent(sal_Int64 nValue)
284 if (GetUnit() != FUNIT_CUSTOM)
285 nValue = MetricField::Denormalize(nValue);
286 else
288 sal_Int64 nFactor = ImpPower10(nOldDigits);
289 nValue = ((nValue+(nFactor/2)) / nFactor);
292 return nValue;
295 /*--------------------------------------------------------------------
296 Beschreibung:
297 --------------------------------------------------------------------*/
299 BOOL PercentField::IsValueModified()
301 if (GetUnit() == FUNIT_CUSTOM)
302 return TRUE;
303 else
304 return MetricField::IsValueModified();
307 /*--------------------------------------------------------------------
308 Beschreibung:
309 --------------------------------------------------------------------*/
311 sal_Int64 PercentField::ImpPower10( USHORT n )
313 USHORT i;
314 sal_Int64 nValue = 1;
316 for ( i=0; i < n; i++ )
317 nValue *= 10;
319 return nValue;
322 /*--------------------------------------------------------------------
323 Beschreibung:
324 --------------------------------------------------------------------*/
326 sal_Int64 PercentField::GetRealValue(FieldUnit eOutUnit)
328 if (GetUnit() != FUNIT_CUSTOM)
329 return GetValue(eOutUnit);
330 else
331 return Convert(GetValue(), GetUnit(), eOutUnit);
334 /*--------------------------------------------------------------------
335 Beschreibung:
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()))
343 return nValue;
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);
352 else
353 return ConvertValue(NormalizePercent(nTwipValue), 0, nOldDigits, FUNIT_TWIP, eOutUnit);
356 if (eOutUnit == FUNIT_CUSTOM)
358 // Umrechnen in Prozent
359 sal_Int64 nAktWidth;
360 nValue = DenormalizePercent(nValue);
362 if (eInUnit == FUNIT_TWIP) // Nur wandeln, wenn unbedingt notwendig
363 nAktWidth = nValue;
364 else
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);