]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Program: wxWidgets 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 | #if wxUSE_COMBOBOX | |
28 | ||
29 | // for all others, include the necessary headers | |
30 | #ifndef WX_PRECOMP | |
31 | #include "wx/log.h" | |
32 | ||
33 | #include "wx/bitmap.h" | |
34 | #include "wx/button.h" | |
35 | #include "wx/checkbox.h" | |
36 | #include "wx/combobox.h" | |
37 | #include "wx/radiobox.h" | |
38 | #include "wx/statbox.h" | |
39 | #include "wx/textctrl.h" | |
40 | #endif | |
41 | ||
42 | #include "wx/sizer.h" | |
43 | ||
44 | #include "itemcontainer.h" | |
45 | #include "widgets.h" | |
46 | #if 1 | |
47 | #include "icons/combobox.xpm" | |
48 | ||
49 | // ---------------------------------------------------------------------------- | |
50 | // constants | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
53 | // control ids | |
54 | enum | |
55 | { | |
56 | ComboPage_Reset = wxID_HIGHEST, | |
57 | ComboPage_SetCurrent, | |
58 | ComboPage_CurText, | |
59 | ComboPage_InsertionPointText, | |
60 | ComboPage_Insert, | |
61 | ComboPage_InsertText, | |
62 | ComboPage_Add, | |
63 | ComboPage_AddText, | |
64 | ComboPage_AddSeveral, | |
65 | ComboPage_AddMany, | |
66 | ComboPage_Clear, | |
67 | ComboPage_Change, | |
68 | ComboPage_ChangeText, | |
69 | ComboPage_Delete, | |
70 | ComboPage_DeleteText, | |
71 | ComboPage_DeleteSel, | |
72 | ComboPage_SetValue, | |
73 | ComboPage_SetValueText, | |
74 | ComboPage_Combo, | |
75 | ComboPage_ContainerTests | |
76 | }; | |
77 | ||
78 | // kinds of comboboxes | |
79 | enum | |
80 | { | |
81 | ComboKind_Default, | |
82 | ComboKind_Simple, | |
83 | ComboKind_DropDown | |
84 | }; | |
85 | ||
86 | // ---------------------------------------------------------------------------- | |
87 | // ComboboxWidgetsPage | |
88 | // ---------------------------------------------------------------------------- | |
89 | ||
90 | class ComboboxWidgetsPage : public ItemContainerWidgetsPage | |
91 | { | |
92 | public: | |
93 | ComboboxWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist); | |
94 | ||
95 | virtual wxControl *GetWidget() const { return m_combobox; } | |
96 | virtual wxTextEntryBase *GetTextEntry() const { return m_combobox; } | |
97 | virtual wxItemContainer* GetContainer() const { return m_combobox; } | |
98 | virtual void RecreateWidget() { CreateCombo(); } | |
99 | ||
100 | // lazy creation of the content | |
101 | virtual void CreateContent(); | |
102 | ||
103 | protected: | |
104 | // event handlers | |
105 | void OnButtonReset(wxCommandEvent& event); | |
106 | void OnButtonChange(wxCommandEvent& event); | |
107 | void OnButtonDelete(wxCommandEvent& event); | |
108 | void OnButtonDeleteSel(wxCommandEvent& event); | |
109 | void OnButtonClear(wxCommandEvent& event); | |
110 | void OnButtonInsert(wxCommandEvent &event); | |
111 | void OnButtonAdd(wxCommandEvent& event); | |
112 | void OnButtonAddSeveral(wxCommandEvent& event); | |
113 | void OnButtonAddMany(wxCommandEvent& event); | |
114 | void OnButtonSetValue(wxCommandEvent& event); | |
115 | void OnButtonSetCurrent(wxCommandEvent& event); | |
116 | ||
117 | void OnDropdown(wxCommandEvent& event); | |
118 | void OnCloseup(wxCommandEvent& event); | |
119 | void OnComboBox(wxCommandEvent& event); | |
120 | void OnComboText(wxCommandEvent& event); | |
121 | ||
122 | void OnCheckOrRadioBox(wxCommandEvent& event); | |
123 | ||
124 | void OnUpdateUIInsertionPointText(wxUpdateUIEvent& event); | |
125 | ||
126 | void OnUpdateUIInsert(wxUpdateUIEvent& event); | |
127 | void OnUpdateUIAddSeveral(wxUpdateUIEvent& event); | |
128 | void OnUpdateUIClearButton(wxUpdateUIEvent& event); | |
129 | void OnUpdateUIDeleteButton(wxUpdateUIEvent& event); | |
130 | void OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event); | |
131 | void OnUpdateUIResetButton(wxUpdateUIEvent& event); | |
132 | void OnUpdateUISetCurrent(wxUpdateUIEvent& event); | |
133 | ||
134 | // reset the combobox parameters | |
135 | void Reset(); | |
136 | ||
137 | // (re)create the combobox | |
138 | void CreateCombo(); | |
139 | ||
140 | // the controls | |
141 | // ------------ | |
142 | ||
143 | // the sel mode radiobox | |
144 | wxRadioBox *m_radioKind; | |
145 | ||
146 | // the checkboxes for styles | |
147 | wxCheckBox *m_chkSort, | |
148 | *m_chkReadonly, | |
149 | *m_chkFilename; | |
150 | ||
151 | // the combobox itself and the sizer it is in | |
152 | wxComboBox *m_combobox; | |
153 | wxSizer *m_sizerCombo; | |
154 | ||
155 | // the text entries for "Add/change string" and "Delete" buttons | |
156 | wxTextCtrl *m_textInsert, | |
157 | *m_textAdd, | |
158 | *m_textChange, | |
159 | *m_textSetValue, | |
160 | *m_textDelete, | |
161 | *m_textCur; | |
162 | ||
163 | private: | |
164 | DECLARE_EVENT_TABLE() | |
165 | DECLARE_WIDGETS_PAGE(ComboboxWidgetsPage) | |
166 | }; | |
167 | ||
168 | // ---------------------------------------------------------------------------- | |
169 | // event tables | |
170 | // ---------------------------------------------------------------------------- | |
171 | ||
172 | BEGIN_EVENT_TABLE(ComboboxWidgetsPage, WidgetsPage) | |
173 | EVT_BUTTON(ComboPage_Reset, ComboboxWidgetsPage::OnButtonReset) | |
174 | EVT_BUTTON(ComboPage_Change, ComboboxWidgetsPage::OnButtonChange) | |
175 | EVT_BUTTON(ComboPage_Delete, ComboboxWidgetsPage::OnButtonDelete) | |
176 | EVT_BUTTON(ComboPage_DeleteSel, ComboboxWidgetsPage::OnButtonDeleteSel) | |
177 | EVT_BUTTON(ComboPage_Clear, ComboboxWidgetsPage::OnButtonClear) | |
178 | EVT_BUTTON(ComboPage_Insert, ComboboxWidgetsPage::OnButtonInsert) | |
179 | EVT_BUTTON(ComboPage_Add, ComboboxWidgetsPage::OnButtonAdd) | |
180 | EVT_BUTTON(ComboPage_AddSeveral, ComboboxWidgetsPage::OnButtonAddSeveral) | |
181 | EVT_BUTTON(ComboPage_AddMany, ComboboxWidgetsPage::OnButtonAddMany) | |
182 | EVT_BUTTON(ComboPage_SetValue, ComboboxWidgetsPage::OnButtonSetValue) | |
183 | EVT_BUTTON(ComboPage_SetCurrent, ComboboxWidgetsPage::OnButtonSetCurrent) | |
184 | EVT_BUTTON(ComboPage_ContainerTests, ItemContainerWidgetsPage::OnButtonTestItemContainer) | |
185 | ||
186 | EVT_TEXT_ENTER(ComboPage_InsertText, ComboboxWidgetsPage::OnButtonInsert) | |
187 | EVT_TEXT_ENTER(ComboPage_AddText, ComboboxWidgetsPage::OnButtonAdd) | |
188 | EVT_TEXT_ENTER(ComboPage_DeleteText, ComboboxWidgetsPage::OnButtonDelete) | |
189 | ||
190 | EVT_UPDATE_UI(ComboPage_InsertionPointText, ComboboxWidgetsPage::OnUpdateUIInsertionPointText) | |
191 | ||
192 | EVT_UPDATE_UI(ComboPage_Reset, ComboboxWidgetsPage::OnUpdateUIResetButton) | |
193 | EVT_UPDATE_UI(ComboPage_Insert, ComboboxWidgetsPage::OnUpdateUIInsert) | |
194 | EVT_UPDATE_UI(ComboPage_AddSeveral, ComboboxWidgetsPage::OnUpdateUIAddSeveral) | |
195 | EVT_UPDATE_UI(ComboPage_Clear, ComboboxWidgetsPage::OnUpdateUIClearButton) | |
196 | EVT_UPDATE_UI(ComboPage_DeleteText, ComboboxWidgetsPage::OnUpdateUIClearButton) | |
197 | EVT_UPDATE_UI(ComboPage_Delete, ComboboxWidgetsPage::OnUpdateUIDeleteButton) | |
198 | EVT_UPDATE_UI(ComboPage_Change, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton) | |
199 | EVT_UPDATE_UI(ComboPage_ChangeText, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton) | |
200 | EVT_UPDATE_UI(ComboPage_DeleteSel, ComboboxWidgetsPage::OnUpdateUIDeleteSelButton) | |
201 | EVT_UPDATE_UI(ComboPage_SetCurrent, ComboboxWidgetsPage::OnUpdateUISetCurrent) | |
202 | ||
203 | EVT_COMBOBOX(ComboPage_Combo, ComboboxWidgetsPage::OnComboBox) | |
204 | EVT_COMBOBOX_DROPDOWN(ComboPage_Combo, ComboboxWidgetsPage::OnDropdown) | |
205 | EVT_COMBOBOX_CLOSEUP(ComboPage_Combo, ComboboxWidgetsPage::OnCloseup) | |
206 | EVT_TEXT(ComboPage_Combo, ComboboxWidgetsPage::OnComboText) | |
207 | EVT_TEXT_ENTER(ComboPage_Combo, ComboboxWidgetsPage::OnComboText) | |
208 | ||
209 | EVT_CHECKBOX(wxID_ANY, ComboboxWidgetsPage::OnCheckOrRadioBox) | |
210 | EVT_RADIOBOX(wxID_ANY, ComboboxWidgetsPage::OnCheckOrRadioBox) | |
211 | END_EVENT_TABLE() | |
212 | ||
213 | // ============================================================================ | |
214 | // implementation | |
215 | // ============================================================================ | |
216 | ||
217 | #if defined(__WXUNIVERSAL__) | |
218 | #define FAMILY_CTRLS UNIVERSAL_CTRLS | |
219 | #else | |
220 | #define FAMILY_CTRLS NATIVE_CTRLS | |
221 | #endif | |
222 | ||
223 | IMPLEMENT_WIDGETS_PAGE(ComboboxWidgetsPage, wxT("Combobox"), | |
224 | FAMILY_CTRLS | WITH_ITEMS_CTRLS | COMBO_CTRLS | |
225 | ); | |
226 | ||
227 | ComboboxWidgetsPage::ComboboxWidgetsPage(WidgetsBookCtrl *book, | |
228 | wxImageList *imaglist) | |
229 | : ItemContainerWidgetsPage(book, imaglist, combobox_xpm) | |
230 | { | |
231 | // init everything | |
232 | m_chkSort = | |
233 | m_chkReadonly = | |
234 | m_chkFilename = (wxCheckBox *)NULL; | |
235 | ||
236 | m_combobox = (wxComboBox *)NULL; | |
237 | m_sizerCombo = (wxSizer *)NULL; | |
238 | } | |
239 | ||
240 | void ComboboxWidgetsPage::CreateContent() | |
241 | { | |
242 | /* | |
243 | What we create here is a frame having 3 panes: style pane is the | |
244 | leftmost one, in the middle the pane with buttons allowing to perform | |
245 | miscellaneous combobox operations and the pane containing the combobox | |
246 | itself to the right | |
247 | */ | |
248 | wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL); | |
249 | ||
250 | // left pane | |
251 | wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("&Set style")); | |
252 | ||
253 | // should be in sync with ComboKind_XXX values | |
254 | static const wxString kinds[] = | |
255 | { | |
256 | wxT("default"), | |
257 | wxT("simple"), | |
258 | wxT("drop down"), | |
259 | }; | |
260 | ||
261 | m_radioKind = new wxRadioBox(this, wxID_ANY, wxT("Combobox &kind:"), | |
262 | wxDefaultPosition, wxDefaultSize, | |
263 | WXSIZEOF(kinds), kinds, | |
264 | 1, wxRA_SPECIFY_COLS); | |
265 | ||
266 | wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL); | |
267 | ||
268 | m_chkSort = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Sort items")); | |
269 | m_chkReadonly = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Read only")); | |
270 | m_chkFilename = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&File name")); | |
271 | m_chkFilename->Disable(); // not implemented yet | |
272 | ||
273 | sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer | |
274 | sizerLeft->Add(m_radioKind, 0, wxGROW | wxALL, 5); | |
275 | ||
276 | wxButton *btn = new wxButton(this, ComboPage_Reset, wxT("&Reset")); | |
277 | sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15); | |
278 | ||
279 | // middle pane | |
280 | wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, | |
281 | wxT("&Change combobox contents")); | |
282 | wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL); | |
283 | ||
284 | wxSizer *sizerRow; | |
285 | ||
286 | sizerRow = CreateSizerWithTextAndButton(ComboPage_SetCurrent, | |
287 | wxT("Current &selection"), | |
288 | ComboPage_CurText, | |
289 | &m_textCur); | |
290 | ||
291 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
292 | ||
293 | wxTextCtrl *text; | |
294 | sizerRow = CreateSizerWithTextAndLabel(wxT("Insertion Point"), | |
295 | ComboPage_InsertionPointText, | |
296 | &text); | |
297 | text->SetEditable(false); | |
298 | ||
299 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
300 | ||
301 | sizerRow = CreateSizerWithTextAndButton(ComboPage_Insert, | |
302 | wxT("&Insert this string"), | |
303 | ComboPage_InsertText, | |
304 | &m_textInsert); | |
305 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
306 | ||
307 | sizerRow = CreateSizerWithTextAndButton(ComboPage_Add, | |
308 | wxT("&Add this string"), | |
309 | ComboPage_AddText, | |
310 | &m_textAdd); | |
311 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
312 | ||
313 | btn = new wxButton(this, ComboPage_AddSeveral, wxT("&Append a few strings")); | |
314 | sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); | |
315 | ||
316 | btn = new wxButton(this, ComboPage_AddMany, wxT("Append &many strings")); | |
317 | sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); | |
318 | ||
319 | sizerRow = CreateSizerWithTextAndButton(ComboPage_Change, | |
320 | wxT("C&hange current"), | |
321 | ComboPage_ChangeText, | |
322 | &m_textChange); | |
323 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
324 | ||
325 | sizerRow = CreateSizerWithTextAndButton(ComboPage_Delete, | |
326 | wxT("&Delete this item"), | |
327 | ComboPage_DeleteText, | |
328 | &m_textDelete); | |
329 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
330 | ||
331 | btn = new wxButton(this, ComboPage_DeleteSel, wxT("Delete &selection")); | |
332 | sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); | |
333 | ||
334 | btn = new wxButton(this, ComboPage_Clear, wxT("&Clear")); | |
335 | sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); | |
336 | ||
337 | sizerRow = CreateSizerWithTextAndButton(ComboPage_SetValue, | |
338 | wxT("SetValue"), | |
339 | ComboPage_SetValueText, | |
340 | &m_textSetValue); | |
341 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
342 | ||
343 | btn = new wxButton(this, ComboPage_ContainerTests, wxT("Run &tests")); | |
344 | sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); | |
345 | ||
346 | ||
347 | ||
348 | // right pane | |
349 | wxSizer *sizerRight = new wxBoxSizer(wxVERTICAL); | |
350 | m_combobox = new wxComboBox(this, ComboPage_Combo, wxEmptyString, | |
351 | wxDefaultPosition, wxDefaultSize, | |
352 | 0, NULL, | |
353 | 0); | |
354 | sizerRight->Add(m_combobox, 0, wxGROW | wxALL, 5); | |
355 | sizerRight->SetMinSize(150, 0); | |
356 | m_sizerCombo = sizerRight; // save it to modify it later | |
357 | ||
358 | // the 3 panes panes compose the window | |
359 | sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10); | |
360 | sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10); | |
361 | sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10); | |
362 | ||
363 | // final initializations | |
364 | Reset(); | |
365 | ||
366 | SetSizer(sizerTop); | |
367 | } | |
368 | ||
369 | // ---------------------------------------------------------------------------- | |
370 | // operations | |
371 | // ---------------------------------------------------------------------------- | |
372 | ||
373 | void ComboboxWidgetsPage::Reset() | |
374 | { | |
375 | m_chkSort->SetValue(false); | |
376 | m_chkReadonly->SetValue(false); | |
377 | m_chkFilename->SetValue(false); | |
378 | } | |
379 | ||
380 | void ComboboxWidgetsPage::CreateCombo() | |
381 | { | |
382 | int flags = ms_defaultFlags; | |
383 | ||
384 | if ( m_chkSort->GetValue() ) | |
385 | flags |= wxCB_SORT; | |
386 | if ( m_chkReadonly->GetValue() ) | |
387 | flags |= wxCB_READONLY; | |
388 | ||
389 | switch ( m_radioKind->GetSelection() ) | |
390 | { | |
391 | default: | |
392 | wxFAIL_MSG( wxT("unknown combo kind") ); | |
393 | // fall through | |
394 | ||
395 | case ComboKind_Default: | |
396 | break; | |
397 | ||
398 | case ComboKind_Simple: | |
399 | flags |= wxCB_SIMPLE; | |
400 | break; | |
401 | ||
402 | case ComboKind_DropDown: | |
403 | flags = wxCB_DROPDOWN; | |
404 | break; | |
405 | } | |
406 | ||
407 | wxArrayString items; | |
408 | if ( m_combobox ) | |
409 | { | |
410 | unsigned int count = m_combobox->GetCount(); | |
411 | for ( unsigned int n = 0; n < count; n++ ) | |
412 | { | |
413 | items.Add(m_combobox->GetString(n)); | |
414 | } | |
415 | ||
416 | m_sizerCombo->Detach( m_combobox ); | |
417 | delete m_combobox; | |
418 | } | |
419 | ||
420 | m_combobox = new wxComboBox(this, ComboPage_Combo, wxEmptyString, | |
421 | wxDefaultPosition, wxDefaultSize, | |
422 | 0, NULL, | |
423 | flags); | |
424 | ||
425 | #if 0 | |
426 | if ( m_chkFilename->GetValue() ) | |
427 | ; | |
428 | #endif // TODO | |
429 | ||
430 | unsigned int count = items.GetCount(); | |
431 | for ( unsigned int n = 0; n < count; n++ ) | |
432 | { | |
433 | m_combobox->Append(items[n]); | |
434 | } | |
435 | ||
436 | m_sizerCombo->Add(m_combobox, 0, wxGROW | wxALL, 5); | |
437 | m_sizerCombo->Layout(); | |
438 | } | |
439 | ||
440 | // ---------------------------------------------------------------------------- | |
441 | // event handlers | |
442 | // ---------------------------------------------------------------------------- | |
443 | ||
444 | void ComboboxWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event)) | |
445 | { | |
446 | Reset(); | |
447 | ||
448 | CreateCombo(); | |
449 | } | |
450 | ||
451 | void ComboboxWidgetsPage::OnButtonChange(wxCommandEvent& WXUNUSED(event)) | |
452 | { | |
453 | int sel = m_combobox->GetSelection(); | |
454 | if ( sel != wxNOT_FOUND ) | |
455 | { | |
456 | #ifndef __WXGTK__ | |
457 | m_combobox->SetString(sel, m_textChange->GetValue()); | |
458 | #else | |
459 | wxLogMessage(wxT("Not implemented in wxGTK")); | |
460 | #endif | |
461 | } | |
462 | } | |
463 | ||
464 | void ComboboxWidgetsPage::OnButtonDelete(wxCommandEvent& WXUNUSED(event)) | |
465 | { | |
466 | unsigned long n; | |
467 | if ( !m_textDelete->GetValue().ToULong(&n) || | |
468 | (n >= m_combobox->GetCount()) ) | |
469 | { | |
470 | return; | |
471 | } | |
472 | ||
473 | m_combobox->Delete(n); | |
474 | } | |
475 | ||
476 | void ComboboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent& WXUNUSED(event)) | |
477 | { | |
478 | int sel = m_combobox->GetSelection(); | |
479 | if ( sel != wxNOT_FOUND ) | |
480 | { | |
481 | m_combobox->Delete(sel); | |
482 | } | |
483 | } | |
484 | ||
485 | void ComboboxWidgetsPage::OnButtonSetValue(wxCommandEvent& WXUNUSED(event)) | |
486 | { | |
487 | wxString value = m_textSetValue->GetValue(); | |
488 | m_combobox->SetValue( value ); | |
489 | } | |
490 | ||
491 | void ComboboxWidgetsPage::OnButtonClear(wxCommandEvent& WXUNUSED(event)) | |
492 | { | |
493 | m_combobox->Clear(); | |
494 | } | |
495 | ||
496 | void ComboboxWidgetsPage::OnButtonInsert(wxCommandEvent& WXUNUSED(event)) | |
497 | { | |
498 | static unsigned int s_item = 0; | |
499 | ||
500 | wxString s = m_textInsert->GetValue(); | |
501 | if ( !m_textInsert->IsModified() ) | |
502 | { | |
503 | // update the default string | |
504 | m_textInsert->SetValue(wxString::Format(wxT("test item %u"), ++s_item)); | |
505 | } | |
506 | ||
507 | if (m_combobox->GetSelection() >= 0) | |
508 | m_combobox->Insert(s, m_combobox->GetSelection()); | |
509 | } | |
510 | ||
511 | void ComboboxWidgetsPage::OnButtonAdd(wxCommandEvent& WXUNUSED(event)) | |
512 | { | |
513 | static unsigned int s_item = 0; | |
514 | ||
515 | wxString s = m_textAdd->GetValue(); | |
516 | if ( !m_textAdd->IsModified() ) | |
517 | { | |
518 | // update the default string | |
519 | m_textAdd->SetValue(wxString::Format(wxT("test item %u"), ++s_item)); | |
520 | } | |
521 | ||
522 | m_combobox->Append(s); | |
523 | } | |
524 | ||
525 | void ComboboxWidgetsPage::OnButtonAddMany(wxCommandEvent& WXUNUSED(event)) | |
526 | { | |
527 | // "many" means 1000 here | |
528 | for ( unsigned int n = 0; n < 1000; n++ ) | |
529 | { | |
530 | m_combobox->Append(wxString::Format(wxT("item #%u"), n)); | |
531 | } | |
532 | } | |
533 | ||
534 | void ComboboxWidgetsPage::OnButtonSetCurrent(wxCommandEvent& WXUNUSED(event)) | |
535 | { | |
536 | long n; | |
537 | if ( !m_textCur->GetValue().ToLong(&n) ) | |
538 | return; | |
539 | ||
540 | m_combobox->SetSelection(n); | |
541 | } | |
542 | ||
543 | void ComboboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent& WXUNUSED(event)) | |
544 | { | |
545 | m_combobox->Append(wxT("First")); | |
546 | m_combobox->Append(wxT("another one")); | |
547 | m_combobox->Append(wxT("and the last (very very very very very very very very very very long) one")); | |
548 | } | |
549 | ||
550 | void ComboboxWidgetsPage::OnUpdateUIInsertionPointText(wxUpdateUIEvent& event) | |
551 | { | |
552 | if (m_combobox) | |
553 | event.SetText( wxString::Format(wxT("%ld"), m_combobox->GetInsertionPoint()) ); | |
554 | } | |
555 | ||
556 | void ComboboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event) | |
557 | { | |
558 | event.Enable( m_chkSort->GetValue() || | |
559 | m_chkReadonly->GetValue() || | |
560 | m_chkFilename->GetValue() ); | |
561 | } | |
562 | ||
563 | void ComboboxWidgetsPage::OnUpdateUIInsert(wxUpdateUIEvent& event) | |
564 | { | |
565 | if (m_combobox) | |
566 | { | |
567 | bool enable = !(m_combobox->GetWindowStyle() & wxCB_SORT) && | |
568 | (m_combobox->GetSelection() >= 0); | |
569 | ||
570 | event.Enable(enable); | |
571 | } | |
572 | } | |
573 | ||
574 | void ComboboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent& event) | |
575 | { | |
576 | if (m_combobox) | |
577 | { | |
578 | unsigned long n; | |
579 | event.Enable(m_textDelete->GetValue().ToULong(&n) && | |
580 | (n < (unsigned)m_combobox->GetCount())); | |
581 | } | |
582 | } | |
583 | ||
584 | void ComboboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event) | |
585 | { | |
586 | if (m_combobox) | |
587 | event.Enable(m_combobox->GetSelection() != wxNOT_FOUND); | |
588 | } | |
589 | ||
590 | void ComboboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent& event) | |
591 | { | |
592 | if (m_combobox) | |
593 | event.Enable(m_combobox->GetCount() != 0); | |
594 | } | |
595 | ||
596 | void ComboboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent& event) | |
597 | { | |
598 | if (m_combobox) | |
599 | event.Enable(!(m_combobox->GetWindowStyle() & wxCB_SORT)); | |
600 | } | |
601 | ||
602 | void ComboboxWidgetsPage::OnUpdateUISetCurrent(wxUpdateUIEvent& event) | |
603 | { | |
604 | long n; | |
605 | event.Enable( m_textCur->GetValue().ToLong(&n) && | |
606 | (n == wxNOT_FOUND || | |
607 | (n >= 0 && (unsigned)n < m_combobox->GetCount())) ); | |
608 | } | |
609 | ||
610 | void ComboboxWidgetsPage::OnComboText(wxCommandEvent& event) | |
611 | { | |
612 | if (!m_combobox) | |
613 | return; | |
614 | ||
615 | wxString s = event.GetString(); | |
616 | ||
617 | wxASSERT_MSG( s == m_combobox->GetValue(), | |
618 | wxT("event and combobox values should be the same") ); | |
619 | ||
620 | if (event.GetEventType() == wxEVT_COMMAND_TEXT_ENTER) | |
621 | { | |
622 | wxLogMessage(wxT("Combobox enter pressed (now '%s')"), s.c_str()); | |
623 | } | |
624 | else | |
625 | { | |
626 | wxLogMessage(wxT("Combobox text changed (now '%s')"), s.c_str()); | |
627 | } | |
628 | } | |
629 | ||
630 | void ComboboxWidgetsPage::OnComboBox(wxCommandEvent& event) | |
631 | { | |
632 | long sel = event.GetInt(); | |
633 | const wxString selstr = wxString::Format(wxT("%ld"), sel); | |
634 | m_textDelete->SetValue(selstr); | |
635 | m_textCur->SetValue(selstr); | |
636 | ||
637 | wxLogMessage(wxT("Combobox item %ld selected"), sel); | |
638 | ||
639 | wxLogMessage(wxT("Combobox GetValue(): %s"), m_combobox->GetValue().c_str() ); | |
640 | } | |
641 | ||
642 | void ComboboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event)) | |
643 | { | |
644 | CreateCombo(); | |
645 | } | |
646 | ||
647 | void ComboboxWidgetsPage::OnDropdown(wxCommandEvent& WXUNUSED(event)) | |
648 | { | |
649 | wxLogMessage(wxT("Combobox dropped down")); | |
650 | } | |
651 | ||
652 | void ComboboxWidgetsPage::OnCloseup(wxCommandEvent& WXUNUSED(event)) | |
653 | { | |
654 | wxLogMessage(wxT("Combobox closed up")); | |
655 | } | |
656 | ||
657 | #endif //wxUSE_COMBOBOX | |
658 | ||
659 | #endif |