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