1 // This may look like C code, but it's really -*- C++ -*-
2 /*
3  * Copyright (C) 2012 Emweb bv, Herent, Belgium.
4  *
5  * See the LICENSE file for terms of use.
6  */
7 #ifndef WDATE_EDIT_H_
8 #define WDATE_EDIT_H_
9 
10 #include <Wt/WDateValidator.h>
11 #include <Wt/WLineEdit.h>
12 
13 namespace Wt {
14 
15 /*! \class WDateEdit Wt/WDateEdit.h Wt/WDateEdit.h
16  *  \brief A date edit.
17  *
18  * A date picker is a line edit with support for date entry (using an
19  * icon and a calendar).
20  *
21  * A WDateValidator is used to validate date entry.
22  *
23  * In many cases, it provides a more convenient implementation of a
24  * date picker compared to WDatePicker since it is implemented as a
25  * line edit. This also makes the implementation ready for a native
26  * HTML5 control.
27  */
28 class WT_API WDateEdit : public WLineEdit
29 {
30 public:
31   /*! \brief Creates a new date edit.
32    */
33   WDateEdit();
34 
35   virtual ~WDateEdit();
36 
37   /*! \brief Sets the date.
38    *
39    * Does nothing if the current date is \p Null.
40    *
41    * \sa date()
42    */
43   void setDate(const WDate& date);
44 
45   /*! \brief Returns the date.
46    *
47    * Reads the current date.
48    *
49    * \if cpp
50    * Returns an invalid date (for which WDate::isValid() returns
51    * \c false) if the date could not be parsed using the current
52    * format(). <br>
53    * \elseif java
54    * Returns \c null if the date could not be parsed using the current
55    * format(). <br>
56    * \endif
57    *
58    * \sa setDate(), WDate::fromString(), WLineEdit::text()
59    */
60   WDate date() const;
61 
62   /*! \brief Returns the validator.
63    *
64    * Most of the configuration of the date edit is stored in the
65    * validator.
66    */
67   virtual std::shared_ptr<WDateValidator> dateValidator() const;
68 
69   /*! \brief Sets the format used for representing the date.
70    *
71    * This sets the format in the validator.
72    *
73    * The default format is based on the current WLocale.
74    *
75    * \sa WDateValidator::setFormat()
76    */
77   void setFormat(const WT_USTRING& format);
78 
79   /*! \brief Returns the format.
80    *
81    * \sa setFormat()
82    */
83   WT_USTRING format() const;
84 
85   /*! \brief Sets the lower limit of the valid date range.
86    *
87    * This sets the lower limit of the valid date range in the
88    * validator.
89    *
90    * \sa WDateValidator::setBottom()
91    */
92   void setBottom(const WDate& bottom);
93 
94   /*! \brief Returns the lower limit of the valid date range.
95    *
96    * \sa setBottom()
97    */
98   WDate bottom() const;
99 
100   /*! \brief Sets the upper limit of the valid date range.
101    *
102    * This sets the upper limit of the valid date range in the
103    * validator.
104    *
105    * \sa WDateValidator::setTop()
106    */
107   void setTop(const WDate& top);
108 
109   /*! \brief Returns the upper limit of the valid range.
110    *
111    * \sa setTop()
112    */
113   WDate top() const;
114 
115   /*! \brief Returns the calendar widget.
116    *
117    * The calendar may be 0 (e.g. when using a native date entry
118    * widget).
119    */
calendar()120   WCalendar *calendar() const { return calendar_; }
121 
122   /*! \brief Hide/unhide the widget.
123    */
124   virtual void setHidden(bool hidden,
125                          const WAnimation& animation = WAnimation()) override;
126 
127   virtual void load() override;
128   virtual void refresh() override;
129 
130 protected:
131   virtual void render(WFlags<RenderFlag> flags) override;
132   virtual void propagateSetEnabled(bool enabled) override;
133 
134   /*! \brief Sets the value from the calendar to the line edit.
135    */
136   virtual void setFromCalendar();
137 
138   /*! \brief Sets the value from the line edit to the calendar.
139    */
140   virtual void setFromLineEdit();
141 
142 private:
143   std::unique_ptr<WPopupWidget> popup_;
144   std::unique_ptr<WCalendar> uCalendar_;
145   WCalendar *calendar_;
146   bool customFormat_;
147 
148   void defineJavaScript();
149   void connectJavaScript(Wt::EventSignalBase& s, const std::string& methodName);
150   void setFocusTrue();
151 };
152 
153 }
154 
155 #endif // WDATE_EDIT_H_
156