]>
Commit | Line | Data |
---|---|---|
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 | |
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 | ||
f0fa4312 WS |
122 | #if defined(__WXMSW__) |
123 | #define FAMILY_CTRLS NATIVE_CTRLS | |
124 | #else | |
125 | #define FAMILY_CTRLS GENERIC_CTRLS | |
126 | #endif | |
127 | ||
f2fdc4d5 | 128 | IMPLEMENT_WIDGETS_PAGE(DatePickerWidgetsPage, wxT("DatePicker"), |
f0fa4312 | 129 | FAMILY_CTRLS | PICKER_CTRLS |
f2fdc4d5 WS |
130 | ); |
131 | ||
132 | DatePickerWidgetsPage::DatePickerWidgetsPage(WidgetsBookCtrl *book, | |
133 | wxImageList *imaglist) | |
134 | :WidgetsPage(book) | |
135 | { | |
136 | imaglist->Add(wxBitmap(datepick_xpm)); | |
137 | ||
138 | wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL); | |
139 | ||
140 | // left pane | |
141 | wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("Date details")); | |
142 | ||
143 | wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL); | |
144 | ||
d8d07a79 WS |
145 | sizerLeft->Add( CreateSizerWithTextAndLabel( wxT("&Day:"), DatePickerPage_Day , &m_day ), |
146 | 0, wxALL | wxALIGN_RIGHT , 5 ); | |
f2fdc4d5 | 147 | |
d8d07a79 WS |
148 | sizerLeft->Add( CreateSizerWithTextAndLabel( wxT("&Month:"), DatePickerPage_Month , &m_month ), |
149 | 0, wxALL | wxALIGN_RIGHT , 5 ); | |
f2fdc4d5 | 150 | |
d8d07a79 WS |
151 | sizerLeft->Add( CreateSizerWithTextAndLabel( wxT("&Year:"), DatePickerPage_Year , &m_year ), |
152 | 0, wxALL | wxALIGN_RIGHT , 5 ); | |
f2fdc4d5 WS |
153 | |
154 | sizerLeft->Add( new wxButton( this, wxID_ANY, wxT("&Set date") ), | |
d8d07a79 | 155 | 0, wxALL , 5 ); |
f2fdc4d5 WS |
156 | |
157 | // right pane | |
158 | wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL); | |
159 | ||
160 | m_datePicker = new wxDatePickerCtrl(this, DatePickerPage_Picker); | |
161 | ||
162 | sizerRight->Add(0, 0, 1, wxCENTRE); | |
163 | sizerRight->Add(m_datePicker, 1, wxCENTRE); | |
164 | sizerRight->Add(0, 0, 1, wxCENTRE); | |
165 | sizerRight->SetMinSize(150, 0); | |
166 | m_sizerDatePicker = sizerRight; // save it to modify it later | |
167 | ||
168 | // the 3 panes panes compose the window | |
169 | sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10); | |
170 | sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10); | |
171 | ||
172 | // final initializations | |
173 | Reset(); | |
174 | ||
175 | SetSizer(sizerTop); | |
176 | ||
177 | sizerTop->Fit(this); | |
178 | } | |
179 | ||
180 | void DatePickerWidgetsPage::Reset() | |
181 | { | |
182 | const wxDateTime today = wxDateTime::Today(); | |
183 | ||
184 | m_datePicker->SetValue(today); | |
185 | m_day->SetValue(wxString::Format(_T("%d"), today.GetDay())); | |
186 | m_month->SetValue(wxString::Format(_T("%d"), today.GetMonth())); | |
187 | m_year->SetValue(wxString::Format(_T("%d"), today.GetYear())); | |
188 | } | |
189 | ||
190 | void DatePickerWidgetsPage::CreateDatePicker() | |
191 | { | |
192 | const wxDateTime value = m_datePicker->GetValue(); | |
193 | ||
194 | size_t count = m_sizerDatePicker->GetChildren().GetCount(); | |
195 | for ( size_t n = 0; n < count; n++ ) | |
196 | { | |
197 | m_sizerDatePicker->Remove(0); | |
198 | } | |
199 | ||
200 | delete m_datePicker; | |
201 | ||
202 | m_datePicker = new wxDatePickerCtrl(this, DatePickerPage_Picker, value); | |
203 | ||
204 | m_sizerDatePicker->Add(0, 0, 1, wxCENTRE); | |
205 | m_sizerDatePicker->Add(m_datePicker, 1, wxCENTRE); | |
206 | m_sizerDatePicker->Add(0, 0, 1, wxCENTRE); | |
207 | m_sizerDatePicker->Layout(); | |
208 | } | |
209 | ||
210 | // ---------------------------------------------------------------------------- | |
211 | // event handlers | |
212 | // ---------------------------------------------------------------------------- | |
213 | ||
214 | void DatePickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event)) | |
215 | { | |
216 | Reset(); | |
217 | ||
218 | CreateDatePicker(); | |
219 | } | |
220 | ||
221 | void DatePickerWidgetsPage::OnButtonSet(wxCommandEvent& WXUNUSED(event)) | |
222 | { | |
223 | } | |
224 | ||
225 | #endif // wxUSE_DATEPICKCTRL |