]> git.saurik.com Git - wxWidgets.git/blob - samples/checklst/checklst.cpp
and now it even compiles
[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 ~CheckListBoxFrame();
52
53 // notifications
54 void OnQuit (wxCommandEvent& event);
55 void OnAbout (wxCommandEvent& event);
56 void OnListboxSelect (wxCommandEvent& event);
57 void OnCheckboxToggle (wxCommandEvent& event);
58 void OnListboxDblClick(wxCommandEvent& event);
59 void OnButtonUp (wxCommandEvent& event);
60 void OnButtonDown (wxCommandEvent& event);
61
62 private:
63 void OnButtonMove(bool up);
64
65 wxCheckListBox *m_pListBox;
66
67 DECLARE_EVENT_TABLE()
68 };
69
70 enum
71 {
72 Menu_Quit = 1,
73 Control_First = 1000,
74 Control_Listbox,
75 Btn_Up,
76 Btn_Down
77 };
78
79 BEGIN_EVENT_TABLE(CheckListBoxFrame, wxFrame)
80 EVT_MENU(Menu_Quit, CheckListBoxFrame::OnQuit)
81
82 EVT_LISTBOX(Control_Listbox, CheckListBoxFrame::OnListboxSelect)
83 EVT_CHECKLISTBOX(Control_Listbox, CheckListBoxFrame::OnCheckboxToggle)
84 EVT_LISTBOX_DCLICK(Control_Listbox, CheckListBoxFrame::OnListboxDblClick)
85
86 EVT_BUTTON(Btn_Up, CheckListBoxFrame::OnButtonUp)
87 EVT_BUTTON(Btn_Down, CheckListBoxFrame::OnButtonDown)
88 END_EVENT_TABLE()
89
90 IMPLEMENT_APP(CheckListBoxApp);
91
92 // init our app: create windows
93 bool CheckListBoxApp::OnInit(void)
94 {
95 CheckListBoxFrame *pFrame = new CheckListBoxFrame
96 (
97 NULL,
98 "wxWindows Checklistbox Sample",
99 50, 50, 480, 320
100 );
101 SetTopWindow(pFrame);
102
103 return TRUE;
104 }
105
106 // main frame constructor
107 CheckListBoxFrame::CheckListBoxFrame(wxFrame *frame,
108 const char *title,
109 int x, int y, int w, int h)
110 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
111 {
112 // create the status line
113 const int widths[] = { -1, 60 };
114 CreateStatusBar(2);
115 SetStatusWidths(2, widths);
116 wxLogStatus(this, _T("no selection"));
117
118 // Make a menubar
119 wxMenu *file_menu = new wxMenu;
120
121 // construct submenu
122 file_menu->Append(Menu_Quit, "E&xit");
123
124 wxMenuBar *menu_bar = new wxMenuBar;
125 menu_bar->Append(file_menu, "&File");
126 SetMenuBar(menu_bar);
127
128 // make a panel with some controls
129 wxPanel *panel = new wxPanel(this, -1, wxPoint(0, 0),
130 wxSize(400, 200), wxTAB_TRAVERSAL);
131
132 // check list box
133 static const char* aszChoices[] =
134 {
135 "Zeroth",
136 "First", "Second", "Third",
137 "Fourth", "Fifth", "Sixth",
138 "Seventh", "Eighth", "Nineth"
139 };
140
141 wxString *astrChoices = new wxString[WXSIZEOF(aszChoices)];
142 unsigned int ui;
143 for ( ui = 0; ui < WXSIZEOF(aszChoices); ui++ )
144 astrChoices[ui] = aszChoices[ui];
145
146 m_pListBox = new wxCheckListBox
147 (
148 panel, // parent
149 Control_Listbox, // control id
150 wxPoint(10, 10), // listbox poistion
151 wxSize(400, 100), // listbox size
152 WXSIZEOF(aszChoices), // number of strings
153 astrChoices // array of strings
154 );
155
156 //m_pListBox->SetBackgroundColour(*wxGREEN);
157
158 delete [] astrChoices;
159
160 // not implemented in other ports yet
161 #ifdef __WXMSW__
162 // set grey background for every second entry
163 for ( ui = 0; ui < WXSIZEOF(aszChoices); ui += 2 ) {
164 m_pListBox->GetItem(ui)->SetBackgroundColour(wxColor(200, 200, 200));
165 }
166 #endif // wxGTK
167
168 m_pListBox->Check(2);
169
170 // create buttons for moving the items around
171 wxButton *button1 = new wxButton(panel, Btn_Up, " &Up ", wxPoint(420, 90));
172 wxButton *button2 = new wxButton(panel, Btn_Down, "&Down", wxPoint(420, 120));
173
174
175 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
176
177 mainsizer->Add( m_pListBox, 1, wxGROW|wxALL, 10 );
178
179 wxBoxSizer *bottomsizer = new wxBoxSizer( wxHORIZONTAL );
180
181 bottomsizer->Add( button1, 0, wxALL, 10 );
182 bottomsizer->Add( button2, 0, wxALL, 10 );
183
184 mainsizer->Add( bottomsizer, 0, wxCENTER );
185
186 // tell frame to make use of sizer (or constraints, if any)
187 panel->SetAutoLayout( TRUE );
188 panel->SetSizer( mainsizer );
189
190 // don't allow frame to get smaller than what the sizers tell ye
191 mainsizer->SetSizeHints( this );
192
193
194 Show(TRUE);
195 }
196
197 CheckListBoxFrame::~CheckListBoxFrame()
198 {
199 }
200
201 void CheckListBoxFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
202 {
203 Close(TRUE);
204 }
205
206 void CheckListBoxFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
207 {
208 wxMessageBox(_T("Demo of wxCheckListBox control\n"
209 "© Vadim Zeitlin 1998-1999"),
210 _T("About wxCheckListBox"),
211 wxICON_INFORMATION, this);
212 }
213
214 void CheckListBoxFrame::OnListboxSelect(wxCommandEvent& event)
215 {
216 int nSel = event.GetSelection();
217 wxLogStatus(this, _T("item %d selected (%schecked)"), nSel,
218 m_pListBox->IsChecked(nSel) ? _T("") : _T("not "));
219 }
220
221 void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent& WXUNUSED(event))
222 {
223 wxString strSelection;
224 strSelection.sprintf(_T("item %d double clicked"), m_pListBox->GetSelection());
225 wxMessageDialog dialog(this, strSelection);
226 dialog.ShowModal();
227 }
228
229 void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent& event)
230 {
231 unsigned int nItem = event.GetInt();
232
233 wxLogStatus(this, _T("item %d was %schecked"), nItem,
234 m_pListBox->IsChecked(nItem) ? _T("") : _T("un"));
235 }
236
237 void CheckListBoxFrame::OnButtonUp(wxCommandEvent& WXUNUSED(event))
238 {
239 OnButtonMove(TRUE);
240 }
241
242 void CheckListBoxFrame::OnButtonDown(wxCommandEvent& WXUNUSED(event))
243 {
244 OnButtonMove(FALSE);
245 }
246
247 void CheckListBoxFrame::OnButtonMove(bool up)
248 {
249 int selection = m_pListBox->GetSelection();
250 if ( selection != -1 )
251 {
252 wxString label = m_pListBox->GetString(selection);
253
254 int positionNew = up ? selection - 1 : selection + 2;
255 if ( positionNew < 0 || positionNew > m_pListBox->Number() )
256 {
257 wxLogStatus(this, _T("Can't move this item %s"), up ? _T("up") : _T("down"));
258 }
259 else
260 {
261 bool wasChecked = m_pListBox->IsChecked(selection);
262
263 int positionOld = up ? selection + 1 : selection;
264
265 // insert the item
266 m_pListBox->InsertItems(1, &label, positionNew);
267
268 // and delete the old one
269 m_pListBox->Delete(positionOld);
270
271 int selectionNew = up ? positionNew : positionNew - 1;
272 m_pListBox->Check(selectionNew, wasChecked);
273 m_pListBox->SetSelection(selectionNew);
274
275 wxLogStatus(this, _T("Item moved %s"), up ? _T("up") : _T("down"));
276 }
277 }
278 else
279 {
280 wxLogStatus(this, _T("Please select an item"));
281 }
282 }