]>
git.saurik.com Git - wxWidgets.git/blob - samples/checklst/checklst.cpp
e9f4de3dabef45df5368599d9fd900f162763d10
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 virtual ~CheckListBoxFrame(){};
53 void OnQuit(wxCommandEvent
& event
);
54 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 OnAddItems(wxCommandEvent
& event
);
62 void OnListboxSelect(wxCommandEvent
& event
);
63 void OnCheckboxToggle(wxCommandEvent
& event
);
64 void OnListboxDblClick(wxCommandEvent
& event
);
66 void OnButtonUp(wxCommandEvent
& event
);
67 void OnButtonDown(wxCommandEvent
& event
);
70 void CreateCheckListbox(long flags
= 0);
72 void OnButtonMove(bool up
);
74 void AdjustColour(size_t index
);
78 wxCheckListBox
*m_pListBox
;
85 Menu_About
= wxID_ABOUT
,
86 Menu_Quit
= wxID_EXIT
,
88 Menu_CheckFirst
= wxID_HIGHEST
,
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")
129 SetTopWindow(pFrame
);
134 // main frame constructor
135 CheckListBoxFrame::CheckListBoxFrame(wxFrame
*frame
,
137 : wxFrame(frame
, wxID_ANY
, title
)
140 // create the status line
141 const int widths
[] = { -1, 60 };
143 SetStatusWidths(2, widths
);
144 #endif // wxUSE_STATUSBAR
150 wxMenu
*menuFile
= new wxMenu
;
151 menuFile
->Append(Menu_About
, _T("&About...\tF1"));
152 menuFile
->AppendSeparator();
153 menuFile
->Append(Menu_Quit
, _T("E&xit\tAlt-X"));
156 wxMenu
*menuList
= new wxMenu
;
157 menuList
->Append(Menu_CheckFirst
, _T("Check the first item\tCtrl-C"));
158 menuList
->Append(Menu_UncheckFirst
, _T("Uncheck the first item\tCtrl-U"));
159 menuList
->Append(Menu_ToggleFirst
, _T("Toggle the first item\tCtrl-T"));
160 menuList
->AppendSeparator();
161 menuList
->Append(Menu_AddItems
, _T("Add more items\tCtrl-A"));
162 menuList
->AppendSeparator();
163 menuList
->AppendCheckItem(Menu_Selection
, _T("Multiple selection\tCtrl-M"));
165 // put it all together
166 wxMenuBar
*menu_bar
= new wxMenuBar
;
167 menu_bar
->Append(menuFile
, _T("&File"));
168 menu_bar
->Append(menuList
, _T("&List"));
169 SetMenuBar(menu_bar
);
171 // make a panel with some controls
172 m_panel
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxTAB_TRAVERSAL
);
174 CreateCheckListbox();
176 // create buttons for moving the items around
177 wxButton
*button1
= new wxButton(m_panel
, Btn_Up
);
178 wxButton
*button2
= new wxButton(m_panel
, Btn_Down
);
181 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
183 mainsizer
->Add( m_pListBox
, 1, wxGROW
|wxALL
, 10 );
185 wxBoxSizer
*bottomsizer
= new wxBoxSizer( wxHORIZONTAL
);
187 bottomsizer
->Add( button1
, 0, wxALL
, 10 );
188 bottomsizer
->Add( button2
, 0, wxALL
, 10 );
190 mainsizer
->Add( bottomsizer
, 0, wxCENTER
);
192 // tell frame to make use of sizer (or constraints, if any)
193 m_panel
->SetAutoLayout( true );
194 m_panel
->SetSizer( mainsizer
);
197 // don't allow frame to get smaller than what the sizers tell ye
198 mainsizer
->SetSizeHints( this );
204 void CheckListBoxFrame::CreateCheckListbox(long flags
)
207 static const wxChar
*aszChoices
[] =
210 _T("First"), _T("Second"), _T("Third"),
211 _T("Fourth"), _T("Fifth"), _T("Sixth"),
212 _T("Seventh"), _T("Eighth"), _T("Nineth")
215 wxString
*astrChoices
= new wxString
[WXSIZEOF(aszChoices
)];
217 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
++ )
218 astrChoices
[ui
] = aszChoices
[ui
];
220 m_pListBox
= new wxCheckListBox
223 Control_Listbox
, // control id
224 wxPoint(10, 10), // listbox poistion
225 wxSize(400, 100), // listbox size
226 WXSIZEOF(aszChoices
), // number of strings
227 astrChoices
, // array of strings
231 //m_pListBox->SetBackgroundColour(*wxGREEN);
233 delete [] astrChoices
;
235 // set grey background for every second entry
236 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
+= 2 ) {
240 m_pListBox
->Check(2);
241 m_pListBox
->Select(3);
244 void CheckListBoxFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
249 void CheckListBoxFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
251 wxMessageBox(wxT("Demo of wxCheckListBox control\n(c) Vadim Zeitlin 1998-2002"),
252 wxT("About wxCheckListBox"),
253 wxICON_INFORMATION
, this);
256 void CheckListBoxFrame::OnCheckFirstItem(wxCommandEvent
& WXUNUSED(event
))
258 if ( !m_pListBox
->IsEmpty() )
259 m_pListBox
->Check(0);
262 void CheckListBoxFrame::OnUncheckFirstItem(wxCommandEvent
& WXUNUSED(event
))
264 if ( !m_pListBox
->IsEmpty() )
265 m_pListBox
->Check(0, false);
268 void CheckListBoxFrame::OnToggleFirstItem(wxCommandEvent
& WXUNUSED(event
))
270 if ( !m_pListBox
->IsEmpty() )
271 m_pListBox
->Check(0, !m_pListBox
->IsChecked(0));
274 void CheckListBoxFrame::OnAddItems(wxCommandEvent
& WXUNUSED(event
))
276 static size_t s_nItem
= 0;
278 items
.Add(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem
));
279 items
.Add(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem
));
280 items
.Add(wxString::Format(_T("New item %lu"), (unsigned long)++s_nItem
));
282 m_pListBox
->InsertItems(items
, 0);//m_pListBox->GetCount());
285 void CheckListBoxFrame::OnToggleSelection(wxCommandEvent
& event
)
287 wxSizer
*sizer
= m_panel
->GetSizer();
289 sizer
->Detach( m_pListBox
);
292 CreateCheckListbox(event
.IsChecked() ? wxLB_EXTENDED
: 0);
294 sizer
->Insert(0, m_pListBox
, 1, wxGROW
| wxALL
, 10);
299 void CheckListBoxFrame::OnListboxSelect(wxCommandEvent
& event
)
301 int nSel
= event
.GetSelection();
302 wxLogStatus(this, wxT("Item %d selected (%schecked)"), nSel
,
303 m_pListBox
->IsChecked(nSel
) ? wxT("") : wxT("not "));
306 void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent
& WXUNUSED(event
))
309 if(m_pListBox
->GetWindowStyle() & wxLB_EXTENDED
)
312 m_pListBox
->GetSelections(list
);
315 selection
= list
.Item(0);
320 selection
= m_pListBox
->GetSelection();
323 wxString strSelection
;
324 if ( selection
!= -1 )
326 strSelection
.Printf(wxT("Item %d double clicked"), selection
);
330 strSelection
= wxT("List double clicked in multiple selection mode");
332 wxMessageDialog
dialog(this, strSelection
, wxT("wxCheckListBox message"), wxICON_INFORMATION
);
336 void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent
& event
)
338 unsigned int nItem
= event
.GetInt();
340 wxLogStatus(this, wxT("item %d was %schecked"), nItem
,
341 m_pListBox
->IsChecked(nItem
) ? wxT("") : wxT("un"));
344 void CheckListBoxFrame::OnButtonUp(wxCommandEvent
& WXUNUSED(event
))
349 void CheckListBoxFrame::OnButtonDown(wxCommandEvent
& WXUNUSED(event
))
354 void CheckListBoxFrame::OnButtonMove(bool up
)
357 if(m_pListBox
->GetWindowStyle() & wxLB_EXTENDED
)
360 m_pListBox
->GetSelections(list
);
363 selection
= list
.Item(0);
368 selection
= m_pListBox
->GetSelection();
370 if ( selection
!= wxNOT_FOUND
)
372 wxString label
= m_pListBox
->GetString(selection
);
374 int positionNew
= up
? selection
- 1 : selection
+ 2;
375 if ( positionNew
< 0 || positionNew
> m_pListBox
->GetCount() )
377 wxLogStatus(this, wxT("Can't move this item %s"), up
? wxT("up") : wxT("down"));
381 bool wasChecked
= m_pListBox
->IsChecked(selection
);
383 int positionOld
= up
? selection
+ 1 : selection
;
386 m_pListBox
->InsertItems(1, &label
, positionNew
);
388 // and delete the old one
389 m_pListBox
->Delete(positionOld
);
391 int selectionNew
= up
? positionNew
: positionNew
- 1;
392 m_pListBox
->Check(selectionNew
, wasChecked
);
393 m_pListBox
->SetSelection(selectionNew
);
394 m_pListBox
->SetFocus();
396 AdjustColour(selection
);
397 AdjustColour(selectionNew
);
399 wxLogStatus(this, wxT("Item moved %s"), up
? wxT("up") : wxT("down"));
404 wxLogStatus(this, wxT("Please select single item"));
408 // not implemented in ports other than (native) MSW yet
409 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) && !defined(__WXWINCE__)
410 void CheckListBoxFrame::AdjustColour(size_t index
)
412 // even items have grey backround, odd ones - white
413 unsigned char c
= index
% 2 ? 255 : 200;
414 m_pListBox
->GetItem(index
)->SetBackgroundColour(wxColor(c
, c
, c
));
417 void CheckListBoxFrame::AdjustColour(size_t WXUNUSED(index
))