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"
32 #include "wx/listctrl.h"
33 #include "wx/timer.h" // for wxStopWatch
36 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
37 EVT_MENU(BUSY_ON
, MyFrame::BusyOn
)
38 EVT_MENU(BUSY_OFF
, MyFrame::BusyOff
)
39 EVT_MENU(LIST_QUIT
, MyFrame::OnQuit
)
40 EVT_MENU(LIST_ABOUT
, MyFrame::OnAbout
)
41 EVT_MENU(LIST_LIST_VIEW
, MyFrame::OnListView
)
42 EVT_MENU(LIST_REPORT_VIEW
, MyFrame::OnReportView
)
43 EVT_MENU(LIST_ICON_VIEW
, MyFrame::OnIconView
)
44 EVT_MENU(LIST_ICON_TEXT_VIEW
, MyFrame::OnIconTextView
)
45 EVT_MENU(LIST_SMALL_ICON_VIEW
, MyFrame::OnSmallIconView
)
46 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW
, MyFrame::OnSmallIconTextView
)
47 EVT_MENU(LIST_DESELECT_ALL
, MyFrame::OnDeselectAll
)
48 EVT_MENU(LIST_SELECT_ALL
, MyFrame::OnSelectAll
)
49 EVT_MENU(LIST_DELETE_ALL
, MyFrame::OnDeleteAll
)
50 EVT_MENU(LIST_SORT
, MyFrame::OnSort
)
53 BEGIN_EVENT_TABLE(MyListCtrl
, wxListCtrl
)
54 EVT_LIST_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnBeginDrag
)
55 EVT_LIST_BEGIN_RDRAG(LIST_CTRL
, MyListCtrl::OnBeginRDrag
)
56 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnBeginLabelEdit
)
57 EVT_LIST_END_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnEndLabelEdit
)
58 EVT_LIST_DELETE_ITEM(LIST_CTRL
, MyListCtrl::OnDeleteItem
)
59 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL
, MyListCtrl::OnDeleteAllItems
)
60 EVT_LIST_GET_INFO(LIST_CTRL
, MyListCtrl::OnGetInfo
)
61 EVT_LIST_SET_INFO(LIST_CTRL
, MyListCtrl::OnSetInfo
)
62 EVT_LIST_ITEM_SELECTED(LIST_CTRL
, MyListCtrl::OnSelected
)
63 EVT_LIST_ITEM_DESELECTED(LIST_CTRL
, MyListCtrl::OnDeselected
)
64 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl::OnListKeyDown
)
65 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl::OnActivated
)
66 EVT_LIST_COL_CLICK(LIST_CTRL
, MyListCtrl::OnColClick
)
71 int wxCALLBACK
MyCompareFunction(long item1
, long item2
, long sortData
)
77 // `Main program' equivalent, creating windows and returning main app frame
78 bool MyApp::OnInit(void)
80 // Create the main frame window
81 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, "wxListCtrl Test", 50, 50, 450, 340);
83 // This reduces flicker effects - even better would be to define OnEraseBackground
84 // to do nothing. When the list control's scrollbars are show or hidden, the
85 // frame is sent a background erase event.
86 frame
->SetBackgroundColour( *wxWHITE
);
89 frame
->SetIcon( wxICON(mondrian
) );
91 // Make an image list containing large icons
92 m_imageListNormal
= new wxImageList(32, 32, TRUE
);
93 m_imageListSmall
= new wxImageList(16, 16, TRUE
);
96 m_imageListNormal
->Add( wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE
) );
97 m_imageListNormal
->Add( wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE
) );
98 m_imageListNormal
->Add( wxIcon("icon3", wxBITMAP_TYPE_ICO_RESOURCE
) );
99 m_imageListNormal
->Add( wxIcon("icon4", wxBITMAP_TYPE_ICO_RESOURCE
) );
100 m_imageListNormal
->Add( wxIcon("icon5", wxBITMAP_TYPE_ICO_RESOURCE
) );
101 m_imageListNormal
->Add( wxIcon("icon6", wxBITMAP_TYPE_ICO_RESOURCE
) );
102 m_imageListNormal
->Add( wxIcon("icon7", wxBITMAP_TYPE_ICO_RESOURCE
) );
103 m_imageListNormal
->Add( wxIcon("icon8", wxBITMAP_TYPE_ICO_RESOURCE
) );
104 m_imageListNormal
->Add( wxIcon("icon9", wxBITMAP_TYPE_ICO_RESOURCE
) );
106 m_imageListSmall
->Add( wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE
) );
110 #include "bitmaps/toolbrai.xpm"
111 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
112 #include "bitmaps/toolchar.xpm"
113 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
114 #include "bitmaps/tooldata.xpm"
115 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
116 #include "bitmaps/toolnote.xpm"
117 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
118 #include "bitmaps/tooltodo.xpm"
119 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
120 #include "bitmaps/toolchec.xpm"
121 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
122 #include "bitmaps/toolgame.xpm"
123 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
124 #include "bitmaps/tooltime.xpm"
125 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
126 #include "bitmaps/toolword.xpm"
127 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
129 #include "bitmaps/small1.xpm"
130 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
135 wxMenu
*file_menu
= new wxMenu
;
137 file_menu
->Append(LIST_LIST_VIEW
, "&List view\tF1");
138 file_menu
->Append(LIST_REPORT_VIEW
, "&Report view\tF2");
139 file_menu
->Append(LIST_ICON_VIEW
, "&Icon view\tF3");
140 file_menu
->Append(LIST_ICON_TEXT_VIEW
, "Icon view with &text\tF4");
141 file_menu
->Append(LIST_SMALL_ICON_VIEW
, "&Small icon view\tF5");
142 file_menu
->Append(LIST_SMALL_ICON_TEXT_VIEW
, "Small icon &view with text\tF6");
143 file_menu
->Append(LIST_DESELECT_ALL
, "&Deselect All");
144 file_menu
->Append(LIST_SELECT_ALL
, "S&elect All");
145 file_menu
->AppendSeparator();
146 file_menu
->Append(LIST_SORT
, "&Sort\tCtrl-S");
147 file_menu
->AppendSeparator();
148 file_menu
->Append(LIST_DELETE_ALL
, "Delete &all items");
149 file_menu
->AppendSeparator();
150 file_menu
->Append(BUSY_ON
, "&Busy cursor on");
151 file_menu
->Append(BUSY_OFF
, "&Busy cursor off");
152 file_menu
->AppendSeparator();
153 file_menu
->Append(LIST_ABOUT
, "&About");
154 file_menu
->Append(LIST_QUIT
, "E&xit\tAlt-X");
155 wxMenuBar
*menu_bar
= new wxMenuBar
;
156 menu_bar
->Append(file_menu
, "&File");
157 frame
->SetMenuBar(menu_bar
);
159 // Make a panel with a message
160 frame
->m_listCtrl
= new MyListCtrl(frame
, LIST_CTRL
, wxPoint(0, 0), wxSize(400, 200),
161 wxLC_LIST
|wxSUNKEN_BORDER
|wxLC_EDIT_LABELS
);
162 // wxLC_LIST|wxLC_USER_TEXT|wxSUNKEN_BORDER); // wxLC_USER_TEXT requires app to supply all text on demand
163 frame
->m_logWindow
= new wxTextCtrl(frame
, -1, "", wxPoint(0, 0), wxSize(400, 200), wxTE_MULTILINE
|wxSUNKEN_BORDER
);
165 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
166 c
->top
.SameAs (frame
, wxTop
);
167 c
->left
.SameAs (frame
, wxLeft
);
168 c
->right
.SameAs (frame
, wxRight
);
169 c
->height
.PercentOf (frame
, wxHeight
, 66);
170 frame
->m_listCtrl
->SetConstraints(c
);
172 c
= new wxLayoutConstraints
;
173 c
->top
.Below (frame
->m_listCtrl
);
174 c
->left
.SameAs (frame
, wxLeft
);
175 c
->right
.SameAs (frame
, wxRight
);
176 c
->bottom
.SameAs (frame
, wxBottom
);
177 frame
->m_logWindow
->SetConstraints(c
);
178 frame
->SetAutoLayout(TRUE
);
180 for ( int i
=0; i
< 30; i
++)
183 wxSprintf(buf
, _T("Item %d"), i
);
184 frame
->m_listCtrl
->InsertItem(i
, buf
);
187 frame
->CreateStatusBar(3);
188 frame
->SetStatusText("", 0);
198 // My frame constructor
199 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
200 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
202 m_listCtrl
= (MyListCtrl
*) NULL
;
203 m_logWindow
= (wxTextCtrl
*) NULL
;
206 MyFrame::~MyFrame(void)
208 delete wxGetApp().m_imageListNormal
;
209 delete wxGetApp().m_imageListSmall
;
212 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
217 void MyFrame::BusyOn(wxCommandEvent
& WXUNUSED(event
))
222 void MyFrame::BusyOff(wxCommandEvent
& WXUNUSED(event
))
227 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
229 wxMessageDialog
dialog(this, "List test sample\nJulian Smart (c) 1997",
230 "About list test", wxOK
|wxCANCEL
);
235 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
237 int n
= m_listCtrl
->GetItemCount();
238 for (int i
= 0; i
< n
; i
++)
239 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
242 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
244 int n
= m_listCtrl
->GetItemCount();
245 for (int i
= 0; i
< n
; i
++)
246 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
249 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
251 m_listCtrl
->ClearAll();
252 m_logWindow
->Clear();
254 m_listCtrl
->SetSingleStyle(wxLC_LIST
);
255 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_NORMAL
);
256 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_SMALL
);
258 for ( int i
=0; i
< 30; i
++)
261 wxSprintf(buf
, _T("Item %d"), i
);
262 m_listCtrl
->InsertItem(i
, buf
);
266 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
268 m_logWindow
->Clear();
269 m_listCtrl
->ClearAll();
271 m_listCtrl
->SetSingleStyle(wxLC_REPORT
);
272 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_NORMAL
);
273 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
275 m_listCtrl
->InsertColumn(0, "Column 1"); // , wxLIST_FORMAT_LEFT, 140);
276 m_listCtrl
->InsertColumn(1, "Column 2"); // , wxLIST_FORMAT_LEFT, 140);
277 m_listCtrl
->InsertColumn(2, "One More Column (2)"); // , wxLIST_FORMAT_LEFT, 140);
279 // to speed up inserting we hide the control temporarily
285 static const int NUM_ITEMS
= 3000;
286 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
288 buf
.Printf(_T("This is item %d"), i
);
289 long tmp
= m_listCtrl
->InsertItem(i
, buf
, 0);
290 m_listCtrl
->SetItemData(tmp
, i
);
292 buf
.Printf(_T("Col 1, item %d"), i
);
293 tmp
= m_listCtrl
->SetItem(i
, 1, buf
);
295 buf
.Printf(_T("Item %d in column 2"), i
);
296 tmp
= m_listCtrl
->SetItem(i
, 2, buf
);
299 m_logWindow
->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
300 NUM_ITEMS
, sw
.Time()));
303 // we leave all mask fields to 0 and only change the colour
306 item
.SetTextColour(*wxRED
);
307 m_listCtrl
->SetItem( item
);
310 item
.SetTextColour(*wxGREEN
);
311 m_listCtrl
->SetItem( item
);
313 item
.SetTextColour(*wxLIGHT_GREY
);
314 item
.SetFont(*wxITALIC_FONT
);
315 item
.SetBackgroundColour(*wxRED
);
316 m_listCtrl
->SetItem( item
);
318 m_listCtrl
->SetTextColour(*wxBLUE
);
319 m_listCtrl
->SetBackgroundColour(*wxLIGHT_GREY
);
321 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
322 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
323 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
326 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
328 m_logWindow
->Clear();
329 m_listCtrl
->ClearAll();
331 m_listCtrl
->SetSingleStyle(wxLC_ICON
);
332 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
333 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
335 for ( int i
=0; i
< 9; i
++)
337 m_listCtrl
->InsertItem(i
, i
);
341 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
343 m_logWindow
->Clear();
344 m_listCtrl
->ClearAll();
346 m_listCtrl
->SetSingleStyle(wxLC_ICON
);
347 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
348 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
350 for ( int i
=0; i
< 9; i
++)
353 wxSprintf(buf
, _T("Label %d"), i
);
354 m_listCtrl
->InsertItem(i
, buf
, i
);
358 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
360 m_logWindow
->Clear();
361 m_listCtrl
->ClearAll();
363 m_listCtrl
->SetSingleStyle(wxLC_SMALL_ICON
);
364 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
365 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
367 for ( int i
=0; i
< 9; i
++)
369 m_listCtrl
->InsertItem(i
, 0);
373 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
375 m_logWindow
->Clear();
376 m_listCtrl
->ClearAll();
378 m_listCtrl
->SetSingleStyle(wxLC_SMALL_ICON
);
379 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
380 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
382 for ( int i
=0; i
< 9; i
++)
384 m_listCtrl
->InsertItem(i
, "Label", 0);
388 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
392 m_listCtrl
->SortItems(MyCompareFunction
, 0);
394 m_logWindow
->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
395 m_listCtrl
->GetItemCount(),
399 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
403 m_listCtrl
->DeleteAllItems();
405 m_logWindow
->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
406 m_listCtrl
->GetItemCount(),
412 void MyListCtrl::OnColClick(wxListEvent
& event
)
414 if ( !wxGetApp().GetTopWindow() )
417 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
422 msg
.Printf( "OnColumnClick at %d.\n", event
.GetColumn() );
423 text
->WriteText(msg
);
426 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
428 if ( !wxGetApp().GetTopWindow() )
431 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
436 msg
.Printf( "OnBeginDrag at %d,%d.\n", event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
437 text
->WriteText(msg
);
440 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
442 if ( !wxGetApp().GetTopWindow() )
445 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
450 msg
.Printf( "OnBeginRDrag at %d,%d.\n", event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
451 text
->WriteText(msg
);
454 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
456 if ( !wxGetApp().GetTopWindow() )
459 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
463 text
->WriteText("OnBeginLabelEdit: ");
464 text
->WriteText(event
.m_item
.m_text
);
465 text
->WriteText("\n");
468 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
470 if ( !wxGetApp().GetTopWindow() )
473 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
477 text
->WriteText("OnEndLabelEdit: ");
478 text
->WriteText(event
.m_item
.m_text
);
479 text
->WriteText("\n");
482 void MyListCtrl::OnDeleteItem(wxListEvent
& WXUNUSED(event
))
484 if ( !wxGetApp().GetTopWindow() )
487 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
491 text
->WriteText("OnDeleteItem\n");
494 void MyListCtrl::OnDeleteAllItems(wxListEvent
& WXUNUSED(event
))
496 if ( !wxGetApp().GetTopWindow() )
499 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
503 text
->WriteText("OnDeleteAllItems\n");
506 void MyListCtrl::OnGetInfo(wxListEvent
& event
)
508 if ( !wxGetApp().GetTopWindow() )
511 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
515 text
->WriteText("OnGetInfo\n");
517 (*text
) << "OnGetInfo (" << event
.m_item
.m_itemId
<< ", " << event
.m_item
.m_col
<< ")";
518 if ( event
.m_item
.m_mask
& wxLIST_MASK_STATE
)
519 (*text
) << " wxLIST_MASK_STATE";
520 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
521 (*text
) << " wxLIST_MASK_TEXT";
522 if ( event
.m_item
.m_mask
& wxLIST_MASK_IMAGE
)
523 (*text
) << " wxLIST_MASK_IMAGE";
524 if ( event
.m_item
.m_mask
& wxLIST_MASK_DATA
)
525 (*text
) << " wxLIST_MASK_DATA";
526 if ( event
.m_item
.m_mask
& wxLIST_SET_ITEM
)
527 (*text
) << " wxLIST_SET_ITEM";
528 if ( event
.m_item
.m_mask
& wxLIST_MASK_WIDTH
)
529 (*text
) << " wxLIST_MASK_WIDTH";
530 if ( event
.m_item
.m_mask
& wxLIST_MASK_FORMAT
)
531 (*text
) << " wxLIST_MASK_WIDTH";
533 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
535 event
.m_item
.m_text
= "My callback text";
540 void MyListCtrl::OnSetInfo(wxListEvent
& WXUNUSED(event
))
542 if ( !wxGetApp().GetTopWindow() )
545 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
549 text
->WriteText("OnSetInfo\n");
552 void MyListCtrl::OnSelected(wxListEvent
& event
)
554 if ( !wxGetApp().GetTopWindow() )
557 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
561 if ( GetWindowStyle() & wxLC_REPORT
)
564 info
.m_itemId
= event
.m_itemIndex
;
566 info
.m_mask
= wxLIST_MASK_TEXT
;
569 *text
<< "Value of the 2nd field of the selected item: "
570 << info
.m_text
<< '\n';
574 wxFAIL_MSG("wxListCtrl::GetItem() failed");
578 text
->WriteText("OnSelected\n");
581 void MyListCtrl::OnDeselected(wxListEvent
& WXUNUSED(event
))
583 if ( !wxGetApp().GetTopWindow() )
586 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
590 text
->WriteText("OnDeselected\n");
593 void MyListCtrl::OnActivated(wxListEvent
& WXUNUSED(event
))
595 if ( !wxGetApp().GetTopWindow() )
598 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
602 text
->WriteText("OnActivated\n");
605 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
607 if ( !wxGetApp().GetTopWindow() )
610 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
614 text
->WriteText("OnListKeyDown\n");