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 
29 class SvNumberFormatter;
30 
31 namespace validation
32 {
33  // the states of our automat.
34  enum State
35  {
36  START, // at the very start of the string
37  NUM_START, // the very start of the number
38 
39  DIGIT_PRE_COMMA, // some pre-comma digits are read, perhaps including some thousand separators
40 
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
45 
46  END // reached the end of the string
47  };
48 
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;
51 
52  // a single transition
53  typedef StateTransitions::value_type Transition;
54 
55  // the complete transition table
56  typedef ::std::map< State, StateTransitions > TransitionTable;
57 
58  // the validator class
60  {
61  private:
62  TransitionTable m_aTransitions;
63 
64  public:
65  NumberValidator( const sal_Unicode _cThSep, const sal_Unicode _cDecSep );
66 
67  bool isValidNumericFragment( const OUString& _rText );
68 
69  private:
70  bool implValidateNormalized( const OUString& _rText );
71  };
72 }
73 
75 {
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,
82 };
83 
85 {
86 private:
87  // A SvNumberFormatter is very expensive (regarding time and space), it is a Singleton
89  {
92  public:
94  ~StaticFormatter();
95 
96  operator SvNumberFormatter* () { return GetFormatter(); }
97  UNLESS_MERGELIBS(VCL_DLLPUBLIC) static SvNumberFormatter* GetFormatter();
98  };
99 
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;
105 
106  double m_dMinValue;
107  double m_dMaxValue;
108  bool m_bHasMin : 1;
109  bool m_bHasMax : 1;
110 
111  bool m_bWrapOnLimits : 1;
112  bool m_bStrictFormat : 1;
113 
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 };
122 
126 
127  double m_dSpinSize;
128  double m_dSpinFirst;
129  double m_dSpinLast;
130 
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
134  // And with the following members we can use it for formatted text output as well ...
136  OUString m_sDefaultText;
137 
138  // The last color from the Formatter at the last output operation (not we would use it, but you can get it)
140 
142 
145 
146 public:
147  Formatter();
148  virtual ~Formatter();
149 
150  void SetFieldText(const OUString& rText, const Selection& rNewSelection);
151 
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;
158 
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; }
164 
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; }
169 
170  // Current value
171  void SetValue(double dVal);
172  double GetValue();
173  // The default implementation uses a formatter, if available
174 
175  void SetTextValue(const OUString& rText);
176  // The String is transformed to a double (with a formatter) and SetValue is called afterwards
177 
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
181 
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; }
185 
186  void SetLastSelection(const Selection& rSelection) { m_aLastSelection = rSelection; }
187 
188  // Settings for the format
189  sal_uLong GetFormatKey() const { return m_nFormatKey; }
190  void SetFormatKey(sal_uLong nFormatKey);
191 
192  SvNumberFormatter* GetOrCreateFormatter() const { return m_pFormatter ? m_pFormatter : const_cast<Formatter*>(this)->CreateFormatter(); }
193 
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
198 
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
202 
203  void DisableRemainderFactor();
204  bool GetDisableRemainderFactor() const { return m_bDisableRemainderFactor; }
205 
206  void SetWrapOnLimits(bool bWrapOnLimits) { m_bWrapOnLimits = bWrapOnLimits; }
207 
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
211 
212  SvNumberFormatter* StandardFormatter() { return m_aStaticFormatter; }
213  // If no new Formatter is created explicitly, this can be used in SetFormatter...
214 
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!
219 
220  bool IsStrictFormat() const { return m_bStrictFormat; }
221  void SetStrictFormat(bool bEnable) { m_bStrictFormat = bEnable; }
222  // Check format during input
223 
224  virtual void SetSpinSize(double dStep) { m_dSpinSize = dStep; }
225  double GetSpinSize() const { return m_dSpinSize; }
226 
227  void SetSpinFirst(double dFirst) { m_dSpinFirst = dFirst; }
228  double GetSpinFirst() const { return m_dSpinFirst; }
229 
230  void SetSpinLast(double dLast) { m_dSpinLast = dLast; }
231  double GetSpinLast() const { return m_dSpinLast; }
232 
233  bool TreatingAsNumber() const { return m_bTreatAsNumber; }
234  void TreatAsNumber(bool bDoSo) { m_bTreatAsNumber = bDoSo; }
235 
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:
239 
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;
245 
246  void SetDefaultText(const OUString& rDefault) { m_sDefaultText = rDefault; }
247  const OUString& GetDefaultText() const { return m_sDefaultText; }
248 
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; }
252 
257  void Commit();
258 
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);
264 
279  void EnableNotANumber( bool _bEnable );
280 
285  void UseInputStringForFormatting();
286  bool IsUsingInputStringForFormatting() const { return m_bUseInputStringForFormatting;}
287 
288  void Modify(bool makeValueDirty = true);
289 
290  void EntryLostFocus();
291 
292  void ReFormat();
293 
294  // any aspect of the current format has changed
295  virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat);
296 
297 protected:
298 
299  // Override CheckText for input-time checks
300  virtual bool CheckText(const OUString&) const { return true; }
301 
302  void ImplSetTextImpl(const OUString& rNew, Selection const * pNewSel);
303  void ImplSetValue(double dValue, bool bForce);
304  bool ImplGetValue(double& dNewVal);
305 
306  void ImplSetFormatKey(sal_uLong nFormatKey);
307  // SetFormatKey without FormatChanged notification
308 
309  SvNumberFormatter* CreateFormatter() { SetFormatter(StandardFormatter()); return m_pFormatter; }
310 
311  virtual void UpdateCurrentValue(double dCurrentValue) { m_dCurrentValue = dCurrentValue; }
312 };
313 
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool HasMaxValue() const
Definition: formatter.hxx:165
void SetSpinLast(double dLast)
Definition: formatter.hxx:230
bool isValidNumericFragment(const OUString &_rText)
Definition: fmtfield.cxx:212
static SvNumberFormatter * s_cFormatter
Definition: formatter.hxx:90
void SetInputHdl(const Link< sal_Int64 *, TriState > &rLink)
Definition: formatter.hxx:236
double m_dSpinSize
Definition: formatter.hxx:127
std::string GetValue
SelectionOptions
Definition: settings.hxx:181
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
virtual void ClearMaxValue()
Definition: formatter.hxx:166
bool TreatingAsNumber() const
Definition: formatter.hxx:233
double m_dDefaultValue
Definition: formatter.hxx:121
sal_uIntPtr sal_uLong
SvNumberFormatter * StandardFormatter()
Definition: formatter.hxx:212
FORMAT_CHANGE_TYPE
Definition: formatter.hxx:74
::std::map< sal_Unicode, State > StateTransitions
Definition: formatter.hxx:50
double m_dSpinFirst
Definition: formatter.hxx:128
void SetDefaultValue(double dDefault)
Definition: formatter.hxx:182
bool IsUsingInputStringForFormatting() const
Definition: formatter.hxx:286
bool m_bUseInputStringForFormatting
Definition: formatter.hxx:141
TransitionTable m_aTransitions
Definition: formatter.hxx:62
void SetSpinFirst(double dFirst)
Definition: formatter.hxx:227
const Color * GetLastOutputColor() const
Definition: formatter.hxx:251
OUString m_sCurrentTextValue
Definition: formatter.hxx:135
bool IsEmptyFieldEnabled() const
Definition: formatter.hxx:178
sal_uInt16 sal_Unicode
StateTransitions::value_type Transition
Definition: formatter.hxx:53
virtual void UpdateCurrentValue(double dCurrentValue)
Definition: formatter.hxx:311
SvNumberFormatter * CreateFormatter()
Definition: formatter.hxx:309
void TreatAsNumber(bool bDoSo)
Definition: formatter.hxx:234
class UNLESS_MERGELIBS(VCL_DLLPUBLIC) Accelerator
Definition: accel.hxx:33
double GetSpinSize() const
Definition: formatter.hxx:225
void SetOutputHdl(const Link< LinkParamNone *, bool > &rLink)
Definition: formatter.hxx:237
StaticFormatter m_aStaticFormatter
Definition: formatter.hxx:125
const Color * m_pLastOutputColor
Definition: formatter.hxx:139
double GetMaxValue() const
Definition: formatter.hxx:168
virtual void ClearMinValue()
Definition: formatter.hxx:161
sal_uLong GetFormatKey() const
Definition: formatter.hxx:189
bool m_bTreatAsNumber
Definition: formatter.hxx:133
SvNumberFormatter * m_pFormatter
Definition: formatter.hxx:124
bool implValidateNormalized(const OUString &_rText)
Definition: fmtfield.cxx:173
double GetDefaultValue() const
Definition: formatter.hxx:184
SvNumberFormatter * GetFormatter() const
Definition: formatter.hxx:194
valueState m_ValueState
Definition: formatter.hxx:119
double GetSpinLast() const
Definition: formatter.hxx:231
void SetDefaultText(const OUString &rDefault)
Definition: formatter.hxx:246
bool GetDisableRemainderFactor() const
Definition: formatter.hxx:204
void SetStrictFormat(bool bEnable)
Definition: formatter.hxx:221
double m_dSpinLast
Definition: formatter.hxx:129
NumberValidator(const sal_Unicode _cThSep, const sal_Unicode _cDecSep)
Definition: fmtfield.cxx:85
double m_dCurrentValue
Definition: formatter.hxx:120
bool HasMinValue() const
Definition: formatter.hxx:160
void SetWrapOnLimits(bool bWrapOnLimits)
Definition: formatter.hxx:206
Link< sal_Int64 *, TriState > m_aInputHdl
Definition: formatter.hxx:143
virtual bool CheckText(const OUString &) const
Definition: formatter.hxx:300
void SetLastSelection(const Selection &rSelection)
Definition: formatter.hxx:186
static sal_uLong s_nReferences
Definition: formatter.hxx:91
SvNumberFormatter * GetOrCreateFormatter() const
Definition: formatter.hxx:192
OUString m_sDefaultText
Definition: formatter.hxx:136
double GetMinValue() const
Definition: formatter.hxx:163
double GetSpinFirst() const
Definition: formatter.hxx:228
bool IsStrictFormat() const
Definition: formatter.hxx:220
const OUString & GetDefaultText() const
Definition: formatter.hxx:247
sal_uLong m_nFormatKey
Definition: formatter.hxx:123
::std::map< State, StateTransitions > TransitionTable
Definition: formatter.hxx:56
virtual void SetSpinSize(double dStep)
Definition: formatter.hxx:224
void SetFormat(LotusContext &rContext, SCCOL nCol, SCROW nRow, SCTAB nTab, sal_uInt8 nFormat, sal_uInt8 nSt)
Link< LinkParamNone *, bool > m_aOutputHdl
Definition: formatter.hxx:144