]>
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"
30 #include "wx/menuitem.h"
31 #include "wx/checklst.h"
33 // Define a new application type
34 class CheckListBoxApp
: public wxApp
40 // Define a new frame type
41 class CheckListBoxFrame
: public wxFrame
45 CheckListBoxFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
);
49 void OnQuit (wxCommandEvent
& event
);
50 void OnAbout (wxCommandEvent
& event
);
51 void OnListboxSelect (wxCommandEvent
& event
);
52 void OnCheckboxToggle (wxCommandEvent
& event
);
53 void OnListboxDblClick (wxCommandEvent
& event
);
54 bool OnClose () { return TRUE
; }
59 wxCheckListBox
*m_pListBox
;
66 Control_Listbox
, Control_Listbox2
,
69 BEGIN_EVENT_TABLE(CheckListBoxFrame
, wxFrame
)
70 EVT_MENU(Menu_Quit
, CheckListBoxFrame::OnQuit
)
71 EVT_LISTBOX(Control_Listbox
, CheckListBoxFrame::OnListboxSelect
)
72 EVT_CHECKLISTBOX(Control_Listbox
, CheckListBoxFrame::OnCheckboxToggle
)
73 EVT_LISTBOX_DCLICK(Control_Listbox
, CheckListBoxFrame::OnListboxDblClick
)
76 IMPLEMENT_APP(CheckListBoxApp
);
78 // init our app: create windows
79 bool CheckListBoxApp::OnInit(void)
81 CheckListBoxFrame
*pFrame
= new CheckListBoxFrame(NULL
, "wxWindows Ownerdraw Sample",
88 // main frame constructor
89 CheckListBoxFrame::CheckListBoxFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
)
90 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
))
92 // create the status line
93 const int widths
[] = { -1, 60 };
95 SetStatusWidths(2, widths
);
96 SetStatusText("no selection", 0);
99 wxMenu
*file_menu
= new wxMenu
;
102 file_menu
->Append(Menu_Quit
, "E&xit");
104 wxMenuBar
*menu_bar
= new wxMenuBar
;
105 menu_bar
->Append(file_menu
, "&File");
106 SetMenuBar(menu_bar
);
108 // make a panel with some controls
109 wxPanel
*pPanel
= new wxPanel(this, -1, wxPoint(0, 0),
110 wxSize(400, 200), wxTAB_TRAVERSAL
);
113 static const char* aszChoices
[] = { "Hello", "world", "and",
114 "goodbye", "cruel", "world",
115 "-------", "owner-drawn", "listbox" };
117 wxString
*astrChoices
= new wxString
[WXSIZEOF(aszChoices
)];
119 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
++ )
120 astrChoices
[ui
] = aszChoices
[ui
];
122 m_pListBox
= new wxCheckListBox
125 Control_Listbox
, // control id
126 wxPoint(10, 10), // listbox poistion
127 wxSize(400, 200), // listbox size
128 WXSIZEOF(aszChoices
), // number of strings
129 astrChoices
// array of strings
132 delete [] astrChoices
;
134 // not implemented in wxGTK yet
136 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
+= 2 ) {
137 m_pListBox
->GetItem(ui
)->SetBackgroundColour(wxColor(200, 200, 200));
141 m_pListBox
->Check(2);
146 CheckListBoxFrame::~CheckListBoxFrame()
150 void CheckListBoxFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
155 void CheckListBoxFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
157 wxMessageDialog
dialog(this, "Demo of wxCheckListBox control\n"
158 "About wxCheckListBox", wxYES_NO
| wxCANCEL
);
162 void CheckListBoxFrame::OnListboxSelect(wxCommandEvent
& event
)
164 wxString strSelection
;
165 unsigned int nSel
= event
.GetSelection();
166 strSelection
.sprintf("item %d selected (%schecked)", nSel
,
167 m_pListBox
->IsChecked((int)nSel
) ? "" : "not ");
168 SetStatusText(strSelection
);
171 void CheckListBoxFrame::OnListboxDblClick(wxCommandEvent
& WXUNUSED(event
))
173 wxString strSelection
;
174 strSelection
.sprintf("item %d double clicked", m_pListBox
->GetSelection());
175 wxMessageDialog
dialog(this, strSelection
);
179 void CheckListBoxFrame::OnCheckboxToggle(wxCommandEvent
& event
)
181 wxString strSelection
;
182 unsigned int nItem
= event
.GetInt();
184 strSelection
.sprintf("item %d was %schecked", nItem
,
185 m_pListBox
->IsChecked(nItem
) ? "" : "un");
186 SetStatusText(strSelection
);