]>
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$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation | |
14 | #pragma interface | |
15 | #endif | |
16 | ||
17 | // For compilers that support precompilation, includes "wx/wx.h". | |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WX_PRECOMP | |
25 | #include "wx/wx.h" | |
26 | #endif | |
27 | ||
28 | #include "wx/listctrl.h" | |
29 | #include "listtest.h" | |
30 | ||
31 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
32 | EVT_MENU(LIST_QUIT, MyFrame::OnQuit) | |
33 | EVT_MENU(LIST_ABOUT, MyFrame::OnAbout) | |
34 | EVT_MENU(LIST_LIST_VIEW, MyFrame::OnListView) | |
35 | EVT_MENU(LIST_REPORT_VIEW, MyFrame::OnReportView) | |
36 | EVT_MENU(LIST_ICON_VIEW, MyFrame::OnIconView) | |
37 | EVT_MENU(LIST_ICON_TEXT_VIEW, MyFrame::OnIconTextView) | |
38 | EVT_MENU(LIST_SMALL_ICON_VIEW, MyFrame::OnSmallIconView) | |
39 | EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView) | |
40 | END_EVENT_TABLE() | |
41 | ||
42 | BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl) | |
43 | EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag) | |
44 | EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag) | |
45 | EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnBeginLabelEdit) | |
46 | EVT_LIST_END_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnEndLabelEdit) | |
47 | EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem) | |
48 | EVT_LIST_GET_INFO(LIST_CTRL, MyListCtrl::OnGetInfo) | |
49 | EVT_LIST_SET_INFO(LIST_CTRL, MyListCtrl::OnSetInfo) | |
50 | EVT_LIST_ITEM_SELECTED(LIST_CTRL, MyListCtrl::OnSelected) | |
51 | EVT_LIST_ITEM_DESELECTED(LIST_CTRL, MyListCtrl::OnDeselected) | |
52 | EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnKeyDown) | |
53 | END_EVENT_TABLE() | |
54 | ||
55 | IMPLEMENT_APP(MyApp) | |
56 | ||
57 | // `Main program' equivalent, creating windows and returning main app frame | |
58 | bool MyApp::OnInit(void) | |
59 | { | |
60 | // Create the main frame window | |
61 | MyFrame *frame = new MyFrame(NULL, "wxListCtrl Test", 50, 50, 450, 340); | |
62 | ||
63 | // This reduces flicker effects - even better would be to define OnEraseBackground | |
64 | // to do nothing. When the list control's scrollbars are show or hidden, the | |
65 | // frame is sent a background erase event. | |
66 | frame->SetBackgroundColour(wxColour(255, 255, 255)); | |
67 | ||
68 | // Give it an icon | |
69 | #ifdef __WINDOWS__ | |
70 | frame->SetIcon(wxIcon("mondrian")); | |
71 | #endif | |
72 | #ifdef __X__ | |
73 | frame->SetIcon(wxIcon("aiai.xbm")); | |
74 | #endif | |
75 | ||
76 | // Make an image list containing large icons | |
77 | m_imageListNormal = new wxImageList(32, 32, TRUE); | |
78 | m_imageListSmall = new wxImageList(16, 16, TRUE); | |
79 | ||
80 | wxIcon *icon = new wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE); | |
81 | m_imageListNormal->Add(*icon); | |
82 | delete icon; | |
83 | icon = new wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE); | |
84 | m_imageListNormal->Add(*icon); | |
85 | delete icon; | |
86 | icon = new wxIcon("icon3", wxBITMAP_TYPE_ICO_RESOURCE); | |
87 | m_imageListNormal->Add(*icon); | |
88 | delete icon; | |
89 | icon = new wxIcon("icon4", wxBITMAP_TYPE_ICO_RESOURCE); | |
90 | m_imageListNormal->Add(*icon); | |
91 | delete icon; | |
92 | icon = new wxIcon("icon5", wxBITMAP_TYPE_ICO_RESOURCE); | |
93 | m_imageListNormal->Add(*icon); | |
94 | delete icon; | |
95 | icon = new wxIcon("icon6", wxBITMAP_TYPE_ICO_RESOURCE); | |
96 | m_imageListNormal->Add(*icon); | |
97 | delete icon; | |
98 | icon = new wxIcon("icon7", wxBITMAP_TYPE_ICO_RESOURCE); | |
99 | m_imageListNormal->Add(*icon); | |
100 | delete icon; | |
101 | icon = new wxIcon("icon8", wxBITMAP_TYPE_ICO_RESOURCE); | |
102 | m_imageListNormal->Add(*icon); | |
103 | delete icon; | |
104 | icon = new wxIcon("icon9", wxBITMAP_TYPE_ICO_RESOURCE); | |
105 | m_imageListNormal->Add(*icon); | |
106 | delete icon; | |
107 | ||
108 | icon = new wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE); | |
109 | m_imageListSmall->Add(*icon); | |
110 | delete icon; | |
111 | ||
112 | // Make a menubar | |
113 | wxMenu *file_menu = new wxMenu; | |
114 | ||
115 | file_menu->Append(LIST_LIST_VIEW, "&List view"); | |
116 | file_menu->Append(LIST_REPORT_VIEW, "&Report view"); | |
117 | file_menu->Append(LIST_ICON_VIEW, "&Icon view"); | |
118 | file_menu->Append(LIST_ICON_TEXT_VIEW, "Icon view with &text"); | |
119 | file_menu->Append(LIST_SMALL_ICON_VIEW, "&Small icon view"); | |
120 | file_menu->Append(LIST_SMALL_ICON_TEXT_VIEW, "Small icon &view with text"); | |
121 | file_menu->AppendSeparator(); | |
122 | file_menu->Append(LIST_ABOUT, "&About"); | |
123 | file_menu->Append(LIST_QUIT, "E&xit"); | |
124 | wxMenuBar *menu_bar = new wxMenuBar; | |
125 | menu_bar->Append(file_menu, "&File"); | |
126 | frame->SetMenuBar(menu_bar); | |
127 | ||
128 | // Make a panel with a message | |
129 | frame->m_listCtrl = new MyListCtrl(frame, LIST_CTRL, wxPoint(0, 0), wxSize(400, 200), | |
130 | wxLC_LIST|wxSUNKEN_BORDER); | |
131 | // wxLC_LIST|wxLC_USER_TEXT|wxSUNKEN_BORDER); // wxLC_USER_TEXT requires app to supply all text on demand | |
132 | frame->m_logWindow = new wxTextCtrl(frame, -1, "", wxPoint(0, 0), wxSize(400, 200), wxTE_MULTILINE|wxSUNKEN_BORDER); | |
133 | ||
134 | wxLayoutConstraints *c = new wxLayoutConstraints; | |
135 | c->top.SameAs (frame, wxTop); | |
136 | c->left.SameAs (frame, wxLeft); | |
137 | c->right.SameAs (frame, wxRight); | |
138 | c->height.PercentOf (frame, wxHeight, 66); | |
139 | frame->m_listCtrl->SetConstraints(c); | |
140 | ||
141 | c = new wxLayoutConstraints; | |
142 | c->top.Below (frame->m_listCtrl); | |
143 | c->left.SameAs (frame, wxLeft); | |
144 | c->right.SameAs (frame, wxRight); | |
145 | c->bottom.SameAs (frame, wxBottom); | |
146 | frame->m_logWindow->SetConstraints(c); | |
147 | frame->SetAutoLayout(TRUE); | |
148 | ||
149 | for ( int i=0; i < 30; i++) | |
150 | { | |
151 | char buf[20]; | |
152 | sprintf(buf, "Item %d", i); | |
153 | long tmp = frame->m_listCtrl->InsertItem(i, buf); | |
154 | } | |
155 | ||
156 | frame->CreateStatusBar(3); | |
157 | frame->SetStatusText("", 0); | |
158 | ||
159 | // Show the frame | |
160 | frame->Show(TRUE); | |
161 | ||
162 | SetTopWindow(frame); | |
163 | ||
164 | return TRUE; | |
165 | } | |
166 | ||
167 | // My frame constructor | |
168 | MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h): | |
169 | wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h)) | |
170 | { | |
171 | m_listCtrl = NULL; | |
172 | m_logWindow = NULL; | |
173 | } | |
174 | ||
175 | MyFrame::~MyFrame(void) | |
176 | { | |
177 | delete wxGetApp().m_imageListNormal; | |
178 | delete wxGetApp().m_imageListSmall; | |
179 | } | |
180 | ||
181 | void MyFrame::OnQuit(wxCommandEvent& event) | |
182 | { | |
183 | Close(TRUE); | |
184 | } | |
185 | ||
186 | void MyFrame::OnAbout(wxCommandEvent& event) | |
187 | { | |
188 | wxMessageDialog dialog(this, "List test sample\nJulian Smart (c) 1997", | |
189 | "About list test", wxOK|wxCANCEL); | |
190 | ||
191 | dialog.ShowModal(); | |
192 | } | |
193 | ||
194 | void MyFrame::OnListView(wxCommandEvent& event) | |
195 | { | |
196 | m_logWindow->Clear(); | |
197 | m_listCtrl->DeleteAllItems(); | |
198 | m_listCtrl->SetSingleStyle(wxLC_LIST); | |
199 | m_listCtrl->SetImageList(NULL, wxIMAGE_LIST_NORMAL); | |
200 | m_listCtrl->SetImageList(NULL, wxIMAGE_LIST_SMALL); | |
201 | ||
202 | for ( int i=0; i < 30; i++) | |
203 | { | |
204 | char buf[20]; | |
205 | sprintf(buf, "Item %d", i); | |
206 | long tmp = m_listCtrl->InsertItem(i, buf); | |
207 | } | |
208 | } | |
209 | ||
210 | void MyFrame::OnReportView(wxCommandEvent& event) | |
211 | { | |
212 | m_logWindow->Clear(); | |
213 | m_listCtrl->DeleteAllItems(); | |
214 | m_listCtrl->SetSingleStyle(wxLC_REPORT); | |
215 | m_listCtrl->SetImageList(NULL, wxIMAGE_LIST_NORMAL); | |
216 | m_listCtrl->SetImageList(wxGetApp().m_imageListSmall, wxIMAGE_LIST_SMALL); | |
217 | ||
218 | m_listCtrl->InsertColumn(0, "Column 1", wxLIST_FORMAT_LEFT, 140); | |
219 | m_listCtrl->InsertColumn(1, "Column 2", wxLIST_FORMAT_LEFT, 140); | |
220 | ||
221 | for ( int i=0; i < 30; i++) | |
222 | { | |
223 | char buf[20]; | |
224 | sprintf(buf, "Item %d, col 1", i); | |
225 | long tmp = m_listCtrl->InsertItem(i, buf, 0); | |
226 | ||
227 | sprintf(buf, "Item %d, col 2", i); | |
228 | tmp = m_listCtrl->SetItem(i, 1, buf); | |
229 | } | |
230 | } | |
231 | ||
232 | void MyFrame::OnIconView(wxCommandEvent& event) | |
233 | { | |
234 | m_logWindow->Clear(); | |
235 | m_listCtrl->DeleteAllItems(); | |
236 | m_listCtrl->SetSingleStyle(wxLC_ICON); | |
237 | m_listCtrl->SetImageList(wxGetApp().m_imageListNormal, wxIMAGE_LIST_NORMAL); | |
238 | m_listCtrl->SetImageList(wxGetApp().m_imageListSmall, wxIMAGE_LIST_SMALL); | |
239 | ||
240 | for ( int i=0; i < 9; i++) | |
241 | { | |
242 | long tmp = m_listCtrl->InsertItem(i, i); | |
243 | } | |
244 | } | |
245 | ||
246 | void MyFrame::OnIconTextView(wxCommandEvent& event) | |
247 | { | |
248 | m_logWindow->Clear(); | |
249 | m_listCtrl->DeleteAllItems(); | |
250 | m_listCtrl->SetSingleStyle(wxLC_ICON); | |
251 | m_listCtrl->SetImageList(wxGetApp().m_imageListNormal, wxIMAGE_LIST_NORMAL); | |
252 | m_listCtrl->SetImageList(wxGetApp().m_imageListSmall, wxIMAGE_LIST_SMALL); | |
253 | ||
254 | for ( int i=0; i < 9; i++) | |
255 | { | |
256 | char buf[20]; | |
257 | sprintf(buf, "Label %d", i); | |
258 | long tmp = m_listCtrl->InsertItem(i, buf, i); | |
259 | } | |
260 | } | |
261 | ||
262 | void MyFrame::OnSmallIconView(wxCommandEvent& event) | |
263 | { | |
264 | m_logWindow->Clear(); | |
265 | m_listCtrl->DeleteAllItems(); | |
266 | m_listCtrl->SetSingleStyle(wxLC_SMALL_ICON); | |
267 | m_listCtrl->SetImageList(wxGetApp().m_imageListNormal, wxIMAGE_LIST_NORMAL); | |
268 | m_listCtrl->SetImageList(wxGetApp().m_imageListSmall, wxIMAGE_LIST_SMALL); | |
269 | ||
270 | for ( int i=0; i < 9; i++) | |
271 | { | |
272 | long tmp = m_listCtrl->InsertItem(i, 0); | |
273 | } | |
274 | } | |
275 | ||
276 | void MyFrame::OnSmallIconTextView(wxCommandEvent& event) | |
277 | { | |
278 | m_logWindow->Clear(); | |
279 | m_listCtrl->DeleteAllItems(); | |
280 | m_listCtrl->SetSingleStyle(wxLC_SMALL_ICON); | |
281 | m_listCtrl->SetImageList(wxGetApp().m_imageListNormal, wxIMAGE_LIST_NORMAL); | |
282 | m_listCtrl->SetImageList(wxGetApp().m_imageListSmall, wxIMAGE_LIST_SMALL); | |
283 | ||
284 | for ( int i=0; i < 9; i++) | |
285 | { | |
286 | long tmp = m_listCtrl->InsertItem(i, "Label", 0); | |
287 | } | |
288 | } | |
289 | ||
290 | // MyListCtrl | |
291 | ||
292 | void MyListCtrl::OnBeginDrag(wxListEvent& event) | |
293 | { | |
294 | if ( !wxGetApp().GetTopWindow() ) | |
295 | return; | |
296 | ||
297 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
298 | if ( !text ) | |
299 | return; | |
300 | ||
301 | #ifndef __GNUWIN32__ | |
302 | ostream str(text); | |
303 | ||
304 | str << "OnBeginDrag\n"; | |
305 | str.flush(); | |
306 | #endif | |
307 | } | |
308 | ||
309 | void MyListCtrl::OnBeginRDrag(wxListEvent& event) | |
310 | { | |
311 | if ( !wxGetApp().GetTopWindow() ) | |
312 | return; | |
313 | ||
314 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
315 | if ( !text ) | |
316 | return; | |
317 | #ifndef __GNUWIN32__ | |
318 | ostream str(text); | |
319 | ||
320 | str << "OnBeginRDrag\n"; | |
321 | str.flush(); | |
322 | #endif | |
323 | } | |
324 | ||
325 | void MyListCtrl::OnBeginLabelEdit(wxListEvent& event) | |
326 | { | |
327 | if ( !wxGetApp().GetTopWindow() ) | |
328 | return; | |
329 | ||
330 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
331 | if ( !text ) | |
332 | return; | |
333 | ||
334 | #ifndef __GNUWIN32__ | |
335 | ostream str(text); | |
336 | ||
337 | str << "OnBeginLabelEdit\n"; | |
338 | str.flush(); | |
339 | #endif | |
340 | } | |
341 | ||
342 | void MyListCtrl::OnEndLabelEdit(wxListEvent& event) | |
343 | { | |
344 | if ( !wxGetApp().GetTopWindow() ) | |
345 | return; | |
346 | ||
347 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
348 | if ( !text ) | |
349 | return; | |
350 | ||
351 | #ifndef __GNUWIN32__ | |
352 | ostream str(text); | |
353 | ||
354 | str << "OnEndLabelEdit\n"; | |
355 | str.flush(); | |
356 | #endif | |
357 | } | |
358 | ||
359 | void MyListCtrl::OnDeleteItem(wxListEvent& event) | |
360 | { | |
361 | if ( !wxGetApp().GetTopWindow() ) | |
362 | return; | |
363 | ||
364 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
365 | if ( !text ) | |
366 | return; | |
367 | ||
368 | #ifndef __GNUWIN32__ | |
369 | ostream str(text); | |
370 | ||
371 | str << "OnDeleteItem\n"; | |
372 | str.flush(); | |
373 | #endif | |
374 | } | |
375 | ||
376 | void MyListCtrl::OnGetInfo(wxListEvent& event) | |
377 | { | |
378 | if ( !wxGetApp().GetTopWindow() ) | |
379 | return; | |
380 | ||
381 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
382 | if ( !text ) | |
383 | return; | |
384 | ||
385 | #ifndef __GNUWIN32__ | |
386 | ostream str(text); | |
387 | ||
388 | str << "OnGetInfo (" << event.m_item.m_itemId << ", " << event.m_item.m_col << ")"; | |
389 | if ( event.m_item.m_mask & wxLIST_MASK_STATE ) | |
390 | str << " wxLIST_MASK_STATE"; | |
391 | if ( event.m_item.m_mask & wxLIST_MASK_TEXT ) | |
392 | str << " wxLIST_MASK_TEXT"; | |
393 | if ( event.m_item.m_mask & wxLIST_MASK_IMAGE ) | |
394 | str << " wxLIST_MASK_IMAGE"; | |
395 | if ( event.m_item.m_mask & wxLIST_MASK_DATA ) | |
396 | str << " wxLIST_MASK_DATA"; | |
397 | if ( event.m_item.m_mask & wxLIST_SET_ITEM ) | |
398 | str << " wxLIST_SET_ITEM"; | |
399 | if ( event.m_item.m_mask & wxLIST_MASK_WIDTH ) | |
400 | str << " wxLIST_MASK_WIDTH"; | |
401 | if ( event.m_item.m_mask & wxLIST_MASK_FORMAT ) | |
402 | str << " wxLIST_MASK_WIDTH"; | |
403 | ||
404 | if ( event.m_item.m_mask & wxLIST_MASK_TEXT ) | |
405 | { | |
406 | event.m_item.m_text = "My callback text"; | |
407 | } | |
408 | str << "\n"; | |
409 | str.flush(); | |
410 | #endif | |
411 | } | |
412 | ||
413 | void MyListCtrl::OnSetInfo(wxListEvent& event) | |
414 | { | |
415 | if ( !wxGetApp().GetTopWindow() ) | |
416 | return; | |
417 | ||
418 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
419 | if ( !text ) | |
420 | return; | |
421 | ||
422 | #ifndef __GNUWIN32__ | |
423 | ostream str(text); | |
424 | ||
425 | str << "OnSetInfo\n"; | |
426 | str.flush(); | |
427 | #endif | |
428 | } | |
429 | ||
430 | void MyListCtrl::OnSelected(wxListEvent& event) | |
431 | { | |
432 | if ( !wxGetApp().GetTopWindow() ) | |
433 | return; | |
434 | ||
435 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
436 | if ( !text ) | |
437 | return; | |
438 | ||
439 | #ifndef __GNUWIN32__ | |
440 | ostream str(text); | |
441 | ||
442 | str << "OnSelected\n"; | |
443 | str.flush(); | |
444 | #endif | |
445 | } | |
446 | ||
447 | void MyListCtrl::OnDeselected(wxListEvent& event) | |
448 | { | |
449 | if ( !wxGetApp().GetTopWindow() ) | |
450 | return; | |
451 | ||
452 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
453 | if ( !text ) | |
454 | return; | |
455 | ||
456 | #ifndef __GNUWIN32__ | |
457 | ostream str(text); | |
458 | ||
459 | str << "OnDeselected\n"; | |
460 | str.flush(); | |
461 | #endif | |
462 | } | |
463 | ||
464 | void MyListCtrl::OnKeyDown(wxListEvent& event) | |
465 | { | |
466 | if ( !wxGetApp().GetTopWindow() ) | |
467 | return; | |
468 | ||
469 | wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow; | |
470 | if ( !text ) | |
471 | return; | |
472 | ||
473 | #ifndef __GNUWIN32__ | |
474 | ostream str(text); | |
475 | ||
476 | str << "OnKeyDown\n"; | |
477 | str.flush(); | |
478 | #endif | |
479 | } | |
480 |