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