]> git.saurik.com Git - wxWidgets.git/blob - samples/listctrl/listtest.cpp
compile fixes for EMX
[wxWidgets.git] / samples / listctrl / listtest.cpp
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 #ifndef __WXMSW__
29 #include "mondrian.xpm"
30 #endif
31
32 #include "wx/listctrl.h"
33 #include "listtest.h"
34
35 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
36 EVT_MENU(BUSY_ON, MyFrame::BusyOn)
37 EVT_MENU(BUSY_OFF, MyFrame::BusyOff)
38 EVT_MENU(LIST_QUIT, MyFrame::OnQuit)
39 EVT_MENU(LIST_ABOUT, MyFrame::OnAbout)
40 EVT_MENU(LIST_LIST_VIEW, MyFrame::OnListView)
41 EVT_MENU(LIST_REPORT_VIEW, MyFrame::OnReportView)
42 EVT_MENU(LIST_ICON_VIEW, MyFrame::OnIconView)
43 EVT_MENU(LIST_ICON_TEXT_VIEW, MyFrame::OnIconTextView)
44 EVT_MENU(LIST_SMALL_ICON_VIEW, MyFrame::OnSmallIconView)
45 EVT_MENU(LIST_SMALL_ICON_TEXT_VIEW, MyFrame::OnSmallIconTextView)
46 EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
47 EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
48 EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll)
49 END_EVENT_TABLE()
50
51 BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
52 EVT_LIST_BEGIN_DRAG(LIST_CTRL, MyListCtrl::OnBeginDrag)
53 EVT_LIST_BEGIN_RDRAG(LIST_CTRL, MyListCtrl::OnBeginRDrag)
54 EVT_LIST_BEGIN_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnBeginLabelEdit)
55 EVT_LIST_END_LABEL_EDIT(LIST_CTRL, MyListCtrl::OnEndLabelEdit)
56 EVT_LIST_DELETE_ITEM(LIST_CTRL, MyListCtrl::OnDeleteItem)
57 EVT_LIST_GET_INFO(LIST_CTRL, MyListCtrl::OnGetInfo)
58 EVT_LIST_SET_INFO(LIST_CTRL, MyListCtrl::OnSetInfo)
59 EVT_LIST_ITEM_SELECTED(LIST_CTRL, MyListCtrl::OnSelected)
60 EVT_LIST_ITEM_DESELECTED(LIST_CTRL, MyListCtrl::OnDeselected)
61 EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
62 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
63 END_EVENT_TABLE()
64
65 IMPLEMENT_APP(MyApp)
66
67 // `Main program' equivalent, creating windows and returning main app frame
68 bool MyApp::OnInit(void)
69 {
70 // Create the main frame window
71 MyFrame *frame = new MyFrame((wxFrame *) NULL, "wxListCtrl Test", 50, 50, 450, 340);
72
73 // This reduces flicker effects - even better would be to define OnEraseBackground
74 // to do nothing. When the list control's scrollbars are show or hidden, the
75 // frame is sent a background erase event.
76 frame->SetBackgroundColour( *wxWHITE );
77
78 // Give it an icon
79 frame->SetIcon( wxICON(mondrian) );
80
81 // Make an image list containing large icons
82 m_imageListNormal = new wxImageList(32, 32, TRUE);
83 m_imageListSmall = new wxImageList(16, 16, TRUE);
84
85 #ifdef __WXMSW__
86 m_imageListNormal->Add( wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE) );
87 m_imageListNormal->Add( wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE) );
88 m_imageListNormal->Add( wxIcon("icon3", wxBITMAP_TYPE_ICO_RESOURCE) );
89 m_imageListNormal->Add( wxIcon("icon4", wxBITMAP_TYPE_ICO_RESOURCE) );
90 m_imageListNormal->Add( wxIcon("icon5", wxBITMAP_TYPE_ICO_RESOURCE) );
91 m_imageListNormal->Add( wxIcon("icon6", wxBITMAP_TYPE_ICO_RESOURCE) );
92 m_imageListNormal->Add( wxIcon("icon7", wxBITMAP_TYPE_ICO_RESOURCE) );
93 m_imageListNormal->Add( wxIcon("icon8", wxBITMAP_TYPE_ICO_RESOURCE) );
94 m_imageListNormal->Add( wxIcon("icon9", wxBITMAP_TYPE_ICO_RESOURCE) );
95
96 m_imageListSmall->Add( wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE) );
97
98 #else
99
100 #include "bitmaps/toolbrai.xpm"
101 m_imageListNormal->Add( wxIcon( toolbrai_xpm ) );
102 #include "bitmaps/toolchar.xpm"
103 m_imageListNormal->Add( wxIcon( toolchar_xpm ) );
104 #include "bitmaps/tooldata.xpm"
105 m_imageListNormal->Add( wxIcon( tooldata_xpm ) );
106 #include "bitmaps/toolnote.xpm"
107 m_imageListNormal->Add( wxIcon( toolnote_xpm ) );
108 #include "bitmaps/tooltodo.xpm"
109 m_imageListNormal->Add( wxIcon( tooltodo_xpm ) );
110 #include "bitmaps/toolchec.xpm"
111 m_imageListNormal->Add( wxIcon( toolchec_xpm ) );
112 #include "bitmaps/toolgame.xpm"
113 m_imageListNormal->Add( wxIcon( toolgame_xpm ) );
114 #include "bitmaps/tooltime.xpm"
115 m_imageListNormal->Add( wxIcon( tooltime_xpm ) );
116 #include "bitmaps/toolword.xpm"
117 m_imageListNormal->Add( wxIcon( toolword_xpm ) );
118
119 #include "bitmaps/small1.xpm"
120 m_imageListSmall->Add( wxIcon( small1_xpm) );
121
122 #endif
123
124 // Make a menubar
125 wxMenu *file_menu = new wxMenu;
126
127 file_menu->Append(LIST_LIST_VIEW, "&List view");
128 file_menu->Append(LIST_REPORT_VIEW, "&Report view");
129 file_menu->Append(LIST_ICON_VIEW, "&Icon view");
130 file_menu->Append(LIST_ICON_TEXT_VIEW, "Icon view with &text");
131 file_menu->Append(LIST_SMALL_ICON_VIEW, "&Small icon view");
132 file_menu->Append(LIST_SMALL_ICON_TEXT_VIEW, "Small icon &view with text");
133 file_menu->Append(LIST_DESELECT_ALL, "&Deselect All");
134 file_menu->Append(LIST_SELECT_ALL, "S&elect All");
135 file_menu->AppendSeparator();
136 file_menu->Append(LIST_DELETE_ALL, "Delete &all items");
137 file_menu->AppendSeparator();
138 file_menu->Append(BUSY_ON, "&Busy cursor on");
139 file_menu->Append(BUSY_OFF, "&Busy cursor off");
140 file_menu->AppendSeparator();
141 file_menu->Append(LIST_ABOUT, "&About");
142 file_menu->Append(LIST_QUIT, "E&xit");
143 wxMenuBar *menu_bar = new wxMenuBar;
144 menu_bar->Append(file_menu, "&File");
145 frame->SetMenuBar(menu_bar);
146
147 // Make a panel with a message
148 frame->m_listCtrl = new MyListCtrl(frame, LIST_CTRL, wxPoint(0, 0), wxSize(400, 200),
149 wxLC_LIST|wxSUNKEN_BORDER|wxLC_EDIT_LABELS);
150 // wxLC_LIST|wxLC_USER_TEXT|wxSUNKEN_BORDER); // wxLC_USER_TEXT requires app to supply all text on demand
151 frame->m_logWindow = new wxTextCtrl(frame, -1, "", wxPoint(0, 0), wxSize(400, 200), wxTE_MULTILINE|wxSUNKEN_BORDER);
152
153 wxLayoutConstraints *c = new wxLayoutConstraints;
154 c->top.SameAs (frame, wxTop);
155 c->left.SameAs (frame, wxLeft);
156 c->right.SameAs (frame, wxRight);
157 c->height.PercentOf (frame, wxHeight, 66);
158 frame->m_listCtrl->SetConstraints(c);
159
160 c = new wxLayoutConstraints;
161 c->top.Below (frame->m_listCtrl);
162 c->left.SameAs (frame, wxLeft);
163 c->right.SameAs (frame, wxRight);
164 c->bottom.SameAs (frame, wxBottom);
165 frame->m_logWindow->SetConstraints(c);
166 frame->SetAutoLayout(TRUE);
167
168 for ( int i=0; i < 30; i++)
169 {
170 wxChar buf[20];
171 wxSprintf(buf, _T("Item %d"), i);
172 frame->m_listCtrl->InsertItem(i, buf);
173 }
174
175 frame->CreateStatusBar(3);
176 frame->SetStatusText("", 0);
177
178 // Show the frame
179 frame->Show(TRUE);
180
181 SetTopWindow(frame);
182
183 return TRUE;
184 }
185
186 // My frame constructor
187 MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h):
188 wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
189 {
190 m_listCtrl = (MyListCtrl *) NULL;
191 m_logWindow = (wxTextCtrl *) NULL;
192 }
193
194 MyFrame::~MyFrame(void)
195 {
196 delete wxGetApp().m_imageListNormal;
197 delete wxGetApp().m_imageListSmall;
198 }
199
200 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
201 {
202 Close(TRUE);
203 }
204
205 void MyFrame::BusyOn(wxCommandEvent& WXUNUSED(event))
206 {
207 wxBeginBusyCursor();
208 }
209
210 void MyFrame::BusyOff(wxCommandEvent& WXUNUSED(event))
211 {
212 wxEndBusyCursor();
213 }
214
215 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
216 {
217 wxMessageDialog dialog(this, "List test sample\nJulian Smart (c) 1997",
218 "About list test", wxOK|wxCANCEL);
219
220 dialog.ShowModal();
221 }
222
223 void MyFrame::OnDeselectAll(wxCommandEvent& WXUNUSED(event))
224 {
225 int n = m_listCtrl->GetItemCount();
226 for (int i = 0; i < n; i++)
227 m_listCtrl->SetItemState(i,0,wxLIST_STATE_SELECTED);
228 }
229
230 void MyFrame::OnSelectAll(wxCommandEvent& WXUNUSED(event))
231 {
232 int n = m_listCtrl->GetItemCount();
233 for (int i = 0; i < n; i++)
234 m_listCtrl->SetItemState(i,wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
235 }
236
237 void MyFrame::OnListView(wxCommandEvent& WXUNUSED(event))
238 {
239 m_listCtrl->DeleteAllItems();
240 m_logWindow->Clear();
241 m_listCtrl->SetSingleStyle(wxLC_LIST);
242 m_listCtrl->SetImageList((wxImageList *) NULL, wxIMAGE_LIST_NORMAL);
243 m_listCtrl->SetImageList((wxImageList *) NULL, wxIMAGE_LIST_SMALL);
244
245 for ( int i=0; i < 30; i++)
246 {
247 wxChar buf[20];
248 wxSprintf(buf, _T("Item %d"), i);
249 m_listCtrl->InsertItem(i, buf);
250 }
251 }
252
253 void MyFrame::OnReportView(wxCommandEvent& WXUNUSED(event))
254 {
255 m_listCtrl->DeleteAllItems();
256 m_listCtrl->DeleteAllColumns();
257 m_logWindow->Clear();
258
259 m_listCtrl->SetSingleStyle(wxLC_REPORT);
260 m_listCtrl->SetImageList((wxImageList *) NULL, wxIMAGE_LIST_NORMAL);
261 m_listCtrl->SetImageList(wxGetApp().m_imageListSmall, wxIMAGE_LIST_SMALL);
262
263 m_listCtrl->InsertColumn(0, "Column 1"); // , wxLIST_FORMAT_LEFT, 140);
264 m_listCtrl->InsertColumn(1, "Column 2"); // , wxLIST_FORMAT_LEFT, 140);
265 m_listCtrl->InsertColumn(2, "One More Column (2)"); // , wxLIST_FORMAT_LEFT, 140);
266
267 for ( int i = 0; i < 3000; i++ )
268 {
269 wxChar buf[50];
270 wxSprintf(buf, _T("This is item %d"), i);
271 long tmp = m_listCtrl->InsertItem(i, buf, 0);
272
273 wxSprintf(buf, _T("Col 1, item %d"), i);
274 tmp = m_listCtrl->SetItem(i, 1, buf);
275
276 wxSprintf(buf, _T("Item %d in column 2"), i);
277 tmp = m_listCtrl->SetItem(i, 2, buf);
278 }
279
280 m_listCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE );
281 m_listCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE );
282 m_listCtrl->SetColumnWidth( 2, wxLIST_AUTOSIZE );
283 }
284
285 void MyFrame::OnIconView(wxCommandEvent& WXUNUSED(event))
286 {
287 m_listCtrl->DeleteAllItems();
288 m_logWindow->Clear();
289 m_listCtrl->SetSingleStyle(wxLC_ICON);
290 m_listCtrl->SetImageList(wxGetApp().m_imageListNormal, wxIMAGE_LIST_NORMAL);
291 m_listCtrl->SetImageList(wxGetApp().m_imageListSmall, wxIMAGE_LIST_SMALL);
292
293 for ( int i=0; i < 9; i++)
294 {
295 m_listCtrl->InsertItem(i, i);
296 }
297 }
298
299 void MyFrame::OnIconTextView(wxCommandEvent& WXUNUSED(event))
300 {
301 m_listCtrl->DeleteAllItems();
302 m_logWindow->Clear();
303 m_listCtrl->SetSingleStyle(wxLC_ICON);
304 m_listCtrl->SetImageList(wxGetApp().m_imageListNormal, wxIMAGE_LIST_NORMAL);
305 m_listCtrl->SetImageList(wxGetApp().m_imageListSmall, wxIMAGE_LIST_SMALL);
306
307 for ( int i=0; i < 9; i++)
308 {
309 wxChar buf[20];
310 wxSprintf(buf, _T("Label %d"), i);
311 m_listCtrl->InsertItem(i, buf, i);
312 }
313 }
314
315 void MyFrame::OnSmallIconView(wxCommandEvent& WXUNUSED(event))
316 {
317 m_listCtrl->DeleteAllItems();
318 m_logWindow->Clear();
319 m_listCtrl->SetSingleStyle(wxLC_SMALL_ICON);
320 m_listCtrl->SetImageList(wxGetApp().m_imageListNormal, wxIMAGE_LIST_NORMAL);
321 m_listCtrl->SetImageList(wxGetApp().m_imageListSmall, wxIMAGE_LIST_SMALL);
322
323 for ( int i=0; i < 9; i++)
324 {
325 m_listCtrl->InsertItem(i, 0);
326 }
327 }
328
329 void MyFrame::OnSmallIconTextView(wxCommandEvent& WXUNUSED(event))
330 {
331 m_listCtrl->DeleteAllItems();
332 m_logWindow->Clear();
333 m_listCtrl->SetSingleStyle(wxLC_SMALL_ICON);
334 m_listCtrl->SetImageList(wxGetApp().m_imageListNormal, wxIMAGE_LIST_NORMAL);
335 m_listCtrl->SetImageList(wxGetApp().m_imageListSmall, wxIMAGE_LIST_SMALL);
336
337 for ( int i=0; i < 9; i++)
338 {
339 m_listCtrl->InsertItem(i, "Label", 0);
340 }
341 }
342
343 void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
344 {
345 (void)wxGetElapsedTime(TRUE);
346
347 int nItems = m_listCtrl->GetItemCount();
348 m_listCtrl->DeleteAllItems();
349
350 wxLogMessage("Deleting %d items took %ld ms",
351 nItems, wxGetElapsedTime());
352 }
353
354 // MyListCtrl
355
356 void MyListCtrl::OnBeginDrag(wxListEvent& event)
357 {
358 if ( !wxGetApp().GetTopWindow() )
359 return;
360
361 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
362 if ( !text )
363 return;
364
365 wxString msg;
366 msg.Printf( "OnBeginDrag at %d,%d.\n", event.m_pointDrag.x, event.m_pointDrag.y );
367 text->WriteText(msg);
368 }
369
370 void MyListCtrl::OnBeginRDrag(wxListEvent& event)
371 {
372 if ( !wxGetApp().GetTopWindow() )
373 return;
374
375 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
376 if ( !text )
377 return;
378
379 wxString msg;
380 msg.Printf( "OnBeginRDrag at %d,%d.\n", event.m_pointDrag.x, event.m_pointDrag.y );
381 text->WriteText(msg);
382 }
383
384 void MyListCtrl::OnBeginLabelEdit(wxListEvent& event)
385 {
386 if ( !wxGetApp().GetTopWindow() )
387 return;
388
389 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
390 if ( !text )
391 return;
392
393 text->WriteText("OnBeginLabelEdit: ");
394 text->WriteText(event.m_item.m_text);
395 text->WriteText("\n");
396 }
397
398 void MyListCtrl::OnEndLabelEdit(wxListEvent& event)
399 {
400 if ( !wxGetApp().GetTopWindow() )
401 return;
402
403 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
404 if ( !text )
405 return;
406
407 text->WriteText("OnEndLabelEdit: ");
408 text->WriteText(event.m_item.m_text);
409 if (event.m_cancelled)
410 text->WriteText(" cancelled by user\n");
411 else
412 text->WriteText(" accepted by user\n");
413 }
414
415 void MyListCtrl::OnDeleteItem(wxListEvent& WXUNUSED(event))
416 {
417 if ( !wxGetApp().GetTopWindow() )
418 return;
419
420 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
421 if ( !text )
422 return;
423
424 text->WriteText("OnDeleteItem\n");
425 }
426
427 void MyListCtrl::OnGetInfo(wxListEvent& event)
428 {
429 if ( !wxGetApp().GetTopWindow() )
430 return;
431
432 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
433 if ( !text )
434 return;
435
436 text->WriteText("OnGetInfo\n");
437
438 (*text) << "OnGetInfo (" << event.m_item.m_itemId << ", " << event.m_item.m_col << ")";
439 if ( event.m_item.m_mask & wxLIST_MASK_STATE )
440 (*text) << " wxLIST_MASK_STATE";
441 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
442 (*text) << " wxLIST_MASK_TEXT";
443 if ( event.m_item.m_mask & wxLIST_MASK_IMAGE )
444 (*text) << " wxLIST_MASK_IMAGE";
445 if ( event.m_item.m_mask & wxLIST_MASK_DATA )
446 (*text) << " wxLIST_MASK_DATA";
447 if ( event.m_item.m_mask & wxLIST_SET_ITEM )
448 (*text) << " wxLIST_SET_ITEM";
449 if ( event.m_item.m_mask & wxLIST_MASK_WIDTH )
450 (*text) << " wxLIST_MASK_WIDTH";
451 if ( event.m_item.m_mask & wxLIST_MASK_FORMAT )
452 (*text) << " wxLIST_MASK_WIDTH";
453
454 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
455 {
456 event.m_item.m_text = "My callback text";
457 }
458 (*text) << "\n";
459 }
460
461 void MyListCtrl::OnSetInfo(wxListEvent& WXUNUSED(event))
462 {
463 if ( !wxGetApp().GetTopWindow() )
464 return;
465
466 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
467 if ( !text )
468 return;
469
470 text->WriteText("OnSetInfo\n");
471 }
472
473 void MyListCtrl::OnSelected(wxListEvent& WXUNUSED(event))
474 {
475 if ( !wxGetApp().GetTopWindow() )
476 return;
477
478 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
479 if ( !text )
480 return;
481
482 text->WriteText("OnSelected\n");
483 }
484
485 void MyListCtrl::OnDeselected(wxListEvent& WXUNUSED(event))
486 {
487 if ( !wxGetApp().GetTopWindow() )
488 return;
489
490 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
491 if ( !text )
492 return;
493
494 text->WriteText("OnDeselected\n");
495 }
496
497 void MyListCtrl::OnActivated(wxListEvent& WXUNUSED(event))
498 {
499 if ( !wxGetApp().GetTopWindow() )
500 return;
501
502 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
503 if ( !text )
504 return;
505
506 text->WriteText("OnActivated\n");
507 }
508
509 void MyListCtrl::OnListKeyDown(wxListEvent& event)
510 {
511 if ( !wxGetApp().GetTopWindow() )
512 return;
513
514 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
515 if ( !text )
516 return;
517
518 text->WriteText("OnListKeyDown\n");
519 }
520
521