1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxListCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #ifndef wxHAS_IMAGES_IN_RESOURCES
24 #include "../sample.xpm"
27 #ifndef wxHAS_IMAGES_IN_RESOURCES
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(wxIntPtr item1
, wxIntPtr 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"));
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
112 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
113 EVT_SIZE(MyFrame::OnSize
)
115 EVT_MENU(LIST_QUIT
, MyFrame::OnQuit
)
116 EVT_MENU(LIST_ABOUT
, MyFrame::OnAbout
)
117 EVT_MENU(LIST_LIST_VIEW
, MyFrame::OnListView
)
118 EVT_MENU(LIST_REPORT_VIEW
, MyFrame::OnReportView
)
119 EVT_MENU(LIST_ICON_VIEW
, MyFrame::OnIconView
)
120 EVT_MENU(LIST_ICON_TEXT_VIEW
, MyFrame::OnIconTextView
)
121 EVT_MENU(LIST_SMALL_ICON_VIEW
, MyFrame::OnSmallIconView
)
122 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW
, MyFrame::OnSmallIconTextView
)
123 EVT_MENU(LIST_VIRTUAL_VIEW
, MyFrame::OnVirtualView
)
124 EVT_MENU(LIST_SMALL_VIRTUAL_VIEW
, MyFrame::OnSmallVirtualView
)
126 EVT_MENU(LIST_SET_ITEMS_COUNT
, MyFrame::OnSetItemsCount
)
128 EVT_MENU(LIST_GOTO
, MyFrame::OnGoTo
)
129 EVT_MENU(LIST_FOCUS_LAST
, MyFrame::OnFocusLast
)
130 EVT_MENU(LIST_TOGGLE_FIRST
, MyFrame::OnToggleFirstSel
)
131 EVT_MENU(LIST_DESELECT_ALL
, MyFrame::OnDeselectAll
)
132 EVT_MENU(LIST_SELECT_ALL
, MyFrame::OnSelectAll
)
133 EVT_MENU(LIST_DELETE
, MyFrame::OnDelete
)
134 EVT_MENU(LIST_ADD
, MyFrame::OnAdd
)
135 EVT_MENU(LIST_EDIT
, MyFrame::OnEdit
)
136 EVT_MENU(LIST_DELETE_ALL
, MyFrame::OnDeleteAll
)
137 EVT_MENU(LIST_SORT
, MyFrame::OnSort
)
138 EVT_MENU(LIST_SET_FG_COL
, MyFrame::OnSetFgColour
)
139 EVT_MENU(LIST_SET_BG_COL
, MyFrame::OnSetBgColour
)
140 EVT_MENU(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnToggleMultiSel
)
141 EVT_MENU(LIST_SHOW_COL_INFO
, MyFrame::OnShowColInfo
)
142 EVT_MENU(LIST_SHOW_SEL_INFO
, MyFrame::OnShowSelInfo
)
143 EVT_MENU(LIST_SHOW_VIEW_RECT
, MyFrame::OnShowViewRect
)
144 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
145 EVT_MENU(LIST_SET_COL_ORDER
, MyFrame::OnSetColOrder
)
146 EVT_MENU(LIST_GET_COL_ORDER
, MyFrame::OnGetColOrder
)
147 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
148 EVT_MENU(LIST_FREEZE
, MyFrame::OnFreeze
)
149 EVT_MENU(LIST_THAW
, MyFrame::OnThaw
)
150 EVT_MENU(LIST_TOGGLE_LINES
, MyFrame::OnToggleLines
)
151 EVT_MENU(LIST_TOGGLE_HEADER
, MyFrame::OnToggleHeader
)
152 EVT_MENU(LIST_TOGGLE_BELL
, MyFrame::OnToggleBell
)
154 EVT_MENU(LIST_MAC_USE_GENERIC
, MyFrame::OnToggleMacUseGeneric
)
156 EVT_MENU(LIST_FIND
, MyFrame::OnFind
)
158 EVT_UPDATE_UI(LIST_SHOW_COL_INFO
, MyFrame::OnUpdateUIEnableInReport
)
159 EVT_UPDATE_UI(LIST_TOGGLE_HEADER
, MyFrame::OnUpdateUIEnableInReport
)
161 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnUpdateToggleMultiSel
)
162 EVT_UPDATE_UI(LIST_TOGGLE_HEADER
, MyFrame::OnUpdateToggleHeader
)
165 // My frame constructor
166 MyFrame::MyFrame(const wxChar
*title
)
167 : wxFrame(NULL
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(600, 500))
171 m_smallVirtual
= false;
175 SetIcon(wxICON(sample
));
177 // Make an image list containing large icons
178 m_imageListNormal
= new wxImageList(32, 32, true);
179 m_imageListSmall
= new wxImageList(16, 16, true);
181 #ifdef wxHAS_IMAGES_IN_RESOURCES
182 m_imageListNormal
->Add( wxIcon(wxT("icon1"), wxBITMAP_TYPE_ICO_RESOURCE
) );
183 m_imageListNormal
->Add( wxIcon(wxT("icon2"), wxBITMAP_TYPE_ICO_RESOURCE
) );
184 m_imageListNormal
->Add( wxIcon(wxT("icon3"), wxBITMAP_TYPE_ICO_RESOURCE
) );
185 m_imageListNormal
->Add( wxIcon(wxT("icon4"), wxBITMAP_TYPE_ICO_RESOURCE
) );
186 m_imageListNormal
->Add( wxIcon(wxT("icon5"), wxBITMAP_TYPE_ICO_RESOURCE
) );
187 m_imageListNormal
->Add( wxIcon(wxT("icon6"), wxBITMAP_TYPE_ICO_RESOURCE
) );
188 m_imageListNormal
->Add( wxIcon(wxT("icon7"), wxBITMAP_TYPE_ICO_RESOURCE
) );
189 m_imageListNormal
->Add( wxIcon(wxT("icon8"), wxBITMAP_TYPE_ICO_RESOURCE
) );
190 m_imageListNormal
->Add( wxIcon(wxT("icon9"), wxBITMAP_TYPE_ICO_RESOURCE
) );
192 m_imageListSmall
->Add( wxIcon(wxT("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE
) );
195 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
196 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
197 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
198 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
199 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
200 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
201 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
202 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
203 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
205 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
209 wxMenu
*menuFile
= new wxMenu
;
210 menuFile
->Append(LIST_ABOUT
, wxT("&About"));
211 menuFile
->AppendSeparator();
212 menuFile
->Append(LIST_QUIT
, wxT("E&xit\tAlt-X"));
214 wxMenu
*menuView
= new wxMenu
;
215 menuView
->Append(LIST_LIST_VIEW
, wxT("&List view\tF1"));
216 menuView
->Append(LIST_REPORT_VIEW
, wxT("&Report view\tF2"));
217 menuView
->Append(LIST_ICON_VIEW
, wxT("&Icon view\tF3"));
218 menuView
->Append(LIST_ICON_TEXT_VIEW
, wxT("Icon view with &text\tF4"));
219 menuView
->Append(LIST_SMALL_ICON_VIEW
, wxT("&Small icon view\tF5"));
220 menuView
->Append(LIST_SMALL_ICON_TEXT_VIEW
, wxT("Small icon &view with text\tF6"));
221 menuView
->Append(LIST_VIRTUAL_VIEW
, wxT("&Virtual view\tF7"));
222 menuView
->Append(LIST_SMALL_VIRTUAL_VIEW
, wxT("Small virtual vie&w\tF8"));
223 menuView
->AppendSeparator();
224 menuView
->Append(LIST_SET_ITEMS_COUNT
, "Set &number of items");
226 menuView
->AppendSeparator();
227 menuView
->AppendCheckItem(LIST_MAC_USE_GENERIC
, wxT("Mac: Use Generic Control"));
230 wxMenu
*menuList
= new wxMenu
;
231 menuList
->Append(LIST_GOTO
, wxT("&Go to item #3\tCtrl-3"));
232 menuList
->Append(LIST_FOCUS_LAST
, wxT("&Make last item current\tCtrl-L"));
233 menuList
->Append(LIST_TOGGLE_FIRST
, wxT("To&ggle first item\tCtrl-G"));
234 menuList
->Append(LIST_DESELECT_ALL
, wxT("&Deselect All\tCtrl-D"));
235 menuList
->Append(LIST_SELECT_ALL
, wxT("S&elect All\tCtrl-A"));
236 menuList
->AppendSeparator();
237 menuList
->Append(LIST_SHOW_COL_INFO
, wxT("Show &column info\tCtrl-C"));
238 menuList
->Append(LIST_SHOW_SEL_INFO
, wxT("Show &selected items\tCtrl-S"));
239 menuList
->Append(LIST_SHOW_VIEW_RECT
, wxT("Show &view rect"));
240 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
241 menuList
->Append(LIST_SET_COL_ORDER
, wxT("Se&t columns order\tShift-Ctrl-O"));
242 menuList
->Append(LIST_GET_COL_ORDER
, wxT("Sho&w columns order\tCtrl-O"));
243 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
244 menuList
->AppendSeparator();
245 menuList
->Append(LIST_SORT
, wxT("Sor&t\tCtrl-T"));
246 menuList
->Append(LIST_FIND
, "Test Find() performance");
247 menuList
->AppendSeparator();
248 menuList
->Append(LIST_ADD
, wxT("&Append an item\tCtrl-P"));
249 menuList
->Append(LIST_EDIT
, wxT("&Edit the item\tCtrl-E"));
250 menuList
->Append(LIST_DELETE
, wxT("&Delete first item\tCtrl-X"));
251 menuList
->Append(LIST_DELETE_ALL
, wxT("Delete &all items"));
252 menuList
->AppendSeparator();
253 menuList
->Append(LIST_FREEZE
, wxT("Free&ze\tCtrl-Z"));
254 menuList
->Append(LIST_THAW
, wxT("Tha&w\tCtrl-W"));
255 menuList
->AppendSeparator();
256 menuList
->AppendCheckItem(LIST_TOGGLE_LINES
, wxT("Toggle &lines\tCtrl-I"));
257 menuList
->AppendCheckItem(LIST_TOGGLE_MULTI_SEL
,
258 wxT("&Multiple selection\tCtrl-M"));
259 menuList
->Check(LIST_TOGGLE_MULTI_SEL
, true);
260 menuList
->AppendCheckItem(LIST_TOGGLE_HEADER
, "Toggle &header\tCtrl-H");
261 menuList
->Check(LIST_TOGGLE_HEADER
, true);
262 menuList
->AppendCheckItem(LIST_TOGGLE_BELL
, "Toggle &bell on no match");
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
);
371 void MyFrame::OnToggleBell(wxCommandEvent
& event
)
373 m_listCtrl
->EnableBellOnNoMatch(event
.IsChecked());
378 void MyFrame::OnToggleMacUseGeneric(wxCommandEvent
& event
)
380 wxSystemOptions::SetOption(wxT("mac.listctrl.always_use_generic"), event
.IsChecked());
385 void MyFrame::OnGoTo(wxCommandEvent
& WXUNUSED(event
))
388 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
390 long sel
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
391 wxLIST_STATE_SELECTED
);
393 m_listCtrl
->SetItemState(sel
, 0, wxLIST_STATE_SELECTED
);
394 m_listCtrl
->SetItemState(index
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
397 void MyFrame::OnFocusLast(wxCommandEvent
& WXUNUSED(event
))
399 long index
= m_listCtrl
->GetItemCount() - 1;
405 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
406 m_listCtrl
->EnsureVisible(index
);
409 void MyFrame::OnToggleFirstSel(wxCommandEvent
& WXUNUSED(event
))
411 m_listCtrl
->SetItemState(0, (~m_listCtrl
->GetItemState(0, wxLIST_STATE_SELECTED
) ) & wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
414 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
416 if ( !CheckNonVirtual() )
419 int n
= m_listCtrl
->GetItemCount();
420 for (int i
= 0; i
< n
; i
++)
421 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
424 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
426 if ( !CheckNonVirtual() )
429 int n
= m_listCtrl
->GetItemCount();
430 for (int i
= 0; i
< n
; i
++)
431 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
434 // ----------------------------------------------------------------------------
435 // changing listctrl modes
436 // ----------------------------------------------------------------------------
438 void MyFrame::RecreateList(long flags
, bool withText
)
440 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
441 // style, but it is more trouble to do it than not
443 if ( !m_listCtrl
|| ((flags
& wxLC_VIRTUAL
) !=
444 (m_listCtrl
->GetWindowStyleFlag() & wxLC_VIRTUAL
)) )
449 m_listCtrl
= new MyListCtrl(m_panel
, LIST_CTRL
,
450 wxDefaultPosition
, wxDefaultSize
,
452 wxBORDER_THEME
| wxLC_EDIT_LABELS
);
454 switch ( flags
& wxLC_MASK_TYPE
)
461 InitWithIconItems(withText
);
464 case wxLC_SMALL_ICON
:
465 InitWithIconItems(withText
, true);
469 if ( flags
& wxLC_VIRTUAL
)
470 InitWithVirtualItems();
472 InitWithReportItems();
476 wxFAIL_MSG( wxT("unknown listctrl mode") );
479 wxMenuBar
* const mb
= GetMenuBar();
481 m_listCtrl
->EnableBellOnNoMatch(mb
->IsChecked(LIST_TOGGLE_BELL
));
486 m_logWindow
->Clear();
489 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
491 RecreateList(wxLC_LIST
);
494 void MyFrame::InitWithListItems()
496 for ( int i
= 0; i
< m_numListItems
; i
++ )
498 m_listCtrl
->InsertItem(i
, wxString::Format(wxT("Item %d"), i
));
502 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
504 RecreateList(wxLC_REPORT
);
507 void MyFrame::InitWithReportItems()
509 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
511 // note that under MSW for SetColumnWidth() to work we need to create the
512 // items with images initially even if we specify dummy image id
514 itemCol
.SetText(wxT("Column 1"));
515 itemCol
.SetImage(-1);
516 m_listCtrl
->InsertColumn(0, itemCol
);
518 itemCol
.SetText(wxT("Column 2"));
519 itemCol
.SetAlign(wxLIST_FORMAT_CENTRE
);
520 m_listCtrl
->InsertColumn(1, itemCol
);
522 itemCol
.SetText(wxT("Column 3"));
523 itemCol
.SetAlign(wxLIST_FORMAT_RIGHT
);
524 m_listCtrl
->InsertColumn(2, itemCol
);
526 // to speed up inserting we hide the control temporarily
531 for ( int i
= 0; i
< m_numListItems
; i
++ )
533 m_listCtrl
->InsertItemInReportView(i
);
536 m_logWindow
->WriteText(wxString::Format(wxT("%d items inserted in %ldms\n"),
537 m_numListItems
, sw
.Time()));
540 // we leave all mask fields to 0 and only change the colour
543 item
.SetTextColour(*wxRED
);
544 m_listCtrl
->SetItem( item
);
547 item
.SetTextColour(*wxGREEN
);
548 m_listCtrl
->SetItem( item
);
550 item
.SetTextColour(*wxLIGHT_GREY
);
551 item
.SetFont(*wxITALIC_FONT
);
552 item
.SetBackgroundColour(*wxRED
);
553 m_listCtrl
->SetItem( item
);
555 m_listCtrl
->SetTextColour(*wxBLUE
);
557 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
558 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
559 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
561 // Set images in columns
562 m_listCtrl
->SetItemColumnImage(1, 1, 0);
568 m_listCtrl
->SetItem(info
);
570 // test SetItemFont too
571 m_listCtrl
->SetItemFont(0, *wxITALIC_FONT
);
574 void MyFrame::InitWithIconItems(bool withText
, bool sameIcon
)
576 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
577 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
579 for ( int i
= 0; i
< NUM_ICONS
; i
++ )
581 int image
= sameIcon
? 0 : i
;
585 // Make labels of different widths to test the layout.
588 label
.Printf("Longer label %d", i
);
590 label
.Printf("Label %d", i
);
592 m_listCtrl
->InsertItem(i
, label
, image
);
596 m_listCtrl
->InsertItem(i
, image
);
601 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
603 RecreateList(wxLC_ICON
, false);
606 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
608 RecreateList(wxLC_ICON
);
611 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
613 RecreateList(wxLC_SMALL_ICON
, false);
616 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
618 RecreateList(wxLC_SMALL_ICON
);
621 void MyFrame::OnVirtualView(wxCommandEvent
& WXUNUSED(event
))
623 m_smallVirtual
= false;
624 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
627 void MyFrame::OnSmallVirtualView(wxCommandEvent
& WXUNUSED(event
))
629 m_smallVirtual
= true;
630 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
633 void MyFrame::OnSetItemsCount(wxCommandEvent
& WXUNUSED(event
))
635 int numItems
= wxGetNumberFromUser
637 "Enter the initial number of items for "
638 "the list and report views",
640 "wxWidgets wxListCtrl sample",
646 if ( numItems
== -1 || numItems
== m_numListItems
)
649 m_numListItems
= numItems
;
651 if ( m_listCtrl
->HasFlag(wxLC_REPORT
) &&
652 !m_listCtrl
->HasFlag(wxLC_VIRTUAL
) )
653 RecreateList(wxLC_REPORT
);
654 else if ( m_listCtrl
->HasFlag(wxLC_LIST
) )
655 RecreateList(wxLC_LIST
);
658 void MyFrame::InitWithVirtualItems()
660 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
662 if ( m_smallVirtual
)
664 m_listCtrl
->InsertColumn(0, wxT("Animal"));
665 m_listCtrl
->InsertColumn(1, wxT("Sound"));
666 m_listCtrl
->SetItemCount(WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
));
670 m_listCtrl
->InsertColumn(0, wxT("First Column"));
671 m_listCtrl
->InsertColumn(1, wxT("Second Column"));
672 m_listCtrl
->SetColumnWidth(0, 150);
673 m_listCtrl
->SetColumnWidth(1, 150);
674 m_listCtrl
->SetItemCount(1000000);
678 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
682 m_listCtrl
->SortItems(MyCompareFunction
, 0);
684 m_logWindow
->WriteText(wxString::Format(wxT("Sorting %d items took %ld ms\n"),
685 m_listCtrl
->GetItemCount(),
689 void MyFrame::OnFind(wxCommandEvent
& WXUNUSED(event
))
693 const int itemCount
= m_listCtrl
->GetItemCount();
694 for ( int i
= 0; i
< itemCount
; i
++ )
695 m_listCtrl
->FindItem(-1, i
);
697 wxLogMessage("Calling Find() for all %d items took %ld ms",
698 itemCount
, sw
.Time());
701 void MyFrame::OnShowSelInfo(wxCommandEvent
& WXUNUSED(event
))
703 int selCount
= m_listCtrl
->GetSelectedItemCount();
704 wxLogMessage(wxT("%d items selected:"), selCount
);
706 // don't show too many items
707 size_t shownCount
= 0;
709 long item
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
710 wxLIST_STATE_SELECTED
);
713 wxLogMessage(wxT("\t%ld (%s)"),
714 item
, m_listCtrl
->GetItemText(item
).c_str());
716 if ( ++shownCount
> 10 )
718 wxLogMessage(wxT("\t... more selected items snipped..."));
722 item
= m_listCtrl
->GetNextItem(item
, wxLIST_NEXT_ALL
,
723 wxLIST_STATE_SELECTED
);
727 void MyFrame::OnShowViewRect(wxCommandEvent
& WXUNUSED(event
))
729 const wxRect r
= m_listCtrl
->GetViewRect();
730 wxLogMessage("View rect: (%d, %d)-(%d, %d)",
731 r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
734 // ----------------------------------------------------------------------------
735 // column order tests
736 // ----------------------------------------------------------------------------
738 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
740 static wxString
DumpIntArray(const wxArrayInt
& a
)
743 const size_t count
= a
.size();
744 for ( size_t n
= 0; n
< count
; n
++ )
748 s
+= wxString::Format("%lu", (unsigned long)a
[n
]);
756 void MyFrame::OnSetColOrder(wxCommandEvent
& WXUNUSED(event
))
762 if ( m_listCtrl
->SetColumnsOrder(order
) )
764 wxLogMessage("Column order set to %s", DumpIntArray(order
));
768 void MyFrame::OnGetColOrder(wxCommandEvent
& WXUNUSED(event
))
770 // show what GetColumnsOrder() returns
771 const wxArrayInt order
= m_listCtrl
->GetColumnsOrder();
772 wxString msg
= "Columns order: " +
773 DumpIntArray(m_listCtrl
->GetColumnsOrder()) + "\n";
776 const int count
= m_listCtrl
->GetColumnCount();
778 // show the results of GetColumnOrder() for each column
779 msg
+= "GetColumnOrder() results:\n";
780 for ( n
= 0; n
< count
; n
++ )
782 msg
+= wxString::Format(" %2d -> %2d\n",
783 n
, m_listCtrl
->GetColumnOrder(n
));
786 // and the results of GetColumnIndexFromOrder() too
787 msg
+= "GetColumnIndexFromOrder() results:\n";
788 for ( n
= 0; n
< count
; n
++ )
790 msg
+= wxString::Format(" %2d -> %2d\n",
791 n
, m_listCtrl
->GetColumnIndexFromOrder(n
));
794 wxLogMessage("%s", msg
);
797 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
799 void MyFrame::OnShowColInfo(wxCommandEvent
& WXUNUSED(event
))
801 int count
= m_listCtrl
->GetColumnCount();
802 wxLogMessage(wxT("%d columns:"), count
);
803 for ( int c
= 0; c
< count
; c
++ )
805 wxLogMessage(wxT("\tcolumn %d has width %d"), c
,
806 m_listCtrl
->GetColumnWidth(c
));
810 void MyFrame::OnUpdateUIEnableInReport(wxUpdateUIEvent
& event
)
812 event
.Enable( (m_listCtrl
->GetWindowStyleFlag() & wxLC_REPORT
) != 0 );
815 void MyFrame::OnToggleMultiSel(wxCommandEvent
& WXUNUSED(event
))
817 long flags
= m_listCtrl
->GetWindowStyleFlag();
818 if ( flags
& wxLC_SINGLE_SEL
)
819 flags
&= ~wxLC_SINGLE_SEL
;
821 flags
|= wxLC_SINGLE_SEL
;
823 m_logWindow
->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
824 (flags
& wxLC_SINGLE_SEL
) ? wxT("sing") : wxT("multip")));
829 void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent
& event
)
831 event
.Check(!m_listCtrl
->HasFlag(wxLC_SINGLE_SEL
));
834 void MyFrame::OnUpdateToggleHeader(wxUpdateUIEvent
& event
)
836 event
.Check(!m_listCtrl
->HasFlag(wxLC_NO_HEADER
));
839 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
841 m_listCtrl
->SetForegroundColour(wxGetColourFromUser(this));
842 m_listCtrl
->Refresh();
845 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
847 m_listCtrl
->SetBackgroundColour(wxGetColourFromUser(this));
848 m_listCtrl
->Refresh();
851 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
))
853 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), wxT("Appended item"));
856 void MyFrame::OnEdit(wxCommandEvent
& WXUNUSED(event
))
858 // demonstrate cancelling editing: this currently is wxMSW-only
859 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
860 if ( m_listCtrl
->GetEditControl() )
862 m_listCtrl
->EndEditLabel(true);
864 else // start editing
867 long itemCur
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
868 wxLIST_STATE_FOCUSED
);
872 m_listCtrl
->EditLabel(itemCur
);
876 m_logWindow
->WriteText(wxT("No item to edit"));
881 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
883 if ( m_listCtrl
->GetItemCount() )
885 m_listCtrl
->DeleteItem(0);
889 m_logWindow
->WriteText(wxT("Nothing to delete"));
893 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
897 int itemCount
= m_listCtrl
->GetItemCount();
899 m_listCtrl
->DeleteAllItems();
901 m_logWindow
->WriteText(wxString::Format(wxT("Deleting %d items took %ld ms\n"),
907 // ----------------------------------------------------------------------------
909 // ----------------------------------------------------------------------------
911 BEGIN_EVENT_TABLE(MyListCtrl
, wxListCtrl
)
912 EVT_LIST_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnBeginDrag
)
913 EVT_LIST_BEGIN_RDRAG(LIST_CTRL
, MyListCtrl::OnBeginRDrag
)
914 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnBeginLabelEdit
)
915 EVT_LIST_END_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnEndLabelEdit
)
916 EVT_LIST_DELETE_ITEM(LIST_CTRL
, MyListCtrl::OnDeleteItem
)
917 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL
, MyListCtrl::OnDeleteAllItems
)
918 EVT_LIST_ITEM_SELECTED(LIST_CTRL
, MyListCtrl::OnSelected
)
919 EVT_LIST_ITEM_DESELECTED(LIST_CTRL
, MyListCtrl::OnDeselected
)
920 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl::OnListKeyDown
)
921 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl::OnActivated
)
922 EVT_LIST_ITEM_FOCUSED(LIST_CTRL
, MyListCtrl::OnFocused
)
924 EVT_LIST_COL_CLICK(LIST_CTRL
, MyListCtrl::OnColClick
)
925 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL
, MyListCtrl::OnColRightClick
)
926 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnColBeginDrag
)
927 EVT_LIST_COL_DRAGGING(LIST_CTRL
, MyListCtrl::OnColDragging
)
928 EVT_LIST_COL_END_DRAG(LIST_CTRL
, MyListCtrl::OnColEndDrag
)
930 EVT_LIST_CACHE_HINT(LIST_CTRL
, MyListCtrl::OnCacheHint
)
933 EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu
)
935 EVT_CHAR(MyListCtrl::OnChar
)
937 EVT_RIGHT_DOWN(MyListCtrl::OnRightClick
)
940 void MyListCtrl::OnCacheHint(wxListEvent
& event
)
942 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
943 event
.GetCacheFrom(), event
.GetCacheTo() );
946 void MyListCtrl::SetColumnImage(int col
, int image
)
949 item
.SetMask(wxLIST_MASK_IMAGE
);
950 item
.SetImage(image
);
951 SetColumn(col
, item
);
954 void MyListCtrl::OnColClick(wxListEvent
& event
)
956 int col
= event
.GetColumn();
958 // set or unset image
959 static bool x
= false;
961 SetColumnImage(col
, x
? 0 : -1);
963 wxLogMessage( wxT("OnColumnClick at %d."), col
);
966 void MyListCtrl::OnColRightClick(wxListEvent
& event
)
968 int col
= event
.GetColumn();
971 SetColumnImage(col
, -1);
974 // Show popupmenu at position
975 wxMenu
menu(wxT("Test"));
976 menu
.Append(LIST_ABOUT
, wxT("&About"));
977 PopupMenu(&menu
, event
.GetPoint());
979 wxLogMessage( wxT("OnColumnRightClick at %d."), event
.GetColumn() );
982 void MyListCtrl::LogColEvent(const wxListEvent
& event
, const wxChar
*name
)
984 const int col
= event
.GetColumn();
986 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
989 event
.GetItem().GetWidth(),
990 GetColumnWidth(col
));
993 void MyListCtrl::OnColBeginDrag(wxListEvent
& event
)
995 LogColEvent( event
, wxT("OnColBeginDrag") );
997 if ( event
.GetColumn() == 0 )
999 wxLogMessage(wxT("Resizing this column shouldn't work."));
1005 void MyListCtrl::OnColDragging(wxListEvent
& event
)
1007 LogColEvent( event
, wxT("OnColDragging") );
1010 void MyListCtrl::OnColEndDrag(wxListEvent
& event
)
1012 LogColEvent( event
, wxT("OnColEndDrag") );
1015 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
1017 const wxPoint
& pt
= event
.m_pointDrag
;
1020 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
1021 pt
.x
, pt
.y
, HitTest(pt
, flags
) );
1024 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
1026 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
1027 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
1030 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
1032 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event
.m_item
.m_text
.c_str());
1034 wxTextCtrl
* const text
= GetEditControl();
1037 wxLogMessage("BUG: started to edit but no edit control");
1041 wxLogMessage("Edit control value: \"%s\"", text
->GetValue());
1045 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
1047 wxLogMessage( wxT("OnEndLabelEdit: %s"),
1049 event
.IsEditCancelled() ?
1050 wxString("[cancelled]") :
1056 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
1058 LogEvent(event
, wxT("OnDeleteItem"));
1059 wxLogMessage( wxT("Number of items when delete event is sent: %d"), GetItemCount() );
1062 void MyListCtrl::OnDeleteAllItems(wxListEvent
& event
)
1064 LogEvent(event
, wxT("OnDeleteAllItems"));
1067 void MyListCtrl::OnSelected(wxListEvent
& event
)
1069 LogEvent(event
, wxT("OnSelected"));
1071 if ( GetWindowStyle() & wxLC_REPORT
)
1074 info
.m_itemId
= event
.m_itemIndex
;
1076 info
.m_mask
= wxLIST_MASK_TEXT
;
1077 if ( GetItem(info
) )
1079 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
1080 info
.m_text
.c_str());
1084 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
1089 void MyListCtrl::OnDeselected(wxListEvent
& event
)
1091 LogEvent(event
, wxT("OnDeselected"));
1094 void MyListCtrl::OnActivated(wxListEvent
& event
)
1096 LogEvent(event
, wxT("OnActivated"));
1099 void MyListCtrl::OnFocused(wxListEvent
& event
)
1101 LogEvent(event
, wxT("OnFocused"));
1106 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
1110 if ( !wxGetKeyState(WXK_SHIFT
) )
1112 LogEvent(event
, wxT("OnListKeyDown"));
1117 switch ( event
.GetKeyCode() )
1119 case 'C': // colorize
1122 info
.m_itemId
= event
.GetIndex();
1123 if ( info
.m_itemId
== -1 )
1131 wxListItemAttr
*attr
= info
.GetAttributes();
1132 if ( !attr
|| !attr
->HasTextColour() )
1134 info
.SetTextColour(*wxCYAN
);
1138 RefreshItem(info
.m_itemId
);
1144 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
1145 if ( item
++ == GetItemCount() - 1 )
1150 wxLogMessage(wxT("Focusing item %ld"), item
);
1152 SetItemState(item
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
1153 EnsureVisible(item
);
1156 case 'R': // show bounding rectangle
1158 item
= event
.GetIndex();
1160 if ( !GetItemRect(item
, r
) )
1162 wxLogError(wxT("Failed to retrieve rect of item %ld"), item
);
1166 wxLogMessage(wxT("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
1167 item
, r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
1171 case '1': // show sub item bounding rectangle for the given column
1172 case '2': // (and icon/label rectangle if Shift/Ctrl is pressed)
1174 case '4': // this column is invalid but we want to test it too
1175 if ( InReportView() )
1177 int subItem
= event
.GetKeyCode() - '1';
1178 item
= event
.GetIndex();
1181 int code
= wxLIST_RECT_BOUNDS
;
1182 if ( wxGetKeyState(WXK_SHIFT
) )
1183 code
= wxLIST_RECT_ICON
;
1184 else if ( wxGetKeyState(WXK_CONTROL
) )
1185 code
= wxLIST_RECT_LABEL
;
1187 if ( !GetSubItemRect(item
, subItem
, r
, code
) )
1189 wxLogError(wxT("Failed to retrieve rect of item %ld column %d"), item
, subItem
+ 1);
1193 wxLogMessage(wxT("Bounding rect of item %ld column %d is (%d, %d)-(%d, %d)"),
1195 r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
1203 if ( m_updated
!= -1 )
1204 RefreshItem(m_updated
);
1206 m_updated
= event
.GetIndex();
1207 if ( m_updated
!= -1 )
1209 // we won't see changes to this item as it's selected, update
1210 // the next one (or the first one if we're on the last item)
1211 if ( ++m_updated
== GetItemCount() )
1214 wxLogMessage("Updating colour of the item %ld", m_updated
);
1215 RefreshItem(m_updated
);
1220 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1221 while ( item
!= -1 )
1225 wxLogMessage(wxT("Item %ld deleted"), item
);
1227 // -1 because the indices were shifted by DeleteItem()
1228 item
= GetNextItem(item
- 1,
1229 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1234 if ( GetWindowStyle() & wxLC_REPORT
)
1236 if ( GetWindowStyle() & wxLC_VIRTUAL
)
1238 SetItemCount(GetItemCount() + 1);
1242 InsertItemInReportView(event
.GetIndex());
1245 //else: fall through
1248 LogEvent(event
, wxT("OnListKeyDown"));
1254 void MyListCtrl::OnChar(wxKeyEvent
& event
)
1256 wxLogMessage(wxT("Got char event."));
1261 void MyListCtrl::OnRightClick(wxMouseEvent
& event
)
1263 if ( !event
.ControlDown() )
1271 long item
= HitTest(event
.GetPosition(), flags
, &subitem
);
1276 case wxLIST_HITTEST_ABOVE
: where
= wxT("above"); break;
1277 case wxLIST_HITTEST_BELOW
: where
= wxT("below"); break;
1278 case wxLIST_HITTEST_NOWHERE
: where
= wxT("nowhere near"); break;
1279 case wxLIST_HITTEST_ONITEMICON
: where
= wxT("on icon of"); break;
1280 case wxLIST_HITTEST_ONITEMLABEL
: where
= wxT("on label of"); break;
1281 case wxLIST_HITTEST_ONITEMRIGHT
: where
= wxT("right on"); break;
1282 case wxLIST_HITTEST_TOLEFT
: where
= wxT("to the left of"); break;
1283 case wxLIST_HITTEST_TORIGHT
: where
= wxT("to the right of"); break;
1284 default: where
= wxT("not clear exactly where on"); break;
1287 wxLogMessage(wxT("Right double click %s item %ld, subitem %ld"),
1288 where
.c_str(), item
, subitem
);
1291 void MyListCtrl::LogEvent(const wxListEvent
& event
, const wxChar
*eventName
)
1293 wxLogMessage(wxT("Item %ld: %s (item text = %s, data = %ld)"),
1294 event
.GetIndex(), eventName
,
1295 event
.GetText().c_str(), event
.GetData());
1298 wxString
MyListCtrl::OnGetItemText(long item
, long column
) const
1300 if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
) )
1302 return SMALL_VIRTUAL_VIEW_ITEMS
[item
][column
];
1304 else // "big" virtual control
1306 return wxString::Format(wxT("Column %ld of item %ld"), column
, item
);
1310 int MyListCtrl::OnGetItemColumnImage(long item
, long column
) const
1315 if (!(item
% 3) && column
== 1)
1321 wxListItemAttr
*MyListCtrl::OnGetItemAttr(long item
) const
1323 // test to check that RefreshItem() works correctly: when m_updated is
1324 // set to some item and it is refreshed, we highlight the item
1325 if ( item
== m_updated
)
1327 static wxListItemAttr
s_attrHighlight(*wxRED
, wxNullColour
, wxNullFont
);
1328 return &s_attrHighlight
;
1331 return item
% 2 ? NULL
: (wxListItemAttr
*)&m_attr
;
1334 void MyListCtrl::InsertItemInReportView(int i
)
1337 buf
.Printf(wxT("This is item %d"), i
);
1338 long tmp
= InsertItem(i
, buf
, 0);
1339 SetItemData(tmp
, i
);
1341 buf
.Printf(wxT("Col 1, item %d"), i
);
1342 SetItem(tmp
, 1, buf
);
1344 buf
.Printf(wxT("Item %d in column 2"), i
);
1345 SetItem(tmp
, 2, buf
);
1348 #if USE_CONTEXT_MENU
1349 void MyListCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1351 if (GetEditControl() == NULL
)
1353 wxPoint point
= event
.GetPosition();
1355 if ( (point
.x
== -1) && (point
.y
== -1) )
1357 wxSize size
= GetSize();
1358 point
.x
= size
.x
/ 2;
1359 point
.y
= size
.y
/ 2;
1363 point
= ScreenToClient(point
);
1365 ShowContextMenu(point
);
1369 // the user is editing:
1370 // allow the text control to display its context menu
1371 // if it has one (it has on Windows) rather than display our one
1377 void MyListCtrl::ShowContextMenu(const wxPoint
& pos
)
1381 menu
.Append(wxID_ABOUT
, wxT("&About"));
1382 menu
.AppendSeparator();
1383 menu
.Append(wxID_EXIT
, wxT("E&xit"));
1385 PopupMenu(&menu
, pos
.x
, pos
.y
);