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