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