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