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"
35 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
36 EVT_MENU(BUSY_ON
, MyFrame::BusyOn
)
37 EVT_MENU(BUSY_OFF
, MyFrame::BusyOff
)
38 EVT_MENU(LIST_QUIT
, MyFrame::OnQuit
)
39 EVT_MENU(LIST_ABOUT
, MyFrame::OnAbout
)
40 EVT_MENU(LIST_LIST_VIEW
, MyFrame::OnListView
)
41 EVT_MENU(LIST_REPORT_VIEW
, MyFrame::OnReportView
)
42 EVT_MENU(LIST_ICON_VIEW
, MyFrame::OnIconView
)
43 EVT_MENU(LIST_ICON_TEXT_VIEW
, MyFrame::OnIconTextView
)
44 EVT_MENU(LIST_SMALL_ICON_VIEW
, MyFrame::OnSmallIconView
)
45 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW
, MyFrame::OnSmallIconTextView
)
46 EVT_MENU(LIST_DESELECT_ALL
, MyFrame::OnDeselectAll
)
47 EVT_MENU(LIST_SELECT_ALL
, MyFrame::OnSelectAll
)
48 EVT_MENU(LIST_DELETE_ALL
, MyFrame::OnDeleteAll
)
51 BEGIN_EVENT_TABLE(MyListCtrl
, wxListCtrl
)
52 EVT_LIST_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnBeginDrag
)
53 EVT_LIST_BEGIN_RDRAG(LIST_CTRL
, MyListCtrl::OnBeginRDrag
)
54 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnBeginLabelEdit
)
55 EVT_LIST_END_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnEndLabelEdit
)
56 EVT_LIST_DELETE_ITEM(LIST_CTRL
, MyListCtrl::OnDeleteItem
)
57 EVT_LIST_GET_INFO(LIST_CTRL
, MyListCtrl::OnGetInfo
)
58 EVT_LIST_SET_INFO(LIST_CTRL
, MyListCtrl::OnSetInfo
)
59 EVT_LIST_ITEM_SELECTED(LIST_CTRL
, MyListCtrl::OnSelected
)
60 EVT_LIST_ITEM_DESELECTED(LIST_CTRL
, MyListCtrl::OnDeselected
)
61 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl::OnListKeyDown
)
62 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl::OnActivated
)
63 EVT_LIST_COL_CLICK(LIST_CTRL
, MyListCtrl::OnColClick
)
68 // `Main program' equivalent, creating windows and returning main app frame
69 bool MyApp::OnInit(void)
71 // Create the main frame window
72 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, "wxListCtrl Test", 50, 50, 450, 340);
74 // This reduces flicker effects - even better would be to define OnEraseBackground
75 // to do nothing. When the list control's scrollbars are show or hidden, the
76 // frame is sent a background erase event.
77 frame
->SetBackgroundColour( *wxWHITE
);
80 frame
->SetIcon( wxICON(mondrian
) );
82 // Make an image list containing large icons
83 m_imageListNormal
= new wxImageList(32, 32, TRUE
);
84 m_imageListSmall
= new wxImageList(16, 16, TRUE
);
87 m_imageListNormal
->Add( wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE
) );
88 m_imageListNormal
->Add( wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE
) );
89 m_imageListNormal
->Add( wxIcon("icon3", wxBITMAP_TYPE_ICO_RESOURCE
) );
90 m_imageListNormal
->Add( wxIcon("icon4", wxBITMAP_TYPE_ICO_RESOURCE
) );
91 m_imageListNormal
->Add( wxIcon("icon5", wxBITMAP_TYPE_ICO_RESOURCE
) );
92 m_imageListNormal
->Add( wxIcon("icon6", wxBITMAP_TYPE_ICO_RESOURCE
) );
93 m_imageListNormal
->Add( wxIcon("icon7", wxBITMAP_TYPE_ICO_RESOURCE
) );
94 m_imageListNormal
->Add( wxIcon("icon8", wxBITMAP_TYPE_ICO_RESOURCE
) );
95 m_imageListNormal
->Add( wxIcon("icon9", wxBITMAP_TYPE_ICO_RESOURCE
) );
97 m_imageListSmall
->Add( wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE
) );
101 #include "bitmaps/toolbrai.xpm"
102 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
103 #include "bitmaps/toolchar.xpm"
104 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
105 #include "bitmaps/tooldata.xpm"
106 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
107 #include "bitmaps/toolnote.xpm"
108 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
109 #include "bitmaps/tooltodo.xpm"
110 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
111 #include "bitmaps/toolchec.xpm"
112 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
113 #include "bitmaps/toolgame.xpm"
114 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
115 #include "bitmaps/tooltime.xpm"
116 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
117 #include "bitmaps/toolword.xpm"
118 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
120 #include "bitmaps/small1.xpm"
121 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
126 wxMenu
*file_menu
= new wxMenu
;
128 file_menu
->Append(LIST_LIST_VIEW
, "&List view");
129 file_menu
->Append(LIST_REPORT_VIEW
, "&Report view");
130 file_menu
->Append(LIST_ICON_VIEW
, "&Icon view");
131 file_menu
->Append(LIST_ICON_TEXT_VIEW
, "Icon view with &text");
132 file_menu
->Append(LIST_SMALL_ICON_VIEW
, "&Small icon view");
133 file_menu
->Append(LIST_SMALL_ICON_TEXT_VIEW
, "Small icon &view with text");
134 file_menu
->Append(LIST_DESELECT_ALL
, "&Deselect All");
135 file_menu
->Append(LIST_SELECT_ALL
, "S&elect All");
136 file_menu
->AppendSeparator();
137 file_menu
->Append(LIST_DELETE_ALL
, "Delete &all items");
138 file_menu
->AppendSeparator();
139 file_menu
->Append(BUSY_ON
, "&Busy cursor on");
140 file_menu
->Append(BUSY_OFF
, "&Busy cursor off");
141 file_menu
->AppendSeparator();
142 file_menu
->Append(LIST_ABOUT
, "&About");
143 file_menu
->Append(LIST_QUIT
, "E&xit");
144 wxMenuBar
*menu_bar
= new wxMenuBar
;
145 menu_bar
->Append(file_menu
, "&File");
146 frame
->SetMenuBar(menu_bar
);
148 // Make a panel with a message
149 frame
->m_listCtrl
= new MyListCtrl(frame
, LIST_CTRL
, wxPoint(0, 0), wxSize(400, 200),
150 wxLC_LIST
|wxSUNKEN_BORDER
|wxLC_EDIT_LABELS
);
151 // wxLC_LIST|wxLC_USER_TEXT|wxSUNKEN_BORDER); // wxLC_USER_TEXT requires app to supply all text on demand
152 frame
->m_logWindow
= new wxTextCtrl(frame
, -1, "", wxPoint(0, 0), wxSize(400, 200), wxTE_MULTILINE
|wxSUNKEN_BORDER
);
154 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
155 c
->top
.SameAs (frame
, wxTop
);
156 c
->left
.SameAs (frame
, wxLeft
);
157 c
->right
.SameAs (frame
, wxRight
);
158 c
->height
.PercentOf (frame
, wxHeight
, 66);
159 frame
->m_listCtrl
->SetConstraints(c
);
161 c
= new wxLayoutConstraints
;
162 c
->top
.Below (frame
->m_listCtrl
);
163 c
->left
.SameAs (frame
, wxLeft
);
164 c
->right
.SameAs (frame
, wxRight
);
165 c
->bottom
.SameAs (frame
, wxBottom
);
166 frame
->m_logWindow
->SetConstraints(c
);
167 frame
->SetAutoLayout(TRUE
);
169 for ( int i
=0; i
< 30; i
++)
172 wxSprintf(buf
, _T("Item %d"), i
);
173 frame
->m_listCtrl
->InsertItem(i
, buf
);
176 frame
->CreateStatusBar(3);
177 frame
->SetStatusText("", 0);
187 // My frame constructor
188 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
189 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
191 m_listCtrl
= (MyListCtrl
*) NULL
;
192 m_logWindow
= (wxTextCtrl
*) NULL
;
195 MyFrame::~MyFrame(void)
197 delete wxGetApp().m_imageListNormal
;
198 delete wxGetApp().m_imageListSmall
;
201 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
206 void MyFrame::BusyOn(wxCommandEvent
& WXUNUSED(event
))
211 void MyFrame::BusyOff(wxCommandEvent
& WXUNUSED(event
))
216 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
218 wxMessageDialog
dialog(this, "List test sample\nJulian Smart (c) 1997",
219 "About list test", wxOK
|wxCANCEL
);
224 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
226 int n
= m_listCtrl
->GetItemCount();
227 for (int i
= 0; i
< n
; i
++)
228 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
231 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
233 int n
= m_listCtrl
->GetItemCount();
234 for (int i
= 0; i
< n
; i
++)
235 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
238 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
240 m_listCtrl
->DeleteAllItems();
241 m_logWindow
->Clear();
242 m_listCtrl
->SetSingleStyle(wxLC_LIST
);
243 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_NORMAL
);
244 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_SMALL
);
246 for ( int i
=0; i
< 30; i
++)
249 wxSprintf(buf
, _T("Item %d"), i
);
250 m_listCtrl
->InsertItem(i
, buf
);
254 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
256 m_listCtrl
->DeleteAllItems();
257 m_listCtrl
->DeleteAllColumns();
258 m_logWindow
->Clear();
260 m_listCtrl
->SetSingleStyle(wxLC_REPORT
);
261 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_NORMAL
);
262 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
264 m_listCtrl
->InsertColumn(0, "Column 1"); // , wxLIST_FORMAT_LEFT, 140);
265 m_listCtrl
->InsertColumn(1, "Column 2"); // , wxLIST_FORMAT_LEFT, 140);
266 m_listCtrl
->InsertColumn(2, "One More Column (2)"); // , wxLIST_FORMAT_LEFT, 140);
268 for ( int i
= 0; i
< 3000; i
++ )
271 wxSprintf(buf
, _T("This is item %d"), i
);
272 long tmp
= m_listCtrl
->InsertItem(i
, buf
, 0);
274 wxSprintf(buf
, _T("Col 1, item %d"), i
);
275 tmp
= m_listCtrl
->SetItem(i
, 1, buf
);
277 wxSprintf(buf
, _T("Item %d in column 2"), i
);
278 tmp
= m_listCtrl
->SetItem(i
, 2, buf
);
281 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
282 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
283 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
286 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
288 m_listCtrl
->DeleteAllItems();
289 m_logWindow
->Clear();
290 m_listCtrl
->SetSingleStyle(wxLC_ICON
);
291 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
292 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
294 for ( int i
=0; i
< 9; i
++)
296 m_listCtrl
->InsertItem(i
, i
);
300 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
302 m_listCtrl
->DeleteAllItems();
303 m_logWindow
->Clear();
304 m_listCtrl
->SetSingleStyle(wxLC_ICON
);
305 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
306 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
308 for ( int i
=0; i
< 9; i
++)
311 wxSprintf(buf
, _T("Label %d"), i
);
312 m_listCtrl
->InsertItem(i
, buf
, i
);
316 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
318 m_listCtrl
->DeleteAllItems();
319 m_logWindow
->Clear();
320 m_listCtrl
->SetSingleStyle(wxLC_SMALL_ICON
);
321 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
322 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
324 for ( int i
=0; i
< 9; i
++)
326 m_listCtrl
->InsertItem(i
, 0);
330 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
332 m_listCtrl
->DeleteAllItems();
333 m_logWindow
->Clear();
334 m_listCtrl
->SetSingleStyle(wxLC_SMALL_ICON
);
335 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
336 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
338 for ( int i
=0; i
< 9; i
++)
340 m_listCtrl
->InsertItem(i
, "Label", 0);
344 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
346 (void)wxGetElapsedTime(TRUE
);
348 int nItems
= m_listCtrl
->GetItemCount();
349 m_listCtrl
->DeleteAllItems();
351 wxLogMessage("Deleting %d items took %ld ms",
352 nItems
, wxGetElapsedTime());
357 void MyListCtrl::OnColClick(wxListEvent
& event
)
359 if ( !wxGetApp().GetTopWindow() )
362 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
367 msg
.Printf( "OnColumnClick at %d.\n", event
.GetColumn() );
368 text
->WriteText(msg
);
371 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
373 if ( !wxGetApp().GetTopWindow() )
376 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
381 msg
.Printf( "OnBeginDrag at %d,%d.\n", event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
382 text
->WriteText(msg
);
385 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
387 if ( !wxGetApp().GetTopWindow() )
390 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
395 msg
.Printf( "OnBeginRDrag at %d,%d.\n", event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
396 text
->WriteText(msg
);
399 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
401 if ( !wxGetApp().GetTopWindow() )
404 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
408 text
->WriteText("OnBeginLabelEdit: ");
409 text
->WriteText(event
.m_item
.m_text
);
410 text
->WriteText("\n");
413 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
415 if ( !wxGetApp().GetTopWindow() )
418 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
422 text
->WriteText("OnEndLabelEdit: ");
423 text
->WriteText(event
.m_item
.m_text
);
424 text
->WriteText("\n");
427 void MyListCtrl::OnDeleteItem(wxListEvent
& WXUNUSED(event
))
429 if ( !wxGetApp().GetTopWindow() )
432 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
436 text
->WriteText("OnDeleteItem\n");
439 void MyListCtrl::OnGetInfo(wxListEvent
& event
)
441 if ( !wxGetApp().GetTopWindow() )
444 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
448 text
->WriteText("OnGetInfo\n");
450 (*text
) << "OnGetInfo (" << event
.m_item
.m_itemId
<< ", " << event
.m_item
.m_col
<< ")";
451 if ( event
.m_item
.m_mask
& wxLIST_MASK_STATE
)
452 (*text
) << " wxLIST_MASK_STATE";
453 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
454 (*text
) << " wxLIST_MASK_TEXT";
455 if ( event
.m_item
.m_mask
& wxLIST_MASK_IMAGE
)
456 (*text
) << " wxLIST_MASK_IMAGE";
457 if ( event
.m_item
.m_mask
& wxLIST_MASK_DATA
)
458 (*text
) << " wxLIST_MASK_DATA";
459 if ( event
.m_item
.m_mask
& wxLIST_SET_ITEM
)
460 (*text
) << " wxLIST_SET_ITEM";
461 if ( event
.m_item
.m_mask
& wxLIST_MASK_WIDTH
)
462 (*text
) << " wxLIST_MASK_WIDTH";
463 if ( event
.m_item
.m_mask
& wxLIST_MASK_FORMAT
)
464 (*text
) << " wxLIST_MASK_WIDTH";
466 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
468 event
.m_item
.m_text
= "My callback text";
473 void MyListCtrl::OnSetInfo(wxListEvent
& WXUNUSED(event
))
475 if ( !wxGetApp().GetTopWindow() )
478 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
482 text
->WriteText("OnSetInfo\n");
485 void MyListCtrl::OnSelected(wxListEvent
& event
)
487 if ( !wxGetApp().GetTopWindow() )
490 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
494 if ( GetWindowStyle() & wxLC_REPORT
)
497 info
.m_itemId
= event
.m_itemIndex
;
499 info
.m_mask
= wxLIST_MASK_TEXT
;
502 *text
<< "Value of the 2nd field of the selected item: "
503 << info
.m_text
<< '\n';
507 wxFAIL_MSG("wxListCtrl::GetItem() failed");
511 text
->WriteText("OnSelected\n");
514 void MyListCtrl::OnDeselected(wxListEvent
& WXUNUSED(event
))
516 if ( !wxGetApp().GetTopWindow() )
519 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
523 text
->WriteText("OnDeselected\n");
526 void MyListCtrl::OnActivated(wxListEvent
& WXUNUSED(event
))
528 if ( !wxGetApp().GetTopWindow() )
531 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
535 text
->WriteText("OnActivated\n");
538 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
540 if ( !wxGetApp().GetTopWindow() )
543 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
547 text
->WriteText("OnListKeyDown\n");