]>
Commit | Line | Data |
---|---|---|
32b8ec41 | 1 | ///////////////////////////////////////////////////////////////////////////// |
be5a51fb | 2 | // Program: wxWidgets Widgets Sample |
32b8ec41 VZ |
3 | // Name: listbox.cpp |
4 | // Purpose: Part of the widgets sample showing wxListbox | |
5 | // Author: Vadim Zeitlin | |
6 | // Created: 27.03.01 | |
32b8ec41 | 7 | // Copyright: (c) 2001 Vadim Zeitlin |
526954c5 | 8 | // Licence: wxWindows licence |
32b8ec41 VZ |
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 | ||
eecdb000 WS |
26 | #if wxUSE_LISTBOX |
27 | ||
32b8ec41 VZ |
28 | // for all others, include the necessary headers |
29 | #ifndef WX_PRECOMP | |
30 | #include "wx/log.h" | |
31 | ||
3bb70c40 | 32 | #include "wx/bitmap.h" |
32b8ec41 VZ |
33 | #include "wx/button.h" |
34 | #include "wx/checkbox.h" | |
35 | #include "wx/combobox.h" | |
36 | #include "wx/listbox.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 "wx/checklst.h" | |
45 | ||
a236aa20 | 46 | #include "itemcontainer.h" |
32b8ec41 | 47 | #include "widgets.h" |
eecdb000 | 48 | |
32b8ec41 VZ |
49 | #include "icons/listbox.xpm" |
50 | ||
51 | // ---------------------------------------------------------------------------- | |
52 | // constants | |
53 | // ---------------------------------------------------------------------------- | |
54 | ||
55 | // control ids | |
56 | enum | |
57 | { | |
f0fa4312 | 58 | ListboxPage_Reset = wxID_HIGHEST, |
32b8ec41 VZ |
59 | ListboxPage_Add, |
60 | ListboxPage_AddText, | |
61 | ListboxPage_AddSeveral, | |
62 | ListboxPage_AddMany, | |
63 | ListboxPage_Clear, | |
64 | ListboxPage_Change, | |
65 | ListboxPage_ChangeText, | |
66 | ListboxPage_Delete, | |
67 | ListboxPage_DeleteText, | |
68 | ListboxPage_DeleteSel, | |
a236aa20 | 69 | ListboxPage_Listbox, |
0d29a482 VZ |
70 | ListboxPage_EnsureVisible, |
71 | ListboxPage_EnsureVisibleText, | |
a236aa20 | 72 | ListboxPage_ContainerTests |
32b8ec41 VZ |
73 | }; |
74 | ||
75 | // ---------------------------------------------------------------------------- | |
76 | // ListboxWidgetsPage | |
77 | // ---------------------------------------------------------------------------- | |
78 | ||
a236aa20 | 79 | class ListboxWidgetsPage : public ItemContainerWidgetsPage |
32b8ec41 VZ |
80 | { |
81 | public: | |
f2fdc4d5 | 82 | ListboxWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist); |
32b8ec41 | 83 | |
195df7a7 | 84 | virtual wxControl *GetWidget() const { return m_lbox; } |
a236aa20 | 85 | virtual wxItemContainer* GetContainer() const { return m_lbox; } |
1301e228 | 86 | virtual void RecreateWidget() { CreateLbox(); } |
195df7a7 | 87 | |
453535a7 WS |
88 | // lazy creation of the content |
89 | virtual void CreateContent(); | |
90 | ||
32b8ec41 VZ |
91 | protected: |
92 | // event handlers | |
93 | void OnButtonReset(wxCommandEvent& event); | |
94 | void OnButtonChange(wxCommandEvent& event); | |
0d29a482 | 95 | void OnButtonEnsureVisible(wxCommandEvent& event); |
32b8ec41 VZ |
96 | void OnButtonDelete(wxCommandEvent& event); |
97 | void OnButtonDeleteSel(wxCommandEvent& event); | |
98 | void OnButtonClear(wxCommandEvent& event); | |
99 | void OnButtonAdd(wxCommandEvent& event); | |
100 | void OnButtonAddSeveral(wxCommandEvent& event); | |
101 | void OnButtonAddMany(wxCommandEvent& event); | |
102 | ||
103 | void OnListbox(wxCommandEvent& event); | |
104 | void OnListboxDClick(wxCommandEvent& event); | |
105 | void OnCheckListbox(wxCommandEvent& event); | |
106 | ||
107 | void OnCheckOrRadioBox(wxCommandEvent& event); | |
108 | ||
109 | void OnUpdateUIAddSeveral(wxUpdateUIEvent& event); | |
110 | void OnUpdateUIClearButton(wxUpdateUIEvent& event); | |
0d29a482 | 111 | void OnUpdateUIEnsureVisibleButton(wxUpdateUIEvent& event); |
32b8ec41 VZ |
112 | void OnUpdateUIDeleteButton(wxUpdateUIEvent& event); |
113 | void OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event); | |
114 | void OnUpdateUIResetButton(wxUpdateUIEvent& event); | |
115 | ||
116 | // reset the listbox parameters | |
117 | void Reset(); | |
118 | ||
119 | // (re)create the listbox | |
120 | void CreateLbox(); | |
121 | ||
0d29a482 VZ |
122 | // read the value of a listbox item index from the given control, return |
123 | // false if it's invalid | |
124 | bool GetValidIndexFromText(const wxTextCtrl *text, int *n = NULL) const; | |
125 | ||
126 | ||
32b8ec41 VZ |
127 | // listbox parameters |
128 | // ------------------ | |
129 | ||
130 | // the selection mode | |
131 | enum LboxSelection | |
132 | { | |
133 | LboxSel_Single, | |
134 | LboxSel_Extended, | |
135 | LboxSel_Multiple | |
136 | } m_lboxSelMode; | |
137 | ||
138 | // should it be sorted? | |
139 | bool m_sorted; | |
140 | ||
141 | // should it have horz scroll/vert scrollbar permanently shown? | |
142 | bool m_horzScroll, | |
143 | m_vertScrollAlways; | |
144 | ||
145 | // the controls | |
146 | // ------------ | |
147 | ||
148 | // the sel mode radiobox | |
149 | wxRadioBox *m_radioSelMode; | |
150 | ||
151 | // the checkboxes | |
822b9009 | 152 | wxCheckBox *m_chkVScroll, |
32b8ec41 | 153 | *m_chkHScroll, |
822b9009 VZ |
154 | *m_chkCheck, |
155 | *m_chkSort, | |
156 | *m_chkOwnerDraw; | |
32b8ec41 VZ |
157 | |
158 | // the listbox itself and the sizer it is in | |
b3c33d35 WS |
159 | #ifdef __WXWINCE__ |
160 | wxListBoxBase | |
161 | #else | |
453535a7 | 162 | wxListBox |
b3c33d35 WS |
163 | #endif |
164 | *m_lbox; | |
165 | ||
32b8ec41 VZ |
166 | wxSizer *m_sizerLbox; |
167 | ||
168 | // the text entries for "Add/change string" and "Delete" buttons | |
169 | wxTextCtrl *m_textAdd, | |
170 | *m_textChange, | |
0d29a482 | 171 | *m_textEnsureVisible, |
32b8ec41 VZ |
172 | *m_textDelete; |
173 | ||
174 | private: | |
5e173f35 GD |
175 | DECLARE_EVENT_TABLE() |
176 | DECLARE_WIDGETS_PAGE(ListboxWidgetsPage) | |
32b8ec41 VZ |
177 | }; |
178 | ||
179 | // ---------------------------------------------------------------------------- | |
180 | // event tables | |
181 | // ---------------------------------------------------------------------------- | |
182 | ||
183 | BEGIN_EVENT_TABLE(ListboxWidgetsPage, WidgetsPage) | |
184 | EVT_BUTTON(ListboxPage_Reset, ListboxWidgetsPage::OnButtonReset) | |
185 | EVT_BUTTON(ListboxPage_Change, ListboxWidgetsPage::OnButtonChange) | |
186 | EVT_BUTTON(ListboxPage_Delete, ListboxWidgetsPage::OnButtonDelete) | |
187 | EVT_BUTTON(ListboxPage_DeleteSel, ListboxWidgetsPage::OnButtonDeleteSel) | |
0d29a482 | 188 | EVT_BUTTON(ListboxPage_EnsureVisible, ListboxWidgetsPage::OnButtonEnsureVisible) |
32b8ec41 VZ |
189 | EVT_BUTTON(ListboxPage_Clear, ListboxWidgetsPage::OnButtonClear) |
190 | EVT_BUTTON(ListboxPage_Add, ListboxWidgetsPage::OnButtonAdd) | |
191 | EVT_BUTTON(ListboxPage_AddSeveral, ListboxWidgetsPage::OnButtonAddSeveral) | |
192 | EVT_BUTTON(ListboxPage_AddMany, ListboxWidgetsPage::OnButtonAddMany) | |
a236aa20 | 193 | EVT_BUTTON(ListboxPage_ContainerTests, ItemContainerWidgetsPage::OnButtonTestItemContainer) |
32b8ec41 VZ |
194 | |
195 | EVT_TEXT_ENTER(ListboxPage_AddText, ListboxWidgetsPage::OnButtonAdd) | |
196 | EVT_TEXT_ENTER(ListboxPage_DeleteText, ListboxWidgetsPage::OnButtonDelete) | |
0d29a482 | 197 | EVT_TEXT_ENTER(ListboxPage_EnsureVisibleText, ListboxWidgetsPage::OnButtonEnsureVisible) |
32b8ec41 VZ |
198 | |
199 | EVT_UPDATE_UI(ListboxPage_Reset, ListboxWidgetsPage::OnUpdateUIResetButton) | |
200 | EVT_UPDATE_UI(ListboxPage_AddSeveral, ListboxWidgetsPage::OnUpdateUIAddSeveral) | |
201 | EVT_UPDATE_UI(ListboxPage_Clear, ListboxWidgetsPage::OnUpdateUIClearButton) | |
202 | EVT_UPDATE_UI(ListboxPage_DeleteText, ListboxWidgetsPage::OnUpdateUIClearButton) | |
203 | EVT_UPDATE_UI(ListboxPage_Delete, ListboxWidgetsPage::OnUpdateUIDeleteButton) | |
204 | EVT_UPDATE_UI(ListboxPage_Change, ListboxWidgetsPage::OnUpdateUIDeleteSelButton) | |
205 | EVT_UPDATE_UI(ListboxPage_ChangeText, ListboxWidgetsPage::OnUpdateUIDeleteSelButton) | |
206 | EVT_UPDATE_UI(ListboxPage_DeleteSel, ListboxWidgetsPage::OnUpdateUIDeleteSelButton) | |
0d29a482 | 207 | EVT_UPDATE_UI(ListboxPage_EnsureVisible, ListboxWidgetsPage::OnUpdateUIEnsureVisibleButton) |
32b8ec41 VZ |
208 | |
209 | EVT_LISTBOX(ListboxPage_Listbox, ListboxWidgetsPage::OnListbox) | |
210 | EVT_LISTBOX_DCLICK(ListboxPage_Listbox, ListboxWidgetsPage::OnListboxDClick) | |
211 | EVT_CHECKLISTBOX(ListboxPage_Listbox, ListboxWidgetsPage::OnCheckListbox) | |
212 | ||
206d3a16 JS |
213 | EVT_CHECKBOX(wxID_ANY, ListboxWidgetsPage::OnCheckOrRadioBox) |
214 | EVT_RADIOBOX(wxID_ANY, ListboxWidgetsPage::OnCheckOrRadioBox) | |
32b8ec41 VZ |
215 | END_EVENT_TABLE() |
216 | ||
217 | // ============================================================================ | |
218 | // implementation | |
219 | // ============================================================================ | |
220 | ||
f0fa4312 WS |
221 | #if defined(__WXUNIVERSAL__) |
222 | #define FAMILY_CTRLS UNIVERSAL_CTRLS | |
223 | #else | |
224 | #define FAMILY_CTRLS NATIVE_CTRLS | |
225 | #endif | |
226 | ||
9a83f860 | 227 | IMPLEMENT_WIDGETS_PAGE(ListboxWidgetsPage, wxT("Listbox"), |
f0fa4312 | 228 | FAMILY_CTRLS | WITH_ITEMS_CTRLS |
f2fdc4d5 | 229 | ); |
32b8ec41 | 230 | |
f2fdc4d5 | 231 | ListboxWidgetsPage::ListboxWidgetsPage(WidgetsBookCtrl *book, |
32b8ec41 | 232 | wxImageList *imaglist) |
a236aa20 | 233 | : ItemContainerWidgetsPage(book, imaglist, listbox_xpm) |
32b8ec41 | 234 | { |
32b8ec41 VZ |
235 | // init everything |
236 | m_radioSelMode = (wxRadioBox *)NULL; | |
237 | ||
238 | m_chkVScroll = | |
239 | m_chkHScroll = | |
240 | m_chkCheck = | |
822b9009 VZ |
241 | m_chkSort = |
242 | m_chkOwnerDraw = (wxCheckBox *)NULL; | |
32b8ec41 | 243 | |
b3c33d35 | 244 | m_lbox = NULL; |
32b8ec41 VZ |
245 | m_sizerLbox = (wxSizer *)NULL; |
246 | ||
453535a7 WS |
247 | } |
248 | ||
249 | void ListboxWidgetsPage::CreateContent() | |
250 | { | |
32b8ec41 VZ |
251 | /* |
252 | What we create here is a frame having 3 panes: style pane is the | |
253 | leftmost one, in the middle the pane with buttons allowing to perform | |
254 | miscellaneous listbox operations and the pane containing the listbox | |
255 | itself to the right | |
256 | */ | |
257 | wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL); | |
258 | ||
259 | // left pane | |
206d3a16 | 260 | wxStaticBox *box = new wxStaticBox(this, wxID_ANY, |
9a83f860 | 261 | wxT("&Set listbox parameters")); |
32b8ec41 VZ |
262 | wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL); |
263 | ||
264 | static const wxString modes[] = | |
265 | { | |
9a83f860 VZ |
266 | wxT("single"), |
267 | wxT("extended"), | |
268 | wxT("multiple"), | |
32b8ec41 VZ |
269 | }; |
270 | ||
9a83f860 | 271 | m_radioSelMode = new wxRadioBox(this, wxID_ANY, wxT("Selection &mode:"), |
32b8ec41 VZ |
272 | wxDefaultPosition, wxDefaultSize, |
273 | WXSIZEOF(modes), modes, | |
274 | 1, wxRA_SPECIFY_COLS); | |
275 | ||
276 | m_chkVScroll = CreateCheckBoxAndAddToSizer | |
277 | ( | |
278 | sizerLeft, | |
9a83f860 | 279 | wxT("Always show &vertical scrollbar") |
32b8ec41 VZ |
280 | ); |
281 | m_chkHScroll = CreateCheckBoxAndAddToSizer | |
282 | ( | |
283 | sizerLeft, | |
9a83f860 | 284 | wxT("Show &horizontal scrollbar") |
32b8ec41 | 285 | ); |
9a83f860 VZ |
286 | m_chkCheck = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Check list box")); |
287 | m_chkSort = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Sort items")); | |
288 | m_chkOwnerDraw = CreateCheckBoxAndAddToSizer(sizerLeft, wxT("&Owner drawn")); | |
32b8ec41 VZ |
289 | |
290 | sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer | |
291 | sizerLeft->Add(m_radioSelMode, 0, wxGROW | wxALL, 5); | |
292 | ||
9a83f860 | 293 | wxButton *btn = new wxButton(this, ListboxPage_Reset, wxT("&Reset")); |
32b8ec41 VZ |
294 | sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15); |
295 | ||
296 | // middle pane | |
206d3a16 | 297 | wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, |
9a83f860 | 298 | wxT("&Change listbox contents")); |
32b8ec41 VZ |
299 | wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL); |
300 | ||
301 | wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL); | |
9a83f860 VZ |
302 | btn = new wxButton(this, ListboxPage_Add, wxT("&Add this string")); |
303 | m_textAdd = new wxTextCtrl(this, ListboxPage_AddText, wxT("test item 0")); | |
32b8ec41 VZ |
304 | sizerRow->Add(btn, 0, wxRIGHT, 5); |
305 | sizerRow->Add(m_textAdd, 1, wxLEFT, 5); | |
306 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
307 | ||
9a83f860 | 308 | btn = new wxButton(this, ListboxPage_AddSeveral, wxT("&Insert a few strings")); |
32b8ec41 VZ |
309 | sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); |
310 | ||
9a83f860 | 311 | btn = new wxButton(this, ListboxPage_AddMany, wxT("Add &many strings")); |
32b8ec41 VZ |
312 | sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); |
313 | ||
314 | sizerRow = new wxBoxSizer(wxHORIZONTAL); | |
9a83f860 | 315 | btn = new wxButton(this, ListboxPage_Change, wxT("C&hange current")); |
206d3a16 | 316 | m_textChange = new wxTextCtrl(this, ListboxPage_ChangeText, wxEmptyString); |
32b8ec41 VZ |
317 | sizerRow->Add(btn, 0, wxRIGHT, 5); |
318 | sizerRow->Add(m_textChange, 1, wxLEFT, 5); | |
319 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
320 | ||
0d29a482 | 321 | sizerRow = new wxBoxSizer(wxHORIZONTAL); |
9a83f860 | 322 | btn = new wxButton(this, ListboxPage_EnsureVisible, wxT("Make item &visible")); |
0d29a482 VZ |
323 | m_textEnsureVisible = new wxTextCtrl(this, ListboxPage_EnsureVisibleText, wxEmptyString); |
324 | sizerRow->Add(btn, 0, wxRIGHT, 5); | |
325 | sizerRow->Add(m_textEnsureVisible, 1, wxLEFT, 5); | |
326 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
327 | ||
32b8ec41 | 328 | sizerRow = new wxBoxSizer(wxHORIZONTAL); |
9a83f860 | 329 | btn = new wxButton(this, ListboxPage_Delete, wxT("&Delete this item")); |
206d3a16 | 330 | m_textDelete = new wxTextCtrl(this, ListboxPage_DeleteText, wxEmptyString); |
32b8ec41 VZ |
331 | sizerRow->Add(btn, 0, wxRIGHT, 5); |
332 | sizerRow->Add(m_textDelete, 1, wxLEFT, 5); | |
333 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
334 | ||
9a83f860 | 335 | btn = new wxButton(this, ListboxPage_DeleteSel, wxT("Delete &selection")); |
32b8ec41 VZ |
336 | sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); |
337 | ||
9a83f860 | 338 | btn = new wxButton(this, ListboxPage_Clear, wxT("&Clear")); |
32b8ec41 VZ |
339 | sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); |
340 | ||
9a83f860 | 341 | btn = new wxButton(this, ListboxPage_ContainerTests, wxT("Run &tests")); |
a236aa20 VZ |
342 | sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); |
343 | ||
32b8ec41 VZ |
344 | // right pane |
345 | wxSizer *sizerRight = new wxBoxSizer(wxVERTICAL); | |
346 | m_lbox = new wxListBox(this, ListboxPage_Listbox, | |
347 | wxDefaultPosition, wxDefaultSize, | |
348 | 0, NULL, | |
349 | wxLB_HSCROLL); | |
350 | sizerRight->Add(m_lbox, 1, wxGROW | wxALL, 5); | |
7b127900 | 351 | sizerRight->SetMinSize(150, 0); |
32b8ec41 VZ |
352 | m_sizerLbox = sizerRight; // save it to modify it later |
353 | ||
354 | // the 3 panes panes compose the window | |
355 | sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10); | |
356 | sizerTop->Add(sizerMiddle, 1, wxGROW | wxALL, 10); | |
357 | sizerTop->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10); | |
358 | ||
359 | // final initializations | |
360 | Reset(); | |
361 | ||
32b8ec41 | 362 | SetSizer(sizerTop); |
32b8ec41 VZ |
363 | } |
364 | ||
365 | // ---------------------------------------------------------------------------- | |
366 | // operations | |
367 | // ---------------------------------------------------------------------------- | |
368 | ||
369 | void ListboxWidgetsPage::Reset() | |
370 | { | |
371 | m_radioSelMode->SetSelection(LboxSel_Single); | |
206d3a16 | 372 | m_chkVScroll->SetValue(false); |
822b9009 VZ |
373 | m_chkHScroll->SetValue(true); |
374 | m_chkCheck->SetValue(false); | |
375 | m_chkSort->SetValue(false); | |
376 | m_chkOwnerDraw->SetValue(false); | |
32b8ec41 VZ |
377 | } |
378 | ||
379 | void ListboxWidgetsPage::CreateLbox() | |
380 | { | |
1301e228 | 381 | int flags = ms_defaultFlags; |
32b8ec41 VZ |
382 | switch ( m_radioSelMode->GetSelection() ) |
383 | { | |
384 | default: | |
9a83f860 | 385 | wxFAIL_MSG( wxT("unexpected radio box selection") ); |
32b8ec41 VZ |
386 | |
387 | case LboxSel_Single: flags |= wxLB_SINGLE; break; | |
388 | case LboxSel_Extended: flags |= wxLB_EXTENDED; break; | |
389 | case LboxSel_Multiple: flags |= wxLB_MULTIPLE; break; | |
390 | } | |
391 | ||
392 | if ( m_chkVScroll->GetValue() ) | |
393 | flags |= wxLB_ALWAYS_SB; | |
394 | if ( m_chkHScroll->GetValue() ) | |
395 | flags |= wxLB_HSCROLL; | |
396 | if ( m_chkSort->GetValue() ) | |
397 | flags |= wxLB_SORT; | |
822b9009 VZ |
398 | if ( m_chkOwnerDraw->GetValue() ) |
399 | flags |= wxLB_OWNERDRAW; | |
32b8ec41 VZ |
400 | |
401 | wxArrayString items; | |
402 | if ( m_lbox ) | |
403 | { | |
404 | int count = m_lbox->GetCount(); | |
405 | for ( int n = 0; n < count; n++ ) | |
406 | { | |
407 | items.Add(m_lbox->GetString(n)); | |
408 | } | |
409 | ||
12a3f227 | 410 | m_sizerLbox->Detach( m_lbox ); |
32b8ec41 VZ |
411 | delete m_lbox; |
412 | } | |
413 | ||
e640f823 | 414 | #if wxUSE_CHECKLISTBOX |
32b8ec41 VZ |
415 | if ( m_chkCheck->GetValue() ) |
416 | { | |
417 | m_lbox = new wxCheckListBox(this, ListboxPage_Listbox, | |
418 | wxDefaultPosition, wxDefaultSize, | |
419 | 0, NULL, | |
420 | flags); | |
421 | } | |
422 | else // just a listbox | |
e640f823 | 423 | #endif |
32b8ec41 VZ |
424 | { |
425 | m_lbox = new wxListBox(this, ListboxPage_Listbox, | |
426 | wxDefaultPosition, wxDefaultSize, | |
427 | 0, NULL, | |
428 | flags); | |
429 | } | |
430 | ||
431 | m_lbox->Set(items); | |
432 | m_sizerLbox->Add(m_lbox, 1, wxGROW | wxALL, 5); | |
433 | m_sizerLbox->Layout(); | |
434 | } | |
435 | ||
0d29a482 VZ |
436 | // ---------------------------------------------------------------------------- |
437 | // miscellaneous helpers | |
438 | // ---------------------------------------------------------------------------- | |
439 | ||
440 | bool | |
441 | ListboxWidgetsPage::GetValidIndexFromText(const wxTextCtrl *text, int *n) const | |
442 | { | |
443 | unsigned long idx; | |
444 | if ( !text->GetValue().ToULong(&idx) || (idx >= m_lbox->GetCount()) ) | |
445 | { | |
446 | // don't give the warning if we're just testing but do give it if we | |
447 | // want to retrieve the value as this is only done in answer to a user | |
448 | // action | |
449 | if ( n ) | |
9fceb168 | 450 | { |
0d29a482 | 451 | wxLogWarning("Invalid index \"%s\"", text->GetValue()); |
9fceb168 VZ |
452 | } |
453 | ||
0d29a482 VZ |
454 | return false; |
455 | } | |
456 | ||
457 | if ( n ) | |
458 | *n = idx; | |
459 | ||
460 | return true; | |
461 | } | |
462 | ||
32b8ec41 VZ |
463 | // ---------------------------------------------------------------------------- |
464 | // event handlers | |
465 | // ---------------------------------------------------------------------------- | |
466 | ||
467 | void ListboxWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event)) | |
468 | { | |
469 | Reset(); | |
470 | ||
471 | CreateLbox(); | |
472 | } | |
473 | ||
474 | void ListboxWidgetsPage::OnButtonChange(wxCommandEvent& WXUNUSED(event)) | |
475 | { | |
476 | wxArrayInt selections; | |
477 | int count = m_lbox->GetSelections(selections); | |
478 | wxString s = m_textChange->GetValue(); | |
479 | for ( int n = 0; n < count; n++ ) | |
480 | { | |
481 | m_lbox->SetString(selections[n], s); | |
482 | } | |
483 | } | |
484 | ||
0d29a482 VZ |
485 | void ListboxWidgetsPage::OnButtonEnsureVisible(wxCommandEvent& WXUNUSED(event)) |
486 | { | |
487 | int n; | |
488 | if ( !GetValidIndexFromText(m_textEnsureVisible, &n) ) | |
489 | { | |
490 | return; | |
491 | } | |
492 | ||
493 | m_lbox->EnsureVisible(n); | |
494 | } | |
495 | ||
32b8ec41 VZ |
496 | void ListboxWidgetsPage::OnButtonDelete(wxCommandEvent& WXUNUSED(event)) |
497 | { | |
0d29a482 VZ |
498 | int n; |
499 | if ( !GetValidIndexFromText(m_textDelete, &n) ) | |
32b8ec41 VZ |
500 | { |
501 | return; | |
502 | } | |
503 | ||
504 | m_lbox->Delete(n); | |
505 | } | |
506 | ||
507 | void ListboxWidgetsPage::OnButtonDeleteSel(wxCommandEvent& WXUNUSED(event)) | |
508 | { | |
509 | wxArrayInt selections; | |
510 | int n = m_lbox->GetSelections(selections); | |
511 | while ( n > 0 ) | |
512 | { | |
513 | m_lbox->Delete(selections[--n]); | |
514 | } | |
515 | } | |
516 | ||
c02e5a31 | 517 | void ListboxWidgetsPage::OnButtonClear(wxCommandEvent& WXUNUSED(event)) |
32b8ec41 VZ |
518 | { |
519 | m_lbox->Clear(); | |
520 | } | |
521 | ||
c02e5a31 | 522 | void ListboxWidgetsPage::OnButtonAdd(wxCommandEvent& WXUNUSED(event)) |
32b8ec41 | 523 | { |
0c61716c | 524 | static unsigned int s_item = 0; |
32b8ec41 VZ |
525 | |
526 | wxString s = m_textAdd->GetValue(); | |
527 | if ( !m_textAdd->IsModified() ) | |
528 | { | |
529 | // update the default string | |
9a83f860 | 530 | m_textAdd->SetValue(wxString::Format(wxT("test item %u"), ++s_item)); |
32b8ec41 VZ |
531 | } |
532 | ||
533 | m_lbox->Append(s); | |
534 | } | |
535 | ||
536 | void ListboxWidgetsPage::OnButtonAddMany(wxCommandEvent& WXUNUSED(event)) | |
537 | { | |
538 | // "many" means 1000 here | |
0c61716c | 539 | for ( unsigned int n = 0; n < 1000; n++ ) |
32b8ec41 | 540 | { |
9a83f860 | 541 | m_lbox->Append(wxString::Format(wxT("item #%u"), n)); |
32b8ec41 VZ |
542 | } |
543 | } | |
544 | ||
c02e5a31 | 545 | void ListboxWidgetsPage::OnButtonAddSeveral(wxCommandEvent& WXUNUSED(event)) |
32b8ec41 VZ |
546 | { |
547 | wxArrayString items; | |
9a83f860 VZ |
548 | items.Add(wxT("First")); |
549 | items.Add(wxT("another one")); | |
550 | items.Add(wxT("and the last (very very very very very very very very very very long) one")); | |
32b8ec41 VZ |
551 | m_lbox->InsertItems(items, 0); |
552 | } | |
553 | ||
554 | void ListboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event) | |
555 | { | |
556 | event.Enable( (m_radioSelMode->GetSelection() != LboxSel_Single) || | |
557 | m_chkSort->GetValue() || | |
822b9009 | 558 | m_chkOwnerDraw->GetValue() || |
32b8ec41 VZ |
559 | !m_chkHScroll->GetValue() || |
560 | m_chkVScroll->GetValue() ); | |
561 | } | |
562 | ||
0d29a482 VZ |
563 | void ListboxWidgetsPage::OnUpdateUIEnsureVisibleButton(wxUpdateUIEvent& event) |
564 | { | |
565 | event.Enable(GetValidIndexFromText(m_textEnsureVisible)); | |
566 | } | |
567 | ||
32b8ec41 VZ |
568 | void ListboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent& event) |
569 | { | |
0d29a482 | 570 | event.Enable(GetValidIndexFromText(m_textDelete)); |
32b8ec41 VZ |
571 | } |
572 | ||
573 | void ListboxWidgetsPage::OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event) | |
574 | { | |
575 | wxArrayInt selections; | |
576 | event.Enable(m_lbox->GetSelections(selections) != 0); | |
577 | } | |
578 | ||
579 | void ListboxWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent& event) | |
580 | { | |
581 | event.Enable(m_lbox->GetCount() != 0); | |
582 | } | |
583 | ||
584 | void ListboxWidgetsPage::OnUpdateUIAddSeveral(wxUpdateUIEvent& event) | |
585 | { | |
586 | event.Enable(!(m_lbox->GetWindowStyle() & wxLB_SORT)); | |
587 | } | |
588 | ||
589 | void ListboxWidgetsPage::OnListbox(wxCommandEvent& event) | |
590 | { | |
a0086878 | 591 | long sel = event.GetSelection(); |
9a83f860 | 592 | m_textDelete->SetValue(wxString::Format(wxT("%ld"), sel)); |
32b8ec41 | 593 | |
a0086878 | 594 | if (event.IsSelection()) |
43b2d5e7 | 595 | { |
9a83f860 | 596 | wxLogMessage(wxT("Listbox item %ld selected"), sel); |
43b2d5e7 | 597 | } |
a0086878 | 598 | else |
43b2d5e7 | 599 | { |
9a83f860 | 600 | wxLogMessage(wxT("Listbox item %ld deselected"), sel); |
43b2d5e7 | 601 | } |
32b8ec41 VZ |
602 | } |
603 | ||
604 | void ListboxWidgetsPage::OnListboxDClick(wxCommandEvent& event) | |
605 | { | |
9a83f860 | 606 | wxLogMessage( wxT("Listbox item %d double clicked"), event.GetInt() ); |
32b8ec41 VZ |
607 | } |
608 | ||
609 | void ListboxWidgetsPage::OnCheckListbox(wxCommandEvent& event) | |
610 | { | |
9a83f860 | 611 | wxLogMessage( wxT("Listbox item %d toggled"), event.GetInt() ); |
32b8ec41 VZ |
612 | } |
613 | ||
c02e5a31 | 614 | void ListboxWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event)) |
32b8ec41 VZ |
615 | { |
616 | CreateLbox(); | |
617 | } | |
618 | ||
eecdb000 | 619 | #endif // wxUSE_LISTBOX |