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