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 #if !defined(__WXMSW__) && !defined(__WXPM__)
24 #include "../sample.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(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
)
153 EVT_MENU(LIST_MAC_USE_GENERIC
, MyFrame::OnToggleMacUseGeneric
)
155 EVT_MENU(LIST_FIND
, MyFrame::OnFind
)
157 EVT_UPDATE_UI(LIST_SHOW_COL_INFO
, MyFrame::OnUpdateUIEnableInReport
)
158 EVT_UPDATE_UI(LIST_TOGGLE_HEADER
, MyFrame::OnUpdateUIEnableInReport
)
160 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnUpdateToggleMultiSel
)
161 EVT_UPDATE_UI(LIST_TOGGLE_HEADER
, MyFrame::OnUpdateToggleHeader
)
164 // My frame constructor
165 MyFrame::MyFrame(const wxChar
*title
)
166 : wxFrame(NULL
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(600, 500))
170 m_smallVirtual
= false;
174 SetIcon(wxICON(sample
));
176 // Make an image list containing large icons
177 m_imageListNormal
= new wxImageList(32, 32, true);
178 m_imageListSmall
= new wxImageList(16, 16, true);
181 m_imageListNormal
->Add( wxIcon(wxT("icon1"), wxBITMAP_TYPE_ICO_RESOURCE
) );
182 m_imageListNormal
->Add( wxIcon(wxT("icon2"), wxBITMAP_TYPE_ICO_RESOURCE
) );
183 m_imageListNormal
->Add( wxIcon(wxT("icon3"), wxBITMAP_TYPE_ICO_RESOURCE
) );
184 m_imageListNormal
->Add( wxIcon(wxT("icon4"), wxBITMAP_TYPE_ICO_RESOURCE
) );
185 m_imageListNormal
->Add( wxIcon(wxT("icon5"), wxBITMAP_TYPE_ICO_RESOURCE
) );
186 m_imageListNormal
->Add( wxIcon(wxT("icon6"), wxBITMAP_TYPE_ICO_RESOURCE
) );
187 m_imageListNormal
->Add( wxIcon(wxT("icon7"), wxBITMAP_TYPE_ICO_RESOURCE
) );
188 m_imageListNormal
->Add( wxIcon(wxT("icon8"), wxBITMAP_TYPE_ICO_RESOURCE
) );
189 m_imageListNormal
->Add( wxIcon(wxT("icon9"), wxBITMAP_TYPE_ICO_RESOURCE
) );
191 m_imageListSmall
->Add( wxIcon(wxT("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE
) );
194 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
195 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
196 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
197 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
198 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
199 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
200 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
201 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
202 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
204 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
208 wxMenu
*menuFile
= new wxMenu
;
209 menuFile
->Append(LIST_ABOUT
, wxT("&About"));
210 menuFile
->AppendSeparator();
211 menuFile
->Append(LIST_QUIT
, wxT("E&xit\tAlt-X"));
213 wxMenu
*menuView
= new wxMenu
;
214 menuView
->Append(LIST_LIST_VIEW
, wxT("&List view\tF1"));
215 menuView
->Append(LIST_REPORT_VIEW
, wxT("&Report view\tF2"));
216 menuView
->Append(LIST_ICON_VIEW
, wxT("&Icon view\tF3"));
217 menuView
->Append(LIST_ICON_TEXT_VIEW
, wxT("Icon view with &text\tF4"));
218 menuView
->Append(LIST_SMALL_ICON_VIEW
, wxT("&Small icon view\tF5"));
219 menuView
->Append(LIST_SMALL_ICON_TEXT_VIEW
, wxT("Small icon &view with text\tF6"));
220 menuView
->Append(LIST_VIRTUAL_VIEW
, wxT("&Virtual view\tF7"));
221 menuView
->Append(LIST_SMALL_VIRTUAL_VIEW
, wxT("Small virtual vie&w\tF8"));
222 menuView
->AppendSeparator();
223 menuView
->Append(LIST_SET_ITEMS_COUNT
, "Set &number of items");
225 menuView
->AppendSeparator();
226 menuView
->AppendCheckItem(LIST_MAC_USE_GENERIC
, wxT("Mac: Use Generic Control"));
229 wxMenu
*menuList
= new wxMenu
;
230 menuList
->Append(LIST_GOTO
, wxT("&Go to item #3\tCtrl-3"));
231 menuList
->Append(LIST_FOCUS_LAST
, wxT("&Make last item current\tCtrl-L"));
232 menuList
->Append(LIST_TOGGLE_FIRST
, wxT("To&ggle first item\tCtrl-G"));
233 menuList
->Append(LIST_DESELECT_ALL
, wxT("&Deselect All\tCtrl-D"));
234 menuList
->Append(LIST_SELECT_ALL
, wxT("S&elect All\tCtrl-A"));
235 menuList
->AppendSeparator();
236 menuList
->Append(LIST_SHOW_COL_INFO
, wxT("Show &column info\tCtrl-C"));
237 menuList
->Append(LIST_SHOW_SEL_INFO
, wxT("Show &selected items\tCtrl-S"));
238 menuList
->Append(LIST_SHOW_VIEW_RECT
, wxT("Show &view rect"));
239 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
240 menuList
->Append(LIST_SET_COL_ORDER
, wxT("Se&t columns order\tShift-Ctrl-O"));
241 menuList
->Append(LIST_GET_COL_ORDER
, wxT("Sho&w columns order\tCtrl-O"));
242 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
243 menuList
->AppendSeparator();
244 menuList
->Append(LIST_SORT
, wxT("Sor&t\tCtrl-T"));
245 menuList
->Append(LIST_FIND
, "Test Find() performance");
246 menuList
->AppendSeparator();
247 menuList
->Append(LIST_ADD
, wxT("&Append an item\tCtrl-P"));
248 menuList
->Append(LIST_EDIT
, wxT("&Edit the item\tCtrl-E"));
249 menuList
->Append(LIST_DELETE
, wxT("&Delete first item\tCtrl-X"));
250 menuList
->Append(LIST_DELETE_ALL
, wxT("Delete &all items"));
251 menuList
->AppendSeparator();
252 menuList
->Append(LIST_FREEZE
, wxT("Free&ze\tCtrl-Z"));
253 menuList
->Append(LIST_THAW
, wxT("Tha&w\tCtrl-W"));
254 menuList
->AppendSeparator();
255 menuList
->AppendCheckItem(LIST_TOGGLE_LINES
, wxT("Toggle &lines\tCtrl-I"));
256 menuList
->AppendCheckItem(LIST_TOGGLE_MULTI_SEL
,
257 wxT("&Multiple selection\tCtrl-M"));
258 menuList
->Check(LIST_TOGGLE_MULTI_SEL
, true);
259 menuList
->AppendCheckItem(LIST_TOGGLE_HEADER
, "Toggle &header\tCtrl-H");
260 menuList
->Check(LIST_TOGGLE_HEADER
, true);
262 wxMenu
*menuCol
= new wxMenu
;
263 menuCol
->Append(LIST_SET_FG_COL
, wxT("&Foreground colour..."));
264 menuCol
->Append(LIST_SET_BG_COL
, wxT("&Background colour..."));
266 wxMenuBar
*menubar
= new wxMenuBar
;
267 menubar
->Append(menuFile
, wxT("&File"));
268 menubar
->Append(menuView
, wxT("&View"));
269 menubar
->Append(menuList
, wxT("&List"));
270 menubar
->Append(menuCol
, wxT("&Colour"));
273 m_panel
= new wxPanel(this, wxID_ANY
);
274 m_logWindow
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
,
275 wxDefaultPosition
, wxDefaultSize
,
276 wxTE_READONLY
| wxTE_MULTILINE
| wxSUNKEN_BORDER
);
278 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
280 RecreateList(wxLC_REPORT
| wxLC_SINGLE_SEL
);
283 // this is useful to know specially when debugging :)
284 wxLogMessage("Your version of comctl32.dll is: %d",
285 wxApp::GetComCtl32Version());
290 #endif // wxUSE_STATUSBAR
295 delete wxLog::SetActiveTarget(m_logOld
);
297 delete m_imageListNormal
;
298 delete m_imageListSmall
;
301 void MyFrame::OnSize(wxSizeEvent
& event
)
308 void MyFrame::DoSize()
313 wxSize size
= GetClientSize();
314 wxCoord y
= (2*size
.y
)/3;
315 m_listCtrl
->SetSize(0, 0, size
.x
, y
);
316 m_logWindow
->SetSize(0, y
+ 1, size
.x
, size
.y
- y
-1);
319 bool MyFrame::CheckNonVirtual() const
321 if ( !m_listCtrl
->HasFlag(wxLC_VIRTUAL
) )
324 // "this" == whatever
325 wxLogWarning(wxT("Can't do this in virtual view, sorry."));
330 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
335 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
337 wxMessageDialog
dialog(this, wxT("List test sample\nJulian Smart (c) 1997"),
338 wxT("About list test"), wxOK
|wxCANCEL
);
343 void MyFrame::OnFreeze(wxCommandEvent
& WXUNUSED(event
))
345 wxLogMessage(wxT("Freezing the control"));
347 m_listCtrl
->Freeze();
350 void MyFrame::OnThaw(wxCommandEvent
& WXUNUSED(event
))
352 wxLogMessage(wxT("Thawing the control"));
357 void MyFrame::OnToggleLines(wxCommandEvent
& event
)
359 m_listCtrl
->SetSingleStyle(wxLC_HRULES
| wxLC_VRULES
, event
.IsChecked());
362 void MyFrame::OnToggleHeader(wxCommandEvent
& event
)
364 wxLogMessage("%s the header", event
.IsChecked() ? "Showing" : "Hiding");
366 m_listCtrl
->ToggleWindowStyle(wxLC_NO_HEADER
);
371 void MyFrame::OnToggleMacUseGeneric(wxCommandEvent
& event
)
373 wxSystemOptions::SetOption(wxT("mac.listctrl.always_use_generic"), event
.IsChecked());
378 void MyFrame::OnGoTo(wxCommandEvent
& WXUNUSED(event
))
381 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
383 long sel
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
384 wxLIST_STATE_SELECTED
);
386 m_listCtrl
->SetItemState(sel
, 0, wxLIST_STATE_SELECTED
);
387 m_listCtrl
->SetItemState(index
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
390 void MyFrame::OnFocusLast(wxCommandEvent
& WXUNUSED(event
))
392 long index
= m_listCtrl
->GetItemCount() - 1;
398 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
399 m_listCtrl
->EnsureVisible(index
);
402 void MyFrame::OnToggleFirstSel(wxCommandEvent
& WXUNUSED(event
))
404 m_listCtrl
->SetItemState(0, (~m_listCtrl
->GetItemState(0, wxLIST_STATE_SELECTED
) ) & wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
407 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
409 if ( !CheckNonVirtual() )
412 int n
= m_listCtrl
->GetItemCount();
413 for (int i
= 0; i
< n
; i
++)
414 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
417 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
419 if ( !CheckNonVirtual() )
422 int n
= m_listCtrl
->GetItemCount();
423 for (int i
= 0; i
< n
; i
++)
424 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
427 // ----------------------------------------------------------------------------
428 // changing listctrl modes
429 // ----------------------------------------------------------------------------
431 void MyFrame::RecreateList(long flags
, bool withText
)
433 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
434 // style, but it is more trouble to do it than not
436 if ( !m_listCtrl
|| ((flags
& wxLC_VIRTUAL
) !=
437 (m_listCtrl
->GetWindowStyleFlag() & wxLC_VIRTUAL
)) )
442 m_listCtrl
= new MyListCtrl(m_panel
, LIST_CTRL
,
443 wxDefaultPosition
, wxDefaultSize
,
445 wxBORDER_THEME
| wxLC_EDIT_LABELS
);
447 switch ( flags
& wxLC_MASK_TYPE
)
454 InitWithIconItems(withText
);
457 case wxLC_SMALL_ICON
:
458 InitWithIconItems(withText
, true);
462 if ( flags
& wxLC_VIRTUAL
)
463 InitWithVirtualItems();
465 InitWithReportItems();
469 wxFAIL_MSG( wxT("unknown listctrl mode") );
475 m_logWindow
->Clear();
478 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
480 RecreateList(wxLC_LIST
);
483 void MyFrame::InitWithListItems()
485 for ( int i
= 0; i
< m_numListItems
; i
++ )
487 m_listCtrl
->InsertItem(i
, wxString::Format(wxT("Item %d"), i
));
491 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
493 RecreateList(wxLC_REPORT
);
496 void MyFrame::InitWithReportItems()
498 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
500 // note that under MSW for SetColumnWidth() to work we need to create the
501 // items with images initially even if we specify dummy image id
503 itemCol
.SetText(wxT("Column 1"));
504 itemCol
.SetImage(-1);
505 m_listCtrl
->InsertColumn(0, itemCol
);
507 itemCol
.SetText(wxT("Column 2"));
508 itemCol
.SetAlign(wxLIST_FORMAT_CENTRE
);
509 m_listCtrl
->InsertColumn(1, itemCol
);
511 itemCol
.SetText(wxT("Column 3"));
512 itemCol
.SetAlign(wxLIST_FORMAT_RIGHT
);
513 m_listCtrl
->InsertColumn(2, itemCol
);
515 // to speed up inserting we hide the control temporarily
520 for ( int i
= 0; i
< m_numListItems
; i
++ )
522 m_listCtrl
->InsertItemInReportView(i
);
525 m_logWindow
->WriteText(wxString::Format(wxT("%d items inserted in %ldms\n"),
526 m_numListItems
, sw
.Time()));
529 // we leave all mask fields to 0 and only change the colour
532 item
.SetTextColour(*wxRED
);
533 m_listCtrl
->SetItem( item
);
536 item
.SetTextColour(*wxGREEN
);
537 m_listCtrl
->SetItem( item
);
539 item
.SetTextColour(*wxLIGHT_GREY
);
540 item
.SetFont(*wxITALIC_FONT
);
541 item
.SetBackgroundColour(*wxRED
);
542 m_listCtrl
->SetItem( item
);
544 m_listCtrl
->SetTextColour(*wxBLUE
);
546 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
547 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
548 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
550 // Set images in columns
551 m_listCtrl
->SetItemColumnImage(1, 1, 0);
557 m_listCtrl
->SetItem(info
);
559 // test SetItemFont too
560 m_listCtrl
->SetItemFont(0, *wxITALIC_FONT
);
563 void MyFrame::InitWithIconItems(bool withText
, bool sameIcon
)
565 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
566 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
568 for ( int i
= 0; i
< NUM_ICONS
; i
++ )
570 int image
= sameIcon
? 0 : i
;
574 // Make labels of different widths to test the layout.
577 label
.Printf("Longer label %d", i
);
579 label
.Printf("Label %d", i
);
581 m_listCtrl
->InsertItem(i
, label
, image
);
585 m_listCtrl
->InsertItem(i
, image
);
590 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
592 RecreateList(wxLC_ICON
, false);
595 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
597 RecreateList(wxLC_ICON
);
600 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
602 RecreateList(wxLC_SMALL_ICON
, false);
605 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
607 RecreateList(wxLC_SMALL_ICON
);
610 void MyFrame::OnVirtualView(wxCommandEvent
& WXUNUSED(event
))
612 m_smallVirtual
= false;
613 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
616 void MyFrame::OnSmallVirtualView(wxCommandEvent
& WXUNUSED(event
))
618 m_smallVirtual
= true;
619 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
622 void MyFrame::OnSetItemsCount(wxCommandEvent
& WXUNUSED(event
))
624 int numItems
= wxGetNumberFromUser
626 "Enter the initial number of items for "
627 "the list and report views",
629 "wxWidgets wxListCtrl sample",
635 if ( numItems
== -1 || numItems
== m_numListItems
)
638 m_numListItems
= numItems
;
640 if ( m_listCtrl
->HasFlag(wxLC_REPORT
) &&
641 !m_listCtrl
->HasFlag(wxLC_VIRTUAL
) )
642 RecreateList(wxLC_REPORT
);
643 else if ( m_listCtrl
->HasFlag(wxLC_LIST
) )
644 RecreateList(wxLC_LIST
);
647 void MyFrame::InitWithVirtualItems()
649 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
651 if ( m_smallVirtual
)
653 m_listCtrl
->InsertColumn(0, wxT("Animal"));
654 m_listCtrl
->InsertColumn(1, wxT("Sound"));
655 m_listCtrl
->SetItemCount(WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
));
659 m_listCtrl
->InsertColumn(0, wxT("First Column"));
660 m_listCtrl
->InsertColumn(1, wxT("Second Column"));
661 m_listCtrl
->SetColumnWidth(0, 150);
662 m_listCtrl
->SetColumnWidth(1, 150);
663 m_listCtrl
->SetItemCount(1000000);
667 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
671 m_listCtrl
->SortItems(MyCompareFunction
, 0);
673 m_logWindow
->WriteText(wxString::Format(wxT("Sorting %d items took %ld ms\n"),
674 m_listCtrl
->GetItemCount(),
678 void MyFrame::OnFind(wxCommandEvent
& WXUNUSED(event
))
682 const int itemCount
= m_listCtrl
->GetItemCount();
683 for ( int i
= 0; i
< itemCount
; i
++ )
684 m_listCtrl
->FindItem(-1, i
);
686 wxLogMessage("Calling Find() for all %d items took %ld ms",
687 itemCount
, sw
.Time());
690 void MyFrame::OnShowSelInfo(wxCommandEvent
& WXUNUSED(event
))
692 int selCount
= m_listCtrl
->GetSelectedItemCount();
693 wxLogMessage(wxT("%d items selected:"), selCount
);
695 // don't show too many items
696 size_t shownCount
= 0;
698 long item
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
699 wxLIST_STATE_SELECTED
);
702 wxLogMessage(wxT("\t%ld (%s)"),
703 item
, m_listCtrl
->GetItemText(item
).c_str());
705 if ( ++shownCount
> 10 )
707 wxLogMessage(wxT("\t... more selected items snipped..."));
711 item
= m_listCtrl
->GetNextItem(item
, wxLIST_NEXT_ALL
,
712 wxLIST_STATE_SELECTED
);
716 void MyFrame::OnShowViewRect(wxCommandEvent
& WXUNUSED(event
))
718 const wxRect r
= m_listCtrl
->GetViewRect();
719 wxLogMessage("View rect: (%d, %d)-(%d, %d)",
720 r
.GetLeft(), r
.GetTop(), r
.GetRight(), r
.GetBottom());
723 // ----------------------------------------------------------------------------
724 // column order tests
725 // ----------------------------------------------------------------------------
727 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
729 static wxString
DumpIntArray(const wxArrayInt
& a
)
732 const size_t count
= a
.size();
733 for ( size_t n
= 0; n
< count
; n
++ )
737 s
+= wxString::Format("%lu", (unsigned long)a
[n
]);
745 void MyFrame::OnSetColOrder(wxCommandEvent
& WXUNUSED(event
))
751 if ( m_listCtrl
->SetColumnsOrder(order
) )
753 wxLogMessage("Column order set to %s", DumpIntArray(order
));
757 void MyFrame::OnGetColOrder(wxCommandEvent
& WXUNUSED(event
))
759 // show what GetColumnsOrder() returns
760 const wxArrayInt order
= m_listCtrl
->GetColumnsOrder();
761 wxString msg
= "Columns order: " +
762 DumpIntArray(m_listCtrl
->GetColumnsOrder()) + "\n";
765 const int count
= m_listCtrl
->GetColumnCount();
767 // show the results of GetColumnOrder() for each column
768 msg
+= "GetColumnOrder() results:\n";
769 for ( n
= 0; n
< count
; n
++ )
771 msg
+= wxString::Format(" %2d -> %2d\n",
772 n
, m_listCtrl
->GetColumnOrder(n
));
775 // and the results of GetColumnIndexFromOrder() too
776 msg
+= "GetColumnIndexFromOrder() results:\n";
777 for ( n
= 0; n
< count
; n
++ )
779 msg
+= wxString::Format(" %2d -> %2d\n",
780 n
, m_listCtrl
->GetColumnIndexFromOrder(n
));
783 wxLogMessage("%s", msg
);
786 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
788 void MyFrame::OnShowColInfo(wxCommandEvent
& WXUNUSED(event
))
790 int count
= m_listCtrl
->GetColumnCount();
791 wxLogMessage(wxT("%d columns:"), count
);
792 for ( int c
= 0; c
< count
; c
++ )
794 wxLogMessage(wxT("\tcolumn %d has width %d"), c
,
795 m_listCtrl
->GetColumnWidth(c
));
799 void MyFrame::OnUpdateUIEnableInReport(wxUpdateUIEvent
& event
)
801 event
.Enable( (m_listCtrl
->GetWindowStyleFlag() & wxLC_REPORT
) != 0 );
804 void MyFrame::OnToggleMultiSel(wxCommandEvent
& WXUNUSED(event
))
806 long flags
= m_listCtrl
->GetWindowStyleFlag();
807 if ( flags
& wxLC_SINGLE_SEL
)
808 flags
&= ~wxLC_SINGLE_SEL
;
810 flags
|= wxLC_SINGLE_SEL
;
812 m_logWindow
->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
813 (flags
& wxLC_SINGLE_SEL
) ? wxT("sing") : wxT("multip")));
818 void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent
& event
)
820 event
.Check(!m_listCtrl
->HasFlag(wxLC_SINGLE_SEL
));
823 void MyFrame::OnUpdateToggleHeader(wxUpdateUIEvent
& event
)
825 event
.Check(!m_listCtrl
->HasFlag(wxLC_NO_HEADER
));
828 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
830 m_listCtrl
->SetForegroundColour(wxGetColourFromUser(this));
831 m_listCtrl
->Refresh();
834 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
836 m_listCtrl
->SetBackgroundColour(wxGetColourFromUser(this));
837 m_listCtrl
->Refresh();
840 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
))
842 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), wxT("Appended item"));
845 void MyFrame::OnEdit(wxCommandEvent
& WXUNUSED(event
))
847 // demonstrate cancelling editing: this currently is wxMSW-only
849 if ( m_listCtrl
->GetEditControl() )
851 m_listCtrl
->EndEditLabel(true);
853 else // start editing
856 long itemCur
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
857 wxLIST_STATE_FOCUSED
);
861 m_listCtrl
->EditLabel(itemCur
);
865 m_logWindow
->WriteText(wxT("No item to edit"));
870 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
872 if ( m_listCtrl
->GetItemCount() )
874 m_listCtrl
->DeleteItem(0);
878 m_logWindow
->WriteText(wxT("Nothing to delete"));
882 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
886 int itemCount
= m_listCtrl
->GetItemCount();
888 m_listCtrl
->DeleteAllItems();
890 m_logWindow
->WriteText(wxString::Format(wxT("Deleting %d items took %ld ms\n"),
896 // ----------------------------------------------------------------------------
898 // ----------------------------------------------------------------------------
900 BEGIN_EVENT_TABLE(MyListCtrl
, wxListCtrl
)
901 EVT_LIST_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnBeginDrag
)
902 EVT_LIST_BEGIN_RDRAG(LIST_CTRL
, MyListCtrl::OnBeginRDrag
)
903 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnBeginLabelEdit
)
904 EVT_LIST_END_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnEndLabelEdit
)
905 EVT_LIST_DELETE_ITEM(LIST_CTRL
, MyListCtrl::OnDeleteItem
)
906 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL
, MyListCtrl::OnDeleteAllItems
)
907 EVT_LIST_ITEM_SELECTED(LIST_CTRL
, MyListCtrl::OnSelected
)
908 EVT_LIST_ITEM_DESELECTED(LIST_CTRL
, MyListCtrl::OnDeselected
)
909 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl::OnListKeyDown
)
910 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl::OnActivated
)
911 EVT_LIST_ITEM_FOCUSED(LIST_CTRL
, MyListCtrl::OnFocused
)
913 EVT_LIST_COL_CLICK(LIST_CTRL
, MyListCtrl::OnColClick
)
914 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL
, MyListCtrl::OnColRightClick
)
915 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnColBeginDrag
)
916 EVT_LIST_COL_DRAGGING(LIST_CTRL
, MyListCtrl::OnColDragging
)
917 EVT_LIST_COL_END_DRAG(LIST_CTRL
, MyListCtrl::OnColEndDrag
)
919 EVT_LIST_CACHE_HINT(LIST_CTRL
, MyListCtrl::OnCacheHint
)
922 EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu
)
924 EVT_CHAR(MyListCtrl::OnChar
)
926 EVT_RIGHT_DOWN(MyListCtrl::OnRightClick
)
929 void MyListCtrl::OnCacheHint(wxListEvent
& event
)
931 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
932 event
.GetCacheFrom(), event
.GetCacheTo() );
935 void MyListCtrl::SetColumnImage(int col
, int image
)
938 item
.SetMask(wxLIST_MASK_IMAGE
);
939 item
.SetImage(image
);
940 SetColumn(col
, item
);
943 void MyListCtrl::OnColClick(wxListEvent
& event
)
945 int col
= event
.GetColumn();
947 // set or unset image
948 static bool x
= false;
950 SetColumnImage(col
, x
? 0 : -1);
952 wxLogMessage( wxT("OnColumnClick at %d."), col
);
955 void MyListCtrl::OnColRightClick(wxListEvent
& event
)
957 int col
= event
.GetColumn();
960 SetColumnImage(col
, -1);
963 // Show popupmenu at position
964 wxMenu
menu(wxT("Test"));
965 menu
.Append(LIST_ABOUT
, wxT("&About"));
966 PopupMenu(&menu
, event
.GetPoint());
968 wxLogMessage( wxT("OnColumnRightClick at %d."), event
.GetColumn() );
971 void MyListCtrl::LogColEvent(const wxListEvent
& event
, const wxChar
*name
)
973 const int col
= event
.GetColumn();
975 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
978 event
.GetItem().GetWidth(),
979 GetColumnWidth(col
));
982 void MyListCtrl::OnColBeginDrag(wxListEvent
& event
)
984 LogColEvent( event
, wxT("OnColBeginDrag") );
986 if ( event
.GetColumn() == 0 )
988 wxLogMessage(wxT("Resizing this column shouldn't work."));
994 void MyListCtrl::OnColDragging(wxListEvent
& event
)
996 LogColEvent( event
, wxT("OnColDragging") );
999 void MyListCtrl::OnColEndDrag(wxListEvent
& event
)
1001 LogColEvent( event
, wxT("OnColEndDrag") );
1004 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
1006 const wxPoint
& pt
= event
.m_pointDrag
;
1009 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
1010 pt
.x
, pt
.y
, HitTest(pt
, flags
) );
1013 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
1015 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
1016 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
1019 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
1021 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event
.m_item
.m_text
.c_str());
1023 wxTextCtrl
* const text
= GetEditControl();
1026 wxLogMessage("BUG: started to edit but no edit control");
1030 wxLogMessage("Edit control value: \"%s\"", text
->GetValue());
1034 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
1036 wxLogMessage( wxT("OnEndLabelEdit: %s"),
1038 event
.IsEditCancelled() ?
1039 wxString("[cancelled]") :
1045 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
1047 LogEvent(event
, wxT("OnDeleteItem"));
1048 wxLogMessage( wxT("Number of items when delete event is sent: %d"), GetItemCount() );
1051 void MyListCtrl::OnDeleteAllItems(wxListEvent
& event
)
1053 LogEvent(event
, wxT("OnDeleteAllItems"));
1056 void MyListCtrl::OnSelected(wxListEvent
& event
)
1058 LogEvent(event
, wxT("OnSelected"));
1060 if ( GetWindowStyle() & wxLC_REPORT
)
1063 info
.m_itemId
= event
.m_itemIndex
;
1065 info
.m_mask
= wxLIST_MASK_TEXT
;
1066 if ( GetItem(info
) )
1068 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
1069 info
.m_text
.c_str());
1073 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
1078 void MyListCtrl::OnDeselected(wxListEvent
& event
)
1080 LogEvent(event
, wxT("OnDeselected"));
1083 void MyListCtrl::OnActivated(wxListEvent
& event
)
1085 LogEvent(event
, wxT("OnActivated"));
1088 void MyListCtrl::OnFocused(wxListEvent
& event
)
1090 LogEvent(event
, wxT("OnFocused"));
1095 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
1099 switch ( event
.GetKeyCode() )
1101 case 'C': // colorize
1104 info
.m_itemId
= event
.GetIndex();
1105 if ( info
.m_itemId
== -1 )
1113 wxListItemAttr
*attr
= info
.GetAttributes();
1114 if ( !attr
|| !attr
->HasTextColour() )
1116 info
.SetTextColour(*wxCYAN
);
1120 RefreshItem(info
.m_itemId
);
1126 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
1127 if ( item
++ == GetItemCount() - 1 )
1132 wxLogMessage(wxT("Focusing item %ld"), item
);
1134 SetItemState(item
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
1135 EnsureVisible(item
);
1138 case 'R': // show bounding rectangle
1140 item
= event
.GetIndex();
1142 if ( !GetItemRect(item
, r
) )
1144 wxLogError(wxT("Failed to retrieve rect of item %ld"), item
);
1148 wxLogMessage(wxT("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
1149 item
, r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
1153 case '1': // show sub item bounding rectangle for the given column
1154 case '2': // (and icon/label rectangle if Shift/Ctrl is pressed)
1156 case '4': // this column is invalid but we want to test it too
1157 if ( InReportView() )
1159 int subItem
= event
.GetKeyCode() - '1';
1160 item
= event
.GetIndex();
1163 int code
= wxLIST_RECT_BOUNDS
;
1164 if ( wxGetKeyState(WXK_SHIFT
) )
1165 code
= wxLIST_RECT_ICON
;
1166 else if ( wxGetKeyState(WXK_CONTROL
) )
1167 code
= wxLIST_RECT_LABEL
;
1169 if ( !GetSubItemRect(item
, subItem
, r
, code
) )
1171 wxLogError(wxT("Failed to retrieve rect of item %ld column %d"), item
, subItem
+ 1);
1175 wxLogMessage(wxT("Bounding rect of item %ld column %d is (%d, %d)-(%d, %d)"),
1177 r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
1185 if ( m_updated
!= -1 )
1186 RefreshItem(m_updated
);
1188 m_updated
= event
.GetIndex();
1189 if ( m_updated
!= -1 )
1191 // we won't see changes to this item as it's selected, update
1192 // the next one (or the first one if we're on the last item)
1193 if ( ++m_updated
== GetItemCount() )
1196 wxLogMessage("Updating colour of the item %ld", m_updated
);
1197 RefreshItem(m_updated
);
1202 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1203 while ( item
!= -1 )
1207 wxLogMessage(wxT("Item %ld deleted"), item
);
1209 // -1 because the indices were shifted by DeleteItem()
1210 item
= GetNextItem(item
- 1,
1211 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1216 if ( GetWindowStyle() & wxLC_REPORT
)
1218 if ( GetWindowStyle() & wxLC_VIRTUAL
)
1220 SetItemCount(GetItemCount() + 1);
1224 InsertItemInReportView(event
.GetIndex());
1227 //else: fall through
1230 LogEvent(event
, wxT("OnListKeyDown"));
1236 void MyListCtrl::OnChar(wxKeyEvent
& event
)
1238 wxLogMessage(wxT("Got char event."));
1240 switch ( event
.GetKeyCode() )
1254 // these are the keys we process ourselves
1262 void MyListCtrl::OnRightClick(wxMouseEvent
& event
)
1264 if ( !event
.ControlDown() )
1272 long item
= HitTest(event
.GetPosition(), flags
, &subitem
);
1277 case wxLIST_HITTEST_ABOVE
: where
= wxT("above"); break;
1278 case wxLIST_HITTEST_BELOW
: where
= wxT("below"); break;
1279 case wxLIST_HITTEST_NOWHERE
: where
= wxT("nowhere near"); break;
1280 case wxLIST_HITTEST_ONITEMICON
: where
= wxT("on icon of"); break;
1281 case wxLIST_HITTEST_ONITEMLABEL
: where
= wxT("on label of"); break;
1282 case wxLIST_HITTEST_ONITEMRIGHT
: where
= wxT("right on"); break;
1283 case wxLIST_HITTEST_TOLEFT
: where
= wxT("to the left of"); break;
1284 case wxLIST_HITTEST_TORIGHT
: where
= wxT("to the right of"); break;
1285 default: where
= wxT("not clear exactly where on"); break;
1288 wxLogMessage(wxT("Right double click %s item %ld, subitem %ld"),
1289 where
.c_str(), item
, subitem
);
1292 void MyListCtrl::LogEvent(const wxListEvent
& event
, const wxChar
*eventName
)
1294 wxLogMessage(wxT("Item %ld: %s (item text = %s, data = %ld)"),
1295 event
.GetIndex(), eventName
,
1296 event
.GetText().c_str(), event
.GetData());
1299 wxString
MyListCtrl::OnGetItemText(long item
, long column
) const
1301 if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
) )
1303 return SMALL_VIRTUAL_VIEW_ITEMS
[item
][column
];
1305 else // "big" virtual control
1307 return wxString::Format(wxT("Column %ld of item %ld"), column
, item
);
1311 int MyListCtrl::OnGetItemColumnImage(long item
, long column
) const
1316 if (!(item
% 3) && column
== 1)
1322 wxListItemAttr
*MyListCtrl::OnGetItemAttr(long item
) const
1324 // test to check that RefreshItem() works correctly: when m_updated is
1325 // set to some item and it is refreshed, we highlight the item
1326 if ( item
== m_updated
)
1328 static wxListItemAttr
s_attrHighlight(*wxRED
, wxNullColour
, wxNullFont
);
1329 return &s_attrHighlight
;
1332 return item
% 2 ? NULL
: (wxListItemAttr
*)&m_attr
;
1335 void MyListCtrl::InsertItemInReportView(int i
)
1338 buf
.Printf(wxT("This is item %d"), i
);
1339 long tmp
= InsertItem(i
, buf
, 0);
1340 SetItemData(tmp
, i
);
1342 buf
.Printf(wxT("Col 1, item %d"), i
);
1343 SetItem(tmp
, 1, buf
);
1345 buf
.Printf(wxT("Item %d in column 2"), i
);
1346 SetItem(tmp
, 2, buf
);
1349 #if USE_CONTEXT_MENU
1350 void MyListCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1352 if (GetEditControl() == NULL
)
1354 wxPoint point
= event
.GetPosition();
1356 if ( (point
.x
== -1) && (point
.y
== -1) )
1358 wxSize size
= GetSize();
1359 point
.x
= size
.x
/ 2;
1360 point
.y
= size
.y
/ 2;
1364 point
= ScreenToClient(point
);
1366 ShowContextMenu(point
);
1370 // the user is editing:
1371 // allow the text control to display its context menu
1372 // if it has one (it has on Windows) rather than display our one
1378 void MyListCtrl::ShowContextMenu(const wxPoint
& pos
)
1382 menu
.Append(wxID_ABOUT
, wxT("&About"));
1383 menu
.AppendSeparator();
1384 menu
.Append(wxID_EXIT
, wxT("E&xit"));
1386 PopupMenu(&menu
, pos
.x
, pos
.y
);