]> git.saurik.com Git - wxWidgets.git/blob - samples/listctrl/listtest.cpp
added wxDocument::AlreadySaved() and use it in OnUpdateFileSave() to ensure that...
[wxWidgets.git] / samples / listctrl / listtest.cpp
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
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #if !defined(__WXMSW__) && !defined(__WXPM__)
24 #include "mondrian.xpm"
25 #endif
26
27 #ifndef __WXMSW__
28 #include "bitmaps/toolbrai.xpm"
29 #include "bitmaps/toolchar.xpm"
30 #include "bitmaps/tooldata.xpm"
31 #include "bitmaps/toolnote.xpm"
32 #include "bitmaps/tooltodo.xpm"
33 #include "bitmaps/toolchec.xpm"
34 #include "bitmaps/toolgame.xpm"
35 #include "bitmaps/tooltime.xpm"
36 #include "bitmaps/toolword.xpm"
37 #include "bitmaps/small1.xpm"
38 #endif
39
40 #include "wx/imaglist.h"
41 #include "wx/listctrl.h"
42 #include "wx/timer.h" // for wxStopWatch
43 #include "wx/colordlg.h" // for wxGetColourFromUser
44 #include "wx/settings.h"
45 #include "wx/sysopt.h"
46
47 #include "listtest.h"
48
49 const wxChar *SMALL_VIRTUAL_VIEW_ITEMS[][2] =
50 {
51 { _T("Cat"), _T("meow") },
52 { _T("Cow"), _T("moo") },
53 { _T("Crow"), _T("caw") },
54 { _T("Dog"), _T("woof") },
55 { _T("Duck"), _T("quack") },
56 { _T("Mouse"), _T("squeak") },
57 { _T("Owl"), _T("hoo") },
58 { _T("Pig"), _T("oink") },
59 { _T("Pigeon"), _T("coo") },
60 { _T("Sheep"), _T("baaah") },
61 };
62
63
64 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
65 EVT_SIZE(MyFrame::OnSize)
66
67 EVT_MENU(LIST_QUIT, MyFrame::OnQuit)
68 EVT_MENU(LIST_ABOUT, MyFrame::OnAbout)
69 EVT_MENU(LIST_LIST_VIEW, MyFrame::OnListView)
70 EVT_MENU(LIST_REPORT_VIEW, MyFrame::OnReportView)
71 EVT_MENU(LIST_ICON_VIEW, MyFrame::OnIconView)
72 EVT_MENU(LIST_ICON_TEXT_VIEW, MyFrame::OnIconTextView)
73 EVT_MENU(LIST_SMALL_ICON_VIEW, MyFrame::OnSmallIconView)
74 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView)
75 EVT_MENU(LIST_VIRTUAL_VIEW, MyFrame::OnVirtualView)
76 EVT_MENU(LIST_SMALL_VIRTUAL_VIEW, MyFrame::OnSmallVirtualView)
77
78 EVT_MENU(LIST_GOTO, MyFrame::OnGoTo)
79 EVT_MENU(LIST_FOCUS_LAST, MyFrame::OnFocusLast)
80 EVT_MENU(LIST_TOGGLE_FIRST, MyFrame::OnToggleFirstSel)
81 EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
82 EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
83 EVT_MENU(LIST_DELETE, MyFrame::OnDelete)
84 EVT_MENU(LIST_ADD, MyFrame::OnAdd)
85 EVT_MENU(LIST_EDIT, MyFrame::OnEdit)
86 EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll)
87 EVT_MENU(LIST_SORT, MyFrame::OnSort)
88 EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour)
89 EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour)
90 EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel)
91 EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo)
92 EVT_MENU(LIST_SHOW_SEL_INFO, MyFrame::OnShowSelInfo)
93 EVT_MENU(LIST_SHOW_VIEW_RECT, MyFrame::OnShowViewRect)
94 EVT_MENU(LIST_FREEZE, MyFrame::OnFreeze)
95 EVT_MENU(LIST_THAW, MyFrame::OnThaw)
96 EVT_MENU(LIST_TOGGLE_LINES, MyFrame::OnToggleLines)
97 EVT_MENU(LIST_MAC_USE_GENERIC, MyFrame::OnToggleMacUseGeneric)
98
99 EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo)
100 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL, MyFrame::OnUpdateToggleMultiSel)
101 END_EVENT_TABLE()
102
103 BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
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)
109 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems)
110 EVT_LIST_ITEM_SELECTED(LIST_CTRL, MyListCtrl::OnSelected)
111 EVT_LIST_ITEM_DESELECTED(LIST_CTRL, MyListCtrl::OnDeselected)
112 EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
113 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
114 EVT_LIST_ITEM_FOCUSED(LIST_CTRL, MyListCtrl::OnFocused)
115
116 EVT_LIST_COL_CLICK(LIST_CTRL, MyListCtrl::OnColClick)
117 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL, MyListCtrl::OnColRightClick)
118 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnColBeginDrag)
119 EVT_LIST_COL_DRAGGING(LIST_CTRL, MyListCtrl::OnColDragging)
120 EVT_LIST_COL_END_DRAG(LIST_CTRL, MyListCtrl::OnColEndDrag)
121
122 EVT_LIST_CACHE_HINT(LIST_CTRL, MyListCtrl::OnCacheHint)
123
124 #if USE_CONTEXT_MENU
125 EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu)
126 #endif
127 EVT_CHAR(MyListCtrl::OnChar)
128
129 EVT_RIGHT_DOWN(MyListCtrl::OnRightClick)
130 END_EVENT_TABLE()
131
132 IMPLEMENT_APP(MyApp)
133
134 // number of items in list/report view
135 static const int NUM_ITEMS = 10;
136
137 // number of items in icon/small icon view
138 static const int NUM_ICONS = 9;
139
140 int wxCALLBACK MyCompareFunction(long item1, long item2, long WXUNUSED(sortData))
141 {
142 // inverse the order
143 if (item1 < item2)
144 return -1;
145 if (item1 > item2)
146 return 1;
147
148 return 0;
149 }
150
151 // `Main program' equivalent, creating windows and returning main app frame
152 bool MyApp::OnInit()
153 {
154 if ( !wxApp::OnInit() )
155 return false;
156
157 // Create the main frame window
158 MyFrame *frame = new MyFrame(wxT("wxListCtrl Test"));
159
160 // Show the frame
161 frame->Show(true);
162
163 SetTopWindow(frame);
164
165 return true;
166 }
167
168 // My frame constructor
169 MyFrame::MyFrame(const wxChar *title)
170 : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(600, 500))
171 {
172 m_listCtrl = NULL;
173 m_logWindow = NULL;
174 m_smallVirtual = false;
175
176 // Give it an icon
177 SetIcon( wxICON(mondrian) );
178
179 // Make an image list containing large icons
180 m_imageListNormal = new wxImageList(32, 32, true);
181 m_imageListSmall = new wxImageList(16, 16, true);
182
183 #ifdef __WXMSW__
184 m_imageListNormal->Add( wxIcon(_T("icon1"), wxBITMAP_TYPE_ICO_RESOURCE) );
185 m_imageListNormal->Add( wxIcon(_T("icon2"), wxBITMAP_TYPE_ICO_RESOURCE) );
186 m_imageListNormal->Add( wxIcon(_T("icon3"), wxBITMAP_TYPE_ICO_RESOURCE) );
187 m_imageListNormal->Add( wxIcon(_T("icon4"), wxBITMAP_TYPE_ICO_RESOURCE) );
188 m_imageListNormal->Add( wxIcon(_T("icon5"), wxBITMAP_TYPE_ICO_RESOURCE) );
189 m_imageListNormal->Add( wxIcon(_T("icon6"), wxBITMAP_TYPE_ICO_RESOURCE) );
190 m_imageListNormal->Add( wxIcon(_T("icon7"), wxBITMAP_TYPE_ICO_RESOURCE) );
191 m_imageListNormal->Add( wxIcon(_T("icon8"), wxBITMAP_TYPE_ICO_RESOURCE) );
192 m_imageListNormal->Add( wxIcon(_T("icon9"), wxBITMAP_TYPE_ICO_RESOURCE) );
193
194 m_imageListSmall->Add( wxIcon(_T("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE) );
195
196 #else
197 m_imageListNormal->Add( wxIcon( toolbrai_xpm ) );
198 m_imageListNormal->Add( wxIcon( toolchar_xpm ) );
199 m_imageListNormal->Add( wxIcon( tooldata_xpm ) );
200 m_imageListNormal->Add( wxIcon( toolnote_xpm ) );
201 m_imageListNormal->Add( wxIcon( tooltodo_xpm ) );
202 m_imageListNormal->Add( wxIcon( toolchec_xpm ) );
203 m_imageListNormal->Add( wxIcon( toolgame_xpm ) );
204 m_imageListNormal->Add( wxIcon( tooltime_xpm ) );
205 m_imageListNormal->Add( wxIcon( toolword_xpm ) );
206
207 m_imageListSmall->Add( wxIcon( small1_xpm) );
208 #endif
209
210 // Make a menubar
211 wxMenu *menuFile = new wxMenu;
212 menuFile->Append(LIST_ABOUT, _T("&About"));
213 menuFile->AppendSeparator();
214 menuFile->Append(LIST_QUIT, _T("E&xit\tAlt-X"));
215
216 wxMenu *menuView = new wxMenu;
217 menuView->Append(LIST_LIST_VIEW, _T("&List view\tF1"));
218 menuView->Append(LIST_REPORT_VIEW, _T("&Report view\tF2"));
219 menuView->Append(LIST_ICON_VIEW, _T("&Icon view\tF3"));
220 menuView->Append(LIST_ICON_TEXT_VIEW, _T("Icon view with &text\tF4"));
221 menuView->Append(LIST_SMALL_ICON_VIEW, _T("&Small icon view\tF5"));
222 menuView->Append(LIST_SMALL_ICON_TEXT_VIEW, _T("Small icon &view with text\tF6"));
223 menuView->Append(LIST_VIRTUAL_VIEW, _T("&Virtual view\tF7"));
224 menuView->Append(LIST_SMALL_VIRTUAL_VIEW, _T("Small virtual vie&w\tF8"));
225 #ifdef __WXMAC__
226 menuView->AppendCheckItem(LIST_MAC_USE_GENERIC, _T("Mac: Use Generic Control"));
227 #endif
228
229 wxMenu *menuList = new wxMenu;
230 menuList->Append(LIST_GOTO, _T("&Go to item #3\tCtrl-3"));
231 menuList->Append(LIST_FOCUS_LAST, _T("&Make last item current\tCtrl-L"));
232 menuList->Append(LIST_TOGGLE_FIRST, _T("To&ggle first item\tCtrl-G"));
233 menuList->Append(LIST_DESELECT_ALL, _T("&Deselect All\tCtrl-D"));
234 menuList->Append(LIST_SELECT_ALL, _T("S&elect All\tCtrl-A"));
235 menuList->AppendSeparator();
236 menuList->Append(LIST_SHOW_COL_INFO, _T("Show &column info\tCtrl-C"));
237 menuList->Append(LIST_SHOW_SEL_INFO, _T("Show &selected items\tCtrl-S"));
238 menuList->Append(LIST_SHOW_VIEW_RECT, _T("Show &view rect"));
239 menuList->AppendSeparator();
240 menuList->Append(LIST_SORT, _T("Sor&t\tCtrl-T"));
241 menuList->AppendSeparator();
242 menuList->Append(LIST_ADD, _T("&Append an item\tCtrl-P"));
243 menuList->Append(LIST_EDIT, _T("&Edit the item\tCtrl-E"));
244 menuList->Append(LIST_DELETE, _T("&Delete first item\tCtrl-X"));
245 menuList->Append(LIST_DELETE_ALL, _T("Delete &all items"));
246 menuList->AppendSeparator();
247 menuList->Append(LIST_FREEZE, _T("Free&ze\tCtrl-Z"));
248 menuList->Append(LIST_THAW, _T("Tha&w\tCtrl-W"));
249 menuList->AppendSeparator();
250 menuList->AppendCheckItem(LIST_TOGGLE_LINES, _T("Toggle &lines\tCtrl-I"));
251 menuList->Append(LIST_TOGGLE_MULTI_SEL, _T("&Multiple selection\tCtrl-M"),
252 _T("Toggle multiple selection"), true);
253
254 wxMenu *menuCol = new wxMenu;
255 menuCol->Append(LIST_SET_FG_COL, _T("&Foreground colour..."));
256 menuCol->Append(LIST_SET_BG_COL, _T("&Background colour..."));
257
258 wxMenuBar *menubar = new wxMenuBar;
259 menubar->Append(menuFile, _T("&File"));
260 menubar->Append(menuView, _T("&View"));
261 menubar->Append(menuList, _T("&List"));
262 menubar->Append(menuCol, _T("&Colour"));
263 SetMenuBar(menubar);
264
265 m_panel = new wxPanel(this, wxID_ANY);
266 m_logWindow = new wxTextCtrl(m_panel, wxID_ANY, wxEmptyString,
267 wxDefaultPosition, wxDefaultSize,
268 wxTE_READONLY | wxTE_MULTILINE | wxSUNKEN_BORDER);
269
270 m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow));
271
272 RecreateList(wxLC_REPORT | wxLC_SINGLE_SEL);
273
274 #if wxUSE_STATUSBAR
275 CreateStatusBar();
276 #endif // wxUSE_STATUSBAR
277 }
278
279 MyFrame::~MyFrame()
280 {
281 delete wxLog::SetActiveTarget(m_logOld);
282
283 delete m_imageListNormal;
284 delete m_imageListSmall;
285 }
286
287 void MyFrame::OnSize(wxSizeEvent& event)
288 {
289 DoSize();
290
291 event.Skip();
292 }
293
294 void MyFrame::DoSize()
295 {
296 if ( !m_logWindow )
297 return;
298
299 wxSize size = GetClientSize();
300 wxCoord y = (2*size.y)/3;
301 m_listCtrl->SetSize(0, 0, size.x, y);
302 m_logWindow->SetSize(0, y + 1, size.x, size.y - y);
303 }
304
305 bool MyFrame::CheckNonVirtual() const
306 {
307 if ( !m_listCtrl->HasFlag(wxLC_VIRTUAL) )
308 return true;
309
310 // "this" == whatever
311 wxLogWarning(_T("Can't do this in virtual view, sorry."));
312
313 return false;
314 }
315
316 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
317 {
318 Close(true);
319 }
320
321 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
322 {
323 wxMessageDialog dialog(this, _T("List test sample\nJulian Smart (c) 1997"),
324 _T("About list test"), wxOK|wxCANCEL);
325
326 dialog.ShowModal();
327 }
328
329 void MyFrame::OnFreeze(wxCommandEvent& WXUNUSED(event))
330 {
331 wxLogMessage(_T("Freezing the control"));
332
333 m_listCtrl->Freeze();
334 }
335
336 void MyFrame::OnThaw(wxCommandEvent& WXUNUSED(event))
337 {
338 wxLogMessage(_T("Thawing the control"));
339
340 m_listCtrl->Thaw();
341 }
342
343 void MyFrame::OnToggleLines(wxCommandEvent& event)
344 {
345 m_listCtrl->SetSingleStyle(wxLC_HRULES | wxLC_VRULES, event.IsChecked());
346 }
347
348 void MyFrame::OnToggleMacUseGeneric(wxCommandEvent& event)
349 {
350 wxSystemOptions::SetOption(wxT("mac.listctrl.always_use_generic"), event.IsChecked());
351 }
352
353 void MyFrame::OnGoTo(wxCommandEvent& WXUNUSED(event))
354 {
355 long index = 3;
356 m_listCtrl->SetItemState(index, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
357
358 long sel = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
359 wxLIST_STATE_SELECTED);
360 if ( sel != -1 )
361 m_listCtrl->SetItemState(sel, 0, wxLIST_STATE_SELECTED);
362 m_listCtrl->SetItemState(index, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
363 }
364
365 void MyFrame::OnFocusLast(wxCommandEvent& WXUNUSED(event))
366 {
367 long index = m_listCtrl->GetItemCount() - 1;
368 if ( index == -1 )
369 {
370 return;
371 }
372
373 m_listCtrl->SetItemState(index, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
374 m_listCtrl->EnsureVisible(index);
375 }
376
377 void MyFrame::OnToggleFirstSel(wxCommandEvent& WXUNUSED(event))
378 {
379 m_listCtrl->SetItemState(0, (~m_listCtrl->GetItemState(0, wxLIST_STATE_SELECTED) ) & wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
380 }
381
382 void MyFrame::OnDeselectAll(wxCommandEvent& WXUNUSED(event))
383 {
384 if ( !CheckNonVirtual() )
385 return;
386
387 int n = m_listCtrl->GetItemCount();
388 for (int i = 0; i < n; i++)
389 m_listCtrl->SetItemState(i,0,wxLIST_STATE_SELECTED);
390 }
391
392 void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event))
393 {
394 if ( !CheckNonVirtual() )
395 return;
396
397 int n = m_listCtrl->GetItemCount();
398 for (int i = 0; i < n; i++)
399 m_listCtrl->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
400 }
401
402 // ----------------------------------------------------------------------------
403 // changing listctrl modes
404 // ----------------------------------------------------------------------------
405
406 void MyFrame::RecreateList(long flags, bool withText)
407 {
408 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
409 // style, but it is more trouble to do it than not
410 #if 0
411 if ( !m_listCtrl || ((flags & wxLC_VIRTUAL) !=
412 (m_listCtrl->GetWindowStyleFlag() & wxLC_VIRTUAL)) )
413 #endif
414 {
415 delete m_listCtrl;
416
417 m_listCtrl = new MyListCtrl(m_panel, LIST_CTRL,
418 wxDefaultPosition, wxDefaultSize,
419 flags |
420 wxSUNKEN_BORDER | wxLC_EDIT_LABELS);
421
422 switch ( flags & wxLC_MASK_TYPE )
423 {
424 case wxLC_LIST:
425 InitWithListItems();
426 break;
427
428 case wxLC_ICON:
429 InitWithIconItems(withText);
430 break;
431
432 case wxLC_SMALL_ICON:
433 InitWithIconItems(withText, true);
434 break;
435
436 case wxLC_REPORT:
437 if ( flags & wxLC_VIRTUAL )
438 InitWithVirtualItems();
439 else
440 InitWithReportItems();
441 break;
442
443 default:
444 wxFAIL_MSG( _T("unknown listctrl mode") );
445 }
446 }
447
448 DoSize();
449
450 m_logWindow->Clear();
451 }
452
453 void MyFrame::OnListView(wxCommandEvent& WXUNUSED(event))
454 {
455 RecreateList(wxLC_LIST);
456 }
457
458 void MyFrame::InitWithListItems()
459 {
460 for ( int i = 0; i < NUM_ITEMS; i++ )
461 {
462 m_listCtrl->InsertItem(i, wxString::Format(_T("Item %d"), i));
463 }
464 }
465
466 void MyFrame::OnReportView(wxCommandEvent& WXUNUSED(event))
467 {
468 RecreateList(wxLC_REPORT);
469 }
470
471 void MyFrame::InitWithReportItems()
472 {
473 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
474
475 // note that under MSW for SetColumnWidth() to work we need to create the
476 // items with images initially even if we specify dummy image id
477 wxListItem itemCol;
478 itemCol.SetText(_T("Column 1"));
479 itemCol.SetImage(-1);
480 m_listCtrl->InsertColumn(0, itemCol);
481
482 itemCol.SetText(_T("Column 2"));
483 itemCol.SetAlign(wxLIST_FORMAT_CENTRE);
484 m_listCtrl->InsertColumn(1, itemCol);
485
486 itemCol.SetText(_T("Column 3"));
487 itemCol.SetAlign(wxLIST_FORMAT_RIGHT);
488 m_listCtrl->InsertColumn(2, itemCol);
489
490 // to speed up inserting we hide the control temporarily
491 m_listCtrl->Hide();
492
493 wxStopWatch sw;
494
495 for ( int i = 0; i < NUM_ITEMS; i++ )
496 {
497 m_listCtrl->InsertItemInReportView(i);
498 }
499
500 m_logWindow->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
501 NUM_ITEMS, sw.Time()));
502 m_listCtrl->Show();
503
504 // we leave all mask fields to 0 and only change the colour
505 wxListItem item;
506 item.m_itemId = 0;
507 item.SetTextColour(*wxRED);
508 m_listCtrl->SetItem( item );
509
510 item.m_itemId = 2;
511 item.SetTextColour(*wxGREEN);
512 m_listCtrl->SetItem( item );
513 item.m_itemId = 4;
514 item.SetTextColour(*wxLIGHT_GREY);
515 item.SetFont(*wxITALIC_FONT);
516 item.SetBackgroundColour(*wxRED);
517 m_listCtrl->SetItem( item );
518
519 m_listCtrl->SetTextColour(*wxBLUE);
520
521 m_listCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE );
522 m_listCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE );
523 m_listCtrl->SetColumnWidth( 2, wxLIST_AUTOSIZE );
524
525 // Set images in columns
526 m_listCtrl->SetItemColumnImage(1, 1, 0);
527
528 wxListItem info;
529 info.SetImage(0);
530 info.SetId(3);
531 info.SetColumn(2);
532 m_listCtrl->SetItem(info);
533
534 // test SetItemFont too
535 m_listCtrl->SetItemFont(0, *wxITALIC_FONT);
536 }
537
538 void MyFrame::InitWithIconItems(bool withText, bool sameIcon)
539 {
540 m_listCtrl->SetImageList(m_imageListNormal, wxIMAGE_LIST_NORMAL);
541 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
542
543 for ( int i = 0; i < NUM_ICONS; i++ )
544 {
545 int image = sameIcon ? 0 : i;
546
547 if ( withText )
548 {
549 m_listCtrl->InsertItem(i, wxString::Format(_T("Label %d"), i),
550 image);
551 }
552 else
553 {
554 m_listCtrl->InsertItem(i, image);
555 }
556 }
557 }
558
559 void MyFrame::OnIconView(wxCommandEvent& WXUNUSED(event))
560 {
561 RecreateList(wxLC_ICON, false);
562 }
563
564 void MyFrame::OnIconTextView(wxCommandEvent& WXUNUSED(event))
565 {
566 RecreateList(wxLC_ICON);
567 }
568
569 void MyFrame::OnSmallIconView(wxCommandEvent& WXUNUSED(event))
570 {
571 RecreateList(wxLC_SMALL_ICON, false);
572 }
573
574 void MyFrame::OnSmallIconTextView(wxCommandEvent& WXUNUSED(event))
575 {
576 RecreateList(wxLC_SMALL_ICON);
577 }
578
579 void MyFrame::OnVirtualView(wxCommandEvent& WXUNUSED(event))
580 {
581 m_smallVirtual = false;
582 RecreateList(wxLC_REPORT | wxLC_VIRTUAL);
583 }
584
585 void MyFrame::OnSmallVirtualView(wxCommandEvent& WXUNUSED(event))
586 {
587 m_smallVirtual = true;
588 RecreateList(wxLC_REPORT | wxLC_VIRTUAL);
589 }
590
591 void MyFrame::InitWithVirtualItems()
592 {
593 m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
594
595 if ( m_smallVirtual )
596 {
597 m_listCtrl->InsertColumn(0, _T("Animal"));
598 m_listCtrl->InsertColumn(1, _T("Sound"));
599 m_listCtrl->SetItemCount(WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS));
600 }
601 else
602 {
603 m_listCtrl->InsertColumn(0, _T("First Column"));
604 m_listCtrl->InsertColumn(1, _T("Second Column"));
605 m_listCtrl->SetColumnWidth(0, 150);
606 m_listCtrl->SetColumnWidth(1, 150);
607 m_listCtrl->SetItemCount(1000000);
608 }
609 }
610
611 void MyFrame::OnSort(wxCommandEvent& WXUNUSED(event))
612 {
613 wxStopWatch sw;
614
615 m_listCtrl->SortItems(MyCompareFunction, 0);
616
617 m_logWindow->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
618 m_listCtrl->GetItemCount(),
619 sw.Time()));
620 }
621
622 void MyFrame::OnShowSelInfo(wxCommandEvent& WXUNUSED(event))
623 {
624 int selCount = m_listCtrl->GetSelectedItemCount();
625 wxLogMessage(_T("%d items selected:"), selCount);
626
627 // don't show too many items
628 size_t shownCount = 0;
629
630 long item = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
631 wxLIST_STATE_SELECTED);
632 while ( item != -1 )
633 {
634 wxLogMessage(_T("\t%ld (%s)"),
635 item, m_listCtrl->GetItemText(item).c_str());
636
637 if ( ++shownCount > 10 )
638 {
639 wxLogMessage(_T("\t... more selected items snipped..."));
640 break;
641 }
642
643 item = m_listCtrl->GetNextItem(item, wxLIST_NEXT_ALL,
644 wxLIST_STATE_SELECTED);
645 }
646 }
647
648 void MyFrame::OnShowViewRect(wxCommandEvent& WXUNUSED(event))
649 {
650 const wxRect r = m_listCtrl->GetViewRect();
651 wxLogMessage("View rect: (%d, %d)-(%d, %d)",
652 r.GetLeft(), r.GetTop(), r.GetRight(), r.GetBottom());
653 }
654
655 void MyFrame::OnShowColInfo(wxCommandEvent& WXUNUSED(event))
656 {
657 int count = m_listCtrl->GetColumnCount();
658 wxLogMessage(wxT("%d columns:"), count);
659 for ( int c = 0; c < count; c++ )
660 {
661 wxLogMessage(wxT("\tcolumn %d has width %d"), c,
662 m_listCtrl->GetColumnWidth(c));
663 }
664 }
665
666 void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent& event)
667 {
668 event.Enable( (m_listCtrl->GetWindowStyleFlag() & wxLC_REPORT) != 0 );
669 }
670
671 void MyFrame::OnToggleMultiSel(wxCommandEvent& WXUNUSED(event))
672 {
673 long flags = m_listCtrl->GetWindowStyleFlag();
674 if ( flags & wxLC_SINGLE_SEL )
675 flags &= ~wxLC_SINGLE_SEL;
676 else
677 flags |= wxLC_SINGLE_SEL;
678
679 m_logWindow->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
680 (flags & wxLC_SINGLE_SEL) ? _T("sing") : _T("multip")));
681
682 RecreateList(flags);
683 }
684
685 void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent& event)
686 {
687 event.Check((m_listCtrl->GetWindowStyleFlag() & wxLC_SINGLE_SEL) == 0);
688 }
689
690 void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event))
691 {
692 m_listCtrl->SetForegroundColour(wxGetColourFromUser(this));
693 m_listCtrl->Refresh();
694 }
695
696 void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event))
697 {
698 m_listCtrl->SetBackgroundColour(wxGetColourFromUser(this));
699 m_listCtrl->Refresh();
700 }
701
702 void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event))
703 {
704 m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), _T("Appended item"));
705 }
706
707 void MyFrame::OnEdit(wxCommandEvent& WXUNUSED(event))
708 {
709 long itemCur = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL,
710 wxLIST_STATE_FOCUSED);
711
712 if ( itemCur != -1 )
713 {
714 m_listCtrl->EditLabel(itemCur);
715 }
716 else
717 {
718 m_logWindow->WriteText(_T("No item to edit"));
719 }
720 }
721
722 void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event))
723 {
724 if ( m_listCtrl->GetItemCount() )
725 {
726 m_listCtrl->DeleteItem(0);
727 }
728 else
729 {
730 m_logWindow->WriteText(_T("Nothing to delete"));
731 }
732 }
733
734 void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
735 {
736 wxStopWatch sw;
737
738 int itemCount = m_listCtrl->GetItemCount();
739
740 m_listCtrl->DeleteAllItems();
741
742 m_logWindow->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
743 itemCount,
744 sw.Time()));
745 }
746
747 // MyListCtrl
748
749 void MyListCtrl::OnCacheHint(wxListEvent& event)
750 {
751 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
752 event.GetCacheFrom(), event.GetCacheTo() );
753 }
754
755 void MyListCtrl::SetColumnImage(int col, int image)
756 {
757 wxListItem item;
758 item.SetMask(wxLIST_MASK_IMAGE);
759 item.SetImage(image);
760 SetColumn(col, item);
761 }
762
763 void MyListCtrl::OnColClick(wxListEvent& event)
764 {
765 int col = event.GetColumn();
766
767 // set or unset image
768 static bool x = false;
769 x = !x;
770 SetColumnImage(col, x ? 0 : -1);
771
772 wxLogMessage( wxT("OnColumnClick at %d."), col );
773 }
774
775 void MyListCtrl::OnColRightClick(wxListEvent& event)
776 {
777 int col = event.GetColumn();
778 if ( col != -1 )
779 {
780 SetColumnImage(col, -1);
781 }
782
783 // Show popupmenu at position
784 wxMenu menu(wxT("Test"));
785 menu.Append(LIST_ABOUT, _T("&About"));
786 PopupMenu(&menu, event.GetPoint());
787
788 wxLogMessage( wxT("OnColumnRightClick at %d."), event.GetColumn() );
789 }
790
791 void MyListCtrl::LogColEvent(const wxListEvent& event, const wxChar *name)
792 {
793 const int col = event.GetColumn();
794
795 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
796 name,
797 col,
798 event.GetItem().GetWidth(),
799 GetColumnWidth(col));
800 }
801
802 void MyListCtrl::OnColBeginDrag(wxListEvent& event)
803 {
804 LogColEvent( event, wxT("OnColBeginDrag") );
805
806 if ( event.GetColumn() == 0 )
807 {
808 wxLogMessage(_T("Resizing this column shouldn't work."));
809
810 event.Veto();
811 }
812 }
813
814 void MyListCtrl::OnColDragging(wxListEvent& event)
815 {
816 LogColEvent( event, wxT("OnColDragging") );
817 }
818
819 void MyListCtrl::OnColEndDrag(wxListEvent& event)
820 {
821 LogColEvent( event, wxT("OnColEndDrag") );
822 }
823
824 void MyListCtrl::OnBeginDrag(wxListEvent& event)
825 {
826 const wxPoint& pt = event.m_pointDrag;
827
828 int flags;
829 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
830 pt.x, pt.y, HitTest(pt, flags) );
831 }
832
833 void MyListCtrl::OnBeginRDrag(wxListEvent& event)
834 {
835 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
836 event.m_pointDrag.x, event.m_pointDrag.y );
837 }
838
839 void MyListCtrl::OnBeginLabelEdit(wxListEvent& event)
840 {
841 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event.m_item.m_text.c_str());
842
843 wxTextCtrl * const text = GetEditControl();
844 if ( !text )
845 {
846 wxLogMessage("BUG: started to edit but no edit control");
847 }
848 else
849 {
850 wxLogMessage("Edit control value: \"%s\"", text->GetValue());
851 }
852 }
853
854 void MyListCtrl::OnEndLabelEdit(wxListEvent& event)
855 {
856 wxLogMessage( wxT("OnEndLabelEdit: %s"),
857 (
858 event.IsEditCancelled() ?
859 wxString("[cancelled]") :
860 event.m_item.m_text
861 ).c_str()
862 );
863 }
864
865 void MyListCtrl::OnDeleteItem(wxListEvent& event)
866 {
867 LogEvent(event, _T("OnDeleteItem"));
868 wxLogMessage( wxT("Number of items when delete event is sent: %d"), GetItemCount() );
869 }
870
871 void MyListCtrl::OnDeleteAllItems(wxListEvent& event)
872 {
873 LogEvent(event, _T("OnDeleteAllItems"));
874 }
875
876 void MyListCtrl::OnSelected(wxListEvent& event)
877 {
878 LogEvent(event, _T("OnSelected"));
879
880 if ( GetWindowStyle() & wxLC_REPORT )
881 {
882 wxListItem info;
883 info.m_itemId = event.m_itemIndex;
884 info.m_col = 1;
885 info.m_mask = wxLIST_MASK_TEXT;
886 if ( GetItem(info) )
887 {
888 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
889 info.m_text.c_str());
890 }
891 else
892 {
893 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
894 }
895 }
896 }
897
898 void MyListCtrl::OnDeselected(wxListEvent& event)
899 {
900 LogEvent(event, _T("OnDeselected"));
901 }
902
903 void MyListCtrl::OnActivated(wxListEvent& event)
904 {
905 LogEvent(event, _T("OnActivated"));
906 }
907
908 void MyListCtrl::OnFocused(wxListEvent& event)
909 {
910 LogEvent(event, _T("OnFocused"));
911
912 event.Skip();
913 }
914
915 void MyListCtrl::OnListKeyDown(wxListEvent& event)
916 {
917 long item;
918
919 switch ( event.GetKeyCode() )
920 {
921 case 'C': // colorize
922 {
923 wxListItem info;
924 info.m_itemId = event.GetIndex();
925 if ( info.m_itemId == -1 )
926 {
927 // no item
928 break;
929 }
930
931 GetItem(info);
932
933 wxListItemAttr *attr = info.GetAttributes();
934 if ( !attr || !attr->HasTextColour() )
935 {
936 info.SetTextColour(*wxCYAN);
937
938 SetItem(info);
939
940 RefreshItem(info.m_itemId);
941 }
942 }
943 break;
944
945 case 'N': // next
946 item = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
947 if ( item++ == GetItemCount() - 1 )
948 {
949 item = 0;
950 }
951
952 wxLogMessage(_T("Focusing item %ld"), item);
953
954 SetItemState(item, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
955 EnsureVisible(item);
956 break;
957
958 case 'R': // show bounding rectangle
959 {
960 item = event.GetIndex();
961 wxRect r;
962 if ( !GetItemRect(item, r) )
963 {
964 wxLogError(_T("Failed to retrieve rect of item %ld"), item);
965 break;
966 }
967
968 wxLogMessage(_T("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
969 item, r.x, r.y, r.x + r.width, r.y + r.height);
970 }
971 break;
972
973 case '1': // show sub item bounding rectangle
974 case '2':
975 case '3':
976 case '4': // this column is invalid but we want to test it too
977 if ( InReportView() )
978 {
979 int subItem = event.GetKeyCode() - '1';
980 item = event.GetIndex();
981 wxRect r;
982 if ( !GetSubItemRect(item, subItem, r) )
983 {
984 wxLogError(_T("Failed to retrieve rect of item %ld column %d"), item, subItem + 1);
985 break;
986 }
987
988 wxLogMessage(_T("Bounding rect of item %ld column %d is (%d, %d)-(%d, %d)"),
989 item, subItem + 1,
990 r.x, r.y, r.x + r.width, r.y + r.height);
991 }
992 break;
993
994 case 'U': // update
995 if ( !IsVirtual() )
996 break;
997
998 if ( m_updated != -1 )
999 RefreshItem(m_updated);
1000
1001 m_updated = event.GetIndex();
1002 if ( m_updated != -1 )
1003 {
1004 // we won't see changes to this item as it's selected, update
1005 // the next one (or the first one if we're on the last item)
1006 if ( ++m_updated == GetItemCount() )
1007 m_updated = 0;
1008
1009 wxLogMessage("Updating colour of the item %ld", m_updated);
1010 RefreshItem(m_updated);
1011 }
1012 break;
1013
1014 case 'D': // delete
1015 item = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
1016 while ( item != -1 )
1017 {
1018 DeleteItem(item);
1019
1020 wxLogMessage(_T("Item %ld deleted"), item);
1021
1022 // -1 because the indices were shifted by DeleteItem()
1023 item = GetNextItem(item - 1,
1024 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
1025 }
1026 break;
1027
1028 case 'I': // insert
1029 if ( GetWindowStyle() & wxLC_REPORT )
1030 {
1031 if ( GetWindowStyle() & wxLC_VIRTUAL )
1032 {
1033 SetItemCount(GetItemCount() + 1);
1034 }
1035 else // !virtual
1036 {
1037 InsertItemInReportView(event.GetIndex());
1038 }
1039 }
1040 //else: fall through
1041
1042 default:
1043 LogEvent(event, _T("OnListKeyDown"));
1044
1045 event.Skip();
1046 }
1047 }
1048
1049 void MyListCtrl::OnChar(wxKeyEvent& event)
1050 {
1051 wxLogMessage(_T("Got char event."));
1052
1053 switch ( event.GetKeyCode() )
1054 {
1055 case 'n':
1056 case 'N':
1057 case 'c':
1058 case 'C':
1059 case 'r':
1060 case 'R':
1061 case 'u':
1062 case 'U':
1063 case 'd':
1064 case 'D':
1065 case 'i':
1066 case 'I':
1067 // these are the keys we process ourselves
1068 break;
1069
1070 default:
1071 event.Skip();
1072 }
1073 }
1074
1075 void MyListCtrl::OnRightClick(wxMouseEvent& event)
1076 {
1077 if ( !event.ControlDown() )
1078 {
1079 event.Skip();
1080 return;
1081 }
1082
1083 int flags;
1084 long subitem;
1085 long item = HitTest(event.GetPosition(), flags, &subitem);
1086
1087 wxString where;
1088 switch ( flags )
1089 {
1090 case wxLIST_HITTEST_ABOVE: where = _T("above"); break;
1091 case wxLIST_HITTEST_BELOW: where = _T("below"); break;
1092 case wxLIST_HITTEST_NOWHERE: where = _T("nowhere near"); break;
1093 case wxLIST_HITTEST_ONITEMICON: where = _T("on icon of"); break;
1094 case wxLIST_HITTEST_ONITEMLABEL: where = _T("on label of"); break;
1095 case wxLIST_HITTEST_ONITEMRIGHT: where = _T("right on"); break;
1096 case wxLIST_HITTEST_TOLEFT: where = _T("to the left of"); break;
1097 case wxLIST_HITTEST_TORIGHT: where = _T("to the right of"); break;
1098 default: where = _T("not clear exactly where on"); break;
1099 }
1100
1101 wxLogMessage(_T("Right double click %s item %ld, subitem %ld"),
1102 where.c_str(), item, subitem);
1103 }
1104
1105 void MyListCtrl::LogEvent(const wxListEvent& event, const wxChar *eventName)
1106 {
1107 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
1108 event.GetIndex(), eventName,
1109 event.GetText().c_str(), event.GetData());
1110 }
1111
1112 wxString MyListCtrl::OnGetItemText(long item, long column) const
1113 {
1114 if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS) )
1115 {
1116 return SMALL_VIRTUAL_VIEW_ITEMS[item][column];
1117 }
1118 else // "big" virtual control
1119 {
1120 return wxString::Format(_T("Column %ld of item %ld"), column, item);
1121 }
1122 }
1123
1124 int MyListCtrl::OnGetItemColumnImage(long item, long column) const
1125 {
1126 if (!column)
1127 return 0;
1128
1129 if (!(item % 3) && column == 1)
1130 return 0;
1131
1132 return -1;
1133 }
1134
1135 wxListItemAttr *MyListCtrl::OnGetItemAttr(long item) const
1136 {
1137 // test to check that RefreshItem() works correctly: when m_updated is
1138 // set to some item and it is refreshed, we highlight the item
1139 if ( item == m_updated )
1140 {
1141 static wxListItemAttr s_attrHighlight(*wxRED, wxNullColour, wxNullFont);
1142 return &s_attrHighlight;
1143 }
1144
1145 return item % 2 ? NULL : (wxListItemAttr *)&m_attr;
1146 }
1147
1148 void MyListCtrl::InsertItemInReportView(int i)
1149 {
1150 wxString buf;
1151 buf.Printf(_T("This is item %d"), i);
1152 long tmp = InsertItem(i, buf, 0);
1153 SetItemData(tmp, i);
1154
1155 buf.Printf(_T("Col 1, item %d"), i);
1156 SetItem(tmp, 1, buf);
1157
1158 buf.Printf(_T("Item %d in column 2"), i);
1159 SetItem(tmp, 2, buf);
1160 }
1161
1162 #if USE_CONTEXT_MENU
1163 void MyListCtrl::OnContextMenu(wxContextMenuEvent& event)
1164 {
1165 if (GetEditControl() == NULL)
1166 {
1167 wxPoint point = event.GetPosition();
1168 // If from keyboard
1169 if ( (point.x == -1) && (point.y == -1) )
1170 {
1171 wxSize size = GetSize();
1172 point.x = size.x / 2;
1173 point.y = size.y / 2;
1174 }
1175 else
1176 {
1177 point = ScreenToClient(point);
1178 }
1179 ShowContextMenu(point);
1180 }
1181 else
1182 {
1183 // the user is editing:
1184 // allow the text control to display its context menu
1185 // if it has one (it has on Windows) rather than display our one
1186 event.Skip();
1187 }
1188 }
1189 #endif
1190
1191 void MyListCtrl::ShowContextMenu(const wxPoint& pos)
1192 {
1193 wxMenu menu;
1194
1195 menu.Append(wxID_ABOUT, _T("&About"));
1196 menu.AppendSeparator();
1197 menu.Append(wxID_EXIT, _T("E&xit"));
1198
1199 PopupMenu(&menu, pos.x, pos.y);
1200 }