]>
git.saurik.com Git - wxWidgets.git/blob - samples/checklst/checklst.cpp
9ddcbf7b137279785b75824b66cc1d49cd91da34
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"
34 #include "wx/menuitem.h"
35 #include "wx/checklst.h"
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
, const wxChar
*title
,
50 int x
, int y
, int w
, int h
);
51 virtual ~CheckListBoxFrame();
54 void OnQuit(wxCommandEvent
& event
);
55 void OnAbout(wxCommandEvent
& event
);
56 void OnCheckFirstItem(wxCommandEvent
& event
);
57 void OnUncheckFirstItem(wxCommandEvent
& event
);
58 void OnToggleFirstItem(wxCommandEvent
& event
);
59 void OnToggleSelection(wxCommandEvent
& event
);
60 void OnListboxSelect(wxCommandEvent
& event
);
61 void OnCheckboxToggle(wxCommandEvent
& event
);
62 void OnListboxDblClick(wxCommandEvent
& event
);
63 void OnButtonUp(wxCommandEvent
& event
);
64 void OnButtonDown(wxCommandEvent
& event
);
67 void CreateCheckListbox(long flags
= 0);
69 void OnButtonMove(bool up
);
71 void AdjustColour(size_t index
);
75 wxCheckListBox
*m_pListBox
;
96 BEGIN_EVENT_TABLE(CheckListBoxFrame
, wxFrame
)
97 EVT_MENU(Menu_About
, CheckListBoxFrame::OnAbout
)
98 EVT_MENU(Menu_Quit
, CheckListBoxFrame::OnQuit
)
100 EVT_MENU(Menu_CheckFirst
, CheckListBoxFrame::OnCheckFirstItem
)
101 EVT_MENU(Menu_UncheckFirst
, CheckListBoxFrame::OnUncheckFirstItem
)
102 EVT_MENU(Menu_ToggleFirst
, CheckListBoxFrame::OnToggleFirstItem
)
103 EVT_MENU(Menu_Selection
, CheckListBoxFrame::OnToggleSelection
)
105 EVT_LISTBOX(Control_Listbox
, CheckListBoxFrame::OnListboxSelect
)
106 EVT_CHECKLISTBOX(Control_Listbox
, CheckListBoxFrame::OnCheckboxToggle
)
107 EVT_LISTBOX_DCLICK(Control_Listbox
, CheckListBoxFrame::OnListboxDblClick
)
109 EVT_BUTTON(Btn_Up
, CheckListBoxFrame::OnButtonUp
)
110 EVT_BUTTON(Btn_Down
, CheckListBoxFrame::OnButtonDown
)
113 IMPLEMENT_APP(CheckListBoxApp
);
115 // init our app: create windows
116 bool CheckListBoxApp::OnInit(void)
118 CheckListBoxFrame
*pFrame
= new CheckListBoxFrame
121 _T("wxWindows Checklistbox Sample"),
124 SetTopWindow(pFrame
);
129 // main frame constructor
130 CheckListBoxFrame::CheckListBoxFrame(wxFrame
*frame
,
132 int x
, int y
, int w
, int h
)
133 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
135 // create the status line
136 const int widths
[] = { -1, 60 };
138 SetStatusWidths(2, widths
);
139 wxLogStatus(this, _T("no selection"));
145 wxMenu
*menuFile
= new wxMenu
;
146 menuFile
->Append(Menu_About
, _T("&About...\tF1"));
147 menuFile
->AppendSeparator();
148 menuFile
->Append(Menu_Quit
, _T("E&xit\tAlt-X"));
151 wxMenu
*menuList
= new wxMenu
;
152 menuList
->Append(Menu_CheckFirst
, _T("Check the first item\tCtrl-C"));
153 menuList
->Append(Menu_UncheckFirst
, _T("Uncheck the first item\tCtrl-U"));
154 menuList
->Append(Menu_ToggleFirst
, _T("Toggle the first item\tCtrl-T"));
155 menuList
->AppendSeparator();
156 menuList
->AppendCheckItem(Menu_Selection
, _T("Multiple selection\tCtrl-M"));
158 // put it all together
159 wxMenuBar
*menu_bar
= new wxMenuBar
;
160 menu_bar
->Append(menuFile
, _T("&File"));
161 menu_bar
->Append(menuList
, _T("&List"));
162 SetMenuBar(menu_bar
);
164 // make a panel with some controls
165 m_panel
= new wxPanel(this, -1, wxPoint(0, 0),
166 wxSize(400, 200), wxTAB_TRAVERSAL
);
168 CreateCheckListbox();
170 // create buttons for moving the items around
171 wxButton
*button1
= new wxButton(m_panel
, Btn_Up
, _T(" &Up "), wxPoint(420, 90));
172 wxButton
*button2
= new wxButton(m_panel
, Btn_Down
, _T("&Down"), wxPoint(420, 120));
175 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
177 mainsizer
->Add( m_pListBox
, 1, wxGROW
|wxALL
, 10 );
179 wxBoxSizer
*bottomsizer
= new wxBoxSizer( wxHORIZONTAL
);
181 bottomsizer
->Add( button1
, 0, wxALL
, 10 );
182 bottomsizer
->Add( button2
, 0, wxALL
, 10 );
184 mainsizer
->Add( bottomsizer
, 0, wxCENTER
);
186 // tell frame to make use of sizer (or constraints, if any)
187 m_panel
->SetAutoLayout( TRUE
);
188 m_panel
->SetSizer( mainsizer
);
190 // don't allow frame to get smaller than what the sizers tell ye
191 mainsizer
->SetSizeHints( this );
196 void CheckListBoxFrame::CreateCheckListbox(long flags
)
199 static const wxChar
*aszChoices
[] =
202 _T("First"), _T("Second"), _T("Third"),
203 _T("Fourth"), _T("Fifth"), _T("Sixth"),
204 _T("Seventh"), _T("Eighth"), _T("Nineth")
207 wxString
*astrChoices
= new wxString
[WXSIZEOF(aszChoices
)];
209 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
++ )
210 astrChoices
[ui
] = aszChoices
[ui
];
212 m_pListBox
= new wxCheckListBox
215 Control_Listbox
, // control id
216 wxPoint(10, 10), // listbox poistion
217 wxSize(400, 100), // listbox size
218 WXSIZEOF(aszChoices
), // number of strings
219 astrChoices
, // array of strings
223 //m_pListBox->SetBackgroundColour(*wxGREEN);
225 delete [] astrChoices
;
227 // set grey background for every second entry
228 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
+= 2 ) {
232 m_pListBox
->Check(2);
233 m_pListBox
->Select(3);
236 CheckListBoxFrame::~CheckListBoxFrame()
240 void CheckListBoxFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
245 void CheckListBoxFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
247 wxMessageBox(wxT("Demo of wxCheckListBox control\n© Vadim Zeitlin 1998-2002"),
248 wxT("About wxCheckListBox"),
249 wxICON_INFORMATION
, this);
252 void CheckListBoxFrame::OnCheckFirstItem(wxCommandEvent
& event
)
254 if ( !m_pListBox
->IsEmpty() )
255 m_pListBox
->Check(0);
258 void CheckListBoxFrame::OnUncheckFirstItem(wxCommandEvent
& event
)
260 if ( !m_pListBox
->IsEmpty() )
261 m_pListBox
->Check(0, FALSE
);
264 void CheckListBoxFrame::OnToggleFirstItem(wxCommandEvent
& event
)
266 if ( !m_pListBox
->IsEmpty() )
267 m_pListBox
->Check(0, !m_pListBox
->IsChecked(0));
270 void CheckListBoxFrame::OnToggleSelection(wxCommandEvent
& event
)
272 wxSizer
*sizer
= m_panel
->GetSizer();
274 sizer
->Remove(m_pListBox
);
277 CreateCheckListbox(event
.IsChecked() ? wxLB_EXTENDED
: 0);
279 sizer
->Insert(0, m_pListBox
, 1, wxGROW
| wxALL
, 10);
284 void CheckListBoxFrame::OnListboxSelect(wxCommandEvent
& event
)
286 int nSel
= event
.GetSelection();
287 wxLogStatus(this, wxT("Item %d selected (%schecked)"), nSel
,
288 m_pListBox
->IsChecked(nSel
) ? _T("") : wxT("not "));
291 void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent
& WXUNUSED(event
))
293 wxString strSelection
;
294 strSelection
.sprintf(wxT("Item %d double clicked"), m_pListBox
->GetSelection());
295 wxMessageDialog
dialog(this, strSelection
, wxT("wxCheckListBox message"), wxICON_INFORMATION
);
299 void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent
& event
)
301 unsigned int nItem
= event
.GetInt();
303 wxLogStatus(this, wxT("item %d was %schecked"), nItem
,
304 m_pListBox
->IsChecked(nItem
) ? wxT("") : wxT("un"));
307 void CheckListBoxFrame::OnButtonUp(wxCommandEvent
& WXUNUSED(event
))
312 void CheckListBoxFrame::OnButtonDown(wxCommandEvent
& WXUNUSED(event
))
317 void CheckListBoxFrame::OnButtonMove(bool up
)
319 int selection
= m_pListBox
->GetSelection();
320 if ( selection
!= -1 )
322 wxString label
= m_pListBox
->GetString(selection
);
324 int positionNew
= up
? selection
- 1 : selection
+ 2;
325 if ( positionNew
< 0 || positionNew
> m_pListBox
->GetCount() )
327 wxLogStatus(this, wxT("Can't move this item %s"), up
? wxT("up") : wxT("down"));
331 bool wasChecked
= m_pListBox
->IsChecked(selection
);
333 int positionOld
= up
? selection
+ 1 : selection
;
336 m_pListBox
->InsertItems(1, &label
, positionNew
);
338 // and delete the old one
339 m_pListBox
->Delete(positionOld
);
341 int selectionNew
= up
? positionNew
: positionNew
- 1;
342 m_pListBox
->Check(selectionNew
, wasChecked
);
343 m_pListBox
->SetSelection(selectionNew
);
345 AdjustColour(selection
);
346 AdjustColour(selectionNew
);
348 wxLogStatus(this, wxT("Item moved %s"), up
? wxT("up") : wxT("down"));
353 wxLogStatus(this, wxT("Please select an item"));
357 void CheckListBoxFrame::AdjustColour(size_t index
)
359 // not implemented in ports other than (native) MSW yet
360 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
361 // even items have grey backround, odd ones - white
362 unsigned char c
= index
% 2 ? 255 : 200;
363 m_pListBox
->GetItem(index
)->SetBackgroundColour(wxColor(c
, c
, c
));