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