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_SHOW_VIEW_RECT
, MyFrame::OnShowViewRect
)
94 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
95 EVT_MENU(LIST_SET_COL_ORDER
, MyFrame::OnSetColOrder
)
96 EVT_MENU(LIST_GET_COL_ORDER
, MyFrame::OnGetColOrder
)
97 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
98 EVT_MENU(LIST_FREEZE
, MyFrame::OnFreeze
)
99 EVT_MENU(LIST_THAW
, MyFrame::OnThaw
)
100 EVT_MENU(LIST_TOGGLE_LINES
, MyFrame::OnToggleLines
)
101 EVT_MENU(LIST_MAC_USE_GENERIC
, MyFrame::OnToggleMacUseGeneric
)
103 EVT_UPDATE_UI(LIST_SHOW_COL_INFO
, MyFrame::OnUpdateShowColInfo
)
104 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnUpdateToggleMultiSel
)
107 BEGIN_EVENT_TABLE(MyListCtrl
, wxListCtrl
)
108 EVT_LIST_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnBeginDrag
)
109 EVT_LIST_BEGIN_RDRAG(LIST_CTRL
, MyListCtrl::OnBeginRDrag
)
110 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnBeginLabelEdit
)
111 EVT_LIST_END_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnEndLabelEdit
)
112 EVT_LIST_DELETE_ITEM(LIST_CTRL
, MyListCtrl::OnDeleteItem
)
113 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL
, MyListCtrl::OnDeleteAllItems
)
114 EVT_LIST_ITEM_SELECTED(LIST_CTRL
, MyListCtrl::OnSelected
)
115 EVT_LIST_ITEM_DESELECTED(LIST_CTRL
, MyListCtrl::OnDeselected
)
116 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl::OnListKeyDown
)
117 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl::OnActivated
)
118 EVT_LIST_ITEM_FOCUSED(LIST_CTRL
, MyListCtrl::OnFocused
)
120 EVT_LIST_COL_CLICK(LIST_CTRL
, MyListCtrl::OnColClick
)
121 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL
, MyListCtrl::OnColRightClick
)
122 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnColBeginDrag
)
123 EVT_LIST_COL_DRAGGING(LIST_CTRL
, MyListCtrl::OnColDragging
)
124 EVT_LIST_COL_END_DRAG(LIST_CTRL
, MyListCtrl::OnColEndDrag
)
126 EVT_LIST_CACHE_HINT(LIST_CTRL
, MyListCtrl::OnCacheHint
)
129 EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu
)
131 EVT_CHAR(MyListCtrl::OnChar
)
133 EVT_RIGHT_DOWN(MyListCtrl::OnRightClick
)
138 // number of items in list/report view
139 static const int NUM_ITEMS
= 10;
141 // number of items in icon/small icon view
142 static const int NUM_ICONS
= 9;
144 int wxCALLBACK
MyCompareFunction(long item1
, long item2
, long WXUNUSED(sortData
))
155 // `Main program' equivalent, creating windows and returning main app frame
158 if ( !wxApp::OnInit() )
161 // Create the main frame window
162 MyFrame
*frame
= new MyFrame(wxT("wxListCtrl Test"));
172 // My frame constructor
173 MyFrame::MyFrame(const wxChar
*title
)
174 : wxFrame(NULL
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(800, 500))
178 m_simpleListCtrl
= NULL
;
179 m_smallVirtual
= false;
182 SetIcon( wxICON(mondrian
) );
184 // Make an image list containing large icons
185 m_imageListNormal
= new wxImageList(32, 32, true);
186 m_imageListSmall
= new wxImageList(16, 16, true);
189 m_imageListNormal
->Add( wxIcon(_T("icon1"), wxBITMAP_TYPE_ICO_RESOURCE
) );
190 m_imageListNormal
->Add( wxIcon(_T("icon2"), wxBITMAP_TYPE_ICO_RESOURCE
) );
191 m_imageListNormal
->Add( wxIcon(_T("icon3"), wxBITMAP_TYPE_ICO_RESOURCE
) );
192 m_imageListNormal
->Add( wxIcon(_T("icon4"), wxBITMAP_TYPE_ICO_RESOURCE
) );
193 m_imageListNormal
->Add( wxIcon(_T("icon5"), wxBITMAP_TYPE_ICO_RESOURCE
) );
194 m_imageListNormal
->Add( wxIcon(_T("icon6"), wxBITMAP_TYPE_ICO_RESOURCE
) );
195 m_imageListNormal
->Add( wxIcon(_T("icon7"), wxBITMAP_TYPE_ICO_RESOURCE
) );
196 m_imageListNormal
->Add( wxIcon(_T("icon8"), wxBITMAP_TYPE_ICO_RESOURCE
) );
197 m_imageListNormal
->Add( wxIcon(_T("icon9"), wxBITMAP_TYPE_ICO_RESOURCE
) );
199 m_imageListSmall
->Add( wxIcon(_T("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE
) );
202 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
203 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
204 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
205 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
206 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
207 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
208 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
209 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
210 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
212 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
216 wxMenu
*menuFile
= new wxMenu
;
217 menuFile
->Append(LIST_ABOUT
, _T("&About"));
218 menuFile
->AppendSeparator();
219 menuFile
->Append(LIST_QUIT
, _T("E&xit\tAlt-X"));
221 wxMenu
*menuView
= new wxMenu
;
222 menuView
->Append(LIST_LIST_VIEW
, _T("&List view\tF1"));
223 menuView
->Append(LIST_REPORT_VIEW
, _T("&Report view\tF2"));
224 menuView
->Append(LIST_ICON_VIEW
, _T("&Icon view\tF3"));
225 menuView
->Append(LIST_ICON_TEXT_VIEW
, _T("Icon view with &text\tF4"));
226 menuView
->Append(LIST_SMALL_ICON_VIEW
, _T("&Small icon view\tF5"));
227 menuView
->Append(LIST_SMALL_ICON_TEXT_VIEW
, _T("Small icon &view with text\tF6"));
228 menuView
->Append(LIST_VIRTUAL_VIEW
, _T("&Virtual view\tF7"));
229 menuView
->Append(LIST_SMALL_VIRTUAL_VIEW
, _T("Small virtual vie&w\tF8"));
231 menuView
->AppendCheckItem(LIST_MAC_USE_GENERIC
, _T("Mac: Use Generic Control"));
234 wxMenu
*menuList
= new wxMenu
;
235 menuList
->Append(LIST_GOTO
, _T("&Go to item #3\tCtrl-3"));
236 menuList
->Append(LIST_FOCUS_LAST
, _T("&Make last item current\tCtrl-L"));
237 menuList
->Append(LIST_TOGGLE_FIRST
, _T("To&ggle first item\tCtrl-G"));
238 menuList
->Append(LIST_DESELECT_ALL
, _T("&Deselect All\tCtrl-D"));
239 menuList
->Append(LIST_SELECT_ALL
, _T("S&elect All\tCtrl-A"));
240 menuList
->AppendSeparator();
241 menuList
->Append(LIST_SHOW_COL_INFO
, _T("Show &column info\tCtrl-C"));
242 menuList
->Append(LIST_SHOW_SEL_INFO
, _T("Show &selected items\tCtrl-S"));
243 menuList
->Append(LIST_SHOW_VIEW_RECT
, _T("Show &view rect"));
244 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
245 menuList
->Append(LIST_SET_COL_ORDER
, _T("Se&t columns order\tShift-Ctrl-O"));
246 menuList
->Append(LIST_GET_COL_ORDER
, _T("Show&w columns order\tCtrl-O"));
247 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
248 menuList
->AppendSeparator();
249 menuList
->Append(LIST_SORT
, _T("Sor&t\tCtrl-T"));
250 menuList
->AppendSeparator();
251 menuList
->Append(LIST_ADD
, _T("&Append an item\tCtrl-P"));
252 menuList
->Append(LIST_EDIT
, _T("&Edit the item\tCtrl-E"));
253 menuList
->Append(LIST_DELETE
, _T("&Delete first item\tCtrl-X"));
254 menuList
->Append(LIST_DELETE_ALL
, _T("Delete &all items"));
255 menuList
->AppendSeparator();
256 menuList
->Append(LIST_FREEZE
, _T("Free&ze\tCtrl-Z"));
257 menuList
->Append(LIST_THAW
, _T("Tha&w\tCtrl-W"));
258 menuList
->AppendSeparator();
259 menuList
->AppendCheckItem(LIST_TOGGLE_LINES
, _T("Toggle &lines\tCtrl-I"));
260 menuList
->Append(LIST_TOGGLE_MULTI_SEL
, _T("&Multiple selection\tCtrl-M"),
261 _T("Toggle multiple selection"), true);
263 wxMenu
*menuCol
= new wxMenu
;
264 menuCol
->Append(LIST_SET_FG_COL
, _T("&Foreground colour..."));
265 menuCol
->Append(LIST_SET_BG_COL
, _T("&Background colour..."));
267 wxMenuBar
*menubar
= new wxMenuBar
;
268 menubar
->Append(menuFile
, _T("&File"));
269 menubar
->Append(menuView
, _T("&View"));
270 menubar
->Append(menuList
, _T("&List"));
271 menubar
->Append(menuCol
, _T("&Colour"));
274 m_panel
= new wxPanel(this, wxID_ANY
);
275 m_logWindow
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
,
276 wxDefaultPosition
, wxDefaultSize
,
277 wxTE_READONLY
| wxTE_MULTILINE
| wxSUNKEN_BORDER
);
279 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
281 RecreateList(wxLC_REPORT
| wxLC_SINGLE_SEL
);
283 // Also add a simple list to the sample
285 m_simpleListCtrl
= new wxListCtrl( m_panel
, -1, wxDefaultPosition
, wxDefaultSize
,
286 wxLC_REPORT
| wxLC_SINGLE_SEL
);
287 m_simpleListCtrl
->InsertColumn(0, "Column 1", wxLIST_FORMAT_LEFT
, 80);
288 m_simpleListCtrl
->InsertColumn(1, "Column 2", wxLIST_FORMAT_LEFT
, 100);
289 m_simpleListCtrl
->InsertColumn(2, "Column 3", wxLIST_FORMAT_LEFT
, 100);
291 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
294 buf
.Printf(_T("Item %d"), i
);
295 long tmp
= m_simpleListCtrl
->InsertItem(i
, buf
, 0);
296 buf
.Printf(_T("Col 1, item %d"), i
);
297 m_simpleListCtrl
->SetItem(tmp
, 1, buf
);
298 buf
.Printf(_T("Col 2, item %d"), i
);
299 m_simpleListCtrl
->SetItem(tmp
, 2, buf
);
304 #endif // wxUSE_STATUSBAR
309 delete wxLog::SetActiveTarget(m_logOld
);
311 delete m_imageListNormal
;
312 delete m_imageListSmall
;
315 void MyFrame::OnSize(wxSizeEvent
& event
)
322 void MyFrame::DoSize()
327 wxSize size
= GetClientSize();
328 wxCoord y
= (2*size
.y
)/3;
329 m_listCtrl
->SetSize(0, 0, size
.x
-320, y
);
330 if (m_simpleListCtrl
) m_simpleListCtrl
->SetSize(size
.x
-320+1, 0, 320-1, y
);
331 m_logWindow
->SetSize(0, y
+ 1, size
.x
, size
.y
- y
);
334 bool MyFrame::CheckNonVirtual() const
336 if ( !m_listCtrl
->HasFlag(wxLC_VIRTUAL
) )
339 // "this" == whatever
340 wxLogWarning(_T("Can't do this in virtual view, sorry."));
345 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
350 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
352 wxMessageDialog
dialog(this, _T("List test sample\nJulian Smart (c) 1997"),
353 _T("About list test"), wxOK
|wxCANCEL
);
358 void MyFrame::OnFreeze(wxCommandEvent
& WXUNUSED(event
))
360 wxLogMessage(_T("Freezing the control"));
362 m_listCtrl
->Freeze();
365 void MyFrame::OnThaw(wxCommandEvent
& WXUNUSED(event
))
367 wxLogMessage(_T("Thawing the control"));
372 void MyFrame::OnToggleLines(wxCommandEvent
& event
)
374 m_listCtrl
->SetSingleStyle(wxLC_HRULES
| wxLC_VRULES
, event
.IsChecked());
377 void MyFrame::OnToggleMacUseGeneric(wxCommandEvent
& event
)
379 wxSystemOptions::SetOption(wxT("mac.listctrl.always_use_generic"), event
.IsChecked());
382 void MyFrame::OnGoTo(wxCommandEvent
& WXUNUSED(event
))
385 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
387 long sel
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
388 wxLIST_STATE_SELECTED
);
390 m_listCtrl
->SetItemState(sel
, 0, wxLIST_STATE_SELECTED
);
391 m_listCtrl
->SetItemState(index
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
394 void MyFrame::OnFocusLast(wxCommandEvent
& WXUNUSED(event
))
396 long index
= m_listCtrl
->GetItemCount() - 1;
402 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
403 m_listCtrl
->EnsureVisible(index
);
406 void MyFrame::OnToggleFirstSel(wxCommandEvent
& WXUNUSED(event
))
408 m_listCtrl
->SetItemState(0, (~m_listCtrl
->GetItemState(0, wxLIST_STATE_SELECTED
) ) & wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
411 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
413 if ( !CheckNonVirtual() )
416 int n
= m_listCtrl
->GetItemCount();
417 for (int i
= 0; i
< n
; i
++)
418 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
421 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
423 if ( !CheckNonVirtual() )
426 int n
= m_listCtrl
->GetItemCount();
427 for (int i
= 0; i
< n
; i
++)
428 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
431 // ----------------------------------------------------------------------------
432 // changing listctrl modes
433 // ----------------------------------------------------------------------------
435 void MyFrame::RecreateList(long flags
, bool withText
)
437 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
438 // style, but it is more trouble to do it than not
440 if ( !m_listCtrl
|| ((flags
& wxLC_VIRTUAL
) !=
441 (m_listCtrl
->GetWindowStyleFlag() & wxLC_VIRTUAL
)) )
446 m_listCtrl
= new MyListCtrl(m_panel
, LIST_CTRL
,
447 wxDefaultPosition
, wxDefaultSize
,
449 wxBORDER_THEME
| wxLC_EDIT_LABELS
);
451 switch ( flags
& wxLC_MASK_TYPE
)
458 InitWithIconItems(withText
);
461 case wxLC_SMALL_ICON
:
462 InitWithIconItems(withText
, true);
466 if ( flags
& wxLC_VIRTUAL
)
467 InitWithVirtualItems();
469 InitWithReportItems();
473 wxFAIL_MSG( _T("unknown listctrl mode") );
479 m_logWindow
->Clear();
482 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
484 RecreateList(wxLC_LIST
);
487 void MyFrame::InitWithListItems()
489 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
491 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Item %d"), i
));
495 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
497 RecreateList(wxLC_REPORT
);
500 void MyFrame::InitWithReportItems()
502 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
504 // note that under MSW for SetColumnWidth() to work we need to create the
505 // items with images initially even if we specify dummy image id
507 itemCol
.SetText(_T("Column 1"));
508 itemCol
.SetImage(-1);
509 m_listCtrl
->InsertColumn(0, itemCol
);
511 itemCol
.SetText(_T("Column 2"));
512 itemCol
.SetAlign(wxLIST_FORMAT_CENTRE
);
513 m_listCtrl
->InsertColumn(1, itemCol
);
515 itemCol
.SetText(_T("Column 3"));
516 itemCol
.SetAlign(wxLIST_FORMAT_RIGHT
);
517 m_listCtrl
->InsertColumn(2, itemCol
);
519 // to speed up inserting we hide the control temporarily
524 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
526 m_listCtrl
->InsertItemInReportView(i
);
529 m_logWindow
->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
530 NUM_ITEMS
, sw
.Time()));
533 // we leave all mask fields to 0 and only change the colour
536 item
.SetTextColour(*wxRED
);
537 m_listCtrl
->SetItem( item
);
540 item
.SetTextColour(*wxGREEN
);
541 m_listCtrl
->SetItem( item
);
543 item
.SetTextColour(*wxLIGHT_GREY
);
544 item
.SetFont(*wxITALIC_FONT
);
545 item
.SetBackgroundColour(*wxRED
);
546 m_listCtrl
->SetItem( item
);
548 m_listCtrl
->SetTextColour(*wxBLUE
);
550 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
551 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
552 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
554 // Set images in columns
555 m_listCtrl
->SetItemColumnImage(1, 1, 0);
561 m_listCtrl
->SetItem(info
);
563 // test SetItemFont too
564 m_listCtrl
->SetItemFont(0, *wxITALIC_FONT
);
567 void MyFrame::InitWithIconItems(bool withText
, bool sameIcon
)
569 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
570 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
572 for ( int i
= 0; i
< NUM_ICONS
; i
++ )
574 int image
= sameIcon
? 0 : i
;
578 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Label %d"), i
),
583 m_listCtrl
->InsertItem(i
, image
);
588 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
590 RecreateList(wxLC_ICON
, false);
593 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
595 RecreateList(wxLC_ICON
);
598 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
600 RecreateList(wxLC_SMALL_ICON
, false);
603 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
605 RecreateList(wxLC_SMALL_ICON
);
608 void MyFrame::OnVirtualView(wxCommandEvent
& WXUNUSED(event
))
610 m_smallVirtual
= false;
611 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
614 void MyFrame::OnSmallVirtualView(wxCommandEvent
& WXUNUSED(event
))
616 m_smallVirtual
= true;
617 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
620 void MyFrame::InitWithVirtualItems()
622 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
624 if ( m_smallVirtual
)
626 m_listCtrl
->InsertColumn(0, _T("Animal"));
627 m_listCtrl
->InsertColumn(1, _T("Sound"));
628 m_listCtrl
->SetItemCount(WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
));
632 m_listCtrl
->InsertColumn(0, _T("First Column"));
633 m_listCtrl
->InsertColumn(1, _T("Second Column"));
634 m_listCtrl
->SetColumnWidth(0, 150);
635 m_listCtrl
->SetColumnWidth(1, 150);
636 m_listCtrl
->SetItemCount(1000000);
640 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
644 m_listCtrl
->SortItems(MyCompareFunction
, 0);
646 m_logWindow
->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
647 m_listCtrl
->GetItemCount(),
651 void MyFrame::OnShowSelInfo(wxCommandEvent
& WXUNUSED(event
))
653 int selCount
= m_listCtrl
->GetSelectedItemCount();
654 wxLogMessage(_T("%d items selected:"), selCount
);
656 // don't show too many items
657 size_t shownCount
= 0;
659 long item
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
660 wxLIST_STATE_SELECTED
);
663 wxLogMessage(_T("\t%ld (%s)"),
664 item
, m_listCtrl
->GetItemText(item
).c_str());
666 if ( ++shownCount
> 10 )
668 wxLogMessage(_T("\t... more selected items snipped..."));
672 item
= m_listCtrl
->GetNextItem(item
, wxLIST_NEXT_ALL
,
673 wxLIST_STATE_SELECTED
);
677 void MyFrame::OnShowViewRect(wxCommandEvent
& WXUNUSED(event
))
679 const wxRect r
= m_listCtrl
->GetViewRect();
680 wxLogMessage("View rect: (%d, %d)-(%d, %d)",
681 r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
684 // ----------------------------------------------------------------------------
685 // column order tests
686 // ----------------------------------------------------------------------------
688 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
690 static wxString
DumpIntArray(const wxArrayInt
& a
)
693 const size_t count
= a
.size();
694 for ( size_t n
= 0; n
< count
; n
++ )
698 s
+= wxString::Format("%lu", (unsigned long)a
[n
]);
706 void MyFrame::OnSetColOrder(wxCommandEvent
& WXUNUSED(event
))
712 if ( m_listCtrl
->SetColumnsOrder(order
) )
713 wxLogMessage("Column order set to %s", DumpIntArray(order
));
716 void MyFrame::OnGetColOrder(wxCommandEvent
& WXUNUSED(event
))
718 // show what GetColumnsOrder() returns
719 const wxArrayInt order
= m_listCtrl
->GetColumnsOrder();
720 wxString msg
= "Columns order: " +
721 DumpIntArray(m_listCtrl
->GetColumnsOrder()) + "\n";
724 const int count
= m_listCtrl
->GetColumnCount();
726 // show the results of GetColumnOrder() for each column
727 msg
+= "GetColumnOrder() results:\n";
728 for ( n
= 0; n
< count
; n
++ )
730 msg
+= wxString::Format(" %2d -> %2d\n",
731 n
, m_listCtrl
->GetColumnOrder(n
));
734 // and the results of GetColumnIndexFromOrder() too
735 msg
+= "GetColumnIndexFromOrder() results:\n";
736 for ( n
= 0; n
< count
; n
++ )
738 msg
+= wxString::Format(" %2d -> %2d\n",
739 n
, m_listCtrl
->GetColumnIndexFromOrder(n
));
742 wxLogMessage("%s", msg
);
745 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
747 void MyFrame::OnShowColInfo(wxCommandEvent
& WXUNUSED(event
))
749 int count
= m_listCtrl
->GetColumnCount();
750 wxLogMessage(wxT("%d columns:"), count
);
751 for ( int c
= 0; c
< count
; c
++ )
753 wxLogMessage(wxT("\tcolumn %d has width %d"), c
,
754 m_listCtrl
->GetColumnWidth(c
));
758 void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent
& event
)
760 event
.Enable( (m_listCtrl
->GetWindowStyleFlag() & wxLC_REPORT
) != 0 );
763 void MyFrame::OnToggleMultiSel(wxCommandEvent
& WXUNUSED(event
))
765 long flags
= m_listCtrl
->GetWindowStyleFlag();
766 if ( flags
& wxLC_SINGLE_SEL
)
767 flags
&= ~wxLC_SINGLE_SEL
;
769 flags
|= wxLC_SINGLE_SEL
;
771 m_logWindow
->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
772 (flags
& wxLC_SINGLE_SEL
) ? _T("sing") : _T("multip")));
777 void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent
& event
)
779 event
.Check((m_listCtrl
->GetWindowStyleFlag() & wxLC_SINGLE_SEL
) == 0);
782 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
784 m_listCtrl
->SetForegroundColour(wxGetColourFromUser(this));
785 m_listCtrl
->Refresh();
788 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
790 m_listCtrl
->SetBackgroundColour(wxGetColourFromUser(this));
791 m_listCtrl
->Refresh();
794 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
))
796 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), _T("Appended item"));
799 void MyFrame::OnEdit(wxCommandEvent
& WXUNUSED(event
))
801 long itemCur
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
802 wxLIST_STATE_FOCUSED
);
806 m_listCtrl
->EditLabel(itemCur
);
810 m_logWindow
->WriteText(_T("No item to edit"));
814 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
816 if ( m_listCtrl
->GetItemCount() )
818 m_listCtrl
->DeleteItem(0);
822 m_logWindow
->WriteText(_T("Nothing to delete"));
826 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
830 int itemCount
= m_listCtrl
->GetItemCount();
832 m_listCtrl
->DeleteAllItems();
834 m_logWindow
->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
841 void MyListCtrl::OnCacheHint(wxListEvent
& event
)
843 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
844 event
.GetCacheFrom(), event
.GetCacheTo() );
847 void MyListCtrl::SetColumnImage(int col
, int image
)
850 item
.SetMask(wxLIST_MASK_IMAGE
);
851 item
.SetImage(image
);
852 SetColumn(col
, item
);
855 void MyListCtrl::OnColClick(wxListEvent
& event
)
857 int col
= event
.GetColumn();
859 // set or unset image
860 static bool x
= false;
862 SetColumnImage(col
, x
? 0 : -1);
864 wxLogMessage( wxT("OnColumnClick at %d."), col
);
867 void MyListCtrl::OnColRightClick(wxListEvent
& event
)
869 int col
= event
.GetColumn();
872 SetColumnImage(col
, -1);
875 // Show popupmenu at position
876 wxMenu
menu(wxT("Test"));
877 menu
.Append(LIST_ABOUT
, _T("&About"));
878 PopupMenu(&menu
, event
.GetPoint());
880 wxLogMessage( wxT("OnColumnRightClick at %d."), event
.GetColumn() );
883 void MyListCtrl::LogColEvent(const wxListEvent
& event
, const wxChar
*name
)
885 const int col
= event
.GetColumn();
887 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
890 event
.GetItem().GetWidth(),
891 GetColumnWidth(col
));
894 void MyListCtrl::OnColBeginDrag(wxListEvent
& event
)
896 LogColEvent( event
, wxT("OnColBeginDrag") );
898 if ( event
.GetColumn() == 0 )
900 wxLogMessage(_T("Resizing this column shouldn't work."));
906 void MyListCtrl::OnColDragging(wxListEvent
& event
)
908 LogColEvent( event
, wxT("OnColDragging") );
911 void MyListCtrl::OnColEndDrag(wxListEvent
& event
)
913 LogColEvent( event
, wxT("OnColEndDrag") );
916 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
918 const wxPoint
& pt
= event
.m_pointDrag
;
921 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
922 pt
.x
, pt
.y
, HitTest(pt
, flags
) );
925 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
927 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
928 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
931 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
933 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event
.m_item
.m_text
.c_str());
935 wxTextCtrl
* const text
= GetEditControl();
938 wxLogMessage("BUG: started to edit but no edit control");
942 wxLogMessage("Edit control value: \"%s\"", text
->GetValue());
946 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
948 wxLogMessage( wxT("OnEndLabelEdit: %s"),
950 event
.IsEditCancelled() ?
951 wxString("[cancelled]") :
957 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
959 LogEvent(event
, _T("OnDeleteItem"));
960 wxLogMessage( wxT("Number of items when delete event is sent: %d"), GetItemCount() );
963 void MyListCtrl::OnDeleteAllItems(wxListEvent
& event
)
965 LogEvent(event
, _T("OnDeleteAllItems"));
968 void MyListCtrl::OnSelected(wxListEvent
& event
)
970 LogEvent(event
, _T("OnSelected"));
972 if ( GetWindowStyle() & wxLC_REPORT
)
975 info
.m_itemId
= event
.m_itemIndex
;
977 info
.m_mask
= wxLIST_MASK_TEXT
;
980 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
981 info
.m_text
.c_str());
985 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
990 void MyListCtrl::OnDeselected(wxListEvent
& event
)
992 LogEvent(event
, _T("OnDeselected"));
995 void MyListCtrl::OnActivated(wxListEvent
& event
)
997 LogEvent(event
, _T("OnActivated"));
1000 void MyListCtrl::OnFocused(wxListEvent
& event
)
1002 LogEvent(event
, _T("OnFocused"));
1007 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
1011 switch ( event
.GetKeyCode() )
1013 case 'C': // colorize
1016 info
.m_itemId
= event
.GetIndex();
1017 if ( info
.m_itemId
== -1 )
1025 wxListItemAttr
*attr
= info
.GetAttributes();
1026 if ( !attr
|| !attr
->HasTextColour() )
1028 info
.SetTextColour(*wxCYAN
);
1032 RefreshItem(info
.m_itemId
);
1038 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
1039 if ( item
++ == GetItemCount() - 1 )
1044 wxLogMessage(_T("Focusing item %ld"), item
);
1046 SetItemState(item
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
1047 EnsureVisible(item
);
1050 case 'R': // show bounding rectangle
1052 item
= event
.GetIndex();
1054 if ( !GetItemRect(item
, r
) )
1056 wxLogError(_T("Failed to retrieve rect of item %ld"), item
);
1060 wxLogMessage(_T("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
1061 item
, r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
1065 case '1': // show sub item bounding rectangle
1068 case '4': // this column is invalid but we want to test it too
1069 if ( InReportView() )
1071 int subItem
= event
.GetKeyCode() - '1';
1072 item
= event
.GetIndex();
1074 if ( !GetSubItemRect(item
, subItem
, r
) )
1076 wxLogError(_T("Failed to retrieve rect of item %ld column %d"), item
, subItem
+ 1);
1080 wxLogMessage(_T("Bounding rect of item %ld column %d is (%d, %d)-(%d, %d)"),
1082 r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
1090 if ( m_updated
!= -1 )
1091 RefreshItem(m_updated
);
1093 m_updated
= event
.GetIndex();
1094 if ( m_updated
!= -1 )
1096 // we won't see changes to this item as it's selected, update
1097 // the next one (or the first one if we're on the last item)
1098 if ( ++m_updated
== GetItemCount() )
1101 wxLogMessage("Updating colour of the item %ld", m_updated
);
1102 RefreshItem(m_updated
);
1107 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1108 while ( item
!= -1 )
1112 wxLogMessage(_T("Item %ld deleted"), item
);
1114 // -1 because the indices were shifted by DeleteItem()
1115 item
= GetNextItem(item
- 1,
1116 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1121 if ( GetWindowStyle() & wxLC_REPORT
)
1123 if ( GetWindowStyle() & wxLC_VIRTUAL
)
1125 SetItemCount(GetItemCount() + 1);
1129 InsertItemInReportView(event
.GetIndex());
1132 //else: fall through
1135 LogEvent(event
, _T("OnListKeyDown"));
1141 void MyListCtrl::OnChar(wxKeyEvent
& event
)
1143 wxLogMessage(_T("Got char event."));
1145 switch ( event
.GetKeyCode() )
1159 // these are the keys we process ourselves
1167 void MyListCtrl::OnRightClick(wxMouseEvent
& event
)
1169 if ( !event
.ControlDown() )
1177 long item
= HitTest(event
.GetPosition(), flags
, &subitem
);
1182 case wxLIST_HITTEST_ABOVE
: where
= _T("above"); break;
1183 case wxLIST_HITTEST_BELOW
: where
= _T("below"); break;
1184 case wxLIST_HITTEST_NOWHERE
: where
= _T("nowhere near"); break;
1185 case wxLIST_HITTEST_ONITEMICON
: where
= _T("on icon of"); break;
1186 case wxLIST_HITTEST_ONITEMLABEL
: where
= _T("on label of"); break;
1187 case wxLIST_HITTEST_ONITEMRIGHT
: where
= _T("right on"); break;
1188 case wxLIST_HITTEST_TOLEFT
: where
= _T("to the left of"); break;
1189 case wxLIST_HITTEST_TORIGHT
: where
= _T("to the right of"); break;
1190 default: where
= _T("not clear exactly where on"); break;
1193 wxLogMessage(_T("Right double click %s item %ld, subitem %ld"),
1194 where
.c_str(), item
, subitem
);
1197 void MyListCtrl::LogEvent(const wxListEvent
& event
, const wxChar
*eventName
)
1199 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
1200 event
.GetIndex(), eventName
,
1201 event
.GetText().c_str(), event
.GetData());
1204 wxString
MyListCtrl::OnGetItemText(long item
, long column
) const
1206 if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
) )
1208 return SMALL_VIRTUAL_VIEW_ITEMS
[item
][column
];
1210 else // "big" virtual control
1212 return wxString::Format(_T("Column %ld of item %ld"), column
, item
);
1216 int MyListCtrl::OnGetItemColumnImage(long item
, long column
) const
1221 if (!(item
% 3) && column
== 1)
1227 wxListItemAttr
*MyListCtrl::OnGetItemAttr(long item
) const
1229 // test to check that RefreshItem() works correctly: when m_updated is
1230 // set to some item and it is refreshed, we highlight the item
1231 if ( item
== m_updated
)
1233 static wxListItemAttr
s_attrHighlight(*wxRED
, wxNullColour
, wxNullFont
);
1234 return &s_attrHighlight
;
1237 return item
% 2 ? NULL
: (wxListItemAttr
*)&m_attr
;
1240 void MyListCtrl::InsertItemInReportView(int i
)
1243 buf
.Printf(_T("This is item %d"), i
);
1244 long tmp
= InsertItem(i
, buf
, 0);
1245 SetItemData(tmp
, i
);
1247 buf
.Printf(_T("Col 1, item %d"), i
);
1248 SetItem(tmp
, 1, buf
);
1250 buf
.Printf(_T("Item %d in column 2"), i
);
1251 SetItem(tmp
, 2, buf
);
1254 #if USE_CONTEXT_MENU
1255 void MyListCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1257 if (GetEditControl() == NULL
)
1259 wxPoint point
= event
.GetPosition();
1261 if ( (point
.x
== -1) && (point
.y
== -1) )
1263 wxSize size
= GetSize();
1264 point
.x
= size
.x
/ 2;
1265 point
.y
= size
.y
/ 2;
1269 point
= ScreenToClient(point
);
1271 ShowContextMenu(point
);
1275 // the user is editing:
1276 // allow the text control to display its context menu
1277 // if it has one (it has on Windows) rather than display our one
1283 void MyListCtrl::ShowContextMenu(const wxPoint
& pos
)
1287 menu
.Append(wxID_ABOUT
, _T("&About"));
1288 menu
.AppendSeparator();
1289 menu
.Append(wxID_EXIT
, _T("E&xit"));
1291 PopupMenu(&menu
, pos
.x
, pos
.y
);