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_DESELECT_ALL
, MyFrame::OnDeselectAll
)
61 EVT_MENU(LIST_SELECT_ALL
, MyFrame::OnSelectAll
)
62 EVT_MENU(LIST_DELETE_ALL
, MyFrame::OnDeleteAll
)
63 EVT_MENU(LIST_SORT
, MyFrame::OnSort
)
64 EVT_MENU(LIST_SET_FG_COL
, MyFrame::OnSetFgColour
)
65 EVT_MENU(LIST_SET_BG_COL
, MyFrame::OnSetBgColour
)
66 EVT_MENU(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnToggleMultiSel
)
69 BEGIN_EVENT_TABLE(MyListCtrl
, wxListCtrl
)
70 EVT_LIST_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnBeginDrag
)
71 EVT_LIST_BEGIN_RDRAG(LIST_CTRL
, MyListCtrl::OnBeginRDrag
)
72 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnBeginLabelEdit
)
73 EVT_LIST_END_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnEndLabelEdit
)
74 EVT_LIST_DELETE_ITEM(LIST_CTRL
, MyListCtrl::OnDeleteItem
)
75 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL
, MyListCtrl::OnDeleteAllItems
)
76 EVT_LIST_GET_INFO(LIST_CTRL
, MyListCtrl::OnGetInfo
)
77 EVT_LIST_SET_INFO(LIST_CTRL
, MyListCtrl::OnSetInfo
)
78 EVT_LIST_ITEM_SELECTED(LIST_CTRL
, MyListCtrl::OnSelected
)
79 EVT_LIST_ITEM_DESELECTED(LIST_CTRL
, MyListCtrl::OnDeselected
)
80 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl::OnListKeyDown
)
81 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl::OnActivated
)
82 EVT_LIST_COL_CLICK(LIST_CTRL
, MyListCtrl::OnColClick
)
87 int wxCALLBACK
MyCompareFunction(long item1
, long item2
, long sortData
)
93 // `Main program' equivalent, creating windows and returning main app frame
96 // Create the main frame window
97 MyFrame
*frame
= new MyFrame("wxListCtrl Test", 50, 50, 450, 340);
107 // My frame constructor
108 MyFrame::MyFrame(const wxChar
*title
, int x
, int y
, int w
, int h
)
109 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
111 m_listCtrl
= (MyListCtrl
*) NULL
;
112 m_logWindow
= (wxTextCtrl
*) NULL
;
115 SetIcon( wxICON(mondrian
) );
117 // Make an image list containing large icons
118 m_imageListNormal
= new wxImageList(32, 32, TRUE
);
119 m_imageListSmall
= new wxImageList(16, 16, TRUE
);
122 m_imageListNormal
->Add( wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE
) );
123 m_imageListNormal
->Add( wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE
) );
124 m_imageListNormal
->Add( wxIcon("icon3", wxBITMAP_TYPE_ICO_RESOURCE
) );
125 m_imageListNormal
->Add( wxIcon("icon4", wxBITMAP_TYPE_ICO_RESOURCE
) );
126 m_imageListNormal
->Add( wxIcon("icon5", wxBITMAP_TYPE_ICO_RESOURCE
) );
127 m_imageListNormal
->Add( wxIcon("icon6", wxBITMAP_TYPE_ICO_RESOURCE
) );
128 m_imageListNormal
->Add( wxIcon("icon7", wxBITMAP_TYPE_ICO_RESOURCE
) );
129 m_imageListNormal
->Add( wxIcon("icon8", wxBITMAP_TYPE_ICO_RESOURCE
) );
130 m_imageListNormal
->Add( wxIcon("icon9", wxBITMAP_TYPE_ICO_RESOURCE
) );
132 m_imageListSmall
->Add( wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE
) );
135 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
136 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
137 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
138 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
139 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
140 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
141 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
142 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
143 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
145 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
149 wxMenu
*menuFile
= new wxMenu
;
150 menuFile
->Append(LIST_ABOUT
, "&About");
151 menuFile
->AppendSeparator();
152 #if 0 // what is this for? (VZ)
153 menuFile
->Append(BUSY_ON
, "&Busy cursor on");
154 menuFile
->Append(BUSY_OFF
, "&Busy cursor off");
155 menuFile
->AppendSeparator();
157 menuFile
->Append(LIST_QUIT
, "E&xit\tAlt-X");
159 wxMenu
*menuView
= new wxMenu
;
160 menuView
->Append(LIST_LIST_VIEW
, "&List view\tF1");
161 menuView
->Append(LIST_REPORT_VIEW
, "&Report view\tF2");
162 menuView
->Append(LIST_ICON_VIEW
, "&Icon view\tF3");
163 menuView
->Append(LIST_ICON_TEXT_VIEW
, "Icon view with &text\tF4");
164 menuView
->Append(LIST_SMALL_ICON_VIEW
, "&Small icon view\tF5");
165 menuView
->Append(LIST_SMALL_ICON_TEXT_VIEW
, "Small icon &view with text\tF6");
167 wxMenu
*menuList
= new wxMenu
;
168 menuList
->Append(LIST_DESELECT_ALL
, "&Deselect All\tCtrl-D");
169 menuList
->Append(LIST_SELECT_ALL
, "S&elect All\tCtrl-A");
170 menuList
->AppendSeparator();
171 menuList
->Append(LIST_SORT
, "&Sort\tCtrl-S");
172 menuList
->AppendSeparator();
173 menuList
->Append(LIST_DELETE_ALL
, "Delete &all items");
174 menuList
->AppendSeparator();
175 menuList
->Append(LIST_TOGGLE_MULTI_SEL
, "&Multiple selection\tCtrl-M",
176 "Toggle multiple selection", TRUE
);
178 wxMenu
*menuCol
= new wxMenu
;
179 menuCol
->Append(LIST_SET_FG_COL
, "&Foreground colour...");
180 menuCol
->Append(LIST_SET_BG_COL
, "&Background colour...");
182 wxMenuBar
*menubar
= new wxMenuBar
;
183 menubar
->Append(menuFile
, "&File");
184 menubar
->Append(menuView
, "&View");
185 menubar
->Append(menuList
, "&List");
186 menubar
->Append(menuCol
, "&Colour");
189 m_listCtrl
= new MyListCtrl(this, LIST_CTRL
,
190 wxDefaultPosition
, wxDefaultSize
,
194 // wxLC_USER_TEXT requires app to supply all
200 m_logWindow
= new wxTextCtrl(this, -1, wxEmptyString
,
201 wxDefaultPosition
, wxDefaultSize
,
202 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
204 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
206 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
207 c
->top
.SameAs (this, wxTop
);
208 c
->left
.SameAs (this, wxLeft
);
209 c
->right
.SameAs (this, wxRight
);
210 c
->height
.PercentOf (this, wxHeight
, 66);
211 m_listCtrl
->SetConstraints(c
);
213 c
= new wxLayoutConstraints
;
214 c
->top
.Below (m_listCtrl
);
215 c
->left
.SameAs (this, wxLeft
);
216 c
->right
.SameAs (this, wxRight
);
217 c
->bottom
.SameAs (this, wxBottom
);
218 m_logWindow
->SetConstraints(c
);
221 for ( int i
= 0; i
< 30; i
++ )
223 long idx
= m_listCtrl
->InsertItem(i
, wxString::Format(_T("Item %d"), i
));
224 m_listCtrl
->SetItemData(idx
, i
*i
);
232 delete wxLog::SetActiveTarget(m_logOld
);
234 delete m_imageListNormal
;
235 delete m_imageListSmall
;
238 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
243 void MyFrame::BusyOn(wxCommandEvent
& WXUNUSED(event
))
248 void MyFrame::BusyOff(wxCommandEvent
& WXUNUSED(event
))
253 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
255 wxMessageDialog
dialog(this, "List test sample\nJulian Smart (c) 1997",
256 "About list test", wxOK
|wxCANCEL
);
261 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
263 int n
= m_listCtrl
->GetItemCount();
264 for (int i
= 0; i
< n
; i
++)
265 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
268 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
270 int n
= m_listCtrl
->GetItemCount();
271 for (int i
= 0; i
< n
; i
++)
272 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
275 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
277 m_listCtrl
->ClearAll();
278 m_logWindow
->Clear();
280 m_listCtrl
->SetSingleStyle(wxLC_LIST
);
281 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_NORMAL
);
282 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_SMALL
);
284 for ( int i
=0; i
< 30; i
++)
287 wxSprintf(buf
, _T("Item %d"), i
);
288 m_listCtrl
->InsertItem(i
, buf
);
292 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
294 m_logWindow
->Clear();
295 m_listCtrl
->ClearAll();
297 m_listCtrl
->SetSingleStyle(wxLC_REPORT
);
298 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_NORMAL
);
299 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
301 m_listCtrl
->InsertColumn(0, "Column 1"); // , wxLIST_FORMAT_LEFT, 140);
302 m_listCtrl
->InsertColumn(1, "Column 2"); // , wxLIST_FORMAT_LEFT, 140);
303 m_listCtrl
->InsertColumn(2, "One More Column (2)"); // , wxLIST_FORMAT_LEFT, 140);
305 // to speed up inserting we hide the control temporarily
311 static const int NUM_ITEMS
= 3000;
312 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
314 buf
.Printf(_T("This is item %d"), i
);
315 long tmp
= m_listCtrl
->InsertItem(i
, buf
, 0);
316 m_listCtrl
->SetItemData(tmp
, i
);
318 buf
.Printf(_T("Col 1, item %d"), i
);
319 tmp
= m_listCtrl
->SetItem(i
, 1, buf
);
321 buf
.Printf(_T("Item %d in column 2"), i
);
322 tmp
= m_listCtrl
->SetItem(i
, 2, buf
);
325 m_logWindow
->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
326 NUM_ITEMS
, sw
.Time()));
329 // we leave all mask fields to 0 and only change the colour
332 item
.SetTextColour(*wxRED
);
333 m_listCtrl
->SetItem( item
);
336 item
.SetTextColour(*wxGREEN
);
337 m_listCtrl
->SetItem( item
);
339 item
.SetTextColour(*wxLIGHT_GREY
);
340 item
.SetFont(*wxITALIC_FONT
);
341 item
.SetBackgroundColour(*wxRED
);
342 m_listCtrl
->SetItem( item
);
344 m_listCtrl
->SetTextColour(*wxBLUE
);
345 m_listCtrl
->SetBackgroundColour(*wxLIGHT_GREY
);
347 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
348 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
349 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
352 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
354 m_logWindow
->Clear();
355 m_listCtrl
->ClearAll();
357 m_listCtrl
->SetSingleStyle(wxLC_ICON
);
358 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
359 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
361 for ( int i
=0; i
< 9; i
++)
363 m_listCtrl
->InsertItem(i
, i
);
367 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
369 m_logWindow
->Clear();
370 m_listCtrl
->ClearAll();
372 m_listCtrl
->SetSingleStyle(wxLC_ICON
);
373 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
374 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
376 for ( int i
=0; i
< 9; i
++)
379 wxSprintf(buf
, _T("Label %d"), i
);
380 m_listCtrl
->InsertItem(i
, buf
, i
);
384 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
386 m_logWindow
->Clear();
387 m_listCtrl
->ClearAll();
389 m_listCtrl
->SetSingleStyle(wxLC_SMALL_ICON
);
390 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
391 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
393 for ( int i
=0; i
< 9; i
++)
395 m_listCtrl
->InsertItem(i
, 0);
399 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
401 m_logWindow
->Clear();
402 m_listCtrl
->ClearAll();
404 m_listCtrl
->SetSingleStyle(wxLC_SMALL_ICON
);
405 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
406 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
408 for ( int i
=0; i
< 9; i
++)
410 m_listCtrl
->InsertItem(i
, "Label", 0);
414 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
418 m_listCtrl
->SortItems(MyCompareFunction
, 0);
420 m_logWindow
->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
421 m_listCtrl
->GetItemCount(),
425 void MyFrame::OnToggleMultiSel(wxCommandEvent
& WXUNUSED(event
))
427 m_logWindow
->WriteText("Current selection mode: ");
429 long flags
= m_listCtrl
->GetWindowStyleFlag();
430 if ( flags
& wxLC_SINGLE_SEL
)
432 m_listCtrl
->SetWindowStyleFlag(flags
& ~wxLC_SINGLE_SEL
);
433 m_logWindow
->WriteText("multiple");
437 m_listCtrl
->SetWindowStyleFlag(flags
| wxLC_SINGLE_SEL
);
438 m_logWindow
->WriteText("single");
441 m_logWindow
->WriteText("\nRecreate the control now\n");
444 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
446 m_listCtrl
->SetForegroundColour(wxGetColourFromUser(this));
447 m_listCtrl
->Refresh();
450 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
452 m_listCtrl
->SetBackgroundColour(wxGetColourFromUser(this));
453 m_listCtrl
->Refresh();
456 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
460 m_listCtrl
->DeleteAllItems();
462 m_logWindow
->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
463 m_listCtrl
->GetItemCount(),
469 void MyListCtrl::OnColClick(wxListEvent
& event
)
471 wxLogMessage( "OnColumnClick at %d.", event
.GetColumn() );
474 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
476 wxLogMessage( "OnBeginDrag at %d,%d.",
477 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
480 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
482 wxLogMessage( "OnBeginRDrag at %d,%d.",
483 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
486 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
488 wxLogMessage("OnBeginLabelEdit: %s", event
.m_item
.m_text
.c_str());
491 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
493 wxLogMessage("OnEndLabelEdit: %s", event
.m_item
.m_text
.c_str());
496 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
498 LogEvent(event
, _T("OnDeleteItem"));
501 void MyListCtrl::OnDeleteAllItems(wxListEvent
& event
)
503 LogEvent(event
, _T("OnDeleteAllItems"));
506 void MyListCtrl::OnGetInfo(wxListEvent
& event
)
510 msg
<< "OnGetInfo (" << event
.m_item
.m_itemId
<< ", " << event
.m_item
.m_col
<< ")";
511 if ( event
.m_item
.m_mask
& wxLIST_MASK_STATE
)
512 msg
<< " wxLIST_MASK_STATE";
513 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
514 msg
<< " wxLIST_MASK_TEXT";
515 if ( event
.m_item
.m_mask
& wxLIST_MASK_IMAGE
)
516 msg
<< " wxLIST_MASK_IMAGE";
517 if ( event
.m_item
.m_mask
& wxLIST_MASK_DATA
)
518 msg
<< " wxLIST_MASK_DATA";
519 if ( event
.m_item
.m_mask
& wxLIST_SET_ITEM
)
520 msg
<< " wxLIST_SET_ITEM";
521 if ( event
.m_item
.m_mask
& wxLIST_MASK_WIDTH
)
522 msg
<< " wxLIST_MASK_WIDTH";
523 if ( event
.m_item
.m_mask
& wxLIST_MASK_FORMAT
)
524 msg
<< " wxLIST_MASK_WIDTH";
526 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
528 event
.m_item
.m_text
= "My callback text";
534 void MyListCtrl::OnSetInfo(wxListEvent
& event
)
536 LogEvent(event
, _T("OnSetInfo"));
539 void MyListCtrl::OnSelected(wxListEvent
& event
)
541 LogEvent(event
, _T("OnSelected"));
543 if ( GetWindowStyle() & wxLC_REPORT
)
546 info
.m_itemId
= event
.m_itemIndex
;
548 info
.m_mask
= wxLIST_MASK_TEXT
;
551 wxLogMessage("Value of the 2nd field of the selected item: %s",
552 info
.m_text
.c_str());
556 wxFAIL_MSG("wxListCtrl::GetItem() failed");
561 void MyListCtrl::OnDeselected(wxListEvent
& event
)
563 LogEvent(event
, _T("OnDeselected"));
566 void MyListCtrl::OnActivated(wxListEvent
& event
)
568 LogEvent(event
, _T("OnActivated"));
571 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
573 LogEvent(event
, _T("OnListKeyDown"));
576 void MyListCtrl::LogEvent(const wxListEvent
& event
, const wxChar
*eventName
)
578 wxLogMessage(_T("Item %ld: %s (item data = %ld)"),
579 event
.GetIndex(), eventName
, event
.GetData());