]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/clrpicker.cpp
Make storing non-trivial data in wxThreadSpecificInfo possible.
[wxWidgets.git] / samples / widgets / clrpicker.cpp
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 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_COLOURPICKERCTRL
27
28 // for all others, include the necessary headers
29 #ifndef WX_PRECOMP
30 #include "wx/app.h"
31 #include "wx/log.h"
32 #include "wx/radiobox.h"
33 #endif
34
35 #include "wx/artprov.h"
36 #include "wx/sizer.h"
37 #include "wx/stattext.h"
38 #include "wx/checkbox.h"
39 #include "wx/imaglist.h"
40
41 #include "wx/clrpicker.h"
42 #include "widgets.h"
43
44 #include "icons/clrpicker.xpm"
45
46 // ----------------------------------------------------------------------------
47 // constants
48 // ----------------------------------------------------------------------------
49
50 // control ids
51 enum
52 {
53 PickerPage_Reset = wxID_HIGHEST,
54 PickerPage_Colour
55 };
56
57
58 // ----------------------------------------------------------------------------
59 // ColourPickerWidgetsPage
60 // ----------------------------------------------------------------------------
61
62 class ColourPickerWidgetsPage : public WidgetsPage
63 {
64 public:
65 ColourPickerWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
66 virtual ~ColourPickerWidgetsPage(){};
67
68 virtual wxControl *GetWidget() const { return m_clrPicker; }
69 virtual void RecreateWidget() { RecreatePicker(); }
70
71 // lazy creation of the content
72 virtual void CreateContent();
73
74 protected:
75
76 // called only once at first construction
77 void CreatePicker();
78
79 // called to recreate an existing control
80 void RecreatePicker();
81
82 // restore the checkboxes state to the initial values
83 void Reset();
84
85 // get the initial style for the picker of the given kind
86 long GetPickerStyle();
87
88
89 void OnColourChange(wxColourPickerEvent &ev);
90 void OnCheckBox(wxCommandEvent &ev);
91 void OnButtonReset(wxCommandEvent &ev);
92
93 // the picker
94 wxColourPickerCtrl *m_clrPicker;
95
96
97 // other controls
98 // --------------
99
100 wxCheckBox *m_chkColourTextCtrl,
101 *m_chkColourShowLabel;
102 wxBoxSizer *m_sizer;
103
104 private:
105 DECLARE_EVENT_TABLE()
106 DECLARE_WIDGETS_PAGE(ColourPickerWidgetsPage)
107 };
108
109 // ----------------------------------------------------------------------------
110 // event tables
111 // ----------------------------------------------------------------------------
112
113 BEGIN_EVENT_TABLE(ColourPickerWidgetsPage, WidgetsPage)
114 EVT_BUTTON(PickerPage_Reset, ColourPickerWidgetsPage::OnButtonReset)
115
116 EVT_COLOURPICKER_CHANGED(PickerPage_Colour, ColourPickerWidgetsPage::OnColourChange)
117
118 EVT_CHECKBOX(wxID_ANY, ColourPickerWidgetsPage::OnCheckBox)
119 END_EVENT_TABLE()
120
121 // ============================================================================
122 // implementation
123 // ============================================================================
124
125 #if defined(__WXGTK24__)
126 #define FAMILY_CTRLS NATIVE_CTRLS
127 #else
128 #define FAMILY_CTRLS GENERIC_CTRLS
129 #endif
130
131 IMPLEMENT_WIDGETS_PAGE(ColourPickerWidgetsPage, wxT("ColourPicker"),
132 PICKER_CTRLS | FAMILY_CTRLS);
133
134 ColourPickerWidgetsPage::ColourPickerWidgetsPage(WidgetsBookCtrl *book,
135 wxImageList *imaglist)
136 : WidgetsPage(book, imaglist, clrpicker_xpm)
137 {
138 }
139
140 void ColourPickerWidgetsPage::CreateContent()
141 {
142 // left pane
143 wxSizer *boxleft = new wxBoxSizer(wxVERTICAL);
144
145 wxStaticBoxSizer *clrbox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&ColourPicker style"));
146 m_chkColourTextCtrl = CreateCheckBoxAndAddToSizer(clrbox, wxT("With textctrl"));
147 m_chkColourShowLabel = CreateCheckBoxAndAddToSizer(clrbox, wxT("With label"));
148 boxleft->Add(clrbox, 0, wxALL|wxGROW, 5);
149
150 boxleft->Add(new wxButton(this, PickerPage_Reset, wxT("&Reset")),
151 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
152
153 Reset(); // set checkboxes state
154
155 // create pickers
156 m_clrPicker = NULL;
157 CreatePicker();
158
159 // right pane
160 m_sizer = new wxBoxSizer(wxVERTICAL);
161 m_sizer->Add(1, 1, 1, wxGROW | wxALL, 5); // spacer
162 m_sizer->Add(m_clrPicker, 0, wxALIGN_CENTER|wxALL, 5);
163 m_sizer->Add(1, 1, 1, wxGROW | wxALL, 5); // spacer
164
165 // global pane
166 wxSizer *sz = new wxBoxSizer(wxHORIZONTAL);
167 sz->Add(boxleft, 0, wxGROW|wxALL, 5);
168 sz->Add(m_sizer, 1, wxGROW|wxALL, 5);
169
170 SetSizer(sz);
171 }
172
173 void ColourPickerWidgetsPage::CreatePicker()
174 {
175 delete m_clrPicker;
176
177 m_clrPicker = new wxColourPickerCtrl(this, PickerPage_Colour, *wxRED,
178 wxDefaultPosition, wxDefaultSize,
179 GetPickerStyle());
180 }
181
182 long ColourPickerWidgetsPage::GetPickerStyle()
183 {
184 long style = 0;
185
186 if ( m_chkColourTextCtrl->GetValue() )
187 style |= wxCLRP_USE_TEXTCTRL;
188
189 if ( m_chkColourShowLabel->GetValue() )
190 style |= wxCLRP_SHOW_LABEL;
191
192 return style;
193 }
194
195 void ColourPickerWidgetsPage::RecreatePicker()
196 {
197 m_sizer->Remove(1);
198 CreatePicker();
199 m_sizer->Insert(1, m_clrPicker, 0, wxALIGN_CENTER|wxALL, 5);
200
201 m_sizer->Layout();
202 }
203
204 void ColourPickerWidgetsPage::Reset()
205 {
206 m_chkColourTextCtrl->SetValue((wxCLRP_DEFAULT_STYLE & wxCLRP_USE_TEXTCTRL) != 0);
207 m_chkColourShowLabel->SetValue((wxCLRP_DEFAULT_STYLE & wxCLRP_SHOW_LABEL) != 0);
208 }
209
210
211 // ----------------------------------------------------------------------------
212 // event handlers
213 // ----------------------------------------------------------------------------
214
215 void ColourPickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
216 {
217 Reset();
218 RecreatePicker();
219 }
220
221 void ColourPickerWidgetsPage::OnColourChange(wxColourPickerEvent& event)
222 {
223 wxLogMessage(wxT("The colour changed to '%s' !"),
224 event.GetColour().GetAsString(wxC2S_CSS_SYNTAX).c_str());
225 }
226
227 void ColourPickerWidgetsPage::OnCheckBox(wxCommandEvent &event)
228 {
229 if (event.GetEventObject() == m_chkColourTextCtrl ||
230 event.GetEventObject() == m_chkColourShowLabel)
231 RecreatePicker();
232 }
233
234 #endif // wxUSE_COLOURPICKERCTRL