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