1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26
27 // include ---------------------------------------------------------------
28
29
30 #include "prcntfld.hxx"
31
32 // STATIC DATA -----------------------------------------------------------
33
34 /*--------------------------------------------------------------------
35 Beschreibung:
36 --------------------------------------------------------------------*/
37
PercentField(Window * pWin,const ResId & rResId)38 PercentField::PercentField( Window* pWin, const ResId& rResId ) :
39 MetricField ( pWin, rResId ),
40
41 nOldMax (0),
42 nOldMin (0),
43 nLastPercent(-1),
44 nLastValue (-1),
45 eOldUnit (FUNIT_NONE),
46 bLockAutoCalculation(sal_False)
47 {
48
49 nOldSpinSize = GetSpinSize();
50 nRefValue = DenormalizePercent(MetricField::GetMax(FUNIT_TWIP));
51 nOldDigits = GetDecimalDigits();
52 SetCustomUnitText('%');
53 }
54
55 /*--------------------------------------------------------------------
56 Beschreibung:
57 --------------------------------------------------------------------*/
58
SetRefValue(sal_Int64 nValue)59 void PercentField::SetRefValue(sal_Int64 nValue)
60 {
61 sal_Int64 nRealValue = GetRealValue(eOldUnit);
62
63 nRefValue = nValue;
64
65 if (!bLockAutoCalculation && (GetUnit() == FUNIT_CUSTOM))
66 SetPrcntValue(nRealValue, eOldUnit);
67 }
68
69 /*--------------------------------------------------------------------
70 Beschreibung:
71 --------------------------------------------------------------------*/
72
ShowPercent(sal_Bool bPercent)73 void PercentField::ShowPercent(sal_Bool bPercent)
74 {
75 if ((bPercent && GetUnit() == FUNIT_CUSTOM) ||
76 (!bPercent && GetUnit() != FUNIT_CUSTOM))
77 return;
78
79 sal_Int64 nOldValue;
80
81 if (bPercent)
82 {
83 sal_Int64 nAktWidth, nPercent;
84
85 nOldValue = GetValue();
86
87 eOldUnit = GetUnit();
88 nOldDigits = GetDecimalDigits();
89 nOldMin = GetMin();
90 nOldMax = GetMax();
91 nOldSpinSize = GetSpinSize();
92 nOldBaseValue = GetBaseValue();
93 SetUnit(FUNIT_CUSTOM);
94 SetDecimalDigits( 0 );
95
96 nAktWidth = ConvertValue(nOldMin, 0, nOldDigits, eOldUnit, FUNIT_TWIP);
97 // Um 0.5 Prozent aufrunden
98 nPercent = ((nAktWidth * 10) / nRefValue + 5) / 10;
99
100 MetricField::SetMin(Max(static_cast< sal_Int64 >(1), nPercent));
101 MetricField::SetMax(100);
102 SetSpinSize(5);
103 MetricField::SetBaseValue(0);
104 if (nOldValue != nLastValue)
105 {
106 nAktWidth = ConvertValue(nOldValue, 0, nOldDigits, eOldUnit, FUNIT_TWIP);
107 nPercent = ((nAktWidth * 10) / nRefValue + 5) / 10;
108 MetricFormatter::SetValue(nPercent);
109 nLastPercent = nPercent;
110 nLastValue = nOldValue;
111 }
112 else
113 MetricFormatter::SetValue(nLastPercent);
114 // SetValue(100, FUNIT_CUSTOM);
115 }
116 else
117 {
118 sal_Int64 nOldPercent = GetValue(FUNIT_CUSTOM);
119
120 nOldValue = Convert(GetValue(), GetUnit(), eOldUnit);
121
122 SetUnit(eOldUnit);
123 SetDecimalDigits(nOldDigits);
124 MetricField::SetMin(nOldMin);
125 MetricField::SetMax(nOldMax);
126 SetSpinSize(nOldSpinSize);
127 MetricField::SetBaseValue(nOldBaseValue);
128
129 if (nOldPercent != nLastPercent)
130 {
131 SetPrcntValue(nOldValue, eOldUnit);
132 nLastPercent = nOldPercent;
133 nLastValue = nOldValue;
134 }
135 else
136 SetPrcntValue(nLastValue, eOldUnit);
137 }
138 }
139
140 /*--------------------------------------------------------------------
141 Beschreibung:
142 --------------------------------------------------------------------*/
SetValue(sal_Int64 nNewValue,FieldUnit eInUnit)143 void PercentField::SetValue(sal_Int64 nNewValue, FieldUnit eInUnit)
144 {
145 MetricFormatter::SetValue(nNewValue, eInUnit);
146 }
147 /*--------------------------------------------------------------------
148 Beschreibung:
149 --------------------------------------------------------------------*/
SetPrcntValue(sal_Int64 nNewValue,FieldUnit eInUnit)150 void PercentField::SetPrcntValue(sal_Int64 nNewValue, FieldUnit eInUnit)
151 {
152 if (GetUnit() != FUNIT_CUSTOM || eInUnit == FUNIT_CUSTOM)
153 MetricFormatter::SetValue(Convert(nNewValue, eInUnit, GetUnit()));
154
155 else
156 {
157 // Ausgangswert ueberschreiben, nicht spaeter restaurieren
158 sal_Int64 nPercent, nAktWidth;
159 if(eInUnit == FUNIT_TWIP)
160 {
161 nAktWidth = ConvertValue(nNewValue, 0, nOldDigits, FUNIT_TWIP, FUNIT_TWIP);
162 }
163 else
164 {
165 sal_Int64 nValue = Convert(nNewValue, eInUnit, eOldUnit);
166 nAktWidth = ConvertValue(nValue, 0, nOldDigits, eOldUnit, FUNIT_TWIP);
167 }
168 nPercent = ((nAktWidth * 10) / nRefValue + 5) / 10;
169 MetricFormatter::SetValue(nPercent);
170 }
171 }
172
173 /*--------------------------------------------------------------------
174 Beschreibung:
175 --------------------------------------------------------------------*/
176
SetUserValue(sal_Int64 nNewValue,FieldUnit eInUnit)177 void PercentField::SetUserValue( sal_Int64 nNewValue, FieldUnit eInUnit )
178 {
179 if (GetUnit() != FUNIT_CUSTOM || eInUnit == FUNIT_CUSTOM)
180 MetricField::SetUserValue(Convert(nNewValue, eInUnit, GetUnit()),FUNIT_NONE);
181
182 else
183 {
184 // Ausgangswert ueberschreiben, nicht spaeter restaurieren
185 sal_Int64 nPercent, nAktWidth;
186 if(eInUnit == FUNIT_TWIP)
187 {
188 nAktWidth = ConvertValue(nNewValue, 0, nOldDigits, FUNIT_TWIP, FUNIT_TWIP);
189 }
190 else
191 {
192 sal_Int64 nValue = Convert(nNewValue, eInUnit, eOldUnit);
193 nAktWidth = ConvertValue(nValue, 0, nOldDigits, eOldUnit, FUNIT_TWIP);
194 }
195 nPercent = ((nAktWidth * 10) / nRefValue + 5) / 10;
196 MetricField::SetUserValue(nPercent,FUNIT_NONE);
197 }
198
199 }
200
201 /*--------------------------------------------------------------------
202 Beschreibung:
203 --------------------------------------------------------------------*/
204
SetBaseValue(sal_Int64 nNewValue,FieldUnit eInUnit)205 void PercentField::SetBaseValue(sal_Int64 nNewValue, FieldUnit eInUnit)
206 {
207 if (GetUnit() == FUNIT_CUSTOM)
208 nOldBaseValue = ConvertValue(nNewValue, 0, nOldDigits, eInUnit, eOldUnit);
209 else
210 MetricField::SetBaseValue(nNewValue, eInUnit);
211 }
212
213 /*--------------------------------------------------------------------
214 Beschreibung:
215 --------------------------------------------------------------------*/
216
GetValue(FieldUnit eOutUnit)217 sal_Int64 PercentField::GetValue( FieldUnit eOutUnit )
218 {
219 return Convert(MetricField::GetValue(), GetUnit(), eOutUnit);
220 }
221
222 /*--------------------------------------------------------------------
223 Beschreibung:
224 --------------------------------------------------------------------*/
225
SetMin(sal_Int64 nNewMin,FieldUnit eInUnit)226 void PercentField::SetMin(sal_Int64 nNewMin, FieldUnit eInUnit)
227 {
228 if (GetUnit() != FUNIT_CUSTOM)
229 MetricField::SetMin(nNewMin, eInUnit);
230 else
231 {
232 if (eInUnit == FUNIT_NONE)
233 eInUnit = eOldUnit;
234 nOldMin = Convert(nNewMin, eInUnit, eOldUnit);
235
236 sal_Int64 nPercent = Convert(nNewMin, eInUnit, FUNIT_CUSTOM);
237 MetricField::SetMin(Max( static_cast< sal_Int64 >(1), nPercent));
238 }
239 }
240
241 /*--------------------------------------------------------------------
242 Beschreibung:
243 --------------------------------------------------------------------*/
244
SetMax(sal_Int64 nNewMax,FieldUnit eInUnit)245 void PercentField::SetMax(sal_Int64 nNewMax, FieldUnit eInUnit)
246 {
247 if (GetUnit() != FUNIT_CUSTOM)
248 MetricField::SetMax(nNewMax, eInUnit);
249 else
250 {
251 if (eInUnit == FUNIT_NONE)
252 eInUnit = eOldUnit;
253 // SetRefValue(Convert(nNewMax, eInUnit, FUNIT_TWIP));
254 }
255 }
256
257 /*--------------------------------------------------------------------
258 Beschreibung:
259 --------------------------------------------------------------------*/
260
NormalizePercent(sal_Int64 nValue)261 sal_Int64 PercentField::NormalizePercent(sal_Int64 nValue)
262 {
263 if (GetUnit() != FUNIT_CUSTOM)
264 nValue = MetricField::Normalize(nValue);
265 else
266 nValue = nValue * ImpPower10(nOldDigits);
267
268 return nValue;
269 }
270
271 /*--------------------------------------------------------------------
272 Beschreibung:
273 --------------------------------------------------------------------*/
274
DenormalizePercent(sal_Int64 nValue)275 sal_Int64 PercentField::DenormalizePercent(sal_Int64 nValue)
276 {
277 if (GetUnit() != FUNIT_CUSTOM)
278 nValue = MetricField::Denormalize(nValue);
279 else
280 {
281 sal_Int64 nFactor = ImpPower10(nOldDigits);
282 nValue = ((nValue+(nFactor/2)) / nFactor);
283 }
284
285 return nValue;
286 }
287
288 /*--------------------------------------------------------------------
289 Beschreibung:
290 --------------------------------------------------------------------*/
291
IsValueModified()292 sal_Bool PercentField::IsValueModified()
293 {
294 if (GetUnit() == FUNIT_CUSTOM)
295 return sal_True;
296 else
297 return MetricField::IsValueModified();
298 }
299
300 /*--------------------------------------------------------------------
301 Beschreibung:
302 --------------------------------------------------------------------*/
303
ImpPower10(sal_uInt16 n)304 sal_Int64 PercentField::ImpPower10( sal_uInt16 n )
305 {
306 sal_uInt16 i;
307 sal_Int64 nValue = 1;
308
309 for ( i=0; i < n; i++ )
310 nValue *= 10;
311
312 return nValue;
313 }
314
315 /*--------------------------------------------------------------------
316 Beschreibung:
317 --------------------------------------------------------------------*/
318
GetRealValue(FieldUnit eOutUnit)319 sal_Int64 PercentField::GetRealValue(FieldUnit eOutUnit)
320 {
321 if (GetUnit() != FUNIT_CUSTOM)
322 return GetValue(eOutUnit);
323 else
324 return Convert(GetValue(), GetUnit(), eOutUnit);
325 }
326
327 /*--------------------------------------------------------------------
328 Beschreibung:
329 --------------------------------------------------------------------*/
330
Convert(sal_Int64 nValue,FieldUnit eInUnit,FieldUnit eOutUnit)331 sal_Int64 PercentField::Convert(sal_Int64 nValue, FieldUnit eInUnit, FieldUnit eOutUnit)
332 {
333 if (eInUnit == eOutUnit ||
334 (eInUnit == FUNIT_NONE && eOutUnit == GetUnit()) ||
335 (eOutUnit == FUNIT_NONE && eInUnit == GetUnit()))
336 return nValue;
337
338 if (eInUnit == FUNIT_CUSTOM)
339 {
340 // Umrechnen in Metrik
341 sal_Int64 nTwipValue = (nRefValue * nValue + 50) / 100;
342
343 if (eOutUnit == FUNIT_TWIP) // Nur wandeln, wenn unbedingt notwendig
344 return NormalizePercent(nTwipValue);
345 else
346 return ConvertValue(NormalizePercent(nTwipValue), 0, nOldDigits, FUNIT_TWIP, eOutUnit);
347 }
348
349 if (eOutUnit == FUNIT_CUSTOM)
350 {
351 // Umrechnen in Prozent
352 sal_Int64 nAktWidth;
353 nValue = DenormalizePercent(nValue);
354
355 if (eInUnit == FUNIT_TWIP) // Nur wandeln, wenn unbedingt notwendig
356 nAktWidth = nValue;
357 else
358 nAktWidth = ConvertValue(nValue, 0, nOldDigits, eInUnit, FUNIT_TWIP);
359 // Um 0.5 Prozent runden
360 return ((nAktWidth * 1000) / nRefValue + 5) / 10;
361 }
362
363 return ConvertValue(nValue, 0, nOldDigits, eInUnit, eOutUnit);
364 }
365
366
367