nss: upgrade to release 3.73
[LibreOffice.git] / include / vcl / formatter.hxx
blob946e7e77b7279e01a2de00d819691aa7fbf4d238
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #pragma once
22 #include <config_options.h>
23 #include <i18nlangtag/lang.h>
24 #include <tools/link.hxx>
25 #include <vcl/settings.hxx>
26 #include <map>
27 #include <memory>
29 class SvNumberFormatter;
31 namespace validation
33 // the states of our automat.
34 enum State
36 START, // at the very start of the string
37 NUM_START, // the very start of the number
39 DIGIT_PRE_COMMA, // some pre-comma digits are read, perhaps including some thousand separators
41 DIGIT_POST_COMMA, // reading digits after the comma
42 EXPONENT_START, // at the very start of the exponent value
43 // (means: not including the "e" which denotes the exponent)
44 EXPONENT_DIGIT, // currently reading the digits of the exponent
46 END // reached the end of the string
49 // a row in the transition table (means the set of states to be reached from a given state)
50 typedef ::std::map< sal_Unicode, State > StateTransitions;
52 // a single transition
53 typedef StateTransitions::value_type Transition;
55 // the complete transition table
56 typedef ::std::map< State, StateTransitions > TransitionTable;
58 // the validator class
59 class NumberValidator
61 private:
62 TransitionTable m_aTransitions;
64 public:
65 NumberValidator( const sal_Unicode _cThSep, const sal_Unicode _cDecSep );
67 bool isValidNumericFragment( const OUString& _rText );
69 private:
70 bool implValidateNormalized( const OUString& _rText );
74 enum class FORMAT_CHANGE_TYPE
76 KEYONLY = 0x00, // only a new key was set
77 FORMATTER = 0x01, // a new formatter was set, usually implies a change of the key, too
78 PRECISION = 0x02, // a new precision was set
79 THOUSANDSSEP = 0x03, // the thousands separator setting changed
80 CURRENCY_SYMBOL = 0x10,
81 CURRSYM_POSITION = 0x20,
84 class VCL_DLLPUBLIC Formatter
86 private:
87 // A SvNumberFormatter is very expensive (regarding time and space), it is a Singleton
88 class StaticFormatter
90 static SvNumberFormatter* s_cFormatter;
91 static sal_uLong s_nReferences;
92 public:
93 StaticFormatter();
94 ~StaticFormatter();
96 operator SvNumberFormatter* () { return GetFormatter(); }
97 UNLESS_MERGELIBS(VCL_DLLPUBLIC) static SvNumberFormatter* GetFormatter();
100 protected:
101 OUString m_sLastValidText;
102 // Has nothing to do with the current value. It is the last text, which was valid at input (checked by CheckText,
103 // not yet through formatter)
104 Selection m_aLastSelection;
106 double m_dMinValue;
107 double m_dMaxValue;
108 bool m_bHasMin : 1;
109 bool m_bHasMax : 1;
111 bool m_bWrapOnLimits : 1;
112 bool m_bStrictFormat : 1;
114 bool m_bEnableEmptyField : 1;
115 bool m_bAutoColor : 1;
116 bool m_bEnableNaN : 1;
117 bool m_bDisableRemainderFactor : 1;
118 enum valueState { valueDirty, valueString, valueDouble };
119 valueState m_ValueState;
120 double m_dCurrentValue;
121 double m_dDefaultValue;
123 sal_uLong m_nFormatKey;
124 SvNumberFormatter* m_pFormatter;
125 StaticFormatter m_aStaticFormatter;
127 double m_dSpinSize;
128 double m_dSpinFirst;
129 double m_dSpinLast;
131 // There is a difference, when text formatting is enabled, if LostFocus formats the current String and displays it,
132 // or if a double is created from the String and then
133 bool m_bTreatAsNumber;
134 // And with the following members we can use it for formatted text output as well ...
135 OUString m_sCurrentTextValue;
136 OUString m_sDefaultText;
138 // The last color from the Formatter at the last output operation (not we would use it, but you can get it)
139 const Color* m_pLastOutputColor;
141 bool m_bUseInputStringForFormatting;
143 Link<sal_Int64*, TriState> m_aInputHdl;
144 Link<LinkParamNone*, bool> m_aOutputHdl;
146 public:
147 Formatter();
148 virtual ~Formatter();
150 void SetFieldText(const OUString& rText, const Selection& rNewSelection);
152 virtual Selection GetEntrySelection() const = 0;
153 virtual OUString GetEntryText() const = 0;
154 virtual SelectionOptions GetEntrySelectionOptions() const = 0;
155 virtual void SetEntryText(const OUString& rText, const Selection& rSel) = 0;
156 virtual void SetEntryTextColor(const Color* pColor) = 0;
157 virtual void FieldModified() = 0;
159 // Min-/Max-management
160 bool HasMinValue() const { return m_bHasMin; }
161 virtual void ClearMinValue() { m_bHasMin = false; }
162 virtual void SetMinValue(double dMin);
163 double GetMinValue() const { return m_dMinValue; }
165 bool HasMaxValue() const { return m_bHasMax; }
166 virtual void ClearMaxValue() { m_bHasMax = false; }
167 virtual void SetMaxValue(double dMax);
168 double GetMaxValue() const { return m_dMaxValue; }
170 // Current value
171 void SetValue(double dVal);
172 double GetValue();
173 // The default implementation uses a formatter, if available
175 void SetTextValue(const OUString& rText);
176 // The String is transformed to a double (with a formatter) and SetValue is called afterwards
178 bool IsEmptyFieldEnabled() const { return m_bEnableEmptyField; }
179 void EnableEmptyField(bool bEnable);
180 // If disabled, the value will be reset to the last valid value on leave
182 void SetDefaultValue(double dDefault) { m_dDefaultValue = dDefault; m_ValueState = valueDirty; }
183 // If the current String is invalid, GetValue() returns this value
184 double GetDefaultValue() const { return m_dDefaultValue; }
186 void SetLastSelection(const Selection& rSelection) { m_aLastSelection = rSelection; }
188 // Settings for the format
189 sal_uLong GetFormatKey() const { return m_nFormatKey; }
190 void SetFormatKey(sal_uLong nFormatKey);
192 SvNumberFormatter* GetOrCreateFormatter() const { return m_pFormatter ? m_pFormatter : const_cast<Formatter*>(this)->CreateFormatter(); }
194 SvNumberFormatter* GetFormatter() const { return m_pFormatter; }
195 void SetFormatter(SvNumberFormatter* pFormatter, bool bResetFormat = true);
196 // If bResetFormat is sal_False, the old format is tried to be kept. (expensive, if it is no default format, available in all formatters)
197 // If sal_True, the new FormatKey is set to zero
199 bool GetThousandsSep() const;
200 void SetThousandsSep(bool _bUseSeparator);
201 // the is no check if the current format is numeric, so be cautious when calling these functions
203 void DisableRemainderFactor();
204 bool GetDisableRemainderFactor() const { return m_bDisableRemainderFactor; }
206 void SetWrapOnLimits(bool bWrapOnLimits) { m_bWrapOnLimits = bWrapOnLimits; }
208 sal_uInt16 GetDecimalDigits() const;
209 void SetDecimalDigits(sal_uInt16 _nPrecision);
210 // There is no check if the current format is numeric, so be cautious when calling these functions
212 SvNumberFormatter* StandardFormatter() { return m_aStaticFormatter; }
213 // If no new Formatter is created explicitly, this can be used in SetFormatter...
215 OUString GetFormat(LanguageType& eLang) const;
216 bool SetFormat(const OUString& rFormatString, LanguageType eLang);
217 // sal_False, if the FormatString could not be set (and very probably is invalid)
218 // This Object is shared via all instances, so be careful!
220 bool IsStrictFormat() const { return m_bStrictFormat; }
221 void SetStrictFormat(bool bEnable) { m_bStrictFormat = bEnable; }
222 // Check format during input
224 virtual void SetSpinSize(double dStep) { m_dSpinSize = dStep; }
225 double GetSpinSize() const { return m_dSpinSize; }
227 void SetSpinFirst(double dFirst) { m_dSpinFirst = dFirst; }
228 double GetSpinFirst() const { return m_dSpinFirst; }
230 void SetSpinLast(double dLast) { m_dSpinLast = dLast; }
231 double GetSpinLast() const { return m_dSpinLast; }
233 bool TreatingAsNumber() const { return m_bTreatAsNumber; }
234 void TreatAsNumber(bool bDoSo) { m_bTreatAsNumber = bDoSo; }
236 void SetInputHdl(const Link<sal_Int64*,TriState>& rLink) { m_aInputHdl = rLink; }
237 void SetOutputHdl(const Link<LinkParamNone*, bool>& rLink) { m_aOutputHdl = rLink; }
238 public:
240 //The following methods are interesting, if m_bTreatAsNumber is set to sal_False
241 //If someone does not care about all the double handling and just wants to print the text formatted.
242 //(((The text will be formatted, using the Formatter, and then set)
243 void SetTextFormatted(const OUString& rText);
244 OUString const & GetTextValue() const;
246 void SetDefaultText(const OUString& rDefault) { m_sDefaultText = rDefault; }
247 const OUString& GetDefaultText() const { return m_sDefaultText; }
249 // The last colour from the Formatter's last output operation. Output operations get triggered by:
250 // SetValue, SetTextValue, SetTextFormatted, also indirectly via SetMin - / -MaxValue
251 const Color* GetLastOutputColor() const { return m_pLastOutputColor; }
253 /** reformats the current text. Interesting if the user entered some text in an "input format", and
254 this should be formatted in the "output format" (which may differ, e.g. by additional numeric
255 digits or such).
257 void Commit();
259 // enable automatic coloring. if set to sal_True, and the format the field is working with for any current value
260 // says that it has to be painted in a special color (e.g. a format where negative numbers should be printed
261 // red), the text is painted with that color automatically.
262 // The color used is the same as returned by GetLastOutputColor()
263 void SetAutoColor(bool _bAutomatic);
265 /** enables handling of not-a-number value.
267 When this is set to <FALSE/> (the default), then invalid inputs (i.e. text which cannot be
268 interpreted, according to the current formatting) will be handled as if the default value
269 has been entered. GetValue the will return this default value.
271 When set to <TRUE/>, then GetValue will return NaN (not a number, see <method scope="rtl::math">isNan</method>)
272 when the current input is invalid.
274 Note that setting this to <TRUE/> implies that upon leaving the control, the input
275 will *not* be corrected to a valid value. For example, if the user enters "foo" in the
276 control, and then tabs out of it, the text "foo" will persist, and GetValue will
277 return NaN in subsequent calls.
279 void EnableNotANumber( bool _bEnable );
281 /** When being set to true, the strings in the field are formatted using the
282 InputLine format. That's also what you get in Calc when you edit a cell
283 using F2
285 void UseInputStringForFormatting();
286 bool IsUsingInputStringForFormatting() const { return m_bUseInputStringForFormatting;}
288 void Modify(bool makeValueDirty = true);
290 void EntryLostFocus();
292 void ReFormat();
294 // any aspect of the current format has changed
295 virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat);
297 protected:
299 // Override CheckText for input-time checks
300 virtual bool CheckText(const OUString&) const { return true; }
302 void ImplSetTextImpl(const OUString& rNew, Selection const * pNewSel);
303 void ImplSetValue(double dValue, bool bForce);
304 bool ImplGetValue(double& dNewVal);
306 void ImplSetFormatKey(sal_uLong nFormatKey);
307 // SetFormatKey without FormatChanged notification
309 SvNumberFormatter* CreateFormatter() { SetFormatter(StandardFormatter()); return m_pFormatter; }
311 virtual void UpdateCurrentValue(double dCurrentValue) { m_dCurrentValue = dCurrentValue; }
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */