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