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
)
67 // `Main program' equivalent, creating windows and returning main app frame
68 bool MyApp::OnInit(void)
70 // Create the main frame window
71 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, "wxListCtrl Test", 50, 50, 450, 340);
73 // This reduces flicker effects - even better would be to define OnEraseBackground
74 // to do nothing. When the list control's scrollbars are show or hidden, the
75 // frame is sent a background erase event.
76 frame
->SetBackgroundColour( *wxWHITE
);
79 frame
->SetIcon( wxICON(mondrian
) );
81 // Make an image list containing large icons
82 m_imageListNormal
= new wxImageList(32, 32, TRUE
);
83 m_imageListSmall
= new wxImageList(16, 16, TRUE
);
86 m_imageListNormal
->Add( wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE
) );
87 m_imageListNormal
->Add( wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE
) );
88 m_imageListNormal
->Add( wxIcon("icon3", wxBITMAP_TYPE_ICO_RESOURCE
) );
89 m_imageListNormal
->Add( wxIcon("icon4", wxBITMAP_TYPE_ICO_RESOURCE
) );
90 m_imageListNormal
->Add( wxIcon("icon5", wxBITMAP_TYPE_ICO_RESOURCE
) );
91 m_imageListNormal
->Add( wxIcon("icon6", wxBITMAP_TYPE_ICO_RESOURCE
) );
92 m_imageListNormal
->Add( wxIcon("icon7", wxBITMAP_TYPE_ICO_RESOURCE
) );
93 m_imageListNormal
->Add( wxIcon("icon8", wxBITMAP_TYPE_ICO_RESOURCE
) );
94 m_imageListNormal
->Add( wxIcon("icon9", wxBITMAP_TYPE_ICO_RESOURCE
) );
96 m_imageListSmall
->Add( wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE
) );
100 #include "bitmaps/toolbrai.xpm"
101 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
102 #include "bitmaps/toolchar.xpm"
103 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
104 #include "bitmaps/tooldata.xpm"
105 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
106 #include "bitmaps/toolnote.xpm"
107 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
108 #include "bitmaps/tooltodo.xpm"
109 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
110 #include "bitmaps/toolchec.xpm"
111 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
112 #include "bitmaps/toolgame.xpm"
113 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
114 #include "bitmaps/tooltime.xpm"
115 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
116 #include "bitmaps/toolword.xpm"
117 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
119 #include "bitmaps/small1.xpm"
120 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
125 wxMenu
*file_menu
= new wxMenu
;
127 file_menu
->Append(LIST_LIST_VIEW
, "&List view");
128 file_menu
->Append(LIST_REPORT_VIEW
, "&Report view");
129 file_menu
->Append(LIST_ICON_VIEW
, "&Icon view");
130 file_menu
->Append(LIST_ICON_TEXT_VIEW
, "Icon view with &text");
131 file_menu
->Append(LIST_SMALL_ICON_VIEW
, "&Small icon view");
132 file_menu
->Append(LIST_SMALL_ICON_TEXT_VIEW
, "Small icon &view with text");
133 file_menu
->Append(LIST_DESELECT_ALL
, "&Deselect All");
134 file_menu
->Append(LIST_SELECT_ALL
, "S&elect All");
135 file_menu
->AppendSeparator();
136 file_menu
->Append(LIST_DELETE_ALL
, "Delete &all items");
137 file_menu
->AppendSeparator();
138 file_menu
->Append(BUSY_ON
, "&Busy cursor on");
139 file_menu
->Append(BUSY_OFF
, "&Busy cursor off");
140 file_menu
->AppendSeparator();
141 file_menu
->Append(LIST_ABOUT
, "&About");
142 file_menu
->Append(LIST_QUIT
, "E&xit");
143 wxMenuBar
*menu_bar
= new wxMenuBar
;
144 menu_bar
->Append(file_menu
, "&File");
145 frame
->SetMenuBar(menu_bar
);
147 // Make a panel with a message
148 frame
->m_listCtrl
= new MyListCtrl(frame
, LIST_CTRL
, wxPoint(0, 0), wxSize(400, 200),
149 wxLC_LIST
|wxSUNKEN_BORDER
|wxLC_EDIT_LABELS
);
150 // wxLC_LIST|wxLC_USER_TEXT|wxSUNKEN_BORDER); // wxLC_USER_TEXT requires app to supply all text on demand
151 frame
->m_logWindow
= new wxTextCtrl(frame
, -1, "", wxPoint(0, 0), wxSize(400, 200), wxTE_MULTILINE
|wxSUNKEN_BORDER
);
153 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
154 c
->top
.SameAs (frame
, wxTop
);
155 c
->left
.SameAs (frame
, wxLeft
);
156 c
->right
.SameAs (frame
, wxRight
);
157 c
->height
.PercentOf (frame
, wxHeight
, 66);
158 frame
->m_listCtrl
->SetConstraints(c
);
160 c
= new wxLayoutConstraints
;
161 c
->top
.Below (frame
->m_listCtrl
);
162 c
->left
.SameAs (frame
, wxLeft
);
163 c
->right
.SameAs (frame
, wxRight
);
164 c
->bottom
.SameAs (frame
, wxBottom
);
165 frame
->m_logWindow
->SetConstraints(c
);
166 frame
->SetAutoLayout(TRUE
);
168 for ( int i
=0; i
< 30; i
++)
171 wxSprintf(buf
, _T("Item %d"), i
);
172 frame
->m_listCtrl
->InsertItem(i
, buf
);
175 frame
->CreateStatusBar(3);
176 frame
->SetStatusText("", 0);
186 // My frame constructor
187 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
188 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
190 m_listCtrl
= (MyListCtrl
*) NULL
;
191 m_logWindow
= (wxTextCtrl
*) NULL
;
194 MyFrame::~MyFrame(void)
196 delete wxGetApp().m_imageListNormal
;
197 delete wxGetApp().m_imageListSmall
;
200 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
205 void MyFrame::BusyOn(wxCommandEvent
& WXUNUSED(event
))
210 void MyFrame::BusyOff(wxCommandEvent
& WXUNUSED(event
))
215 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
217 wxMessageDialog
dialog(this, "List test sample\nJulian Smart (c) 1997",
218 "About list test", wxOK
|wxCANCEL
);
223 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
225 int n
= m_listCtrl
->GetItemCount();
226 for (int i
= 0; i
< n
; i
++)
227 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
230 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
232 int n
= m_listCtrl
->GetItemCount();
233 for (int i
= 0; i
< n
; i
++)
234 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
237 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
239 m_listCtrl
->DeleteAllItems();
240 m_logWindow
->Clear();
241 m_listCtrl
->SetSingleStyle(wxLC_LIST
);
242 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_NORMAL
);
243 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_SMALL
);
245 for ( int i
=0; i
< 30; i
++)
248 wxSprintf(buf
, _T("Item %d"), i
);
249 m_listCtrl
->InsertItem(i
, buf
);
253 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
255 m_listCtrl
->DeleteAllItems();
256 m_listCtrl
->DeleteAllColumns();
257 m_logWindow
->Clear();
259 m_listCtrl
->SetSingleStyle(wxLC_REPORT
);
260 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_NORMAL
);
261 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
263 m_listCtrl
->InsertColumn(0, "Column 1"); // , wxLIST_FORMAT_LEFT, 140);
264 m_listCtrl
->InsertColumn(1, "Column 2"); // , wxLIST_FORMAT_LEFT, 140);
265 m_listCtrl
->InsertColumn(2, "One More Column (2)"); // , wxLIST_FORMAT_LEFT, 140);
267 for ( int i
= 0; i
< 3000; i
++ )
270 wxSprintf(buf
, _T("This is item %d"), i
);
271 long tmp
= m_listCtrl
->InsertItem(i
, buf
, 0);
273 wxSprintf(buf
, _T("Col 1, item %d"), i
);
274 tmp
= m_listCtrl
->SetItem(i
, 1, buf
);
276 wxSprintf(buf
, _T("Item %d in column 2"), i
);
277 tmp
= m_listCtrl
->SetItem(i
, 2, buf
);
280 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
281 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
282 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
285 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
287 m_listCtrl
->DeleteAllItems();
288 m_logWindow
->Clear();
289 m_listCtrl
->SetSingleStyle(wxLC_ICON
);
290 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
291 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
293 for ( int i
=0; i
< 9; i
++)
295 m_listCtrl
->InsertItem(i
, i
);
299 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
301 m_listCtrl
->DeleteAllItems();
302 m_logWindow
->Clear();
303 m_listCtrl
->SetSingleStyle(wxLC_ICON
);
304 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
305 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
307 for ( int i
=0; i
< 9; i
++)
310 wxSprintf(buf
, _T("Label %d"), i
);
311 m_listCtrl
->InsertItem(i
, buf
, i
);
315 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
317 m_listCtrl
->DeleteAllItems();
318 m_logWindow
->Clear();
319 m_listCtrl
->SetSingleStyle(wxLC_SMALL_ICON
);
320 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
321 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
323 for ( int i
=0; i
< 9; i
++)
325 m_listCtrl
->InsertItem(i
, 0);
329 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
331 m_listCtrl
->DeleteAllItems();
332 m_logWindow
->Clear();
333 m_listCtrl
->SetSingleStyle(wxLC_SMALL_ICON
);
334 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
335 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
337 for ( int i
=0; i
< 9; i
++)
339 m_listCtrl
->InsertItem(i
, "Label", 0);
343 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
345 (void)wxGetElapsedTime(TRUE
);
347 int nItems
= m_listCtrl
->GetItemCount();
348 m_listCtrl
->DeleteAllItems();
350 wxLogMessage("Deleting %d items took %ld ms",
351 nItems
, wxGetElapsedTime());
356 void MyListCtrl::OnBeginDrag(wxListEvent
& WXUNUSED(event
))
358 if ( !wxGetApp().GetTopWindow() )
361 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
365 text
->WriteText("OnBeginDrag\n");
368 void MyListCtrl::OnBeginRDrag(wxListEvent
& WXUNUSED(event
))
370 if ( !wxGetApp().GetTopWindow() )
373 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
376 text
->WriteText("OnBeginRDrag\n");
379 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& WXUNUSED(event
))
381 if ( !wxGetApp().GetTopWindow() )
384 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
388 text
->WriteText("OnBeginLabelEdit\n");
391 void MyListCtrl::OnEndLabelEdit(wxListEvent
& WXUNUSED(event
))
393 if ( !wxGetApp().GetTopWindow() )
396 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
400 text
->WriteText("OnEndLabelEdit\n");
403 void MyListCtrl::OnDeleteItem(wxListEvent
& WXUNUSED(event
))
405 if ( !wxGetApp().GetTopWindow() )
408 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
412 text
->WriteText("OnDeleteItem\n");
415 void MyListCtrl::OnGetInfo(wxListEvent
& /*event*/)
417 if ( !wxGetApp().GetTopWindow() )
420 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
424 text
->WriteText("OnGetInfo\n");
429 str << "OnGetInfo (" << event.m_item.m_itemId << ", " << event.m_item.m_col << ")";
430 if ( event.m_item.m_mask & wxLIST_MASK_STATE )
431 str << " wxLIST_MASK_STATE";
432 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
433 str << " wxLIST_MASK_TEXT";
434 if ( event.m_item.m_mask & wxLIST_MASK_IMAGE )
435 str << " wxLIST_MASK_IMAGE";
436 if ( event.m_item.m_mask & wxLIST_MASK_DATA )
437 str << " wxLIST_MASK_DATA";
438 if ( event.m_item.m_mask & wxLIST_SET_ITEM )
439 str << " wxLIST_SET_ITEM";
440 if ( event.m_item.m_mask & wxLIST_MASK_WIDTH )
441 str << " wxLIST_MASK_WIDTH";
442 if ( event.m_item.m_mask & wxLIST_MASK_FORMAT )
443 str << " wxLIST_MASK_WIDTH";
445 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
447 event.m_item.m_text = "My callback text";
454 void MyListCtrl::OnSetInfo(wxListEvent
& WXUNUSED(event
))
456 if ( !wxGetApp().GetTopWindow() )
459 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
463 text
->WriteText("OnSetInfo\n");
466 void MyListCtrl::OnSelected(wxListEvent
& WXUNUSED(event
))
468 if ( !wxGetApp().GetTopWindow() )
471 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
475 text
->WriteText("OnSelected\n");
478 void MyListCtrl::OnDeselected(wxListEvent
& WXUNUSED(event
))
480 if ( !wxGetApp().GetTopWindow() )
483 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
487 text
->WriteText("OnDeselected\n");
490 void MyListCtrl::OnActivated(wxListEvent
& WXUNUSED(event
))
492 if ( !wxGetApp().GetTopWindow() )
495 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
499 text
->WriteText("OnActivated\n");
502 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
504 if ( !wxGetApp().GetTopWindow() )
507 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
511 text
->WriteText("OnListKeyDown\n");