wxUniv port mentioned instead of native, wxButton ports listed, minor placement fixes.
[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 // 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
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 protected:
79 // event handlers
80 void OnButtonSet(wxCommandEvent& event);
81
82 void OnButtonReset(wxCommandEvent& event);
83
84 // reset the date picker parameters
85 void Reset();
86
87 // (re)create the date picker
88 void CreateDatePicker();
89
90 // the controls
91 // ------------
92
93 // the checkbox itself and the sizer it is in
94 wxDatePickerCtrl *m_datePicker;
95 wxSizer *m_sizerDatePicker;
96
97 wxTextCtrl *m_day;
98 wxTextCtrl *m_month;
99 wxTextCtrl *m_year;
100
101 // the text entries for command parameters
102 wxTextCtrl *m_textLabel;
103
104 private:
105 DECLARE_EVENT_TABLE()
106 DECLARE_WIDGETS_PAGE(DatePickerWidgetsPage)
107 };
108
109 // ----------------------------------------------------------------------------
110 // event tables
111 // ----------------------------------------------------------------------------
112
113 BEGIN_EVENT_TABLE(DatePickerWidgetsPage, WidgetsPage)
114 EVT_BUTTON(DatePickerPage_Reset, DatePickerWidgetsPage::OnButtonReset)
115 EVT_BUTTON(DatePickerPage_Set, DatePickerWidgetsPage::OnButtonSet)
116 END_EVENT_TABLE()
117
118 // ============================================================================
119 // implementation
120 // ============================================================================
121
122 IMPLEMENT_WIDGETS_PAGE(DatePickerWidgetsPage, wxT("DatePicker"),
123 (int)wxPlatform(GENERIC_CTRLS).If(wxMSW,NATIVE_CTRLS)
124 | PICKER_CTRLS
125 );
126
127 DatePickerWidgetsPage::DatePickerWidgetsPage(WidgetsBookCtrl *book,
128 wxImageList *imaglist)
129 :WidgetsPage(book)
130 {
131 imaglist->Add(wxBitmap(datepick_xpm));
132
133 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
134
135 // left pane
136 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("Date details"));
137
138 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
139
140 sizerLeft->Add( CreateSizerWithTextAndLabel( wxT("&Day:"), DatePickerPage_Day , &m_day ),
141 0, wxALL | wxALIGN_RIGHT , 5 );
142
143 sizerLeft->Add( CreateSizerWithTextAndLabel( wxT("&Month:"), DatePickerPage_Month , &m_month ),
144 0, wxALL | wxALIGN_RIGHT , 5 );
145
146 sizerLeft->Add( CreateSizerWithTextAndLabel( wxT("&Year:"), DatePickerPage_Year , &m_year ),
147 0, wxALL | wxALIGN_RIGHT , 5 );
148
149 sizerLeft->Add( new wxButton( this, wxID_ANY, wxT("&Set date") ),
150 0, wxALL , 5 );
151
152 // right pane
153 wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
154
155 m_datePicker = new wxDatePickerCtrl(this, DatePickerPage_Picker);
156
157 sizerRight->Add(0, 0, 1, wxCENTRE);
158 sizerRight->Add(m_datePicker, 1, wxCENTRE);
159 sizerRight->Add(0, 0, 1, wxCENTRE);
160 sizerRight->SetMinSize(150, 0);
161 m_sizerDatePicker = sizerRight; // save it to modify it later
162
163 // the 3 panes panes compose the window
164 sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
165 sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
166
167 // final initializations
168 Reset();
169
170 SetSizer(sizerTop);
171
172 sizerTop->Fit(this);
173 }
174
175 void DatePickerWidgetsPage::Reset()
176 {
177 const wxDateTime today = wxDateTime::Today();
178
179 m_datePicker->SetValue(today);
180 m_day->SetValue(wxString::Format(_T("%d"), today.GetDay()));
181 m_month->SetValue(wxString::Format(_T("%d"), today.GetMonth()));
182 m_year->SetValue(wxString::Format(_T("%d"), today.GetYear()));
183 }
184
185 void DatePickerWidgetsPage::CreateDatePicker()
186 {
187 const wxDateTime value = m_datePicker->GetValue();
188
189 size_t count = m_sizerDatePicker->GetChildren().GetCount();
190 for ( size_t n = 0; n < count; n++ )
191 {
192 m_sizerDatePicker->Remove(0);
193 }
194
195 delete m_datePicker;
196
197 m_datePicker = new wxDatePickerCtrl(this, DatePickerPage_Picker, value);
198
199 m_sizerDatePicker->Add(0, 0, 1, wxCENTRE);
200 m_sizerDatePicker->Add(m_datePicker, 1, wxCENTRE);
201 m_sizerDatePicker->Add(0, 0, 1, wxCENTRE);
202 m_sizerDatePicker->Layout();
203 }
204
205 // ----------------------------------------------------------------------------
206 // event handlers
207 // ----------------------------------------------------------------------------
208
209 void DatePickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
210 {
211 Reset();
212
213 CreateDatePicker();
214 }
215
216 void DatePickerWidgetsPage::OnButtonSet(wxCommandEvent& WXUNUSED(event))
217 {
218 }
219
220 #endif // wxUSE_DATEPICKCTRL