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