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