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