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