Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / vcl / formatter.hxx
blob54d6745587e12a9d45b2ae59c993da329fd21ad1
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 <tools/solar.h>
26 #include <vcl/settings.hxx>
27 #include <map>
28 #include <memory>
29 #include <string_view>
31 class SvNumberFormatter;
33 namespace validation
35 // the states of our automat.
36 enum State
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
61 class NumberValidator
63 private:
64 TransitionTable m_aTransitions;
66 public:
67 NumberValidator( const sal_Unicode _cThSep, const sal_Unicode _cDecSep );
69 bool isValidNumericFragment( std::u16string_view _rText );
71 private:
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
88 public:
89 // A SvNumberFormatter is very expensive (regarding time and space), it is a Singleton
90 class StaticFormatter
92 static SvNumberFormatter* s_cFormatter;
93 static sal_uLong s_nReferences;
94 public:
95 StaticFormatter();
96 ~StaticFormatter();
98 operator SvNumberFormatter* () const { return GetFormatter(); }
99 UNLESS_MERGELIBS(VCL_DLLPUBLIC) static SvNumberFormatter* GetFormatter();
102 protected:
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;
108 double m_dMinValue;
109 double m_dMaxValue;
110 bool m_bHasMin : 1;
111 bool m_bHasMax : 1;
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 bool m_bDefaultValueSet : 1;
121 enum valueState { valueDirty, valueString, valueDouble };
122 valueState m_ValueState;
123 double m_dCurrentValue;
124 double m_dDefaultValue;
126 sal_uLong m_nFormatKey;
127 SvNumberFormatter* m_pFormatter;
128 StaticFormatter m_aStaticFormatter;
130 double m_dSpinSize;
131 double m_dSpinFirst;
132 double m_dSpinLast;
134 // There is a difference, when text formatting is enabled, if LostFocus formats the current String and displays it,
135 // or if a double is created from the String and then
136 bool m_bTreatAsNumber;
137 // And with the following members we can use it for formatted text output as well ...
138 OUString m_sCurrentTextValue;
139 OUString m_sDefaultText;
141 // The last color from the Formatter at the last output operation (not we would use it, but you can get it)
142 const Color* m_pLastOutputColor;
144 bool m_bUseInputStringForFormatting;
146 Link<sal_Int64*, TriState> m_aInputHdl;
147 Link<LinkParamNone*, bool> m_aOutputHdl;
149 public:
150 Formatter();
151 virtual ~Formatter();
153 void SetFieldText(const OUString& rText, const Selection& rNewSelection);
155 virtual Selection GetEntrySelection() const = 0;
156 virtual OUString GetEntryText() const = 0;
157 virtual SelectionOptions GetEntrySelectionOptions() const = 0;
158 virtual void SetEntryText(const OUString& rText, const Selection& rSel) = 0;
159 virtual void SetEntryTextColor(const Color* pColor) = 0;
160 virtual void FieldModified() = 0;
162 // Min-/Max-management
163 bool HasMinValue() const { return m_bHasMin; }
164 virtual void ClearMinValue() { m_bHasMin = false; }
165 virtual void SetMinValue(double dMin);
166 double GetMinValue() const { return m_dMinValue; }
168 bool HasMaxValue() const { return m_bHasMax; }
169 virtual void ClearMaxValue() { m_bHasMax = false; }
170 virtual void SetMaxValue(double dMax);
171 double GetMaxValue() const { return m_dMaxValue; }
173 // Current value
174 void SetValue(double dVal);
175 double GetValue();
176 // The default implementation uses a formatter, if available
178 void SetTextValue(const OUString& rText);
179 // The String is transformed to a double (with a formatter) and SetValue is called afterwards
181 bool IsEmptyFieldEnabled() const { return m_bEnableEmptyField; }
182 void EnableEmptyField(bool bEnable);
183 // If disabled, the value will be reset to the last valid value on leave
185 void SetDefaultValue(double dDefault) { m_dDefaultValue = dDefault; m_ValueState = valueDirty; m_bDefaultValueSet = true; }
186 // If the current String is invalid, GetValue() returns this value
187 double GetDefaultValue() const { return m_dDefaultValue; }
189 // Make the formatter recreate the output text from the value on the next
190 // format attempt even if the value is the same as the current value.
191 // Needed if the associated widget had its text changed by something other
192 // that this Formatter (typically blanked out) since the last formatting
193 void InvalidateValueState() { m_ValueState = valueDirty; }
195 void SetLastSelection(const Selection& rSelection) { m_aLastSelection = rSelection; }
197 // Settings for the format
198 sal_uLong GetFormatKey() const { return m_nFormatKey; }
199 void SetFormatKey(sal_uLong nFormatKey);
201 SvNumberFormatter* GetOrCreateFormatter() const { return m_pFormatter ? m_pFormatter : const_cast<Formatter*>(this)->CreateFormatter(); }
203 SvNumberFormatter* GetFormatter() const { return m_pFormatter; }
204 void SetFormatter(SvNumberFormatter* pFormatter, bool bResetFormat = true);
205 // If bResetFormat is sal_False, the old format is tried to be kept. (expensive, if it is no default format, available in all formatters)
206 // If sal_True, the new FormatKey is set to zero
208 bool GetThousandsSep() const;
209 void SetThousandsSep(bool _bUseSeparator);
210 // the is no check if the current format is numeric, so be cautious when calling these functions
212 void DisableRemainderFactor();
213 bool GetDisableRemainderFactor() const { return m_bDisableRemainderFactor; }
215 void SetWrapOnLimits(bool bWrapOnLimits) { m_bWrapOnLimits = bWrapOnLimits; }
217 sal_uInt16 GetDecimalDigits() const;
218 void SetDecimalDigits(sal_uInt16 _nPrecision);
219 // There is no check if the current format is numeric, so be cautious when calling these functions
221 SvNumberFormatter* StandardFormatter() { return m_aStaticFormatter; }
222 // If no new Formatter is created explicitly, this can be used in SetFormatter...
224 OUString GetFormat(LanguageType& eLang) const;
225 bool SetFormat(const OUString& rFormatString, LanguageType eLang);
226 // sal_False, if the FormatString could not be set (and very probably is invalid)
227 // This Object is shared via all instances, so be careful!
229 bool IsStrictFormat() const { return m_bStrictFormat; }
230 void SetStrictFormat(bool bEnable) { m_bStrictFormat = bEnable; }
231 // Check format during input
233 virtual void SetSpinSize(double dStep) { m_dSpinSize = dStep; }
234 double GetSpinSize() const { return m_dSpinSize; }
236 void SetSpinFirst(double dFirst) { m_dSpinFirst = dFirst; }
237 double GetSpinFirst() const { return m_dSpinFirst; }
239 void SetSpinLast(double dLast) { m_dSpinLast = dLast; }
240 double GetSpinLast() const { return m_dSpinLast; }
242 bool TreatingAsNumber() const { return m_bTreatAsNumber; }
243 void TreatAsNumber(bool bDoSo) { m_bTreatAsNumber = bDoSo; }
245 void SetInputHdl(const Link<sal_Int64*,TriState>& rLink) { m_aInputHdl = rLink; }
246 void SetOutputHdl(const Link<LinkParamNone*, bool>& rLink) { m_aOutputHdl = rLink; }
247 public:
249 //The following methods are interesting, if m_bTreatAsNumber is set to sal_False
250 //If someone does not care about all the double handling and just wants to print the text formatted.
251 //(((The text will be formatted, using the Formatter, and then set)
252 void SetTextFormatted(const OUString& rText);
253 OUString const & GetTextValue() const;
255 void SetDefaultText(const OUString& rDefault) { m_sDefaultText = rDefault; }
256 const OUString& GetDefaultText() const { return m_sDefaultText; }
258 // The last colour from the Formatter's last output operation. Output operations get triggered by:
259 // SetValue, SetTextValue, SetTextFormatted, also indirectly via SetMin - / -MaxValue
260 const Color* GetLastOutputColor() const { return m_pLastOutputColor; }
262 /** reformats the current text. Interesting if the user entered some text in an "input format", and
263 this should be formatted in the "output format" (which may differ, e.g. by additional numeric
264 digits or such).
266 void Commit();
268 // enable automatic coloring. if set to sal_True, and the format the field is working with for any current value
269 // says that it has to be painted in a special color (e.g. a format where negative numbers should be printed
270 // red), the text is painted with that color automatically.
271 // The color used is the same as returned by GetLastOutputColor()
272 void SetAutoColor(bool _bAutomatic);
274 /** enables handling of not-a-number value.
276 When this is set to <FALSE/> (the default), then invalid inputs (i.e. text which cannot be
277 interpreted, according to the current formatting) will be handled as if the default value
278 has been entered. GetValue the will return this default value.
280 When set to <TRUE/>, then GetValue will return NaN (not a number, see <method scope="rtl::math">isNan</method>)
281 when the current input is invalid.
283 Note that setting this to <TRUE/> implies that upon leaving the control, the input
284 will *not* be corrected to a valid value. For example, if the user enters "foo" in the
285 control, and then tabs out of it, the text "foo" will persist, and GetValue will
286 return NaN in subsequent calls.
288 void EnableNotANumber( bool _bEnable );
290 /** When being set to true, the strings in the field are formatted using the
291 InputLine format. That's also what you get in Calc when you edit a cell
292 using F2
294 void UseInputStringForFormatting();
295 bool IsUsingInputStringForFormatting() const { return m_bUseInputStringForFormatting;}
297 void Modify(bool makeValueDirty = true);
299 void EntryLostFocus();
301 void ReFormat();
303 // any aspect of the current format has changed
304 virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat);
306 protected:
308 // Override CheckText for input-time checks
309 virtual bool CheckText(const OUString&) const { return true; }
311 void ImplSetTextImpl(const OUString& rNew, Selection const * pNewSel);
312 void ImplSetValue(double dValue, bool bForce);
313 bool ImplGetValue(double& dNewVal);
315 void ImplSetFormatKey(sal_uLong nFormatKey);
316 // SetFormatKey without FormatChanged notification
318 SvNumberFormatter* CreateFormatter() { SetFormatter(StandardFormatter()); return m_pFormatter; }
320 virtual void UpdateCurrentValue(double dCurrentValue) { m_dCurrentValue = dCurrentValue; }
323 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */