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_GET_INFO(LIST_CTRL
, MyListCtrl::OnGetInfo
)
60 EVT_LIST_SET_INFO(LIST_CTRL
, MyListCtrl::OnSetInfo
)
61 EVT_LIST_ITEM_SELECTED(LIST_CTRL
, MyListCtrl::OnSelected
)
62 EVT_LIST_ITEM_DESELECTED(LIST_CTRL
, MyListCtrl::OnDeselected
)
63 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl::OnListKeyDown
)
64 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl::OnActivated
)
65 EVT_LIST_COL_CLICK(LIST_CTRL
, MyListCtrl::OnColClick
)
70 int wxCALLBACK
MyCompareFunction(long item1
, long item2
, long sortData
)
76 // `Main program' equivalent, creating windows and returning main app frame
77 bool MyApp::OnInit(void)
79 // Create the main frame window
80 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, "wxListCtrl Test", 50, 50, 450, 340);
82 // This reduces flicker effects - even better would be to define OnEraseBackground
83 // to do nothing. When the list control's scrollbars are show or hidden, the
84 // frame is sent a background erase event.
85 frame
->SetBackgroundColour( *wxWHITE
);
88 frame
->SetIcon( wxICON(mondrian
) );
90 // Make an image list containing large icons
91 m_imageListNormal
= new wxImageList(32, 32, TRUE
);
92 m_imageListSmall
= new wxImageList(16, 16, TRUE
);
95 m_imageListNormal
->Add( wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE
) );
96 m_imageListNormal
->Add( wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE
) );
97 m_imageListNormal
->Add( wxIcon("icon3", wxBITMAP_TYPE_ICO_RESOURCE
) );
98 m_imageListNormal
->Add( wxIcon("icon4", wxBITMAP_TYPE_ICO_RESOURCE
) );
99 m_imageListNormal
->Add( wxIcon("icon5", wxBITMAP_TYPE_ICO_RESOURCE
) );
100 m_imageListNormal
->Add( wxIcon("icon6", wxBITMAP_TYPE_ICO_RESOURCE
) );
101 m_imageListNormal
->Add( wxIcon("icon7", wxBITMAP_TYPE_ICO_RESOURCE
) );
102 m_imageListNormal
->Add( wxIcon("icon8", wxBITMAP_TYPE_ICO_RESOURCE
) );
103 m_imageListNormal
->Add( wxIcon("icon9", wxBITMAP_TYPE_ICO_RESOURCE
) );
105 m_imageListSmall
->Add( wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE
) );
109 #include "bitmaps/toolbrai.xpm"
110 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
111 #include "bitmaps/toolchar.xpm"
112 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
113 #include "bitmaps/tooldata.xpm"
114 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
115 #include "bitmaps/toolnote.xpm"
116 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
117 #include "bitmaps/tooltodo.xpm"
118 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
119 #include "bitmaps/toolchec.xpm"
120 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
121 #include "bitmaps/toolgame.xpm"
122 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
123 #include "bitmaps/tooltime.xpm"
124 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
125 #include "bitmaps/toolword.xpm"
126 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
128 #include "bitmaps/small1.xpm"
129 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
134 wxMenu
*file_menu
= new wxMenu
;
136 file_menu
->Append(LIST_LIST_VIEW
, "&List view\tF1");
137 file_menu
->Append(LIST_REPORT_VIEW
, "&Report view\tF2");
138 file_menu
->Append(LIST_ICON_VIEW
, "&Icon view\tF3");
139 file_menu
->Append(LIST_ICON_TEXT_VIEW
, "Icon view with &text\tF4");
140 file_menu
->Append(LIST_SMALL_ICON_VIEW
, "&Small icon view\tF5");
141 file_menu
->Append(LIST_SMALL_ICON_TEXT_VIEW
, "Small icon &view with text\tF6");
142 file_menu
->Append(LIST_DESELECT_ALL
, "&Deselect All");
143 file_menu
->Append(LIST_SELECT_ALL
, "S&elect All");
144 file_menu
->AppendSeparator();
145 file_menu
->Append(LIST_SORT
, "&Sort\tCtrl-S");
146 file_menu
->AppendSeparator();
147 file_menu
->Append(LIST_DELETE_ALL
, "Delete &all items");
148 file_menu
->AppendSeparator();
149 file_menu
->Append(BUSY_ON
, "&Busy cursor on");
150 file_menu
->Append(BUSY_OFF
, "&Busy cursor off");
151 file_menu
->AppendSeparator();
152 file_menu
->Append(LIST_ABOUT
, "&About");
153 file_menu
->Append(LIST_QUIT
, "E&xit\tAlt-X");
154 wxMenuBar
*menu_bar
= new wxMenuBar
;
155 menu_bar
->Append(file_menu
, "&File");
156 frame
->SetMenuBar(menu_bar
);
158 // Make a panel with a message
159 frame
->m_listCtrl
= new MyListCtrl(frame
, LIST_CTRL
, wxPoint(0, 0), wxSize(400, 200),
160 wxLC_LIST
|wxSUNKEN_BORDER
|wxLC_EDIT_LABELS
);
161 // wxLC_LIST|wxLC_USER_TEXT|wxSUNKEN_BORDER); // wxLC_USER_TEXT requires app to supply all text on demand
162 frame
->m_logWindow
= new wxTextCtrl(frame
, -1, "", wxPoint(0, 0), wxSize(400, 200), wxTE_MULTILINE
|wxSUNKEN_BORDER
);
164 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
165 c
->top
.SameAs (frame
, wxTop
);
166 c
->left
.SameAs (frame
, wxLeft
);
167 c
->right
.SameAs (frame
, wxRight
);
168 c
->height
.PercentOf (frame
, wxHeight
, 66);
169 frame
->m_listCtrl
->SetConstraints(c
);
171 c
= new wxLayoutConstraints
;
172 c
->top
.Below (frame
->m_listCtrl
);
173 c
->left
.SameAs (frame
, wxLeft
);
174 c
->right
.SameAs (frame
, wxRight
);
175 c
->bottom
.SameAs (frame
, wxBottom
);
176 frame
->m_logWindow
->SetConstraints(c
);
177 frame
->SetAutoLayout(TRUE
);
179 for ( int i
=0; i
< 30; i
++)
182 wxSprintf(buf
, _T("Item %d"), i
);
183 frame
->m_listCtrl
->InsertItem(i
, buf
);
186 frame
->CreateStatusBar(3);
187 frame
->SetStatusText("", 0);
197 // My frame constructor
198 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
199 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
201 m_listCtrl
= (MyListCtrl
*) NULL
;
202 m_logWindow
= (wxTextCtrl
*) NULL
;
205 MyFrame::~MyFrame(void)
207 delete wxGetApp().m_imageListNormal
;
208 delete wxGetApp().m_imageListSmall
;
211 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
216 void MyFrame::BusyOn(wxCommandEvent
& WXUNUSED(event
))
221 void MyFrame::BusyOff(wxCommandEvent
& WXUNUSED(event
))
226 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
228 wxMessageDialog
dialog(this, "List test sample\nJulian Smart (c) 1997",
229 "About list test", wxOK
|wxCANCEL
);
234 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
236 int n
= m_listCtrl
->GetItemCount();
237 for (int i
= 0; i
< n
; i
++)
238 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
241 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
243 int n
= m_listCtrl
->GetItemCount();
244 for (int i
= 0; i
< n
; i
++)
245 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
248 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
250 m_listCtrl
->DeleteAllItems();
251 m_logWindow
->Clear();
252 m_listCtrl
->SetSingleStyle(wxLC_LIST
);
253 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_NORMAL
);
254 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_SMALL
);
256 for ( int i
=0; i
< 30; i
++)
259 wxSprintf(buf
, _T("Item %d"), i
);
260 m_listCtrl
->InsertItem(i
, buf
);
264 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
266 m_listCtrl
->DeleteAllItems();
267 m_listCtrl
->DeleteAllColumns();
268 m_logWindow
->Clear();
270 m_listCtrl
->SetSingleStyle(wxLC_REPORT
);
271 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_NORMAL
);
272 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
274 m_listCtrl
->InsertColumn(0, "Column 1"); // , wxLIST_FORMAT_LEFT, 140);
275 m_listCtrl
->InsertColumn(1, "Column 2"); // , wxLIST_FORMAT_LEFT, 140);
276 m_listCtrl
->InsertColumn(2, "One More Column (2)"); // , wxLIST_FORMAT_LEFT, 140);
278 // to speed up inserting we hide the control temporarily
284 static const int NUM_ITEMS
= 3000;
285 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
287 buf
.Printf(_T("This is item %d"), i
);
288 long tmp
= m_listCtrl
->InsertItem(i
, buf
, 0);
289 m_listCtrl
->SetItemData(tmp
, i
);
291 buf
.Printf(_T("Col 1, item %d"), i
);
292 tmp
= m_listCtrl
->SetItem(i
, 1, buf
);
294 buf
.Printf(_T("Item %d in column 2"), i
);
295 tmp
= m_listCtrl
->SetItem(i
, 2, buf
);
298 m_logWindow
->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
299 NUM_ITEMS
, sw
.Time()));
302 // we leave all mask fields to 0 and only change the colour
305 item
.SetTextColour(*wxRED
);
306 m_listCtrl
->SetItem( item
);
309 item
.SetTextColour(*wxGREEN
);
310 m_listCtrl
->SetItem( item
);
312 item
.SetTextColour(*wxLIGHT_GREY
);
313 item
.SetFont(*wxITALIC_FONT
);
314 item
.SetBackgroundColour(*wxRED
);
315 m_listCtrl
->SetItem( item
);
317 m_listCtrl
->SetTextColour(*wxBLUE
);
318 m_listCtrl
->SetBackgroundColour(*wxLIGHT_GREY
);
320 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
321 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
322 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
325 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
327 m_listCtrl
->DeleteAllItems();
328 m_logWindow
->Clear();
329 m_listCtrl
->SetSingleStyle(wxLC_ICON
);
330 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
331 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
333 for ( int i
=0; i
< 9; i
++)
335 m_listCtrl
->InsertItem(i
, i
);
339 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
341 m_listCtrl
->DeleteAllItems();
342 m_logWindow
->Clear();
343 m_listCtrl
->SetSingleStyle(wxLC_ICON
);
344 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
345 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
347 for ( int i
=0; i
< 9; i
++)
350 wxSprintf(buf
, _T("Label %d"), i
);
351 m_listCtrl
->InsertItem(i
, buf
, i
);
355 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
357 m_listCtrl
->DeleteAllItems();
358 m_logWindow
->Clear();
359 m_listCtrl
->SetSingleStyle(wxLC_SMALL_ICON
);
360 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
361 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
363 for ( int i
=0; i
< 9; i
++)
365 m_listCtrl
->InsertItem(i
, 0);
369 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
371 m_listCtrl
->DeleteAllItems();
372 m_logWindow
->Clear();
373 m_listCtrl
->SetSingleStyle(wxLC_SMALL_ICON
);
374 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
375 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
377 for ( int i
=0; i
< 9; i
++)
379 m_listCtrl
->InsertItem(i
, "Label", 0);
383 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
387 m_listCtrl
->SortItems(MyCompareFunction
, 0);
389 m_logWindow
->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
390 m_listCtrl
->GetItemCount(),
394 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
398 m_listCtrl
->DeleteAllItems();
400 m_logWindow
->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
401 m_listCtrl
->GetItemCount(),
407 void MyListCtrl::OnColClick(wxListEvent
& event
)
409 if ( !wxGetApp().GetTopWindow() )
412 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
417 msg
.Printf( "OnColumnClick at %d.\n", event
.GetColumn() );
418 text
->WriteText(msg
);
421 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
423 if ( !wxGetApp().GetTopWindow() )
426 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
431 msg
.Printf( "OnBeginDrag at %d,%d.\n", event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
432 text
->WriteText(msg
);
435 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
437 if ( !wxGetApp().GetTopWindow() )
440 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
445 msg
.Printf( "OnBeginRDrag at %d,%d.\n", event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
446 text
->WriteText(msg
);
449 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
451 if ( !wxGetApp().GetTopWindow() )
454 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
458 text
->WriteText("OnBeginLabelEdit: ");
459 text
->WriteText(event
.m_item
.m_text
);
460 text
->WriteText("\n");
463 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
465 if ( !wxGetApp().GetTopWindow() )
468 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
472 text
->WriteText("OnEndLabelEdit: ");
473 text
->WriteText(event
.m_item
.m_text
);
474 text
->WriteText("\n");
477 void MyListCtrl::OnDeleteItem(wxListEvent
& WXUNUSED(event
))
479 if ( !wxGetApp().GetTopWindow() )
482 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
486 text
->WriteText("OnDeleteItem\n");
489 void MyListCtrl::OnGetInfo(wxListEvent
& event
)
491 if ( !wxGetApp().GetTopWindow() )
494 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
498 text
->WriteText("OnGetInfo\n");
500 (*text
) << "OnGetInfo (" << event
.m_item
.m_itemId
<< ", " << event
.m_item
.m_col
<< ")";
501 if ( event
.m_item
.m_mask
& wxLIST_MASK_STATE
)
502 (*text
) << " wxLIST_MASK_STATE";
503 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
504 (*text
) << " wxLIST_MASK_TEXT";
505 if ( event
.m_item
.m_mask
& wxLIST_MASK_IMAGE
)
506 (*text
) << " wxLIST_MASK_IMAGE";
507 if ( event
.m_item
.m_mask
& wxLIST_MASK_DATA
)
508 (*text
) << " wxLIST_MASK_DATA";
509 if ( event
.m_item
.m_mask
& wxLIST_SET_ITEM
)
510 (*text
) << " wxLIST_SET_ITEM";
511 if ( event
.m_item
.m_mask
& wxLIST_MASK_WIDTH
)
512 (*text
) << " wxLIST_MASK_WIDTH";
513 if ( event
.m_item
.m_mask
& wxLIST_MASK_FORMAT
)
514 (*text
) << " wxLIST_MASK_WIDTH";
516 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
518 event
.m_item
.m_text
= "My callback text";
523 void MyListCtrl::OnSetInfo(wxListEvent
& WXUNUSED(event
))
525 if ( !wxGetApp().GetTopWindow() )
528 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
532 text
->WriteText("OnSetInfo\n");
535 void MyListCtrl::OnSelected(wxListEvent
& event
)
537 if ( !wxGetApp().GetTopWindow() )
540 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
544 if ( GetWindowStyle() & wxLC_REPORT
)
547 info
.m_itemId
= event
.m_itemIndex
;
549 info
.m_mask
= wxLIST_MASK_TEXT
;
552 *text
<< "Value of the 2nd field of the selected item: "
553 << info
.m_text
<< '\n';
557 wxFAIL_MSG("wxListCtrl::GetItem() failed");
561 text
->WriteText("OnSelected\n");
564 void MyListCtrl::OnDeselected(wxListEvent
& WXUNUSED(event
))
566 if ( !wxGetApp().GetTopWindow() )
569 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
573 text
->WriteText("OnDeselected\n");
576 void MyListCtrl::OnActivated(wxListEvent
& WXUNUSED(event
))
578 if ( !wxGetApp().GetTopWindow() )
581 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
585 text
->WriteText("OnActivated\n");
588 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
590 if ( !wxGetApp().GetTopWindow() )
593 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
597 text
->WriteText("OnListKeyDown\n");