clrpicker.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Program: wxWidgets Widgets Sample
  3. // Name: clrpicker.cpp
  4. // Purpose: Shows wxColourPickerCtrl
  5. // Author: Francesco Montorsi
  6. // Created: 20/6/2006
  7. // Copyright: (c) 2006 Francesco Montorsi
  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_COLOURPICKERCTRL
  22. // for all others, include the necessary headers
  23. #ifndef WX_PRECOMP
  24. #include "wx/app.h"
  25. #include "wx/log.h"
  26. #include "wx/radiobox.h"
  27. #endif
  28. #include "wx/artprov.h"
  29. #include "wx/sizer.h"
  30. #include "wx/stattext.h"
  31. #include "wx/checkbox.h"
  32. #include "wx/imaglist.h"
  33. #include "wx/clrpicker.h"
  34. #include "widgets.h"
  35. #include "icons/clrpicker.xpm"
  36. // ----------------------------------------------------------------------------
  37. // constants
  38. // ----------------------------------------------------------------------------
  39. // control ids
  40. enum
  41. {
  42. PickerPage_Reset = wxID_HIGHEST,
  43. PickerPage_Colour
  44. };
  45. // ----------------------------------------------------------------------------
  46. // ColourPickerWidgetsPage
  47. // ----------------------------------------------------------------------------
  48. class ColourPickerWidgetsPage : public WidgetsPage
  49. {
  50. public:
  51. ColourPickerWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
  52. virtual ~ColourPickerWidgetsPage(){};
  53. virtual wxControl *GetWidget() const { return m_clrPicker; }
  54. virtual void RecreateWidget() { RecreatePicker(); }
  55. // lazy creation of the content
  56. virtual void CreateContent();
  57. protected:
  58. // called only once at first construction
  59. void CreatePicker();
  60. // called to recreate an existing control
  61. void RecreatePicker();
  62. // restore the checkboxes state to the initial values
  63. void Reset();
  64. // get the initial style for the picker of the given kind
  65. long GetPickerStyle();
  66. void OnColourChange(wxColourPickerEvent &ev);
  67. void OnCheckBox(wxCommandEvent &ev);
  68. void OnButtonReset(wxCommandEvent &ev);
  69. // the picker
  70. wxColourPickerCtrl *m_clrPicker;
  71. // other controls
  72. // --------------
  73. wxCheckBox *m_chkColourTextCtrl,
  74. *m_chkColourShowLabel;
  75. wxBoxSizer *m_sizer;
  76. private:
  77. wxDECLARE_EVENT_TABLE();
  78. DECLARE_WIDGETS_PAGE(ColourPickerWidgetsPage)
  79. };
  80. // ----------------------------------------------------------------------------
  81. // event tables
  82. // ----------------------------------------------------------------------------
  83. wxBEGIN_EVENT_TABLE(ColourPickerWidgetsPage, WidgetsPage)
  84. EVT_BUTTON(PickerPage_Reset, ColourPickerWidgetsPage::OnButtonReset)
  85. EVT_COLOURPICKER_CHANGED(PickerPage_Colour, ColourPickerWidgetsPage::OnColourChange)
  86. EVT_CHECKBOX(wxID_ANY, ColourPickerWidgetsPage::OnCheckBox)
  87. wxEND_EVENT_TABLE()
  88. // ============================================================================
  89. // implementation
  90. // ============================================================================
  91. #if defined(__WXGTK24__)
  92. #define FAMILY_CTRLS NATIVE_CTRLS
  93. #else
  94. #define FAMILY_CTRLS GENERIC_CTRLS
  95. #endif
  96. IMPLEMENT_WIDGETS_PAGE(ColourPickerWidgetsPage, wxT("ColourPicker"),
  97. PICKER_CTRLS | FAMILY_CTRLS);
  98. ColourPickerWidgetsPage::ColourPickerWidgetsPage(WidgetsBookCtrl *book,
  99. wxImageList *imaglist)
  100. : WidgetsPage(book, imaglist, clrpicker_xpm)
  101. {
  102. }
  103. void ColourPickerWidgetsPage::CreateContent()
  104. {
  105. // left pane
  106. wxSizer *boxleft = new wxBoxSizer(wxVERTICAL);
  107. wxStaticBoxSizer *clrbox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&ColourPicker style"));
  108. m_chkColourTextCtrl = CreateCheckBoxAndAddToSizer(clrbox, wxT("With textctrl"));
  109. m_chkColourShowLabel = CreateCheckBoxAndAddToSizer(clrbox, wxT("With label"));
  110. boxleft->Add(clrbox, 0, wxALL|wxGROW, 5);
  111. boxleft->Add(new wxButton(this, PickerPage_Reset, wxT("&Reset")),
  112. 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
  113. Reset(); // set checkboxes state
  114. // create pickers
  115. m_clrPicker = NULL;
  116. CreatePicker();
  117. // right pane
  118. m_sizer = new wxBoxSizer(wxVERTICAL);
  119. m_sizer->Add(1, 1, 1, wxGROW | wxALL, 5); // spacer
  120. m_sizer->Add(m_clrPicker, 0, wxALIGN_CENTER|wxALL, 5);
  121. m_sizer->Add(1, 1, 1, wxGROW | wxALL, 5); // spacer
  122. // global pane
  123. wxSizer *sz = new wxBoxSizer(wxHORIZONTAL);
  124. sz->Add(boxleft, 0, wxGROW|wxALL, 5);
  125. sz->Add(m_sizer, 1, wxGROW|wxALL, 5);
  126. SetSizer(sz);
  127. }
  128. void ColourPickerWidgetsPage::CreatePicker()
  129. {
  130. delete m_clrPicker;
  131. m_clrPicker = new wxColourPickerCtrl(this, PickerPage_Colour, *wxRED,
  132. wxDefaultPosition, wxDefaultSize,
  133. GetPickerStyle());
  134. }
  135. long ColourPickerWidgetsPage::GetPickerStyle()
  136. {
  137. long style = 0;
  138. if ( m_chkColourTextCtrl->GetValue() )
  139. style |= wxCLRP_USE_TEXTCTRL;
  140. if ( m_chkColourShowLabel->GetValue() )
  141. style |= wxCLRP_SHOW_LABEL;
  142. return style;
  143. }
  144. void ColourPickerWidgetsPage::RecreatePicker()
  145. {
  146. m_sizer->Remove(1);
  147. CreatePicker();
  148. m_sizer->Insert(1, m_clrPicker, 0, wxALIGN_CENTER|wxALL, 5);
  149. m_sizer->Layout();
  150. }
  151. void ColourPickerWidgetsPage::Reset()
  152. {
  153. m_chkColourTextCtrl->SetValue((wxCLRP_DEFAULT_STYLE & wxCLRP_USE_TEXTCTRL) != 0);
  154. m_chkColourShowLabel->SetValue((wxCLRP_DEFAULT_STYLE & wxCLRP_SHOW_LABEL) != 0);
  155. }
  156. // ----------------------------------------------------------------------------
  157. // event handlers
  158. // ----------------------------------------------------------------------------
  159. void ColourPickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
  160. {
  161. Reset();
  162. RecreatePicker();
  163. }
  164. void ColourPickerWidgetsPage::OnColourChange(wxColourPickerEvent& event)
  165. {
  166. wxLogMessage(wxT("The colour changed to '%s' !"),
  167. event.GetColour().GetAsString(wxC2S_CSS_SYNTAX).c_str());
  168. }
  169. void ColourPickerWidgetsPage::OnCheckBox(wxCommandEvent &event)
  170. {
  171. if (event.GetEventObject() == m_chkColourTextCtrl ||
  172. event.GetEventObject() == m_chkColourShowLabel)
  173. RecreatePicker();
  174. }
  175. #endif // wxUSE_COLOURPICKERCTRL