]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/widgets/datepick.cpp
Support using GetTextExtent() with empty string to get descent in wxOSX.
[wxWidgets.git] / samples / widgets / datepick.cpp
index 65c5f84b8732b8689ec52743b02741e7873c0e0a..f9cdbced6210c9d5b4e0446e03cb4c254ac0ee7c 100644 (file)
@@ -6,7 +6,7 @@
 // Created:     27 Sep 2003
 // Id:          $Id$
 // Copyright:   (c) 2003 wxWindows team
 // Created:     27 Sep 2003
 // Id:          $Id$
 // Copyright:   (c) 2003 wxWindows team
-// License:     wxWindows license
+// Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 // ============================================================================
 /////////////////////////////////////////////////////////////////////////////
 
 // ============================================================================
@@ -42,6 +42,7 @@
 #endif
 
 #include "wx/datectrl.h"
 #endif
 
 #include "wx/datectrl.h"
+#include "wx/dateevt.h"
 
 #include "widgets.h"
 
 
 #include "widgets.h"
 
 enum
 {
     DatePickerPage_Reset = wxID_HIGHEST,
 enum
 {
     DatePickerPage_Reset = wxID_HIGHEST,
-    DatePickerPage_Day,
-    DatePickerPage_Month,
-    DatePickerPage_Year,
     DatePickerPage_Set,
     DatePickerPage_Set,
+    DatePickerPage_SetRange,
     DatePickerPage_Picker
 };
 
     DatePickerPage_Picker
 };
 
@@ -75,10 +74,15 @@ public:
     virtual wxControl *GetWidget() const { return m_datePicker; }
     virtual void RecreateWidget() { CreateDatePicker(); }
 
     virtual wxControl *GetWidget() const { return m_datePicker; }
     virtual void RecreateWidget() { CreateDatePicker(); }
 
+    // lazy creation of the content
+    virtual void CreateContent();
+
 protected:
     // event handlers
 protected:
     // event handlers
-    void OnButtonSet(wxCommandEvent& event);
+    void OnDateChanged(wxDateEvent& event);
 
 
+    void OnButtonSet(wxCommandEvent& event);
+    void OnButtonSetRange(wxCommandEvent& event);
     void OnButtonReset(wxCommandEvent& event);
 
     // reset the date picker parameters
     void OnButtonReset(wxCommandEvent& event);
 
     // reset the date picker parameters
@@ -94,9 +98,13 @@ protected:
     wxDatePickerCtrl *m_datePicker;
     wxSizer *m_sizerDatePicker;
 
     wxDatePickerCtrl *m_datePicker;
     wxSizer *m_sizerDatePicker;
 
-    wxTextCtrl *m_day;
-    wxTextCtrl *m_month;
-    wxTextCtrl *m_year;
+    wxTextCtrl *m_textCur;
+    wxTextCtrl *m_textMin;
+    wxTextCtrl *m_textMax;
+
+    wxRadioBox* m_radioKind;
+    wxCheckBox* m_chkStyleCentury;
+    wxCheckBox* m_chkStyleAllowNone;
 
     // the text entries for command parameters
     wxTextCtrl *m_textLabel;
 
     // the text entries for command parameters
     wxTextCtrl *m_textLabel;
@@ -113,6 +121,9 @@ private:
 BEGIN_EVENT_TABLE(DatePickerWidgetsPage, WidgetsPage)
     EVT_BUTTON(DatePickerPage_Reset, DatePickerWidgetsPage::OnButtonReset)
     EVT_BUTTON(DatePickerPage_Set, DatePickerWidgetsPage::OnButtonSet)
 BEGIN_EVENT_TABLE(DatePickerWidgetsPage, WidgetsPage)
     EVT_BUTTON(DatePickerPage_Reset, DatePickerWidgetsPage::OnButtonReset)
     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()
 
 // ============================================================================
