summaryrefslogtreecommitdiff
path: root/svtools/inc/svtools/fmtfield.hxx
blob: 406c827a7636ac07e72260fef4c9e24a912205f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#ifndef _FMTFIELD_HXX_
#define _FMTFIELD_HXX_

#include "svtools/svtdllapi.h"
#include <vcl/spinfld.hxx>
#include <svl/zforlist.hxx>

//#define REGEXP_SUPPORT

#ifdef REGEXP_SUPPORT
    #ifndef _UNOTOOLS_TEXTSEARCH_HXX
    #include <unotools/textsearch.hxx>
    #endif
#else
    // use a hand-made regular expression parsing for the small expression we're interested in
    // as soon as OOo does have regular expression support, we can switch on the REGEXP_SUPPORT define
    namespace validation { class NumberValidator; }
#endif

typedef USHORT FORMAT_CHANGE_TYPE;
#define FCT_KEYONLY         0x00        // only a new key was set
#define FCT_FORMATTER       0x01        // a new formatter weas set, usually implies a change of the key, too
#define FCT_PRECISION       0x02        // a new precision was set
#define FCT_THOUSANDSSEP    0x03        // the thousands separator setting changed

//------------------------------------------------------------------------------
class SVT_DLLPUBLIC FormattedField : public SpinField
{
private:
    // Da ein SvNumberFormatter eine ziemlich teure (sowohl zeit- als auch platz-maessig) Angelegenheit ist,
    // haelt sich nicht jedes Field, an dem kein Formatter gesetzt wurde, eine eigenen Instanz, sondern es gibt nur eine
    // einzige statische.
    class StaticFormatter
    {
        static SvNumberFormatter*   s_cFormatter;
        static ULONG                s_nReferences;
    public:
        StaticFormatter();
        ~StaticFormatter();

        operator SvNumberFormatter* () { return GetFormatter(); }
        SVT_DLLPUBLIC SvNumberFormatter* GetFormatter();
    };

protected:
    String      m_sLastValidText;
        // hat nichts mit dem current value zu tun, ist der letzte Text, der waehrend einer Eingabe als gueltig erkannt
        // wurde (also durch CheckText geprueft, nicht durch den Formatter gejagt)
    Selection   m_aLastSelection;

    double              m_dMinValue;
    double              m_dMaxValue;
    BOOL                m_bHasMin : 1;
    BOOL                m_bHasMax : 1;

    BOOL                m_bStrictFormat : 1;

    BOOL                m_bValueDirty : 1;
    BOOL                m_bEnableEmptyField : 1;
    BOOL                m_bAutoColor : 1;
    BOOL                m_bEnableNaN : 1;
    double              m_dCurrentValue;
    double              m_dDefaultValue;

    ULONG               m_nFormatKey;
    SvNumberFormatter*  m_pFormatter;
    StaticFormatter     m_aStaticFormatter;

    double              m_dSpinSize;
    double              m_dSpinFirst;
    double              m_dSpinLast;

    // es macht einen Unterschied, ob man bei eingestellter Textformatierung beim LostFocus den aktuellen String durch
    // den Formatter jagt und das Ergebnis anzeigt oder erst aus dem String ein double macht, das formatiert und dann
    // ausgibt
    BOOL                m_bTreatAsNumber;
    // und mit den folgenden Members koennen wir das Ganze hier auch zur formatierten Text-Ausgabe benutzen ...
    String              m_sCurrentTextValue;
    String              m_sDefaultText;

    // die bei der letzten Ausgabe-Operation vom Formatter gelieferte Farbe (nicht dass wir sie beachten wuerden, aber
    // man kann sie von aussen abfragen)
    Color*              m_pLastOutputColor;

    bool                m_bUseInputStringForFormatting;

public:
    FormattedField(Window* pParent, WinBits nStyle = 0, SvNumberFormatter* pInitialFormatter = NULL, INT32 nFormatKey = 0);
    FormattedField(Window* pParent, const ResId& rResId, SvNumberFormatter* pInitialFormatter = NULL, INT32 nFormatKey = 0);
    virtual ~FormattedField();

    // Min-/Max-Verwaltung
    BOOL    HasMinValue() const         { return m_bHasMin; }
    void    ClearMinValue()             { m_bHasMin = FALSE; }
    void    SetMinValue(double dMin);
    double  GetMinValue() const         { return m_dMinValue; }

    BOOL    HasMaxValue() const         { return m_bHasMax; }
    void    ClearMaxValue()             { m_bHasMax = FALSE; }
    void    SetMaxValue(double dMax);
    double  GetMaxValue() const         { return m_dMaxValue; }

    // aktueller Wert
    virtual void    SetValue(double dVal);
    virtual double  GetValue();
        // die Standard-Implementierung jagt die Eingabe jeweils durch den Formatter, so einer vorhanden ist

