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