1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <string_view>
24 #include <comphelper/string.hxx>
25 #include <tools/bigint.hxx>
26 #include <sal/log.hxx>
28 #include <vcl/event.hxx>
29 #include <vcl/svapp.hxx>
30 #include <vcl/toolkit/longcurr.hxx>
31 #include <vcl/weldutils.hxx>
33 #include <unotools/localedatawrapper.hxx>
35 using namespace ::comphelper
;
40 BigInt
ImplPower10( sal_uInt16 n
)
45 for ( i
=0; i
< n
; i
++ )
51 OUString
ImplGetCurr( const LocaleDataWrapper
& rLocaleDataWrapper
, const BigInt
&rNumber
, sal_uInt16 nDigits
, std::u16string_view rCurrSymbol
, bool bShowThousandSep
)
53 SAL_WARN_IF( nDigits
>= 10, "vcl", "LongCurrency may only have 9 decimal places" );
55 if ( rNumber
.IsZero() || static_cast<tools::Long
>(rNumber
) )
56 return rLocaleDataWrapper
.getCurr( static_cast<tools::Long
>(rNumber
), nDigits
, rCurrSymbol
, bShowThousandSep
);
58 BigInt
aTmp( ImplPower10( nDigits
) );
59 BigInt
aInteger( rNumber
);
62 BigInt
aFraction( rNumber
);
65 if ( !aInteger
.IsZero() )
70 if ( rNumber
.IsNeg() )
73 OUStringBuffer
aTemplate(rLocaleDataWrapper
.getCurr( static_cast<tools::Long
>(aFraction
), nDigits
, rCurrSymbol
, bShowThousandSep
));
74 while( !aInteger
.IsZero() )
79 if( !aInteger
.IsZero() )
82 OUString aFractionStr
= rLocaleDataWrapper
.getNum( static_cast<tools::Long
>(aFraction
), 0 );
84 sal_Int32 nSPos
= aTemplate
.indexOf( '1' );
87 if ( aFractionStr
.getLength() == 1 )
88 aTemplate
[ nSPos
] = aFractionStr
[0];
91 aTemplate
.remove( nSPos
, 1 );
92 aTemplate
.insert( nSPos
, aFractionStr
);
96 return aTemplate
.makeStringAndClear();
99 bool ImplCurrencyGetValue( const OUString
& rStr
, BigInt
& rValue
,
100 sal_uInt16 nDecDigits
, const LocaleDataWrapper
& rLocaleDataWrapper
)
102 OUString aStr
= rStr
;
103 OUStringBuffer aStr1
;
104 OUStringBuffer aStr2
;
106 bool bNegative
= false;
109 if ( rStr
.isEmpty() )
112 // Trim leading and trailing spaces
113 aStr
= string::strip(aStr
, ' ');
115 // Find decimal sign's position
116 nDecPos
= aStr
.indexOf( rLocaleDataWrapper
.getNumDecimalSep() );
117 if (nDecPos
< 0 && !rLocaleDataWrapper
.getNumDecimalSepAlt().isEmpty())
118 nDecPos
= aStr
.indexOf( rLocaleDataWrapper
.getNumDecimalSepAlt() );
122 aStr1
= aStr
.subView( 0, nDecPos
);
123 aStr2
.append(aStr
.subView(nDecPos
+1));
129 if ( (aStr
[ 0 ] == '(') && (aStr
[ aStr
.getLength()-1 ] == ')') )
133 for (sal_Int32 i
=0; i
< aStr
.getLength(); i
++ )
135 if ( (aStr
[ i
] >= '0') && (aStr
[ i
] <= '9') )
137 else if ( aStr
[ i
] == '-' )
144 if ( !bNegative
&& !aStr
.isEmpty() )
146 sal_uInt16 nFormat
= rLocaleDataWrapper
.getCurrNegativeFormat();
147 if ( (nFormat
== 3) || (nFormat
== 6) ||
148 (nFormat
== 7) || (nFormat
== 10) )
150 for (sal_Int32 i
= aStr
.getLength()-1; i
> 0; i
++ )
152 if ( (aStr
[ i
] >= '0') && (aStr
[ i
] <= '9') )
154 else if ( aStr
[ i
] == '-' )
163 // delete unwanted characters
164 for (sal_Int32 i
=0; i
< aStr1
.getLength(); )
166 if ( (aStr1
[ i
] >= '0') && (aStr1
[ i
] <= '9') )
169 aStr1
.remove( i
, 1 );
171 for (sal_Int32 i
=0; i
< aStr2
.getLength(); )
173 if ((aStr2
[i
] >= '0') && (aStr2
[i
] <= '9'))
179 if ( aStr1
.isEmpty() && aStr2
.isEmpty())
182 if ( aStr1
.isEmpty() )
185 aStr1
.insert( 0, '-');
187 // Cut down decimal part and round while doing so
189 if (aStr2
.getLength() > nDecDigits
)
191 if (aStr2
[nDecDigits
] >= '5')
193 string::truncateToLength(aStr2
, nDecDigits
);
195 string::padToLength(aStr2
, nDecDigits
, '0');
198 aStr
= aStr1
.makeStringAndClear();
201 BigInt
nValue( aStr
);
217 static bool ImplLongCurrencyGetValue( const OUString
& rStr
, BigInt
& rValue
,
218 sal_uInt16 nDecDigits
, const LocaleDataWrapper
& rLocaleDataWrapper
)
220 return ImplCurrencyGetValue( rStr
, rValue
, nDecDigits
, rLocaleDataWrapper
);
225 IMPL_LINK_NOARG(LongCurrencyFormatter
, FormatOutputHdl
, LinkParamNone
*, bool)
227 const LocaleDataWrapper
& rLocaleDataWrapper
= Application::GetSettings().GetLocaleDataWrapper();
228 const OUString
& rCurrencySymbol
= !m_aCurrencySymbol
.isEmpty() ? m_aCurrencySymbol
: rLocaleDataWrapper
.getCurrSymbol();
229 double fValue
= GetValue() * weld::SpinButton::Power10(GetDecimalDigits());
230 OUString aText
= ImplGetCurr(rLocaleDataWrapper
, fValue
, GetDecimalDigits(), rCurrencySymbol
, m_bThousandSep
);
231 ImplSetTextImpl(aText
, nullptr);
235 IMPL_LINK(LongCurrencyFormatter
, ParseInputHdl
, sal_Int64
*, result
, TriState
)
237 const LocaleDataWrapper
& rLocaleDataWrapper
= Application::GetSettings().GetLocaleDataWrapper();
240 bool bRet
= ImplLongCurrencyGetValue(GetEntryText(), value
, GetDecimalDigits(), rLocaleDataWrapper
);
243 *result
= double(value
);
245 return bRet
? TRISTATE_TRUE
: TRISTATE_FALSE
;
249 bool ImplLongCurrencyReformat( const OUString
& rStr
, BigInt
const & nMin
, BigInt
const & nMax
,
250 sal_uInt16 nDecDigits
,
251 const LocaleDataWrapper
& rLocaleDataWrapper
, OUString
& rOutStr
,
252 LongCurrencyFormatter
const & rFormatter
)
255 if ( !ImplCurrencyGetValue( rStr
, nValue
, nDecDigits
, rLocaleDataWrapper
) )
259 BigInt nTempVal
= nValue
;
260 if ( nTempVal
> nMax
)
262 else if ( nTempVal
< nMin
)
265 rOutStr
= ImplGetCurr( rLocaleDataWrapper
, nTempVal
, nDecDigits
, rFormatter
.GetCurrencySymbol(), /*IsUseThousandSep*/true );
270 void LongCurrencyFormatter::ImpInit()
277 SetDecimalDigits( 0 );
280 LongCurrencyFormatter::LongCurrencyFormatter(Edit
* pEdit
)
281 : FormatterBase(pEdit
)
286 LongCurrencyFormatter::~LongCurrencyFormatter()
290 OUString
const & LongCurrencyFormatter::GetCurrencySymbol() const
292 return GetLocaleDataWrapper().getCurrSymbol();
295 void LongCurrencyFormatter::SetValue(const BigInt
& rNewValue
)
297 SetUserValue(rNewValue
);
298 SetEmptyFieldValueData( false );
301 void LongCurrencyFormatter::SetUserValue( BigInt nNewValue
)
303 if ( nNewValue
> mnMax
)
305 else if ( nNewValue
< mnMin
)
307 mnLastValue
= nNewValue
;
312 OUString aStr
= ImplGetCurr( GetLocaleDataWrapper(), nNewValue
, GetDecimalDigits(), GetCurrencySymbol(), /*UseThousandSep*/true );
313 if ( GetField()->HasFocus() )
315 Selection aSelection
= GetField()->GetSelection();
316 GetField()->SetText( aStr
);
317 GetField()->SetSelection( aSelection
);
320 GetField()->SetText( aStr
);
321 MarkToBeReformatted( false );
324 BigInt
LongCurrencyFormatter::GetValue() const
330 if ( ImplLongCurrencyGetValue( GetField()->GetText(), nTempValue
, GetDecimalDigits(), GetLocaleDataWrapper() ) )
332 if ( nTempValue
> mnMax
)
334 else if ( nTempValue
< mnMin
)
342 void LongCurrencyFormatter::Reformat()
347 if ( GetField()->GetText().isEmpty() && ImplGetEmptyFieldValue() )
351 bool bOK
= ImplLongCurrencyReformat( GetField()->GetText(), mnMin
, mnMax
,
352 GetDecimalDigits(), GetLocaleDataWrapper(), aStr
, *this );
356 if ( !aStr
.isEmpty() )
358 GetField()->SetText( aStr
);
359 MarkToBeReformatted( false );
360 ImplLongCurrencyGetValue( aStr
, mnLastValue
, GetDecimalDigits(), GetLocaleDataWrapper() );
363 SetValue( mnLastValue
);
366 void LongCurrencyFormatter::ReformatAll()
371 void LongCurrencyFormatter::SetDecimalDigits( sal_uInt16 nDigits
)
376 mnDecimalDigits
= nDigits
;
382 LongCurrencyBox::LongCurrencyBox(vcl::Window
* pParent
, WinBits nWinStyle
)
383 : ComboBox(pParent
, nWinStyle
)
384 , LongCurrencyFormatter(this)
389 bool LongCurrencyBox::EventNotify( NotifyEvent
& rNEvt
)
391 if( rNEvt
.GetType() == NotifyEventType::GETFOCUS
)
393 MarkToBeReformatted( false );
395 else if( rNEvt
.GetType() == NotifyEventType::LOSEFOCUS
)
397 if ( MustBeReformatted() )
403 return ComboBox::EventNotify( rNEvt
);
406 void LongCurrencyBox::Modify()
408 MarkToBeReformatted( true );
412 void LongCurrencyBox::ReformatAll()
415 SetUpdateMode( false );
416 const sal_Int32 nEntryCount
= GetEntryCount();
417 for ( sal_Int32 i
=0; i
< nEntryCount
; ++i
)
419 ImplLongCurrencyReformat( GetEntry( i
), mnMin
, mnMax
,
420 GetDecimalDigits(), GetLocaleDataWrapper(),
423 InsertEntry( aStr
, i
);
425 LongCurrencyFormatter::Reformat();
426 SetUpdateMode( true );
429 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */