]>
Commit | Line | Data |
---|---|---|
bbf1f0e5 KB |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: ownerdrw.cpp | |
3 | // Purpose: Owner-draw sample, for Windows | |
4 | // Author: Vadim Zeitlin | |
925e9792 | 5 | // Modified by: |
bbf1f0e5 | 6 | // Created: 13.11.97 |
bbf1f0e5 | 7 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> |
526954c5 | 8 | // Licence: wxWindows licence |
bbf1f0e5 KB |
9 | /////////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | // ============================================================================ | |
12 | // headers & declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // For compilers that support precompilation, includes "wx.h". | |
16 | #include "wx/wxprec.h" | |
17 | ||
18 | #ifdef __BORLANDC__ | |
4ccf704a | 19 | #pragma hdrstop |
bbf1f0e5 KB |
20 | #endif |
21 | ||
22 | #ifndef WX_PRECOMP | |
4ccf704a | 23 | #include "wx/wx.h" |
bbf1f0e5 KB |
24 | #endif |
25 | ||
26 | #include "wx/ownerdrw.h" | |
27 | #include "wx/menuitem.h" | |
f0f574b0 | 28 | #include "wx/checklst.h" |
bbf1f0e5 KB |
29 | |
30 | // Define a new application type | |
31 | class OwnerDrawnApp: public wxApp | |
32 | { | |
33 | public: | |
4ccf704a | 34 | bool OnInit(); |
bbf1f0e5 KB |
35 | }; |
36 | ||
37 | // Define a new frame type | |
38 | class OwnerDrawnFrame : public wxFrame | |
39 | { | |
40 | public: | |
4ccf704a | 41 | // ctor & dtor |
fbfb8bcc | 42 | OwnerDrawnFrame(wxFrame *frame, const wxChar *title, int x, int y, int w, int h); |
925e9792 | 43 | ~OwnerDrawnFrame(){}; |
bbf1f0e5 | 44 | |
4ccf704a VZ |
45 | // notifications |
46 | void OnQuit (wxCommandEvent& event); | |
1bf0f0af | 47 | void OnMenuToggle (wxCommandEvent& event); |
4ccf704a VZ |
48 | void OnAbout (wxCommandEvent& event); |
49 | void OnListboxSelect (wxCommandEvent& event); | |
50 | void OnCheckboxToggle (wxCommandEvent& event); | |
51 | void OnListboxDblClick (wxCommandEvent& event); | |
f0f574b0 | 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; |
1bf0f0af | 60 | wxMenuItem *pAboutItem; |
bbf1f0e5 KB |
61 | }; |
62 | ||
925e9792 | 63 | enum |
4ccf704a | 64 | { |
925e9792 | 65 | Menu_Quit = 1, |
4ccf704a | 66 | Menu_First = 100, |
925e9792 WS |
67 | Menu_Test1, Menu_Test2, Menu_Test3, |
68 | Menu_Bitmap, Menu_Bitmap2, | |
4ccf704a | 69 | Menu_Submenu, Menu_Sub1, Menu_Sub2, Menu_Sub3, |
1bf0f0af | 70 | Menu_Toggle, Menu_About, |
aa4919ed VZ |
71 | Menu_Drawn1, Menu_Drawn2, Menu_Drawn3, Menu_Drawn4, Menu_Drawn5, |
72 | Menu_Native1, Menu_Native2, Menu_Native3, Menu_Native4, Menu_Native5, | |
4ccf704a | 73 | Control_First = 1000, |
004f4002 | 74 | Control_Listbox, Control_Listbox2 |
bbf1f0e5 KB |
75 | }; |
76 | ||
77 | BEGIN_EVENT_TABLE(OwnerDrawnFrame, wxFrame) | |
1bf0f0af VZ |
78 | EVT_MENU(Menu_Toggle, OwnerDrawnFrame::OnMenuToggle) |
79 | EVT_MENU(Menu_About, OwnerDrawnFrame::OnAbout) | |
4ccf704a VZ |
80 | EVT_MENU(Menu_Quit, OwnerDrawnFrame::OnQuit) |
81 | EVT_LISTBOX(Control_Listbox, OwnerDrawnFrame::OnListboxSelect) | |
82 | EVT_CHECKLISTBOX(Control_Listbox, OwnerDrawnFrame::OnCheckboxToggle) | |
ce7fe42e | 83 | EVT_COMMAND(Control_Listbox, wxEVT_LISTBOX_DCLICK, |
4ccf704a | 84 | OwnerDrawnFrame::OnListboxDblClick) |
bbf1f0e5 KB |
85 | END_EVENT_TABLE() |
86 | ||
004f4002 | 87 | IMPLEMENT_APP(OwnerDrawnApp) |
bbf1f0e5 KB |
88 | |
89 | // init our app: create windows | |
90 | bool OwnerDrawnApp::OnInit(void) | |
91 | { | |
45e6e6f8 VZ |
92 | if ( !wxApp::OnInit() ) |
93 | return false; | |
94 | ||
4ccf704a | 95 | OwnerDrawnFrame *pFrame |
9a83f860 | 96 | = new OwnerDrawnFrame(NULL, wxT("wxWidgets Ownerdraw Sample"), |
4ccf704a VZ |
97 | 50, 50, 450, 340); |
98 | ||
f0f574b0 | 99 | return true; |
bbf1f0e5 KB |
100 | } |
101 | ||
102 | // create the menu bar for the main frame | |
103 | void OwnerDrawnFrame::InitMenu() | |
104 | { | |
4ccf704a | 105 | // Make a menubar |
f0f574b0 WS |
106 | wxMenuItem *pItem; |
107 | wxMenu *file_menu = new wxMenu; | |
108 | ||
109 | #ifndef __WXUNIVERSAL__ | |
110 | wxMenu *sub_menu = new wxMenu; | |
4ccf704a VZ |
111 | |
112 | // vars used for menu construction | |
f0f574b0 WS |
113 | wxFont fontLarge(18, wxROMAN, wxNORMAL, wxBOLD, false), |
114 | fontUlined(12, wxDEFAULT, wxNORMAL, wxNORMAL, true), | |
115 | fontItalic(12, wxMODERN, wxITALIC, wxBOLD, false), | |
4ccf704a | 116 | // should be at least of the size of bitmaps |
f0f574b0 | 117 | fontBmp(14, wxDEFAULT, wxNORMAL, wxNORMAL, false); |
4ccf704a VZ |
118 | |
119 | // sorry for my artistic skills... | |
9a83f860 VZ |
120 | wxBitmap bmpBell(wxT("bell")), |
121 | bmpSound(wxT("sound")), | |
122 | bmpNoSound(wxT("nosound")), | |
123 | bmpInfo(wxT("info")), | |
124 | bmpInfo_mono(wxT("info_mono")); | |
4ccf704a VZ |
125 | |
126 | // construct submenu | |
9a83f860 | 127 | pItem = new wxMenuItem(sub_menu, Menu_Sub1, wxT("Submenu &first"), wxT("large")); |
4ccf704a VZ |
128 | |
129 | pItem->SetFont(fontLarge); | |
130 | sub_menu->Append(pItem); | |
131 | ||
9a83f860 | 132 | pItem = new wxMenuItem(sub_menu, Menu_Sub2, wxT("Submenu &second"), wxT("italic"), |
4ccf704a VZ |
133 | wxITEM_CHECK); |
134 | pItem->SetFont(fontItalic); | |
135 | sub_menu->Append(pItem); | |
136 | ||
9a83f860 | 137 | pItem = new wxMenuItem(sub_menu, Menu_Sub3, wxT("Submenu &third"), wxT("underlined"), |
4ccf704a VZ |
138 | wxITEM_CHECK); |
139 | pItem->SetFont(fontUlined); | |
140 | sub_menu->Append(pItem); | |
141 | ||
142 | // construct menu | |
9a83f860 | 143 | pItem = new wxMenuItem(file_menu, Menu_Test1, wxT("&Uncheckable"), wxT("red item")); |
4ccf704a VZ |
144 | pItem->SetFont(*wxITALIC_FONT); |
145 | pItem->SetTextColour(wxColor(255, 0, 0)); | |
4ccf704a VZ |
146 | file_menu->Append(pItem); |
147 | ||
9a83f860 VZ |
148 | pItem = new wxMenuItem(file_menu, Menu_Test2, wxT("&Checkable"), |
149 | wxT("checkable item"), wxITEM_CHECK); | |
4ccf704a VZ |
150 | pItem->SetFont(*wxSMALL_FONT); |
151 | file_menu->Append(pItem); | |
f0f574b0 | 152 | file_menu->Check(Menu_Test2, true); |
4ccf704a | 153 | |
9a83f860 | 154 | pItem = new wxMenuItem(file_menu, Menu_Test3, wxT("&Disabled"), wxT("disabled item")); |
4ccf704a VZ |
155 | pItem->SetFont(*wxNORMAL_FONT); |
156 | file_menu->Append(pItem); | |
f0f574b0 | 157 | file_menu->Enable(Menu_Test3, false); |
4ccf704a VZ |
158 | |
159 | file_menu->AppendSeparator(); | |
160 | ||
9a83f860 VZ |
161 | pItem = new wxMenuItem(file_menu, Menu_Bitmap, wxT("&Bell"), |
162 | wxT("check/uncheck me!"), wxITEM_CHECK); | |
4ccf704a VZ |
163 | pItem->SetFont(fontBmp); |
164 | pItem->SetBitmaps(bmpBell); | |
165 | file_menu->Append(pItem); | |
166 | ||
9a83f860 VZ |
167 | pItem = new wxMenuItem(file_menu, Menu_Bitmap2, wxT("So&und"), |
168 | wxT("icon changes!"), wxITEM_CHECK); | |
4ccf704a VZ |
169 | pItem->SetFont(fontBmp); |
170 | pItem->SetBitmaps(bmpSound, bmpNoSound); | |
171 | file_menu->Append(pItem); | |
172 | ||
173 | file_menu->AppendSeparator(); | |
174 | ||
9a83f860 | 175 | pItem = new wxMenuItem(file_menu, Menu_Submenu, wxT("&Sub menu"), wxT(""), |
4ccf704a VZ |
176 | wxITEM_CHECK, sub_menu); |
177 | pItem->SetFont(*wxSWISS_FONT); | |
178 | file_menu->Append(pItem); | |
179 | ||
1bf0f0af | 180 | file_menu->AppendSeparator(); |
9a83f860 VZ |
181 | pItem = new wxMenuItem(file_menu, Menu_Toggle, wxT("&Disable/Enable\tCtrl+D"), |
182 | wxT("enables/disables the About-Item"), wxITEM_NORMAL); | |
1bf0f0af VZ |
183 | pItem->SetFont(*wxNORMAL_FONT); |
184 | file_menu->Append(pItem); | |
185 | ||
186 | // Of course Ctrl+RatherLongAccel will not work in this example: | |
9a83f860 VZ |
187 | pAboutItem = new wxMenuItem(file_menu, Menu_About, wxT("&About\tCtrl+RatherLongAccel"), |
188 | wxT("display program information"), wxITEM_NORMAL); | |
1bf0f0af VZ |
189 | pAboutItem->SetBitmap(bmpInfo); |
190 | pAboutItem->SetDisabledBitmap(bmpInfo_mono); | |
1bf0f0af VZ |
191 | file_menu->Append(pAboutItem); |
192 | ||
4ccf704a | 193 | file_menu->AppendSeparator(); |
f0f574b0 WS |
194 | #endif |
195 | ||
9a83f860 | 196 | pItem = new wxMenuItem(file_menu, Menu_Quit, wxT("&Quit"), wxT("Normal item"), |
2a2a71e3 | 197 | wxITEM_NORMAL); |
2a2a71e3 | 198 | file_menu->Append(pItem); |
4ccf704a | 199 | |
aa4919ed VZ |
200 | wxMenu* drawn_menu = new wxMenu; |
201 | pItem = new wxMenuItem(drawn_menu, Menu_Drawn1, wxT("&Menu item\tCtrl+K")); | |
202 | drawn_menu->Append(pItem); | |
203 | ||
204 | drawn_menu->AppendSeparator(); | |
205 | ||
b5415496 | 206 | pItem = new wxMenuItem(drawn_menu, Menu_Drawn2, wxT("&Checked item"), |
aa4919ed VZ |
207 | wxT("check/uncheck me!"), wxITEM_CHECK); |
208 | drawn_menu->Append(pItem); | |
209 | drawn_menu->Check(Menu_Drawn2, true); | |
210 | ||
211 | pItem = new wxMenuItem(drawn_menu, Menu_Drawn3, wxT("&Radio item"), | |
212 | wxT("check/uncheck me!"), wxITEM_RADIO); | |
213 | drawn_menu->Append(pItem); | |
214 | ||
215 | drawn_menu->AppendSeparator(); | |
216 | ||
217 | pItem = new wxMenuItem(drawn_menu, Menu_Drawn4, wxT("&Disabled item\tCtrl+RatherLongAccel"), | |
218 | wxT("disabled item")); | |
219 | pItem->Enable(false); | |
220 | drawn_menu->Append(pItem); | |
221 | ||
222 | pItem = new wxMenuItem(drawn_menu, Menu_Drawn5, wxT("&Other\tCtrl+O"), wxT("other item")); | |
223 | pItem->SetTextColour(*wxRED); | |
224 | drawn_menu->Append(pItem); | |
225 | ||
226 | wxMenu* native_menu = new wxMenu; | |
227 | pItem = new wxMenuItem(native_menu, Menu_Native1, wxT("&Menu item\tCtrl+K")); | |
228 | native_menu->Append(pItem); | |
229 | ||
230 | native_menu->AppendSeparator(); | |
231 | ||
b5415496 | 232 | pItem = new wxMenuItem(native_menu, Menu_Native2, wxT("&Checked item"), |
aa4919ed VZ |
233 | wxT("check/uncheck me!"), wxITEM_CHECK); |
234 | native_menu->Append(pItem); | |
235 | native_menu->Check(Menu_Native2, true); | |
236 | ||
237 | pItem = new wxMenuItem(native_menu, Menu_Native3, wxT("&Radio item"), | |
238 | wxT("check/uncheck me!"), wxITEM_RADIO); | |
239 | native_menu->Append(pItem); | |
240 | ||
241 | native_menu->AppendSeparator(); | |
242 | ||
243 | pItem = new wxMenuItem(native_menu, Menu_Native4, wxT("&Disabled item\tCtrl+RatherLongAccel"), | |
244 | wxT("disabled item")); | |
245 | pItem->Enable(false); | |
246 | native_menu->Append(pItem); | |
247 | ||
248 | pItem = new wxMenuItem(native_menu, Menu_Native5, wxT("&Other\tCtrl+O"), wxT("other item")); | |
249 | native_menu->Append(pItem); | |
250 | ||
4ccf704a VZ |
251 | wxMenuBar *menu_bar = new wxMenuBar; |
252 | ||
9a83f860 | 253 | menu_bar->Append(file_menu, wxT("&File")); |
aa4919ed VZ |
254 | menu_bar->Append(drawn_menu, wxT("&Drawn")); |
255 | menu_bar->Append(native_menu, wxT("&Native")); | |
4ccf704a | 256 | SetMenuBar(menu_bar); |
bbf1f0e5 KB |
257 | } |
258 | ||
259 | // main frame constructor | |
fbfb8bcc | 260 | OwnerDrawnFrame::OwnerDrawnFrame(wxFrame *frame, const wxChar *title, |
4ccf704a | 261 | int x, int y, int w, int h) |
f0f574b0 | 262 | : wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)) |
bbf1f0e5 | 263 | { |
4ccf704a | 264 | // set the icon |
3cb332c1 | 265 | SetIcon(wxICON(sample)); |
bbf1f0e5 | 266 | |
4ccf704a VZ |
267 | // create the menu |
268 | InitMenu(); | |
bbf1f0e5 | 269 | |
8520f137 | 270 | #if wxUSE_STATUSBAR |
4ccf704a VZ |
271 | // create the status line |
272 | const int widths[] = { -1, 60 }; | |
273 | CreateStatusBar(2); | |
274 | SetStatusWidths(2, widths); | |
9a83f860 | 275 | SetStatusText(wxT("no selection"), 0); |
8520f137 | 276 | #endif // wxUSE_STATUSBAR |
88b0e1c8 | 277 | |
4ccf704a | 278 | // make a panel with some controls |
f8a0822b | 279 | wxPanel *pPanel = new wxPanel(this); |
bbf1f0e5 | 280 | |
4ccf704a | 281 | // check list box |
9a83f860 VZ |
282 | static const wxChar* aszChoices[] = { wxT("Hello"), wxT("world"), wxT("and"), |
283 | wxT("goodbye"), wxT("cruel"), wxT("world"), | |
284 | wxT("-------"), wxT("owner-drawn"), wxT("listbox") }; | |
bbf1f0e5 | 285 | |
4ccf704a VZ |
286 | wxString *astrChoices = new wxString[WXSIZEOF(aszChoices)]; |
287 | unsigned int ui; | |
288 | for ( ui = 0; ui < WXSIZEOF(aszChoices); ui++ ) | |
289 | astrChoices[ui] = aszChoices[ui]; | |
290 | ||
291 | m_pListBox = new wxCheckListBox | |
292 | ( | |
293 | pPanel, // parent | |
294 | Control_Listbox, // control id | |
295 | wxPoint(10, 10), // listbox position | |
296 | wxSize(200, 200), // listbox size | |
297 | WXSIZEOF(aszChoices), // number of strings | |
298 | astrChoices // array of strings | |
299 | ); | |
300 | ||
301 | delete [] astrChoices; | |
302 | ||
303 | for ( ui = 0; ui < WXSIZEOF(aszChoices); ui += 2 ) | |
304 | { | |
f0f574b0 | 305 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) |
4ccf704a | 306 | m_pListBox->GetItem(ui)->SetBackgroundColour(wxColor(200, 200, 200)); |
f0f574b0 | 307 | #endif |
4ccf704a VZ |
308 | } |
309 | ||
310 | m_pListBox->Check(2); | |
311 | ||
312 | // normal (but owner-drawn) listbox | |
9a83f860 VZ |
313 | static const wxChar* aszColors[] = { wxT("Red"), wxT("Blue"), wxT("Pink"), |
314 | wxT("Green"), wxT("Yellow"), | |
315 | wxT("Black"), wxT("Violet") }; | |
4ccf704a VZ |
316 | |
317 | astrChoices = new wxString[WXSIZEOF(aszColors)]; | |
318 | ||
319 | for ( ui = 0; ui < WXSIZEOF(aszColors); ui++ ) | |
320 | { | |
321 | astrChoices[ui] = aszColors[ui]; | |
322 | } | |
323 | ||
324 | wxListBox *pListBox = new wxListBox | |
325 | ( | |
326 | pPanel, // parent | |
327 | Control_Listbox2, // control id | |
328 | wxPoint(220, 10), // listbox position | |
f8a0822b VZ |
329 | wxSize(200, 200), // listbox size |
330 | WXSIZEOF(aszColors), // number of strings | |
4ccf704a | 331 | astrChoices, // array of strings |
f8a0822b | 332 | wxLB_OWNERDRAW // owner-drawn |
4ccf704a VZ |
333 | ); |
334 | ||
f0f574b0 WS |
335 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) |
336 | ||
925e9792 | 337 | struct { unsigned char r, g, b; } aColors[] = |
f0f574b0 WS |
338 | { |
339 | {255,0,0}, {0,0,255}, {255,128,192}, | |
925e9792 | 340 | {0,255,0}, {255,255,128}, |
f0f574b0 WS |
341 | {0,0,0}, {128,0,255} |
342 | }; | |
343 | ||
4ccf704a VZ |
344 | for ( ui = 0; ui < WXSIZEOF(aszColors); ui++ ) |
345 | { | |
346 | pListBox->GetItem(ui)->SetTextColour(wxColor(aColors[ui].r, | |
925e9792 | 347 | aColors[ui].g, |
4ccf704a VZ |
348 | aColors[ui].b)); |
349 | // yellow on white is horrible... | |
350 | if ( ui == 4 ) | |
351 | { | |
352 | pListBox->GetItem(ui)->SetBackgroundColour(wxColor(0, 0, 0)); | |
353 | } | |
4ccf704a VZ |
354 | } |
355 | ||
f0f574b0 WS |
356 | #else |
357 | wxUnusedVar( pListBox ); | |
358 | #endif | |
359 | ||
4ccf704a VZ |
360 | delete[] astrChoices; |
361 | ||
f0f574b0 | 362 | Show(true); |
bbf1f0e5 KB |
363 | } |
364 | ||
33c96451 | 365 | void OwnerDrawnFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
bbf1f0e5 | 366 | { |
f0f574b0 | 367 | Close(true); |
bbf1f0e5 KB |
368 | } |
369 | ||
33c96451 | 370 | void OwnerDrawnFrame::OnMenuToggle(wxCommandEvent& WXUNUSED(event)) |
1bf0f0af VZ |
371 | { |
372 | // This example shows the use of bitmaps in ownerdrawn menuitems and is not a good | |
373 | // example on how to enable and disable menuitems - this should be done with the help of | |
374 | // EVT_UPDATE_UI and EVT_UPDATE_UI_RANGE ! | |
f0f574b0 | 375 | pAboutItem->Enable( pAboutItem->IsEnabled() ? false : true ); |
1bf0f0af VZ |
376 | } |
377 | ||
33c96451 | 378 | void OwnerDrawnFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
bbf1f0e5 | 379 | { |
4ccf704a | 380 | wxMessageDialog dialog(this, |
9a83f860 VZ |
381 | wxT("Demo of owner-drawn controls\n"), |
382 | wxT("About wxOwnerDrawn"), wxYES_NO | wxCANCEL); | |
4ccf704a | 383 | dialog.ShowModal(); |
bbf1f0e5 KB |
384 | } |
385 | ||
386 | void OwnerDrawnFrame::OnListboxSelect(wxCommandEvent& event) | |
387 | { | |
8520f137 | 388 | #if wxUSE_STATUSBAR |
4ccf704a VZ |
389 | wxString strSelection; |
390 | unsigned int nSel = event.GetSelection(); | |
600683ca MB |
391 | strSelection.Printf(wxT("item %d selected (%schecked)"), nSel, |
392 | m_pListBox->IsChecked(nSel) ? wxT("") : wxT("not ")); | |
4ccf704a | 393 | SetStatusText(strSelection); |
8520f137 WS |
394 | #else |
395 | wxUnusedVar(event); | |
396 | #endif // wxUSE_STATUSBAR | |
bbf1f0e5 KB |
397 | } |
398 | ||
33c96451 | 399 | void OwnerDrawnFrame::OnListboxDblClick(wxCommandEvent& WXUNUSED(event)) |
bbf1f0e5 | 400 | { |
4ccf704a | 401 | wxString strSelection; |
600683ca MB |
402 | strSelection.Printf(wxT("item %d double clicked"), |
403 | m_pListBox->GetSelection()); | |
4ccf704a VZ |
404 | wxMessageDialog dialog(this, strSelection); |
405 | dialog.ShowModal(); | |
bbf1f0e5 KB |
406 | } |
407 | ||
408 | void OwnerDrawnFrame::OnCheckboxToggle(wxCommandEvent& event) | |
409 | { | |
8520f137 | 410 | #if wxUSE_STATUSBAR |
4ccf704a VZ |
411 | wxString strSelection; |
412 | unsigned int nItem = event.GetInt(); | |
600683ca MB |
413 | strSelection.Printf(wxT("item %d was %schecked"), nItem, |
414 | m_pListBox->IsChecked(nItem) ? wxT("") : wxT("un")); | |
4ccf704a | 415 | SetStatusText(strSelection); |
8520f137 WS |
416 | #else |
417 | wxUnusedVar(event); | |
418 | #endif // wxUSE_STATUSBAR | |
88b0e1c8 | 419 | } |