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