]>
Commit | Line | Data |
---|---|---|
457814b5 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: listctrl.cpp | |
3 | // Purpose: wxListCtrl sample | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6aa89a22 | 8 | // Copyright: (c) Julian Smart |
5ea47806 | 9 | // Licence: wxWindows license |
457814b5 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation | |
14 | #pragma interface | |
15 | #endif | |
16 | ||
17 | // For compilers that support precompilation, includes "wx/wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WX_PRECOMP | |
25 | #include "wx/wx.h" | |
26 | #endif | |
27 | ||
907789a0 | 28 | #ifndef __WXMSW__ |
c41ea66a VZ |
29 | #include "mondrian.xpm" |
30 | ||
31 | #include "bitmaps/toolbrai.xpm" | |
32 | #include "bitmaps/toolchar.xpm" | |
33 | #include "bitmaps/tooldata.xpm" | |
34 | #include "bitmaps/toolnote.xpm" | |
35 | #include "bitmaps/tooltodo.xpm" | |
36 | #include "bitmaps/toolchec.xpm" | |
37 | #include "bitmaps/toolgame.xpm" | |
38 | #include "bitmaps/tooltime.xpm" | |
39 | #include "bitmaps/toolword.xpm" | |
40 | #include "bitmaps/small1.xpm" | |
907789a0 RR |
41 | #endif |
42 | ||
2e8a1588 | 43 | #include "wx/imaglist.h" |
457814b5 | 44 | #include "wx/listctrl.h" |
81278df2 | 45 | #include "wx/timer.h" // for wxStopWatch |
11f26ea0 VZ |
46 | #include "wx/colordlg.h" // for wxGetColourFromUser |
47 | ||
457814b5 JS |
48 | #include "listtest.h" |
49 | ||
50 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
98ec9dbe VZ |
51 | EVT_SIZE(MyFrame::OnSize) |
52 | ||
5ea47806 VZ |
53 | EVT_MENU(LIST_QUIT, MyFrame::OnQuit) |
54 | EVT_MENU(LIST_ABOUT, MyFrame::OnAbout) | |
55 | EVT_MENU(LIST_LIST_VIEW, MyFrame::OnListView) | |
56 | EVT_MENU(LIST_REPORT_VIEW, MyFrame::OnReportView) | |
57 | EVT_MENU(LIST_ICON_VIEW, MyFrame::OnIconView) | |
58 | EVT_MENU(LIST_ICON_TEXT_VIEW, MyFrame::OnIconTextView) | |
59 | EVT_MENU(LIST_SMALL_ICON_VIEW, MyFrame::OnSmallIconView) | |
60 | EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView) | |
98ec9dbe VZ |
61 | EVT_MENU(LIST_VIRTUAL_VIEW, MyFrame::OnVirtualView) |
62 | ||
88b792af | 63 | EVT_MENU(LIST_FOCUS_LAST, MyFrame::OnFocusLast) |
58b3bdc9 | 64 | EVT_MENU(LIST_TOGGLE_FIRST, MyFrame::OnToggleFirstSel) |
5ea47806 VZ |
65 | EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll) |
66 | EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll) | |
bec0a261 | 67 | EVT_MENU(LIST_DELETE, MyFrame::OnDelete) |
cf1dfa6b | 68 | EVT_MENU(LIST_ADD, MyFrame::OnAdd) |
efe23391 | 69 | EVT_MENU(LIST_EDIT, MyFrame::OnEdit) |
5ea47806 | 70 | EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll) |
fa5f6926 | 71 | EVT_MENU(LIST_SORT, MyFrame::OnSort) |
11f26ea0 VZ |
72 | EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour) |
73 | EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour) | |
7b848b0d | 74 | EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel) |
f6bcfd97 | 75 | EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo) |
b54e41c5 | 76 | EVT_MENU(LIST_SHOW_SEL_INFO, MyFrame::OnShowSelInfo) |
c5c528fc VZ |
77 | EVT_MENU(LIST_FREEZE, MyFrame::OnFreeze) |
78 | EVT_MENU(LIST_THAW, MyFrame::OnThaw) | |
494ab5de | 79 | EVT_MENU(LIST_TOGGLE_LINES, MyFrame::OnToggleLines) |
98ec9dbe | 80 | |
f6bcfd97 | 81 | EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo) |
ae403b11 | 82 | EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL, MyFrame::OnUpdateToggleMultiSel) |
457814b5 JS |
83 | END_EVENT_TABLE() |
84 | ||
85 | BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl) | |
5ea47806 VZ |
86 | EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag) |
87 | EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag) | |
88 | EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnBeginLabelEdit) | |
89 | EVT_LIST_END_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnEndLabelEdit) | |
90 | EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem) | |
12c1b46a | 91 | EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems) |
5ea47806 VZ |
92 | EVT_LIST_GET_INFO(LIST_CTRL, MyListCtrl::OnGetInfo) |
93 | EVT_LIST_SET_INFO(LIST_CTRL, MyListCtrl::OnSetInfo) | |
94 | EVT_LIST_ITEM_SELECTED(LIST_CTRL, MyListCtrl::OnSelected) | |
95 | EVT_LIST_ITEM_DESELECTED(LIST_CTRL, MyListCtrl::OnDeselected) | |
96 | EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown) | |
97 | EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated) | |
0ddefeb0 | 98 | EVT_LIST_ITEM_FOCUSED(LIST_CTRL, MyListCtrl::OnFocused) |
11358d39 | 99 | |
8636aed8 | 100 | EVT_LIST_COL_CLICK(LIST_CTRL, MyListCtrl::OnColClick) |
11358d39 VZ |
101 | EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL, MyListCtrl::OnColRightClick) |
102 | EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnColBeginDrag) | |
103 | EVT_LIST_COL_DRAGGING(LIST_CTRL, MyListCtrl::OnColDragging) | |
104 | EVT_LIST_COL_END_DRAG(LIST_CTRL, MyListCtrl::OnColEndDrag) | |
105 | ||
614391dc | 106 | EVT_LIST_CACHE_HINT(LIST_CTRL, MyListCtrl::OnCacheHint) |
f6bcfd97 BP |
107 | |
108 | EVT_CHAR(MyListCtrl::OnChar) | |
457814b5 JS |
109 | END_EVENT_TABLE() |
110 | ||
111 | IMPLEMENT_APP(MyApp) | |
112 | ||
776a33cf VZ |
113 | // number of items in list/report view |
114 | static const int NUM_ITEMS = 30; | |
115 | ||
116 | // number of items in icon/small icon view | |
117 | static const int NUM_ICONS = 9; | |
118 | ||
c71963cd | 119 | int wxCALLBACK MyCompareFunction(long item1, long item2, long WXUNUSED(sortData)) |
fa5f6926 VZ |
120 | { |
121 | // inverse the order | |
c71963cd VZ |
122 | if (item1 < item2) |
123 | return -1; | |
124 | if (item1 > item2) | |
125 | return 1; | |
126 | ||
2b5f62a0 | 127 | return 0; |
fa5f6926 VZ |
128 | } |
129 | ||
457814b5 | 130 | // `Main program' equivalent, creating windows and returning main app frame |
11f26ea0 | 131 | bool MyApp::OnInit() |
457814b5 JS |
132 | { |
133 | // Create the main frame window | |
4acb6ca6 | 134 | MyFrame *frame = new MyFrame(wxT("wxListCtrl Test"), 50, 50, 450, 340); |
457814b5 JS |
135 | |
136 | // Show the frame | |
1550e402 | 137 | frame->Show(true); |
5ea47806 | 138 | |
457814b5 JS |
139 | SetTopWindow(frame); |
140 | ||
1550e402 | 141 | return true; |
457814b5 JS |
142 | } |
143 | ||
144 | // My frame constructor | |
c41ea66a | 145 | MyFrame::MyFrame(const wxChar *title, int x, int y, int w, int h) |
1550e402 | 146 | : wxFrame((wxFrame *)NULL, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)) |
457814b5 | 147 | { |
5ea47806 VZ |
148 | m_listCtrl = (MyListCtrl *) NULL; |
149 | m_logWindow = (wxTextCtrl *) NULL; | |
c41ea66a VZ |
150 | |
151 | // Give it an icon | |
152 | SetIcon( wxICON(mondrian) ); | |
153 | ||
154 | // Make an image list containing large icons | |
1550e402 WS |
155 | m_imageListNormal = new wxImageList(32, 32, true); |
156 | m_imageListSmall = new wxImageList(16, 16, true); | |
c41ea66a VZ |
157 | |
158 | #ifdef __WXMSW__ | |
42ed7532 MB |
159 | m_imageListNormal->Add( wxIcon(_T("icon1"), wxBITMAP_TYPE_ICO_RESOURCE) ); |
160 | m_imageListNormal->Add( wxIcon(_T("icon2"), wxBITMAP_TYPE_ICO_RESOURCE) ); | |
161 | m_imageListNormal->Add( wxIcon(_T("icon3"), wxBITMAP_TYPE_ICO_RESOURCE) ); | |
162 | m_imageListNormal->Add( wxIcon(_T("icon4"), wxBITMAP_TYPE_ICO_RESOURCE) ); | |
163 | m_imageListNormal->Add( wxIcon(_T("icon5"), wxBITMAP_TYPE_ICO_RESOURCE) ); | |
164 | m_imageListNormal->Add( wxIcon(_T("icon6"), wxBITMAP_TYPE_ICO_RESOURCE) ); | |
165 | m_imageListNormal->Add( wxIcon(_T("icon7"), wxBITMAP_TYPE_ICO_RESOURCE) ); | |
166 | m_imageListNormal->Add( wxIcon(_T("icon8"), wxBITMAP_TYPE_ICO_RESOURCE) ); | |
167 | m_imageListNormal->Add( wxIcon(_T("icon9"), wxBITMAP_TYPE_ICO_RESOURCE) ); | |
168 | ||
169 | m_imageListSmall->Add( wxIcon(_T("iconsmall"), wxBITMAP_TYPE_ICO_RESOURCE) ); | |
c41ea66a VZ |
170 | |
171 | #else | |
172 | m_imageListNormal->Add( wxIcon( toolbrai_xpm ) ); | |
173 | m_imageListNormal->Add( wxIcon( toolchar_xpm ) ); | |
174 | m_imageListNormal->Add( wxIcon( tooldata_xpm ) ); | |
175 | m_imageListNormal->Add( wxIcon( toolnote_xpm ) ); | |
176 | m_imageListNormal->Add( wxIcon( tooltodo_xpm ) ); | |
177 | m_imageListNormal->Add( wxIcon( toolchec_xpm ) ); | |
178 | m_imageListNormal->Add( wxIcon( toolgame_xpm ) ); | |
179 | m_imageListNormal->Add( wxIcon( tooltime_xpm ) ); | |
180 | m_imageListNormal->Add( wxIcon( toolword_xpm ) ); | |
181 | ||
182 | m_imageListSmall->Add( wxIcon( small1_xpm) ); | |
183 | #endif | |
184 | ||
185 | // Make a menubar | |
186 | wxMenu *menuFile = new wxMenu; | |
98ec9dbe | 187 | menuFile->Append(LIST_ABOUT, _T("&About")); |
c41ea66a | 188 | menuFile->AppendSeparator(); |
98ec9dbe | 189 | menuFile->Append(LIST_QUIT, _T("E&xit\tAlt-X")); |
c41ea66a VZ |
190 | |
191 | wxMenu *menuView = new wxMenu; | |
98ec9dbe VZ |
192 | menuView->Append(LIST_LIST_VIEW, _T("&List view\tF1")); |
193 | menuView->Append(LIST_REPORT_VIEW, _T("&Report view\tF2")); | |
194 | menuView->Append(LIST_ICON_VIEW, _T("&Icon view\tF3")); | |
195 | menuView->Append(LIST_ICON_TEXT_VIEW, _T("Icon view with &text\tF4")); | |
196 | menuView->Append(LIST_SMALL_ICON_VIEW, _T("&Small icon view\tF5")); | |
197 | menuView->Append(LIST_SMALL_ICON_TEXT_VIEW, _T("Small icon &view with text\tF6")); | |
198 | menuView->Append(LIST_VIRTUAL_VIEW, _T("Virtual view\tF7")); | |
c41ea66a VZ |
199 | |
200 | wxMenu *menuList = new wxMenu; | |
88b792af | 201 | menuList->Append(LIST_FOCUS_LAST, _T("&Make last item current\tCtrl-L")); |
c5c528fc | 202 | menuList->Append(LIST_TOGGLE_FIRST, _T("To&ggle first item\tCtrl-G")); |
98ec9dbe VZ |
203 | menuList->Append(LIST_DESELECT_ALL, _T("&Deselect All\tCtrl-D")); |
204 | menuList->Append(LIST_SELECT_ALL, _T("S&elect All\tCtrl-A")); | |
b54e41c5 | 205 | menuList->AppendSeparator(); |
98ec9dbe | 206 | menuList->Append(LIST_SHOW_COL_INFO, _T("Show &column info\tCtrl-C")); |
b54e41c5 | 207 | menuList->Append(LIST_SHOW_SEL_INFO, _T("Show &selected items\tCtrl-S")); |
c41ea66a | 208 | menuList->AppendSeparator(); |
98ec9dbe | 209 | menuList->Append(LIST_SORT, _T("&Sort\tCtrl-S")); |
c41ea66a | 210 | menuList->AppendSeparator(); |
cf1dfa6b | 211 | menuList->Append(LIST_ADD, _T("&Append an item\tCtrl-P")); |
efe23391 | 212 | menuList->Append(LIST_EDIT, _T("&Edit the item\tCtrl-E")); |
cf1dfa6b | 213 | menuList->Append(LIST_DELETE, _T("&Delete first item\tCtrl-X")); |
98ec9dbe | 214 | menuList->Append(LIST_DELETE_ALL, _T("Delete &all items")); |
c41ea66a | 215 | menuList->AppendSeparator(); |
c5c528fc VZ |
216 | menuList->Append(LIST_FREEZE, _T("Free&ze\tCtrl-Z")); |
217 | menuList->Append(LIST_THAW, _T("Tha&w\tCtrl-W")); | |
218 | menuList->AppendSeparator(); | |
494ab5de | 219 | menuList->AppendCheckItem(LIST_TOGGLE_LINES, _T("Toggle &lines\tCtrl-I")); |
98ec9dbe | 220 | menuList->Append(LIST_TOGGLE_MULTI_SEL, _T("&Multiple selection\tCtrl-M"), |
1550e402 | 221 | _T("Toggle multiple selection"), true); |
c41ea66a VZ |
222 | |
223 | wxMenu *menuCol = new wxMenu; | |
98ec9dbe VZ |
224 | menuCol->Append(LIST_SET_FG_COL, _T("&Foreground colour...")); |
225 | menuCol->Append(LIST_SET_BG_COL, _T("&Background colour...")); | |
c41ea66a VZ |
226 | |
227 | wxMenuBar *menubar = new wxMenuBar; | |
98ec9dbe VZ |
228 | menubar->Append(menuFile, _T("&File")); |
229 | menubar->Append(menuView, _T("&View")); | |
230 | menubar->Append(menuList, _T("&List")); | |
231 | menubar->Append(menuCol, _T("&Colour")); | |
c41ea66a VZ |
232 | SetMenuBar(menubar); |
233 | ||
1550e402 WS |
234 | m_panel = new wxPanel(this, wxID_ANY); |
235 | m_logWindow = new wxTextCtrl(m_panel, wxID_ANY, wxEmptyString, | |
c41ea66a VZ |
236 | wxDefaultPosition, wxDefaultSize, |
237 | wxTE_MULTILINE | wxSUNKEN_BORDER); | |
238 | ||
239 | m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow)); | |
240 | ||
776a33cf | 241 | RecreateList(wxLC_REPORT | wxLC_SINGLE_SEL); |
c41ea66a | 242 | |
8520f137 | 243 | #if wxUSE_STATUSBAR |
c41ea66a | 244 | CreateStatusBar(3); |
8520f137 | 245 | #endif // wxUSE_STATUSBAR |
457814b5 JS |
246 | } |
247 | ||
11f26ea0 | 248 | MyFrame::~MyFrame() |
457814b5 | 249 | { |
c41ea66a VZ |
250 | delete wxLog::SetActiveTarget(m_logOld); |
251 | ||
252 | delete m_imageListNormal; | |
253 | delete m_imageListSmall; | |
457814b5 JS |
254 | } |
255 | ||
98ec9dbe | 256 | void MyFrame::OnSize(wxSizeEvent& event) |
07bf769e VZ |
257 | { |
258 | DoSize(); | |
259 | ||
260 | event.Skip(); | |
261 | } | |
262 | ||
263 | void MyFrame::DoSize() | |
457814b5 | 264 | { |
98ec9dbe VZ |
265 | if ( !m_logWindow ) |
266 | return; | |
457814b5 | 267 | |
98ec9dbe VZ |
268 | wxSize size = GetClientSize(); |
269 | wxCoord y = (2*size.y)/3; | |
270 | m_listCtrl->SetSize(0, 0, size.x, y); | |
271 | m_logWindow->SetSize(0, y + 1, size.x, size.y - y); | |
57246713 KB |
272 | } |
273 | ||
68457180 VZ |
274 | bool MyFrame::CheckNonVirtual() const |
275 | { | |
276 | if ( !m_listCtrl->HasFlag(wxLC_VIRTUAL) ) | |
277 | return true; | |
278 | ||
279 | // "this" == whatever | |
280 | wxLogWarning(_T("Can't do this in virtual view, sorry.")); | |
281 | ||
282 | return false; | |
283 | } | |
284 | ||
98ec9dbe | 285 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
57246713 | 286 | { |
1550e402 | 287 | Close(true); |
57246713 KB |
288 | } |
289 | ||
b0d77f43 | 290 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 291 | { |
efe23391 VZ |
292 | wxMessageDialog dialog(this, _T("List test sample\nJulian Smart (c) 1997"), |
293 | _T("About list test"), wxOK|wxCANCEL); | |
457814b5 | 294 | |
5ea47806 | 295 | dialog.ShowModal(); |
457814b5 JS |
296 | } |
297 | ||
c71963cd | 298 | void MyFrame::OnFreeze(wxCommandEvent& WXUNUSED(event)) |
c5c528fc VZ |
299 | { |
300 | wxLogMessage(_T("Freezing the control")); | |
301 | ||
302 | m_listCtrl->Freeze(); | |
303 | } | |
304 | ||
c71963cd | 305 | void MyFrame::OnThaw(wxCommandEvent& WXUNUSED(event)) |
c5c528fc VZ |
306 | { |
307 | wxLogMessage(_T("Thawing the control")); | |
308 | ||
309 | m_listCtrl->Thaw(); | |
310 | } | |
311 | ||
494ab5de VZ |
312 | void MyFrame::OnToggleLines(wxCommandEvent& event) |
313 | { | |
314 | m_listCtrl->SetSingleStyle(wxLC_HRULES | wxLC_VRULES, event.IsChecked()); | |
315 | } | |
316 | ||
88b792af VZ |
317 | void MyFrame::OnFocusLast(wxCommandEvent& WXUNUSED(event)) |
318 | { | |
319 | long index = m_listCtrl->GetItemCount() - 1; | |
320 | if ( index == -1 ) | |
321 | { | |
322 | return; | |
323 | } | |
324 | ||
325 | m_listCtrl->SetItemState(index, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED); | |
326 | m_listCtrl->EnsureVisible(index); | |
327 | } | |
328 | ||
58b3bdc9 VZ |
329 | void MyFrame::OnToggleFirstSel(wxCommandEvent& WXUNUSED(event)) |
330 | { | |
95f913a9 | 331 | m_listCtrl->SetItemState(0, (~m_listCtrl->GetItemState(0, wxLIST_STATE_SELECTED) ) & wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED); |
58b3bdc9 VZ |
332 | } |
333 | ||
c4771147 KB |
334 | void MyFrame::OnDeselectAll(wxCommandEvent& WXUNUSED(event)) |
335 | { | |
68457180 VZ |
336 | if ( !CheckNonVirtual() ) |
337 | return; | |
338 | ||
5ea47806 VZ |
339 | int n = m_listCtrl->GetItemCount(); |
340 | for (int i = 0; i < n; i++) | |
341 | m_listCtrl->SetItemState(i,0,wxLIST_STATE_SELECTED); | |
c4771147 KB |
342 | } |
343 | ||
344 | void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event)) | |
345 | { | |
68457180 VZ |
346 | if ( !CheckNonVirtual() ) |
347 | return; | |
348 | ||
5ea47806 VZ |
349 | int n = m_listCtrl->GetItemCount(); |
350 | for (int i = 0; i < n; i++) | |
351 | m_listCtrl->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED); | |
c4771147 KB |
352 | } |
353 | ||
776a33cf VZ |
354 | // ---------------------------------------------------------------------------- |
355 | // changing listctrl modes | |
356 | // ---------------------------------------------------------------------------- | |
357 | ||
358 | void MyFrame::RecreateList(long flags, bool withText) | |
457814b5 | 359 | { |
776a33cf VZ |
360 | // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL |
361 | // style, but it is more trouble to do it than not | |
362 | #if 0 | |
363 | if ( !m_listCtrl || ((flags & wxLC_VIRTUAL) != | |
364 | (m_listCtrl->GetWindowStyleFlag() & wxLC_VIRTUAL)) ) | |
365 | #endif | |
366 | { | |
367 | delete m_listCtrl; | |
368 | ||
2b5f62a0 | 369 | m_listCtrl = new MyListCtrl(m_panel, LIST_CTRL, |
776a33cf VZ |
370 | wxDefaultPosition, wxDefaultSize, |
371 | flags | | |
efe23391 | 372 | wxSUNKEN_BORDER | wxLC_EDIT_LABELS); |
776a33cf VZ |
373 | |
374 | switch ( flags & wxLC_MASK_TYPE ) | |
375 | { | |
376 | case wxLC_LIST: | |
377 | InitWithListItems(); | |
378 | break; | |
379 | ||
380 | case wxLC_ICON: | |
381 | InitWithIconItems(withText); | |
382 | break; | |
383 | ||
384 | case wxLC_SMALL_ICON: | |
1550e402 | 385 | InitWithIconItems(withText, true); |
776a33cf VZ |
386 | break; |
387 | ||
388 | case wxLC_REPORT: | |
389 | if ( flags & wxLC_VIRTUAL ) | |
390 | InitWithVirtualItems(); | |
391 | else | |
392 | InitWithReportItems(); | |
393 | break; | |
394 | ||
395 | default: | |
396 | wxFAIL_MSG( _T("unknown listctrl mode") ); | |
397 | } | |
398 | } | |
399 | ||
07bf769e | 400 | DoSize(); |
776a33cf | 401 | |
0180dad6 | 402 | m_logWindow->Clear(); |
776a33cf | 403 | } |
95bf655c | 404 | |
776a33cf VZ |
405 | void MyFrame::OnListView(wxCommandEvent& WXUNUSED(event)) |
406 | { | |
407 | RecreateList(wxLC_LIST); | |
408 | } | |
457814b5 | 409 | |
776a33cf VZ |
410 | void MyFrame::InitWithListItems() |
411 | { | |
412 | for ( int i = 0; i < NUM_ITEMS; i++ ) | |
0180dad6 | 413 | { |
98ec9dbe | 414 | m_listCtrl->InsertItem(i, wxString::Format(_T("Item %d"), i)); |
0180dad6 | 415 | } |
457814b5 JS |
416 | } |
417 | ||
b0d77f43 | 418 | void MyFrame::OnReportView(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 419 | { |
776a33cf VZ |
420 | RecreateList(wxLC_REPORT); |
421 | } | |
5ea47806 | 422 | |
776a33cf VZ |
423 | void MyFrame::InitWithReportItems() |
424 | { | |
c41ea66a | 425 | m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL); |
457814b5 | 426 | |
caa20b1e VZ |
427 | // note that under MSW for SetColumnWidth() to work we need to create the |
428 | // items with images initially even if we specify dummy image id | |
6f806543 | 429 | wxListItem itemCol; |
caa20b1e VZ |
430 | itemCol.SetText(_T("Column 1")); |
431 | itemCol.SetImage(-1); | |
6f806543 | 432 | m_listCtrl->InsertColumn(0, itemCol); |
caa20b1e VZ |
433 | |
434 | itemCol.SetText(_T("Column 2")); | |
435 | itemCol.SetAlign(wxLIST_FORMAT_CENTRE); | |
6f806543 | 436 | m_listCtrl->InsertColumn(1, itemCol); |
caa20b1e VZ |
437 | |
438 | itemCol.SetText(_T("Column 3")); | |
439 | itemCol.SetAlign(wxLIST_FORMAT_RIGHT); | |
6f806543 | 440 | m_listCtrl->InsertColumn(2, itemCol); |
0180dad6 | 441 | |
81278df2 VZ |
442 | // to speed up inserting we hide the control temporarily |
443 | m_listCtrl->Hide(); | |
444 | ||
445 | wxStopWatch sw; | |
446 | ||
81278df2 | 447 | for ( int i = 0; i < NUM_ITEMS; i++ ) |
0180dad6 | 448 | { |
5cd89174 | 449 | m_listCtrl->InsertItemInReportView(i); |
0180dad6 | 450 | } |
bdc72a22 | 451 | |
81278df2 VZ |
452 | m_logWindow->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"), |
453 | NUM_ITEMS, sw.Time())); | |
454 | m_listCtrl->Show(); | |
455 | ||
f6b77239 | 456 | // we leave all mask fields to 0 and only change the colour |
bdc72a22 VZ |
457 | wxListItem item; |
458 | item.m_itemId = 0; | |
0530737d | 459 | item.SetTextColour(*wxRED); |
bdc72a22 VZ |
460 | m_listCtrl->SetItem( item ); |
461 | ||
462 | item.m_itemId = 2; | |
d62228a6 | 463 | item.SetTextColour(*wxGREEN); |
bdc72a22 | 464 | m_listCtrl->SetItem( item ); |
d62228a6 | 465 | item.m_itemId = 4; |
bdc72a22 | 466 | item.SetTextColour(*wxLIGHT_GREY); |
d62228a6 VZ |
467 | item.SetFont(*wxITALIC_FONT); |
468 | item.SetBackgroundColour(*wxRED); | |
bdc72a22 | 469 | m_listCtrl->SetItem( item ); |
5ea47806 | 470 | |
d62228a6 VZ |
471 | m_listCtrl->SetTextColour(*wxBLUE); |
472 | m_listCtrl->SetBackgroundColour(*wxLIGHT_GREY); | |
473 | ||
0180dad6 RR |
474 | m_listCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE ); |
475 | m_listCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE ); | |
476 | m_listCtrl->SetColumnWidth( 2, wxLIST_AUTOSIZE ); | |
457814b5 JS |
477 | } |
478 | ||
776a33cf | 479 | void MyFrame::InitWithIconItems(bool withText, bool sameIcon) |
457814b5 | 480 | { |
c41ea66a VZ |
481 | m_listCtrl->SetImageList(m_imageListNormal, wxIMAGE_LIST_NORMAL); |
482 | m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL); | |
457814b5 | 483 | |
776a33cf | 484 | for ( int i = 0; i < NUM_ICONS; i++ ) |
5ea47806 | 485 | { |
776a33cf VZ |
486 | int image = sameIcon ? 0 : i; |
487 | ||
488 | if ( withText ) | |
489 | { | |
490 | m_listCtrl->InsertItem(i, wxString::Format(_T("Label %d"), i), | |
491 | image); | |
492 | } | |
493 | else | |
494 | { | |
495 | m_listCtrl->InsertItem(i, image); | |
496 | } | |
5ea47806 | 497 | } |
457814b5 JS |
498 | } |
499 | ||
776a33cf | 500 | void MyFrame::OnIconView(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 501 | { |
1550e402 | 502 | RecreateList(wxLC_ICON, false); |
776a33cf | 503 | } |
457814b5 | 504 | |
776a33cf VZ |
505 | void MyFrame::OnIconTextView(wxCommandEvent& WXUNUSED(event)) |
506 | { | |
507 | RecreateList(wxLC_ICON); | |
457814b5 JS |
508 | } |
509 | ||
b0d77f43 | 510 | void MyFrame::OnSmallIconView(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 511 | { |
1550e402 | 512 | RecreateList(wxLC_SMALL_ICON, false); |
457814b5 JS |
513 | } |
514 | ||
b0d77f43 | 515 | void MyFrame::OnSmallIconTextView(wxCommandEvent& WXUNUSED(event)) |
457814b5 | 516 | { |
776a33cf | 517 | RecreateList(wxLC_SMALL_ICON); |
5ea47806 VZ |
518 | } |
519 | ||
98ec9dbe VZ |
520 | void MyFrame::OnVirtualView(wxCommandEvent& WXUNUSED(event)) |
521 | { | |
776a33cf VZ |
522 | RecreateList(wxLC_REPORT | wxLC_VIRTUAL); |
523 | } | |
98ec9dbe | 524 | |
776a33cf VZ |
525 | void MyFrame::InitWithVirtualItems() |
526 | { | |
27770773 VZ |
527 | m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL); |
528 | ||
efe23391 VZ |
529 | m_listCtrl->InsertColumn(0, _T("First Column")); |
530 | m_listCtrl->InsertColumn(1, _T("Second Column")); | |
98ec9dbe VZ |
531 | m_listCtrl->SetColumnWidth(0, 150); |
532 | m_listCtrl->SetColumnWidth(1, 150); | |
533 | ||
534 | m_listCtrl->SetItemCount(1000000); | |
98ec9dbe VZ |
535 | } |
536 | ||
fa5f6926 VZ |
537 | void MyFrame::OnSort(wxCommandEvent& WXUNUSED(event)) |
538 | { | |
81278df2 VZ |
539 | wxStopWatch sw; |
540 | ||
fa5f6926 | 541 | m_listCtrl->SortItems(MyCompareFunction, 0); |
81278df2 VZ |
542 | |
543 | m_logWindow->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"), | |
544 | m_listCtrl->GetItemCount(), | |
545 | sw.Time())); | |
fa5f6926 VZ |
546 | } |
547 | ||
c71963cd | 548 | void MyFrame::OnShowSelInfo(wxCommandEvent& WXUNUSED(event)) |
b54e41c5 VZ |
549 | { |
550 | int selCount = m_listCtrl->GetSelectedItemCount(); | |
551 | wxLogMessage(_T("%d items selected:"), selCount); | |
552 | ||
553 | // don't show too many items | |
554 | size_t shownCount = 0; | |
555 | ||
556 | long item = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL, | |
557 | wxLIST_STATE_SELECTED); | |
558 | while ( item != -1 ) | |
559 | { | |
560 | wxLogMessage(_T("\t%ld (%s)"), | |
561 | item, m_listCtrl->GetItemText(item).c_str()); | |
562 | ||
563 | if ( ++shownCount > 10 ) | |
564 | { | |
565 | wxLogMessage(_T("\t... more selected items snipped...")); | |
566 | break; | |
567 | } | |
568 | ||
569 | item = m_listCtrl->GetNextItem(item, wxLIST_NEXT_ALL, | |
570 | wxLIST_STATE_SELECTED); | |
571 | } | |
572 | } | |
573 | ||
c71963cd | 574 | void MyFrame::OnShowColInfo(wxCommandEvent& WXUNUSED(event)) |
f6bcfd97 BP |
575 | { |
576 | int count = m_listCtrl->GetColumnCount(); | |
4acb6ca6 | 577 | wxLogMessage(wxT("%d columns:"), count); |
f6bcfd97 BP |
578 | for ( int c = 0; c < count; c++ ) |
579 | { | |
4acb6ca6 | 580 | wxLogMessage(wxT("\tcolumn %d has width %d"), c, |
f6bcfd97 BP |
581 | m_listCtrl->GetColumnWidth(c)); |
582 | } | |
583 | } | |
584 | ||
585 | void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent& event) | |
586 | { | |
587 | event.Enable( (m_listCtrl->GetWindowStyleFlag() & wxLC_REPORT) != 0 ); | |
588 | } | |
589 | ||
7b848b0d VZ |
590 | void MyFrame::OnToggleMultiSel(wxCommandEvent& WXUNUSED(event)) |
591 | { | |
7b848b0d VZ |
592 | long flags = m_listCtrl->GetWindowStyleFlag(); |
593 | if ( flags & wxLC_SINGLE_SEL ) | |
776a33cf | 594 | flags &= ~wxLC_SINGLE_SEL; |
7b848b0d | 595 | else |
776a33cf VZ |
596 | flags |= wxLC_SINGLE_SEL; |
597 | ||
4acb6ca6 | 598 | m_logWindow->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"), |
efe23391 | 599 | (flags & wxLC_SINGLE_SEL) ? _T("sing") : _T("multip"))); |
7b848b0d | 600 | |
776a33cf | 601 | RecreateList(flags); |
7b848b0d VZ |
602 | } |
603 | ||
ae403b11 JS |
604 | void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent& event) |
605 | { | |
606 | event.Check((m_listCtrl->GetWindowStyleFlag() & wxLC_SINGLE_SEL) == 0); | |
607 | } | |
608 | ||
11f26ea0 VZ |
609 | void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event)) |
610 | { | |
611 | m_listCtrl->SetForegroundColour(wxGetColourFromUser(this)); | |
612 | m_listCtrl->Refresh(); | |
613 | } | |
614 | ||
615 | void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event)) | |
616 | { | |
617 | m_listCtrl->SetBackgroundColour(wxGetColourFromUser(this)); | |
618 | m_listCtrl->Refresh(); | |
619 | } | |
620 | ||
cf1dfa6b VZ |
621 | void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event)) |
622 | { | |
623 | m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), _T("Appended item")); | |
624 | } | |
625 | ||
efe23391 VZ |
626 | void MyFrame::OnEdit(wxCommandEvent& WXUNUSED(event)) |
627 | { | |
628 | long itemCur = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL, | |
629 | wxLIST_STATE_FOCUSED); | |
630 | ||
631 | if ( itemCur != -1 ) | |
632 | { | |
633 | m_listCtrl->EditLabel(itemCur); | |
634 | } | |
635 | else | |
636 | { | |
637 | m_logWindow->WriteText(_T("No item to edit")); | |
638 | } | |
639 | } | |
640 | ||
bec0a261 VZ |
641 | void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event)) |
642 | { | |
643 | if ( m_listCtrl->GetItemCount() ) | |
644 | { | |
645 | m_listCtrl->DeleteItem(0); | |
646 | } | |
647 | else | |
648 | { | |
efe23391 | 649 | m_logWindow->WriteText(_T("Nothing to delete")); |
bec0a261 VZ |
650 | } |
651 | } | |
652 | ||
5ea47806 VZ |
653 | void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event)) |
654 | { | |
81278df2 | 655 | wxStopWatch sw; |
5ea47806 | 656 | |
5ea47806 VZ |
657 | m_listCtrl->DeleteAllItems(); |
658 | ||
81278df2 VZ |
659 | m_logWindow->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"), |
660 | m_listCtrl->GetItemCount(), | |
661 | sw.Time())); | |
457814b5 JS |
662 | } |
663 | ||
664 | // MyListCtrl | |
665 | ||
614391dc VZ |
666 | void MyListCtrl::OnCacheHint(wxListEvent& event) |
667 | { | |
4acb6ca6 | 668 | wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"), |
614391dc VZ |
669 | event.GetCacheFrom(), event.GetCacheTo() ); |
670 | } | |
671 | ||
6f806543 VZ |
672 | void MyListCtrl::SetColumnImage(int col, int image) |
673 | { | |
674 | wxListItem item; | |
675 | item.SetMask(wxLIST_MASK_IMAGE); | |
676 | item.SetImage(image); | |
677 | SetColumn(col, item); | |
678 | } | |
679 | ||
8636aed8 RR |
680 | void MyListCtrl::OnColClick(wxListEvent& event) |
681 | { | |
6f806543 VZ |
682 | int col = event.GetColumn(); |
683 | SetColumnImage(col, 0); | |
684 | ||
685 | wxLogMessage( wxT("OnColumnClick at %d."), col ); | |
8636aed8 RR |
686 | } |
687 | ||
11358d39 VZ |
688 | void MyListCtrl::OnColRightClick(wxListEvent& event) |
689 | { | |
6f806543 | 690 | int col = event.GetColumn(); |
62313c27 VZ |
691 | if ( col != -1 ) |
692 | { | |
693 | SetColumnImage(col, -1); | |
694 | } | |
6f806543 | 695 | |
77ace0c5 WS |
696 | // Show popupmenu at position |
697 | wxMenu menu(wxT("Test")); | |
698 | menu.Append(LIST_ABOUT, _T("&About")); | |
699 | PopupMenu(&menu, event.GetPoint()); | |
700 | ||
11358d39 VZ |
701 | wxLogMessage( wxT("OnColumnRightClick at %d."), event.GetColumn() ); |
702 | } | |
703 | ||
2b5f62a0 VZ |
704 | void MyListCtrl::LogColEvent(const wxListEvent& event, const wxChar *name) |
705 | { | |
706 | const int col = event.GetColumn(); | |
707 | ||
708 | wxLogMessage(wxT("%s: column %d (width = %d or %d)."), | |
709 | name, | |
710 | col, | |
711 | event.GetItem().GetWidth(), | |
712 | GetColumnWidth(col)); | |
713 | } | |
714 | ||
11358d39 VZ |
715 | void MyListCtrl::OnColBeginDrag(wxListEvent& event) |
716 | { | |
2b5f62a0 | 717 | LogColEvent( event, wxT("OnColBeginDrag") ); |
355f2407 VZ |
718 | |
719 | if ( event.GetColumn() == 0 ) | |
720 | { | |
721 | wxLogMessage(_T("Resizing this column shouldn't work.")); | |
722 | ||
723 | event.Veto(); | |
724 | } | |
11358d39 VZ |
725 | } |
726 | ||
727 | void MyListCtrl::OnColDragging(wxListEvent& event) | |
728 | { | |
2b5f62a0 | 729 | LogColEvent( event, wxT("OnColDragging") ); |
11358d39 VZ |
730 | } |
731 | ||
732 | void MyListCtrl::OnColEndDrag(wxListEvent& event) | |
733 | { | |
2b5f62a0 | 734 | LogColEvent( event, wxT("OnColEndDrag") ); |
11358d39 VZ |
735 | } |
736 | ||
fd9811b1 | 737 | void MyListCtrl::OnBeginDrag(wxListEvent& event) |
457814b5 | 738 | { |
6dc34ebb VZ |
739 | const wxPoint& pt = event.m_pointDrag; |
740 | ||
741 | int flags; | |
742 | wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."), | |
743 | pt.x, pt.y, HitTest(pt, flags) ); | |
457814b5 JS |
744 | } |
745 | ||
fd9811b1 | 746 | void MyListCtrl::OnBeginRDrag(wxListEvent& event) |
457814b5 | 747 | { |
4acb6ca6 | 748 | wxLogMessage( wxT("OnBeginRDrag at %d,%d."), |
c41ea66a | 749 | event.m_pointDrag.x, event.m_pointDrag.y ); |
457814b5 JS |
750 | } |
751 | ||
fd9811b1 | 752 | void MyListCtrl::OnBeginLabelEdit(wxListEvent& event) |
457814b5 | 753 | { |
4acb6ca6 | 754 | wxLogMessage( wxT("OnBeginLabelEdit: %s"), event.m_item.m_text.c_str()); |
457814b5 JS |
755 | } |
756 | ||
fd9811b1 | 757 | void MyListCtrl::OnEndLabelEdit(wxListEvent& event) |
457814b5 | 758 | { |
1bf37bf5 VZ |
759 | wxLogMessage( wxT("OnEndLabelEdit: %s"), |
760 | event.IsEditCancelled() ? _T("[cancelled]") | |
761 | : event.m_item.m_text.c_str()); | |
457814b5 JS |
762 | } |
763 | ||
efbb7287 | 764 | void MyListCtrl::OnDeleteItem(wxListEvent& event) |
457814b5 | 765 | { |
c41ea66a | 766 | LogEvent(event, _T("OnDeleteItem")); |
457814b5 JS |
767 | } |
768 | ||
c41ea66a | 769 | void MyListCtrl::OnDeleteAllItems(wxListEvent& event) |
12c1b46a | 770 | { |
c41ea66a | 771 | LogEvent(event, _T("OnDeleteAllItems")); |
12c1b46a RR |
772 | } |
773 | ||
fd9811b1 | 774 | void MyListCtrl::OnGetInfo(wxListEvent& event) |
457814b5 | 775 | { |
c41ea66a | 776 | wxString msg; |
5ea47806 | 777 | |
efe23391 | 778 | msg << _T("OnGetInfo (") << event.m_item.m_itemId << _T(", ") << event.m_item.m_col << _T(")"); |
5ea47806 | 779 | if ( event.m_item.m_mask & wxLIST_MASK_STATE ) |
efe23391 | 780 | msg << _T(" wxLIST_MASK_STATE"); |
5ea47806 | 781 | if ( event.m_item.m_mask & wxLIST_MASK_TEXT ) |
efe23391 | 782 | msg << _T(" wxLIST_MASK_TEXT"); |
5ea47806 | 783 | if ( event.m_item.m_mask & wxLIST_MASK_IMAGE ) |
efe23391 | 784 | msg << _T(" wxLIST_MASK_IMAGE"); |
5ea47806 | 785 | if ( event.m_item.m_mask & wxLIST_MASK_DATA ) |
efe23391 | 786 | msg << _T(" wxLIST_MASK_DATA"); |
5ea47806 | 787 | if ( event.m_item.m_mask & wxLIST_SET_ITEM ) |
efe23391 | 788 | msg << _T(" wxLIST_SET_ITEM"); |
5ea47806 | 789 | if ( event.m_item.m_mask & wxLIST_MASK_WIDTH ) |
efe23391 | 790 | msg << _T(" wxLIST_MASK_WIDTH"); |
5ea47806 | 791 | if ( event.m_item.m_mask & wxLIST_MASK_FORMAT ) |
efe23391 | 792 | msg << _T(" wxLIST_MASK_WIDTH"); |
5ea47806 VZ |
793 | |
794 | if ( event.m_item.m_mask & wxLIST_MASK_TEXT ) | |
795 | { | |
efe23391 | 796 | event.m_item.m_text = _T("My callback text"); |
5ea47806 | 797 | } |
c41ea66a VZ |
798 | |
799 | wxLogMessage(msg); | |
457814b5 JS |
800 | } |
801 | ||
efbb7287 | 802 | void MyListCtrl::OnSetInfo(wxListEvent& event) |
457814b5 | 803 | { |
c41ea66a | 804 | LogEvent(event, _T("OnSetInfo")); |
457814b5 JS |
805 | } |
806 | ||
74b31181 | 807 | void MyListCtrl::OnSelected(wxListEvent& event) |
457814b5 | 808 | { |
c41ea66a | 809 | LogEvent(event, _T("OnSelected")); |
457814b5 | 810 | |
40779a03 | 811 | if ( GetWindowStyle() & wxLC_REPORT ) |
74b31181 | 812 | { |
40779a03 VZ |
813 | wxListItem info; |
814 | info.m_itemId = event.m_itemIndex; | |
815 | info.m_col = 1; | |
816 | info.m_mask = wxLIST_MASK_TEXT; | |
817 | if ( GetItem(info) ) | |
818 | { | |
4acb6ca6 | 819 | wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"), |
c41ea66a | 820 | info.m_text.c_str()); |
40779a03 VZ |
821 | } |
822 | else | |
823 | { | |
4acb6ca6 | 824 | wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed")); |
40779a03 | 825 | } |
74b31181 | 826 | } |
457814b5 JS |
827 | } |
828 | ||
efbb7287 | 829 | void MyListCtrl::OnDeselected(wxListEvent& event) |
457814b5 | 830 | { |
c41ea66a | 831 | LogEvent(event, _T("OnDeselected")); |
457814b5 JS |
832 | } |
833 | ||
efbb7287 | 834 | void MyListCtrl::OnActivated(wxListEvent& event) |
435fe83e | 835 | { |
c41ea66a | 836 | LogEvent(event, _T("OnActivated")); |
435fe83e RR |
837 | } |
838 | ||
0ddefeb0 VZ |
839 | void MyListCtrl::OnFocused(wxListEvent& event) |
840 | { | |
841 | LogEvent(event, _T("OnFocused")); | |
27770773 VZ |
842 | |
843 | event.Skip(); | |
0ddefeb0 VZ |
844 | } |
845 | ||
8e1d4f96 | 846 | void MyListCtrl::OnListKeyDown(wxListEvent& event) |
457814b5 | 847 | { |
2936eaf0 | 848 | switch ( event.GetKeyCode() ) |
5cd89174 | 849 | { |
2d33aec9 VZ |
850 | case 'c': // colorize |
851 | case 'C': | |
6c02c329 VZ |
852 | { |
853 | wxListItem info; | |
854 | info.m_itemId = event.GetIndex(); | |
855 | GetItem(info); | |
856 | ||
857 | wxListItemAttr *attr = info.GetAttributes(); | |
858 | if ( !attr || !attr->HasTextColour() ) | |
859 | { | |
860 | info.SetTextColour(*wxCYAN); | |
861 | ||
862 | SetItem(info); | |
2d33aec9 VZ |
863 | |
864 | RefreshItem(info.m_itemId); | |
6c02c329 VZ |
865 | } |
866 | } | |
867 | break; | |
868 | ||
2d33aec9 VZ |
869 | case 'n': // next |
870 | case 'N': | |
871 | { | |
872 | long item = GetNextItem(-1, | |
873 | wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED); | |
874 | if ( item++ == GetItemCount() - 1 ) | |
875 | { | |
876 | item = 0; | |
877 | } | |
878 | ||
879 | wxLogMessage(_T("Focusing item %ld"), item); | |
880 | ||
881 | SetItemState(item, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED); | |
882 | EnsureVisible(item); | |
883 | } | |
884 | break; | |
885 | ||
5cd89174 | 886 | case WXK_DELETE: |
91c6cc0e VZ |
887 | { |
888 | long item = GetNextItem(-1, | |
889 | wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED); | |
890 | while ( item != -1 ) | |
891 | { | |
892 | DeleteItem(item); | |
893 | ||
894 | wxLogMessage(_T("Item %ld deleted"), item); | |
f6bcfd97 | 895 | |
91c6cc0e VZ |
896 | // -1 because the indices were shifted by DeleteItem() |
897 | item = GetNextItem(item - 1, | |
898 | wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED); | |
899 | } | |
900 | } | |
5cd89174 VZ |
901 | break; |
902 | ||
903 | case WXK_INSERT: | |
904 | if ( GetWindowStyle() & wxLC_REPORT ) | |
905 | { | |
906 | if ( GetWindowStyle() & wxLC_VIRTUAL ) | |
907 | { | |
908 | SetItemCount(GetItemCount() + 1); | |
909 | } | |
910 | else // !virtual | |
911 | { | |
912 | InsertItemInReportView(event.GetIndex()); | |
913 | } | |
914 | } | |
915 | //else: fall through | |
916 | ||
917 | default: | |
918 | LogEvent(event, _T("OnListKeyDown")); | |
919 | ||
920 | event.Skip(); | |
921 | } | |
f6bcfd97 BP |
922 | } |
923 | ||
924 | void MyListCtrl::OnChar(wxKeyEvent& event) | |
925 | { | |
926 | wxLogMessage(_T("Got char event.")); | |
927 | ||
2d33aec9 VZ |
928 | switch ( event.GetKeyCode() ) |
929 | { | |
930 | case 'n': | |
931 | case 'N': | |
932 | case 'c': | |
933 | case 'C': | |
934 | // these are the keys we process ourselves | |
935 | break; | |
936 | ||
937 | default: | |
938 | event.Skip(); | |
939 | } | |
457814b5 JS |
940 | } |
941 | ||
c41ea66a VZ |
942 | void MyListCtrl::LogEvent(const wxListEvent& event, const wxChar *eventName) |
943 | { | |
f6bcfd97 BP |
944 | wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"), |
945 | event.GetIndex(), eventName, | |
946 | event.GetText().c_str(), event.GetData()); | |
c41ea66a | 947 | } |
435fe83e | 948 | |
98ec9dbe VZ |
949 | wxString MyListCtrl::OnGetItemText(long item, long column) const |
950 | { | |
951 | return wxString::Format(_T("Column %ld of item %ld"), column, item); | |
952 | } | |
953 | ||
c71963cd | 954 | int MyListCtrl::OnGetItemImage(long WXUNUSED(item)) const |
98ec9dbe VZ |
955 | { |
956 | return 0; | |
957 | } | |
958 | ||
6c02c329 VZ |
959 | wxListItemAttr *MyListCtrl::OnGetItemAttr(long item) const |
960 | { | |
961 | return item % 2 ? NULL : (wxListItemAttr *)&m_attr; | |
962 | } | |
963 | ||
5cd89174 VZ |
964 | void MyListCtrl::InsertItemInReportView(int i) |
965 | { | |
966 | wxString buf; | |
967 | buf.Printf(_T("This is item %d"), i); | |
968 | long tmp = InsertItem(i, buf, 0); | |
969 | SetItemData(tmp, i); | |
970 | ||
971 | buf.Printf(_T("Col 1, item %d"), i); | |
972 | SetItem(i, 1, buf); | |
973 | ||
974 | buf.Printf(_T("Item %d in column 2"), i); | |
975 | SetItem(i, 2, buf); | |
976 | } | |
977 |