@@ -133,26 +144,66 @@ DatePickerWidgetsPage::DatePickerWidgetsPage(WidgetsBookCtrl *book,
                                          wxImageList *imaglist)
                       :WidgetsPage(book, imaglist, datepick_xpm)
 {
                                          wxImageList *imaglist)
                       :WidgetsPage(book, imaglist, datepick_xpm)
 {
-    wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
-
-    // left pane
-    wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("Date details"));
-
-    wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
-
-    sizerLeft->Add( CreateSizerWithTextAndLabel( wxT("&Day:"), DatePickerPage_Day , &m_day ),
-                    0, wxALL | wxALIGN_RIGHT , 5 );
-
-    sizerLeft->Add( CreateSizerWithTextAndLabel( wxT("&Month:"), DatePickerPage_Month , &m_month ),
-                    0, wxALL | wxALIGN_RIGHT , 5 );
-
-    sizerLeft->Add( CreateSizerWithTextAndLabel( wxT("&Year:"), DatePickerPage_Year , &m_year ),
-                    0, wxALL | wxALIGN_RIGHT , 5 );
+}
 
 
-    sizerLeft->Add( new wxButton( this, wxID_ANY, wxT("&Set date") ),
-                    0, wxALL , 5 );
+void DatePickerWidgetsPage::CreateContent()
+{
+    wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
 
 
-    // right pane
+    // left pane: style
+    wxSizer* const sizerLeft = new wxBoxSizer(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());
+
+    wxSizer* const sizerStyle = new wxStaticBoxSizer(wxVERTICAL, this, "&Style");
+    m_chkStyleCentury = CreateCheckBoxAndAddToSizer(sizerStyle, "Show &century");
+    m_chkStyleAllowNone = CreateCheckBoxAndAddToSizer(sizerStyle, "Allow &no value");
+
+    sizerLeft->Add(sizerStyle, wxSizerFlags().Expand().Border());
+
+    sizerLeft->Add(new wxButton(this, DatePickerPage_Reset, "&Recreate"),
+                   wxSizerFlags().Centre().Border());
+
+
+    // 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);
 
     m_datePicker = new wxDatePickerCtrl(this, DatePickerPage_Picker);
     wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
 
     m_datePicker = new wxDatePickerCtrl(this, DatePickerPage_Picker);
@@ -160,19 +211,18 @@ DatePickerWidgetsPage::DatePickerWidgetsPage(WidgetsBookCtrl *book,
     sizerRight->Add(0, 0, 1, wxCENTRE);
     sizerRight->Add(m_datePicker, 1, wxCENTRE);
     sizerRight->Add(0, 0, 1, wxCENTRE);
     sizerRight->Add(0, 0, 1, wxCENTRE);
     sizerRight->Add(m_datePicker, 1, wxCENTRE);
     sizerRight->Add(0, 0, 1, wxCENTRE);
-    sizerRight->SetMinSize(150, 0);
     m_sizerDatePicker = sizerRight; // save it to modify it later
 
     // the 3 panes panes compose the window
     sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
     m_sizerDatePicker = sizerRight; // save it to modify it later
 
     // the 3 panes panes compose the window
     sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
+    sizerTop->Add(sizerMiddle, 0, (wxTOP | wxBOTTOM), 10);
     sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
 
     // final initializations
     sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
 
     // final initializations
+    m_chkStyleCentury->SetValue(true);
     Reset();
 
     SetSizer(sizerTop);
     Reset();
 
     SetSizer(sizerTop);
-
-    sizerTop->Fit(this);
 }
 
 void DatePickerWidgetsPage::Reset()
 }
 
 void DatePickerWidgetsPage::Reset()
@@ -180,9 +230,7 @@ void DatePickerWidgetsPage::Reset()
     const wxDateTime today = wxDateTime::Today();
 
     m_datePicker->SetValue(today);
     const wxDateTime today = wxDateTime::Today();
 
     m_datePicker->SetValue(today);
-    m_day->SetValue(wxString::Format(_T("%d"), today.GetDay()));
-    m_month->SetValue(wxString::Format(_T("%d"), today.GetMonth()));
-    m_year->SetValue(wxString::Format(_T("%d"), today.GetYear()));
+    m_textCur->SetValue(today.FormatISODate());
 }
 
 void DatePickerWidgetsPage::CreateDatePicker()
 }
 
 void DatePickerWidgetsPage::CreateDatePicker()
@@ -197,7 +245,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(m_datePicker, 1, wxCENTRE);
 
     m_sizerDatePicker->Add(0, 0, 1, wxCENTRE);
     m_sizerDatePicker->Add(m_datePicker, 1, wxCENTRE);
@@ -216,8 +287,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))
 {
+    wxDateTime dt;
+    if ( GetDateFromTextControl(dt, m_textCur) )
+        m_datePicker->SetValue(dt);
+}
+
+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) )
+    {
+        wxLogMessage("No range set");
+    }
+    else
+    {
+        m_textMin->SetValue(dt1.IsValid() ? dt1.FormatISODate() : wxString());
+        m_textMax->SetValue(dt2.IsValid() ? dt2.FormatISODate() : wxString());
+
+        wxLogMessage("Date picker range updated");
+    }
+}
+
+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