]>
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 ///////////////////////////////////////////////////////////////////////////////
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 #if !wxUSE_CHECKLISTBOX
38 #error "This sample can't be built without wxUSE_CHECKLISTBOX"
39 #endif // wxUSE_CHECKLISTBOX
41 // Define a new application type
42 class CheckListBoxApp
: public wxApp
48 // Define a new frame type
49 class CheckListBoxFrame
: public wxFrame
53 CheckListBoxFrame(wxFrame
*frame
, const wxChar
*title
,
54 int x
, int y
, int w
, int h
);
55 virtual ~CheckListBoxFrame(){};
58 void OnQuit(wxCommandEvent
& event
);
59 void OnAbout(wxCommandEvent
& event
);
61 void OnCheckFirstItem(wxCommandEvent
& event
);
62 void OnUncheckFirstItem(wxCommandEvent
& event
);
63 void OnToggleFirstItem(wxCommandEvent
& event
);
64 void OnToggleSelection(wxCommandEvent
& event
);
65 void OnAddItems(wxCommandEvent
& event
);
67 void OnListboxSelect(wxCommandEvent
& event
);
68 void OnCheckboxToggle(wxCommandEvent
& event
);
69 void OnListboxDblClick(wxCommandEvent
& event
);
71 void OnButtonUp(wxCommandEvent
& event
);
72 void OnButtonDown(wxCommandEvent
& event
);
75 void CreateCheckListbox(long flags
= 0);
77 void OnButtonMove(bool up
);
79 void AdjustColour(size_t index
);
83 wxCheckListBox
*m_pListBox
;
105 BEGIN_EVENT_TABLE(CheckListBoxFrame
, wxFrame
)
106 EVT_MENU(Menu_About
, CheckListBoxFrame::OnAbout
)
107 EVT_MENU(Menu_Quit
, CheckListBoxFrame::OnQuit
)
109 EVT_MENU(Menu_CheckFirst
, CheckListBoxFrame::OnCheckFirstItem
)
110 EVT_MENU(Menu_UncheckFirst
, CheckListBoxFrame::OnUncheckFirstItem
)
111 EVT_MENU(Menu_ToggleFirst
, CheckListBoxFrame::OnToggleFirstItem
)
112 EVT_MENU(Menu_Selection
, CheckListBoxFrame::OnToggleSelection
)
113 EVT_MENU(Menu_AddItems
, CheckListBoxFrame::OnAddItems
)
115 EVT_LISTBOX(Control_Listbox
, CheckListBoxFrame::OnListboxSelect
)
116 EVT_CHECKLISTBOX(Control_Listbox
, CheckListBoxFrame::OnCheckboxToggle
)
117 EVT_LISTBOX_DCLICK(Control_Listbox
, CheckListBoxFrame::OnListboxDblClick
)
119 EVT_BUTTON(Btn_Up
, CheckListBoxFrame::OnButtonUp
)
120 EVT_BUTTON(Btn_Down
, CheckListBoxFrame::OnButtonDown
)
123 IMPLEMENT_APP(CheckListBoxApp
);
125 // init our app: create windows
126 bool CheckListBoxApp::OnInit(void)
128 CheckListBoxFrame
*pFrame
= new CheckListBoxFrame
131 _T("wxWidgets Checklistbox Sample"),
134 SetTopWindow(pFrame
);
139 // main frame constructor
140 CheckListBoxFrame::CheckListBoxFrame(wxFrame
*frame
,
142 int x
, int y
, int w
, int h
)
143 : wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
146 // create the status line
147 const int widths
[] = { -1, 60 };
149 SetStatusWidths(2, widths
);
150 wxLogStatus(this, _T("no selection"));
151 #endif // wxUSE_STATUSBAR
157 wxMenu
*menuFile
= new wxMenu
;
158 menuFile
->Append(Menu_About
, _T("&About...\tF1"));
159 menuFile
->AppendSeparator();
160 menuFile
->Append(Menu_Quit
, _T("E&xit\tAlt-X"));
163 wxMenu
*menuList
= new wxMenu
;
164 menuList
->Append(Menu_CheckFirst
, _T("Check the first item\tCtrl-C"));
165 menuList
->Append(Menu_UncheckFirst
, _T("Uncheck the first item\tCtrl-U"));
166 menuList
->Append(Menu_ToggleFirst
, _T("Toggle the first item\tCtrl-T"));
167 menuList
->AppendSeparator();
168 menuList
->AppendCheckItem(Menu_AddItems
, _T("Add more items\tCtrl-A"));
169 menuList
->AppendSeparator();
170 menuList
->AppendCheckItem(Menu_Selection
, _T("Multiple selection\tCtrl-M"));
172 // put it all together
173 wxMenuBar
*menu_bar
= new wxMenuBar
;
174 menu_bar
->Append(menuFile
, _T("&File"));
175 menu_bar
->Append(menuList
, _T("&List"));
176 SetMenuBar(menu_bar
);
178 // make a panel with some controls
179 m_panel
= new wxPanel(this, wxID_ANY
, wxPoint(0, 0),
180 wxSize(400, 200), wxTAB_TRAVERSAL
);
182 CreateCheckListbox();
184 // create buttons for moving the items around
185 wxButton
*button1
= new wxButton(m_panel
, Btn_Up
, _T(" &Up "), wxPoint(420, 90));
186 wxButton
*button2
= new wxButton(m_panel
, Btn_Down
, _T("&Down"), wxPoint(420, 120));
189 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
191 mainsizer
->Add( m_pListBox
, 1, wxGROW
|wxALL
, 10 );
193 wxBoxSizer
*bottomsizer
= new wxBoxSizer( wxHORIZONTAL
);
195 bottomsizer
->Add( button1
, 0, wxALL
, 10 );
196 bottomsizer
->Add( button2
, 0, wxALL
, 10 );
198 mainsizer
->Add( bottomsizer
, 0, wxCENTER
);
200 // tell frame to make use of sizer (or constraints, if any)
201 m_panel
->SetAutoLayout( true );
202 m_panel
->SetSizer( mainsizer
);
204 // don't allow frame to get smaller than what the sizers tell ye
205 mainsizer
->SetSizeHints( this );
210 void CheckListBoxFrame::CreateCheckListbox(long flags
)
213 static const wxChar
*aszChoices
[] =
216 _T("First"), _T("Second"), _T("Third"),
217 _T("Fourth"), _T("Fifth"), _T("Sixth"),
218 _T("Seventh"), _T("Eighth"), _T("Nineth")
221 wxString
*astrChoices
= new wxString
[WXSIZEOF(aszChoices
)];
223 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
++ )
224 astrChoices
[ui
] = aszChoices
[ui
];
226 m_pListBox
= new wxCheckListBox
229 Control_Listbox
, // control id
230 wxPoint(10, 10), // listbox poistion
231 wxSize(400, 100), // listbox size
232 WXSIZEOF(aszChoices
), // number of strings
233 astrChoices
, // array of strings
237 //m_pListBox->SetBackgroundColour(*wxGREEN);
239 delete [] astrChoices
;
241 // set grey background for every second entry
242 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
+= 2 ) {
246 m_pListBox
->Check(2);
247 m_pListBox
->Select(3);
250 void CheckListBoxFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
255 void CheckListBoxFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
257 wxMessageBox(wxT("Demo of wxCheckListBox control\n(c) Vadim Zeitlin 1998-2002"),
258 wxT("About wxCheckListBox"),
259 wxICON_INFORMATION
, this);
262 void CheckListBoxFrame::OnCheckFirstItem(wxCommandEvent
& WXUNUSED(event
))
264 if ( !m_pListBox
->IsEmpty() )
265 m_pListBox
->Check(0);
268 void CheckListBoxFrame::OnUncheckFirstItem(wxCommandEvent
& WXUNUSED(event
))
270 if ( !m_pListBox
->IsEmpty() )
271 m_pListBox
->Check(0, false);
274 void CheckListBoxFrame::OnToggleFirstItem(wxCommandEvent
& WXUNUSED(event
))
276 if ( !m_pListBox
->IsEmpty() )
277 m_pListBox
->Check(0, !m_pListBox
->IsChecked(0));
280 void CheckListBoxFrame::OnAddItems(wxCommandEvent
& WXUNUSED(event
))
282 static size_t s_nItem
= 0;
284 items
.Add(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem
));
285 items
.Add(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem
));
286 items
.Add(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem
));
288 m_pListBox
->InsertItems(items
, 0);//m_pListBox->GetCount());
291 void CheckListBoxFrame::OnToggleSelection(wxCommandEvent
& event
)
293 wxSizer
*sizer
= m_panel
->GetSizer();
295 sizer
->Detach( m_pListBox
);
298 CreateCheckListbox(event
.IsChecked() ? wxLB_EXTENDED
: 0);
300 sizer
->Insert(0, m_pListBox
, 1, wxGROW
| wxALL
, 10);
305 void CheckListBoxFrame::OnListboxSelect(wxCommandEvent
& event
)
307 int nSel
= event
.GetSelection();
308 wxLogStatus(this, wxT("Item %d selected (%schecked)"), nSel
,
309 m_pListBox
->IsChecked(nSel
) ? wxT("") : wxT("not "));
312 void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent
& WXUNUSED(event
))
315 if(m_pListBox
->GetWindowStyle() & wxLB_EXTENDED
)
318 m_pListBox
->GetSelections(list
);
321 selection
= list
.Item(0);
326 selection
= m_pListBox
->GetSelection();
329 wxString strSelection
;
330 if ( selection
!= -1 )
332 strSelection
.Printf(wxT("Item %d double clicked"), selection
);
336 strSelection
= wxT("List double clicked in multiple selection mode");
338 wxMessageDialog
dialog(this, strSelection
, wxT("wxCheckListBox message"), wxICON_INFORMATION
);
342 void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent
& event
)
344 unsigned int nItem
= event
.GetInt();
346 wxLogStatus(this, wxT("item %d was %schecked"), nItem
,
347 m_pListBox
->IsChecked(nItem
) ? wxT("") : wxT("un"));
350 void CheckListBoxFrame::OnButtonUp(wxCommandEvent
& WXUNUSED(event
))
355 void CheckListBoxFrame::OnButtonDown(wxCommandEvent
& WXUNUSED(event
))
360 void CheckListBoxFrame::OnButtonMove(bool up
)
363 if(m_pListBox
->GetWindowStyle() & wxLB_EXTENDED
)
366 m_pListBox
->GetSelections(list
);
369 selection
= list
.Item(0);
374 selection
= m_pListBox
->GetSelection();
376 if ( selection
!= -1 )
378 wxString label
= m_pListBox
->GetString(selection
);
380 int positionNew
= up
? selection
- 1 : selection
+ 2;
381 if ( positionNew
< 0 || positionNew
> m_pListBox
->GetCount() )
383 wxLogStatus(this, wxT("Can't move this item %s"), up
? wxT("up") : wxT("down"));
387 bool wasChecked
= m_pListBox
->IsChecked(selection
);
389 int positionOld
= up
? selection
+ 1 : selection
;
392 m_pListBox
->InsertItems(1, &label
, positionNew
);
394 // and delete the old one
395 m_pListBox
->Delete(positionOld
);
397 int selectionNew
= up
? positionNew
: positionNew
- 1;
398 m_pListBox
->Check(selectionNew
, wasChecked
);
399 m_pListBox
->SetSelection(selectionNew
);
401 AdjustColour(selection
);
402 AdjustColour(selectionNew
);
404 wxLogStatus(this, wxT("Item moved %s"), up
? wxT("up") : wxT("down"));
409 wxLogStatus(this, wxT("Please select single item"));
413 // not implemented in ports other than (native) MSW yet
414 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
415 void CheckListBoxFrame::AdjustColour(size_t index
)
417 // even items have grey backround, odd ones - white
418 unsigned char c
= index
% 2 ? 255 : 200;
419 m_pListBox
->GetItem(index
)->SetBackgroundColour(wxColor(c
, c
, c
));
422 void CheckListBoxFrame::AdjustColour(size_t WXUNUSED(index
))