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 #ifndef INCLUDED_SVTOOLS_FMTFIELD_HXX
21 #define INCLUDED_SVTOOLS_FMTFIELD_HXX
23 #include <svtools/svtdllapi.h>
24 #include <vcl/spinfld.hxx>
25 #include <svl/zforlist.hxx>
27 namespace validation
{ class NumberValidator
; }
29 enum class FORMAT_CHANGE_TYPE
31 KEYONLY
= 0x00, // only a new key was set
32 FORMATTER
= 0x01, // a new formatter was set, usually implies a change of the key, too
33 PRECISION
= 0x02, // a new precision was set
34 THOUSANDSSEP
= 0x03, // the thousands separator setting changed
35 CURRENCY_SYMBOL
= 0x10,
36 CURRSYM_POSITION
= 0x20,
40 class SVT_DLLPUBLIC FormattedField
: public SpinField
43 // A SvNumberFormatter is very expensive (regarding time and space), it is a Singleton
46 static SvNumberFormatter
* s_cFormatter
;
47 static sal_uLong s_nReferences
;
52 operator SvNumberFormatter
* () { return GetFormatter(); }
53 SVT_DLLPUBLIC SvNumberFormatter
* GetFormatter();
57 OUString m_sLastValidText
;
58 // Has nothing to do with the current value. It is the last text, which was valid at input (checked by CheckText,
59 // not yet through formatter)
60 Selection m_aLastSelection
;
67 bool m_bStrictFormat
: 1;
69 bool m_bEnableEmptyField
: 1;
70 bool m_bAutoColor
: 1;
71 bool m_bEnableNaN
: 1;
72 enum valueState
{ valueDirty
, valueString
, valueDouble
};
73 valueState m_ValueState
;
74 double m_dCurrentValue
;
75 double m_dDefaultValue
;
77 sal_uLong m_nFormatKey
;
78 SvNumberFormatter
* m_pFormatter
;
79 StaticFormatter m_aStaticFormatter
;
85 // There is a difference, when text formatting is enabled, if LostFocus formats the current String and displays it,
86 // or if a double is created from the String and then
87 bool m_bTreatAsNumber
;
88 // And with the following members we can use it for formatted text output as well ...
89 OUString m_sCurrentTextValue
;
90 OUString m_sDefaultText
;
92 // The last color from the Formatter at the last output operation (not we would use it, but you can get it)
93 Color
* m_pLastOutputColor
;
95 bool m_bUseInputStringForFormatting
;
98 FormattedField(vcl::Window
* pParent
, WinBits nStyle
= 0, SvNumberFormatter
* pInitialFormatter
= NULL
, sal_Int32 nFormatKey
= 0);
100 // Min-/Max-management
101 bool HasMinValue() const { return m_bHasMin
; }
102 void ClearMinValue() { m_bHasMin
= false; }
103 void SetMinValue(double dMin
);
104 double GetMinValue() const { return m_dMinValue
; }
106 bool HasMaxValue() const { return m_bHasMax
; }
107 void ClearMaxValue() { m_bHasMax
= false; }
108 void SetMaxValue(double dMax
);
109 double GetMaxValue() const { return m_dMaxValue
; }
112 void SetValue(double dVal
);
114 // The default implementation uses a formatter, if available
116 void GetColor() const;
118 void SetTextValue(const OUString
& rText
);
119 // The String is transformed to a double (with a formatter) and SetValue is called afterwards
121 bool IsEmptyFieldEnabled() const { return m_bEnableEmptyField
; }
122 void EnableEmptyField(bool bEnable
);
123 // If disabled, the value will be resetted to the last valid value on leave
125 void SetDefaultValue(double dDefault
) { m_dDefaultValue
= dDefault
; m_ValueState
= valueDirty
; }
126 // If the current String is invalid, GetValue() returns this value
127 double GetDefaultValue() const { return m_dDefaultValue
; }
129 // Settings for the format
130 sal_uLong
GetFormatKey() const { return m_nFormatKey
; }
131 void SetFormatKey(sal_uLong nFormatKey
);
133 SvNumberFormatter
* GetFormatter() const { return m_pFormatter
; }
134 void SetFormatter(SvNumberFormatter
* pFormatter
, bool bResetFormat
= true);
135 // If bResetFormat is sal_False, the old format is tried to be kept. (expensive, if it is no default format, available in all formatters)
136 // If sal_True, the new FormatKey is set to zero
138 bool GetThousandsSep() const;
139 void SetThousandsSep(bool _bUseSeparator
);
140 // the is no check if the current format is numeric, so be cautious when calling these functions
142 sal_uInt16
GetDecimalDigits() const;
143 void SetDecimalDigits(sal_uInt16 _nPrecision
);
144 // There is no check if the current format is numeric, so be cautious when calling these functions
146 SvNumberFormatter
* StandardFormatter() { return m_aStaticFormatter
; }
147 // If no new Formatter is created explicitly, this can be used in SetFormatter...
149 OUString
GetFormat(LanguageType
& eLang
) const;
150 bool SetFormat(const OUString
& rFormatString
, LanguageType eLang
);
151 // sal_False, if the FormatString could not be set (and very probably is invalid)
152 // This Object is shared via all instances, so be careful!
154 bool IsStrictFormat() const { return m_bStrictFormat
; }
155 void SetStrictFormat(bool bEnable
) { m_bStrictFormat
= bEnable
; }
156 // Check format during input
159 virtual void Up() SAL_OVERRIDE
;
160 virtual void Down() SAL_OVERRIDE
;
161 // Default Implementation: +/- default spin size to the double value
162 virtual void First() SAL_OVERRIDE
;
163 virtual void Last() SAL_OVERRIDE
;
164 // Default Implementation: Current double is set to the first or last value
166 void SetSpinSize(double dStep
) { m_dSpinSize
= dStep
; }
167 double GetSpinSize() const { return m_dSpinSize
; }
169 void SetSpinFirst(double dFirst
) { m_dSpinFirst
= dFirst
; }
170 double GetSpinFirst() const { return m_dSpinFirst
; }
172 void SetSpinLast(double dLast
) { m_dSpinLast
= dLast
; }
173 double GetSpinLast() const { return m_dSpinLast
; }
175 bool TreatingAsNumber() const { return m_bTreatAsNumber
; }
176 void TreatAsNumber(bool bDoSo
) { m_bTreatAsNumber
= bDoSo
; }
179 virtual void SetText( const OUString
& rStr
) SAL_OVERRIDE
;
180 virtual void SetText( const OUString
& rStr
, const Selection
& rNewSelection
) SAL_OVERRIDE
;
182 //The following methods are interesting, if m_bTreatAsNumber is set to sal_False
183 //If someone does not care about all the double handling and just wants to print the text formatted.
184 //(((The text will be formatted, using the Formatter, and then set)
185 void SetTextFormatted(const OUString
& rText
);
186 OUString
GetTextValue() const;
188 void SetDefaultText(const OUString
& rDefault
) { m_sDefaultText
= rDefault
; }
189 OUString
GetDefaultText() const { return m_sDefaultText
; }
191 // The last colour from the Formatter's last output operation. Output operations get triggered by:
192 // SetValue, SetTextValue, SetTextFormatted, also indirectly via SetMin - / -MaxValue
193 Color
* GetLastOutputColor() const { return m_pLastOutputColor
; }
195 /** reformats the current text. Interesting if the user entered some text in an "input format", and
196 this should be formatted in the "output format" (which may differ, e.g. by additional numeric
201 // enable automatic coloring. if set to sal_True, and the format the field is working with for any current value
202 // says that it has to be painted in a special color (e.g. a format where negative numbers should be printed
203 // red), the text is painted with that color automatically.
204 // The color used is the same as returned by GetLastOutputColor()
205 void SetAutoColor(bool _bAutomatic
);
206 bool GetAutoColor() const { return m_bAutoColor
; }
208 /** enables handling of not-a-number value.
210 When this is set to <FALSE/> (the default), then invalid inputs (i.e. text which cannot be
211 intepreted, according to the current formatting) will be handled as if the default value
212 has been entered. GetValue the will return this default value.
214 When set to <TRUE/>, then GetValue will return NaN (not a number, see <method scope="rtl::math">isNan</method>)
215 when the current input is invalid.
217 Note that setting this to <TRUE/> implies that upon leaving the control, the input
218 will *not* be corrected to a valid value. For example, if the user enters "foo" in the
219 control, and then tabs out of it, the text "foo" will persist, and GetValue will
220 return NaN in subsequent calls.
222 void EnableNotANumber( bool _bEnable
);
223 bool IsNotANumberEnabled( ) const { return m_bEnableNaN
; }
225 /** When being set to true, the strings in the field are formatted using the
226 InputLine format. That's also what you get in Calc when you edit a cell
229 void UseInputStringForFormatting( bool bUseInputStr
= true );
230 bool IsUsingInputStringForFormatting() const { return m_bUseInputStringForFormatting
;}
233 virtual bool Notify(NotifyEvent
& rNEvt
) SAL_OVERRIDE
;
234 void impl_Modify(bool makeValueDirty
= true);
235 virtual void Modify() SAL_OVERRIDE
;
237 // Override CheckTextfor input-time checks
238 virtual bool CheckText(const OUString
&) const { return true; }
240 // any aspect of the current format has changed
241 virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat
);
243 void ImplSetTextImpl(const OUString
& rNew
, Selection
* pNewSel
);
244 void ImplSetValue(double dValue
, bool bForce
);
245 bool ImplGetValue(double& dNewVal
);
247 void ImplSetFormatKey(sal_uLong nFormatKey
);
248 // SetFormatKey without FormatChanged notification
250 SvNumberFormatter
* CreateFormatter() { SetFormatter(StandardFormatter()); return m_pFormatter
; }
251 SvNumberFormatter
* ImplGetFormatter() const { return m_pFormatter
? m_pFormatter
: const_cast<FormattedField
*>(this)->CreateFormatter(); }
253 bool PreNotify(NotifyEvent
& rNEvt
) SAL_OVERRIDE
;
259 class SVT_DLLPUBLIC DoubleNumericField
: public FormattedField
262 validation::NumberValidator
* m_pNumberValidator
;
265 DoubleNumericField(vcl::Window
* pParent
, WinBits nStyle
= 0)
266 :FormattedField(pParent
, nStyle
)
267 ,m_pNumberValidator( NULL
)
269 ResetConformanceTester();
272 virtual ~DoubleNumericField();
273 virtual void dispose() SAL_OVERRIDE
;
276 virtual bool CheckText(const OUString
& sText
) const SAL_OVERRIDE
;
278 virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat
) SAL_OVERRIDE
;
279 void ResetConformanceTester();
283 class DoubleCurrencyField
: public FormattedField
285 OUString m_sCurrencySymbol
;
286 bool m_bPrependCurrSym
;
287 bool m_bChangingFormat
;
290 DoubleCurrencyField(vcl::Window
* pParent
, WinBits nStyle
= 0);
292 OUString
getCurrencySymbol() const { return m_sCurrencySymbol
; }
293 void setCurrencySymbol(const OUString
& rSymbol
);
295 bool getPrependCurrSym() const { return m_bPrependCurrSym
; }
296 void setPrependCurrSym(bool _bPrepend
);
299 virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat
) SAL_OVERRIDE
;
301 void UpdateCurrencyFormat();
304 #endif // INCLUDED_SVTOOLS_FMTFIELD_HXX
306 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */