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/imaglist.h"
44 #include "wx/listctrl.h"
45 #include "wx/timer.h" // for wxStopWatch
46 #include "wx/colordlg.h" // for wxGetColourFromUser
50 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
51 EVT_MENU(BUSY_ON
, MyFrame::BusyOn
)
52 EVT_MENU(BUSY_OFF
, MyFrame::BusyOff
)
53 EVT_MENU(LIST_QUIT
, MyFrame::OnQuit
)
54 EVT_MENU(LIST_ABOUT
, MyFrame::OnAbout
)
55 EVT_MENU(LIST_LIST_VIEW
, MyFrame::OnListView
)
56 EVT_MENU(LIST_REPORT_VIEW
, MyFrame::OnReportView
)
57 EVT_MENU(LIST_ICON_VIEW
, MyFrame::OnIconView
)
58 EVT_MENU(LIST_ICON_TEXT_VIEW
, MyFrame::OnIconTextView
)
59 EVT_MENU(LIST_SMALL_ICON_VIEW
, MyFrame::OnSmallIconView
)
60 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW
, MyFrame::OnSmallIconTextView
)
61 EVT_MENU(LIST_TOGGLE_FIRST
, MyFrame::OnToggleFirstSel
)
62 EVT_MENU(LIST_DESELECT_ALL
, MyFrame::OnDeselectAll
)
63 EVT_MENU(LIST_SELECT_ALL
, MyFrame::OnSelectAll
)
64 EVT_MENU(LIST_DELETE_ALL
, MyFrame::OnDeleteAll
)
65 EVT_MENU(LIST_SORT
, MyFrame::OnSort
)
66 EVT_MENU(LIST_SET_FG_COL
, MyFrame::OnSetFgColour
)
67 EVT_MENU(LIST_SET_BG_COL
, MyFrame::OnSetBgColour
)
68 EVT_MENU(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnToggleMultiSel
)
71 BEGIN_EVENT_TABLE(MyListCtrl
, wxListCtrl
)
72 EVT_LIST_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnBeginDrag
)
73 EVT_LIST_BEGIN_RDRAG(LIST_CTRL
, MyListCtrl::OnBeginRDrag
)
74 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnBeginLabelEdit
)
75 EVT_LIST_END_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnEndLabelEdit
)
76 EVT_LIST_DELETE_ITEM(LIST_CTRL
, MyListCtrl::OnDeleteItem
)
77 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL
, MyListCtrl::OnDeleteAllItems
)
78 EVT_LIST_GET_INFO(LIST_CTRL
, MyListCtrl::OnGetInfo
)
79 EVT_LIST_SET_INFO(LIST_CTRL
, MyListCtrl::OnSetInfo
)
80 EVT_LIST_ITEM_SELECTED(LIST_CTRL
, MyListCtrl::OnSelected
)
81 EVT_LIST_ITEM_DESELECTED(LIST_CTRL
, MyListCtrl::OnDeselected
)
82 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl::OnListKeyDown
)
83 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl::OnActivated
)
84 EVT_LIST_COL_CLICK(LIST_CTRL
, MyListCtrl::OnColClick
)
89 int wxCALLBACK
MyCompareFunction(long item1
, long item2
, long sortData
)
95 // `Main program' equivalent, creating windows and returning main app frame
98 // Create the main frame window
99 MyFrame
*frame
= new MyFrame("wxListCtrl Test", 50, 50, 450, 340);
109 // My frame constructor
110 MyFrame::MyFrame(const wxChar
*title
, int x
, int y
, int w
, int h
)
111 : wxFrame((wxFrame
*)NULL
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
113 m_listCtrl
= (MyListCtrl
*) NULL
;
114 m_logWindow
= (wxTextCtrl
*) NULL
;
117 SetIcon( wxICON(mondrian
) );
119 // Make an image list containing large icons
120 m_imageListNormal
= new wxImageList(32, 32, TRUE
);
121 m_imageListSmall
= new wxImageList(16, 16, TRUE
);
124 m_imageListNormal
->Add( wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE
) );
125 m_imageListNormal
->Add( wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE
) );
126 m_imageListNormal
->Add( wxIcon("icon3", wxBITMAP_TYPE_ICO_RESOURCE
) );
127 m_imageListNormal
->Add( wxIcon("icon4", wxBITMAP_TYPE_ICO_RESOURCE
) );
128 m_imageListNormal
->Add( wxIcon("icon5", wxBITMAP_TYPE_ICO_RESOURCE
) );
129 m_imageListNormal
->Add( wxIcon("icon6", wxBITMAP_TYPE_ICO_RESOURCE
) );
130 m_imageListNormal
->Add( wxIcon("icon7", wxBITMAP_TYPE_ICO_RESOURCE
) );
131 m_imageListNormal
->Add( wxIcon("icon8", wxBITMAP_TYPE_ICO_RESOURCE
) );
132 m_imageListNormal
->Add( wxIcon("icon9", wxBITMAP_TYPE_ICO_RESOURCE
) );
134 m_imageListSmall
->Add( wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE
) );
137 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
138 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
139 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
140 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
141 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
142 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
143 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
144 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
145 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
147 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
151 wxMenu
*menuFile
= new wxMenu
;
152 menuFile
->Append(LIST_ABOUT
, "&About");
153 menuFile
->AppendSeparator();
154 #if 0 // what is this for? (VZ)
155 menuFile
->Append(BUSY_ON
, "&Busy cursor on");
156 menuFile
->Append(BUSY_OFF
, "&Busy cursor off");
157 menuFile
->AppendSeparator();
159 menuFile
->Append(LIST_QUIT
, "E&xit\tAlt-X");
161 wxMenu
*menuView
= new wxMenu
;
162 menuView
->Append(LIST_LIST_VIEW
, "&List view\tF1");
163 menuView
->Append(LIST_REPORT_VIEW
, "&Report view\tF2");
164 menuView
->Append(LIST_ICON_VIEW
, "&Icon view\tF3");
165 menuView
->Append(LIST_ICON_TEXT_VIEW
, "Icon view with &text\tF4");
166 menuView
->Append(LIST_SMALL_ICON_VIEW
, "&Small icon view\tF5");
167 menuView
->Append(LIST_SMALL_ICON_TEXT_VIEW
, "Small icon &view with text\tF6");
169 wxMenu
*menuList
= new wxMenu
;
170 menuList
->Append(LIST_TOGGLE_FIRST
, "&Toggle first item\tCtrl-T");
171 menuList
->Append(LIST_DESELECT_ALL
, "&Deselect All\tCtrl-D");
172 menuList
->Append(LIST_SELECT_ALL
, "S&elect All\tCtrl-A");
173 menuList
->AppendSeparator();
174 menuList
->Append(LIST_SORT
, "&Sort\tCtrl-S");
175 menuList
->AppendSeparator();
176 menuList
->Append(LIST_DELETE_ALL
, "Delete &all items");
177 menuList
->AppendSeparator();
178 menuList
->Append(LIST_TOGGLE_MULTI_SEL
, "&Multiple selection\tCtrl-M",
179 "Toggle multiple selection", TRUE
);
181 wxMenu
*menuCol
= new wxMenu
;
182 menuCol
->Append(LIST_SET_FG_COL
, "&Foreground colour...");
183 menuCol
->Append(LIST_SET_BG_COL
, "&Background colour...");
185 wxMenuBar
*menubar
= new wxMenuBar
;
186 menubar
->Append(menuFile
, "&File");
187 menubar
->Append(menuView
, "&View");
188 menubar
->Append(menuList
, "&List");
189 menubar
->Append(menuCol
, "&Colour");
192 m_listCtrl
= new MyListCtrl(this, LIST_CTRL
,
193 wxDefaultPosition
, wxDefaultSize
,
197 // wxLC_USER_TEXT requires app to supply all
203 m_logWindow
= new wxTextCtrl(this, -1, wxEmptyString
,
204 wxDefaultPosition
, wxDefaultSize
,
205 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
207 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
209 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
210 c
->top
.SameAs (this, wxTop
);
211 c
->left
.SameAs (this, wxLeft
);
212 c
->right
.SameAs (this, wxRight
);
213 c
->height
.PercentOf (this, wxHeight
, 66);
214 m_listCtrl
->SetConstraints(c
);
216 c
= new wxLayoutConstraints
;
217 c
->top
.Below (m_listCtrl
);
218 c
->left
.SameAs (this, wxLeft
);
219 c
->right
.SameAs (this, wxRight
);
220 c
->bottom
.SameAs (this, wxBottom
);
221 m_logWindow
->SetConstraints(c
);
224 for ( int i
= 0; i
< 30; i
++ )
226 long idx
= m_listCtrl
->InsertItem(i
, wxString::Format(_T("Item %d"), i
));
227 m_listCtrl
->SetItemData(idx
, i
*i
);
235 delete wxLog::SetActiveTarget(m_logOld
);
237 delete m_imageListNormal
;
238 delete m_imageListSmall
;
241 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
246 void MyFrame::BusyOn(wxCommandEvent
& WXUNUSED(event
))
251 void MyFrame::BusyOff(wxCommandEvent
& WXUNUSED(event
))
256 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
258 wxMessageDialog
dialog(this, "List test sample\nJulian Smart (c) 1997",
259 "About list test", wxOK
|wxCANCEL
);
264 void MyFrame::OnToggleFirstSel(wxCommandEvent
& WXUNUSED(event
))
266 m_listCtrl
->SetItemState(0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
269 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
271 int n
= m_listCtrl
->GetItemCount();
272 for (int i
= 0; i
< n
; i
++)
273 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
276 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
278 int n
= m_listCtrl
->GetItemCount();
279 for (int i
= 0; i
< n
; i
++)
280 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
283 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
285 m_listCtrl
->ClearAll();
286 m_logWindow
->Clear();
288 m_listCtrl
->SetSingleStyle(wxLC_LIST
);
289 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_NORMAL
);
290 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_SMALL
);
292 for ( int i
=0; i
< 30; i
++)
295 wxSprintf(buf
, _T("Item %d"), i
);
296 m_listCtrl
->InsertItem(i
, buf
);
300 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
302 m_logWindow
->Clear();
303 m_listCtrl
->ClearAll();
305 m_listCtrl
->SetSingleStyle(wxLC_REPORT
);
306 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_NORMAL
);
307 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
309 m_listCtrl
->InsertColumn(0, "Column 1"); // , wxLIST_FORMAT_LEFT, 140);
310 m_listCtrl
->InsertColumn(1, "Column 2"); // , wxLIST_FORMAT_LEFT, 140);
311 m_listCtrl
->InsertColumn(2, "One More Column (2)"); // , wxLIST_FORMAT_LEFT, 140);
313 // to speed up inserting we hide the control temporarily
319 static const int NUM_ITEMS
= 3000;
320 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
322 buf
.Printf(_T("This is item %d"), i
);
323 long tmp
= m_listCtrl
->InsertItem(i
, buf
, 0);
324 m_listCtrl
->SetItemData(tmp
, i
);
326 buf
.Printf(_T("Col 1, item %d"), i
);
327 tmp
= m_listCtrl
->SetItem(i
, 1, buf
);
329 buf
.Printf(_T("Item %d in column 2"), i
);
330 tmp
= m_listCtrl
->SetItem(i
, 2, buf
);
333 m_logWindow
->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
334 NUM_ITEMS
, sw
.Time()));
337 // we leave all mask fields to 0 and only change the colour
340 item
.SetTextColour(*wxRED
);
341 m_listCtrl
->SetItem( item
);
344 item
.SetTextColour(*wxGREEN
);
345 m_listCtrl
->SetItem( item
);
347 item
.SetTextColour(*wxLIGHT_GREY
);
348 item
.SetFont(*wxITALIC_FONT
);
349 item
.SetBackgroundColour(*wxRED
);
350 m_listCtrl
->SetItem( item
);
352 m_listCtrl
->SetTextColour(*wxBLUE
);
353 m_listCtrl
->SetBackgroundColour(*wxLIGHT_GREY
);
355 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
356 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
357 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
360 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
362 m_logWindow
->Clear();
363 m_listCtrl
->ClearAll();
365 m_listCtrl
->SetSingleStyle(wxLC_ICON
);
366 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
367 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
369 for ( int i
=0; i
< 9; i
++)
371 m_listCtrl
->InsertItem(i
, i
);
375 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
377 m_logWindow
->Clear();
378 m_listCtrl
->ClearAll();
380 m_listCtrl
->SetSingleStyle(wxLC_ICON
);
381 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
382 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
384 for ( int i
=0; i
< 9; i
++)
387 wxSprintf(buf
, _T("Label %d"), i
);
388 m_listCtrl
->InsertItem(i
, buf
, i
);
392 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
394 m_logWindow
->Clear();
395 m_listCtrl
->ClearAll();
397 m_listCtrl
->SetSingleStyle(wxLC_SMALL_ICON
);
398 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
399 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
401 for ( int i
=0; i
< 9; i
++)
403 m_listCtrl
->InsertItem(i
, 0);
407 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
409 m_logWindow
->Clear();
410 m_listCtrl
->ClearAll();
412 m_listCtrl
->SetSingleStyle(wxLC_SMALL_ICON
);
413 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
414 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
416 for ( int i
=0; i
< 9; i
++)
418 m_listCtrl
->InsertItem(i
, "Label", 0);
422 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
426 m_listCtrl
->SortItems(MyCompareFunction
, 0);
428 m_logWindow
->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
429 m_listCtrl
->GetItemCount(),
433 void MyFrame::OnToggleMultiSel(wxCommandEvent
& WXUNUSED(event
))
435 m_logWindow
->WriteText("Current selection mode: ");
437 long flags
= m_listCtrl
->GetWindowStyleFlag();
438 if ( flags
& wxLC_SINGLE_SEL
)
440 m_listCtrl
->SetWindowStyleFlag(flags
& ~wxLC_SINGLE_SEL
);
441 m_logWindow
->WriteText("multiple");
445 m_listCtrl
->SetWindowStyleFlag(flags
| wxLC_SINGLE_SEL
);
446 m_logWindow
->WriteText("single");
449 m_logWindow
->WriteText("\nRecreate the control now\n");
452 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
454 m_listCtrl
->SetForegroundColour(wxGetColourFromUser(this));
455 m_listCtrl
->Refresh();
458 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
460 m_listCtrl
->SetBackgroundColour(wxGetColourFromUser(this));
461 m_listCtrl
->Refresh();
464 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
468 m_listCtrl
->DeleteAllItems();
470 m_logWindow
->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
471 m_listCtrl
->GetItemCount(),
477 void MyListCtrl::OnColClick(wxListEvent
& event
)
479 wxLogMessage( "OnColumnClick at %d.", event
.GetColumn() );
482 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
484 wxLogMessage( "OnBeginDrag at %d,%d.",
485 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
488 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
490 wxLogMessage( "OnBeginRDrag at %d,%d.",
491 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
494 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
496 wxLogMessage("OnBeginLabelEdit: %s", event
.m_item
.m_text
.c_str());
499 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
501 wxLogMessage("OnEndLabelEdit: %s", event
.m_item
.m_text
.c_str());
504 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
506 LogEvent(event
, _T("OnDeleteItem"));
509 void MyListCtrl::OnDeleteAllItems(wxListEvent
& event
)
511 LogEvent(event
, _T("OnDeleteAllItems"));
514 void MyListCtrl::OnGetInfo(wxListEvent
& event
)
518 msg
<< "OnGetInfo (" << event
.m_item
.m_itemId
<< ", " << event
.m_item
.m_col
<< ")";
519 if ( event
.m_item
.m_mask
& wxLIST_MASK_STATE
)
520 msg
<< " wxLIST_MASK_STATE";
521 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
522 msg
<< " wxLIST_MASK_TEXT";
523 if ( event
.m_item
.m_mask
& wxLIST_MASK_IMAGE
)
524 msg
<< " wxLIST_MASK_IMAGE";
525 if ( event
.m_item
.m_mask
& wxLIST_MASK_DATA
)
526 msg
<< " wxLIST_MASK_DATA";
527 if ( event
.m_item
.m_mask
& wxLIST_SET_ITEM
)
528 msg
<< " wxLIST_SET_ITEM";
529 if ( event
.m_item
.m_mask
& wxLIST_MASK_WIDTH
)
530 msg
<< " wxLIST_MASK_WIDTH";
531 if ( event
.m_item
.m_mask
& wxLIST_MASK_FORMAT
)
532 msg
<< " wxLIST_MASK_WIDTH";
534 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
536 event
.m_item
.m_text
= "My callback text";
542 void MyListCtrl::OnSetInfo(wxListEvent
& event
)
544 LogEvent(event
, _T("OnSetInfo"));
547 void MyListCtrl::OnSelected(wxListEvent
& event
)
549 LogEvent(event
, _T("OnSelected"));
551 if ( GetWindowStyle() & wxLC_REPORT
)
554 info
.m_itemId
= event
.m_itemIndex
;
556 info
.m_mask
= wxLIST_MASK_TEXT
;
559 wxLogMessage("Value of the 2nd field of the selected item: %s",
560 info
.m_text
.c_str());
564 wxFAIL_MSG("wxListCtrl::GetItem() failed");
569 void MyListCtrl::OnDeselected(wxListEvent
& event
)
571 LogEvent(event
, _T("OnDeselected"));
574 void MyListCtrl::OnActivated(wxListEvent
& event
)
576 LogEvent(event
, _T("OnActivated"));
579 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
581 LogEvent(event
, _T("OnListKeyDown"));
584 void MyListCtrl::LogEvent(const wxListEvent
& event
, const wxChar
*eventName
)
586 wxLogMessage(_T("Item %ld: %s (item data = %ld)"),
587 event
.GetIndex(), eventName
, event
.GetData());