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