]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/datepick.cpp
ensure all topic overviews have an horizontal rule with the list of the sections...
[wxWidgets.git] / samples / widgets / datepick.cpp
CommitLineData
f2fdc4d5
WS
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// License: wxWindows license
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
55enum
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
69class DatePickerWidgetsPage : public WidgetsPage
70{
71public:
72 DatePickerWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
73 virtual ~DatePickerWidgetsPage(){};
74
75 virtual wxControl *GetWidget() const { return m_datePicker; }
76 virtual void RecreateWidget() { CreateDatePicker(); }
77
453535a7
WS
78 // lazy creation of the content
79 virtual void CreateContent();
80
f2fdc4d5
WS
81protected:
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
107private:
108 DECLARE_EVENT_TABLE()
109 DECLARE_WIDGETS_PAGE(DatePickerWidgetsPage)
110};
111
112// ----------------------------------------------------------------------------
113// event tables
114// ----------------------------------------------------------------------------
115
116BEGIN_EVENT_TABLE(DatePickerWidgetsPage, WidgetsPage)
117 EVT_BUTTON(DatePickerPage_Reset, DatePickerWidgetsPage::OnButtonReset)
118 EVT_BUTTON(DatePickerPage_Set, DatePickerWidgetsPage::OnButtonSet)
119END_EVENT_TABLE()
120
121// ============================================================================
122// implementation
123// ============================================================================
124
f0fa4312
WS
125#if defined(__WXMSW__)
126 #define FAMILY_CTRLS NATIVE_CTRLS
127#else
128 #define FAMILY_CTRLS GENERIC_CTRLS
129#endif
130
f2fdc4d5 131IMPLEMENT_WIDGETS_PAGE(DatePickerWidgetsPage, wxT("DatePicker"),
f0fa4312 132 FAMILY_CTRLS | PICKER_CTRLS
f2fdc4d5
WS
133 );
134
135DatePickerWidgetsPage::DatePickerWidgetsPage(WidgetsBookCtrl *book,
136 wxImageList *imaglist)
261357eb 137 :WidgetsPage(book, imaglist, datepick_xpm)
453535a7
WS
138{
139}
140
141void DatePickerWidgetsPage::CreateContent()
f2fdc4d5 142{
f2fdc4d5
WS
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
d8d07a79
WS
150 sizerLeft->Add( CreateSizerWithTextAndLabel( wxT("&Day:"), DatePickerPage_Day , &m_day ),
151 0, wxALL | wxALIGN_RIGHT , 5 );
f2fdc4d5 152
d8d07a79
WS
153 sizerLeft->Add( CreateSizerWithTextAndLabel( wxT("&Month:"), DatePickerPage_Month , &m_month ),
154 0, wxALL | wxALIGN_RIGHT , 5 );
f2fdc4d5 155
d8d07a79
WS
156 sizerLeft->Add( CreateSizerWithTextAndLabel( wxT("&Year:"), DatePickerPage_Year , &m_year ),
157 0, wxALL | wxALIGN_RIGHT , 5 );
f2fdc4d5 158
95c336d6 159 sizerLeft->Add( new wxButton( this, DatePickerPage_Set, wxT("&Set date") ),
d8d07a79 160 0, wxALL , 5 );
f2fdc4d5
WS
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);
f2fdc4d5
WS
181}
182
183void DatePickerWidgetsPage::Reset()
184{
185 const wxDateTime today = wxDateTime::Today();
186
187 m_datePicker->SetValue(today);
188 m_day->SetValue(wxString::Format(_T("%d"), today.GetDay()));
189 m_month->SetValue(wxString::Format(_T("%d"), today.GetMonth()));
190 m_year->SetValue(wxString::Format(_T("%d"), today.GetYear()));
191}
192
193void 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
217void DatePickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
218{
219 Reset();
220
221 CreateDatePicker();
222}
223
224void DatePickerWidgetsPage::OnButtonSet(wxCommandEvent& WXUNUSED(event))
225{
1525e543
VZ
226 long day = 0,
227 month = 0,
228 year = 0;
95c336d6
WS
229 if ( m_day->GetValue().ToLong(&day) &&
230 m_month->GetValue().ToLong(&month) &&
231 m_year->GetValue().ToLong(&year) )
232 {
84abef1a 233 const wxDateTime someDay(day, wxDateTime::Month(month - 1), year);
2995b688 234 if ( someDay.IsValid() )
95c336d6
WS
235 {
236 m_datePicker->SetValue(someDay);
237 }
238 else
239 {
240 wxLogError(_T("Date is invalid"));
241 }
242 }
243 else
244 {
245 wxLogError(_T("One of inputs is not number"));
246 }
f2fdc4d5
WS
247}
248
249#endif // wxUSE_DATEPICKCTRL