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