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