Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include "prcntfld.hxx"
31 : :
32 : 0 : PercentField::PercentField( Window* pWin, const ResId& rResId ) :
33 : : MetricField ( pWin, rResId ),
34 : :
35 : : nOldMax (0),
36 : : nOldMin (0),
37 : : nLastPercent(-1),
38 : : nLastValue (-1),
39 : : eOldUnit (FUNIT_NONE),
40 : 0 : bLockAutoCalculation(sal_False)
41 : : {
42 : :
43 : 0 : nOldSpinSize = GetSpinSize();
44 [ # # ][ # # ]: 0 : nRefValue = DenormalizePercent(MetricField::GetMax(FUNIT_TWIP));
45 [ # # ]: 0 : nOldDigits = GetDecimalDigits();
46 [ # # ][ # # ]: 0 : SetCustomUnitText(rtl::OUString('%'));
[ # # ]
47 : 0 : }
48 : :
49 : 0 : void PercentField::SetRefValue(sal_Int64 nValue)
50 : : {
51 : 0 : sal_Int64 nRealValue = GetRealValue(eOldUnit);
52 : :
53 : 0 : nRefValue = nValue;
54 : :
55 [ # # ][ # # ]: 0 : if (!bLockAutoCalculation && (GetUnit() == FUNIT_CUSTOM))
[ # # ]
56 : 0 : SetPrcntValue(nRealValue, eOldUnit);
57 : 0 : }
58 : :
59 : 0 : void PercentField::ShowPercent(sal_Bool bPercent)
60 : : {
61 [ # # ][ # # ]: 0 : if ((bPercent && GetUnit() == FUNIT_CUSTOM) ||
[ # # # # ]
[ # # ]
62 : 0 : (!bPercent && GetUnit() != FUNIT_CUSTOM))
63 : 0 : return;
64 : :
65 : : sal_Int64 nOldValue;
66 : :
67 [ # # ]: 0 : if (bPercent)
68 : : {
69 : : sal_Int64 nAktWidth, nPercent;
70 : :
71 : 0 : nOldValue = GetValue();
72 : :
73 : 0 : eOldUnit = GetUnit();
74 : 0 : nOldDigits = GetDecimalDigits();
75 : 0 : nOldMin = GetMin();
76 : 0 : nOldMax = GetMax();
77 : 0 : nOldSpinSize = GetSpinSize();
78 : 0 : nOldBaseValue = GetBaseValue();
79 : 0 : SetUnit(FUNIT_CUSTOM);
80 : 0 : SetDecimalDigits( 0 );
81 : :
82 : 0 : nAktWidth = ConvertValue(nOldMin, 0, nOldDigits, eOldUnit, FUNIT_TWIP);
83 : : // round to 0.5 percent
84 : 0 : nPercent = ((nAktWidth * 10) / nRefValue + 5) / 10;
85 : :
86 : 0 : MetricField::SetMin(Max(static_cast< sal_Int64 >(1), nPercent));
87 : 0 : MetricField::SetMax(100);
88 : 0 : SetSpinSize(5);
89 : 0 : MetricField::SetBaseValue(0);
90 [ # # ]: 0 : if (nOldValue != nLastValue)
91 : : {
92 : 0 : nAktWidth = ConvertValue(nOldValue, 0, nOldDigits, eOldUnit, FUNIT_TWIP);
93 : 0 : nPercent = ((nAktWidth * 10) / nRefValue + 5) / 10;
94 : 0 : MetricFormatter::SetValue(nPercent);
95 : 0 : nLastPercent = nPercent;
96 : 0 : nLastValue = nOldValue;
97 : : }
98 : : else
99 : 0 : MetricFormatter::SetValue(nLastPercent);
100 : : }
101 : : else
102 : : {
103 : 0 : sal_Int64 nOldPercent = GetValue(FUNIT_CUSTOM);
104 : :
105 : 0 : nOldValue = Convert(GetValue(), GetUnit(), eOldUnit);
106 : :
107 : 0 : SetUnit(eOldUnit);
108 : 0 : SetDecimalDigits(nOldDigits);
109 : 0 : MetricField::SetMin(nOldMin);
110 : 0 : MetricField::SetMax(nOldMax);
111 : 0 : SetSpinSize(nOldSpinSize);
112 : 0 : MetricField::SetBaseValue(nOldBaseValue);
113 : :
114 [ # # ]: 0 : if (nOldPercent != nLastPercent)
115 : : {
116 : 0 : SetPrcntValue(nOldValue, eOldUnit);
117 : 0 : nLastPercent = nOldPercent;
118 : 0 : nLastValue = nOldValue;
119 : : }
120 : : else
121 : 0 : SetPrcntValue(nLastValue, eOldUnit);
122 : : }
123 : : }
124 : :
125 : 0 : void PercentField::SetValue(sal_Int64 nNewValue, FieldUnit eInUnit)
126 : : {
127 : 0 : MetricFormatter::SetValue(nNewValue, eInUnit);
128 : 0 : }
129 : :
130 : 0 : void PercentField::SetPrcntValue(sal_Int64 nNewValue, FieldUnit eInUnit)
131 : : {
132 [ # # ][ # # ]: 0 : if (GetUnit() != FUNIT_CUSTOM || eInUnit == FUNIT_CUSTOM)
[ # # ]
133 : 0 : MetricFormatter::SetValue(Convert(nNewValue, eInUnit, GetUnit()));
134 : :
135 : : else
136 : : {
137 : : // Ausgangswert ueberschreiben, nicht spaeter restaurieren
138 : : sal_Int64 nPercent, nAktWidth;
139 [ # # ]: 0 : if(eInUnit == FUNIT_TWIP)
140 : : {
141 : 0 : nAktWidth = ConvertValue(nNewValue, 0, nOldDigits, FUNIT_TWIP, FUNIT_TWIP);
142 : : }
143 : : else
144 : : {
145 : 0 : sal_Int64 nValue = Convert(nNewValue, eInUnit, eOldUnit);
146 : 0 : nAktWidth = ConvertValue(nValue, 0, nOldDigits, eOldUnit, FUNIT_TWIP);
147 : : }
148 : 0 : nPercent = ((nAktWidth * 10) / nRefValue + 5) / 10;
149 : 0 : MetricFormatter::SetValue(nPercent);
150 : : }
151 : 0 : }
152 : :
153 : 0 : void PercentField::SetUserValue( sal_Int64 nNewValue, FieldUnit eInUnit )
154 : : {
155 [ # # ][ # # ]: 0 : if (GetUnit() != FUNIT_CUSTOM || eInUnit == FUNIT_CUSTOM)
[ # # ]
156 : 0 : MetricField::SetUserValue(Convert(nNewValue, eInUnit, GetUnit()),FUNIT_NONE);
157 : :
158 : : else
159 : : {
160 : : // Ausgangswert ueberschreiben, nicht spaeter restaurieren
161 : : sal_Int64 nPercent, nAktWidth;
162 [ # # ]: 0 : if(eInUnit == FUNIT_TWIP)
163 : : {
164 : 0 : nAktWidth = ConvertValue(nNewValue, 0, nOldDigits, FUNIT_TWIP, FUNIT_TWIP);
165 : : }
166 : : else
167 : : {
168 : 0 : sal_Int64 nValue = Convert(nNewValue, eInUnit, eOldUnit);
169 : 0 : nAktWidth = ConvertValue(nValue, 0, nOldDigits, eOldUnit, FUNIT_TWIP);
170 : : }
171 : 0 : nPercent = ((nAktWidth * 10) / nRefValue + 5) / 10;
172 : 0 : MetricField::SetUserValue(nPercent,FUNIT_NONE);
173 : : }
174 : :
175 : 0 : }
176 : :
177 : 0 : void PercentField::SetBaseValue(sal_Int64 nNewValue, FieldUnit eInUnit)
178 : : {
179 [ # # ]: 0 : if (GetUnit() == FUNIT_CUSTOM)
180 : 0 : nOldBaseValue = ConvertValue(nNewValue, 0, nOldDigits, eInUnit, eOldUnit);
181 : : else
182 : 0 : MetricField::SetBaseValue(nNewValue, eInUnit);
183 : 0 : }
184 : :
185 : 0 : sal_Int64 PercentField::GetValue( FieldUnit eOutUnit )
186 : : {
187 : 0 : return Convert(MetricField::GetValue(), GetUnit(), eOutUnit);
188 : : }
189 : :
190 : 0 : void PercentField::SetMin(sal_Int64 nNewMin, FieldUnit eInUnit)
191 : : {
192 [ # # ]: 0 : if (GetUnit() != FUNIT_CUSTOM)
193 : 0 : MetricField::SetMin(nNewMin, eInUnit);
194 : : else
195 : : {
196 [ # # ]: 0 : if (eInUnit == FUNIT_NONE)
197 : 0 : eInUnit = eOldUnit;
198 : 0 : nOldMin = Convert(nNewMin, eInUnit, eOldUnit);
199 : :
200 : 0 : sal_Int64 nPercent = Convert(nNewMin, eInUnit, FUNIT_CUSTOM);
201 : 0 : MetricField::SetMin(Max( static_cast< sal_Int64 >(1), nPercent));
202 : : }
203 : 0 : }
204 : :
205 : 0 : void PercentField::SetMax(sal_Int64 nNewMax, FieldUnit eInUnit)
206 : : {
207 [ # # ]: 0 : if (GetUnit() != FUNIT_CUSTOM)
208 : 0 : MetricField::SetMax(nNewMax, eInUnit);
209 : : else
210 : : {
211 [ # # ]: 0 : if (eInUnit == FUNIT_NONE)
212 : 0 : eInUnit = eOldUnit;
213 : : }
214 : 0 : }
215 : :
216 : 0 : sal_Int64 PercentField::NormalizePercent(sal_Int64 nValue)
217 : : {
218 [ # # ]: 0 : if (GetUnit() != FUNIT_CUSTOM)
219 : 0 : nValue = MetricField::Normalize(nValue);
220 : : else
221 : 0 : nValue = nValue * ImpPower10(nOldDigits);
222 : :
223 : 0 : return nValue;
224 : : }
225 : :
226 : 0 : sal_Int64 PercentField::DenormalizePercent(sal_Int64 nValue)
227 : : {
228 [ # # ]: 0 : if (GetUnit() != FUNIT_CUSTOM)
229 : 0 : nValue = MetricField::Denormalize(nValue);
230 : : else
231 : : {
232 : 0 : sal_Int64 nFactor = ImpPower10(nOldDigits);
233 : 0 : nValue = ((nValue+(nFactor/2)) / nFactor);
234 : : }
235 : :
236 : 0 : return nValue;
237 : : }
238 : :
239 : 0 : sal_Bool PercentField::IsValueModified()
240 : : {
241 [ # # ]: 0 : if (GetUnit() == FUNIT_CUSTOM)
242 : 0 : return sal_True;
243 : : else
244 : 0 : return MetricField::IsValueModified();
245 : : }
246 : :
247 : 0 : sal_Int64 PercentField::ImpPower10( sal_uInt16 n )
248 : : {
249 : : sal_uInt16 i;
250 : 0 : sal_Int64 nValue = 1;
251 : :
252 [ # # ]: 0 : for ( i=0; i < n; i++ )
253 : 0 : nValue *= 10;
254 : :
255 : 0 : return nValue;
256 : : }
257 : :
258 : 0 : sal_Int64 PercentField::GetRealValue(FieldUnit eOutUnit)
259 : : {
260 [ # # ]: 0 : if (GetUnit() != FUNIT_CUSTOM)
261 : 0 : return GetValue(eOutUnit);
262 : : else
263 : 0 : return Convert(GetValue(), GetUnit(), eOutUnit);
264 : : }
265 : :
266 : 0 : sal_Int64 PercentField::Convert(sal_Int64 nValue, FieldUnit eInUnit, FieldUnit eOutUnit)
267 : : {
268 [ # # ]: 0 : if (eInUnit == eOutUnit ||
[ # # # # ]
[ # # # # ]
[ # # ]
269 : 0 : (eInUnit == FUNIT_NONE && eOutUnit == GetUnit()) ||
270 : 0 : (eOutUnit == FUNIT_NONE && eInUnit == GetUnit()))
271 : 0 : return nValue;
272 : :
273 [ # # ]: 0 : if (eInUnit == FUNIT_CUSTOM)
274 : : {
275 : : // Umrechnen in Metrik
276 : 0 : sal_Int64 nTwipValue = (nRefValue * nValue + 50) / 100;
277 : :
278 [ # # ]: 0 : if (eOutUnit == FUNIT_TWIP) // Nur wandeln, wenn unbedingt notwendig
279 : 0 : return NormalizePercent(nTwipValue);
280 : : else
281 : 0 : return ConvertValue(NormalizePercent(nTwipValue), 0, nOldDigits, FUNIT_TWIP, eOutUnit);
282 : : }
283 : :
284 [ # # ]: 0 : if (eOutUnit == FUNIT_CUSTOM)
285 : : {
286 : : // Umrechnen in Prozent
287 : : sal_Int64 nAktWidth;
288 : 0 : nValue = DenormalizePercent(nValue);
289 : :
290 [ # # ]: 0 : if (eInUnit == FUNIT_TWIP) // Nur wandeln, wenn unbedingt notwendig
291 : 0 : nAktWidth = nValue;
292 : : else
293 : 0 : nAktWidth = ConvertValue(nValue, 0, nOldDigits, eInUnit, FUNIT_TWIP);
294 : : // Round to 0.5 percent
295 : 0 : return ((nAktWidth * 1000) / nRefValue + 5) / 10;
296 : : }
297 : :
298 : 0 : return ConvertValue(nValue, 0, nOldDigits, eInUnit, eOutUnit);
299 : : }
300 : :
301 : :
302 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|