LibreOffice Module vcl (master)  1
fmtfield.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 #ifndef INCLUDED_VCL_FMTFIELD_HXX
21 #define INCLUDED_VCL_FMTFIELD_HXX
22 
23 #include <vcl/spinfld.hxx>
24 #include <memory>
25 
26 class SvNumberFormatter;
27 
28 namespace validation { class NumberValidator; }
29 
31 {
32  KEYONLY = 0x00, // only a new key was set
33  FORMATTER = 0x01, // a new formatter was set, usually implies a change of the key, too
34  PRECISION = 0x02, // a new precision was set
35  THOUSANDSSEP = 0x03, // the thousands separator setting changed
36  CURRENCY_SYMBOL = 0x10,
37  CURRSYM_POSITION = 0x20,
38 };
39 
40 
42 {
43 private:
44  // A SvNumberFormatter is very expensive (regarding time and space), it is a Singleton
46  {
49  public:
51  ~StaticFormatter();
52 
53  operator SvNumberFormatter* () { return GetFormatter(); }
54  VCL_DLLPUBLIC static SvNumberFormatter* GetFormatter();
55  };
56 
57 protected:
58  OUString m_sLastValidText;
59  // Has nothing to do with the current value. It is the last text, which was valid at input (checked by CheckText,
60  // not yet through formatter)
62 
63  double m_dMinValue;
64  double m_dMaxValue;
65  bool m_bHasMin : 1;
66  bool m_bHasMax : 1;
67 
68  bool m_bStrictFormat : 1;
69 
70  bool m_bEnableEmptyField : 1;
71  bool m_bAutoColor : 1;
72  bool m_bEnableNaN : 1;
73  bool m_bDisableRemainderFactor : 1;
74  enum valueState { valueDirty, valueString, valueDouble };
78 
82 
83  double m_dSpinSize;
84  double m_dSpinFirst;
85  double m_dSpinLast;
86 
87  // There is a difference, when text formatting is enabled, if LostFocus formats the current String and displays it,
88  // or if a double is created from the String and then
90  // And with the following members we can use it for formatted text output as well ...
92  OUString m_sDefaultText;
93 
94  // The last color from the Formatter at the last output operation (not we would use it, but you can get it)
96 
98 
99 public:
100  FormattedField(vcl::Window* pParent, WinBits nStyle);
101 
102  // Min-/Max-management
103  bool HasMinValue() const { return m_bHasMin; }
104  void ClearMinValue() { m_bHasMin = false; }
105  void SetMinValue(double dMin);
106  double GetMinValue() const { return m_dMinValue; }
107 
108  bool HasMaxValue() const { return m_bHasMax; }
109  void ClearMaxValue() { m_bHasMax = false; }
110  void SetMaxValue(double dMax);
111  double GetMaxValue() const { return m_dMaxValue; }
112 
113  // Current value
114  void SetValue(double dVal);
115  double GetValue();
116  // The default implementation uses a formatter, if available
117 
118  void SetTextValue(const OUString& rText);
119  // The String is transformed to a double (with a formatter) and SetValue is called afterwards
120 
121  bool IsEmptyFieldEnabled() const { return m_bEnableEmptyField; }
122  void EnableEmptyField(bool bEnable);
123  // If disabled, the value will be reset to the last valid value on leave
124 
125  void SetDefaultValue(double dDefault) { m_dDefaultValue = dDefault; m_ValueState = valueDirty; }
126  // If the current String is invalid, GetValue() returns this value
127  double GetDefaultValue() const { return m_dDefaultValue; }
128 
129  // Settings for the format
130  sal_uLong GetFormatKey() const { return m_nFormatKey; }
131  void SetFormatKey(sal_uLong nFormatKey);
132 
133  SvNumberFormatter* GetFormatter() const { return m_pFormatter; }
134  void SetFormatter(SvNumberFormatter* pFormatter, bool bResetFormat = true);
135  // If bResetFormat is sal_False, the old format is tried to be kept. (expensive, if it is no default format, available in all formatters)
136  // If sal_True, the new FormatKey is set to zero
137 
138  bool GetThousandsSep() const;
139  void SetThousandsSep(bool _bUseSeparator);
140  // the is no check if the current format is numeric, so be cautious when calling these functions
141 
142  void DisableRemainderFactor();
143 
144  sal_uInt16 GetDecimalDigits() const;
145  void SetDecimalDigits(sal_uInt16 _nPrecision);
146  // There is no check if the current format is numeric, so be cautious when calling these functions
147 
148  SvNumberFormatter* StandardFormatter() { return m_aStaticFormatter; }
149  // If no new Formatter is created explicitly, this can be used in SetFormatter...
150 
151  OUString GetFormat(LanguageType& eLang) const;
152  bool SetFormat(const OUString& rFormatString, LanguageType eLang);
153  // sal_False, if the FormatString could not be set (and very probably is invalid)
154  // This Object is shared via all instances, so be careful!
155 
156  bool IsStrictFormat() const { return m_bStrictFormat; }
157  void SetStrictFormat(bool bEnable) { m_bStrictFormat = bEnable; }
158  // Check format during input
159 
160  // Spin-Handling
161  virtual void Up() override;
162  virtual void Down() override;
163  // Default Implementation: +/- default spin size to the double value
164  virtual void First() override;
165  virtual void Last() override;
166  // Default Implementation: Current double is set to the first or last value
167 
168  virtual bool set_property(const OString &rKey, const OUString &rValue) override;
169 
170  void SetSpinSize(double dStep) { m_dSpinSize = dStep; }
171  double GetSpinSize() const { return m_dSpinSize; }
172 
173  void SetSpinFirst(double dFirst) { m_dSpinFirst = dFirst; }
174  double GetSpinFirst() const { return m_dSpinFirst; }
175 
176  void SetSpinLast(double dLast) { m_dSpinLast = dLast; }
177  double GetSpinLast() const { return m_dSpinLast; }
178 
179  bool TreatingAsNumber() const { return m_bTreatAsNumber; }
180  void TreatAsNumber(bool bDoSo) { m_bTreatAsNumber = bDoSo; }
181 
182  void SetOutputHdl(const Link<Edit&, bool>& rLink) { m_aOutputHdl = rLink; }
183  void SetInputHdl(const Link<sal_Int64*,TriState>& rLink) { m_aInputHdl = rLink; }
184 public:
185  virtual void SetText( const OUString& rStr ) override;
186  virtual void SetText( const OUString& rStr, const Selection& rNewSelection ) override;
187 
188  //The following methods are interesting, if m_bTreatAsNumber is set to sal_False
189  //If someone does not care about all the double handling and just wants to print the text formatted.
190  //(((The text will be formatted, using the Formatter, and then set)
191  void SetTextFormatted(const OUString& rText);
192  OUString const & GetTextValue() const;
193 
194  void SetDefaultText(const OUString& rDefault) { m_sDefaultText = rDefault; }
195  const OUString& GetDefaultText() const { return m_sDefaultText; }
196 
197  // The last colour from the Formatter's last output operation. Output operations get triggered by:
198  // SetValue, SetTextValue, SetTextFormatted, also indirectly via SetMin - / -MaxValue
199  Color* GetLastOutputColor() const { return m_pLastOutputColor; }
200 
205  void Commit();
206 
207  // enable automatic coloring. if set to sal_True, and the format the field is working with for any current value
208  // says that it has to be painted in a special color (e.g. a format where negative numbers should be printed
209  // red), the text is painted with that color automatically.
210  // The color used is the same as returned by GetLastOutputColor()
211  void SetAutoColor(bool _bAutomatic);
212 
227  void EnableNotANumber( bool _bEnable );
228 
233  void UseInputStringForFormatting();
234  bool IsUsingInputStringForFormatting() const { return m_bUseInputStringForFormatting;}
235 
236 protected:
237  virtual bool EventNotify(NotifyEvent& rNEvt) override;
238  void impl_Modify(bool makeValueDirty = true);
239  virtual void Modify() override;
240 
241  // Override CheckText for input-time checks
242  virtual bool CheckText(const OUString&) const { return true; }
243 
244  // any aspect of the current format has changed
245  virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat);
246 
247  void ImplSetTextImpl(const OUString& rNew, Selection const * pNewSel);
248  void ImplSetValue(double dValue, bool bForce);
249  bool ImplGetValue(double& dNewVal);
250 
251  void ImplSetFormatKey(sal_uLong nFormatKey);
252  // SetFormatKey without FormatChanged notification
253 
254  SvNumberFormatter* CreateFormatter() { SetFormatter(StandardFormatter()); return m_pFormatter; }
255  SvNumberFormatter* ImplGetFormatter() const { return m_pFormatter ? m_pFormatter : const_cast<FormattedField*>(this)->CreateFormatter(); }
256 
257  bool PreNotify(NotifyEvent& rNEvt) override;
258 
259  void ReFormat();
260 private:
263 };
264 
266 {
267 public:
268  DoubleNumericField(vcl::Window* pParent, WinBits nStyle);
269 
270  virtual ~DoubleNumericField() override;
271 
272 private:
273  virtual bool CheckText(const OUString& sText) const override;
274 
275  virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat) override;
276  void ResetConformanceTester();
277 
278  std::unique_ptr<validation::NumberValidator> m_pNumberValidator;
279 };
280 
281 
283 {
284 public:
285  DoubleCurrencyField(vcl::Window* pParent, WinBits nStyle);
286 
287  const OUString& getCurrencySymbol() const { return m_sCurrencySymbol; }
288  void setCurrencySymbol(const OUString& rSymbol);
289 
290  bool getPrependCurrSym() const { return m_bPrependCurrSym; }
291  void setPrependCurrSym(bool _bPrepend);
292 
293 private:
294  virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat) override;
295 
296  void UpdateCurrencyFormat();
297 
301 };
302 
303 #endif // INCLUDED_VCL_FMTFIELD_HXX
304 
305 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool HasMaxValue() const
Definition: fmtfield.hxx:108
void SetStrictFormat(bool bEnable)
Definition: fmtfield.hxx:157
sal_uLong GetFormatKey() const
Definition: fmtfield.hxx:130
bool IsEmptyFieldEnabled() const
Definition: fmtfield.hxx:121
SvNumberFormatter * ImplGetFormatter() const
Definition: fmtfield.hxx:255
std::string GetValue
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
FORMAT_CHANGE_TYPE
Definition: fmtfield.hxx:30
StaticFormatter m_aStaticFormatter
Definition: fmtfield.hxx:81
sal_uIntPtr sal_uLong
void SetDefaultText(const OUString &rDefault)
Definition: fmtfield.hxx:194
void ClearMinValue()
Definition: fmtfield.hxx:104
double GetMinValue() const
Definition: fmtfield.hxx:106
Link< sal_Int64 *, TriState > m_aInputHdl
Definition: fmtfield.hxx:262
OUString m_sDefaultText
Definition: fmtfield.hxx:92
SvNumberFormatter * CreateFormatter()
Definition: fmtfield.hxx:254
void ClearMaxValue()
Definition: fmtfield.hxx:109
double GetSpinFirst() const
Definition: fmtfield.hxx:174
virtual bool set_property(const OString &rKey, const OUString &rValue) override
Definition: edit.cxx:176
OUString m_sCurrencySymbol
Definition: fmtfield.hxx:298
virtual void Up()
Definition: spinfld.cxx:359
double GetMaxValue() const
Definition: fmtfield.hxx:111
SvNumberFormatter * StandardFormatter()
Definition: fmtfield.hxx:148
double m_dDefaultValue
Definition: fmtfield.hxx:77
static sal_uLong s_nReferences
Definition: fmtfield.hxx:48
bool IsUsingInputStringForFormatting() const
Definition: fmtfield.hxx:234
bool m_bUseInputStringForFormatting
Definition: fmtfield.hxx:97
sal_Int64 WinBits
double m_dCurrentValue
Definition: fmtfield.hxx:76
double m_dMinValue
Definition: fmtfield.hxx:63
const OUString & GetDefaultText() const
Definition: fmtfield.hxx:195
Color * GetLastOutputColor() const
Definition: fmtfield.hxx:199
bool HasMinValue() const
Definition: fmtfield.hxx:103
double m_dSpinFirst
Definition: fmtfield.hxx:84
std::unique_ptr< validation::NumberValidator > m_pNumberValidator
Definition: fmtfield.hxx:278
virtual void Down()
Definition: spinfld.cxx:364
static SvNumberFormatter * s_cFormatter
Definition: fmtfield.hxx:47
Color * m_pLastOutputColor
Definition: fmtfield.hxx:95
Selection m_aLastSelection
Definition: fmtfield.hxx:61
virtual bool EventNotify(NotifyEvent &rNEvt) override
Definition: spinfld.cxx:490
OUString m_sCurrentTextValue
Definition: fmtfield.hxx:91
Link< Edit &, bool > m_aOutputHdl
Definition: fmtfield.hxx:261
sal_uLong m_nFormatKey
Definition: fmtfield.hxx:79
bool IsStrictFormat() const
Definition: fmtfield.hxx:156
void SetDefaultValue(double dDefault)
Definition: fmtfield.hxx:125
double m_dSpinSize
Definition: fmtfield.hxx:83
virtual void Modify()
Definition: edit.cxx:2305
void SetSpinLast(double dLast)
Definition: fmtfield.hxx:176
SvNumberFormatter * m_pFormatter
Definition: fmtfield.hxx:80
double GetDefaultValue() const
Definition: fmtfield.hxx:127
virtual bool CheckText(const OUString &) const
Definition: fmtfield.hxx:242
double m_dSpinLast
Definition: fmtfield.hxx:85
SvNumberFormatter * GetFormatter() const
Definition: fmtfield.hxx:133
virtual void First()
Definition: spinfld.cxx:369
OUString m_sLastValidText
Definition: fmtfield.hxx:58
double GetSpinSize() const
Definition: fmtfield.hxx:171
const OUString & getCurrencySymbol() const
Definition: fmtfield.hxx:287
bool m_bTreatAsNumber
Definition: fmtfield.hxx:89
void SetOutputHdl(const Link< Edit &, bool > &rLink)
Definition: fmtfield.hxx:182
virtual void Last()
Definition: spinfld.cxx:374
virtual bool PreNotify(NotifyEvent &rNEvt) override
Definition: spinfld.cxx:835
void SetInputHdl(const Link< sal_Int64 *, TriState > &rLink)
Definition: fmtfield.hxx:183
void TreatAsNumber(bool bDoSo)
Definition: fmtfield.hxx:180
bool getPrependCurrSym() const
Definition: fmtfield.hxx:290
double m_dMaxValue
Definition: fmtfield.hxx:64
virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat)
Definition: fmtfield.cxx:693
valueState m_ValueState
Definition: fmtfield.hxx:75
virtual void SetText(const OUString &rStr) override
Definition: edit.cxx:2545
void SetSpinFirst(double dFirst)
Definition: fmtfield.hxx:173
void SetSpinSize(double dStep)
Definition: fmtfield.hxx:170
bool TreatingAsNumber() const
Definition: fmtfield.hxx:179
double GetSpinLast() const
Definition: fmtfield.hxx:177
void SetFormat(LotusContext &rContext, SCCOL nCol, SCROW nRow, SCTAB nTab, sal_uInt8 nFormat, sal_uInt8 nSt)