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