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