]> git.saurik.com Git - wxWidgets.git/blob - samples/checklst/checklst.cpp
Applied Ryan's native wxListBox and wxCheckListBox patch
[wxWidgets.git] / samples / checklst / checklst.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: checklst.cpp
3 // Purpose: wxCheckListBox sample
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 13.11.97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #ifdef __WXMSW__
24 #include "wx/ownerdrw.h"
25 #endif
26
27 #include "wx/log.h"
28
29 #include "wx/sizer.h"
30 #include "wx/menuitem.h"
31 #include "wx/checklst.h"
32
33 #if !wxUSE_CHECKLISTBOX
34 #error "This sample can't be built without wxUSE_CHECKLISTBOX"
35 #endif // wxUSE_CHECKLISTBOX
36
37 // Define a new application type
38 class CheckListBoxApp: public wxApp
39 {
40 public:
41 bool OnInit();
42 };
43
44 // Define a new frame type
45 class CheckListBoxFrame : public wxFrame
46 {
47 public:
48 // ctor & dtor
49 CheckListBoxFrame(wxFrame *frame, const wxChar *title);
50 virtual ~CheckListBoxFrame(){};
51
52 // notifications
53 void OnQuit(wxCommandEvent& event);
54 void OnAbout(wxCommandEvent& event);
55
56 void OnCheckFirstItem(wxCommandEvent& event);
57 void OnUncheckFirstItem(wxCommandEvent& event);
58 void OnToggleFirstItem(wxCommandEvent& event);
59 void OnToggleSelection(wxCommandEvent& event);
60 void OnToggleSorting(wxCommandEvent& event);
61 void OnToggleExtended(wxCommandEvent& event);
62
63 void OnInsertItemsStart(wxCommandEvent& event);
64 void OnInsertItemsMiddle(wxCommandEvent& event);
65 void OnInsertItemsEnd(wxCommandEvent& event);
66 void OnAppendItems(wxCommandEvent& event);
67 void OnRemoveItems(wxCommandEvent& event);
68
69 void OnGetItemHeight(wxCommandEvent& event);
70 void OnGetBestSize(wxCommandEvent& event);
71
72 void OnMakeItemFirst(wxCommandEvent& event);
73
74 void OnListboxSelect(wxCommandEvent& event);
75 void OnCheckboxToggle(wxCommandEvent& event);
76 void OnListboxDblClick(wxCommandEvent& event);
77
78 void OnButtonUp(wxCommandEvent& event);
79 void OnButtonDown(wxCommandEvent& event);
80
81 private:
82 void CreateCheckListbox(long flags = 0);
83
84 void OnButtonMove(bool up);
85
86 void AdjustColour(size_t index);
87
88 wxPanel *m_panel;
89
90 wxCheckListBox *m_pListBox;
91
92 DECLARE_EVENT_TABLE()
93 };
94
95 enum
96 {
97 Menu_About = wxID_ABOUT,
98 Menu_Quit = wxID_EXIT,
99
100 Menu_CheckFirst = wxID_HIGHEST,
101 Menu_UncheckFirst,
102 Menu_ToggleFirst,
103 Menu_Selection,
104 Menu_Extended,
105 Menu_Sorting,
106 Menu_InsertItemsStart,
107 Menu_InsertItemsMiddle,
108 Menu_InsertItemsEnd,
109 Menu_AppendItems,
110 Menu_RemoveItems,
111 Menu_GetItemHeight,
112 Menu_GetBestSize,
113 Menu_MakeItemFirst,
114
115 Control_First,
116 Control_Listbox,
117
118 Btn_Up = wxID_UP,
119 Btn_Down = wxID_DOWN
120 };
121
122 BEGIN_EVENT_TABLE(CheckListBoxFrame, wxFrame)
123 EVT_MENU(Menu_About, CheckListBoxFrame::OnAbout)
124 EVT_MENU(Menu_Quit, CheckListBoxFrame::OnQuit)
125
126 EVT_MENU(Menu_CheckFirst, CheckListBoxFrame::OnCheckFirstItem)
127 EVT_MENU(Menu_UncheckFirst, CheckListBoxFrame::OnUncheckFirstItem)
128 EVT_MENU(Menu_ToggleFirst, CheckListBoxFrame::OnToggleFirstItem)
129 EVT_MENU(Menu_Selection, CheckListBoxFrame::OnToggleSelection)
130 EVT_MENU(Menu_Extended, CheckListBoxFrame::OnToggleExtended)
131 EVT_MENU(Menu_Sorting, CheckListBoxFrame::OnToggleSorting)
132
133 EVT_MENU(Menu_InsertItemsStart, CheckListBoxFrame::OnInsertItemsStart)
134 EVT_MENU(Menu_InsertItemsMiddle, CheckListBoxFrame::OnInsertItemsMiddle)
135 EVT_MENU(Menu_InsertItemsEnd, CheckListBoxFrame::OnInsertItemsEnd)
136 EVT_MENU(Menu_AppendItems, CheckListBoxFrame::OnAppendItems)
137 EVT_MENU(Menu_RemoveItems, CheckListBoxFrame::OnRemoveItems)
138
139 EVT_MENU(Menu_GetItemHeight, CheckListBoxFrame::OnGetItemHeight)
140 EVT_MENU(Menu_GetBestSize, CheckListBoxFrame::OnGetBestSize)
141
142 EVT_MENU(Menu_MakeItemFirst, CheckListBoxFrame::OnMakeItemFirst)
143
144 EVT_LISTBOX(Control_Listbox, CheckListBoxFrame::OnListboxSelect)
145 EVT_CHECKLISTBOX(Control_Listbox, CheckListBoxFrame::OnCheckboxToggle)
146 EVT_LISTBOX_DCLICK(Control_Listbox, CheckListBoxFrame::OnListboxDblClick)
147
148 EVT_BUTTON(Btn_Up, CheckListBoxFrame::OnButtonUp)
149 EVT_BUTTON(Btn_Down, CheckListBoxFrame::OnButtonDown)
150 END_EVENT_TABLE()
151
152 IMPLEMENT_APP(CheckListBoxApp);
153
154 // init our app: create windows
155 bool CheckListBoxApp::OnInit(void)
156 {
157 CheckListBoxFrame *pFrame = new CheckListBoxFrame
158 (
159 NULL,
160 _T("wxWidgets Checklistbox Sample")
161 );
162 SetTopWindow(pFrame);
163
164 return true;
165 }
166
167 // main frame constructor
168 CheckListBoxFrame::CheckListBoxFrame(wxFrame *frame,
169 const wxChar *title)
170 : wxFrame(frame, wxID_ANY, title)
171 {
172 #if wxUSE_STATUSBAR
173 // create the status line
174 const int widths[] = { -1, 60 };
175 CreateStatusBar(2);
176 SetStatusWidths(2, widths);
177 #endif // wxUSE_STATUSBAR
178
179 // Make a menubar
180 // --------------
181
182 // file submenu
183 wxMenu *menuFile = new wxMenu;
184 menuFile->Append(Menu_About, _T("&About...\tF1"));
185 menuFile->AppendSeparator();
186 menuFile->Append(Menu_Quit, _T("E&xit\tAlt-X"));
187
188 // listbox submenu
189 wxMenu *menuList = new wxMenu;
190 menuList->Append(Menu_CheckFirst, _T("Check the first item\tCtrl-C"));
191 menuList->Append(Menu_UncheckFirst, _T("Uncheck the first item\tCtrl-U"));
192 menuList->Append(Menu_ToggleFirst, _T("Toggle the first item\tCtrl-T"));
193 menuList->AppendSeparator();
194 menuList->Append(Menu_InsertItemsStart, _T("Insert some item at the beginning"));
195 menuList->Append(Menu_InsertItemsMiddle, _T("Insert some item at the middle"));
196 menuList->Append(Menu_InsertItemsEnd, _T("Insert some item at the end"));
197 menuList->Append(Menu_AppendItems, _T("Append some items\tCtrl-A"));
198 menuList->Append(Menu_RemoveItems, _T("Remove some items"));
199 menuList->AppendSeparator();
200 menuList->AppendCheckItem(Menu_Selection, _T("Multiple selection\tCtrl-M"));
201 menuList->AppendCheckItem(Menu_Extended, _T("Extended selection"));
202 menuList->AppendCheckItem(Menu_Sorting, _T("Sorting"));
203 menuList->AppendSeparator();
204 menuList->Append(Menu_GetItemHeight, _T("Get the height of an item"));
205 menuList->Append(Menu_GetBestSize, _T("Get the best size of the checklistbox control"));
206 menuList->AppendSeparator();
207 menuList->Append(Menu_MakeItemFirst, _T("Make selected item the first item"));
208
209
210 // put it all together
211 wxMenuBar *menu_bar = new wxMenuBar;
212 menu_bar->Append(menuFile, _T("&File"));
213 menu_bar->Append(menuList, _T("&List"));
214 SetMenuBar(menu_bar);
215
216 // make a panel with some controls
217 m_panel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL);
218
219 CreateCheckListbox();
220
221 // create buttons for moving the items around
222 wxButton *button1 = new wxButton(m_panel, Btn_Up);
223 wxButton *button2 = new wxButton(m_panel, Btn_Down);
224
225
226 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
227
228 mainsizer->Add( m_pListBox, 1, wxGROW|wxALL, 10 );
229
230 wxBoxSizer *bottomsizer = new wxBoxSizer( wxHORIZONTAL );
231
232 bottomsizer->Add( button1, 0, wxALL, 10 );
233 bottomsizer->Add( button2, 0, wxALL, 10 );
234
235 mainsizer->Add( bottomsizer, 0, wxCENTER );
236
237 // tell frame to make use of sizer (or constraints, if any)
238 m_panel->SetAutoLayout( true );
239 m_panel->SetSizer( mainsizer );
240
241 #ifndef __WXWINCE__
242 // don't allow frame to get smaller than what the sizers tell ye
243 mainsizer->SetSizeHints( this );
244 #endif
245
246 Show(true);
247 }
248
249 void CheckListBoxFrame::CreateCheckListbox(long flags)
250 {
251 // check list box
252 static const wxChar *aszChoices[] =
253 {
254 _T("Zeroth"),
255 _T("First"), _T("Second"), _T("Third"),
256 _T("Fourth"), _T("Fifth"), _T("Sixth"),
257 _T("Seventh"), _T("Eighth"), _T("Nineth")
258 };
259
260 wxString *astrChoices = new wxString[WXSIZEOF(aszChoices)];
261 unsigned int ui;
262 for ( ui = 0; ui < WXSIZEOF(aszChoices); ui++ )
263 astrChoices[ui] = aszChoices[ui];
264
265 m_pListBox = new wxCheckListBox
266 (
267 m_panel, // parent
268 Control_Listbox, // control id
269 wxPoint(10, 10), // listbox poistion
270 wxSize(400, 100), // listbox size
271 WXSIZEOF(aszChoices), // number of strings
272 astrChoices, // array of strings
273 flags
274 );
275
276 //m_pListBox->SetBackgroundColour(*wxGREEN);
277
278 delete [] astrChoices;
279
280 // set grey background for every second entry
281 for ( ui = 0; ui < WXSIZEOF(aszChoices); ui += 2 ) {
282 AdjustColour(ui);
283 }
284
285 m_pListBox->Check(2);
286 m_pListBox->Select(3);
287 }
288
289 void CheckListBoxFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
290 {
291 Close(true);
292 }
293
294 void CheckListBoxFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
295 {
296 wxMessageBox(wxT("Demo of wxCheckListBox control\n(c) Vadim Zeitlin 1998-2002"),
297 wxT("About wxCheckListBox"),
298 wxICON_INFORMATION, this);
299 }
300
301 void CheckListBoxFrame::OnCheckFirstItem(wxCommandEvent& WXUNUSED(event))
302 {
303 if ( !m_pListBox->IsEmpty() )
304 m_pListBox->Check(0);
305 }
306
307 void CheckListBoxFrame::OnUncheckFirstItem(wxCommandEvent& WXUNUSED(event))
308 {
309 if ( !m_pListBox->IsEmpty() )
310 m_pListBox->Check(0, false);
311 }
312
313 void CheckListBoxFrame::OnToggleFirstItem(wxCommandEvent& WXUNUSED(event))
314 {
315 if ( !m_pListBox->IsEmpty() )
316 m_pListBox->Check(0, !m_pListBox->IsChecked(0));
317 }
318
319 void CheckListBoxFrame::OnInsertItemsStart(wxCommandEvent& WXUNUSED(event))
320 {
321 static size_t s_nItem = 0;
322 wxArrayString items;
323 items.Add(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem));
324 items.Add(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem));
325 items.Add(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem));
326
327 m_pListBox->InsertItems(items, 0);//m_pListBox->GetCount());
328 }
329
330 void CheckListBoxFrame::OnInsertItemsMiddle(wxCommandEvent& WXUNUSED(event))
331 {
332 static size_t s_nItem = 0;
333 wxArrayString items;
334 items.Add(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem));
335 items.Add(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem));
336 items.Add(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem));
337
338 m_pListBox->InsertItems(items, m_pListBox->GetCount() ? 1 : 0);
339 }
340
341 void CheckListBoxFrame::OnInsertItemsEnd(wxCommandEvent& WXUNUSED(event))
342 {
343 static size_t s_nItem = 0;
344 wxArrayString items;
345 items.Add(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem));
346 items.Add(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem));
347 items.Add(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem));
348
349 m_pListBox->InsertItems(items, m_pListBox->GetCount() );
350 }
351
352 void CheckListBoxFrame::OnAppendItems(wxCommandEvent& WXUNUSED(event))
353 {
354 static size_t s_nItem = 0;
355 m_pListBox->Append(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem));
356 m_pListBox->Append(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem));
357 m_pListBox->Append(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem));
358 }
359
360 void CheckListBoxFrame::OnRemoveItems(wxCommandEvent& WXUNUSED(event))
361 {
362 if(m_pListBox->GetCount())
363 m_pListBox->Delete(0);
364 if(m_pListBox->GetCount())
365 m_pListBox->Delete(0);
366 if(m_pListBox->GetCount())
367 m_pListBox->Delete(0);
368 }
369
370 void CheckListBoxFrame::OnGetItemHeight(wxCommandEvent& WXUNUSED(event))
371 {
372 int height = m_pListBox->GetItemHeight();
373
374 wxMessageBox(wxString::Format(wxT("Height of an item is:%i"),
375 height
376 )
377 );
378 }
379
380 void CheckListBoxFrame::OnGetBestSize(wxCommandEvent& WXUNUSED(event))
381 {
382 wxSize bestSize = m_pListBox->GetBestSize();
383
384 wxMessageBox(wxString::Format(wxT("Best size of the checklistbox is:[%i,%i]"),
385 bestSize.x, bestSize.y
386 )
387 );
388 }
389
390 void CheckListBoxFrame::OnMakeItemFirst(wxCommandEvent& WXUNUSED(event))
391 {
392 if(m_pListBox->GetSelection() != -1)
393 m_pListBox->SetFirstItem(m_pListBox->GetSelection());
394 else
395 wxMessageBox(wxT("Nothing selected!"));
396 }
397
398 void CheckListBoxFrame::OnToggleSelection(wxCommandEvent& event)
399 {
400 wxSizer *sizer = m_panel->GetSizer();
401
402 sizer->Detach( m_pListBox );
403 delete m_pListBox;
404
405 CreateCheckListbox(event.IsChecked() ? wxLB_EXTENDED : 0);
406
407 sizer->Insert(0, m_pListBox, 1, wxGROW | wxALL, 10);
408
409 m_panel->Layout();
410 }
411
412 void CheckListBoxFrame::OnToggleExtended(wxCommandEvent& event)
413 {
414 wxSizer *sizer = m_panel->GetSizer();
415
416 sizer->Detach( m_pListBox );
417 delete m_pListBox;
418
419 CreateCheckListbox(event.IsChecked() ? wxLB_EXTENDED : 0);
420
421 sizer->Insert(0, m_pListBox, 1, wxGROW | wxALL, 10);
422
423 m_panel->Layout();
424 }
425
426 void CheckListBoxFrame::OnToggleSorting(wxCommandEvent& event)
427 {
428 wxSizer *sizer = m_panel->GetSizer();
429
430 sizer->Detach( m_pListBox );
431 delete m_pListBox;
432
433 CreateCheckListbox(event.IsChecked() ? wxLB_SORT : 0);
434
435 sizer->Insert(0, m_pListBox, 1, wxGROW | wxALL, 10);
436
437 m_panel->Layout();
438 }
439
440 void CheckListBoxFrame::OnListboxSelect(wxCommandEvent& event)
441 {
442 int nSel = event.GetSelection();
443 wxLogStatus(this, wxT("Item %d selected (%schecked)"), nSel,
444 m_pListBox->IsChecked(nSel) ? wxT("") : wxT("not "));
445 }
446
447 void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent& WXUNUSED(event))
448 {
449 int selection = -1;
450 if(m_pListBox->GetWindowStyle() & wxLB_EXTENDED)
451 {
452 wxArrayInt list;
453 m_pListBox->GetSelections(list);
454 if(list.Count()==1)
455 {
456 selection = list.Item(0);
457 }
458 }
459 else
460 {
461 selection = m_pListBox->GetSelection();
462 }
463
464 wxString strSelection;
465 if ( selection != -1 )
466 {
467 strSelection.Printf(wxT("Item %d double clicked"), selection);
468 }
469 else
470 {
471 strSelection = wxT("List double clicked in multiple selection mode");
472 }
473 wxMessageDialog dialog(this, strSelection, wxT("wxCheckListBox message"), wxICON_INFORMATION);
474 dialog.ShowModal();
475 }
476
477 void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent& event)
478 {
479 unsigned int nItem = event.GetInt();
480
481 wxLogStatus(this, wxT("item %d was %schecked"), nItem,
482 m_pListBox->IsChecked(nItem) ? wxT("") : wxT("un"));
483 }
484
485 void CheckListBoxFrame::OnButtonUp(wxCommandEvent& WXUNUSED(event))
486 {
487 OnButtonMove(true);
488 }
489
490 void CheckListBoxFrame::OnButtonDown(wxCommandEvent& WXUNUSED(event))
491 {
492 OnButtonMove(false);
493 }
494
495 void CheckListBoxFrame::OnButtonMove(bool up)
496 {
497 int selection = -1;
498 if(m_pListBox->GetWindowStyle() & wxLB_EXTENDED)
499 {
500 wxArrayInt list;
501 m_pListBox->GetSelections(list);
502 if(list.Count()==1)
503 {
504 selection = list.Item(0);
505 }
506 }
507 else
508 {
509 selection = m_pListBox->GetSelection();
510 }
511 if ( selection != wxNOT_FOUND )
512 {
513 wxString label = m_pListBox->GetString(selection);
514
515 int positionNew = up ? selection - 1 : selection + 2;
516 if ( positionNew < 0 || positionNew > m_pListBox->GetCount() )
517 {
518 wxLogStatus(this, wxT("Can't move this item %s"), up ? wxT("up") : wxT("down"));
519 }
520 else
521 {
522 bool wasChecked = m_pListBox->IsChecked(selection);
523
524 int positionOld = up ? selection + 1 : selection;
525
526 // insert the item
527 m_pListBox->InsertItems(1, &label, positionNew);
528
529 // and delete the old one
530 m_pListBox->Delete(positionOld);
531
532 int selectionNew = up ? positionNew : positionNew - 1;
533 m_pListBox->Check(selectionNew, wasChecked);
534 m_pListBox->SetSelection(selectionNew);
535 m_pListBox->SetFocus();
536
537 AdjustColour(selection);
538 AdjustColour(selectionNew);
539
540 wxLogStatus(this, wxT("Item moved %s"), up ? wxT("up") : wxT("down"));
541 }
542 }
543 else
544 {
545 wxLogStatus(this, wxT("Please select single item"));
546 }
547 }
548
549 // not implemented in ports other than (native) MSW yet
550 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) && !defined(__WXWINCE__)
551 void CheckListBoxFrame::AdjustColour(size_t index)
552 {
553 // even items have grey backround, odd ones - white
554 unsigned char c = index % 2 ? 255 : 200;
555 m_pListBox->GetItem(index)->SetBackgroundColour(wxColor(c, c, c));
556 }
557 #else
558 void CheckListBoxFrame::AdjustColour(size_t WXUNUSED(index))
559 {
560 }
561 #endif // wxMSW