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