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