]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/checklst/checklst.cpp
wxUSE_LOG fixes.
[wxWidgets.git] / samples / checklst / checklst.cpp
... / ...
CommitLineData
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
38class CheckListBoxApp: public wxApp
39{
40public:
41 bool OnInit();
42};
43
44// Define a new frame type
45class CheckListBoxFrame : public wxFrame
46{
47public:
48 // ctor & dtor
49 CheckListBoxFrame(wxFrame *frame, const wxChar *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
57 void OnCheckFirstItem(wxCommandEvent& event);
58 void OnUncheckFirstItem(wxCommandEvent& event);
59 void OnToggleFirstItem(wxCommandEvent& event);
60 void OnToggleSelection(wxCommandEvent& event);
61 void OnAddItems(wxCommandEvent& event);
62
63 void OnListboxSelect(wxCommandEvent& event);
64 void OnCheckboxToggle(wxCommandEvent& event);
65 void OnListboxDblClick(wxCommandEvent& event);
66
67 void OnButtonUp(wxCommandEvent& event);
68 void OnButtonDown(wxCommandEvent& event);
69
70private:
71 void CreateCheckListbox(long flags = 0);
72
73 void OnButtonMove(bool up);
74
75 void AdjustColour(size_t index);
76
77 wxPanel *m_panel;
78
79 wxCheckListBox *m_pListBox;
80
81 DECLARE_EVENT_TABLE()
82};
83
84enum
85{
86 Menu_About = 100,
87 Menu_Quit,
88
89 Menu_CheckFirst,
90 Menu_UncheckFirst,
91 Menu_ToggleFirst,
92 Menu_Selection,
93 Menu_AddItems,
94
95 Control_First = 1000,
96 Control_Listbox,
97 Btn_Up,
98 Btn_Down
99};
100
101BEGIN_EVENT_TABLE(CheckListBoxFrame, wxFrame)
102 EVT_MENU(Menu_About, CheckListBoxFrame::OnAbout)
103 EVT_MENU(Menu_Quit, CheckListBoxFrame::OnQuit)
104
105 EVT_MENU(Menu_CheckFirst, CheckListBoxFrame::OnCheckFirstItem)
106 EVT_MENU(Menu_UncheckFirst, CheckListBoxFrame::OnUncheckFirstItem)
107 EVT_MENU(Menu_ToggleFirst, CheckListBoxFrame::OnToggleFirstItem)
108 EVT_MENU(Menu_Selection, CheckListBoxFrame::OnToggleSelection)
109 EVT_MENU(Menu_AddItems, CheckListBoxFrame::OnAddItems)
110
111 EVT_LISTBOX(Control_Listbox, CheckListBoxFrame::OnListboxSelect)
112 EVT_CHECKLISTBOX(Control_Listbox, CheckListBoxFrame::OnCheckboxToggle)
113 EVT_LISTBOX_DCLICK(Control_Listbox, CheckListBoxFrame::OnListboxDblClick)
114
115 EVT_BUTTON(Btn_Up, CheckListBoxFrame::OnButtonUp)
116 EVT_BUTTON(Btn_Down, CheckListBoxFrame::OnButtonDown)
117END_EVENT_TABLE()
118
119IMPLEMENT_APP(CheckListBoxApp);
120
121// init our app: create windows
122bool CheckListBoxApp::OnInit(void)
123{
124 CheckListBoxFrame *pFrame = new CheckListBoxFrame
125 (
126 NULL,
127 _T("wxWidgets Checklistbox Sample"),
128 50, 50, 480, 320
129 );
130 SetTopWindow(pFrame);
131
132 return true;
133}
134
135// main frame constructor
136CheckListBoxFrame::CheckListBoxFrame(wxFrame *frame,
137 const wxChar *title,
138 int x, int y, int w, int h)
139 : wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
140{
141#if wxUSE_STATUSBAR
142 // create the status line
143 const int widths[] = { -1, 60 };
144 CreateStatusBar(2);
145 SetStatusWidths(2, widths);
146 wxLogStatus(this, _T("no selection"));
147#endif // wxUSE_STATUSBAR
148
149 // Make a menubar
150 // --------------
151
152 // file submenu
153 wxMenu *menuFile = new wxMenu;
154 menuFile->Append(Menu_About, _T("&About...\tF1"));
155 menuFile->AppendSeparator();
156 menuFile->Append(Menu_Quit, _T("E&xit\tAlt-X"));
157
158 // listbox submenu
159 wxMenu *menuList = new wxMenu;
160 menuList->Append(Menu_CheckFirst, _T("Check the first item\tCtrl-C"));
161 menuList->Append(Menu_UncheckFirst, _T("Uncheck the first item\tCtrl-U"));
162 menuList->Append(Menu_ToggleFirst, _T("Toggle the first item\tCtrl-T"));
163 menuList->AppendSeparator();
164 menuList->AppendCheckItem(Menu_AddItems, _T("Add more items\tCtrl-A"));
165 menuList->AppendSeparator();
166 menuList->AppendCheckItem(Menu_Selection, _T("Multiple selection\tCtrl-M"));
167
168 // put it all together
169 wxMenuBar *menu_bar = new wxMenuBar;
170 menu_bar->Append(menuFile, _T("&File"));
171 menu_bar->Append(menuList, _T("&List"));
172 SetMenuBar(menu_bar);
173
174 // make a panel with some controls
175 m_panel = new wxPanel(this, wxID_ANY, wxPoint(0, 0),
176 wxSize(400, 200), wxTAB_TRAVERSAL);
177
178 CreateCheckListbox();
179
180 // create buttons for moving the items around
181 wxButton *button1 = new wxButton(m_panel, Btn_Up, _T(" &Up "), wxPoint(420, 90));
182 wxButton *button2 = new wxButton(m_panel, Btn_Down, _T("&Down"), wxPoint(420, 120));
183
184
185 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
186
187 mainsizer->Add( m_pListBox, 1, wxGROW|wxALL, 10 );
188
189 wxBoxSizer *bottomsizer = new wxBoxSizer( wxHORIZONTAL );
190
191 bottomsizer->Add( button1, 0, wxALL, 10 );
192 bottomsizer->Add( button2, 0, wxALL, 10 );
193
194 mainsizer->Add( bottomsizer, 0, wxCENTER );
195
196 // tell frame to make use of sizer (or constraints, if any)
197 m_panel->SetAutoLayout( true );
198 m_panel->SetSizer( mainsizer );
199
200 // don't allow frame to get smaller than what the sizers tell ye
201 mainsizer->SetSizeHints( this );
202
203 Show(true);
204}
205
206void CheckListBoxFrame::CreateCheckListbox(long flags)
207{
208 // check list box
209 static const wxChar *aszChoices[] =
210 {
211 _T("Zeroth"),
212 _T("First"), _T("Second"), _T("Third"),
213 _T("Fourth"), _T("Fifth"), _T("Sixth"),
214 _T("Seventh"), _T("Eighth"), _T("Nineth")
215 };
216
217 wxString *astrChoices = new wxString[WXSIZEOF(aszChoices)];
218 unsigned int ui;
219 for ( ui = 0; ui < WXSIZEOF(aszChoices); ui++ )
220 astrChoices[ui] = aszChoices[ui];
221
222 m_pListBox = new wxCheckListBox
223 (
224 m_panel, // parent
225 Control_Listbox, // control id
226 wxPoint(10, 10), // listbox poistion
227 wxSize(400, 100), // listbox size
228 WXSIZEOF(aszChoices), // number of strings
229 astrChoices, // array of strings
230 flags
231 );
232
233 //m_pListBox->SetBackgroundColour(*wxGREEN);
234
235 delete [] astrChoices;
236
237 // set grey background for every second entry
238 for ( ui = 0; ui < WXSIZEOF(aszChoices); ui += 2 ) {
239 AdjustColour(ui);
240 }
241
242 m_pListBox->Check(2);
243 m_pListBox->Select(3);
244}
245
246CheckListBoxFrame::~CheckListBoxFrame()
247{
248}
249
250void CheckListBoxFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
251{
252 Close(true);
253}
254
255void CheckListBoxFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
256{
257