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
,
73 Control_Listbox
, Control_Listbox2
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
)
86 IMPLEMENT_APP(OwnerDrawnApp
)
88 // init our app: create windows
89 bool OwnerDrawnApp::OnInit(void)
91 if ( !wxApp::OnInit() )
94 OwnerDrawnFrame
*pFrame
95 = new OwnerDrawnFrame(NULL
, wxT("wxWidgets Ownerdraw Sample"),
103 // create the menu bar for the main frame
104 void OwnerDrawnFrame::InitMenu()
108 wxMenu
*file_menu
= new wxMenu
;
110 #ifndef __WXUNIVERSAL__
111 wxMenu
*sub_menu
= new wxMenu
;
113 // vars used for menu construction
114 wxFont
fontLarge(18, wxROMAN
, wxNORMAL
, wxBOLD
, false),
115 fontUlined(12, wxDEFAULT
, wxNORMAL
, wxNORMAL
, true),
116 fontItalic(12, wxMODERN
, wxITALIC
, wxBOLD
, false),
117 // should be at least of the size of bitmaps
118 fontBmp(14, wxDEFAULT
, wxNORMAL
, wxNORMAL
, false);
120 // sorry for my artistic skills...
121 wxBitmap
bmpBell(wxT("bell")),
122 bmpSound(wxT("sound")),
123 bmpNoSound(wxT("nosound")),
124 bmpInfo(wxT("info")),
125 bmpInfo_mono(wxT("info_mono"));
128 pItem
= new wxMenuItem(sub_menu
, Menu_Sub1
, wxT("Submenu &first"), wxT("large"));
130 pItem
->SetFont(fontLarge
);
131 sub_menu
->Append(pItem
);
133 pItem
= new wxMenuItem(sub_menu
, Menu_Sub2
, wxT("Submenu &second"), wxT("italic"),
135 pItem
->SetFont(fontItalic
);
136 sub_menu
->Append(pItem
);
138 pItem
= new wxMenuItem(sub_menu
, Menu_Sub3
, wxT("Submenu &third"), wxT("underlined"),
140 pItem
->SetFont(fontUlined
);
141 sub_menu
->Append(pItem
);
144 pItem
= new wxMenuItem(file_menu
, Menu_Test1
, wxT("&Uncheckable"), wxT("red item"));
145 pItem
->SetFont(*wxITALIC_FONT
);
146 pItem
->SetTextColour(wxColor(255, 0, 0));
147 file_menu
->Append(pItem
);
149 pItem
= new wxMenuItem(file_menu
, Menu_Test2
, wxT("&Checkable"),
150 wxT("checkable item"), wxITEM_CHECK
);
151 pItem
->SetFont(*wxSMALL_FONT
);
152 file_menu
->Append(pItem
);
153 file_menu
->Check(Menu_Test2
, true);
155 pItem
= new wxMenuItem(file_menu
, Menu_Test3
, wxT("&Disabled"), wxT("disabled item"));
156 pItem
->SetFont(*wxNORMAL_FONT
);
157 file_menu
->Append(pItem
);
158 file_menu
->Enable(Menu_Test3
, false);
160 file_menu
->AppendSeparator();
162 pItem
= new wxMenuItem(file_menu
, Menu_Bitmap
, wxT("&Bell"),
163 wxT("check/uncheck me!"), wxITEM_CHECK
);
164 pItem
->SetFont(fontBmp
);
165 pItem
->SetBitmaps(bmpBell
);
166 file_menu
->Append(pItem
);
168 pItem
= new wxMenuItem(file_menu
, Menu_Bitmap2
, wxT("So&und"),
169 wxT("icon changes!"), wxITEM_CHECK
);
170 pItem
->SetFont(fontBmp
);
171 pItem
->SetBitmaps(bmpSound
, bmpNoSound
);
172 file_menu
->Append(pItem
);
174 file_menu
->AppendSeparator();
176 pItem
= new wxMenuItem(file_menu
, Menu_Submenu
, wxT("&Sub menu"), wxT(""),
177 wxITEM_CHECK
, sub_menu
);
178 pItem
->SetFont(*wxSWISS_FONT
);
179 file_menu
->Append(pItem
);
181 file_menu
->AppendSeparator();
182 pItem
= new wxMenuItem(file_menu
, Menu_Toggle
, wxT("&Disable/Enable\tCtrl+D"),
183 wxT("enables/disables the About-Item"), wxITEM_NORMAL
);
184 pItem
->SetFont(*wxNORMAL_FONT
);
185 file_menu
->Append(pItem
);
187 // Of course Ctrl+RatherLongAccel will not work in this example:
188 pAboutItem
= new wxMenuItem(file_menu
, Menu_About
, wxT("&About\tCtrl+RatherLongAccel"),
189 wxT("display program information"), wxITEM_NORMAL
);
190 pAboutItem
->SetBitmap(bmpInfo
);
191 pAboutItem
->SetDisabledBitmap(bmpInfo_mono
);
192 file_menu
->Append(pAboutItem
);
194 file_menu
->AppendSeparator();
197 pItem
= new wxMenuItem(file_menu
, Menu_Quit
, wxT("&Quit"), wxT("Normal item"),
199 file_menu
->Append(pItem
);
201 wxMenuBar
*menu_bar
= new wxMenuBar
;
203 menu_bar
->Append(file_menu
, wxT("&File"));
204 SetMenuBar(menu_bar
);
207 // main frame constructor
208 OwnerDrawnFrame::OwnerDrawnFrame(wxFrame
*frame
, const wxChar
*title
,
209 int x
, int y
, int w
, int h
)
210 : wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
213 SetIcon(wxIcon(wxT("mondrian")));
219 // create the status line
220 const int widths
[] = { -1, 60 };
222 SetStatusWidths(2, widths
);
223 SetStatusText(wxT("no selection"), 0);
224 #endif // wxUSE_STATUSBAR
226 // make a panel with some controls
227 wxPanel
*pPanel
= new wxPanel(this);
230 static const wxChar
* aszChoices
[] = { wxT("Hello"), wxT("world"), wxT("and"),
231 wxT("goodbye"), wxT("cruel"), wxT("world"),
232 wxT("-------"), wxT("owner-drawn"), wxT("listbox") };
234 wxString
*astrChoices
= new wxString
[WXSIZEOF(aszChoices
)];
236 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
++ )
237 astrChoices
[ui
] = aszChoices
[ui
];
239 m_pListBox
= new wxCheckListBox
242 Control_Listbox
, // control id
243 wxPoint(10, 10), // listbox position
244 wxSize(200, 200), // listbox size
245 WXSIZEOF(aszChoices
), // number of strings
246 astrChoices
// array of strings
249 delete [] astrChoices
;
251 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
+= 2 )
253 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
254 m_pListBox
->GetItem(ui
)->SetBackgroundColour(wxColor(200, 200, 200));
258 m_pListBox
->Check(2);
260 // normal (but owner-drawn) listbox
261 static const wxChar
* aszColors
[] = { wxT("Red"), wxT("Blue"), wxT("Pink"),
262 wxT("Green"), wxT("Yellow"),
263 wxT("Black"), wxT("Violet") };
265 astrChoices
= new wxString
[WXSIZEOF(aszColors
)];
267 for ( ui
= 0; ui
< WXSIZEOF(aszColors
); ui
++ )
269 astrChoices
[ui
] = aszColors
[ui
];
272 wxListBox
*pListBox
= new wxListBox
275 Control_Listbox2
, // control id
276 wxPoint(220, 10), // listbox position
277 wxSize(200, 200), // listbox size
278 WXSIZEOF(aszColors
), // number of strings
279 astrChoices
, // array of strings
280 wxLB_OWNERDRAW
// owner-drawn
283 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
285 struct { unsigned char r
, g
, b
; } aColors
[] =
287 {255,0,0}, {0,0,255}, {255,128,192},
288 {0,255,0}, {255,255,128},
292 for ( ui
= 0; ui
< WXSIZEOF(aszColors
); ui
++ )
294 pListBox
->GetItem(ui
)->SetTextColour(wxColor(aColors
[ui
].r
,
297 // yellow on white is horrible...
300 pListBox
->GetItem(ui
)->SetBackgroundColour(wxColor(0, 0, 0));
305 wxUnusedVar( pListBox
);
308 delete[] astrChoices
;
313 void OwnerDrawnFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
318 void OwnerDrawnFrame::OnMenuToggle(wxCommandEvent
& WXUNUSED(event
))
320 // This example shows the use of bitmaps in ownerdrawn menuitems and is not a good
321 // example on how to enable and disable menuitems - this should be done with the help of
322 // EVT_UPDATE_UI and EVT_UPDATE_UI_RANGE !
323 pAboutItem
->Enable( pAboutItem
->IsEnabled() ? false : true );
326 void OwnerDrawnFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
328 wxMessageDialog
dialog(this,
329 wxT("Demo of owner-drawn controls\n"),
330 wxT("About wxOwnerDrawn"), wxYES_NO
| wxCANCEL
);
334 void OwnerDrawnFrame::OnListboxSelect(wxCommandEvent
& event
)
337 wxString strSelection
;
338 unsigned int nSel
= event
.GetSelection();
339 strSelection
.Printf(wxT("item %d selected (%schecked)"), nSel
,
340 m_pListBox
->IsChecked(nSel
) ? wxT("") : wxT("not "));
341 SetStatusText(strSelection
);
344 #endif // wxUSE_STATUSBAR
347 void OwnerDrawnFrame::OnListboxDblClick(wxCommandEvent
& WXUNUSED(event
))
349 wxString strSelection
;
350 strSelection
.Printf(wxT("item %d double clicked"),
351 m_pListBox
->GetSelection());
352 wxMessageDialog
dialog(this, strSelection
);
356 void OwnerDrawnFrame::OnCheckboxToggle(wxCommandEvent
& event
)
359 wxString strSelection
;
360 unsigned int nItem
= event
.GetInt();
361 strSelection
.Printf(wxT("item %d was %schecked"), nItem
,
362 m_pListBox
->IsChecked(nItem
) ? wxT("") : wxT("un"));
363 SetStatusText(strSelection
);
366 #endif // wxUSE_STATUSBAR