]>
Commit | Line | Data |
---|---|---|
bbf1f0e5 KB |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: ownerdrw.cpp | |
3 | // Purpose: Owner-draw sample, for Windows | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 13.11.97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // Licence: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // headers & declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
4ccf704a | 20 | #pragma hdrstop |
bbf1f0e5 KB |
21 | #endif |
22 | ||
23 | #ifndef WX_PRECOMP | |
4ccf704a | 24 | #include "wx/wx.h" |
bbf1f0e5 KB |
25 | #endif |
26 | ||
27 | #include "wx/ownerdrw.h" | |
28 | #include "wx/menuitem.h" | |
29 | #include "wx/msw/checklst.h" | |
30 | ||
31 | // Define a new application type | |
32 | class OwnerDrawnApp: public wxApp | |
33 | { | |
34 | public: | |
4ccf704a | 35 | bool OnInit(); |
bbf1f0e5 KB |
36 | }; |
37 | ||
38 | // Define a new frame type | |
39 | class OwnerDrawnFrame : public wxFrame | |
40 | { | |
41 | public: | |
4ccf704a | 42 | // ctor & dtor |
600683ca | 43 | OwnerDrawnFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h); |
4ccf704a | 44 | ~OwnerDrawnFrame(); |
bbf1f0e5 | 45 | |
4ccf704a VZ |
46 | // notifications |
47 | void OnQuit (wxCommandEvent& event); | |
48 | void OnAbout (wxCommandEvent& event); | |
49 | void OnListboxSelect (wxCommandEvent& event); | |
50 | void OnCheckboxToggle (wxCommandEvent& event); | |
51 | void OnListboxDblClick (wxCommandEvent& event); | |
52 | bool OnClose () { return TRUE; } | |
bbf1f0e5 | 53 | |
4ccf704a | 54 | DECLARE_EVENT_TABLE() |
bbf1f0e5 KB |
55 | |
56 | private: | |
4ccf704a | 57 | void InitMenu(); |
bbf1f0e5 | 58 | |
4ccf704a | 59 | wxCheckListBox *m_pListBox; |
bbf1f0e5 KB |
60 | }; |
61 | ||
62 | enum | |
4ccf704a VZ |
63 | { |
64 | Menu_Quit = 1, | |
65 | Menu_First = 100, | |
66 | Menu_Test1, Menu_Test2, Menu_Test3, | |
67 | Menu_Bitmap, Menu_Bitmap2, | |
68 | Menu_Submenu, Menu_Sub1, Menu_Sub2, Menu_Sub3, | |
69 | Control_First = 1000, | |
70 | Control_Listbox, Control_Listbox2, | |
bbf1f0e5 KB |
71 | }; |
72 | ||
73 | BEGIN_EVENT_TABLE(OwnerDrawnFrame, wxFrame) | |
4ccf704a VZ |
74 | EVT_MENU(Menu_Quit, OwnerDrawnFrame::OnQuit) |
75 | EVT_LISTBOX(Control_Listbox, OwnerDrawnFrame::OnListboxSelect) | |
76 | EVT_CHECKLISTBOX(Control_Listbox, OwnerDrawnFrame::OnCheckboxToggle) | |
77 | EVT_COMMAND(Control_Listbox, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, | |
78 | OwnerDrawnFrame::OnListboxDblClick) | |
bbf1f0e5 KB |
79 | END_EVENT_TABLE() |
80 | ||
81 | IMPLEMENT_APP(OwnerDrawnApp); | |
82 | ||
83 | // init our app: create windows | |
84 | bool OwnerDrawnApp::OnInit(void) | |
85 | { | |
4ccf704a | 86 | OwnerDrawnFrame *pFrame |
600683ca | 87 | = new OwnerDrawnFrame(NULL, _T("wxWindows Ownerdraw Sample"), |
4ccf704a VZ |
88 | 50, 50, 450, 340); |
89 | ||
90 | SetTopWindow(pFrame); | |
bbf1f0e5 | 91 | |
4ccf704a | 92 | return TRUE; |
bbf1f0e5 KB |
93 | } |
94 | ||
95 | // create the menu bar for the main frame | |
96 | void OwnerDrawnFrame::InitMenu() | |
97 | { | |
4ccf704a VZ |
98 | // Make a menubar |
99 | wxMenu *file_menu = new wxMenu, | |
100 | *sub_menu = new wxMenu; | |
101 | ||
102 | // vars used for menu construction | |
103 | wxMenuItem *pItem; | |
104 | wxFont fontLarge(18, wxROMAN, wxNORMAL, wxBOLD, FALSE), | |
105 | fontUlined(12, wxDEFAULT, wxNORMAL, wxNORMAL, TRUE), | |
106 | fontItalic(12, wxMODERN, wxITALIC, wxBOLD, FALSE), | |
107 | // should be at least of the size of bitmaps | |
108 | fontBmp(14, wxDEFAULT, wxNORMAL, wxNORMAL, FALSE); | |
109 | ||
110 | // sorry for my artistic skills... | |
600683ca | 111 | wxBitmap bmpBell(_T("bell")), bmpSound(_T("sound")), bmpNoSound(_T("nosound")); |
4ccf704a VZ |
112 | |
113 | // construct submenu | |
600683ca | 114 | pItem = new wxMenuItem(sub_menu, Menu_Sub1, _T("Submenu &first"), _T("large")); |
4ccf704a VZ |
115 | |
116 | pItem->SetFont(fontLarge); | |
117 | sub_menu->Append(pItem); | |
118 | ||
600683ca | 119 | pItem = new wxMenuItem(sub_menu, Menu_Sub2, _T("Submenu &second"), _T("italic"), |
4ccf704a VZ |
120 | wxITEM_CHECK); |
121 | pItem->SetFont(fontItalic); | |
122 | sub_menu->Append(pItem); | |
123 | ||
600683ca | 124 | pItem = new wxMenuItem(sub_menu, Menu_Sub3, _T("Submenu &third"), _T("underlined"), |
4ccf704a VZ |
125 | wxITEM_CHECK); |
126 | pItem->SetFont(fontUlined); | |
127 | sub_menu->Append(pItem); | |
128 | ||
129 | // construct menu | |
600683ca | 130 | pItem = new wxMenuItem(file_menu, Menu_Test1, _T("&Uncheckable"), _T("red item")); |
4ccf704a VZ |
131 | pItem->SetFont(*wxITALIC_FONT); |
132 | pItem->SetTextColour(wxColor(255, 0, 0)); | |
133 | pItem->SetMarginWidth(23); | |
134 | file_menu->Append(pItem); | |
135 | ||
600683ca MB |
136 | pItem = new wxMenuItem(file_menu, Menu_Test2, _T("&Checkable"), |
137 | _T("checkable item"), wxITEM_CHECK); | |
4ccf704a VZ |
138 | pItem->SetFont(*wxSMALL_FONT); |
139 | file_menu->Append(pItem); | |
140 | file_menu->Check(Menu_Test2, TRUE); | |
141 | ||
600683ca | 142 | pItem = new wxMenuItem(file_menu, Menu_Test3, _T("&Disabled"), _T("disabled item")); |
4ccf704a VZ |
143 | pItem->SetFont(*wxNORMAL_FONT); |
144 | file_menu->Append(pItem); | |
145 | file_menu->Enable(Menu_Test3, FALSE); | |
146 | ||
147 | file_menu->AppendSeparator(); | |
148 | ||
600683ca MB |
149 | pItem = new wxMenuItem(file_menu, Menu_Bitmap, _T("&Bell"), |
150 | _T("check/uncheck me!"), wxITEM_CHECK); | |
4ccf704a VZ |
151 | pItem->SetFont(fontBmp); |
152 | pItem->SetBitmaps(bmpBell); | |
153 | file_menu->Append(pItem); | |
154 | ||
600683ca MB |
155 | pItem = new wxMenuItem(file_menu, Menu_Bitmap2, _T("So&und"), |
156 | _T("icon changes!"), wxITEM_CHECK); | |
4ccf704a VZ |
157 | pItem->SetFont(fontBmp); |
158 | pItem->SetBitmaps(bmpSound, bmpNoSound); | |
159 | file_menu->Append(pItem); | |
160 | ||
161 | file_menu->AppendSeparator(); | |
162 | ||
600683ca | 163 | pItem = new wxMenuItem(file_menu, Menu_Submenu, _T("&Sub menu"), _T(""), |
4ccf704a VZ |
164 | wxITEM_CHECK, sub_menu); |
165 | pItem->SetFont(*wxSWISS_FONT); | |
166 | file_menu->Append(pItem); | |
167 | ||
168 | file_menu->AppendSeparator(); | |
600683ca | 169 | pItem = new wxMenuItem(file_menu, Menu_Quit, _T("&Quit"), _T("Normal item"), |
2a2a71e3 JS |
170 | wxITEM_NORMAL); |
171 | pItem->SetFont(*wxNORMAL_FONT); | |
172 | file_menu->Append(pItem); | |
4ccf704a VZ |
173 | |
174 | wxMenuBar *menu_bar = new wxMenuBar; | |
175 | ||
600683ca | 176 | menu_bar->Append(file_menu, _T("&File")); |
4ccf704a | 177 | SetMenuBar(menu_bar); |
bbf1f0e5 KB |
178 | } |
179 | ||
180 | // main frame constructor | |
600683ca | 181 | OwnerDrawnFrame::OwnerDrawnFrame(wxFrame *frame, wxChar *title, |
4ccf704a | 182 | int x, int y, int w, int h) |
bbf1f0e5 KB |
183 | : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h)) |
184 | { | |
4ccf704a | 185 | // set the icon |
600683ca | 186 | SetIcon(wxIcon(_T("mondrian"))); |
bbf1f0e5 | 187 | |
4ccf704a VZ |
188 | // create the menu |
189 | InitMenu(); | |
bbf1f0e5 | 190 | |
4ccf704a VZ |
191 | // create the status line |
192 | const int widths[] = { -1, 60 }; | |
193 | CreateStatusBar(2); | |
194 | SetStatusWidths(2, widths); | |
600683ca | 195 | SetStatusText(_T("no selection"), 0); |
88b0e1c8 | 196 | |
4ccf704a VZ |
197 | // make a panel with some controls |
198 | wxPanel *pPanel = new wxPanel(this, -1, wxPoint(0, 0), | |
199 | wxSize(400, 200), wxTAB_TRAVERSAL); | |
bbf1f0e5 | 200 | |
4ccf704a | 201 | // check list box |
600683ca MB |
202 | static const wxChar* aszChoices[] = { _T("Hello"), _T("world"), _T("and"), |
203 | _T("goodbye"), _T("cruel"), _T("world"), | |
204 | _T("-------"), _T("owner-drawn"), _T("listbox") }; | |
bbf1f0e5 | 205 | |
4ccf704a VZ |
206 | wxString *astrChoices = new wxString[WXSIZEOF(aszChoices)]; |
207 | unsigned int ui; | |
208 | for ( ui = 0; ui < WXSIZEOF(aszChoices); ui++ ) | |
209 | astrChoices[ui] = aszChoices[ui]; | |
210 | ||
211 | m_pListBox = new wxCheckListBox | |
212 | ( | |
213 | pPanel, // parent | |
214 | Control_Listbox, // control id | |
215 | wxPoint(10, 10), // listbox position | |
216 | wxSize(200, 200), // listbox size | |
217 | WXSIZEOF(aszChoices), // number of strings | |
218 | astrChoices // array of strings | |
219 | ); | |
220 | ||
221 | delete [] astrChoices; | |
222 | ||
223 | for ( ui = 0; ui < WXSIZEOF(aszChoices); ui += 2 ) | |
224 | { | |
225 | m_pListBox->GetItem(ui)->SetBackgroundColour(wxColor(200, 200, 200)); | |
226 | } | |
227 | ||
228 | m_pListBox->Check(2); | |
229 | ||
230 | // normal (but owner-drawn) listbox | |
600683ca MB |
231 | static const wxChar* aszColors[] = { _T("Red"), _T("Blue"), _T("Pink"), |
232 | _T("Green"), _T("Yellow"), | |
233 | _T("Black"), _T("Violet") }; | |
4ccf704a VZ |
234 | struct { unsigned int r, g, b; } aColors[] = |
235 | { | |
236 | {255,0,0}, {0,0,255}, {255,128,192}, | |
237 | {0,255,0}, {255,255,128}, | |
238 | {0,0,0}, {128,0,255} | |
239 | }; | |
240 | ||
241 | astrChoices = new wxString[WXSIZEOF(aszColors)]; | |
242 | ||
243 | for ( ui = 0; ui < WXSIZEOF(aszColors); ui++ ) | |
244 | { | |
245 | astrChoices[ui] = aszColors[ui]; | |
246 | } | |
247 | ||
248 | wxListBox *pListBox = new wxListBox | |
249 | ( | |
250 | pPanel, // parent | |
251 | Control_Listbox2, // control id | |
252 | wxPoint(220, 10), // listbox position | |
253 | wxDefaultSize, // listbox size | |
254 | WXSIZEOF(aszColors), // number of strings | |
255 | astrChoices, // array of strings | |
256 | wxLB_OWNERDRAW, // owner-drawn | |
257 | wxDefaultValidator, // | |
258 | wxListBoxNameStr | |
259 | ); | |
260 | ||
261 | for ( ui = 0; ui < WXSIZEOF(aszColors); ui++ ) | |
262 | { | |
263 | pListBox->GetItem(ui)->SetTextColour(wxColor(aColors[ui].r, | |
264 | aColors[ui].g, | |
265 | aColors[ui].b)); | |
266 | // yellow on white is horrible... | |
267 | if ( ui == 4 ) | |
268 | { | |
269 | pListBox->GetItem(ui)->SetBackgroundColour(wxColor(0, 0, 0)); | |
270 | } | |
271 | ||
272 | } | |
273 | ||
274 | delete[] astrChoices; | |
275 | ||
276 | Show(TRUE); | |
bbf1f0e5 KB |
277 | } |
278 | ||
279 | OwnerDrawnFrame::~OwnerDrawnFrame() | |
280 | { | |
281 | } | |
282 | ||
283 | void OwnerDrawnFrame::OnQuit(wxCommandEvent& event) | |
284 | { | |
4ccf704a | 285 | Close(TRUE); |
bbf1f0e5 KB |
286 | } |
287 | ||
288 | void OwnerDrawnFrame::OnAbout(wxCommandEvent& event) | |
289 | { | |
4ccf704a | 290 | wxMessageDialog dialog(this, |
600683ca MB |
291 | _T("Demo of owner-drawn controls\n"), |
292 | _T("About wxOwnerDrawn"), wxYES_NO | wxCANCEL); | |
4ccf704a | 293 | dialog.ShowModal(); |
bbf1f0e5 KB |
294 | } |
295 | ||
296 | void OwnerDrawnFrame::OnListboxSelect(wxCommandEvent& event) | |
297 | { | |
4ccf704a VZ |
298 | wxString strSelection; |
299 | unsigned int nSel = event.GetSelection(); | |
600683ca MB |
300 | strSelection.Printf(wxT("item %d selected (%schecked)"), nSel, |
301 | m_pListBox->IsChecked(nSel) ? wxT("") : wxT("not ")); | |
4ccf704a | 302 | SetStatusText(strSelection); |
bbf1f0e5 KB |
303 | } |
304 | ||
305 | void OwnerDrawnFrame::OnListboxDblClick(wxCommandEvent& event) | |
306 | { | |
4ccf704a | 307 | wxString strSelection; |
600683ca MB |
308 | strSelection.Printf(wxT("item %d double clicked"), |
309 | m_pListBox->GetSelection()); | |
4ccf704a VZ |
310 | wxMessageDialog dialog(this, strSelection); |
311 | dialog.ShowModal(); | |
bbf1f0e5 KB |
312 | } |
313 | ||
314 | void OwnerDrawnFrame::OnCheckboxToggle(wxCommandEvent& event) | |
315 | { | |
4ccf704a VZ |
316 | wxString strSelection; |
317 | unsigned int nItem = event.GetInt(); | |
600683ca MB |
318 | strSelection.Printf(wxT("item %d was %schecked"), nItem, |
319 | m_pListBox->IsChecked(nItem) ? wxT("") : wxT("un")); | |
4ccf704a | 320 | SetStatusText(strSelection); |
88b0e1c8 | 321 | } |