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