1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxCheckListBox sample
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
13 //#pragma implementation
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
28 #include "wx/ownerdrw.h"
30 #include "wx/menuitem.h"
31 #include "wx/checklst.h"
34 #include "mondrian.xpm"
37 // Define a new application type
38 class CheckListBoxApp
: public wxApp
44 // Define a new frame type
45 class CheckListBoxFrame
: public wxFrame
49 CheckListBoxFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
);
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
; }
63 wxCheckListBox
*m_pListBox
;
70 Control_Listbox
, Control_Listbox2
,
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
)
81 IMPLEMENT_APP(CheckListBoxApp
);
83 // init our app: create windows
84 bool CheckListBoxApp::OnInit(void)
86 CheckListBoxFrame
*pFrame
= new CheckListBoxFrame(NULL
, "wxWindows Ownerdraw Sample",
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
))
98 SetIcon(wxICON(mondrian
));
101 wxMenu
*file_menu
= new wxMenu
;
104 file_menu
->Append(Menu_Quit
, "E&xit");
106 wxMenuBar
*menu_bar
= new wxMenuBar
;
107 menu_bar
->Append(file_menu
, "&File");
108 SetMenuBar(menu_bar
);
110 // make a panel with some controls
111 wxPanel
*pPanel
= new wxPanel(this, -1, wxPoint(0, 0),
112 wxSize(400, 200), wxTAB_TRAVERSAL
);
115 static const char* aszChoices
[] = { "Hello", "world", "and",
116 "goodbye", "cruel", "world",
117 "-------", "owner-drawn", "listbox" };
119 wxString
*astrChoices
= new wxString
[WXSIZEOF(aszChoices
)];
121 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
++ )
122 astrChoices
[ui
] = aszChoices
[ui
];
124 m_pListBox
= new wxCheckListBox
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
134 delete [] astrChoices
;
137 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
+= 2 ) {
138 m_pListBox
->GetItem(ui
)->SetBackgroundColour(wxColor(200, 200, 200));
142 m_pListBox
->Check(2);
144 // create the status line
145 const int widths
[] = { -1, 60 };
147 SetStatusWidths(2, widths
);
148 SetStatusText("no selection", 0);
153 CheckListBoxFrame::~CheckListBoxFrame()
157 void CheckListBoxFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
162 void CheckListBoxFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
164 wxMessageDialog
dialog(this, "Demo of wxCheckListBox control\n"
165 "About wxCheckListBox", wxYES_NO
| wxCANCEL
);
169 void CheckListBoxFrame::OnListboxSelect(wxCommandEvent
& event
)
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
);
178 void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent
& WXUNUSED(event
))
180 wxString strSelection
;
181 strSelection
.sprintf("item %d double clicked", m_pListBox
->GetSelection());
182 wxMessageDialog
dialog(this, strSelection
);
186 void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent
& event
)
188 wxString strSelection
;
189 unsigned int nItem
= event
.GetInt();
191 strSelection
.sprintf("item %d was %schecked", nItem
,
192 m_pListBox
->IsChecked(nItem
) ? "" : "un");
193 SetStatusText(strSelection
);