]>
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 void AdjustColour(size_t index
);
67 wxCheckListBox
*m_pListBox
;
81 BEGIN_EVENT_TABLE(CheckListBoxFrame
, wxFrame
)
82 EVT_MENU(Menu_Quit
, CheckListBoxFrame::OnQuit
)
84 EVT_LISTBOX(Control_Listbox
, CheckListBoxFrame::OnListboxSelect
)
85 EVT_CHECKLISTBOX(Control_Listbox
, CheckListBoxFrame::OnCheckboxToggle
)
86 EVT_LISTBOX_DCLICK(Control_Listbox
, CheckListBoxFrame::OnListboxDblClick
)
88 EVT_BUTTON(Btn_Up
, CheckListBoxFrame::OnButtonUp
)
89 EVT_BUTTON(Btn_Down
, CheckListBoxFrame::OnButtonDown
)
92 IMPLEMENT_APP(CheckListBoxApp
);
94 // init our app: create windows
95 bool CheckListBoxApp::OnInit(void)
97 CheckListBoxFrame
*pFrame
= new CheckListBoxFrame
100 "wxWindows Checklistbox Sample",
103 SetTopWindow(pFrame
);
108 // main frame constructor
109 CheckListBoxFrame::CheckListBoxFrame(wxFrame
*frame
,
111 int x
, int y
, int w
, int h
)
112 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
114 // create the status line
115 const int widths
[] = { -1, 60 };
117 SetStatusWidths(2, widths
);
118 wxLogStatus(this, _T("no selection"));
121 wxMenu
*file_menu
= new wxMenu
;
124 file_menu
->Append(Menu_Quit
, "E&xit");
126 wxMenuBar
*menu_bar
= new wxMenuBar
;
127 menu_bar
->Append(file_menu
, "&File");
128 SetMenuBar(menu_bar
);
130 // make a panel with some controls
131 wxPanel
*panel
= new wxPanel(this, -1, wxPoint(0, 0),
132 wxSize(400, 200), wxTAB_TRAVERSAL
);
135 static const char* aszChoices
[] =
138 "First", "Second", "Third",
139 "Fourth", "Fifth", "Sixth",
140 "Seventh", "Eighth", "Nineth"
143 wxString
*astrChoices
= new wxString
[WXSIZEOF(aszChoices
)];
145 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
++ )
146 astrChoices
[ui
] = aszChoices
[ui
];
148 m_pListBox
= new wxCheckListBox
151 Control_Listbox
, // control id
152 wxPoint(10, 10), // listbox poistion
153 wxSize(400, 100), // listbox size
154 WXSIZEOF(aszChoices
), // number of strings
155 astrChoices
// array of strings
158 //m_pListBox->SetBackgroundColour(*wxGREEN);
160 delete [] astrChoices
;
162 // set grey background for every second entry
163 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
+= 2 ) {
167 m_pListBox
->Check(2);
169 // create buttons for moving the items around
170 wxButton
*button1
= new wxButton(panel
, Btn_Up
, " &Up ", wxPoint(420, 90));
171 wxButton
*button2
= new wxButton(panel
, Btn_Down
, "&Down", wxPoint(420, 120));
174 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
176 mainsizer
->Add( m_pListBox
, 1, wxGROW
|wxALL
, 10 );
178 wxBoxSizer
*bottomsizer
= new wxBoxSizer( wxHORIZONTAL
);
180 bottomsizer
->Add( button1
, 0, wxALL
, 10 );
181 bottomsizer
->Add( button2
, 0, wxALL
, 10 );
183 mainsizer
->Add( bottomsizer
, 0, wxCENTER
);
185 // tell frame to make use of sizer (or constraints, if any)
186 panel
->SetAutoLayout( TRUE
);
187 panel
->SetSizer( mainsizer
);
189 // don't allow frame to get smaller than what the sizers tell ye
190 mainsizer
->SetSizeHints( this );
196 CheckListBoxFrame::~CheckListBoxFrame()
200 void CheckListBoxFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
205 void CheckListBoxFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
207 wxMessageBox(wxT("Demo of wxCheckListBox control\n"
208 "© Vadim Zeitlin 1998-1999"),
209 wxT("About wxCheckListBox"),
210 wxICON_INFORMATION
, this);
213 void CheckListBoxFrame::OnListboxSelect(wxCommandEvent
& event
)
215 int nSel
= event
.GetSelection();
216 wxLogStatus(this, wxT("Item %d selected (%schecked)"), nSel
,
217 m_pListBox
->IsChecked(nSel
) ? _T("") : wxT("not "));
220 void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent
& WXUNUSED(event
))
222 wxString strSelection
;
223 strSelection
.sprintf(wxT("Item %d double clicked"), m_pListBox
->GetSelection());
224 wxMessageDialog
dialog(this, strSelection
, wxT("wxCheckListBox message"), wxICON_INFORMATION
);
228 void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent
& event
)
230 unsigned int nItem
= event
.GetInt();
232 wxLogStatus(this, wxT("item %d was %schecked"), nItem
,
233 m_pListBox
->IsChecked(nItem
) ? wxT("") : wxT("un"));
236 void CheckListBoxFrame::OnButtonUp(wxCommandEvent
& WXUNUSED(event
))
241 void CheckListBoxFrame::OnButtonDown(wxCommandEvent
& WXUNUSED(event
))
246 void CheckListBoxFrame::OnButtonMove(bool up
)
248 int selection
= m_pListBox
->GetSelection();
249 if ( selection
!= -1 )
251 wxString label
= m_pListBox
->GetString(selection
);
253 int positionNew
= up
? selection
- 1 : selection
+ 2;
254 if ( positionNew
< 0 || positionNew
> m_pListBox
->GetCount() )
256 wxLogStatus(this, wxT("Can't move this item %s"), up
? wxT("up") : wxT("down"));
260 bool wasChecked
= m_pListBox
->IsChecked(selection
);
262 int positionOld
= up
? selection
+ 1 : selection
;
265 m_pListBox
->InsertItems(1, &label
, positionNew
);
267 // and delete the old one
268 m_pListBox
->Delete(positionOld
);
270 int selectionNew
= up
? positionNew
: positionNew
- 1;
271 m_pListBox
->Check(selectionNew
, wasChecked
);
272 m_pListBox
->SetSelection(selectionNew
);
274 AdjustColour(selection
);
275 AdjustColour(selectionNew
);
277 wxLogStatus(this, wxT("Item moved %s"), up
? wxT("up") : wxT("down"));
282 wxLogStatus(this, wxT("Please select an item"));
286 void CheckListBoxFrame::AdjustColour(size_t index
)
288 // not implemented in other ports yet
290 // even items have grey backround, odd ones - white
291 unsigned char c
= index
% 2 ? 255 : 200;
292 m_pListBox
->GetItem(index
)->SetBackgroundColour(wxColor(c
, c
, c
));