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(600, 500))
178 m_smallVirtual
= false;
181 SetIcon( wxICON(mondrian
) );
183 // Make an image list containing large icons
184 m_imageListNormal
= new wxImageList(32, 32, true);
185 m_imageListSmall
= new wxImageList(16, 16, true);
188 m_imageListNormal
->Add( wxIcon(_T("icon1"), wxBITMAP_TYPE_ICO_RESOURCE
) );
189 m_imageListNormal
->Add( wxIcon(_T("icon2"), wxBITMAP_TYPE_ICO_RESOURCE
) );
190 m_imageListNormal
->Add( wxIcon(_T("icon3"), wxBITMAP_TYPE_ICO_RESOURCE
) );
191 m_imageListNormal
->Add( wxIcon(_T("icon4"), wxBITMAP_TYPE_ICO_RESOURCE
) );
192 m_imageListNormal
->Add( wxIcon(_T("icon5"), wxBITMAP_TYPE_ICO_RESOURCE
) );
193 m_imageListNormal
->Add( wxIcon(_T("icon6"), wxBITMAP_TYPE_ICO_RESOURCE
) );
194 m_imageListNormal
->Add( wxIcon(_T("icon7"), wxBITMAP_TYPE_ICO_RESOURCE
) );
195 m_imageListNormal
->Add( wxIcon(_T("icon8"), wxBITMAP_TYPE_ICO_RESOURCE
) );
196 m_imageListNormal
->Add( wxIcon(_T("icon9"), wxBITMAP_TYPE_ICO_RESOURCE
) );
198 m_imageListSmall
->Add( wxIcon(_T("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE
) );
201 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
202 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
203 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
204 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
205 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
206 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
207 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
208 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
209 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
211 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
215 wxMenu
*menuFile
= new wxMenu
;
216 menuFile
->Append(LIST_ABOUT
, _T("&About"));
217 menuFile
->AppendSeparator();
218 menuFile
->Append(LIST_QUIT
, _T("E&xit\tAlt-X"));
220 wxMenu
*menuView
= new wxMenu
;
221 menuView
->Append(LIST_LIST_VIEW
, _T("&List view\tF1"));
222 menuView
->Append(LIST_REPORT_VIEW
, _T("&Report view\tF2"));
223 menuView
->Append(LIST_ICON_VIEW
, _T("&Icon view\tF3"));
224 menuView
->Append(LIST_ICON_TEXT_VIEW
, _T("Icon view with &text\tF4"));
225 menuView
->Append(LIST_SMALL_ICON_VIEW
, _T("&Small icon view\tF5"));
226 menuView
->Append(LIST_SMALL_ICON_TEXT_VIEW
, _T("Small icon &view with text\tF6"));
227 menuView
->Append(LIST_VIRTUAL_VIEW
, _T("&Virtual view\tF7"));
228 menuView
->Append(LIST_SMALL_VIRTUAL_VIEW
, _T("Small virtual vie&w\tF8"));
230 menuView
->AppendCheckItem(LIST_MAC_USE_GENERIC
, _T("Mac: Use Generic Control"));
233 wxMenu
*menuList
= new wxMenu
;
234 menuList
->Append(LIST_GOTO
, _T("&Go to item #3\tCtrl-3"));
235 menuList
->Append(LIST_FOCUS_LAST
, _T("&Make last item current\tCtrl-L"));
236 menuList
->Append(LIST_TOGGLE_FIRST
, _T("To&ggle first item\tCtrl-G"));
237 menuList
->Append(LIST_DESELECT_ALL
, _T("&Deselect All\tCtrl-D"));
238 menuList
->Append(LIST_SELECT_ALL
, _T("S&elect All\tCtrl-A"));
239 menuList
->AppendSeparator();
240 menuList
->Append(LIST_SHOW_COL_INFO
, _T("Show &column info\tCtrl-C"));
241 menuList
->Append(LIST_SHOW_SEL_INFO
, _T("Show &selected items\tCtrl-S"));
242 menuList
->Append(LIST_SHOW_VIEW_RECT
, _T("Show &view rect"));
243 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
244 menuList
->Append(LIST_SET_COL_ORDER
, _T("Se&t columns order\tShift-Ctrl-O"));
245 menuList
->Append(LIST_GET_COL_ORDER
, _T("Show&w columns order\tCtrl-O"));
246 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
247 menuList
->AppendSeparator();
248 menuList
->Append(LIST_SORT
, _T("Sor&t\tCtrl-T"));
249 menuList
->AppendSeparator();
250 menuList
->Append(LIST_ADD
, _T("&Append an item\tCtrl-P"));
251 menuList
->Append(LIST_EDIT
, _T("&Edit the item\tCtrl-E"));
252 menuList
->Append(LIST_DELETE
, _T("&Delete first item\tCtrl-X"));
253 menuList
->Append(LIST_DELETE_ALL
, _T("Delete &all items"));
254 menuList
->AppendSeparator();
255 menuList
->Append(LIST_FREEZE
, _T("Free&ze\tCtrl-Z"));
256 menuList
->Append(LIST_THAW
, _T("Tha&w\tCtrl-W"));
257 menuList
->AppendSeparator();
258 menuList
->AppendCheckItem(LIST_TOGGLE_LINES
, _T("Toggle &lines\tCtrl-I"));
259 menuList
->Append(LIST_TOGGLE_MULTI_SEL
, _T("&Multiple selection\tCtrl-M"),
260 _T("Toggle multiple selection"), true);
262 wxMenu
*menuCol
= new wxMenu
;
263 menuCol
->Append(LIST_SET_FG_COL
, _T("&Foreground colour..."));
264 menuCol
->Append(LIST_SET_BG_COL
, _T("&Background colour..."));
266 wxMenuBar
*menubar
= new wxMenuBar
;
267 menubar
->Append(menuFile
, _T("&File"));
268 menubar
->Append(menuView
, _T("&View"));
269 menubar
->Append(menuList
, _T("&List"));
270 menubar
->Append(menuCol
, _T("&Colour"));
273 m_panel
= new wxPanel(this, wxID_ANY
);
274 m_logWindow
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
,
275 wxDefaultPosition
, wxDefaultSize
,
276 wxTE_READONLY
| wxTE_MULTILINE
| wxSUNKEN_BORDER
);
278 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
280 RecreateList(wxLC_REPORT
| wxLC_SINGLE_SEL
);
284 #endif // wxUSE_STATUSBAR
289 delete wxLog::SetActiveTarget(m_logOld
);
291 delete m_imageListNormal
;
292 delete m_imageListSmall
;
295 void MyFrame::OnSize(wxSizeEvent
& event
)
302 void MyFrame::DoSize()
307 wxSize size
= GetClientSize();
308 wxCoord y
= (2*size
.y
)/3;
309 m_listCtrl
->SetSize(0, 0, size
.x
, y
);
310 m_logWindow
->SetSize(0, y
+ 1, size
.x
, size
.y
- y
);
313 bool MyFrame::CheckNonVirtual() const
315 if ( !m_listCtrl
->HasFlag(wxLC_VIRTUAL
) )
318 // "this" == whatever
319 wxLogWarning(_T("Can't do this in virtual view, sorry."));
324 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
329 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
331 wxMessageDialog
dialog(this, _T("List test sample\nJulian Smart (c) 1997"),
332 _T("About list test"), wxOK
|wxCANCEL
);
337 void MyFrame::OnFreeze(wxCommandEvent
& WXUNUSED(event
))
339 wxLogMessage(_T("Freezing the control"));
341 m_listCtrl
->Freeze();
344 void MyFrame::OnThaw(wxCommandEvent
& WXUNUSED(event
))
346 wxLogMessage(_T("Thawing the control"));
351 void MyFrame::OnToggleLines(wxCommandEvent
& event
)
353 m_listCtrl
->SetSingleStyle(wxLC_HRULES
| wxLC_VRULES
, event
.IsChecked());
356 void MyFrame::OnToggleMacUseGeneric(wxCommandEvent
& event
)
358 wxSystemOptions::SetOption(wxT("mac.listctrl.always_use_generic"), event
.IsChecked());
361 void MyFrame::OnGoTo(wxCommandEvent
& WXUNUSED(event
))
364 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
366 long sel
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
367 wxLIST_STATE_SELECTED
);
369 m_listCtrl
->SetItemState(sel
, 0, wxLIST_STATE_SELECTED
);
370 m_listCtrl
->SetItemState(index
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
373 void MyFrame::OnFocusLast(wxCommandEvent
& WXUNUSED(event
))
375 long index
= m_listCtrl
->GetItemCount() - 1;
381 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
382 m_listCtrl
->EnsureVisible(index
);
385 void MyFrame::OnToggleFirstSel(wxCommandEvent
& WXUNUSED(event
))
387 m_listCtrl
->SetItemState(0, (~m_listCtrl
->GetItemState(0, wxLIST_STATE_SELECTED
) ) & wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
390 void MyFrame::OnDeselectAll(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
,0,wxLIST_STATE_SELECTED
);
400 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
402 if ( !CheckNonVirtual() )
405 int n
= m_listCtrl
->GetItemCount();
406 for (int i
= 0; i
< n
; i
++)
407 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
410 // ----------------------------------------------------------------------------
411 // changing listctrl modes
412 // ----------------------------------------------------------------------------
414 void MyFrame::RecreateList(long flags
, bool withText
)
416 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
417 // style, but it is more trouble to do it than not
419 if ( !m_listCtrl
|| ((flags
& wxLC_VIRTUAL
) !=
420 (m_listCtrl
->GetWindowStyleFlag() & wxLC_VIRTUAL
)) )
425 m_listCtrl
= new MyListCtrl(m_panel
, LIST_CTRL
,
426 wxDefaultPosition
, wxDefaultSize
,
428 wxSUNKEN_BORDER
| wxLC_EDIT_LABELS
);
430 switch ( flags
& wxLC_MASK_TYPE
)
437 InitWithIconItems(withText
);
440 case wxLC_SMALL_ICON
:
441 InitWithIconItems(withText
, true);
445 if ( flags
& wxLC_VIRTUAL
)
446 InitWithVirtualItems();
448 InitWithReportItems();
452 wxFAIL_MSG( _T("unknown listctrl mode") );
458 m_logWindow
->Clear();
461 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
463 RecreateList(wxLC_LIST
);
466 void MyFrame::InitWithListItems()
468 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
470 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Item %d"), i
));
474 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
476 RecreateList(wxLC_REPORT
);
479 void MyFrame::InitWithReportItems()
481 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
483 // note that under MSW for SetColumnWidth() to work we need to create the
484 // items with images initially even if we specify dummy image id
486 itemCol
.SetText(_T("Column 1"));
487 itemCol
.SetImage(-1);
488 m_listCtrl
->InsertColumn(0, itemCol
);
490 itemCol
.SetText(_T("Column 2"));
491 itemCol
.SetAlign(wxLIST_FORMAT_CENTRE
);
492 m_listCtrl
->InsertColumn(1, itemCol
);
494 itemCol
.SetText(_T("Column 3"));
495 itemCol
.SetAlign(wxLIST_FORMAT_RIGHT
);
496 m_listCtrl
->InsertColumn(2, itemCol
);
498 // to speed up inserting we hide the control temporarily
503 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
505 m_listCtrl
->InsertItemInReportView(i
);
508 m_logWindow
->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
509 NUM_ITEMS
, sw
.Time()));
512 // we leave all mask fields to 0 and only change the colour
515 item
.SetTextColour(*wxRED
);
516 m_listCtrl
->SetItem( item
);
519 item
.SetTextColour(*wxGREEN
);
520 m_listCtrl
->SetItem( item
);
522 item
.SetTextColour(*wxLIGHT_GREY
);
523 item
.SetFont(*wxITALIC_FONT
);
524 item
.SetBackgroundColour(*wxRED
);
525 m_listCtrl
->SetItem( item
);
527 m_listCtrl
->SetTextColour(*wxBLUE
);
529 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
530 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
531 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
533 // Set images in columns
534 m_listCtrl
->SetItemColumnImage(1, 1, 0);
540 m_listCtrl
->SetItem(info
);
542 // test SetItemFont too
543 m_listCtrl
->SetItemFont(0, *wxITALIC_FONT
);
546 void MyFrame::InitWithIconItems(bool withText
, bool sameIcon
)
548 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
549 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
551 for ( int i
= 0; i
< NUM_ICONS
; i
++ )
553 int image
= sameIcon
? 0 : i
;
557 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Label %d"), i
),
562 m_listCtrl
->InsertItem(i
, image
);
567 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
569 RecreateList(wxLC_ICON
, false);
572 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
574 RecreateList(wxLC_ICON
);
577 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
579 RecreateList(wxLC_SMALL_ICON
, false);
582 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
584 RecreateList(wxLC_SMALL_ICON
);
587 void MyFrame::OnVirtualView(wxCommandEvent
& WXUNUSED(event
))
589 m_smallVirtual
= false;
590 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
593 void MyFrame::OnSmallVirtualView(wxCommandEvent
& WXUNUSED(event
))
595 m_smallVirtual
= true;
596 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
599 void MyFrame::InitWithVirtualItems()
601 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
603 if ( m_smallVirtual
)
605 m_listCtrl
->InsertColumn(0, _T("Animal"));
606 m_listCtrl
->InsertColumn(1, _T("Sound"));
607 m_listCtrl
->SetItemCount(WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
));
611 m_listCtrl
->InsertColumn(0, _T("First Column"));
612 m_listCtrl
->InsertColumn(1, _T("Second Column"));
613 m_listCtrl
->SetColumnWidth(0, 150);
614 m_listCtrl
->SetColumnWidth(1, 150);
615 m_listCtrl
->SetItemCount(1000000);
619 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
623 m_listCtrl
->SortItems(MyCompareFunction
, 0);
625 m_logWindow
->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
626 m_listCtrl
->GetItemCount(),
630 void MyFrame::OnShowSelInfo(wxCommandEvent
& WXUNUSED(event
))
632 int selCount
= m_listCtrl
->GetSelectedItemCount();
633 wxLogMessage(_T("%d items selected:"), selCount
);
635 // don't show too many items
636 size_t shownCount
= 0;
638 long item
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
639 wxLIST_STATE_SELECTED
);
642 wxLogMessage(_T("\t%ld (%s)"),
643 item
, m_listCtrl
->GetItemText(item
).c_str());
645 if ( ++shownCount
> 10 )
647 wxLogMessage(_T("\t... more selected items snipped..."));
651 item
= m_listCtrl
->GetNextItem(item
, wxLIST_NEXT_ALL
,
652 wxLIST_STATE_SELECTED
);
656 void MyFrame::OnShowViewRect(wxCommandEvent
& WXUNUSED(event
))
658 const wxRect r
= m_listCtrl
->GetViewRect();
659 wxLogMessage("View rect: (%d, %d)-(%d, %d)",
660 r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
663 // ----------------------------------------------------------------------------
664 // column order tests
665 // ----------------------------------------------------------------------------
667 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
669 static wxString
DumpIntArray(const wxArrayInt
& a
)
672 const size_t count
= a
.size();
673 for ( size_t n
= 0; n
< count
; n
++ )
677 s
+= wxString::Format("%lu", (unsigned long)a
[n
]);
685 void MyFrame::OnSetColOrder(wxCommandEvent
& WXUNUSED(event
))
691 if ( m_listCtrl
->SetColumnsOrder(order
) )
692 wxLogMessage("Column order set to %s", DumpIntArray(order
));
695 void MyFrame::OnGetColOrder(wxCommandEvent
& WXUNUSED(event
))
697 // show what GetColumnsOrder() returns
698 const wxArrayInt order
= m_listCtrl
->GetColumnsOrder();
699 wxString msg
= "Columns order: " +
700 DumpIntArray(m_listCtrl
->GetColumnsOrder()) + "\n";
703 const int count
= m_listCtrl
->GetColumnCount();
705 // show the results of GetColumnOrder() for each column
706 msg
+= "GetColumnOrder() results:\n";
707 for ( n
= 0; n
< count
; n
++ )
709 msg
+= wxString::Format(" %2d -> %2d\n",
710 n
, m_listCtrl
->GetColumnOrder(n
));
713 // and the results of GetColumnIndexFromOrder() too
714 msg
+= "GetColumnIndexFromOrder() results:\n";
715 for ( n
= 0; n
< count
; n
++ )
717 msg
+= wxString::Format(" %2d -> %2d\n",
718 n
, m_listCtrl
->GetColumnIndexFromOrder(n
));
721 wxLogMessage("%s", msg
);
724 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
726 void MyFrame::OnShowColInfo(wxCommandEvent
& WXUNUSED(event
))
728 int count
= m_listCtrl
->GetColumnCount();
729 wxLogMessage(wxT("%d columns:"), count
);
730 for ( int c
= 0; c
< count
; c
++ )
732 wxLogMessage(wxT("\tcolumn %d has width %d"), c
,
733 m_listCtrl
->GetColumnWidth(c
));
737 void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent
& event
)
739 event
.Enable( (m_listCtrl
->GetWindowStyleFlag() & wxLC_REPORT
) != 0 );
742 void MyFrame::OnToggleMultiSel(wxCommandEvent
& WXUNUSED(event
))
744 long flags
= m_listCtrl
->GetWindowStyleFlag();
745 if ( flags
& wxLC_SINGLE_SEL
)
746 flags
&= ~wxLC_SINGLE_SEL
;
748 flags
|= wxLC_SINGLE_SEL
;
750 m_logWindow
->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
751 (flags
& wxLC_SINGLE_SEL
) ? _T("sing") : _T("multip")));
756 void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent
& event
)
758 event
.Check((m_listCtrl
->GetWindowStyleFlag() & wxLC_SINGLE_SEL
) == 0);
761 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
763 m_listCtrl
->SetForegroundColour(wxGetColourFromUser(this));
764 m_listCtrl
->Refresh();
767 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
769 m_listCtrl
->SetBackgroundColour(wxGetColourFromUser(this));
770 m_listCtrl
->Refresh();
773 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
))
775 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), _T("Appended item"));
778 void MyFrame::OnEdit(wxCommandEvent
& WXUNUSED(event
))
780 long itemCur
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
781 wxLIST_STATE_FOCUSED
);
785 m_listCtrl
->EditLabel(itemCur
);
789 m_logWindow
->WriteText(_T("No item to edit"));
793 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
795 if ( m_listCtrl
->GetItemCount() )
797 m_listCtrl
->DeleteItem(0);
801 m_logWindow
->WriteText(_T("Nothing to delete"));
805 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
809 int itemCount
= m_listCtrl
->GetItemCount();
811 m_listCtrl
->DeleteAllItems();
813 m_logWindow
->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
820 void MyListCtrl::OnCacheHint(wxListEvent
& event
)
822 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
823 event
.GetCacheFrom(), event
.GetCacheTo() );
826 void MyListCtrl::SetColumnImage(int col
, int image
)
829 item
.SetMask(wxLIST_MASK_IMAGE
);
830 item
.SetImage(image
);
831 SetColumn(col
, item
);
834 void MyListCtrl::OnColClick(wxListEvent
& event
)
836 int col
= event
.GetColumn();
838 // set or unset image
839 static bool x
= false;
841 SetColumnImage(col
, x
? 0 : -1);
843 wxLogMessage( wxT("OnColumnClick at %d."), col
);
846 void MyListCtrl::OnColRightClick(wxListEvent
& event
)
848 int col
= event
.GetColumn();
851 SetColumnImage(col
, -1);
854 // Show popupmenu at position
855 wxMenu
menu(wxT("Test"));
856 menu
.Append(LIST_ABOUT
, _T("&About"));
857 PopupMenu(&menu
, event
.GetPoint());
859 wxLogMessage( wxT("OnColumnRightClick at %d."), event
.GetColumn() );
862 void MyListCtrl::LogColEvent(const wxListEvent
& event
, const wxChar
*name
)
864 const int col
= event
.GetColumn();
866 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
869 event
.GetItem().GetWidth(),
870 GetColumnWidth(col
));
873 void MyListCtrl::OnColBeginDrag(wxListEvent
& event
)
875 LogColEvent( event
, wxT("OnColBeginDrag") );
877 if ( event
.GetColumn() == 0 )
879 wxLogMessage(_T("Resizing this column shouldn't work."));
885 void MyListCtrl::OnColDragging(wxListEvent
& event
)
887 LogColEvent( event
, wxT("OnColDragging") );
890 void MyListCtrl::OnColEndDrag(wxListEvent
& event
)
892 LogColEvent( event
, wxT("OnColEndDrag") );
895 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
897 const wxPoint
& pt
= event
.m_pointDrag
;
900 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
901 pt
.x
, pt
.y
, HitTest(pt
, flags
) );
904 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
906 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
907 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
910 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
912 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event
.m_item
.m_text
.c_str());
914 wxTextCtrl
* const text
= GetEditControl();
917 wxLogMessage("BUG: started to edit but no edit control");
921 wxLogMessage("Edit control value: \"%s\"", text
->GetValue());
925 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
927 wxLogMessage( wxT("OnEndLabelEdit: %s"),
929 event
.IsEditCancelled() ?
930 wxString("[cancelled]") :
936 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
938 LogEvent(event
, _T("OnDeleteItem"));
939 wxLogMessage( wxT("Number of items when delete event is sent: %d"), GetItemCount() );
942 void MyListCtrl::OnDeleteAllItems(wxListEvent
& event
)
944 LogEvent(event
, _T("OnDeleteAllItems"));
947 void MyListCtrl::OnSelected(wxListEvent
& event
)
949 LogEvent(event
, _T("OnSelected"));
951 if ( GetWindowStyle() & wxLC_REPORT
)
954 info
.m_itemId
= event
.m_itemIndex
;
956 info
.m_mask
= wxLIST_MASK_TEXT
;
959 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
960 info
.m_text
.c_str());
964 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
969 void MyListCtrl::OnDeselected(wxListEvent
& event
)
971 LogEvent(event
, _T("OnDeselected"));
974 void MyListCtrl::OnActivated(wxListEvent
& event
)
976 LogEvent(event
, _T("OnActivated"));
979 void MyListCtrl::OnFocused(wxListEvent
& event
)
981 LogEvent(event
, _T("OnFocused"));
986 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
990 switch ( event
.GetKeyCode() )
992 case 'C': // colorize
995 info
.m_itemId
= event
.GetIndex();
996 if ( info
.m_itemId
== -1 )
1004 wxListItemAttr
*attr
= info
.GetAttributes();
1005 if ( !attr
|| !attr
->HasTextColour() )
1007 info
.SetTextColour(*wxCYAN
);
1011 RefreshItem(info
.m_itemId
);
1017 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
1018 if ( item
++ == GetItemCount() - 1 )
1023 wxLogMessage(_T("Focusing item %ld"), item
);
1025 SetItemState(item
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
1026 EnsureVisible(item
);
1029 case 'R': // show bounding rectangle
1031 item
= event
.GetIndex();
1033 if ( !GetItemRect(item
, r
) )
1035 wxLogError(_T("Failed to retrieve rect of item %ld"), item
);
1039 wxLogMessage(_T("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
1040 item
, r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
1044 case '1': // show sub item bounding rectangle
1047 case '4': // this column is invalid but we want to test it too
1048 if ( InReportView() )
1050 int subItem
= event
.GetKeyCode() - '1';
1051 item
= event
.GetIndex();
1053 if ( !GetSubItemRect(item
, subItem
, r
) )
1055 wxLogError(_T("Failed to retrieve rect of item %ld column %d"), item
, subItem
+ 1);
1059 wxLogMessage(_T("Bounding rect of item %ld column %d is (%d, %d)-(%d, %d)"),
1061 r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
1069 if ( m_updated
!= -1 )
1070 RefreshItem(m_updated
);
1072 m_updated
= event
.GetIndex();
1073 if ( m_updated
!= -1 )
1075 // we won't see changes to this item as it's selected, update
1076 // the next one (or the first one if we're on the last item)
1077 if ( ++m_updated
== GetItemCount() )
1080 wxLogMessage("Updating colour of the item %ld", m_updated
);
1081 RefreshItem(m_updated
);
1086 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1087 while ( item
!= -1 )
1091 wxLogMessage(_T("Item %ld deleted"), item
);
1093 // -1 because the indices were shifted by DeleteItem()
1094 item
= GetNextItem(item
- 1,
1095 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1100 if ( GetWindowStyle() & wxLC_REPORT
)
1102 if ( GetWindowStyle() & wxLC_VIRTUAL
)
1104 SetItemCount(GetItemCount() + 1);
1108 InsertItemInReportView(event
.GetIndex());
1111 //else: fall through
1114 LogEvent(event
, _T("OnListKeyDown"));
1120 void MyListCtrl::OnChar(wxKeyEvent
& event
)
1122 wxLogMessage(_T("Got char event."));
1124 switch ( event
.GetKeyCode() )
1138 // these are the keys we process ourselves
1146 void MyListCtrl::OnRightClick(wxMouseEvent
& event
)
1148 if ( !event
.ControlDown() )
1156 long item
= HitTest(event
.GetPosition(), flags
, &subitem
);
1161 case wxLIST_HITTEST_ABOVE
: where
= _T("above"); break;
1162 case wxLIST_HITTEST_BELOW
: where
= _T("below"); break;
1163 case wxLIST_HITTEST_NOWHERE
: where
= _T("nowhere near"); break;
1164 case wxLIST_HITTEST_ONITEMICON
: where
= _T("on icon of"); break;
1165 case wxLIST_HITTEST_ONITEMLABEL
: where
= _T("on label of"); break;
1166 case wxLIST_HITTEST_ONITEMRIGHT
: where
= _T("right on"); break;
1167 case wxLIST_HITTEST_TOLEFT
: where
= _T("to the left of"); break;
1168 case wxLIST_HITTEST_TORIGHT
: where
= _T("to the right of"); break;
1169 default: where
= _T("not clear exactly where on"); break;
1172 wxLogMessage(_T("Right double click %s item %ld, subitem %ld"),
1173 where
.c_str(), item
, subitem
);
1176 void MyListCtrl::LogEvent(const wxListEvent
& event
, const wxChar
*eventName
)
1178 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
1179 event
.GetIndex(), eventName
,
1180 event
.GetText().c_str(), event
.GetData());
1183 wxString
MyListCtrl::OnGetItemText(long item
, long column
) const
1185 if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
) )
1187 return SMALL_VIRTUAL_VIEW_ITEMS
[item
][column
];
1189 else // "big" virtual control
1191 return wxString::Format(_T("Column %ld of item %ld"), column
, item
);
1195 int MyListCtrl::OnGetItemColumnImage(long item
, long column
) const
1200 if (!(item
% 3) && column
== 1)
1206 wxListItemAttr
*MyListCtrl::OnGetItemAttr(long item
) const
1208 // test to check that RefreshItem() works correctly: when m_updated is
1209 // set to some item and it is refreshed, we highlight the item
1210 if ( item
== m_updated
)
1212 static wxListItemAttr
s_attrHighlight(*wxRED
, wxNullColour
, wxNullFont
);
1213 return &s_attrHighlight
;
1216 return item
% 2 ? NULL
: (wxListItemAttr
*)&m_attr
;
1219 void MyListCtrl::InsertItemInReportView(int i
)
1222 buf
.Printf(_T("This is item %d"), i
);
1223 long tmp
= InsertItem(i
, buf
, 0);
1224 SetItemData(tmp
, i
);
1226 buf
.Printf(_T("Col 1, item %d"), i
);
1227 SetItem(tmp
, 1, buf
);
1229 buf
.Printf(_T("Item %d in column 2"), i
);
1230 SetItem(tmp
, 2, buf
);
1233 #if USE_CONTEXT_MENU
1234 void MyListCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1236 if (GetEditControl() == NULL
)
1238 wxPoint point
= event
.GetPosition();
1240 if ( (point
.x
== -1) && (point
.y
== -1) )
1242 wxSize size
= GetSize();
1243 point
.x
= size
.x
/ 2;
1244 point
.y
= size
.y
/ 2;
1248 point
= ScreenToClient(point
);
1250 ShowContextMenu(point
);
1254 // the user is editing:
1255 // allow the text control to display its context menu
1256 // if it has one (it has on Windows) rather than display our one
1262 void MyListCtrl::ShowContextMenu(const wxPoint
& pos
)
1266 menu
.Append(wxID_ABOUT
, _T("&About"));
1267 menu
.AppendSeparator();
1268 menu
.Append(wxID_EXIT
, _T("E&xit"));
1270 PopupMenu(&menu
, pos
.x
, pos
.y
);