1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Owner-draw sample, for Windows
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/ownerdrw.h"
28 #include "wx/menuitem.h"
29 #include "wx/checklst.h"
31 // Define a new application type
32 class OwnerDrawnApp
: public wxApp
38 // Define a new frame type
39 class OwnerDrawnFrame
: public wxFrame
43 OwnerDrawnFrame(wxFrame
*frame
, const wxChar
*title
, int x
, int y
, int w
, int h
);
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; }
60 wxCheckListBox
*m_pListBox
;
61 wxMenuItem
*pAboutItem
;
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 Menu_Drawn1
, Menu_Drawn2
, Menu_Drawn3
, Menu_Drawn4
, Menu_Drawn5
,
73 Menu_Native1
, Menu_Native2
, Menu_Native3
, Menu_Native4
, Menu_Native5
,
75 Control_Listbox
, Control_Listbox2
78 BEGIN_EVENT_TABLE(OwnerDrawnFrame
, wxFrame
)
79 EVT_MENU(Menu_Toggle
, OwnerDrawnFrame::OnMenuToggle
)
80 EVT_MENU(Menu_About
, OwnerDrawnFrame::OnAbout
)
81 EVT_MENU(Menu_Quit
, OwnerDrawnFrame::OnQuit
)
82 EVT_LISTBOX(Control_Listbox
, OwnerDrawnFrame::OnListboxSelect
)
83 EVT_CHECKLISTBOX(Control_Listbox
, OwnerDrawnFrame::OnCheckboxToggle
)
84 EVT_COMMAND(Control_Listbox
, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
,
85 OwnerDrawnFrame::OnListboxDblClick
)
88 IMPLEMENT_APP(OwnerDrawnApp
)
90 // init our app: create windows
91 bool OwnerDrawnApp::OnInit(void)
93 if ( !wxApp::OnInit() )
96 OwnerDrawnFrame
*pFrame
97 = new OwnerDrawnFrame(NULL
, wxT("wxWidgets Ownerdraw Sample"),
100 SetTopWindow(pFrame
);
105 // create the menu bar for the main frame
106 void OwnerDrawnFrame::InitMenu()
110 wxMenu
*file_menu
= new wxMenu
;
112 #ifndef __WXUNIVERSAL__
113 wxMenu
*sub_menu
= new wxMenu
;
115 // vars used for menu construction
116 wxFont
fontLarge(18, wxROMAN
, wxNORMAL
, wxBOLD
, false),
117 fontUlined(12, wxDEFAULT
, wxNORMAL
, wxNORMAL
, true),
118 fontItalic(12, wxMODERN
, wxITALIC
, wxBOLD
, false),
119 // should be at least of the size of bitmaps
120 fontBmp(14, wxDEFAULT
, wxNORMAL
, wxNORMAL
, false);
122 // sorry for my artistic skills...
123 wxBitmap
bmpBell(wxT("bell")),
124 bmpSound(wxT("sound")),
125 bmpNoSound(wxT("nosound")),
126 bmpInfo(wxT("info")),
127 bmpInfo_mono(wxT("info_mono"));
130 pItem
= new wxMenuItem(sub_menu
, Menu_Sub1
, wxT("Submenu &first"), wxT("large"));
132 pItem
->SetFont(fontLarge
);
133 sub_menu
->Append(pItem
);
135 pItem
= new wxMenuItem(sub_menu
, Menu_Sub2
, wxT("Submenu &second"), wxT("italic"),
137 pItem
->SetFont(fontItalic
);
138 sub_menu
->Append(pItem
);
140 pItem
= new wxMenuItem(sub_menu
, Menu_Sub3
, wxT("Submenu &third"), wxT("underlined"),
142 pItem
->SetFont(fontUlined
);
143 sub_menu
->Append(pItem
);
146 pItem
= new wxMenuItem(file_menu
, Menu_Test1
, wxT("&Uncheckable"), wxT("red item"));
147 pItem
->SetFont(*wxITALIC_FONT
);
148 pItem
->SetTextColour(wxColor(255, 0, 0));
149 file_menu
->Append(pItem
);
151 pItem
= new wxMenuItem(file_menu
, Menu_Test2
, wxT("&Checkable"),
152 wxT("checkable item"), wxITEM_CHECK
);
153 pItem
->SetFont(*wxSMALL_FONT
);
154 file_menu
->Append(pItem
);
155 file_menu
->Check(Menu_Test2
, true);
157 pItem
= new wxMenuItem(file_menu
, Menu_Test3
, wxT("&Disabled"), wxT("disabled item"));
158 pItem
->SetFont(*wxNORMAL_FONT
);
159 file_menu
->Append(pItem
);
160 file_menu
->Enable(Menu_Test3
, false);
162 file_menu
->AppendSeparator();
164 pItem
= new wxMenuItem(file_menu
, Menu_Bitmap
, wxT("&Bell"),
165 wxT("check/uncheck me!"), wxITEM_CHECK
);
166 pItem
->SetFont(fontBmp
);
167 pItem
->SetBitmaps(bmpBell
);
168 file_menu
->Append(pItem
);
170 pItem
= new wxMenuItem(file_menu
, Menu_Bitmap2
, wxT("So&und"),
171 wxT("icon changes!"), wxITEM_CHECK
);
172 pItem
->SetFont(fontBmp
);
173 pItem
->SetBitmaps(bmpSound
, bmpNoSound
);
174 file_menu
->Append(pItem
);
176 file_menu
->AppendSeparator();
178 pItem
= new wxMenuItem(file_menu
, Menu_Submenu
, wxT("&Sub menu"), wxT(""),
179 wxITEM_CHECK
, sub_menu
);
180 pItem
->SetFont(*wxSWISS_FONT
);
181 file_menu
->Append(pItem
);
183 file_menu
->AppendSeparator();
184 pItem
= new wxMenuItem(file_menu
, Menu_Toggle
, wxT("&Disable/Enable\tCtrl+D"),
185 wxT("enables/disables the About-Item"), wxITEM_NORMAL
);
186 pItem
->SetFont(*wxNORMAL_FONT
);
187 file_menu
->Append(pItem
);
189 // Of course Ctrl+RatherLongAccel will not work in this example:
190 pAboutItem
= new wxMenuItem(file_menu
, Menu_About
, wxT("&About\tCtrl+RatherLongAccel"),
191 wxT("display program information"), wxITEM_NORMAL
);
192 pAboutItem
->SetBitmap(bmpInfo
);
193 pAboutItem
->SetDisabledBitmap(bmpInfo_mono
);
194 file_menu
->Append(pAboutItem
);
196 file_menu
->AppendSeparator();
199 pItem
= new wxMenuItem(file_menu
, Menu_Quit
, wxT("&Quit"), wxT("Normal item"),
201 file_menu
->Append(pItem
);
203 wxMenu
* drawn_menu
= new wxMenu
;
204 pItem
= new wxMenuItem(drawn_menu
, Menu_Drawn1
, wxT("&Menu item\tCtrl+K"));
205 drawn_menu
->Append(pItem
);
207 drawn_menu
->AppendSeparator();
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);
214 pItem
= new wxMenuItem(drawn_menu
, Menu_Drawn3
, wxT("&Radio item"),
215 wxT("check/uncheck me!"), wxITEM_RADIO
);
216 drawn_menu
->Append(pItem
);
218 drawn_menu
->AppendSeparator();
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
);
225 pItem
= new wxMenuItem(drawn_menu
, Menu_Drawn5
, wxT("&Other\tCtrl+O"), wxT("other item"));
226 pItem
->SetTextColour(*wxRED
);
227 drawn_menu
->Append(pItem
);
229 wxMenu
* native_menu
= new wxMenu
;
230 pItem
= new wxMenuItem(native_menu
, Menu_Native1
, wxT("&Menu item\tCtrl+K"));
231 native_menu
->Append(pItem
);
233 native_menu
->AppendSeparator();
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);
240 pItem
= new wxMenuItem(native_menu
, Menu_Native3
, wxT("&Radio item"),
241 wxT("check/uncheck me!"), wxITEM_RADIO
);
242 native_menu
->Append(pItem
);
244 native_menu
->AppendSeparator();
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
);
251 pItem
= new wxMenuItem(native_menu
, Menu_Native5
, wxT("&Other\tCtrl+O"), wxT("other item"));
252 native_menu
->Append(pItem
);
254 wxMenuBar
*menu_bar
= new wxMenuBar
;
256 menu_bar
->Append(file_menu
, wxT("&File"));
257 menu_bar
->Append(drawn_menu
, wxT("&Drawn"));
258 menu_bar
->Append(native_menu
, wxT("&Native"));
259 SetMenuBar(menu_bar
);
262 // main frame constructor
263 OwnerDrawnFrame::OwnerDrawnFrame(wxFrame
*frame
, const wxChar
*title
,
264 int x
, int y
, int w
, int h
)
265 : wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
268 SetIcon(wxIcon(wxT("mondrian")));
274 // create the status line
275 const int widths
[] = { -1, 60 };
277 SetStatusWidths(2, widths
);
278 SetStatusText(wxT("no selection"), 0);
279 #endif // wxUSE_STATUSBAR
281 // make a panel with some controls
282 wxPanel
*pPanel
= new wxPanel(this);
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") };
289 wxString
*astrChoices
= new wxString
[WXSIZEOF(aszChoices
)];
291 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
++ )
292 astrChoices
[ui
] = aszChoices
[ui
];
294 m_pListBox
= new wxCheckListBox
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
304 delete [] astrChoices
;
306 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
+= 2 )
308 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
309 m_pListBox
->GetItem(ui
)->SetBackgroundColour(wxColor(200, 200, 200));
313 m_pListBox
->Check(2);
315 // normal (but owner-drawn) listbox
316 static const wxChar
* aszColors
[] = { wxT("Red"), wxT("Blue"), wxT("Pink"),
317 wxT("Green"), wxT("Yellow"),
318 wxT("Black"), wxT("Violet") };
320 astrChoices
= new wxString
[WXSIZEOF(aszColors
)];
322 for ( ui
= 0; ui
< WXSIZEOF(aszColors
); ui
++ )
324 astrChoices
[ui
] = aszColors
[ui
];
327 wxListBox
*pListBox
= new wxListBox
330 Control_Listbox2
, // control id
331 wxPoint(220, 10), // listbox position
332 wxSize(200, 200), // listbox size
333 WXSIZEOF(aszColors
), // number of strings
334 astrChoices
, // array of strings
335 wxLB_OWNERDRAW
// owner-drawn
338 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
340 struct { unsigned char r
, g
, b
; } aColors
[] =
342 {255,0,0}, {0,0,255}, {255,128,192},
343 {0,255,0}, {255,255,128},
347 for ( ui
= 0; ui
< WXSIZEOF(aszColors
); ui
++ )
349 pListBox
->GetItem(ui
)->SetTextColour(wxColor(aColors
[ui
].r
,
352 // yellow on white is horrible...
355 pListBox
->GetItem(ui
)->SetBackgroundColour(wxColor(0, 0, 0));
360 wxUnusedVar( pListBox
);
363 delete[] astrChoices
;
368 void OwnerDrawnFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
373 void OwnerDrawnFrame::OnMenuToggle(wxCommandEvent
& WXUNUSED(event
))
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 !
378 pAboutItem
->Enable( pAboutItem
->IsEnabled() ? false : true );
381 void OwnerDrawnFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
383 wxMessageDialog
dialog(this,
384 wxT("Demo of owner-drawn controls\n"),
385 wxT("About wxOwnerDrawn"), wxYES_NO
| wxCANCEL
);
389 void OwnerDrawnFrame::OnListboxSelect(wxCommandEvent
& event
)
392 wxString strSelection
;
393 unsigned int nSel
= event
.GetSelection();
394 strSelection
.Printf(wxT("item %d selected (%schecked)"), nSel
,
395 m_pListBox
->IsChecked(nSel
) ? wxT("") : wxT("not "));
396 SetStatusText(strSelection
);
399 #endif // wxUSE_STATUSBAR
402 void OwnerDrawnFrame::OnListboxDblClick(wxCommandEvent
& WXUNUSED(event
))
404 wxString strSelection
;
405 strSelection
.Printf(wxT("item %d double clicked"),
406 m_pListBox
->GetSelection());
407 wxMessageDialog
dialog(this, strSelection
);
411 void OwnerDrawnFrame::OnCheckboxToggle(wxCommandEvent
& event
)
414 wxString strSelection
;
415 unsigned int nItem
= event
.GetInt();
416 strSelection
.Printf(wxT("item %d was %schecked"), nItem
,
417 m_pListBox
->IsChecked(nItem
) ? wxT("") : wxT("un"));
418 SetStatusText(strSelection
);
421 #endif // wxUSE_STATUSBAR