]>
git.saurik.com Git - wxWidgets.git/blob - samples/checklst/checklst.cpp
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 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
24 #include "wx/ownerdrw.h"
30 #include "wx/menuitem.h"
31 #include "wx/checklst.h"
33 #if !wxUSE_CHECKLISTBOX
34 #error "This sample can't be built without wxUSE_CHECKLISTBOX"
35 #endif // wxUSE_CHECKLISTBOX
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
);
57 void OnCheckFirstItem(wxCommandEvent
& event
);
58 void OnUncheckFirstItem(wxCommandEvent
& event
);
59 void OnToggleFirstItem(wxCommandEvent
& event
);
60 void OnToggleSelection(wxCommandEvent
& event
);
61 void OnAddItems(wxCommandEvent
& event
);
63 void OnListboxSelect(wxCommandEvent
& event
);
64 void OnCheckboxToggle(wxCommandEvent
& event
);
65 void OnListboxDblClick(wxCommandEvent
& event
);
67 void OnButtonUp(wxCommandEvent
& event
);
68 void OnButtonDown(wxCommandEvent
& event
);
71 void CreateCheckListbox(long flags
= 0);
73 void OnButtonMove(bool up
);
75 void AdjustColour(size_t index
);
79 wxCheckListBox
*m_pListBox
;
101 BEGIN_EVENT_TABLE(CheckListBoxFrame
, wxFrame
)
102 EVT_MENU(Menu_About
, CheckListBoxFrame::OnAbout
)
103 EVT_MENU(Menu_Quit
, CheckListBoxFrame::OnQuit
)
105 EVT_MENU(Menu_CheckFirst
, CheckListBoxFrame::OnCheckFirstItem
)
106 EVT_MENU(Menu_UncheckFirst
, CheckListBoxFrame::OnUncheckFirstItem
)
107 EVT_MENU(Menu_ToggleFirst
, CheckListBoxFrame::OnToggleFirstItem
)
108 EVT_MENU(Menu_Selection
, CheckListBoxFrame::OnToggleSelection
)
109 EVT_MENU(Menu_AddItems
, CheckListBoxFrame::OnAddItems
)
111 EVT_LISTBOX(Control_Listbox
, CheckListBoxFrame::OnListboxSelect
)
112 EVT_CHECKLISTBOX(Control_Listbox
, CheckListBoxFrame::OnCheckboxToggle
)
113 EVT_LISTBOX_DCLICK(Control_Listbox
, CheckListBoxFrame::OnListboxDblClick
)
115 EVT_BUTTON(Btn_Up
, CheckListBoxFrame::OnButtonUp
)
116 EVT_BUTTON(Btn_Down
, CheckListBoxFrame::OnButtonDown
)
119 IMPLEMENT_APP(CheckListBoxApp
);
121 // init our app: create windows
122 bool CheckListBoxApp::OnInit(void)
124 CheckListBoxFrame
*pFrame
= new CheckListBoxFrame
127 _T("wxWidgets Checklistbox Sample"),
130 SetTopWindow(pFrame
);
135 // main frame constructor
136 CheckListBoxFrame::CheckListBoxFrame(wxFrame
*frame
,
138 int x
, int y
, int w
, int h
)
139 : wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
142 // create the status line
143 const int widths
[] = { -1, 60 };
145 SetStatusWidths(2, widths
);
146 wxLogStatus(this, _T("no selection"));
147 #endif // wxUSE_STATUSBAR
153 wxMenu
*menuFile
= new wxMenu
;
154 menuFile
->Append(Menu_About
, _T("&About...\tF1"));
155 menuFile
->AppendSeparator();
156 menuFile
->Append(Menu_Quit
, _T("E&xit\tAlt-X"));
159 wxMenu
*menuList
= new wxMenu
;
160 menuList
->Append(Menu_CheckFirst
, _T("Check the first item\tCtrl-C"));
161 menuList
->Append(Menu_UncheckFirst
, _T("Uncheck the first item\tCtrl-U"));
162 menuList
->Append(Menu_ToggleFirst
, _T("Toggle the first item\tCtrl-T"));
163 menuList
->AppendSeparator();
164 menuList
->AppendCheckItem(Menu_AddItems
, _T("Add more items\tCtrl-A"));
165 menuList
->AppendSeparator();
166 menuList
->AppendCheckItem(Menu_Selection
, _T("Multiple selection\tCtrl-M"));
168 // put it all together
169 wxMenuBar
*menu_bar
= new wxMenuBar
;
170 menu_bar
->Append(menuFile
, _T("&File"));
171 menu_bar
->Append(menuList
, _T("&List"));
172 SetMenuBar(menu_bar
);
174 // make a panel with some controls
175 m_panel
= new wxPanel(this, wxID_ANY
, wxPoint(0, 0),
176 wxSize(400, 200), wxTAB_TRAVERSAL
);
178 CreateCheckListbox();
180 // create buttons for moving the items around
181 wxButton
*button1
= new wxButton(m_panel
, Btn_Up
, _T(" &Up "), wxPoint(420, 90));
182 wxButton
*button2
= new wxButton(m_panel
, Btn_Down
, _T("&Down"), wxPoint(420, 120));
185 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
187 mainsizer
->Add( m_pListBox
, 1, wxGROW
|wxALL
, 10 );
189 wxBoxSizer
*bottomsizer
= new wxBoxSizer( wxHORIZONTAL
);
191 bottomsizer
->Add( button1
, 0, wxALL
, 10 );
192 bottomsizer
->Add( button2
, 0, wxALL
, 10 );
194 mainsizer
->Add( bottomsizer
, 0, wxCENTER
);
196 // tell frame to make use of sizer (or constraints, if any)
197 m_panel
->SetAutoLayout( true );
198 m_panel
->SetSizer( mainsizer
);
200 // don't allow frame to get smaller than what the sizers tell ye
201 mainsizer
->SetSizeHints( this );
206 void CheckListBoxFrame::CreateCheckListbox(long flags
)
209 static const wxChar
*aszChoices
[] =
212 _T("First"), _T("Second"), _T("Third"),
213 _T("Fourth"), _T("Fifth"), _T("Sixth"),
214 _T("Seventh"), _T("Eighth"), _T("Nineth")
217 wxString
*astrChoices
= new wxString
[WXSIZEOF(aszChoices
)];
219 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
++ )
220 astrChoices
[ui
] = aszChoices
[ui
];
222 m_pListBox
= new wxCheckListBox
225 Control_Listbox
, // control id
226 wxPoint(10, 10), // listbox poistion
227 wxSize(400, 100), // listbox size
228 WXSIZEOF(aszChoices
), // number of strings
229 astrChoices
, // array of strings
233 //m_pListBox->SetBackgroundColour(*wxGREEN);
235 delete [] astrChoices
;
237 // set grey background for every second entry
238 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
+= 2 ) {
242 m_pListBox
->Check(2);
243 m_pListBox
->Select(3);
246 void CheckListBoxFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
251 void CheckListBoxFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
253 wxMessageBox(wxT("Demo of wxCheckListBox control\n(c) Vadim Zeitlin 1998-2002"),
254 wxT("About wxCheckListBox"),
255 wxICON_INFORMATION
, this);
258 void CheckListBoxFrame::OnCheckFirstItem(wxCommandEvent
& WXUNUSED(event
))
260 if ( !m_pListBox
->IsEmpty() )
261 m_pListBox
->Check(0);
264 void CheckListBoxFrame::OnUncheckFirstItem(wxCommandEvent
& WXUNUSED(event
))
266 if ( !m_pListBox
->IsEmpty() )
267 m_pListBox
->Check(0, false);
270 void CheckListBoxFrame::OnToggleFirstItem(wxCommandEvent
& WXUNUSED(event
))
272 if ( !m_pListBox
->IsEmpty() )
273 m_pListBox
->Check(0, !m_pListBox
->IsChecked(0));
276 void CheckListBoxFrame::OnAddItems(wxCommandEvent
& WXUNUSED(event
))
278 static size_t s_nItem
= 0;
280 items
.Add(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem
));
281 items
.Add(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem
));
282 items
.Add(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem
));
284 m_pListBox
->InsertItems(items
, 0);//m_pListBox->GetCount());
287 void CheckListBoxFrame::OnToggleSelection(wxCommandEvent
& event
)
289 wxSizer
*sizer
= m_panel
->GetSizer();
291 sizer
->Detach( m_pListBox
);
294 CreateCheckListbox(event
.IsChecked() ? wxLB_EXTENDED
: 0);
296 sizer
->Insert(0, m_pListBox
, 1, wxGROW
| wxALL
, 10);
301 void CheckListBoxFrame::OnListboxSelect(wxCommandEvent
& event
)
303 int nSel
= event
.GetSelection();
304 wxLogStatus(this, wxT("Item %d selected (%schecked)"), nSel
,
305 m_pListBox
->IsChecked(nSel
) ? wxT("") : wxT("not "));
308 void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent
& WXUNUSED(event
))
311 if(m_pListBox
->GetWindowStyle() & wxLB_EXTENDED
)
314 m_pListBox
->GetSelections(list
);
317 selection
= list
.Item(0);
322 selection
= m_pListBox
->GetSelection();
325 wxString strSelection
;
326 if ( selection
!= -1 )
328 strSelection
.Printf(wxT("Item %d double clicked"), selection
);
332 strSelection
= wxT("List double clicked in multiple selection mode");
334 wxMessageDialog
dialog(this, strSelection
, wxT("wxCheckListBox message"), wxICON_INFORMATION
);
338 void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent
& event
)
340 unsigned int nItem
= event
.GetInt();
342 wxLogStatus(this, wxT("item %d was %schecked"), nItem
,
343 m_pListBox
->IsChecked(nItem
) ? wxT("") : wxT("un"));
346 void CheckListBoxFrame::OnButtonUp(wxCommandEvent
& WXUNUSED(event
))
351 void CheckListBoxFrame::OnButtonDown(wxCommandEvent
& WXUNUSED(event
))
356 void CheckListBoxFrame::OnButtonMove(bool up
)
359 if(m_pListBox
->GetWindowStyle() & wxLB_EXTENDED
)
362 m_pListBox
->GetSelections(list
);
365 selection
= list
.Item(0);
370 selection
= m_pListBox
->GetSelection();
372 if ( selection
!= -1 )
374 wxString label
= m_pListBox
->GetString(selection
);
376 int positionNew
= up
? selection
- 1 : selection
+ 2;
377 if ( positionNew
< 0 || positionNew
> m_pListBox
->GetCount() )
379 wxLogStatus(this, wxT("Can't move this item %s"), up
? wxT("up") : wxT("down"));
383 bool wasChecked
= m_pListBox
->IsChecked(selection
);
385 int positionOld
= up
? selection
+ 1 : selection
;
388 m_pListBox
->InsertItems(1, &label
, positionNew
);
390 // and delete the old one
391 m_pListBox
->Delete(positionOld
);
393 int selectionNew
= up
? positionNew
: positionNew
- 1;
394 m_pListBox
->Check(selectionNew
, wasChecked
);
395 m_pListBox
->SetSelection(selectionNew
);
397 AdjustColour(selection
);
398 AdjustColour(selectionNew
);
400 wxLogStatus(this, wxT("Item moved %s"), up
? wxT("up") : wxT("down"));
405 wxLogStatus(this, wxT("Please select single item"));
409 // not implemented in ports other than (native) MSW yet
410 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
411 void CheckListBoxFrame::AdjustColour(size_t index
)
413 // even items have grey backround, odd ones - white
414 unsigned char c
= index
% 2 ? 255 : 200;
415 m_pListBox
->GetItem(index
)->SetBackgroundColour(wxColor(c
, c
, c
));
418 void CheckListBoxFrame::AdjustColour(size_t WXUNUSED(index
))