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