]> git.saurik.com Git - wxWidgets.git/blame - samples/listctrl/listtest.cpp
added missing include
[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$
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
50BEGIN_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)
5ea47806 67 EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll)
fa5f6926 68 EVT_MENU(LIST_SORT, MyFrame::OnSort)
11f26ea0
VZ
69 EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour)
70 EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour)
7b848b0d 71 EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel)
f6bcfd97 72 EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo)
98ec9dbe 73
f6bcfd97 74 EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo)
457814b5
JS
75END_EVENT_TABLE()
76
77BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
5ea47806
VZ
78 EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag)
79 EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag)
80 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnBeginLabelEdit)
81 EVT_LIST_END_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnEndLabelEdit)
82 EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem)
12c1b46a 83 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems)
5ea47806
VZ
84 EVT_LIST_GET_INFO(LIST_CTRL, MyListCtrl::OnGetInfo)
85 EVT_LIST_SET_INFO(LIST_CTRL, MyListCtrl::OnSetInfo)
86 EVT_LIST_ITEM_SELECTED(LIST_CTRL, MyListCtrl::OnSelected)
87 EVT_LIST_ITEM_DESELECTED(LIST_CTRL, MyListCtrl::OnDeselected)
88 EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
89 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
8636aed8 90 EVT_LIST_COL_CLICK(LIST_CTRL, MyListCtrl::OnColClick)
f6bcfd97
BP
91
92 EVT_CHAR(MyListCtrl::OnChar)
457814b5
JS
93END_EVENT_TABLE()
94
95IMPLEMENT_APP(MyApp)
96
fa5f6926
VZ
97int wxCALLBACK MyCompareFunction(long item1, long item2, long sortData)
98{
99 // inverse the order
100 return item1 < item2;
101}
102
457814b5 103// `Main program' equivalent, creating windows and returning main app frame
11f26ea0 104bool MyApp::OnInit()
457814b5
JS
105{
106 // Create the main frame window
c41ea66a 107 MyFrame *frame = new MyFrame("wxListCtrl Test", 50, 50, 450, 340);
457814b5
JS
108
109 // Show the frame
110 frame->Show(TRUE);
5ea47806 111
457814b5
JS
112 SetTopWindow(frame);
113
114 return TRUE;
115}
116
117// My frame constructor
c41ea66a
VZ
118MyFrame::MyFrame(const wxChar *title, int x, int y, int w, int h)
119 : wxFrame((wxFrame *)NULL, -1, title, wxPoint(x, y), wxSize(w, h))
457814b5 120{
5ea47806
VZ
121 m_listCtrl = (MyListCtrl *) NULL;
122 m_logWindow = (wxTextCtrl *) NULL;
c41ea66a
VZ
123
124 // Give it an icon
125 SetIcon( wxICON(mondrian) );
126
127 // Make an image list containing large icons
128 m_imageListNormal = new wxImageList(32, 32, TRUE);
129 m_imageListSmall = new wxImageList(16, 16, TRUE);
130
131#ifdef __WXMSW__
132 m_imageListNormal->Add( wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE) );
133 m_imageListNormal->Add( wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE) );
134 m_imageListNormal->Add( wxIcon("icon3", wxBITMAP_TYPE_ICO_RESOURCE) );
135 m_imageListNormal->Add( wxIcon("icon4", wxBITMAP_TYPE_ICO_RESOURCE) );
136 m_imageListNormal->Add( wxIcon("icon5", wxBITMAP_TYPE_ICO_RESOURCE) );
137 m_imageListNormal->Add( wxIcon("icon6", wxBITMAP_TYPE_ICO_RESOURCE) );
138 m_imageListNormal->Add( wxIcon("icon7", wxBITMAP_TYPE_ICO_RESOURCE) );
139 m_imageListNormal->Add( wxIcon("icon8", wxBITMAP_TYPE_ICO_RESOURCE) );
140 m_imageListNormal->Add( wxIcon("icon9", wxBITMAP_TYPE_ICO_RESOURCE) );
141
142 m_imageListSmall->Add( wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE) );
143
144#else
145 m_imageListNormal->Add( wxIcon( toolbrai_xpm ) );
146 m_imageListNormal->Add( wxIcon( toolchar_xpm ) );
147 m_imageListNormal->Add( wxIcon( tooldata_xpm ) );
148 m_imageListNormal->Add( wxIcon( toolnote_xpm ) );
149 m_imageListNormal->Add( wxIcon( tooltodo_xpm ) );
150 m_imageListNormal->Add( wxIcon( toolchec_xpm ) );
151 m_imageListNormal->Add( wxIcon( toolgame_xpm ) );
152 m_imageListNormal->Add( wxIcon( tooltime_xpm ) );
153 m_imageListNormal->Add( wxIcon( toolword_xpm ) );
154
155 m_imageListSmall->Add( wxIcon( small1_xpm) );
156#endif
157
158 // Make a menubar
159 wxMenu *menuFile = new wxMenu;
98ec9dbe 160 menuFile->Append(LIST_ABOUT, _T("&About"));
c41ea66a 161 menuFile->AppendSeparator();
98ec9dbe 162 menuFile->Append(LIST_QUIT, _T("E&xit\tAlt-X"));
c41ea66a
VZ
163
164 wxMenu *menuView = new wxMenu;
98ec9dbe
VZ
165 menuView->Append(LIST_LIST_VIEW, _T("&List view\tF1"));
166 menuView->Append(LIST_REPORT_VIEW, _T("&Report view\tF2"));
167 menuView->Append(LIST_ICON_VIEW, _T("&Icon view\tF3"));
168 menuView->Append(LIST_ICON_TEXT_VIEW, _T("Icon view with &text\tF4"));
169 menuView->Append(LIST_SMALL_ICON_VIEW, _T("&Small icon view\tF5"));
170 menuView->Append(LIST_SMALL_ICON_TEXT_VIEW, _T("Small icon &view with text\tF6"));
171 menuView->Append(LIST_VIRTUAL_VIEW, _T("Virtual view\tF7"));
c41ea66a
VZ
172
173 wxMenu *menuList = new wxMenu;
98ec9dbe
VZ
174 menuList->Append(LIST_TOGGLE_FIRST, _T("&Toggle first item\tCtrl-T"));
175 menuList->Append(LIST_DESELECT_ALL, _T("&Deselect All\tCtrl-D"));
176 menuList->Append(LIST_SELECT_ALL, _T("S&elect All\tCtrl-A"));
177 menuList->Append(LIST_SHOW_COL_INFO, _T("Show &column info\tCtrl-C"));
c41ea66a 178 menuList->AppendSeparator();
98ec9dbe 179 menuList->Append(LIST_SORT, _T("&Sort\tCtrl-S"));
c41ea66a 180 menuList->AppendSeparator();
98ec9dbe
VZ
181 menuList->Append(LIST_DELETE, _T("&Delete first item"));
182 menuList->Append(LIST_DELETE_ALL, _T("Delete &all items"));
c41ea66a 183 menuList->AppendSeparator();
98ec9dbe
VZ
184 menuList->Append(LIST_TOGGLE_MULTI_SEL, _T("&Multiple selection\tCtrl-M"),
185 _T("Toggle multiple selection"), TRUE);
c41ea66a
VZ
186
187 wxMenu *menuCol = new wxMenu;
98ec9dbe
VZ
188 menuCol->Append(LIST_SET_FG_COL, _T("&Foreground colour..."));
189 menuCol->Append(LIST_SET_BG_COL, _T("&Background colour..."));
c41ea66a
VZ
190
191 wxMenuBar *menubar = new wxMenuBar;
98ec9dbe
VZ
192 menubar->Append(menuFile, _T("&File"));
193 menubar->Append(menuView, _T("&View"));
194 menubar->Append(menuList, _T("&List"));
195 menubar->Append(menuCol, _T("&Colour"));
c41ea66a
VZ
196 SetMenuBar(menubar);
197
198 m_listCtrl = new MyListCtrl(this, LIST_CTRL,
199 wxDefaultPosition, wxDefaultSize,
200 wxLC_LIST |
201 wxSUNKEN_BORDER |
202 wxLC_EDIT_LABELS |
c41ea66a
VZ
203 wxLC_SINGLE_SEL
204 );
205
206 m_logWindow = new wxTextCtrl(this, -1, wxEmptyString,
207 wxDefaultPosition, wxDefaultSize,
208 wxTE_MULTILINE | wxSUNKEN_BORDER);
209
210 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow));
211
c41ea66a
VZ
212 for ( int i = 0; i < 30; i++ )
213 {
214 long idx = m_listCtrl->InsertItem(i, wxString::Format(_T("Item %d"), i));
215 m_listCtrl->SetItemData(idx, i*i);
216 }
217
218 CreateStatusBar(3);
457814b5
JS
219}
220
11f26ea0 221MyFrame::~MyFrame()
457814b5 222{
c41ea66a
VZ
223 delete wxLog::SetActiveTarget(m_logOld);
224
225 delete m_imageListNormal;
226 delete m_imageListSmall;
457814b5
JS
227}
228
98ec9dbe 229void MyFrame::OnSize(wxSizeEvent& event)
457814b5 230{
98ec9dbe
VZ
231 if ( !m_logWindow )
232 return;
457814b5 233
98ec9dbe
VZ
234 wxSize size = GetClientSize();
235 wxCoord y = (2*size.y)/3;
236 m_listCtrl->SetSize(0, 0, size.x, y);
237 m_logWindow->SetSize(0, y + 1, size.x, size.y - y);
238
239 event.Skip();
57246713
KB
240}
241
98ec9dbe 242void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
57246713 243{
98ec9dbe 244 Close(TRUE);
57246713
KB
245}
246
b0d77f43 247void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
457814b5 248{
5ea47806
VZ
249 wxMessageDialog dialog(this, "List test sample\nJulian Smart (c) 1997",
250 "About list test", wxOK|wxCANCEL);
457814b5 251
5ea47806 252 dialog.ShowModal();
457814b5
JS
253}
254
58b3bdc9
VZ
255void MyFrame::OnToggleFirstSel(wxCommandEvent& WXUNUSED(event))
256{
257 m_listCtrl->SetItemState(0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
258}
259
c4771147
KB
260void MyFrame::OnDeselectAll(wxCommandEvent& WXUNUSED(event))
261{
5ea47806
VZ
262 int n = m_listCtrl->GetItemCount();
263 for (int i = 0; i < n; i++)
264 m_listCtrl->SetItemState(i,0,wxLIST_STATE_SELECTED);
c4771147
KB
265}
266
267void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event))
268{
5ea47806
VZ
269 int n = m_listCtrl->GetItemCount();
270 for (int i = 0; i < n; i++)
271 m_listCtrl->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
c4771147
KB
272}
273
b0d77f43 274void MyFrame::OnListView(wxCommandEvent& WXUNUSED(event))
457814b5 275{
95bf655c 276 m_listCtrl->ClearAll();
0180dad6 277 m_logWindow->Clear();
95bf655c 278
0180dad6 279 m_listCtrl->SetSingleStyle(wxLC_LIST);
c67daf87
UR
280 m_listCtrl->SetImageList((wxImageList *) NULL, wxIMAGE_LIST_NORMAL);
281 m_listCtrl->SetImageList((wxImageList *) NULL, wxIMAGE_LIST_SMALL);
457814b5 282
0180dad6
RR
283 for ( int i=0; i < 30; i++)
284 {
98ec9dbe 285 m_listCtrl->InsertItem(i, wxString::Format(_T("Item %d"), i));
0180dad6 286 }
457814b5
JS
287}
288
b0d77f43 289void MyFrame::OnReportView(wxCommandEvent& WXUNUSED(event))
457814b5 290{
0180dad6 291 m_logWindow->Clear();
95bf655c 292 m_listCtrl->ClearAll();
5ea47806 293
0180dad6 294 m_listCtrl->SetSingleStyle(wxLC_REPORT);
c67daf87 295 m_listCtrl->SetImageList((wxImageList *) NULL, wxIMAGE_LIST_NORMAL);
c41ea66a 296 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
457814b5 297
0180dad6
RR
298 m_listCtrl->InsertColumn(0, "Column 1"); // , wxLIST_FORMAT_LEFT, 140);
299 m_listCtrl->InsertColumn(1, "Column 2"); // , wxLIST_FORMAT_LEFT, 140);
300 m_listCtrl->InsertColumn(2, "One More Column (2)"); // , wxLIST_FORMAT_LEFT, 140);
301
81278df2
VZ
302 // to speed up inserting we hide the control temporarily
303 m_listCtrl->Hide();
304
305 wxStopWatch sw;
306
307 wxString buf;
f6bcfd97 308 static const int NUM_ITEMS = 30;//00;
81278df2 309 for ( int i = 0; i < NUM_ITEMS; i++ )
0180dad6 310 {
81278df2 311 buf.Printf(_T("This is item %d"), i);
5ea47806 312 long tmp = m_listCtrl->InsertItem(i, buf, 0);
d62228a6 313 m_listCtrl->SetItemData(tmp, i);
5ea47806 314
81278df2 315 buf.Printf(_T("Col 1, item %d"), i);
5ea47806
VZ
316 tmp = m_listCtrl->SetItem(i, 1, buf);
317
81278df2 318 buf.Printf(_T("Item %d in column 2"), i);
5ea47806 319 tmp = m_listCtrl->SetItem(i, 2, buf);
0180dad6 320 }
bdc72a22 321
81278df2
VZ
322 m_logWindow->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
323 NUM_ITEMS, sw.Time()));
324 m_listCtrl->Show();
325
f6b77239 326 // we leave all mask fields to 0 and only change the colour
bdc72a22
VZ
327 wxListItem item;
328 item.m_itemId = 0;
0530737d 329 item.SetTextColour(*wxRED);
bdc72a22
VZ
330 m_listCtrl->SetItem( item );
331
332 item.m_itemId = 2;
d62228a6 333 item.SetTextColour(*wxGREEN);
bdc72a22 334 m_listCtrl->SetItem( item );
d62228a6 335 item.m_itemId = 4;
bdc72a22 336 item.SetTextColour(*wxLIGHT_GREY);
d62228a6
VZ
337 item.SetFont(*wxITALIC_FONT);
338 item.SetBackgroundColour(*wxRED);
bdc72a22 339 m_listCtrl->SetItem( item );
5ea47806 340
d62228a6
VZ
341 m_listCtrl->SetTextColour(*wxBLUE);
342 m_listCtrl->SetBackgroundColour(*wxLIGHT_GREY);
343
0180dad6
RR
344 m_listCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE );
345 m_listCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE );
346 m_listCtrl->SetColumnWidth( 2, wxLIST_AUTOSIZE );
457814b5
JS
347}
348
b0d77f43 349void MyFrame::OnIconView(wxCommandEvent& WXUNUSED(event))
457814b5 350{
5ea47806 351 m_logWindow->Clear();
95bf655c
RR
352 m_listCtrl->ClearAll();
353
5ea47806 354 m_listCtrl->SetSingleStyle(wxLC_ICON);
c41ea66a
VZ
355 m_listCtrl->SetImageList(m_imageListNormal, wxIMAGE_LIST_NORMAL);
356 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
457814b5 357
5ea47806
VZ
358 for ( int i=0; i < 9; i++)
359 {
360 m_listCtrl->InsertItem(i, i);
361 }
457814b5
JS
362}
363
b0d77f43 364void MyFrame::OnIconTextView(wxCommandEvent& WXUNUSED(event))
457814b5 365{
5ea47806 366 m_logWindow->Clear();
95bf655c
RR
367 m_listCtrl->ClearAll();
368
5ea47806 369 m_listCtrl->SetSingleStyle(wxLC_ICON);
c41ea66a
VZ
370 m_listCtrl->SetImageList(m_imageListNormal, wxIMAGE_LIST_NORMAL);
371 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
457814b5 372
5ea47806
VZ
373 for ( int i=0; i < 9; i++)
374 {
375 wxChar buf[20];
376 wxSprintf(buf, _T("Label %d"), i);
377 m_listCtrl->InsertItem(i, buf, i);
378 }
457814b5
JS
379}
380
b0d77f43 381void MyFrame::OnSmallIconView(wxCommandEvent& WXUNUSED(event))
457814b5 382{
5ea47806 383 m_logWindow->Clear();
95bf655c
RR
384 m_listCtrl->ClearAll();
385
5ea47806 386 m_listCtrl->SetSingleStyle(wxLC_SMALL_ICON);
c41ea66a
VZ
387 m_listCtrl->SetImageList(m_imageListNormal, wxIMAGE_LIST_NORMAL);
388 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
457814b5 389
5ea47806
VZ
390 for ( int i=0; i < 9; i++)
391 {
392 m_listCtrl->InsertItem(i, 0);
393 }
457814b5
JS
394}
395
b0d77f43 396void MyFrame::OnSmallIconTextView(wxCommandEvent& WXUNUSED(event))
457814b5 397{
5ea47806 398 m_logWindow->Clear();
95bf655c
RR
399 m_listCtrl->ClearAll();
400
5ea47806 401 m_listCtrl->SetSingleStyle(wxLC_SMALL_ICON);
c41ea66a
VZ
402 m_listCtrl->SetImageList(m_imageListNormal, wxIMAGE_LIST_NORMAL);
403 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
457814b5 404
5ea47806
VZ
405 for ( int i=0; i < 9; i++)
406 {
407 m_listCtrl->InsertItem(i, "Label", 0);
408 }
409}
410
98ec9dbe
VZ
411void MyFrame::OnVirtualView(wxCommandEvent& WXUNUSED(event))
412{
413#ifdef __WXMSW__
414 m_logWindow->Clear();
415
416 // we really have to recreate it
417 delete m_listCtrl;
418
419 m_listCtrl = new MyListCtrl(this, LIST_CTRL,
420 wxDefaultPosition, wxDefaultSize,
421 wxLC_REPORT |
422 wxSUNKEN_BORDER |
423 wxLC_SINGLE_SEL |
424 wxLC_VIRTUAL
425 );
426
427 m_listCtrl->InsertColumn(0, "First Column");
428 m_listCtrl->InsertColumn(1, "Second Column");
429 m_listCtrl->SetColumnWidth(0, 150);
430 m_listCtrl->SetColumnWidth(1, 150);
431
432 m_listCtrl->SetItemCount(1000000);
433
434 SendSizeEvent();
435#else
436 wxLogError(_T("Sorry, not implemented"));
437#endif
438}
439
fa5f6926
VZ
440void MyFrame::OnSort(wxCommandEvent& WXUNUSED(event))
441{
81278df2
VZ
442 wxStopWatch sw;
443
fa5f6926 444 m_listCtrl->SortItems(MyCompareFunction, 0);
81278df2
VZ
445
446 m_logWindow->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
447 m_listCtrl->GetItemCount(),
448 sw.Time()));
fa5f6926
VZ
449}
450
f6bcfd97
BP
451void MyFrame::OnShowColInfo(wxCommandEvent& event)
452{
453 int count = m_listCtrl->GetColumnCount();
454 wxLogMessage("%d columns:", count);
455 for ( int c = 0; c < count; c++ )
456 {
457 wxLogMessage("\tcolumn %d has width %d", c,
458 m_listCtrl->GetColumnWidth(c));
459 }
460}
461
462void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent& event)
463{
464 event.Enable( (m_listCtrl->GetWindowStyleFlag() & wxLC_REPORT) != 0 );
465}
466
7b848b0d
VZ
467void MyFrame::OnToggleMultiSel(wxCommandEvent& WXUNUSED(event))
468{
469 m_logWindow->WriteText("Current selection mode: ");
470
471 long flags = m_listCtrl->GetWindowStyleFlag();
472 if ( flags & wxLC_SINGLE_SEL )
473 {
474 m_listCtrl->SetWindowStyleFlag(flags & ~wxLC_SINGLE_SEL);
475 m_logWindow->WriteText("multiple");
476 }
477 else
478 {
479 m_listCtrl->SetWindowStyleFlag(flags | wxLC_SINGLE_SEL);
480 m_logWindow->WriteText("single");
481 }
482
483 m_logWindow->WriteText("\nRecreate the control now\n");
484}
485
11f26ea0
VZ
486void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event))
487{
488 m_listCtrl->SetForegroundColour(wxGetColourFromUser(this));
489 m_listCtrl->Refresh();
490}
491
492void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event))
493{
494 m_listCtrl->SetBackgroundColour(wxGetColourFromUser(this));
495 m_listCtrl->Refresh();
496}
497
bec0a261
VZ
498void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
499{
500 if ( m_listCtrl->GetItemCount() )
501 {
502 m_listCtrl->DeleteItem(0);
503 }
504 else
505 {
506 m_logWindow->WriteText("Nothing to delete");
507 }
508}
509
5ea47806
VZ
510void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
511{
81278df2 512 wxStopWatch sw;
5ea47806 513
5ea47806
VZ
514 m_listCtrl->DeleteAllItems();
515
81278df2
VZ
516 m_logWindow->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
517 m_listCtrl->GetItemCount(),
518 sw.Time()));
457814b5
JS
519}
520
521// MyListCtrl
522
8636aed8
RR
523void MyListCtrl::OnColClick(wxListEvent& event)
524{
c41ea66a 525 wxLogMessage( "OnColumnClick at %d.", event.GetColumn() );
8636aed8
RR
526}
527
fd9811b1 528void MyListCtrl::OnBeginDrag(wxListEvent& event)
457814b5 529{
c41ea66a
VZ
530 wxLogMessage( "OnBeginDrag at %d,%d.",
531 event.m_pointDrag.x, event.m_pointDrag.y );
457814b5
JS
532}
533
fd9811b1 534void MyListCtrl::OnBeginRDrag(wxListEvent& event)
457814b5 535{
c41ea66a
VZ
536 wxLogMessage( "OnBeginRDrag at %d,%d.",
537 event.m_pointDrag.x, event.m_pointDrag.y );
457814b5
JS
538}
539
fd9811b1 540void MyListCtrl::OnBeginLabelEdit(wxListEvent& event)
457814b5 541{
c41ea66a 542 wxLogMessage("OnBeginLabelEdit: %s", event.m_item.m_text.c_str());
457814b5
JS
543}
544
fd9811b1 545void MyListCtrl::OnEndLabelEdit(wxListEvent& event)
457814b5 546{
c41ea66a 547 wxLogMessage("OnEndLabelEdit: %s", event.m_item.m_text.c_str());
457814b5
JS
548}
549
efbb7287 550void MyListCtrl::OnDeleteItem(wxListEvent& event)
457814b5 551{
c41ea66a 552 LogEvent(event, _T("OnDeleteItem"));
457814b5
JS
553}
554
c41ea66a 555void MyListCtrl::OnDeleteAllItems(wxListEvent& event)
12c1b46a 556{
c41ea66a 557 LogEvent(event, _T("OnDeleteAllItems"));
12c1b46a
RR
558}
559
fd9811b1 560void MyListCtrl::OnGetInfo(wxListEvent& event)
457814b5 561{
c41ea66a 562 wxString msg;
5ea47806 563
c41ea66a 564 msg << "OnGetInfo (" << event.m_item.m_itemId << ", " << event.m_item.m_col << ")";
5ea47806 565 if ( event.m_item.m_mask & wxLIST_MASK_STATE )
c41ea66a 566 msg << " wxLIST_MASK_STATE";
5ea47806 567 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
c41ea66a 568 msg << " wxLIST_MASK_TEXT";
5ea47806 569 if ( event.m_item.m_mask & wxLIST_MASK_IMAGE )
c41ea66a 570 msg << " wxLIST_MASK_IMAGE";
5ea47806 571 if ( event.m_item.m_mask & wxLIST_MASK_DATA )
c41ea66a 572 msg << " wxLIST_MASK_DATA";
5ea47806 573 if ( event.m_item.m_mask & wxLIST_SET_ITEM )
c41ea66a 574 msg << " wxLIST_SET_ITEM";
5ea47806 575 if ( event.m_item.m_mask & wxLIST_MASK_WIDTH )
c41ea66a 576 msg << " wxLIST_MASK_WIDTH";
5ea47806 577 if ( event.m_item.m_mask & wxLIST_MASK_FORMAT )
c41ea66a 578 msg << " wxLIST_MASK_WIDTH";
5ea47806
VZ
579
580 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
581 {
582 event.m_item.m_text = "My callback text";
583 }
c41ea66a
VZ
584
585 wxLogMessage(msg);
457814b5
JS
586}
587
efbb7287 588void MyListCtrl::OnSetInfo(wxListEvent& event)
457814b5 589{
c41ea66a 590 LogEvent(event, _T("OnSetInfo"));
457814b5
JS
591}
592
74b31181 593void MyListCtrl::OnSelected(wxListEvent& event)
457814b5 594{
c41ea66a 595 LogEvent(event, _T("OnSelected"));
457814b5 596
40779a03 597 if ( GetWindowStyle() & wxLC_REPORT )
74b31181 598 {
40779a03
VZ
599 wxListItem info;
600 info.m_itemId = event.m_itemIndex;
601 info.m_col = 1;
602 info.m_mask = wxLIST_MASK_TEXT;
603 if ( GetItem(info) )
604 {
c41ea66a
VZ
605 wxLogMessage("Value of the 2nd field of the selected item: %s",
606 info.m_text.c_str());
40779a03
VZ
607 }
608 else
609 {
610 wxFAIL_MSG("wxListCtrl::GetItem() failed");
611 }
74b31181 612 }
457814b5
JS
613}
614
efbb7287 615void MyListCtrl::OnDeselected(wxListEvent& event)
457814b5 616{
c41ea66a 617 LogEvent(event, _T("OnDeselected"));
457814b5
JS
618}
619
efbb7287 620void MyListCtrl::OnActivated(wxListEvent& event)
435fe83e 621{
c41ea66a 622 LogEvent(event, _T("OnActivated"));
435fe83e
RR
623}
624
8e1d4f96 625void MyListCtrl::OnListKeyDown(wxListEvent& event)
457814b5 626{
c41ea66a 627 LogEvent(event, _T("OnListKeyDown"));
f6bcfd97
BP
628
629 event.Skip();
630}
631
632void MyListCtrl::OnChar(wxKeyEvent& event)
633{
634 wxLogMessage(_T("Got char event."));
635
636 event.Skip();
457814b5
JS
637}
638
c41ea66a
VZ
639void MyListCtrl::LogEvent(const wxListEvent& event, const wxChar *eventName)
640{
f6bcfd97
BP
641 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
642 event.GetIndex(), eventName,
643 event.GetText().c_str(), event.GetData());
c41ea66a 644}
435fe83e 645
98ec9dbe
VZ
646wxString MyListCtrl::OnGetItemText(long item, long column) const
647{
648 return wxString::Format(_T("Column %ld of item %ld"), column, item);
649}
650
651int MyListCtrl::OnGetItemImage(long item) const
652{
653 return 0;
654}
655