]>
Commit | Line | Data |
---|---|---|
457814b5 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: listctrl.cpp | |
3 | // Purpose: wxListCtrl sample | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
5ea47806 | 9 | // Licence: wxWindows license |
457814b5 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation | |
14 | #pragma interface | |
15 | #endif | |
16 | ||
17 | // For compilers that support precompilation, includes "wx/wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WX_PRECOMP | |
25 | #include "wx/wx.h" | |
26 | #endif | |
27 | ||
907789a0 | 28 | #ifndef __WXMSW__ |
c41ea66a VZ |
29 | #include "mondrian.xpm" |
30 | ||
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" | |
907789a0 RR |
41 | #endif |
42 | ||
2e8a1588 | 43 | #include "wx/imaglist.h" |
457814b5 | 44 | #include "wx/listctrl.h" |
81278df2 | 45 | #include "wx/timer.h" // for wxStopWatch |
11f26ea0 VZ |
46 | #include "wx/colordlg.h" // for wxGetColourFromUser |
47 | ||
457814b5 JS |
48 | #include "listtest.h" |
49 | ||
50 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
98ec9dbe VZ |
51 | EVT_SIZE(MyFrame::OnSize) |
52 | ||
5ea47806 VZ |
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) | |
98ec9dbe VZ |
61 | EVT_MENU(LIST_VIRTUAL_VIEW, MyFrame::OnVirtualView) |
62 | ||
58b3bdc9 | 63 | EVT_MENU(LIST_TOGGLE_FIRST, MyFrame::OnToggleFirstSel) |
5ea47806 VZ |
64 | EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll) |
65 | EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll) | |
bec0a261 | 66 | EVT_MENU(LIST_DELETE, MyFrame::OnDelete) |
cf1dfa6b | 67 | EVT_MENU(LIST_ADD, MyFrame::OnAdd) |
5ea47806 | 68 | EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll) |
fa5f6926 | 69 | EVT_MENU(LIST_SORT, MyFrame::OnSort) |
11f26ea0 VZ |
70 | EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour) |
71 | EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour) | |
7b848b0d | 72 | EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel) |
f6bcfd97 | 73 | EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo) |
98ec9dbe | 74 | |
f6bcfd97 | 75 | EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo) |
457814b5 JS |
76 | END_EVENT_TABLE() |
77 | ||
78 | BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl) | |
5ea47806 VZ |
79 | EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag) |
80 | EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag) | |
81 | EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnBeginLabelEdit) | |
82 | EVT_LIST_END_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnEndLabelEdit) | |
83 | EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem) | |
12c1b46a | 84 | EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems) |
5ea47806 VZ |
85 | EVT_LIST_GET_INFO(LIST_CTRL, MyListCtrl::OnGetInfo) |
86 | EVT_LIST_SET_INFO(LIST_CTRL, MyListCtrl::OnSetInfo) | |
87 | EVT_LIST_ITEM_SELECTED(LIST_CTRL, MyListCtrl::OnSelected) | |
88 | EVT_LIST_ITEM_DESELECTED(LIST_CTRL, MyListCtrl::OnDeselected) | |
89 | EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown) | |
90 | EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated) | |
8636aed8 | 91 | EVT_LIST_COL_CLICK(LIST_CTRL, MyListCtrl::OnColClick) |
f6bcfd97 BP |
92 | |
93 | EVT_CHAR(MyListCtrl::OnChar) | |
457814b5 JS |
94 | END_EVENT_TABLE() |
95 | ||
96 | IMPLEMENT_APP(MyApp) | |
97 | ||
776a33cf VZ |
98 | // number of items in list/report view |
99 | static const int NUM_ITEMS = 30; | |
100 | ||
101 | // number of items in icon/small icon view | |
102 | static const int NUM_ICONS = 9; | |
103 | ||
fa5f6926 VZ |
104 | int wxCALLBACK MyCompareFunction(long item1, long item2, long sortData) |
105 | { | |
106 | // inverse the order | |
107 | return item1 < item2; | |
108 | } | |
109 | ||
457814b5 | 110 | // `Main program' equivalent, creating windows and returning main app frame |
11f26ea0 | 111 | bool MyApp::OnInit() |
457814b5 JS |
112 | { |
113 | // Create the main frame window | |
c41ea66a | 114 | MyFrame *frame = new MyFrame("wxListCtrl Test", 50, 50, 450, 340); |
457814b5 JS |
115 | |
116 | // Show the frame | |
117 | frame->Show(TRUE); | |
5ea47806 | 118 | |
457814b5 JS |
119 | SetTopWindow(frame); |
120 | ||
121 | return TRUE; | |
122 | } | |
123 | ||
124 | // My frame constructor | |
c41ea66a VZ |
125 | MyFrame::MyFrame(const wxChar *title, int x, int y, int w, int h) |
126 | : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h)) | |
457814b5 | 127 | { |
5ea47806 VZ |
128 | m_listCtrl = (MyListCtrl *) NULL; |
129 | m_logWindow = (wxTextCtrl *) NULL; | |
c41ea66a VZ |
130 | |
131 | // Give it an icon | |
132 | SetIcon( wxICON(mondrian) ); | |
133 | ||
134 | // Make an image list containing large icons | |
135 | m_imageListNormal = new wxImageList(32, 32, TRUE); | |
136 | m_imageListSmall = new wxImageList(16, 16, TRUE); | |
137 | ||
138 | #ifdef __WXMSW__ | |
139 | m_imageListNormal->Add( wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE) ); | |
140 | m_imageListNormal->Add( wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE) ); | |
141 | m_imageListNormal->Add( wxIcon("icon3", wxBITMAP_TYPE_ICO_RESOURCE) ); | |
142 | m_imageListNormal->Add( wxIcon("icon4", wxBITMAP_TYPE_ICO_RESOURCE) ); | |
143 | m_imageListNormal->Add( wxIcon("icon5", wxBITMAP_TYPE_ICO_RESOURCE) ); | |
144 | m_imageListNormal->Add( wxIcon("icon6", wxBITMAP_TYPE_ICO_RESOURCE) ); | |
145 | m_imageListNormal->Add( wxIcon("icon7", wxBITMAP_TYPE_ICO_RESOURCE) ); | |
146 | m_imageListNormal->Add( wxIcon("icon8", wxBITMAP_TYPE_ICO_RESOURCE) ); | |
147 | m_imageListNormal->Add( wxIcon("icon9", wxBITMAP_TYPE_ICO_RESOURCE) ); | |
148 | ||
149 | m_imageListSmall->Add( wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE) ); | |
150 | ||
151 | #else | |
152 | m_imageListNormal->Add( wxIcon( toolbrai_xpm ) ); | |
153 | m_imageListNormal->Add( wxIcon( toolchar_xpm ) ); | |
154 | m_imageListNormal->Add( wxIcon( tooldata_xpm ) ); | |
155 | m_imageListNormal->Add( wxIcon( toolnote_xpm ) ); | |
156 | m_imageListNormal->Add( wxIcon( tooltodo_xpm ) ); | |
157 | m_imageListNormal->Add( wxIcon( toolchec_xpm ) ); | |
158 | m_imageListNormal->Add( wxIcon( toolgame_xpm ) ); | |
159 | m_imageListNormal->Add( wxIcon( tooltime_xpm ) ); | |
160 | m_imageListNormal->Add( wxIcon( toolword_xpm ) ); | |
161 | ||
162 | m_imageListSmall->Add( wxIcon( small1_xpm) ); | |
163 | #endif | |
164 | ||
165 | // Make a menubar | |
166 | wxMenu *menuFile = new wxMenu; | |
98ec9dbe | 167 | menuFile->Append(LIST_ABOUT, _T("&About")); |
c41ea66a | 168 | menuFile->AppendSeparator(); |
98ec9dbe | 169 | menuFile->Append(LIST_QUIT, _T("E&xit\tAlt-X")); |
c41ea66a VZ |
170 | |
171 | wxMenu *menuView = new wxMenu; | |
98ec9dbe VZ |
172 | menuView->Append(LIST_LIST_VIEW, _T("&List view\tF1")); |
173 | menuView->Append(LIST_REPORT_VIEW, _T("&Report view\tF2")); | |
174 | menuView->Append(LIST_ICON_VIEW, _T("&Icon view\tF3")); | |
175 | menuView->Append(LIST_ICON_TEXT_VIEW, _T("Icon view with &text\tF4")); | |
176 | menuView->Append(LIST_SMALL_ICON_VIEW, _T("&Small icon view\tF5")); | |
177 | menuView->Append(LIST_SMALL_ICON_TEXT_VIEW, _T("Small icon &view with text\tF6")); | |
178 | menuView->Append(LIST_VIRTUAL_VIEW, _T("Virtual view\tF7")); | |
c41ea66a VZ |
179 | |
180 | wxMenu *menuList = new wxMenu; | |
98ec9dbe VZ |
181 | menuList->Append(LIST_TOGGLE_FIRST, _T("&Toggle first item\tCtrl-T")); |
182 | menuList->Append(LIST_DESELECT_ALL, _T("&Deselect All\tCtrl-D")); | |
183 | menuList->Append(LIST_SELECT_ALL, _T("S&elect All\tCtrl-A")); | |
184 | menuList->Append(LIST_SHOW_COL_INFO, _T("Show &column info\tCtrl-C")); | |
c41ea66a | 185 | menuList->AppendSeparator(); |
98ec9dbe | 186 | menuList->Append(LIST_SORT, _T("&Sort\tCtrl-S")); |
c41ea66a | 187 | menuList->AppendSeparator(); |
cf1dfa6b VZ |
188 | menuList->Append(LIST_ADD, _T("&Append an item\tCtrl-P")); |
189 | menuList->Append(LIST_DELETE, _T("&Delete first item\tCtrl-X")); | |
98ec9dbe | 190 | menuList->Append(LIST_DELETE_ALL, _T("Delete &all items")); |
c41ea66a | 191 | menuList->AppendSeparator(); |
98ec9dbe VZ |
192 | menuList->Append(LIST_TOGGLE_MULTI_SEL, _T("&Multiple selection\tCtrl-M"), |
193 | _T("Toggle multiple selection"), TRUE); | |
c41ea66a VZ |
194 | |
195 | wxMenu *menuCol = new wxMenu; | |
98ec9dbe VZ |
196 | menuCol->Append(LIST_SET_FG_COL, _T("&Foreground colour...")); |
197 | menuCol->Append(LIST_SET_BG_COL, _T("&Background colour...")); | |
c41ea66a VZ |
198 | |
199 | wxMenuBar *menubar = new wxMenuBar; | |
98ec9dbe VZ |
200 | menubar->Append(menuFile, _T("&File")); |
201 | menubar->Append(menuView, _T("&View")); | |
202 | menubar->Append(menuList, _T("&List")); | |
203 | menubar->Append(menuCol, _T("&Colour")); | |
c41ea66a VZ |
204 | SetMenuBar(menubar); |
205 | ||
c41ea66a VZ |
206 | m_logWindow = new wxTextCtrl(this, -1, wxEmptyString, |
207 | wxDefaultPosition, wxDefaultSize, | |
208 | wxTE_MULTILINE | wxSUNKEN_BORDER); | |
209 | ||
210 | m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow)); | |
211 | ||
776a33cf | 212 | RecreateList(wxLC_REPORT | wxLC_SINGLE_SEL); |
c41ea66a VZ |
213 | |
214 | CreateStatusBar(3); | |
457814b5 JS |
215 | } |
216 | ||
11f26ea0 | 217 | MyFrame::~MyFrame() |
457814b5 | 218 | { |
c41ea66a VZ |
219 | delete wxLog::SetActiveTarget(m_logOld); |
220 | ||
221 | delete m_imageListNormal; | |
222 | delete m_imageListSmall; | |
457814b5 JS |
223 | } |
224 | ||
98ec9dbe | 225 | void MyFrame::OnSize(wxSizeEvent& event) |
457814b5 | 226 | { |
98ec9dbe VZ |
227 | if ( !m_logWindow ) |
228 | return; | |
457814b5 | 229 | |
98ec9dbe VZ |
230 | wxSize size = GetClientSize(); |
231 | wxCoord y = (2*size.y)/3; | |
232 | m_listCtrl->SetSize(0, 0, size.x, y); | |
233 | m_logWindow->SetSize(0, y + 1, size.x, size.y - y); | |
234 | ||
235 | event.Skip(); | |
57246713 KB |
236 | } |
237 | ||
98ec9dbe | 238 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
57246713 | 239 | { |
98ec9dbe | 240 | Close(TRUE); |
57246713 KB |
241 | } |
242 | ||
b0d77f43 | 243 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 244 | { |
5ea47806 VZ |
245 | wxMessageDialog dialog(this, "List test sample\nJulian Smart (c) 1997", |
246 | "About list test", wxOK|wxCANCEL); | |
457814b5 | 247 | |
5ea47806 | 248 | dialog.ShowModal(); |
457814b5 JS |
249 | } |
250 | ||
58b3bdc9 VZ |
251 | void MyFrame::OnToggleFirstSel(wxCommandEvent& WXUNUSED(event)) |
252 | { | |
253 | m_listCtrl->SetItemState(0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED); | |
254 | } | |
255 | ||
c4771147 KB |
256 | void MyFrame::OnDeselectAll(wxCommandEvent& WXUNUSED(event)) |
257 | { | |
5ea47806 VZ |
258 | int n = m_listCtrl->GetItemCount(); |
259 | for (int i = 0; i < n; i++) | |
260 | m_listCtrl->SetItemState(i,0,wxLIST_STATE_SELECTED); | |
c4771147 KB |
261 | } |
262 | ||
263 | void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event)) | |
264 | { | |
5ea47806 VZ |
265 | int n = m_listCtrl->GetItemCount(); |
266 | for (int i = 0; i < n; i++) | |
267 | m_listCtrl->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED); | |
c4771147 KB |
268 | } |
269 | ||
776a33cf VZ |
270 | // ---------------------------------------------------------------------------- |
271 | // changing listctrl modes | |
272 | // ---------------------------------------------------------------------------- | |
273 | ||
274 | void MyFrame::RecreateList(long flags, bool withText) | |
457814b5 | 275 | { |
776a33cf VZ |
276 | // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL |
277 | // style, but it is more trouble to do it than not | |
278 | #if 0 | |
279 | if ( !m_listCtrl || ((flags & wxLC_VIRTUAL) != | |
280 | (m_listCtrl->GetWindowStyleFlag() & wxLC_VIRTUAL)) ) | |
281 | #endif | |
282 | { | |
283 | delete m_listCtrl; | |
284 | ||
285 | m_listCtrl = new MyListCtrl(this, LIST_CTRL, | |
286 | wxDefaultPosition, wxDefaultSize, | |
287 | flags | | |
288 | wxSUNKEN_BORDER); | |
289 | ||
290 | switch ( flags & wxLC_MASK_TYPE ) | |
291 | { | |
292 | case wxLC_LIST: | |
293 | InitWithListItems(); | |
294 | break; | |
295 | ||
296 | case wxLC_ICON: | |
297 | InitWithIconItems(withText); | |
298 | break; | |
299 | ||
300 | case wxLC_SMALL_ICON: | |
301 | InitWithIconItems(withText, TRUE); | |
302 | break; | |
303 | ||
304 | case wxLC_REPORT: | |
305 | if ( flags & wxLC_VIRTUAL ) | |
306 | InitWithVirtualItems(); | |
307 | else | |
308 | InitWithReportItems(); | |
309 | break; | |
310 | ||
311 | default: | |
312 | wxFAIL_MSG( _T("unknown listctrl mode") ); | |
313 | } | |
314 | } | |
315 | ||
316 | #ifdef __WXMSW__ | |
317 | SendSizeEvent(); | |
318 | #endif | |
319 | ||
0180dad6 | 320 | m_logWindow->Clear(); |
776a33cf | 321 | } |
95bf655c | 322 | |
776a33cf VZ |
323 | void MyFrame::OnListView(wxCommandEvent& WXUNUSED(event)) |
324 | { | |
325 | RecreateList(wxLC_LIST); | |
326 | } | |
457814b5 | 327 | |
776a33cf VZ |
328 | void MyFrame::InitWithListItems() |
329 | { | |
330 | for ( int i = 0; i < NUM_ITEMS; i++ ) | |
0180dad6 | 331 | { |
98ec9dbe | 332 | m_listCtrl->InsertItem(i, wxString::Format(_T("Item %d"), i)); |
0180dad6 | 333 | } |
457814b5 JS |
334 | } |
335 | ||
b0d77f43 | 336 | void MyFrame::OnReportView(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 337 | { |
776a33cf VZ |
338 | RecreateList(wxLC_REPORT); |
339 | } | |
5ea47806 | 340 | |
776a33cf VZ |
341 | void MyFrame::InitWithReportItems() |
342 | { | |
c41ea66a | 343 | m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL); |
457814b5 | 344 | |
0180dad6 RR |
345 | m_listCtrl->InsertColumn(0, "Column 1"); // , wxLIST_FORMAT_LEFT, 140); |
346 | m_listCtrl->InsertColumn(1, "Column 2"); // , wxLIST_FORMAT_LEFT, 140); | |
347 | m_listCtrl->InsertColumn(2, "One More Column (2)"); // , wxLIST_FORMAT_LEFT, 140); | |
348 | ||
81278df2 VZ |
349 | // to speed up inserting we hide the control temporarily |
350 | m_listCtrl->Hide(); | |
351 | ||
352 | wxStopWatch sw; | |
353 | ||
354 | wxString buf; | |
81278df2 | 355 | for ( int i = 0; i < NUM_ITEMS; i++ ) |
0180dad6 | 356 | { |
81278df2 | 357 | buf.Printf(_T("This is item %d"), i); |
5ea47806 | 358 | long tmp = m_listCtrl->InsertItem(i, buf, 0); |
d62228a6 | 359 | m_listCtrl->SetItemData(tmp, i); |
5ea47806 | 360 | |
81278df2 | 361 | buf.Printf(_T("Col 1, item %d"), i); |
5ea47806 VZ |
362 | tmp = m_listCtrl->SetItem(i, 1, buf); |
363 | ||
81278df2 | 364 | buf.Printf(_T("Item %d in column 2"), i); |
5ea47806 | 365 | tmp = m_listCtrl->SetItem(i, 2, buf); |
0180dad6 | 366 | } |
bdc72a22 | 367 | |
81278df2 VZ |
368 | m_logWindow->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"), |
369 | NUM_ITEMS, sw.Time())); | |
370 | m_listCtrl->Show(); | |
371 | ||
f6b77239 | 372 | // we leave all mask fields to 0 and only change the colour |
bdc72a22 VZ |
373 | wxListItem item; |
374 | item.m_itemId = 0; | |
0530737d | 375 | item.SetTextColour(*wxRED); |
bdc72a22 VZ |
376 | m_listCtrl->SetItem( item ); |
377 | ||
378 | item.m_itemId = 2; | |
d62228a6 | 379 | item.SetTextColour(*wxGREEN); |
bdc72a22 | 380 | m_listCtrl->SetItem( item ); |
d62228a6 | 381 | item.m_itemId = 4; |
bdc72a22 | 382 | item.SetTextColour(*wxLIGHT_GREY); |
d62228a6 VZ |
383 | item.SetFont(*wxITALIC_FONT); |
384 | item.SetBackgroundColour(*wxRED); | |
bdc72a22 | 385 | m_listCtrl->SetItem( item ); |
5ea47806 | 386 | |
d62228a6 VZ |
387 | m_listCtrl->SetTextColour(*wxBLUE); |
388 | m_listCtrl->SetBackgroundColour(*wxLIGHT_GREY); | |
389 | ||
0180dad6 RR |
390 | m_listCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE ); |
391 | m_listCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE ); | |
392 | m_listCtrl->SetColumnWidth( 2, wxLIST_AUTOSIZE ); | |
457814b5 JS |
393 | } |
394 | ||
776a33cf | 395 | void MyFrame::InitWithIconItems(bool withText, bool sameIcon) |
457814b5 | 396 | { |
c41ea66a VZ |
397 | m_listCtrl->SetImageList(m_imageListNormal, wxIMAGE_LIST_NORMAL); |
398 | m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL); | |
457814b5 | 399 | |
776a33cf | 400 | for ( int i = 0; i < NUM_ICONS; i++ ) |
5ea47806 | 401 | { |
776a33cf VZ |
402 | int image = sameIcon ? 0 : i; |
403 | ||
404 | if ( withText ) | |
405 | { | |
406 | m_listCtrl->InsertItem(i, wxString::Format(_T("Label %d"), i), | |
407 | image); | |
408 | } | |
409 | else | |
410 | { | |
411 | m_listCtrl->InsertItem(i, image); | |
412 | } | |
5ea47806 | 413 | } |
457814b5 JS |
414 | } |
415 | ||
776a33cf | 416 | void MyFrame::OnIconView(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 417 | { |
776a33cf VZ |
418 | RecreateList(wxLC_ICON, FALSE); |
419 | } | |
457814b5 | 420 | |
776a33cf VZ |
421 | void MyFrame::OnIconTextView(wxCommandEvent& WXUNUSED(event)) |
422 | { | |
423 | RecreateList(wxLC_ICON); | |
457814b5 JS |
424 | } |
425 | ||
b0d77f43 | 426 | void MyFrame::OnSmallIconView(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 427 | { |
776a33cf | 428 | RecreateList(wxLC_SMALL_ICON, FALSE); |
457814b5 JS |
429 | } |
430 | ||
b0d77f43 | 431 | void MyFrame::OnSmallIconTextView(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 432 | { |
776a33cf | 433 | RecreateList(wxLC_SMALL_ICON); |
5ea47806 VZ |
434 | } |
435 | ||
98ec9dbe VZ |
436 | void MyFrame::OnVirtualView(wxCommandEvent& WXUNUSED(event)) |
437 | { | |
776a33cf VZ |
438 | RecreateList(wxLC_REPORT | wxLC_VIRTUAL); |
439 | } | |
98ec9dbe | 440 | |
776a33cf VZ |
441 | void MyFrame::InitWithVirtualItems() |
442 | { | |
98ec9dbe VZ |
443 | m_listCtrl->InsertColumn(0, "First Column"); |
444 | m_listCtrl->InsertColumn(1, "Second Column"); | |
445 | m_listCtrl->SetColumnWidth(0, 150); | |
446 | m_listCtrl->SetColumnWidth(1, 150); | |
447 | ||
448 | m_listCtrl->SetItemCount(1000000); | |
98ec9dbe VZ |
449 | } |
450 | ||
fa5f6926 VZ |
451 | void MyFrame::OnSort(wxCommandEvent& WXUNUSED(event)) |
452 | { | |
81278df2 VZ |
453 | wxStopWatch sw; |
454 | ||
fa5f6926 | 455 | m_listCtrl->SortItems(MyCompareFunction, 0); |
81278df2 VZ |
456 | |
457 | m_logWindow->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"), | |
458 | m_listCtrl->GetItemCount(), | |
459 | sw.Time())); | |
fa5f6926 VZ |
460 | } |
461 | ||
f6bcfd97 BP |
462 | void MyFrame::OnShowColInfo(wxCommandEvent& event) |
463 | { | |
464 | int count = m_listCtrl->GetColumnCount(); | |
465 | wxLogMessage("%d columns:", count); | |
466 | for ( int c = 0; c < count; c++ ) | |
467 | { | |
468 | wxLogMessage("\tcolumn %d has width %d", c, | |
469 | m_listCtrl->GetColumnWidth(c)); | |
470 | } | |
471 | } | |
472 | ||
473 | void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent& event) | |
474 | { | |
475 | event.Enable( (m_listCtrl->GetWindowStyleFlag() & wxLC_REPORT) != 0 ); | |
476 | } | |
477 | ||
7b848b0d VZ |
478 | void MyFrame::OnToggleMultiSel(wxCommandEvent& WXUNUSED(event)) |
479 | { | |
7b848b0d VZ |
480 | long flags = m_listCtrl->GetWindowStyleFlag(); |
481 | if ( flags & wxLC_SINGLE_SEL ) | |
776a33cf | 482 | flags &= ~wxLC_SINGLE_SEL; |
7b848b0d | 483 | else |
776a33cf VZ |
484 | flags |= wxLC_SINGLE_SEL; |
485 | ||
486 | m_logWindow->WriteText(wxString::Format("Current selection mode: %sle\n", | |
487 | (flags & wxLC_SINGLE_SEL) ? "sing" : "multip")); | |
7b848b0d | 488 | |
776a33cf | 489 | RecreateList(flags); |
7b848b0d VZ |
490 | } |
491 | ||
11f26ea0 VZ |
492 | void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event)) |
493 | { | |
494 | m_listCtrl->SetForegroundColour(wxGetColourFromUser(this)); | |
495 | m_listCtrl->Refresh(); | |
496 | } | |
497 | ||
498 | void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event)) | |
499 | { | |
500 | m_listCtrl->SetBackgroundColour(wxGetColourFromUser(this)); | |
501 | m_listCtrl->Refresh(); | |
502 | } | |
503 | ||
cf1dfa6b VZ |
504 | void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event)) |
505 | { | |
506 | m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), _T("Appended item")); | |
507 | } | |
508 | ||
bec0a261 VZ |
509 | void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event)) |
510 | { | |
511 | if ( m_listCtrl->GetItemCount() ) | |
512 | { | |
513 | m_listCtrl->DeleteItem(0); | |
514 | } | |
515 | else | |
516 | { | |
517 | m_logWindow->WriteText("Nothing to delete"); | |
518 | } | |
519 | } | |
520 | ||
5ea47806 VZ |
521 | void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event)) |
522 | { | |
81278df2 | 523 | wxStopWatch sw; |
5ea47806 | 524 | |
5ea47806 VZ |
525 | m_listCtrl->DeleteAllItems(); |
526 | ||
81278df2 VZ |
527 | m_logWindow->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"), |
528 | m_listCtrl->GetItemCount(), | |
529 | sw.Time())); | |
457814b5 JS |
530 | } |
531 | ||
532 | // MyListCtrl | |
533 | ||
8636aed8 RR |
534 | void MyListCtrl::OnColClick(wxListEvent& event) |
535 | { | |
c41ea66a | 536 | wxLogMessage( "OnColumnClick at %d.", event.GetColumn() ); |
8636aed8 RR |
537 | } |
538 | ||
fd9811b1 | 539 | void MyListCtrl::OnBeginDrag(wxListEvent& event) |
457814b5 | 540 | { |
c41ea66a VZ |
541 | wxLogMessage( "OnBeginDrag at %d,%d.", |
542 | event.m_pointDrag.x, event.m_pointDrag.y ); | |
457814b5 JS |
543 | } |
544 | ||
fd9811b1 | 545 | void MyListCtrl::OnBeginRDrag(wxListEvent& event) |
457814b5 | 546 | { |
c41ea66a VZ |
547 | wxLogMessage( "OnBeginRDrag at %d,%d.", |
548 | event.m_pointDrag.x, event.m_pointDrag.y ); | |
457814b5 JS |
549 | } |
550 | ||
fd9811b1 | 551 | void MyListCtrl::OnBeginLabelEdit(wxListEvent& event) |
457814b5 | 552 | { |
c41ea66a | 553 | wxLogMessage("OnBeginLabelEdit: %s", event.m_item.m_text.c_str()); |
457814b5 JS |
554 | } |
555 | ||
fd9811b1 | 556 | void MyListCtrl::OnEndLabelEdit(wxListEvent& event) |
457814b5 | 557 | { |
c41ea66a | 558 | wxLogMessage("OnEndLabelEdit: %s", event.m_item.m_text.c_str()); |
457814b5 JS |
559 | } |
560 | ||
efbb7287 | 561 | void MyListCtrl::OnDeleteItem(wxListEvent& event) |
457814b5 | 562 | { |
c41ea66a | 563 | LogEvent(event, _T("OnDeleteItem")); |
457814b5 JS |
564 | } |
565 | ||
c41ea66a | 566 | void MyListCtrl::OnDeleteAllItems(wxListEvent& event) |
12c1b46a | 567 | { |
c41ea66a | 568 | LogEvent(event, _T("OnDeleteAllItems")); |
12c1b46a RR |
569 | } |
570 | ||
fd9811b1 | 571 | void MyListCtrl::OnGetInfo(wxListEvent& event) |
457814b5 | 572 | { |
c41ea66a | 573 | wxString msg; |
5ea47806 | 574 | |
c41ea66a | 575 | msg << "OnGetInfo (" << event.m_item.m_itemId << ", " << event.m_item.m_col << ")"; |
5ea47806 | 576 | if ( event.m_item.m_mask & wxLIST_MASK_STATE ) |
c41ea66a | 577 | msg << " wxLIST_MASK_STATE"; |
5ea47806 | 578 | if ( event.m_item.m_mask & wxLIST_MASK_TEXT ) |
c41ea66a | 579 | msg << " wxLIST_MASK_TEXT"; |
5ea47806 | 580 | if ( event.m_item.m_mask & wxLIST_MASK_IMAGE ) |
c41ea66a | 581 | msg << " wxLIST_MASK_IMAGE"; |
5ea47806 | 582 | if ( event.m_item.m_mask & wxLIST_MASK_DATA ) |
c41ea66a | 583 | msg << " wxLIST_MASK_DATA"; |
5ea47806 | 584 | if ( event.m_item.m_mask & wxLIST_SET_ITEM ) |
c41ea66a | 585 | msg << " wxLIST_SET_ITEM"; |
5ea47806 | 586 | if ( event.m_item.m_mask & wxLIST_MASK_WIDTH ) |
c41ea66a | 587 | msg << " wxLIST_MASK_WIDTH"; |
5ea47806 | 588 | if ( event.m_item.m_mask & wxLIST_MASK_FORMAT ) |
c41ea66a | 589 | msg << " wxLIST_MASK_WIDTH"; |
5ea47806 VZ |
590 | |
591 | if ( event.m_item.m_mask & wxLIST_MASK_TEXT ) | |
592 | { | |
593 | event.m_item.m_text = "My callback text"; | |
594 | } | |
c41ea66a VZ |
595 | |
596 | wxLogMessage(msg); | |
457814b5 JS |
597 | } |
598 | ||
efbb7287 | 599 | void MyListCtrl::OnSetInfo(wxListEvent& event) |
457814b5 | 600 | { |
c41ea66a | 601 | LogEvent(event, _T("OnSetInfo")); |
457814b5 JS |
602 | } |
603 | ||
74b31181 | 604 | void MyListCtrl::OnSelected(wxListEvent& event) |
457814b5 | 605 | { |
c41ea66a | 606 | LogEvent(event, _T("OnSelected")); |
457814b5 | 607 | |
40779a03 | 608 | if ( GetWindowStyle() & wxLC_REPORT ) |
74b31181 | 609 | { |
40779a03 VZ |
610 | wxListItem info; |
611 | info.m_itemId = event.m_itemIndex; | |
612 | info.m_col = 1; | |
613 | info.m_mask = wxLIST_MASK_TEXT; | |
614 | if ( GetItem(info) ) | |
615 | { | |
c41ea66a VZ |
616 | wxLogMessage("Value of the 2nd field of the selected item: %s", |
617 | info.m_text.c_str()); | |
40779a03 VZ |
618 | } |
619 | else | |
620 | { | |
621 | wxFAIL_MSG("wxListCtrl::GetItem() failed"); | |
622 | } | |
74b31181 | 623 | } |
457814b5 JS |
624 | } |
625 | ||
efbb7287 | 626 | void MyListCtrl::OnDeselected(wxListEvent& event) |
457814b5 | 627 | { |
c41ea66a | 628 | LogEvent(event, _T("OnDeselected")); |
457814b5 JS |
629 | } |
630 | ||
efbb7287 | 631 | void MyListCtrl::OnActivated(wxListEvent& event) |
435fe83e | 632 | { |
c41ea66a | 633 | LogEvent(event, _T("OnActivated")); |
435fe83e RR |
634 | } |
635 | ||
8e1d4f96 | 636 | void MyListCtrl::OnListKeyDown(wxListEvent& event) |
457814b5 | 637 | { |
c41ea66a | 638 | LogEvent(event, _T("OnListKeyDown")); |
f6bcfd97 BP |
639 | |
640 | event.Skip(); | |
641 | } | |
642 | ||
643 | void MyListCtrl::OnChar(wxKeyEvent& event) | |
644 | { | |
645 | wxLogMessage(_T("Got char event.")); | |
646 | ||
647 | event.Skip(); | |
457814b5 JS |
648 | } |
649 | ||
c41ea66a VZ |
650 | void MyListCtrl::LogEvent(const wxListEvent& event, const wxChar *eventName) |
651 | { | |
f6bcfd97 BP |
652 | wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"), |
653 | event.GetIndex(), eventName, | |
654 | event.GetText().c_str(), event.GetData()); | |
c41ea66a | 655 | } |
435fe83e | 656 | |
98ec9dbe VZ |
657 | wxString MyListCtrl::OnGetItemText(long item, long column) const |
658 | { | |
659 | return wxString::Format(_T("Column %ld of item %ld"), column, item); | |
660 | } | |
661 | ||
662 | int MyListCtrl::OnGetItemImage(long item) const | |
663 | { | |
664 | return 0; | |
665 | } | |
666 |