LCOV - code coverage report
Current view: top level - include/svtools - fmtfield.hxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 24 36 66.7 %
Date: 2014-04-11 Functions: 22 34 64.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10