bump product version to 4.1.6.2
[LibreOffice.git] / sw / source / ui / utlui / prcntfld.cxx
blob100176e00796a41851268b5456f0a239f50ee81e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "prcntfld.hxx"
23 PercentField::PercentField( Window* pWin, const ResId& rResId ) :
24 MetricField ( pWin, rResId ),
26 nOldMax (0),
27 nOldMin (0),
28 nLastPercent(-1),
29 nLastValue (-1),
30 eOldUnit (FUNIT_NONE),
31 bLockAutoCalculation(sal_False)
34 nOldSpinSize = GetSpinSize();
35 nRefValue = DenormalizePercent(MetricField::GetMax(FUNIT_TWIP));
36 nOldDigits = GetDecimalDigits();
37 SetCustomUnitText(OUString('%'));
40 PercentFieldWrap::PercentFieldWrap()
41 : nOldMax(0)
42 , nOldMin(0)
43 , nLastPercent(-1)
44 , nLastValue(-1)
45 , eOldUnit(FUNIT_NONE)
46 , bLockAutoCalculation(sal_False)
50 void PercentFieldWrap::set(MetricField *pField)
52 m_pField = pField;
53 nOldSpinSize = m_pField->GetSpinSize();
54 nRefValue = DenormalizePercent(m_pField->GetMax(FUNIT_TWIP));
55 nOldDigits = m_pField->GetDecimalDigits();
56 m_pField->SetCustomUnitText(OUString('%'));
59 void PercentField::SetRefValue(sal_Int64 nValue)
61 sal_Int64 nRealValue = GetRealValue(eOldUnit);
63 nRefValue = nValue;
65 if (!bLockAutoCalculation && (GetUnit() == FUNIT_CUSTOM))
66 SetPrcntValue(nRealValue, eOldUnit);
69 void PercentFieldWrap::SetRefValue(sal_Int64 nValue)
71 sal_Int64 nRealValue = GetRealValue(eOldUnit);
73 nRefValue = nValue;
75 if (!bLockAutoCalculation && (m_pField->GetUnit() == FUNIT_CUSTOM))
76 SetPrcntValue(nRealValue, eOldUnit);
79 void PercentField::ShowPercent(sal_Bool bPercent)
81 if ((bPercent && GetUnit() == FUNIT_CUSTOM) ||
82 (!bPercent && GetUnit() != FUNIT_CUSTOM))
83 return;
85 sal_Int64 nOldValue;
87 if (bPercent)
89 sal_Int64 nAktWidth, nPercent;
91 nOldValue = GetValue();
93 eOldUnit = GetUnit();
94 nOldDigits = GetDecimalDigits();
95 nOldMin = GetMin();
96 nOldMax = GetMax();
97 nOldSpinSize = GetSpinSize();
98 nOldBaseValue = GetBaseValue();
99 SetUnit(FUNIT_CUSTOM);
100 SetDecimalDigits( 0 );
102 nAktWidth = ConvertValue(nOldMin, 0, nOldDigits, eOldUnit, FUNIT_TWIP);
103 // round to 0.5 percent
104 nPercent = ((nAktWidth * 10) / nRefValue + 5) / 10;
106 MetricField::SetMin(std::max(static_cast< sal_Int64 >(1), nPercent));
107 MetricField::SetMax(100);
108 SetSpinSize(5);
109 MetricField::SetBaseValue(0);
110 if (nOldValue != nLastValue)
112 nAktWidth = ConvertValue(nOldValue, 0, nOldDigits, eOldUnit, FUNIT_TWIP);
113 nPercent = ((nAktWidth * 10) / nRefValue + 5) / 10;
114 MetricFormatter::SetValue(nPercent);
115 nLastPercent = nPercent;
116 nLastValue = nOldValue;
118 else
119 MetricFormatter::SetValue(nLastPercent);
121 else
123 sal_Int64 nOldPercent = GetValue(FUNIT_CUSTOM);
125 nOldValue = Convert(GetValue(), GetUnit(), eOldUnit);
127 SetUnit(eOldUnit);
128 SetDecimalDigits(nOldDigits);
129 MetricField::SetMin(nOldMin);
130 MetricField::SetMax(nOldMax);
131 SetSpinSize(nOldSpinSize);
132 MetricField::SetBaseValue(nOldBaseValue);
134 if (nOldPercent != nLastPercent)
136 SetPrcntValue(nOldValue, eOldUnit);
137 nLastPercent = nOldPercent;
138 nLastValue = nOldValue;
140 else
141 SetPrcntValue(nLastValue, eOldUnit);
145 void PercentFieldWrap::ShowPercent(bool bPercent)
147 if ((bPercent && m_pField->GetUnit() == FUNIT_CUSTOM) ||
148 (!bPercent && m_pField->GetUnit() != FUNIT_CUSTOM))
149 return;
151 sal_Int64 nOldValue;
153 if (bPercent)
155 sal_Int64 nAktWidth, nPercent;
157 nOldValue = GetValue();
159 eOldUnit = m_pField->GetUnit();
160 nOldDigits = m_pField->GetDecimalDigits();
161 nOldMin = m_pField->GetMin();
162 nOldMax = m_pField->GetMax();
163 nOldSpinSize = m_pField->GetSpinSize();
164 nOldBaseValue = m_pField->GetBaseValue();
165 m_pField->SetUnit(FUNIT_CUSTOM);
166 m_pField->SetDecimalDigits( 0 );
168 nAktWidth = m_pField->ConvertValue(nOldMin, 0, nOldDigits, eOldUnit, FUNIT_TWIP);
169 // round to 0.5 percent
170 nPercent = ((nAktWidth * 10) / nRefValue + 5) / 10;
172 m_pField->SetMin(std::max(static_cast< sal_Int64 >(1), nPercent));
173 m_pField->SetMax(100);
174 m_pField->SetSpinSize(5);
175 m_pField->SetBaseValue(0);
176 if (nOldValue != nLastValue)
178 nAktWidth = m_pField->ConvertValue(nOldValue, 0, nOldDigits, eOldUnit, FUNIT_TWIP);
179 nPercent = ((nAktWidth * 10) / nRefValue + 5) / 10;
180 m_pField->SetValue(nPercent);
181 nLastPercent = nPercent;
182 nLastValue = nOldValue;
184 else
185 m_pField->SetValue(nLastPercent);
187 else
189 sal_Int64 nOldPercent = GetValue(FUNIT_CUSTOM);
191 nOldValue = Convert(GetValue(), m_pField->GetUnit(), eOldUnit);
193 m_pField->SetUnit(eOldUnit);
194 m_pField->SetDecimalDigits(nOldDigits);
195 m_pField->SetMin(nOldMin);
196 m_pField->SetMax(nOldMax);
197 m_pField->SetSpinSize(nOldSpinSize);
198 m_pField->SetBaseValue(nOldBaseValue);
200 if (nOldPercent != nLastPercent)
202 SetPrcntValue(nOldValue, eOldUnit);
203 nLastPercent = nOldPercent;
204 nLastValue = nOldValue;
206 else
207 SetPrcntValue(nLastValue, eOldUnit);
211 void PercentField::SetValue(sal_Int64 nNewValue, FieldUnit eInUnit)
213 MetricFormatter::SetValue(nNewValue, eInUnit);
216 void PercentField::SetPrcntValue(sal_Int64 nNewValue, FieldUnit eInUnit)
218 if (GetUnit() != FUNIT_CUSTOM || eInUnit == FUNIT_CUSTOM)
219 MetricFormatter::SetValue(Convert(nNewValue, eInUnit, GetUnit()));
221 else
223 // Overwrite output value, do not restore later
224 sal_Int64 nPercent, nAktWidth;
225 if(eInUnit == FUNIT_TWIP)
227 nAktWidth = ConvertValue(nNewValue, 0, nOldDigits, FUNIT_TWIP, FUNIT_TWIP);
229 else
231 sal_Int64 nValue = Convert(nNewValue, eInUnit, eOldUnit);
232 nAktWidth = ConvertValue(nValue, 0, nOldDigits, eOldUnit, FUNIT_TWIP);
234 nPercent = ((nAktWidth * 10) / nRefValue + 5) / 10;
235 MetricFormatter::SetValue(nPercent);
239 void PercentFieldWrap::SetPrcntValue(sal_Int64 nNewValue, FieldUnit eInUnit)
241 if (m_pField->GetUnit() != FUNIT_CUSTOM || eInUnit == FUNIT_CUSTOM)
242 m_pField->SetValue(Convert(nNewValue, eInUnit, m_pField->GetUnit()));
243 else
245 // Overwrite output value, do not restore later
246 sal_Int64 nPercent, nAktWidth;
247 if(eInUnit == FUNIT_TWIP)
249 nAktWidth = m_pField->ConvertValue(nNewValue, 0, nOldDigits, FUNIT_TWIP, FUNIT_TWIP);
251 else
253 sal_Int64 nValue = Convert(nNewValue, eInUnit, eOldUnit);
254 nAktWidth = m_pField->ConvertValue(nValue, 0, nOldDigits, eOldUnit, FUNIT_TWIP);
256 nPercent = ((nAktWidth * 10) / nRefValue + 5) / 10;
257 m_pField->SetValue(nPercent);
261 void PercentField::SetUserValue( sal_Int64 nNewValue, FieldUnit eInUnit )
263 if (GetUnit() != FUNIT_CUSTOM || eInUnit == FUNIT_CUSTOM)
264 MetricField::SetUserValue(Convert(nNewValue, eInUnit, GetUnit()),FUNIT_NONE);
266 else
268 // Overwrite output value, do not restore later
269 sal_Int64 nPercent, nAktWidth;
270 if(eInUnit == FUNIT_TWIP)
272 nAktWidth = ConvertValue(nNewValue, 0, nOldDigits, FUNIT_TWIP, FUNIT_TWIP);
274 else
276 sal_Int64 nValue = Convert(nNewValue, eInUnit, eOldUnit);
277 nAktWidth = ConvertValue(nValue, 0, nOldDigits, eOldUnit, FUNIT_TWIP);
279 nPercent = ((nAktWidth * 10) / nRefValue + 5) / 10;
280 MetricField::SetUserValue(nPercent,FUNIT_NONE);
285 void PercentField::SetBaseValue(sal_Int64 nNewValue, FieldUnit eInUnit)
287 if (GetUnit() == FUNIT_CUSTOM)
288 nOldBaseValue = ConvertValue(nNewValue, 0, nOldDigits, eInUnit, eOldUnit);
289 else
290 MetricField::SetBaseValue(nNewValue, eInUnit);
293 sal_Int64 PercentField::GetValue( FieldUnit eOutUnit )
295 return Convert(MetricField::GetValue(), GetUnit(), eOutUnit);
298 sal_Int64 PercentFieldWrap::GetValue(FieldUnit eOutUnit)
300 return Convert(m_pField->GetValue(), m_pField->GetUnit(), eOutUnit);
303 void PercentField::SetMin(sal_Int64 nNewMin, FieldUnit eInUnit)
305 if (GetUnit() != FUNIT_CUSTOM)
306 MetricField::SetMin(nNewMin, eInUnit);
307 else
309 if (eInUnit == FUNIT_NONE)
310 eInUnit = eOldUnit;
311 nOldMin = Convert(nNewMin, eInUnit, eOldUnit);
313 sal_Int64 nPercent = Convert(nNewMin, eInUnit, FUNIT_CUSTOM);
314 MetricField::SetMin(std::max( static_cast< sal_Int64 >(1), nPercent));
318 void PercentFieldWrap::SetMin(sal_Int64 nNewMin, FieldUnit eInUnit)
320 if (m_pField->GetUnit() != FUNIT_CUSTOM)
321 m_pField->SetMin(nNewMin, eInUnit);
322 else
324 if (eInUnit == FUNIT_NONE)
325 eInUnit = eOldUnit;
326 nOldMin = Convert(nNewMin, eInUnit, eOldUnit);
328 sal_Int64 nPercent = Convert(nNewMin, eInUnit, FUNIT_CUSTOM);
329 m_pField->SetMin(std::max( static_cast< sal_Int64 >(1), nPercent));
333 void PercentField::SetMax(sal_Int64 nNewMax, FieldUnit eInUnit)
335 if (GetUnit() != FUNIT_CUSTOM)
336 MetricField::SetMax(nNewMax, eInUnit);
337 else
339 if (eInUnit == FUNIT_NONE)
340 eInUnit = eOldUnit;
344 void PercentFieldWrap::SetMax(sal_Int64 nNewMax, FieldUnit eInUnit)
346 if (m_pField->GetUnit() != FUNIT_CUSTOM)
347 m_pField->SetMax(nNewMax, eInUnit);
348 else
350 if (eInUnit == FUNIT_NONE)
351 eInUnit = eOldUnit;
355 sal_Int64 PercentField::NormalizePercent(sal_Int64 nValue)
357 if (GetUnit() != FUNIT_CUSTOM)
358 nValue = MetricField::Normalize(nValue);
359 else
360 nValue = nValue * ImpPower10(nOldDigits);
362 return nValue;
365 sal_Int64 PercentFieldWrap::NormalizePercent(sal_Int64 nValue)
367 if (m_pField->GetUnit() != FUNIT_CUSTOM)
368 nValue = m_pField->Normalize(nValue);
369 else
370 nValue = nValue * ImpPower10(nOldDigits);
371 return nValue;
374 sal_Int64 PercentField::DenormalizePercent(sal_Int64 nValue)
376 if (GetUnit() != FUNIT_CUSTOM)
377 nValue = MetricField::Denormalize(nValue);
378 else
380 sal_Int64 nFactor = ImpPower10(nOldDigits);
381 nValue = ((nValue+(nFactor/2)) / nFactor);
384 return nValue;
387 sal_Int64 PercentFieldWrap::DenormalizePercent(sal_Int64 nValue)
389 if (m_pField->GetUnit() != FUNIT_CUSTOM)
390 nValue = m_pField->Denormalize(nValue);
391 else
393 sal_Int64 nFactor = ImpPower10(nOldDigits);
394 nValue = ((nValue+(nFactor/2)) / nFactor);
396 return nValue;
399 sal_Bool PercentField::IsValueModified()
401 if (GetUnit() == FUNIT_CUSTOM)
402 return sal_True;
403 else
404 return MetricField::IsValueModified();
407 sal_Int64 PercentField::ImpPower10( sal_uInt16 n )
409 sal_uInt16 i;
410 sal_Int64 nValue = 1;
412 for ( i=0; i < n; i++ )
413 nValue *= 10;
415 return nValue;
418 sal_Int64 PercentFieldWrap::ImpPower10(sal_uInt16 n)
420 sal_Int64 nValue = 1;
422 for (sal_uInt16 i=0; i < n; ++i)
423 nValue *= 10;
425 return nValue;
428 sal_Int64 PercentField::GetRealValue(FieldUnit eOutUnit)
430 if (GetUnit() != FUNIT_CUSTOM)
431 return GetValue(eOutUnit);
432 else
433 return Convert(GetValue(), GetUnit(), eOutUnit);
436 sal_Int64 PercentFieldWrap::GetRealValue(FieldUnit eOutUnit)
438 if (m_pField->GetUnit() != FUNIT_CUSTOM)
439 return GetValue(eOutUnit);
440 else
441 return Convert(GetValue(), m_pField->GetUnit(), eOutUnit);
444 sal_Int64 PercentField::Convert(sal_Int64 nValue, FieldUnit eInUnit, FieldUnit eOutUnit)
446 if (eInUnit == eOutUnit ||
447 (eInUnit == FUNIT_NONE && eOutUnit == GetUnit()) ||
448 (eOutUnit == FUNIT_NONE && eInUnit == GetUnit()))
449 return nValue;
451 if (eInUnit == FUNIT_CUSTOM)
453 // Convert to metric
454 sal_Int64 nTwipValue = (nRefValue * nValue + 50) / 100;
456 if (eOutUnit == FUNIT_TWIP) // Only convert if necessary
457 return NormalizePercent(nTwipValue);
458 else
459 return ConvertValue(NormalizePercent(nTwipValue), 0, nOldDigits, FUNIT_TWIP, eOutUnit);
462 if (eOutUnit == FUNIT_CUSTOM)
464 // Convert to percent
465 sal_Int64 nAktWidth;
466 nValue = DenormalizePercent(nValue);
468 if (eInUnit == FUNIT_TWIP) // Only convert if necessary
469 nAktWidth = nValue;
470 else
471 nAktWidth = ConvertValue(nValue, 0, nOldDigits, eInUnit, FUNIT_TWIP);
472 // Round to 0.5 percent
473 return ((nAktWidth * 1000) / nRefValue + 5) / 10;
476 return ConvertValue(nValue, 0, nOldDigits, eInUnit, eOutUnit);
479 sal_Int64 PercentFieldWrap::Convert(sal_Int64 nValue, FieldUnit eInUnit, FieldUnit eOutUnit)
481 if (eInUnit == eOutUnit ||
482 (eInUnit == FUNIT_NONE && eOutUnit == m_pField->GetUnit()) ||
483 (eOutUnit == FUNIT_NONE && eInUnit == m_pField->GetUnit()))
484 return nValue;
486 if (eInUnit == FUNIT_CUSTOM)
488 // Convert to metric
489 sal_Int64 nTwipValue = (nRefValue * nValue + 50) / 100;
491 if (eOutUnit == FUNIT_TWIP) // Only convert if necessary
492 return NormalizePercent(nTwipValue);
493 else
494 return m_pField->ConvertValue(NormalizePercent(nTwipValue), 0, nOldDigits, FUNIT_TWIP, eOutUnit);
497 if (eOutUnit == FUNIT_CUSTOM)
499 // Convert to percent
500 sal_Int64 nAktWidth;
501 nValue = DenormalizePercent(nValue);
503 if (eInUnit == FUNIT_TWIP) // Only convert if necessary
504 nAktWidth = nValue;
505 else
506 nAktWidth = m_pField->ConvertValue(nValue, 0, nOldDigits, eInUnit, FUNIT_TWIP);
507 // Round to 0.5 percent
508 return ((nAktWidth * 1000) / nRefValue + 5) / 10;
511 return m_pField->ConvertValue(nValue, 0, nOldDigits, eInUnit, eOutUnit);
514 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */