1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxListCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
29 #include "mondrian.xpm"
31 #include "bitmaps/toolbrai.xpm"
32 #include "bitmaps/toolchar.xpm"
33 #include "bitmaps/tooldata.xpm"
34 #include "bitmaps/toolnote.xpm"
35 #include "bitmaps/tooltodo.xpm"
36 #include "bitmaps/toolchec.xpm"
37 #include "bitmaps/toolgame.xpm"
38 #include "bitmaps/tooltime.xpm"
39 #include "bitmaps/toolword.xpm"
40 #include "bitmaps/small1.xpm"
43 #include "wx/listctrl.h"
44 #include "wx/timer.h" // for wxStopWatch
45 #include "wx/colordlg.h" // for wxGetColourFromUser
49 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
50 EVT_MENU(BUSY_ON
, MyFrame::BusyOn
)
51 EVT_MENU(BUSY_OFF
, MyFrame::BusyOff
)
52 EVT_MENU(LIST_QUIT
, MyFrame::OnQuit
)
53 EVT_MENU(LIST_ABOUT
, MyFrame::OnAbout
)
54 EVT_MENU(LIST_LIST_VIEW
, MyFrame::OnListView
)
55 EVT_MENU(LIST_REPORT_VIEW
, MyFrame::OnReportView
)
56 EVT_MENU(LIST_ICON_VIEW
, MyFrame::OnIconView
)
57 EVT_MENU(LIST_ICON_TEXT_VIEW
, MyFrame::OnIconTextView
)
58 EVT_MENU(LIST_SMALL_ICON_VIEW
, MyFrame::OnSmallIconView
)
59 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW
, MyFrame::OnSmallIconTextView
)
60 EVT_MENU(LIST_TOGGLE_FIRST
, MyFrame::OnToggleFirstSel
)
61 EVT_MENU(LIST_DESELECT_ALL
, MyFrame::OnDeselectAll
)
62 EVT_MENU(LIST_SELECT_ALL
, MyFrame::OnSelectAll
)
63 EVT_MENU(LIST_DELETE_ALL
, MyFrame::OnDeleteAll
)
64 EVT_MENU(LIST_SORT
, MyFrame::OnSort
)
65 EVT_MENU(LIST_SET_FG_COL
, MyFrame::OnSetFgColour
)
66 EVT_MENU(LIST_SET_BG_COL
, MyFrame::OnSetBgColour
)
67 EVT_MENU(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnToggleMultiSel
)
70 BEGIN_EVENT_TABLE(MyListCtrl
, wxListCtrl
)
71 EVT_LIST_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnBeginDrag
)
72 EVT_LIST_BEGIN_RDRAG(LIST_CTRL
, MyListCtrl::OnBeginRDrag
)
73 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnBeginLabelEdit
)
74 EVT_LIST_END_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnEndLabelEdit
)
75 EVT_LIST_DELETE_ITEM(LIST_CTRL
, MyListCtrl::OnDeleteItem
)
76 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL
, MyListCtrl::OnDeleteAllItems
)
77 EVT_LIST_GET_INFO(LIST_CTRL
, MyListCtrl::OnGetInfo
)
78 EVT_LIST_SET_INFO(LIST_CTRL
, MyListCtrl::OnSetInfo
)
79 EVT_LIST_ITEM_SELECTED(LIST_CTRL
, MyListCtrl::OnSelected
)
80 EVT_LIST_ITEM_DESELECTED(LIST_CTRL
, MyListCtrl::OnDeselected
)
81 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl::OnListKeyDown
)
82 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl::OnActivated
)
83 EVT_LIST_COL_CLICK(LIST_CTRL
, MyListCtrl::OnColClick
)
88 int wxCALLBACK
MyCompareFunction(long item1
, long item2
, long sortData
)
94 // `Main program' equivalent, creating windows and returning main app frame
97 // Create the main frame window
98 MyFrame
*frame
= new MyFrame("wxListCtrl Test", 50, 50, 450, 340);
108 // My frame constructor
109 MyFrame::MyFrame(const wxChar
*title
, int x
, int y
, int w
, int h
)
110 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
112 m_listCtrl
= (MyListCtrl
*) NULL
;
113 m_logWindow
= (wxTextCtrl
*) NULL
;
116 SetIcon( wxICON(mondrian
) );
118 // Make an image list containing large icons
119 m_imageListNormal
= new wxImageList(32, 32, TRUE
);
120 m_imageListSmall
= new wxImageList(16, 16, TRUE
);
123 m_imageListNormal
->Add( wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE
) );
124 m_imageListNormal
->Add( wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE
) );
125 m_imageListNormal
->Add( wxIcon("icon3", wxBITMAP_TYPE_ICO_RESOURCE
) );
126 m_imageListNormal
->Add( wxIcon("icon4", wxBITMAP_TYPE_ICO_RESOURCE
) );
127 m_imageListNormal
->Add( wxIcon("icon5", wxBITMAP_TYPE_ICO_RESOURCE
) );
128 m_imageListNormal
->Add( wxIcon("icon6", wxBITMAP_TYPE_ICO_RESOURCE
) );
129 m_imageListNormal
->Add( wxIcon("icon7", wxBITMAP_TYPE_ICO_RESOURCE
) );
130 m_imageListNormal
->Add( wxIcon("icon8", wxBITMAP_TYPE_ICO_RESOURCE
) );
131 m_imageListNormal
->Add( wxIcon("icon9", wxBITMAP_TYPE_ICO_RESOURCE
) );
133 m_imageListSmall
->Add( wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE
) );
136 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
137 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
138 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
139 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
140 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
141 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
142 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
143 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
144 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
146 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
150 wxMenu
*menuFile
= new wxMenu
;
151 menuFile
->Append(LIST_ABOUT
, "&About");
152 menuFile
->AppendSeparator();
153 #if 0 // what is this for? (VZ)
154 menuFile
->Append(BUSY_ON
, "&Busy cursor on");
155 menuFile
->Append(BUSY_OFF
, "&Busy cursor off");
156 menuFile
->AppendSeparator();
158 menuFile
->Append(LIST_QUIT
, "E&xit\tAlt-X");
160 wxMenu
*menuView
= new wxMenu
;
161 menuView
->Append(LIST_LIST_VIEW
, "&List view\tF1");
162 menuView
->Append(LIST_REPORT_VIEW
, "&Report view\tF2");
163 menuView
->Append(LIST_ICON_VIEW
, "&Icon view\tF3");
164 menuView
->Append(LIST_ICON_TEXT_VIEW
, "Icon view with &text\tF4");
165 menuView
->Append(LIST_SMALL_ICON_VIEW
, "&Small icon view\tF5");
166 menuView
->Append(LIST_SMALL_ICON_TEXT_VIEW
, "Small icon &view with text\tF6");
168 wxMenu
*menuList
= new wxMenu
;
169 menuList
->Append(LIST_TOGGLE_FIRST
, "&Toggle first item\tCtrl-T");
170 menuList
->Append(LIST_DESELECT_ALL
, "&Deselect All\tCtrl-D");
171 menuList
->Append(LIST_SELECT_ALL
, "S&elect All\tCtrl-A");
172 menuList
->AppendSeparator();
173 menuList
->Append(LIST_SORT
, "&Sort\tCtrl-S");
174 menuList
->AppendSeparator();
175 menuList
->Append(LIST_DELETE_ALL
, "Delete &all items");
176 menuList
->AppendSeparator();
177 menuList
->Append(LIST_TOGGLE_MULTI_SEL
, "&Multiple selection\tCtrl-M",
178 "Toggle multiple selection", TRUE
);
180 wxMenu
*menuCol
= new wxMenu
;
181 menuCol
->Append(LIST_SET_FG_COL
, "&Foreground colour...");
182 menuCol
->Append(LIST_SET_BG_COL
, "&Background colour...");
184 wxMenuBar
*menubar
= new wxMenuBar
;
185 menubar
->Append(menuFile
, "&File");
186 menubar
->Append(menuView
, "&View");
187 menubar
->Append(menuList
, "&List");
188 menubar
->Append(menuCol
, "&Colour");
191 m_listCtrl
= new MyListCtrl(this, LIST_CTRL
,
192 wxDefaultPosition
, wxDefaultSize
,
196 // wxLC_USER_TEXT requires app to supply all
202 m_logWindow
= new wxTextCtrl(this, -1, wxEmptyString
,
203 wxDefaultPosition
, wxDefaultSize
,
204 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
206 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
208 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
209 c
->top
.SameAs (this, wxTop
);
210 c
->left
.SameAs (this, wxLeft
);
211 c
->right
.SameAs (this, wxRight
);
212 c
->height
.PercentOf (this, wxHeight
, 66);
213 m_listCtrl
->SetConstraints(c
);
215 c
= new wxLayoutConstraints
;
216 c
->top
.Below (m_listCtrl
);
217 c
->left
.SameAs (this, wxLeft
);
218 c
->right
.SameAs (this, wxRight
);
219 c
->bottom
.SameAs (this, wxBottom
);
220 m_logWindow
->SetConstraints(c
);
223 for ( int i
= 0; i
< 30; i
++ )
225 long idx
= m_listCtrl
->InsertItem(i
, wxString::Format(_T("Item %d"), i
));
226 m_listCtrl
->SetItemData(idx
, i
*i
);
234 delete wxLog::SetActiveTarget(m_logOld
);
236 delete m_imageListNormal
;
237 delete m_imageListSmall
;
240 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
245 void MyFrame::BusyOn(wxCommandEvent
& WXUNUSED(event
))
250 void MyFrame::BusyOff(wxCommandEvent
& WXUNUSED(event
))
255 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
257 wxMessageDialog
dialog(this, "List test sample\nJulian Smart (c) 1997",
258 "About list test", wxOK
|wxCANCEL
);
263 void MyFrame::OnToggleFirstSel(wxCommandEvent
& WXUNUSED(event
))
265 m_listCtrl
->SetItemState(0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
268 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
270 int n
= m_listCtrl
->GetItemCount();
271 for (int i
= 0; i
< n
; i
++)
272 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
275 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
277 int n
= m_listCtrl
->GetItemCount();
278 for (int i
= 0; i
< n
; i
++)
279 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
282 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
284 m_listCtrl
->ClearAll();
285 m_logWindow
->Clear();
287 m_listCtrl
->SetSingleStyle(wxLC_LIST
);
288 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_NORMAL
);
289 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_SMALL
);
291 for ( int i
=0; i
< 30; i
++)
294 wxSprintf(buf
, _T("Item %d"), i
);
295 m_listCtrl
->InsertItem(i
, buf
);
299 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
301 m_logWindow
->Clear();
302 m_listCtrl
->ClearAll();
304 m_listCtrl
->SetSingleStyle(wxLC_REPORT
);
305 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_NORMAL
);
306 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
308 m_listCtrl
->InsertColumn(0, "Column 1"); // , wxLIST_FORMAT_LEFT, 140);
309 m_listCtrl
->InsertColumn(1, "Column 2"); // , wxLIST_FORMAT_LEFT, 140);
310 m_listCtrl
->InsertColumn(2, "One More Column (2)"); // , wxLIST_FORMAT_LEFT, 140);
312 // to speed up inserting we hide the control temporarily
318 static const int NUM_ITEMS
= 3000;
319 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
321 buf
.Printf(_T("This is item %d"), i
);
322 long tmp
= m_listCtrl
->InsertItem(i
, buf
, 0);
323 m_listCtrl
->SetItemData(tmp
, i
);
325 buf
.Printf(_T("Col 1, item %d"), i
);
326 tmp
= m_listCtrl
->SetItem(i
, 1, buf
);
328 buf
.Printf(_T("Item %d in column 2"), i
);
329 tmp
= m_listCtrl
->SetItem(i
, 2, buf
);
332 m_logWindow
->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
333 NUM_ITEMS
, sw
.Time()));
336 // we leave all mask fields to 0 and only change the colour
339 item
.SetTextColour(*wxRED
);
340 m_listCtrl
->SetItem( item
);
343 item
.SetTextColour(*wxGREEN
);
344 m_listCtrl
->SetItem( item
);
346 item
.SetTextColour(*wxLIGHT_GREY
);
347 item
.SetFont(*wxITALIC_FONT
);
348 item
.SetBackgroundColour(*wxRED
);
349 m_listCtrl
->SetItem( item
);
351 m_listCtrl
->SetTextColour(*wxBLUE
);
352 m_listCtrl
->SetBackgroundColour(*wxLIGHT_GREY
);
354 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
355 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
356 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
359 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
361 m_logWindow
->Clear();
362 m_listCtrl
->ClearAll();
364 m_listCtrl
->SetSingleStyle(wxLC_ICON
);
365 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
366 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
368 for ( int i
=0; i
< 9; i
++)
370 m_listCtrl
->InsertItem(i
, i
);
374 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
376 m_logWindow
->Clear();
377 m_listCtrl
->ClearAll();
379 m_listCtrl
->SetSingleStyle(wxLC_ICON
);
380 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
381 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
383 for ( int i
=0; i
< 9; i
++)
386 wxSprintf(buf
, _T("Label %d"), i
);
387 m_listCtrl
->InsertItem(i
, buf
, i
);
391 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
393 m_logWindow
->Clear();
394 m_listCtrl
->ClearAll();
396 m_listCtrl
->SetSingleStyle(wxLC_SMALL_ICON
);
397 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
398 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
400 for ( int i
=0; i
< 9; i
++)
402 m_listCtrl
->InsertItem(i
, 0);
406 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
408 m_logWindow
->Clear();
409 m_listCtrl
->ClearAll();
411 m_listCtrl
->SetSingleStyle(wxLC_SMALL_ICON
);
412 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
413 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
415 for ( int i
=0; i
< 9; i
++)
417 m_listCtrl
->InsertItem(i
, "Label", 0);
421 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
425 m_listCtrl
->SortItems(MyCompareFunction
, 0);
427 m_logWindow
->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
428 m_listCtrl
->GetItemCount(),
432 void MyFrame::OnToggleMultiSel(wxCommandEvent
& WXUNUSED(event
))
434 m_logWindow
->WriteText("Current selection mode: ");
436 long flags
= m_listCtrl
->GetWindowStyleFlag();
437 if ( flags
& wxLC_SINGLE_SEL
)
439 m_listCtrl
->SetWindowStyleFlag(flags
& ~wxLC_SINGLE_SEL
);
440 m_logWindow
->WriteText("multiple");
444 m_listCtrl
->SetWindowStyleFlag(flags
| wxLC_SINGLE_SEL
);
445 m_logWindow
->WriteText("single");
448 m_logWindow
->WriteText("\nRecreate the control now\n");
451 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
453 m_listCtrl
->SetForegroundColour(wxGetColourFromUser(this));
454 m_listCtrl
->Refresh();
457 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
459 m_listCtrl
->SetBackgroundColour(wxGetColourFromUser(this));
460 m_listCtrl
->Refresh();
463 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
467 m_listCtrl
->DeleteAllItems();
469 m_logWindow
->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
470 m_listCtrl
->GetItemCount(),
476 void MyListCtrl::OnColClick(wxListEvent
& event
)
478 wxLogMessage( "OnColumnClick at %d.", event
.GetColumn() );
481 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
483 wxLogMessage( "OnBeginDrag at %d,%d.",
484 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
487 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
489 wxLogMessage( "OnBeginRDrag at %d,%d.",
490 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
493 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
495 wxLogMessage("OnBeginLabelEdit: %s", event
.m_item
.m_text
.c_str());
498 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
500 wxLogMessage("OnEndLabelEdit: %s", event
.m_item
.m_text
.c_str());
503 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
505 LogEvent(event
, _T("OnDeleteItem"));
508 void MyListCtrl::OnDeleteAllItems(wxListEvent
& event
)
510 LogEvent(event
, _T("OnDeleteAllItems"));
513 void MyListCtrl::OnGetInfo(wxListEvent
& event
)
517 msg
<< "OnGetInfo (" << event
.m_item
.m_itemId
<< ", " << event
.m_item
.m_col
<< ")";
518 if ( event
.m_item
.m_mask
& wxLIST_MASK_STATE
)
519 msg
<< " wxLIST_MASK_STATE";
520 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
521 msg
<< " wxLIST_MASK_TEXT";
522 if ( event
.m_item
.m_mask
& wxLIST_MASK_IMAGE
)
523 msg
<< " wxLIST_MASK_IMAGE";
524 if ( event
.m_item
.m_mask
& wxLIST_MASK_DATA
)
525 msg
<< " wxLIST_MASK_DATA";
526 if ( event
.m_item
.m_mask
& wxLIST_SET_ITEM
)
527 msg
<< " wxLIST_SET_ITEM";
528 if ( event
.m_item
.m_mask
& wxLIST_MASK_WIDTH
)
529 msg
<< " wxLIST_MASK_WIDTH";
530 if ( event
.m_item
.m_mask
& wxLIST_MASK_FORMAT
)
531 msg
<< " wxLIST_MASK_WIDTH";
533 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
535 event
.m_item
.m_text
= "My callback text";
541 void MyListCtrl::OnSetInfo(wxListEvent
& event
)
543 LogEvent(event
, _T("OnSetInfo"));
546 void MyListCtrl::OnSelected(wxListEvent
& event
)
548 LogEvent(event
, _T("OnSelected"));
550 if ( GetWindowStyle() & wxLC_REPORT
)
553 info
.m_itemId
= event
.m_itemIndex
;
555 info
.m_mask
= wxLIST_MASK_TEXT
;
558 wxLogMessage("Value of the 2nd field of the selected item: %s",
559 info
.m_text
.c_str());
563 wxFAIL_MSG("wxListCtrl::GetItem() failed");
568 void MyListCtrl::OnDeselected(wxListEvent
& event
)
570 LogEvent(event
, _T("OnDeselected"));
573 void MyListCtrl::OnActivated(wxListEvent
& event
)
575 LogEvent(event
, _T("OnActivated"));
578 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
580 LogEvent(event
, _T("OnListKeyDown"));
583 void MyListCtrl::LogEvent(const wxListEvent
& event
, const wxChar
*eventName
)
585 wxLogMessage(_T("Item %ld: %s (item data = %ld)"),
586 event
.GetIndex(), eventName
, event
.GetData());