]>
Commit | Line | Data |
---|---|---|
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 |