Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include "prcntfld.hxx"
22 :
23 0 : PercentField::PercentField( Window* pWin, const ResId& rResId ) :
24 : MetricField ( pWin, rResId ),
25 :
26 : nOldMax (0),
27 : nOldMin (0),
28 : nLastPercent(-1),
29 : nLastValue (-1),
30 : eOldUnit (FUNIT_NONE),
31 0 : bLockAutoCalculation(sal_False)
32 : {
33 :
34 0 : nOldSpinSize = GetSpinSize();
35 0 : nRefValue = DenormalizePercent(MetricField::GetMax(FUNIT_TWIP));
36 0 : nOldDigits = GetDecimalDigits();
37 0 : SetCustomUnitText(rtl::OUString('%'));
38 0 : }
39 :
40 0 : void PercentField::SetRefValue(sal_Int64 nValue)
41 : {
42 0 : sal_Int64 nRealValue = GetRealValue(eOldUnit);
43 :
44 0 : nRefValue = nValue;
45 :
46 0 : if (!bLockAutoCalculation && (GetUnit() == FUNIT_CUSTOM))
47 0 : SetPrcntValue(nRealValue, eOldUnit);
48 0 : }
49 :
50 0 : void PercentField::ShowPercent(sal_Bool bPercent)
51 : {
52 0 : if ((bPercent && GetUnit() == FUNIT_CUSTOM) ||
53 0 : (!bPercent && GetUnit() != FUNIT_CUSTOM))
54 0 : return;
55 :
56 : sal_Int64 nOldValue;
57 :
58 0 : if (bPercent)
59 : {
60 : sal_Int64 nAktWidth, nPercent;
61 :
62 0 : nOldValue = GetValue();
63 :
64 0 : eOldUnit = GetUnit();
65 0 : nOldDigits = GetDecimalDigits();
66 0 : nOldMin = GetMin();
67 0 : nOldMax = GetMax();
68 0 : nOldSpinSize = GetSpinSize();
69 0 : nOldBaseValue = GetBaseValue();
70 0 : SetUnit(FUNIT_CUSTOM);
71 0 : SetDecimalDigits( 0 );
72 :
73 0 : nAktWidth = ConvertValue(nOldMin, 0, nOldDigits, eOldUnit, FUNIT_TWIP);
74 : // round to 0.5 percent
75 0 : nPercent = ((nAktWidth * 10) / nRefValue + 5) / 10;
76 :
77 0 : MetricField::SetMin(Max(static_cast< sal_Int64 >(1), nPercent));
78 0 : MetricField::SetMax(100);
79 0 : SetSpinSize(5);
80 0 : MetricField::SetBaseValue(0);
81 0 : if (nOldValue != nLastValue)
82 : {
83 0 : nAktWidth = ConvertValue(nOldValue, 0, nOldDigits, eOldUnit, FUNIT_TWIP);
84 0 : nPercent = ((nAktWidth * 10) / nRefValue + 5) / 10;
85 0 : MetricFormatter::SetValue(nPercent);
86 0 : nLastPercent = nPercent;
87 0 : nLastValue = nOldValue;
88 : }
89 : else
90 0 : MetricFormatter::SetValue(nLastPercent);
91 : }
92 : else
93 : {
94 0 : sal_Int64 nOldPercent = GetValue(FUNIT_CUSTOM);
95 :
96 0 : nOldValue = Convert(GetValue(), GetUnit(), eOldUnit);
97 :
98 0 : SetUnit(eOldUnit);
99 0 : SetDecimalDigits(nOldDigits);
100 0 : MetricField::SetMin(nOldMin);
101 0 : MetricField::SetMax(nOldMax);
102 0 : SetSpinSize(nOldSpinSize);
103 0 : MetricField::SetBaseValue(nOldBaseValue);
104 :
105 0 : if (nOldPercent != nLastPercent)
106 : {
107 0 : SetPrcntValue(nOldValue, eOldUnit);
108 0 : nLastPercent = nOldPercent;
109 0 : nLastValue = nOldValue;
110 : }
111 : else
112 0 : SetPrcntValue(nLastValue, eOldUnit);
113 : }
114 : }
115 :
116 0 : void PercentField::SetValue(sal_Int64 nNewValue, FieldUnit eInUnit)
117 : {
118 0 : MetricFormatter::SetValue(nNewValue, eInUnit);
119 0 : }
120 :
121 0 : void PercentField::SetPrcntValue(sal_Int64 nNewValue, FieldUnit eInUnit)
122 : {
123 0 : if (GetUnit() != FUNIT_CUSTOM || eInUnit == FUNIT_CUSTOM)
124 0 : MetricFormatter::SetValue(Convert(nNewValue, eInUnit, GetUnit()));
125 :
126 : else
127 : {
128 : // Ausgangswert ueberschreiben, nicht spaeter restaurieren
129 : sal_Int64 nPercent, nAktWidth;
130 0 : if(eInUnit == FUNIT_TWIP)
131 : {
132 0 : nAktWidth = ConvertValue(nNewValue, 0, nOldDigits, FUNIT_TWIP, FUNIT_TWIP);
133 : }
134 : else
135 : {
136 0 : sal_Int64 nValue = Convert(nNewValue, eInUnit, eOldUnit);
137 0 : nAktWidth = ConvertValue(nValue, 0, nOldDigits, eOldUnit, FUNIT_TWIP);
138 : }
139 0 : nPercent = ((nAktWidth * 10) / nRefValue + 5) / 10;
140 0 : MetricFormatter::SetValue(nPercent);
141 : }
142 0 : }
143 :
144 0 : void PercentField::SetUserValue( sal_Int64 nNewValue, FieldUnit eInUnit )
145 : {
146 0 : if (GetUnit() != FUNIT_CUSTOM || eInUnit == FUNIT_CUSTOM)
147 0 : MetricField::SetUserValue(Convert(nNewValue, eInUnit, GetUnit()),FUNIT_NONE);
148 :
149 : else
150 : {
151 : // Ausgangswert ueberschreiben, nicht spaeter restaurieren
152 : sal_Int64 nPercent, nAktWidth;
153 0 : if(eInUnit == FUNIT_TWIP)
154 : {
155 0 : nAktWidth = ConvertValue(nNewValue, 0, nOldDigits, FUNIT_TWIP, FUNIT_TWIP);
156 : }
157 : else
158 : {
159 0 : sal_Int64 nValue = Convert(nNewValue, eInUnit, eOldUnit);
160 0 : nAktWidth = ConvertValue(nValue, 0, nOldDigits, eOldUnit, FUNIT_TWIP);
161 : }
162 0 : nPercent = ((nAktWidth * 10) / nRefValue + 5) / 10;
163 0 : MetricField::SetUserValue(nPercent,FUNIT_NONE);
164 : }
165 :
166 0 : }
167 :
168 0 : void PercentField::SetBaseValue(sal_Int64 nNewValue, FieldUnit eInUnit)
169 : {
170 0 : if (GetUnit() == FUNIT_CUSTOM)
171 0 : nOldBaseValue = ConvertValue(nNewValue, 0, nOldDigits, eInUnit, eOldUnit);
172 : else
173 0 : MetricField::SetBaseValue(nNewValue, eInUnit);
174 0 : }
175 :
176 0 : sal_Int64 PercentField::GetValue( FieldUnit eOutUnit )
177 : {
178 0 : return Convert(MetricField::GetValue(), GetUnit(), eOutUnit);
179 : }
180 :
181 0 : void PercentField::SetMin(sal_Int64 nNewMin, FieldUnit eInUnit)
182 : {
183 0 : if (GetUnit() != FUNIT_CUSTOM)
184 0 : MetricField::SetMin(nNewMin, eInUnit);
185 : else
186 : {
187 0 : if (eInUnit == FUNIT_NONE)
188 0 : eInUnit = eOldUnit;
189 0 : nOldMin = Convert(nNewMin, eInUnit, eOldUnit);
190 :
191 0 : sal_Int64 nPercent = Convert(nNewMin, eInUnit, FUNIT_CUSTOM);
192 0 : MetricField::SetMin(Max( static_cast< sal_Int64 >(1), nPercent));
193 : }
194 0 : }
195 :
196 0 : void PercentField::SetMax(sal_Int64 nNewMax, FieldUnit eInUnit)
197 : {
198 0 : if (GetUnit() != FUNIT_CUSTOM)
199 0 : MetricField::SetMax(nNewMax, eInUnit);
200 : else
201 : {
202 0 : if (eInUnit == FUNIT_NONE)
203 0 : eInUnit = eOldUnit;
204 : }
205 0 : }
206 :
207 0 : sal_Int64 PercentField::NormalizePercent(sal_Int64 nValue)
208 : {
209 0 : if (GetUnit() != FUNIT_CUSTOM)
210 0 : nValue = MetricField::Normalize(nValue);
211 : else
212 0 : nValue = nValue * ImpPower10(nOldDigits);
213 :
214 0 : return nValue;
215 : }
216 :
217 0 : sal_Int64 PercentField::DenormalizePercent(sal_Int64 nValue)
218 : {
219 0 : if (GetUnit() != FUNIT_CUSTOM)
220 0 : nValue = MetricField::Denormalize(nValue);
221 : else
222 : {
223 0 : sal_Int64 nFactor = ImpPower10(nOldDigits);
224 0 : nValue = ((nValue+(nFactor/2)) / nFactor);
225 : }
226 :
227 0 : return nValue;
228 : }
229 :
230 0 : sal_Bool PercentField::IsValueModified()
231 : {
232 0 : if (GetUnit() == FUNIT_CUSTOM)
233 0 : return sal_True;
234 : else
235 0 : return MetricField::IsValueModified();
236 : }
237 :
238 0 : sal_Int64 PercentField::ImpPower10( sal_uInt16 n )
239 : {
240 : sal_uInt16 i;
241 0 : sal_Int64 nValue = 1;
242 :
243 0 : for ( i=0; i < n; i++ )
244 0 : nValue *= 10;
245 :
246 0 : return nValue;
247 : }
248 :
249 0 : sal_Int64 PercentField::GetRealValue(FieldUnit eOutUnit)
250 : {
251 0 : if (GetUnit() != FUNIT_CUSTOM)
252 0 : return GetValue(eOutUnit);
253 : else
254 0 : return Convert(GetValue(), GetUnit(), eOutUnit);
255 : }
256 :
257 0 : sal_Int64 PercentField::Convert(sal_Int64 nValue, FieldUnit eInUnit, FieldUnit eOutUnit)
258 : {
259 0 : if (eInUnit == eOutUnit ||
260 0 : (eInUnit == FUNIT_NONE && eOutUnit == GetUnit()) ||
261 0 : (eOutUnit == FUNIT_NONE && eInUnit == GetUnit()))
262 0 : return nValue;
263 :
264 0 : if (eInUnit == FUNIT_CUSTOM)
265 : {
266 : // Umrechnen in Metrik
267 0 : sal_Int64 nTwipValue = (nRefValue * nValue + 50) / 100;
268 :
269 0 : if (eOutUnit == FUNIT_TWIP) // Nur wandeln, wenn unbedingt notwendig
270 0 : return NormalizePercent(nTwipValue);
271 : else
272 0 : return ConvertValue(NormalizePercent(nTwipValue), 0, nOldDigits, FUNIT_TWIP, eOutUnit);
273 : }
274 :
275 0 : if (eOutUnit == FUNIT_CUSTOM)
276 : {
277 : // Umrechnen in Prozent
278 : sal_Int64 nAktWidth;
279 0 : nValue = DenormalizePercent(nValue);
280 :
281 0 : if (eInUnit == FUNIT_TWIP) // Nur wandeln, wenn unbedingt notwendig
282 0 : nAktWidth = nValue;
283 : else
284 0 : nAktWidth = ConvertValue(nValue, 0, nOldDigits, eInUnit, FUNIT_TWIP);
285 : // Round to 0.5 percent
286 0 : return ((nAktWidth * 1000) / nRefValue + 5) / 10;
287 : }
288 :
289 0 : return ConvertValue(nValue, 0, nOldDigits, eInUnit, eOutUnit);
290 : }
291 :
292 :
293 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|