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