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"
46 #include "wx/numdlg.h"
51 // ----------------------------------------------------------------------------
52 // Constants and globals
53 // ----------------------------------------------------------------------------
55 const wxChar
*SMALL_VIRTUAL_VIEW_ITEMS
[][2] =
57 { wxT("Cat"), wxT("meow") },
58 { wxT("Cow"), wxT("moo") },
59 { wxT("Crow"), wxT("caw") },
60 { wxT("Dog"), wxT("woof") },
61 { wxT("Duck"), wxT("quack") },
62 { wxT("Mouse"), wxT("squeak") },
63 { wxT("Owl"), wxT("hoo") },
64 { wxT("Pig"), wxT("oink") },
65 { wxT("Pigeon"), wxT("coo") },
66 { wxT("Sheep"), wxT("baaah") },
69 // number of items in icon/small icon view
70 static const int NUM_ICONS
= 9;
73 MyCompareFunction(long item1
, long item2
, wxIntPtr
WXUNUSED(sortData
))
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
91 // `Main program' equivalent, creating windows and returning main app frame
94 if ( !wxApp::OnInit() )
97 // Create the main frame window
98 MyFrame
*frame
= new MyFrame(wxT("wxListCtrl Test"));
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
115 EVT_SIZE(MyFrame::OnSize
)
117 EVT_MENU(LIST_QUIT
, MyFrame::OnQuit
)
118 EVT_MENU(LIST_ABOUT
, MyFrame::OnAbout
)
119 EVT_MENU(LIST_LIST_VIEW
, MyFrame::OnListView
)
120 EVT_MENU(LIST_REPORT_VIEW
, MyFrame::OnReportView
)
121 EVT_MENU(LIST_ICON_VIEW
, MyFrame::OnIconView
)
122 EVT_MENU(LIST_ICON_TEXT_VIEW
, MyFrame::OnIconTextView
)
123 EVT_MENU(LIST_SMALL_ICON_VIEW
, MyFrame::OnSmallIconView
)
124 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW
, MyFrame::OnSmallIconTextView
)
125 EVT_MENU(LIST_VIRTUAL_VIEW
, MyFrame::OnVirtualView
)
126 EVT_MENU(LIST_SMALL_VIRTUAL_VIEW
, MyFrame::OnSmallVirtualView
)
128 EVT_MENU(LIST_SET_ITEMS_COUNT
, MyFrame::OnSetItemsCount
)
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
)
153 EVT_MENU(LIST_TOGGLE_HEADER
, MyFrame::OnToggleHeader
)
155 EVT_MENU(LIST_MAC_USE_GENERIC
, MyFrame::OnToggleMacUseGeneric
)
157 EVT_MENU(LIST_FIND
, MyFrame::OnFind
)
159 EVT_UPDATE_UI(LIST_SHOW_COL_INFO
, MyFrame::OnUpdateUIEnableInReport
)
160 EVT_UPDATE_UI(LIST_TOGGLE_HEADER
, MyFrame::OnUpdateUIEnableInReport
)
162 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnUpdateToggleMultiSel
)
163 EVT_UPDATE_UI(LIST_TOGGLE_HEADER
, MyFrame::OnUpdateToggleHeader
)
166 // My frame constructor
167 MyFrame::MyFrame(const wxChar
*title
)
168 : wxFrame(NULL
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(600, 500))
172 m_smallVirtual
= false;
176 SetIcon( wxICON(mondrian
) );
178 // Make an image list containing large icons
179 m_imageListNormal
= new wxImageList(32, 32, true);
180 m_imageListSmall
= new wxImageList(16, 16, true);
183 m_imageListNormal
->Add( wxIcon(wxT("icon1"), wxBITMAP_TYPE_ICO_RESOURCE
) );
184 m_imageListNormal
->Add( wxIcon(wxT("icon2"), wxBITMAP_TYPE_ICO_RESOURCE
) );
185 m_imageListNormal
->Add( wxIcon(wxT("icon3"), wxBITMAP_TYPE_ICO_RESOURCE
) );
186 m_imageListNormal
->Add( wxIcon(wxT("icon4"), wxBITMAP_TYPE_ICO_RESOURCE
) );
187 m_imageListNormal
->Add( wxIcon(wxT("icon5"), wxBITMAP_TYPE_ICO_RESOURCE
) );
188 m_imageListNormal
->Add( wxIcon(wxT("icon6"), wxBITMAP_TYPE_ICO_RESOURCE
) );
189 m_imageListNormal
->Add( wxIcon(wxT("icon7"), wxBITMAP_TYPE_ICO_RESOURCE
) );
190 m_imageListNormal
->Add( wxIcon(wxT("icon8"), wxBITMAP_TYPE_ICO_RESOURCE
) );
191 m_imageListNormal
->Add( wxIcon(wxT("icon9"), wxBITMAP_TYPE_ICO_RESOURCE
) );
193 m_imageListSmall
->Add( wxIcon(wxT("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE
) );
196 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
197 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
198 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
199 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
200 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
201 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
202 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
203 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
204 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
206 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
210 wxMenu
*menuFile
= new wxMenu
;
211 menuFile
->Append(LIST_ABOUT
, wxT("&About"));
212 menuFile
->AppendSeparator();
213 menuFile
->Append(LIST_QUIT
, wxT("E&xit\tAlt-X"));
215 wxMenu
*menuView
= new wxMenu
;
216 menuView
->Append(LIST_LIST_VIEW
, wxT("&List view\tF1"));
217 menuView
->Append(LIST_REPORT_VIEW
, wxT("&Report view\tF2"));
218 menuView
->Append(LIST_ICON_VIEW
, wxT("&Icon view\tF3"));
219 menuView
->Append(LIST_ICON_TEXT_VIEW
, wxT("Icon view with &text\tF4"));
220 menuView
->Append(LIST_SMALL_ICON_VIEW
, wxT("&Small icon view\tF5"));
221 menuView
->Append(LIST_SMALL_ICON_TEXT_VIEW
, wxT("Small icon &view with text\tF6"));
222 menuView
->Append(LIST_VIRTUAL_VIEW
, wxT("&Virtual view\tF7"));
223 menuView
->Append(LIST_SMALL_VIRTUAL_VIEW
, wxT("Small virtual vie&w\tF8"));
224 menuView
->AppendSeparator();
225 menuView
->Append(LIST_SET_ITEMS_COUNT
, "Set &number of items");
227 menuView
->AppendSeparator();
228 menuView
->AppendCheckItem(LIST_MAC_USE_GENERIC
, wxT("Mac: Use Generic Control"));
231 wxMenu
*menuList
= new wxMenu
;
232 menuList
->Append(LIST_GOTO
, wxT("&Go to item #3\tCtrl-3"));
233 menuList
->Append(LIST_FOCUS_LAST
, wxT("&Make last item current\tCtrl-L"));
234 menuList
->Append(LIST_TOGGLE_FIRST
, wxT("To&ggle first item\tCtrl-G"));
235 menuList
->Append(LIST_DESELECT_ALL
, wxT("&Deselect All\tCtrl-D"));
236 menuList
->Append(LIST_SELECT_ALL
, wxT("S&elect All\tCtrl-A"));
237 menuList
->AppendSeparator();
238 menuList
->Append(LIST_SHOW_COL_INFO
, wxT("Show &column info\tCtrl-C"));
239 menuList
->Append(LIST_SHOW_SEL_INFO
, wxT("Show &selected items\tCtrl-S"));
240 menuList
->Append(LIST_SHOW_VIEW_RECT
, wxT("Show &view rect"));
241 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
242 menuList
->Append(LIST_SET_COL_ORDER
, wxT("Se&t columns order\tShift-Ctrl-O"));
243 menuList
->Append(LIST_GET_COL_ORDER
, wxT("Sho&w columns order\tCtrl-O"));
244 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
245 menuList
->AppendSeparator();
246 menuList
->Append(LIST_SORT
, wxT("Sor&t\tCtrl-T"));
247 menuList
->Append(LIST_FIND
, "Test Find() performance");
248 menuList
->AppendSeparator();
249 menuList
->Append(LIST_ADD
, wxT("&Append an item\tCtrl-P"));
250 menuList
->Append(LIST_EDIT
, wxT("&Edit the item\tCtrl-E"));
251 menuList
->Append(LIST_DELETE
, wxT("&Delete first item\tCtrl-X"));
252 menuList
->Append(LIST_DELETE_ALL
, wxT("Delete &all items"));
253 menuList
->AppendSeparator();
254 menuList
->Append(LIST_FREEZE
, wxT("Free&ze\tCtrl-Z"));
255 menuList
->Append(LIST_THAW
, wxT("Tha&w\tCtrl-W"));
256 menuList
->AppendSeparator();
257 menuList
->AppendCheckItem(LIST_TOGGLE_LINES
, wxT("Toggle &lines\tCtrl-I"));
258 menuList
->AppendCheckItem(LIST_TOGGLE_MULTI_SEL
,
259 wxT("&Multiple selection\tCtrl-M"));
260 menuList
->Check(LIST_TOGGLE_MULTI_SEL
, true);
261 menuList
->AppendCheckItem(LIST_TOGGLE_HEADER
, "Toggle &header\tCtrl-H");
262 menuList
->Check(LIST_TOGGLE_HEADER
, true);
264 wxMenu
*menuCol
= new wxMenu
;
265 menuCol
->Append(LIST_SET_FG_COL
, wxT("&Foreground colour..."));
266 menuCol
->Append(LIST_SET_BG_COL
, wxT("&Background colour..."));
268 wxMenuBar
*menubar
= new wxMenuBar
;
269 menubar
->Append(menuFile
, wxT("&File"));
270 menubar
->Append(menuView
, wxT("&View"));
271 menubar
->Append(menuList
, wxT("&List"));
272 menubar
->Append(menuCol
, wxT("&Colour"));
275 m_panel
= new wxPanel(this, wxID_ANY
);
276 m_logWindow
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
,
277 wxDefaultPosition
, wxDefaultSize
,
278 wxTE_READONLY
| wxTE_MULTILINE
| wxSUNKEN_BORDER
);
280 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
282 RecreateList(wxLC_REPORT
| wxLC_SINGLE_SEL
);
285 // this is useful to know specially when debugging :)
286 wxLogMessage("Your version of comctl32.dll is: %d",
287 wxApp::GetComCtl32Version());
292 #endif // wxUSE_STATUSBAR
297 delete wxLog::SetActiveTarget(m_logOld
);
299 delete m_imageListNormal
;
300 delete m_imageListSmall
;
303 void MyFrame::OnSize(wxSizeEvent
& event
)
310 void MyFrame::DoSize()
315 wxSize size
= GetClientSize();
316 wxCoord y
= (2*size
.y
)/3;
317 m_listCtrl
->SetSize(0, 0, size
.x
, y
);
318 m_logWindow
->SetSize(0, y
+ 1, size
.x
, size
.y
- y
-1);
321 bool MyFrame::CheckNonVirtual() const
323 if ( !m_listCtrl
->HasFlag(wxLC_VIRTUAL
) )
326 // "this" == whatever
327 wxLogWarning(wxT("Can't do this in virtual view, sorry."));
332 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
337 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
339 wxMessageDialog
dialog(this, wxT("List test sample\nJulian Smart (c) 1997"),
340 wxT("About list test"), wxOK
|wxCANCEL
);
345 void MyFrame::OnFreeze(wxCommandEvent
& WXUNUSED(event
))
347 wxLogMessage(wxT("Freezing the control"));
349 m_listCtrl
->Freeze();
352 void MyFrame::OnThaw(wxCommandEvent
& WXUNUSED(event
))
354 wxLogMessage(wxT("Thawing the control"));
359 void MyFrame::OnToggleLines(wxCommandEvent
& event
)
361 m_listCtrl
->SetSingleStyle(wxLC_HRULES
| wxLC_VRULES
, event
.IsChecked());
364 void MyFrame::OnToggleHeader(wxCommandEvent
& event
)
366 wxLogMessage("%s the header", event
.IsChecked() ? "Showing" : "Hiding");
368 m_listCtrl
->ToggleWindowStyle(wxLC_NO_HEADER
);
373 void MyFrame::OnToggleMacUseGeneric(wxCommandEvent
& event
)
375 wxSystemOptions::SetOption(wxT("mac.listctrl.always_use_generic"), event
.IsChecked());
380 void MyFrame::OnGoTo(wxCommandEvent
& WXUNUSED(event
))
383 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
385 long sel
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
386 wxLIST_STATE_SELECTED
);
388 m_listCtrl
->SetItemState(sel
, 0, wxLIST_STATE_SELECTED
);
389 m_listCtrl
->SetItemState(index
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
392 void MyFrame::OnFocusLast(wxCommandEvent
& WXUNUSED(event
))
394 long index
= m_listCtrl
->GetItemCount() - 1;
400 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
401 m_listCtrl
->EnsureVisible(index
);
404 void MyFrame::OnToggleFirstSel(wxCommandEvent
& WXUNUSED(event
))
406 m_listCtrl
->SetItemState(0, (~m_listCtrl
->GetItemState(0, wxLIST_STATE_SELECTED
) ) & wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
409 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
411 if ( !CheckNonVirtual() )
414 int n
= m_listCtrl
->GetItemCount();
415 for (int i
= 0; i
< n
; i
++)
416 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
419 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
421 if ( !CheckNonVirtual() )
424 int n
= m_listCtrl
->GetItemCount();
425 for (int i
= 0; i
< n
; i
++)
426 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
429 // ----------------------------------------------------------------------------
430 // changing listctrl modes
431 // ----------------------------------------------------------------------------
433 void MyFrame::RecreateList(long flags
, bool withText
)
435 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
436 // style, but it is more trouble to do it than not
438 if ( !m_listCtrl
|| ((flags
& wxLC_VIRTUAL
) !=
439 (m_listCtrl
->GetWindowStyleFlag() & wxLC_VIRTUAL
)) )
444 m_listCtrl
= new MyListCtrl(m_panel
, LIST_CTRL
,
445 wxDefaultPosition
, wxDefaultSize
,
447 wxBORDER_THEME
| wxLC_EDIT_LABELS
);
449 switch ( flags
& wxLC_MASK_TYPE
)
456 InitWithIconItems(withText
);
459 case wxLC_SMALL_ICON
:
460 InitWithIconItems(withText
, true);
464 if ( flags
& wxLC_VIRTUAL
)
465 InitWithVirtualItems();
467 InitWithReportItems();
471 wxFAIL_MSG( wxT("unknown listctrl mode") );
477 m_logWindow
->Clear();
480 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
482 RecreateList(wxLC_LIST
);
485 void MyFrame::InitWithListItems()
487 for ( int i
= 0; i
< m_numListItems
; i
++ )
489 m_listCtrl
->InsertItem(i
, wxString::Format(wxT("Item %d"), i
));
493 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
495 RecreateList(wxLC_REPORT
);
498 void MyFrame::InitWithReportItems()
500 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
502 // note that under MSW for SetColumnWidth() to work we need to create the
503 // items with images initially even if we specify dummy image id
505 itemCol
.SetText(wxT("Column 1"));
506 itemCol
.SetImage(-1);
507 m_listCtrl
->InsertColumn(0, itemCol
);
509 itemCol
.SetText(wxT("Column 2"));
510 itemCol
.SetAlign(wxLIST_FORMAT_CENTRE
);
511 m_listCtrl
->InsertColumn(1, itemCol
);
513 itemCol
.SetText(wxT("Column 3"));
514 itemCol
.SetAlign(wxLIST_FORMAT_RIGHT
);
515 m_listCtrl
->InsertColumn(2, itemCol
);
517 // to speed up inserting we hide the control temporarily
522 for ( int i
= 0; i
< m_numListItems
; i
++ )
524 m_listCtrl
->InsertItemInReportView(i
);
527 m_logWindow
->WriteText(wxString::Format(wxT("%d items inserted in %ldms\n"),
528 m_numListItems
, sw
.Time()));
531 // we leave all mask fields to 0 and only change the colour
534 item
.SetTextColour(*wxRED
);
535 m_listCtrl
->SetItem( item
);
538 item
.SetTextColour(*wxGREEN
);
539 m_listCtrl
->SetItem( item
);
541 item
.SetTextColour(*wxLIGHT_GREY
);
542 item
.SetFont(*wxITALIC_FONT
);
543 item
.SetBackgroundColour(*wxRED
);
544 m_listCtrl
->SetItem( item
);
546 m_listCtrl
->SetTextColour(*wxBLUE
);
548 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
549 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
550 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
552 // Set images in columns
553 m_listCtrl
->SetItemColumnImage(1, 1, 0);
559 m_listCtrl
->SetItem(info
);
561 // test SetItemFont too
562 m_listCtrl
->SetItemFont(0, *wxITALIC_FONT
);
565 void MyFrame::InitWithIconItems(bool withText
, bool sameIcon
)
567 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
568 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
570 for ( int i
= 0; i
< NUM_ICONS
; i
++ )
572 int image
= sameIcon
? 0 : i
;
576 m_listCtrl
->InsertItem(i
, wxString::Format(wxT("Label %d"), i
),
581 m_listCtrl
->InsertItem(i
, image
);
586 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
588 RecreateList(wxLC_ICON
, false);
591 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
593 RecreateList(wxLC_ICON
);
596 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
598 RecreateList(wxLC_SMALL_ICON
, false);
601 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
603 RecreateList(wxLC_SMALL_ICON
);
606 void MyFrame::OnVirtualView(wxCommandEvent
& WXUNUSED(event
))
608 m_smallVirtual
= false;
609 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
612 void MyFrame::OnSmallVirtualView(wxCommandEvent
& WXUNUSED(event
))
614 m_smallVirtual
= true;
615 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
618 void MyFrame::OnSetItemsCount(wxCommandEvent
& WXUNUSED(event
))
620 int numItems
= wxGetNumberFromUser
622 "Enter the initial number of items for "
623 "the list and report views",
625 "wxWidgets wxListCtrl sample",
631 if ( numItems
== -1 || numItems
== m_numListItems
)
634 m_numListItems
= numItems
;
636 if ( m_listCtrl
->HasFlag(wxLC_REPORT
) &&
637 !m_listCtrl
->HasFlag(wxLC_VIRTUAL
) )
638 RecreateList(wxLC_REPORT
);
639 else if ( m_listCtrl
->HasFlag(wxLC_LIST
) )
640 RecreateList(wxLC_LIST
);
643 void MyFrame::InitWithVirtualItems()
645 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
647 if ( m_smallVirtual
)
649 m_listCtrl
->InsertColumn(0, wxT("Animal"));
650 m_listCtrl
->InsertColumn(1, wxT("Sound"));
651 m_listCtrl
->SetItemCount(WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
));
655 m_listCtrl
->InsertColumn(0, wxT("First Column"));
656 m_listCtrl
->InsertColumn(1, wxT("Second Column"));
657 m_listCtrl
->SetColumnWidth(0, 150);
658 m_listCtrl
->SetColumnWidth(1, 150);
659 m_listCtrl
->SetItemCount(1000000);
663 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
667 m_listCtrl
->SortItems(MyCompareFunction
, 0);
669 m_logWindow
->WriteText(wxString::Format(wxT("Sorting %d items took %ld ms\n"),
670 m_listCtrl
->GetItemCount(),
674 void MyFrame::OnFind(wxCommandEvent
& WXUNUSED(event
))
678 const int itemCount
= m_listCtrl
->GetItemCount();
679 for ( int i
= 0; i
< itemCount
; i
++ )
680 m_listCtrl
->FindItem(-1, i
);
682 wxLogMessage("Calling Find() for all %d items took %ld ms",
683 itemCount
, sw
.Time());
686 void MyFrame::OnShowSelInfo(wxCommandEvent
& WXUNUSED(event
))
688 int selCount
= m_listCtrl
->GetSelectedItemCount();
689 wxLogMessage(wxT("%d items selected:"), selCount
);
691 // don't show too many items
692 size_t shownCount
= 0;
694 long item
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
695 wxLIST_STATE_SELECTED
);
698 wxLogMessage(wxT("\t%ld (%s)"),
699 item
, m_listCtrl
->GetItemText(item
).c_str());
701 if ( ++shownCount
> 10 )
703 wxLogMessage(wxT("\t... more selected items snipped..."));
707 item
= m_listCtrl
->GetNextItem(item
, wxLIST_NEXT_ALL
,
708 wxLIST_STATE_SELECTED
);
712 void MyFrame::OnShowViewRect(wxCommandEvent
& WXUNUSED(event
))
714 const wxRect r
= m_listCtrl
->GetViewRect();
715 wxLogMessage("View rect: (%d, %d)-(%d, %d)",
716 r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
719 // ----------------------------------------------------------------------------
720 // column order tests
721 // ----------------------------------------------------------------------------
723 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
725 static wxString
DumpIntArray(const wxArrayInt
& a
)
728 const size_t count
= a
.size();
729 for ( size_t n
= 0; n
< count
; n
++ )
733 s
+= wxString::Format("%lu", (unsigned long)a
[n
]);
741 void MyFrame::OnSetColOrder(wxCommandEvent
& WXUNUSED(event
))
747 if ( m_listCtrl
->SetColumnsOrder(order
) )
749 wxLogMessage("Column order set to %s", DumpIntArray(order
));
753 void MyFrame::OnGetColOrder(wxCommandEvent
& WXUNUSED(event
))
755 // show what GetColumnsOrder() returns
756 const wxArrayInt order
= m_listCtrl
->GetColumnsOrder();
757 wxString msg
= "Columns order: " +
758 DumpIntArray(m_listCtrl
->GetColumnsOrder()) + "\n";
761 const int count
= m_listCtrl
->GetColumnCount();
763 // show the results of GetColumnOrder() for each column
764 msg
+= "GetColumnOrder() results:\n";
765 for ( n
= 0; n
< count
; n
++ )
767 msg
+= wxString::Format(" %2d -> %2d\n",
768 n
, m_listCtrl
->GetColumnOrder(n
));
771 // and the results of GetColumnIndexFromOrder() too
772 msg
+= "GetColumnIndexFromOrder() results:\n";
773 for ( n
= 0; n
< count
; n
++ )
775 msg
+= wxString::Format(" %2d -> %2d\n",
776 n
, m_listCtrl
->GetColumnIndexFromOrder(n
));
779 wxLogMessage("%s", msg
);
782 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
784 void MyFrame::OnShowColInfo(wxCommandEvent
& WXUNUSED(event
))
786 int count
= m_listCtrl
->GetColumnCount();
787 wxLogMessage(wxT("%d columns:"), count
);
788 for ( int c
= 0; c
< count
; c
++ )
790 wxLogMessage(wxT("\tcolumn %d has width %d"), c
,
791 m_listCtrl
->GetColumnWidth(c
));
795 void MyFrame::OnUpdateUIEnableInReport(wxUpdateUIEvent
& event
)
797 event
.Enable( (m_listCtrl
->GetWindowStyleFlag() & wxLC_REPORT
) != 0 );
800 void MyFrame::OnToggleMultiSel(wxCommandEvent
& WXUNUSED(event
))
802 long flags
= m_listCtrl
->GetWindowStyleFlag();
803 if ( flags
& wxLC_SINGLE_SEL
)
804 flags
&= ~wxLC_SINGLE_SEL
;
806 flags
|= wxLC_SINGLE_SEL
;
808 m_logWindow
->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
809 (flags
& wxLC_SINGLE_SEL
) ? wxT("sing") : wxT("multip")));
814 void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent
& event
)
816 event
.Check(!m_listCtrl
->HasFlag(wxLC_SINGLE_SEL
));
819 void MyFrame::OnUpdateToggleHeader(wxUpdateUIEvent
& event
)
821 event
.Check(!m_listCtrl
->HasFlag(wxLC_NO_HEADER
));
824 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
826 m_listCtrl
->SetForegroundColour(wxGetColourFromUser(this));
827 m_listCtrl
->Refresh();
830 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
832 m_listCtrl
->SetBackgroundColour(wxGetColourFromUser(this));
833 m_listCtrl
->Refresh();
836 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
))
838 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), wxT("Appended item"));
841 void MyFrame::OnEdit(wxCommandEvent
& WXUNUSED(event
))
843 // demonstrate cancelling editing: this currently is wxMSW-only
845 if ( m_listCtrl
->GetEditControl() )
847 m_listCtrl
->EndEditLabel(true);
849 else // start editing
852 long itemCur
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
853 wxLIST_STATE_FOCUSED
);
857 m_listCtrl
->EditLabel(itemCur
);
861 m_logWindow
->WriteText(wxT("No item to edit"));
866 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
868 if ( m_listCtrl
->GetItemCount() )
870 m_listCtrl
->DeleteItem(0);
874 m_logWindow
->WriteText(wxT("Nothing to delete"));
878 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
882 int itemCount
= m_listCtrl
->GetItemCount();
884 m_listCtrl
->DeleteAllItems();
886 m_logWindow
->WriteText(wxString::Format(wxT("Deleting %d items took %ld ms\n"),
892 // ----------------------------------------------------------------------------
894 // ----------------------------------------------------------------------------
896 BEGIN_EVENT_TABLE(MyListCtrl
, wxListCtrl
)
897 EVT_LIST_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnBeginDrag
)
898 EVT_LIST_BEGIN_RDRAG(LIST_CTRL
, MyListCtrl::OnBeginRDrag
)
899 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnBeginLabelEdit
)
900 EVT_LIST_END_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnEndLabelEdit
)
901 EVT_LIST_DELETE_ITEM(LIST_CTRL
, MyListCtrl::OnDeleteItem
)
902 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL
, MyListCtrl::OnDeleteAllItems
)
903 EVT_LIST_ITEM_SELECTED(LIST_CTRL
, MyListCtrl::OnSelected
)
904 EVT_LIST_ITEM_DESELECTED(LIST_CTRL
, MyListCtrl::OnDeselected
)
905 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl::OnListKeyDown
)
906 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl::OnActivated
)
907 EVT_LIST_ITEM_FOCUSED(LIST_CTRL
, MyListCtrl::OnFocused
)
909 EVT_LIST_COL_CLICK(LIST_CTRL
, MyListCtrl::OnColClick
)
910 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL
, MyListCtrl::OnColRightClick
)
911 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnColBeginDrag
)
912 EVT_LIST_COL_DRAGGING(LIST_CTRL
, MyListCtrl::OnColDragging
)
913 EVT_LIST_COL_END_DRAG(LIST_CTRL
, MyListCtrl::OnColEndDrag
)
915 EVT_LIST_CACHE_HINT(LIST_CTRL
, MyListCtrl::OnCacheHint
)
918 EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu
)
920 EVT_CHAR(MyListCtrl::OnChar
)
922 EVT_RIGHT_DOWN(MyListCtrl::OnRightClick
)
925 void MyListCtrl::OnCacheHint(wxListEvent
& event
)
927 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
928 event
.GetCacheFrom(), event
.GetCacheTo() );
931 void MyListCtrl::SetColumnImage(int col
, int image
)
934 item
.SetMask(wxLIST_MASK_IMAGE
);
935 item
.SetImage(image
);
936 SetColumn(col
, item
);
939 void MyListCtrl::OnColClick(wxListEvent
& event
)
941 int col
= event
.GetColumn();
943 // set or unset image
944 static bool x
= false;
946 SetColumnImage(col
, x
? 0 : -1);
948 wxLogMessage( wxT("OnColumnClick at %d."), col
);
951 void MyListCtrl::OnColRightClick(wxListEvent
& event
)
953 int col
= event
.GetColumn();
956 SetColumnImage(col
, -1);
959 // Show popupmenu at position
960 wxMenu
menu(wxT("Test"));
961 menu
.Append(LIST_ABOUT
, wxT("&About"));
962 PopupMenu(&menu
, event
.GetPoint());
964 wxLogMessage( wxT("OnColumnRightClick at %d."), event
.GetColumn() );
967 void MyListCtrl::LogColEvent(const wxListEvent
& event
, const wxChar
*name
)
969 const int col
= event
.GetColumn();
971 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
974 event
.GetItem().GetWidth(),
975 GetColumnWidth(col
));
978 void MyListCtrl::OnColBeginDrag(wxListEvent
& event
)
980 LogColEvent( event
, wxT("OnColBeginDrag") );
982 if ( event
.GetColumn() == 0 )
984 wxLogMessage(wxT("Resizing this column shouldn't work."));
990 void MyListCtrl::OnColDragging(wxListEvent
& event
)
992 LogColEvent( event
, wxT("OnColDragging") );
995 void MyListCtrl::OnColEndDrag(wxListEvent
& event
)
997 LogColEvent( event
, wxT("OnColEndDrag") );
1000 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
1002 const wxPoint
& pt
= event
.m_pointDrag
;
1005 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
1006 pt
.x
, pt
.y
, HitTest(pt
, flags
) );
1009 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
1011 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
1012 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
1015 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
1017 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event
.m_item
.m_text
.c_str());
1019 wxTextCtrl
* const text
= GetEditControl();
1022 wxLogMessage("BUG: started to edit but no edit control");
1026 wxLogMessage("Edit control value: \"%s\"", text
->GetValue());
1030 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
1032 wxLogMessage( wxT("OnEndLabelEdit: %s"),
1034 event
.IsEditCancelled() ?
1035 wxString("[cancelled]") :
1041 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
1043 LogEvent(event
, wxT("OnDeleteItem"));
1044 wxLogMessage( wxT("Number of items when delete event is sent: %d"), GetItemCount() );
1047 void MyListCtrl::OnDeleteAllItems(wxListEvent
& event
)
1049 LogEvent(event
, wxT("OnDeleteAllItems"));
1052 void MyListCtrl::OnSelected(wxListEvent
& event
)
1054 LogEvent(event
, wxT("OnSelected"));
1056 if ( GetWindowStyle() & wxLC_REPORT
)
1059 info
.m_itemId
= event
.m_itemIndex
;
1061 info
.m_mask
= wxLIST_MASK_TEXT
;
1062 if ( GetItem(info
) )
1064 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
1065 info
.m_text
.c_str());
1069 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
1074 void MyListCtrl::OnDeselected(wxListEvent
& event
)
1076 LogEvent(event
, wxT("OnDeselected"));
1079 void MyListCtrl::OnActivated(wxListEvent
& event
)
1081 LogEvent(event
, wxT("OnActivated"));
1084 void MyListCtrl::OnFocused(wxListEvent
& event
)
1086 LogEvent(event
, wxT("OnFocused"));
1091 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
1095 switch ( event
.GetKeyCode() )
1097 case 'C': // colorize
1100 info
.m_itemId
= event
.GetIndex();
1101 if ( info
.m_itemId
== -1 )
1109 wxListItemAttr
*attr
= info
.GetAttributes();
1110 if ( !attr
|| !attr
->HasTextColour() )
1112 info
.SetTextColour(*wxCYAN
);
1116 RefreshItem(info
.m_itemId
);
1122 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
1123 if ( item
++ == GetItemCount() - 1 )
1128 wxLogMessage(wxT("Focusing item %ld"), item
);
1130 SetItemState(item
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
1131 EnsureVisible(item
);
1134 case 'R': // show bounding rectangle
1136 item
= event
.GetIndex();
1138 if ( !GetItemRect(item
, r
) )
1140 wxLogError(wxT("Failed to retrieve rect of item %ld"), item
);
1144 wxLogMessage(wxT("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
1145 item
, r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
1149 case '1': // show sub item bounding rectangle for the given column
1150 case '2': // (and icon/label rectangle if Shift/Ctrl is pressed)
1152 case '4': // this column is invalid but we want to test it too
1153 if ( InReportView() )
1155 int subItem
= event
.GetKeyCode() - '1';
1156 item
= event
.GetIndex();
1159 int code
= wxLIST_RECT_BOUNDS
;
1160 if ( wxGetKeyState(WXK_SHIFT
) )
1161 code
= wxLIST_RECT_ICON
;
1162 else if ( wxGetKeyState(WXK_CONTROL
) )
1163 code
= wxLIST_RECT_LABEL
;
1165 if ( !GetSubItemRect(item
, subItem
, r
, code
) )
1167 wxLogError(wxT("Failed to retrieve rect of item %ld column %d"), item
, subItem
+ 1);
1171 wxLogMessage(wxT("Bounding rect of item %ld column %d is (%d, %d)-(%d, %d)"),
1173 r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
1181 if ( m_updated
!= -1 )
1182 RefreshItem(m_updated
);
1184 m_updated
= event
.GetIndex();
1185 if ( m_updated
!= -1 )
1187 // we won't see changes to this item as it's selected, update
1188 // the next one (or the first one if we're on the last item)
1189 if ( ++m_updated
== GetItemCount() )
1192 wxLogMessage("Updating colour of the item %ld", m_updated
);
1193 RefreshItem(m_updated
);
1198 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1199 while ( item
!= -1 )
1203 wxLogMessage(wxT("Item %ld deleted"), item
);
1205 // -1 because the indices were shifted by DeleteItem()
1206 item
= GetNextItem(item
- 1,
1207 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1212 if ( GetWindowStyle() & wxLC_REPORT
)
1214 if ( GetWindowStyle() & wxLC_VIRTUAL
)
1216 SetItemCount(GetItemCount() + 1);
1220 InsertItemInReportView(event
.GetIndex());
1223 //else: fall through
1226 LogEvent(event
, wxT("OnListKeyDown"));
1232 void MyListCtrl::OnChar(wxKeyEvent
& event
)
1234 wxLogMessage(wxT("Got char event."));
1236 switch ( event
.GetKeyCode() )
1250 // these are the keys we process ourselves
1258 void MyListCtrl::OnRightClick(wxMouseEvent
& event
)
1260 if ( !event
.ControlDown() )
1268 long item
= HitTest(event
.GetPosition(), flags
, &subitem
);
1273 case wxLIST_HITTEST_ABOVE
: where
= wxT("above"); break;
1274 case wxLIST_HITTEST_BELOW
: where
= wxT("below"); break;
1275 case wxLIST_HITTEST_NOWHERE
: where
= wxT("nowhere near"); break;
1276 case wxLIST_HITTEST_ONITEMICON
: where
= wxT("on icon of"); break;
1277 case wxLIST_HITTEST_ONITEMLABEL
: where
= wxT("on label of"); break;
1278 case wxLIST_HITTEST_ONITEMRIGHT
: where
= wxT("right on"); break;
1279 case wxLIST_HITTEST_TOLEFT
: where
= wxT("to the left of"); break;
1280 case wxLIST_HITTEST_TORIGHT
: where
= wxT("to the right of"); break;
1281 default: where
= wxT("not clear exactly where on"); break;
1284 wxLogMessage(wxT("Right double click %s item %ld, subitem %ld"),
1285 where
.c_str(), item
, subitem
);
1288 void MyListCtrl::LogEvent(const wxListEvent
& event
, const wxChar
*eventName
)
1290 wxLogMessage(wxT("Item %ld: %s (item text = %s, data = %ld)"),
1291 event
.GetIndex(), eventName
,
1292 event
.GetText().c_str(), event
.GetData());
1295 wxString
MyListCtrl::OnGetItemText(long item
, long column
) const
1297 if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
) )
1299 return SMALL_VIRTUAL_VIEW_ITEMS
[item
][column
];
1301 else // "big" virtual control
1303 return wxString::Format(wxT("Column %ld of item %ld"), column
, item
);
1307 int MyListCtrl::OnGetItemColumnImage(long item
, long column
) const
1312 if (!(item
% 3) && column
== 1)
1318 wxListItemAttr
*MyListCtrl::OnGetItemAttr(long item
) const
1320 // test to check that RefreshItem() works correctly: when m_updated is
1321 // set to some item and it is refreshed, we highlight the item
1322 if ( item
== m_updated
)
1324 static wxListItemAttr
s_attrHighlight(*wxRED
, wxNullColour
, wxNullFont
);
1325 return &s_attrHighlight
;
1328 return item
% 2 ? NULL
: (wxListItemAttr
*)&m_attr
;
1331 void MyListCtrl::InsertItemInReportView(int i
)
1334 buf
.Printf(wxT("This is item %d"), i
);
1335 long tmp
= InsertItem(i
, buf
, 0);
1336 SetItemData(tmp
, i
);
1338 buf
.Printf(wxT("Col 1, item %d"), i
);
1339 SetItem(tmp
, 1, buf
);
1341 buf
.Printf(wxT("Item %d in column 2"), i
);
1342 SetItem(tmp
, 2, buf
);
1345 #if USE_CONTEXT_MENU
1346 void MyListCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1348 if (GetEditControl() == NULL
)
1350 wxPoint point
= event
.GetPosition();
1352 if ( (point
.x
== -1) && (point
.y
== -1) )
1354 wxSize size
= GetSize();
1355 point
.x
= size
.x
/ 2;
1356 point
.y
= size
.y
/ 2;
1360 point
= ScreenToClient(point
);
1362 ShowContextMenu(point
);
1366 // the user is editing:
1367 // allow the text control to display its context menu
1368 // if it has one (it has on Windows) rather than display our one
1374 void MyListCtrl::ShowContextMenu(const wxPoint
& pos
)
1378 menu
.Append(wxID_ABOUT
, wxT("&About"));
1379 menu
.AppendSeparator();
1380 menu
.Append(wxID_EXIT
, wxT("E&xit"));
1382 PopupMenu(&menu
, pos
.x
, pos
.y
);