xref: /AOO41X/main/vcl/source/control/field.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_vcl.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #ifndef _BIGINT_HXX
32*cdf0e10cSrcweir #define _TOOLS_BIGINT
33*cdf0e10cSrcweir #include "tools/bigint.hxx"
34*cdf0e10cSrcweir #endif
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include "tools/debug.hxx"
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include "tools/rc.h"
39*cdf0e10cSrcweir #include "tools/resary.hxx"
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include "vcl/field.hxx"
42*cdf0e10cSrcweir #include "vcl/event.hxx"
43*cdf0e10cSrcweir #include "vcl/svapp.hxx"
44*cdf0e10cSrcweir #include "vcl/unohelp.hxx"
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #include "svids.hrc"
47*cdf0e10cSrcweir #include "svdata.hxx"
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #include "i18nutil/unicode.hxx"
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir #include "rtl/math.hxx"
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir #include <unotools/localedatawrapper.hxx>
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir using namespace ::com::sun::star;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir // -----------------------------------------------------------------------
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir #define FORMAT_NUMERIC       1
61*cdf0e10cSrcweir #define FORMAT_METRIC        2
62*cdf0e10cSrcweir #define FORMAT_CURRENCY      3
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir // -----------------------------------------------------------------------
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir static sal_Int64 ImplPower10( sal_uInt16 n )
67*cdf0e10cSrcweir {
68*cdf0e10cSrcweir     sal_uInt16 i;
69*cdf0e10cSrcweir     sal_Int64   nValue = 1;
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir     for ( i=0; i < n; i++ )
72*cdf0e10cSrcweir         nValue *= 10;
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir     return nValue;
75*cdf0e10cSrcweir }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir // -----------------------------------------------------------------------
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir static sal_Bool ImplNumericProcessKeyInput( Edit*, const KeyEvent& rKEvt,
80*cdf0e10cSrcweir                                         sal_Bool bStrictFormat, sal_Bool bThousandSep,
81*cdf0e10cSrcweir                                         const LocaleDataWrapper& rLocaleDataWrappper )
82*cdf0e10cSrcweir {
83*cdf0e10cSrcweir     if ( !bStrictFormat )
84*cdf0e10cSrcweir         return sal_False;
85*cdf0e10cSrcweir     else
86*cdf0e10cSrcweir     {
87*cdf0e10cSrcweir         xub_Unicode cChar = rKEvt.GetCharCode();
88*cdf0e10cSrcweir         sal_uInt16      nGroup = rKEvt.GetKeyCode().GetGroup();
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir         if ( (nGroup == KEYGROUP_FKEYS) || (nGroup == KEYGROUP_CURSOR) ||
91*cdf0e10cSrcweir              (nGroup == KEYGROUP_MISC) ||
92*cdf0e10cSrcweir              ((cChar >= '0') && (cChar <= '9')) ||
93*cdf0e10cSrcweir              (cChar == rLocaleDataWrappper.getNumDecimalSep() ) ||
94*cdf0e10cSrcweir              (bThousandSep && (cChar == rLocaleDataWrappper.getNumThousandSep())) ||
95*cdf0e10cSrcweir              (cChar == '-') )
96*cdf0e10cSrcweir             return sal_False;
97*cdf0e10cSrcweir         else
98*cdf0e10cSrcweir             return sal_True;
99*cdf0e10cSrcweir     }
100*cdf0e10cSrcweir }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir // -----------------------------------------------------------------------
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir static sal_Bool ImplNumericGetValue( const XubString& rStr, double& rValue,
105*cdf0e10cSrcweir                                  sal_uInt16 nDecDigits, const LocaleDataWrapper& rLocaleDataWrappper,
106*cdf0e10cSrcweir                                  sal_Bool bCurrency = sal_False )
107*cdf0e10cSrcweir {
108*cdf0e10cSrcweir     XubString   aStr = rStr;
109*cdf0e10cSrcweir     XubString   aStr1;
110*cdf0e10cSrcweir     XubString   aStr2;
111*cdf0e10cSrcweir     sal_Bool        bNegative = sal_False;
112*cdf0e10cSrcweir     xub_StrLen  nDecPos;
113*cdf0e10cSrcweir     xub_StrLen  i;
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir     // Reaktion auf leeren String
116*cdf0e10cSrcweir     if ( !rStr.Len() )
117*cdf0e10cSrcweir         return sal_False;
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir     // Fuehrende und nachfolgende Leerzeichen entfernen
120*cdf0e10cSrcweir     aStr.EraseLeadingAndTrailingChars( ' ' );
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir     // Position des Dezimalpunktes suchen
123*cdf0e10cSrcweir     nDecPos = aStr.Search( rLocaleDataWrappper.getNumDecimalSep() );
124*cdf0e10cSrcweir     if ( nDecPos != STRING_NOTFOUND )
125*cdf0e10cSrcweir     {
126*cdf0e10cSrcweir         aStr1 = aStr.Copy( 0, nDecPos );
127*cdf0e10cSrcweir         aStr2 = aStr.Copy( nDecPos+1 );
128*cdf0e10cSrcweir     }
129*cdf0e10cSrcweir     else
130*cdf0e10cSrcweir         aStr1 = aStr;
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir     // Negativ ?
133*cdf0e10cSrcweir     if ( bCurrency )
134*cdf0e10cSrcweir     {
135*cdf0e10cSrcweir         if ( (aStr.GetChar( 0 ) == '(') && (aStr.GetChar( aStr.Len()-1 ) == ')') )
136*cdf0e10cSrcweir             bNegative = sal_True;
137*cdf0e10cSrcweir         if ( !bNegative )
138*cdf0e10cSrcweir         {
139*cdf0e10cSrcweir             for ( i=0; i < aStr.Len(); i++ )
140*cdf0e10cSrcweir             {
141*cdf0e10cSrcweir                 if ( (aStr.GetChar( i ) >= '0') && (aStr.GetChar( i ) <= '9') )
142*cdf0e10cSrcweir                     break;
143*cdf0e10cSrcweir                 else if ( aStr.GetChar( i ) == '-' )
144*cdf0e10cSrcweir                 {
145*cdf0e10cSrcweir                     bNegative = sal_True;
146*cdf0e10cSrcweir                     break;
147*cdf0e10cSrcweir                 }
148*cdf0e10cSrcweir             }
149*cdf0e10cSrcweir         }
150*cdf0e10cSrcweir         if ( !bNegative && bCurrency && aStr.Len() )
151*cdf0e10cSrcweir         {
152*cdf0e10cSrcweir             sal_uInt16 nFormat = rLocaleDataWrappper.getCurrNegativeFormat();
153*cdf0e10cSrcweir             if ( (nFormat == 3) || (nFormat == 6)  ||
154*cdf0e10cSrcweir                  (nFormat == 7) || (nFormat == 10) )
155*cdf0e10cSrcweir             {
156*cdf0e10cSrcweir                 for ( i = (xub_StrLen)(aStr.Len()-1); i > 0; i++ )
157*cdf0e10cSrcweir                 {
158*cdf0e10cSrcweir                     if ( (aStr.GetChar( i ) >= '0') && (aStr.GetChar( i ) <= '9') )
159*cdf0e10cSrcweir                         break;
160*cdf0e10cSrcweir                     else if ( aStr.GetChar( i ) == '-' )
161*cdf0e10cSrcweir                     {
162*cdf0e10cSrcweir                         bNegative = sal_True;
163*cdf0e10cSrcweir                         break;
164*cdf0e10cSrcweir                     }
165*cdf0e10cSrcweir                 }
166*cdf0e10cSrcweir             }
167*cdf0e10cSrcweir         }
168*cdf0e10cSrcweir     }
169*cdf0e10cSrcweir     else
170*cdf0e10cSrcweir     {
171*cdf0e10cSrcweir         if ( aStr1.GetChar( 0 ) == '-' )
172*cdf0e10cSrcweir             bNegative = sal_True;
173*cdf0e10cSrcweir     }
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir     // Alle unerwuenschten Zeichen rauswerfen
176*cdf0e10cSrcweir     for ( i=0; i < aStr1.Len(); )
177*cdf0e10cSrcweir     {
178*cdf0e10cSrcweir         if ( (aStr1.GetChar( i ) >= '0') && (aStr1.GetChar( i ) <= '9') )
179*cdf0e10cSrcweir             i++;
180*cdf0e10cSrcweir         else
181*cdf0e10cSrcweir             aStr1.Erase( i, 1 );
182*cdf0e10cSrcweir     }
183*cdf0e10cSrcweir     for ( i=0; i < aStr2.Len(); )
184*cdf0e10cSrcweir     {
185*cdf0e10cSrcweir         if ( (aStr2.GetChar( i ) >= '0') && (aStr2.GetChar( i ) <= '9') )
186*cdf0e10cSrcweir             i++;
187*cdf0e10cSrcweir         else
188*cdf0e10cSrcweir             aStr2.Erase( i, 1 );
189*cdf0e10cSrcweir     }
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir     if ( !aStr1.Len() && !aStr2.Len() )
192*cdf0e10cSrcweir         return sal_False;
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir     if ( !aStr1.Len() )
195*cdf0e10cSrcweir         aStr1.Insert( '0' );
196*cdf0e10cSrcweir     if ( bNegative )
197*cdf0e10cSrcweir         aStr1.Insert( '-', 0 );
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir     // Nachkommateil zurechtstutzen und dabei runden
200*cdf0e10cSrcweir     sal_Bool bRound = sal_False;
201*cdf0e10cSrcweir     if ( aStr2.Len() > nDecDigits )
202*cdf0e10cSrcweir     {
203*cdf0e10cSrcweir         if ( aStr2.GetChar( nDecDigits ) >= '5' )
204*cdf0e10cSrcweir             bRound = sal_True;
205*cdf0e10cSrcweir         aStr2.Erase( nDecDigits );
206*cdf0e10cSrcweir     }
207*cdf0e10cSrcweir     if ( aStr2.Len() < nDecDigits )
208*cdf0e10cSrcweir         aStr2.Expand( nDecDigits, '0' );
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir     aStr  = aStr1;
211*cdf0e10cSrcweir     aStr += aStr2;
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir     // Bereichsueberpruefung
214*cdf0e10cSrcweir     double nValue = aStr.ToDouble();
215*cdf0e10cSrcweir     if ( bRound )
216*cdf0e10cSrcweir     {
217*cdf0e10cSrcweir         if ( !bNegative )
218*cdf0e10cSrcweir             nValue++;
219*cdf0e10cSrcweir         else
220*cdf0e10cSrcweir             nValue--;
221*cdf0e10cSrcweir     }
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir     rValue = nValue;
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir     return sal_True;
226*cdf0e10cSrcweir }
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir static void ImplUpdateSeparatorString( String& io_rText,
229*cdf0e10cSrcweir                                        const String& rOldDecSep, const String& rNewDecSep,
230*cdf0e10cSrcweir                                        const String& rOldThSep, const String& rNewThSep )
231*cdf0e10cSrcweir {
232*cdf0e10cSrcweir     rtl::OUStringBuffer aBuf( io_rText.Len() );
233*cdf0e10cSrcweir     xub_StrLen nIndexDec = 0, nIndexTh = 0, nIndex = 0;
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir     const sal_Unicode* pBuffer = io_rText.GetBuffer();
236*cdf0e10cSrcweir     while( nIndex != STRING_NOTFOUND )
237*cdf0e10cSrcweir     {
238*cdf0e10cSrcweir         nIndexDec = io_rText.Search( rOldDecSep, nIndex );
239*cdf0e10cSrcweir         nIndexTh = io_rText.Search( rOldThSep, nIndex );
240*cdf0e10cSrcweir         if(   (nIndexTh != STRING_NOTFOUND && nIndexDec != STRING_NOTFOUND && nIndexTh < nIndexDec )
241*cdf0e10cSrcweir            || (nIndexTh != STRING_NOTFOUND && nIndexDec == STRING_NOTFOUND)
242*cdf0e10cSrcweir            )
243*cdf0e10cSrcweir         {
244*cdf0e10cSrcweir             aBuf.append( pBuffer + nIndex, nIndexTh - nIndex );
245*cdf0e10cSrcweir             aBuf.append( rNewThSep );
246*cdf0e10cSrcweir             nIndex = nIndexTh + rOldThSep.Len();
247*cdf0e10cSrcweir         }
248*cdf0e10cSrcweir         else if( nIndexDec != STRING_NOTFOUND )
249*cdf0e10cSrcweir         {
250*cdf0e10cSrcweir             aBuf.append( pBuffer + nIndex, nIndexDec - nIndex );
251*cdf0e10cSrcweir             aBuf.append( rNewDecSep );
252*cdf0e10cSrcweir             nIndex = nIndexDec + rOldDecSep.Len();
253*cdf0e10cSrcweir         }
254*cdf0e10cSrcweir         else
255*cdf0e10cSrcweir         {
256*cdf0e10cSrcweir             aBuf.append( pBuffer + nIndex );
257*cdf0e10cSrcweir             nIndex = STRING_NOTFOUND;
258*cdf0e10cSrcweir         }
259*cdf0e10cSrcweir     }
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir     io_rText = aBuf.makeStringAndClear();
262*cdf0e10cSrcweir }
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir static void ImplUpdateSeparators( const String& rOldDecSep, const String& rNewDecSep,
265*cdf0e10cSrcweir                                   const String& rOldThSep, const String& rNewThSep,
266*cdf0e10cSrcweir                                   Edit* pEdit )
267*cdf0e10cSrcweir {
268*cdf0e10cSrcweir     bool bChangeDec = (rOldDecSep != rNewDecSep);
269*cdf0e10cSrcweir     bool bChangeTh = (rOldThSep != rNewThSep );
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir     if( bChangeDec || bChangeTh )
272*cdf0e10cSrcweir     {
273*cdf0e10cSrcweir         sal_Bool bUpdateMode = pEdit->IsUpdateMode();
274*cdf0e10cSrcweir         pEdit->SetUpdateMode( sal_False );
275*cdf0e10cSrcweir         String aText = pEdit->GetText();
276*cdf0e10cSrcweir         ImplUpdateSeparatorString( aText, rOldDecSep, rNewDecSep, rOldThSep, rNewThSep );
277*cdf0e10cSrcweir         pEdit->SetText( aText );
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir         ComboBox* pCombo = dynamic_cast<ComboBox*>(pEdit);
280*cdf0e10cSrcweir         if( pCombo )
281*cdf0e10cSrcweir         {
282*cdf0e10cSrcweir             // update box entries
283*cdf0e10cSrcweir             sal_uInt16 nEntryCount = pCombo->GetEntryCount();
284*cdf0e10cSrcweir             for ( sal_uInt16 i=0; i < nEntryCount; i++ )
285*cdf0e10cSrcweir             {
286*cdf0e10cSrcweir                 aText = pCombo->GetEntry( i );
287*cdf0e10cSrcweir                 void* pEntryData = pCombo->GetEntryData( i );
288*cdf0e10cSrcweir                 ImplUpdateSeparatorString( aText, rOldDecSep, rNewDecSep, rOldThSep, rNewThSep );
289*cdf0e10cSrcweir                 pCombo->RemoveEntry( i );
290*cdf0e10cSrcweir                 pCombo->InsertEntry( aText, i );
291*cdf0e10cSrcweir                 pCombo->SetEntryData( i, pEntryData );
292*cdf0e10cSrcweir             }
293*cdf0e10cSrcweir         }
294*cdf0e10cSrcweir         if( bUpdateMode )
295*cdf0e10cSrcweir             pEdit->SetUpdateMode( bUpdateMode );
296*cdf0e10cSrcweir     }
297*cdf0e10cSrcweir }
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir // -----------------------------------------------------------------------
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir FormatterBase::FormatterBase( Edit* pField )
302*cdf0e10cSrcweir {
303*cdf0e10cSrcweir     mpField                     = pField;
304*cdf0e10cSrcweir     mpLocaleDataWrapper         = NULL;
305*cdf0e10cSrcweir     mbReformat                  = sal_False;
306*cdf0e10cSrcweir     mbStrictFormat              = sal_False;
307*cdf0e10cSrcweir     mbEmptyFieldValue           = sal_False;
308*cdf0e10cSrcweir     mbEmptyFieldValueEnabled    = sal_False;
309*cdf0e10cSrcweir     mbDefaultLocale             = sal_True;
310*cdf0e10cSrcweir }
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir // -----------------------------------------------------------------------
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir FormatterBase::~FormatterBase()
315*cdf0e10cSrcweir {
316*cdf0e10cSrcweir     delete mpLocaleDataWrapper;
317*cdf0e10cSrcweir }
318*cdf0e10cSrcweir 
319*cdf0e10cSrcweir // -----------------------------------------------------------------------
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir LocaleDataWrapper& FormatterBase::ImplGetLocaleDataWrapper() const
322*cdf0e10cSrcweir {
323*cdf0e10cSrcweir     if ( !mpLocaleDataWrapper )
324*cdf0e10cSrcweir     {
325*cdf0e10cSrcweir         ((FormatterBase*)this)->mpLocaleDataWrapper = new LocaleDataWrapper( vcl::unohelper::GetMultiServiceFactory(), GetLocale() );
326*cdf0e10cSrcweir     }
327*cdf0e10cSrcweir     return *mpLocaleDataWrapper;
328*cdf0e10cSrcweir }
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir const LocaleDataWrapper& FormatterBase::GetLocaleDataWrapper() const
331*cdf0e10cSrcweir {
332*cdf0e10cSrcweir     return ImplGetLocaleDataWrapper();
333*cdf0e10cSrcweir }
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir // -----------------------------------------------------------------------
336*cdf0e10cSrcweir 
337*cdf0e10cSrcweir void FormatterBase::Reformat()
338*cdf0e10cSrcweir {
339*cdf0e10cSrcweir }
340*cdf0e10cSrcweir 
341*cdf0e10cSrcweir // -----------------------------------------------------------------------
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir void FormatterBase::ReformatAll()
344*cdf0e10cSrcweir {
345*cdf0e10cSrcweir     Reformat();
346*cdf0e10cSrcweir };
347*cdf0e10cSrcweir 
348*cdf0e10cSrcweir // -----------------------------------------------------------------------
349*cdf0e10cSrcweir 
350*cdf0e10cSrcweir void FormatterBase::SetStrictFormat( sal_Bool bStrict )
351*cdf0e10cSrcweir {
352*cdf0e10cSrcweir     if ( bStrict != mbStrictFormat )
353*cdf0e10cSrcweir     {
354*cdf0e10cSrcweir         mbStrictFormat = bStrict;
355*cdf0e10cSrcweir         if ( mbStrictFormat )
356*cdf0e10cSrcweir             ReformatAll();
357*cdf0e10cSrcweir     }
358*cdf0e10cSrcweir }
359*cdf0e10cSrcweir 
360*cdf0e10cSrcweir // -----------------------------------------------------------------------
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir void FormatterBase::SetLocale( const lang::Locale& rLocale )
363*cdf0e10cSrcweir {
364*cdf0e10cSrcweir     ImplGetLocaleDataWrapper().setLocale( rLocale );
365*cdf0e10cSrcweir     mbDefaultLocale = sal_False;
366*cdf0e10cSrcweir     ReformatAll();
367*cdf0e10cSrcweir }
368*cdf0e10cSrcweir 
369*cdf0e10cSrcweir // -----------------------------------------------------------------------
370*cdf0e10cSrcweir 
371*cdf0e10cSrcweir const lang::Locale& FormatterBase::GetLocale() const
372*cdf0e10cSrcweir {
373*cdf0e10cSrcweir     if ( !mpLocaleDataWrapper || mbDefaultLocale )
374*cdf0e10cSrcweir     {
375*cdf0e10cSrcweir         if ( mpField )
376*cdf0e10cSrcweir             return mpField->GetSettings().GetLocale();
377*cdf0e10cSrcweir         else
378*cdf0e10cSrcweir             return Application::GetSettings().GetLocale();
379*cdf0e10cSrcweir     }
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir     return mpLocaleDataWrapper->getLocale();
382*cdf0e10cSrcweir }
383*cdf0e10cSrcweir 
384*cdf0e10cSrcweir // -----------------------------------------------------------------------
385*cdf0e10cSrcweir 
386*cdf0e10cSrcweir const AllSettings& FormatterBase::GetFieldSettings() const
387*cdf0e10cSrcweir {
388*cdf0e10cSrcweir     if ( mpField )
389*cdf0e10cSrcweir         return mpField->GetSettings();
390*cdf0e10cSrcweir     else
391*cdf0e10cSrcweir         return Application::GetSettings();
392*cdf0e10cSrcweir }
393*cdf0e10cSrcweir 
394*cdf0e10cSrcweir // -----------------------------------------------------------------------
395*cdf0e10cSrcweir 
396*cdf0e10cSrcweir void FormatterBase::SetFieldText( const XubString& rText, sal_Bool bKeepSelection )
397*cdf0e10cSrcweir {
398*cdf0e10cSrcweir     if ( mpField )
399*cdf0e10cSrcweir     {
400*cdf0e10cSrcweir         Selection aNewSelection( 0xFFFF, 0xFFFF );
401*cdf0e10cSrcweir         if ( bKeepSelection )
402*cdf0e10cSrcweir             aNewSelection = mpField->GetSelection();
403*cdf0e10cSrcweir 
404*cdf0e10cSrcweir         ImplSetText( rText, &aNewSelection );
405*cdf0e10cSrcweir     }
406*cdf0e10cSrcweir }
407*cdf0e10cSrcweir 
408*cdf0e10cSrcweir // -----------------------------------------------------------------------
409*cdf0e10cSrcweir 
410*cdf0e10cSrcweir void FormatterBase::ImplSetText( const XubString& rText, Selection* pNewSelection )
411*cdf0e10cSrcweir {
412*cdf0e10cSrcweir     if ( mpField )
413*cdf0e10cSrcweir     {
414*cdf0e10cSrcweir         if ( pNewSelection )
415*cdf0e10cSrcweir             mpField->SetText( rText, *pNewSelection );
416*cdf0e10cSrcweir         else
417*cdf0e10cSrcweir         {
418*cdf0e10cSrcweir             Selection aSel = mpField->GetSelection();
419*cdf0e10cSrcweir             aSel.Min() = aSel.Max();
420*cdf0e10cSrcweir             mpField->SetText( rText, aSel );
421*cdf0e10cSrcweir         }
422*cdf0e10cSrcweir 
423*cdf0e10cSrcweir         MarkToBeReformatted( sal_False );
424*cdf0e10cSrcweir     }
425*cdf0e10cSrcweir }
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir // -----------------------------------------------------------------------
428*cdf0e10cSrcweir 
429*cdf0e10cSrcweir void FormatterBase::SetEmptyFieldValue()
430*cdf0e10cSrcweir {
431*cdf0e10cSrcweir     if ( mpField )
432*cdf0e10cSrcweir         mpField->SetText( ImplGetSVEmptyStr() );
433*cdf0e10cSrcweir     mbEmptyFieldValue = sal_True;
434*cdf0e10cSrcweir }
435*cdf0e10cSrcweir 
436*cdf0e10cSrcweir // -----------------------------------------------------------------------
437*cdf0e10cSrcweir 
438*cdf0e10cSrcweir sal_Bool FormatterBase::IsEmptyFieldValue() const
439*cdf0e10cSrcweir {
440*cdf0e10cSrcweir     return (!mpField || !mpField->GetText().Len());
441*cdf0e10cSrcweir }
442*cdf0e10cSrcweir 
443*cdf0e10cSrcweir // -----------------------------------------------------------------------
444*cdf0e10cSrcweir 
445*cdf0e10cSrcweir sal_Bool NumericFormatter::ImplNumericReformat( const XubString& rStr, double& rValue,
446*cdf0e10cSrcweir                                             XubString& rOutStr )
447*cdf0e10cSrcweir {
448*cdf0e10cSrcweir     if ( !ImplNumericGetValue( rStr, rValue, GetDecimalDigits(), ImplGetLocaleDataWrapper() ) )
449*cdf0e10cSrcweir         return sal_True;
450*cdf0e10cSrcweir     else
451*cdf0e10cSrcweir     {
452*cdf0e10cSrcweir         double nTempVal = rValue;
453*cdf0e10cSrcweir         // caution: precision loss in double cast
454*cdf0e10cSrcweir         if ( nTempVal > mnMax )
455*cdf0e10cSrcweir             nTempVal = (double)mnMax;
456*cdf0e10cSrcweir         else if ( nTempVal < mnMin )
457*cdf0e10cSrcweir             nTempVal = (double)mnMin;
458*cdf0e10cSrcweir 
459*cdf0e10cSrcweir         if ( GetErrorHdl().IsSet() && (rValue != nTempVal) )
460*cdf0e10cSrcweir         {
461*cdf0e10cSrcweir             mnCorrectedValue = (sal_Int64)nTempVal;
462*cdf0e10cSrcweir             if ( !GetErrorHdl().Call( this ) )
463*cdf0e10cSrcweir             {
464*cdf0e10cSrcweir                 mnCorrectedValue = 0;
465*cdf0e10cSrcweir                 return sal_False;
466*cdf0e10cSrcweir             }
467*cdf0e10cSrcweir             else
468*cdf0e10cSrcweir                 mnCorrectedValue = 0;
469*cdf0e10cSrcweir         }
470*cdf0e10cSrcweir 
471*cdf0e10cSrcweir         rOutStr = CreateFieldText( (sal_Int64)nTempVal );
472*cdf0e10cSrcweir         return sal_True;
473*cdf0e10cSrcweir     }
474*cdf0e10cSrcweir }
475*cdf0e10cSrcweir 
476*cdf0e10cSrcweir // -----------------------------------------------------------------------
477*cdf0e10cSrcweir 
478*cdf0e10cSrcweir void NumericFormatter::ImplInit()
479*cdf0e10cSrcweir {
480*cdf0e10cSrcweir     mnFieldValue        = 0;
481*cdf0e10cSrcweir     mnLastValue         = 0;
482*cdf0e10cSrcweir     mnMin               = 0;
483*cdf0e10cSrcweir     mnMax               = 0x7FFFFFFFFFFFFFFFLL;
484*cdf0e10cSrcweir     mnCorrectedValue    = 0;
485*cdf0e10cSrcweir     mnDecimalDigits     = 2;
486*cdf0e10cSrcweir     mnType              = FORMAT_NUMERIC;
487*cdf0e10cSrcweir     mbThousandSep       = sal_True;
488*cdf0e10cSrcweir     mbShowTrailingZeros = sal_True;
489*cdf0e10cSrcweir 
490*cdf0e10cSrcweir     // for fields
491*cdf0e10cSrcweir     mnSpinSize          = 1;
492*cdf0e10cSrcweir     mnFirst             = mnMin;
493*cdf0e10cSrcweir     mnLast              = mnMax;
494*cdf0e10cSrcweir 
495*cdf0e10cSrcweir     SetDecimalDigits( 0 );
496*cdf0e10cSrcweir }
497*cdf0e10cSrcweir 
498*cdf0e10cSrcweir // -----------------------------------------------------------------------
499*cdf0e10cSrcweir 
500*cdf0e10cSrcweir NumericFormatter::NumericFormatter()
501*cdf0e10cSrcweir {
502*cdf0e10cSrcweir     ImplInit();
503*cdf0e10cSrcweir }
504*cdf0e10cSrcweir 
505*cdf0e10cSrcweir // -----------------------------------------------------------------------
506*cdf0e10cSrcweir 
507*cdf0e10cSrcweir void NumericFormatter::ImplLoadRes( const ResId& rResId )
508*cdf0e10cSrcweir {
509*cdf0e10cSrcweir     ResMgr*     pMgr = rResId.GetResMgr();
510*cdf0e10cSrcweir 
511*cdf0e10cSrcweir     if( pMgr )
512*cdf0e10cSrcweir     {
513*cdf0e10cSrcweir         sal_uLong nMask = pMgr->ReadLong();
514*cdf0e10cSrcweir 
515*cdf0e10cSrcweir         if ( NUMERICFORMATTER_MIN & nMask )
516*cdf0e10cSrcweir             mnMin = pMgr->ReadLong();
517*cdf0e10cSrcweir 
518*cdf0e10cSrcweir         if ( NUMERICFORMATTER_MAX & nMask )
519*cdf0e10cSrcweir             mnMax = pMgr->ReadLong();
520*cdf0e10cSrcweir 
521*cdf0e10cSrcweir         if ( NUMERICFORMATTER_STRICTFORMAT & nMask )
522*cdf0e10cSrcweir             SetStrictFormat( (sal_Bool)pMgr->ReadShort() );
523*cdf0e10cSrcweir 
524*cdf0e10cSrcweir         if ( NUMERICFORMATTER_DECIMALDIGITS & nMask )
525*cdf0e10cSrcweir             SetDecimalDigits( pMgr->ReadShort() );
526*cdf0e10cSrcweir 
527*cdf0e10cSrcweir         if ( NUMERICFORMATTER_VALUE & nMask )
528*cdf0e10cSrcweir         {
529*cdf0e10cSrcweir             mnFieldValue = pMgr->ReadLong();
530*cdf0e10cSrcweir             if ( mnFieldValue > mnMax )
531*cdf0e10cSrcweir                 mnFieldValue = mnMax;
532*cdf0e10cSrcweir             else if ( mnFieldValue < mnMin )
533*cdf0e10cSrcweir                 mnFieldValue = mnMin;
534*cdf0e10cSrcweir             mnLastValue = mnFieldValue;
535*cdf0e10cSrcweir         }
536*cdf0e10cSrcweir 
537*cdf0e10cSrcweir         if ( NUMERICFORMATTER_NOTHOUSANDSEP & nMask )
538*cdf0e10cSrcweir             SetUseThousandSep( !(sal_Bool)pMgr->ReadShort() );
539*cdf0e10cSrcweir     }
540*cdf0e10cSrcweir }
541*cdf0e10cSrcweir 
542*cdf0e10cSrcweir // -----------------------------------------------------------------------
543*cdf0e10cSrcweir 
544*cdf0e10cSrcweir NumericFormatter::~NumericFormatter()
545*cdf0e10cSrcweir {
546*cdf0e10cSrcweir }
547*cdf0e10cSrcweir 
548*cdf0e10cSrcweir // -----------------------------------------------------------------------
549*cdf0e10cSrcweir 
550*cdf0e10cSrcweir void NumericFormatter::SetMin( sal_Int64 nNewMin )
551*cdf0e10cSrcweir {
552*cdf0e10cSrcweir     mnMin = nNewMin;
553*cdf0e10cSrcweir     if ( !IsEmptyFieldValue() )
554*cdf0e10cSrcweir         ReformatAll();
555*cdf0e10cSrcweir }
556*cdf0e10cSrcweir 
557*cdf0e10cSrcweir // -----------------------------------------------------------------------
558*cdf0e10cSrcweir 
559*cdf0e10cSrcweir void NumericFormatter::SetMax( sal_Int64 nNewMax )
560*cdf0e10cSrcweir {
561*cdf0e10cSrcweir     mnMax = nNewMax;
562*cdf0e10cSrcweir     if ( !IsEmptyFieldValue() )
563*cdf0e10cSrcweir         ReformatAll();
564*cdf0e10cSrcweir }
565*cdf0e10cSrcweir 
566*cdf0e10cSrcweir // -----------------------------------------------------------------------
567*cdf0e10cSrcweir 
568*cdf0e10cSrcweir void NumericFormatter::SetUseThousandSep( sal_Bool b )
569*cdf0e10cSrcweir {
570*cdf0e10cSrcweir     mbThousandSep = b;
571*cdf0e10cSrcweir     ReformatAll();
572*cdf0e10cSrcweir }
573*cdf0e10cSrcweir 
574*cdf0e10cSrcweir // -----------------------------------------------------------------------
575*cdf0e10cSrcweir 
576*cdf0e10cSrcweir void NumericFormatter::SetDecimalDigits( sal_uInt16 nDigits )
577*cdf0e10cSrcweir {
578*cdf0e10cSrcweir     mnDecimalDigits = nDigits;
579*cdf0e10cSrcweir     ReformatAll();
580*cdf0e10cSrcweir }
581*cdf0e10cSrcweir 
582*cdf0e10cSrcweir // -----------------------------------------------------------------------
583*cdf0e10cSrcweir 
584*cdf0e10cSrcweir void NumericFormatter::SetShowTrailingZeros( sal_Bool bShowTrailingZeros )
585*cdf0e10cSrcweir {
586*cdf0e10cSrcweir     if ( mbShowTrailingZeros != bShowTrailingZeros )
587*cdf0e10cSrcweir     {
588*cdf0e10cSrcweir         mbShowTrailingZeros = bShowTrailingZeros;
589*cdf0e10cSrcweir         ReformatAll();
590*cdf0e10cSrcweir     }
591*cdf0e10cSrcweir }
592*cdf0e10cSrcweir 
593*cdf0e10cSrcweir // -----------------------------------------------------------------------
594*cdf0e10cSrcweir 
595*cdf0e10cSrcweir sal_uInt16 NumericFormatter::GetDecimalDigits() const
596*cdf0e10cSrcweir {
597*cdf0e10cSrcweir     return mnDecimalDigits;
598*cdf0e10cSrcweir }
599*cdf0e10cSrcweir 
600*cdf0e10cSrcweir // -----------------------------------------------------------------------
601*cdf0e10cSrcweir 
602*cdf0e10cSrcweir void NumericFormatter::SetValue( sal_Int64 nNewValue )
603*cdf0e10cSrcweir {
604*cdf0e10cSrcweir     SetUserValue( nNewValue );
605*cdf0e10cSrcweir     mnFieldValue = mnLastValue;
606*cdf0e10cSrcweir     SetEmptyFieldValueData( sal_False );
607*cdf0e10cSrcweir }
608*cdf0e10cSrcweir 
609*cdf0e10cSrcweir // -----------------------------------------------------------------------
610*cdf0e10cSrcweir 
611*cdf0e10cSrcweir XubString NumericFormatter::CreateFieldText( sal_Int64 nValue ) const
612*cdf0e10cSrcweir {
613*cdf0e10cSrcweir     return ImplGetLocaleDataWrapper().getNum( nValue, GetDecimalDigits(), IsUseThousandSep(), IsShowTrailingZeros() );
614*cdf0e10cSrcweir }
615*cdf0e10cSrcweir 
616*cdf0e10cSrcweir // -----------------------------------------------------------------------
617*cdf0e10cSrcweir 
618*cdf0e10cSrcweir void NumericFormatter::ImplSetUserValue( sal_Int64 nNewValue, Selection* pNewSelection )
619*cdf0e10cSrcweir {
620*cdf0e10cSrcweir     if ( nNewValue > mnMax )
621*cdf0e10cSrcweir         nNewValue = mnMax;
622*cdf0e10cSrcweir     else if ( nNewValue < mnMin )
623*cdf0e10cSrcweir         nNewValue = mnMin;
624*cdf0e10cSrcweir     mnLastValue = nNewValue;
625*cdf0e10cSrcweir 
626*cdf0e10cSrcweir     if ( GetField() )
627*cdf0e10cSrcweir         ImplSetText( CreateFieldText( nNewValue ), pNewSelection );
628*cdf0e10cSrcweir }
629*cdf0e10cSrcweir 
630*cdf0e10cSrcweir // -----------------------------------------------------------------------
631*cdf0e10cSrcweir 
632*cdf0e10cSrcweir void NumericFormatter::SetUserValue( sal_Int64 nNewValue )
633*cdf0e10cSrcweir {
634*cdf0e10cSrcweir     ImplSetUserValue( nNewValue );
635*cdf0e10cSrcweir }
636*cdf0e10cSrcweir 
637*cdf0e10cSrcweir // -----------------------------------------------------------------------
638*cdf0e10cSrcweir 
639*cdf0e10cSrcweir sal_Int64 NumericFormatter::GetValue() const
640*cdf0e10cSrcweir {
641*cdf0e10cSrcweir     if ( !GetField() )
642*cdf0e10cSrcweir         return 0;
643*cdf0e10cSrcweir 
644*cdf0e10cSrcweir     double nTempValue;
645*cdf0e10cSrcweir 
646*cdf0e10cSrcweir     if ( ImplNumericGetValue( GetField()->GetText(), nTempValue,
647*cdf0e10cSrcweir                               GetDecimalDigits(), ImplGetLocaleDataWrapper() ) )
648*cdf0e10cSrcweir     {
649*cdf0e10cSrcweir         // caution: precision loss in double cast
650*cdf0e10cSrcweir         if ( nTempValue > mnMax )
651*cdf0e10cSrcweir             nTempValue = (double)mnMax;
652*cdf0e10cSrcweir         else if ( nTempValue < mnMin )
653*cdf0e10cSrcweir             nTempValue = (double)mnMin;
654*cdf0e10cSrcweir         return (sal_Int64)nTempValue;
655*cdf0e10cSrcweir     }
656*cdf0e10cSrcweir     else
657*cdf0e10cSrcweir         return mnLastValue;
658*cdf0e10cSrcweir }
659*cdf0e10cSrcweir 
660*cdf0e10cSrcweir // -----------------------------------------------------------------------
661*cdf0e10cSrcweir 
662*cdf0e10cSrcweir sal_Bool NumericFormatter::IsValueModified() const
663*cdf0e10cSrcweir {
664*cdf0e10cSrcweir     if ( ImplGetEmptyFieldValue() )
665*cdf0e10cSrcweir         return !IsEmptyFieldValue();
666*cdf0e10cSrcweir     else if ( GetValue() != mnFieldValue )
667*cdf0e10cSrcweir         return sal_True;
668*cdf0e10cSrcweir     else
669*cdf0e10cSrcweir         return sal_False;
670*cdf0e10cSrcweir }
671*cdf0e10cSrcweir 
672*cdf0e10cSrcweir // -----------------------------------------------------------------------
673*cdf0e10cSrcweir 
674*cdf0e10cSrcweir Fraction NumericFormatter::ConvertToFraction( sal_Int64 nValue )
675*cdf0e10cSrcweir {
676*cdf0e10cSrcweir     // caution: precision loss in double cast (and in fraction anyhow)
677*cdf0e10cSrcweir     return Fraction( (double)nValue/(double)ImplPower10( GetDecimalDigits() ) );
678*cdf0e10cSrcweir }
679*cdf0e10cSrcweir 
680*cdf0e10cSrcweir // -----------------------------------------------------------------------
681*cdf0e10cSrcweir 
682*cdf0e10cSrcweir sal_Int64 NumericFormatter::ConvertToLong( const Fraction& rValue )
683*cdf0e10cSrcweir {
684*cdf0e10cSrcweir     Fraction aFract = rValue;
685*cdf0e10cSrcweir     aFract *= Fraction( (long)ImplPower10( GetDecimalDigits() ), 1 );
686*cdf0e10cSrcweir     return (sal_Int64)(double)aFract;
687*cdf0e10cSrcweir }
688*cdf0e10cSrcweir 
689*cdf0e10cSrcweir // -----------------------------------------------------------------------
690*cdf0e10cSrcweir 
691*cdf0e10cSrcweir sal_Int64 NumericFormatter::Normalize( sal_Int64 nValue ) const
692*cdf0e10cSrcweir {
693*cdf0e10cSrcweir     return (nValue * ImplPower10( GetDecimalDigits() ) );
694*cdf0e10cSrcweir }
695*cdf0e10cSrcweir 
696*cdf0e10cSrcweir // -----------------------------------------------------------------------
697*cdf0e10cSrcweir 
698*cdf0e10cSrcweir sal_Int64 NumericFormatter::Denormalize( sal_Int64 nValue ) const
699*cdf0e10cSrcweir {
700*cdf0e10cSrcweir     sal_Int64 nFactor = ImplPower10( GetDecimalDigits() );
701*cdf0e10cSrcweir     if( nValue < 0 )
702*cdf0e10cSrcweir         return ((nValue-(nFactor/2)) / nFactor );
703*cdf0e10cSrcweir     else
704*cdf0e10cSrcweir         return ((nValue+(nFactor/2)) / nFactor );
705*cdf0e10cSrcweir }
706*cdf0e10cSrcweir 
707*cdf0e10cSrcweir // -----------------------------------------------------------------------
708*cdf0e10cSrcweir 
709*cdf0e10cSrcweir void NumericFormatter::Reformat()
710*cdf0e10cSrcweir {
711*cdf0e10cSrcweir     if ( !GetField() )
712*cdf0e10cSrcweir         return;
713*cdf0e10cSrcweir 
714*cdf0e10cSrcweir     if ( !GetField()->GetText().Len() && ImplGetEmptyFieldValue() )
715*cdf0e10cSrcweir         return;
716*cdf0e10cSrcweir 
717*cdf0e10cSrcweir     XubString aStr;
718*cdf0e10cSrcweir     // caution: precision loss in double cast
719*cdf0e10cSrcweir     double nTemp = (double)mnLastValue;
720*cdf0e10cSrcweir     sal_Bool bOK = ImplNumericReformat( GetField()->GetText(), nTemp, aStr );
721*cdf0e10cSrcweir     mnLastValue = (sal_Int64)nTemp;
722*cdf0e10cSrcweir     if ( !bOK )
723*cdf0e10cSrcweir         return;
724*cdf0e10cSrcweir 
725*cdf0e10cSrcweir     if ( aStr.Len() )
726*cdf0e10cSrcweir         ImplSetText( aStr );
727*cdf0e10cSrcweir     else
728*cdf0e10cSrcweir         SetValue( mnLastValue );
729*cdf0e10cSrcweir }
730*cdf0e10cSrcweir 
731*cdf0e10cSrcweir // -----------------------------------------------------------------------
732*cdf0e10cSrcweir 
733*cdf0e10cSrcweir void NumericFormatter::FieldUp()
734*cdf0e10cSrcweir {
735*cdf0e10cSrcweir     sal_Int64 nValue = GetValue();
736*cdf0e10cSrcweir     nValue += mnSpinSize;
737*cdf0e10cSrcweir     if ( nValue > mnMax )
738*cdf0e10cSrcweir         nValue = mnMax;
739*cdf0e10cSrcweir 
740*cdf0e10cSrcweir     ImplNewFieldValue( nValue );
741*cdf0e10cSrcweir }
742*cdf0e10cSrcweir 
743*cdf0e10cSrcweir // -----------------------------------------------------------------------
744*cdf0e10cSrcweir 
745*cdf0e10cSrcweir void NumericFormatter::FieldDown()
746*cdf0e10cSrcweir {
747*cdf0e10cSrcweir     sal_Int64 nValue = GetValue();
748*cdf0e10cSrcweir     nValue -= mnSpinSize;
749*cdf0e10cSrcweir     if ( nValue < mnMin )
750*cdf0e10cSrcweir         nValue = mnMin;
751*cdf0e10cSrcweir 
752*cdf0e10cSrcweir     ImplNewFieldValue( nValue );
753*cdf0e10cSrcweir }
754*cdf0e10cSrcweir 
755*cdf0e10cSrcweir // -----------------------------------------------------------------------
756*cdf0e10cSrcweir 
757*cdf0e10cSrcweir void NumericFormatter::FieldFirst()
758*cdf0e10cSrcweir {
759*cdf0e10cSrcweir     ImplNewFieldValue( mnFirst );
760*cdf0e10cSrcweir }
761*cdf0e10cSrcweir 
762*cdf0e10cSrcweir // -----------------------------------------------------------------------
763*cdf0e10cSrcweir 
764*cdf0e10cSrcweir void NumericFormatter::FieldLast()
765*cdf0e10cSrcweir {
766*cdf0e10cSrcweir     ImplNewFieldValue( mnLast );
767*cdf0e10cSrcweir }
768*cdf0e10cSrcweir 
769*cdf0e10cSrcweir // -----------------------------------------------------------------------
770*cdf0e10cSrcweir 
771*cdf0e10cSrcweir void NumericFormatter::ImplNewFieldValue( sal_Int64 nNewValue )
772*cdf0e10cSrcweir {
773*cdf0e10cSrcweir     if ( GetField() )
774*cdf0e10cSrcweir     {
775*cdf0e10cSrcweir         // !!! TH-18.2.99: Wenn wir Zeit haben sollte mal geklaert werden,
776*cdf0e10cSrcweir         // !!! warum nicht bei ImplSetUserValue() geprueft wird, ob
777*cdf0e10cSrcweir         // !!! sich der Wert aendert. Denn auch hier muesste dieses
778*cdf0e10cSrcweir         // !!! gemacht werden, da ansonsten der Modify-Aufruf
779*cdf0e10cSrcweir         // !!! nicht gemacht werden duerfte. Jedenfalls sollten die
780*cdf0e10cSrcweir         // !!! Wege von ImplNewFieldValue, ImplSetUserValue und
781*cdf0e10cSrcweir         // !!! ImplSetText ueberprueft und klarer gestalltet (mit Kommentar)
782*cdf0e10cSrcweir         // !!! werden, damit wir mal wissen, was dort ablaeuft!!!
783*cdf0e10cSrcweir 
784*cdf0e10cSrcweir         Selection aSelection = GetField()->GetSelection();
785*cdf0e10cSrcweir         aSelection.Justify();
786*cdf0e10cSrcweir         XubString aText = GetField()->GetText();
787*cdf0e10cSrcweir         // Wenn bis ans Ende selektiert war, soll das auch so bleiben...
788*cdf0e10cSrcweir         if ( (xub_StrLen)aSelection.Max() == aText.Len() )
789*cdf0e10cSrcweir         {
790*cdf0e10cSrcweir             if ( !aSelection.Len() )
791*cdf0e10cSrcweir                 aSelection.Min() = SELECTION_MAX;
792*cdf0e10cSrcweir             aSelection.Max() = SELECTION_MAX;
793*cdf0e10cSrcweir         }
794*cdf0e10cSrcweir 
795*cdf0e10cSrcweir         sal_Int64 nOldLastValue  = mnLastValue;
796*cdf0e10cSrcweir         ImplSetUserValue( nNewValue, &aSelection );
797*cdf0e10cSrcweir         mnLastValue = nOldLastValue;
798*cdf0e10cSrcweir 
799*cdf0e10cSrcweir         // Modify am Edit wird nur bei KeyInput gesetzt...
800*cdf0e10cSrcweir         if ( GetField()->GetText() != aText )
801*cdf0e10cSrcweir         {
802*cdf0e10cSrcweir             GetField()->SetModifyFlag();
803*cdf0e10cSrcweir             GetField()->Modify();
804*cdf0e10cSrcweir         }
805*cdf0e10cSrcweir     }
806*cdf0e10cSrcweir }
807*cdf0e10cSrcweir 
808*cdf0e10cSrcweir // -----------------------------------------------------------------------
809*cdf0e10cSrcweir 
810*cdf0e10cSrcweir NumericField::NumericField( Window* pParent, WinBits nWinStyle ) :
811*cdf0e10cSrcweir     SpinField( pParent, nWinStyle )
812*cdf0e10cSrcweir {
813*cdf0e10cSrcweir     SetField( this );
814*cdf0e10cSrcweir     Reformat();
815*cdf0e10cSrcweir }
816*cdf0e10cSrcweir 
817*cdf0e10cSrcweir // -----------------------------------------------------------------------
818*cdf0e10cSrcweir 
819*cdf0e10cSrcweir NumericField::NumericField( Window* pParent, const ResId& rResId ) :
820*cdf0e10cSrcweir     SpinField( WINDOW_NUMERICFIELD )
821*cdf0e10cSrcweir {
822*cdf0e10cSrcweir     rResId.SetRT( RSC_NUMERICFIELD );
823*cdf0e10cSrcweir     WinBits nStyle = ImplInitRes( rResId ) ;
824*cdf0e10cSrcweir     SpinField::ImplInit( pParent, nStyle );
825*cdf0e10cSrcweir     SetField( this );
826*cdf0e10cSrcweir     ImplLoadRes( rResId );
827*cdf0e10cSrcweir     Reformat();
828*cdf0e10cSrcweir 
829*cdf0e10cSrcweir     if ( !(nStyle & WB_HIDE ) )
830*cdf0e10cSrcweir         Show();
831*cdf0e10cSrcweir }
832*cdf0e10cSrcweir 
833*cdf0e10cSrcweir // -----------------------------------------------------------------------
834*cdf0e10cSrcweir 
835*cdf0e10cSrcweir void NumericField::ImplLoadRes( const ResId& rResId )
836*cdf0e10cSrcweir {
837*cdf0e10cSrcweir     SpinField::ImplLoadRes( rResId );
838*cdf0e10cSrcweir     NumericFormatter::ImplLoadRes( ResId( (RSHEADER_TYPE *)GetClassRes(), *rResId.GetResMgr() ) );
839*cdf0e10cSrcweir 
840*cdf0e10cSrcweir     sal_uLong      nMask = ReadLongRes();
841*cdf0e10cSrcweir 
842*cdf0e10cSrcweir     if ( NUMERICFIELD_FIRST & nMask )
843*cdf0e10cSrcweir         mnFirst = ReadLongRes();
844*cdf0e10cSrcweir 
845*cdf0e10cSrcweir     if ( NUMERICFIELD_LAST & nMask )
846*cdf0e10cSrcweir         mnLast = ReadLongRes();
847*cdf0e10cSrcweir 
848*cdf0e10cSrcweir     if ( NUMERICFIELD_SPINSIZE & nMask )
849*cdf0e10cSrcweir         mnSpinSize = ReadLongRes();
850*cdf0e10cSrcweir }
851*cdf0e10cSrcweir 
852*cdf0e10cSrcweir // -----------------------------------------------------------------------
853*cdf0e10cSrcweir 
854*cdf0e10cSrcweir NumericField::~NumericField()
855*cdf0e10cSrcweir {
856*cdf0e10cSrcweir }
857*cdf0e10cSrcweir 
858*cdf0e10cSrcweir // -----------------------------------------------------------------------
859*cdf0e10cSrcweir 
860*cdf0e10cSrcweir long NumericField::PreNotify( NotifyEvent& rNEvt )
861*cdf0e10cSrcweir {
862*cdf0e10cSrcweir         if ( (rNEvt.GetType() == EVENT_KEYINPUT) && !rNEvt.GetKeyEvent()->GetKeyCode().IsMod2() )
863*cdf0e10cSrcweir     {
864*cdf0e10cSrcweir         if ( ImplNumericProcessKeyInput( GetField(), *rNEvt.GetKeyEvent(), IsStrictFormat(), IsUseThousandSep(), ImplGetLocaleDataWrapper() ) )
865*cdf0e10cSrcweir             return 1;
866*cdf0e10cSrcweir     }
867*cdf0e10cSrcweir 
868*cdf0e10cSrcweir     return SpinField::PreNotify( rNEvt );
869*cdf0e10cSrcweir }
870*cdf0e10cSrcweir 
871*cdf0e10cSrcweir // -----------------------------------------------------------------------
872*cdf0e10cSrcweir 
873*cdf0e10cSrcweir long NumericField::Notify( NotifyEvent& rNEvt )
874*cdf0e10cSrcweir {
875*cdf0e10cSrcweir     if ( rNEvt.GetType() == EVENT_GETFOCUS )
876*cdf0e10cSrcweir         MarkToBeReformatted( sal_False );
877*cdf0e10cSrcweir     else if ( rNEvt.GetType() == EVENT_LOSEFOCUS )
878*cdf0e10cSrcweir     {
879*cdf0e10cSrcweir         if ( MustBeReformatted() && (GetText().Len() || !IsEmptyFieldValueEnabled()) )
880*cdf0e10cSrcweir             Reformat();
881*cdf0e10cSrcweir     }
882*cdf0e10cSrcweir 
883*cdf0e10cSrcweir     return SpinField::Notify( rNEvt );
884*cdf0e10cSrcweir }
885*cdf0e10cSrcweir 
886*cdf0e10cSrcweir // -----------------------------------------------------------------------
887*cdf0e10cSrcweir 
888*cdf0e10cSrcweir void NumericField::DataChanged( const DataChangedEvent& rDCEvt )
889*cdf0e10cSrcweir {
890*cdf0e10cSrcweir     SpinField::DataChanged( rDCEvt );
891*cdf0e10cSrcweir 
892*cdf0e10cSrcweir     if ( (rDCEvt.GetType() == DATACHANGED_SETTINGS) && (rDCEvt.GetFlags() & SETTINGS_LOCALE) )
893*cdf0e10cSrcweir     {
894*cdf0e10cSrcweir         String sOldDecSep = ImplGetLocaleDataWrapper().getNumDecimalSep();
895*cdf0e10cSrcweir         String sOldThSep = ImplGetLocaleDataWrapper().getNumThousandSep();
896*cdf0e10cSrcweir         if ( IsDefaultLocale() )
897*cdf0e10cSrcweir             ImplGetLocaleDataWrapper().setLocale( GetSettings().GetLocale() );
898*cdf0e10cSrcweir         String sNewDecSep = ImplGetLocaleDataWrapper().getNumDecimalSep();
899*cdf0e10cSrcweir         String sNewThSep = ImplGetLocaleDataWrapper().getNumThousandSep();
900*cdf0e10cSrcweir         ImplUpdateSeparators( sOldDecSep, sNewDecSep, sOldThSep, sNewThSep, this );
901*cdf0e10cSrcweir         ReformatAll();
902*cdf0e10cSrcweir     }
903*cdf0e10cSrcweir }
904*cdf0e10cSrcweir 
905*cdf0e10cSrcweir // -----------------------------------------------------------------------
906*cdf0e10cSrcweir 
907*cdf0e10cSrcweir void NumericField::Modify()
908*cdf0e10cSrcweir {
909*cdf0e10cSrcweir     MarkToBeReformatted( sal_True );
910*cdf0e10cSrcweir     SpinField::Modify();
911*cdf0e10cSrcweir }
912*cdf0e10cSrcweir 
913*cdf0e10cSrcweir // -----------------------------------------------------------------------
914*cdf0e10cSrcweir 
915*cdf0e10cSrcweir void NumericField::Up()
916*cdf0e10cSrcweir {
917*cdf0e10cSrcweir     FieldUp();
918*cdf0e10cSrcweir     SpinField::Up();
919*cdf0e10cSrcweir }
920*cdf0e10cSrcweir 
921*cdf0e10cSrcweir // -----------------------------------------------------------------------
922*cdf0e10cSrcweir 
923*cdf0e10cSrcweir void NumericField::Down()
924*cdf0e10cSrcweir {
925*cdf0e10cSrcweir     FieldDown();
926*cdf0e10cSrcweir     SpinField::Down();
927*cdf0e10cSrcweir }
928*cdf0e10cSrcweir 
929*cdf0e10cSrcweir // -----------------------------------------------------------------------
930*cdf0e10cSrcweir 
931*cdf0e10cSrcweir void NumericField::First()
932*cdf0e10cSrcweir {
933*cdf0e10cSrcweir     FieldFirst();
934*cdf0e10cSrcweir     SpinField::First();
935*cdf0e10cSrcweir }
936*cdf0e10cSrcweir 
937*cdf0e10cSrcweir // -----------------------------------------------------------------------
938*cdf0e10cSrcweir 
939*cdf0e10cSrcweir void NumericField::Last()
940*cdf0e10cSrcweir {
941*cdf0e10cSrcweir     FieldLast();
942*cdf0e10cSrcweir     SpinField::Last();
943*cdf0e10cSrcweir }
944*cdf0e10cSrcweir 
945*cdf0e10cSrcweir // -----------------------------------------------------------------------
946*cdf0e10cSrcweir 
947*cdf0e10cSrcweir NumericBox::NumericBox( Window* pParent, WinBits nWinStyle ) :
948*cdf0e10cSrcweir     ComboBox( pParent, nWinStyle )
949*cdf0e10cSrcweir {
950*cdf0e10cSrcweir     SetField( this );
951*cdf0e10cSrcweir     Reformat();
952*cdf0e10cSrcweir }
953*cdf0e10cSrcweir 
954*cdf0e10cSrcweir // -----------------------------------------------------------------------
955*cdf0e10cSrcweir 
956*cdf0e10cSrcweir NumericBox::NumericBox( Window* pParent, const ResId& rResId ) :
957*cdf0e10cSrcweir     ComboBox( WINDOW_NUMERICBOX )
958*cdf0e10cSrcweir {
959*cdf0e10cSrcweir     rResId.SetRT( RSC_NUMERICBOX );
960*cdf0e10cSrcweir     WinBits nStyle = ImplInitRes( rResId );
961*cdf0e10cSrcweir     ComboBox::ImplInit( pParent, nStyle );
962*cdf0e10cSrcweir     SetField( this );
963*cdf0e10cSrcweir     ComboBox::ImplLoadRes( rResId );
964*cdf0e10cSrcweir     NumericFormatter::ImplLoadRes( ResId( (RSHEADER_TYPE *)GetClassRes(), *rResId.GetResMgr() ) );
965*cdf0e10cSrcweir     Reformat();
966*cdf0e10cSrcweir 
967*cdf0e10cSrcweir     if ( !(nStyle & WB_HIDE ) )
968*cdf0e10cSrcweir         Show();
969*cdf0e10cSrcweir }
970*cdf0e10cSrcweir 
971*cdf0e10cSrcweir // -----------------------------------------------------------------------
972*cdf0e10cSrcweir 
973*cdf0e10cSrcweir NumericBox::~NumericBox()
974*cdf0e10cSrcweir {
975*cdf0e10cSrcweir }
976*cdf0e10cSrcweir 
977*cdf0e10cSrcweir // -----------------------------------------------------------------------
978*cdf0e10cSrcweir 
979*cdf0e10cSrcweir long NumericBox::PreNotify( NotifyEvent& rNEvt )
980*cdf0e10cSrcweir {
981*cdf0e10cSrcweir     if ( (rNEvt.GetType() == EVENT_KEYINPUT) && !rNEvt.GetKeyEvent()->GetKeyCode().IsMod2() )
982*cdf0e10cSrcweir     {
983*cdf0e10cSrcweir         if ( ImplNumericProcessKeyInput( GetField(), *rNEvt.GetKeyEvent(), IsStrictFormat(), IsUseThousandSep(), ImplGetLocaleDataWrapper() ) )
984*cdf0e10cSrcweir             return 1;
985*cdf0e10cSrcweir     }
986*cdf0e10cSrcweir 
987*cdf0e10cSrcweir     return ComboBox::PreNotify( rNEvt );
988*cdf0e10cSrcweir }
989*cdf0e10cSrcweir 
990*cdf0e10cSrcweir // -----------------------------------------------------------------------
991*cdf0e10cSrcweir 
992*cdf0e10cSrcweir long NumericBox::Notify( NotifyEvent& rNEvt )
993*cdf0e10cSrcweir {
994*cdf0e10cSrcweir     if ( rNEvt.GetType() == EVENT_GETFOCUS )
995*cdf0e10cSrcweir         MarkToBeReformatted( sal_False );
996*cdf0e10cSrcweir     else if ( rNEvt.GetType() == EVENT_LOSEFOCUS )
997*cdf0e10cSrcweir     {
998*cdf0e10cSrcweir         if ( MustBeReformatted() && (GetText().Len() || !IsEmptyFieldValueEnabled()) )
999*cdf0e10cSrcweir             Reformat();
1000*cdf0e10cSrcweir     }
1001*cdf0e10cSrcweir 
1002*cdf0e10cSrcweir     return ComboBox::Notify( rNEvt );
1003*cdf0e10cSrcweir }
1004*cdf0e10cSrcweir 
1005*cdf0e10cSrcweir // -----------------------------------------------------------------------
1006*cdf0e10cSrcweir 
1007*cdf0e10cSrcweir void NumericBox::DataChanged( const DataChangedEvent& rDCEvt )
1008*cdf0e10cSrcweir {
1009*cdf0e10cSrcweir     ComboBox::DataChanged( rDCEvt );
1010*cdf0e10cSrcweir 
1011*cdf0e10cSrcweir     if ( (rDCEvt.GetType() == DATACHANGED_SETTINGS) && (rDCEvt.GetFlags() & SETTINGS_LOCALE) )
1012*cdf0e10cSrcweir     {
1013*cdf0e10cSrcweir         String sOldDecSep = ImplGetLocaleDataWrapper().getNumDecimalSep();
1014*cdf0e10cSrcweir         String sOldThSep = ImplGetLocaleDataWrapper().getNumThousandSep();
1015*cdf0e10cSrcweir         if ( IsDefaultLocale() )
1016*cdf0e10cSrcweir             ImplGetLocaleDataWrapper().setLocale( GetSettings().GetLocale() );
1017*cdf0e10cSrcweir         String sNewDecSep = ImplGetLocaleDataWrapper().getNumDecimalSep();
1018*cdf0e10cSrcweir         String sNewThSep = ImplGetLocaleDataWrapper().getNumThousandSep();
1019*cdf0e10cSrcweir         ImplUpdateSeparators( sOldDecSep, sNewDecSep, sOldThSep, sNewThSep, this );
1020*cdf0e10cSrcweir         ReformatAll();
1021*cdf0e10cSrcweir     }
1022*cdf0e10cSrcweir }
1023*cdf0e10cSrcweir 
1024*cdf0e10cSrcweir // -----------------------------------------------------------------------
1025*cdf0e10cSrcweir 
1026*cdf0e10cSrcweir void NumericBox::Modify()
1027*cdf0e10cSrcweir {
1028*cdf0e10cSrcweir     MarkToBeReformatted( sal_True );
1029*cdf0e10cSrcweir     ComboBox::Modify();
1030*cdf0e10cSrcweir }
1031*cdf0e10cSrcweir 
1032*cdf0e10cSrcweir // -----------------------------------------------------------------------
1033*cdf0e10cSrcweir 
1034*cdf0e10cSrcweir void NumericBox::ReformatAll()
1035*cdf0e10cSrcweir {
1036*cdf0e10cSrcweir     double nValue;
1037*cdf0e10cSrcweir     XubString aStr;
1038*cdf0e10cSrcweir     SetUpdateMode( sal_False );
1039*cdf0e10cSrcweir     sal_uInt16 nEntryCount = GetEntryCount();
1040*cdf0e10cSrcweir     for ( sal_uInt16 i=0; i < nEntryCount; i++ )
1041*cdf0e10cSrcweir     {
1042*cdf0e10cSrcweir         ImplNumericReformat( GetEntry( i ), nValue, aStr );
1043*cdf0e10cSrcweir         RemoveEntry( i );
1044*cdf0e10cSrcweir         InsertEntry( aStr, i );
1045*cdf0e10cSrcweir     }
1046*cdf0e10cSrcweir     NumericFormatter::Reformat();
1047*cdf0e10cSrcweir     SetUpdateMode( sal_True );
1048*cdf0e10cSrcweir }
1049*cdf0e10cSrcweir 
1050*cdf0e10cSrcweir // -----------------------------------------------------------------------
1051*cdf0e10cSrcweir 
1052*cdf0e10cSrcweir void NumericBox::InsertValue( sal_Int64 nValue, sal_uInt16 nPos )
1053*cdf0e10cSrcweir {
1054*cdf0e10cSrcweir     ComboBox::InsertEntry( CreateFieldText( nValue ), nPos );
1055*cdf0e10cSrcweir }
1056*cdf0e10cSrcweir 
1057*cdf0e10cSrcweir // -----------------------------------------------------------------------
1058*cdf0e10cSrcweir 
1059*cdf0e10cSrcweir void NumericBox::RemoveValue( sal_Int64 nValue )
1060*cdf0e10cSrcweir {
1061*cdf0e10cSrcweir     ComboBox::RemoveEntry( CreateFieldText( nValue ) );
1062*cdf0e10cSrcweir }
1063*cdf0e10cSrcweir 
1064*cdf0e10cSrcweir // -----------------------------------------------------------------------
1065*cdf0e10cSrcweir 
1066*cdf0e10cSrcweir sal_Int64 NumericBox::GetValue( sal_uInt16 nPos ) const
1067*cdf0e10cSrcweir {
1068*cdf0e10cSrcweir     double nValue = 0;
1069*cdf0e10cSrcweir     ImplNumericGetValue( ComboBox::GetEntry( nPos ), nValue, GetDecimalDigits(), ImplGetLocaleDataWrapper() );
1070*cdf0e10cSrcweir     return (sal_Int64)nValue;
1071*cdf0e10cSrcweir }
1072*cdf0e10cSrcweir 
1073*cdf0e10cSrcweir // -----------------------------------------------------------------------
1074*cdf0e10cSrcweir 
1075*cdf0e10cSrcweir sal_uInt16 NumericBox::GetValuePos( sal_Int64 nValue ) const
1076*cdf0e10cSrcweir {
1077*cdf0e10cSrcweir     return ComboBox::GetEntryPos( CreateFieldText( nValue ) );
1078*cdf0e10cSrcweir }
1079*cdf0e10cSrcweir 
1080*cdf0e10cSrcweir // -----------------------------------------------------------------------
1081*cdf0e10cSrcweir 
1082*cdf0e10cSrcweir static sal_Bool ImplMetricProcessKeyInput( Edit* pEdit, const KeyEvent& rKEvt,
1083*cdf0e10cSrcweir                                        sal_Bool, sal_Bool bUseThousandSep, const LocaleDataWrapper& rWrapper )
1084*cdf0e10cSrcweir {
1085*cdf0e10cSrcweir     // Es gibt hier kein sinnvolles StrictFormat, also alle
1086*cdf0e10cSrcweir     // Zeichen erlauben
1087*cdf0e10cSrcweir     return ImplNumericProcessKeyInput( pEdit, rKEvt, sal_False, bUseThousandSep, rWrapper );
1088*cdf0e10cSrcweir }
1089*cdf0e10cSrcweir 
1090*cdf0e10cSrcweir // -----------------------------------------------------------------------
1091*cdf0e10cSrcweir 
1092*cdf0e10cSrcweir static XubString ImplMetricGetUnitText( const XubString& rStr )
1093*cdf0e10cSrcweir {
1094*cdf0e10cSrcweir     // Einheitentext holen
1095*cdf0e10cSrcweir     XubString aStr;
1096*cdf0e10cSrcweir     for ( short i = rStr.Len()-1; i >= 0; i-- )
1097*cdf0e10cSrcweir     {
1098*cdf0e10cSrcweir         xub_Unicode c = rStr.GetChar( i );
1099*cdf0e10cSrcweir         if ( unicode::isAlpha( c ) ||
1100*cdf0e10cSrcweir              (c == '\'') || (c == '\"') || (c == '%' ) )
1101*cdf0e10cSrcweir             aStr.Insert( c, 0 );
1102*cdf0e10cSrcweir         else
1103*cdf0e10cSrcweir         {
1104*cdf0e10cSrcweir             if ( aStr.Len() )
1105*cdf0e10cSrcweir                 break;
1106*cdf0e10cSrcweir         }
1107*cdf0e10cSrcweir     }
1108*cdf0e10cSrcweir     return aStr;
1109*cdf0e10cSrcweir 
1110*cdf0e10cSrcweir /*
1111*cdf0e10cSrcweir     // MT: #90545# Preparation for translated strings...
1112*cdf0e10cSrcweir     String aMetricText;
1113*cdf0e10cSrcweir     for ( sal_uInt16 n = rStr.Len(); n; )
1114*cdf0e10cSrcweir     {
1115*cdf0e10cSrcweir         sal_Unicode c = rStr.GetChar( --n );
1116*cdf0e10cSrcweir         sal_Int32 nType = xCharClass->getStringType( rStr, n, 1, rLocale );
1117*cdf0e10cSrcweir 
1118*cdf0e10cSrcweir         if ( CharClass::isLetterType( nType ) )
1119*cdf0e10cSrcweir         {
1120*cdf0e10cSrcweir             aMetricText.Insert( c, 0 );
1121*cdf0e10cSrcweir         }
1122*cdf0e10cSrcweir         else
1123*cdf0e10cSrcweir         {
1124*cdf0e10cSrcweir             if ( aMetricText.Len() )
1125*cdf0e10cSrcweir                 break;
1126*cdf0e10cSrcweir         }
1127*cdf0e10cSrcweir     }
1128*cdf0e10cSrcweir */
1129*cdf0e10cSrcweir }
1130*cdf0e10cSrcweir 
1131*cdf0e10cSrcweir // -----------------------------------------------------------------------
1132*cdf0e10cSrcweir 
1133*cdf0e10cSrcweir // #104355# support localized mesaurements
1134*cdf0e10cSrcweir 
1135*cdf0e10cSrcweir static const String& ImplMetricToString( FieldUnit rUnit )
1136*cdf0e10cSrcweir {
1137*cdf0e10cSrcweir     FieldUnitStringList* pList = ImplGetFieldUnits();
1138*cdf0e10cSrcweir     if( pList )
1139*cdf0e10cSrcweir     {
1140*cdf0e10cSrcweir         // return unit's default string (ie, the first one )
1141*cdf0e10cSrcweir         for( FieldUnitStringList::const_iterator it = pList->begin(); it != pList->end(); ++it )
1142*cdf0e10cSrcweir         {
1143*cdf0e10cSrcweir             if ( it->second == rUnit )
1144*cdf0e10cSrcweir                 return it->first;
1145*cdf0e10cSrcweir         }
1146*cdf0e10cSrcweir     }
1147*cdf0e10cSrcweir 
1148*cdf0e10cSrcweir     return String::EmptyString();
1149*cdf0e10cSrcweir }
1150*cdf0e10cSrcweir 
1151*cdf0e10cSrcweir static FieldUnit ImplStringToMetric( const String &rMetricString )
1152*cdf0e10cSrcweir {
1153*cdf0e10cSrcweir     FieldUnitStringList* pList = ImplGetCleanedFieldUnits();
1154*cdf0e10cSrcweir     if( pList )
1155*cdf0e10cSrcweir     {
1156*cdf0e10cSrcweir         // return FieldUnit
1157*cdf0e10cSrcweir         String aStr( rMetricString );
1158*cdf0e10cSrcweir         aStr.ToLowerAscii();
1159*cdf0e10cSrcweir         aStr.EraseAllChars( sal_Unicode( ' ' ) );
1160*cdf0e10cSrcweir         for( FieldUnitStringList::const_iterator it = pList->begin(); it != pList->end(); ++it )
1161*cdf0e10cSrcweir         {
1162*cdf0e10cSrcweir             if ( it->first.Equals( aStr ) )
1163*cdf0e10cSrcweir                 return it->second;
1164*cdf0e10cSrcweir         }
1165*cdf0e10cSrcweir     }
1166*cdf0e10cSrcweir 
1167*cdf0e10cSrcweir     return FUNIT_NONE;
1168*cdf0e10cSrcweir }
1169*cdf0e10cSrcweir 
1170*cdf0e10cSrcweir // -----------------------------------------------------------------------
1171*cdf0e10cSrcweir 
1172*cdf0e10cSrcweir static FieldUnit ImplMetricGetUnit( const XubString& rStr )
1173*cdf0e10cSrcweir {
1174*cdf0e10cSrcweir     XubString aStr = ImplMetricGetUnitText( rStr );
1175*cdf0e10cSrcweir     return ImplStringToMetric( aStr );
1176*cdf0e10cSrcweir }
1177*cdf0e10cSrcweir 
1178*cdf0e10cSrcweir #define K *1000L
1179*cdf0e10cSrcweir #define M *1000000L
1180*cdf0e10cSrcweir #define X *5280L
1181*cdf0e10cSrcweir 
1182*cdf0e10cSrcweir static const sal_Int64 aImplFactor[FUNIT_MILE+1][FUNIT_MILE+1] =
1183*cdf0e10cSrcweir { /*
1184*cdf0e10cSrcweir mm/100    mm    cm       m     km  twip point  pica  inch    foot     mile */
1185*cdf0e10cSrcweir {    1,  100,  1 K,  100 K, 100 M, 2540, 2540, 2540, 2540,2540*12,2540*12 X },
1186*cdf0e10cSrcweir {    1,    1,   10,    1 K,   1 M, 2540, 2540, 2540, 2540,2540*12,2540*12 X },
1187*cdf0e10cSrcweir {    1,    1,    1,    100, 100 K,  254,  254,  254,  254, 254*12, 254*12 X },
1188*cdf0e10cSrcweir {    1,    1,    1,      1,   1 K,  254,  254,  254,  254, 254*12, 254*12 X },
1189*cdf0e10cSrcweir {    1,    1,    1,      1,     1,    0,  254,  254,  254, 254*12, 254*12 X },
1190*cdf0e10cSrcweir { 1440,144 K,144 K,14400 K,     0,    1,   20,  240, 1440,1440*12,1440*12 X },
1191*cdf0e10cSrcweir {   72, 7200, 7200,  720 K, 720 M,    1,    1,   12,   72,  72*12,  72*12 X },
1192*cdf0e10cSrcweir {    6,  600,  600,   60 K,  60 M,    1,    1,    1,    6,   6*12,   6*12 X },
1193*cdf0e10cSrcweir {    1,  100,  100,   10 K,  10 M,    1,    1,    1,    1,     12,     12 X },
1194*cdf0e10cSrcweir {    1,  100,  100,   10 K,  10 M,    1,    1,    1,    1,      1,      1 X },
1195*cdf0e10cSrcweir {    1,  100,  100,   10 K,  10 M,    1,    1,    1,    1,      1,        1 }
1196*cdf0e10cSrcweir };
1197*cdf0e10cSrcweir 
1198*cdf0e10cSrcweir #undef X
1199*cdf0e10cSrcweir #undef M
1200*cdf0e10cSrcweir #undef K
1201*cdf0e10cSrcweir // twip in km 254/14400 M
1202*cdf0e10cSrcweir 
1203*cdf0e10cSrcweir static FieldUnit eDefaultUnit = FUNIT_NONE;
1204*cdf0e10cSrcweir 
1205*cdf0e10cSrcweir FieldUnit MetricField::GetDefaultUnit() { return eDefaultUnit; }
1206*cdf0e10cSrcweir void MetricField::SetDefaultUnit( FieldUnit meUnit ) { eDefaultUnit = meUnit; }
1207*cdf0e10cSrcweir 
1208*cdf0e10cSrcweir static FieldUnit ImplMap2FieldUnit( MapUnit meUnit, long& nDecDigits )
1209*cdf0e10cSrcweir {
1210*cdf0e10cSrcweir     switch( meUnit )
1211*cdf0e10cSrcweir     {
1212*cdf0e10cSrcweir         case MAP_100TH_MM :
1213*cdf0e10cSrcweir             nDecDigits -= 2;
1214*cdf0e10cSrcweir             return FUNIT_MM;
1215*cdf0e10cSrcweir         case MAP_10TH_MM :
1216*cdf0e10cSrcweir             nDecDigits -= 1;
1217*cdf0e10cSrcweir             return FUNIT_MM;
1218*cdf0e10cSrcweir         case MAP_MM :
1219*cdf0e10cSrcweir             return FUNIT_MM;
1220*cdf0e10cSrcweir         case MAP_CM :
1221*cdf0e10cSrcweir             return FUNIT_CM;
1222*cdf0e10cSrcweir         case MAP_1000TH_INCH :
1223*cdf0e10cSrcweir             nDecDigits -= 3;
1224*cdf0e10cSrcweir             return FUNIT_INCH;
1225*cdf0e10cSrcweir         case MAP_100TH_INCH :
1226*cdf0e10cSrcweir             nDecDigits -= 2;
1227*cdf0e10cSrcweir             return FUNIT_INCH;
1228*cdf0e10cSrcweir         case MAP_10TH_INCH :
1229*cdf0e10cSrcweir             nDecDigits -= 1;
1230*cdf0e10cSrcweir             return FUNIT_INCH;
1231*cdf0e10cSrcweir         case MAP_INCH :
1232*cdf0e10cSrcweir             return FUNIT_INCH;
1233*cdf0e10cSrcweir         case MAP_POINT :
1234*cdf0e10cSrcweir             return FUNIT_POINT;
1235*cdf0e10cSrcweir         case MAP_TWIP :
1236*cdf0e10cSrcweir             return FUNIT_TWIP;
1237*cdf0e10cSrcweir         default:
1238*cdf0e10cSrcweir             DBG_ERROR( "default eInUnit" );
1239*cdf0e10cSrcweir             break;
1240*cdf0e10cSrcweir     }
1241*cdf0e10cSrcweir     return FUNIT_NONE;
1242*cdf0e10cSrcweir }
1243*cdf0e10cSrcweir 
1244*cdf0e10cSrcweir // -----------------------------------------------------------------------
1245*cdf0e10cSrcweir 
1246*cdf0e10cSrcweir static double nonValueDoubleToValueDouble( double nValue )
1247*cdf0e10cSrcweir {
1248*cdf0e10cSrcweir     return rtl::math::isFinite( nValue ) ? nValue : 0.0;
1249*cdf0e10cSrcweir }
1250*cdf0e10cSrcweir 
1251*cdf0e10cSrcweir sal_Int64 MetricField::ConvertValue( sal_Int64 nValue, sal_Int64 mnBaseValue, sal_uInt16 nDecDigits,
1252*cdf0e10cSrcweir                                      FieldUnit eInUnit, FieldUnit eOutUnit )
1253*cdf0e10cSrcweir {
1254*cdf0e10cSrcweir     // caution: precision loss in double cast
1255*cdf0e10cSrcweir     return static_cast<sal_Int64>(
1256*cdf0e10cSrcweir         // #150733# cast double to sal_Int64 can throw a
1257*cdf0e10cSrcweir         // EXCEPTION_FLT_INVALID_OPERATION on Windows
1258*cdf0e10cSrcweir         nonValueDoubleToValueDouble(
1259*cdf0e10cSrcweir             ConvertDoubleValue( (double)nValue, mnBaseValue, nDecDigits,
1260*cdf0e10cSrcweir                                 eInUnit, eOutUnit ) ) );
1261*cdf0e10cSrcweir }
1262*cdf0e10cSrcweir 
1263*cdf0e10cSrcweir // -----------------------------------------------------------------------
1264*cdf0e10cSrcweir 
1265*cdf0e10cSrcweir sal_Int64 MetricField::ConvertValue( sal_Int64 nValue, sal_uInt16 nDigits,
1266*cdf0e10cSrcweir                                      MapUnit eInUnit, FieldUnit eOutUnit )
1267*cdf0e10cSrcweir {
1268*cdf0e10cSrcweir     return static_cast<sal_Int64>(
1269*cdf0e10cSrcweir         // #150733# cast double to sal_Int64 can throw a
1270*cdf0e10cSrcweir         // EXCEPTION_FLT_INVALID_OPERATION on Windows
1271*cdf0e10cSrcweir         nonValueDoubleToValueDouble(
1272*cdf0e10cSrcweir             ConvertDoubleValue( nValue, nDigits, eInUnit, eOutUnit ) ) );
1273*cdf0e10cSrcweir }
1274*cdf0e10cSrcweir 
1275*cdf0e10cSrcweir // -----------------------------------------------------------------------
1276*cdf0e10cSrcweir 
1277*cdf0e10cSrcweir sal_Int64 MetricField::ConvertValue( sal_Int64 nValue, sal_uInt16 nDigits,
1278*cdf0e10cSrcweir                                      FieldUnit eInUnit, MapUnit eOutUnit )
1279*cdf0e10cSrcweir {
1280*cdf0e10cSrcweir     return static_cast<sal_Int64>(
1281*cdf0e10cSrcweir         // #150733# cast double to sal_Int64 can throw a
1282*cdf0e10cSrcweir         // EXCEPTION_FLT_INVALID_OPERATION on Windows
1283*cdf0e10cSrcweir         nonValueDoubleToValueDouble(
1284*cdf0e10cSrcweir             ConvertDoubleValue( nValue, nDigits, eInUnit, eOutUnit ) ) );
1285*cdf0e10cSrcweir }
1286*cdf0e10cSrcweir 
1287*cdf0e10cSrcweir // -----------------------------------------------------------------------
1288*cdf0e10cSrcweir 
1289*cdf0e10cSrcweir double MetricField::ConvertDoubleValue( double nValue, sal_Int64 mnBaseValue, sal_uInt16 nDecDigits,
1290*cdf0e10cSrcweir                                         FieldUnit eInUnit, FieldUnit eOutUnit )
1291*cdf0e10cSrcweir {
1292*cdf0e10cSrcweir     if ( eInUnit != eOutUnit )
1293*cdf0e10cSrcweir     {
1294*cdf0e10cSrcweir         sal_Int64 nMult = 1, nDiv = 1;
1295*cdf0e10cSrcweir 
1296*cdf0e10cSrcweir         if ( eInUnit == FUNIT_PERCENT )
1297*cdf0e10cSrcweir         {
1298*cdf0e10cSrcweir             if ( (mnBaseValue <= 0) || (nValue <= 0) )
1299*cdf0e10cSrcweir                 return nValue;
1300*cdf0e10cSrcweir             nDiv = 100;
1301*cdf0e10cSrcweir             for ( sal_uInt16 i=0; i < nDecDigits; i++ )
1302*cdf0e10cSrcweir                 nDiv *= 10;
1303*cdf0e10cSrcweir 
1304*cdf0e10cSrcweir             nMult = mnBaseValue;
1305*cdf0e10cSrcweir         }
1306*cdf0e10cSrcweir         else if ( eOutUnit == FUNIT_PERCENT ||
1307*cdf0e10cSrcweir                   eOutUnit == FUNIT_CUSTOM ||
1308*cdf0e10cSrcweir                   eOutUnit == FUNIT_NONE ||
1309*cdf0e10cSrcweir                   eInUnit  == FUNIT_CUSTOM ||
1310*cdf0e10cSrcweir                   eInUnit  == FUNIT_NONE )
1311*cdf0e10cSrcweir              return nValue;
1312*cdf0e10cSrcweir         else
1313*cdf0e10cSrcweir         {
1314*cdf0e10cSrcweir             if ( eOutUnit == FUNIT_100TH_MM )
1315*cdf0e10cSrcweir                 eOutUnit = FUNIT_NONE;
1316*cdf0e10cSrcweir             if ( eInUnit == FUNIT_100TH_MM )
1317*cdf0e10cSrcweir                 eInUnit = FUNIT_NONE;
1318*cdf0e10cSrcweir 
1319*cdf0e10cSrcweir             nDiv  = aImplFactor[eInUnit][eOutUnit];
1320*cdf0e10cSrcweir             nMult = aImplFactor[eOutUnit][eInUnit];
1321*cdf0e10cSrcweir 
1322*cdf0e10cSrcweir             DBG_ASSERT( nMult > 0, "illegal *" );
1323*cdf0e10cSrcweir             DBG_ASSERT( nDiv  > 0, "illegal /" );
1324*cdf0e10cSrcweir         }
1325*cdf0e10cSrcweir 
1326*cdf0e10cSrcweir         if ( nMult != 1 && nMult > 0 )
1327*cdf0e10cSrcweir             nValue *= nMult;
1328*cdf0e10cSrcweir         if ( nDiv != 1 && nDiv > 0 )
1329*cdf0e10cSrcweir         {
1330*cdf0e10cSrcweir             nValue += ( nValue < 0 ) ? (-nDiv/2) : (nDiv/2);
1331*cdf0e10cSrcweir             nValue /= nDiv;
1332*cdf0e10cSrcweir         }
1333*cdf0e10cSrcweir     }
1334*cdf0e10cSrcweir 
1335*cdf0e10cSrcweir     return nValue;
1336*cdf0e10cSrcweir }
1337*cdf0e10cSrcweir 
1338*cdf0e10cSrcweir // -----------------------------------------------------------------------
1339*cdf0e10cSrcweir 
1340*cdf0e10cSrcweir double MetricField::ConvertDoubleValue( double nValue, sal_uInt16 nDigits,
1341*cdf0e10cSrcweir                                         MapUnit eInUnit, FieldUnit eOutUnit )
1342*cdf0e10cSrcweir {
1343*cdf0e10cSrcweir     if ( eOutUnit == FUNIT_PERCENT ||
1344*cdf0e10cSrcweir          eOutUnit == FUNIT_CUSTOM ||
1345*cdf0e10cSrcweir          eOutUnit == FUNIT_NONE ||
1346*cdf0e10cSrcweir          eInUnit == MAP_PIXEL ||
1347*cdf0e10cSrcweir          eInUnit == MAP_SYSFONT ||
1348*cdf0e10cSrcweir          eInUnit == MAP_APPFONT ||
1349*cdf0e10cSrcweir          eInUnit == MAP_RELATIVE )
1350*cdf0e10cSrcweir     {
1351*cdf0e10cSrcweir         DBG_ERROR( "invalid parameters" );
1352*cdf0e10cSrcweir         return nValue;
1353*cdf0e10cSrcweir     }
1354*cdf0e10cSrcweir 
1355*cdf0e10cSrcweir     long nDecDigits = nDigits;
1356*cdf0e10cSrcweir     FieldUnit eFieldUnit = ImplMap2FieldUnit( eInUnit, nDecDigits );
1357*cdf0e10cSrcweir 
1358*cdf0e10cSrcweir     if ( nDecDigits < 0 )
1359*cdf0e10cSrcweir     {
1360*cdf0e10cSrcweir         while ( nDecDigits )
1361*cdf0e10cSrcweir         {
1362*cdf0e10cSrcweir             nValue += 5;
1363*cdf0e10cSrcweir             nValue /= 10;
1364*cdf0e10cSrcweir             nDecDigits++;
1365*cdf0e10cSrcweir         }
1366*cdf0e10cSrcweir     }
1367*cdf0e10cSrcweir     else
1368*cdf0e10cSrcweir     {
1369*cdf0e10cSrcweir         while ( nDecDigits )
1370*cdf0e10cSrcweir         {
1371*cdf0e10cSrcweir             nValue *= 10;
1372*cdf0e10cSrcweir             nDecDigits--;
1373*cdf0e10cSrcweir         }
1374*cdf0e10cSrcweir     }
1375*cdf0e10cSrcweir 
1376*cdf0e10cSrcweir     if ( eFieldUnit != eOutUnit )
1377*cdf0e10cSrcweir     {
1378*cdf0e10cSrcweir         sal_Int64 nDiv  = aImplFactor[eFieldUnit][eOutUnit];
1379*cdf0e10cSrcweir         sal_Int64 nMult = aImplFactor[eOutUnit][eFieldUnit];
1380*cdf0e10cSrcweir 
1381*cdf0e10cSrcweir         DBG_ASSERT( nMult > 0, "illegal *" );
1382*cdf0e10cSrcweir         DBG_ASSERT( nDiv  > 0, "illegal /" );
1383*cdf0e10cSrcweir 
1384*cdf0e10cSrcweir         if ( nMult != 1 && nMult > 0)
1385*cdf0e10cSrcweir             nValue *= nMult;
1386*cdf0e10cSrcweir         if ( nDiv != 1 && nDiv > 0 )
1387*cdf0e10cSrcweir         {
1388*cdf0e10cSrcweir             nValue += (nValue < 0) ? (-nDiv/2) : (nDiv/2);
1389*cdf0e10cSrcweir             nValue /= nDiv;
1390*cdf0e10cSrcweir         }
1391*cdf0e10cSrcweir     }
1392*cdf0e10cSrcweir     return nValue;
1393*cdf0e10cSrcweir }
1394*cdf0e10cSrcweir 
1395*cdf0e10cSrcweir // -----------------------------------------------------------------------
1396*cdf0e10cSrcweir 
1397*cdf0e10cSrcweir double MetricField::ConvertDoubleValue( double nValue, sal_uInt16 nDigits,
1398*cdf0e10cSrcweir                                         FieldUnit eInUnit, MapUnit eOutUnit )
1399*cdf0e10cSrcweir {
1400*cdf0e10cSrcweir     if ( eInUnit == FUNIT_PERCENT ||
1401*cdf0e10cSrcweir          eInUnit == FUNIT_CUSTOM ||
1402*cdf0e10cSrcweir          eInUnit == FUNIT_NONE ||
1403*cdf0e10cSrcweir          eOutUnit == MAP_PIXEL ||
1404*cdf0e10cSrcweir          eOutUnit == MAP_SYSFONT ||
1405*cdf0e10cSrcweir          eOutUnit == MAP_APPFONT ||
1406*cdf0e10cSrcweir          eOutUnit == MAP_RELATIVE )
1407*cdf0e10cSrcweir     {
1408*cdf0e10cSrcweir         DBG_ERROR( "invalid parameters" );
1409*cdf0e10cSrcweir         return nValue;
1410*cdf0e10cSrcweir     }
1411*cdf0e10cSrcweir 
1412*cdf0e10cSrcweir     long nDecDigits = nDigits;
1413*cdf0e10cSrcweir     FieldUnit eFieldUnit = ImplMap2FieldUnit( eOutUnit, nDecDigits );
1414*cdf0e10cSrcweir 
1415*cdf0e10cSrcweir     if ( nDecDigits < 0 )
1416*cdf0e10cSrcweir     {
1417*cdf0e10cSrcweir         while ( nDecDigits )
1418*cdf0e10cSrcweir         {
1419*cdf0e10cSrcweir             nValue *= 10;
1420*cdf0e10cSrcweir             nDecDigits++;
1421*cdf0e10cSrcweir         }
1422*cdf0e10cSrcweir     }
1423*cdf0e10cSrcweir     else
1424*cdf0e10cSrcweir     {
1425*cdf0e10cSrcweir         while ( nDecDigits )
1426*cdf0e10cSrcweir         {
1427*cdf0e10cSrcweir             nValue += 5;
1428*cdf0e10cSrcweir             nValue /= 10;
1429*cdf0e10cSrcweir             nDecDigits--;
1430*cdf0e10cSrcweir         }
1431*cdf0e10cSrcweir     }
1432*cdf0e10cSrcweir 
1433*cdf0e10cSrcweir     if ( eFieldUnit != eInUnit )
1434*cdf0e10cSrcweir     {
1435*cdf0e10cSrcweir         sal_Int64 nDiv  = aImplFactor[eInUnit][eFieldUnit];
1436*cdf0e10cSrcweir         sal_Int64 nMult = aImplFactor[eFieldUnit][eInUnit];
1437*cdf0e10cSrcweir 
1438*cdf0e10cSrcweir         DBG_ASSERT( nMult > 0, "illegal *" );
1439*cdf0e10cSrcweir         DBG_ASSERT( nDiv  > 0, "illegal /" );
1440*cdf0e10cSrcweir 
1441*cdf0e10cSrcweir         if( nMult != 1 && nMult > 0 )
1442*cdf0e10cSrcweir             nValue *= nMult;
1443*cdf0e10cSrcweir         if( nDiv != 1 && nDiv > 0 )
1444*cdf0e10cSrcweir         {
1445*cdf0e10cSrcweir             nValue += (nValue < 0) ? (-nDiv/2) : (nDiv/2);
1446*cdf0e10cSrcweir             nValue /= nDiv;
1447*cdf0e10cSrcweir         }
1448*cdf0e10cSrcweir     }
1449*cdf0e10cSrcweir     return nValue;
1450*cdf0e10cSrcweir }
1451*cdf0e10cSrcweir 
1452*cdf0e10cSrcweir // -----------------------------------------------------------------------
1453*cdf0e10cSrcweir 
1454*cdf0e10cSrcweir static sal_Bool ImplMetricGetValue( const XubString& rStr, double& rValue, sal_Int64 nBaseValue,
1455*cdf0e10cSrcweir                                 sal_uInt16 nDecDigits, const LocaleDataWrapper& rLocaleDataWrapper, FieldUnit eUnit )
1456*cdf0e10cSrcweir {
1457*cdf0e10cSrcweir     // Zahlenwert holen
1458*cdf0e10cSrcweir     if ( !ImplNumericGetValue( rStr, rValue, nDecDigits, rLocaleDataWrapper ) )
1459*cdf0e10cSrcweir         return sal_False;
1460*cdf0e10cSrcweir 
1461*cdf0e10cSrcweir     // Einheit rausfinden
1462*cdf0e10cSrcweir     FieldUnit eEntryUnit = ImplMetricGetUnit( rStr );
1463*cdf0e10cSrcweir 
1464*cdf0e10cSrcweir     // Einheiten umrechnen
1465*cdf0e10cSrcweir     rValue = MetricField::ConvertDoubleValue( rValue, nBaseValue, nDecDigits, eEntryUnit, eUnit );
1466*cdf0e10cSrcweir 
1467*cdf0e10cSrcweir     return sal_True;
1468*cdf0e10cSrcweir }
1469*cdf0e10cSrcweir 
1470*cdf0e10cSrcweir // -----------------------------------------------------------------------
1471*cdf0e10cSrcweir 
1472*cdf0e10cSrcweir sal_Bool MetricFormatter::ImplMetricReformat( const XubString& rStr, double& rValue, XubString& rOutStr )
1473*cdf0e10cSrcweir {
1474*cdf0e10cSrcweir     if ( !ImplMetricGetValue( rStr, rValue, mnBaseValue, GetDecimalDigits(), ImplGetLocaleDataWrapper(), meUnit ) )
1475*cdf0e10cSrcweir         return sal_True;
1476*cdf0e10cSrcweir     else
1477*cdf0e10cSrcweir     {
1478*cdf0e10cSrcweir         double nTempVal = rValue;
1479*cdf0e10cSrcweir         // caution: precision loss in double cast
1480*cdf0e10cSrcweir         if ( nTempVal > GetMax() )
1481*cdf0e10cSrcweir             nTempVal = (double)GetMax();
1482*cdf0e10cSrcweir         else if ( nTempVal < GetMin())
1483*cdf0e10cSrcweir             nTempVal = (double)GetMin();
1484*cdf0e10cSrcweir 
1485*cdf0e10cSrcweir         if ( GetErrorHdl().IsSet() && (rValue != nTempVal) )
1486*cdf0e10cSrcweir         {
1487*cdf0e10cSrcweir             mnCorrectedValue = (sal_Int64)nTempVal;
1488*cdf0e10cSrcweir             if ( !GetErrorHdl().Call( this ) )
1489*cdf0e10cSrcweir             {
1490*cdf0e10cSrcweir                 mnCorrectedValue = 0;
1491*cdf0e10cSrcweir                 return sal_False;
1492*cdf0e10cSrcweir             }
1493*cdf0e10cSrcweir             else
1494*cdf0e10cSrcweir                 mnCorrectedValue = 0;
1495*cdf0e10cSrcweir         }
1496*cdf0e10cSrcweir 
1497*cdf0e10cSrcweir         rOutStr = CreateFieldText( (sal_Int64)nTempVal );
1498*cdf0e10cSrcweir         return sal_True;
1499*cdf0e10cSrcweir     }
1500*cdf0e10cSrcweir }
1501*cdf0e10cSrcweir 
1502*cdf0e10cSrcweir // -----------------------------------------------------------------------
1503*cdf0e10cSrcweir 
1504*cdf0e10cSrcweir inline void MetricFormatter::ImplInit()
1505*cdf0e10cSrcweir {
1506*cdf0e10cSrcweir     mnBaseValue = 0;
1507*cdf0e10cSrcweir     meUnit = MetricField::GetDefaultUnit();
1508*cdf0e10cSrcweir     mnType = FORMAT_METRIC;
1509*cdf0e10cSrcweir }
1510*cdf0e10cSrcweir 
1511*cdf0e10cSrcweir // -----------------------------------------------------------------------
1512*cdf0e10cSrcweir 
1513*cdf0e10cSrcweir MetricFormatter::MetricFormatter()
1514*cdf0e10cSrcweir {
1515*cdf0e10cSrcweir     ImplInit();
1516*cdf0e10cSrcweir }
1517*cdf0e10cSrcweir 
1518*cdf0e10cSrcweir // -----------------------------------------------------------------------
1519*cdf0e10cSrcweir 
1520*cdf0e10cSrcweir void MetricFormatter::ImplLoadRes( const ResId& rResId )
1521*cdf0e10cSrcweir {
1522*cdf0e10cSrcweir     NumericFormatter::ImplLoadRes( rResId );
1523*cdf0e10cSrcweir 
1524*cdf0e10cSrcweir     ResMgr*     pMgr = rResId.GetResMgr();
1525*cdf0e10cSrcweir     if( pMgr )
1526*cdf0e10cSrcweir     {
1527*cdf0e10cSrcweir         sal_uLong       nMask = pMgr->ReadLong();
1528*cdf0e10cSrcweir 
1529*cdf0e10cSrcweir         if ( METRICFORMATTER_UNIT & nMask )
1530*cdf0e10cSrcweir             meUnit = (FieldUnit)pMgr->ReadLong();
1531*cdf0e10cSrcweir 
1532*cdf0e10cSrcweir         if ( METRICFORMATTER_CUSTOMUNITTEXT & nMask )
1533*cdf0e10cSrcweir             maCustomUnitText = pMgr->ReadString();
1534*cdf0e10cSrcweir     }
1535*cdf0e10cSrcweir }
1536*cdf0e10cSrcweir 
1537*cdf0e10cSrcweir // -----------------------------------------------------------------------
1538*cdf0e10cSrcweir 
1539*cdf0e10cSrcweir MetricFormatter::~MetricFormatter()
1540*cdf0e10cSrcweir {
1541*cdf0e10cSrcweir }
1542*cdf0e10cSrcweir 
1543*cdf0e10cSrcweir // -----------------------------------------------------------------------
1544*cdf0e10cSrcweir 
1545*cdf0e10cSrcweir void MetricFormatter::SetUnit( FieldUnit eNewUnit )
1546*cdf0e10cSrcweir {
1547*cdf0e10cSrcweir     if ( eNewUnit == FUNIT_100TH_MM )
1548*cdf0e10cSrcweir     {
1549*cdf0e10cSrcweir         SetDecimalDigits( GetDecimalDigits() + 2 );
1550*cdf0e10cSrcweir         meUnit = FUNIT_MM;
1551*cdf0e10cSrcweir     }
1552*cdf0e10cSrcweir     else
1553*cdf0e10cSrcweir         meUnit = eNewUnit;
1554*cdf0e10cSrcweir     ReformatAll();
1555*cdf0e10cSrcweir }
1556*cdf0e10cSrcweir 
1557*cdf0e10cSrcweir // -----------------------------------------------------------------------
1558*cdf0e10cSrcweir 
1559*cdf0e10cSrcweir void MetricFormatter::SetCustomUnitText( const XubString& rStr )
1560*cdf0e10cSrcweir {
1561*cdf0e10cSrcweir     maCustomUnitText = rStr;
1562*cdf0e10cSrcweir     ReformatAll();
1563*cdf0e10cSrcweir }
1564*cdf0e10cSrcweir 
1565*cdf0e10cSrcweir // -----------------------------------------------------------------------
1566*cdf0e10cSrcweir 
1567*cdf0e10cSrcweir void MetricFormatter::SetValue( sal_Int64 nNewValue, FieldUnit eInUnit )
1568*cdf0e10cSrcweir {
1569*cdf0e10cSrcweir     SetUserValue( nNewValue, eInUnit );
1570*cdf0e10cSrcweir     mnFieldValue = mnLastValue;
1571*cdf0e10cSrcweir }
1572*cdf0e10cSrcweir 
1573*cdf0e10cSrcweir // -----------------------------------------------------------------------
1574*cdf0e10cSrcweir 
1575*cdf0e10cSrcweir XubString MetricFormatter::CreateFieldText( sal_Int64 nValue ) const
1576*cdf0e10cSrcweir {
1577*cdf0e10cSrcweir     XubString aStr = NumericFormatter::CreateFieldText( nValue );
1578*cdf0e10cSrcweir 
1579*cdf0e10cSrcweir     if( meUnit == FUNIT_CUSTOM )
1580*cdf0e10cSrcweir         aStr += maCustomUnitText;
1581*cdf0e10cSrcweir     else
1582*cdf0e10cSrcweir         aStr += ImplMetricToString( meUnit );
1583*cdf0e10cSrcweir 
1584*cdf0e10cSrcweir     return aStr;
1585*cdf0e10cSrcweir }
1586*cdf0e10cSrcweir 
1587*cdf0e10cSrcweir // -----------------------------------------------------------------------
1588*cdf0e10cSrcweir 
1589*cdf0e10cSrcweir void MetricFormatter::SetUserValue( sal_Int64 nNewValue, FieldUnit eInUnit )
1590*cdf0e10cSrcweir {
1591*cdf0e10cSrcweir     // Umrechnen auf eingestellte Einheiten
1592*cdf0e10cSrcweir     nNewValue = MetricField::ConvertValue( nNewValue, mnBaseValue, GetDecimalDigits(), eInUnit, meUnit );
1593*cdf0e10cSrcweir     NumericFormatter::SetUserValue( nNewValue );
1594*cdf0e10cSrcweir }
1595*cdf0e10cSrcweir 
1596*cdf0e10cSrcweir // -----------------------------------------------------------------------
1597*cdf0e10cSrcweir 
1598*cdf0e10cSrcweir sal_Int64 MetricFormatter::GetValue( FieldUnit eOutUnit ) const
1599*cdf0e10cSrcweir {
1600*cdf0e10cSrcweir     if ( !GetField() )
1601*cdf0e10cSrcweir         return 0;
1602*cdf0e10cSrcweir 
1603*cdf0e10cSrcweir     double nTempValue;
1604*cdf0e10cSrcweir     // caution: precision loss in double cast
1605*cdf0e10cSrcweir     if ( !ImplMetricGetValue( GetField()->GetText(), nTempValue, mnBaseValue, GetDecimalDigits(), ImplGetLocaleDataWrapper(), meUnit ) )
1606*cdf0e10cSrcweir         nTempValue = (double)mnLastValue;
1607*cdf0e10cSrcweir 
1608*cdf0e10cSrcweir     // caution: precision loss in double cast
1609*cdf0e10cSrcweir     if ( nTempValue > mnMax )
1610*cdf0e10cSrcweir         nTempValue = (double)mnMax;
1611*cdf0e10cSrcweir     else if ( nTempValue < mnMin )
1612*cdf0e10cSrcweir         nTempValue = (double)mnMin;
1613*cdf0e10cSrcweir 
1614*cdf0e10cSrcweir     // Umrechnen auf gewuenschte Einheiten
1615*cdf0e10cSrcweir     return MetricField::ConvertValue( (sal_Int64)nTempValue, mnBaseValue, GetDecimalDigits(), meUnit, eOutUnit );
1616*cdf0e10cSrcweir }
1617*cdf0e10cSrcweir 
1618*cdf0e10cSrcweir // -----------------------------------------------------------------------
1619*cdf0e10cSrcweir 
1620*cdf0e10cSrcweir void MetricFormatter::SetValue( sal_Int64 nValue )
1621*cdf0e10cSrcweir {
1622*cdf0e10cSrcweir     // Implementation not inline, because it is a virtual Function
1623*cdf0e10cSrcweir     SetValue( nValue, FUNIT_NONE );
1624*cdf0e10cSrcweir }
1625*cdf0e10cSrcweir 
1626*cdf0e10cSrcweir // -----------------------------------------------------------------------
1627*cdf0e10cSrcweir 
1628*cdf0e10cSrcweir sal_Int64 MetricFormatter::GetValue() const
1629*cdf0e10cSrcweir {
1630*cdf0e10cSrcweir     // Implementation not inline, because it is a virtual Function
1631*cdf0e10cSrcweir     return GetValue( FUNIT_NONE );
1632*cdf0e10cSrcweir }
1633*cdf0e10cSrcweir 
1634*cdf0e10cSrcweir // -----------------------------------------------------------------------
1635*cdf0e10cSrcweir 
1636*cdf0e10cSrcweir void MetricFormatter::SetMin( sal_Int64 nNewMin, FieldUnit eInUnit )
1637*cdf0e10cSrcweir {
1638*cdf0e10cSrcweir     // Umrechnen auf gewuenschte Einheiten
1639*cdf0e10cSrcweir     NumericFormatter::SetMin( MetricField::ConvertValue( nNewMin, mnBaseValue, GetDecimalDigits(),
1640*cdf0e10cSrcweir                                                          eInUnit, meUnit ) );
1641*cdf0e10cSrcweir }
1642*cdf0e10cSrcweir 
1643*cdf0e10cSrcweir // -----------------------------------------------------------------------
1644*cdf0e10cSrcweir 
1645*cdf0e10cSrcweir sal_Int64 MetricFormatter::GetMin( FieldUnit eOutUnit ) const
1646*cdf0e10cSrcweir {
1647*cdf0e10cSrcweir     // Umrechnen auf gewuenschte Einheiten
1648*cdf0e10cSrcweir     return MetricField::ConvertValue( NumericFormatter::GetMin(), mnBaseValue,
1649*cdf0e10cSrcweir                                       GetDecimalDigits(), meUnit, eOutUnit );
1650*cdf0e10cSrcweir }
1651*cdf0e10cSrcweir 
1652*cdf0e10cSrcweir // -----------------------------------------------------------------------
1653*cdf0e10cSrcweir 
1654*cdf0e10cSrcweir void MetricFormatter::SetMax( sal_Int64 nNewMax, FieldUnit eInUnit )
1655*cdf0e10cSrcweir {
1656*cdf0e10cSrcweir     // Umrechnen auf gewuenschte Einheiten
1657*cdf0e10cSrcweir     NumericFormatter::SetMax( MetricField::ConvertValue( nNewMax, mnBaseValue, GetDecimalDigits(),
1658*cdf0e10cSrcweir                                                          eInUnit, meUnit ) );
1659*cdf0e10cSrcweir }
1660*cdf0e10cSrcweir 
1661*cdf0e10cSrcweir // -----------------------------------------------------------------------
1662*cdf0e10cSrcweir 
1663*cdf0e10cSrcweir sal_Int64 MetricFormatter::GetMax( FieldUnit eOutUnit ) const
1664*cdf0e10cSrcweir {
1665*cdf0e10cSrcweir     // Umrechnen auf gewuenschte Einheiten
1666*cdf0e10cSrcweir     return MetricField::ConvertValue( NumericFormatter::GetMax(), mnBaseValue,
1667*cdf0e10cSrcweir                                       GetDecimalDigits(), meUnit, eOutUnit );
1668*cdf0e10cSrcweir }
1669*cdf0e10cSrcweir 
1670*cdf0e10cSrcweir // -----------------------------------------------------------------------
1671*cdf0e10cSrcweir 
1672*cdf0e10cSrcweir void MetricFormatter::SetBaseValue( sal_Int64 nNewBase, FieldUnit eInUnit )
1673*cdf0e10cSrcweir {
1674*cdf0e10cSrcweir     mnBaseValue = MetricField::ConvertValue( nNewBase, mnBaseValue, GetDecimalDigits(),
1675*cdf0e10cSrcweir                                              eInUnit, meUnit );
1676*cdf0e10cSrcweir }
1677*cdf0e10cSrcweir 
1678*cdf0e10cSrcweir // -----------------------------------------------------------------------
1679*cdf0e10cSrcweir 
1680*cdf0e10cSrcweir sal_Int64 MetricFormatter::GetBaseValue( FieldUnit eOutUnit ) const
1681*cdf0e10cSrcweir {
1682*cdf0e10cSrcweir     // Umrechnen auf gewuenschte Einheiten
1683*cdf0e10cSrcweir     return MetricField::ConvertValue( mnBaseValue, mnBaseValue, GetDecimalDigits(),
1684*cdf0e10cSrcweir                                       meUnit, eOutUnit );
1685*cdf0e10cSrcweir }
1686*cdf0e10cSrcweir 
1687*cdf0e10cSrcweir // -----------------------------------------------------------------------
1688*cdf0e10cSrcweir 
1689*cdf0e10cSrcweir void MetricFormatter::Reformat()
1690*cdf0e10cSrcweir {
1691*cdf0e10cSrcweir     if ( !GetField() )
1692*cdf0e10cSrcweir         return;
1693*cdf0e10cSrcweir 
1694*cdf0e10cSrcweir     XubString aText = GetField()->GetText();
1695*cdf0e10cSrcweir     if ( meUnit == FUNIT_CUSTOM )
1696*cdf0e10cSrcweir         maCurUnitText = ImplMetricGetUnitText( aText );
1697*cdf0e10cSrcweir 
1698*cdf0e10cSrcweir     XubString aStr;
1699*cdf0e10cSrcweir     // caution: precision loss in double cast
1700*cdf0e10cSrcweir     double nTemp = (double)mnLastValue;
1701*cdf0e10cSrcweir     sal_Bool bOK = ImplMetricReformat( aText, nTemp, aStr );
1702*cdf0e10cSrcweir     mnLastValue = (sal_Int64)nTemp;
1703*cdf0e10cSrcweir 
1704*cdf0e10cSrcweir     if ( !bOK )
1705*cdf0e10cSrcweir         return;
1706*cdf0e10cSrcweir 
1707*cdf0e10cSrcweir     if ( aStr.Len() )
1708*cdf0e10cSrcweir     {
1709*cdf0e10cSrcweir         ImplSetText( aStr );
1710*cdf0e10cSrcweir         if ( meUnit == FUNIT_CUSTOM )
1711*cdf0e10cSrcweir             CustomConvert();
1712*cdf0e10cSrcweir     }
1713*cdf0e10cSrcweir     else
1714*cdf0e10cSrcweir         SetValue( mnLastValue );
1715*cdf0e10cSrcweir     maCurUnitText.Erase();
1716*cdf0e10cSrcweir }
1717*cdf0e10cSrcweir 
1718*cdf0e10cSrcweir // -----------------------------------------------------------------------
1719*cdf0e10cSrcweir 
1720*cdf0e10cSrcweir sal_Int64 MetricFormatter::GetCorrectedValue( FieldUnit eOutUnit ) const
1721*cdf0e10cSrcweir {
1722*cdf0e10cSrcweir     // Umrechnen auf gewuenschte Einheiten
1723*cdf0e10cSrcweir     return MetricField::ConvertValue( mnCorrectedValue, mnBaseValue, GetDecimalDigits(),
1724*cdf0e10cSrcweir                                       meUnit, eOutUnit );
1725*cdf0e10cSrcweir }
1726*cdf0e10cSrcweir 
1727*cdf0e10cSrcweir // -----------------------------------------------------------------------
1728*cdf0e10cSrcweir 
1729*cdf0e10cSrcweir MetricField::MetricField( Window* pParent, WinBits nWinStyle ) :
1730*cdf0e10cSrcweir     SpinField( pParent, nWinStyle )
1731*cdf0e10cSrcweir {
1732*cdf0e10cSrcweir     SetField( this );
1733*cdf0e10cSrcweir     Reformat();
1734*cdf0e10cSrcweir }
1735*cdf0e10cSrcweir 
1736*cdf0e10cSrcweir // -----------------------------------------------------------------------
1737*cdf0e10cSrcweir 
1738*cdf0e10cSrcweir MetricField::MetricField( Window* pParent, const ResId& rResId ) :
1739*cdf0e10cSrcweir     SpinField( WINDOW_METRICFIELD )
1740*cdf0e10cSrcweir {
1741*cdf0e10cSrcweir     rResId.SetRT( RSC_METRICFIELD );
1742*cdf0e10cSrcweir     WinBits nStyle = ImplInitRes( rResId ) ;
1743*cdf0e10cSrcweir     SpinField::ImplInit( pParent, nStyle );
1744*cdf0e10cSrcweir     SetField( this );
1745*cdf0e10cSrcweir     ImplLoadRes( rResId );
1746*cdf0e10cSrcweir 
1747*cdf0e10cSrcweir     if ( !(nStyle & WB_HIDE ) )
1748*cdf0e10cSrcweir         Show();
1749*cdf0e10cSrcweir }
1750*cdf0e10cSrcweir 
1751*cdf0e10cSrcweir // -----------------------------------------------------------------------
1752*cdf0e10cSrcweir 
1753*cdf0e10cSrcweir void MetricField::ImplLoadRes( const ResId& rResId )
1754*cdf0e10cSrcweir {
1755*cdf0e10cSrcweir     SpinField::ImplLoadRes( rResId );
1756*cdf0e10cSrcweir     MetricFormatter::ImplLoadRes( ResId( (RSHEADER_TYPE *)GetClassRes(), *rResId.GetResMgr() ) );
1757*cdf0e10cSrcweir 
1758*cdf0e10cSrcweir     sal_uLong      nMask = ReadLongRes();
1759*cdf0e10cSrcweir 
1760*cdf0e10cSrcweir     if ( METRICFIELD_FIRST & nMask )
1761*cdf0e10cSrcweir         mnFirst = ReadLongRes();
1762*cdf0e10cSrcweir 
1763*cdf0e10cSrcweir     if ( METRICFIELD_LAST & nMask )
1764*cdf0e10cSrcweir         mnLast = ReadLongRes();
1765*cdf0e10cSrcweir 
1766*cdf0e10cSrcweir     if ( METRICFIELD_SPINSIZE & nMask )
1767*cdf0e10cSrcweir         mnSpinSize = ReadLongRes();
1768*cdf0e10cSrcweir 
1769*cdf0e10cSrcweir     Reformat();
1770*cdf0e10cSrcweir }
1771*cdf0e10cSrcweir 
1772*cdf0e10cSrcweir // -----------------------------------------------------------------------
1773*cdf0e10cSrcweir 
1774*cdf0e10cSrcweir MetricField::~MetricField()
1775*cdf0e10cSrcweir {
1776*cdf0e10cSrcweir }
1777*cdf0e10cSrcweir 
1778*cdf0e10cSrcweir // -----------------------------------------------------------------------
1779*cdf0e10cSrcweir 
1780*cdf0e10cSrcweir void MetricField::SetFirst( sal_Int64 nNewFirst, FieldUnit eInUnit )
1781*cdf0e10cSrcweir {
1782*cdf0e10cSrcweir     // convert
1783*cdf0e10cSrcweir     nNewFirst = MetricField::ConvertValue( nNewFirst, mnBaseValue, GetDecimalDigits(),
1784*cdf0e10cSrcweir                                            eInUnit, meUnit );
1785*cdf0e10cSrcweir     mnFirst = nNewFirst;
1786*cdf0e10cSrcweir }
1787*cdf0e10cSrcweir 
1788*cdf0e10cSrcweir // -----------------------------------------------------------------------
1789*cdf0e10cSrcweir 
1790*cdf0e10cSrcweir sal_Int64 MetricField::GetFirst( FieldUnit eOutUnit ) const
1791*cdf0e10cSrcweir {
1792*cdf0e10cSrcweir     // convert
1793*cdf0e10cSrcweir     return MetricField::ConvertValue( mnFirst, mnBaseValue, GetDecimalDigits(),
1794*cdf0e10cSrcweir                                       meUnit, eOutUnit );
1795*cdf0e10cSrcweir }
1796*cdf0e10cSrcweir 
1797*cdf0e10cSrcweir // -----------------------------------------------------------------------
1798*cdf0e10cSrcweir 
1799*cdf0e10cSrcweir void MetricField::SetLast( sal_Int64 nNewLast, FieldUnit eInUnit )
1800*cdf0e10cSrcweir {
1801*cdf0e10cSrcweir     // Umrechnen
1802*cdf0e10cSrcweir     nNewLast = MetricField::ConvertValue( nNewLast, mnBaseValue, GetDecimalDigits(),
1803*cdf0e10cSrcweir                                           eInUnit, meUnit );
1804*cdf0e10cSrcweir     mnLast = nNewLast;
1805*cdf0e10cSrcweir }
1806*cdf0e10cSrcweir 
1807*cdf0e10cSrcweir // -----------------------------------------------------------------------
1808*cdf0e10cSrcweir 
1809*cdf0e10cSrcweir sal_Int64 MetricField::GetLast( FieldUnit eOutUnit ) const
1810*cdf0e10cSrcweir {
1811*cdf0e10cSrcweir     // Umrechnen
1812*cdf0e10cSrcweir     return MetricField::ConvertValue( mnLast, mnBaseValue, GetDecimalDigits(),
1813*cdf0e10cSrcweir                                       meUnit, eOutUnit );
1814*cdf0e10cSrcweir }
1815*cdf0e10cSrcweir 
1816*cdf0e10cSrcweir // -----------------------------------------------------------------------
1817*cdf0e10cSrcweir 
1818*cdf0e10cSrcweir long MetricField::PreNotify( NotifyEvent& rNEvt )
1819*cdf0e10cSrcweir {
1820*cdf0e10cSrcweir     if ( (rNEvt.GetType() == EVENT_KEYINPUT) && !rNEvt.GetKeyEvent()->GetKeyCode().IsMod2() )
1821*cdf0e10cSrcweir     {
1822*cdf0e10cSrcweir         if ( ImplMetricProcessKeyInput( GetField(), *rNEvt.GetKeyEvent(), IsStrictFormat(), IsUseThousandSep(), ImplGetLocaleDataWrapper() ) )
1823*cdf0e10cSrcweir             return 1;
1824*cdf0e10cSrcweir     }
1825*cdf0e10cSrcweir 
1826*cdf0e10cSrcweir     return SpinField::PreNotify( rNEvt );
1827*cdf0e10cSrcweir }
1828*cdf0e10cSrcweir 
1829*cdf0e10cSrcweir // -----------------------------------------------------------------------
1830*cdf0e10cSrcweir 
1831*cdf0e10cSrcweir long MetricField::Notify( NotifyEvent& rNEvt )
1832*cdf0e10cSrcweir {
1833*cdf0e10cSrcweir     if ( rNEvt.GetType() == EVENT_GETFOCUS )
1834*cdf0e10cSrcweir         MarkToBeReformatted( sal_False );
1835*cdf0e10cSrcweir     else if ( rNEvt.GetType() == EVENT_LOSEFOCUS )
1836*cdf0e10cSrcweir     {
1837*cdf0e10cSrcweir         if ( MustBeReformatted() && (GetText().Len() || !IsEmptyFieldValueEnabled()) )
1838*cdf0e10cSrcweir             Reformat();
1839*cdf0e10cSrcweir     }
1840*cdf0e10cSrcweir 
1841*cdf0e10cSrcweir     return SpinField::Notify( rNEvt );
1842*cdf0e10cSrcweir }
1843*cdf0e10cSrcweir 
1844*cdf0e10cSrcweir // -----------------------------------------------------------------------
1845*cdf0e10cSrcweir 
1846*cdf0e10cSrcweir void MetricField::DataChanged( const DataChangedEvent& rDCEvt )
1847*cdf0e10cSrcweir {
1848*cdf0e10cSrcweir     SpinField::DataChanged( rDCEvt );
1849*cdf0e10cSrcweir 
1850*cdf0e10cSrcweir     if ( (rDCEvt.GetType() == DATACHANGED_SETTINGS) && (rDCEvt.GetFlags() & SETTINGS_LOCALE) )
1851*cdf0e10cSrcweir     {
1852*cdf0e10cSrcweir         String sOldDecSep = ImplGetLocaleDataWrapper().getNumDecimalSep();
1853*cdf0e10cSrcweir         String sOldThSep = ImplGetLocaleDataWrapper().getNumThousandSep();
1854*cdf0e10cSrcweir         if ( IsDefaultLocale() )
1855*cdf0e10cSrcweir             ImplGetLocaleDataWrapper().setLocale( GetSettings().GetLocale() );
1856*cdf0e10cSrcweir         String sNewDecSep = ImplGetLocaleDataWrapper().getNumDecimalSep();
1857*cdf0e10cSrcweir         String sNewThSep = ImplGetLocaleDataWrapper().getNumThousandSep();
1858*cdf0e10cSrcweir         ImplUpdateSeparators( sOldDecSep, sNewDecSep, sOldThSep, sNewThSep, this );
1859*cdf0e10cSrcweir         ReformatAll();
1860*cdf0e10cSrcweir     }
1861*cdf0e10cSrcweir }
1862*cdf0e10cSrcweir 
1863*cdf0e10cSrcweir // -----------------------------------------------------------------------
1864*cdf0e10cSrcweir 
1865*cdf0e10cSrcweir void MetricField::Modify()
1866*cdf0e10cSrcweir {
1867*cdf0e10cSrcweir     MarkToBeReformatted( sal_True );
1868*cdf0e10cSrcweir     SpinField::Modify();
1869*cdf0e10cSrcweir }
1870*cdf0e10cSrcweir 
1871*cdf0e10cSrcweir // -----------------------------------------------------------------------
1872*cdf0e10cSrcweir 
1873*cdf0e10cSrcweir void MetricField::Up()
1874*cdf0e10cSrcweir {
1875*cdf0e10cSrcweir     FieldUp();
1876*cdf0e10cSrcweir     SpinField::Up();
1877*cdf0e10cSrcweir }
1878*cdf0e10cSrcweir 
1879*cdf0e10cSrcweir // -----------------------------------------------------------------------
1880*cdf0e10cSrcweir 
1881*cdf0e10cSrcweir void MetricField::Down()
1882*cdf0e10cSrcweir {
1883*cdf0e10cSrcweir     FieldDown();
1884*cdf0e10cSrcweir     SpinField::Down();
1885*cdf0e10cSrcweir }
1886*cdf0e10cSrcweir 
1887*cdf0e10cSrcweir // -----------------------------------------------------------------------
1888*cdf0e10cSrcweir 
1889*cdf0e10cSrcweir void MetricField::First()
1890*cdf0e10cSrcweir {
1891*cdf0e10cSrcweir     FieldFirst();
1892*cdf0e10cSrcweir     SpinField::First();
1893*cdf0e10cSrcweir }
1894*cdf0e10cSrcweir 
1895*cdf0e10cSrcweir // -----------------------------------------------------------------------
1896*cdf0e10cSrcweir 
1897*cdf0e10cSrcweir void MetricField::Last()
1898*cdf0e10cSrcweir {
1899*cdf0e10cSrcweir     FieldLast();
1900*cdf0e10cSrcweir     SpinField::Last();
1901*cdf0e10cSrcweir }
1902*cdf0e10cSrcweir 
1903*cdf0e10cSrcweir // -----------------------------------------------------------------------
1904*cdf0e10cSrcweir 
1905*cdf0e10cSrcweir void MetricField::CustomConvert()
1906*cdf0e10cSrcweir {
1907*cdf0e10cSrcweir     maCustomConvertLink.Call( this );
1908*cdf0e10cSrcweir }
1909*cdf0e10cSrcweir 
1910*cdf0e10cSrcweir // -----------------------------------------------------------------------
1911*cdf0e10cSrcweir 
1912*cdf0e10cSrcweir MetricBox::MetricBox( Window* pParent, WinBits nWinStyle ) :
1913*cdf0e10cSrcweir     ComboBox( pParent, nWinStyle )
1914*cdf0e10cSrcweir {
1915*cdf0e10cSrcweir     SetField( this );
1916*cdf0e10cSrcweir     Reformat();
1917*cdf0e10cSrcweir }
1918*cdf0e10cSrcweir 
1919*cdf0e10cSrcweir // -----------------------------------------------------------------------
1920*cdf0e10cSrcweir 
1921*cdf0e10cSrcweir MetricBox::MetricBox( Window* pParent, const ResId& rResId ) :
1922*cdf0e10cSrcweir     ComboBox( WINDOW_METRICBOX )
1923*cdf0e10cSrcweir {
1924*cdf0e10cSrcweir     rResId.SetRT( RSC_METRICBOX );
1925*cdf0e10cSrcweir     WinBits nStyle = ImplInitRes( rResId );
1926*cdf0e10cSrcweir     ComboBox::ImplInit( pParent, nStyle );
1927*cdf0e10cSrcweir     SetField( this );
1928*cdf0e10cSrcweir     Reformat();
1929*cdf0e10cSrcweir     ComboBox::ImplLoadRes( rResId );
1930*cdf0e10cSrcweir     MetricFormatter::ImplLoadRes( ResId( (RSHEADER_TYPE *)GetClassRes(), *rResId.GetResMgr() ) );
1931*cdf0e10cSrcweir 
1932*cdf0e10cSrcweir     if ( !(nStyle & WB_HIDE ) )
1933*cdf0e10cSrcweir         Show();
1934*cdf0e10cSrcweir }
1935*cdf0e10cSrcweir 
1936*cdf0e10cSrcweir // -----------------------------------------------------------------------
1937*cdf0e10cSrcweir 
1938*cdf0e10cSrcweir MetricBox::~MetricBox()
1939*cdf0e10cSrcweir {
1940*cdf0e10cSrcweir }
1941*cdf0e10cSrcweir 
1942*cdf0e10cSrcweir // -----------------------------------------------------------------------
1943*cdf0e10cSrcweir 
1944*cdf0e10cSrcweir long MetricBox::PreNotify( NotifyEvent& rNEvt )
1945*cdf0e10cSrcweir {
1946*cdf0e10cSrcweir     if ( (rNEvt.GetType() == EVENT_KEYINPUT) && !rNEvt.GetKeyEvent()->GetKeyCode().IsMod2()  )
1947*cdf0e10cSrcweir     {
1948*cdf0e10cSrcweir         if ( ImplMetricProcessKeyInput( GetField(), *rNEvt.GetKeyEvent(), IsStrictFormat(), IsUseThousandSep(), ImplGetLocaleDataWrapper() ) )
1949*cdf0e10cSrcweir             return 1;
1950*cdf0e10cSrcweir     }
1951*cdf0e10cSrcweir 
1952*cdf0e10cSrcweir     return ComboBox::PreNotify( rNEvt );
1953*cdf0e10cSrcweir }
1954*cdf0e10cSrcweir 
1955*cdf0e10cSrcweir // -----------------------------------------------------------------------
1956*cdf0e10cSrcweir 
1957*cdf0e10cSrcweir long MetricBox::Notify( NotifyEvent& rNEvt )
1958*cdf0e10cSrcweir {
1959*cdf0e10cSrcweir     if ( rNEvt.GetType() == EVENT_GETFOCUS )
1960*cdf0e10cSrcweir         MarkToBeReformatted( sal_False );
1961*cdf0e10cSrcweir     else if ( rNEvt.GetType() == EVENT_LOSEFOCUS )
1962*cdf0e10cSrcweir     {
1963*cdf0e10cSrcweir         if ( MustBeReformatted() && (GetText().Len() || !IsEmptyFieldValueEnabled()) )
1964*cdf0e10cSrcweir             Reformat();
1965*cdf0e10cSrcweir     }
1966*cdf0e10cSrcweir 
1967*cdf0e10cSrcweir     return ComboBox::Notify( rNEvt );
1968*cdf0e10cSrcweir }
1969*cdf0e10cSrcweir 
1970*cdf0e10cSrcweir // -----------------------------------------------------------------------
1971*cdf0e10cSrcweir 
1972*cdf0e10cSrcweir void MetricBox::DataChanged( const DataChangedEvent& rDCEvt )
1973*cdf0e10cSrcweir {
1974*cdf0e10cSrcweir     ComboBox::DataChanged( rDCEvt );
1975*cdf0e10cSrcweir 
1976*cdf0e10cSrcweir     if ( (rDCEvt.GetType() == DATACHANGED_SETTINGS) && (rDCEvt.GetFlags() & SETTINGS_LOCALE) )
1977*cdf0e10cSrcweir     {
1978*cdf0e10cSrcweir         String sOldDecSep = ImplGetLocaleDataWrapper().getNumDecimalSep();
1979*cdf0e10cSrcweir         String sOldThSep = ImplGetLocaleDataWrapper().getNumThousandSep();
1980*cdf0e10cSrcweir         if ( IsDefaultLocale() )
1981*cdf0e10cSrcweir             ImplGetLocaleDataWrapper().setLocale( GetSettings().GetLocale() );
1982*cdf0e10cSrcweir         String sNewDecSep = ImplGetLocaleDataWrapper().getNumDecimalSep();
1983*cdf0e10cSrcweir         String sNewThSep = ImplGetLocaleDataWrapper().getNumThousandSep();
1984*cdf0e10cSrcweir         ImplUpdateSeparators( sOldDecSep, sNewDecSep, sOldThSep, sNewThSep, this );
1985*cdf0e10cSrcweir         ReformatAll();
1986*cdf0e10cSrcweir     }
1987*cdf0e10cSrcweir }
1988*cdf0e10cSrcweir 
1989*cdf0e10cSrcweir // -----------------------------------------------------------------------
1990*cdf0e10cSrcweir 
1991*cdf0e10cSrcweir void MetricBox::Modify()
1992*cdf0e10cSrcweir {
1993*cdf0e10cSrcweir     MarkToBeReformatted( sal_True );
1994*cdf0e10cSrcweir     ComboBox::Modify();
1995*cdf0e10cSrcweir }
1996*cdf0e10cSrcweir 
1997*cdf0e10cSrcweir // -----------------------------------------------------------------------
1998*cdf0e10cSrcweir 
1999*cdf0e10cSrcweir void MetricBox::ReformatAll()
2000*cdf0e10cSrcweir {
2001*cdf0e10cSrcweir     double nValue;
2002*cdf0e10cSrcweir     XubString aStr;
2003*cdf0e10cSrcweir     SetUpdateMode( sal_False );
2004*cdf0e10cSrcweir     sal_uInt16 nEntryCount = GetEntryCount();
2005*cdf0e10cSrcweir     for ( sal_uInt16 i=0; i < nEntryCount; i++ )
2006*cdf0e10cSrcweir     {
2007*cdf0e10cSrcweir         ImplMetricReformat( GetEntry( i ), nValue, aStr );
2008*cdf0e10cSrcweir         RemoveEntry( i );
2009*cdf0e10cSrcweir         InsertEntry( aStr, i );
2010*cdf0e10cSrcweir     }
2011*cdf0e10cSrcweir     MetricFormatter::Reformat();
2012*cdf0e10cSrcweir     SetUpdateMode( sal_True );
2013*cdf0e10cSrcweir }
2014*cdf0e10cSrcweir 
2015*cdf0e10cSrcweir // -----------------------------------------------------------------------
2016*cdf0e10cSrcweir 
2017*cdf0e10cSrcweir void MetricBox::CustomConvert()
2018*cdf0e10cSrcweir {
2019*cdf0e10cSrcweir     maCustomConvertLink.Call( this );
2020*cdf0e10cSrcweir }
2021*cdf0e10cSrcweir 
2022*cdf0e10cSrcweir // -----------------------------------------------------------------------
2023*cdf0e10cSrcweir 
2024*cdf0e10cSrcweir void MetricBox::InsertValue( sal_Int64 nValue, FieldUnit eInUnit, sal_uInt16 nPos )
2025*cdf0e10cSrcweir {
2026*cdf0e10cSrcweir     // Umrechnen auf eingestellte Einheiten
2027*cdf0e10cSrcweir     nValue = MetricField::ConvertValue( nValue, mnBaseValue, GetDecimalDigits(),
2028*cdf0e10cSrcweir                                         eInUnit, meUnit );
2029*cdf0e10cSrcweir     ComboBox::InsertEntry( CreateFieldText( nValue ), nPos );
2030*cdf0e10cSrcweir }
2031*cdf0e10cSrcweir 
2032*cdf0e10cSrcweir // -----------------------------------------------------------------------
2033*cdf0e10cSrcweir 
2034*cdf0e10cSrcweir void MetricBox::RemoveValue( sal_Int64 nValue, FieldUnit eInUnit )
2035*cdf0e10cSrcweir {
2036*cdf0e10cSrcweir     // Umrechnen auf eingestellte Einheiten
2037*cdf0e10cSrcweir     nValue = MetricField::ConvertValue( nValue, mnBaseValue, GetDecimalDigits(),
2038*cdf0e10cSrcweir                                         eInUnit, meUnit );
2039*cdf0e10cSrcweir     ComboBox::RemoveEntry( CreateFieldText( nValue ) );
2040*cdf0e10cSrcweir }
2041*cdf0e10cSrcweir 
2042*cdf0e10cSrcweir // -----------------------------------------------------------------------
2043*cdf0e10cSrcweir 
2044*cdf0e10cSrcweir sal_Int64 MetricBox::GetValue( sal_uInt16 nPos, FieldUnit eOutUnit ) const
2045*cdf0e10cSrcweir {
2046*cdf0e10cSrcweir     double nValue = 0;
2047*cdf0e10cSrcweir     ImplMetricGetValue( ComboBox::GetEntry( nPos ), nValue, mnBaseValue,
2048*cdf0e10cSrcweir                         GetDecimalDigits(), ImplGetLocaleDataWrapper(), meUnit );
2049*cdf0e10cSrcweir 
2050*cdf0e10cSrcweir     // Umrechnen auf eingestellte Einheiten
2051*cdf0e10cSrcweir     sal_Int64 nRetValue = MetricField::ConvertValue( (sal_Int64)nValue, mnBaseValue, GetDecimalDigits(),
2052*cdf0e10cSrcweir                                                      meUnit, eOutUnit );
2053*cdf0e10cSrcweir 
2054*cdf0e10cSrcweir     return nRetValue;
2055*cdf0e10cSrcweir }
2056*cdf0e10cSrcweir 
2057*cdf0e10cSrcweir // -----------------------------------------------------------------------
2058*cdf0e10cSrcweir 
2059*cdf0e10cSrcweir sal_uInt16 MetricBox::GetValuePos( sal_Int64 nValue, FieldUnit eInUnit ) const
2060*cdf0e10cSrcweir {
2061*cdf0e10cSrcweir     // Umrechnen auf eingestellte Einheiten
2062*cdf0e10cSrcweir     nValue = MetricField::ConvertValue( nValue, mnBaseValue, GetDecimalDigits(),
2063*cdf0e10cSrcweir                                         eInUnit, meUnit );
2064*cdf0e10cSrcweir     return ComboBox::GetEntryPos( CreateFieldText( nValue ) );
2065*cdf0e10cSrcweir }
2066*cdf0e10cSrcweir 
2067*cdf0e10cSrcweir // -----------------------------------------------------------------------
2068*cdf0e10cSrcweir 
2069*cdf0e10cSrcweir sal_Int64 MetricBox::GetValue( FieldUnit eOutUnit ) const
2070*cdf0e10cSrcweir {
2071*cdf0e10cSrcweir     // Implementation not inline, because it is a virtual Function
2072*cdf0e10cSrcweir     return MetricFormatter::GetValue( eOutUnit );
2073*cdf0e10cSrcweir }
2074*cdf0e10cSrcweir 
2075*cdf0e10cSrcweir // -----------------------------------------------------------------------
2076*cdf0e10cSrcweir 
2077*cdf0e10cSrcweir sal_Int64 MetricBox::GetValue() const
2078*cdf0e10cSrcweir {
2079*cdf0e10cSrcweir     // Implementation not inline, because it is a virtual Function
2080*cdf0e10cSrcweir     return GetValue( FUNIT_NONE );
2081*cdf0e10cSrcweir }
2082*cdf0e10cSrcweir 
2083*cdf0e10cSrcweir // -----------------------------------------------------------------------
2084*cdf0e10cSrcweir 
2085*cdf0e10cSrcweir static sal_Bool ImplCurrencyProcessKeyInput( Edit* pEdit, const KeyEvent& rKEvt,
2086*cdf0e10cSrcweir                                          sal_Bool, sal_Bool bUseThousandSep, const LocaleDataWrapper& rWrapper )
2087*cdf0e10cSrcweir {
2088*cdf0e10cSrcweir     // Es gibt hier kein sinnvolles StrictFormat, also alle
2089*cdf0e10cSrcweir     // Zeichen erlauben
2090*cdf0e10cSrcweir     return ImplNumericProcessKeyInput( pEdit, rKEvt, sal_False, bUseThousandSep, rWrapper );
2091*cdf0e10cSrcweir }
2092*cdf0e10cSrcweir 
2093*cdf0e10cSrcweir // -----------------------------------------------------------------------
2094*cdf0e10cSrcweir 
2095*cdf0e10cSrcweir inline sal_Bool ImplCurrencyGetValue( const XubString& rStr, double& rValue,
2096*cdf0e10cSrcweir                                   sal_uInt16 nDecDigits, const LocaleDataWrapper& rWrapper )
2097*cdf0e10cSrcweir {
2098*cdf0e10cSrcweir     // Zahlenwert holen
2099*cdf0e10cSrcweir     return ImplNumericGetValue( rStr, rValue, nDecDigits, rWrapper, sal_True );
2100*cdf0e10cSrcweir }
2101*cdf0e10cSrcweir 
2102*cdf0e10cSrcweir // -----------------------------------------------------------------------
2103*cdf0e10cSrcweir 
2104*cdf0e10cSrcweir sal_Bool CurrencyFormatter::ImplCurrencyReformat( const XubString& rStr,
2105*cdf0e10cSrcweir                                               XubString& rOutStr )
2106*cdf0e10cSrcweir {
2107*cdf0e10cSrcweir     double nValue;
2108*cdf0e10cSrcweir     if ( !ImplNumericGetValue( rStr, nValue, GetDecimalDigits(), ImplGetLocaleDataWrapper(), sal_True ) )
2109*cdf0e10cSrcweir         return sal_True;
2110*cdf0e10cSrcweir     else
2111*cdf0e10cSrcweir     {
2112*cdf0e10cSrcweir         double nTempVal = nValue;
2113*cdf0e10cSrcweir         // caution: precision loss in double cast
2114*cdf0e10cSrcweir         if ( nTempVal > GetMax() )
2115*cdf0e10cSrcweir             nTempVal = (double)GetMax();
2116*cdf0e10cSrcweir         else if ( nTempVal < GetMin())
2117*cdf0e10cSrcweir             nTempVal = (double)GetMin();
2118*cdf0e10cSrcweir 
2119*cdf0e10cSrcweir         if ( GetErrorHdl().IsSet() && (nValue != nTempVal) )
2120*cdf0e10cSrcweir         {
2121*cdf0e10cSrcweir             mnCorrectedValue = (sal_Int64)nTempVal;
2122*cdf0e10cSrcweir             if ( !GetErrorHdl().Call( this ) )
2123*cdf0e10cSrcweir             {
2124*cdf0e10cSrcweir                 mnCorrectedValue = 0;
2125*cdf0e10cSrcweir                 return sal_False;
2126*cdf0e10cSrcweir             }
2127*cdf0e10cSrcweir             else
2128*cdf0e10cSrcweir                 mnCorrectedValue = 0;
2129*cdf0e10cSrcweir         }
2130*cdf0e10cSrcweir 
2131*cdf0e10cSrcweir         rOutStr = CreateFieldText( (long)nTempVal );
2132*cdf0e10cSrcweir         return sal_True;
2133*cdf0e10cSrcweir     }
2134*cdf0e10cSrcweir }
2135*cdf0e10cSrcweir 
2136*cdf0e10cSrcweir // -----------------------------------------------------------------------
2137*cdf0e10cSrcweir 
2138*cdf0e10cSrcweir inline void CurrencyFormatter::ImplInit()
2139*cdf0e10cSrcweir {
2140*cdf0e10cSrcweir     mnType = FORMAT_CURRENCY;
2141*cdf0e10cSrcweir }
2142*cdf0e10cSrcweir 
2143*cdf0e10cSrcweir // -----------------------------------------------------------------------
2144*cdf0e10cSrcweir 
2145*cdf0e10cSrcweir CurrencyFormatter::CurrencyFormatter()
2146*cdf0e10cSrcweir {
2147*cdf0e10cSrcweir     ImplInit();
2148*cdf0e10cSrcweir }
2149*cdf0e10cSrcweir 
2150*cdf0e10cSrcweir // -----------------------------------------------------------------------
2151*cdf0e10cSrcweir 
2152*cdf0e10cSrcweir CurrencyFormatter::~CurrencyFormatter()
2153*cdf0e10cSrcweir {
2154*cdf0e10cSrcweir }
2155*cdf0e10cSrcweir 
2156*cdf0e10cSrcweir // -----------------------------------------------------------------------
2157*cdf0e10cSrcweir 
2158*cdf0e10cSrcweir void CurrencyFormatter::SetCurrencySymbol( const String& rStr )
2159*cdf0e10cSrcweir {
2160*cdf0e10cSrcweir     maCurrencySymbol= rStr;
2161*cdf0e10cSrcweir     ReformatAll();
2162*cdf0e10cSrcweir }
2163*cdf0e10cSrcweir 
2164*cdf0e10cSrcweir // -----------------------------------------------------------------------
2165*cdf0e10cSrcweir 
2166*cdf0e10cSrcweir String CurrencyFormatter::GetCurrencySymbol() const
2167*cdf0e10cSrcweir {
2168*cdf0e10cSrcweir     return maCurrencySymbol.Len() ? maCurrencySymbol : ImplGetLocaleDataWrapper().getCurrSymbol();
2169*cdf0e10cSrcweir }
2170*cdf0e10cSrcweir 
2171*cdf0e10cSrcweir // -----------------------------------------------------------------------
2172*cdf0e10cSrcweir 
2173*cdf0e10cSrcweir void CurrencyFormatter::SetValue( sal_Int64 nNewValue )
2174*cdf0e10cSrcweir {
2175*cdf0e10cSrcweir     SetUserValue( nNewValue );
2176*cdf0e10cSrcweir     mnFieldValue = mnLastValue;
2177*cdf0e10cSrcweir     SetEmptyFieldValueData( sal_False );
2178*cdf0e10cSrcweir }
2179*cdf0e10cSrcweir 
2180*cdf0e10cSrcweir // -----------------------------------------------------------------------
2181*cdf0e10cSrcweir 
2182*cdf0e10cSrcweir XubString CurrencyFormatter::CreateFieldText( sal_Int64 nValue ) const
2183*cdf0e10cSrcweir {
2184*cdf0e10cSrcweir     return ImplGetLocaleDataWrapper().getCurr( nValue, GetDecimalDigits(), GetCurrencySymbol(), IsUseThousandSep() );
2185*cdf0e10cSrcweir }
2186*cdf0e10cSrcweir 
2187*cdf0e10cSrcweir // -----------------------------------------------------------------------
2188*cdf0e10cSrcweir 
2189*cdf0e10cSrcweir sal_Int64 CurrencyFormatter::GetValue() const
2190*cdf0e10cSrcweir {
2191*cdf0e10cSrcweir     if ( !GetField() )
2192*cdf0e10cSrcweir         return 0;
2193*cdf0e10cSrcweir 
2194*cdf0e10cSrcweir     double nTempValue;
2195*cdf0e10cSrcweir     if ( ImplCurrencyGetValue( GetField()->GetText(), nTempValue, GetDecimalDigits(), ImplGetLocaleDataWrapper() ) )
2196*cdf0e10cSrcweir     {
2197*cdf0e10cSrcweir         // caution: precision loss in double cast
2198*cdf0e10cSrcweir         if ( nTempValue > mnMax )
2199*cdf0e10cSrcweir             nTempValue = (double)mnMax;
2200*cdf0e10cSrcweir         else if ( nTempValue < mnMin )
2201*cdf0e10cSrcweir             nTempValue = (double)mnMin;
2202*cdf0e10cSrcweir         return (sal_Int64)nTempValue;
2203*cdf0e10cSrcweir     }
2204*cdf0e10cSrcweir     else
2205*cdf0e10cSrcweir         return mnLastValue;
2206*cdf0e10cSrcweir }
2207*cdf0e10cSrcweir 
2208*cdf0e10cSrcweir // -----------------------------------------------------------------------
2209*cdf0e10cSrcweir 
2210*cdf0e10cSrcweir void CurrencyFormatter::Reformat()
2211*cdf0e10cSrcweir {
2212*cdf0e10cSrcweir     if ( !GetField() )
2213*cdf0e10cSrcweir         return;
2214*cdf0e10cSrcweir 
2215*cdf0e10cSrcweir     XubString aStr;
2216*cdf0e10cSrcweir     sal_Bool bOK = ImplCurrencyReformat( GetField()->GetText(), aStr );
2217*cdf0e10cSrcweir     if ( !bOK )
2218*cdf0e10cSrcweir         return;
2219*cdf0e10cSrcweir 
2220*cdf0e10cSrcweir     if ( aStr.Len() )
2221*cdf0e10cSrcweir     {
2222*cdf0e10cSrcweir         ImplSetText( aStr  );
2223*cdf0e10cSrcweir         // caution: precision loss in double cast
2224*cdf0e10cSrcweir         double nTemp = (double)mnLastValue;
2225*cdf0e10cSrcweir         ImplCurrencyGetValue( aStr, nTemp, GetDecimalDigits(), ImplGetLocaleDataWrapper() );
2226*cdf0e10cSrcweir         mnLastValue = (sal_Int64)nTemp;
2227*cdf0e10cSrcweir     }
2228*cdf0e10cSrcweir     else
2229*cdf0e10cSrcweir         SetValue( mnLastValue );
2230*cdf0e10cSrcweir }
2231*cdf0e10cSrcweir 
2232*cdf0e10cSrcweir // -----------------------------------------------------------------------
2233*cdf0e10cSrcweir 
2234*cdf0e10cSrcweir CurrencyField::CurrencyField( Window* pParent, WinBits nWinStyle ) :
2235*cdf0e10cSrcweir     SpinField( pParent, nWinStyle )
2236*cdf0e10cSrcweir {
2237*cdf0e10cSrcweir     SetField( this );
2238*cdf0e10cSrcweir     Reformat();
2239*cdf0e10cSrcweir }
2240*cdf0e10cSrcweir 
2241*cdf0e10cSrcweir // -----------------------------------------------------------------------
2242*cdf0e10cSrcweir 
2243*cdf0e10cSrcweir CurrencyField::CurrencyField( Window* pParent, const ResId& rResId ) :
2244*cdf0e10cSrcweir     SpinField( WINDOW_CURRENCYFIELD )
2245*cdf0e10cSrcweir {
2246*cdf0e10cSrcweir     rResId.SetRT( RSC_CURRENCYFIELD );
2247*cdf0e10cSrcweir     WinBits nStyle = ImplInitRes( rResId );
2248*cdf0e10cSrcweir     SpinField::ImplInit( pParent, nStyle);
2249*cdf0e10cSrcweir     SetField( this );
2250*cdf0e10cSrcweir     ImplLoadRes( rResId );
2251*cdf0e10cSrcweir 
2252*cdf0e10cSrcweir     if ( !(nStyle & WB_HIDE ) )
2253*cdf0e10cSrcweir         Show();
2254*cdf0e10cSrcweir }
2255*cdf0e10cSrcweir 
2256*cdf0e10cSrcweir // -----------------------------------------------------------------------
2257*cdf0e10cSrcweir 
2258*cdf0e10cSrcweir void CurrencyField::ImplLoadRes( const ResId& rResId )
2259*cdf0e10cSrcweir {
2260*cdf0e10cSrcweir     SpinField::ImplLoadRes( rResId );
2261*cdf0e10cSrcweir     CurrencyFormatter::ImplLoadRes( ResId( (RSHEADER_TYPE *)GetClassRes(), *rResId.GetResMgr() ) );
2262*cdf0e10cSrcweir 
2263*cdf0e10cSrcweir     sal_uLong      nMask = ReadLongRes();
2264*cdf0e10cSrcweir 
2265*cdf0e10cSrcweir     if ( CURRENCYFIELD_FIRST & nMask )
2266*cdf0e10cSrcweir         mnFirst = ReadLongRes();
2267*cdf0e10cSrcweir 
2268*cdf0e10cSrcweir     if ( CURRENCYFIELD_LAST & nMask )
2269*cdf0e10cSrcweir         mnLast = ReadLongRes();
2270*cdf0e10cSrcweir 
2271*cdf0e10cSrcweir     if ( CURRENCYFIELD_SPINSIZE & nMask )
2272*cdf0e10cSrcweir         mnSpinSize = ReadLongRes();
2273*cdf0e10cSrcweir 
2274*cdf0e10cSrcweir     Reformat();
2275*cdf0e10cSrcweir }
2276*cdf0e10cSrcweir 
2277*cdf0e10cSrcweir // -----------------------------------------------------------------------
2278*cdf0e10cSrcweir 
2279*cdf0e10cSrcweir CurrencyField::~CurrencyField()
2280*cdf0e10cSrcweir {
2281*cdf0e10cSrcweir }
2282*cdf0e10cSrcweir 
2283*cdf0e10cSrcweir // -----------------------------------------------------------------------
2284*cdf0e10cSrcweir 
2285*cdf0e10cSrcweir long CurrencyField::PreNotify( NotifyEvent& rNEvt )
2286*cdf0e10cSrcweir {
2287*cdf0e10cSrcweir     if ( (rNEvt.GetType() == EVENT_KEYINPUT) && !rNEvt.GetKeyEvent()->GetKeyCode().IsMod2() )
2288*cdf0e10cSrcweir     {
2289*cdf0e10cSrcweir         if ( ImplCurrencyProcessKeyInput( GetField(), *rNEvt.GetKeyEvent(), IsStrictFormat(), IsUseThousandSep(), ImplGetLocaleDataWrapper() ) )
2290*cdf0e10cSrcweir             return 1;
2291*cdf0e10cSrcweir     }
2292*cdf0e10cSrcweir 
2293*cdf0e10cSrcweir     return SpinField::PreNotify( rNEvt );
2294*cdf0e10cSrcweir }
2295*cdf0e10cSrcweir 
2296*cdf0e10cSrcweir // -----------------------------------------------------------------------
2297*cdf0e10cSrcweir 
2298*cdf0e10cSrcweir long CurrencyField::Notify( NotifyEvent& rNEvt )
2299*cdf0e10cSrcweir {
2300*cdf0e10cSrcweir     if ( rNEvt.GetType() == EVENT_GETFOCUS )
2301*cdf0e10cSrcweir         MarkToBeReformatted( sal_False );
2302*cdf0e10cSrcweir     else if ( rNEvt.GetType() == EVENT_LOSEFOCUS )
2303*cdf0e10cSrcweir     {
2304*cdf0e10cSrcweir         if ( MustBeReformatted() && (GetText().Len() || !IsEmptyFieldValueEnabled()) )
2305*cdf0e10cSrcweir             Reformat();
2306*cdf0e10cSrcweir     }
2307*cdf0e10cSrcweir 
2308*cdf0e10cSrcweir     return SpinField::Notify( rNEvt );
2309*cdf0e10cSrcweir }
2310*cdf0e10cSrcweir 
2311*cdf0e10cSrcweir // -----------------------------------------------------------------------
2312*cdf0e10cSrcweir 
2313*cdf0e10cSrcweir void CurrencyField::DataChanged( const DataChangedEvent& rDCEvt )
2314*cdf0e10cSrcweir {
2315*cdf0e10cSrcweir     SpinField::DataChanged( rDCEvt );
2316*cdf0e10cSrcweir 
2317*cdf0e10cSrcweir     if ( (rDCEvt.GetType() == DATACHANGED_SETTINGS) && (rDCEvt.GetFlags() & SETTINGS_LOCALE) )
2318*cdf0e10cSrcweir     {
2319*cdf0e10cSrcweir         String sOldDecSep = ImplGetLocaleDataWrapper().getNumDecimalSep();
2320*cdf0e10cSrcweir         String sOldThSep = ImplGetLocaleDataWrapper().getNumThousandSep();
2321*cdf0e10cSrcweir         if ( IsDefaultLocale() )
2322*cdf0e10cSrcweir             ImplGetLocaleDataWrapper().setLocale( GetSettings().GetLocale() );
2323*cdf0e10cSrcweir         String sNewDecSep = ImplGetLocaleDataWrapper().getNumDecimalSep();
2324*cdf0e10cSrcweir         String sNewThSep = ImplGetLocaleDataWrapper().getNumThousandSep();
2325*cdf0e10cSrcweir         ImplUpdateSeparators( sOldDecSep, sNewDecSep, sOldThSep, sNewThSep, this );
2326*cdf0e10cSrcweir         ReformatAll();
2327*cdf0e10cSrcweir     }
2328*cdf0e10cSrcweir }
2329*cdf0e10cSrcweir 
2330*cdf0e10cSrcweir // -----------------------------------------------------------------------
2331*cdf0e10cSrcweir 
2332*cdf0e10cSrcweir void CurrencyField::Modify()
2333*cdf0e10cSrcweir {
2334*cdf0e10cSrcweir     MarkToBeReformatted( sal_True );
2335*cdf0e10cSrcweir     SpinField::Modify();
2336*cdf0e10cSrcweir }
2337*cdf0e10cSrcweir 
2338*cdf0e10cSrcweir // -----------------------------------------------------------------------
2339*cdf0e10cSrcweir 
2340*cdf0e10cSrcweir void CurrencyField::Up()
2341*cdf0e10cSrcweir {
2342*cdf0e10cSrcweir     FieldUp();
2343*cdf0e10cSrcweir     SpinField::Up();
2344*cdf0e10cSrcweir }
2345*cdf0e10cSrcweir 
2346*cdf0e10cSrcweir // -----------------------------------------------------------------------
2347*cdf0e10cSrcweir 
2348*cdf0e10cSrcweir void CurrencyField::Down()
2349*cdf0e10cSrcweir {
2350*cdf0e10cSrcweir     FieldDown();
2351*cdf0e10cSrcweir     SpinField::Down();
2352*cdf0e10cSrcweir }
2353*cdf0e10cSrcweir 
2354*cdf0e10cSrcweir // -----------------------------------------------------------------------
2355*cdf0e10cSrcweir 
2356*cdf0e10cSrcweir void CurrencyField::First()
2357*cdf0e10cSrcweir {
2358*cdf0e10cSrcweir     FieldFirst();
2359*cdf0e10cSrcweir     SpinField::First();
2360*cdf0e10cSrcweir }
2361*cdf0e10cSrcweir 
2362*cdf0e10cSrcweir // -----------------------------------------------------------------------
2363*cdf0e10cSrcweir 
2364*cdf0e10cSrcweir void CurrencyField::Last()
2365*cdf0e10cSrcweir {
2366*cdf0e10cSrcweir     FieldLast();
2367*cdf0e10cSrcweir     SpinField::Last();
2368*cdf0e10cSrcweir }
2369*cdf0e10cSrcweir 
2370*cdf0e10cSrcweir // -----------------------------------------------------------------------
2371*cdf0e10cSrcweir 
2372*cdf0e10cSrcweir CurrencyBox::CurrencyBox( Window* pParent, WinBits nWinStyle ) :
2373*cdf0e10cSrcweir     ComboBox( pParent, nWinStyle )
2374*cdf0e10cSrcweir {
2375*cdf0e10cSrcweir     SetField( this );
2376*cdf0e10cSrcweir     Reformat();
2377*cdf0e10cSrcweir }
2378*cdf0e10cSrcweir 
2379*cdf0e10cSrcweir // -----------------------------------------------------------------------
2380*cdf0e10cSrcweir 
2381*cdf0e10cSrcweir CurrencyBox::CurrencyBox( Window* pParent, const ResId& rResId ) :
2382*cdf0e10cSrcweir     ComboBox( WINDOW_CURRENCYBOX )
2383*cdf0e10cSrcweir {
2384*cdf0e10cSrcweir     rResId.SetRT( RSC_CURRENCYBOX );
2385*cdf0e10cSrcweir     WinBits nStyle = ImplInitRes( rResId );
2386*cdf0e10cSrcweir     ComboBox::ImplInit( pParent, nStyle );
2387*cdf0e10cSrcweir     CurrencyFormatter::ImplLoadRes( ResId( (RSHEADER_TYPE *)GetClassRes(), *rResId.GetResMgr() ) );
2388*cdf0e10cSrcweir     SetField( this );
2389*cdf0e10cSrcweir     ComboBox::ImplLoadRes( rResId );
2390*cdf0e10cSrcweir     Reformat();
2391*cdf0e10cSrcweir 
2392*cdf0e10cSrcweir     if ( !(nStyle & WB_HIDE ) )
2393*cdf0e10cSrcweir         Show();
2394*cdf0e10cSrcweir }
2395*cdf0e10cSrcweir 
2396*cdf0e10cSrcweir // -----------------------------------------------------------------------
2397*cdf0e10cSrcweir 
2398*cdf0e10cSrcweir CurrencyBox::~CurrencyBox()
2399*cdf0e10cSrcweir {
2400*cdf0e10cSrcweir }
2401*cdf0e10cSrcweir 
2402*cdf0e10cSrcweir // -----------------------------------------------------------------------
2403*cdf0e10cSrcweir 
2404*cdf0e10cSrcweir long CurrencyBox::PreNotify( NotifyEvent& rNEvt )
2405*cdf0e10cSrcweir {
2406*cdf0e10cSrcweir     if ( (rNEvt.GetType() == EVENT_KEYINPUT) && !rNEvt.GetKeyEvent()->GetKeyCode().IsMod2() )
2407*cdf0e10cSrcweir     {
2408*cdf0e10cSrcweir         if ( ImplCurrencyProcessKeyInput( GetField(), *rNEvt.GetKeyEvent(), IsStrictFormat(), IsUseThousandSep(), ImplGetLocaleDataWrapper() ) )
2409*cdf0e10cSrcweir             return 1;
2410*cdf0e10cSrcweir     }
2411*cdf0e10cSrcweir 
2412*cdf0e10cSrcweir     return ComboBox::PreNotify( rNEvt );
2413*cdf0e10cSrcweir }
2414*cdf0e10cSrcweir 
2415*cdf0e10cSrcweir // -----------------------------------------------------------------------
2416*cdf0e10cSrcweir 
2417*cdf0e10cSrcweir long CurrencyBox::Notify( NotifyEvent& rNEvt )
2418*cdf0e10cSrcweir {
2419*cdf0e10cSrcweir     if ( rNEvt.GetType() == EVENT_GETFOCUS )
2420*cdf0e10cSrcweir         MarkToBeReformatted( sal_False );
2421*cdf0e10cSrcweir     else if ( rNEvt.GetType() == EVENT_LOSEFOCUS )
2422*cdf0e10cSrcweir     {
2423*cdf0e10cSrcweir         if ( MustBeReformatted() && (GetText().Len() || !IsEmptyFieldValueEnabled()) )
2424*cdf0e10cSrcweir             Reformat();
2425*cdf0e10cSrcweir     }
2426*cdf0e10cSrcweir 
2427*cdf0e10cSrcweir     return ComboBox::Notify( rNEvt );
2428*cdf0e10cSrcweir }
2429*cdf0e10cSrcweir 
2430*cdf0e10cSrcweir // -----------------------------------------------------------------------
2431*cdf0e10cSrcweir 
2432*cdf0e10cSrcweir void CurrencyBox::DataChanged( const DataChangedEvent& rDCEvt )
2433*cdf0e10cSrcweir {
2434*cdf0e10cSrcweir     ComboBox::DataChanged( rDCEvt );
2435*cdf0e10cSrcweir 
2436*cdf0e10cSrcweir     if ( (rDCEvt.GetType() == DATACHANGED_SETTINGS) && (rDCEvt.GetFlags() & SETTINGS_LOCALE) )
2437*cdf0e10cSrcweir     {
2438*cdf0e10cSrcweir         String sOldDecSep = ImplGetLocaleDataWrapper().getNumDecimalSep();
2439*cdf0e10cSrcweir         String sOldThSep = ImplGetLocaleDataWrapper().getNumThousandSep();
2440*cdf0e10cSrcweir         if ( IsDefaultLocale() )
2441*cdf0e10cSrcweir             ImplGetLocaleDataWrapper().setLocale( GetSettings().GetLocale() );
2442*cdf0e10cSrcweir         String sNewDecSep = ImplGetLocaleDataWrapper().getNumDecimalSep();
2443*cdf0e10cSrcweir         String sNewThSep = ImplGetLocaleDataWrapper().getNumThousandSep();
2444*cdf0e10cSrcweir         ImplUpdateSeparators( sOldDecSep, sNewDecSep, sOldThSep, sNewThSep, this );
2445*cdf0e10cSrcweir         ReformatAll();
2446*cdf0e10cSrcweir     }
2447*cdf0e10cSrcweir }
2448*cdf0e10cSrcweir 
2449*cdf0e10cSrcweir // -----------------------------------------------------------------------
2450*cdf0e10cSrcweir 
2451*cdf0e10cSrcweir void CurrencyBox::Modify()
2452*cdf0e10cSrcweir {
2453*cdf0e10cSrcweir     MarkToBeReformatted( sal_True );
2454*cdf0e10cSrcweir     ComboBox::Modify();
2455*cdf0e10cSrcweir }
2456*cdf0e10cSrcweir 
2457*cdf0e10cSrcweir // -----------------------------------------------------------------------
2458*cdf0e10cSrcweir 
2459*cdf0e10cSrcweir void CurrencyBox::ReformatAll()
2460*cdf0e10cSrcweir {
2461*cdf0e10cSrcweir     XubString aStr;
2462*cdf0e10cSrcweir     SetUpdateMode( sal_False );
2463*cdf0e10cSrcweir     sal_uInt16 nEntryCount = GetEntryCount();
2464*cdf0e10cSrcweir     for ( sal_uInt16 i=0; i < nEntryCount; i++ )
2465*cdf0e10cSrcweir     {
2466*cdf0e10cSrcweir         ImplCurrencyReformat( GetEntry( i ), aStr );
2467*cdf0e10cSrcweir         RemoveEntry( i );
2468*cdf0e10cSrcweir         InsertEntry( aStr, i );
2469*cdf0e10cSrcweir     }
2470*cdf0e10cSrcweir     CurrencyFormatter::Reformat();
2471*cdf0e10cSrcweir     SetUpdateMode( sal_True );
2472*cdf0e10cSrcweir }
2473*cdf0e10cSrcweir 
2474*cdf0e10cSrcweir // -----------------------------------------------------------------------
2475*cdf0e10cSrcweir 
2476*cdf0e10cSrcweir void CurrencyBox::InsertValue( sal_Int64 nValue, sal_uInt16 nPos )
2477*cdf0e10cSrcweir {
2478*cdf0e10cSrcweir     ComboBox::InsertEntry( CreateFieldText( nValue ), nPos );
2479*cdf0e10cSrcweir }
2480*cdf0e10cSrcweir 
2481*cdf0e10cSrcweir // -----------------------------------------------------------------------
2482*cdf0e10cSrcweir 
2483*cdf0e10cSrcweir void CurrencyBox::RemoveValue( sal_Int64 nValue )
2484*cdf0e10cSrcweir {
2485*cdf0e10cSrcweir     ComboBox::RemoveEntry( CreateFieldText( nValue ) );
2486*cdf0e10cSrcweir }
2487*cdf0e10cSrcweir 
2488*cdf0e10cSrcweir // -----------------------------------------------------------------------
2489*cdf0e10cSrcweir 
2490*cdf0e10cSrcweir sal_Int64 CurrencyBox::GetValue( sal_uInt16 nPos ) const
2491*cdf0e10cSrcweir {
2492*cdf0e10cSrcweir     double nValue = 0;
2493*cdf0e10cSrcweir     ImplCurrencyGetValue( ComboBox::GetEntry( nPos ), nValue, GetDecimalDigits(), ImplGetLocaleDataWrapper() );
2494*cdf0e10cSrcweir     return (sal_Int64)nValue;
2495*cdf0e10cSrcweir }
2496*cdf0e10cSrcweir 
2497*cdf0e10cSrcweir // -----------------------------------------------------------------------
2498*cdf0e10cSrcweir 
2499*cdf0e10cSrcweir sal_uInt16 CurrencyBox::GetValuePos( sal_Int64 nValue ) const
2500*cdf0e10cSrcweir {
2501*cdf0e10cSrcweir     return ComboBox::GetEntryPos( CreateFieldText( nValue ) );
2502*cdf0e10cSrcweir }
2503*cdf0e10cSrcweir 
2504*cdf0e10cSrcweir // -----------------------------------------------------------------------
2505*cdf0e10cSrcweir 
2506*cdf0e10cSrcweir sal_Int64 CurrencyBox::GetValue() const
2507*cdf0e10cSrcweir {
2508*cdf0e10cSrcweir     // Implementation not inline, because it is a virtual Function
2509*cdf0e10cSrcweir     return CurrencyFormatter::GetValue();
2510*cdf0e10cSrcweir }
2511