LibreOffice Module vcl (master)  1
field.hxx
Go to the documentation of this file.
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_VCL_FIELD_HXX
21 #define INCLUDED_VCL_FIELD_HXX
22 
23 #include <config_options.h>
24 #include <memory>
25 #include <vcl/dllapi.h>
26 #include <tools/link.hxx>
27 #include <tools/date.hxx>
28 #include <tools/time.hxx>
29 #include <vcl/spinfld.hxx>
30 #include <tools/fldunit.hxx>
31 
32 namespace com::sun::star::lang { struct Locale; }
33 
34 class CalendarWrapper;
35 class LocaleDataWrapper;
36 class LanguageTag;
37 
38 
40 {
41 private:
43  std::unique_ptr<LocaleDataWrapper>
45  bool mbReformat;
49 
50 protected:
51  SAL_DLLPRIVATE void ImplSetText( const OUString& rText, Selection const * pNewSel = nullptr );
52  SAL_DLLPRIVATE bool ImplGetEmptyFieldValue() const { return mbEmptyFieldValue; }
53 
54  void SetEmptyFieldValueData( bool bValue ) { mbEmptyFieldValue = bValue; }
55 
56  SAL_DLLPRIVATE LocaleDataWrapper& ImplGetLocaleDataWrapper() const;
57 
58  Edit* GetField() const { return mpField; }
59  void ClearField() { mpField.clear(); }
60 
61 public:
62  explicit FormatterBase(Edit* pField);
63  virtual ~FormatterBase();
64 
66 
67  bool MustBeReformatted() const { return mbReformat; }
68  void MarkToBeReformatted( bool b ) { mbReformat = b; }
69 
70  void SetStrictFormat( bool bStrict );
71  bool IsStrictFormat() const { return mbStrictFormat; }
72 
73  virtual void Reformat();
74  virtual void ReformatAll();
75 
76  const css::lang::Locale& GetLocale() const;
77  const LanguageTag& GetLanguageTag() const;
78 
79  void SetEmptyFieldValue();
80  bool IsEmptyFieldValue() const;
81 
82  void EnableEmptyFieldValue( bool bEnable ) { mbEmptyFieldValueEnabled = bEnable; }
83  bool IsEmptyFieldValueEnabled() const { return mbEmptyFieldValueEnabled; }
84 };
85 
86 #define PATTERN_FORMAT_EMPTYLITERALS (sal_uInt16(0x0001))
87 
88 class UNLESS_MERGELIBS(VCL_DLLPUBLIC) PatternFormatter : public FormatterBase
89 {
90 private:
91  OString m_aEditMask;
92  OUString maLiteralMask;
93  bool mbSameMask;
94  bool mbInPattKeyInput;
95 
96 protected:
97  PatternFormatter(Edit* pEdit);
98 
99  SAL_DLLPRIVATE void ImplSetMask(const OString& rEditMask,
100  const OUString& rLiteralMask);
101  SAL_DLLPRIVATE bool ImplIsSameMask() const { return mbSameMask; }
102  SAL_DLLPRIVATE bool& ImplGetInPattKeyInput() { return mbInPattKeyInput; }
103 
104 public:
105  virtual ~PatternFormatter() override;
106 
107  virtual void Reformat() override;
108 
109  void SetMask(const OString& rEditMask, const OUString& rLiteralMask );
110  const OString& GetEditMask() const { return m_aEditMask; }
111  const OUString& GetLiteralMask() const { return maLiteralMask; }
112 
113  void SetString( const OUString& rStr );
114  OUString GetString() const;
115 };
116 
117 
119 {
120 public:
121  virtual ~NumericFormatter() override;
122 
123  virtual void Reformat() override;
124 
125  void SetMin( sal_Int64 nNewMin );
126  sal_Int64 GetMin() const { return mnMin; }
127  void SetMax( sal_Int64 nNewMax );
128  sal_Int64 GetMax() const { return mnMax; }
129 
130  sal_Int64 ClipAgainstMinMax(sal_Int64 nValue) const;
131 
132  void SetFirst( sal_Int64 nNewFirst ) { mnFirst = nNewFirst; }
133  sal_Int64 GetFirst() const { return mnFirst; }
134  void SetLast( sal_Int64 nNewLast ) { mnLast = nNewLast; }
135  sal_Int64 GetLast() const { return mnLast; }
136  void SetSpinSize( sal_Int64 nNewSize ) { mnSpinSize = nNewSize; }
137  sal_Int64 GetSpinSize() const { return mnSpinSize; }
138 
139  void SetDecimalDigits( sal_uInt16 nDigits );
140  sal_uInt16 GetDecimalDigits() const { return mnDecimalDigits;}
141 
142  void SetUseThousandSep( bool b );
143  bool IsUseThousandSep() const { return mbThousandSep; }
144 
145  void SetUserValue( sal_Int64 nNewValue );
146  virtual void SetValue( sal_Int64 nNewValue );
147  sal_Int64 GetValue() const;
148  virtual OUString CreateFieldText( sal_Int64 nValue ) const;
149 
150  sal_Int64 Normalize( sal_Int64 nValue ) const;
151  sal_Int64 Denormalize( sal_Int64 nValue ) const;
152 
153 protected:
154  sal_Int64 mnLastValue;
155  sal_Int64 mnMin;
156  sal_Int64 mnMax;
159 
160  // the members below are used in all derivatives of NumericFormatter
161  // not in NumericFormatter itself.
162  sal_Int64 mnSpinSize;
163  sal_Int64 mnFirst;
164  sal_Int64 mnLast;
165 
166  NumericFormatter(Edit* pEdit);
167 
168  void FieldUp();
169  void FieldDown();
170  void FieldFirst();
171  void FieldLast();
172  void FormatValue(Selection const * pNewSelection = nullptr);
173 
174  SAL_DLLPRIVATE void ImplNumericReformat();
175  SAL_DLLPRIVATE void ImplNewFieldValue( sal_Int64 nNewValue );
176  SAL_DLLPRIVATE void ImplSetUserValue( sal_Int64 nNewValue, Selection const * pNewSelection = nullptr );
177 
178  virtual sal_Int64 GetValueFromString(const OUString& rStr) const;
179 
180 private:
181  sal_uInt16 mnDecimalDigits;
183 
184 };
185 
186 class UNLESS_MERGELIBS(VCL_DLLPUBLIC) DateFormatter : public FormatterBase
187 {
188 private:
189  std::unique_ptr<CalendarWrapper> mxCalendarWrapper;
190  Date maFieldDate;
191  Date maLastDate;
192  Date maMin;
193  Date maMax;
194  bool mbLongFormat;
195  bool mbShowDateCentury;
196  ExtDateFieldFormat mnExtDateFormat;
197  bool mbEnforceValidValue;
198 
199 protected:
200  DateFormatter(Edit* pEdit);
201 
202  SAL_DLLPRIVATE const Date& ImplGetFieldDate() const { return maFieldDate; }
203  SAL_DLLPRIVATE void ImplDateReformat( const OUString& rStr, OUString& rOutStr );
204  SAL_DLLPRIVATE void ImplSetUserDate( const Date& rNewDate,
205  Selection const * pNewSelection = nullptr );
206  SAL_DLLPRIVATE OUString ImplGetDateAsText( const Date& rDate ) const;
207  SAL_DLLPRIVATE void ImplNewFieldValue( const Date& rDate );
208  CalendarWrapper& GetCalendarWrapper() const;
209 
210  SAL_DLLPRIVATE bool ImplAllowMalformedInput() const;
211 
212 public:
213  virtual ~DateFormatter() override;
214 
215  virtual void Reformat() override;
216  virtual void ReformatAll() override;
217 
218  void SetExtDateFormat( ExtDateFieldFormat eFormat );
219  ExtDateFieldFormat GetExtDateFormat( bool bResolveSystemFormat = false ) const;
220 
221  void SetMin( const Date& rNewMin );
222  const Date& GetMin() const { return maMin; }
223 
224  void SetMax( const Date& rNewMax );
225  const Date& GetMax() const { return maMax; }
226 
227 
228  // MT: Remove these methods too, ExtDateFormat should be enough!
229  // What should happen if using DDMMYYYY, but ShowCentury=false?
230 
231  void SetLongFormat( bool bLong );
232  bool IsLongFormat() const { return mbLongFormat; }
233  void SetShowDateCentury( bool bShowCentury );
234  bool IsShowDateCentury() const { return mbShowDateCentury; }
235 
236 
237  void SetDate( const Date& rNewDate );
238  Date GetDate() const;
239  void SetEmptyDate();
240  bool IsEmptyDate() const;
241 
242  void ResetLastDate() { maLastDate = Date( Date::EMPTY ); }
243 
244  static void ExpandCentury( Date& rDate );
245  static void ExpandCentury( Date& rDate, sal_uInt16 nTwoDigitYearStart );
246 
259  void EnforceValidValue( bool _bEnforce ) { mbEnforceValidValue = _bEnforce; }
260  bool IsEnforceValidValue( ) const { return mbEnforceValidValue; }
261 };
262 
263 class UNLESS_MERGELIBS(VCL_DLLPUBLIC) TimeFormatter : public FormatterBase
264 {
265 private:
266  tools::Time maLastTime;
267  tools::Time maMin;
268  tools::Time maMax;
269  TimeFieldFormat meFormat;
270  TimeFormat mnTimeFormat;
271  bool mbDuration;
272  bool mbEnforceValidValue;
273 
274 protected:
275  tools::Time maFieldTime;
276 
277  TimeFormatter(Edit* pEdit);
278 
279  SAL_DLLPRIVATE void ImplTimeReformat( const OUString& rStr, OUString& rOutStr );
280  SAL_DLLPRIVATE void ImplNewFieldValue( const tools::Time& rTime );
281  SAL_DLLPRIVATE void ImplSetUserTime( const tools::Time& rNewTime, Selection const * pNewSelection = nullptr );
282  SAL_DLLPRIVATE bool ImplAllowMalformedInput() const;
283 
284 public:
285  static OUString FormatTime(const tools::Time& rNewTime, TimeFieldFormat eFormat, TimeFormat eHourFormat, bool bDuration, const LocaleDataWrapper& rLocaleData);
286  static bool TextToTime(const OUString& rStr, tools::Time& rTime, TimeFieldFormat eFormat, bool bDuration, const LocaleDataWrapper& rLocaleDataWrapper, bool _bSkipInvalidCharacters = true);
287  static int GetTimeArea(TimeFieldFormat eFormat, const OUString& rText, int nCursor,
288  const LocaleDataWrapper& rLocaleDataWrapper);
289  static tools::Time SpinTime(bool bUp, const tools::Time& rTime, TimeFieldFormat eFormat,
290  bool bDuration, const OUString& rText, int nCursor,
291  const LocaleDataWrapper& rLocaleDataWrapper);
292 
293  virtual ~TimeFormatter() override;
294 
295  virtual void Reformat() override;
296  virtual void ReformatAll() override;
297 
298  void SetMin( const tools::Time& rNewMin );
299  const tools::Time& GetMin() const { return maMin; }
300  void SetMax( const tools::Time& rNewMax );
301  const tools::Time& GetMax() const { return maMax; }
302 
303  void SetTimeFormat( TimeFormat eNewFormat );
304  TimeFormat GetTimeFormat() const { return mnTimeFormat;}
305 
306  void SetFormat( TimeFieldFormat eNewFormat );
307  TimeFieldFormat GetFormat() const { return meFormat; }
308 
309  void SetDuration( bool mbDuration );
310  bool IsDuration() const { return mbDuration; }
311 
312  void SetTime( const tools::Time& rNewTime );
313  void SetUserTime( const tools::Time& rNewTime );
314  tools::Time GetTime() const;
315  void SetEmptyTime() { FormatterBase::SetEmptyFieldValue(); }
316  bool IsEmptyTime() const { return FormatterBase::IsEmptyFieldValue(); }
317 
330  void EnforceValidValue( bool _bEnforce ) { mbEnforceValidValue = _bEnforce; }
331  bool IsEnforceValidValue( ) const { return mbEnforceValidValue; }
332 };
333 
334 class UNLESS_MERGELIBS(VCL_DLLPUBLIC) PatternField final : public SpinField, public PatternFormatter
335 {
336 public:
337  explicit PatternField( vcl::Window* pParent, WinBits nWinStyle );
338 
339  virtual bool PreNotify( NotifyEvent& rNEvt ) override;
340  virtual bool EventNotify( NotifyEvent& rNEvt ) override;
341  virtual void Modify() override;
342  virtual void dispose() override;
343 };
344 
345 
346 class UNLESS_MERGELIBS(VCL_DLLPUBLIC) NumericField : public SpinField, public NumericFormatter
347 {
348 public:
349  explicit NumericField( vcl::Window* pParent, WinBits nWinStyle );
350 
351  virtual bool PreNotify( NotifyEvent& rNEvt ) override;
352  virtual bool EventNotify( NotifyEvent& rNEvt ) override;
353  virtual void DataChanged( const DataChangedEvent& rDCEvt ) override;
354 
355  virtual Size CalcMinimumSize() const override;
356 
357  virtual void Modify() override;
358 
359  virtual void Up() override;
360  virtual void Down() override;
361  virtual void First() override;
362  virtual void Last() override;
363  virtual bool set_property(const OString &rKey, const OUString &rValue) override;
364  virtual void dispose() override;
365 
366  virtual boost::property_tree::ptree DumpAsPropertyTree() override;
367 };
368 
369 class UNLESS_MERGELIBS(VCL_DLLPUBLIC) DateField : public SpinField, public DateFormatter
370 {
371 private:
372  Date maFirst;
373  Date maLast;
374 
375 protected:
376  SAL_DLLPRIVATE void ImplDateSpinArea( bool bUp );
377 
378 public:
379  explicit DateField( vcl::Window* pParent, WinBits nWinStyle );
380 
381  virtual bool PreNotify( NotifyEvent& rNEvt ) override;
382  virtual bool EventNotify( NotifyEvent& rNEvt ) override;
383  virtual void DataChanged( const DataChangedEvent& rDCEvt ) override;
384 
385  virtual void Modify() override;
386 
387  virtual void Up() override;
388  virtual void Down() override;
389  virtual void First() override;
390  virtual void Last() override;
391 
392  void SetFirst( const Date& rNewFirst ) { maFirst = rNewFirst; }
393  const Date& GetFirst() const { return maFirst; }
394  void SetLast( const Date& rNewLast ) { maLast = rNewLast; }
395  const Date& GetLast() const { return maLast; }
396  virtual void dispose() override;
397 };
398 
399 class UNLESS_MERGELIBS(VCL_DLLPUBLIC) TimeField final : public SpinField, public TimeFormatter
400 {
401 private:
402  tools::Time maFirst;
403  tools::Time maLast;
404 
405  SAL_DLLPRIVATE void ImplTimeSpinArea( bool bUp );
406 
407 public:
408  explicit TimeField( vcl::Window* pParent, WinBits nWinStyle );
409 
410  virtual bool PreNotify( NotifyEvent& rNEvt ) override;
411  virtual bool EventNotify( NotifyEvent& rNEvt ) override;
412  virtual void DataChanged( const DataChangedEvent& rDCEvt ) override;
413 
414  virtual void Modify() override;
415 
416  virtual void Up() override;
417  virtual void Down() override;
418  virtual void First() override;
419  virtual void Last() override;
420 
421  void SetFirst( const tools::Time& rNewFirst ) { maFirst = rNewFirst; }
422  const tools::Time& GetFirst() const { return maFirst; }
423  void SetLast( const tools::Time& rNewLast ) { maLast = rNewLast; }
424  const tools::Time& GetLast() const { return maLast; }
425 
426  void SetExtFormat( ExtTimeFieldFormat eFormat );
427  virtual void dispose() override;
428 };
429 
430 #endif // INCLUDED_VCL_FIELD_HXX
431 
432 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int64 GetFirst() const
Definition: field.hxx:133
ExtTimeFieldFormat
Definition: vclenum.hxx:148
TimeFieldFormat
Definition: vclenum.hxx:28
bool mbFormatting
Definition: field.hxx:158
std::string GetValue
#define VCL_DLLPUBLIC
Definition: dllapi.h:29
sal_Int64 GetSpinSize() const
Definition: field.hxx:137
bool mbThousandSep
Definition: field.hxx:182
Any GetTime(const OUString &val)
void SetEmptyFieldValue()
Definition: field.cxx:514
sal_Int64 mnLastValue
Definition: field.hxx:154
bool IsEmptyFieldValueEnabled() const
Definition: field.hxx:83
virtual bool set_property(const OString &rKey, const OUString &rValue) override
Definition: edit.cxx:179
virtual void Up()
Definition: spinfld.cxx:359
std::unique_ptr< LocaleDataWrapper > mpLocaleDataWrapper
Definition: field.hxx:44
bool mbEmptyFieldValueEnabled
Definition: field.hxx:48
bool MustBeReformatted() const
Definition: field.hxx:67
void ClearField()
Definition: field.hxx:59
void SetSpinSize(sal_Int64 nNewSize)
Definition: field.hxx:136
void SetString(SwCursor &rCursor, const OUString &rString)
OUString GetString(int nId)
sal_Int64 WinBits
Edit * GetField() const
Definition: field.hxx:58
virtual void dispose() override
This is intended to be used to clear any locally held references to other Window-subclass objects...
Definition: spinfld.cxx:352
TimeFormat
Definition: vclenum.hxx:143
bool mbEmptyFieldValue
Definition: field.hxx:47
sal_uInt16 GetDecimalDigits() const
Definition: field.hxx:140
bool IsEmptyFieldValue() const
Definition: field.cxx:521
Definition: edit.hxx:56
virtual void Down()
Definition: spinfld.cxx:364
sal_Int64 GetMin() const
Definition: field.hxx:126
bool mbStrictFormat
Definition: field.hxx:46
virtual void ReformatAll()
Definition: field.cxx:467
virtual boost::property_tree::ptree DumpAsPropertyTree()
Dumps itself and potentially its children to a property tree, to be written easily to JSON...
Definition: window.cxx:3410
virtual bool EventNotify(NotifyEvent &rNEvt) override
Definition: spinfld.cxx:485
void clear()
Definition: vclptr.hxx:190
virtual void Reformat()
Definition: field.cxx:463
bool mbReformat
Definition: field.hxx:45
bool IsStrictFormat() const
Definition: field.hxx:71
void SetEmptyFieldValueData(bool bValue)
Definition: field.hxx:54
virtual void Modify()
Definition: edit.cxx:2310
void EnableEmptyFieldValue(bool bEnable)
Definition: field.hxx:82
const LocaleDataWrapper & GetLocaleDataWrapper(LanguageType nLang)
SAL_DLLPRIVATE bool ImplGetEmptyFieldValue() const
Definition: field.hxx:52
sal_Int64 mnLast
Definition: field.hxx:164
ExtDateFieldFormat
Definition: vclenum.hxx:156
void MarkToBeReformatted(bool b)
Definition: field.hxx:68
sal_Int64 mnMax
Definition: field.hxx:156
virtual void First()
Definition: spinfld.cxx:369
virtual void DataChanged(const DataChangedEvent &rDCEvt) override
Definition: spinfld.cxx:808
sal_Int64 GetMax() const
Definition: field.hxx:128
void SetFirst(sal_Int64 nNewFirst)
Definition: field.hxx:132
sal_Int64 GetLast() const
Definition: field.hxx:135
virtual Size CalcMinimumSize() const override
Definition: spinfld.cxx:917
sal_Int64 mnSpinSize
Definition: field.hxx:162
sal_Int64 mnFirst
Definition: field.hxx:163
virtual void Last()
Definition: spinfld.cxx:374
virtual bool PreNotify(NotifyEvent &rNEvt) override
Definition: spinfld.cxx:830
bool IsUseThousandSep() const
Definition: field.hxx:143
sal_Int64 mnMin
Definition: field.hxx:155
bool mbWrapOnLimits
Definition: field.hxx:157
void SetLast(sal_Int64 nNewLast)
Definition: field.hxx:134
sal_uInt16 mnDecimalDigits
Definition: field.hxx:181
void SetFormat(LotusContext &rContext, SCCOL nCol, SCROW nRow, SCTAB nTab, sal_uInt8 nFormat, sal_uInt8 nSt)
VclPtr< Edit > mpField
Definition: field.hxx:42