]> git.saurik.com Git - wxWidgets.git/blob - samples/checklst/checklst.cpp
added test for loading image from wxMemoryStream
[wxWidgets.git] / samples / checklst / checklst.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: checklst.cpp
3 // Purpose: wxCheckListBox sample
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 13.11.97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 //#pragma implementation
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 #ifdef __WXMSW__
28 #include "wx/ownerdrw.h"
29 #endif
30
31 #include "wx/log.h"
32
33 #include "wx/sizer.h"
34 #include "wx/menuitem.h"
35 #include "wx/checklst.h"
36
37 // Define a new application type
38 class CheckListBoxApp: public wxApp
39 {
40 public:
41 bool OnInit();
42 };
43
44 // Define a new frame type
45 class CheckListBoxFrame : public wxFrame
46 {
47 public:
48 // ctor & dtor
49 CheckListBoxFrame(wxFrame *frame, const char *title,
50 int x, int y, int w, int h);
51 virtual ~CheckListBoxFrame();
52
53 // notifications
54 void OnQuit (wxCommandEvent& event);
55 void OnAbout (wxCommandEvent& event);
56 void OnToggleSelection(wxCommandEvent& event);
57 void OnListboxSelect (wxCommandEvent& event);
58 void OnCheckboxToggle (wxCommandEvent& event);
59 void OnListboxDblClick(wxCommandEvent& event);
60 void OnButtonUp (wxCommandEvent& event);
61 void OnButtonDown (wxCommandEvent& event);
62
63 private:
64 void CreateCheckListbox(long flags = 0);
65
66 void OnButtonMove(bool up);
67
68 void AdjustColour(size_t index);
69
70 wxPanel *m_panel;
71
72 wxCheckListBox *m_pListBox;
73
74 DECLARE_EVENT_TABLE()
75 };
76
77 enum
78 {
79 Menu_About = 100,
80 Menu_Quit,
81 Menu_Selection,
82
83 Control_First = 1000,
84 Control_Listbox,
85 Btn_Up,
86 Btn_Down
87 };
88
89 BEGIN_EVENT_TABLE(CheckListBoxFrame, wxFrame)
90 EVT_MENU(Menu_About, CheckListBoxFrame::OnAbout)
91 EVT_MENU(Menu_Quit, CheckListBoxFrame::OnQuit)
92
93 EVT_MENU(Menu_Selection, CheckListBoxFrame::OnToggleSelection)
94
95 EVT_LISTBOX(Control_Listbox, CheckListBoxFrame::OnListboxSelect)
96 EVT_CHECKLISTBOX(Control_Listbox, CheckListBoxFrame::OnCheckboxToggle)
97 EVT_LISTBOX_DCLICK(Control_Listbox, CheckListBoxFrame::OnListboxDblClick)
98
99 EVT_BUTTON(Btn_Up, CheckListBoxFrame::OnButtonUp)
100 EVT_BUTTON(Btn_Down, CheckListBoxFrame::OnButtonDown)
101 END_EVENT_TABLE()
102
103 IMPLEMENT_APP(CheckListBoxApp);
104
105 // init our app: create windows
106 bool CheckListBoxApp::OnInit(void)
107 {
108 CheckListBoxFrame *pFrame = new CheckListBoxFrame
109 (
110 NULL,
111 _T("wxWindows Checklistbox Sample"),
112 50, 50, 480, 320
113 );
114 SetTopWindow(pFrame);
115
116 return TRUE;
117 }
118
119 // main frame constructor
120 CheckListBoxFrame::CheckListBoxFrame(wxFrame *frame,
121 const wxChar *title,
122 int x, int y, int w, int h)
123 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
124 {
125 // create the status line
126 const int widths[] = { -1, 60 };
127 CreateStatusBar(2);
128 SetStatusWidths(2, widths);
129 wxLogStatus(this, _T("no selection"));
130
131 // Make a menubar
132 // --------------
133
134 // file submenu
135 wxMenu *menuFile = new wxMenu;
136 menuFile->Append(Menu_About, _T("&About...\tF1"));
137 menuFile->AppendSeparator();
138 menuFile->Append(Menu_Quit, _T("E&xit\tAlt-X"));
139
140 // listbox submenu
141 wxMenu *menuList = new wxMenu;
142 menuList->AppendCheckItem(Menu_Selection, _T("Multiple selection\tCtrl-M"));
143
144 // put it all together
145 wxMenuBar *menu_bar = new wxMenuBar;
146 menu_bar->Append(menuFile, _T("&File"));
147 menu_bar->Append(menuList, _T("&List"));
148 SetMenuBar(menu_bar);
149
150 // make a panel with some controls
151 m_panel = new wxPanel(this, -1, wxPoint(0, 0),
152 wxSize(400, 200), wxTAB_TRAVERSAL);
153
154 CreateCheckListbox();
155
156 // create buttons for moving the items around
157 wxButton *button1 = new wxButton(m_panel, Btn_Up, _T(" &Up "), wxPoint(420, 90));
158 wxButton *button2 = new wxButton(m_panel, Btn_Down, _T("&Down"), wxPoint(420, 120));
159
160
161 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
162
163 mainsizer->Add( m_pListBox, 1, wxGROW|wxALL, 10 );
164
165 wxBoxSizer *bottomsizer = new wxBoxSizer( wxHORIZONTAL );
166
167 bottomsizer->Add( button1, 0, wxALL, 10 );
168 bottomsizer->Add( button2, 0, wxALL, 10 );
169
170 mainsizer->Add( bottomsizer, 0, wxCENTER );
171
172 // tell frame to make use of sizer (or constraints, if any)
173 m_panel->SetAutoLayout( TRUE );
174 m_panel->SetSizer( mainsizer );
175
176 // don't allow frame to get smaller than what the sizers tell ye
177 mainsizer->SetSizeHints( this );
178
179 Show(TRUE);
180 }
181
182 void CheckListBoxFrame::CreateCheckListbox(long flags)
183 {
184 // check list box
185 static const wxChar *aszChoices[] =
186 {
187 _T("Zeroth"),
188 _T("First"), _T("Second"), _T("Third"),
189 _T("Fourth"), _T("Fifth"), _T("Sixth"),
190 _T("Seventh"), _T("Eighth"), _T("Nineth")
191 };
192
193 wxString *astrChoices = new wxString[WXSIZEOF(aszChoices)];
194 unsigned int ui;
195 for ( ui = 0; ui < WXSIZEOF(aszChoices); ui++ )
196 astrChoices[ui] = aszChoices[ui];
197
198 m_pListBox = new wxCheckListBox
199 (
200 m_panel, // parent
201 Control_Listbox, // control id
202 wxPoint(10, 10), // listbox poistion
203 wxSize(400, 100), // listbox size
204 WXSIZEOF(aszChoices), // number of strings
205 astrChoices, // array of strings
206 flags
207 );
208
209 //m_pListBox->SetBackgroundColour(*wxGREEN);
210
211 delete [] astrChoices;
212
213 // set grey background for every second entry
214 for ( ui = 0; ui < WXSIZEOF(aszChoices); ui += 2 ) {
215 AdjustColour(ui);
216 }
217
218 m_pListBox->Check(2);
219 m_pListBox->Select(3);
220 }
221
222 CheckListBoxFrame::~CheckListBoxFrame()
223 {
224 }
225
226 void CheckListBoxFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
227 {
228 Close(TRUE);
229 }
230
231 void CheckListBoxFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
232 {
233 wxMessageBox(wxT("Demo of wxCheckListBox control\n© Vadim Zeitlin 1998-2002"),
234 wxT("About wxCheckListBox"),
235 wxICON_INFORMATION, this);
236 }
237
238 void CheckListBoxFrame::OnToggleSelection(wxCommandEvent& event)
239 {
240 wxSizer *sizer = m_panel->GetSizer();
241
242 sizer->Remove(m_pListBox);
243 delete m_pListBox;
244
245 CreateCheckListbox(event.IsChecked() ? wxLB_EXTENDED : 0);
246
247 sizer->Insert(0, m_pListBox, 1, wxGROW | wxALL, 10);
248
249 m_panel->Layout();
250 }
251
252 void CheckListBoxFrame::OnListboxSelect(wxCommandEvent& event)
253 {
254 int nSel = event.GetSelection();
255 wxLogStatus(this, wxT("Item %d selected (%schecked)"), nSel,
256 m_pListBox->IsChecked(nSel) ? _T("") : wxT("not "));
257 }
258
259 void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent& WXUNUSED(event))
260 {
261 wxString strSelection;
262 strSelection.sprintf(wxT("Item %d double clicked"), m_pListBox->GetSelection());
263 wxMessageDialog dialog(this, strSelection, wxT("wxCheckListBox message"), wxICON_INFORMATION);
264 dialog.ShowModal();
265 }
266
267 void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent& event)
268 {
269 unsigned int nItem = event.GetInt();
270
271 wxLogStatus(this, wxT("item %d was %schecked"), nItem,
272 m_pListBox->IsChecked(nItem) ? wxT("") : wxT("un"));
273 }
274
275 void CheckListBoxFrame::OnButtonUp(wxCommandEvent& WXUNUSED(event))
276 {
277 OnButtonMove(TRUE);
278 }
279
280 void CheckListBoxFrame::OnButtonDown(wxCommandEvent& WXUNUSED(event))
281 {
282 OnButtonMove(FALSE);
283 }
284
285 void CheckListBoxFrame::OnButtonMove(bool up)
286 {
287 int selection = m_pListBox->GetSelection();
288 if ( selection != -1 )
289 {
290 wxString label = m_pListBox->GetString(selection);
291
292 int positionNew = up ? selection - 1 : selection + 2;
293 if ( positionNew < 0 || positionNew > m_pListBox->GetCount() )
294 {
295 wxLogStatus(this, wxT("Can't move this item %s"), up ? wxT("up") : wxT("down"));
296 }
297 else
298 {
299 bool wasChecked = m_pListBox->IsChecked(selection);
300
301 int positionOld = up ? selection + 1 : selection;
302
303 // insert the item
304 m_pListBox->InsertItems(1, &label, positionNew);
305
306 // and delete the old one
307 m_pListBox->Delete(positionOld);
308
309 int selectionNew = up ? positionNew : positionNew - 1;
310 m_pListBox->Check(selectionNew, wasChecked);
311 m_pListBox->SetSelection(selectionNew);
312
313 AdjustColour(selection);
314 AdjustColour(selectionNew);
315
316 wxLogStatus(this, wxT("Item moved %s"), up ? wxT("up") : wxT("down"));
317 }
318 }
319 else
320 {
321 wxLogStatus(this, wxT("Please select an item"));
322 }
323 }
324
325 void CheckListBoxFrame::AdjustColour(size_t index)
326 {
327 // not implemented in other ports yet
328 #ifdef __WXMSW__
329 // even items have grey backround, odd ones - white
330 unsigned char c = index % 2 ? 255 : 200;
331 m_pListBox->GetItem(index)->SetBackgroundColour(wxColor(c, c, c));
332 #endif // wxMSW
333 }