]> git.saurik.com Git - wxWidgets.git/blob - samples/ownerdrw/ownerdrw.cpp
Implement NSMenuValidation protocol for the wxNSMenuItemTarget
[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/image.h"
30 #include "wx/msw/checklst.h"
31
32 // Define a new application type
33 class OwnerDrawnApp: public wxApp
34 {
35 public:
36 bool OnInit();
37 };
38
39 // Define a new frame type
40 class OwnerDrawnFrame : public wxFrame
41 {
42 public:
43 // ctor & dtor
44 OwnerDrawnFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h);
45 ~OwnerDrawnFrame();
46
47 // notifications
48 void OnQuit (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 };
62
63 enum
64 {
65 Menu_Quit = 1,
66 Menu_First = 100,
67 Menu_Test1, Menu_Test2, Menu_Test3,
68 Menu_Bitmap, Menu_Bitmap2,
69 Menu_Submenu, Menu_Sub1, Menu_Sub2, Menu_Sub3,
70 Control_First = 1000,
71 Control_Listbox, Control_Listbox2,
72 };
73
74 BEGIN_EVENT_TABLE(OwnerDrawnFrame, wxFrame)
75 EVT_MENU(Menu_Quit, OwnerDrawnFrame::OnQuit)
76 EVT_LISTBOX(Control_Listbox, OwnerDrawnFrame::OnListboxSelect)
77 EVT_CHECKLISTBOX(Control_Listbox, OwnerDrawnFrame::OnCheckboxToggle)
78 EVT_COMMAND(Control_Listbox, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED,
79 OwnerDrawnFrame::OnListboxDblClick)
80 END_EVENT_TABLE()
81
82 IMPLEMENT_APP(OwnerDrawnApp);
83
84 // init our app: create windows
85 bool OwnerDrawnApp::OnInit(void)
86 {
87 wxInitAllImageHandlers();
88 OwnerDrawnFrame *pFrame
89 = new OwnerDrawnFrame(NULL, _T("wxWindows Ownerdraw Sample"),
90 50, 50, 450, 340);
91
92 SetTopWindow(pFrame);
93
94 return TRUE;
95 }
96
97 // create the menu bar for the main frame
98 void OwnerDrawnFrame::InitMenu()
99 {
100 // Make a menubar
101 wxMenu *file_menu = new wxMenu,
102 *sub_menu = new wxMenu;
103
104 // vars used for menu construction
105 wxMenuItem *pItem;
106 wxFont fontLarge(18, wxROMAN, wxNORMAL, wxBOLD, FALSE),
107 fontUlined(12, wxDEFAULT, wxNORMAL, wxNORMAL, TRUE),
108 fontItalic(12, wxMODERN, wxITALIC, wxBOLD, FALSE),
109 // should be at least of the size of bitmaps
110 fontBmp(14, wxDEFAULT, wxNORMAL, wxNORMAL, FALSE);
111
112 // sorry for my artistic skills...
113 wxBitmap bmpBell(_T("bell"));
114 wxBitmap bmpSound(_T("sound.png"), wxBITMAP_TYPE_PNG);
115 wxBitmap bmpNoSound(_T("nosound.png"), wxBITMAP_TYPE_PNG);
116
117 // construct submenu
118 pItem = new wxMenuItem(sub_menu, Menu_Sub1, _T("Submenu &first"), _T("large"));
119
120 pItem->SetFont(fontLarge);
121 sub_menu->Append(pItem);
122
123 pItem = new wxMenuItem(sub_menu, Menu_Sub2, _T("Submenu &second"), _T("italic"),
124 wxITEM_CHECK);
125 pItem->SetFont(fontItalic);
126 sub_menu->Append(pItem);
127
128 pItem = new wxMenuItem(sub_menu, Menu_Sub3, _T("Submenu &third"), _T("underlined"),
129 wxITEM_CHECK);
130 pItem->SetFont(fontUlined);
131 sub_menu->Append(pItem);
132
133 // construct menu
134 pItem = new wxMenuItem(file_menu, Menu_Test1, _T("&Uncheckable"), _T("red item"));
135 pItem->SetFont(*wxITALIC_FONT);
136 pItem->SetTextColour(wxColor(255, 0, 0));
137 pItem->SetMarginWidth(23);
138 file_menu->Append(pItem);
139
140 pItem = new wxMenuItem(file_menu, Menu_Test2, _T("&Checkable"),
141 _T("checkable item"), wxITEM_CHECK);
142 pItem->SetFont(*wxSMALL_FONT);
143 file_menu->Append(pItem);
144 file_menu->Check(Menu_Test2, TRUE);
145
146 pItem = new wxMenuItem(file_menu, Menu_Test3, _T("&Disabled"), _T("disabled item"));
147 pItem->SetFont(*wxNORMAL_FONT);
148 file_menu->Append(pItem);
149 file_menu->Enable(Menu_Test3, FALSE);
150
151 file_menu->AppendSeparator();
152
153 pItem = new wxMenuItem(file_menu, Menu_Bitmap, _T("&Bell"),
154 _T("check/uncheck me!"), wxITEM_CHECK);
155 pItem->SetFont(fontBmp);
156 pItem->SetBitmaps(bmpBell);
157 file_menu->Append(pItem);
158
159 pItem = new wxMenuItem(file_menu, Menu_Bitmap2, _T("So&und"),
160 _T("icon changes!"), wxITEM_CHECK);
161 pItem->SetFont(fontBmp);
162 pItem->SetBitmaps(bmpSound, bmpNoSound);
163 file_menu->Append(pItem);
164
165 file_menu->AppendSeparator();
166
167 pItem = new wxMenuItem(file_menu, Menu_Submenu, _T("&Sub menu"), _T(""),
168 wxITEM_CHECK, sub_menu);
169 pItem->SetFont(*wxSWISS_FONT);
170 file_menu->Append(pItem);
171
172 file_menu->AppendSeparator();
173 pItem = new wxMenuItem(file_menu, Menu_Quit, _T("&Quit"), _T("Normal item"),
174 wxITEM_NORMAL);
175 pItem->SetFont(*wxNORMAL_FONT);
176 file_menu->Append(pItem);
177
178 wxMenuBar *menu_bar = new wxMenuBar;
179
180 menu_bar->Append(file_menu, _T("&File"));
181 SetMenuBar(menu_bar);
182 }
183
184 // main frame constructor
185 OwnerDrawnFrame::OwnerDrawnFrame(wxFrame *frame, wxChar *title,
186 int x, int y, int w, int h)
187 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
188 {
189 // set the icon
190 SetIcon(wxIcon(_T("mondrian")));
191
192 // create the menu
193 InitMenu();
194
195 // create the status line
196 const int widths[] = { -1, 60 };
197 CreateStatusBar(2);
198 SetStatusWidths(2, widths);
199 SetStatusText(_T("no selection"), 0);
200
201 // make a panel with some controls
202 wxPanel *pPanel = new wxPanel(this, -1, wxPoint(0, 0),
203 wxSize(400, 200), wxTAB_TRAVERSAL);
204
205 // check list box
206 static const wxChar* aszChoices[] = { _T("Hello"), _T("world"), _T("and"),
207 _T("goodbye"), _T("cruel"), _T("world"),
208 _T("-------"), _T("owner-drawn"), _T("listbox") };
209
210 wxString *astrChoices = new wxString[WXSIZEOF(aszChoices)];
211 unsigned int ui;
212 for ( ui = 0; ui < WXSIZEOF(aszChoices); ui++ )
213 astrChoices[ui] = aszChoices[ui];
214
215 m_pListBox = new wxCheckListBox
216 (
217 pPanel, // parent
218 Control_Listbox, // control id
219 wxPoint(10, 10), // listbox position
220 wxSize(200, 200), // listbox size
221 WXSIZEOF(aszChoices), // number of strings
222 astrChoices // array of strings
223 );
224
225 delete [] astrChoices;
226
227 for ( ui = 0; ui < WXSIZEOF(aszChoices); ui += 2 )
228 {
229 m_pListBox->GetItem(ui)->SetBackgroundColour(wxColor(200, 200, 200));
230 }
231
232 m_pListBox->Check(2);
233
234 // normal (but owner-drawn) listbox
235 static const wxChar* aszColors[] = { _T("Red"), _T("Blue"), _T("Pink"),
236 _T("Green"), _T("Yellow"),
237 _T("Black"), _T("Violet") };
238 struct { unsigned int r, g, b; } aColors[] =
239 {
240 {255,0,0}, {0,0,255}, {255,128,192},
241 {0,255,0}, {255,255,128},
242 {0,0,0}, {128,0,255}
243 };
244
245 astrChoices = new wxString[WXSIZEOF(aszColors)];
246
247 for ( ui = 0; ui < WXSIZEOF(aszColors); ui++ )
248 {
249 astrChoices[ui] = aszColors[ui];
250 }
251
252 wxListBox *pListBox = new wxListBox
253 (
254 pPanel, // parent
255 Control_Listbox2, // control id
256 wxPoint(220, 10), // listbox position
257 wxDefaultSize, // listbox size
258 WXSIZEOF(aszColors), // number of strings
259 astrChoices, // array of strings
260 wxLB_OWNERDRAW, // owner-drawn
261 wxDefaultValidator, //
262 wxListBoxNameStr
263 );
264
265 for ( ui = 0; ui < WXSIZEOF(aszColors); ui++ )
266 {
267 pListBox->GetItem(ui)->SetTextColour(wxColor(aColors[ui].r,
268 aColors[ui].g,
269 aColors[ui].b));
270 // yellow on white is horrible...
271 if ( ui == 4 )
272 {
273 pListBox->GetItem(ui)->SetBackgroundColour(wxColor(0, 0, 0));
274 }
275
276 }
277
278 delete[] astrChoices;
279
280 Show(TRUE);
281 }
282
283 OwnerDrawnFrame::~OwnerDrawnFrame()
284 {
285 }
286
287 void OwnerDrawnFrame::OnQuit(wxCommandEvent& event)
288 {
289 Close(TRUE);
290 }
291
292 void OwnerDrawnFrame::OnAbout(wxCommandEvent& event)
293 {
294 wxMessageDialog dialog(this,
295 _T("Demo of owner-drawn controls\n"),
296 _T("About wxOwnerDrawn"), wxYES_NO | wxCANCEL);
297 dialog.ShowModal();
298 }
299
300 void OwnerDrawnFrame::OnListboxSelect(wxCommandEvent& event)
301 {
302 wxString strSelection;
303 unsigned int nSel = event.GetSelection();
304 strSelection.Printf(wxT("item %d selected (%schecked)"), nSel,
305 m_pListBox->IsChecked(nSel) ? wxT("") : wxT("not "));
306 SetStatusText(strSelection);
307 }
308
309 void OwnerDrawnFrame::OnListboxDblClick(wxCommandEvent& event)
310 {
311 wxString strSelection;
312 strSelection.Printf(wxT("item %d double clicked"),
313 m_pListBox->GetSelection());
314 wxMessageDialog dialog(this, strSelection);
315 dialog.ShowModal();
316 }
317
318 void OwnerDrawnFrame::OnCheckboxToggle(wxCommandEvent& event)
319 {
320 wxString strSelection;
321 unsigned int nItem = event.GetInt();
322 strSelection.Printf(wxT("item %d was %schecked"), nItem,
323 m_pListBox->IsChecked(nItem) ? wxT("") : wxT("un"));
324 SetStatusText(strSelection);
325 }