    void    GetColor() const;

    void    SetTextValue(const XubString& rText);
        // der String wird in ein double umgewandelt (durch den Formatter) und anschliessen in SetValue gesteckt

    BOOL    IsEmptyFieldEnabled() const         { return m_bEnableEmptyField; }
    void    EnableEmptyField(BOOL bEnable);
        // wenn nicht enabled, wird beim Verlassen des Feldes der Text auf den letzten gueltigen zurueckgesetzt

    void    SetDefaultValue(double dDefault)    { m_dDefaultValue = dDefault; m_bValueDirty = TRUE; }
        // wenn der aktuelle String ungueltig ist, liefert GetValue() diesen Default-Wert
    double  GetDefaultValue() const             { return m_dDefaultValue; }

    // Einstellungen fuer das Format
    ULONG   GetFormatKey() const                { return m_nFormatKey; }
    void    SetFormatKey(ULONG nFormatKey);

    SvNumberFormatter*  GetFormatter() const    { return m_pFormatter; }
    void    SetFormatter(SvNumberFormatter* pFormatter, BOOL bResetFormat = TRUE);
        // wenn bResetFormat FALSE ist, wird versucht, das alte eingestellte Format mit 'hinueberzuretten' (teuer, wenn es sich nicht
        // um eines der Standard-Formate handelt, die in allen Formattern gleich sind)
        // wenn TRUE, wird als neuer FormatKey 0 gesetzt

    BOOL    GetThousandsSep() const;
    void    SetThousandsSep(BOOL _bUseSeparator);
        // the is no check if the current format is numeric, so be cautious when calling these functions

    USHORT  GetDecimalDigits() const;
    void    SetDecimalDigits(USHORT _nPrecision);
        // the is no check if the current format is numeric, so be cautious when calling these functions

    SvNumberFormatter*  StandardFormatter() { return m_aStaticFormatter; }
        // Wenn man keinen eigenen Formatter explizit anlegen will, kann man diesen hier in SetFormatter stecken ...
        // Das hier gelieferte Objekt wird allerdings zwischen allen Instanzen der Klasse geteilt (aus Zeit- und Platzgruenden),
        // also ist etwas Vorsicht angebracht ...

    void        GetFormat(XubString& rFormatString, LanguageType& eLang) const;
    BOOL        SetFormat(const XubString& rFormatString, LanguageType eLang);
        // FALSE, wenn der FormatString nicht gesetzt werden konnte (also wahrscheinlich ungueltig ist)

    BOOL    IsStrictFormat() const              { return m_bStrictFormat; }
    void    SetStrictFormat(BOOL bEnable)       { m_bStrictFormat = bEnable; }
        // Formatueberpruefung waehrend der Eingabe ?

    // Spin-Handling
    virtual void Up();
    virtual void Down();
        // Standard-Implementierung : hoch- oder runterzaehlen des aktuellen double einfach um die gesetzte SpinSize
    virtual void First();
    virtual void Last();
        // Standard-Implementierung : aktuelles double setzen auf eingestellten first respektive last value

    void    SetSpinSize(double dStep)   { m_dSpinSize = dStep; }
    double  GetSpinSize() const         { return m_dSpinSize; }

    void    SetSpinFirst(double dFirst) { m_dSpinFirst = dFirst; }
    double  GetSpinFirst() const        { return m_dSpinFirst; }

    void    SetSpinLast(double dLast)   { m_dSpinLast = dLast; }
    double  GetSpinLast() const         { return m_dSpinLast; }

    BOOL    TreatingAsNumber() const    { return m_bTreatAsNumber; }
    void    TreatAsNumber(BOOL bDoSo) { m_bTreatAsNumber = bDoSo; }

public:
    virtual void SetText( const XubString& rStr );
    virtual void SetText( const XubString& rStr, const Selection& rNewSelection );
    void    SetValidateText(const XubString& rText, const String* pErrorText = NULL);

    // die folgenden Methoden sind interesant, wenn m_bTreatAsNumber auf FALSE sitzt
    /** nehmen wir mal an, irgendjemand will das ganze schoene double-Handling gar nicht haben, sondern
        einfach den Text formatiert ausgeben ...
        (der Text wird einfach nur durch den Formatter gejagt und dann gesetzt)
    */
    void SetTextFormatted(const XubString& rText);
    String  GetTextValue() const;

    void    SetDefaultText(const XubString& rDefault) { m_sDefaultText = rDefault; }
    String  GetDefaultText() const { return m_sDefaultText; }

    // die bei der letzten Ausgabe-Operation vom Formatter gelieferte Farbe (Ausgabe-Operationen werden getriggert durch
    // SetValue, SetTextValue, SetTextFormatted, also indirekt eventuell auch durch SetMin-/-MaxValue)
    Color*  GetLastOutputColor() const { return m_pLastOutputColor; }

