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