]>
git.saurik.com Git - wxWidgets.git/blob - samples/checklst/checklst.cpp
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxCheckListBox sample
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
24 #include "wx/ownerdrw.h"
30 #include "wx/menuitem.h"
31 #include "wx/checklst.h"
33 #if !wxUSE_CHECKLISTBOX
34 #error "This sample can't be built without wxUSE_CHECKLISTBOX"
35 #endif // wxUSE_CHECKLISTBOX
37 // Define a new application type
38 class CheckListBoxApp
: public wxApp
44 // Define a new frame type
45 class CheckListBoxFrame
: public wxFrame
49 CheckListBoxFrame(wxFrame
*frame
, const wxChar
*title
);
50 virtual ~CheckListBoxFrame(){};
53 void OnQuit(wxCommandEvent
& event
);
54 void OnAbout(wxCommandEvent
& event
);
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
);
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
);
69 void OnGetItemHeight(wxCommandEvent
& event
);
70 void OnGetBestSize(wxCommandEvent
& event
);
72 void OnMakeItemFirst(wxCommandEvent
& event
);
74 void OnListboxSelect(wxCommandEvent
& event
);
75 void OnCheckboxToggle(wxCommandEvent
& event
);
76 void OnListboxDblClick(wxCommandEvent
& event
);
78 void OnButtonUp(wxCommandEvent
& event
);
79 void OnButtonDown(wxCommandEvent
& event
);
82 void CreateCheckListbox(long flags
= 0);
84 void OnButtonMove(bool up
);
86 void AdjustColour(size_t index
);
90 wxCheckListBox
*m_pListBox
;
97 Menu_About
= wxID_ABOUT
,
98 Menu_Quit
= wxID_EXIT
,
100 Menu_CheckFirst
= wxID_HIGHEST
,
106 Menu_InsertItemsStart
,
107 Menu_InsertItemsMiddle
,
122 BEGIN_EVENT_TABLE(CheckListBoxFrame
, wxFrame
)
123 EVT_MENU(Menu_About
, CheckListBoxFrame::OnAbout
)
124 EVT_MENU(Menu_Quit
, CheckListBoxFrame::OnQuit
)
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
)
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
)
139 EVT_MENU(Menu_GetItemHeight
, CheckListBoxFrame::OnGetItemHeight
)
140 EVT_MENU(Menu_GetBestSize
, CheckListBoxFrame::OnGetBestSize
)
142 EVT_MENU(Menu_MakeItemFirst
, CheckListBoxFrame::OnMakeItemFirst
)
144 EVT_LISTBOX(Control_Listbox
, CheckListBoxFrame::OnListboxSelect
)
145 EVT_CHECKLISTBOX(Control_Listbox
, CheckListBoxFrame::OnCheckboxToggle
)
146 EVT_LISTBOX_DCLICK(Control_Listbox
, CheckListBoxFrame::OnListboxDblClick
)
148 EVT_BUTTON(Btn_Up
, CheckListBoxFrame::OnButtonUp
)
149 EVT_BUTTON(Btn_Down
, CheckListBoxFrame::OnButtonDown
)
152 IMPLEMENT_APP(CheckListBoxApp
);
154 // init our app: create windows
155 bool CheckListBoxApp::OnInit(void)
157 CheckListBoxFrame
*pFrame
= new CheckListBoxFrame
160 _T("wxWidgets Checklistbox Sample")
162 SetTopWindow(pFrame
);
167 // main frame constructor
168 CheckListBoxFrame::CheckListBoxFrame(wxFrame
*frame
,
170 : wxFrame(frame
, wxID_ANY
, title
)
173 // create the status line
174 const int widths
[] = { -1, 60 };
176 SetStatusWidths(2, widths
);
177 #endif // wxUSE_STATUSBAR
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"));
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"));
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
);
216 // make a panel with some controls
217 m_panel
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxTAB_TRAVERSAL
);
219 CreateCheckListbox();
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
);
226 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
228 mainsizer
->Add( m_pListBox
, 1, wxGROW
|wxALL
, 10 );
230 wxBoxSizer
*bottomsizer
= new wxBoxSizer( wxHORIZONTAL
);
232 bottomsizer
->Add( button1
, 0, wxALL
, 10 );
233 bottomsizer
->Add( button2
, 0, wxALL
, 10 );
235 mainsizer
->Add( bottomsizer
, 0, wxCENTER
);
237 // tell frame to make use of sizer (or constraints, if any)
238 m_panel
->SetAutoLayout( true );
239 m_panel
->SetSizer( mainsizer
);
242 // don't allow frame to get smaller than what the sizers tell ye
243 mainsizer
->SetSizeHints( this );
249 void CheckListBoxFrame::CreateCheckListbox(long flags
)
252 static const wxChar
*aszChoices
[] =
255 _T("First"), _T("Second"), _T("Third"),
256 _T("Fourth"), _T("Fifth"), _T("Sixth"),
257 _T("Seventh"), _T("Eighth"), _T("Nineth")
260 wxString
*astrChoices
= new wxString
[WXSIZEOF(aszChoices
)];
262 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
++ )
263 astrChoices
[ui
] = aszChoices
[ui
];
265 m_pListBox
= new wxCheckListBox
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
276 //m_pListBox->SetBackgroundColour(*wxGREEN);
278 delete [] astrChoices
;
280 // set grey background for every second entry
281 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
+= 2 ) {
285 m_pListBox
->Check(2);
286 m_pListBox
->Select(3);
289 void CheckListBoxFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
294 void CheckListBoxFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
296 wxMessageBox(wxT("Demo of wxCheckListBox control\n(c) Vadim Zeitlin 1998-2002"),
297 wxT("About wxCheckListBox"),
298 wxICON_INFORMATION
, this);
301 void CheckListBoxFrame::OnCheckFirstItem(wxCommandEvent
& WXUNUSED(event
))
303 if ( !m_pListBox
->IsEmpty() )
304 m_pListBox
->Check(0);
307 void CheckListBoxFrame::OnUncheckFirstItem(wxCommandEvent
& WXUNUSED(event
))
309 if ( !m_pListBox
->IsEmpty() )
310 m_pListBox
->Check(0, false);
313 void CheckListBoxFrame::OnToggleFirstItem(wxCommandEvent
& WXUNUSED(event
))
315 if ( !m_pListBox
->IsEmpty() )
316 m_pListBox
->Check(0, !m_pListBox
->IsChecked(0));
319 void CheckListBoxFrame::OnInsertItemsStart(wxCommandEvent
& WXUNUSED(event
))
321 static size_t s_nItem
= 0;
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
));
327 m_pListBox
->InsertItems(items
, 0);//m_pListBox->GetCount());
330 void CheckListBoxFrame::OnInsertItemsMiddle(wxCommandEvent
& WXUNUSED(event
))
332 static size_t s_nItem
= 0;
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
));
338 m_pListBox
->InsertItems(items
, m_pListBox
->GetCount() ? 1 : 0);
341 void CheckListBoxFrame::OnInsertItemsEnd(wxCommandEvent
& WXUNUSED(event
))
343 static size_t s_nItem
= 0;
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
));
349 m_pListBox
->InsertItems(items
, m_pListBox
->GetCount() );
352 void CheckListBoxFrame::OnAppendItems(wxCommandEvent
& WXUNUSED(event
))
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
));
360 void CheckListBoxFrame::OnRemoveItems(wxCommandEvent
& WXUNUSED(event
))
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);
370 void CheckListBoxFrame::OnGetItemHeight(wxCommandEvent
& WXUNUSED(event
))
372 int height
= m_pListBox
->GetItemHeight();
374 wxMessageBox(wxString::Format(wxT("Height of an item is:%i"),
380 void CheckListBoxFrame::OnGetBestSize(wxCommandEvent
& WXUNUSED(event
))
382 wxSize bestSize
= m_pListBox
->GetBestSize();
384 wxMessageBox(wxString::Format(wxT("Best size of the checklistbox is:[%i,%i]"),
385 bestSize
.x
, bestSize
.y
390 void CheckListBoxFrame::OnMakeItemFirst(wxCommandEvent
& WXUNUSED(event
))
392 if(m_pListBox
->GetSelection() != -1)
393 m_pListBox
->SetFirstItem(m_pListBox
->GetSelection());
395 wxMessageBox(wxT("Nothing selected!"));
398 void CheckListBoxFrame::OnToggleSelection(wxCommandEvent
& event
)
400 wxSizer
*sizer
= m_panel
->GetSizer();
402 sizer
->Detach( m_pListBox
);
405 CreateCheckListbox(event
.IsChecked() ? wxLB_EXTENDED
: 0);
407 sizer
->Insert(0, m_pListBox
, 1, wxGROW
| wxALL
, 10);
412 void CheckListBoxFrame::OnToggleExtended(wxCommandEvent
& event
)
414 wxSizer
*sizer
= m_panel
->GetSizer();
416 sizer
->Detach( m_pListBox
);
419 CreateCheckListbox(event
.IsChecked() ? wxLB_EXTENDED
: 0);
421 sizer
->Insert(0, m_pListBox
, 1, wxGROW
| wxALL
, 10);
426 void CheckListBoxFrame::OnToggleSorting(wxCommandEvent
& event
)
428 wxSizer
*sizer
= m_panel
->GetSizer();
430 sizer
->Detach( m_pListBox
);
433 CreateCheckListbox(event
.IsChecked() ? wxLB_SORT
: 0);
435 sizer
->Insert(0, m_pListBox
, 1, wxGROW
| wxALL
, 10);
440 void CheckListBoxFrame::OnListboxSelect(wxCommandEvent
& event
)
442 int nSel
= event
.GetSelection();
443 wxLogStatus(this, wxT("Item %d selected (%schecked)"), nSel
,
444 m_pListBox
->IsChecked(nSel
) ? wxT("") : wxT("not "));
447 void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent
& WXUNUSED(event
))
450 if(m_pListBox
->GetWindowStyle() & wxLB_EXTENDED
)
453 m_pListBox
->GetSelections(list
);
456 selection
= list
.Item(0);
461 selection
= m_pListBox
->GetSelection();
464 wxString strSelection
;
465 if ( selection
!= -1 )
467 strSelection
.Printf(wxT("Item %d double clicked"), selection
);
471 strSelection
= wxT("List double clicked in multiple selection mode");
473 wxMessageDialog
dialog(this, strSelection
, wxT("wxCheckListBox message"), wxICON_INFORMATION
);
477 void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent
& event
)
479 unsigned int nItem
= event
.GetInt();
481 wxLogStatus(this, wxT("item %d was %schecked"), nItem
,
482 m_pListBox
->IsChecked(nItem
) ? wxT("") : wxT("un"));
485 void CheckListBoxFrame::OnButtonUp(wxCommandEvent
& WXUNUSED(event
))
490 void CheckListBoxFrame::OnButtonDown(wxCommandEvent
& WXUNUSED(event
))
495 void CheckListBoxFrame::OnButtonMove(bool up
)
498 if(m_pListBox
->GetWindowStyle() & wxLB_EXTENDED
)
501 m_pListBox
->GetSelections(list
);
504 selection
= list
.Item(0);
509 selection
= m_pListBox
->GetSelection();
511 if ( selection
!= wxNOT_FOUND
)
513 wxString label
= m_pListBox
->GetString(selection
);
515 int positionNew
= up
? selection
- 1 : selection
+ 2;
516 if ( positionNew
< 0 || positionNew
> m_pListBox
->GetCount() )
518 wxLogStatus(this, wxT("Can't move this item %s"), up
? wxT("up") : wxT("down"));
522 bool wasChecked
= m_pListBox
->IsChecked(selection
);
524 int positionOld
= up
? selection
+ 1 : selection
;
527 m_pListBox
->InsertItems(1, &label
, positionNew
);
529 // and delete the old one
530 m_pListBox
->Delete(positionOld
);
532 int selectionNew
= up
? positionNew
: positionNew
- 1;
533 m_pListBox
->Check(selectionNew
, wasChecked
);
534 m_pListBox
->SetSelection(selectionNew
);
535 m_pListBox
->SetFocus();
537 AdjustColour(selection
);
538 AdjustColour(selectionNew
);
540 wxLogStatus(this, wxT("Item moved %s"), up
? wxT("up") : wxT("down"));
545 wxLogStatus(this, wxT("Please select single item"));
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
)
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
));
558 void CheckListBoxFrame::AdjustColour(size_t WXUNUSED(index
))