]> git.saurik.com Git - wxWidgets.git/blob - samples/checklst/checklst.cpp
8f26703e8a0b01c736d42bec700e20eb9087505e
[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 #include "wx/menuitem.h"
31 #include "wx/checklst.h"
32
33 // Define a new application type
34 class CheckListBoxApp: public wxApp
35 {
36 public:
37 bool OnInit();
38 };
39
40 // Define a new frame type
41 class CheckListBoxFrame : public wxFrame
42 {
43 public:
44 // ctor & dtor
45 CheckListBoxFrame(wxFrame *frame, char *title, int x, int y, int w, int h);
46 ~CheckListBoxFrame();
47
48 // notifications
49 void OnQuit (wxCommandEvent& event);
50 void OnAbout (wxCommandEvent& event);
51 void OnListboxSelect (wxCommandEvent& event);
52 void OnCheckboxToggle (wxCommandEvent& event);
53 void OnListboxDblClick (wxCommandEvent& event);
54 bool OnClose () { return TRUE; }
55
56 DECLARE_EVENT_TABLE()
57
58 private:
59 wxCheckListBox *m_pListBox;
60 };
61
62 enum
63 {
64 Menu_Quit = 1,
65 Control_First = 1000,
66 Control_Listbox, Control_Listbox2,
67 };
68
69 BEGIN_EVENT_TABLE(CheckListBoxFrame, wxFrame)
70 EVT_MENU(Menu_Quit, CheckListBoxFrame::OnQuit)
71 EVT_LISTBOX(Control_Listbox, CheckListBoxFrame::OnListboxSelect)
72 EVT_CHECKLISTBOX(Control_Listbox, CheckListBoxFrame::OnCheckboxToggle)
73 EVT_LISTBOX_DCLICK(Control_Listbox, CheckListBoxFrame::OnListboxDblClick)
74 END_EVENT_TABLE()
75
76 IMPLEMENT_APP(CheckListBoxApp);
77
78 // init our app: create windows
79 bool CheckListBoxApp::OnInit(void)
80 {
81 CheckListBoxFrame *pFrame = new CheckListBoxFrame(NULL, "wxWindows Ownerdraw Sample",
82 50, 50, 450, 320);
83 SetTopWindow(pFrame);
84
85 return TRUE;
86 }
87
88 // main frame constructor
89 CheckListBoxFrame::CheckListBoxFrame(wxFrame *frame, char *title, int x, int y, int w, int h)
90 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
91 {
92 // create the status line
93 const int widths[] = { -1, 60 };
94 CreateStatusBar(2);
95 SetStatusWidths(2, widths);
96 SetStatusText("no selection", 0);
97
98 // Make a menubar
99 wxMenu *file_menu = new wxMenu;
100
101 // construct submenu
102 file_menu->Append(Menu_Quit, "E&xit");
103
104 wxMenuBar *menu_bar = new wxMenuBar;
105 menu_bar->Append(file_menu, "&File");
106 SetMenuBar(menu_bar);
107
108 // make a panel with some controls
109 wxPanel *pPanel = new wxPanel(this, -1, wxPoint(0, 0),
110 wxSize(400, 200), wxTAB_TRAVERSAL);
111
112 // check list box
113 static const char* aszChoices[] = { "Hello", "world", "and",
114 "goodbye", "cruel", "world",
115 "-------", "owner-drawn", "listbox" };
116
117 wxString *astrChoices = new wxString[WXSIZEOF(aszChoices)];
118 unsigned int ui;
119 for ( ui = 0; ui < WXSIZEOF(aszChoices); ui++ )
120 astrChoices[ui] = aszChoices[ui];
121
122 m_pListBox = new wxCheckListBox
123 (
124 pPanel, // parent
125 Control_Listbox, // control id
126 wxPoint(10, 10), // listbox poistion
127 wxSize(400, 200), // listbox size
128 WXSIZEOF(aszChoices), // number of strings
129 astrChoices // array of strings
130 );
131
132 delete [] astrChoices;
133
134 // not implemented in wxGTK yet
135 #ifndef __WXGTK__
136 for ( ui = 0; ui < WXSIZEOF(aszChoices); ui += 2 ) {
137 m_pListBox->GetItem(ui)->SetBackgroundColour(wxColor(200, 200, 200));
138 }
139 #endif // wxGTK
140
141 m_pListBox->Check(2);
142
143 Show(TRUE);
144 }
145
146 CheckListBoxFrame::~CheckListBoxFrame()
147 {
148 }
149
150 void CheckListBoxFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
151 {
152 Close(TRUE);
153 }
154
155 void CheckListBoxFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
156 {
157 wxMessageDialog dialog(this, "Demo of wxCheckListBox control\n"
158 "About wxCheckListBox", wxYES_NO | wxCANCEL);
159 dialog.ShowModal();
160 }
161
162 void CheckListBoxFrame::OnListboxSelect(wxCommandEvent& event)
163 {
164 wxString strSelection;
165 unsigned int nSel = event.GetSelection();
166 strSelection.sprintf("item %d selected (%schecked)", nSel,
167 m_pListBox->IsChecked((int)nSel) ? "" : "not ");
168 SetStatusText(strSelection);
169 }
170
171 void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent& WXUNUSED(event))
172 {
173 wxString strSelection;
174 strSelection.sprintf("item %d double clicked", m_pListBox->GetSelection());
175 wxMessageDialog dialog(this, strSelection);
176 dialog.ShowModal();
177 }
178
179 void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent& event)
180 {
181 wxString strSelection;
182 unsigned int nItem = event.GetInt();
183
184 strSelection.sprintf("item %d was %schecked", nItem,
185 m_pListBox->IsChecked(nItem) ? "" : "un");
186 SetStatusText(strSelection);
187 }