1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxListCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #if !defined(__WXMSW__) && !defined(__WXPM__)
24 #include "mondrian.xpm"
28 #include "bitmaps/toolbrai.xpm"
29 #include "bitmaps/toolchar.xpm"
30 #include "bitmaps/tooldata.xpm"
31 #include "bitmaps/toolnote.xpm"
32 #include "bitmaps/tooltodo.xpm"
33 #include "bitmaps/toolchec.xpm"
34 #include "bitmaps/toolgame.xpm"
35 #include "bitmaps/tooltime.xpm"
36 #include "bitmaps/toolword.xpm"
37 #include "bitmaps/small1.xpm"
40 #include "wx/imaglist.h"
41 #include "wx/listctrl.h"
42 #include "wx/timer.h" // for wxStopWatch
43 #include "wx/colordlg.h" // for wxGetColourFromUser
44 #include "wx/settings.h"
45 #include "wx/sysopt.h"
49 const wxChar
*SMALL_VIRTUAL_VIEW_ITEMS
[][2] =
51 { _T("Cat"), _T("meow") },
52 { _T("Cow"), _T("moo") },
53 { _T("Crow"), _T("caw") },
54 { _T("Dog"), _T("woof") },
55 { _T("Duck"), _T("quack") },
56 { _T("Mouse"), _T("squeak") },
57 { _T("Owl"), _T("hoo") },
58 { _T("Pig"), _T("oink") },
59 { _T("Pigeon"), _T("coo") },
60 { _T("Sheep"), _T("baaah") },
64 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
65 EVT_SIZE(MyFrame::OnSize
)
67 EVT_MENU(LIST_QUIT
, MyFrame::OnQuit
)
68 EVT_MENU(LIST_ABOUT
, MyFrame::OnAbout
)
69 EVT_MENU(LIST_LIST_VIEW
, MyFrame::OnListView
)
70 EVT_MENU(LIST_REPORT_VIEW
, MyFrame::OnReportView
)
71 EVT_MENU(LIST_ICON_VIEW
, MyFrame::OnIconView
)
72 EVT_MENU(LIST_ICON_TEXT_VIEW
, MyFrame::OnIconTextView
)
73 EVT_MENU(LIST_SMALL_ICON_VIEW
, MyFrame::OnSmallIconView
)
74 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW
, MyFrame::OnSmallIconTextView
)
75 EVT_MENU(LIST_VIRTUAL_VIEW
, MyFrame::OnVirtualView
)
76 EVT_MENU(LIST_SMALL_VIRTUAL_VIEW
, MyFrame::OnSmallVirtualView
)
78 EVT_MENU(LIST_FOCUS_LAST
, MyFrame::OnFocusLast
)
79 EVT_MENU(LIST_TOGGLE_FIRST
, MyFrame::OnToggleFirstSel
)
80 EVT_MENU(LIST_DESELECT_ALL
, MyFrame::OnDeselectAll
)
81 EVT_MENU(LIST_SELECT_ALL
, MyFrame::OnSelectAll
)
82 EVT_MENU(LIST_DELETE
, MyFrame::OnDelete
)
83 EVT_MENU(LIST_ADD
, MyFrame::OnAdd
)
84 EVT_MENU(LIST_EDIT
, MyFrame::OnEdit
)
85 EVT_MENU(LIST_DELETE_ALL
, MyFrame::OnDeleteAll
)
86 EVT_MENU(LIST_SORT
, MyFrame::OnSort
)
87 EVT_MENU(LIST_SET_FG_COL
, MyFrame::OnSetFgColour
)
88 EVT_MENU(LIST_SET_BG_COL
, MyFrame::OnSetBgColour
)
89 EVT_MENU(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnToggleMultiSel
)
90 EVT_MENU(LIST_SHOW_COL_INFO
, MyFrame::OnShowColInfo
)
91 EVT_MENU(LIST_SHOW_SEL_INFO
, MyFrame::OnShowSelInfo
)
92 EVT_MENU(LIST_FREEZE
, MyFrame::OnFreeze
)
93 EVT_MENU(LIST_THAW
, MyFrame::OnThaw
)
94 EVT_MENU(LIST_TOGGLE_LINES
, MyFrame::OnToggleLines
)
95 EVT_MENU(LIST_MAC_USE_GENERIC
, MyFrame::OnToggleMacUseGeneric
)
97 EVT_UPDATE_UI(LIST_SHOW_COL_INFO
, MyFrame::OnUpdateShowColInfo
)
98 EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL
, MyFrame::OnUpdateToggleMultiSel
)
101 BEGIN_EVENT_TABLE(MyListCtrl
, wxListCtrl
)
102 EVT_LIST_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnBeginDrag
)
103 EVT_LIST_BEGIN_RDRAG(LIST_CTRL
, MyListCtrl::OnBeginRDrag
)
104 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnBeginLabelEdit
)
105 EVT_LIST_END_LABEL_EDIT(LIST_CTRL
, MyListCtrl::OnEndLabelEdit
)
106 EVT_LIST_DELETE_ITEM(LIST_CTRL
, MyListCtrl::OnDeleteItem
)
107 EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL
, MyListCtrl::OnDeleteAllItems
)
108 EVT_LIST_ITEM_SELECTED(LIST_CTRL
, MyListCtrl::OnSelected
)
109 EVT_LIST_ITEM_DESELECTED(LIST_CTRL
, MyListCtrl::OnDeselected
)
110 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl::OnListKeyDown
)
111 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl::OnActivated
)
112 EVT_LIST_ITEM_FOCUSED(LIST_CTRL
, MyListCtrl::OnFocused
)
114 EVT_LIST_COL_CLICK(LIST_CTRL
, MyListCtrl::OnColClick
)
115 EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL
, MyListCtrl::OnColRightClick
)
116 EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL
, MyListCtrl::OnColBeginDrag
)
117 EVT_LIST_COL_DRAGGING(LIST_CTRL
, MyListCtrl::OnColDragging
)
118 EVT_LIST_COL_END_DRAG(LIST_CTRL
, MyListCtrl::OnColEndDrag
)
120 EVT_LIST_CACHE_HINT(LIST_CTRL
, MyListCtrl::OnCacheHint
)
123 EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu
)
125 EVT_CHAR(MyListCtrl::OnChar
)
127 EVT_RIGHT_DOWN(MyListCtrl::OnRightClick
)
132 // number of items in list/report view
133 static const int NUM_ITEMS
= 30;
135 // number of items in icon/small icon view
136 static const int NUM_ICONS
= 9;
138 int wxCALLBACK
MyCompareFunction(long item1
, long item2
, long WXUNUSED(sortData
))
149 // `Main program' equivalent, creating windows and returning main app frame
152 if ( !wxApp::OnInit() )
155 // Create the main frame window
156 MyFrame
*frame
= new MyFrame(wxT("wxListCtrl Test"));
166 // My frame constructor
167 MyFrame::MyFrame(const wxChar
*title
)
168 : wxFrame(NULL
, wxID_ANY
, title
)
172 m_smallVirtual
= false;
174 if (wxSystemSettings::GetScreenType() > wxSYS_SCREEN_SMALL
)
175 SetSize(wxSize(450, 340));
178 SetIcon( wxICON(mondrian
) );
180 // Make an image list containing large icons
181 m_imageListNormal
= new wxImageList(32, 32, true);
182 m_imageListSmall
= new wxImageList(16, 16, true);
185 m_imageListNormal
->Add( wxIcon(_T("icon1"), wxBITMAP_TYPE_ICO_RESOURCE
) );
186 m_imageListNormal
->Add( wxIcon(_T("icon2"), wxBITMAP_TYPE_ICO_RESOURCE
) );
187 m_imageListNormal
->Add( wxIcon(_T("icon3"), wxBITMAP_TYPE_ICO_RESOURCE
) );
188 m_imageListNormal
->Add( wxIcon(_T("icon4"), wxBITMAP_TYPE_ICO_RESOURCE
) );
189 m_imageListNormal
->Add( wxIcon(_T("icon5"), wxBITMAP_TYPE_ICO_RESOURCE
) );
190 m_imageListNormal
->Add( wxIcon(_T("icon6"), wxBITMAP_TYPE_ICO_RESOURCE
) );
191 m_imageListNormal
->Add( wxIcon(_T("icon7"), wxBITMAP_TYPE_ICO_RESOURCE
) );
192 m_imageListNormal
->Add( wxIcon(_T("icon8"), wxBITMAP_TYPE_ICO_RESOURCE
) );
193 m_imageListNormal
->Add( wxIcon(_T("icon9"), wxBITMAP_TYPE_ICO_RESOURCE
) );
195 m_imageListSmall
->Add( wxIcon(_T("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE
) );
198 m_imageListNormal
->Add( wxIcon( toolbrai_xpm
) );
199 m_imageListNormal
->Add( wxIcon( toolchar_xpm
) );
200 m_imageListNormal
->Add( wxIcon( tooldata_xpm
) );
201 m_imageListNormal
->Add( wxIcon( toolnote_xpm
) );
202 m_imageListNormal
->Add( wxIcon( tooltodo_xpm
) );
203 m_imageListNormal
->Add( wxIcon( toolchec_xpm
) );
204 m_imageListNormal
->Add( wxIcon( toolgame_xpm
) );
205 m_imageListNormal
->Add( wxIcon( tooltime_xpm
) );
206 m_imageListNormal
->Add( wxIcon( toolword_xpm
) );
208 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
212 wxMenu
*menuFile
= new wxMenu
;
213 menuFile
->Append(LIST_ABOUT
, _T("&About"));
214 menuFile
->AppendSeparator();
215 menuFile
->Append(LIST_QUIT
, _T("E&xit\tAlt-X"));
217 wxMenu
*menuView
= new wxMenu
;
218 menuView
->Append(LIST_LIST_VIEW
, _T("&List view\tF1"));
219 menuView
->Append(LIST_REPORT_VIEW
, _T("&Report view\tF2"));
220 menuView
->Append(LIST_ICON_VIEW
, _T("&Icon view\tF3"));
221 menuView
->Append(LIST_ICON_TEXT_VIEW
, _T("Icon view with &text\tF4"));
222 menuView
->Append(LIST_SMALL_ICON_VIEW
, _T("&Small icon view\tF5"));
223 menuView
->Append(LIST_SMALL_ICON_TEXT_VIEW
, _T("Small icon &view with text\tF6"));
224 menuView
->Append(LIST_VIRTUAL_VIEW
, _T("&Virtual view\tF7"));
225 menuView
->Append(LIST_SMALL_VIRTUAL_VIEW
, _T("Small virtual vie&w\tF8"));
227 menuView
->AppendCheckItem(LIST_MAC_USE_GENERIC
, _T("Mac: Use Generic Control"));
230 wxMenu
*menuList
= new wxMenu
;
231 menuList
->Append(LIST_FOCUS_LAST
, _T("&Make last item current\tCtrl-L"));
232 menuList
->Append(LIST_TOGGLE_FIRST
, _T("To&ggle first item\tCtrl-G"));
233 menuList
->Append(LIST_DESELECT_ALL
, _T("&Deselect All\tCtrl-D"));
234 menuList
->Append(LIST_SELECT_ALL
, _T("S&elect All\tCtrl-A"));
235 menuList
->AppendSeparator();
236 menuList
->Append(LIST_SHOW_COL_INFO
, _T("Show &column info\tCtrl-C"));
237 menuList
->Append(LIST_SHOW_SEL_INFO
, _T("Show &selected items\tCtrl-S"));
238 menuList
->AppendSeparator();
239 menuList
->Append(LIST_SORT
, _T("&Sort\tCtrl-S"));
240 menuList
->AppendSeparator();
241 menuList
->Append(LIST_ADD
, _T("&Append an item\tCtrl-P"));
242 menuList
->Append(LIST_EDIT
, _T("&Edit the item\tCtrl-E"));
243 menuList
->Append(LIST_DELETE
, _T("&Delete first item\tCtrl-X"));
244 menuList
->Append(LIST_DELETE_ALL
, _T("Delete &all items"));
245 menuList
->AppendSeparator();
246 menuList
->Append(LIST_FREEZE
, _T("Free&ze\tCtrl-Z"));
247 menuList
->Append(LIST_THAW
, _T("Tha&w\tCtrl-W"));
248 menuList
->AppendSeparator();
249 menuList
->AppendCheckItem(LIST_TOGGLE_LINES
, _T("Toggle &lines\tCtrl-I"));
250 menuList
->Append(LIST_TOGGLE_MULTI_SEL
, _T("&Multiple selection\tCtrl-M"),
251 _T("Toggle multiple selection"), true);
253 wxMenu
*menuCol
= new wxMenu
;
254 menuCol
->Append(LIST_SET_FG_COL
, _T("&Foreground colour..."));
255 menuCol
->Append(LIST_SET_BG_COL
, _T("&Background colour..."));
257 wxMenuBar
*menubar
= new wxMenuBar
;
258 menubar
->Append(menuFile
, _T("&File"));
259 menubar
->Append(menuView
, _T("&View"));
260 menubar
->Append(menuList
, _T("&List"));
261 menubar
->Append(menuCol
, _T("&Colour"));
264 m_panel
= new wxPanel(this, wxID_ANY
);
265 m_logWindow
= new wxTextCtrl(m_panel
, wxID_ANY
, wxEmptyString
,
266 wxDefaultPosition
, wxDefaultSize
,
267 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
269 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
271 RecreateList(wxLC_REPORT
| wxLC_SINGLE_SEL
);
275 #endif // wxUSE_STATUSBAR
280 delete wxLog::SetActiveTarget(m_logOld
);
282 delete m_imageListNormal
;
283 delete m_imageListSmall
;
286 void MyFrame::OnSize(wxSizeEvent
& event
)
293 void MyFrame::DoSize()
298 wxSize size
= GetClientSize();
299 wxCoord y
= (2*size
.y
)/3;
300 m_listCtrl
->SetSize(0, 0, size
.x
, y
);
301 m_logWindow
->SetSize(0, y
+ 1, size
.x
, size
.y
- y
);
304 bool MyFrame::CheckNonVirtual() const
306 if ( !m_listCtrl
->HasFlag(wxLC_VIRTUAL
) )
309 // "this" == whatever
310 wxLogWarning(_T("Can't do this in virtual view, sorry."));
315 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
320 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
322 wxMessageDialog
dialog(this, _T("List test sample\nJulian Smart (c) 1997"),
323 _T("About list test"), wxOK
|wxCANCEL
);
328 void MyFrame::OnFreeze(wxCommandEvent
& WXUNUSED(event
))
330 wxLogMessage(_T("Freezing the control"));
332 m_listCtrl
->Freeze();
335 void MyFrame::OnThaw(wxCommandEvent
& WXUNUSED(event
))
337 wxLogMessage(_T("Thawing the control"));
342 void MyFrame::OnToggleLines(wxCommandEvent
& event
)
344 m_listCtrl
->SetSingleStyle(wxLC_HRULES
| wxLC_VRULES
, event
.IsChecked());
347 void MyFrame::OnToggleMacUseGeneric(wxCommandEvent
& event
)
349 wxSystemOptions::SetOption(wxT("mac.listctrl.always_use_generic"), event
.IsChecked());
352 void MyFrame::OnFocusLast(wxCommandEvent
& WXUNUSED(event
))
354 long index
= m_listCtrl
->GetItemCount() - 1;
360 m_listCtrl
->SetItemState(index
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
361 m_listCtrl
->EnsureVisible(index
);
364 void MyFrame::OnToggleFirstSel(wxCommandEvent
& WXUNUSED(event
))
366 m_listCtrl
->SetItemState(0, (~m_listCtrl
->GetItemState(0, wxLIST_STATE_SELECTED
) ) & wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
369 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
371 if ( !CheckNonVirtual() )
374 int n
= m_listCtrl
->GetItemCount();
375 for (int i
= 0; i
< n
; i
++)
376 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
379 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
381 if ( !CheckNonVirtual() )
384 int n
= m_listCtrl
->GetItemCount();
385 for (int i
= 0; i
< n
; i
++)
386 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
389 // ----------------------------------------------------------------------------
390 // changing listctrl modes
391 // ----------------------------------------------------------------------------
393 void MyFrame::RecreateList(long flags
, bool withText
)
395 // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL
396 // style, but it is more trouble to do it than not
398 if ( !m_listCtrl
|| ((flags
& wxLC_VIRTUAL
) !=
399 (m_listCtrl
->GetWindowStyleFlag() & wxLC_VIRTUAL
)) )
404 m_listCtrl
= new MyListCtrl(m_panel
, LIST_CTRL
,
405 wxDefaultPosition
, wxDefaultSize
,
407 wxSUNKEN_BORDER
| wxLC_EDIT_LABELS
);
409 switch ( flags
& wxLC_MASK_TYPE
)
416 InitWithIconItems(withText
);
419 case wxLC_SMALL_ICON
:
420 InitWithIconItems(withText
, true);
424 if ( flags
& wxLC_VIRTUAL
)
425 InitWithVirtualItems();
427 InitWithReportItems();
431 wxFAIL_MSG( _T("unknown listctrl mode") );
437 m_logWindow
->Clear();
440 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
442 RecreateList(wxLC_LIST
);
445 void MyFrame::InitWithListItems()
447 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
449 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Item %d"), i
));
453 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
455 RecreateList(wxLC_REPORT
);
458 void MyFrame::InitWithReportItems()
460 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
462 // note that under MSW for SetColumnWidth() to work we need to create the
463 // items with images initially even if we specify dummy image id
465 itemCol
.SetText(_T("Column 1"));
466 itemCol
.SetImage(-1);
467 m_listCtrl
->InsertColumn(0, itemCol
);
469 itemCol
.SetText(_T("Column 2"));
470 itemCol
.SetAlign(wxLIST_FORMAT_CENTRE
);
471 m_listCtrl
->InsertColumn(1, itemCol
);
473 itemCol
.SetText(_T("Column 3"));
474 itemCol
.SetAlign(wxLIST_FORMAT_RIGHT
);
475 m_listCtrl
->InsertColumn(2, itemCol
);
477 // to speed up inserting we hide the control temporarily
482 for ( int i
= 0; i
< NUM_ITEMS
; i
++ )
484 m_listCtrl
->InsertItemInReportView(i
);
487 m_logWindow
->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"),
488 NUM_ITEMS
, sw
.Time()));
491 // we leave all mask fields to 0 and only change the colour
494 item
.SetTextColour(*wxRED
);
495 m_listCtrl
->SetItem( item
);
498 item
.SetTextColour(*wxGREEN
);
499 m_listCtrl
->SetItem( item
);
501 item
.SetTextColour(*wxLIGHT_GREY
);
502 item
.SetFont(*wxITALIC_FONT
);
503 item
.SetBackgroundColour(*wxRED
);
504 m_listCtrl
->SetItem( item
);
506 m_listCtrl
->SetTextColour(*wxBLUE
);
508 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
509 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
510 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
512 // Set images in columns
513 m_listCtrl
->SetItemColumnImage(1, 1, 0);
519 m_listCtrl
->SetItem(info
);
521 // test SetItemFont too
522 m_listCtrl
->SetItemFont(0, *wxITALIC_FONT
);
525 void MyFrame::InitWithIconItems(bool withText
, bool sameIcon
)
527 m_listCtrl
->SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
528 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
530 for ( int i
= 0; i
< NUM_ICONS
; i
++ )
532 int image
= sameIcon
? 0 : i
;
536 m_listCtrl
->InsertItem(i
, wxString::Format(_T("Label %d"), i
),
541 m_listCtrl
->InsertItem(i
, image
);
546 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
548 RecreateList(wxLC_ICON
, false);
551 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
553 RecreateList(wxLC_ICON
);
556 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
558 RecreateList(wxLC_SMALL_ICON
, false);
561 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
563 RecreateList(wxLC_SMALL_ICON
);
566 void MyFrame::OnVirtualView(wxCommandEvent
& WXUNUSED(event
))
568 m_smallVirtual
= false;
569 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
572 void MyFrame::OnSmallVirtualView(wxCommandEvent
& WXUNUSED(event
))
574 m_smallVirtual
= true;
575 RecreateList(wxLC_REPORT
| wxLC_VIRTUAL
);
578 void MyFrame::InitWithVirtualItems()
580 m_listCtrl
->SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
582 if ( m_smallVirtual
)
584 m_listCtrl
->InsertColumn(0, _T("Animal"));
585 m_listCtrl
->InsertColumn(1, _T("Sound"));
586 m_listCtrl
->SetItemCount(WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
));
590 m_listCtrl
->InsertColumn(0, _T("First Column"));
591 m_listCtrl
->InsertColumn(1, _T("Second Column"));
592 m_listCtrl
->SetColumnWidth(0, 150);
593 m_listCtrl
->SetColumnWidth(1, 150);
594 m_listCtrl
->SetItemCount(1000000);
598 void MyFrame::OnSort(wxCommandEvent
& WXUNUSED(event
))
602 m_listCtrl
->SortItems(MyCompareFunction
, 0);
604 m_logWindow
->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"),
605 m_listCtrl
->GetItemCount(),
609 void MyFrame::OnShowSelInfo(wxCommandEvent
& WXUNUSED(event
))
611 int selCount
= m_listCtrl
->GetSelectedItemCount();
612 wxLogMessage(_T("%d items selected:"), selCount
);
614 // don't show too many items
615 size_t shownCount
= 0;
617 long item
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
618 wxLIST_STATE_SELECTED
);
621 wxLogMessage(_T("\t%ld (%s)"),
622 item
, m_listCtrl
->GetItemText(item
).c_str());
624 if ( ++shownCount
> 10 )
626 wxLogMessage(_T("\t... more selected items snipped..."));
630 item
= m_listCtrl
->GetNextItem(item
, wxLIST_NEXT_ALL
,
631 wxLIST_STATE_SELECTED
);
635 void MyFrame::OnShowColInfo(wxCommandEvent
& WXUNUSED(event
))
637 int count
= m_listCtrl
->GetColumnCount();
638 wxLogMessage(wxT("%d columns:"), count
);
639 for ( int c
= 0; c
< count
; c
++ )
641 wxLogMessage(wxT("\tcolumn %d has width %d"), c
,
642 m_listCtrl
->GetColumnWidth(c
));
646 void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent
& event
)
648 event
.Enable( (m_listCtrl
->GetWindowStyleFlag() & wxLC_REPORT
) != 0 );
651 void MyFrame::OnToggleMultiSel(wxCommandEvent
& WXUNUSED(event
))
653 long flags
= m_listCtrl
->GetWindowStyleFlag();
654 if ( flags
& wxLC_SINGLE_SEL
)
655 flags
&= ~wxLC_SINGLE_SEL
;
657 flags
|= wxLC_SINGLE_SEL
;
659 m_logWindow
->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"),
660 (flags
& wxLC_SINGLE_SEL
) ? _T("sing") : _T("multip")));
665 void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent
& event
)
667 event
.Check((m_listCtrl
->GetWindowStyleFlag() & wxLC_SINGLE_SEL
) == 0);
670 void MyFrame::OnSetFgColour(wxCommandEvent
& WXUNUSED(event
))
672 m_listCtrl
->SetForegroundColour(wxGetColourFromUser(this));
673 m_listCtrl
->Refresh();
676 void MyFrame::OnSetBgColour(wxCommandEvent
& WXUNUSED(event
))
678 m_listCtrl
->SetBackgroundColour(wxGetColourFromUser(this));
679 m_listCtrl
->Refresh();
682 void MyFrame::OnAdd(wxCommandEvent
& WXUNUSED(event
))
684 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), _T("Appended item"));
687 void MyFrame::OnEdit(wxCommandEvent
& WXUNUSED(event
))
689 long itemCur
= m_listCtrl
->GetNextItem(-1, wxLIST_NEXT_ALL
,
690 wxLIST_STATE_FOCUSED
);
694 m_listCtrl
->EditLabel(itemCur
);
698 m_logWindow
->WriteText(_T("No item to edit"));
702 void MyFrame::OnDelete(wxCommandEvent
& WXUNUSED(event
))
704 if ( m_listCtrl
->GetItemCount() )
706 m_listCtrl
->DeleteItem(0);
710 m_logWindow
->WriteText(_T("Nothing to delete"));
714 void MyFrame::OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
718 int itemCount
= m_listCtrl
->GetItemCount();
720 m_listCtrl
->DeleteAllItems();
722 m_logWindow
->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"),
729 void MyListCtrl::OnCacheHint(wxListEvent
& event
)
731 wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"),
732 event
.GetCacheFrom(), event
.GetCacheTo() );
735 void MyListCtrl::SetColumnImage(int col
, int image
)
738 item
.SetMask(wxLIST_MASK_IMAGE
);
739 item
.SetImage(image
);
740 SetColumn(col
, item
);
743 void MyListCtrl::OnColClick(wxListEvent
& event
)
745 int col
= event
.GetColumn();
747 // set or unset image
748 static bool x
= false;
750 SetColumnImage(col
, x
? 0 : -1);
752 wxLogMessage( wxT("OnColumnClick at %d."), col
);
755 void MyListCtrl::OnColRightClick(wxListEvent
& event
)
757 int col
= event
.GetColumn();
760 SetColumnImage(col
, -1);
763 // Show popupmenu at position
764 wxMenu
menu(wxT("Test"));
765 menu
.Append(LIST_ABOUT
, _T("&About"));
766 PopupMenu(&menu
, event
.GetPoint());
768 wxLogMessage( wxT("OnColumnRightClick at %d."), event
.GetColumn() );
771 void MyListCtrl::LogColEvent(const wxListEvent
& event
, const wxChar
*name
)
773 const int col
= event
.GetColumn();
775 wxLogMessage(wxT("%s: column %d (width = %d or %d)."),
778 event
.GetItem().GetWidth(),
779 GetColumnWidth(col
));
782 void MyListCtrl::OnColBeginDrag(wxListEvent
& event
)
784 LogColEvent( event
, wxT("OnColBeginDrag") );
786 if ( event
.GetColumn() == 0 )
788 wxLogMessage(_T("Resizing this column shouldn't work."));
794 void MyListCtrl::OnColDragging(wxListEvent
& event
)
796 LogColEvent( event
, wxT("OnColDragging") );
799 void MyListCtrl::OnColEndDrag(wxListEvent
& event
)
801 LogColEvent( event
, wxT("OnColEndDrag") );
804 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
806 const wxPoint
& pt
= event
.m_pointDrag
;
809 wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."),
810 pt
.x
, pt
.y
, HitTest(pt
, flags
) );
813 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
815 wxLogMessage( wxT("OnBeginRDrag at %d,%d."),
816 event
.m_pointDrag
.x
, event
.m_pointDrag
.y
);
819 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
821 wxLogMessage( wxT("OnBeginLabelEdit: %s"), event
.m_item
.m_text
.c_str());
824 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
826 wxLogMessage( wxT("OnEndLabelEdit: %s"),
827 event
.IsEditCancelled() ? _T("[cancelled]")
828 : event
.m_item
.m_text
.c_str());
831 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
833 LogEvent(event
, _T("OnDeleteItem"));
834 wxLogMessage( wxT("Number of items when delete event is sent: %d"), GetItemCount() );
837 void MyListCtrl::OnDeleteAllItems(wxListEvent
& event
)
839 LogEvent(event
, _T("OnDeleteAllItems"));
842 void MyListCtrl::OnSelected(wxListEvent
& event
)
844 LogEvent(event
, _T("OnSelected"));
846 if ( GetWindowStyle() & wxLC_REPORT
)
849 info
.m_itemId
= event
.m_itemIndex
;
851 info
.m_mask
= wxLIST_MASK_TEXT
;
854 wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"),
855 info
.m_text
.c_str());
859 wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed"));
864 void MyListCtrl::OnDeselected(wxListEvent
& event
)
866 LogEvent(event
, _T("OnDeselected"));
869 void MyListCtrl::OnActivated(wxListEvent
& event
)
871 LogEvent(event
, _T("OnActivated"));
874 void MyListCtrl::OnFocused(wxListEvent
& event
)
876 LogEvent(event
, _T("OnFocused"));
881 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
885 switch ( event
.GetKeyCode() )
887 case 'c': // colorize
891 info
.m_itemId
= event
.GetIndex();
892 if ( info
.m_itemId
== -1 )
900 wxListItemAttr
*attr
= info
.GetAttributes();
901 if ( !attr
|| !attr
->HasTextColour() )
903 info
.SetTextColour(*wxCYAN
);
907 RefreshItem(info
.m_itemId
);
914 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_FOCUSED
);
915 if ( item
++ == GetItemCount() - 1 )
920 wxLogMessage(_T("Focusing item %ld"), item
);
922 SetItemState(item
, wxLIST_STATE_FOCUSED
, wxLIST_STATE_FOCUSED
);
926 case 'r': // show bounding Rect
929 item
= event
.GetIndex();
931 if ( !GetItemRect(item
, r
) )
933 wxLogError(_T("Failed to retrieve rect of item %ld"), item
);
937 wxLogMessage(_T("Bounding rect of item %ld is (%d, %d)-(%d, %d)"),
938 item
, r
.x
, r
.y
, r
.x
+ r
.width
, r
.y
+ r
.height
);
943 item
= GetNextItem(-1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
948 wxLogMessage(_T("Item %ld deleted"), item
);
950 // -1 because the indices were shifted by DeleteItem()
951 item
= GetNextItem(item
- 1,
952 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
957 if ( GetWindowStyle() & wxLC_REPORT
)
959 if ( GetWindowStyle() & wxLC_VIRTUAL
)
961 SetItemCount(GetItemCount() + 1);
965 InsertItemInReportView(event
.GetIndex());
971 LogEvent(event
, _T("OnListKeyDown"));
977 void MyListCtrl::OnChar(wxKeyEvent
& event
)
979 wxLogMessage(_T("Got char event."));
981 switch ( event
.GetKeyCode() )
987 // these are the keys we process ourselves
995 void MyListCtrl::OnRightClick(wxMouseEvent
& event
)
997 if ( !event
.ControlDown() )
1005 long item
= HitTest(event
.GetPosition(), flags
, &subitem
);
1010 case wxLIST_HITTEST_ABOVE
: where
= _T("above"); break;
1011 case wxLIST_HITTEST_BELOW
: where
= _T("below"); break;
1012 case wxLIST_HITTEST_NOWHERE
: where
= _T("nowhere near"); break;
1013 case wxLIST_HITTEST_ONITEMICON
: where
= _T("on icon of"); break;
1014 case wxLIST_HITTEST_ONITEMLABEL
: where
= _T("on label of"); break;
1015 case wxLIST_HITTEST_ONITEMRIGHT
: where
= _T("right on"); break;
1016 case wxLIST_HITTEST_TOLEFT
: where
= _T("to the left of"); break;
1017 case wxLIST_HITTEST_TORIGHT
: where
= _T("to the right of"); break;
1018 default: where
= _T("not clear exactly where on"); break;
1021 wxLogMessage(_T("Right double click %s item %ld, subitem %ld"),
1022 where
.c_str(), item
, subitem
);
1025 void MyListCtrl::LogEvent(const wxListEvent
& event
, const wxChar
*eventName
)
1027 wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
1028 event
.GetIndex(), eventName
,
1029 event
.GetText().c_str(), event
.GetData());
1032 wxString
MyListCtrl::OnGetItemText(long item
, long column
) const
1034 if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS
) )
1036 return SMALL_VIRTUAL_VIEW_ITEMS
[item
][column
];
1040 return wxString::Format(_T("Column %ld of item %ld"), column
, item
);
1044 int MyListCtrl::OnGetItemColumnImage(long item
, long column
) const
1049 if (!(item %3
) && column
== 1)
1055 wxListItemAttr
*MyListCtrl::OnGetItemAttr(long item
) const
1057 return item
% 2 ? NULL
: (wxListItemAttr
*)&m_attr
;
1060 void MyListCtrl::InsertItemInReportView(int i
)
1063 buf
.Printf(_T("This is item %d"), i
);
1064 long tmp
= InsertItem(i
, buf
, 0);
1065 SetItemData(tmp
, i
);
1067 buf
.Printf(_T("Col 1, item %d"), i
);
1068 SetItem(tmp
, 1, buf
);
1070 buf
.Printf(_T("Item %d in column 2"), i
);
1071 SetItem(tmp
, 2, buf
);
1074 #if USE_CONTEXT_MENU
1075 void MyListCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1077 wxPoint point
= event
.GetPosition();
1079 if (point
.x
== -1 && point
.y
== -1) {
1080 wxSize size
= GetSize();
1081 point
.x
= size
.x
/ 2;
1082 point
.y
= size
.y
/ 2;
1084 point
= ScreenToClient(point
);
1086 ShowContextMenu(point
);
1090 void MyListCtrl::ShowContextMenu(const wxPoint
& pos
)
1094 menu
.Append(wxID_ABOUT
, _T("&About"));
1095 menu
.AppendSeparator();
1096 menu
.Append(wxID_EXIT
, _T("E&xit"));
1098 PopupMenu(&menu
, pos
.x
, pos
.y
);