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