1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxListCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #if !defined(__WXMSW__) && !defined(__WXPM__)
24 #include "mondrian.xpm"
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"
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"
49 const wxChar
*SMALL_VIRTUAL_VIEW_ITEMS
[][2] =
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") },
64 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
65 EVT_SIZE(MyFrame::OnSize
)
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
)
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_FREEZE
, MyFrame::OnFreeze
)
94 EVT_MENU(LIST_THAW
, MyFrame::OnThaw
)
95 EVT_MENU(LIST_TOGGLE_LINES
, MyFrame::OnToggleLines
)
96 EVT_MENU(LIST_MAC_USE_GENERIC
, MyFrame::OnToggleMacUseGeneric
)
98 EVT_UPDATE_UI(LIST_SHOW_COL_INFO
, MyFrame::OnUpdateShowColInfo
)
99 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnUpdateToggleMultiSel
)
102 BEGIN_EVENT_TABLE(MyListCtrl
, wxListCtrl
)
103 EVT_LIST_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnBeginDrag
)
104 EVT_LIST_BEGIN_RDRAG(LIST_CTRL
, MyListCtrl::OnBeginRDrag
)
105 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnBeginLabelEdit
)
106 EVT_LIST_END_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnEndLabelEdit
)
107 EVT_LIST_DELETE_ITEM(LIST_CTRL
, MyListCtrl::OnDeleteItem
)
108 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL
, MyListCtrl::OnDeleteAllItems
)
109 EVT_LIST_ITEM_SELECTED(LIST_CTRL
, MyListCtrl::OnSelected
)
110 EVT_LIST_ITEM_DESELECTED(LIST_CTRL
, MyListCtrl::OnDeselected
)
111 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl::OnListKeyDown
)
112 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl::OnActivated
)
113 EVT_LIST_ITEM_FOCUSED(LIST_CTRL
, MyListCtrl::OnFocused
)
115 EVT_LIST_COL_CLICK(LIST_CTRL
, MyListCtrl::OnColClick
)
116 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL
, MyListCtrl::OnColRightClick
)
117 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnColBeginDrag
)
118 EVT_LIST_COL_DRAGGING(LIST_CTRL
, MyListCtrl::OnColDragging
)
119 EVT_LIST_COL_END_DRAG(LIST_CTRL
, MyListCtrl::OnColEndDrag
)
121 EVT_LIST_CACHE_HINT(LIST_CTRL
, MyListCtrl::OnCacheHint
)
124 EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu
)
126 EVT_CHAR(MyListCtrl::OnChar
)
128 EVT_RIGHT_DOWN(MyListCtrl::OnRightClick
)
133 // number of items in list/report view
134 static const int NUM_ITEMS
= 10;
136 // number of items in icon/small icon view
137 static const int NUM_ICONS
= 9;
139 int wxCALLBACK
MyCompareFunction(long item1
, long item2
, long WXUNUSED(sortData
))
150 // `Main program' equivalent, creating windows and returning main app frame
153 if ( !wxApp::OnInit() )
156 // Create the main frame window
157 MyFrame
*frame
= new MyFrame(wxT("wxListCtrl Test"));
167 // My frame constructor
168 MyFrame::MyFrame(const wxChar
*title
)
169 : wxFrame(NULL
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(600, 500))
173 m_smallVirtual
= false;
176 SetIcon( wxICON(mondrian
) );
178 // Make an image list containing large icons
179 m_imageListNormal
= new wxImageList(32, 32, true);
180 m_imageListSmall
= new wxImageList(16, 16, true);
183 m_imageListNormal
->Add( wxIcon(_T("icon1"), wxBITMAP_TYPE_ICO_RESOURCE
) );
184 m_imageListNormal
->Add( wxIcon(_T("icon2"), wxBITMAP_TYPE_ICO_RESOURCE
) );
185 m_imageListNormal
->Add( wxIcon(_T("icon3"), wxBITMAP_TYPE_ICO_RESOURCE
) );
186 m_imageListNormal
->Add( wxIcon(_T("icon4"), wxBITMAP_TYPE_ICO_RESOURCE
) );
187 m_imageListNormal
->Add( wxIcon(_T("icon5"), wxBITMAP_TYPE_ICO_RESOURCE
) );
188 m_imageListNormal
->Add( wxIcon(_T("icon6"), wxBITMAP_TYPE_ICO_RESOURCE
) );
189 m_imageListNormal
->Add( wxIcon(_T("icon7"), wxBITMAP_TYPE_ICO_RESOURCE
) );
190 m_imageListNormal
->Add( wxIcon(_T("icon8"), wxBITMAP_TYPE_ICO_RESOURCE
) );
191 m_imageListNormal
->Add( wxIcon(_T("icon9"), wxBITMAP_TYPE_ICO_RESOURCE
) );
193 m_imageListSmall
->Add( wxIcon(_T("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE
) );
196 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
197 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
198 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
199 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
200 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
201 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
202 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
203 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
204 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
206 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
210 wxMenu
*menuFile
= new wxMenu
;
211 menuFile
->Append(LIST_ABOUT
, _T("&About"));
212 menuFile
->AppendSeparator();
213 menuFile
->Append(LIST_QUIT
, _T("E&xit\tAlt-X"));
215 wxMenu
*menuView
= new wxMenu
;
216 menuView
->Append(LIST_LIST_VIEW
, _T("&List view\tF1"));
217 menuView
->Append(LIST_REPORT_VIEW
, _T("&Report view\tF2"));
218 menuView
->Append(LIST_ICON_VIEW
, _T("&Icon view\tF3"));
219 menuView
->Append(LIST_ICON_TEXT_VIEW
, _T("Icon view with &text\tF4"));
220 menuView
->Append(LIST_SMALL_ICON_VIEW
, _T("&Small icon view\tF5"));
221 menuView
->Append(LIST_SMALL_ICON_TEXT_VIEW
, _T("Small icon &view with text\tF6"));
222 menuView
->Append(LIST_VIRTUAL_VIEW
, _T("&Virtual view\tF7"));
223 menuView
->Append(LIST_SMALL_VIRTUAL_VIEW
, _T("Small virtual vie&w\tF8"));
225 menuView
->AppendCheckItem(LIST_MAC_USE_GENERIC
, _T("Mac: Use Generic Control"));
228 wxMenu
*menuList
= new wxMenu
;
229 menuList
->Append(LIST_GOTO
, _T("&Go to item #3\tCtrl-3"));
230 menuList
->Append(LIST_FOCUS_LAST
, _T("&Make last item current\tCtrl-L"));
231 menuList
->Append(LIST_TOGGLE_FIRST
, _T("To&ggle first item\tCtrl-G"));
232 menuList
->Append(LIST_DESELECT_ALL
, _T("&Deselect All\tCtrl-D"));
233 menuList
->Append(LIST_SELECT_ALL
, _T("S&elect All\tCtrl-A"));
234 menuList
->AppendSeparator();
235 menuList
->Append(LIST_SHOW_COL_INFO
, _T("Show &column info\tCtrl-C"));
236 menuList
->Append(LIST_SHOW_SEL_INFO
, _T("Show &selected items\tCtrl-S"));
237 menuList
->AppendSeparator();
238 menuList
->Append(LIST_SORT
, _T("&Sort\tCtrl-S"));
239 menuList
->AppendSeparator();
240 menuList
->Append(LIST_ADD
, _T("&Append an item\tCtrl-P"));
241 menuList
->Append(LIST_EDIT
, _T("&Edit the item\tCtrl-E"));
242 menuList
->Append(LIST_DELETE
, _T("&Delete first item\tCtrl-X"));
243 menuList
->Append(LIST_DELETE_ALL
, _T("Delete &all items"));
244 menuList
->AppendSeparator();
245 menuList
->Append(LIST_FREEZE
, _T("Free&ze\tCtrl-Z"));
246 menuList
->Append(LIST_THAW
, _T("Tha&w\tCtrl-W"));
247 menuList
->AppendSeparator();
248 menuList
->AppendCheckItem(LIST_TOGGLE_LINES
, _T("Toggle &lines\tCtrl-I"));
249 menuList
->Append(LIST_TOGGLE_MULTI_SEL
, _T("&Multiple selection\tCtrl-M"),
250 _T("Toggle multiple selection"), true);
252 wxMenu
*menuCol
= new wxMenu
;
253 menuCol
->Append(LIST_SET_FG_COL
, _T("&Foreground colour..."));
254 menuCol
->Append(LIST_SET_BG_COL
, _T("&Background colour..."));
256 wxMenuBar
*menubar
= new wxMenuBar
;
257 menubar
->Append(menuFile
, _T("&File"));
258 menubar
->Append(menuView
, _T("&View"));
259 menubar
->Append(menuList
, _T("&List"));
260 menubar
->Append(menuCol
, _T("&Colour"));
263 m_panel
= new wxPanel(this, wxID_ANY
);
264 m_logWindow
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
,
265 wxDefaultPosition
, wxDefaultSize
,
266 wxTE_READONLY
| wxTE_MULTILINE
| wxSUNKEN_BORDER
);
268 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
270 RecreateList(wxLC_REPORT
| wxLC_SINGLE_SEL
);
274 #endif // wxUSE_STATUSBAR
279 delete wxLog::SetActiveTarget(m_logOld
);
281 delete m_imageListNormal
;
282 delete m_imageListSmall
;
285 void MyFrame::OnSize(wxSizeEvent
& event
)
292 void MyFrame::DoSize()
297 wxSize size
= GetClientSize();
298 wxCoord y
= (2*size
.y
)/3;
299 m_listCtrl
->SetSize(0, 0, size
.x
, y
);
300 m_logWindow
->SetSize(0, y
+ 1, size
.x
, size
.y
- y
);
303 bool MyFrame::CheckNonVirtual() const
305 if ( !m_listCtrl
->HasFlag(wxLC_VIRTUAL
) )
308 // "this" == whatever
309 wxLogWarning(_T("Can't do this in virtual view, sorry."));
314 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
319 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
321 wxMessageDialog
dialog(this, _T("List test sample\nJulian Smart (c) 1997"),
322 _T("About list test"), wxOK
|wxCANCEL
);
327 void MyFrame::OnFreeze(wxCommandEvent
& WXUNUSED(event
))
329 wxLogMessage(_T("Freezing the control"));
331 m_listCtrl
->Freeze();
334 void MyFrame::OnThaw(wxCommandEvent
& WXUNUSED(event
))
336 wxLogMessage(_T("Thawing the control"));
341 void MyFrame::OnToggleLines(wxCommandEvent
& event
)
343 m_listCtrl
->SetSingleStyle(wxLC_HRULES
| wxLC_VRULES
, event
.IsChecked());
346 void MyFrame::OnToggleMacUseGeneric(wxCommandEvent
& event
)
348 wxSystemOptions::SetOption(wxT("mac.listctrl.always_use_generic"), event
.IsChecked());
351 void MyFrame::OnGoTo(wxCommandEvent
& WXUNUSED(event
))
354 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
356 long sel
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
357 wxLIST_STATE_SELECTED
);
359 m_listCtrl
->SetItemState(sel
, 0, wxLIST_STATE_SELECTED
);
360 m_listCtrl
->SetItemState(index
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
363 void MyFrame::OnFocusLast(wxCommandEvent
& WXUNUSED(event
))
365 long index
= m_listCtrl
->GetItemCount() - 1;
371 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
372 m_listCtrl
->EnsureVisible(index
);
375 void MyFrame::OnToggleFirstSel(wxCommandEvent
& WXUNUSED(event
))
377 m_listCtrl
->SetItemState(0, (~m_listCtrl
->GetItemState(0, wxLIST_STATE_SELECTED
) ) & wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
380 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
382 if ( !CheckNonVirtual() )
385 int n
= m_listCtrl
->GetItemCount();
386 for (int i
= 0; i
< n
; i
++)
387 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
390 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
392 if ( !CheckNonVirtual() )
395 int n
= m_listCtrl
->GetItemCount();
396 for (int i
= 0; i
< n
; i
++)
397 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
400 // ----------------------------------------------------------------------------
401 // changing listctrl modes
402 // ----------------------------------------------------------------------------
404 void MyFrame::RecreateList(long flags
, bool withText
)
406 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
407 // style, but it is more trouble to do it than not
409 if ( !m_listCtrl
|| ((flags
& wxLC_VIRTUAL
) !=
410 (m_listCtrl
->GetWindowStyleFlag() & wxLC_VIRTUAL
)) )
415 m_listCtrl
= new MyListCtrl(m_panel
, LIST_CTRL
,
416 wxDefaultPosition
, wxDefaultSize
,
418 wxSUNKEN_BORDER
| wxLC_EDIT_LABELS
);
420 switch ( flags
& wxLC_MASK_TYPE
)
427 InitWithIconItems(withText
);
430 case wxLC_SMALL_ICON
:
431 InitWithIconItems(withText
, true);
435 if ( flags
& wxLC_VIRTUAL
)
436 InitWithVirtualItems();
438 InitWithReportItems();
442 wxFAIL_MSG( _T("unknown listctrl mode") );
448 m_logWindow
->Clear();
451 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
453 RecreateList(wxLC_LIST
);
456 void MyFrame::InitWithListItems()
458 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
460 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Item %d"), i
));
464 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
466 RecreateList(wxLC_REPORT
);
469 void MyFrame::InitWithReportItems()
471 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
473 // note that under MSW for SetColumnWidth() to work we need to create the
474 // items with images initially even if we specify dummy image id
476 itemCol
.SetText(_T("Column 1"));
477 itemCol
.SetImage(-1);
478 m_listCtrl
->InsertColumn(0, itemCol
);
480 itemCol
.SetText(_T("Column 2"));
481 itemCol
.SetAlign(wxLIST_FORMAT_CENTRE
);
482 m_listCtrl
->InsertColumn(1, itemCol
);
484 itemCol
.SetText(_T("Column 3"));
485 itemCol
.SetAlign(wxLIST_FORMAT_RIGHT
);
486 m_listCtrl
->InsertColumn(2, itemCol
);
488 // to speed up inserting we hide the control temporarily
493 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
495 m_listCtrl
->InsertItemInReportView(i
);
498 m_logWindow
->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
499 NUM_ITEMS
, sw
.Time()));
502 // we leave all mask fields to 0 and only change the colour
505 item
.SetTextColour(*wxRED
);
506 m_listCtrl
->SetItem( item
);
509 item
.SetTextColour(*wxGREEN
);
510 m_listCtrl
->SetItem( item
);
512 item
.SetTextColour(*wxLIGHT_GREY
);
513 item
.SetFont(*wxITALIC_FONT
);
514 item
.SetBackgroundColour(*wxRED
);
515 m_listCtrl
->SetItem( item
);
517 m_listCtrl
->SetTextColour(*wxBLUE
);
519 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
520 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
521 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
523 // Set images in columns
524 m_listCtrl
->SetItemColumnImage(1, 1, 0);
530 m_listCtrl
->SetItem(info
);
532 // test SetItemFont too
533 m_listCtrl
->SetItemFont(0, *wxITALIC_FONT
);
536 void MyFrame::InitWithIconItems(bool withText
, bool sameIcon
)
538 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
539 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
541 for ( int i
= 0; i
< NUM_ICONS
; i
++ )
543 int image
= sameIcon
? 0 : i
;
547 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Label %d"), i
),
552 m_listCtrl
->InsertItem(i
, image
);
557 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
559 RecreateList(wxLC_ICON
, false);
562 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
564 RecreateList(wxLC_ICON
);
567 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
569 RecreateList(wxLC_SMALL_ICON
, false);
572 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
574 RecreateList(wxLC_SMALL_ICON
);
577 void MyFrame::OnVirtualView(wxCommandEvent
& WXUNUSED(event
))
579 m_smallVirtual
= false;
580 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
583 void MyFrame::OnSmallVirtualView(wxCommandEvent
& WXUNUSED(event
))
585 m_smallVirtual
= true;
586 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
589 void MyFrame::InitWithVirtualItems()
591 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
593 if ( m_smallVirtual
)
595 m_listCtrl
->InsertColumn(0, _T("Animal"));
596 m_listCtrl
->InsertColumn(1, _T("Sound"));
597 m_listCtrl
->SetItemCount(WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
));
601 m_listCtrl
->InsertColumn(0, _T("First Column"));
602 m_listCtrl
->InsertColumn(1, _T("Second Column"));
603 m_listCtrl
->SetColumnWidth(0, 150);
604 m_listCtrl
->SetColumnWidth(1, 150);
605 m_listCtrl
->SetItemCount(1000000);
609 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
613 m_listCtrl
->SortItems(MyCompareFunction
, 0);
615 m_logWindow
->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
616 m_listCtrl
->GetItemCount(),
620 void MyFrame::OnShowSelInfo(wxCommandEvent
& WXUNUSED(event
))
622 int selCount
= m_listCtrl
->GetSelectedItemCount();
623 wxLogMessage(_T("%d items selected:"), selCount
);
625 // don't show too many items
626 size_t shownCount
= 0;
628 long item
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
629 wxLIST_STATE_SELECTED
);
632 wxLogMessage(_T("\t%ld (%s)"),
633 item
, m_listCtrl
->GetItemText(item
).c_str());
635 if ( ++shownCount
> 10 )
637 wxLogMessage(_T("\t... more selected items snipped..."));
641 item
= m_listCtrl
->GetNextItem(item
, wxLIST_NEXT_ALL
,
642 wxLIST_STATE_SELECTED
);
646 void MyFrame::OnShowColInfo(wxCommandEvent
& WXUNUSED(event
))
648 int count
= m_listCtrl
->GetColumnCount();
649 wxLogMessage(wxT("%d columns:"), count
);
650 for ( int c
= 0; c
< count
; c
++ )
652 wxLogMessage(wxT("\tcolumn %d has width %d"), c
,
653 m_listCtrl
->GetColumnWidth(c
));
657 void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent
& event
)
659 event
.Enable( (m_listCtrl
->GetWindowStyleFlag() & wxLC_REPORT
) != 0 );
662 void MyFrame::OnToggleMultiSel(wxCommandEvent
& WXUNUSED(event
))
664 long flags
= m_listCtrl
->GetWindowStyleFlag();
665 if ( flags
& wxLC_SINGLE_SEL
)
666 flags
&= ~wxLC_SINGLE_SEL
;
668 flags
|= wxLC_SINGLE_SEL
;
670 m_logWindow
->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
671 (flags
& wxLC_SINGLE_SEL
) ? _T("sing") : _T("multip")));
676 void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent
& event
)
678 event
.Check((m_listCtrl
->GetWindowStyleFlag() & wxLC_SINGLE_SEL
) == 0);
681 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
683 m_listCtrl
->SetForegroundColour(wxGetColourFromUser(this));
684 m_listCtrl
->Refresh();
687 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
689 m_listCtrl
->SetBackgroundColour(wxGetColourFromUser(this));
690 m_listCtrl
->Refresh();
693 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
))
695 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), _T("Appended item"));
698 void MyFrame::OnEdit(wxCommandEvent
& WXUNUSED(event
))
700 long itemCur
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
701 wxLIST_STATE_FOCUSED
);
705 m_listCtrl
->EditLabel(itemCur
);
709 m_logWindow
->WriteText(_T("No item to edit"));
713 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
715 if ( m_listCtrl
->GetItemCount() )
717 m_listCtrl
->DeleteItem(0);
721 m_logWindow
->WriteText(_T("Nothing to delete"));
725 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
729 int itemCount
= m_listCtrl
->GetItemCount();
731 m_listCtrl
->DeleteAllItems();
733 m_logWindow
->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
740 void MyListCtrl::OnCacheHint(wxListEvent
& event
)
742 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
743 event
.GetCacheFrom(), event
.GetCacheTo() );
746 void MyListCtrl::SetColumnImage(int col
, int image
)
749 item
.SetMask(wxLIST_MASK_IMAGE
);
750 item
.SetImage(image
);
751 SetColumn(col
, item
);
754 void MyListCtrl::OnColClick(wxListEvent
& event
)
756 int col
= event
.GetColumn();
758 // set or unset image
759 static bool x
= false;
761 SetColumnImage(col
, x
? 0 : -1);
763 wxLogMessage( wxT("OnColumnClick at %d."), col
);
766 void MyListCtrl::OnColRightClick(wxListEvent
& event
)
768 int col
= event
.GetColumn();
771 SetColumnImage(col
, -1);
774 // Show popupmenu at position
775 wxMenu
menu(wxT("Test"));
776 menu
.Append(LIST_ABOUT
, _T("&About"));
777 PopupMenu(&menu
, event
.GetPoint());
779 wxLogMessage( wxT("OnColumnRightClick at %d."), event
.GetColumn() );
782 void MyListCtrl::LogColEvent(const wxListEvent
& event
, const wxChar
*name
)
784 const int col
= event
.GetColumn();
786 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
789 event
.GetItem().GetWidth(),
790 GetColumnWidth(col
));
793 void MyListCtrl::OnColBeginDrag(wxListEvent
& event
)
795 LogColEvent( event
, wxT("OnColBeginDrag") );
797 if ( event
.GetColumn() == 0 )
799 wxLogMessage(_T("Resizing this column shouldn't work."));
805 void MyListCtrl::OnColDragging(wxListEvent
& event
)
807 LogColEvent( event
, wxT("OnColDragging") );
810 void MyListCtrl::OnColEndDrag(wxListEvent
& event
)
812 LogColEvent( event
, wxT("OnColEndDrag") );
815 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
817 const wxPoint
& pt
= event
.m_pointDrag
;
820 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
821 pt
.x
, pt
.y
, HitTest(pt
, flags
) );
824 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
826 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
827 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
830 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
832 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event
.m_item
.m_text
.c_str());
834 wxTextCtrl
* const text
= GetEditControl();
837 wxLogMessage("BUG: started to edit but no edit control");
841 wxLogMessage("Edit control value: \"%s\"", text
->GetValue());
845 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
847 wxLogMessage( wxT("OnEndLabelEdit: %s"),
849 event
.IsEditCancelled() ?
850 wxString("[cancelled]") :
856 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
858 LogEvent(event
, _T("OnDeleteItem"));
859 wxLogMessage( wxT("Number of items when delete event is sent: %d"), GetItemCount() );
862 void MyListCtrl::OnDeleteAllItems(wxListEvent
& event
)
864 LogEvent(event
, _T("OnDeleteAllItems"));
867 void MyListCtrl::OnSelected(wxListEvent
& event
)
869 LogEvent(event
, _T("OnSelected"));
871 if ( GetWindowStyle() & wxLC_REPORT
)
874 info
.m_itemId
= event
.m_itemIndex
;
876 info
.m_mask
= wxLIST_MASK_TEXT
;
879 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
880 info
.m_text
.c_str());
884 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
889 void MyListCtrl::OnDeselected(wxListEvent
& event
)
891 LogEvent(event
, _T("OnDeselected"));
894 void MyListCtrl::OnActivated(wxListEvent
& event
)
896 LogEvent(event
, _T("OnActivated"));
899 void MyListCtrl::OnFocused(wxListEvent
& event
)
901 LogEvent(event
, _T("OnFocused"));
906 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
910 switch ( event
.GetKeyCode() )
912 case 'C': // colorize
915 info
.m_itemId
= event
.GetIndex();
916 if ( info
.m_itemId
== -1 )
924 wxListItemAttr
*attr
= info
.GetAttributes();
925 if ( !attr
|| !attr
->HasTextColour() )
927 info
.SetTextColour(*wxCYAN
);
931 RefreshItem(info
.m_itemId
);
937 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
938 if ( item
++ == GetItemCount() - 1 )
943 wxLogMessage(_T("Focusing item %ld"), item
);
945 SetItemState(item
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
949 case 'R': // show bounding rectangle
951 item
= event
.GetIndex();
953 if ( !GetItemRect(item
, r
) )
955 wxLogError(_T("Failed to retrieve rect of item %ld"), item
);
959 wxLogMessage(_T("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
960 item
, r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
968 if ( m_updated
!= -1 )
969 RefreshItem(m_updated
);
971 m_updated
= event
.GetIndex();
972 if ( m_updated
!= -1 )
974 // we won't see changes to this item as it's selected, update
975 // the next one (or the first one if we're on the last item)
976 if ( ++m_updated
== GetItemCount() )
979 wxLogMessage("Updating colour of the item %ld", m_updated
);
980 RefreshItem(m_updated
);
985 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
990 wxLogMessage(_T("Item %ld deleted"), item
);
992 // -1 because the indices were shifted by DeleteItem()
993 item
= GetNextItem(item
- 1,
994 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
999 if ( GetWindowStyle() & wxLC_REPORT
)
1001 if ( GetWindowStyle() & wxLC_VIRTUAL
)
1003 SetItemCount(GetItemCount() + 1);
1007 InsertItemInReportView(event
.GetIndex());
1010 //else: fall through
1013 LogEvent(event
, _T("OnListKeyDown"));
1019 void MyListCtrl::OnChar(wxKeyEvent
& event
)
1021 wxLogMessage(_T("Got char event."));
1023 switch ( event
.GetKeyCode() )
1035 // these are the keys we process ourselves
1043 void MyListCtrl::OnRightClick(wxMouseEvent
& event
)
1045 if ( !event
.ControlDown() )
1053 long item
= HitTest(event
.GetPosition(), flags
, &subitem
);
1058 case wxLIST_HITTEST_ABOVE
: where
= _T("above"); break;
1059 case wxLIST_HITTEST_BELOW
: where
= _T("below"); break;
1060 case wxLIST_HITTEST_NOWHERE
: where
= _T("nowhere near"); break;
1061 case wxLIST_HITTEST_ONITEMICON
: where
= _T("on icon of"); break;
1062 case wxLIST_HITTEST_ONITEMLABEL
: where
= _T("on label of"); break;
1063 case wxLIST_HITTEST_ONITEMRIGHT
: where
= _T("right on"); break;
1064 case wxLIST_HITTEST_TOLEFT
: where
= _T("to the left of"); break;
1065 case wxLIST_HITTEST_TORIGHT
: where
= _T("to the right of"); break;
1066 default: where
= _T("not clear exactly where on"); break;
1069 wxLogMessage(_T("Right double click %s item %ld, subitem %ld"),
1070 where
.c_str(), item
, subitem
);
1073 void MyListCtrl::LogEvent(const wxListEvent
& event
, const wxChar
*eventName
)
1075 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
1076 event
.GetIndex(), eventName
,
1077 event
.GetText().c_str(), event
.GetData());
1080 wxString
MyListCtrl::OnGetItemText(long item
, long column
) const
1082 if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
) )
1084 return SMALL_VIRTUAL_VIEW_ITEMS
[item
][column
];
1086 else // "big" virtual control
1088 return wxString::Format(_T("Column %ld of item %ld"), column
, item
);
1092 int MyListCtrl::OnGetItemColumnImage(long item
, long column
) const
1097 if (!(item
% 3) && column
== 1)
1103 wxListItemAttr
*MyListCtrl::OnGetItemAttr(long item
) const
1105 // test to check that RefreshItem() works correctly: when m_updated is
1106 // set to some item and it is refreshed, we highlight the item
1107 if ( item
== m_updated
)
1109 static wxListItemAttr
s_attrHighlight(*wxRED
, wxNullColour
, wxNullFont
);
1110 return &s_attrHighlight
;
1113 return item
% 2 ? NULL
: (wxListItemAttr
*)&m_attr
;
1116 void MyListCtrl::InsertItemInReportView(int i
)
1119 buf
.Printf(_T("This is item %d"), i
);
1120 long tmp
= InsertItem(i
, buf
, 0);
1121 SetItemData(tmp
, i
);
1123 buf
.Printf(_T("Col 1, item %d"), i
);
1124 SetItem(tmp
, 1, buf
);
1126 buf
.Printf(_T("Item %d in column 2"), i
);
1127 SetItem(tmp
, 2, buf
);
1130 #if USE_CONTEXT_MENU
1131 void MyListCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1133 wxPoint point
= event
.GetPosition();
1135 if (point
.x
== -1 && point
.y
== -1) {
1136 wxSize size
= GetSize();
1137 point
.x
= size
.x
/ 2;
1138 point
.y
= size
.y
/ 2;
1140 point
= ScreenToClient(point
);
1142 ShowContextMenu(point
);
1146 void MyListCtrl::ShowContextMenu(const wxPoint
& pos
)
1150 menu
.Append(wxID_ABOUT
, _T("&About"));
1151 menu
.AppendSeparator();
1152 menu
.Append(wxID_EXIT
, _T("E&xit"));
1154 PopupMenu(&menu
, pos
.x
, pos
.y
);