]> git.saurik.com Git - wxWidgets.git/blob - samples/checklst/checklst.cpp
bac838adabee9568d5997dba64e3a8d8e29937b2
[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 SetAutoLayout( TRUE );
188
189 // set frame to minimum size
190 mainsizer->Fit( this );
191
192 // don't allow frame to get smaller than what the sizers tell ye
193 mainsizer->SetSizeHints( this );
194
195 SetSizer( mainsizer );
196
197
198 Show(TRUE);
199 }
200
201 CheckListBoxFrame::~CheckListBoxFrame()
202 {
203 }
204
205 void CheckListBoxFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
206 {
207 Close(TRUE);
208 }
209
210 void CheckListBoxFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
211 {
212 wxMessageBox(_T("Demo of wxCheckListBox control\n"
213 "© Vadim Zeitlin 1998-1999"),
214 _T("About wxCheckListBox"),
215 wxICON_INFORMATION, this);
216 }
217
218 void CheckListBoxFrame::OnListboxSelect(wxCommandEvent& event)
219 {
220 int nSel = event.GetSelection();
221 wxLogStatus(this, _T("item %d selected (%schecked)"), nSel,
222 m_pListBox->IsChecked(nSel) ? _T("") : _T("not "));
223 }
224
225 void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent& WXUNUSED(event))
226 {
227 wxString strSelection;
228 strSelection.sprintf(_T("item %d double clicked"), m_pListBox->GetSelection());
229 wxMessageDialog dialog(this, strSelection);
230 dialog.ShowModal();
231 }
232
233 void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent& event)
234 {
235 unsigned int nItem = event.GetInt();
236
237 wxLogStatus(this, _T("item %d was %schecked"), nItem,
238 m_pListBox->IsChecked(nItem) ? _T("") : _T("un"));
239 }
240
241 void CheckListBoxFrame::OnButtonUp(wxCommandEvent& WXUNUSED(event))
242 {
243 OnButtonMove(TRUE);
244 }
245
246 void CheckListBoxFrame::OnButtonDown(wxCommandEvent& WXUNUSED(event))
247 {
248 OnButtonMove(FALSE);
249 }
250
251 void CheckListBoxFrame::OnButtonMove(bool up)
252 {
253 int selection = m_pListBox->GetSelection();
254 if ( selection != -1 )
255 {
256 wxString label = m_pListBox->GetString(selection);
257
258 int positionNew = up ? selection - 1 : selection + 2;
259 if ( positionNew < 0 || positionNew > m_pListBox->Number() )
260 {
261 wxLogStatus(this, _T("Can't move this item %s"), up ? _T("up") : _T("down"));
262 }
263 else
264 {
265 bool wasChecked = m_pListBox->IsChecked(selection);
266
267 int positionOld = up ? selection + 1 : selection;
268
269 // insert the item
270 m_pListBox->InsertItems(1, &label, positionNew);
271
272 // and delete the old one
273 m_pListBox->Delete(positionOld);
274
275 int selectionNew = up ? positionNew : positionNew - 1;
276 m_pListBox->Check(selectionNew, wasChecked);
277 m_pListBox->SetSelection(selectionNew);
278
279 wxLogStatus(this, _T("Item moved %s"), up ? _T("up") : _T("down"));
280 }
281 }
282 else
283 {
284 wxLogStatus(this, _T("Please select an item"));
285 }
286 }