LibreOffice Module vcl (master) 1
formatter.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#pragma once
21
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>
30
32
33namespace validation
34{
35 // the states of our automat.
36 enum State
37 {
38 START, // at the very start of the string
39 NUM_START, // the very start of the number
40
41 DIGIT_PRE_COMMA, // some pre-comma digits are read, perhaps including some thousand separators
42
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
47
48 END // reached the end of the string
49 };
50
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;
53
54 // a single transition
55 typedef StateTransitions::value_type Transition;
56
57 // the complete transition table
58 typedef ::std::map< State, StateTransitions > TransitionTable;
59
60 // the validator class
62 {
63 private:
65
66 public:
67 NumberValidator( const sal_Unicode _cThSep, const sal_Unicode _cDecSep );
68
69 bool isValidNumericFragment( std::u16string_view _rText );
70
71 private:
72 bool implValidateNormalized( const OUString& _rText );
73 };
74}
75
77{
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,
84};
85
87{
88public:
89 // A SvNumberFormatter is very expensive (regarding time and space), it is a Singleton
91 {
94 public:
97
98 operator SvNumberFormatter* () const { return GetFormatter(); }
100 };
101
102protected:
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)
107
110 bool m_bHasMin : 1;
111 bool m_bHasMax : 1;
112
115
117 bool m_bAutoColor : 1;
118 bool m_bEnableNaN : 1;
121 enum valueState { valueDirty, valueString, valueDouble };
125
129
133
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
137 // And with the following members we can use it for formatted text output as well ...
140
141 // The last color from the Formatter at the last output operation (not we would use it, but you can get it)
143
145
148
149public:
150 Formatter();
151 virtual ~Formatter();
152
153 void SetFieldText(const OUString& rText, const Selection& rNewSelection);
154
155 virtual Selection GetEntrySelection() const = 0;
156 virtual OUString GetEntryText() 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;
161
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; }
167
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; }
172
173 // Current value
174 void SetValue(double dVal);
175 double GetValue();
176 // The default implementation uses a formatter, if available
177
178 void SetTextValue(const OUString& rText);
179 // The String is transformed to a double (with a formatter) and SetValue is called afterwards
180
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
184
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; }
188
189 void SetLastSelection(const Selection& rSelection) { m_aLastSelection = rSelection; }
190
191 // Settings for the format
192 sal_uLong GetFormatKey() const { return m_nFormatKey; }
193 void SetFormatKey(sal_uLong nFormatKey);
194
195 SvNumberFormatter* GetOrCreateFormatter() const { return m_pFormatter ? m_pFormatter : const_cast<Formatter*>(this)->CreateFormatter(); }
196
197 SvNumberFormatter* GetFormatter() const { return m_pFormatter; }
198 void SetFormatter(SvNumberFormatter* pFormatter, bool bResetFormat = true);
199 // If bResetFormat is sal_False, the old format is tried to be kept. (expensive, if it is no default format, available in all formatters)
200 // If sal_True, the new FormatKey is set to zero
201
202 bool GetThousandsSep() const;
203 void SetThousandsSep(bool _bUseSeparator);
204 // the is no check if the current format is numeric, so be cautious when calling these functions
205
206 void DisableRemainderFactor();
207 bool GetDisableRemainderFactor() const { return m_bDisableRemainderFactor; }
208
209 void SetWrapOnLimits(bool bWrapOnLimits) { m_bWrapOnLimits = bWrapOnLimits; }
210
211 sal_uInt16 GetDecimalDigits() const;
212 void SetDecimalDigits(sal_uInt16 _nPrecision);
213 // There is no check if the current format is numeric, so be cautious when calling these functions
214
215 SvNumberFormatter* StandardFormatter() { return m_aStaticFormatter; }
216 // If no new Formatter is created explicitly, this can be used in SetFormatter...
217
218 OUString GetFormat(LanguageType& eLang) const;
219 bool SetFormat(const OUString& rFormatString, LanguageType eLang);
220 // sal_False, if the FormatString could not be set (and very probably is invalid)
221 // This Object is shared via all instances, so be careful!
222
223 bool IsStrictFormat() const { return m_bStrictFormat; }
224 void SetStrictFormat(bool bEnable) { m_bStrictFormat = bEnable; }
225 // Check format during input
226
227 virtual void SetSpinSize(double dStep) { m_dSpinSize = dStep; }
228 double GetSpinSize() const { return m_dSpinSize; }
229
230 void SetSpinFirst(double dFirst) { m_dSpinFirst = dFirst; }
231 double GetSpinFirst() const { return m_dSpinFirst; }
232
233 void SetSpinLast(double dLast) { m_dSpinLast = dLast; }
234 double GetSpinLast() const { return m_dSpinLast; }
235
236 bool TreatingAsNumber() const { return m_bTreatAsNumber; }
237 void TreatAsNumber(bool bDoSo) { m_bTreatAsNumber = bDoSo; }
238
239 void SetInputHdl(const Link<sal_Int64*,TriState>& rLink) { m_aInputHdl = rLink; }
240 void SetOutputHdl(const Link<LinkParamNone*, bool>& rLink) { m_aOutputHdl = rLink; }
241public:
242
243 //The following methods are interesting, if m_bTreatAsNumber is set to sal_False
244 //If someone does not care about all the double handling and just wants to print the text formatted.
245 //(((The text will be formatted, using the Formatter, and then set)
246 void SetTextFormatted(const OUString& rText);
247 OUString const & GetTextValue() const;
248
249 void SetDefaultText(const OUString& rDefault) { m_sDefaultText = rDefault; }
250 const OUString& GetDefaultText() const { return m_sDefaultText; }
251
252 // The last colour from the Formatter's last output operation. Output operations get triggered by:
253 // SetValue, SetTextValue, SetTextFormatted, also indirectly via SetMin - / -MaxValue
254 const Color* GetLastOutputColor() const { return m_pLastOutputColor; }
255
260 void Commit();
261
262 // enable automatic coloring. if set to sal_True, and the format the field is working with for any current value
263 // says that it has to be painted in a special color (e.g. a format where negative numbers should be printed
264 // red), the text is painted with that color automatically.
265 // The color used is the same as returned by GetLastOutputColor()
266 void SetAutoColor(bool _bAutomatic);
267
282 void EnableNotANumber( bool _bEnable );
283
288 void UseInputStringForFormatting();
289 bool IsUsingInputStringForFormatting() const { return m_bUseInputStringForFormatting;}
290
291 void Modify(bool makeValueDirty = true);
292
293 void EntryLostFocus();
294
295 void ReFormat();
296
297 // any aspect of the current format has changed
298 virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat);
299
300protected:
301
302 // Override CheckText for input-time checks
303 virtual bool CheckText(const OUString&) const { return true; }
304
305 void ImplSetTextImpl(const OUString& rNew, Selection const * pNewSel);
306 void ImplSetValue(double dValue, bool bForce);
307 bool ImplGetValue(double& dNewVal);
308
309 void ImplSetFormatKey(sal_uLong nFormatKey);
310 // SetFormatKey without FormatChanged notification
311
312 SvNumberFormatter* CreateFormatter() { SetFormatter(StandardFormatter()); return m_pFormatter; }
313
314 virtual void UpdateCurrentValue(double dCurrentValue) { m_dCurrentValue = dCurrentValue; }
315};
316
317/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
UNLESS_MERGELIBS(VCL_DLLPUBLIC) static SvNumberFormatter *GetFormatter()
static sal_uLong s_nReferences
Definition: formatter.hxx:93
static SvNumberFormatter * s_cFormatter
Definition: formatter.hxx:92
const OUString & GetDefaultText() const
Definition: formatter.hxx:250
Selection m_aLastSelection
Definition: formatter.hxx:106
void SetDefaultText(const OUString &rDefault)
Definition: formatter.hxx:249
double m_dSpinFirst
Definition: formatter.hxx:131
double GetSpinSize() const
Definition: formatter.hxx:228
virtual void SetEntryTextColor(const Color *pColor)=0
bool TreatingAsNumber() const
Definition: formatter.hxx:236
bool m_bDefaultValueSet
Definition: formatter.hxx:120
void TreatAsNumber(bool bDoSo)
Definition: formatter.hxx:237
virtual Selection GetEntrySelection() const =0
bool m_bUseInputStringForFormatting
Definition: formatter.hxx:144
double m_dMaxValue
Definition: formatter.hxx:109
double GetMinValue() const
Definition: formatter.hxx:166
StaticFormatter m_aStaticFormatter
Definition: formatter.hxx:128
virtual void FieldModified()=0
SvNumberFormatter * GetOrCreateFormatter() const
Definition: formatter.hxx:195
double GetSpinLast() const
Definition: formatter.hxx:234
void SetOutputHdl(const Link< LinkParamNone *, bool > &rLink)
Definition: formatter.hxx:240
bool IsUsingInputStringForFormatting() const
Definition: formatter.hxx:289
bool IsStrictFormat() const
Definition: formatter.hxx:223
const Color * GetLastOutputColor() const
Definition: formatter.hxx:254
virtual void SetEntryText(const OUString &rText, const Selection &rSel)=0
Link< LinkParamNone *, bool > m_aOutputHdl
Definition: formatter.hxx:147
SvNumberFormatter * CreateFormatter()
Definition: formatter.hxx:312
SvNumberFormatter * m_pFormatter
Definition: formatter.hxx:127
void SetLastSelection(const Selection &rSelection)
Definition: formatter.hxx:189
sal_uLong m_nFormatKey
Definition: formatter.hxx:126
bool m_bHasMin
Definition: formatter.hxx:110
void SetInputHdl(const Link< sal_Int64 *, TriState > &rLink)
Definition: formatter.hxx:239
void SetStrictFormat(bool bEnable)
Definition: formatter.hxx:224
virtual void ClearMinValue()
Definition: formatter.hxx:164
bool m_bEnableNaN
Definition: formatter.hxx:118
void SetWrapOnLimits(bool bWrapOnLimits)
Definition: formatter.hxx:209
double m_dMinValue
Definition: formatter.hxx:108
bool m_bDisableRemainderFactor
Definition: formatter.hxx:119
double GetMaxValue() const
Definition: formatter.hxx:171
bool GetDisableRemainderFactor() const
Definition: formatter.hxx:207
OUString m_sCurrentTextValue
Definition: formatter.hxx:138
SvNumberFormatter * StandardFormatter()
Definition: formatter.hxx:215
virtual bool CheckText(const OUString &) const
Definition: formatter.hxx:303
bool m_bTreatAsNumber
Definition: formatter.hxx:136
bool m_bAutoColor
Definition: formatter.hxx:117
sal_uLong GetFormatKey() const
Definition: formatter.hxx:192
void SetDefaultValue(double dDefault)
Definition: formatter.hxx:185
double m_dDefaultValue
Definition: formatter.hxx:124
bool m_bWrapOnLimits
Definition: formatter.hxx:113
double GetSpinFirst() const
Definition: formatter.hxx:231
virtual void ClearMaxValue()
Definition: formatter.hxx:169
double m_dSpinLast
Definition: formatter.hxx:132
bool m_bStrictFormat
Definition: formatter.hxx:114
OUString m_sDefaultText
Definition: formatter.hxx:139
double m_dCurrentValue
Definition: formatter.hxx:123
virtual OUString GetEntryText() const =0
void SetSpinLast(double dLast)
Definition: formatter.hxx:233
void SetSpinFirst(double dFirst)
Definition: formatter.hxx:230
bool IsEmptyFieldEnabled() const
Definition: formatter.hxx:181
bool HasMinValue() const
Definition: formatter.hxx:163
double GetDefaultValue() const
Definition: formatter.hxx:187
bool HasMaxValue() const
Definition: formatter.hxx:168
valueState m_ValueState
Definition: formatter.hxx:122
Link< sal_Int64 *, TriState > m_aInputHdl
Definition: formatter.hxx:146
virtual void SetSpinSize(double dStep)
Definition: formatter.hxx:227
SvNumberFormatter * GetFormatter() const
Definition: formatter.hxx:197
double m_dSpinSize
Definition: formatter.hxx:130
const Color * m_pLastOutputColor
Definition: formatter.hxx:142
bool m_bEnableEmptyField
Definition: formatter.hxx:116
bool m_bHasMax
Definition: formatter.hxx:111
OUString m_sLastValidText
Definition: formatter.hxx:103
virtual void UpdateCurrentValue(double dCurrentValue)
Definition: formatter.hxx:314
virtual SelectionOptions GetEntrySelectionOptions() const =0
bool implValidateNormalized(const OUString &_rText)
Definition: fmtfield.cxx:175
TransitionTable m_aTransitions
Definition: formatter.hxx:64
bool isValidNumericFragment(std::u16string_view _rText)
Definition: fmtfield.cxx:214
NumberValidator(const sal_Unicode _cThSep, const sal_Unicode _cDecSep)
Definition: fmtfield.cxx:87
virtual SotClipboardFormatId GetFormat(const TransferableDataHelper &aHelper) override
virtual void SetValue(tools::Long nNew) override
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
FORMAT_CHANGE_TYPE
Definition: formatter.hxx:77
StateTransitions::value_type Transition
Definition: formatter.hxx:55
@ DIGIT_PRE_COMMA
Definition: formatter.hxx:41
@ EXPONENT_START
Definition: formatter.hxx:44
@ DIGIT_POST_COMMA
Definition: formatter.hxx:43
@ EXPONENT_DIGIT
Definition: formatter.hxx:46
::std::map< State, StateTransitions > TransitionTable
Definition: formatter.hxx:58
::std::map< sal_Unicode, State > StateTransitions
Definition: formatter.hxx:52
const char GetValue[]
SelectionOptions
Definition: settings.hxx:179
sal_uIntPtr sal_uLong
void SetFormat(LotusContext &rContext, SCCOL nCol, SCROW nRow, SCTAB nTab, sal_uInt8 nFormat, sal_uInt8 nSt)
sal_uInt16 sal_Unicode