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