]>
git.saurik.com Git - wxWidgets.git/blob - samples/checklst/checklst.cpp
bac838adabee9568d5997dba64e3a8d8e29937b2
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 char *title
,
50 int x
, int y
, int w
, int h
);
54 void OnQuit (wxCommandEvent
& event
);
55 void OnAbout (wxCommandEvent
& event
);
56 void OnListboxSelect (wxCommandEvent
& event
);
57 void OnCheckboxToggle (wxCommandEvent
& event
);
58 void OnListboxDblClick(wxCommandEvent
& event
);
59 void OnButtonUp (wxCommandEvent
& event
);
60 void OnButtonDown (wxCommandEvent
& event
);
63 void OnButtonMove(bool up
);
65 wxCheckListBox
*m_pListBox
;
79 BEGIN_EVENT_TABLE(CheckListBoxFrame
, wxFrame
)
80 EVT_MENU(Menu_Quit
, CheckListBoxFrame::OnQuit
)
82 EVT_LISTBOX(Control_Listbox
, CheckListBoxFrame::OnListboxSelect
)
83 EVT_CHECKLISTBOX(Control_Listbox
, CheckListBoxFrame::OnCheckboxToggle
)
84 EVT_LISTBOX_DCLICK(Control_Listbox
, CheckListBoxFrame::OnListboxDblClick
)
86 EVT_BUTTON(Btn_Up
, CheckListBoxFrame::OnButtonUp
)
87 EVT_BUTTON(Btn_Down
, CheckListBoxFrame::OnButtonDown
)
90 IMPLEMENT_APP(CheckListBoxApp
);
92 // init our app: create windows
93 bool CheckListBoxApp::OnInit(void)
95 CheckListBoxFrame
*pFrame
= new CheckListBoxFrame
98 "wxWindows Checklistbox Sample",
101 SetTopWindow(pFrame
);
106 // main frame constructor
107 CheckListBoxFrame::CheckListBoxFrame(wxFrame
*frame
,
109 int x
, int y
, int w
, int h
)
110 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
112 // create the status line
113 const int widths
[] = { -1, 60 };
115 SetStatusWidths(2, widths
);
116 wxLogStatus(this, _T("no selection"));
119 wxMenu
*file_menu
= new wxMenu
;
122 file_menu
->Append(Menu_Quit
, "E&xit");
124 wxMenuBar
*menu_bar
= new wxMenuBar
;
125 menu_bar
->Append(file_menu
, "&File");
126 SetMenuBar(menu_bar
);
128 // make a panel with some controls
129 wxPanel
*panel
= new wxPanel(this, -1, wxPoint(0, 0),
130 wxSize(400, 200), wxTAB_TRAVERSAL
);
133 static const char* aszChoices
[] =
136 "First", "Second", "Third",
137 "Fourth", "Fifth", "Sixth",
138 "Seventh", "Eighth", "Nineth"
141 wxString
*astrChoices
= new wxString
[WXSIZEOF(aszChoices
)];
143 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
++ )
144 astrChoices
[ui
] = aszChoices
[ui
];
146 m_pListBox
= new wxCheckListBox
149 Control_Listbox
, // control id
150 wxPoint(10, 10), // listbox poistion
151 wxSize(400, 100), // listbox size
152 WXSIZEOF(aszChoices
), // number of strings
153 astrChoices
// array of strings
156 //m_pListBox->SetBackgroundColour(*wxGREEN);
158 delete [] astrChoices
;
160 // not implemented in other ports yet
162 // set grey background for every second entry
163 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
+= 2 ) {
164 m_pListBox
->GetItem(ui
)->SetBackgroundColour(wxColor(200, 200, 200));
168 m_pListBox
->Check(2);
170 // create buttons for moving the items around
171 wxButton
*button1
= new wxButton(panel
, Btn_Up
, " &Up ", wxPoint(420, 90));
172 wxButton
*button2
= new wxButton(panel
, Btn_Down
, "&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 SetAutoLayout( TRUE
);
189 // set frame to minimum size
190 mainsizer
->Fit( this );
192 // don't allow frame to get smaller than what the sizers tell ye
193 mainsizer
->SetSizeHints( this );
195 SetSizer( mainsizer
);
201 CheckListBoxFrame::~CheckListBoxFrame()
205 void CheckListBoxFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
210 void CheckListBoxFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
212 wxMessageBox(_T("Demo of wxCheckListBox control\n"
213 "© Vadim Zeitlin 1998-1999"),
214 _T("About wxCheckListBox"),
215 wxICON_INFORMATION
, this);
218 void CheckListBoxFrame::OnListboxSelect(wxCommandEvent
& event
)
220 int nSel
= event
.GetSelection();
221 wxLogStatus(this, _T("item %d selected (%schecked)"), nSel
,
222 m_pListBox
->IsChecked(nSel
) ? _T("") : _T("not "));
225 void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent
& WXUNUSED(event
))
227 wxString strSelection
;
228 strSelection
.sprintf(_T("item %d double clicked"), m_pListBox
->GetSelection());
229 wxMessageDialog
dialog(this, strSelection
);
233 void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent
& event
)
235 unsigned int nItem
= event
.GetInt();
237 wxLogStatus(this, _T("item %d was %schecked"), nItem
,
238 m_pListBox
->IsChecked(nItem
) ? _T("") : _T("un"));
241 void CheckListBoxFrame::OnButtonUp(wxCommandEvent
& WXUNUSED(event
))
246 void CheckListBoxFrame::OnButtonDown(wxCommandEvent
& WXUNUSED(event
))
251 void CheckListBoxFrame::OnButtonMove(bool up
)
253 int selection
= m_pListBox
->GetSelection();
254 if ( selection
!= -1 )
256 wxString label
= m_pListBox
->GetString(selection
);
258 int positionNew
= up
? selection
- 1 : selection
+ 2;
259 if ( positionNew
< 0 || positionNew
> m_pListBox
->Number() )
261 wxLogStatus(this, _T("Can't move this item %s"), up
? _T("up") : _T("down"));
265 bool wasChecked
= m_pListBox
->IsChecked(selection
);
267 int positionOld
= up
? selection
+ 1 : selection
;
270 m_pListBox
->InsertItems(1, &label
, positionNew
);
272 // and delete the old one
273 m_pListBox
->Delete(positionOld
);
275 int selectionNew
= up
? positionNew
: positionNew
- 1;
276 m_pListBox
->Check(selectionNew
, wasChecked
);
277 m_pListBox
->SetSelection(selectionNew
);
279 wxLogStatus(this, _T("Item moved %s"), up
? _T("up") : _T("down"));
284 wxLogStatus(this, _T("Please select an item"));