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"
29 #include "mondrian.xpm"
32 #include "wx/listctrl.h"
35 BEGIN_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
)
44 EVT_MENU(LIST_DESELECT_ALL
, MyFrame::OnDeselectAll
)
45 EVT_MENU(LIST_SELECT_ALL
, MyFrame::OnSelectAll
)
48 BEGIN_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
)
58 EVT_LIST_KEY_DOWN(LIST_CTRL
, MyListCtrl::OnListKeyDown
)
59 EVT_LIST_ITEM_ACTIVATED(LIST_CTRL
, MyListCtrl::OnActivated
)
64 // `Main program' equivalent, creating windows and returning main app frame
65 bool MyApp::OnInit(void)
67 // Create the main frame window
68 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, "wxListCtrl Test", 50, 50, 450, 340);
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.
73 frame
->SetBackgroundColour( *wxWHITE
);
76 frame
->SetIcon( wxICON(mondrian
) );
78 // Make an image list containing large icons
79 m_imageListNormal
= new wxImageList(32, 32, TRUE
);
80 m_imageListSmall
= new wxImageList(16, 16, TRUE
);
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
) );
93 m_imageListSmall
->Add( wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE
) );
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
) );
116 #include "bitmaps/small1.xpm"
117 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
122 wxMenu
*file_menu
= new wxMenu
;
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");
130 file_menu
->Append(LIST_DESELECT_ALL
, "&Deselect All");
131 file_menu
->Append(LIST_SELECT_ALL
, "S&elect All");
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
);
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
);
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
);
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
);
160 for ( int i
=0; i
< 30; i
++)
163 wxSprintf(buf
, _T("Item %d"), i
);
164 frame
->m_listCtrl
->InsertItem(i
, buf
);
167 frame
->CreateStatusBar(3);
168 frame
->SetStatusText("", 0);
178 // My frame constructor
179 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
):
180 wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
182 m_listCtrl
= (MyListCtrl
*) NULL
;
183 m_logWindow
= (wxTextCtrl
*) NULL
;
186 MyFrame::~MyFrame(void)
188 delete wxGetApp().m_imageListNormal
;
189 delete wxGetApp().m_imageListSmall
;
192 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
197 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
199 wxMessageDialog
dialog(this, "List test sample\nJulian Smart (c) 1997",
200 "About list test", wxOK
|wxCANCEL
);
205 void MyFrame::OnDeselectAll(wxCommandEvent
& WXUNUSED(event
))
207 int n
= m_listCtrl
->GetItemCount();
208 for (int i
= 0; i
< n
; i
++)
209 m_listCtrl
->SetItemState(i
,0,wxLIST_STATE_SELECTED
);
212 void MyFrame::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
214 int n
= m_listCtrl
->GetItemCount();
215 for (int i
= 0; i
< n
; i
++)
216 m_listCtrl
->SetItemState(i
,wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
219 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
221 m_listCtrl
->DeleteAllItems();
222 m_logWindow
->Clear();
223 m_listCtrl
->SetSingleStyle(wxLC_LIST
);
224 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_NORMAL
);
225 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_SMALL
);
227 for ( int i
=0; i
< 30; i
++)
230 wxSprintf(buf
, _T("Item %d"), i
);
231 m_listCtrl
->InsertItem(i
, buf
);
235 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
237 m_listCtrl
->DeleteAllItems();
238 m_listCtrl
->DeleteAllColumns();
239 m_logWindow
->Clear();
241 m_listCtrl
->SetSingleStyle(wxLC_REPORT
);
242 m_listCtrl
->SetImageList((wxImageList
*) NULL
, wxIMAGE_LIST_NORMAL
);
243 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
245 m_listCtrl
->InsertColumn(0, "Column 1"); // , wxLIST_FORMAT_LEFT, 140);
246 m_listCtrl
->InsertColumn(1, "Column 2"); // , wxLIST_FORMAT_LEFT, 140);
247 m_listCtrl
->InsertColumn(2, "One More Column (2)"); // , wxLIST_FORMAT_LEFT, 140);
249 for ( int i
=0; i
< 30; i
++)
252 wxSprintf(buf
, _T("This is item %d"), i
);
253 long tmp
= m_listCtrl
->InsertItem(i
, buf
, 0);
255 wxSprintf(buf
, _T("Col 1, item %d"), i
);
256 tmp
= m_listCtrl
->SetItem(i
, 1, buf
);
258 wxSprintf(buf
, _T("Item %d in column 2"), i
);
259 tmp
= m_listCtrl
->SetItem(i
, 2, buf
);
262 m_listCtrl
->SetColumnWidth( 0, wxLIST_AUTOSIZE
);
263 m_listCtrl
->SetColumnWidth( 1, wxLIST_AUTOSIZE
);
264 m_listCtrl
->SetColumnWidth( 2, wxLIST_AUTOSIZE
);
267 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
269 m_listCtrl
->DeleteAllItems();
270 m_logWindow
->Clear();
271 m_listCtrl
->SetSingleStyle(wxLC_ICON
);
272 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
273 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
275 for ( int i
=0; i
< 9; i
++)
277 m_listCtrl
->InsertItem(i
, i
);
281 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
283 m_listCtrl
->DeleteAllItems();
284 m_logWindow
->Clear();
285 m_listCtrl
->SetSingleStyle(wxLC_ICON
);
286 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
287 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
289 for ( int i
=0; i
< 9; i
++)
292 wxSprintf(buf
, _T("Label %d"), i
);
293 m_listCtrl
->InsertItem(i
, buf
, i
);
297 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
299 m_listCtrl
->DeleteAllItems();
300 m_logWindow
->Clear();
301 m_listCtrl
->SetSingleStyle(wxLC_SMALL_ICON
);
302 m_listCtrl
->SetImageList(wxGetApp().m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
303 m_listCtrl
->SetImageList(wxGetApp().m_imageListSmall
, wxIMAGE_LIST_SMALL
);
305 for ( int i
=0; i
< 9; i
++)
307 m_listCtrl
->InsertItem(i
, 0);
311 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
313 m_listCtrl
->DeleteAllItems();
314 m_logWindow
->Clear();
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
);
319 for ( int i
=0; i
< 9; i
++)
321 m_listCtrl
->InsertItem(i
, "Label", 0);
327 void MyListCtrl::OnBeginDrag(wxListEvent
& WXUNUSED(event
))
329 if ( !wxGetApp().GetTopWindow() )
332 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
336 text
->WriteText("OnBeginDrag\n");
339 void MyListCtrl::OnBeginRDrag(wxListEvent
& WXUNUSED(event
))
341 if ( !wxGetApp().GetTopWindow() )
344 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
347 text
->WriteText("OnBeginRDrag\n");
350 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& WXUNUSED(event
))
352 if ( !wxGetApp().GetTopWindow() )
355 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
359 text
->WriteText("OnBeginLabelEdit\n");
362 void MyListCtrl::OnEndLabelEdit(wxListEvent
& WXUNUSED(event
))
364 if ( !wxGetApp().GetTopWindow() )
367 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
371 text
->WriteText("OnEndLabelEdit\n");
374 void MyListCtrl::OnDeleteItem(wxListEvent
& WXUNUSED(event
))
376 if ( !wxGetApp().GetTopWindow() )
379 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
383 text
->WriteText("OnDeleteItem\n");
386 void MyListCtrl::OnGetInfo(wxListEvent
& /*event*/)
388 if ( !wxGetApp().GetTopWindow() )
391 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
395 text
->WriteText("OnGetInfo\n");
400 str << "OnGetInfo (" << event.m_item.m_itemId << ", " << event.m_item.m_col << ")";
401 if ( event.m_item.m_mask & wxLIST_MASK_STATE )
402 str << " wxLIST_MASK_STATE";
403 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
404 str << " wxLIST_MASK_TEXT";
405 if ( event.m_item.m_mask & wxLIST_MASK_IMAGE )
406 str << " wxLIST_MASK_IMAGE";
407 if ( event.m_item.m_mask & wxLIST_MASK_DATA )
408 str << " wxLIST_MASK_DATA";
409 if ( event.m_item.m_mask & wxLIST_SET_ITEM )
410 str << " wxLIST_SET_ITEM";
411 if ( event.m_item.m_mask & wxLIST_MASK_WIDTH )
412 str << " wxLIST_MASK_WIDTH";
413 if ( event.m_item.m_mask & wxLIST_MASK_FORMAT )
414 str << " wxLIST_MASK_WIDTH";
416 if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
418 event.m_item.m_text = "My callback text";
425 void MyListCtrl::OnSetInfo(wxListEvent
& WXUNUSED(event
))
427 if ( !wxGetApp().GetTopWindow() )
430 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
434 text
->WriteText("OnSetInfo\n");
437 void MyListCtrl::OnSelected(wxListEvent
& WXUNUSED(event
))
439 if ( !wxGetApp().GetTopWindow() )
442 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
446 text
->WriteText("OnSelected\n");
449 void MyListCtrl::OnDeselected(wxListEvent
& WXUNUSED(event
))
451 if ( !wxGetApp().GetTopWindow() )
454 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
458 text
->WriteText("OnDeselected\n");
461 void MyListCtrl::OnActivated(wxListEvent
& WXUNUSED(event
))
463 if ( !wxGetApp().GetTopWindow() )
466 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
470 text
->WriteText("OnActivated\n");
473 void MyListCtrl::OnListKeyDown(wxListEvent
& event
)
475 if ( !wxGetApp().GetTopWindow() )
478 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
482 text
->WriteText("OnListKeyDown\n");