timepick.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Program: wxWidgets Widgets Sample
  3. // Name: timepick.cpp
  4. // Purpose: Part of the widgets sample showing time picker
  5. // Author: Vadim Zeitlin
  6. // Created: 2011-12-20
  7. // Copyright: (c) 2011 wxWindows team
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // for compilers that support precompilation, includes "wx/wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. #if wxUSE_TIMEPICKCTRL
  22. // for all others, include the necessary headers
  23. #ifndef WX_PRECOMP
  24. #include "wx/crt.h"
  25. #include "wx/app.h"
  26. #include "wx/log.h"
  27. #include "wx/button.h"
  28. #include "wx/textctrl.h"
  29. #include "wx/sizer.h"
  30. #endif
  31. #include "wx/timectrl.h"
  32. #include "wx/dateevt.h"
  33. #include "widgets.h"
  34. #include "icons/timepick.xpm"
  35. // ----------------------------------------------------------------------------
  36. // constants
  37. // ----------------------------------------------------------------------------
  38. // control ids
  39. enum
  40. {
  41. TimePickerPage_Reset = wxID_HIGHEST,
  42. TimePickerPage_Set,
  43. TimePickerPage_Picker
  44. };
  45. // ----------------------------------------------------------------------------
  46. // CheckBoxWidgetsPage
  47. // ----------------------------------------------------------------------------
  48. class TimePickerWidgetsPage : public WidgetsPage
  49. {
  50. public:
  51. TimePickerWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
  52. virtual ~TimePickerWidgetsPage(){};
  53. virtual wxControl *GetWidget() const { return m_timePicker; }
  54. virtual void RecreateWidget() { CreateTimePicker(); }
  55. // lazy creation of the content
  56. virtual void CreateContent();
  57. protected:
  58. // event handlers
  59. void OnTimeChanged(wxDateEvent& event);
  60. void OnButtonSet(wxCommandEvent& event);
  61. void OnButtonReset(wxCommandEvent& event);
  62. // reset the time picker parameters
  63. void Reset();
  64. // (re)create the time picker
  65. void CreateTimePicker();
  66. // the controls
  67. // ------------
  68. // the checkbox itself and the sizer it is in
  69. wxTimePickerCtrl *m_timePicker;
  70. wxSizer *m_sizerTimePicker;
  71. wxTextCtrl *m_textCur;
  72. private:
  73. wxDECLARE_EVENT_TABLE();
  74. DECLARE_WIDGETS_PAGE(TimePickerWidgetsPage)
  75. };
  76. // ----------------------------------------------------------------------------
  77. // event tables
  78. // ----------------------------------------------------------------------------
  79. wxBEGIN_EVENT_TABLE(TimePickerWidgetsPage, WidgetsPage)
  80. EVT_BUTTON(TimePickerPage_Reset, TimePickerWidgetsPage::OnButtonReset)
  81. EVT_BUTTON(TimePickerPage_Set, TimePickerWidgetsPage::OnButtonSet)
  82. EVT_TIME_CHANGED(wxID_ANY, TimePickerWidgetsPage::OnTimeChanged)
  83. wxEND_EVENT_TABLE()
  84. // ============================================================================
  85. // implementation
  86. // ============================================================================
  87. #if defined(__WXMSW__)
  88. #define FAMILY_CTRLS NATIVE_CTRLS
  89. #else
  90. #define FAMILY_CTRLS GENERIC_CTRLS
  91. #endif
  92. IMPLEMENT_WIDGETS_PAGE(TimePickerWidgetsPage, wxT("TimePicker"),
  93. FAMILY_CTRLS | PICKER_CTRLS
  94. );
  95. TimePickerWidgetsPage::TimePickerWidgetsPage(WidgetsBookCtrl *book,
  96. wxImageList *imaglist)
  97. : WidgetsPage(book, imaglist, timepick_xpm)
  98. {
  99. }
  100. void TimePickerWidgetsPage::CreateContent()
  101. {
  102. wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
  103. // left pane
  104. wxSizer* const sizerLeft = new wxBoxSizer(wxVERTICAL);
  105. sizerLeft->Add(new wxButton(this, TimePickerPage_Reset, "&Reset"),
  106. wxSizerFlags().Centre().Border());
  107. // middle pane: operations
  108. wxSizer* const sizerMiddle = new wxBoxSizer(wxVERTICAL);
  109. sizerMiddle->Add(CreateSizerWithTextAndButton
  110. (
  111. TimePickerPage_Set,
  112. "&Set time",
  113. wxID_ANY,
  114. &m_textCur
  115. ),
  116. wxSizerFlags().Expand().Border());
  117. m_textCur->SetMinSize(wxSize(GetTextExtent(" 99:99:99 ").x, -1));
  118. // right pane: control itself
  119. wxSizer *sizerRight = new wxBoxSizer(wxHORIZONTAL);
  120. m_timePicker = new wxTimePickerCtrl(this, TimePickerPage_Picker);
  121. sizerRight->Add(0, 0, 1, wxCENTRE);
  122. sizerRight->Add(m_timePicker, 1, wxCENTRE);
  123. sizerRight->Add(0, 0, 1, wxCENTRE);
  124. m_sizerTimePicker = sizerRight; // save it to modify it later
  125. // the 3 panes panes compose the window
  126. sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
  127. sizerTop->Add(sizerMiddle, 0, (wxTOP | wxBOTTOM), 10);
  128. sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
  129. // final initializations
  130. Reset();
  131. SetSizer(sizerTop);
  132. }
  133. void TimePickerWidgetsPage::Reset()
  134. {
  135. const wxDateTime today = wxDateTime::Today();
  136. m_timePicker->SetValue(today);
  137. m_textCur->SetValue(today.FormatISOTime());
  138. }
  139. void TimePickerWidgetsPage::CreateTimePicker()
  140. {
  141. const wxDateTime value = m_timePicker->GetValue();
  142. size_t count = m_sizerTimePicker->GetChildren().GetCount();
  143. for ( size_t n = 0; n < count; n++ )
  144. {
  145. m_sizerTimePicker->Remove(0);
  146. }
  147. delete m_timePicker;
  148. m_timePicker = new wxTimePickerCtrl(this, TimePickerPage_Picker, value);
  149. m_sizerTimePicker->Add(0, 0, 1, wxCENTRE);
  150. m_sizerTimePicker->Add(m_timePicker, 1, wxCENTRE);
  151. m_sizerTimePicker->Add(0, 0, 1, wxCENTRE);
  152. m_sizerTimePicker->Layout();
  153. }
  154. // ----------------------------------------------------------------------------
  155. // event handlers
  156. // ----------------------------------------------------------------------------
  157. void TimePickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
  158. {
  159. Reset();
  160. CreateTimePicker();
  161. }
  162. void TimePickerWidgetsPage::OnButtonSet(wxCommandEvent& WXUNUSED(event))
  163. {
  164. int h, m, s;
  165. if ( wxSscanf(m_textCur->GetValue(), "%d:%d:%d", &h, &m, &s) != 3 )
  166. {
  167. wxLogError("Invalid time, please use HH:MM:SS format.");
  168. return;
  169. }
  170. m_timePicker->SetTime(h, m, s);
  171. }
  172. void TimePickerWidgetsPage::OnTimeChanged(wxDateEvent& event)
  173. {
  174. int h, m, s;
  175. m_timePicker->GetTime(&h, &m, &s);
  176. wxLogMessage("Time changed, now is %s (control value is %02d:%02d:%02d).",
  177. event.GetDate().FormatISOTime(), h, m, s);
  178. }
  179. #endif // wxUSE_TIMEPICKCTRL