lok: calc - send other views our selection in their co-ordinates.
[LibreOffice.git] / include / vcl / fmtfield.hxx
blobc2c3bb7d619b270b8da59f090009a7c922da53f0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_VCL_FMTFIELD_HXX
21 #define INCLUDED_VCL_FMTFIELD_HXX
23 #include <vcl/spinfld.hxx>
24 #include <memory>
26 class SvNumberFormatter;
28 namespace validation { class NumberValidator; }
30 enum class FORMAT_CHANGE_TYPE
32 KEYONLY = 0x00, // only a new key was set
33 FORMATTER = 0x01, // a new formatter was set, usually implies a change of the key, too
34 PRECISION = 0x02, // a new precision was set
35 THOUSANDSSEP = 0x03, // the thousands separator setting changed
36 CURRENCY_SYMBOL = 0x10,
37 CURRSYM_POSITION = 0x20,
41 class VCL_DLLPUBLIC FormattedField : public SpinField
43 private:
44 // A SvNumberFormatter is very expensive (regarding time and space), it is a Singleton
45 class StaticFormatter
47 static SvNumberFormatter* s_cFormatter;
48 static sal_uLong s_nReferences;
49 public:
50 StaticFormatter();
51 ~StaticFormatter();
53 operator SvNumberFormatter* () { return GetFormatter(); }
54 VCL_DLLPUBLIC static SvNumberFormatter* GetFormatter();
57 protected:
58 OUString m_sLastValidText;
59 // Has nothing to do with the current value. It is the last text, which was valid at input (checked by CheckText,
60 // not yet through formatter)
61 Selection m_aLastSelection;
63 double m_dMinValue;
64 double m_dMaxValue;
65 bool m_bHasMin : 1;
66 bool m_bHasMax : 1;
68 bool m_bStrictFormat : 1;
70 bool m_bEnableEmptyField : 1;
71 bool m_bAutoColor : 1;
72 bool m_bEnableNaN : 1;
73 bool m_bDisableRemainderFactor : 1;
74 enum valueState { valueDirty, valueString, valueDouble };
75 valueState m_ValueState;
76 double m_dCurrentValue;
77 double m_dDefaultValue;
79 sal_uLong m_nFormatKey;
80 SvNumberFormatter* m_pFormatter;
81 StaticFormatter m_aStaticFormatter;
83 double m_dSpinSize;
84 double m_dSpinFirst;
85 double m_dSpinLast;
87 // There is a difference, when text formatting is enabled, if LostFocus formats the current String and displays it,
88 // or if a double is created from the String and then
89 bool m_bTreatAsNumber;
90 // And with the following members we can use it for formatted text output as well ...
91 OUString m_sCurrentTextValue;
92 OUString m_sDefaultText;
94 // The last color from the Formatter at the last output operation (not we would use it, but you can get it)
95 Color* m_pLastOutputColor;
97 bool m_bUseInputStringForFormatting;
99 public:
100 FormattedField(vcl::Window* pParent, WinBits nStyle);
102 // Min-/Max-management
103 bool HasMinValue() const { return m_bHasMin; }
104 void ClearMinValue() { m_bHasMin = false; }
105 void SetMinValue(double dMin);
106 double GetMinValue() const { return m_dMinValue; }
108 bool HasMaxValue() const { return m_bHasMax; }
109 void ClearMaxValue() { m_bHasMax = false; }
110 void SetMaxValue(double dMax);
111 double GetMaxValue() const { return m_dMaxValue; }
113 // Current value
114 void SetValue(double dVal);
115 double GetValue();
116 // The default implementation uses a formatter, if available
118 void SetTextValue(const OUString& rText);
119 // The String is transformed to a double (with a formatter) and SetValue is called afterwards
121 bool IsEmptyFieldEnabled() const { return m_bEnableEmptyField; }
122 void EnableEmptyField(bool bEnable);
123 // If disabled, the value will be reset to the last valid value on leave
125 void SetDefaultValue(double dDefault) { m_dDefaultValue = dDefault; m_ValueState = valueDirty; }
126 // If the current String is invalid, GetValue() returns this value
127 double GetDefaultValue() const { return m_dDefaultValue; }
129 // Settings for the format
130 sal_uLong GetFormatKey() const { return m_nFormatKey; }
131 void SetFormatKey(sal_uLong nFormatKey);
133 SvNumberFormatter* GetFormatter() const { return m_pFormatter; }
134 void SetFormatter(SvNumberFormatter* pFormatter, bool bResetFormat = true);
135 // If bResetFormat is sal_False, the old format is tried to be kept. (expensive, if it is no default format, available in all formatters)
136 // If sal_True, the new FormatKey is set to zero
138 bool GetThousandsSep() const;
139 void SetThousandsSep(bool _bUseSeparator);
140 // the is no check if the current format is numeric, so be cautious when calling these functions
142 void DisableRemainderFactor();
144 sal_uInt16 GetDecimalDigits() const;
145 void SetDecimalDigits(sal_uInt16 _nPrecision);
146 // There is no check if the current format is numeric, so be cautious when calling these functions
148 SvNumberFormatter* StandardFormatter() { return m_aStaticFormatter; }
149 // If no new Formatter is created explicitly, this can be used in SetFormatter...
151 OUString GetFormat(LanguageType& eLang) const;
152 bool SetFormat(const OUString& rFormatString, LanguageType eLang);
153 // sal_False, if the FormatString could not be set (and very probably is invalid)
154 // This Object is shared via all instances, so be careful!
156 bool IsStrictFormat() const { return m_bStrictFormat; }
157 void SetStrictFormat(bool bEnable) { m_bStrictFormat = bEnable; }
158 // Check format during input
160 // Spin-Handling
161 virtual void Up() override;
162 virtual void Down() override;
163 // Default Implementation: +/- default spin size to the double value
164 virtual void First() override;
165 virtual void Last() override;
166 // Default Implementation: Current double is set to the first or last value
168 virtual bool set_property(const OString &rKey, const OUString &rValue) override;
170 void SetSpinSize(double dStep) { m_dSpinSize = dStep; }
171 double GetSpinSize() const { return m_dSpinSize; }
173 void SetSpinFirst(double dFirst) { m_dSpinFirst = dFirst; }
174 double GetSpinFirst() const { return m_dSpinFirst; }
176 void SetSpinLast(double dLast) { m_dSpinLast = dLast; }
177 double GetSpinLast() const { return m_dSpinLast; }
179 bool TreatingAsNumber() const { return m_bTreatAsNumber; }
180 void TreatAsNumber(bool bDoSo) { m_bTreatAsNumber = bDoSo; }
182 void SetOutputHdl(const Link<Edit&, bool>& rLink) { m_aOutputHdl = rLink; }
183 void SetInputHdl(const Link<sal_Int64*,TriState>& rLink) { m_aInputHdl = rLink; }
184 public:
185 virtual void SetText( const OUString& rStr ) override;
186 virtual void SetText( const OUString& rStr, const Selection& rNewSelection ) override;
188 //The following methods are interesting, if m_bTreatAsNumber is set to sal_False
189 //If someone does not care about all the double handling and just wants to print the text formatted.
190 //(((The text will be formatted, using the Formatter, and then set)
191 void SetTextFormatted(const OUString& rText);
192 OUString const & GetTextValue() const;
194 void SetDefaultText(const OUString& rDefault) { m_sDefaultText = rDefault; }
195 const OUString& GetDefaultText() const { return m_sDefaultText; }
197 // The last colour from the Formatter's last output operation. Output operations get triggered by:
198 // SetValue, SetTextValue, SetTextFormatted, also indirectly via SetMin - / -MaxValue
199 Color* GetLastOutputColor() const { return m_pLastOutputColor; }
201 /** reformats the current text. Interesting if the user entered some text in an "input format", and
202 this should be formatted in the "output format" (which may differ, e.g. by additional numeric
203 digits or such).
205 void Commit();
207 // enable automatic coloring. if set to sal_True, and the format the field is working with for any current value
208 // says that it has to be painted in a special color (e.g. a format where negative numbers should be printed
209 // red), the text is painted with that color automatically.
210 // The color used is the same as returned by GetLastOutputColor()
211 void SetAutoColor(bool _bAutomatic);
213 /** enables handling of not-a-number value.
215 When this is set to <FALSE/> (the default), then invalid inputs (i.e. text which cannot be
216 interpreted, according to the current formatting) will be handled as if the default value
217 has been entered. GetValue the will return this default value.
219 When set to <TRUE/>, then GetValue will return NaN (not a number, see <method scope="rtl::math">isNan</method>)
220 when the current input is invalid.
222 Note that setting this to <TRUE/> implies that upon leaving the control, the input
223 will *not* be corrected to a valid value. For example, if the user enters "foo" in the
224 control, and then tabs out of it, the text "foo" will persist, and GetValue will
225 return NaN in subsequent calls.
227 void EnableNotANumber( bool _bEnable );
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
233 void UseInputStringForFormatting();
234 bool IsUsingInputStringForFormatting() const { return m_bUseInputStringForFormatting;}
236 protected:
237 virtual bool EventNotify(NotifyEvent& rNEvt) override;
238 void impl_Modify(bool makeValueDirty = true);
239 virtual void Modify() override;
241 // Override CheckText for input-time checks
242 virtual bool CheckText(const OUString&) const { return true; }
244 // any aspect of the current format has changed
245 virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat);
247 void ImplSetTextImpl(const OUString& rNew, Selection const * pNewSel);
248 void ImplSetValue(double dValue, bool bForce);
249 bool ImplGetValue(double& dNewVal);
251 void ImplSetFormatKey(sal_uLong nFormatKey);
252 // SetFormatKey without FormatChanged notification
254 SvNumberFormatter* CreateFormatter() { SetFormatter(StandardFormatter()); return m_pFormatter; }
255 SvNumberFormatter* ImplGetFormatter() const { return m_pFormatter ? m_pFormatter : const_cast<FormattedField*>(this)->CreateFormatter(); }
257 bool PreNotify(NotifyEvent& rNEvt) override;
259 void ReFormat();
260 private:
261 Link<Edit&, bool> m_aOutputHdl;
262 Link<sal_Int64*, TriState> m_aInputHdl;
265 class VCL_DLLPUBLIC DoubleNumericField final : public FormattedField
267 public:
268 DoubleNumericField(vcl::Window* pParent, WinBits nStyle);
270 virtual ~DoubleNumericField() override;
272 private:
273 virtual bool CheckText(const OUString& sText) const override;
275 virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat) override;
276 void ResetConformanceTester();
278 std::unique_ptr<validation::NumberValidator> m_pNumberValidator;
282 class VCL_DLLPUBLIC DoubleCurrencyField final : public FormattedField
284 public:
285 DoubleCurrencyField(vcl::Window* pParent, WinBits nStyle);
287 const OUString& getCurrencySymbol() const { return m_sCurrencySymbol; }
288 void setCurrencySymbol(const OUString& rSymbol);
290 bool getPrependCurrSym() const { return m_bPrependCurrSym; }
291 void setPrependCurrSym(bool _bPrepend);
293 private:
294 virtual void FormatChanged(FORMAT_CHANGE_TYPE nWhat) override;
296 void UpdateCurrencyFormat();
298 OUString m_sCurrencySymbol;
299 bool m_bPrependCurrSym;
300 bool m_bChangingFormat;
303 #endif // INCLUDED_VCL_FMTFIELD_HXX
305 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */