1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxListCtrl sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
28 #include "wx/listctrl.h"
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
)
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
)
57 // `Main program' equivalent, creating windows and returning main app frame
58 bool MyApp::OnInit(void)
60 // Create the main frame window
61 MyFrame
*frame
= new MyFrame(NULL
, "wxListCtrl Test", 50, 50, 450, 340);
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));
70 frame
->SetIcon(wxIcon("mondrian"));
73 frame
->SetIcon(wxIcon("aiai.xbm"));
76 // Make an image list containing large icons
77 m_imageListNormal
= new wxImageList(32, 32, TRUE
);
78 m_imageListSmall
= new wxImageList(16, 16, TRUE
);
80 wxIcon
*icon
= new wxIcon("icon1", wxBITMAP_TYPE_ICO_RESOURCE
);
81 m_imageListNormal
->Add(*icon
);
83 icon
= new wxIcon("icon2", wxBITMAP_TYPE_ICO_RESOURCE
);
84 m_imageListNormal
->Add(*icon
);
86 icon
= new wxIcon("icon3", wxBITMAP_TYPE_ICO_RESOURCE
);
87 m_imageListNormal
->Add(*icon
);
89 icon
= new wxIcon("icon4", wxBITMAP_TYPE_ICO_RESOURCE
);
90 m_imageListNormal
->Add(*icon
);
92 icon
= new wxIcon("icon5", wxBITMAP_TYPE_ICO_RESOURCE
);
93 m_imageListNormal
->Add(*icon
);
95 icon
= new wxIcon("icon6", wxBITMAP_TYPE_ICO_RESOURCE
);
96 m_imageListNormal
->Add(*icon
);
98 icon
= new wxIcon("icon7", wxBITMAP_TYPE_ICO_RESOURCE
);
99 m_imageListNormal
->Add(*icon
);
101 icon
= new wxIcon("icon8", wxBITMAP_TYPE_ICO_RESOURCE
);
102 m_imageListNormal
->Add(*icon
);
104 icon
= new wxIcon("icon9", wxBITMAP_TYPE_ICO_RESOURCE
);
105 m_imageListNormal
->Add(*icon
);
108 icon
= new wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE
);
109 m_imageListSmall
->Add(*icon
);
113 wxMenu
*file_menu
= new wxMenu
;
115 file_menu
->Append(LIST_LIST_VIEW
, "&List view");
116 file_menu
->Append(LIST_REPORT_VIEW
, "&Report view");
117 file_menu
->Append(LIST_ICON_VIEW
, "&Icon view");
118 file_menu
->Append(LIST_ICON_TEXT_VIEW
, "Icon view with &text");
119 file_menu
->Append(LIST_SMALL_ICON_VIEW
, "&Small icon view");
120 file_menu
->Append(LIST_SMALL_ICON_TEXT_VIEW
, "Small icon &view with text");
121 file_menu
->AppendSeparator();
122 file_menu
->Append(LIST_ABOUT
, "&About");
123 file_menu
->Append(LIST_QUIT
, "E&xit");
124 wxMenuBar
*menu_bar
= new wxMenuBar
;
125 menu_bar
->Append(file_menu
, "&File");
126 frame
->SetMenuBar(menu_bar
);
128 // Make a panel with a message
129 frame
->m_listCtrl
= new MyListCtrl(frame
, LIST_CTRL
, wxPoint(0, 0), wxSize(400, 200),
130 wxLC_LIST
|wxSUNKEN_BORDER
);
131 // wxLC_LIST|wxLC_USER_TEXT|wxSUNKEN_BORDER); // wxLC_USER_TEXT requires app to supply all text on demand
132 frame
->m_logWindow
= new wxTextCtrl(frame
, -1, "", wxPoint(0, 0), wxSize(400, 200), wxTE_MULTILINE
|wxSUNKEN_BORDER
);
134 wxLayoutConstraints
*c
= new wxLayoutConstraints
;
135 c
->top
.SameAs (frame
, wxTop
);
136 c
->left
.SameAs (frame
, wxLeft
);
137 c
->right
.SameAs (frame
, wxRight
);
138 c
->height
.PercentOf (frame
, wxHeight
, 66);
139 frame
->m_listCtrl
->SetConstraints(c
);
141 c
= new wxLayoutConstraints
;
142 c
->top
.Below (frame
->m_listCtrl
);
143 c
->left
.SameAs (frame
, wxLeft
);
144 c
->right
.SameAs (frame
, wxRight
);
145 c
->bottom
.SameAs (frame
, wxBottom
);
146 frame
->m_logWindow
->SetConstraints(c
);
147 frame
->SetAutoLayout(TRUE
);
149 for ( int i
=0; i
< 30; i
++)
152 sprintf(buf
, "Item %d", i
);
153 long tmp
= frame
->m_listCtrl
->InsertItem(i
, buf
);
156 frame
->CreateStatusBar(3);
157 frame
->SetStatusText("", 0);
167 // My frame constructor
168 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
169 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
175 MyFrame::~MyFrame(void)
177 delete wxGetApp().m_imageListNormal
;
178 delete wxGetApp().m_imageListSmall
;
181 void MyFrame::OnQuit(wxCommandEvent
& event
)
186 void MyFrame::OnAbout(wxCommandEvent
& event
)
188 wxMessageDialog
dialog(this, "List test sample\nJulian Smart (c) 1997",
189 "About list test", wxOK
|wxCANCEL
);
194 void MyFrame::OnListView(wxCommandEvent
& event
)
196 m_logWindow
->Clear();
197 m_listCtrl
->DeleteAllItems();
198 m_listCtrl
->SetSingleStyle(wxLC_LIST
);
199 m_listCtrl
->SetImageList(NULL
, wxIMAGE_LIST_NORMAL
);
200 m_listCtrl
->SetImageList(NULL
, wxIMAGE_LIST_SMALL
);
202 for ( int i
=0; i
< 30; i
++)
205 sprintf(buf
, "Item %d", i
);
206 long tmp
= m_listCtrl
->InsertItem(i
, buf
);
210 void MyFrame::OnReportView(wxCommandEvent
& event
)
212 m_logWindow
->Clear();
213 m_listCtrl
->DeleteAllItems();
214 m_listCtrl
->SetSingleStyle(wxLC_REPORT
);
215 m_listCtrl
->SetImageList(NULL
, wxIMAGE_LIST_NORMAL
);
216 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
218 m_listCtrl
->InsertColumn(0, "Column 1", wxLIST_FORMAT_LEFT
, 140);
219 m_listCtrl
->InsertColumn(1, "Column 2", wxLIST_FORMAT_LEFT
, 140);
221 for ( int i
=0; i
< 30; i
++)
224 sprintf(buf
, "Item %d, col 1", i
);
225 long tmp
= m_listCtrl
->InsertItem(i
, buf
, 0);
227 sprintf(buf
, "Item %d, col 2", i
);
228 tmp
= m_listCtrl
->SetItem(i
, 1, buf
);
232 void MyFrame::OnIconView(wxCommandEvent
& event
)
234 m_logWindow
->Clear();
235 m_listCtrl
->DeleteAllItems();
236 m_listCtrl
->SetSingleStyle(wxLC_ICON
);
237 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
238 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
240 for ( int i
=0; i
< 9; i
++)
242 long tmp
= m_listCtrl
->InsertItem(i
, i
);
246 void MyFrame::OnIconTextView(wxCommandEvent
& event
)
248 m_logWindow
->Clear();
249 m_listCtrl
->DeleteAllItems();
250 m_listCtrl
->SetSingleStyle(wxLC_ICON
);
251 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
252 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
254 for ( int i
=0; i
< 9; i
++)
257 sprintf(buf
, "Label %d", i
);
258 long tmp
= m_listCtrl
->InsertItem(i
, buf
, i
);
262 void MyFrame::OnSmallIconView(wxCommandEvent
& event
)
264 m_logWindow
->Clear();
265 m_listCtrl
->DeleteAllItems();
266 m_listCtrl
->SetSingleStyle(wxLC_SMALL_ICON
);
267 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
268 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
270 for ( int i
=0; i
< 9; i
++)
272 long tmp
= m_listCtrl
->InsertItem(i
, 0);
276 void MyFrame::OnSmallIconTextView(wxCommandEvent
& event
)
278 m_logWindow
->Clear();
279 m_listCtrl
->DeleteAllItems();
280 m_listCtrl
->SetSingleStyle(wxLC_SMALL_ICON
);
281 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
282 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
284 for ( int i
=0; i
< 9; i
++)
286 long tmp
= m_listCtrl
->InsertItem(i
, "Label", 0);
292 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
294 if ( !wxGetApp().GetTopWindow() )
297 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
304 str
<< "OnBeginDrag\n";
309 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
311 if ( !wxGetApp().GetTopWindow() )
314 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
320 str
<< "OnBeginRDrag\n";
325 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
327 if ( !wxGetApp().GetTopWindow() )
330 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
337 str
<< "OnBeginLabelEdit\n";
342 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
344 if ( !wxGetApp().GetTopWindow() )
347 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
354 str
<< "OnEndLabelEdit\n";
359 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
361 if ( !wxGetApp().GetTopWindow() )
364 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
371 str
<< "OnDeleteItem\n";
376 void MyListCtrl::OnGetInfo(wxListEvent
& event
)
378 if ( !wxGetApp().GetTopWindow() )
381 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
388 str
<< "OnGetInfo (" << event
.m_item
.m_itemId
<< ", " << event
.m_item
.m_col
<< ")";
389 if ( event
.m_item
.m_mask
& wxLIST_MASK_STATE
)
390 str
<< " wxLIST_MASK_STATE";
391 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
392 str
<< " wxLIST_MASK_TEXT";
393 if ( event
.m_item
.m_mask
& wxLIST_MASK_IMAGE
)
394 str
<< " wxLIST_MASK_IMAGE";
395 if ( event
.m_item
.m_mask
& wxLIST_MASK_DATA
)
396 str
<< " wxLIST_MASK_DATA";
397 if ( event
.m_item
.m_mask
& wxLIST_SET_ITEM
)
398 str
<< " wxLIST_SET_ITEM";
399 if ( event
.m_item
.m_mask
& wxLIST_MASK_WIDTH
)
400 str
<< " wxLIST_MASK_WIDTH";
401 if ( event
.m_item
.m_mask
& wxLIST_MASK_FORMAT
)
402 str
<< " wxLIST_MASK_WIDTH";
404 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
406 event
.m_item
.m_text
= "My callback text";
413 void MyListCtrl::OnSetInfo(wxListEvent
& event
)
415 if ( !wxGetApp().GetTopWindow() )
418 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
425 str
<< "OnSetInfo\n";
430 void MyListCtrl::OnSelected(wxListEvent
& event
)
432 if ( !wxGetApp().GetTopWindow() )
435 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
442 str
<< "OnSelected\n";
447 void MyListCtrl::OnDeselected(wxListEvent
& event
)
449 if ( !wxGetApp().GetTopWindow() )
452 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
459 str
<< "OnDeselected\n";
464 void MyListCtrl::OnKeyDown(wxListEvent
& event
)
466 if ( !wxGetApp().GetTopWindow() )
469 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
476 str
<< "OnKeyDown\n";