]>
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 // 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 wxScrolledWindow(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 panel
->SetAutoLayout( TRUE
);
188 panel
->SetSizer( mainsizer
);
190 // don't allow frame to get smaller than what the sizers tell ye
191 mainsizer
->SetSizeHints( this );
197 CheckListBoxFrame::~CheckListBoxFrame()
201 void CheckListBoxFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
206 void CheckListBoxFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
208 wxMessageBox(_T("Demo of wxCheckListBox control\n"
209 "© Vadim Zeitlin 1998-1999"),
210 _T("About wxCheckListBox"),
211 wxICON_INFORMATION
, this);
214 void CheckListBoxFrame::OnListboxSelect(wxCommandEvent
& event
)
216 int nSel
= event
.GetSelection();
217 wxLogStatus(this, _T("item %d selected (%schecked)"), nSel
,
218 m_pListBox
->IsChecked(nSel
) ? _T("") : _T("not "));
221 void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent
& WXUNUSED(event
))
223 wxString strSelection
;
224 strSelection
.sprintf(_T("item %d double clicked"), m_pListBox
->GetSelection());
225 wxMessageDialog
dialog(this, strSelection
);
229 void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent
& event
)
231 unsigned int nItem
= event
.GetInt();
233 wxLogStatus(this, _T("item %d was %schecked"), nItem
,
234 m_pListBox
->IsChecked(nItem
) ? _T("") : _T("un"));
237 void CheckListBoxFrame::OnButtonUp(wxCommandEvent
& WXUNUSED(event
))
242 void CheckListBoxFrame::OnButtonDown(wxCommandEvent
& WXUNUSED(event
))
247 void CheckListBoxFrame::OnButtonMove(bool up
)
249 int selection
= m_pListBox
->GetSelection();
250 if ( selection
!= -1 )
252 wxString label
= m_pListBox
->GetString(selection
);
254 int positionNew
= up
? selection
- 1 : selection
+ 2;
255 if ( positionNew
< 0 || positionNew
> m_pListBox
->Number() )
257 wxLogStatus(this, _T("Can't move this item %s"), up
? _T("up") : _T("down"));
261 bool wasChecked
= m_pListBox
->IsChecked(selection
);
263 int positionOld
= up
? selection
+ 1 : selection
;
266 m_pListBox
->InsertItems(1, &label
, positionNew
);
268 // and delete the old one
269 m_pListBox
->Delete(positionOld
);
271 int selectionNew
= up
? positionNew
: positionNew
- 1;
272 m_pListBox
->Check(selectionNew
, wasChecked
);
273 m_pListBox
->SetSelection(selectionNew
);
275 wxLogStatus(this, _T("Item moved %s"), up
? _T("up") : _T("down"));
280 wxLogStatus(this, _T("Please select an item"));