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