]>
Commit | Line | Data |
---|---|---|
4f37a20b VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: lboxtest.cpp | |
3 | // Purpose: wxListBox sample | |
4 | // Author: Vadim Zeitlin | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 2000 Vadim Zeitlin | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | /* | |
11 | Current bugs: | |
12 | ||
13 | +1. horz scrollbar doesn't appear in listbox | |
14 | +2. truncating text ctrl doesn't update display | |
15 | +3. deleting last listbox item doesn't update display | |
16 | 4. text ctrl background corrupted after resize | |
17 | */ | |
18 | ||
19 | // ============================================================================ | |
20 | // declarations | |
21 | // ============================================================================ | |
22 | ||
4f37a20b VZ |
23 | // ---------------------------------------------------------------------------- |
24 | // headers | |
25 | // ---------------------------------------------------------------------------- | |
26 | ||
27 | // for compilers that support precompilation, includes "wx/wx.h". | |
28 | #include "wx/wxprec.h" | |
29 | ||
30 | #ifdef __BORLANDC__ | |
31 | #pragma hdrstop | |
32 | #endif | |
33 | ||
34 | // for all others, include the necessary headers | |
35 | #ifndef WX_PRECOMP | |
dacaa6f1 | 36 | #include "wx/wx.h" |
4f37a20b VZ |
37 | #include "wx/app.h" |
38 | #include "wx/frame.h" | |
39 | #include "wx/dcclient.h" | |
40 | ||
41 | #include "wx/button.h" | |
42 | #include "wx/checkbox.h" | |
43 | #include "wx/checklst.h" | |
44 | #include "wx/listbox.h" | |
45 | #include "wx/radiobox.h" | |
46 | #include "wx/radiobut.h" | |
47 | #include "wx/statbox.h" | |
48 | #include "wx/stattext.h" | |
49 | #include "wx/textctrl.h" | |
50 | #endif | |
51 | ||
52 | #include "wx/sizer.h" | |
53 | ||
54 | #ifdef __WXUNIVERSAL__ | |
55 | #include "wx/univ/theme.h" | |
56 | #endif // __WXUNIVERSAL__ | |
57 | ||
58 | // ---------------------------------------------------------------------------- | |
59 | // constants | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
62 | // control ids | |
63 | enum | |
64 | { | |
65 | LboxTest_Reset = 100, | |
66 | LboxTest_Create, | |
67 | LboxTest_Add, | |
68 | LboxTest_AddText, | |
69 | LboxTest_AddSeveral, | |
70 | LboxTest_AddMany, | |
71 | LboxTest_Clear, | |
b29903d4 | 72 | #if wxUSE_LOG |
4f37a20b | 73 | LboxTest_ClearLog, |
b29903d4 | 74 | #endif // wxUSE_LOG |
4f37a20b VZ |
75 | LboxTest_Change, |
76 | LboxTest_ChangeText, | |
77 | LboxTest_Delete, | |
78 | LboxTest_DeleteText, | |
79 | LboxTest_DeleteSel, | |
6be6127e | 80 | LboxTest_DeselectAll, |
4f37a20b VZ |
81 | LboxTest_Listbox, |
82 | LboxTest_Quit | |
83 | }; | |
84 | ||
85 | // ---------------------------------------------------------------------------- | |
86 | // our classes | |
87 | // ---------------------------------------------------------------------------- | |
88 | ||
89 | // Define a new application type, each program should derive a class from wxApp | |
90 | class LboxTestApp : public wxApp | |
91 | { | |
92 | public: | |
93 | // override base class virtuals | |
94 | // ---------------------------- | |
95 | ||
96 | // this one is called on application startup and is a good place for the app | |
97 | // initialization (doing it here and not in the ctor allows to have an error | |
98 | // return: if OnInit() returns false, the application terminates) | |
99 | virtual bool OnInit(); | |
100 | }; | |
101 | ||
102 | // Define a new frame type: this is going to be our main frame | |
103 | class LboxTestFrame : public wxFrame | |
104 | { | |
105 | public: | |
106 | // ctor(s) and dtor | |
107 | LboxTestFrame(const wxString& title); | |
108 | virtual ~LboxTestFrame(); | |
109 | ||
110 | protected: | |
111 | // event handlers | |
112 | void OnButtonReset(wxCommandEvent& event); | |
113 | void OnButtonCreate(wxCommandEvent& event); | |
114 | void OnButtonChange(wxCommandEvent& event); | |
115 | void OnButtonDelete(wxCommandEvent& event); | |
116 | void OnButtonDeleteSel(wxCommandEvent& event); | |
6be6127e | 117 | void OnButtonDeselectAll(wxCommandEvent& event); |
4f37a20b | 118 | void OnButtonClear(wxCommandEvent& event); |
6be6127e | 119 | |
b29903d4 | 120 | #if wxUSE_LOG |
4f37a20b | 121 | void OnButtonClearLog(wxCommandEvent& event); |
b29903d4 | 122 | #endif // wxUSE_LOG |
4f37a20b VZ |
123 | void OnButtonAdd(wxCommandEvent& event); |
124 | void OnButtonAddSeveral(wxCommandEvent& event); | |
125 | void OnButtonAddMany(wxCommandEvent& event); | |
126 | void OnButtonQuit(wxCommandEvent& event); | |
127 | ||
128 | void OnListbox(wxCommandEvent& event); | |
129 | void OnListboxDClick(wxCommandEvent& event); | |
c00fed0e | 130 | void OnListboxRDown(wxMouseEvent& event); |
4f37a20b VZ |
131 | |
132 | void OnCheckOrRadioBox(wxCommandEvent& event); | |
133 | ||
134 | void OnUpdateUIAddSeveral(wxUpdateUIEvent& event); | |
135 | void OnUpdateUICreateButton(wxUpdateUIEvent& event); | |
136 | void OnUpdateUIClearButton(wxUpdateUIEvent& event); | |
137 | void OnUpdateUIDeleteButton(wxUpdateUIEvent& event); | |
138 | void OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event); | |
6be6127e | 139 | void OnUpdateUIDeselectAllButton(wxUpdateUIEvent& event); |
4f37a20b VZ |
140 | |
141 | // reset the listbox parameters | |
142 | void Reset(); | |
143 | ||
144 | // (re)create the listbox | |
145 | void CreateLbox(); | |
146 | ||
147 | // listbox parameters | |
148 | // ------------------ | |
149 | ||
150 | // the selection mode | |
151 | enum LboxSelection | |
152 | { | |
153 | LboxSel_Single, | |
154 | LboxSel_Extended, | |
155 | LboxSel_Multiple | |
156 | } m_lboxSelMode; | |
157 | ||
158 | // should it be sorted? | |
159 | bool m_sorted; | |
160 | ||
161 | // should it have horz scroll/vert scrollbar permanently shown? | |
162 | bool m_horzScroll, | |
163 | m_vertScrollAlways; | |
164 | ||
165 | // should the recreate button be enabled? | |
166 | bool m_dirty; | |
167 | ||
168 | // the controls | |
169 | // ------------ | |
170 | ||
171 | // the sel mode radiobox | |
172 | wxRadioBox *m_radioSelMode; | |
173 | ||
174 | // the checkboxes | |
175 | wxCheckBox *m_chkSort, | |
176 | *m_chkHScroll, | |
177 | *m_chkVScroll; | |
178 | ||
179 | // the listbox itself and the sizer it is in | |
180 | wxListBox *m_lbox; | |
181 | wxSizer *m_sizerLbox; | |
182 | ||
b4deaacb VZ |
183 | // panel the controls such as the listbox are in |
184 | wxPanel* m_panel; | |
185 | ||
b29903d4 | 186 | #if wxUSE_LOG |
4f37a20b VZ |
187 | // the listbox for logging messages |
188 | wxListBox *m_lboxLog; | |
b29903d4 | 189 | #endif // wxUSE_LOG |
4f37a20b VZ |
190 | |
191 | // the text entries for "Add/change string" and "Delete" buttons | |
192 | wxTextCtrl *m_textAdd, | |
193 | *m_textChange, | |
194 | *m_textDelete; | |
195 | ||
196 | private: | |
b29903d4 | 197 | #if wxUSE_LOG |
4f37a20b VZ |
198 | // the log target we use to redirect messages to the listbox |
199 | wxLog *m_logTarget; | |
b29903d4 | 200 | #endif // wxUSE_LOG |
4f37a20b | 201 | |
be5a51fb | 202 | // any class wishing to process wxWidgets events must use this macro |
4f37a20b VZ |
203 | DECLARE_EVENT_TABLE() |
204 | }; | |
205 | ||
b29903d4 | 206 | #if wxUSE_LOG |
4f37a20b VZ |
207 | // A log target which just redirects the messages to a listbox |
208 | class LboxLogger : public wxLog | |
209 | { | |
210 | public: | |
211 | LboxLogger(wxListBox *lbox, wxLog *logOld) | |
212 | { | |
213 | m_lbox = lbox; | |
214 | //m_lbox->Disable(); -- looks ugly under MSW | |
215 | m_logOld = logOld; | |
216 | } | |
217 | ||
218 | virtual ~LboxLogger() | |
219 | { | |
220 | wxLog::SetActiveTarget(m_logOld); | |
221 | } | |
222 | ||
223 | private: | |
224 | // implement sink functions | |
225 | virtual void DoLog(wxLogLevel level, const wxChar *szString, time_t t) | |
226 | { | |
227 | // don't put trace messages into listbox or we can get into infinite | |
228 | // recursion | |
229 | if ( level == wxLOG_Trace ) | |
230 | { | |
231 | if ( m_logOld ) | |
232 | { | |
233 | // cast is needed to call protected method | |
234 | ((LboxLogger *)m_logOld)->DoLog(level, szString, t); | |
235 | } | |
236 | } | |
237 | else | |
238 | { | |
239 | wxLog::DoLog(level, szString, t); | |
240 | } | |
241 | } | |
242 | ||
d1f47235 | 243 | virtual void DoLogString(const wxChar *szString, time_t WXUNUSED(t)) |
4f37a20b VZ |
244 | { |
245 | wxString msg; | |
246 | TimeStamp(&msg); | |
247 | msg += szString; | |
248 | #ifdef __WXUNIVERSAL__ | |
249 | m_lbox->AppendAndEnsureVisible(msg); | |
250 | #else // other ports don't have this method yet | |
251 | m_lbox->Append(msg); | |
252 | ||
253 | // SetFirstItem() isn't implemented in wxGTK | |
254 | #ifndef __WXGTK__ | |
255 | m_lbox->SetFirstItem(m_lbox->GetCount() - 1); | |
256 | #endif | |
257 | #endif | |
258 | } | |
259 | ||
260 | // the control we use | |
261 | wxListBox *m_lbox; | |
262 | ||
263 | // the old log target | |
264 | wxLog *m_logOld; | |
265 | }; | |
b29903d4 | 266 | #endif // wxUSE_LOG |
4f37a20b VZ |
267 | |
268 | // ---------------------------------------------------------------------------- | |
269 | // misc macros | |
270 | // ---------------------------------------------------------------------------- | |
271 | ||
272 | IMPLEMENT_APP(LboxTestApp) | |
273 | ||
4f37a20b VZ |
274 | // ---------------------------------------------------------------------------- |
275 | // event tables | |
276 | // ---------------------------------------------------------------------------- | |
277 | ||
278 | BEGIN_EVENT_TABLE(LboxTestFrame, wxFrame) | |
279 | EVT_BUTTON(LboxTest_Reset, LboxTestFrame::OnButtonReset) | |
280 | EVT_BUTTON(LboxTest_Create, LboxTestFrame::OnButtonCreate) | |
281 | EVT_BUTTON(LboxTest_Change, LboxTestFrame::OnButtonChange) | |
282 | EVT_BUTTON(LboxTest_Delete, LboxTestFrame::OnButtonDelete) | |
283 | EVT_BUTTON(LboxTest_DeleteSel, LboxTestFrame::OnButtonDeleteSel) | |
6be6127e | 284 | EVT_BUTTON(LboxTest_DeselectAll, LboxTestFrame::OnButtonDeselectAll) |
4f37a20b | 285 | EVT_BUTTON(LboxTest_Clear, LboxTestFrame::OnButtonClear) |
b29903d4 | 286 | #if wxUSE_LOG |
4f37a20b | 287 | EVT_BUTTON(LboxTest_ClearLog, LboxTestFrame::OnButtonClearLog) |
b29903d4 | 288 | #endif // wxUSE_LOG |
4f37a20b VZ |
289 | EVT_BUTTON(LboxTest_Add, LboxTestFrame::OnButtonAdd) |
290 | EVT_BUTTON(LboxTest_AddSeveral, LboxTestFrame::OnButtonAddSeveral) | |
291 | EVT_BUTTON(LboxTest_AddMany, LboxTestFrame::OnButtonAddMany) | |
292 | EVT_BUTTON(LboxTest_Quit, LboxTestFrame::OnButtonQuit) | |
293 | ||
294 | EVT_TEXT_ENTER(LboxTest_AddText, LboxTestFrame::OnButtonAdd) | |
295 | EVT_TEXT_ENTER(LboxTest_DeleteText, LboxTestFrame::OnButtonDelete) | |
296 | ||
297 | EVT_UPDATE_UI_RANGE(LboxTest_Reset, LboxTest_Create, | |
298 | LboxTestFrame::OnUpdateUICreateButton) | |
299 | ||
300 | EVT_UPDATE_UI(LboxTest_AddSeveral, LboxTestFrame::OnUpdateUIAddSeveral) | |
301 | EVT_UPDATE_UI(LboxTest_Clear, LboxTestFrame::OnUpdateUIClearButton) | |
302 | EVT_UPDATE_UI(LboxTest_DeleteText, LboxTestFrame::OnUpdateUIClearButton) | |
303 | EVT_UPDATE_UI(LboxTest_Delete, LboxTestFrame::OnUpdateUIDeleteButton) | |
304 | EVT_UPDATE_UI(LboxTest_Change, LboxTestFrame::OnUpdateUIDeleteSelButton) | |
305 | EVT_UPDATE_UI(LboxTest_ChangeText, LboxTestFrame::OnUpdateUIDeleteSelButton) | |
306 | EVT_UPDATE_UI(LboxTest_DeleteSel, LboxTestFrame::OnUpdateUIDeleteSelButton) | |
6be6127e | 307 | EVT_UPDATE_UI(LboxTest_DeselectAll, LboxTestFrame::OnUpdateUIDeselectAllButton) |
4f37a20b VZ |
308 | |
309 | EVT_LISTBOX(LboxTest_Listbox, LboxTestFrame::OnListbox) | |
11180f77 WS |
310 | EVT_LISTBOX_DCLICK(wxID_ANY, LboxTestFrame::OnListboxDClick) |
311 | EVT_CHECKBOX(wxID_ANY, LboxTestFrame::OnCheckOrRadioBox) | |
312 | EVT_RADIOBOX(wxID_ANY, LboxTestFrame::OnCheckOrRadioBox) | |
4f37a20b VZ |
313 | END_EVENT_TABLE() |
314 | ||
315 | // ============================================================================ | |
316 | // implementation | |
317 | // ============================================================================ | |
318 | ||
319 | // ---------------------------------------------------------------------------- | |
320 | // app class | |
321 | // ---------------------------------------------------------------------------- | |
322 | ||
323 | bool LboxTestApp::OnInit() | |
324 | { | |
45e6e6f8 VZ |
325 | if ( !wxApp::OnInit() ) |
326 | return false; | |
327 | ||
4f37a20b VZ |
328 | wxFrame *frame = new LboxTestFrame(_T("wxListBox sample")); |
329 | frame->Show(); | |
330 | ||
b29903d4 | 331 | #if wxUSE_LOG |
4f37a20b VZ |
332 | //wxLog::AddTraceMask(_T("listbox")); |
333 | wxLog::AddTraceMask(_T("scrollbar")); | |
b29903d4 | 334 | #endif // wxUSE_LOG |
4f37a20b | 335 | |
11180f77 | 336 | return true; |
4f37a20b VZ |
337 | } |
338 | ||
339 | // ---------------------------------------------------------------------------- | |
340 | // top level frame class | |
341 | // ---------------------------------------------------------------------------- | |
342 | ||
343 | LboxTestFrame::LboxTestFrame(const wxString& title) | |
11180f77 | 344 | : wxFrame(NULL, wxID_ANY, title, wxPoint(100, 100)) |
4f37a20b VZ |
345 | { |
346 | // init everything | |
11180f77 | 347 | m_dirty = false; |
4f37a20b VZ |
348 | m_radioSelMode = (wxRadioBox *)NULL; |
349 | ||
350 | m_chkVScroll = | |
351 | m_chkHScroll = | |
352 | m_chkSort = (wxCheckBox *)NULL; | |
353 | ||
b29903d4 WS |
354 | m_lbox = (wxListBox *)NULL; |
355 | #if wxUSE_LOG | |
4f37a20b | 356 | m_lboxLog = (wxListBox *)NULL; |
b29903d4 | 357 | #endif // wxUSE_LOG |
4f37a20b VZ |
358 | m_sizerLbox = (wxSizer *)NULL; |
359 | ||
b29903d4 | 360 | #if wxUSE_LOG |
4f37a20b | 361 | m_logTarget = (wxLog *)NULL; |
b29903d4 | 362 | #endif // wxUSE_LOG |
4f37a20b | 363 | |
b4deaacb | 364 | m_panel = new wxPanel(this, wxID_ANY); |
4f37a20b VZ |
365 | |
366 | /* | |
367 | What we create here is a frame having 3 panes: the explanatory pane to | |
368 | the left allowing to set the listbox styles and recreate the control, | |
369 | the pane containing the listbox itself and the lower pane containing | |
370 | the buttons which allow to add/change/delete strings to/from it. | |
371 | */ | |
372 | wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL), | |
373 | *sizerUp = new wxBoxSizer(wxHORIZONTAL), | |
374 | *sizerLeft, | |
375 | *sizerRight = new wxBoxSizer(wxVERTICAL); | |
376 | ||
377 | // upper left pane | |
378 | static const wxString modes[] = | |
379 | { | |
380 | _T("single"), | |
381 | _T("extended"), | |
382 | _T("multiple"), | |
383 | }; | |
384 | ||
b4deaacb VZ |
385 | wxStaticBox *box = new wxStaticBox(m_panel, wxID_ANY, _T("&Set listbox parameters")); |
386 | m_radioSelMode = new wxRadioBox(m_panel, wxID_ANY, _T("Selection &mode:"), | |
4f37a20b VZ |
387 | wxDefaultPosition, wxDefaultSize, |
388 | WXSIZEOF(modes), modes, | |
389 | 1, wxRA_SPECIFY_COLS); | |
390 | ||
b4deaacb VZ |
391 | m_chkVScroll = new wxCheckBox(m_panel, wxID_ANY, _T("Always show &vertical scrollbar")); |
392 | m_chkHScroll = new wxCheckBox(m_panel, wxID_ANY, _T("Show &horizontal scrollbar")); | |
393 | m_chkSort = new wxCheckBox(m_panel, wxID_ANY, _T("&Sort items")); | |
4f37a20b VZ |
394 | |
395 | sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL); | |
396 | ||
397 | sizerLeft->Add(m_chkVScroll, 0, wxLEFT | wxRIGHT, 5); | |
398 | sizerLeft->Add(m_chkHScroll, 0, wxLEFT | wxRIGHT, 5); | |
399 | sizerLeft->Add(m_chkSort, 0, wxLEFT | wxRIGHT, 5); | |
400 | sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer | |
401 | sizerLeft->Add(m_radioSelMode, 0, wxGROW | wxALL, 5); | |
402 | ||
403 | wxSizer *sizerBtn = new wxBoxSizer(wxHORIZONTAL); | |
b4deaacb | 404 | wxButton *btn = new wxButton(m_panel, LboxTest_Reset, _T("&Reset")); |
4f37a20b | 405 | sizerBtn->Add(btn, 0, wxLEFT | wxRIGHT, 5); |
b4deaacb | 406 | btn = new wxButton(m_panel, LboxTest_Create, _T("&Create")); |
4f37a20b VZ |
407 | sizerBtn->Add(btn, 0, wxLEFT | wxRIGHT, 5); |
408 | sizerLeft->Add(sizerBtn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15); | |
409 | ||
410 | // middle pane | |
b4deaacb | 411 | wxStaticBox *box2 = new wxStaticBox(m_panel, wxID_ANY, _T("&Change listbox contents")); |
4f37a20b VZ |
412 | wxSizer *sizerMiddle = new wxStaticBoxSizer(box2, wxVERTICAL); |
413 | ||
414 | wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL); | |
b4deaacb VZ |
415 | btn = new wxButton(m_panel, LboxTest_Add, _T("&Add this string")); |
416 | m_textAdd = new wxTextCtrl(m_panel, LboxTest_AddText, _T("test item 0")); | |
4f37a20b VZ |
417 | sizerRow->Add(btn, 0, wxRIGHT, 5); |
418 | sizerRow->Add(m_textAdd, 1, wxLEFT, 5); | |
419 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
420 | ||
b4deaacb | 421 | btn = new wxButton(m_panel, LboxTest_AddSeveral, _T("&Insert a few strings")); |
4f37a20b VZ |
422 | sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); |
423 | ||
b4deaacb | 424 | btn = new wxButton(m_panel, LboxTest_AddMany, _T("Add &many strings")); |
4f37a20b VZ |
425 | sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); |
426 | ||
427 | sizerRow = new wxBoxSizer(wxHORIZONTAL); | |
b4deaacb VZ |
428 | btn = new wxButton(m_panel, LboxTest_Change, _T("C&hange current")); |
429 | m_textChange = new wxTextCtrl(m_panel, LboxTest_ChangeText, wxEmptyString); | |
4f37a20b VZ |
430 | sizerRow->Add(btn, 0, wxRIGHT, 5); |
431 | sizerRow->Add(m_textChange, 1, wxLEFT, 5); | |
432 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
433 | ||
434 | sizerRow = new wxBoxSizer(wxHORIZONTAL); | |
b4deaacb VZ |
435 | btn = new wxButton(m_panel, LboxTest_Delete, _T("&Delete this item")); |
436 | m_textDelete = new wxTextCtrl(m_panel, LboxTest_DeleteText, wxEmptyString); | |
4f37a20b VZ |
437 | sizerRow->Add(btn, 0, wxRIGHT, 5); |
438 | sizerRow->Add(m_textDelete, 1, wxLEFT, 5); | |
439 | sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); | |
440 | ||
b4deaacb | 441 | btn = new wxButton(m_panel, LboxTest_DeleteSel, _T("Delete &selection")); |
4f37a20b VZ |
442 | sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); |
443 | ||
6be6127e VZ |
444 | btn = new wxButton(m_panel, LboxTest_DeselectAll, _T("Deselect All")); |
445 | sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); | |
446 | ||
b4deaacb | 447 | btn = new wxButton(m_panel, LboxTest_Clear, _T("&Clear")); |
4f37a20b VZ |
448 | sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); |
449 | ||
450 | // right pane | |
4f37a20b VZ |
451 | sizerRight->SetMinSize(250, 0); |
452 | m_sizerLbox = sizerRight; // save it to modify it later | |
453 | ||
454 | // the 3 panes panes compose the upper part of the window | |
455 | sizerUp->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10); | |
456 | sizerUp->Add(sizerMiddle, 1, wxGROW | wxALL, 10); | |
457 | sizerUp->Add(sizerRight, 1, wxGROW | (wxALL & ~wxRIGHT), 10); | |
458 | ||
459 | // the lower one only has the log listbox and a button to clear it | |
b29903d4 | 460 | #if wxUSE_LOG |
4f37a20b VZ |
461 | wxSizer *sizerDown = new wxStaticBoxSizer |
462 | ( | |
b4deaacb | 463 | new wxStaticBox(m_panel, wxID_ANY, _T("&Log window")), |
4f37a20b VZ |
464 | wxVERTICAL |
465 | ); | |
b4deaacb | 466 | m_lboxLog = new wxListBox(m_panel, wxID_ANY); |
4f37a20b | 467 | sizerDown->Add(m_lboxLog, 1, wxGROW | wxALL, 5); |
b29903d4 WS |
468 | #else |
469 | wxSizer *sizerDown = new wxBoxSizer(wxVERTICAL); | |
470 | #endif // wxUSE_LOG | |
4f37a20b | 471 | wxBoxSizer *sizerBtns = new wxBoxSizer(wxHORIZONTAL); |
b29903d4 | 472 | #if wxUSE_LOG |
b4deaacb | 473 | btn = new wxButton(m_panel, LboxTest_ClearLog, _T("Clear &log")); |
4f37a20b VZ |
474 | sizerBtns->Add(btn); |
475 | sizerBtns->Add(10, 0); // spacer | |
b29903d4 | 476 | #endif // wxUSE_LOG |
b4deaacb | 477 | btn = new wxButton(m_panel, LboxTest_Quit, _T("E&xit")); |
4f37a20b VZ |
478 | sizerBtns->Add(btn); |
479 | sizerDown->Add(sizerBtns, 0, wxALL | wxALIGN_RIGHT, 5); | |
480 | ||
481 | // put everything together | |
482 | sizerTop->Add(sizerUp, 1, wxGROW | (wxALL & ~wxBOTTOM), 10); | |
483 | sizerTop->Add(0, 5, 0, wxGROW); // spacer in between | |
484 | sizerTop->Add(sizerDown, 0, wxGROW | (wxALL & ~wxTOP), 10); | |
485 | ||
b4deaacb | 486 | // final initialization and create the listbox |
4f37a20b | 487 | Reset(); |
b4deaacb | 488 | CreateLbox(); |
4f37a20b | 489 | |
b4deaacb | 490 | m_panel->SetSizer(sizerTop); |
4f37a20b VZ |
491 | |
492 | sizerTop->Fit(this); | |
493 | sizerTop->SetSizeHints(this); | |
494 | ||
b29903d4 | 495 | #if wxUSE_LOG |
4f37a20b VZ |
496 | // now that everything is created we can redirect the log messages to the |
497 | // listbox | |
498 | m_logTarget = new LboxLogger(m_lboxLog, wxLog::GetActiveTarget()); | |
499 | wxLog::SetActiveTarget(m_logTarget); | |
b29903d4 | 500 | #endif // wxUSE_LOG |
4f37a20b VZ |
501 | } |
502 | ||
503 | LboxTestFrame::~LboxTestFrame() | |
504 | { | |
b29903d4 | 505 | #if wxUSE_LOG |
4f37a20b | 506 | delete m_logTarget; |
b29903d4 | 507 | #endif // wxUSE_LOG |
4f37a20b VZ |
508 | } |
509 | ||
510 | // ---------------------------------------------------------------------------- | |
511 | // operations | |
512 | // ---------------------------------------------------------------------------- | |
513 | ||
514 | void LboxTestFrame::Reset() | |
515 | { | |
516 | if ( m_radioSelMode->GetSelection() == LboxSel_Single && | |
517 | !m_chkSort->GetValue() && | |
518 | m_chkHScroll->GetValue() && | |
519 | !m_chkVScroll->GetValue() ) | |
520 | { | |
521 | // nothing to do | |
522 | return; | |
523 | } | |
524 | ||
525 | m_radioSelMode->SetSelection(LboxSel_Single); | |
11180f77 WS |
526 | m_chkSort->SetValue(false); |
527 | m_chkHScroll->SetValue(true); | |
528 | m_chkVScroll->SetValue(false); | |
4f37a20b | 529 | |
11180f77 | 530 | m_dirty = true; |
4f37a20b VZ |
531 | } |
532 | ||
533 | void LboxTestFrame::CreateLbox() | |
534 | { | |
535 | int flags = 0; | |
536 | switch ( m_radioSelMode->GetSelection() ) | |
537 | { | |
538 | default: | |
539 | wxFAIL_MSG( _T("unexpected radio box selection") ); | |
540 | ||
541 | case LboxSel_Single: flags |= wxLB_SINGLE; break; | |
542 | case LboxSel_Extended: flags |= wxLB_EXTENDED; break; | |
543 | case LboxSel_Multiple: flags |= wxLB_MULTIPLE; break; | |
544 | } | |
545 | ||
546 | if ( m_chkVScroll->GetValue() ) | |
547 | flags |= wxLB_ALWAYS_SB; | |
548 | if ( m_chkHScroll->GetValue() ) | |
549 | flags |= wxLB_HSCROLL; | |
550 | if ( m_chkSort->GetValue() ) | |
551 | flags |= wxLB_SORT; | |
552 | ||
553 | wxArrayString items; | |
b4deaacb VZ |
554 | |
555 | if ( m_lbox ) // cache old items to restore later if listbox existed | |
556 | { | |
023e504b JS |
557 | int count = m_lbox->GetCount(); |
558 | for ( int n = 0; n < count; n++ ) | |
4f37a20b | 559 | { |
023e504b | 560 | items.Add(m_lbox->GetString(n)); |
4f37a20b VZ |
561 | } |
562 | ||
023e504b JS |
563 | m_sizerLbox->Detach(m_lbox); |
564 | delete m_lbox; | |
b4deaacb | 565 | } |
023e504b | 566 | |
b4deaacb | 567 | m_lbox = new wxListBox(m_panel, LboxTest_Listbox, |
4f37a20b VZ |
568 | wxDefaultPosition, wxDefaultSize, |
569 | 0, NULL, | |
570 | flags); | |
b4deaacb | 571 | |
4f37a20b VZ |
572 | m_lbox->Set(items); |
573 | m_sizerLbox->Add(m_lbox, 1, wxGROW | wxALL, 5); | |
574 | m_sizerLbox->Layout(); | |
575 | ||
11180f77 | 576 | m_dirty = false; |
b4deaacb VZ |
577 | |
578 | m_lbox->Connect(wxEVT_RIGHT_DOWN, | |
579 | wxMouseEventHandler(LboxTestFrame::OnListboxRDown), NULL, this); | |
4f37a20b VZ |
580 | } |
581 | ||
582 | // ---------------------------------------------------------------------------- | |
583 | // event handlers | |
584 | // ---------------------------------------------------------------------------- | |
585 | ||
586 | void LboxTestFrame::OnButtonQuit(wxCommandEvent& WXUNUSED(event)) | |
587 | { | |
588 | Close(); | |
589 | } | |
590 | ||
591 | void LboxTestFrame::OnButtonReset(wxCommandEvent& WXUNUSED(event)) | |
592 | { | |
593 | Reset(); | |
594 | } | |
595 | ||
596 | void LboxTestFrame::OnButtonCreate(wxCommandEvent& WXUNUSED(event)) | |
597 | { | |
598 | CreateLbox(); | |
599 | } | |
600 | ||
601 | void LboxTestFrame::OnButtonChange(wxCommandEvent& WXUNUSED(event)) | |
602 | { | |
603 | wxArrayInt selections; | |
604 | int count = m_lbox->GetSelections(selections); | |
605 | wxString s = m_textChange->GetValue(); | |
606 | for ( int n = 0; n < count; n++ ) | |
607 | { | |
608 | m_lbox->SetString(selections[n], s); | |
609 | } | |
610 | } | |
611 | ||
612 | void LboxTestFrame::OnButtonDelete(wxCommandEvent& WXUNUSED(event)) | |
613 | { | |
614 | unsigned long n; | |
615 | if ( !m_textDelete->GetValue().ToULong(&n) || | |
616 | (n >= (unsigned)m_lbox->GetCount()) ) | |
617 | { | |
618 | return; | |
619 | } | |
620 | ||
621 | m_lbox->Delete(n); | |
622 | } | |
623 | ||
624 | void LboxTestFrame::OnButtonDeleteSel(wxCommandEvent& WXUNUSED(event)) | |
625 | { | |
626 | wxArrayInt selections; | |
627 | int n = m_lbox->GetSelections(selections); | |
628 | while ( n > 0 ) | |
629 | { | |
630 | m_lbox->Delete(selections[--n]); | |
631 | } | |
632 | } | |
633 | ||
6be6127e VZ |
634 | void LboxTestFrame::OnButtonDeselectAll(wxCommandEvent& WXUNUSED(event)) |
635 | { | |
636 | m_lbox->SetSelection(-1); | |
637 | } | |
638 | ||
87728739 | 639 | void LboxTestFrame::OnButtonClear(wxCommandEvent& WXUNUSED(event)) |
4f37a20b VZ |
640 | { |
641 | m_lbox->Clear(); | |
642 | } | |
643 | ||
b29903d4 | 644 | #if wxUSE_LOG |
87728739 | 645 | void LboxTestFrame::OnButtonClearLog(wxCommandEvent& WXUNUSED(event)) |
4f37a20b VZ |
646 | { |
647 | m_lboxLog->Clear(); | |
648 | } | |
b29903d4 | 649 | #endif // wxUSE_LOG |
4f37a20b | 650 | |
87728739 | 651 | void LboxTestFrame::OnButtonAdd(wxCommandEvent& WXUNUSED(event)) |
4f37a20b | 652 | { |
b143cf70 | 653 | static unsigned s_item = 0; |
4f37a20b VZ |
654 | |
655 | wxString s = m_textAdd->GetValue(); | |
656 | if ( !m_textAdd->IsModified() ) | |
657 | { | |
658 | // update the default string | |
659 | m_textAdd->SetValue(wxString::Format(_T("test item %u"), ++s_item)); | |
660 | } | |
661 | ||
662 | m_lbox->Append(s); | |
663 | } | |
664 | ||
665 | void LboxTestFrame::OnButtonAddMany(wxCommandEvent& WXUNUSED(event)) | |
666 | { | |
667 | // "many" means 1000 here | |
b143cf70 | 668 | for ( unsigned n = 0; n < 1000; n++ ) |
4f37a20b VZ |
669 | { |
670 | m_lbox->Append(wxString::Format(_T("item #%u"), n)); | |
671 | } | |
672 | } | |
673 | ||
87728739 | 674 | void LboxTestFrame::OnButtonAddSeveral(wxCommandEvent& WXUNUSED(event)) |
4f37a20b VZ |
675 | { |
676 | wxArrayString items; | |
677 | items.Add(_T("First")); | |
678 | items.Add(_T("another one")); | |
679 | items.Add(_T("and the last (very very very very very very very very very very long) one")); | |
680 | m_lbox->InsertItems(items, 0); | |
681 | } | |
682 | ||
683 | void LboxTestFrame::OnUpdateUICreateButton(wxUpdateUIEvent& event) | |
684 | { | |
685 | event.Enable(m_dirty); | |
686 | } | |
687 | ||
688 | void LboxTestFrame::OnUpdateUIDeleteButton(wxUpdateUIEvent& event) | |
689 | { | |
690 | unsigned long n; | |
691 | event.Enable(m_textDelete->GetValue().ToULong(&n) && | |
692 | (n < (unsigned)m_lbox->GetCount())); | |
693 | } | |
694 | ||
695 | void LboxTestFrame::OnUpdateUIDeleteSelButton(wxUpdateUIEvent& event) | |
696 | { | |
697 | wxArrayInt selections; | |
698 | event.Enable(m_lbox->GetSelections(selections) != 0); | |
6be6127e VZ |
699 | } |
700 | ||
701 | void LboxTestFrame::OnUpdateUIDeselectAllButton(wxUpdateUIEvent& event) | |
702 | { | |
703 | wxArrayInt selections; | |
704 | event.Enable(m_lbox->GetSelections(selections) != 0); | |
4f37a20b VZ |
705 | } |
706 | ||
707 | void LboxTestFrame::OnUpdateUIClearButton(wxUpdateUIEvent& event) | |
708 | { | |
709 | event.Enable(m_lbox->GetCount() != 0); | |
710 | } | |
711 | ||
712 | void LboxTestFrame::OnUpdateUIAddSeveral(wxUpdateUIEvent& event) | |
713 | { | |
714 | event.Enable(!(m_lbox->GetWindowStyle() & wxLB_SORT)); | |
715 | } | |
716 | ||
717 | void LboxTestFrame::OnListbox(wxCommandEvent& event) | |
718 | { | |
719 | int sel = event.GetInt(); | |
dacaa6f1 | 720 | m_textDelete->SetValue(wxString::Format(_T("%d"), sel)); |
4f37a20b VZ |
721 | |
722 | wxLogMessage(_T("Listbox item %d selected"), sel); | |
723 | } | |
724 | ||
725 | void LboxTestFrame::OnListboxDClick(wxCommandEvent& event) | |
726 | { | |
dacaa6f1 JS |
727 | int sel = event.GetInt(); |
728 | wxLogMessage(_T("Listbox item %d double clicked"), sel); | |
4f37a20b VZ |
729 | } |
730 | ||
c00fed0e VZ |
731 | void LboxTestFrame::OnListboxRDown(wxMouseEvent& event) |
732 | { | |
733 | int item = m_lbox->HitTest(event.GetPosition()); | |
734 | ||
735 | if ( item != wxNOT_FOUND ) | |
736 | wxLogMessage(_T("Listbox item %d right clicked"), item); | |
737 | else | |
738 | wxLogMessage(_T("Listbox right clicked but no item clicked upon")); | |
739 | ||
740 | event.Skip(); | |
741 | } | |
742 | ||
87728739 | 743 | void LboxTestFrame::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event)) |
4f37a20b | 744 | { |
11180f77 | 745 | m_dirty = true; |
4f37a20b VZ |
746 | } |
747 |