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