]>
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"
33 #include "wx/menuitem.h"
34 #include "wx/checklst.h"
36 // Define a new application type
37 class CheckListBoxApp
: public wxApp
43 // Define a new frame type
44 class CheckListBoxFrame
: public wxFrame
48 CheckListBoxFrame(wxFrame
*frame
, const char *title
,
49 int x
, int y
, int w
, int h
);
53 void OnQuit (wxCommandEvent
& event
);
54 void OnAbout (wxCommandEvent
& event
);
55 void OnListboxSelect (wxCommandEvent
& event
);
56 void OnCheckboxToggle (wxCommandEvent
& event
);
57 void OnListboxDblClick(wxCommandEvent
& event
);
58 void OnButtonUp (wxCommandEvent
& event
);
59 void OnButtonDown (wxCommandEvent
& event
);
62 void OnButtonMove(bool up
);
64 wxCheckListBox
*m_pListBox
;
78 BEGIN_EVENT_TABLE(CheckListBoxFrame
, wxFrame
)
79 EVT_MENU(Menu_Quit
, CheckListBoxFrame::OnQuit
)
81 EVT_LISTBOX(Control_Listbox
, CheckListBoxFrame::OnListboxSelect
)
82 EVT_CHECKLISTBOX(Control_Listbox
, CheckListBoxFrame::OnCheckboxToggle
)
83 EVT_LISTBOX_DCLICK(Control_Listbox
, CheckListBoxFrame::OnListboxDblClick
)
85 EVT_BUTTON(Btn_Up
, CheckListBoxFrame::OnButtonUp
)
86 EVT_BUTTON(Btn_Down
, CheckListBoxFrame::OnButtonDown
)
89 IMPLEMENT_APP(CheckListBoxApp
);
91 // init our app: create windows
92 bool CheckListBoxApp::OnInit(void)
94 CheckListBoxFrame
*pFrame
= new CheckListBoxFrame
97 "wxWindows Checklistbox Sample",
100 SetTopWindow(pFrame
);
105 // main frame constructor
106 CheckListBoxFrame::CheckListBoxFrame(wxFrame
*frame
,
108 int x
, int y
, int w
, int h
)
109 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
111 // create the status line
112 const int widths
[] = { -1, 60 };
114 SetStatusWidths(2, widths
);
115 wxLogStatus(this, "no selection");
118 wxMenu
*file_menu
= new wxMenu
;
121 file_menu
->Append(Menu_Quit
, "E&xit");
123 wxMenuBar
*menu_bar
= new wxMenuBar
;
124 menu_bar
->Append(file_menu
, "&File");
125 SetMenuBar(menu_bar
);
127 // make a panel with some controls
128 wxPanel
*panel
= new wxPanel(this, -1, wxPoint(0, 0),
129 wxSize(400, 200), wxTAB_TRAVERSAL
);
132 static const char* aszChoices
[] =
135 "First", "Second", "Third",
136 "Fourth", "Fifth", "Sixth",
137 "Seventh", "Eighth", "Nineth"
140 wxString
*astrChoices
= new wxString
[WXSIZEOF(aszChoices
)];
142 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
++ )
143 astrChoices
[ui
] = aszChoices
[ui
];
145 m_pListBox
= new wxCheckListBox
148 Control_Listbox
, // control id
149 wxPoint(10, 10), // listbox poistion
150 wxSize(400, 100), // listbox size
151 WXSIZEOF(aszChoices
), // number of strings
152 astrChoices
// array of strings
155 //m_pListBox->SetBackgroundColour(*wxGREEN);
157 delete [] astrChoices
;
159 // not implemented in other ports yet
161 // set grey background for every second entry
162 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
+= 2 ) {
163 m_pListBox
->GetItem(ui
)->SetBackgroundColour(wxColor(200, 200, 200));
167 m_pListBox
->Check(2);
169 // create buttons for moving the items around
170 (void)new wxButton(panel
, Btn_Up
, " &Up ", wxPoint(420, 90));
171 (void)new wxButton(panel
, Btn_Down
, "&Down", wxPoint(420, 120));
176 CheckListBoxFrame::~CheckListBoxFrame()
180 void CheckListBoxFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
185 void CheckListBoxFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
187 wxMessageBox("Demo of wxCheckListBox control\n"
188 "© Vadim Zeitlin 1998-1999",
189 "About wxCheckListBox",
190 wxICON_INFORMATION
, this);
193 void CheckListBoxFrame::OnListboxSelect(wxCommandEvent
& event
)
195 int nSel
= event
.GetSelection();
196 wxLogStatus(this, "item %d selected (%schecked)", nSel
,
197 m_pListBox
->IsChecked(nSel
) ? "" : "not ");
200 void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent
& WXUNUSED(event
))
202 wxString strSelection
;
203 strSelection
.sprintf("item %d double clicked", m_pListBox
->GetSelection());
204 wxMessageDialog
dialog(this, strSelection
);
208 void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent
& event
)
210 unsigned int nItem
= event
.GetInt();
212 wxLogStatus(this, "item %d was %schecked", nItem
,
213 m_pListBox
->IsChecked(nItem
) ? "" : "un");
216 void CheckListBoxFrame::OnButtonUp(wxCommandEvent
& WXUNUSED(event
))
221 void CheckListBoxFrame::OnButtonDown(wxCommandEvent
& WXUNUSED(event
))
226 void CheckListBoxFrame::OnButtonMove(bool up
)
228 int selection
= m_pListBox
->GetSelection();
229 if ( selection
!= -1 )
231 wxString label
= m_pListBox
->GetString(selection
);
233 int positionNew
= up
? selection
- 1 : selection
+ 2;
234 if ( positionNew
< 0 || positionNew
> m_pListBox
->Number() )
236 wxLogStatus(this, "Can't move this item %s", up
? "up" : "down");
240 bool wasChecked
= m_pListBox
->IsChecked(selection
);
242 int positionOld
= up
? selection
+ 1 : selection
;
245 m_pListBox
->InsertItems(1, &label
, positionNew
);
247 // and delete the old one
248 m_pListBox
->Delete(positionOld
);
250 int selectionNew
= up
? positionNew
: positionNew
- 1;
251 m_pListBox
->Check(selectionNew
, wasChecked
);
252 m_pListBox
->SetSelection(selectionNew
);
254 wxLogStatus(this, "Item moved %s", up
? "up" : "down");
259 wxLogStatus(this, "Please select an item");