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