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"
27 #include "wx/ownerdrw.h"
28 #include "wx/menuitem.h"
29 #include "wx/msw/checklst.h"
31 // Define a new application type
32 class CheckListBoxApp
: public wxApp
38 // Define a new frame type
39 class CheckListBoxFrame
: public wxFrame
43 CheckListBoxFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
);
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
; }
57 wxCheckListBox
*m_pListBox
;
64 Control_Listbox
, Control_Listbox2
,
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
)
75 IMPLEMENT_APP(CheckListBoxApp
);
77 // init our app: create windows
78 bool CheckListBoxApp::OnInit(void)
80 CheckListBoxFrame
*pFrame
= new CheckListBoxFrame(NULL
, "wxWindows Ownerdraw Sample",
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
))
92 SetIcon(wxIcon("mondrian"));
95 wxMenu
*file_menu
= new wxMenu
;
98 file_menu
->Append(Menu_Quit
, "E&xit");
100 wxMenuBar
*menu_bar
= new wxMenuBar
;
101 menu_bar
->Append(file_menu
, "&File");
102 SetMenuBar(menu_bar
);
104 // make a panel with some controls
105 wxPanel
*pPanel
= new wxPanel(this, -1, wxPoint(0, 0),
106 wxSize(400, 200), wxTAB_TRAVERSAL
);
109 static const char* aszChoices
[] = { "Hello", "world", "and",
110 "goodbye", "cruel", "world",
111 "-------", "owner-drawn", "listbox" };
113 wxString
*astrChoices
= new wxString
[WXSIZEOF(aszChoices
)];
115 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
++ )
116 astrChoices
[ui
] = aszChoices
[ui
];
118 m_pListBox
= new wxCheckListBox
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
128 delete [] astrChoices
;
130 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
+= 2 ) {
131 m_pListBox
->GetItem(ui
)->SetBackgroundColour(wxColor(200, 200, 200));
134 m_pListBox
->Check(2);
136 // create the status line
137 const int widths
[] = { -1, 60 };
139 SetStatusWidths(2, widths
);
140 SetStatusText("no selection", 0);
145 CheckListBoxFrame::~CheckListBoxFrame()
149 void CheckListBoxFrame::OnQuit(wxCommandEvent
& event
)
154 void CheckListBoxFrame::OnAbout(wxCommandEvent
& event
)
156 wxMessageDialog
dialog(this, "Demo of wxCheckListBox control\n"
157 "About wxCheckListBox", wxYES_NO
| wxCANCEL
);
161 void CheckListBoxFrame::OnListboxSelect(wxCommandEvent
& event
)
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
);
170 void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent
& event
)
172 wxString strSelection
;
173 strSelection
.sprintf("item %d double clicked", m_pListBox
->GetSelection());
174 wxMessageDialog
dialog(this, strSelection
);
178 void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent
& event
)
180 wxString strSelection
;
181 unsigned int nItem
= event
.GetInt();
182 if(event
.GetInt()==-1)
184 strSelection
.sprintf("item %d was %schecked", nItem
,
185 m_pListBox
->IsChecked(nItem
) ? "" : "un");
186 SetStatusText(strSelection
);