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 <vcl/settings.hxx>
26 #include <map>
27 #include <memory>
28 #include <string_view>
29 
30 class SvNumberFormatter;
31 
32 namespace validation
33 {
34  // the states of our automat.
35  enum State
36  {
37  START, // at the very start of the string
38  NUM_START, // the very start of the number
39 
40  DIGIT_PRE_COMMA, // some pre-comma digits are read, perhaps including some thousand separators
41 
42  DIGIT_POST_COMMA, // reading digits after the comma
43  EXPONENT_START, // at the very start of the exponent value
44  // (means: not including the "e" which denotes the exponent)
45  EXPONENT_DIGIT, // currently reading the digits of the exponent
46 
47  END // reached the end of the string
48  };
49 
50  // a row in the transition table (means the set of states to be reached from a given state)
51  typedef ::std::map< sal_Unicode, State > StateTransitions;
52 
53  // a single transition
54  typedef StateTransitions::value_type Transition;
55 
56  // the complete transition table
57  typedef ::std::map< State, StateTransitions > TransitionTable;
58 
59  // the validator class
61  {
62  private:
63  TransitionTable m_aTransitions;
64 
65  public:
66  NumberValidator( const sal_Unicode _cThSep, const sal_Unicode _cDecSep );
67 
68  bool isValidNumericFragment( std::u16string_view _rText );
69 
70  private:
71  bool implValidateNormalized( const OUString& _rText );
72  };
73 }
74 
76 {
77  KEYONLY = 0x00, // only a new key was set
78  FORMATTER = 0x01, // a new formatter was set, usually implies a change of the key, too
79  PRECISION = 0x02, // a new precision was set
80  THOUSANDSSEP = 0x03, // the thousands separator setting changed
81  CURRENCY_SYMBOL = 0x10,
82  CURRSYM_POSITION = 0x20,
83 };
84 
86 {
87 private:
88  // A SvNumberFormatter is very expensive (regarding time and space), it is a Singleton
90  {
93  public:
95  ~StaticFormatter();
96 
97  operator SvNumberFormatter* () { return GetFormatter(); }
98  UNLESS_MERGELIBS(VCL_DLLPUBLIC) static SvNumberFormatter* GetFormatter();
99  };
100 
101 protected:
102  OUString m_sLastValidText;
103  // Has nothing to do with the current value. It is the last text, which was valid at input (checked by CheckText,
104  // not yet through formatter)
105  Selection m_aLastSelection;
106 
107  double m_dMinValue;
108  double m_dMaxValue;
109  bool m_bHasMin : 1;
110  bool m_bHasMax : 1;
111 
112  bool m_bWrapOnLimits : 1;
113  bool m_bStrictFormat : 1;
114 
115  bool m_bEnableEmptyField : 1;
116  bool m_bAutoColor : 1;
117  bool m_bEnableNaN : 1;
118  bool m_bDisableRemainderFactor : 1;
119  enum valueState { valueDirty, valueString, valueDouble };
123 
127 
128  double m_dSpinSize;
129  double m_dSpinFirst;
130  double m_dSpinLast;
131 
132  // There is a difference, when text formatting is enabled, if LostFocus formats the current String and displays it,
133  // or if a double is created from the String and then
135  // And with the following members we can use it for formatted text output as well ...
137  OUString m_sDefaultText;
138 
139  // The last color from the Formatter at the last output operation (not we would use it, but you can get it)
141 
143 
146 
147 public:
148  Formatter();
149  virtual ~Formatter();
150 
151  void SetFieldText(const OUString& rText, const Selection& rNewSelection);
152 
153  virtual Selection GetEntrySelection() const = 0;
154  virtual OUString GetEntryText() const = 0;
155  virtual SelectionOptions GetEntrySelectionOptions() const = 0;
156  virtual void SetEntryText(const OUString& rText, const Selection& rSel) = 0;
157  virtual void SetEntryTextColor(const Color* pColor) = 0;
158  virtual void FieldModified() = 0;
159 
160  // Min-/Max-management
161  bool HasMinValue() const { return m_bHasMin; }
162  virtual void ClearMinValue() { m_bHasMin = false; }
163  virtual void SetMinValue(double dMin);
164  double GetMinValue() const { return m_dMinValue; }
165 
166  bool HasMaxValue() const { return m_bHasMax; }
167  virtual void ClearMaxValue() { m_bHasMax = false; }
168  virtual void SetMaxValue(double dMax);
169  double GetMaxValue() const { return m_dMaxValue; }
170 
171  // Current value
172  void SetValue(double dVal);
173  double GetValue();
174  // The default implementation uses a formatter, if available
175 
176  void SetTextValue(const OUString& rText);
177  // The String is transformed to a double (with a formatter) and SetValue is called afterwards
178 
179  bool IsEmptyFieldEnabled() const { return m_bEnableEmptyField; }
180  void EnableEmptyField(bool bEnable);
181  // If disabled, the value will be reset to the last valid value on leave
182 
183  void SetDefaultValue(double dDefault) { m_dDefaultValue = dDefault; m_ValueState = valueDirty; }
184  // If the current String is invalid, GetValue() returns this value
185  double GetDefaultValue() const { return m_dDefaultValue; }
186 
187  void SetLastSelection(const Selection& rSelection) { m_aLastSelection = rSelection; }
188 
189  // Settings for the format
190  sal_uLong GetFormatKey() const { return m_nFormatKey; }
191  void SetFormatKey(sal_uLong nFormatKey);
192 
193  SvNumberFormatter* GetOrCreateFormatter() const { return m_pFormatter ? m_pFormatter : const_cast<Formatter*>(this)->CreateFormatter(); }
194 
195  SvNumberFormatter* GetFormatter() const { return m_pFormatter; }
196  void SetFormatter(SvNumberFormatter* pFormatter, bool bResetFormat = true);
197  // If bResetFormat is sal_False, the old format is tried to be kept. (expensive, if it is no default format, available in all formatters)
198  // If sal_True, the new FormatKey is set to zero
199 
200  bool GetThousandsSep() const;
201  void SetThousandsSep(bool _bUseSeparator);
202  // the is no check if the current format is numeric, so be cautious when calling these functions
203 
204  void DisableRemainderFactor();
205  bool GetDisableRemainderFactor() const { return m_bDisableRemainderFactor; }
206 
207  void SetWrapOnLimits(bool bWrapOnLimits) { m_bWrapOnLimits = bWrapOnLimits; }
208 
209  sal_uInt16 GetDecimalDigits() const;
210  void SetDecimalDigits(sal_uInt16 _nPrecision);
211  // There is no check if the current format is numeric, so be cautious when calling these functions
212 
213  SvNumberFormatter* StandardFormatter() { return m_aStaticFormatter; }
214  // If no new Formatter is created explicitly, this can be used in SetFormatter...
215 
216  OUString GetFormat(LanguageType& eLang) const;
217  bool SetFormat(const OUString& rFormatString, LanguageType eLang);
218  // sal_False, if the FormatString could not be set (and very probably is invalid)
219  // This Object is shared via all instances, so be careful!
220 
221  bool IsStrictFormat() const { return m_bStrictFormat; }
222  void SetStrictFormat(bool bEnable) { m_bStrictFormat = bEnable; }
223  // Check format during input
224 
225  virtual void SetSpinSize(double dStep) { m_dSpinSize = dStep; }
226  double GetSpinSize() const { return m_dSpinSize; }
227 
228  void SetSpinFirst(double dFirst) { m_dSpinFirst = dFirst; }
229  double GetSpinFirst() const { return m_dSpinFirst; }
230 
231  void SetSpinLast(double dLast) { m_dSpinLast = dLast; }
232  double GetSpinLast() const { return m_dSpinLast; }
233 
234  bool TreatingAsNumber() const { return m_bTreatAsNumber; }
235  void TreatAsNumber(bool bDoSo) { m_bTreatAsNumber = bDoSo; }
236 
237  void SetInputHdl(const Link<sal_Int64*,TriState>& rLink) { m_aInputHdl = rLink; }
238  void SetOutputHdl(const Link<LinkParamNone*, bool>& rLink) { m_aOutputHdl = rLink; }
239 public:
240 
241  //The following methods are interesting, if m_bTreatAsNumber is set to sal_False
242  //If someone does not care about all the double handling and just wants to print the text formatted.
243  //(((The text will be formatted, using the Formatter, and then set)
244  void SetTextFormatted(const OUString& rText);
245  OUString const & GetTextValue() const;
246 
247  void SetDefaultText(const OUString& rDefault) { m_sDefaultText = rDefault; }
248  const OUString& GetDefaultText() const { return m_sDefaultText; }
249 
250  // The last colour from the Formatter's last output operation. Output operations get triggered by:
251  // SetValue, SetTextValue, SetTextFormatted, also indirectly via SetMin - / -MaxValue
252  const Color* GetLastOutputColor() const { return m_pLastOutputColor; }
253 
258  void Commit();
259 
260  // enable automatic coloring. if set to sal_True, and the format the field is working with for any current value
261  // says that it has to be painted in a special color (e.g. a format where negative numbers should be printed
262  // red), the text is painted with that color automatically.
263  // The color used is the same as returned by GetLastOutputColor()
264  void SetAutoColor(bool _bAutomatic);
265 
280  void EnableNotANumber( bool _bEnable );
281 
286  void UseInputStringForFormatting();
287  bool IsUsingInputStringForFormatting() const { return m_bUseInputStringForFormatting;}
288 
289  void Modify(bool makeValueDirty = true);
290 
291  void EntryLostFocus();
292 
293  void ReFormat();
294 
295  // any aspect of the current format has changed
296  virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat);
297 
298 protected:
299 
300  // Override CheckText for input-time checks
301  virtual bool CheckText(const OUString&) const { return true; }
302 
303  void ImplSetTextImpl(const OUString& rNew, Selection const * pNewSel);
304  void ImplSetValue(double dValue, bool bForce);
305  bool ImplGetValue(double& dNewVal);
306 
307  void ImplSetFormatKey(sal_uLong nFormatKey);
308  // SetFormatKey without FormatChanged notification
309 
310  SvNumberFormatter* CreateFormatter() { SetFormatter(StandardFormatter()); return m_pFormatter; }
311 
312  virtual void UpdateCurrentValue(double dCurrentValue) { m_dCurrentValue = dCurrentValue; }
313 };
314 
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool HasMaxValue() const
Definition: formatter.hxx:166
void SetSpinLast(double dLast)
Definition: formatter.hxx:231
static SvNumberFormatter * s_cFormatter
Definition: formatter.hxx:91
void SetInputHdl(const Link< sal_Int64 *, TriState > &rLink)
Definition: formatter.hxx:237
double m_dSpinSize
Definition: formatter.hxx:128
std::string GetValue
SelectionOptions
Definition: settings.hxx:181
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
virtual void ClearMaxValue()
Definition: formatter.hxx:167
bool TreatingAsNumber() const
Definition: formatter.hxx:234
double m_dDefaultValue
Definition: formatter.hxx:122
sal_uIntPtr sal_uLong
SvNumberFormatter * StandardFormatter()
Definition: formatter.hxx:213
FORMAT_CHANGE_TYPE
Definition: formatter.hxx:75
::std::map< sal_Unicode, State > StateTransitions
Definition: formatter.hxx:51
double m_dSpinFirst
Definition: formatter.hxx:129
void SetDefaultValue(double dDefault)
Definition: formatter.hxx:183
bool IsUsingInputStringForFormatting() const
Definition: formatter.hxx:287
bool m_bUseInputStringForFormatting
Definition: formatter.hxx:142
TransitionTable m_aTransitions
Definition: formatter.hxx:63
void SetSpinFirst(double dFirst)
Definition: formatter.hxx:228
const Color * GetLastOutputColor() const
Definition: formatter.hxx:252
OUString m_sCurrentTextValue
Definition: formatter.hxx:136
bool IsEmptyFieldEnabled() const
Definition: formatter.hxx:179
sal_uInt16 sal_Unicode
StateTransitions::value_type Transition
Definition: formatter.hxx:54
virtual void UpdateCurrentValue(double dCurrentValue)
Definition: formatter.hxx:312
SvNumberFormatter * CreateFormatter()
Definition: formatter.hxx:310
void TreatAsNumber(bool bDoSo)
Definition: formatter.hxx:235
class UNLESS_MERGELIBS(VCL_DLLPUBLIC) Accelerator
Definition: accel.hxx:33
double GetSpinSize() const
Definition: formatter.hxx:226
void SetOutputHdl(const Link< LinkParamNone *, bool > &rLink)
Definition: formatter.hxx:238
StaticFormatter m_aStaticFormatter
Definition: formatter.hxx:126
const Color * m_pLastOutputColor
Definition: formatter.hxx:140
double GetMaxValue() const
Definition: formatter.hxx:169
virtual void ClearMinValue()
Definition: formatter.hxx:162
sal_uLong GetFormatKey() const
Definition: formatter.hxx:190
bool m_bTreatAsNumber
Definition: formatter.hxx:134
SvNumberFormatter * m_pFormatter
Definition: formatter.hxx:125
bool implValidateNormalized(const OUString &_rText)
Definition: fmtfield.cxx:173
double GetDefaultValue() const
Definition: formatter.hxx:185
SvNumberFormatter * GetFormatter() const
Definition: formatter.hxx:195
valueState m_ValueState
Definition: formatter.hxx:120
double GetSpinLast() const
Definition: formatter.hxx:232
void SetDefaultText(const OUString &rDefault)
Definition: formatter.hxx:247
bool GetDisableRemainderFactor() const
Definition: formatter.hxx:205
void SetStrictFormat(bool bEnable)
Definition: formatter.hxx:222
double m_dSpinLast
Definition: formatter.hxx:130
NumberValidator(const sal_Unicode _cThSep, const sal_Unicode _cDecSep)
Definition: fmtfield.cxx:85
double m_dCurrentValue
Definition: formatter.hxx:121
bool HasMinValue() const
Definition: formatter.hxx:161
void SetWrapOnLimits(bool bWrapOnLimits)
Definition: formatter.hxx:207
Link< sal_Int64 *, TriState > m_aInputHdl
Definition: formatter.hxx:144
virtual bool CheckText(const OUString &) const
Definition: formatter.hxx:301
void SetLastSelection(const Selection &rSelection)
Definition: formatter.hxx:187
static sal_uLong s_nReferences
Definition: formatter.hxx:92
SvNumberFormatter * GetOrCreateFormatter() const
Definition: formatter.hxx:193
OUString m_sDefaultText
Definition: formatter.hxx:137
double GetMinValue() const
Definition: formatter.hxx:164
double GetSpinFirst() const
Definition: formatter.hxx:229
bool IsStrictFormat() const
Definition: formatter.hxx:221
bool isValidNumericFragment(std::u16string_view _rText)
Definition: fmtfield.cxx:212
const OUString & GetDefaultText() const
Definition: formatter.hxx:248
sal_uLong m_nFormatKey
Definition: formatter.hxx:124
::std::map< State, StateTransitions > TransitionTable
Definition: formatter.hxx:57
virtual void SetSpinSize(double dStep)
Definition: formatter.hxx:225
void SetFormat(LotusContext &rContext, SCCOL nCol, SCROW nRow, SCTAB nTab, sal_uInt8 nFormat, sal_uInt8 nSt)
Link< LinkParamNone *, bool > m_aOutputHdl
Definition: formatter.hxx:145