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