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