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