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"
50 // ----------------------------------------------------------------------------
51 // Constants and globals
52 // ----------------------------------------------------------------------------
54 const wxChar
*SMALL_VIRTUAL_VIEW_ITEMS
[][2] =
56 { _T("Cat"), _T("meow") },
57 { _T("Cow"), _T("moo") },
58 { _T("Crow"), _T("caw") },
59 { _T("Dog"), _T("woof") },
60 { _T("Duck"), _T("quack") },
61 { _T("Mouse"), _T("squeak") },
62 { _T("Owl"), _T("hoo") },
63 { _T("Pig"), _T("oink") },
64 { _T("Pigeon"), _T("coo") },
65 { _T("Sheep"), _T("baaah") },
68 // number of items in list/report view
69 static const int NUM_ITEMS
= 10;
71 // number of items in icon/small icon view
72 static const int NUM_ICONS
= 9;
75 MyCompareFunction(long item1
, long item2
, wxIntPtr
WXUNUSED(sortData
))
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
93 // `Main program' equivalent, creating windows and returning main app frame
96 if ( !wxApp
::OnInit() )
99 // Create the main frame window
100 MyFrame
*frame
= new MyFrame(wxT("wxListCtrl Test"));
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
117 EVT_SIZE(MyFrame
::OnSize
)
119 EVT_MENU(LIST_QUIT
, MyFrame
::OnQuit
)
120 EVT_MENU(LIST_ABOUT
, MyFrame
::OnAbout
)
121 EVT_MENU(LIST_LIST_VIEW
, MyFrame
::OnListView
)
122 EVT_MENU(LIST_REPORT_VIEW
, MyFrame
::OnReportView
)
123 EVT_MENU(LIST_ICON_VIEW
, MyFrame
::OnIconView
)
124 EVT_MENU(LIST_ICON_TEXT_VIEW
, MyFrame
::OnIconTextView
)
125 EVT_MENU(LIST_SMALL_ICON_VIEW
, MyFrame
::OnSmallIconView
)
126 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW
, MyFrame
::OnSmallIconTextView
)
127 EVT_MENU(LIST_VIRTUAL_VIEW
, MyFrame
::OnVirtualView
)
128 EVT_MENU(LIST_SMALL_VIRTUAL_VIEW
, MyFrame
::OnSmallVirtualView
)
130 EVT_MENU(LIST_GOTO
, MyFrame
::OnGoTo
)
131 EVT_MENU(LIST_FOCUS_LAST
, MyFrame
::OnFocusLast
)
132 EVT_MENU(LIST_TOGGLE_FIRST
, MyFrame
::OnToggleFirstSel
)
133 EVT_MENU(LIST_DESELECT_ALL
, MyFrame
::OnDeselectAll
)
134 EVT_MENU(LIST_SELECT_ALL
, MyFrame
::OnSelectAll
)
135 EVT_MENU(LIST_DELETE
, MyFrame
::OnDelete
)
136 EVT_MENU(LIST_ADD
, MyFrame
::OnAdd
)
137 EVT_MENU(LIST_EDIT
, MyFrame
::OnEdit
)
138 EVT_MENU(LIST_DELETE_ALL
, MyFrame
::OnDeleteAll
)
139 EVT_MENU(LIST_SORT
, MyFrame
::OnSort
)
140 EVT_MENU(LIST_SET_FG_COL
, MyFrame
::OnSetFgColour
)
141 EVT_MENU(LIST_SET_BG_COL
, MyFrame
::OnSetBgColour
)
142 EVT_MENU(LIST_TOGGLE_MULTI_SEL
, MyFrame
::OnToggleMultiSel
)
143 EVT_MENU(LIST_SHOW_COL_INFO
, MyFrame
::OnShowColInfo
)
144 EVT_MENU(LIST_SHOW_SEL_INFO
, MyFrame
::OnShowSelInfo
)
145 EVT_MENU(LIST_SHOW_VIEW_RECT
, MyFrame
::OnShowViewRect
)
146 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
147 EVT_MENU(LIST_SET_COL_ORDER
, MyFrame
::OnSetColOrder
)
148 EVT_MENU(LIST_GET_COL_ORDER
, MyFrame
::OnGetColOrder
)
149 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
150 EVT_MENU(LIST_FREEZE
, MyFrame
::OnFreeze
)
151 EVT_MENU(LIST_THAW
, MyFrame
::OnThaw
)
152 EVT_MENU(LIST_TOGGLE_LINES
, MyFrame
::OnToggleLines
)
154 EVT_MENU(LIST_MAC_USE_GENERIC
, MyFrame
::OnToggleMacUseGeneric
)
157 EVT_UPDATE_UI(LIST_SHOW_COL_INFO
, MyFrame
::OnUpdateShowColInfo
)
158 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL
, MyFrame
::OnUpdateToggleMultiSel
)
161 // My frame constructor
162 MyFrame
::MyFrame(const wxChar
*title
)
163 : wxFrame(NULL
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(600, 500))
167 m_smallVirtual
= false;
170 SetIcon( wxICON(mondrian
) );
172 // Make an image list containing large icons
173 m_imageListNormal
= new wxImageList(32, 32, true);
174 m_imageListSmall
= new wxImageList(16, 16, true);
177 m_imageListNormal
->Add( wxIcon(_T("icon1"), wxBITMAP_TYPE_ICO_RESOURCE
) );
178 m_imageListNormal
->Add( wxIcon(_T("icon2"), wxBITMAP_TYPE_ICO_RESOURCE
) );
179 m_imageListNormal
->Add( wxIcon(_T("icon3"), wxBITMAP_TYPE_ICO_RESOURCE
) );
180 m_imageListNormal
->Add( wxIcon(_T("icon4"), wxBITMAP_TYPE_ICO_RESOURCE
) );
181 m_imageListNormal
->Add( wxIcon(_T("icon5"), wxBITMAP_TYPE_ICO_RESOURCE
) );
182 m_imageListNormal
->Add( wxIcon(_T("icon6"), wxBITMAP_TYPE_ICO_RESOURCE
) );
183 m_imageListNormal
->Add( wxIcon(_T("icon7"), wxBITMAP_TYPE_ICO_RESOURCE
) );
184 m_imageListNormal
->Add( wxIcon(_T("icon8"), wxBITMAP_TYPE_ICO_RESOURCE
) );
185 m_imageListNormal
->Add( wxIcon(_T("icon9"), wxBITMAP_TYPE_ICO_RESOURCE
) );
187 m_imageListSmall
->Add( wxIcon(_T("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE
) );
190 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
191 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
192 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
193 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
194 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
195 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
196 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
197 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
198 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
200 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
204 wxMenu
*menuFile
= new wxMenu
;
205 menuFile
->Append(LIST_ABOUT
, _T("&About"));
206 menuFile
->AppendSeparator();
207 menuFile
->Append(LIST_QUIT
, _T("E&xit\tAlt-X"));
209 wxMenu
*menuView
= new wxMenu
;
210 menuView
->Append(LIST_LIST_VIEW
, _T("&List view\tF1"));
211 menuView
->Append(LIST_REPORT_VIEW
, _T("&Report view\tF2"));
212 menuView
->Append(LIST_ICON_VIEW
, _T("&Icon view\tF3"));
213 menuView
->Append(LIST_ICON_TEXT_VIEW
, _T("Icon view with &text\tF4"));
214 menuView
->Append(LIST_SMALL_ICON_VIEW
, _T("&Small icon view\tF5"));
215 menuView
->Append(LIST_SMALL_ICON_TEXT_VIEW
, _T("Small icon &view with text\tF6"));
216 menuView
->Append(LIST_VIRTUAL_VIEW
, _T("&Virtual view\tF7"));
217 menuView
->Append(LIST_SMALL_VIRTUAL_VIEW
, _T("Small virtual vie&w\tF8"));
219 menuView
->AppendCheckItem(LIST_MAC_USE_GENERIC
, _T("Mac: Use Generic Control"));
222 wxMenu
*menuList
= new wxMenu
;
223 menuList
->Append(LIST_GOTO
, _T("&Go to item #3\tCtrl-3"));
224 menuList
->Append(LIST_FOCUS_LAST
, _T("&Make last item current\tCtrl-L"));
225 menuList
->Append(LIST_TOGGLE_FIRST
, _T("To&ggle first item\tCtrl-G"));
226 menuList
->Append(LIST_DESELECT_ALL
, _T("&Deselect All\tCtrl-D"));
227 menuList
->Append(LIST_SELECT_ALL
, _T("S&elect All\tCtrl-A"));
228 menuList
->AppendSeparator();
229 menuList
->Append(LIST_SHOW_COL_INFO
, _T("Show &column info\tCtrl-C"));
230 menuList
->Append(LIST_SHOW_SEL_INFO
, _T("Show &selected items\tCtrl-S"));
231 menuList
->Append(LIST_SHOW_VIEW_RECT
, _T("Show &view rect"));
232 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
233 menuList
->Append(LIST_SET_COL_ORDER
, _T("Se&t columns order\tShift-Ctrl-O"));
234 menuList
->Append(LIST_GET_COL_ORDER
, _T("Show&w columns order\tCtrl-O"));
235 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
236 menuList
->AppendSeparator();
237 menuList
->Append(LIST_SORT
, _T("Sor&t\tCtrl-T"));
238 menuList
->AppendSeparator();
239 menuList
->Append(LIST_ADD
, _T("&Append an item\tCtrl-P"));
240 menuList
->Append(LIST_EDIT
, _T("&Edit the item\tCtrl-E"));
241 menuList
->Append(LIST_DELETE
, _T("&Delete first item\tCtrl-X"));
242 menuList
->Append(LIST_DELETE_ALL
, _T("Delete &all items"));
243 menuList
->AppendSeparator();
244 menuList
->Append(LIST_FREEZE
, _T("Free&ze\tCtrl-Z"));
245 menuList
->Append(LIST_THAW
, _T("Tha&w\tCtrl-W"));
246 menuList
->AppendSeparator();
247 menuList
->AppendCheckItem(LIST_TOGGLE_LINES
, _T("Toggle &lines\tCtrl-I"));
248 menuList
->Append(LIST_TOGGLE_MULTI_SEL
, _T("&Multiple selection\tCtrl-M"),
249 _T("Toggle multiple selection"), true);
251 wxMenu
*menuCol
= new wxMenu
;
252 menuCol
->Append(LIST_SET_FG_COL
, _T("&Foreground colour..."));
253 menuCol
->Append(LIST_SET_BG_COL
, _T("&Background colour..."));
255 wxMenuBar
*menubar
= new wxMenuBar
;
256 menubar
->Append(menuFile
, _T("&File"));
257 menubar
->Append(menuView
, _T("&View"));
258 menubar
->Append(menuList
, _T("&List"));
259 menubar
->Append(menuCol
, _T("&Colour"));
262 m_panel
= new wxPanel(this, wxID_ANY
);
263 m_logWindow
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
,
264 wxDefaultPosition
, wxDefaultSize
,
265 wxTE_READONLY
| wxTE_MULTILINE
| wxSUNKEN_BORDER
);
267 m_logOld
= wxLog
::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
269 RecreateList(wxLC_REPORT
| wxLC_SINGLE_SEL
);
272 // this is useful to know specially when debugging :)
273 wxLogMessage("Your version of comctl32.dll is: %d",
274 wxApp
::GetComCtl32Version());
279 #endif // wxUSE_STATUSBAR
284 delete wxLog
::SetActiveTarget(m_logOld
);
286 delete m_imageListNormal
;
287 delete m_imageListSmall
;
290 void MyFrame
::OnSize(wxSizeEvent
& event
)
297 void MyFrame
::DoSize()
302 wxSize size
= GetClientSize();
303 wxCoord y
= (2*size
.y
)/3;
304 m_listCtrl
->SetSize(0, 0, size
.x
, y
);
305 m_logWindow
->SetSize(0, y
+ 1, size
.x
, size
.y
- y
-1);
308 bool MyFrame
::CheckNonVirtual() const
310 if ( !m_listCtrl
->HasFlag(wxLC_VIRTUAL
) )
313 // "this" == whatever
314 wxLogWarning(_T("Can't do this in virtual view, sorry."));
319 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
324 void MyFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
))
326 wxMessageDialog
dialog(this, _T("List test sample\nJulian Smart (c) 1997"),
327 _T("About list test"), wxOK
|wxCANCEL
);
332 void MyFrame
::OnFreeze(wxCommandEvent
& WXUNUSED(event
))
334 wxLogMessage(_T("Freezing the control"));
336 m_listCtrl
->Freeze();
339 void MyFrame
::OnThaw(wxCommandEvent
& WXUNUSED(event
))
341 wxLogMessage(_T("Thawing the control"));
346 void MyFrame
::OnToggleLines(wxCommandEvent
& event
)
348 m_listCtrl
->SetSingleStyle(wxLC_HRULES
| wxLC_VRULES
, event
.IsChecked());
353 void MyFrame
::OnToggleMacUseGeneric(wxCommandEvent
& event
)
355 wxSystemOptions
::SetOption(wxT("mac.listctrl.always_use_generic"), event
.IsChecked());
360 void MyFrame
::OnGoTo(wxCommandEvent
& WXUNUSED(event
))
363 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
365 long sel
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
366 wxLIST_STATE_SELECTED
);
368 m_listCtrl
->SetItemState(sel
, 0, wxLIST_STATE_SELECTED
);
369 m_listCtrl
->SetItemState(index
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
372 void MyFrame
::OnFocusLast(wxCommandEvent
& WXUNUSED(event
))
374 long index
= m_listCtrl
->GetItemCount() - 1;
380 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
381 m_listCtrl
->EnsureVisible(index
);
384 void MyFrame
::OnToggleFirstSel(wxCommandEvent
& WXUNUSED(event
))
386 m_listCtrl
->SetItemState(0, (~m_listCtrl
->GetItemState(0, wxLIST_STATE_SELECTED
) ) & wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
389 void MyFrame
::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
391 if ( !CheckNonVirtual() )
394 int n
= m_listCtrl
->GetItemCount();
395 for (int i
= 0; i
< n
; i
++)
396 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
399 void MyFrame
::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
401 if ( !CheckNonVirtual() )
404 int n
= m_listCtrl
->GetItemCount();
405 for (int i
= 0; i
< n
; i
++)
406 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
409 // ----------------------------------------------------------------------------
410 // changing listctrl modes
411 // ----------------------------------------------------------------------------
413 void MyFrame
::RecreateList(long flags
, bool withText
)
415 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
416 // style, but it is more trouble to do it than not
418 if ( !m_listCtrl
|| ((flags
& wxLC_VIRTUAL
) !=
419 (m_listCtrl
->GetWindowStyleFlag() & wxLC_VIRTUAL
)) )
424 m_listCtrl
= new MyListCtrl(m_panel
, LIST_CTRL
,
425 wxDefaultPosition
, wxDefaultSize
,
427 wxBORDER_THEME
| wxLC_EDIT_LABELS
);
429 switch ( flags
& wxLC_MASK_TYPE
)
436 InitWithIconItems(withText
);
439 case wxLC_SMALL_ICON
:
440 InitWithIconItems(withText
, true);
444 if ( flags
& wxLC_VIRTUAL
)
445 InitWithVirtualItems();
447 InitWithReportItems();
451 wxFAIL_MSG( _T("unknown listctrl mode") );
457 m_logWindow
->Clear();
460 void MyFrame
::OnListView(wxCommandEvent
& WXUNUSED(event
))
462 RecreateList(wxLC_LIST
);
465 void MyFrame
::InitWithListItems()
467 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
469 m_listCtrl
->InsertItem(i
, wxString
::Format(_T("Item %d"), i
));
473 void MyFrame
::OnReportView(wxCommandEvent
& WXUNUSED(event
))
475 RecreateList(wxLC_REPORT
);
478 void MyFrame
::InitWithReportItems()
480 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
482 // note that under MSW for SetColumnWidth() to work we need to create the
483 // items with images initially even if we specify dummy image id
485 itemCol
.SetText(_T("Column 1"));
486 itemCol
.SetImage(-1);
487 m_listCtrl
->InsertColumn(0, itemCol
);
489 itemCol
.SetText(_T("Column 2"));
490 itemCol
.SetAlign(wxLIST_FORMAT_CENTRE
);
491 m_listCtrl
->InsertColumn(1, itemCol
);
493 itemCol
.SetText(_T("Column 3"));
494 itemCol
.SetAlign(wxLIST_FORMAT_RIGHT
);
495 m_listCtrl
->InsertColumn(2, itemCol
);
497 // to speed up inserting we hide the control temporarily
502 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
504 m_listCtrl
->InsertItemInReportView(i
);
507 m_logWindow
->WriteText(wxString
::Format(_T("%d items inserted in %ldms\n"),
508 NUM_ITEMS
, sw
.Time()));
511 // we leave all mask fields to 0 and only change the colour
514 item
.SetTextColour(*wxRED
);
515 m_listCtrl
->SetItem( item
);
518 item
.SetTextColour(*wxGREEN
);
519 m_listCtrl
->SetItem( item
);
521 item
.SetTextColour(*wxLIGHT_GREY
);
522 item
.SetFont(*wxITALIC_FONT
);
523 item
.SetBackgroundColour(*wxRED
);
524 m_listCtrl
->SetItem( item
);
526 m_listCtrl
->SetTextColour(*wxBLUE
);
528 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
529 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
530 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
532 // Set images in columns
533 m_listCtrl
->SetItemColumnImage(1, 1, 0);
539 m_listCtrl
->SetItem(info
);
541 // test SetItemFont too
542 m_listCtrl
->SetItemFont(0, *wxITALIC_FONT
);
545 void MyFrame
::InitWithIconItems(bool withText
, bool sameIcon
)
547 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
548 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
550 for ( int i
= 0; i
< NUM_ICONS
; i
++ )
552 int image
= sameIcon ?
0 : i
;
556 m_listCtrl
->InsertItem(i
, wxString
::Format(_T("Label %d"), i
),
561 m_listCtrl
->InsertItem(i
, image
);
566 void MyFrame
::OnIconView(wxCommandEvent
& WXUNUSED(event
))
568 RecreateList(wxLC_ICON
, false);
571 void MyFrame
::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
573 RecreateList(wxLC_ICON
);
576 void MyFrame
::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
578 RecreateList(wxLC_SMALL_ICON
, false);
581 void MyFrame
::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
583 RecreateList(wxLC_SMALL_ICON
);
586 void MyFrame
::OnVirtualView(wxCommandEvent
& WXUNUSED(event
))
588 m_smallVirtual
= false;
589 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
592 void MyFrame
::OnSmallVirtualView(wxCommandEvent
& WXUNUSED(event
))
594 m_smallVirtual
= true;
595 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
598 void MyFrame
::InitWithVirtualItems()
600 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
602 if ( m_smallVirtual
)
604 m_listCtrl
->InsertColumn(0, _T("Animal"));
605 m_listCtrl
->InsertColumn(1, _T("Sound"));
606 m_listCtrl
->SetItemCount(WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
));
610 m_listCtrl
->InsertColumn(0, _T("First Column"));
611 m_listCtrl
->InsertColumn(1, _T("Second Column"));
612 m_listCtrl
->SetColumnWidth(0, 150);
613 m_listCtrl
->SetColumnWidth(1, 150);
614 m_listCtrl
->SetItemCount(1000000);
618 void MyFrame
::OnSort(wxCommandEvent
& WXUNUSED(event
))
622 m_listCtrl
->SortItems(MyCompareFunction
, 0);
624 m_logWindow
->WriteText(wxString
::Format(_T("Sorting %d items took %ld ms\n"),
625 m_listCtrl
->GetItemCount(),
629 void MyFrame
::OnShowSelInfo(wxCommandEvent
& WXUNUSED(event
))
631 int selCount
= m_listCtrl
->GetSelectedItemCount();
632 wxLogMessage(_T("%d items selected:"), selCount
);
634 // don't show too many items
635 size_t shownCount
= 0;
637 long item
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
638 wxLIST_STATE_SELECTED
);
641 wxLogMessage(_T("\t%ld (%s)"),
642 item
, m_listCtrl
->GetItemText(item
).c_str());
644 if ( ++shownCount
> 10 )
646 wxLogMessage(_T("\t... more selected items snipped..."));
650 item
= m_listCtrl
->GetNextItem(item
, wxLIST_NEXT_ALL
,
651 wxLIST_STATE_SELECTED
);
655 void MyFrame
::OnShowViewRect(wxCommandEvent
& WXUNUSED(event
))
657 const wxRect r
= m_listCtrl
->GetViewRect();
658 wxLogMessage("View rect: (%d, %d)-(%d, %d)",
659 r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
662 // ----------------------------------------------------------------------------
663 // column order tests
664 // ----------------------------------------------------------------------------
666 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
668 static wxString
DumpIntArray(const wxArrayInt
& a
)
671 const size_t count
= a
.size();
672 for ( size_t n
= 0; n
< count
; n
++ )
676 s
+= wxString
::Format("%lu", (unsigned long)a
[n
]);
684 void MyFrame
::OnSetColOrder(wxCommandEvent
& WXUNUSED(event
))
690 if ( m_listCtrl
->SetColumnsOrder(order
) )
691 wxLogMessage("Column order set to %s", DumpIntArray(order
));
694 void MyFrame
::OnGetColOrder(wxCommandEvent
& WXUNUSED(event
))
696 // show what GetColumnsOrder() returns
697 const wxArrayInt order
= m_listCtrl
->GetColumnsOrder();
698 wxString msg
= "Columns order: " +
699 DumpIntArray(m_listCtrl
->GetColumnsOrder()) + "\n";
702 const int count
= m_listCtrl
->GetColumnCount();
704 // show the results of GetColumnOrder() for each column
705 msg
+= "GetColumnOrder() results:\n";
706 for ( n
= 0; n
< count
; n
++ )
708 msg
+= wxString
::Format(" %2d -> %2d\n",
709 n
, m_listCtrl
->GetColumnOrder(n
));
712 // and the results of GetColumnIndexFromOrder() too
713 msg
+= "GetColumnIndexFromOrder() results:\n";
714 for ( n
= 0; n
< count
; n
++ )
716 msg
+= wxString
::Format(" %2d -> %2d\n",
717 n
, m_listCtrl
->GetColumnIndexFromOrder(n
));
720 wxLogMessage("%s", msg
);
723 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
725 void MyFrame
::OnShowColInfo(wxCommandEvent
& WXUNUSED(event
))
727 int count
= m_listCtrl
->GetColumnCount();
728 wxLogMessage(wxT("%d columns:"), count
);
729 for ( int c
= 0; c
< count
; c
++ )
731 wxLogMessage(wxT("\tcolumn %d has width %d"), c
,
732 m_listCtrl
->GetColumnWidth(c
));
736 void MyFrame
::OnUpdateShowColInfo(wxUpdateUIEvent
& event
)
738 event
.Enable( (m_listCtrl
->GetWindowStyleFlag() & wxLC_REPORT
) != 0 );
741 void MyFrame
::OnToggleMultiSel(wxCommandEvent
& WXUNUSED(event
))
743 long flags
= m_listCtrl
->GetWindowStyleFlag();
744 if ( flags
& wxLC_SINGLE_SEL
)
745 flags
&= ~wxLC_SINGLE_SEL
;
747 flags
|= wxLC_SINGLE_SEL
;
749 m_logWindow
->WriteText(wxString
::Format(wxT("Current selection mode: %sle\n"),
750 (flags
& wxLC_SINGLE_SEL
) ?
_T("sing") : _T("multip")));
755 void MyFrame
::OnUpdateToggleMultiSel(wxUpdateUIEvent
& event
)
757 event
.Check((m_listCtrl
->GetWindowStyleFlag() & wxLC_SINGLE_SEL
) == 0);
760 void MyFrame
::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
762 m_listCtrl
->SetForegroundColour(wxGetColourFromUser(this));
763 m_listCtrl
->Refresh();
766 void MyFrame
::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
768 m_listCtrl
->SetBackgroundColour(wxGetColourFromUser(this));
769 m_listCtrl
->Refresh();
772 void MyFrame
::OnAdd(wxCommandEvent
& WXUNUSED(event
))
774 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), _T("Appended item"));
777 void MyFrame
::OnEdit(wxCommandEvent
& WXUNUSED(event
))
779 long itemCur
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
780 wxLIST_STATE_FOCUSED
);
784 m_listCtrl
->EditLabel(itemCur
);
788 m_logWindow
->WriteText(_T("No item to edit"));
792 void MyFrame
::OnDelete(wxCommandEvent
& WXUNUSED(event
))
794 if ( m_listCtrl
->GetItemCount() )
796 m_listCtrl
->DeleteItem(0);
800 m_logWindow
->WriteText(_T("Nothing to delete"));
804 void MyFrame
::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
808 int itemCount
= m_listCtrl
->GetItemCount();
810 m_listCtrl
->DeleteAllItems();
812 m_logWindow
->WriteText(wxString
::Format(_T("Deleting %d items took %ld ms\n"),
818 // ----------------------------------------------------------------------------
820 // ----------------------------------------------------------------------------
822 BEGIN_EVENT_TABLE(MyListCtrl
, wxListCtrl
)
823 EVT_LIST_BEGIN_DRAG(LIST_CTRL
, MyListCtrl
::OnBeginDrag
)
824 EVT_LIST_BEGIN_RDRAG(LIST_CTRL
, MyListCtrl
::OnBeginRDrag
)
825 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL
, MyListCtrl
::OnBeginLabelEdit
)
826 EVT_LIST_END_LABEL_EDIT(LIST_CTRL
, MyListCtrl
::OnEndLabelEdit
)
827 EVT_LIST_DELETE_ITEM(LIST_CTRL
, MyListCtrl
::OnDeleteItem
)
828 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL
, MyListCtrl
::OnDeleteAllItems
)
829 EVT_LIST_ITEM_SELECTED(LIST_CTRL
, MyListCtrl
::OnSelected
)
830 EVT_LIST_ITEM_DESELECTED(LIST_CTRL
, MyListCtrl
::OnDeselected
)
831 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl
::OnListKeyDown
)
832 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl
::OnActivated
)
833 EVT_LIST_ITEM_FOCUSED(LIST_CTRL
, MyListCtrl
::OnFocused
)
835 EVT_LIST_COL_CLICK(LIST_CTRL
, MyListCtrl
::OnColClick
)
836 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL
, MyListCtrl
::OnColRightClick
)
837 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL
, MyListCtrl
::OnColBeginDrag
)
838 EVT_LIST_COL_DRAGGING(LIST_CTRL
, MyListCtrl
::OnColDragging
)
839 EVT_LIST_COL_END_DRAG(LIST_CTRL
, MyListCtrl
::OnColEndDrag
)
841 EVT_LIST_CACHE_HINT(LIST_CTRL
, MyListCtrl
::OnCacheHint
)
844 EVT_CONTEXT_MENU(MyListCtrl
::OnContextMenu
)
846 EVT_CHAR(MyListCtrl
::OnChar
)
848 EVT_RIGHT_DOWN(MyListCtrl
::OnRightClick
)
851 void MyListCtrl
::OnCacheHint(wxListEvent
& event
)
853 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
854 event
.GetCacheFrom(), event
.GetCacheTo() );
857 void MyListCtrl
::SetColumnImage(int col
, int image
)
860 item
.SetMask(wxLIST_MASK_IMAGE
);
861 item
.SetImage(image
);
862 SetColumn(col
, item
);
865 void MyListCtrl
::OnColClick(wxListEvent
& event
)
867 int col
= event
.GetColumn();
869 // set or unset image
870 static bool x
= false;
872 SetColumnImage(col
, x ?
0 : -1);
874 wxLogMessage( wxT("OnColumnClick at %d."), col
);
877 void MyListCtrl
::OnColRightClick(wxListEvent
& event
)
879 int col
= event
.GetColumn();
882 SetColumnImage(col
, -1);
885 // Show popupmenu at position
886 wxMenu
menu(wxT("Test"));
887 menu
.Append(LIST_ABOUT
, _T("&About"));
888 PopupMenu(&menu
, event
.GetPoint());
890 wxLogMessage( wxT("OnColumnRightClick at %d."), event
.GetColumn() );
893 void MyListCtrl
::LogColEvent(const wxListEvent
& event
, const wxChar
*name
)
895 const int col
= event
.GetColumn();
897 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
900 event
.GetItem().GetWidth(),
901 GetColumnWidth(col
));
904 void MyListCtrl
::OnColBeginDrag(wxListEvent
& event
)
906 LogColEvent( event
, wxT("OnColBeginDrag") );
908 if ( event
.GetColumn() == 0 )
910 wxLogMessage(_T("Resizing this column shouldn't work."));
916 void MyListCtrl
::OnColDragging(wxListEvent
& event
)
918 LogColEvent( event
, wxT("OnColDragging") );
921 void MyListCtrl
::OnColEndDrag(wxListEvent
& event
)
923 LogColEvent( event
, wxT("OnColEndDrag") );
926 void MyListCtrl
::OnBeginDrag(wxListEvent
& event
)
928 const wxPoint
& pt
= event
.m_pointDrag
;
931 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
932 pt
.x
, pt
.y
, HitTest(pt
, flags
) );
935 void MyListCtrl
::OnBeginRDrag(wxListEvent
& event
)
937 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
938 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
941 void MyListCtrl
::OnBeginLabelEdit(wxListEvent
& event
)
943 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event
.m_item
.m_text
.c_str());
945 wxTextCtrl
* const text
= GetEditControl();
948 wxLogMessage("BUG: started to edit but no edit control");
952 wxLogMessage("Edit control value: \"%s\"", text
->GetValue());
956 void MyListCtrl
::OnEndLabelEdit(wxListEvent
& event
)
958 wxLogMessage( wxT("OnEndLabelEdit: %s"),
960 event
.IsEditCancelled() ?
961 wxString("[cancelled]") :
967 void MyListCtrl
::OnDeleteItem(wxListEvent
& event
)
969 LogEvent(event
, _T("OnDeleteItem"));
970 wxLogMessage( wxT("Number of items when delete event is sent: %d"), GetItemCount() );
973 void MyListCtrl
::OnDeleteAllItems(wxListEvent
& event
)
975 LogEvent(event
, _T("OnDeleteAllItems"));
978 void MyListCtrl
::OnSelected(wxListEvent
& event
)
980 LogEvent(event
, _T("OnSelected"));
982 if ( GetWindowStyle() & wxLC_REPORT
)
985 info
.m_itemId
= event
.m_itemIndex
;
987 info
.m_mask
= wxLIST_MASK_TEXT
;
990 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
991 info
.m_text
.c_str());
995 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
1000 void MyListCtrl
::OnDeselected(wxListEvent
& event
)
1002 LogEvent(event
, _T("OnDeselected"));
1005 void MyListCtrl
::OnActivated(wxListEvent
& event
)
1007 LogEvent(event
, _T("OnActivated"));
1010 void MyListCtrl
::OnFocused(wxListEvent
& event
)
1012 LogEvent(event
, _T("OnFocused"));
1017 void MyListCtrl
::OnListKeyDown(wxListEvent
& event
)
1021 switch ( event
.GetKeyCode() )
1023 case 'C': // colorize
1026 info
.m_itemId
= event
.GetIndex();
1027 if ( info
.m_itemId
== -1 )
1035 wxListItemAttr
*attr
= info
.GetAttributes();
1036 if ( !attr
|| !attr
->HasTextColour() )
1038 info
.SetTextColour(*wxCYAN
);
1042 RefreshItem(info
.m_itemId
);
1048 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
1049 if ( item
++ == GetItemCount() - 1 )
1054 wxLogMessage(_T("Focusing item %ld"), item
);
1056 SetItemState(item
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
1057 EnsureVisible(item
);
1060 case 'R': // show bounding rectangle
1062 item
= event
.GetIndex();
1064 if ( !GetItemRect(item
, r
) )
1066 wxLogError(_T("Failed to retrieve rect of item %ld"), item
);
1070 wxLogMessage(_T("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
1071 item
, r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
1075 case '1': // show sub item bounding rectangle
1078 case '4': // this column is invalid but we want to test it too
1079 if ( InReportView() )
1081 int subItem
= event
.GetKeyCode() - '1';
1082 item
= event
.GetIndex();
1084 if ( !GetSubItemRect(item
, subItem
, r
) )
1086 wxLogError(_T("Failed to retrieve rect of item %ld column %d"), item
, subItem
+ 1);
1090 wxLogMessage(_T("Bounding rect of item %ld column %d is (%d, %d)-(%d, %d)"),
1092 r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
1100 if ( m_updated
!= -1 )
1101 RefreshItem(m_updated
);
1103 m_updated
= event
.GetIndex();
1104 if ( m_updated
!= -1 )
1106 // we won't see changes to this item as it's selected, update
1107 // the next one (or the first one if we're on the last item)
1108 if ( ++m_updated
== GetItemCount() )
1111 wxLogMessage("Updating colour of the item %ld", m_updated
);
1112 RefreshItem(m_updated
);
1117 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1118 while ( item
!= -1 )
1122 wxLogMessage(_T("Item %ld deleted"), item
);
1124 // -1 because the indices were shifted by DeleteItem()
1125 item
= GetNextItem(item
- 1,
1126 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1131 if ( GetWindowStyle() & wxLC_REPORT
)
1133 if ( GetWindowStyle() & wxLC_VIRTUAL
)
1135 SetItemCount(GetItemCount() + 1);
1139 InsertItemInReportView(event
.GetIndex());
1142 //else: fall through
1145 LogEvent(event
, _T("OnListKeyDown"));
1151 void MyListCtrl
::OnChar(wxKeyEvent
& event
)
1153 wxLogMessage(_T("Got char event."));
1155 switch ( event
.GetKeyCode() )
1169 // these are the keys we process ourselves
1177 void MyListCtrl
::OnRightClick(wxMouseEvent
& event
)
1179 if ( !event
.ControlDown() )
1187 long item
= HitTest(event
.GetPosition(), flags
, &subitem
);
1192 case wxLIST_HITTEST_ABOVE
: where
= _T("above"); break;
1193 case wxLIST_HITTEST_BELOW
: where
= _T("below"); break;
1194 case wxLIST_HITTEST_NOWHERE
: where
= _T("nowhere near"); break;
1195 case wxLIST_HITTEST_ONITEMICON
: where
= _T("on icon of"); break;
1196 case wxLIST_HITTEST_ONITEMLABEL
: where
= _T("on label of"); break;
1197 case wxLIST_HITTEST_ONITEMRIGHT
: where
= _T("right on"); break;
1198 case wxLIST_HITTEST_TOLEFT
: where
= _T("to the left of"); break;
1199 case wxLIST_HITTEST_TORIGHT
: where
= _T("to the right of"); break;
1200 default: where
= _T("not clear exactly where on"); break;
1203 wxLogMessage(_T("Right double click %s item %ld, subitem %ld"),
1204 where
.c_str(), item
, subitem
);
1207 void MyListCtrl
::LogEvent(const wxListEvent
& event
, const wxChar
*eventName
)
1209 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
1210 event
.GetIndex(), eventName
,
1211 event
.GetText().c_str(), event
.GetData());
1214 wxString MyListCtrl
::OnGetItemText(long item
, long column
) const
1216 if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
) )
1218 return SMALL_VIRTUAL_VIEW_ITEMS
[item
][column
];
1220 else // "big" virtual control
1222 return wxString
::Format(_T("Column %ld of item %ld"), column
, item
);
1226 int MyListCtrl
::OnGetItemColumnImage(long item
, long column
) const
1231 if (!(item
% 3) && column
== 1)
1237 wxListItemAttr
*MyListCtrl
::OnGetItemAttr(long item
) const
1239 // test to check that RefreshItem() works correctly: when m_updated is
1240 // set to some item and it is refreshed, we highlight the item
1241 if ( item
== m_updated
)
1243 static wxListItemAttr
s_attrHighlight(*wxRED
, wxNullColour
, wxNullFont
);
1244 return &s_attrHighlight
;
1247 return item
% 2 ? NULL
: (wxListItemAttr
*)&m_attr
;
1250 void MyListCtrl
::InsertItemInReportView(int i
)
1253 buf
.Printf(_T("This is item %d"), i
);
1254 long tmp
= InsertItem(i
, buf
, 0);
1255 SetItemData(tmp
, i
);
1257 buf
.Printf(_T("Col 1, item %d"), i
);
1258 SetItem(tmp
, 1, buf
);
1260 buf
.Printf(_T("Item %d in column 2"), i
);
1261 SetItem(tmp
, 2, buf
);
1264 #if USE_CONTEXT_MENU
1265 void MyListCtrl
::OnContextMenu(wxContextMenuEvent
& event
)
1267 if (GetEditControl() == NULL
)
1269 wxPoint point
= event
.GetPosition();
1271 if ( (point
.x
== -1) && (point
.y
== -1) )
1273 wxSize size
= GetSize();
1274 point
.x
= size
.x
/ 2;
1275 point
.y
= size
.y
/ 2;
1279 point
= ScreenToClient(point
);
1281 ShowContextMenu(point
);
1285 // the user is editing:
1286 // allow the text control to display its context menu
1287 // if it has one (it has on Windows) rather than display our one
1293 void MyListCtrl
::ShowContextMenu(const wxPoint
& pos
)
1297 menu
.Append(wxID_ABOUT
, _T("&About"));
1298 menu
.AppendSeparator();
1299 menu
.Append(wxID_EXIT
, _T("E&xit"));
1301 PopupMenu(&menu
, pos
.x
, pos
.y
);