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