Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef _FMTFIELD_HXX_
30 : : #define _FMTFIELD_HXX_
31 : :
32 : : #include "svtools/svtdllapi.h"
33 : : #include <vcl/spinfld.hxx>
34 : : #include <svl/zforlist.hxx>
35 : :
36 : : namespace validation { class NumberValidator; }
37 : :
38 : : typedef sal_uInt16 FORMAT_CHANGE_TYPE;
39 : : #define FCT_KEYONLY 0x00 // only a new key was set
40 : : #define FCT_FORMATTER 0x01 // a new formatter weas set, usually implies a change of the key, too
41 : : #define FCT_PRECISION 0x02 // a new precision was set
42 : : #define FCT_THOUSANDSSEP 0x03 // the thousands separator setting changed
43 : :
44 : : //------------------------------------------------------------------------------
45 : : class SVT_DLLPUBLIC FormattedField : public SpinField
46 : : {
47 : : private:
48 : : // Da ein SvNumberFormatter eine ziemlich teure (sowohl zeit- als auch platz-maessig) Angelegenheit ist,
49 : : // haelt sich nicht jedes Field, an dem kein Formatter gesetzt wurde, eine eigenen Instanz, sondern es gibt nur eine
50 : : // einzige statische.
51 : : class StaticFormatter
52 : : {
53 : : static SvNumberFormatter* s_cFormatter;
54 : : static sal_uLong s_nReferences;
55 : : public:
56 : : StaticFormatter();
57 : : ~StaticFormatter();
58 : :
59 : 70 : operator SvNumberFormatter* () { return GetFormatter(); }
60 : : SVT_DLLPUBLIC SvNumberFormatter* GetFormatter();
61 : : };
62 : :
63 : : protected:
64 : : String m_sLastValidText;
65 : : // hat nichts mit dem current value zu tun, ist der letzte Text, der waehrend einer Eingabe als gueltig erkannt
66 : : // wurde (also durch CheckText geprueft, nicht durch den Formatter gejagt)
67 : : Selection m_aLastSelection;
68 : :
69 : : double m_dMinValue;
70 : : double m_dMaxValue;
71 : : sal_Bool m_bHasMin : 1;
72 : : sal_Bool m_bHasMax : 1;
73 : :
74 : : sal_Bool m_bStrictFormat : 1;
75 : :
76 : : sal_Bool m_bValueDirty : 1;
77 : : sal_Bool m_bEnableEmptyField : 1;
78 : : sal_Bool m_bAutoColor : 1;
79 : : sal_Bool m_bEnableNaN : 1;
80 : : double m_dCurrentValue;
81 : : double m_dDefaultValue;
82 : :
83 : : sal_uLong m_nFormatKey;
84 : : SvNumberFormatter* m_pFormatter;
85 : : StaticFormatter m_aStaticFormatter;
86 : :
87 : : double m_dSpinSize;
88 : : double m_dSpinFirst;
89 : : double m_dSpinLast;
90 : :
91 : : // es macht einen Unterschied, ob man bei eingestellter Textformatierung beim LostFocus den aktuellen String durch
92 : : // den Formatter jagt und das Ergebnis anzeigt oder erst aus dem String ein double macht, das formatiert und dann
93 : : // ausgibt
94 : : sal_Bool m_bTreatAsNumber;
95 : : // und mit den folgenden Members koennen wir das Ganze hier auch zur formatierten Text-Ausgabe benutzen ...
96 : : String m_sCurrentTextValue;
97 : : String m_sDefaultText;
98 : :
99 : : // die bei der letzten Ausgabe-Operation vom Formatter gelieferte Farbe (nicht dass wir sie beachten wuerden, aber
100 : : // man kann sie von aussen abfragen)
101 : : Color* m_pLastOutputColor;
102 : :
103 : : bool m_bUseInputStringForFormatting;
104 : :
105 : : public:
106 : : FormattedField(Window* pParent, WinBits nStyle = 0, SvNumberFormatter* pInitialFormatter = NULL, sal_Int32 nFormatKey = 0);
107 : : FormattedField(Window* pParent, const ResId& rResId, SvNumberFormatter* pInitialFormatter = NULL, sal_Int32 nFormatKey = 0);
108 : : virtual ~FormattedField();
109 : :
110 : : // Min-/Max-Verwaltung
111 : 0 : sal_Bool HasMinValue() const { return m_bHasMin; }
112 : 2 : void ClearMinValue() { m_bHasMin = sal_False; }
113 : : void SetMinValue(double dMin);
114 : 0 : double GetMinValue() const { return m_dMinValue; }
115 : :
116 : 0 : sal_Bool HasMaxValue() const { return m_bHasMax; }
117 : 2 : void ClearMaxValue() { m_bHasMax = sal_False; }
118 : : void SetMaxValue(double dMax);
119 : 0 : double GetMaxValue() const { return m_dMaxValue; }
120 : :
121 : : // aktueller Wert
122 : : virtual void SetValue(double dVal);
123 : : virtual double GetValue();
124 : : // die Standard-Implementierung jagt die Eingabe jeweils durch den Formatter, so einer vorhanden ist
125 : :
126 : : void GetColor() const;
127 : :
128 : : void SetTextValue(const XubString& rText);
129 : : // der String wird in ein double umgewandelt (durch den Formatter) und anschliessen in SetValue gesteckt
130 : :
131 : 840 : sal_Bool IsEmptyFieldEnabled() const { return m_bEnableEmptyField; }
132 : : void EnableEmptyField(sal_Bool bEnable);
133 : : // wenn nicht enabled, wird beim Verlassen des Feldes der Text auf den letzten gueltigen zurueckgesetzt
134 : :
135 : 0 : void SetDefaultValue(double dDefault) { m_dDefaultValue = dDefault; m_bValueDirty = sal_True; }
136 : : // wenn der aktuelle String ungueltig ist, liefert GetValue() diesen Default-Wert
137 : 0 : double GetDefaultValue() const { return m_dDefaultValue; }
138 : :
139 : : // Einstellungen fuer das Format
140 : 0 : sal_uLong GetFormatKey() const { return m_nFormatKey; }
141 : : void SetFormatKey(sal_uLong nFormatKey);
142 : :
143 : 2 : SvNumberFormatter* GetFormatter() const { return m_pFormatter; }
144 : : void SetFormatter(SvNumberFormatter* pFormatter, sal_Bool bResetFormat = sal_True);
145 : : // wenn bResetFormat sal_False ist, wird versucht, das alte eingestellte Format mit 'hinueberzuretten' (teuer, wenn es sich nicht
146 : : // um eines der Standard-Formate handelt, die in allen Formattern gleich sind)
147 : : // wenn sal_True, wird als neuer FormatKey 0 gesetzt
148 : :
149 : : sal_Bool GetThousandsSep() const;
150 : : void SetThousandsSep(sal_Bool _bUseSeparator);
151 : : // the is no check if the current format is numeric, so be cautious when calling these functions
152 : :
153 : : sal_uInt16 GetDecimalDigits() const;
154 : : void SetDecimalDigits(sal_uInt16 _nPrecision);
155 : : // the is no check if the current format is numeric, so be cautious when calling these functions
156 : :
157 : 70 : SvNumberFormatter* StandardFormatter() { return m_aStaticFormatter; }
158 : : // Wenn man keinen eigenen Formatter explizit anlegen will, kann man diesen hier in SetFormatter stecken ...
159 : : // Das hier gelieferte Objekt wird allerdings zwischen allen Instanzen der Klasse geteilt (aus Zeit- und Platzgruenden),
160 : : // also ist etwas Vorsicht angebracht ...
161 : :
162 : : void GetFormat(XubString& rFormatString, LanguageType& eLang) const;
163 : : sal_Bool SetFormat(const XubString& rFormatString, LanguageType eLang);
164 : : // sal_False, wenn der FormatString nicht gesetzt werden konnte (also wahrscheinlich ungueltig ist)
165 : :
166 : 138 : sal_Bool IsStrictFormat() const { return m_bStrictFormat; }
167 : 56 : void SetStrictFormat(sal_Bool bEnable) { m_bStrictFormat = bEnable; }
168 : : // Formatueberpruefung waehrend der Eingabe ?
169 : :
170 : : // Spin-Handling
171 : : virtual void Up();
172 : : virtual void Down();
173 : : // Standard-Implementierung : hoch- oder runterzaehlen des aktuellen double einfach um die gesetzte SpinSize
174 : : virtual void First();
175 : : virtual void Last();
176 : : // Standard-Implementierung : aktuelles double setzen auf eingestellten first respektive last value
177 : :
178 : 117 : void SetSpinSize(double dStep) { m_dSpinSize = dStep; }
179 : 0 : double GetSpinSize() const { return m_dSpinSize; }
180 : :
181 : 84 : void SetSpinFirst(double dFirst) { m_dSpinFirst = dFirst; }
182 : 0 : double GetSpinFirst() const { return m_dSpinFirst; }
183 : :
184 : 84 : void SetSpinLast(double dLast) { m_dSpinLast = dLast; }
185 : 0 : double GetSpinLast() const { return m_dSpinLast; }
186 : :
187 : 108 : sal_Bool TreatingAsNumber() const { return m_bTreatAsNumber; }
188 : 58 : void TreatAsNumber(sal_Bool bDoSo) { m_bTreatAsNumber = bDoSo; }
189 : :
190 : : public:
191 : : virtual void SetText( const XubString& rStr );
192 : : virtual void SetText( const XubString& rStr, const Selection& rNewSelection );
193 : :
194 : : // die folgenden Methoden sind interesant, wenn m_bTreatAsNumber auf sal_False sitzt
195 : : /** nehmen wir mal an, irgendjemand will das ganze schoene double-Handling gar nicht haben, sondern
196 : : einfach den Text formatiert ausgeben ...
197 : : (der Text wird einfach nur durch den Formatter gejagt und dann gesetzt)
198 : : */
199 : : void SetTextFormatted(const XubString& rText);
200 : : String GetTextValue() const;
201 : :
202 : 0 : void SetDefaultText(const XubString& rDefault) { m_sDefaultText = rDefault; }
203 : 0 : String GetDefaultText() const { return m_sDefaultText; }
204 : :
205 : : // die bei der letzten Ausgabe-Operation vom Formatter gelieferte Farbe (Ausgabe-Operationen werden getriggert durch
206 : : // SetValue, SetTextValue, SetTextFormatted, also indirekt eventuell auch durch SetMin-/-MaxValue)
207 : 32 : Color* GetLastOutputColor() const { return m_pLastOutputColor; }
208 : :
209 : : /** reformats the current text. Interesting if the user entered some text in an "input format", and
210 : : this should be formatted in the "output format" (which may differ, e.g. by additional numeric
211 : : digits or such).
212 : : */
213 : : void Commit();
214 : :
215 : : // enable automatic coloring. if set to sal_True, and the format the field is working with for any current value
216 : : // says that it has to be painted in a special color (e.g. a format where negative numbers should be printed
217 : : // red), the text is painted with that color automatically.
218 : : // The color used is the same as returned by GetLastOutputColor()
219 : : void SetAutoColor(sal_Bool _bAutomatic);
220 : : sal_Bool GetAutoColor() const { return m_bAutoColor; }
221 : :
222 : : /** enables handling of not-a-number value.
223 : :
224 : : When this is set to <FALSE/> (the default), then invalid inputs (i.e. text which cannot be
225 : : intepreted, according to the current formatting) will be handled as if the default value
226 : : has been entered. GetValue the will return this default value.
227 : :
228 : : When set to <TRUE/>, then GetValue will return NaN (not a number, see <method scope="rtl::math">isNan</method>)
229 : : when the current input is invalid.
230 : :
231 : : Note that setting this to <TRUE/> implies that upon leaving the control, the input
232 : : will *not* be corrected to a valid value. For example, if the user enters "foo" in the
233 : : control, and then tabs out of it, the text "foo" will persist, and GetValue will
234 : : return NaN in subsequent calls.
235 : : */
236 : : void EnableNotANumber( sal_Bool _bEnable );
237 : : sal_Bool IsNotANumberEnabled( ) const { return m_bEnableNaN; }
238 : :
239 : : /** When being set to true, the strings in the field are formatted using the
240 : : InputLine format. That's also what you get in Calc when you edit a cell
241 : : using F2
242 : : */
243 : : void UseInputStringForFormatting( bool bUseInputStr = true );
244 : : bool IsUsingInputStringForFormatting() const;
245 : :
246 : : protected:
247 : : virtual long Notify(NotifyEvent& rNEvt);
248 : : virtual void Modify();
249 : :
250 : : // CheckText ueberschreiben fuer Ueberpruefung zur Eingabezeit
251 : 82 : virtual sal_Bool CheckText(const XubString&) const { return sal_True; }
252 : :
253 : : // any aspect of the current format has changed
254 : : virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat);
255 : :
256 : : void ImplSetTextImpl(const XubString& rNew, Selection* pNewSel);
257 : : void ImplSetValue(double dValue, sal_Bool bForce);
258 : : sal_Bool ImplGetValue(double& dNewVal);
259 : :
260 : : void ImplSetFormatKey(sal_uLong nFormatKey);
261 : : // SetFormatKey without FormatChanged notification
262 : :
263 : 70 : virtual SvNumberFormatter* CreateFormatter() { SetFormatter(StandardFormatter()); return m_pFormatter; }
264 [ + + ]: 2644 : SvNumberFormatter* ImplGetFormatter() const { return m_pFormatter ? m_pFormatter : ((FormattedField*)this)->CreateFormatter(); }
265 : :
266 : : long PreNotify(NotifyEvent& rNEvt);
267 : :
268 : : virtual void ReFormat();
269 : : };
270 : :
271 : : //------------------------------------------------------------------------------
272 : : class SVT_DLLPUBLIC DoubleNumericField : public FormattedField
273 : : {
274 : : protected:
275 : : validation::NumberValidator* m_pNumberValidator;
276 : :
277 : : public:
278 : 34 : DoubleNumericField(Window* pParent, WinBits nStyle = 0)
279 : : :FormattedField(pParent, nStyle)
280 : 34 : ,m_pNumberValidator( NULL )
281 : : {
282 [ + - ]: 34 : ResetConformanceTester();
283 : 34 : }
284 : :
285 : : DoubleNumericField(Window* pParent, const ResId& rResId)
286 : : :FormattedField(pParent, rResId)
287 : : ,m_pNumberValidator( NULL )
288 : : {
289 : : ResetConformanceTester();
290 : : }
291 : : virtual ~DoubleNumericField();
292 : :
293 : : protected:
294 : : virtual sal_Bool CheckText(const XubString& sText) const;
295 : :
296 : : virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat);
297 : : void ResetConformanceTester();
298 : : };
299 : :
300 : : //==============================================================================
301 : : #define FCT_CURRENCY_SYMBOL 0x10
302 : : #define FCT_CURRSYM_POSITION 0x20
303 : :
304 : : //------------------------------------------------------------------------------
305 [ + - ][ - + ]: 68 : class DoubleCurrencyField : public FormattedField
306 : : {
307 : : XubString m_sCurrencySymbol;
308 : : sal_Bool m_bPrependCurrSym;
309 : : sal_Bool m_bChangingFormat;
310 : :
311 : : public:
312 : : DoubleCurrencyField(Window* pParent, WinBits nStyle = 0);
313 : :
314 : 222 : XubString getCurrencySymbol() const { return m_sCurrencySymbol; }
315 : : void setCurrencySymbol(const XubString& _sSymbol);
316 : :
317 : 222 : sal_Bool getPrependCurrSym() const { return m_bPrependCurrSym; }
318 : : void setPrependCurrSym(sal_Bool _bPrepend);
319 : :
320 : : protected:
321 : : virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat);
322 : :
323 : : void UpdateCurrencyFormat();
324 : : };
325 : :
326 : : #endif // _FMTFIELD_HXX_
327 : :
328 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|