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"));
72 #include "mondrian.xpm"
73 frame
->SetIcon(wxIcon(mondrian_xpm
));
76 // Make an image list containing large icons
77 m_imageListNormal
= new wxImageList(32, 32, TRUE
);
78 m_imageListSmall
= new wxImageList(16, 16, TRUE
);
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
) );
91 m_imageListSmall
->Add( wxIcon("iconsmall", wxBITMAP_TYPE_ICO_RESOURCE
) );
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
) );
114 #include "bitmaps/small1.xpm"
115 m_imageListSmall
->Add( wxIcon( small1_xpm
) );
120 wxMenu
*file_menu
= new wxMenu
;
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
);
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
);
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
);
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
);
156 for ( int i
=0; i
< 30; i
++)
159 sprintf(buf
, "Item %d", i
);
160 long tmp
= frame
->m_listCtrl
->InsertItem(i
, buf
);
163 frame
->CreateStatusBar(3);
164 frame
->SetStatusText("", 0);
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
))
182 MyFrame::~MyFrame(void)
184 delete wxGetApp().m_imageListNormal
;
185 delete wxGetApp().m_imageListSmall
;
188 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
193 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
195 wxMessageDialog
dialog(this, "List test sample\nJulian Smart (c) 1997",
196 "About list test", wxOK
|wxCANCEL
);
201 void MyFrame::OnListView(wxCommandEvent
& WXUNUSED(event
))
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
);
209 for ( int i
=0; i
< 30; i
++)
212 sprintf(buf
, "Item %d", i
);
213 long tmp
= m_listCtrl
->InsertItem(i
, buf
);
217 void MyFrame::OnReportView(wxCommandEvent
& WXUNUSED(event
))
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
);
225 m_listCtrl
->InsertColumn(0, "Column 1", wxLIST_FORMAT_LEFT
, 140);
226 m_listCtrl
->InsertColumn(1, "Column 2", wxLIST_FORMAT_LEFT
, 140);
228 for ( int i
=0; i
< 30; i
++)
231 sprintf(buf
, "Item %d, col 1", i
);
232 long tmp
= m_listCtrl
->InsertItem(i
, buf
, 0);
234 sprintf(buf
, "Item %d, col 2", i
);
235 tmp
= m_listCtrl
->SetItem(i
, 1, buf
);
239 void MyFrame::OnIconView(wxCommandEvent
& WXUNUSED(event
))
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
);
247 for ( int i
=0; i
< 9; i
++)
249 long tmp
= m_listCtrl
->InsertItem(i
, i
);
253 void MyFrame::OnIconTextView(wxCommandEvent
& WXUNUSED(event
))
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
);
261 for ( int i
=0; i
< 9; i
++)
264 sprintf(buf
, "Label %d", i
);
265 long tmp
= m_listCtrl
->InsertItem(i
, buf
, i
);
269 void MyFrame::OnSmallIconView(wxCommandEvent
& WXUNUSED(event
))
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
);
277 for ( int i
=0; i
< 9; i
++)
279 long tmp
= m_listCtrl
->InsertItem(i
, 0);
283 void MyFrame::OnSmallIconTextView(wxCommandEvent
& WXUNUSED(event
))
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
);
291 for ( int i
=0; i
< 9; i
++)
293 long tmp
= m_listCtrl
->InsertItem(i
, "Label", 0);
299 void MyListCtrl::OnBeginDrag(wxListEvent
& event
)
301 if ( !wxGetApp().GetTopWindow() )
304 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
311 str
<< "OnBeginDrag\n";
316 void MyListCtrl::OnBeginRDrag(wxListEvent
& event
)
318 if ( !wxGetApp().GetTopWindow() )
321 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
327 str
<< "OnBeginRDrag\n";
332 void MyListCtrl::OnBeginLabelEdit(wxListEvent
& event
)
334 if ( !wxGetApp().GetTopWindow() )
337 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
344 str
<< "OnBeginLabelEdit\n";
349 void MyListCtrl::OnEndLabelEdit(wxListEvent
& event
)
351 if ( !wxGetApp().GetTopWindow() )
354 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
361 str
<< "OnEndLabelEdit\n";
366 void MyListCtrl::OnDeleteItem(wxListEvent
& event
)
368 if ( !wxGetApp().GetTopWindow() )
371 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
378 str
<< "OnDeleteItem\n";
383 void MyListCtrl::OnGetInfo(wxListEvent
& event
)
385 if ( !wxGetApp().GetTopWindow() )
388 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
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";
411 if ( event
.m_item
.m_mask
& wxLIST_MASK_TEXT
)
413 event
.m_item
.m_text
= "My callback text";
420 void MyListCtrl::OnSetInfo(wxListEvent
& event
)
422 if ( !wxGetApp().GetTopWindow() )
425 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
432 str
<< "OnSetInfo\n";
437 void MyListCtrl::OnSelected(wxListEvent
& event
)
439 if ( !wxGetApp().GetTopWindow() )
442 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
449 str
<< "OnSelected\n";
454 void MyListCtrl::OnDeselected(wxListEvent
& event
)
456 if ( !wxGetApp().GetTopWindow() )
459 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
466 str
<< "OnDeselected\n";
471 void MyListCtrl::OnKeyDown(wxListEvent
& event
)
473 if ( !wxGetApp().GetTopWindow() )
476 wxTextCtrl
*text
= ((MyFrame
*)wxGetApp().GetTopWindow())->m_logWindow
;
483 str
<< "OnKeyDown\n";