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