]> git.saurik.com Git - wxWidgets.git/blob - samples/listctrl/listtest.cpp
Removed redundant makefiles and AIAI icons. Changed dynamic sample source name from
[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 #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 __WXMSW__
70 frame->SetIcon(wxIcon("mondrian"));
71 #else
72 #include "mondrian.xpm"
73 frame->SetIcon(wxIcon(mondrian_xpm));
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 #ifdef __WXMSW__
81 m_imageListNormal->Add( wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE) );
82 m_imageListNormal->Add( wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE) );
83 m_imageListNormal->Add( wxIcon("icon3", wxBITMAP_TYPE_ICO_RESOURCE) );
84 m_imageListNormal->Add( wxIcon("icon4", wxBITMAP_TYPE_ICO_RESOURCE) );
85 m_imageListNormal->Add( wxIcon("icon5", wxBITMAP_TYPE_ICO_RESOURCE) );
86 m_imageListNormal->Add( wxIcon("icon6", wxBITMAP_TYPE_ICO_RESOURCE) );
87 m_imageListNormal->Add( wxIcon("icon7", wxBITMAP_TYPE_ICO_RESOURCE) );
88 m_imageListNormal->Add( wxIcon("icon8", wxBITMAP_TYPE_ICO_RESOURCE) );
89 m_imageListNormal->Add( wxIcon("icon9", wxBITMAP_TYPE_ICO_RESOURCE) );
90
91 m_imageListSmall->Add( wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE) );
92
93 #else
94
95 #include "bitmaps/toolbrai.xpm"
96 m_imageListNormal->Add( wxIcon( toolbrai_xpm ) );
97 #include "bitmaps/toolchar.xpm"
98 m_imageListNormal->Add( wxIcon( toolchar_xpm ) );
99 #include "bitmaps/tooldata.xpm"
100 m_imageListNormal->Add( wxIcon( tooldata_xpm ) );
101 #include "bitmaps/toolnote.xpm"
102 m_imageListNormal->Add( wxIcon( toolnote_xpm ) );
103 #include "bitmaps/tooltodo.xpm"
104 m_imageListNormal->Add( wxIcon( tooltodo_xpm ) );
105 #include "bitmaps/toolchec.xpm"
106 m_imageListNormal->Add( wxIcon( toolchec_xpm ) );
107 #include "bitmaps/toolgame.xpm"
108 m_imageListNormal->Add( wxIcon( toolgame_xpm ) );
109 #include "bitmaps/tooltime.xpm"
110 m_imageListNormal->Add( wxIcon( tooltime_xpm ) );
111 #include "bitmaps/toolword.xpm"
112 m_imageListNormal->Add( wxIcon( toolword_xpm ) );
113
114 #include "bitmaps/small1.xpm"
115 m_imageListSmall->Add( wxIcon( small1_xpm) );
116
117 #endif
118
119 // Make a menubar
120 wxMenu *file_menu = new wxMenu;
121
122 file_menu->Append(LIST_LIST_VIEW, "&List view");
123 file_menu->Append(LIST_REPORT_VIEW, "&Report view");
124 file_menu->Append(LIST_ICON_VIEW, "&Icon view");
125 file_menu->Append(LIST_ICON_TEXT_VIEW, "Icon view with &text");
126 file_menu->Append(LIST_SMALL_ICON_VIEW, "&Small icon view");
127 file_menu->Append(LIST_SMALL_ICON_TEXT_VIEW, "Small icon &view with text");
128 file_menu->AppendSeparator();
129 file_menu->Append(LIST_ABOUT, "&About");
130 file_menu->Append(LIST_QUIT, "E&xit");
131 wxMenuBar *menu_bar = new wxMenuBar;
132 menu_bar->Append(file_menu, "&File");
133 frame->SetMenuBar(menu_bar);
134
135 // Make a panel with a message
136 frame->m_listCtrl = new MyListCtrl(frame, LIST_CTRL, wxPoint(0, 0), wxSize(400, 200),
137 wxLC_LIST|wxSUNKEN_BORDER);
138 // wxLC_LIST|wxLC_USER_TEXT|wxSUNKEN_BORDER); // wxLC_USER_TEXT requires app to supply all text on demand
139 frame->m_logWindow = new wxTextCtrl(frame, -1, "", wxPoint(0, 0), wxSize(400, 200), wxTE_MULTILINE|wxSUNKEN_BORDER);
140
141 wxLayoutConstraints *c = new wxLayoutConstraints;
142 c->top.SameAs (frame, wxTop);
143 c->left.SameAs (frame, wxLeft);
144 c->right.SameAs (frame, wxRight);
145 c->height.PercentOf (frame, wxHeight, 66);
146 frame->m_listCtrl->SetConstraints(c);
147
148 c = new wxLayoutConstraints;
149 c->top.Below (frame->m_listCtrl);
150 c->left.SameAs (frame, wxLeft);
151 c->right.SameAs (frame, wxRight);
152 c->bottom.SameAs (frame, wxBottom);
153 frame->m_logWindow->SetConstraints(c);
154 frame->SetAutoLayout(TRUE);
155
156 for ( int i=0; i < 30; i++)
157 {
158 char buf[20];
159 sprintf(buf, "Item %d", i);
160 long tmp = frame->m_listCtrl->InsertItem(i, buf);
161 }
162
163 frame->CreateStatusBar(3);
164 frame->SetStatusText("", 0);
165
166 // Show the frame
167 frame->Show(TRUE);
168
169 SetTopWindow(frame);
170
171 return TRUE;
172 }
173
174 // My frame constructor
175 MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h):
176 wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
177 {
178 m_listCtrl = NULL;
179 m_logWindow = NULL;
180 }
181
182 MyFrame::~MyFrame(void)
183 {
184 delete wxGetApp().m_imageListNormal;
185 delete wxGetApp().m_imageListSmall;
186 }
187
188 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
189 {
190 Close(TRUE);
191 }
192
193 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
194 {
195 wxMessageDialog dialog(this, "List test sample\nJulian Smart (c) 1997",
196 "About list test", wxOK|wxCANCEL);
197
198 dialog.ShowModal();
199 }
200
201 void MyFrame::OnListView(wxCommandEvent& WXUNUSED(event))
202 {
203 m_logWindow->Clear();
204 m_listCtrl->DeleteAllItems();
205 m_listCtrl->SetSingleStyle(wxLC_LIST);
206 m_listCtrl->SetImageList(NULL, wxIMAGE_LIST_NORMAL);
207 m_listCtrl->SetImageList(NULL, wxIMAGE_LIST_SMALL);
208
209 for ( int i=0; i < 30; i++)
210 {
211 char buf[20];
212 sprintf(buf, "Item %d", i);
213 long tmp = m_listCtrl->InsertItem(i, buf);
214 }
215 }
216
217 void MyFrame::OnReportView(wxCommandEvent& WXUNUSED(event))
218 {
219 m_logWindow->Clear();
220 m_listCtrl->DeleteAllItems();
221 m_listCtrl->SetSingleStyle(wxLC_REPORT);
222 m_listCtrl->SetImageList(NULL, wxIMAGE_LIST_NORMAL);
223 m_listCtrl->SetImageList(wxGetApp().m_imageListSmall, wxIMAGE_LIST_SMALL);
224
225 m_listCtrl->InsertColumn(0, "Column 1", wxLIST_FORMAT_LEFT, 140);
226 m_listCtrl->InsertColumn(1, "Column 2", wxLIST_FORMAT_LEFT, 140);
227
228 for ( int i=0; i < 30; i++)
229 {
230 char buf[20];
231 sprintf(buf, "Item %d, col 1", i);
232 long tmp = m_listCtrl->InsertItem(i, buf, 0);
233
234 sprintf(buf, "Item %d, col 2", i);
235 tmp = m_listCtrl->SetItem(i, 1, buf);
236 }
237 }
238
239 void MyFrame::OnIconView(wxCommandEvent& WXUNUSED(event))
240 {
241 m_logWindow->Clear();
242 m_listCtrl->DeleteAllItems();
243 m_listCtrl->SetSingleStyle(wxLC_ICON);
244 m_listCtrl->SetImageList(wxGetApp().m_imageListNormal, wxIMAGE_LIST_NORMAL);
245 m_listCtrl->SetImageList(wxGetApp().m_imageListSmall, wxIMAGE_LIST_SMALL);
246
247 for ( int i=0; i < 9; i++)
248 {
249 long tmp = m_listCtrl->InsertItem(i, i);
250 }
251 }
252
253 void MyFrame::OnIconTextView(wxCommandEvent& WXUNUSED(event))
254 {
255 m_logWindow->Clear();
256 m_listCtrl->DeleteAllItems();
257 m_listCtrl->SetSingleStyle(wxLC_ICON);
258 m_listCtrl->SetImageList(wxGetApp().m_imageListNormal, wxIMAGE_LIST_NORMAL);
259 m_listCtrl->SetImageList(wxGetApp().m_imageListSmall, wxIMAGE_LIST_SMALL);
260
261 for ( int i=0; i < 9; i++)
262 {
263 char buf[20];
264 sprintf(buf, "Label %d", i);
265 long tmp = m_listCtrl->InsertItem(i, buf, i);
266 }
267 }
268
269 void MyFrame::OnSmallIconView(wxCommandEvent& WXUNUSED(event))
270 {
271 m_logWindow->Clear();
272 m_listCtrl->DeleteAllItems();
273 m_listCtrl->SetSingleStyle(wxLC_SMALL_ICON);
274 m_listCtrl->SetImageList(wxGetApp().m_imageListNormal, wxIMAGE_LIST_NORMAL);
275 m_listCtrl->SetImageList(wxGetApp().m_imageListSmall, wxIMAGE_LIST_SMALL);
276
277 for ( int i=0; i < 9; i++)
278 {
279 long tmp = m_listCtrl->InsertItem(i, 0);
280 }
281 }
282
283 void MyFrame::OnSmallIconTextView(wxCommandEvent& WXUNUSED(event))
284 {
285 m_logWindow->Clear();
286 m_listCtrl->DeleteAllItems();
287 m_listCtrl->SetSingleStyle(wxLC_SMALL_ICON);
288 m_listCtrl->SetImageList(wxGetApp().m_imageListNormal, wxIMAGE_LIST_NORMAL);
289 m_listCtrl->SetImageList(wxGetApp().m_imageListSmall, wxIMAGE_LIST_SMALL);
290
291 for ( int i=0; i < 9; i++)
292 {
293 long tmp = m_listCtrl->InsertItem(i, "Label", 0);
294 }
295 }
296
297 // MyListCtrl
298
299 void MyListCtrl::OnBeginDrag(wxListEvent& event)
300 {
301 if ( !wxGetApp().GetTopWindow() )
302 return;
303
304 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
305 if ( !text )
306 return;
307
308 #ifndef __GNUWIN32__
309 ostream str(text);
310
311 str << "OnBeginDrag\n";
312 str.flush();
313 #endif
314 }
315
316 void MyListCtrl::OnBeginRDrag(wxListEvent& event)
317 {
318 if ( !wxGetApp().GetTopWindow() )
319 return;
320
321 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
322 if ( !text )
323 return;
324 #ifndef __GNUWIN32__
325 ostream str(text);
326
327 str << "OnBeginRDrag\n";
328 str.flush();
329 #endif
330 }
331
332 void MyListCtrl::OnBeginLabelEdit(wxListEvent& event)
333 {
334 if ( !wxGetApp().GetTopWindow() )
335 return;
336
337 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
338 if ( !text )
339 return;
340
341 #ifndef __GNUWIN32__
342 ostream str(text);
343
344 str << "OnBeginLabelEdit\n";
345 str.flush();
346 #endif
347 }
348
349 void MyListCtrl::OnEndLabelEdit(wxListEvent& event)
350 {
351 if ( !wxGetApp().GetTopWindow() )
352 return;
353
354 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
355 if ( !text )
356 return;
357
358 #ifndef __GNUWIN32__
359 ostream str(text);
360
361 str << "OnEndLabelEdit\n";
362 str.flush();
363 #endif
364 }
365
366 void MyListCtrl::OnDeleteItem(wxListEvent& event)
367 {
368 if ( !wxGetApp().GetTopWindow() )
369 return;
370
371 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
372 if ( !text )
373 return;
374
375 #ifndef __GNUWIN32__
376 ostream str(text);
377
378 str << "OnDeleteItem\n";
379 str.flush();
380 #endif
381 }
382
383 void MyListCtrl::OnGetInfo(wxListEvent& event)
384 {
385 if ( !wxGetApp().GetTopWindow() )
386 return;
387
388 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
389 if ( !text )
390 return;
391
392 #ifndef __GNUWIN32__
393 ostream str(text);
394
395 str << "OnGetInfo (" << event.m_item.m_itemId << ", " << event.m_item.m_col << ")";
396 if ( event.m_item.m_mask & wxLIST_MASK_STATE )
397 str << " wxLIST_MASK_STATE";
398 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
399 str << " wxLIST_MASK_TEXT";
400 if ( event.m_item.m_mask & wxLIST_MASK_IMAGE )
401 str << " wxLIST_MASK_IMAGE";
402 if ( event.m_item.m_mask & wxLIST_MASK_DATA )
403 str << " wxLIST_MASK_DATA";
404 if ( event.m_item.m_mask & wxLIST_SET_ITEM )
405 str << " wxLIST_SET_ITEM";
406 if ( event.m_item.m_mask & wxLIST_MASK_WIDTH )
407 str << " wxLIST_MASK_WIDTH";
408 if ( event.m_item.m_mask & wxLIST_MASK_FORMAT )
409 str << " wxLIST_MASK_WIDTH";
410
411 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
412 {
413 event.m_item.m_text = "My callback text";
414 }
415 str << "\n";
416 str.flush();
417 #endif
418 }
419
420 void MyListCtrl::OnSetInfo(wxListEvent& event)
421 {
422 if ( !wxGetApp().GetTopWindow() )
423 return;
424
425 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
426 if ( !text )
427 return;
428
429 #ifndef __GNUWIN32__
430 ostream str(text);
431
432 str << "OnSetInfo\n";
433 str.flush();
434 #endif
435 }
436
437 void MyListCtrl::OnSelected(wxListEvent& event)
438 {
439 if ( !wxGetApp().GetTopWindow() )
440 return;
441
442 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
443 if ( !text )
444 return;
445
446 #ifndef __GNUWIN32__
447 ostream str(text);
448
449 str << "OnSelected\n";
450 str.flush();
451 #endif
452 }
453
454 void MyListCtrl::OnDeselected(wxListEvent& event)
455 {
456 if ( !wxGetApp().GetTopWindow() )
457 return;
458
459 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
460 if ( !text )
461 return;
462
463 #ifndef __GNUWIN32__
464 ostream str(text);
465
466 str << "OnDeselected\n";
467 str.flush();
468 #endif
469 }
470
471 void MyListCtrl::OnKeyDown(wxListEvent& event)
472 {
473 if ( !wxGetApp().GetTopWindow() )
474 return;
475
476 wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
477 if ( !text )
478 return;
479
480 #ifndef __GNUWIN32__
481 ostream str(text);
482
483 str << "OnKeyDown\n";
484 str.flush();
485 #endif
486 }
487