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 .
22 #include <config_options.h>
23 #include <i18nlangtag/lang.h>
24 #include <tools/link.hxx>
25 #include <tools/solar.h>
26 #include <vcl/settings.hxx>
29 #include <string_view>
31 class SvNumberFormatter
;
35 // the states of our automat.
38 START
, // at the very start of the string
39 NUM_START
, // the very start of the number
41 DIGIT_PRE_COMMA
, // some pre-comma digits are read, perhaps including some thousand separators
43 DIGIT_POST_COMMA
, // reading digits after the comma
44 EXPONENT_START
, // at the very start of the exponent value
45 // (means: not including the "e" which denotes the exponent)
46 EXPONENT_DIGIT
, // currently reading the digits of the exponent
48 END
// reached the end of the string
51 // a row in the transition table (means the set of states to be reached from a given state)
52 typedef ::std::map
< sal_Unicode
, State
> StateTransitions
;
54 // a single transition
55 typedef StateTransitions::value_type Transition
;
57 // the complete transition table
58 typedef ::std::map
< State
, StateTransitions
> TransitionTable
;
60 // the validator class
64 TransitionTable m_aTransitions
;
67 NumberValidator( const sal_Unicode _cThSep
, const sal_Unicode _cDecSep
);
69 bool isValidNumericFragment( std::u16string_view _rText
);
72 bool implValidateNormalized( const OUString
& _rText
);
76 enum class FORMAT_CHANGE_TYPE
78 KEYONLY
= 0x00, // only a new key was set
79 FORMATTER
= 0x01, // a new formatter was set, usually implies a change of the key, too
80 PRECISION
= 0x02, // a new precision was set
81 THOUSANDSSEP
= 0x03, // the thousands separator setting changed
82 CURRENCY_SYMBOL
= 0x10,
83 CURRSYM_POSITION
= 0x20,
86 class VCL_DLLPUBLIC Formatter
89 // A SvNumberFormatter is very expensive (regarding time and space), it is a Singleton
92 static SvNumberFormatter
* s_cFormatter
;
93 static sal_uLong s_nReferences
;
98 operator SvNumberFormatter
* () const { return GetFormatter(); }
99 UNLESS_MERGELIBS(VCL_DLLPUBLIC
) static SvNumberFormatter
* GetFormatter();
103 OUString m_sLastValidText
;
104 // Has nothing to do with the current value. It is the last text, which was valid at input (checked by CheckText,
105 // not yet through formatter)
106 Selection m_aLastSelection
;
113 bool m_bWrapOnLimits
: 1;
114 bool m_bStrictFormat
: 1;
116 bool m_bEnableEmptyField
: 1;
117 bool m_bAutoColor
: 1;
118 bool m_bEnableNaN
: 1;
119 bool m_bDisableRemainderFactor
: 1;
120 enum valueState
{ valueDirty
, valueString
, valueDouble
};
121 valueState m_ValueState
;
122 double m_dCurrentValue
;
123 double m_dDefaultValue
;
125 sal_uLong m_nFormatKey
;
126 SvNumberFormatter
* m_pFormatter
;
127 StaticFormatter m_aStaticFormatter
;
133 // There is a difference, when text formatting is enabled, if LostFocus formats the current String and displays it,
134 // or if a double is created from the String and then
135 bool m_bTreatAsNumber
;
136 // And with the following members we can use it for formatted text output as well ...
137 OUString m_sCurrentTextValue
;
138 OUString m_sDefaultText
;
140 // The last color from the Formatter at the last output operation (not we would use it, but you can get it)
141 const Color
* m_pLastOutputColor
;
143 bool m_bUseInputStringForFormatting
;
145 Link
<sal_Int64
*, TriState
> m_aInputHdl
;
146 Link
<LinkParamNone
*, bool> m_aOutputHdl
;
150 virtual ~Formatter();
152 void SetFieldText(const OUString
& rText
, const Selection
& rNewSelection
);
154 virtual Selection
GetEntrySelection() const = 0;
155 virtual OUString
GetEntryText() const = 0;
156 virtual SelectionOptions
GetEntrySelectionOptions() const = 0;
157 virtual void SetEntryText(const OUString
& rText
, const Selection
& rSel
) = 0;
158 virtual void SetEntryTextColor(const Color
* pColor
) = 0;
159 virtual void FieldModified() = 0;
161 // Min-/Max-management
162 bool HasMinValue() const { return m_bHasMin
; }
163 virtual void ClearMinValue() { m_bHasMin
= false; }
164 virtual void SetMinValue(double dMin
);
165 double GetMinValue() const { return m_dMinValue
; }
167 bool HasMaxValue() const { return m_bHasMax
; }
168 virtual void ClearMaxValue() { m_bHasMax
= false; }
169 virtual void SetMaxValue(double dMax
);
170 double GetMaxValue() const { return m_dMaxValue
; }
173 void SetValue(double dVal
);
175 // The default implementation uses a formatter, if available
177 void SetTextValue(const OUString
& rText
);
178 // The String is transformed to a double (with a formatter) and SetValue is called afterwards
180 bool IsEmptyFieldEnabled() const { return m_bEnableEmptyField
; }
181 void EnableEmptyField(bool bEnable
);
182 // If disabled, the value will be reset to the last valid value on leave
184 void SetDefaultValue(double dDefault
) { m_dDefaultValue
= dDefault
; m_ValueState
= valueDirty
; }
185 // If the current String is invalid, GetValue() returns this value
186 double GetDefaultValue() const { return m_dDefaultValue
; }
188 void SetLastSelection(const Selection
& rSelection
) { m_aLastSelection
= rSelection
; }
190 // Settings for the format
191 sal_uLong
GetFormatKey() const { return m_nFormatKey
; }
192 void SetFormatKey(sal_uLong nFormatKey
);
194 SvNumberFormatter
* GetOrCreateFormatter() const { return m_pFormatter
? m_pFormatter
: const_cast<Formatter
*>(this)->CreateFormatter(); }
196 SvNumberFormatter
* GetFormatter() const { return m_pFormatter
; }
197 void SetFormatter(SvNumberFormatter
* pFormatter
, bool bResetFormat
= true);
198 // If bResetFormat is sal_False, the old format is tried to be kept. (expensive, if it is no default format, available in all formatters)
199 // If sal_True, the new FormatKey is set to zero
201 bool GetThousandsSep() const;
202 void SetThousandsSep(bool _bUseSeparator
);
203 // the is no check if the current format is numeric, so be cautious when calling these functions
205 void DisableRemainderFactor();
206 bool GetDisableRemainderFactor() const { return m_bDisableRemainderFactor
; }
208 void SetWrapOnLimits(bool bWrapOnLimits
) { m_bWrapOnLimits
= bWrapOnLimits
; }
210 sal_uInt16
GetDecimalDigits() const;
211 void SetDecimalDigits(sal_uInt16 _nPrecision
);
212 // There is no check if the current format is numeric, so be cautious when calling these functions
214 SvNumberFormatter
* StandardFormatter() { return m_aStaticFormatter
; }
215 // If no new Formatter is created explicitly, this can be used in SetFormatter...
217 OUString
GetFormat(LanguageType
& eLang
) const;
218 bool SetFormat(const OUString
& rFormatString
, LanguageType eLang
);
219 // sal_False, if the FormatString could not be set (and very probably is invalid)
220 // This Object is shared via all instances, so be careful!
222 bool IsStrictFormat() const { return m_bStrictFormat
; }
223 void SetStrictFormat(bool bEnable
) { m_bStrictFormat
= bEnable
; }
224 // Check format during input
226 virtual void SetSpinSize(double dStep
) { m_dSpinSize
= dStep
; }
227 double GetSpinSize() const { return m_dSpinSize
; }
229 void SetSpinFirst(double dFirst
) { m_dSpinFirst
= dFirst
; }
230 double GetSpinFirst() const { return m_dSpinFirst
; }
232 void SetSpinLast(double dLast
) { m_dSpinLast
= dLast
; }
233 double GetSpinLast() const { return m_dSpinLast
; }
235 bool TreatingAsNumber() const { return m_bTreatAsNumber
; }
236 void TreatAsNumber(bool bDoSo
) { m_bTreatAsNumber
= bDoSo
; }
238 void SetInputHdl(const Link
<sal_Int64
*,TriState
>& rLink
) { m_aInputHdl
= rLink
; }
239 void SetOutputHdl(const Link
<LinkParamNone
*, bool>& rLink
) { m_aOutputHdl
= rLink
; }
242 //The following methods are interesting, if m_bTreatAsNumber is set to sal_False
243 //If someone does not care about all the double handling and just wants to print the text formatted.
244 //(((The text will be formatted, using the Formatter, and then set)
245 void SetTextFormatted(const OUString
& rText
);
246 OUString
const & GetTextValue() const;
248 void SetDefaultText(const OUString
& rDefault
) { m_sDefaultText
= rDefault
; }
249 const OUString
& GetDefaultText() const { return m_sDefaultText
; }
251 // The last colour from the Formatter's last output operation. Output operations get triggered by:
252 // SetValue, SetTextValue, SetTextFormatted, also indirectly via SetMin - / -MaxValue
253 const Color
* GetLastOutputColor() const { return m_pLastOutputColor
; }
255 /** reformats the current text. Interesting if the user entered some text in an "input format", and
256 this should be formatted in the "output format" (which may differ, e.g. by additional numeric
261 // enable automatic coloring. if set to sal_True, and the format the field is working with for any current value
262 // says that it has to be painted in a special color (e.g. a format where negative numbers should be printed
263 // red), the text is painted with that color automatically.
264 // The color used is the same as returned by GetLastOutputColor()
265 void SetAutoColor(bool _bAutomatic
);
267 /** enables handling of not-a-number value.
269 When this is set to <FALSE/> (the default), then invalid inputs (i.e. text which cannot be
270 interpreted, according to the current formatting) will be handled as if the default value
271 has been entered. GetValue the will return this default value.
273 When set to <TRUE/>, then GetValue will return NaN (not a number, see <method scope="rtl::math">isNan</method>)
274 when the current input is invalid.
276 Note that setting this to <TRUE/> implies that upon leaving the control, the input
277 will *not* be corrected to a valid value. For example, if the user enters "foo" in the
278 control, and then tabs out of it, the text "foo" will persist, and GetValue will
279 return NaN in subsequent calls.
281 void EnableNotANumber( bool _bEnable
);
283 /** When being set to true, the strings in the field are formatted using the
284 InputLine format. That's also what you get in Calc when you edit a cell
287 void UseInputStringForFormatting();
288 bool IsUsingInputStringForFormatting() const { return m_bUseInputStringForFormatting
;}
290 void Modify(bool makeValueDirty
= true);
292 void EntryLostFocus();
296 // any aspect of the current format has changed
297 virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat
);
301 // Override CheckText for input-time checks
302 virtual bool CheckText(const OUString
&) const { return true; }
304 void ImplSetTextImpl(const OUString
& rNew
, Selection
const * pNewSel
);
305 void ImplSetValue(double dValue
, bool bForce
);
306 bool ImplGetValue(double& dNewVal
);
308 void ImplSetFormatKey(sal_uLong nFormatKey
);
309 // SetFormatKey without FormatChanged notification
311 SvNumberFormatter
* CreateFormatter() { SetFormatter(StandardFormatter()); return m_pFormatter
; }
313 virtual void UpdateCurrentValue(double dCurrentValue
) { m_dCurrentValue
= dCurrentValue
; }
316 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */