Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / samples / ownerdrw / ownerdrw.cpp
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 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
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__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 #include "wx/wx.h"
24 #endif
25
26 #include "wx/ownerdrw.h"
27 #include "wx/menuitem.h"
28 #include "wx/checklst.h"
29
30 // Define a new application type
31 class OwnerDrawnApp: public wxApp
32 {
33 public:
34 bool OnInit();
35 };
36
37 // Define a new frame type
38 class OwnerDrawnFrame : public wxFrame
39 {
40 public:
41 // ctor & dtor
42 OwnerDrawnFrame(wxFrame *frame, const wxChar *title, int x, int y, int w, int h);
43 ~OwnerDrawnFrame(){};
44
45 // notifications
46 void OnQuit (wxCommandEvent& event);
47 void OnMenuToggle (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; }
53
54 DECLARE_EVENT_TABLE()
55
56 private:
57 void InitMenu();
58
59 wxCheckListBox *m_pListBox;
60 wxMenuItem *pAboutItem;
61 };
62
63 enum
64 {
65 Menu_Quit = 1,
66 Menu_First = 100,
67 Menu_Test1, Menu_Test2, Menu_Test3,
68 Menu_Bitmap, Menu_Bitmap2,
69 Menu_Submenu, Menu_Sub1, Menu_Sub2, Menu_Sub3,
70 Menu_Toggle, Menu_About,
71 Menu_Drawn1, Menu_Drawn2, Menu_Drawn3, Menu_Drawn4, Menu_Drawn5,
72 Menu_Native1, Menu_Native2, Menu_Native3, Menu_Native4, Menu_Native5,
73 Control_First = 1000,
74 Control_Listbox, Control_Listbox2
75 };
76
77 BEGIN_EVENT_TABLE(OwnerDrawnFrame, wxFrame)
78 EVT_MENU(Menu_Toggle, OwnerDrawnFrame::OnMenuToggle)
79 EVT_MENU(Menu_About, OwnerDrawnFrame::OnAbout)
80 EVT_MENU(Menu_Quit, OwnerDrawnFrame::OnQuit)
81 EVT_LISTBOX(Control_Listbox, OwnerDrawnFrame::OnListboxSelect)
82 EVT_CHECKLISTBOX(Control_Listbox, OwnerDrawnFrame::OnCheckboxToggle)
83 EVT_COMMAND(Control_Listbox, wxEVT_LISTBOX_DCLICK,
84 OwnerDrawnFrame::OnListboxDblClick)
85 END_EVENT_TABLE()
86
87 IMPLEMENT_APP(OwnerDrawnApp)
88
89 // init our app: create windows
90 bool OwnerDrawnApp::OnInit(void)
91 {
92 if ( !wxApp::OnInit() )
93 return false;
94
95 OwnerDrawnFrame *pFrame
96 = new OwnerDrawnFrame(NULL, wxT("wxWidgets Ownerdraw Sample"),
97 50, 50, 450, 340);
98
99 return true;
100 }
101
102 // create the menu bar for the main frame
103 void OwnerDrawnFrame::InitMenu()
104 {
105 // Make a menubar
106 wxMenuItem *pItem;
107 wxMenu *file_menu = new wxMenu;
108
109 #ifndef __WXUNIVERSAL__
110 wxMenu *sub_menu = new wxMenu;
111
112 // vars used for menu construction
113 wxFont fontLarge(18, wxROMAN, wxNORMAL, wxBOLD, false),
114 fontUlined(12, wxDEFAULT, wxNORMAL, wxNORMAL, true),
115 fontItalic(12, wxMODERN, wxITALIC, wxBOLD, false),
116 // should be at least of the size of bitmaps
117 fontBmp(14, wxDEFAULT, wxNORMAL, wxNORMAL, false);
118
119 // sorry for my artistic skills...
120 wxBitmap bmpBell(wxT("bell")),
121 bmpSound(wxT("sound")),
122 bmpNoSound(wxT("nosound")),
123 bmpInfo(wxT("info")),
124 bmpInfo_mono(wxT("info_mono"));
125
126 // construct submenu
127 pItem = new wxMenuItem(sub_menu, Menu_Sub1, wxT("Submenu &first"), wxT("large"));
128
129 pItem->SetFont(fontLarge);
130 sub_menu->Append(pItem);
131
132 pItem = new wxMenuItem(sub_menu, Menu_Sub2, wxT("Submenu &second"), wxT("italic"),
133 wxITEM_CHECK);
134 pItem->SetFont(fontItalic);
135 sub_menu->Append(pItem);
136
137 pItem = new wxMenuItem(sub_menu, Menu_Sub3, wxT("Submenu &third"), wxT("underlined"),
138 wxITEM_CHECK);
139 pItem->SetFont(fontUlined);
140 sub_menu->Append(pItem);
141
142 // construct menu
143 pItem = new wxMenuItem(file_menu, Menu_Test1, wxT("&Uncheckable"), wxT("red item"));
144 pItem->SetFont(*wxITALIC_FONT);
145 pItem->SetTextColour(wxColor(255, 0, 0));
146 file_menu->Append(pItem);
147
148 pItem = new wxMenuItem(file_menu, Menu_Test2, wxT("&Checkable"),
149 wxT("checkable item"), wxITEM_CHECK);
150 pItem->SetFont(*wxSMALL_FONT);
151 file_menu->Append(pItem);
152 file_menu->Check(Menu_Test2, true);
153
154 pItem = new wxMenuItem(file_menu, Menu_Test3, wxT("&Disabled"), wxT("disabled item"));
155 pItem->SetFont(*wxNORMAL_FONT);
156 file_menu->Append(pItem);
157 file_menu->Enable(Menu_Test3, false);
158
159 file_menu->AppendSeparator();
160
161 pItem = new wxMenuItem(file_menu, Menu_Bitmap, wxT("&Bell"),
162 wxT("check/uncheck me!"), wxITEM_CHECK);
163 pItem->SetFont(fontBmp);
164 pItem->SetBitmaps(bmpBell);
165 file_menu->Append(pItem);
166
167 pItem = new wxMenuItem(file_menu, Menu_Bitmap2, wxT("So&und"),
168 wxT("icon changes!"), wxITEM_CHECK);
169 pItem->SetFont(fontBmp);
170 pItem->SetBitmaps(bmpSound, bmpNoSound);
171 file_menu->Append(pItem);
172
173 file_menu->AppendSeparator();
174
175 pItem = new wxMenuItem(file_menu, Menu_Submenu, wxT("&Sub menu"), wxT(""),
176 wxITEM_CHECK, sub_menu);
177 pItem->SetFont(*wxSWISS_FONT);
178 file_menu->Append(pItem);
179
180 file_menu->AppendSeparator();
181 pItem = new wxMenuItem(file_menu, Menu_Toggle, wxT("&Disable/Enable\tCtrl+D"),
182 wxT("enables/disables the About-Item"), wxITEM_NORMAL);
183 pItem->SetFont(*wxNORMAL_FONT);
184 file_menu->Append(pItem);
185
186 // Of course Ctrl+RatherLongAccel will not work in this example:
187 pAboutItem = new wxMenuItem(file_menu, Menu_About, wxT("&About\tCtrl+RatherLongAccel"),
188 wxT("display program information"), wxITEM_NORMAL);
189 pAboutItem->SetBitmap(bmpInfo);
190 pAboutItem->SetDisabledBitmap(bmpInfo_mono);
191 file_menu->Append(pAboutItem);
192
193 file_menu->AppendSeparator();
194 #endif
195
196 pItem = new wxMenuItem(file_menu, Menu_Quit, wxT("&Quit"), wxT("Normal item"),
197 wxITEM_NORMAL);
198 file_menu->Append(pItem);
199
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
206 pItem = new wxMenuItem(drawn_menu, Menu_Drawn2, wxT("&Checked item"),
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
232 pItem = new wxMenuItem(native_menu, Menu_Native2, wxT("&Checked item"),
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
251 wxMenuBar *menu_bar = new wxMenuBar;
252
253 menu_bar->Append(file_menu, wxT("&File"));
254 menu_bar->Append(drawn_menu, wxT("&Drawn"));
255 menu_bar->Append(native_menu, wxT("&Native"));
256 SetMenuBar(menu_bar);
257 }
258
259 // main frame constructor
260 OwnerDrawnFrame::OwnerDrawnFrame(wxFrame *frame, const wxChar *title,
261 int x, int y, int w, int h)
262 : wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
263 {
264 // set the icon
265 SetIcon(wxICON(sample));
266
267 // create the menu
268 InitMenu();
269
270 #if wxUSE_STATUSBAR
271 // create the status line
272 const int widths[] = { -1, 60 };
273 CreateStatusBar(2);
274 SetStatusWidths(2, widths);
275 SetStatusText(wxT("no selection"), 0);
276 #endif // wxUSE_STATUSBAR
277
278 // make a panel with some controls
279 wxPanel *pPanel = new wxPanel(this);
280
281 // check list box
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") };
285
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 {
305 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
306 m_pListBox->GetItem(ui)->SetBackgroundColour(wxColor(200, 200, 200));
307 #endif
308 }
309
310 m_pListBox->Check(2);
311
312 // normal (but owner-drawn) listbox
313 static const wxChar* aszColors[] = { wxT("Red"), wxT("Blue"), wxT("Pink"),
314 wxT("Green"), wxT("Yellow"),
315 wxT("Black"), wxT("Violet") };
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
329 wxSize(200, 200), // listbox size
330 WXSIZEOF(aszColors), // number of strings
331 astrChoices, // array of strings
332 wxLB_OWNERDRAW // owner-drawn
333 );
334
335 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
336
337 struct { unsigned char r, g, b; } aColors[] =
338 {
339 {255,0,0}, {0,0,255}, {255,128,192},
340 {0,255,0}, {255,255,128},
341 {0,0,0}, {128,0,255}
342 };
343
344 for ( ui = 0; ui < WXSIZEOF(aszColors); ui++ )
345 {
346 pListBox->GetItem(ui)->SetTextColour(wxColor(aColors[ui].r,
347 aColors[ui].g,
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 }
354 }
355
356 #else
357 wxUnusedVar( pListBox );
358 #endif
359
360 delete[] astrChoices;
361
362 Show(true);
363 }
364
365 void OwnerDrawnFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
366 {
367 Close(true);
368 }
369
370 void OwnerDrawnFrame::OnMenuToggle(wxCommandEvent& WXUNUSED(event))
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 !
375 pAboutItem->Enable( pAboutItem->IsEnabled() ? false : true );
376 }
377
378 void OwnerDrawnFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
379 {
380 wxMessageDialog dialog(this,
381 wxT("Demo of owner-drawn controls\n"),
382 wxT("About wxOwnerDrawn"), wxYES_NO | wxCANCEL);
383 dialog.ShowModal();
384 }
385
386 void OwnerDrawnFrame::OnListboxSelect(wxCommandEvent& event)
387 {
388 #if wxUSE_STATUSBAR
389 wxString strSelection;
390 unsigned int nSel = event.GetSelection();
391 strSelection.Printf(wxT("item %d selected (%schecked)"), nSel,
392 m_pListBox->IsChecked(nSel) ? wxT("") : wxT("not "));
393 SetStatusText(strSelection);
394 #else
395 wxUnusedVar(event);
396 #endif // wxUSE_STATUSBAR
397 }
398
399 void OwnerDrawnFrame::OnListboxDblClick(wxCommandEvent& WXUNUSED(event))
400 {
401 wxString strSelection;
402 strSelection.Printf(wxT("item %d double clicked"),
403 m_pListBox->GetSelection());
404 wxMessageDialog dialog(this, strSelection);
405 dialog.ShowModal();
406 }
407
408 void OwnerDrawnFrame::OnCheckboxToggle(wxCommandEvent& event)
409 {
410 #if wxUSE_STATUSBAR
411 wxString strSelection;
412 unsigned int nItem = event.GetInt();
413 strSelection.Printf(wxT("item %d was %schecked"), nItem,
414 m_pListBox->IsChecked(nItem) ? wxT("") : wxT("un"));
415 SetStatusText(strSelection);
416 #else
417 wxUnusedVar(event);
418 #endif // wxUSE_STATUSBAR
419 }