    /** reformats the current text. Interesting if the user entered some text in an "input format", and
        this should be formatted in the "output format" (which may differ, e.g. by additional numeric
        digits or such).
    */
    void    Commit();

    // enable automatic coloring. if set to TRUE, and the format the field is working with for any current value
    // says that it has to be painted in a special color (e.g. a format where negative numbers should be printed
    // red), the text is painted with that color automatically.
    // The color used is the same as returned by GetLastOutputColor()
    void    SetAutoColor(BOOL _bAutomatic);
    BOOL    GetAutoColor() const { return m_bAutoColor; }

    /** enables handling of not-a-number value.

        When this is set to <FALSE/> (the default), then invalid inputs (i.e. text which cannot be
        intepreted, according to the current formatting) will be handled as if the default value
        has been entered. GetValue the will return this default value.

        When set to <TRUE/>, then GetValue will return NaN (not a number, see <method scope="rtl::math">isNan</method>)
        when the current input is invalid.

        Note that setting this to <TRUE/> implies that upon leaving the control, the input
        will *not* be corrected to a valid value. For example, if the user enters "foo" in the
        control, and then tabs out of it, the text "foo" will persist, and GetValue will
        return NaN in subsequent calls.
    */
    void    EnableNotANumber( BOOL _bEnable );
    BOOL    IsNotANumberEnabled( ) const { return m_bEnableNaN; }

    /** When being set to true, the strings in the field are formatted using the
        InputLine format.  That's also what you get in Calc when you edit a cell
        using F2
     */
    void    UseInputStringForFormatting( bool bUseInputStr = true );
    bool    IsUsingInputStringForFormatting() const;

protected:
    virtual long Notify(NotifyEvent& rNEvt);
    virtual void Modify();

    // CheckText ueberschreiben fuer Ueberpruefung zur Eingabezeit
    virtual BOOL CheckText(const XubString&) const { return TRUE; }

    // any aspect of the current format has changed
    virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat);

    void ImplSetTextImpl(const XubString& rNew, Selection* pNewSel);
    void ImplSetValue(double dValue, BOOL bForce);
    BOOL ImplGetValue(double& dNewVal);

    void ImplSetFormatKey(ULONG nFormatKey);
        // SetFormatKey without FormatChanged notification

    virtual SvNumberFormatter*  CreateFormatter() { SetFormatter(StandardFormatter()); return m_pFormatter; }
    SvNumberFormatter*  ImplGetFormatter() const { return m_pFormatter ? m_pFormatter : ((FormattedField*)this)->CreateFormatter(); }

    long PreNotify(NotifyEvent& rNEvt);

    virtual void ReFormat();
};

//------------------------------------------------------------------------------
class SVT_DLLPUBLIC DoubleNumericField : public FormattedField
{
protected:
#ifdef REGEXP_SUPPORT
    ::utl::TextSearch*              m_pConformanceTester;
#else
    validation::NumberValidator*    m_pNumberValidator;
#endif

public:
    DoubleNumericField(Window* pParent, WinBits nStyle = 0)
        :FormattedField(pParent, nStyle)
#ifdef REGEXP_SUPPORT
        ,m_pConformanceTester( NULL )
#else
        ,m_pNumberValidator( NULL )
#endif
    {
        ResetConformanceTester();
    }

    DoubleNumericField(Window* pParent, const ResId& rResId)
        :FormattedField(pParent, rResId)
#ifdef REGEXP_SUPPORT
        ,m_pConformanceTester( NULL )
#else
        ,m_pNumberValidator( NULL )
#endif
    {
        ResetConformanceTester();
    }
    virtual ~DoubleNumericField();

protected:
    virtual BOOL CheckText(const XubString& sText) const;

    virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat);
    void ResetConformanceTester();
};

//==============================================================================
#define FCT_CURRENCY_SYMBOL     0x10
#define FCT_CURRSYM_POSITION    0x20

//------------------------------------------------------------------------------
class DoubleCurrencyField : public FormattedField
{
    XubString   m_sCurrencySymbol;
    BOOL        m_bPrependCurrSym;
    BOOL        m_bChangingFormat;

public:
    DoubleCurrencyField(Window* pParent, WinBits nStyle = 0);
    DoubleCurrencyField(Window* pParent, const ResId& rResId);

    XubString   getCurrencySymbol() const { return m_sCurrencySymbol; }
    void        setCurrencySymbol(const XubString& _sSymbol);

    BOOL        getPrependCurrSym() const { return m_bPrependCurrSym; }
    void        setPrependCurrSym(BOOL _bPrepend);

protected:
    virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat);

    void UpdateCurrencyFormat();
};

#endif // _FMTFIELD_HXX_

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */