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