Reorganized the date picker page of the widgets sample.

Make it possible to set the picker styles and set its range and not only the
current value.

Also log the events generated by it.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@70068 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2011-12-20 21:27:02 +00:00
parent 7a7e178339
commit 54450d0907

View File

@@ -42,6 +42,7 @@
#endif #endif
#include "wx/datectrl.h" #include "wx/datectrl.h"
#include "wx/dateevt.h"
#include "widgets.h" #include "widgets.h"
@@ -55,10 +56,8 @@
enum enum
{ {
DatePickerPage_Reset = wxID_HIGHEST, DatePickerPage_Reset = wxID_HIGHEST,
DatePickerPage_Day,
DatePickerPage_Month,
DatePickerPage_Year,
DatePickerPage_Set, DatePickerPage_Set,
DatePickerPage_SetRange,
DatePickerPage_Picker DatePickerPage_Picker
}; };
@@ -80,8 +79,10 @@ public:
protected: protected:
// event handlers // event handlers
void OnButtonSet(wxCommandEvent& event); void OnDateChanged(wxDateEvent& event);
void OnButtonSet(wxCommandEvent& event);
void OnButtonSetRange(wxCommandEvent& event);
void OnButtonReset(wxCommandEvent& event); void OnButtonReset(wxCommandEvent& event);
// reset the date picker parameters // reset the date picker parameters
@@ -97,9 +98,13 @@ protected:
wxDatePickerCtrl *m_datePicker; wxDatePickerCtrl *m_datePicker;
wxSizer *m_sizerDatePicker; wxSizer *m_sizerDatePicker;
wxTextCtrl *m_day; wxTextCtrl *m_textCur;
wxTextCtrl *m_month; wxTextCtrl *m_textMin;
wxTextCtrl *m_year; wxTextCtrl *m_textMax;
wxRadioBox* m_radioKind;
wxCheckBox* m_chkStyleCentury;
wxCheckBox* m_chkStyleAllowNone;
// the text entries for command parameters // the text entries for command parameters
wxTextCtrl *m_textLabel; wxTextCtrl *m_textLabel;
@@ -116,6 +121,9 @@ private:
BEGIN_EVENT_TABLE(DatePickerWidgetsPage, WidgetsPage) BEGIN_EVENT_TABLE(DatePickerWidgetsPage, WidgetsPage)
EVT_BUTTON(DatePickerPage_Reset, DatePickerWidgetsPage::OnButtonReset) EVT_BUTTON(DatePickerPage_Reset, DatePickerWidgetsPage::OnButtonReset)
EVT_BUTTON(DatePickerPage_Set, DatePickerWidgetsPage::OnButtonSet) EVT_BUTTON(DatePickerPage_Set, DatePickerWidgetsPage::OnButtonSet)
EVT_BUTTON(DatePickerPage_SetRange, DatePickerWidgetsPage::OnButtonSetRange)
EVT_DATE_CHANGED(wxID_ANY, DatePickerWidgetsPage::OnDateChanged)
END_EVENT_TABLE() END_EVENT_TABLE()
// ============================================================================ // ============================================================================
@@ -142,24 +150,60 @@ void DatePickerWidgetsPage::CreateContent()
{ {
wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL); wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
// left pane // left pane: style
wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("Date details")); wxSizer* const sizerLeft = new wxBoxSizer(wxVERTICAL);
wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL); static const wxString kinds[] = { "&Default", "&Spin", "Drop do&wn" };
m_radioKind = new wxRadioBox(this, wxID_ANY, "&Kind",
wxDefaultPosition, wxDefaultSize,
WXSIZEOF(kinds), kinds,
1, wxRA_SPECIFY_COLS);
sizerLeft->Add(m_radioKind, wxSizerFlags().Expand().Border());
sizerLeft->Add( CreateSizerWithTextAndLabel( wxT("&Day:"), DatePickerPage_Day , &m_day ), wxSizer* const sizerStyle = new wxStaticBoxSizer(wxVERTICAL, this, "&Style");
0, wxALL | wxALIGN_RIGHT , 5 ); m_chkStyleCentury = CreateCheckBoxAndAddToSizer(sizerStyle, "Show &century");
m_chkStyleAllowNone = CreateCheckBoxAndAddToSizer(sizerStyle, "Allow &no value");
sizerLeft->Add( CreateSizerWithTextAndLabel( wxT("&Month:"), DatePickerPage_Month , &m_month ), sizerLeft->Add(sizerStyle, wxSizerFlags().Expand().Border());
0, wxALL | wxALIGN_RIGHT , 5 );
sizerLeft->Add( CreateSizerWithTextAndLabel( wxT("&Year:"), DatePickerPage_Year , &m_year ), sizerLeft->Add(new wxButton(this, DatePickerPage_Reset, "&Recreate"),
0, wxALL | wxALIGN_RIGHT , 5 ); wxSizerFlags().Centre().Border());
sizerLeft->Add( new wxButton( this, DatePickerPage_Set, wxT("&Set date") ),
0, wxALL , 5 );
// right pane // middle pane: operations
wxSizer* const sizerMiddle = new wxBoxSizer(wxVERTICAL);
sizerMiddle->Add(CreateSizerWithTextAndButton
(
DatePickerPage_Set,
"&Set date",
wxID_ANY,
&m_textCur
),
wxSizerFlags().Expand().Border());
m_textCur->SetMinSize(wxSize(GetTextExtent(" 9999-99-99 ").x, -1));
sizerMiddle->AddSpacer(10);
sizerMiddle->Add(CreateSizerWithTextAndLabel
(
"&Min date",
wxID_ANY,
&m_textMin
),
wxSizerFlags().Expand().Border());
sizerMiddle->Add(CreateSizerWithTextAndLabel
(
"Ma&x date",
wxID_ANY,
&m_textMax
),
wxSizerFlags().Expand().Border());
sizerMiddle->Add(new wxButton(this, DatePickerPage_SetRange, "Set &range"),
wxSizerFlags().Centre().Border());
// right pane: control itself
wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL); wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
m_datePicker = new wxDatePickerCtrl(this, DatePickerPage_Picker); m_datePicker = new wxDatePickerCtrl(this, DatePickerPage_Picker);
@@ -172,9 +216,11 @@ void DatePickerWidgetsPage::CreateContent()
// the 3 panes panes compose the window // the 3 panes panes compose the window
sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10); sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
sizerTop->Add(sizerMiddle, 0, (wxTOP | wxBOTTOM), 10);
sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10); sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
// final initializations // final initializations
m_chkStyleCentury->SetValue(true);
Reset(); Reset();
SetSizer(sizerTop); SetSizer(sizerTop);
@@ -185,9 +231,7 @@ void DatePickerWidgetsPage::Reset()
const wxDateTime today = wxDateTime::Today(); const wxDateTime today = wxDateTime::Today();
m_datePicker->SetValue(today); m_datePicker->SetValue(today);
m_day->SetValue(wxString::Format(wxT("%d"), today.GetDay())); m_textCur->SetValue(today.FormatISODate());
m_month->SetValue(wxString::Format(wxT("%d"), today.GetMonth() + 1));
m_year->SetValue(wxString::Format(wxT("%d"), today.GetYear()));
} }
void DatePickerWidgetsPage::CreateDatePicker() void DatePickerWidgetsPage::CreateDatePicker()
@@ -202,7 +246,30 @@ void DatePickerWidgetsPage::CreateDatePicker()
delete m_datePicker; delete m_datePicker;
m_datePicker = new wxDatePickerCtrl(this, DatePickerPage_Picker, value); long style = 0;
switch ( m_radioKind->GetSelection() )
{
case 0:
style = wxDP_DEFAULT;
break;
case 1:
style = wxDP_SPIN;
break;
case 2:
style = wxDP_DROPDOWN;
break;
}
if ( m_chkStyleCentury->GetValue() )
style |= wxDP_SHOWCENTURY;
if ( m_chkStyleAllowNone->GetValue() )
style |= wxDP_ALLOWNONE;
m_datePicker = new wxDatePickerCtrl(this, DatePickerPage_Picker, value,
wxDefaultPosition, wxDefaultSize,
style);
m_sizerDatePicker->Add(0, 0, 1, wxCENTRE); m_sizerDatePicker->Add(0, 0, 1, wxCENTRE);
m_sizerDatePicker->Add(m_datePicker, 1, wxCENTRE); m_sizerDatePicker->Add(m_datePicker, 1, wxCENTRE);
@@ -221,29 +288,56 @@ void DatePickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
CreateDatePicker(); CreateDatePicker();
} }
static bool GetDateFromTextControl(wxDateTime& dt, const wxTextCtrl* text)
{
const wxString& value = text->GetValue();
if ( !value.empty() )
{
wxString::const_iterator end;
if ( !dt.ParseDate(value, &end) || end != value.end() )
{
wxLogError("Invalid date \"%s\"");
return false;
}
}
return true;
}
void DatePickerWidgetsPage::OnButtonSet(wxCommandEvent& WXUNUSED(event)) void DatePickerWidgetsPage::OnButtonSet(wxCommandEvent& WXUNUSED(event))
{ {
long day = 0, wxDateTime dt;
month = 0, if ( GetDateFromTextControl(dt, m_textCur) )
year = 0; m_datePicker->SetValue(dt);
if ( m_day->GetValue().ToLong(&day) && }
m_month->GetValue().ToLong(&month) &&
m_year->GetValue().ToLong(&year) ) void DatePickerWidgetsPage::OnButtonSetRange(wxCommandEvent& WXUNUSED(event))
{
wxDateTime dt1, dt2;
if ( !GetDateFromTextControl(dt1, m_textMin) ||
!GetDateFromTextControl(dt2, m_textMax) )
return;
m_datePicker->SetRange(dt1, dt2);
if ( !m_datePicker->GetRange(&dt1, &dt2) )
{ {
const wxDateTime someDay(day, wxDateTime::Month(month - 1), year); wxLogMessage("No range set");
if ( someDay.IsValid() )
{
m_datePicker->SetValue(someDay);
} }
else else
{ {
wxLogError(wxT("Date is invalid")); m_textMin->SetValue(dt1.IsValid() ? dt1.FormatISODate() : wxString());
} m_textMax->SetValue(dt2.IsValid() ? dt2.FormatISODate() : wxString());
}
else wxLogMessage("Date picker range updated");
{
wxLogError(wxT("One of inputs is not number"));
} }
} }
void DatePickerWidgetsPage::OnDateChanged(wxDateEvent& event)
{
wxLogMessage("Date changed, now is %s (control value is %s).",
event.GetDate().FormatISOCombined(),
m_datePicker->GetValue().FormatISOCombined());
}
#endif // wxUSE_DATEPICKCTRL #endif // wxUSE_DATEPICKCTRL