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