]>
Commit | Line | Data |
---|---|---|
32b8ec41 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Program: wxWindows Widgets Sample | |
3 | // Name: combobox.cpp | |
4 | // Purpose: Part of the widgets sample showing wxComboBox | |
5 | // Author: Vadim Zeitlin | |
6 | // Created: 27.03.01 | |
7 | // Id: $Id$ | |
8 | // Copyright: (c) 2001 Vadim Zeitlin | |
9 | // License: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // for compilers that support precompilation, includes "wx/wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | // for all others, include the necessary headers | |
28 | #ifndef WX_PRECOMP | |
29 | #include "wx/log.h" | |
30 | ||
31 | #include "wx/button.h" | |
32 | #include "wx/checkbox.h" | |
33 | #include "wx/combobox.h" | |
34 | #include "wx/radiobox.h" | |
35 | #include "wx/statbox.h" | |
36 | #include "wx/textctrl.h" | |
37 | #endif | |
38 | ||
39 | #include "wx/sizer.h" | |
40 | ||
41 | #include "widgets.h" | |
42 | ||
43 | #include "icons/combobox.xpm" | |
44 | ||
45 | // ---------------------------------------------------------------------------- | |
46 | // constants | |
47 | // ---------------------------------------------------------------------------- | |
48 | ||
49 | // control ids | |
50 | enum | |
51 | { | |
52 | ComboPage_Reset = 100, | |
53 | ComboPage_CurText, | |
54 | ComboPage_Add, | |
55 | ComboPage_AddText, | |
56 | ComboPage_AddSeveral, | |
57 | ComboPage_AddMany, | |
58 | ComboPage_Clear, | |
59 | ComboPage_Change, | |
60 | ComboPage_ChangeText, | |
61 | ComboPage_Delete, | |
62 | ComboPage_DeleteText, | |
63 | ComboPage_DeleteSel, | |
64 | ComboPage_Combo | |
65 | }; | |
66 | ||
67 | // kinds of comboboxes | |
68 | enum | |
69 | { | |
70 | ComboKind_Default, | |
71 | ComboKind_Simple, | |
72 | ComboKind_DropDown | |
73 | }; | |
74 | ||
75 | // ---------------------------------------------------------------------------- | |
76 | // ComboboxWidgetsPage | |
77 | // ---------------------------------------------------------------------------- | |
78 | ||
79 | class ComboboxWidgetsPage : public WidgetsPage | |
80 | { | |
81 | public: | |
82 | ComboboxWidgetsPage(wxNotebook *notebook, wxImageList *imaglist); | |
83 | ||
84 | protected: | |
85 | // event handlers | |
86 | void OnButtonReset(wxCommandEvent& event); | |
87 | void OnButtonChange(wxCommandEvent& event); | |
88 | void OnButtonDelete(wxCommandEvent& event); | |
89 | void OnButtonDeleteSel(wxCommandEvent& event); | |
90 | void OnButtonClear(wxCommandEvent& event); | |
91 | void OnButtonAdd(wxCommandEvent& event); | |
92 | void OnButtonAddSeveral(wxCommandEvent& event); | |
93 | void OnButtonAddMany(wxCommandEvent& event); | |
94 | ||
95 | void OnComboBox(wxCommandEvent& event); | |
96 | void OnComboText(wxCommandEvent& event); | |
97 | ||
98 | void OnCheckOrRadioBox(wxCommandEvent& event); | |
99 | ||
100 | void OnUpdateUICurText(wxUpdateUIEvent& event); | |
101 | ||
102 | void OnUpdateUIAddSeveral(wxUpdateUIEvent& event); | |
103 | void OnUpdateUIClearButton(wxUpdateUIEvent& event); | |
104 | void OnUpdateUIDeleteButton(wxUpdateUIEvent& event); | |
105 | void OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event); | |
106 | void OnUpdateUIResetButton(wxUpdateUIEvent& event); | |
107 | ||
108 | // reset the combobox parameters | |
109 | void Reset(); | |
110 | ||
111 | // (re)create the combobox | |
112 | void CreateCombo(); | |
113 | ||
114 | // the controls | |
115 | // ------------ | |
116 | ||
117 | // the sel mode radiobox | |
118 | wxRadioBox *m_radioKind; | |
119 | ||
120 | // the checkboxes for styles | |
121 | wxCheckBox *m_chkSort, | |
122 | *m_chkReadonly; | |
123 | ||
124 | // the combobox itself and the sizer it is in | |
125 | wxComboBox *m_combobox; | |
126 | wxSizer *m_sizerCombo; | |
127 | ||
128 | // the text entries for "Add/change string" and "Delete" buttons | |
129 | wxTextCtrl *m_textAdd, | |
130 | *m_textChange, | |
131 | *m_textDelete; | |
132 | ||
133 | private: | |
134 | DECLARE_EVENT_TABLE(); | |
135 | DECLARE_WIDGETS_PAGE(ComboboxWidgetsPage); | |
136 | }; | |
137 | ||
138 | // ---------------------------------------------------------------------------- | |
139 | // event tables | |
140 | // ---------------------------------------------------------------------------- | |
141 | ||
142 | BEGIN_EVENT_TABLE(ComboboxWidgetsPage, WidgetsPage) | |
143 | EVT_BUTTON(ComboPage_Reset, ComboboxWidgetsPage::OnButtonReset) | |
144 | EVT_BUTTON(ComboPage_Change, ComboboxWidgetsPage::OnButtonChange) | |
145 | EVT_BUTTON(ComboPage_Delete, ComboboxWidgetsPage::OnButtonDelete) | |
146 | EVT_BUTTON(ComboPage_DeleteSel, ComboboxWidgetsPage::OnButtonDeleteSel) | |
147 | EVT_BUTTON(ComboPage_Clear, ComboboxWidgetsPage::OnButtonClear) | |
148 | EVT_BUTTON(ComboPage_Add, ComboboxWidgetsPage::OnButtonAdd) | |
149 | EVT_BUTTON(ComboPage_AddSeveral, ComboboxWidgetsPage::OnButtonAddSeveral) | |
150 | EVT_BUTTON(ComboPage_AddMany, ComboboxWidgetsPage::OnButtonAddMany) | |
151 | ||
152 | EVT_TEXT_ENTER(ComboPage_AddText, ComboboxWidgetsPage::OnButtonAdd) | |
153 | EVT_TEXT_ENTER(ComboPage_DeleteText, ComboboxWidgetsPage::OnButtonDelete) | |
154 | ||
155 | EVT_UPDATE_UI(ComboPage_CurText, ComboboxWidgetsPage::OnUpdateUICurText) | |
156 | ||
157 | EVT_UPDATE_UI(ComboPage_Reset, ComboboxWidgetsPage::OnUpdateUIResetButton) | |
158 | EVT_UPDATE_UI(ComboPage_AddSeveral, ComboboxWidgetsPage::OnUpdateUIAddSeveral) | |
159 | EVT_UPDATE_UI(ComboPage_Clear, ComboboxWidgetsPage::OnUpdateUIClearButton) | |
160 | EVT_UPDATE_UI(ComboPage_DeleteText, ComboboxWidgetsPage::OnUpdateUIClearButton) | |
161 | EVT_UPDATE_UI(ComboPage_Delete, ComboboxWidgetsPage::OnUpdateUIDeleteButton) | |
162 | EVT_UPDATE_UI(ComboPage_Change, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton) | |
163 | EVT_UPDATE_UI(ComboPage_ChangeText, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton) | |
164 | EVT_UPDATE_UI(ComboPage_DeleteSel, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton) | |
165 | ||
166 | EVT_COMBOBOX(ComboPage_Combo, ComboboxWidgetsPage::OnComboBox) | |
167 | EVT_TEXT(ComboPage_Combo, ComboboxWidgetsPage::OnComboText) | |
168 | ||
169 | EVT_CHECKBOX(-1, ComboboxWidgetsPage::OnCheckOrRadioBox) | |
170 | EVT_RADIOBOX(-1, ComboboxWidgetsPage::OnCheckOrRadioBox) | |
171 | END_EVENT_TABLE() | |
172 | ||
173 | // ============================================================================ | |
174 | // implementation | |
175 | // ============================================================================ | |
176 | ||
177 | IMPLEMENT_WIDGETS_PAGE(ComboboxWidgetsPage, _T("Combobox")); | |
178 | ||
179 | ComboboxWidgetsPage::ComboboxWidgetsPage(wxNotebook *notebook, | |
180 | wxImageList *imaglist) | |
181 | : WidgetsPage(notebook) | |
182 | { | |
183 | imaglist->Add(wxBitmap(combobox_xpm)); | |
184 | ||
185 | // init everything | |
186 | m_chkSort = | |
187 | m_chkReadonly = (wxCheckBox *)NULL; | |
188 | ||
189 | m_combobox = (wxComboBox *)NULL; | |
190 | m_sizerCombo = (wxSizer *)NULL; | |
191 | ||
192 | /* | |
193 | What we create here is a frame having 3 panes: style pane is the | |
194 | leftmost one, in the middle the pane with buttons allowing to perform | |
195 | miscellaneous combobox operations and the pane containing the combobox | |
196 | itself to the right | |
197 | */ | |
198 | wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL); | |
199 | ||
200 | // left pane | |
201 | wxStaticBox *box = new wxStaticBox(this, -1, _T("&Set style")); | |
202 | ||
203 | // should be in sync with ComboKind_XXX values | |
204 | static const wxString kinds[] = | |
205 | { | |
206 | _T("default"), | |
207 | _T("simple"), | |
208 | _T("drop down"), | |
209 | }; | |
210 | ||
211 | m_radioKind = new wxRadioBox(this, -1, _T("Combobox &kind:"), | |
212 | wxDefaultPosition, wxDefaultSize, | |
213 | WXSIZEOF(kinds), kinds, | |
214 | 1, wxRA_SPECIFY_COLS); | |
215 | ||
216 | wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL); | |
217 | ||
218 | m_chkSort = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Sort items")); | |
219 | m_chkReadonly = CreateCheckBoxAndAddToSizer(sizerLeft, _T("&Read only")); | |
220 | ||
221 | sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer | |
222 | sizerLeft->Add(m_radioKind, 0, wxGROW | wxALL, 5); | |
223 | ||
224 | wxButton *btn = new wxButton(this, ComboPage_Reset, _T("&Reset")); | |
225 | sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15); | |
226 | ||
227 | // middle pane | |
228 | wxStaticBox *box2 = new wxStaticBox(this, -1, _T("&Change combobox contents")); | |
229 | wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL); | |
230 | ||
231 | wxSizer *sizerRow; | |
232 | ||
233 | wxTextCtrl *text; | |
234 | sizerRow = CreateSizerWithTextAndLabel(_T("Current selection"), | |
235 | ComboPage_CurText, | |
236 | &text); | |
237 | text->SetEditable(FALSE); | |
238 | ||
239 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
240 | ||
241 | sizerRow = CreateSizerWithTextAndButton(ComboPage_Add, | |
242 | _T("&Add this string"), | |
243 | ComboPage_AddText, | |
244 | &m_textAdd); | |
245 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
246 | ||
247 | btn = new wxButton(this, ComboPage_AddSeveral, _T("&Insert a few strings")); | |
248 | sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); | |
249 | ||
250 | btn = new wxButton(this, ComboPage_AddMany, _T("Add &many strings")); | |
251 | sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); | |
252 | ||
253 | sizerRow = CreateSizerWithTextAndButton(ComboPage_Change, | |
254 | _T("C&hange current"), | |
255 | ComboPage_ChangeText, | |
256 | &m_textChange); | |
257 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
258 | ||
259 | sizerRow = CreateSizerWithTextAndButton(ComboPage_Delete, | |
260 | _T("&Delete this item"), | |
261 | ComboPage_DeleteText, | |
262 | &m_textDelete); | |
263 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
264 | ||
265 | btn = new wxButton(this, ComboPage_DeleteSel, _T("Delete &selection")); | |
266 | sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); | |
267 | ||
268 | btn = new wxButton(this, ComboPage_Clear, _T("&Clear")); | |
269 | sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); | |
270 | ||
271 | // right pane | |
272 | wxSizer *sizerRight = new wxBoxSizer(wxVERTICAL); | |
273 | m_combobox = new wxComboBox(this, ComboPage_Combo, _T(""), | |
274 | wxDefaultPosition, wxDefaultSize, | |
275 | 0, NULL, | |
276 | 0); | |
277 | sizerRight->Add(m_combobox, 1, wxGROW | wxALL, 5); | |
278 | sizerRight->SetMinSize(250, 0); | |
279 | m_sizerCombo = sizerRight; // save it to modify it later | |
280 | ||
281 | // the 3 panes panes compose the window | |
282 | sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10); | |
283 | sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10); | |
284 | sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10); | |
285 | ||
286 | // final initializations | |
287 | Reset(); | |
288 | ||
289 | SetAutoLayout(TRUE); | |
290 | SetSizer(sizerTop); | |
291 | ||
292 | sizerTop->Fit(this); | |
293 | } | |
294 | ||
295 | // ---------------------------------------------------------------------------- | |
296 | // operations | |
297 | // ---------------------------------------------------------------------------- | |
298 | ||
299 | void ComboboxWidgetsPage::Reset() | |
300 | { | |
301 | m_chkSort->SetValue(FALSE); | |
302 | m_chkReadonly->SetValue(FALSE); | |
303 | } | |
304 | ||
305 | void ComboboxWidgetsPage::CreateCombo() | |
306 | { | |
307 | int flags = 0; | |
308 | ||
309 | if ( m_chkSort->GetValue() ) | |
310 | flags |= wxCB_SORT; | |
311 | if ( m_chkReadonly->GetValue() ) | |
312 | flags |= wxCB_READONLY; | |
313 | ||
314 | switch ( m_radioKind->GetSelection() ) | |
315 | { | |
316 | default: | |
317 | wxFAIL_MSG( _T("unknown combo kind") ); | |
318 | // fall through | |
319 | ||
320 | case ComboKind_Default: | |
321 | break; | |
322 | ||
323 | case ComboKind_Simple: | |
324 | flags |= wxCB_SIMPLE; | |
325 | break; | |
326 | ||
327 | case ComboKind_DropDown: | |
328 | flags = wxCB_DROPDOWN; | |
329 | break; | |
330 | } | |
331 | ||
332 | wxArrayString items; | |
333 | if ( m_combobox ) | |
334 | { | |
335 | int count = m_combobox->GetCount(); | |
336 | for ( int n = 0; n < count; n++ ) | |
337 | { | |
338 | items.Add(m_combobox->GetString(n)); | |
339 | } | |
340 | ||
341 | m_sizerCombo->Remove(m_combobox); | |
342 | delete m_combobox; | |
343 | } | |
344 | ||
345 | m_combobox = new wxComboBox(this, ComboPage_Combo, _T(""), | |
346 | wxDefaultPosition, wxDefaultSize, | |
347 | 0, NULL, | |
348 | flags); | |
349 | ||
350 | size_t count = items.GetCount(); | |
351 | for ( size_t n = 0; n < count; n++ ) | |
352 | { | |
353 | m_combobox->Append(items[n]); | |
354 | } | |
355 | ||
356 | m_sizerCombo->Add(m_combobox, 1, wxGROW | wxALL, 5); | |
357 | m_sizerCombo->Layout(); | |
358 | } | |
359 | ||
360 | // ---------------------------------------------------------------------------- | |
361 | // event handlers | |
362 | // ---------------------------------------------------------------------------- | |
363 | ||
364 | void ComboboxWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event)) | |
365 | { | |
366 | Reset(); | |
367 | ||
368 | CreateCombo(); | |
369 | } | |
370 | ||
371 | void ComboboxWidgetsPage::OnButtonChange(wxCommandEvent& WXUNUSED(event)) | |
372 | { | |
373 | int sel = m_combobox->GetSelection(); | |
374 | if ( sel != -1 ) | |
375 | { | |
376 | #ifndef __WXGTK__ | |
377 | m_combobox->SetString(sel, m_textChange->GetValue()); | |
378 | #else | |
379 | wxLogMessage(_T("Not implemented in wxGTK")); | |
380 | #endif | |
381 | } | |
382 | } | |
383 | ||
384 | void ComboboxWidgetsPage::OnButtonDelete(wxCommandEvent& WXUNUSED(event)) | |
385 | { | |
386 | unsigned long n; | |
387 | if ( !m_textDelete->GetValue().ToULong(&n) || | |
388 | (n >= (unsigned)m_combobox->GetCount()) ) | |
389 | { | |
390 | return; | |
391 | } | |
392 | ||
393 | m_combobox->Delete(n); | |
394 | } | |
395 | ||
396 | void ComboboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent& WXUNUSED(event)) | |
397 | { | |
398 | int sel = m_combobox->GetSelection(); | |
399 | if ( sel != -1 ) | |
400 | { | |
401 | m_combobox->Delete(sel); | |
402 | } | |
403 | } | |
404 | ||
405 | void ComboboxWidgetsPage::OnButtonClear(wxCommandEvent& event) | |
406 | { | |
407 | m_combobox->Clear(); | |
408 | } | |
409 | ||
410 | void ComboboxWidgetsPage::OnButtonAdd(wxCommandEvent& event) | |
411 | { | |
412 | static size_t s_item = 0; | |
413 | ||
414 | wxString s = m_textAdd->GetValue(); | |
415 | if ( !m_textAdd->IsModified() ) | |
416 | { | |
417 | // update the default string | |
418 | m_textAdd->SetValue(wxString::Format(_T("test item %u"), ++s_item)); | |
419 | } | |
420 | ||
421 | m_combobox->Append(s); | |
422 | } | |
423 | ||
424 | void ComboboxWidgetsPage::OnButtonAddMany(wxCommandEvent& WXUNUSED(event)) | |
425 | { | |
426 | // "many" means 1000 here | |
427 | for ( size_t n = 0; n < 1000; n++ ) | |
428 | { | |
429 | m_combobox->Append(wxString::Format(_T("item #%u"), n)); | |
430 | } | |
431 | } | |
432 | ||
433 | void ComboboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent& event) | |
434 | { | |
435 | m_combobox->Append(_T("First")); | |
436 | m_combobox->Append(_T("another one")); | |
437 | m_combobox->Append(_T("and the last (very very very very very very very very very very long) one")); | |
438 | } | |
439 | ||
440 | void ComboboxWidgetsPage::OnUpdateUICurText(wxUpdateUIEvent& event) | |
441 | { | |
442 | event.SetText( wxString::Format(_T("%d"), m_combobox->GetSelection()) ); | |
443 | } | |
444 | ||
445 | void ComboboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event) | |
446 | { | |
447 | event.Enable( m_chkSort->GetValue() || m_chkReadonly->GetValue() ); | |
448 | } | |
449 | ||
450 | void ComboboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent& event) | |
451 | { | |
452 | unsigned long n; | |
453 | event.Enable(m_textDelete->GetValue().ToULong(&n) && | |
454 | (n < (unsigned)m_combobox->GetCount())); | |
455 | } | |
456 | ||
457 | void ComboboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event) | |
458 | { | |
459 | event.Enable(m_combobox->GetSelection() != -1); | |
460 | } | |
461 | ||
462 | void ComboboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent& event) | |
463 | { | |
464 | event.Enable(m_combobox->GetCount() != 0); | |
465 | } | |
466 | ||
467 | void ComboboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent& event) | |
468 | { | |
469 | event.Enable(!(m_combobox->GetWindowStyle() & wxCB_SORT)); | |
470 | } | |
471 | ||
472 | void ComboboxWidgetsPage::OnComboText(wxCommandEvent& event) | |
473 | { | |
474 | wxString s = event.GetString(); | |
475 | ||
476 | wxASSERT_MSG( s == m_combobox->GetValue(), | |
477 | _T("event and combobox values should be the same") ); | |
478 | ||
479 | wxLogMessage(_T("Combobox text changed (now '%s')"), s.c_str()); | |
480 | } | |
481 | ||
482 | void ComboboxWidgetsPage::OnComboBox(wxCommandEvent& event) | |
483 | { | |
484 | int sel = event.GetInt(); | |
485 | m_textDelete->SetValue(wxString::Format(_T("%ld"), sel)); | |
486 | ||
487 | wxLogMessage(_T("Combobox item %d selected"), sel); | |
488 | } | |
489 | ||
490 | void ComboboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& event) | |
491 | { | |
492 | CreateCombo(); | |
493 | } | |
494 |