]> git.saurik.com Git - wxWidgets.git/blob - samples/ownerdrw/ownerdrw.cpp
More wxMac compilation fixes.
[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/msw/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("wxWindows 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 wxMenu *file_menu = new wxMenu,
105 *sub_menu = new wxMenu;
106
107 // vars used for menu construction
108 wxMenuItem *pItem;
109 wxFont fontLarge(18, wxROMAN, wxNORMAL, wxBOLD, FALSE),
110 fontUlined(12, wxDEFAULT, wxNORMAL, wxNORMAL, TRUE),
111 fontItalic(12, wxMODERN, wxITALIC, wxBOLD, FALSE),
112 // should be at least of the size of bitmaps
113 fontBmp(14, wxDEFAULT, wxNORMAL, wxNORMAL, FALSE);
114
115 // sorry for my artistic skills...
116 wxBitmap bmpBell(_T("bell")),
117 bmpSound(_T("sound")),
118 bmpNoSound(_T("nosound")),
119 bmpInfo(_T("info")),
120 bmpInfo_mono(_T("info_mono"));
121
122 // construct submenu
123 pItem = new wxMenuItem(sub_menu, Menu_Sub1, _T("Submenu &first"), _T("large"));
124
125 pItem->SetFont(fontLarge);
126 sub_menu->Append(pItem);
127
128 pItem = new wxMenuItem(sub_menu, Menu_Sub2, _T("Submenu &second"), _T("italic"),
129 wxITEM_CHECK);
130 pItem->SetFont(fontItalic);
131 sub_menu->Append(pItem);
132
133 pItem = new wxMenuItem(sub_menu, Menu_Sub3, _T("Submenu &third"), _T("underlined"),
134 wxITEM_CHECK);
135 pItem->SetFont(fontUlined);
136 sub_menu->Append(pItem);
137
138 // construct menu
139 pItem = new wxMenuItem(file_menu, Menu_Test1, _T("&Uncheckable"), _T("red item"));
140 pItem->SetFont(*wxITALIC_FONT);
141 pItem->SetTextColour(wxColor(255, 0, 0));
142 pItem->SetMarginWidth(23);
143 file_menu->Append(pItem);
144
145 pItem = new wxMenuItem(file_menu, Menu_Test2, _T("&Checkable"),
146 _T("checkable item"), wxITEM_CHECK);
147 pItem->SetFont(*wxSMALL_FONT);
148 file_menu->Append(pItem);
149 file_menu->Check(Menu_Test2, TRUE);
150
151 pItem = new wxMenuItem(file_menu, Menu_Test3, _T("&Disabled"), _T("disabled item"));
152 pItem->SetFont(*wxNORMAL_FONT);
153 file_menu->Append(pItem);
154 file_menu->Enable(Menu_Test3, FALSE);
155
156 file_menu->AppendSeparator();
157
158 pItem = new wxMenuItem(file_menu, Menu_Bitmap, _T("&Bell"),
159 _T("check/uncheck me!"), wxITEM_CHECK);
160 pItem->SetFont(fontBmp);
161 pItem->SetBitmaps(bmpBell);
162 file_menu->Append(pItem);
163
164 pItem = new wxMenuItem(file_menu, Menu_Bitmap2, _T("So&und"),
165 _T("icon changes!"), wxITEM_CHECK);
166 pItem->SetFont(fontBmp);
167 pItem->SetBitmaps(bmpSound, bmpNoSound);
168 file_menu->Append(pItem);
169
170 file_menu->AppendSeparator();
171
172 pItem = new wxMenuItem(file_menu, Menu_Submenu, _T("&Sub menu"), _T(""),
173 wxITEM_CHECK, sub_menu);
174 pItem->SetFont(*wxSWISS_FONT);
175 file_menu->Append(pItem);
176
177 file_menu->AppendSeparator();
178 pItem = new wxMenuItem(file_menu, Menu_Toggle, _T("&Disable/Enable\tCtrl+D"),
179 _T("enables/disables the About-Item"), wxITEM_NORMAL);
180 pItem->SetFont(*wxNORMAL_FONT);
181 file_menu->Append(pItem);
182
183 // Of course Ctrl+RatherLongAccel will not work in this example:
184 pAboutItem = new wxMenuItem(file_menu, Menu_About, _T("&About\tCtrl+RatherLongAccel"),
185 _T("display program information"), wxITEM_NORMAL);
186 pAboutItem->SetBitmap(bmpInfo);
187 pAboutItem->SetDisabledBitmap(bmpInfo_mono);
188 file_menu->Append(pAboutItem);
189
190 file_menu->AppendSeparator();
191 pItem = new wxMenuItem(file_menu, Menu_Quit, _T("&Quit"), _T("Normal item"),
192 wxITEM_NORMAL);
193 file_menu->Append(pItem);
194
195 wxMenuBar *menu_bar = new wxMenuBar;
196
197 menu_bar->Append(file_menu, _T("&File"));
198 SetMenuBar(menu_bar);
199 }
200
201 // main frame constructor
202 OwnerDrawnFrame::OwnerDrawnFrame(wxFrame *frame, wxChar *title,
203 int x, int y, int w, int h)
204 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
205 {
206 // set the icon
207 SetIcon(wxIcon(_T("mondrian")));
208
209 // create the menu
210 InitMenu();
211
212 // create the status line
213 const int widths[] = { -1, 60 };
214 CreateStatusBar(2);
215 SetStatusWidths(2, widths);
216 SetStatusText(_T("no selection"), 0);
217
218 // make a panel with some controls
219 wxPanel *pPanel = new wxPanel(this, -1, wxPoint(0, 0),
220 wxSize(400, 200), wxTAB_TRAVERSAL);
221
222 // check list box
223 static const wxChar* aszChoices[] = { _T("Hello"), _T("world"), _T("and"),
224 _T("goodbye"), _T("cruel"), _T("world"),
225 _T("-------"), _T("owner-drawn"), _T("listbox") };
226
227 wxString *astrChoices = new wxString[WXSIZEOF(aszChoices)];
228 unsigned int ui;
229 for ( ui = 0; ui < WXSIZEOF(aszChoices); ui++ )
230 astrChoices[ui] = aszChoices[ui];
231
232 m_pListBox = new wxCheckListBox
233 (
234 pPanel, // parent
235 Control_Listbox, // control id
236 wxPoint(10, 10), // listbox position
237 wxSize(200, 200), // listbox size
238 WXSIZEOF(aszChoices), // number of strings
239 astrChoices // array of strings
240 );
241
242 delete [] astrChoices;
243
244 for ( ui = 0; ui < WXSIZEOF(aszChoices); ui += 2 )
245 {
246 m_pListBox->GetItem(ui)->SetBackgroundColour(wxColor(200, 200, 200));
247 }
248
249 m_pListBox->Check(2);
250
251 // normal (but owner-drawn) listbox
252 static const wxChar* aszColors[] = { _T("Red"), _T("Blue"), _T("Pink"),
253 _T("Green"), _T("Yellow"),
254 _T("Black"), _T("Violet") };
255 struct { unsigned int r, g, b; } aColors[] =
256 {
257 {255,0,0}, {0,0,255}, {255,128,192},
258 {0,255,0}, {255,255,128},
259 {0,0,0}, {128,0,255}
260 };
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 wxDefaultSize, // listbox size
275 WXSIZEOF(aszColors), // number of strings
276 astrChoices, // array of strings
277 wxLB_OWNERDRAW, // owner-drawn
278 wxDefaultValidator, //
279 wxListBoxNameStr
280 );
281
282 for ( ui = 0; ui < WXSIZEOF(aszColors); ui++ )
283 {
284 pListBox->GetItem(ui)->SetTextColour(wxColor(aColors[ui].r,
285 aColors[ui].g,
286 aColors[ui].b));
287 // yellow on white is horrible...
288 if ( ui == 4 )
289 {
290 pListBox->GetItem(ui)->SetBackgroundColour(wxColor(0, 0, 0));
291 }
292
293 }
294
295 delete[] astrChoices;
296
297 Show(TRUE);
298 }
299
300 OwnerDrawnFrame::~OwnerDrawnFrame()
301 {
302 }
303
304 void OwnerDrawnFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
305 {
306 Close(TRUE);
307 }
308
309 void OwnerDrawnFrame::OnMenuToggle(wxCommandEvent& WXUNUSED(event))
310 {
311 // This example shows the use of bitmaps in ownerdrawn menuitems and is not a good
312 // example on how to enable and disable menuitems - this should be done with the help of
313 // EVT_UPDATE_UI and EVT_UPDATE_UI_RANGE !
314 pAboutItem->Enable( pAboutItem->IsEnabled() ? FALSE : TRUE );
315 }
316
317 void OwnerDrawnFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
318 {
319 wxMessageDialog dialog(this,
320 _T("Demo of owner-drawn controls\n"),
321 _T("About wxOwnerDrawn"), wxYES_NO | wxCANCEL);
322 dialog.ShowModal();
323 }
324
325 void OwnerDrawnFrame::OnListboxSelect(wxCommandEvent& event)
326 {
327 wxString strSelection;
328 unsigned int nSel = event.GetSelection();
329 strSelection.Printf(wxT("item %d selected (%schecked)"), nSel,
330 m_pListBox->IsChecked(nSel) ? wxT("") : wxT("not "));
331 SetStatusText(strSelection);
332 }
333
334 void OwnerDrawnFrame::OnListboxDblClick(wxCommandEvent& WXUNUSED(event))
335 {
336 wxString strSelection;
337 strSelection.Printf(wxT("item %d double clicked"),
338 m_pListBox->GetSelection());
339 wxMessageDialog dialog(this, strSelection);
340 dialog.ShowModal();
341 }
342
343 void OwnerDrawnFrame::OnCheckboxToggle(wxCommandEvent& event)
344 {
345 wxString strSelection;
346 unsigned int nItem = event.GetInt();
347 strSelection.Printf(wxT("item %d was %schecked"), nItem,
348 m_pListBox->IsChecked(nItem) ? wxT("") : wxT("un"));
349 SetStatusText(strSelection);
350 }