]>
Commit | Line | Data |
---|---|---|
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$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
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 | ||
23 | #if !defined(__WXMSW__) && !defined(__WXPM__) | |
24 | #include "mondrian.xpm" | |
25 | #endif | |
26 | ||
27 | #ifndef __WXMSW__ | |
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" | |
38 | #endif | |
39 | ||
40 | #include "wx/imaglist.h" | |
41 | #include "wx/listctrl.h" | |
42 | #include "wx/timer.h" // for wxStopWatch | |
43 | #include "wx/colordlg.h" // for wxGetColourFromUser | |
44 | #include "wx/settings.h" | |
45 | #include "wx/sysopt.h" | |
46 | ||
47 | #include "listtest.h" | |
48 | ||
49 | ||
50 | // ---------------------------------------------------------------------------- | |
51 | // Constants and globals | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
54 | const wxChar *SMALL_VIRTUAL_VIEW_ITEMS[][2] = | |
55 | { | |
56 | { _T("Cat"), _T("meow") }, | |
57 | { _T("Cow"), _T("moo") }, | |
58 | { _T("Crow"), _T("caw") }, | |
59 | { _T("Dog"), _T("woof") }, | |
60 | { _T("Duck"), _T("quack") }, | |
61 | { _T("Mouse"), _T("squeak") }, | |
62 | { _T("Owl"), _T("hoo") }, | |
63 | { _T("Pig"), _T("oink") }, | |
64 | { _T("Pigeon"), _T("coo") }, | |
65 | { _T("Sheep"), _T("baaah") }, | |
66 | }; | |
67 | ||
68 | // number of items in list/report view | |
69 | static const int NUM_ITEMS = 10; | |
70 | ||
71 | // number of items in icon/small icon view | |
72 | static const int NUM_ICONS = 9; | |
73 | ||
74 | int wxCALLBACK | |
75 | MyCompareFunction(long item1, long item2, wxIntPtr WXUNUSED(sortData)) | |
76 | { | |
77 | // inverse the order | |
78 | if (item1 < item2) | |
79 | return -1; | |
80 | if (item1 > item2) | |
81 | return 1; | |
82 | ||
83 | return 0; | |
84 | } | |
85 | ||
86 | ||
87 | // ---------------------------------------------------------------------------- | |
88 | // MyApp | |
89 | // ---------------------------------------------------------------------------- | |
90 | ||
91 | IMPLEMENT_APP(MyApp) | |
92 | ||
93 | // `Main program' equivalent, creating windows and returning main app frame | |
94 | bool MyApp::OnInit() | |
95 | { | |
96 | if ( !wxApp::OnInit() ) | |
97 | return false; | |
98 | ||
99 | // Create the main frame window | |
100 | MyFrame *frame = new MyFrame(wxT("wxListCtrl Test")); | |
101 | ||
102 | // Show the frame | |
103 | frame->Show(true); | |
104 | ||
105 | SetTopWindow(frame); | |
106 | ||
107 | return true; | |
108 | } | |
109 | ||
110 | ||
111 | ||
112 | // ---------------------------------------------------------------------------- | |
113 | // MyFrame | |
114 | // ---------------------------------------------------------------------------- | |
115 | ||
116 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
117 | EVT_SIZE(MyFrame::OnSize) | |
118 | ||
119 | EVT_MENU(LIST_QUIT, MyFrame::OnQuit) | |
120 | EVT_MENU(LIST_ABOUT, MyFrame::OnAbout) | |
121 | EVT_MENU(LIST_LIST_VIEW, MyFrame::OnListView) | |
122 | EVT_MENU(LIST_REPORT_VIEW, MyFrame::OnReportView) | |
123 | EVT_MENU(LIST_ICON_VIEW, MyFrame::OnIconView) | |
124 | EVT_MENU(LIST_ICON_TEXT_VIEW, MyFrame::OnIconTextView) | |
125 | EVT_MENU(LIST_SMALL_ICON_VIEW, MyFrame::OnSmallIconView) | |
126 | EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView) | |
127 | EVT_MENU(LIST_VIRTUAL_VIEW, MyFrame::OnVirtualView) | |
128 | EVT_MENU(LIST_SMALL_VIRTUAL_VIEW, MyFrame::OnSmallVirtualView) | |
129 | ||
130 | EVT_MENU(LIST_GOTO, MyFrame::OnGoTo) | |
131 | EVT_MENU(LIST_FOCUS_LAST, MyFrame::OnFocusLast) | |
132 | EVT_MENU(LIST_TOGGLE_FIRST, MyFrame::OnToggleFirstSel) | |
133 | EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll) | |
134 | EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll) | |
135 | EVT_MENU(LIST_DELETE, MyFrame::OnDelete) | |
136 | EVT_MENU(LIST_ADD, MyFrame::OnAdd) | |
137 | EVT_MENU(LIST_EDIT, MyFrame::OnEdit) | |
138 | EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll) | |
139 | EVT_MENU(LIST_SORT, MyFrame::OnSort) | |
140 | EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour) | |
141 | EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour) | |
142 | EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel) | |
143 | EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo) | |
144 | EVT_MENU(LIST_SHOW_SEL_INFO, MyFrame::OnShowSelInfo) | |
145 | EVT_MENU(LIST_SHOW_VIEW_RECT, MyFrame::OnShowViewRect) | |
146 | #ifdef wxHAS_LISTCTRL_COLUMN_ORDER | |
147 | EVT_MENU(LIST_SET_COL_ORDER, MyFrame::OnSetColOrder) | |
148 | EVT_MENU(LIST_GET_COL_ORDER, MyFrame::OnGetColOrder) | |
149 | #endif // wxHAS_LISTCTRL_COLUMN_ORDER | |
150 | EVT_MENU(LIST_FREEZE, MyFrame::OnFreeze) | |
151 | EVT_MENU(LIST_THAW, MyFrame::OnThaw) | |
152 | EVT_MENU(LIST_TOGGLE_LINES, MyFrame::OnToggleLines) | |
153 | EVT_MENU(LIST_MAC_USE_GENERIC, MyFrame::OnToggleMacUseGeneric) | |
154 | ||
155 | EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo) | |
156 | EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL, MyFrame::OnUpdateToggleMultiSel) | |
157 | END_EVENT_TABLE() | |
158 | ||
159 | // My frame constructor | |
160 | MyFrame::MyFrame(const wxChar *title) | |
161 | : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(600, 500)) | |
162 | { | |
163 | m_listCtrl = NULL; | |
164 | m_logWindow = NULL; | |
165 | m_smallVirtual = false; | |
166 | ||
167 | // Give it an icon | |
168 | SetIcon( wxICON(mondrian) ); | |
169 | ||
170 | // Make an image list containing large icons | |
171 | m_imageListNormal = new wxImageList(32, 32, true); | |
172 | m_imageListSmall = new wxImageList(16, 16, true); | |
173 | ||
174 | #ifdef __WXMSW__ | |
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) ); | |
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; | |
203 | menuFile->Append(LIST_ABOUT, _T("&About")); | |
204 | menuFile->AppendSeparator(); | |
205 | menuFile->Append(LIST_QUIT, _T("E&xit\tAlt-X")); | |
206 | ||
207 | wxMenu *menuView = new wxMenu; | |
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")); | |
214 | menuView->Append(LIST_VIRTUAL_VIEW, _T("&Virtual view\tF7")); | |
215 | menuView->Append(LIST_SMALL_VIRTUAL_VIEW, _T("Small virtual vie&w\tF8")); | |
216 | #ifdef __WXMAC__ | |
217 | menuView->AppendCheckItem(LIST_MAC_USE_GENERIC, _T("Mac: Use Generic Control")); | |
218 | #endif | |
219 | ||
220 | wxMenu *menuList = new wxMenu; | |
221 | menuList->Append(LIST_GOTO, _T("&Go to item #3\tCtrl-3")); | |
222 | menuList->Append(LIST_FOCUS_LAST, _T("&Make last item current\tCtrl-L")); | |
223 | menuList->Append(LIST_TOGGLE_FIRST, _T("To&ggle first item\tCtrl-G")); | |
224 | menuList->Append(LIST_DESELECT_ALL, _T("&Deselect All\tCtrl-D")); | |
225 | menuList->Append(LIST_SELECT_ALL, _T("S&elect All\tCtrl-A")); | |
226 | menuList->AppendSeparator(); | |
227 | menuList->Append(LIST_SHOW_COL_INFO, _T("Show &column info\tCtrl-C")); | |
228 | menuList->Append(LIST_SHOW_SEL_INFO, _T("Show &selected items\tCtrl-S")); | |
229 | menuList->Append(LIST_SHOW_VIEW_RECT, _T("Show &view rect")); | |
230 | #ifdef wxHAS_LISTCTRL_COLUMN_ORDER | |
231 | menuList->Append(LIST_SET_COL_ORDER, _T("Se&t columns order\tShift-Ctrl-O")); | |
232 | menuList->Append(LIST_GET_COL_ORDER, _T("Show&w columns order\tCtrl-O")); | |
233 | #endif // wxHAS_LISTCTRL_COLUMN_ORDER | |
234 | menuList->AppendSeparator(); | |
235 | menuList->Append(LIST_SORT, _T("Sor&t\tCtrl-T")); | |
236 | menuList->AppendSeparator(); | |
237 | menuList->Append(LIST_ADD, _T("&Append an item\tCtrl-P")); | |
238 | menuList->Append(LIST_EDIT, _T("&Edit the item\tCtrl-E")); | |
239 | menuList->Append(LIST_DELETE, _T("&Delete first item\tCtrl-X")); | |
240 | menuList->Append(LIST_DELETE_ALL, _T("Delete &all items")); | |
241 | menuList->AppendSeparator(); | |
242 | menuList->Append(LIST_FREEZE, _T("Free&ze\tCtrl-Z")); | |
243 | menuList->Append(LIST_THAW, _T("Tha&w\tCtrl-W")); | |
244 | menuList->AppendSeparator(); | |
245 | menuList->AppendCheckItem(LIST_TOGGLE_LINES, _T("Toggle &lines\tCtrl-I")); | |
246 | menuList->Append(LIST_TOGGLE_MULTI_SEL, _T("&Multiple selection\tCtrl-M"), | |
247 | _T("Toggle multiple selection"), true); | |
248 | ||
249 | wxMenu *menuCol = new wxMenu; | |
250 | menuCol->Append(LIST_SET_FG_COL, _T("&Foreground colour...")); | |
251 | menuCol->Append(LIST_SET_BG_COL, _T("&Background colour...")); | |
252 | ||
253 | wxMenuBar *menubar = new wxMenuBar; | |
254 | menubar->Append(menuFile, _T("&File")); | |
255 | menubar->Append(menuView, _T("&View")); | |
256 | menubar->Append(menuList, _T("&List")); | |
257 | menubar->Append(menuCol, _T("&Colour")); | |
258 | SetMenuBar(menubar); | |
259 | ||
260 | m_panel = new wxPanel(this, wxID_ANY); | |
261 | m_logWindow = new wxTextCtrl(m_panel, wxID_ANY, wxEmptyString, | |
262 | wxDefaultPosition, wxDefaultSize, | |
263 | wxTE_READONLY | wxTE_MULTILINE | wxSUNKEN_BORDER); | |
264 | ||
265 | m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow)); | |
266 | ||
267 | RecreateList(wxLC_REPORT | wxLC_SINGLE_SEL); | |
268 | ||
269 | #ifdef __WXMSW__ | |
270 | // this is useful to know specially when debugging :) | |
271 | wxLogMessage("Your version of comctl32.dll is: %d", | |
272 | wxApp::GetComCtl32Version()); | |
273 | #endif | |
274 | ||
275 | #if wxUSE_STATUSBAR | |
276 | CreateStatusBar(); | |
277 | #endif // wxUSE_STATUSBAR | |
278 | } | |
279 | ||
280 | MyFrame::~MyFrame() | |
281 | { | |
282 | delete wxLog::SetActiveTarget(m_logOld); | |
283 | ||
284 | delete m_imageListNormal; | |
285 | delete m_imageListSmall; | |
286 | } | |
287 | ||
288 | void MyFrame::OnSize(wxSizeEvent& event) | |
289 | { | |
290 | DoSize(); | |
291 | ||
292 | event.Skip(); | |
293 | } | |
294 | ||
295 | void MyFrame::DoSize() | |
296 | { | |
297 | if ( !m_logWindow ) | |
298 | return; | |
299 | ||
300 | wxSize size = GetClientSize(); | |
301 | wxCoord y = (2*size.y)/3; | |
302 | m_listCtrl->SetSize(0, 0, size.x, y); | |
303 | m_logWindow->SetSize(0, y + 1, size.x, size.y - y -1); | |
304 | } | |
305 | ||
306 | bool MyFrame::CheckNonVirtual() const | |
307 | { | |
308 | if ( !m_listCtrl->HasFlag(wxLC_VIRTUAL) ) | |
309 | return true; | |
310 | ||
311 | // "this" == whatever | |
312 | wxLogWarning(_T("Can't do this in virtual view, sorry.")); | |
313 | ||
314 | return false; | |
315 | } | |
316 | ||
317 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
318 | { | |
319 | Close(true); | |
320 | } | |
321 | ||
322 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
323 | { | |
324 | wxMessageDialog dialog(this, _T("List test sample\nJulian Smart (c) 1997"), | |
325 | _T("About list test"), wxOK|wxCANCEL); | |
326 | ||
327 | dialog.ShowModal(); | |
328 | } | |
329 | ||
330 | void MyFrame::OnFreeze(wxCommandEvent& WXUNUSED(event)) | |
331 | { | |
332 | wxLogMessage(_T("Freezing the control")); | |
333 | ||
334 | m_listCtrl->Freeze(); | |
335 | } | |
336 | ||
337 | void MyFrame::OnThaw(wxCommandEvent& WXUNUSED(event)) | |
338 | { | |
339 | wxLogMessage(_T("Thawing the control")); | |
340 | ||
341 | m_listCtrl->Thaw(); | |
342 | } | |
343 | ||
344 | void MyFrame::OnToggleLines(wxCommandEvent& event) | |
345 | { | |
346 | m_listCtrl->SetSingleStyle(wxLC_HRULES | wxLC_VRULES, event.IsChecked()); | |
347 | } | |
348 | ||
349 | void MyFrame::OnToggleMacUseGeneric(wxCommandEvent& event) | |
350 | { | |
351 | wxSystemOptions::SetOption(wxT("mac.listctrl.always_use_generic"), event.IsChecked()); | |
352 | } | |
353 | ||
354 | void MyFrame::OnGoTo(wxCommandEvent& WXUNUSED(event)) | |
355 | { | |
356 | long index = 3; | |
357 | m_listCtrl->SetItemState(index, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED); | |
358 | ||
359 | long sel = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL, | |
360 | wxLIST_STATE_SELECTED); | |
361 | if ( sel != -1 ) | |
362 | m_listCtrl->SetItemState(sel, 0, wxLIST_STATE_SELECTED); | |
363 | m_listCtrl->SetItemState(index, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED); | |
364 | } | |
365 | ||
366 | void MyFrame::OnFocusLast(wxCommandEvent& WXUNUSED(event)) | |
367 | { | |
368 | long index = m_listCtrl->GetItemCount() - 1; | |
369 | if ( index == -1 ) | |
370 | { | |
371 | return; | |
372 | } | |
373 | ||
374 | m_listCtrl->SetItemState(index, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED); | |
375 | m_listCtrl->EnsureVisible(index); | |
376 | } | |
377 | ||
378 | void MyFrame::OnToggleFirstSel(wxCommandEvent& WXUNUSED(event)) | |
379 | { | |
380 | m_listCtrl->SetItemState(0, (~m_listCtrl->GetItemState(0, wxLIST_STATE_SELECTED) ) & wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED); | |
381 | } | |
382 | ||
383 | void MyFrame::OnDeselectAll(wxCommandEvent& WXUNUSED(event)) | |
384 | { | |
385 | if ( !CheckNonVirtual() ) | |
386 | return; | |
387 | ||
388 | int n = m_listCtrl->GetItemCount(); | |
389 | for (int i = 0; i < n; i++) | |
390 | m_listCtrl->SetItemState(i,0,wxLIST_STATE_SELECTED); | |
391 | } | |
392 | ||
393 | void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event)) | |
394 | { | |
395 | if ( !CheckNonVirtual() ) | |
396 | return; | |
397 | ||
398 | int n = m_listCtrl->GetItemCount(); | |
399 | for (int i = 0; i < n; i++) | |
400 | m_listCtrl->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED); | |
401 | } | |
402 | ||
403 | // ---------------------------------------------------------------------------- | |
404 | // changing listctrl modes | |
405 | // ---------------------------------------------------------------------------- | |
406 | ||
407 | void MyFrame::RecreateList(long flags, bool withText) | |
408 | { | |
409 | // we could avoid recreating it if we don't set/clear the wxLC_VIRTUAL | |
410 | // style, but it is more trouble to do it than not | |
411 | #if 0 | |
412 | if ( !m_listCtrl || ((flags & wxLC_VIRTUAL) != | |
413 | (m_listCtrl->GetWindowStyleFlag() & wxLC_VIRTUAL)) ) | |
414 | #endif | |
415 | { | |
416 | delete m_listCtrl; | |
417 | ||
418 | m_listCtrl = new MyListCtrl(m_panel, LIST_CTRL, | |
419 | wxDefaultPosition, wxDefaultSize, | |
420 | flags | | |
421 | wxBORDER_THEME | wxLC_EDIT_LABELS); | |
422 | ||
423 | switch ( flags & wxLC_MASK_TYPE ) | |
424 | { | |
425 | case wxLC_LIST: | |
426 | InitWithListItems(); | |
427 | break; | |
428 | ||
429 | case wxLC_ICON: | |
430 | InitWithIconItems(withText); | |
431 | break; | |
432 | ||
433 | case wxLC_SMALL_ICON: | |
434 | InitWithIconItems(withText, true); | |
435 | break; | |
436 | ||
437 | case wxLC_REPORT: | |
438 | if ( flags & wxLC_VIRTUAL ) | |
439 | InitWithVirtualItems(); | |
440 | else | |
441 | InitWithReportItems(); | |
442 | break; | |
443 | ||
444 | default: | |
445 | wxFAIL_MSG( _T("unknown listctrl mode") ); | |
446 | } | |
447 | } | |
448 | ||
449 | DoSize(); | |
450 | ||
451 | m_logWindow->Clear(); | |
452 | } | |
453 | ||
454 | void MyFrame::OnListView(wxCommandEvent& WXUNUSED(event)) | |
455 | { | |
456 | RecreateList(wxLC_LIST); | |
457 | } | |
458 | ||
459 | void MyFrame::InitWithListItems() | |
460 | { | |
461 | for ( int i = 0; i < NUM_ITEMS; i++ ) | |
462 | { | |
463 | m_listCtrl->InsertItem(i, wxString::Format(_T("Item %d"), i)); | |
464 | } | |
465 | } | |
466 | ||
467 | void MyFrame::OnReportView(wxCommandEvent& WXUNUSED(event)) | |
468 | { | |
469 | RecreateList(wxLC_REPORT); | |
470 | } | |
471 | ||
472 | void MyFrame::InitWithReportItems() | |
473 | { | |
474 | m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL); | |
475 | ||
476 | // note that under MSW for SetColumnWidth() to work we need to create the | |
477 | // items with images initially even if we specify dummy image id | |
478 | wxListItem itemCol; | |
479 | itemCol.SetText(_T("Column 1")); | |
480 | itemCol.SetImage(-1); | |
481 | m_listCtrl->InsertColumn(0, itemCol); | |
482 | ||
483 | itemCol.SetText(_T("Column 2")); | |
484 | itemCol.SetAlign(wxLIST_FORMAT_CENTRE); | |
485 | m_listCtrl->InsertColumn(1, itemCol); | |
486 | ||
487 | itemCol.SetText(_T("Column 3")); | |
488 | itemCol.SetAlign(wxLIST_FORMAT_RIGHT); | |
489 | m_listCtrl->InsertColumn(2, itemCol); | |
490 | ||
491 | // to speed up inserting we hide the control temporarily | |
492 | m_listCtrl->Hide(); | |
493 | ||
494 | wxStopWatch sw; | |
495 | ||
496 | for ( int i = 0; i < NUM_ITEMS; i++ ) | |
497 | { | |
498 | m_listCtrl->InsertItemInReportView(i); | |
499 | } | |
500 | ||
501 | m_logWindow->WriteText(wxString::Format(_T("%d items inserted in %ldms\n"), | |
502 | NUM_ITEMS, sw.Time())); | |
503 | m_listCtrl->Show(); | |
504 | ||
505 | // we leave all mask fields to 0 and only change the colour | |
506 | wxListItem item; | |
507 | item.m_itemId = 0; | |
508 | item.SetTextColour(*wxRED); | |
509 | m_listCtrl->SetItem( item ); | |
510 | ||
511 | item.m_itemId = 2; | |
512 | item.SetTextColour(*wxGREEN); | |
513 | m_listCtrl->SetItem( item ); | |
514 | item.m_itemId = 4; | |
515 | item.SetTextColour(*wxLIGHT_GREY); | |
516 | item.SetFont(*wxITALIC_FONT); | |
517 | item.SetBackgroundColour(*wxRED); | |
518 | m_listCtrl->SetItem( item ); | |
519 | ||
520 | m_listCtrl->SetTextColour(*wxBLUE); | |
521 | ||
522 | m_listCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE ); | |
523 | m_listCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE ); | |
524 | m_listCtrl->SetColumnWidth( 2, wxLIST_AUTOSIZE ); | |
525 | ||
526 | // Set images in columns | |
527 | m_listCtrl->SetItemColumnImage(1, 1, 0); | |
528 | ||
529 | wxListItem info; | |
530 | info.SetImage(0); | |
531 | info.SetId(3); | |
532 | info.SetColumn(2); | |
533 | m_listCtrl->SetItem(info); | |
534 | ||
535 | // test SetItemFont too | |
536 | m_listCtrl->SetItemFont(0, *wxITALIC_FONT); | |
537 | } | |
538 | ||
539 | void MyFrame::InitWithIconItems(bool withText, bool sameIcon) | |
540 | { | |
541 | m_listCtrl->SetImageList(m_imageListNormal, wxIMAGE_LIST_NORMAL); | |
542 | m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL); | |
543 | ||
544 | for ( int i = 0; i < NUM_ICONS; i++ ) | |
545 | { | |
546 | int image = sameIcon ? 0 : i; | |
547 | ||
548 | if ( withText ) | |
549 | { | |
550 | m_listCtrl->InsertItem(i, wxString::Format(_T("Label %d"), i), | |
551 | image); | |
552 | } | |
553 | else | |
554 | { | |
555 | m_listCtrl->InsertItem(i, image); | |
556 | } | |
557 | } | |
558 | } | |
559 | ||
560 | void MyFrame::OnIconView(wxCommandEvent& WXUNUSED(event)) | |
561 | { | |
562 | RecreateList(wxLC_ICON, false); | |
563 | } | |
564 | ||
565 | void MyFrame::OnIconTextView(wxCommandEvent& WXUNUSED(event)) | |
566 | { | |
567 | RecreateList(wxLC_ICON); | |
568 | } | |
569 | ||
570 | void MyFrame::OnSmallIconView(wxCommandEvent& WXUNUSED(event)) | |
571 | { | |
572 | RecreateList(wxLC_SMALL_ICON, false); | |
573 | } | |
574 | ||
575 | void MyFrame::OnSmallIconTextView(wxCommandEvent& WXUNUSED(event)) | |
576 | { | |
577 | RecreateList(wxLC_SMALL_ICON); | |
578 | } | |
579 | ||
580 | void MyFrame::OnVirtualView(wxCommandEvent& WXUNUSED(event)) | |
581 | { | |
582 | m_smallVirtual = false; | |
583 | RecreateList(wxLC_REPORT | wxLC_VIRTUAL); | |
584 | } | |
585 | ||
586 | void MyFrame::OnSmallVirtualView(wxCommandEvent& WXUNUSED(event)) | |
587 | { | |
588 | m_smallVirtual = true; | |
589 | RecreateList(wxLC_REPORT | wxLC_VIRTUAL); | |
590 | } | |
591 | ||
592 | void MyFrame::InitWithVirtualItems() | |
593 | { | |
594 | m_listCtrl->SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL); | |
595 | ||
596 | if ( m_smallVirtual ) | |
597 | { | |
598 | m_listCtrl->InsertColumn(0, _T("Animal")); | |
599 | m_listCtrl->InsertColumn(1, _T("Sound")); | |
600 | m_listCtrl->SetItemCount(WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS)); | |
601 | } | |
602 | else | |
603 | { | |
604 | m_listCtrl->InsertColumn(0, _T("First Column")); | |
605 | m_listCtrl->InsertColumn(1, _T("Second Column")); | |
606 | m_listCtrl->SetColumnWidth(0, 150); | |
607 | m_listCtrl->SetColumnWidth(1, 150); | |
608 | m_listCtrl->SetItemCount(1000000); | |
609 | } | |
610 | } | |
611 | ||
612 | void MyFrame::OnSort(wxCommandEvent& WXUNUSED(event)) | |
613 | { | |
614 | wxStopWatch sw; | |
615 | ||
616 | m_listCtrl->SortItems(MyCompareFunction, 0); | |
617 | ||
618 | m_logWindow->WriteText(wxString::Format(_T("Sorting %d items took %ld ms\n"), | |
619 | m_listCtrl->GetItemCount(), | |
620 | sw.Time())); | |
621 | } | |
622 | ||
623 | void MyFrame::OnShowSelInfo(wxCommandEvent& WXUNUSED(event)) | |
624 | { | |
625 | int selCount = m_listCtrl->GetSelectedItemCount(); | |
626 | wxLogMessage(_T("%d items selected:"), selCount); | |
627 | ||
628 | // don't show too many items | |
629 | size_t shownCount = 0; | |
630 | ||
631 | long item = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL, | |
632 | wxLIST_STATE_SELECTED); | |
633 | while ( item != -1 ) | |
634 | { | |
635 | wxLogMessage(_T("\t%ld (%s)"), | |
636 | item, m_listCtrl->GetItemText(item).c_str()); | |
637 | ||
638 | if ( ++shownCount > 10 ) | |
639 | { | |
640 | wxLogMessage(_T("\t... more selected items snipped...")); | |
641 | break; | |
642 | } | |
643 | ||
644 | item = m_listCtrl->GetNextItem(item, wxLIST_NEXT_ALL, | |
645 | wxLIST_STATE_SELECTED); | |
646 | } | |
647 | } | |
648 | ||
649 | void MyFrame::OnShowViewRect(wxCommandEvent& WXUNUSED(event)) | |
650 | { | |
651 | const wxRect r = m_listCtrl->GetViewRect(); | |
652 | wxLogMessage("View rect: (%d, %d)-(%d, %d)", | |
653 | r.GetLeft(), r.GetTop(), r.GetRight(), r.GetBottom()); | |
654 | } | |
655 | ||
656 | // ---------------------------------------------------------------------------- | |
657 | // column order tests | |
658 | // ---------------------------------------------------------------------------- | |
659 | ||
660 | #ifdef wxHAS_LISTCTRL_COLUMN_ORDER | |
661 | ||
662 | static wxString DumpIntArray(const wxArrayInt& a) | |
663 | { | |
664 | wxString s("{ "); | |
665 | const size_t count = a.size(); | |
666 | for ( size_t n = 0; n < count; n++ ) | |
667 | { | |
668 | if ( n ) | |
669 | s += ", "; | |
670 | s += wxString::Format("%lu", (unsigned long)a[n]); | |
671 | } | |
672 | ||
673 | s += " }"; | |
674 | ||
675 | return s; | |
676 | } | |
677 | ||
678 | void MyFrame::OnSetColOrder(wxCommandEvent& WXUNUSED(event)) | |
679 | { | |
680 | wxArrayInt order(3); | |
681 | order[0] = 2; | |
682 | order[1] = 0; | |
683 | order[2] = 1; | |
684 | if ( m_listCtrl->SetColumnsOrder(order) ) | |
685 | wxLogMessage("Column order set to %s", DumpIntArray(order)); | |
686 | } | |
687 | ||
688 | void MyFrame::OnGetColOrder(wxCommandEvent& WXUNUSED(event)) | |
689 | { | |
690 | // show what GetColumnsOrder() returns | |
691 | const wxArrayInt order = m_listCtrl->GetColumnsOrder(); | |
692 | wxString msg = "Columns order: " + | |
693 | DumpIntArray(m_listCtrl->GetColumnsOrder()) + "\n"; | |
694 | ||
695 | int n; | |
696 | const int count = m_listCtrl->GetColumnCount(); | |
697 | ||
698 | // show the results of GetColumnOrder() for each column | |
699 | msg += "GetColumnOrder() results:\n"; | |
700 | for ( n = 0; n < count; n++ ) | |
701 | { | |
702 | msg += wxString::Format(" %2d -> %2d\n", | |
703 | n, m_listCtrl->GetColumnOrder(n)); | |
704 | } | |
705 | ||
706 | // and the results of GetColumnIndexFromOrder() too | |
707 | msg += "GetColumnIndexFromOrder() results:\n"; | |
708 | for ( n = 0; n < count; n++ ) | |
709 | { | |
710 | msg += wxString::Format(" %2d -> %2d\n", | |
711 | n, m_listCtrl->GetColumnIndexFromOrder(n)); | |
712 | } | |
713 | ||
714 | wxLogMessage("%s", msg); | |
715 | } | |
716 | ||
717 | #endif // wxHAS_LISTCTRL_COLUMN_ORDER | |
718 | ||
719 | void MyFrame::OnShowColInfo(wxCommandEvent& WXUNUSED(event)) | |
720 | { | |
721 | int count = m_listCtrl->GetColumnCount(); | |
722 | wxLogMessage(wxT("%d columns:"), count); | |
723 | for ( int c = 0; c < count; c++ ) | |
724 | { | |
725 | wxLogMessage(wxT("\tcolumn %d has width %d"), c, | |
726 | m_listCtrl->GetColumnWidth(c)); | |
727 | } | |
728 | } | |
729 | ||
730 | void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent& event) | |
731 | { | |
732 | event.Enable( (m_listCtrl->GetWindowStyleFlag() & wxLC_REPORT) != 0 ); | |
733 | } | |
734 | ||
735 | void MyFrame::OnToggleMultiSel(wxCommandEvent& WXUNUSED(event)) | |
736 | { | |
737 | long flags = m_listCtrl->GetWindowStyleFlag(); | |
738 | if ( flags & wxLC_SINGLE_SEL ) | |
739 | flags &= ~wxLC_SINGLE_SEL; | |
740 | else | |
741 | flags |= wxLC_SINGLE_SEL; | |
742 | ||
743 | m_logWindow->WriteText(wxString::Format(wxT("Current selection mode: %sle\n"), | |
744 | (flags & wxLC_SINGLE_SEL) ? _T("sing") : _T("multip"))); | |
745 | ||
746 | RecreateList(flags); | |
747 | } | |
748 | ||
749 | void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent& event) | |
750 | { | |
751 | event.Check((m_listCtrl->GetWindowStyleFlag() & wxLC_SINGLE_SEL) == 0); | |
752 | } | |
753 | ||
754 | void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event)) | |
755 | { | |
756 | m_listCtrl->SetForegroundColour(wxGetColourFromUser(this)); | |
757 | m_listCtrl->Refresh(); | |
758 | } | |
759 | ||
760 | void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event)) | |
761 | { | |
762 | m_listCtrl->SetBackgroundColour(wxGetColourFromUser(this)); | |
763 | m_listCtrl->Refresh(); | |
764 | } | |
765 | ||
766 | void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event)) | |
767 | { | |
768 | m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), _T("Appended item")); | |
769 | } | |
770 | ||
771 | void MyFrame::OnEdit(wxCommandEvent& WXUNUSED(event)) | |
772 | { | |
773 | long itemCur = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL, | |
774 | wxLIST_STATE_FOCUSED); | |
775 | ||
776 | if ( itemCur != -1 ) | |
777 | { | |
778 | m_listCtrl->EditLabel(itemCur); | |
779 | } | |
780 | else | |
781 | { | |
782 | m_logWindow->WriteText(_T("No item to edit")); | |
783 | } | |
784 | } | |
785 | ||
786 | void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event)) | |
787 | { | |
788 | if ( m_listCtrl->GetItemCount() ) | |
789 | { | |
790 | m_listCtrl->DeleteItem(0); | |
791 | } | |
792 | else | |
793 | { | |
794 | m_logWindow->WriteText(_T("Nothing to delete")); | |
795 | } | |
796 | } | |
797 | ||
798 | void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event)) | |
799 | { | |
800 | wxStopWatch sw; | |
801 | ||
802 | int itemCount = m_listCtrl->GetItemCount(); | |
803 | ||
804 | m_listCtrl->DeleteAllItems(); | |
805 | ||
806 | m_logWindow->WriteText(wxString::Format(_T("Deleting %d items took %ld ms\n"), | |
807 | itemCount, | |
808 | sw.Time())); | |
809 | } | |
810 | ||
811 | ||
812 | // ---------------------------------------------------------------------------- | |
813 | // MyListCtrl | |
814 | // ---------------------------------------------------------------------------- | |
815 | ||
816 | BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl) | |
817 | EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag) | |
818 | EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag) | |
819 | EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnBeginLabelEdit) | |
820 | EVT_LIST_END_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnEndLabelEdit) | |
821 | EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem) | |
822 | EVT_LIST_DELETE_ALL_ITEMS(LIST_CTRL, MyListCtrl::OnDeleteAllItems) | |
823 | EVT_LIST_ITEM_SELECTED(LIST_CTRL, MyListCtrl::OnSelected) | |
824 | EVT_LIST_ITEM_DESELECTED(LIST_CTRL, MyListCtrl::OnDeselected) | |
825 | EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown) | |
826 | EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated) | |
827 | EVT_LIST_ITEM_FOCUSED(LIST_CTRL, MyListCtrl::OnFocused) | |
828 | ||
829 | EVT_LIST_COL_CLICK(LIST_CTRL, MyListCtrl::OnColClick) | |
830 | EVT_LIST_COL_RIGHT_CLICK(LIST_CTRL, MyListCtrl::OnColRightClick) | |
831 | EVT_LIST_COL_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnColBeginDrag) | |
832 | EVT_LIST_COL_DRAGGING(LIST_CTRL, MyListCtrl::OnColDragging) | |
833 | EVT_LIST_COL_END_DRAG(LIST_CTRL, MyListCtrl::OnColEndDrag) | |
834 | ||
835 | EVT_LIST_CACHE_HINT(LIST_CTRL, MyListCtrl::OnCacheHint) | |
836 | ||
837 | #if USE_CONTEXT_MENU | |
838 | EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu) | |
839 | #endif | |
840 | EVT_CHAR(MyListCtrl::OnChar) | |
841 | ||
842 | EVT_RIGHT_DOWN(MyListCtrl::OnRightClick) | |
843 | END_EVENT_TABLE() | |
844 | ||
845 | void MyListCtrl::OnCacheHint(wxListEvent& event) | |
846 | { | |
847 | wxLogMessage( wxT("OnCacheHint: cache items %ld..%ld"), | |
848 | event.GetCacheFrom(), event.GetCacheTo() ); | |
849 | } | |
850 | ||
851 | void MyListCtrl::SetColumnImage(int col, int image) | |
852 | { | |
853 | wxListItem item; | |
854 | item.SetMask(wxLIST_MASK_IMAGE); | |
855 | item.SetImage(image); | |
856 | SetColumn(col, item); | |
857 | } | |
858 | ||
859 | void MyListCtrl::OnColClick(wxListEvent& event) | |
860 | { | |
861 | int col = event.GetColumn(); | |
862 | ||
863 | // set or unset image | |
864 | static bool x = false; | |
865 | x = !x; | |
866 | SetColumnImage(col, x ? 0 : -1); | |
867 | ||
868 | wxLogMessage( wxT("OnColumnClick at %d."), col ); | |
869 | } | |
870 | ||
871 | void MyListCtrl::OnColRightClick(wxListEvent& event) | |
872 | { | |
873 | int col = event.GetColumn(); | |
874 | if ( col != -1 ) | |
875 | { | |
876 | SetColumnImage(col, -1); | |
877 | } | |
878 | ||
879 | // Show popupmenu at position | |
880 | wxMenu menu(wxT("Test")); | |
881 | menu.Append(LIST_ABOUT, _T("&About")); | |
882 | PopupMenu(&menu, event.GetPoint()); | |
883 | ||
884 | wxLogMessage( wxT("OnColumnRightClick at %d."), event.GetColumn() ); | |
885 | } | |
886 | ||
887 | void MyListCtrl::LogColEvent(const wxListEvent& event, const wxChar *name) | |
888 | { | |
889 | const int col = event.GetColumn(); | |
890 | ||
891 | wxLogMessage(wxT("%s: column %d (width = %d or %d)."), | |
892 | name, | |
893 | col, | |
894 | event.GetItem().GetWidth(), | |
895 | GetColumnWidth(col)); | |
896 | } | |
897 | ||
898 | void MyListCtrl::OnColBeginDrag(wxListEvent& event) | |
899 | { | |
900 | LogColEvent( event, wxT("OnColBeginDrag") ); | |
901 | ||
902 | if ( event.GetColumn() == 0 ) | |
903 | { | |
904 | wxLogMessage(_T("Resizing this column shouldn't work.")); | |
905 | ||
906 | event.Veto(); | |
907 | } | |
908 | } | |
909 | ||
910 | void MyListCtrl::OnColDragging(wxListEvent& event) | |
911 | { | |
912 | LogColEvent( event, wxT("OnColDragging") ); | |
913 | } | |
914 | ||
915 | void MyListCtrl::OnColEndDrag(wxListEvent& event) | |
916 | { | |
917 | LogColEvent( event, wxT("OnColEndDrag") ); | |
918 | } | |
919 | ||
920 | void MyListCtrl::OnBeginDrag(wxListEvent& event) | |
921 | { | |
922 | const wxPoint& pt = event.m_pointDrag; | |
923 | ||
924 | int flags; | |
925 | wxLogMessage( wxT("OnBeginDrag at (%d, %d), item %ld."), | |
926 | pt.x, pt.y, HitTest(pt, flags) ); | |
927 | } | |
928 | ||
929 | void MyListCtrl::OnBeginRDrag(wxListEvent& event) | |
930 | { | |
931 | wxLogMessage( wxT("OnBeginRDrag at %d,%d."), | |
932 | event.m_pointDrag.x, event.m_pointDrag.y ); | |
933 | } | |
934 | ||
935 | void MyListCtrl::OnBeginLabelEdit(wxListEvent& event) | |
936 | { | |
937 | wxLogMessage( wxT("OnBeginLabelEdit: %s"), event.m_item.m_text.c_str()); | |
938 | ||
939 | wxTextCtrl * const text = GetEditControl(); | |
940 | if ( !text ) | |
941 | { | |
942 | wxLogMessage("BUG: started to edit but no edit control"); | |
943 | } | |
944 | else | |
945 | { | |
946 | wxLogMessage("Edit control value: \"%s\"", text->GetValue()); | |
947 | } | |
948 | } | |
949 | ||
950 | void MyListCtrl::OnEndLabelEdit(wxListEvent& event) | |
951 | { | |
952 | wxLogMessage( wxT("OnEndLabelEdit: %s"), | |
953 | ( | |
954 | event.IsEditCancelled() ? | |
955 | wxString("[cancelled]") : | |
956 | event.m_item.m_text | |
957 | ).c_str() | |
958 | ); | |
959 | } | |
960 | ||
961 | void MyListCtrl::OnDeleteItem(wxListEvent& event) | |
962 | { | |
963 | LogEvent(event, _T("OnDeleteItem")); | |
964 | wxLogMessage( wxT("Number of items when delete event is sent: %d"), GetItemCount() ); | |
965 | } | |
966 | ||
967 | void MyListCtrl::OnDeleteAllItems(wxListEvent& event) | |
968 | { | |
969 | LogEvent(event, _T("OnDeleteAllItems")); | |
970 | } | |
971 | ||
972 | void MyListCtrl::OnSelected(wxListEvent& event) | |
973 | { | |
974 | LogEvent(event, _T("OnSelected")); | |
975 | ||
976 | if ( GetWindowStyle() & wxLC_REPORT ) | |
977 | { | |
978 | wxListItem info; | |
979 | info.m_itemId = event.m_itemIndex; | |
980 | info.m_col = 1; | |
981 | info.m_mask = wxLIST_MASK_TEXT; | |
982 | if ( GetItem(info) ) | |
983 | { | |
984 | wxLogMessage(wxT("Value of the 2nd field of the selected item: %s"), | |
985 | info.m_text.c_str()); | |
986 | } | |
987 | else | |
988 | { | |
989 | wxFAIL_MSG(wxT("wxListCtrl::GetItem() failed")); | |
990 | } | |
991 | } | |
992 | } | |
993 | ||
994 | void MyListCtrl::OnDeselected(wxListEvent& event) | |
995 | { | |
996 | LogEvent(event, _T("OnDeselected")); | |
997 | } | |
998 | ||
999 | void MyListCtrl::OnActivated(wxListEvent& event) | |
1000 | { | |
1001 | LogEvent(event, _T("OnActivated")); | |
1002 | } | |
1003 | ||
1004 | void MyListCtrl::OnFocused(wxListEvent& event) | |
1005 | { | |
1006 | LogEvent(event, _T("OnFocused")); | |
1007 | ||
1008 | event.Skip(); | |
1009 | } | |
1010 | ||
1011 | void MyListCtrl::OnListKeyDown(wxListEvent& event) | |
1012 | { | |
1013 | long item; | |
1014 | ||
1015 | switch ( event.GetKeyCode() ) | |
1016 | { | |
1017 | case 'C': // colorize | |
1018 | { | |
1019 | wxListItem info; | |
1020 | info.m_itemId = event.GetIndex(); | |
1021 | if ( info.m_itemId == -1 ) | |
1022 | { | |
1023 | // no item | |
1024 | break; | |
1025 | } | |
1026 | ||
1027 | GetItem(info); | |
1028 | ||
1029 | wxListItemAttr *attr = info.GetAttributes(); | |
1030 | if ( !attr || !attr->HasTextColour() ) | |
1031 | { | |
1032 | info.SetTextColour(*wxCYAN); | |
1033 | ||
1034 | SetItem(info); | |
1035 | ||
1036 | RefreshItem(info.m_itemId); | |
1037 | } | |
1038 | } | |
1039 | break; | |
1040 | ||
1041 | case 'N': // next | |
1042 | item = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED); | |
1043 | if ( item++ == GetItemCount() - 1 ) | |
1044 | { | |
1045 | item = 0; | |
1046 | } | |
1047 | ||
1048 | wxLogMessage(_T("Focusing item %ld"), item); | |
1049 | ||
1050 | SetItemState(item, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED); | |
1051 | EnsureVisible(item); | |
1052 | break; | |
1053 | ||
1054 | case 'R': // show bounding rectangle | |
1055 | { | |
1056 | item = event.GetIndex(); | |
1057 | wxRect r; | |
1058 | if ( !GetItemRect(item, r) ) | |
1059 | { | |
1060 | wxLogError(_T("Failed to retrieve rect of item %ld"), item); | |
1061 | break; | |
1062 | } | |
1063 | ||
1064 | wxLogMessage(_T("Bounding rect of item %ld is (%d, %d)-(%d, %d)"), | |
1065 | item, r.x, r.y, r.x + r.width, r.y + r.height); | |
1066 | } | |
1067 | break; | |
1068 | ||
1069 | case '1': // show sub item bounding rectangle | |
1070 | case '2': | |
1071 | case '3': | |
1072 | case '4': // this column is invalid but we want to test it too | |
1073 | if ( InReportView() ) | |
1074 | { | |
1075 | int subItem = event.GetKeyCode() - '1'; | |
1076 | item = event.GetIndex(); | |
1077 | wxRect r; | |
1078 | if ( !GetSubItemRect(item, subItem, r) ) | |
1079 | { | |
1080 | wxLogError(_T("Failed to retrieve rect of item %ld column %d"), item, subItem + 1); | |
1081 | break; | |
1082 | } | |
1083 | ||
1084 | wxLogMessage(_T("Bounding rect of item %ld column %d is (%d, %d)-(%d, %d)"), | |
1085 | item, subItem + 1, | |
1086 | r.x, r.y, r.x + r.width, r.y + r.height); | |
1087 | } | |
1088 | break; | |
1089 | ||
1090 | case 'U': // update | |
1091 | if ( !IsVirtual() ) | |
1092 | break; | |
1093 | ||
1094 | if ( m_updated != -1 ) | |
1095 | RefreshItem(m_updated); | |
1096 | ||
1097 | m_updated = event.GetIndex(); | |
1098 | if ( m_updated != -1 ) | |
1099 | { | |
1100 | // we won't see changes to this item as it's selected, update | |
1101 | // the next one (or the first one if we're on the last item) | |
1102 | if ( ++m_updated == GetItemCount() ) | |
1103 | m_updated = 0; | |
1104 | ||
1105 | wxLogMessage("Updating colour of the item %ld", m_updated); | |
1106 | RefreshItem(m_updated); | |
1107 | } | |
1108 | break; | |
1109 | ||
1110 | case 'D': // delete | |
1111 | item = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED); | |
1112 | while ( item != -1 ) | |
1113 | { | |
1114 | DeleteItem(item); | |
1115 | ||
1116 | wxLogMessage(_T("Item %ld deleted"), item); | |
1117 | ||
1118 | // -1 because the indices were shifted by DeleteItem() | |
1119 | item = GetNextItem(item - 1, | |
1120 | wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED); | |
1121 | } | |
1122 | break; | |
1123 | ||
1124 | case 'I': // insert | |
1125 | if ( GetWindowStyle() & wxLC_REPORT ) | |
1126 | { | |
1127 | if ( GetWindowStyle() & wxLC_VIRTUAL ) | |
1128 | { | |
1129 | SetItemCount(GetItemCount() + 1); | |
1130 | } | |
1131 | else // !virtual | |
1132 | { | |
1133 | InsertItemInReportView(event.GetIndex()); | |
1134 | } | |
1135 | } | |
1136 | //else: fall through | |
1137 | ||
1138 | default: | |
1139 | LogEvent(event, _T("OnListKeyDown")); | |
1140 | ||
1141 | event.Skip(); | |
1142 | } | |
1143 | } | |
1144 | ||
1145 | void MyListCtrl::OnChar(wxKeyEvent& event) | |
1146 | { | |
1147 | wxLogMessage(_T("Got char event.")); | |
1148 | ||
1149 | switch ( event.GetKeyCode() ) | |
1150 | { | |
1151 | case 'n': | |
1152 | case 'N': | |
1153 | case 'c': | |
1154 | case 'C': | |
1155 | case 'r': | |
1156 | case 'R': | |
1157 | case 'u': | |
1158 | case 'U': | |
1159 | case 'd': | |
1160 | case 'D': | |
1161 | case 'i': | |
1162 | case 'I': | |
1163 | // these are the keys we process ourselves | |
1164 | break; | |
1165 | ||
1166 | default: | |
1167 | event.Skip(); | |
1168 | } | |
1169 | } | |
1170 | ||
1171 | void MyListCtrl::OnRightClick(wxMouseEvent& event) | |
1172 | { | |
1173 | if ( !event.ControlDown() ) | |
1174 | { | |
1175 | event.Skip(); | |
1176 | return; | |
1177 | } | |
1178 | ||
1179 | int flags; | |
1180 | long subitem; | |
1181 | long item = HitTest(event.GetPosition(), flags, &subitem); | |
1182 | ||
1183 | wxString where; | |
1184 | switch ( flags ) | |
1185 | { | |
1186 | case wxLIST_HITTEST_ABOVE: where = _T("above"); break; | |
1187 | case wxLIST_HITTEST_BELOW: where = _T("below"); break; | |
1188 | case wxLIST_HITTEST_NOWHERE: where = _T("nowhere near"); break; | |
1189 | case wxLIST_HITTEST_ONITEMICON: where = _T("on icon of"); break; | |
1190 | case wxLIST_HITTEST_ONITEMLABEL: where = _T("on label of"); break; | |
1191 | case wxLIST_HITTEST_ONITEMRIGHT: where = _T("right on"); break; | |
1192 | case wxLIST_HITTEST_TOLEFT: where = _T("to the left of"); break; | |
1193 | case wxLIST_HITTEST_TORIGHT: where = _T("to the right of"); break; | |
1194 | default: where = _T("not clear exactly where on"); break; | |
1195 | } | |
1196 | ||
1197 | wxLogMessage(_T("Right double click %s item %ld, subitem %ld"), | |
1198 | where.c_str(), item, subitem); | |
1199 | } | |
1200 | ||
1201 | void MyListCtrl::LogEvent(const wxListEvent& event, const wxChar *eventName) | |
1202 | { | |
1203 | wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"), | |
1204 | event.GetIndex(), eventName, | |
1205 | event.GetText().c_str(), event.GetData()); | |
1206 | } | |
1207 | ||
1208 | wxString MyListCtrl::OnGetItemText(long item, long column) const | |
1209 | { | |
1210 | if ( GetItemCount() == WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS) ) | |
1211 | { | |
1212 | return SMALL_VIRTUAL_VIEW_ITEMS[item][column]; | |
1213 | } | |
1214 | else // "big" virtual control | |
1215 | { | |
1216 | return wxString::Format(_T("Column %ld of item %ld"), column, item); | |
1217 | } | |
1218 | } | |
1219 | ||
1220 | int MyListCtrl::OnGetItemColumnImage(long item, long column) const | |
1221 | { | |
1222 | if (!column) | |
1223 | return 0; | |
1224 | ||
1225 | if (!(item % 3) && column == 1) | |
1226 | return 0; | |
1227 | ||
1228 | return -1; | |
1229 | } | |
1230 | ||
1231 | wxListItemAttr *MyListCtrl::OnGetItemAttr(long item) const | |
1232 | { | |
1233 | // test to check that RefreshItem() works correctly: when m_updated is | |
1234 | // set to some item and it is refreshed, we highlight the item | |
1235 | if ( item == m_updated ) | |
1236 | { | |
1237 | static wxListItemAttr s_attrHighlight(*wxRED, wxNullColour, wxNullFont); | |
1238 | return &s_attrHighlight; | |
1239 | } | |
1240 | ||
1241 | return item % 2 ? NULL : (wxListItemAttr *)&m_attr; | |
1242 | } | |
1243 | ||
1244 | void MyListCtrl::InsertItemInReportView(int i) | |
1245 | { | |
1246 | wxString buf; | |
1247 | buf.Printf(_T("This is item %d"), i); | |
1248 | long tmp = InsertItem(i, buf, 0); | |
1249 | SetItemData(tmp, i); | |
1250 | ||
1251 | buf.Printf(_T("Col 1, item %d"), i); | |
1252 | SetItem(tmp, 1, buf); | |
1253 | ||
1254 | buf.Printf(_T("Item %d in column 2"), i); | |
1255 | SetItem(tmp, 2, buf); | |
1256 | } | |
1257 | ||
1258 | #if USE_CONTEXT_MENU | |
1259 | void MyListCtrl::OnContextMenu(wxContextMenuEvent& event) | |
1260 | { | |
1261 | if (GetEditControl() == NULL) | |
1262 | { | |
1263 | wxPoint point = event.GetPosition(); | |
1264 | // If from keyboard | |
1265 | if ( (point.x == -1) && (point.y == -1) ) | |
1266 | { | |
1267 | wxSize size = GetSize(); | |
1268 | point.x = size.x / 2; | |
1269 | point.y = size.y / 2; | |
1270 | } | |
1271 | else | |
1272 | { | |
1273 | point = ScreenToClient(point); | |
1274 | } | |
1275 | ShowContextMenu(point); | |
1276 | } | |
1277 | else | |
1278 | { | |
1279 | // the user is editing: | |
1280 | // allow the text control to display its context menu | |
1281 | // if it has one (it has on Windows) rather than display our one | |
1282 | event.Skip(); | |
1283 | } | |
1284 | } | |
1285 | #endif | |
1286 | ||
1287 | void MyListCtrl::ShowContextMenu(const wxPoint& pos) | |
1288 | { | |
1289 | wxMenu menu; | |
1290 | ||
1291 | menu.Append(wxID_ABOUT, _T("&About")); | |
1292 | menu.AppendSeparator(); | |
1293 | menu.Append(wxID_EXIT, _T("E&xit")); | |
1294 | ||
1295 | PopupMenu(&menu, pos.x, pos.y); | |
1296 | } |