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