]>
Commit | Line | Data |
---|---|---|
457814b5 JS |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: checklst.cpp | |
3 | // Purpose: wxCheckListBox sample | |
4 | // Author: Vadim Zeitlin | |
655822f3 | 5 | // Modified by: |
457814b5 JS |
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 | ||
3349fe92 | 12 | #ifdef __GNUG__ |
bbdbfb0e | 13 | //#pragma implementation |
3349fe92 | 14 | #endif |
457814b5 JS |
15 | |
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
bbdbfb0e | 20 | #pragma hdrstop |
457814b5 JS |
21 | #endif |
22 | ||
23 | #ifndef WX_PRECOMP | |
bbdbfb0e | 24 | #include "wx/wx.h" |
457814b5 JS |
25 | #endif |
26 | ||
b292e2f5 | 27 | #ifdef __WXMSW__ |
bbdbfb0e | 28 | #include "wx/ownerdrw.h" |
b292e2f5 | 29 | #endif |
bbdbfb0e VZ |
30 | |
31 | #include "wx/log.h" | |
32 | ||
d8d474af | 33 | #include "wx/sizer.h" |
457814b5 | 34 | #include "wx/menuitem.h" |
b292e2f5 RR |
35 | #include "wx/checklst.h" |
36 | ||
457814b5 JS |
37 | // Define a new application type |
38 | class CheckListBoxApp: public wxApp | |
39 | { | |
40 | public: | |
bbdbfb0e | 41 | bool OnInit(); |
457814b5 JS |
42 | }; |
43 | ||
44 | // Define a new frame type | |
45 | class CheckListBoxFrame : public wxFrame | |
46 | { | |
47 | public: | |
bbdbfb0e | 48 | // ctor & dtor |
9f84eccd | 49 | CheckListBoxFrame(wxFrame *frame, const wxChar *title, |
df7d383f | 50 | int x, int y, int w, int h); |
958d3a7e | 51 | virtual ~CheckListBoxFrame(){}; |
bbdbfb0e VZ |
52 | |
53 | // notifications | |
3dabb1e5 VZ |
54 | void OnQuit(wxCommandEvent& event); |
55 | void OnAbout(wxCommandEvent& event); | |
d553ceb2 | 56 | |
3dabb1e5 VZ |
57 | void OnCheckFirstItem(wxCommandEvent& event); |
58 | void OnUncheckFirstItem(wxCommandEvent& event); | |
59 | void OnToggleFirstItem(wxCommandEvent& event); | |
df7d383f | 60 | void OnToggleSelection(wxCommandEvent& event); |
d553ceb2 VZ |
61 | void OnAddItems(wxCommandEvent& event); |
62 | ||
3dabb1e5 VZ |
63 | void OnListboxSelect(wxCommandEvent& event); |
64 | void OnCheckboxToggle(wxCommandEvent& event); | |
bbdbfb0e | 65 | void OnListboxDblClick(wxCommandEvent& event); |
d553ceb2 | 66 | |
3dabb1e5 VZ |
67 | void OnButtonUp(wxCommandEvent& event); |
68 | void OnButtonDown(wxCommandEvent& event); | |
457814b5 | 69 | |
bbdbfb0e | 70 | private: |
df7d383f VZ |
71 | void CreateCheckListbox(long flags = 0); |
72 | ||
bbdbfb0e | 73 | void OnButtonMove(bool up); |
457814b5 | 74 | |
f048e32f VZ |
75 | void AdjustColour(size_t index); |
76 | ||
df7d383f VZ |
77 | wxPanel *m_panel; |
78 | ||
bbdbfb0e | 79 | wxCheckListBox *m_pListBox; |
457814b5 | 80 | |
bbdbfb0e | 81 | DECLARE_EVENT_TABLE() |
457814b5 JS |
82 | }; |
83 | ||
655822f3 VZ |
84 | enum |
85 | { | |
df7d383f VZ |
86 | Menu_About = 100, |
87 | Menu_Quit, | |
3dabb1e5 VZ |
88 | |
89 | Menu_CheckFirst, | |
90 | Menu_UncheckFirst, | |
91 | Menu_ToggleFirst, | |
df7d383f | 92 | Menu_Selection, |
d553ceb2 | 93 | Menu_AddItems, |
df7d383f | 94 | |
bbdbfb0e VZ |
95 | Control_First = 1000, |
96 | Control_Listbox, | |
97 | Btn_Up, | |
98 | Btn_Down | |
457814b5 JS |
99 | }; |
100 | ||
101 | BEGIN_EVENT_TABLE(CheckListBoxFrame, wxFrame) | |
df7d383f | 102 | EVT_MENU(Menu_About, CheckListBoxFrame::OnAbout) |
bbdbfb0e VZ |
103 | EVT_MENU(Menu_Quit, CheckListBoxFrame::OnQuit) |
104 | ||
3dabb1e5 VZ |
105 | EVT_MENU(Menu_CheckFirst, CheckListBoxFrame::OnCheckFirstItem) |
106 | EVT_MENU(Menu_UncheckFirst, CheckListBoxFrame::OnUncheckFirstItem) | |
107 | EVT_MENU(Menu_ToggleFirst, CheckListBoxFrame::OnToggleFirstItem) | |
df7d383f | 108 | EVT_MENU(Menu_Selection, CheckListBoxFrame::OnToggleSelection) |
d553ceb2 | 109 | EVT_MENU(Menu_AddItems, CheckListBoxFrame::OnAddItems) |
df7d383f | 110 | |
bbdbfb0e VZ |
111 | EVT_LISTBOX(Control_Listbox, CheckListBoxFrame::OnListboxSelect) |
112 | EVT_CHECKLISTBOX(Control_Listbox, CheckListBoxFrame::OnCheckboxToggle) | |
113 | EVT_LISTBOX_DCLICK(Control_Listbox, CheckListBoxFrame::OnListboxDblClick) | |
114 | ||
115 | EVT_BUTTON(Btn_Up, CheckListBoxFrame::OnButtonUp) | |
116 | EVT_BUTTON(Btn_Down, CheckListBoxFrame::OnButtonDown) | |
457814b5 JS |
117 | END_EVENT_TABLE() |
118 | ||
119 | IMPLEMENT_APP(CheckListBoxApp); | |
120 | ||
121 | // init our app: create windows | |
122 | bool CheckListBoxApp::OnInit(void) | |
123 | { | |
bbdbfb0e VZ |
124 | CheckListBoxFrame *pFrame = new CheckListBoxFrame |
125 | ( | |
126 | NULL, | |
be5a51fb | 127 | _T("wxWidgets Checklistbox Sample"), |
bbdbfb0e VZ |
128 | 50, 50, 480, 320 |
129 | ); | |
130 | SetTopWindow(pFrame); | |
131 | ||
8ad18dc3 | 132 | return true; |
457814b5 JS |
133 | } |
134 | ||
135 | // main frame constructor | |
bbdbfb0e | 136 | CheckListBoxFrame::CheckListBoxFrame(wxFrame *frame, |
df7d383f VZ |
137 | const wxChar *title, |
138 | int x, int y, int w, int h) | |
8ad18dc3 | 139 | : wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)) |
457814b5 | 140 | { |
8520f137 | 141 | #if wxUSE_STATUSBAR |
bbdbfb0e VZ |
142 | // create the status line |
143 | const int widths[] = { -1, 60 }; | |
144 | CreateStatusBar(2); | |
145 | SetStatusWidths(2, widths); | |
befa6d98 | 146 | wxLogStatus(this, _T("no selection")); |
8520f137 | 147 | #endif // wxUSE_STATUSBAR |
bbdbfb0e VZ |
148 | |
149 | // Make a menubar | |
df7d383f | 150 | // -------------- |
bbdbfb0e | 151 | |
df7d383f VZ |
152 | // file submenu |
153 | wxMenu *menuFile = new wxMenu; | |
154 | menuFile->Append(Menu_About, _T("&About...\tF1")); | |
155 | menuFile->AppendSeparator(); | |
156 | menuFile->Append(Menu_Quit, _T("E&xit\tAlt-X")); | |
bbdbfb0e | 157 | |
df7d383f VZ |
158 | // listbox submenu |
159 | wxMenu *menuList = new wxMenu; | |
3dabb1e5 VZ |
160 | menuList->Append(Menu_CheckFirst, _T("Check the first item\tCtrl-C")); |
161 | menuList->Append(Menu_UncheckFirst, _T("Uncheck the first item\tCtrl-U")); | |
162 | menuList->Append(Menu_ToggleFirst, _T("Toggle the first item\tCtrl-T")); | |
163 | menuList->AppendSeparator(); | |
d553ceb2 VZ |
164 | menuList->AppendCheckItem(Menu_AddItems, _T("Add more items\tCtrl-A")); |
165 | menuList->AppendSeparator(); | |
df7d383f VZ |
166 | menuList->AppendCheckItem(Menu_Selection, _T("Multiple selection\tCtrl-M")); |
167 | ||
168 | // put it all together | |
bbdbfb0e | 169 | wxMenuBar *menu_bar = new wxMenuBar; |
df7d383f VZ |
170 | menu_bar->Append(menuFile, _T("&File")); |
171 | menu_bar->Append(menuList, _T("&List")); | |
bbdbfb0e VZ |
172 | SetMenuBar(menu_bar); |
173 | ||
174 | // make a panel with some controls | |
8ad18dc3 | 175 | m_panel = new wxPanel(this, wxID_ANY, wxPoint(0, 0), |
df7d383f VZ |
176 | wxSize(400, 200), wxTAB_TRAVERSAL); |
177 | ||
178 | CreateCheckListbox(); | |
179 | ||
180 | // create buttons for moving the items around | |
181 | wxButton *button1 = new wxButton(m_panel, Btn_Up, _T(" &Up "), wxPoint(420, 90)); | |
182 | wxButton *button2 = new wxButton(m_panel, Btn_Down, _T("&Down"), wxPoint(420, 120)); | |
183 | ||
184 | ||
185 | wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL ); | |
186 | ||
187 | mainsizer->Add( m_pListBox, 1, wxGROW|wxALL, 10 ); | |
bbdbfb0e | 188 | |
df7d383f VZ |
189 | wxBoxSizer *bottomsizer = new wxBoxSizer( wxHORIZONTAL ); |
190 | ||
191 | bottomsizer->Add( button1, 0, wxALL, 10 ); | |
192 | bottomsizer->Add( button2, 0, wxALL, 10 ); | |
193 | ||
194 | mainsizer->Add( bottomsizer, 0, wxCENTER ); | |
195 | ||
196 | // tell frame to make use of sizer (or constraints, if any) | |
8ad18dc3 | 197 | m_panel->SetAutoLayout( true ); |
df7d383f VZ |
198 | m_panel->SetSizer( mainsizer ); |
199 | ||
200 | // don't allow frame to get smaller than what the sizers tell ye | |
201 | mainsizer->SetSizeHints( this ); | |
202 | ||
8ad18dc3 | 203 | Show(true); |
df7d383f VZ |
204 | } |
205 | ||
206 | void CheckListBoxFrame::CreateCheckListbox(long flags) | |
207 | { | |
bbdbfb0e | 208 | // check list box |
df7d383f | 209 | static const wxChar *aszChoices[] = |
bbdbfb0e | 210 | { |
df7d383f VZ |
211 | _T("Zeroth"), |
212 | _T("First"), _T("Second"), _T("Third"), | |
213 | _T("Fourth"), _T("Fifth"), _T("Sixth"), | |
214 | _T("Seventh"), _T("Eighth"), _T("Nineth") | |
bbdbfb0e VZ |
215 | }; |
216 | ||
217 | wxString *astrChoices = new wxString[WXSIZEOF(aszChoices)]; | |
218 | unsigned int ui; | |
219 | for ( ui = 0; ui < WXSIZEOF(aszChoices); ui++ ) | |
220 | astrChoices[ui] = aszChoices[ui]; | |
221 | ||
222 | m_pListBox = new wxCheckListBox | |
223 | ( | |
df7d383f | 224 | m_panel, // parent |
bbdbfb0e VZ |
225 | Control_Listbox, // control id |
226 | wxPoint(10, 10), // listbox poistion | |
8e1d4f96 | 227 | wxSize(400, 100), // listbox size |
bbdbfb0e | 228 | WXSIZEOF(aszChoices), // number of strings |
df7d383f VZ |
229 | astrChoices, // array of strings |
230 | flags | |
bbdbfb0e VZ |
231 | ); |
232 | ||
233 | //m_pListBox->SetBackgroundColour(*wxGREEN); | |
234 | ||
235 | delete [] astrChoices; | |
236 | ||
bbdbfb0e VZ |
237 | // set grey background for every second entry |
238 | for ( ui = 0; ui < WXSIZEOF(aszChoices); ui += 2 ) { | |
f048e32f | 239 | AdjustColour(ui); |
bbdbfb0e | 240 | } |
457814b5 | 241 | |
bbdbfb0e | 242 | m_pListBox->Check(2); |
df7d383f | 243 | m_pListBox->Select(3); |
457814b5 JS |
244 | } |
245 | ||
4f22cf8d | 246 | void CheckListBoxFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 247 | { |
8ad18dc3 | 248 | Close(true); |
457814b5 JS |
249 | } |
250 | ||
4f22cf8d | 251 | void CheckListBoxFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 252 | { |
749bfe9a | 253 | wxMessageBox(wxT("Demo of wxCheckListBox control\n(c) Vadim Zeitlin 1998-2002"), |
e680a378 | 254 | wxT("About wxCheckListBox"), |
bbdbfb0e | 255 | wxICON_INFORMATION, this); |
457814b5 JS |
256 | } |
257 | ||
87728739 | 258 | void CheckListBoxFrame::OnCheckFirstItem(wxCommandEvent& WXUNUSED(event)) |
3dabb1e5 VZ |
259 | { |
260 | if ( !m_pListBox->IsEmpty() ) | |
261 | m_pListBox->Check(0); | |
262 | } | |
263 | ||
87728739 | 264 | void CheckListBoxFrame::OnUncheckFirstItem(wxCommandEvent& WXUNUSED(event)) |
3dabb1e5 VZ |
265 | { |
266 | if ( !m_pListBox->IsEmpty() ) | |
8ad18dc3 | 267 | m_pListBox->Check(0, false); |
3dabb1e5 VZ |
268 | } |
269 | ||
87728739 | 270 | void CheckListBoxFrame::OnToggleFirstItem(wxCommandEvent& WXUNUSED(event)) |
3dabb1e5 VZ |
271 | { |
272 | if ( !m_pListBox->IsEmpty() ) | |
273 | m_pListBox->Check(0, !m_pListBox->IsChecked(0)); | |
274 | } | |
275 | ||
87728739 | 276 | void CheckListBoxFrame::OnAddItems(wxCommandEvent& WXUNUSED(event)) |
d553ceb2 VZ |
277 | { |
278 | static size_t s_nItem = 0; | |
279 | wxArrayString items; | |
280 | items.Add(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem)); | |
281 | items.Add(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem)); | |
282 | items.Add(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem)); | |
283 | ||
284 | m_pListBox->InsertItems(items, 0);//m_pListBox->GetCount()); | |
285 | } | |
286 | ||
df7d383f VZ |
287 | void CheckListBoxFrame::OnToggleSelection(wxCommandEvent& event) |
288 | { | |
289 | wxSizer *sizer = m_panel->GetSizer(); | |
290 | ||
12a3f227 | 291 | sizer->Detach( m_pListBox ); |
df7d383f VZ |
292 | delete m_pListBox; |
293 | ||
294 | CreateCheckListbox(event.IsChecked() ? wxLB_EXTENDED : 0); | |
295 | ||
296 | sizer->Insert(0, m_pListBox, 1, wxGROW | wxALL, 10); | |
297 | ||
298 | m_panel->Layout(); | |
299 | } | |
300 | ||
457814b5 JS |
301 | void CheckListBoxFrame::OnListboxSelect(wxCommandEvent& event) |
302 | { | |
bbdbfb0e | 303 | int nSel = event.GetSelection(); |
e680a378 | 304 | wxLogStatus(this, wxT("Item %d selected (%schecked)"), nSel, |
8ad18dc3 | 305 | m_pListBox->IsChecked(nSel) ? wxT("") : wxT("not ")); |
457814b5 JS |
306 | } |
307 | ||
4f22cf8d | 308 | void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 309 | { |
8ad18dc3 JS |
310 | int selection = -1; |
311 | if(m_pListBox->GetWindowStyle() & wxLB_EXTENDED) | |
312 | { | |
313 | wxArrayInt list; | |
314 | m_pListBox->GetSelections(list); | |
315 | if(list.Count()==1) | |
316 | { | |
317 | selection = list.Item(0); | |
318 | } | |
319 | } | |
320 | else | |
321 | { | |
322 | selection = m_pListBox->GetSelection(); | |
323 | } | |
324 | ||
bbdbfb0e | 325 | wxString strSelection; |
8ad18dc3 JS |
326 | if ( selection != -1 ) |
327 | { | |
328 | strSelection.Printf(wxT("Item %d double clicked"), selection); | |
329 | } | |
330 | else | |
331 | { | |
332 | strSelection = wxT("List double clicked in multiple selection mode"); | |
333 | } | |
e680a378 | 334 | wxMessageDialog dialog(this, strSelection, wxT("wxCheckListBox message"), wxICON_INFORMATION); |
bbdbfb0e | 335 | dialog.ShowModal(); |
457814b5 JS |
336 | } |
337 | ||
338 | void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent& event) | |
339 | { | |
bbdbfb0e | 340 | unsigned int nItem = event.GetInt(); |
655822f3 | 341 | |
e680a378 RR |
342 | wxLogStatus(this, wxT("item %d was %schecked"), nItem, |
343 | m_pListBox->IsChecked(nItem) ? wxT("") : wxT("un")); | |
bbdbfb0e VZ |
344 | } |
345 | ||
346 | void CheckListBoxFrame::OnButtonUp(wxCommandEvent& WXUNUSED(event)) | |
347 | { | |
8ad18dc3 | 348 | OnButtonMove(true); |
bbdbfb0e VZ |
349 | } |
350 | ||
351 | void CheckListBoxFrame::OnButtonDown(wxCommandEvent& WXUNUSED(event)) | |
352 | { | |
8ad18dc3 | 353 | OnButtonMove(false); |
bbdbfb0e VZ |
354 | } |
355 | ||
356 | void CheckListBoxFrame::OnButtonMove(bool up) | |
357 | { | |
8ad18dc3 JS |
358 | int selection = -1; |
359 | if(m_pListBox->GetWindowStyle() & wxLB_EXTENDED) | |
360 | { | |
361 | wxArrayInt list; | |
362 | m_pListBox->GetSelections(list); | |
363 | if(list.Count()==1) | |
364 | { | |
365 | selection = list.Item(0); | |
366 | } | |
367 | } | |
368 | else | |
369 | { | |
370 | selection = m_pListBox->GetSelection(); | |
371 | } | |
bbdbfb0e VZ |
372 | if ( selection != -1 ) |
373 | { | |
374 | wxString label = m_pListBox->GetString(selection); | |
375 | ||
376 | int positionNew = up ? selection - 1 : selection + 2; | |
f048e32f | 377 | if ( positionNew < 0 || positionNew > m_pListBox->GetCount() ) |
bbdbfb0e | 378 | { |
e680a378 | 379 | wxLogStatus(this, wxT("Can't move this item %s"), up ? wxT("up") : wxT("down")); |
bbdbfb0e VZ |
380 | } |
381 | else | |
382 | { | |
383 | bool wasChecked = m_pListBox->IsChecked(selection); | |
384 | ||
385 | int positionOld = up ? selection + 1 : selection; | |
386 | ||
387 | // insert the item | |
388 | m_pListBox->InsertItems(1, &label, positionNew); | |
389 | ||
390 | // and delete the old one | |
391 | m_pListBox->Delete(positionOld); | |
392 | ||
393 | int selectionNew = up ? positionNew : positionNew - 1; | |
394 | m_pListBox->Check(selectionNew, wasChecked); | |
395 | m_pListBox->SetSelection(selectionNew); | |
396 | ||
f048e32f VZ |
397 | AdjustColour(selection); |
398 | AdjustColour(selectionNew); | |
399 | ||
e680a378 | 400 | wxLogStatus(this, wxT("Item moved %s"), up ? wxT("up") : wxT("down")); |
bbdbfb0e VZ |
401 | } |
402 | } | |
403 | else | |
404 | { | |
8ad18dc3 | 405 | wxLogStatus(this, wxT("Please select single item")); |
bbdbfb0e | 406 | } |
655822f3 | 407 | } |
f048e32f | 408 | |
8ad18dc3 JS |
409 | // not implemented in ports other than (native) MSW yet |
410 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) | |
f048e32f VZ |
411 | void CheckListBoxFrame::AdjustColour(size_t index) |
412 | { | |
f048e32f VZ |
413 | // even items have grey backround, odd ones - white |
414 | unsigned char c = index % 2 ? 255 : 200; | |
415 | m_pListBox->GetItem(index)->SetBackgroundColour(wxColor(c, c, c)); | |
f048e32f | 416 | } |
8ad18dc3 JS |
417 | #else |
418 | void CheckListBoxFrame::AdjustColour(size_t WXUNUSED(index)) | |
419 | { | |
420 | } | |
421 | #endif // wxMSW |