]>
git.saurik.com Git - wxWidgets.git/blob - samples/widgets/datepick.cpp
4295abc9c27a13004c62984f67a7c6395bb2adf2
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing date picker
5 // Author: Dimitri Schoolwerth, Vadim Zeitlin
6 // Created: 27 Sep 2003
8 // Copyright: (c) 2003 wxWindows team
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_DATEPICKCTRL
29 // for all others, include the necessary headers
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"
44 #include "wx/datectrl.h"
48 #include "icons/datepick.xpm"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
57 DatePickerPage_Reset
= wxID_HIGHEST
,
65 // ----------------------------------------------------------------------------
66 // CheckBoxWidgetsPage
67 // ----------------------------------------------------------------------------
69 class DatePickerWidgetsPage
: public WidgetsPage
72 DatePickerWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
73 virtual ~DatePickerWidgetsPage(){};
75 virtual wxControl
*GetWidget() const { return m_datePicker
; }
76 virtual void RecreateWidget() { CreateDatePicker(); }
78 // lazy creation of the content
79 virtual void CreateContent();
83 void OnButtonSet(wxCommandEvent
& event
);
85 void OnButtonReset(wxCommandEvent
& event
);
87 // reset the date picker parameters
90 // (re)create the date picker
91 void CreateDatePicker();
96 // the checkbox itself and the sizer it is in
97 wxDatePickerCtrl
*m_datePicker
;
98 wxSizer
*m_sizerDatePicker
;
104 // the text entries for command parameters
105 wxTextCtrl
*m_textLabel
;
108 DECLARE_EVENT_TABLE()
109 DECLARE_WIDGETS_PAGE(DatePickerWidgetsPage
)
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 BEGIN_EVENT_TABLE(DatePickerWidgetsPage
, WidgetsPage
)
117 EVT_BUTTON(DatePickerPage_Reset
, DatePickerWidgetsPage::OnButtonReset
)
118 EVT_BUTTON(DatePickerPage_Set
, DatePickerWidgetsPage::OnButtonSet
)
121 // ============================================================================
123 // ============================================================================
125 #if defined(__WXMSW__)
126 #define FAMILY_CTRLS NATIVE_CTRLS
128 #define FAMILY_CTRLS GENERIC_CTRLS
131 IMPLEMENT_WIDGETS_PAGE(DatePickerWidgetsPage
, wxT("DatePicker"),
132 FAMILY_CTRLS
| PICKER_CTRLS
135 DatePickerWidgetsPage::DatePickerWidgetsPage(WidgetsBookCtrl
*book
,
136 wxImageList
*imaglist
)
137 :WidgetsPage(book
, imaglist
, datepick_xpm
)
141 void DatePickerWidgetsPage::CreateContent()
143 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
146 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("Date details"));
148 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
150 sizerLeft
->Add( CreateSizerWithTextAndLabel( wxT("&Day:"), DatePickerPage_Day
, &m_day
),
151 0, wxALL
| wxALIGN_RIGHT
, 5 );
153 sizerLeft
->Add( CreateSizerWithTextAndLabel( wxT("&Month:"), DatePickerPage_Month
, &m_month
),
154 0, wxALL
| wxALIGN_RIGHT
, 5 );
156 sizerLeft
->Add( CreateSizerWithTextAndLabel( wxT("&Year:"), DatePickerPage_Year
, &m_year
),
157 0, wxALL
| wxALIGN_RIGHT
, 5 );
159 sizerLeft
->Add( new wxButton( this, DatePickerPage_Set
, wxT("&Set date") ),
163 wxSizer
*sizerRight
= new wxBoxSizer(wxHORIZONTAL
);
165 m_datePicker
= new wxDatePickerCtrl(this, DatePickerPage_Picker
);
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
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);
177 // final initializations
183 void DatePickerWidgetsPage::Reset()
185 const wxDateTime today
= wxDateTime::Today();
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()));
193 void DatePickerWidgetsPage::CreateDatePicker()
195 const wxDateTime value
= m_datePicker
->GetValue();
197 size_t count
= m_sizerDatePicker
->GetChildren().GetCount();
198 for ( size_t n
= 0; n
< count
; n
++ )
200 m_sizerDatePicker
->Remove(0);
205 m_datePicker
= new wxDatePickerCtrl(this, DatePickerPage_Picker
, value
);
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();
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
217 void DatePickerWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
224 void DatePickerWidgetsPage::OnButtonSet(wxCommandEvent
& WXUNUSED(event
))
226 long day
, month
, year
;
227 if ( m_day
->GetValue().ToLong(&day
) &&
228 m_month
->GetValue().ToLong(&month
) &&
229 m_year
->GetValue().ToLong(&year
) )
231 const wxDateTime
someDay(day
, wxDateTime::Month(month
), year
);
232 if ( someDay
.IsValid() )
234 m_datePicker
->SetValue(someDay
);
238 wxLogError(_T("Date is invalid"));
243 wxLogError(_T("One of inputs is not number"));
247 #endif // wxUSE_DATEPICKCTRL