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