]> git.saurik.com Git - wxWidgets.git/blob - samples/listctrl/listtest.cpp
Typos.
[wxWidgets.git] / samples / listctrl / listtest.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: listctrl.cpp
3 // Purpose: wxListCtrl sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #pragma interface
15 #endif
16
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WX_PRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #ifndef __WXMSW__
29 #include "mondrian.xpm"
30 #endif
31
32 #include "wx/listctrl.h"
33 #include "listtest.h"
34
35 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
36 EVT_MENU(BUSY_ON, MyFrame::BusyOn)
37 EVT_MENU(BUSY_OFF, MyFrame::BusyOff)
38 EVT_MENU(LIST_QUIT, MyFrame::OnQuit)
39 EVT_MENU(LIST_ABOUT, MyFrame::OnAbout)
40 EVT_MENU(LIST_LIST_VIEW, MyFrame::OnListView)
41 EVT_MENU(LIST_REPORT_VIEW, MyFrame::OnReportView)
42 EVT_MENU(LIST_ICON_VIEW, MyFrame::OnIconView)
43 EVT_MENU(LIST_ICON_TEXT_VIEW, MyFrame::OnIconTextView)
44 EVT_MENU(LIST_SMALL_ICON_VIEW, MyFrame::OnSmallIconView)
45 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView)
46 EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
47 EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
48 EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll)
49 END_EVENT_TABLE()
50
51 BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
52 EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag)
53 EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag)
54 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnBeginLabelEdit)
55 EVT_LIST_END_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnEndLabelEdit)
56 EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem)
57 EVT_LIST_GET_INFO(LIST_CTRL, MyListCtrl::OnGetInfo)
58 EVT_LIST_SET_INFO(LIST_CTRL, MyListCtrl::OnSetInfo)
59 EVT_LIST_ITEM_SELECTED(LIST_CTRL, MyListCtrl::OnSelected)
60 EVT_LIST_ITEM_DESELECTED(LIST_CTRL, MyListCtrl::OnDeselected)
61 EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
62 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
63 END_EVENT_TABLE()
64
65 IMPLEMENT_APP(MyApp)
66
67 // `Main program' equivalent, creating windows and returning main app frame
68 bool MyApp::OnInit(void)
69 {
70 // Create the main frame window
71 MyFrame *frame = new MyFrame((wxFrame *) NULL, "wxListCtrl Test", 50, 50, 450, 340);
72
73 // This reduces flicker effects - even better would be to define OnEraseBackground
74 // to do nothing. When the list control's scrollbars are show or hidden, the
75 // frame is sent a background erase event.
76 frame->SetBackgroundColour( *wxWHITE );
77
78 // Give it an icon
79 frame->SetIcon( wxICON(mondrian) );
80
81 // Make an image list containing large icons
82 m_imageListNormal = new wxImageList(32, 32, TRUE);
83 m_imageListSmall = new wxImageList(16, 16, TRUE);
84
85 #ifdef __WXMSW__
86 m_imageListNormal->Add( wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE) );
87 m_imageListNormal->Add( wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE) );
88 m_imageListNormal->Add( wxIcon("icon3", wxBITMAP_TYPE_ICO_RESOURCE) );
89 m_imageListNormal->Add( wxIcon("icon4", wxBITMAP_TYPE_ICO_RESOURCE) );
90 m_imageListNormal->Add( wxIcon("icon5", wxBITMAP_TYPE_ICO_RESOURCE) );
91 m_imageListNormal->Add( wxIcon("icon6", wxBITMAP_TYPE_ICO_RESOURCE) );
92 m_imageListNormal->Add( wxIcon("icon7", wxBITMAP_TYPE_ICO_RESOURCE) );
93 m_imageListNormal->Add( wxIcon("icon8", wxBITMAP_TYPE_ICO_RESOURCE) );
94 m_imageListNormal->Add( wxIcon("icon9", wxBITMAP_TYPE_ICO_RESOURCE) );
95
96 m_imageListSmall->Add( wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE) );
97
98 #else
99
100 #include "bitmaps/toolbrai.xpm"
101 m_imageListNormal->Add( wxIcon( toolbrai_xpm ) );
102 #include "bitmaps/toolchar.xpm"
103 m_imageListNormal->Add( wxIcon( toolchar_xpm ) );
104 #include "bitmaps/tooldata.xpm"
105 m_imageListNormal->Add( wxIcon( tooldata_xpm ) );
106 #include "bitmaps/toolnote.xpm"
107 m_imageListNormal->Add( wxIcon( toolnote_xpm ) );
108 #include "bitmaps/tooltodo.xpm"
109 m_imageListNormal->Add( wxIcon( tooltodo_xpm ) );
110 #include "bitmaps/toolchec.xpm"
111 m_imageListNormal->Add( wxIcon( toolchec_xpm ) );
112 #include "bitmaps/toolgame.xpm"
113 m_imageListNormal->Add( wxIcon( toolgame_xpm ) );
114 #include "bitmaps/tooltime.xpm"
115 m_imageListNormal->Add( wxIcon( tooltime_xpm ) );
116 #include "bitmaps/toolword.xpm"
117 m_imageListNormal->Add( wxIcon( toolword_xpm ) );
118
119 #include "bitmaps/small1.xpm"
120 m_imageListSmall->Add( wxIcon( small1_xpm) );
121
122 #endif
123
124 // Make a menubar
125 wxMenu *file_menu = new wxMenu;
126
127 file_menu->Append(LIST_LIST_VIEW, "&List view");
128 file_menu->Append(LIST_REPORT_VIEW, "&Report view");
129 file_menu->Append(LIST_ICON_VIEW, "&Icon view");
130 file_menu->Append(LIST_ICON_TEXT_VIEW, "Icon view with &text");
131 file_menu->Append(LIST_SMALL_ICON_VIEW, "&Small icon view");
132 file_menu->Append(LIST_SMALL_ICON_TEXT_VIEW, "Small icon &view with text");
133 file_menu->Append(LIST_DESELECT_ALL, "&Deselect All");
134 file_menu->Append(LIST_SELECT_ALL, "S&elect All");
135 file_menu->AppendSeparator();
136 file_menu->Append(LIST_DELETE_ALL, "Delete &all items");
137 file_menu->AppendSeparator();
138 file_menu->Append(BUSY_ON, "&Busy cursor on");
139 file_menu->Append(BUSY_OFF, "&Busy cursor off");
140 file_menu->AppendSeparator();
141 file_menu->Append(LIST_ABOUT, "&About");
142 file_menu->Append(LIST_QUIT, "E&xit");
143 wxMenuBar *menu_bar = new wxMenuBar;
144 menu_bar->Append(file_menu, "&File");
145 frame->SetMenuBar(menu_bar);
146
147 // Make a panel with a message
148 frame->m_listCtrl = new MyListCtrl(frame, LIST_CTRL, wxPoint(0, 0), wxSize(400, 200),
149 wxLC_LIST|wxSUNKEN_BORDER|wxLC_EDIT_LABELS);
150 // wxLC_LIST|wxLC_USER_TEXT|wxSUNKEN_BORDER); // wxLC_USER_TEXT requires app to supply all text on demand
151 frame->m_logWindow = new wxTextCtrl(frame, -1, "", wxPoint(0, 0), wxSize(400, 200), wxTE_MULTILINE|wxSUNKEN_BORDER);
152
153 wxLayoutConstraints *c = new wxLayoutConstraints;
154 c->top.SameAs (frame, wxTop);
155 c->left.SameAs (frame, wxLeft);
156 c->right.SameAs (frame, wxRight);
157 c->height.PercentOf (frame, wxHeight, 66);
158 frame->m_listCtrl->SetConstraints(c);
159
160 c = new wxLayoutConstraints;
161 c->top.Below (frame->m_listCtrl);
162 c->left.SameAs (frame, wxLeft);
163 c->right.SameAs (frame, wxRight);
164 c->bottom.SameAs (frame, wxBottom);
165 frame->m_logWindow->SetConstraints(c);
166 frame->SetAutoLayout(TRUE);
167
168 for ( int i=0; i < 30; i++)
169 {
170 wxChar buf[20];
171 wxSprintf(buf, _T("Item %d"), i);
172 frame->m_listCtrl->InsertItem(i, buf);
173 }
174
175 frame->CreateStatusBar(3);
176 frame->SetStatusText("", 0);
177
178 // Show the frame
179 frame->Show(TRUE);
180
181 SetTopWindow(frame);
182
183 return TRUE;
184 }
185
186 // My frame constructor
187 MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h):
188 wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
189 {
190 m_listCtrl = (MyListCtrl *) NULL;
191 m_logWindow = (wxTextCtrl *) NULL;
192 }
193
194 MyFrame::~MyFrame(void)
195 {
196 delete wxGetApp().m_imageListNormal;
197 delete wxGetApp().m_imageListSmall;
198 }
199
200 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
201 {
202 Close(TRUE);
203 }
204
205 void MyFrame::BusyOn(wxCommandEvent& WXUNUSED(event))
206 {
207 wxBeginBusyCursor();
208 }
209
210 void MyFrame::BusyOff(wxCommandEvent& WXUNUSED(event))
211 {
212 wxEndBusyCursor();
213 }
214
215 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
216 {
217 wxMessageDialog dialog(this, "List test sample\nJulian Smart (c) 1997",
218 "About list test", wxOK|wxCANCEL);
219
220 dialog.ShowModal();
221 }
222
223 void MyFrame::OnDeselectAll(wxCommandEvent& WXUNUSED(event))
224 {
225 int n = m_listCtrl->GetItemCount();
226 for (int i = 0; i < n; i++)
227 m_listCtrl->SetItemState(i,0,wxLIST_STATE_SELECTED);
228 }
229
230 void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event))
231 {
232 int n = m_listCtrl->GetItemCount();
233 for (int i = 0; i < n; i++)
234 m_listCtrl->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
235 }
236
237 void MyFrame::OnListView(wxCommandEvent& WXUNUSED(event))
238 {
239 m_listCtrl->DeleteAllItems();
240 m_logWindow->Clear();
241 m_listCtrl->SetSingleStyle(wxLC_LIST);
242 m_listCtrl->SetImageList((wxImageList *) NULL, wxIMAGE_LIST_NORMAL);
243 m_listCtrl->SetImageList((wxImageList *) NULL, wxIMAGE_LIST_SMALL);
244
245 for ( int i=0; i < 30; i++)
246 {
247 wxChar buf[20];
248 wxSprintf(buf, _T("Item %d"), i);
249 m_listCtrl->InsertItem(i, buf);
250 }
251 }
252
253 void MyFrame::OnReportView(wxCommandEvent& WXUNUSED(event))
254 {
255 m_listCtrl->DeleteAllItems();
256 m_listCtrl->DeleteAllColumns();
257 m_logWindow->Clear();
258
259 m_listCtrl->SetSingleStyle(wxLC_REPORT);
260 m_listCtrl->SetImageList((wxImageList *) NULL, wxIMAGE_LIST_NORMAL);
261 m_listCtrl->SetImageList(wxGetApp().m_imageListSmall, wxIMAGE_LIST_SMALL);
262
263 m_listCtrl->InsertColumn(0, "Column 1"); // , wxLIST_FORMAT_LEFT, 140);
264 m_listCtrl->InsertColumn(1, "Column 2"); // , wxLIST_FORMAT_LEFT, 140);
265 m_listCtrl->InsertColumn(2, "One More Column (2)"); // , wxLIST_FORMAT_LEFT, 140);
266
267 for ( int i = 0; i < 3000; i++ )
268 {
269 wxChar buf[50];
270 wxSprintf(buf, _T("This is item %d"), i);
271 long tmp = m_listCtrl->InsertItem(i, buf, 0);
272
273 wxSprintf(buf, _T("Col 1, item %d"), i);
274 tmp = m_listCtrl->SetItem(i, 1, buf);
275
276 wxSprintf(buf, _T("Item %d in column 2"), i);
277 tmp = m_listCtrl->SetItem(i, 2, buf);
278 }
279
280 m_listCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE );
281 m_listCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE );
282 m_listCtrl->SetColumnWidth( 2, wxLIST_AUTOSIZE );
283 }
284
285 void MyFrame::OnIconView(wxCommandEvent& WXUNUSED(event))
286 {
287 m_listCtrl->DeleteAllItems();
288 m_logWindow->Clear();
289 m_listCtrl->SetSingleStyle(wxLC_ICON);
290 m_listCtrl->SetImageList(wxGetApp().m_imageListNormal, wxIMAGE_LIST_NORMAL);
291 m_listCtrl->SetImageList(wxGetApp().m_imageListSmall, wxIMAGE_LIST_SMALL);
292
293 for ( int i=0; i < 9; i++)
294 {
295 m_listCtrl->InsertItem(i, i);
296 }
297 }
298
299 void MyFrame::OnIconTextView(wxCommandEvent& WXUNUSED(event))
300 {
301 m_listCtrl->DeleteAllItems();
302 m_logWindow->Clear();
303 m_listCtrl->SetSingleStyle(wxLC_ICON);
304 m_listCtrl->SetImageList(wxGetApp().m_imageListNormal, wxIMAGE_LIST_NORMAL);
305 m_listCtrl->SetImageList(wxGetApp().m_imageListSmall, wxIMAGE_LIST_SMALL);
306
307 for ( int i=0; i < 9; i++)
308 {
309 wxChar buf[20];
310 wxSprintf(buf, _T("Label %d"), i);
311 m_listCtrl->InsertItem(i, buf, i);
312 }
313 }
314
315 void MyFrame::OnSmallIconView(wxCommandEvent& WXUNUSED(event))
316 {
317 m_listCtrl->DeleteAllItems();
318 m_logWindow->Clear();
319 m_listCtrl->SetSingleStyle(wxLC_SMALL_ICON);
320 m_listCtrl->SetImageList(wxGetApp().m_imageListNormal, wxIMAGE_LIST_NORMAL);
321 m_listCtrl->SetImageList(wxGetApp().m_imageListSmall, wxIMAGE_LIST_SMALL);
322
323 for ( int i=0; i < 9; i++)
324 {
325 m_listCtrl->InsertItem(i, 0);
326 }
327 }
328
329 void MyFrame::OnSmallIconTextView(wxCommandEvent& WXUNUSED(event))
330 {
331 m_listCtrl->DeleteAllItems();
332 m_logWindow->Clear();
333 m_listCtrl->SetSingleStyle(wxLC_SMALL_ICON);
334 m_listCtrl->SetImageList(wxGetApp().m_imageListNormal, wxIMAGE_LIST_NORMAL);
335 m_listCtrl->SetImageList(wxGetApp().m_imageListSmall, wxIMAGE_LIST_SMALL);
336
337 for ( int i=0; i < 9; i++)
338 {
339 m_listCtrl->InsertItem(i, "Label", 0);
340 }
341 }
342
343 void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
344 {
345 (void)wxGetElapsedTime(TRUE);
346
347 int nItems = m_listCtrl->GetItemCount();
348 m_listCtrl->DeleteAllItems();
349
350 wxLogMessage("Deleting %d items took %ld ms",
351 nItems, wxGetElapsedTime());
352 }
353
354 // MyListCtrl
355
356 void MyListCtrl::OnBeginDrag(wxListEvent& WXUNUSED(event))
357 {
358 if ( !wxGetApp().GetTopWindow() )
359 return;
360
361 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
362 if ( !text )
363 return;
364
365 text->WriteText("OnBeginDrag\n");
366 }
367
368 void MyListCtrl::OnBeginRDrag(wxListEvent& WXUNUSED(event))
369 {
370 if ( !wxGetApp().GetTopWindow() )
371 return;
372
373 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
374 if ( !text )
375 return;
376 text->WriteText("OnBeginRDrag\n");
377 }
378
379 void MyListCtrl::OnBeginLabelEdit(wxListEvent& WXUNUSED(event))
380 {
381 if ( !wxGetApp().GetTopWindow() )
382 return;
383
384 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
385 if ( !text )
386 return;
387
388 text->WriteText("OnBeginLabelEdit\n");
389 }
390
391 void MyListCtrl::OnEndLabelEdit(wxListEvent& WXUNUSED(event))
392 {
393 if ( !wxGetApp().GetTopWindow() )
394 return;
395
396 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
397 if ( !text )
398 return;
399
400 text->WriteText("OnEndLabelEdit\n");
401 }
402
403 void MyListCtrl::OnDeleteItem(wxListEvent& WXUNUSED(event))
404 {
405 if ( !wxGetApp().GetTopWindow() )
406 return;
407
408 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
409 if ( !text )
410 return;
411
412 text->WriteText("OnDeleteItem\n");
413 }
414
415 void MyListCtrl::OnGetInfo(wxListEvent& /*event*/)
416 {
417 if ( !wxGetApp().GetTopWindow() )
418 return;
419
420 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
421 if ( !text )
422 return;
423
424 text->WriteText("OnGetInfo\n");
425
426 /*
427 ostream str(text);
428
429 str << "OnGetInfo (" << event.m_item.m_itemId << ", " << event.m_item.m_col << ")";
430 if ( event.m_item.m_mask & wxLIST_MASK_STATE )
431 str << " wxLIST_MASK_STATE";
432 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
433 str << " wxLIST_MASK_TEXT";
434 if ( event.m_item.m_mask & wxLIST_MASK_IMAGE )
435 str << " wxLIST_MASK_IMAGE";
436 if ( event.m_item.m_mask & wxLIST_MASK_DATA )
437 str << " wxLIST_MASK_DATA";
438 if ( event.m_item.m_mask & wxLIST_SET_ITEM )
439 str << " wxLIST_SET_ITEM";
440 if ( event.m_item.m_mask & wxLIST_MASK_WIDTH )
441 str << " wxLIST_MASK_WIDTH";
442 if ( event.m_item.m_mask & wxLIST_MASK_FORMAT )
443 str << " wxLIST_MASK_WIDTH";
444
445 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
446 {
447 event.m_item.m_text = "My callback text";
448 }
449 str << "\n";
450 str.flush();
451 */
452 }
453
454 void MyListCtrl::OnSetInfo(wxListEvent& WXUNUSED(event))
455 {
456 if ( !wxGetApp().GetTopWindow() )
457 return;
458
459 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
460 if ( !text )
461 return;
462
463 text->WriteText("OnSetInfo\n");
464 }
465
466 void MyListCtrl::OnSelected(wxListEvent& WXUNUSED(event))
467 {
468 if ( !wxGetApp().GetTopWindow() )
469 return;
470
471 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
472 if ( !text )
473 return;
474
475 text->WriteText("OnSelected\n");
476 }
477
478 void MyListCtrl::OnDeselected(wxListEvent& WXUNUSED(event))
479 {
480 if ( !wxGetApp().GetTopWindow() )
481 return;
482
483 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
484 if ( !text )
485 return;
486
487 text->WriteText("OnDeselected\n");
488 }
489
490 void MyListCtrl::OnActivated(wxListEvent& WXUNUSED(event))
491 {
492 if ( !wxGetApp().GetTopWindow() )
493 return;
494
495 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
496 if ( !text )
497 return;
498
499 text->WriteText("OnActivated\n");
500 }
501
502 void MyListCtrl::OnListKeyDown(wxListEvent& event)
503 {
504 if ( !wxGetApp().GetTopWindow() )
505 return;
506
507 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
508 if ( !text )
509 return;
510
511 text->WriteText("OnListKeyDown\n");
512 }
513
514