]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/datepick.cpp
set eol-style and keywords properties on new files
[wxWidgets.git] / samples / widgets / datepick.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: datepick.cpp
4 // Purpose: Part of the widgets sample showing date picker
5 // Author: Dimitri Schoolwerth, Vadim Zeitlin
6 // Created: 27 Sep 2003
7 // Id: $Id$
8 // Copyright: (c) 2003 wxWindows team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_DATEPICKCTRL
28
29 // for all others, include the necessary headers
30 #ifndef WX_PRECOMP
31 #include "wx/app.h"
32 #include "wx/log.h"
33
34 #include "wx/bitmap.h"
35 #include "wx/button.h"
36 #include "wx/checkbox.h"
37 #include "wx/radiobox.h"
38 #include "wx/statbox.h"
39 #include "wx/textctrl.h"
40
41 #include "wx/sizer.h"
42 #endif
43
44 #include "wx/datectrl.h"
45
46 #include "widgets.h"
47
48 #include "icons/datepick.xpm"
49
50 // ----------------------------------------------------------------------------
51 // constants
52 // ----------------------------------------------------------------------------
53
54 // control ids
55 enum
56 {
57 DatePickerPage_Reset = wxID_HIGHEST,
58 DatePickerPage_Day,
59 DatePickerPage_Month,
60 DatePickerPage_Year,
61 DatePickerPage_Set,
62 DatePickerPage_Picker
63 };
64
65 // ----------------------------------------------------------------------------
66 // CheckBoxWidgetsPage
67 // ----------------------------------------------------------------------------
68
69 class DatePickerWidgetsPage : public WidgetsPage
70 {
71 public:
72 DatePickerWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
73 virtual ~DatePickerWidgetsPage(){};
74
75 virtual wxControl *GetWidget() const { return m_datePicker; }
76 virtual void RecreateWidget() { CreateDatePicker(); }
77
78 // lazy creation of the content
79 virtual void CreateContent();
80
81 protected:
82 // event handlers
83 void OnButtonSet(wxCommandEvent& event);
84
85 void OnButtonReset(wxCommandEvent& event);
86
87 // reset the date picker parameters
88 void Reset();
89
90 // (re)create the date picker
91 void CreateDatePicker();
92
93 // the controls
94 // ------------
95
96 // the checkbox itself and the sizer it is in
97 wxDatePickerCtrl *m_datePicker;
98 wxSizer *m_sizerDatePicker;
99
100 wxTextCtrl *m_day;
101 wxTextCtrl *m_month;
102 wxTextCtrl *m_year;
103
104 // the text entries for command parameters
105 wxTextCtrl *m_textLabel;
106
107 private:
108 DECLARE_EVENT_TABLE()
109 DECLARE_WIDGETS_PAGE(DatePickerWidgetsPage)
110 };
111
112 // ----------------------------------------------------------------------------
113 // event tables
114 // ----------------------------------------------------------------------------
115
116 BEGIN_EVENT_TABLE(DatePickerWidgetsPage, WidgetsPage)
117 EVT_BUTTON(DatePickerPage_Reset, DatePickerWidgetsPage::OnButtonReset)
118 EVT_BUTTON(DatePickerPage_Set, DatePickerWidgetsPage::OnButtonSet)
119 END_EVENT_TABLE()
120
121 // ============================================================================
122 // implementation
123 // ============================================================================
124
125 #if defined(__WXMSW__)
126 #define FAMILY_CTRLS NATIVE_CTRLS
127 #else
128 #define FAMILY_CTRLS GENERIC_CTRLS
129 #endif
130
131 IMPLEMENT_WIDGETS_PAGE(DatePickerWidgetsPage, wxT("DatePicker"),
132 FAMILY_CTRLS | PICKER_CTRLS
133 );
134
135 DatePickerWidgetsPage::DatePickerWidgetsPage(WidgetsBookCtrl *book,
136 wxImageList *imaglist)
137 :WidgetsPage(book, imaglist, datepick_xpm)
138 {
139 }
140
141 void DatePickerWidgetsPage::CreateContent()
142 {
143 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
144
145 // left pane
146 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("Date details"));
147
148 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
149
150 sizerLeft->Add( CreateSizerWithTextAndLabel( wxT("&Day:"), DatePickerPage_Day , &m_day ),
151 0, wxALL | wxALIGN_RIGHT , 5 );
152
153 sizerLeft->Add( CreateSizerWithTextAndLabel( wxT("&Month:"), DatePickerPage_Month , &m_month ),
154 0, wxALL | wxALIGN_RIGHT , 5 );
155
156 sizerLeft->Add( CreateSizerWithTextAndLabel( wxT("&Year:"), DatePickerPage_Year , &m_year ),
157 0, wxALL | wxALIGN_RIGHT , 5 );
158
159 sizerLeft->Add( new wxButton( this, DatePickerPage_Set, wxT("&Set date") ),
160 0, wxALL , 5 );
161
162 // right pane
163 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
164
165 m_datePicker = new wxDatePickerCtrl(this, DatePickerPage_Picker);
166
167 sizerRight->Add(0, 0, 1, wxCENTRE);
168 sizerRight->Add(m_datePicker, 1, wxCENTRE);
169 sizerRight->Add(0, 0, 1, wxCENTRE);
170 sizerRight->SetMinSize(150, 0);
171 m_sizerDatePicker = sizerRight; // save it to modify it later
172
173 // the 3 panes panes compose the window
174 sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
175 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
176
177 // final initializations
178 Reset();
179
180 SetSizer(sizerTop);
181 }
182
183 void DatePickerWidgetsPage::Reset()
184 {
185 const wxDateTime today = wxDateTime::Today();
186
187 m_datePicker->SetValue(today);
188 m_day->SetValue(wxString::Format(wxT("%d"), today.GetDay()));
189 m_month->SetValue(wxString::Format(wxT("%d"), today.GetMonth()));
190 m_year->SetValue(wxString::Format(wxT("%d"), today.GetYear()));
191 }
192
193 void DatePickerWidgetsPage::CreateDatePicker()
194 {
195 const wxDateTime value = m_datePicker->GetValue();
196
197 size_t count = m_sizerDatePicker->GetChildren().GetCount();
198 for ( size_t n = 0; n < count; n++ )
199 {
200 m_sizerDatePicker->Remove(0);
201 }
202
203 delete m_datePicker;
204
205 m_datePicker = new wxDatePickerCtrl(this, DatePickerPage_Picker, value);
206
207 m_sizerDatePicker->Add(0, 0, 1, wxCENTRE);
208 m_sizerDatePicker->Add(m_datePicker, 1, wxCENTRE);
209 m_sizerDatePicker->Add(0, 0, 1, wxCENTRE);
210 m_sizerDatePicker->Layout();
211 }
212
213 // ----------------------------------------------------------------------------
214 // event handlers
215 // ----------------------------------------------------------------------------
216
217 void DatePickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
218 {
219 Reset();
220
221 CreateDatePicker();
222 }
223
224 void DatePickerWidgetsPage::OnButtonSet(wxCommandEvent& WXUNUSED(event))
225 {
226 long day = 0,
227 month = 0,
228 year = 0;
229 if ( m_day->GetValue().ToLong(&day) &&
230 m_month->GetValue().ToLong(&month) &&
231 m_year->GetValue().ToLong(&year) )
232 {
233 const wxDateTime someDay(day, wxDateTime::Month(month - 1), year);
234 if ( someDay.IsValid() )
235 {
236 m_datePicker->SetValue(someDay);
237 }
238 else
239 {
240 wxLogError(wxT("Date is invalid"));
241 }
242 }
243 else
244 {
245 wxLogError(wxT("One of inputs is not number"));
246 }
247 }
248
249 #endif // wxUSE_DATEPICKCTRL