]>
git.saurik.com Git - wxWidgets.git/blob - samples/ownerdrw/ownerdrw.cpp
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Owner-draw sample, for Windows
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
12 // headers & declarations
13 // ============================================================================
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
26 #include "wx/ownerdrw.h"
27 #include "wx/menuitem.h"
28 #include "wx/checklst.h"
30 // Define a new application type
31 class OwnerDrawnApp
: public wxApp
37 // Define a new frame type
38 class OwnerDrawnFrame
: public wxFrame
42 OwnerDrawnFrame(wxFrame
*frame
, const wxChar
*title
, int x
, int y
, int w
, int h
);
46 void OnQuit (wxCommandEvent
& event
);
47 void OnMenuToggle (wxCommandEvent
& event
);
48 void OnAbout (wxCommandEvent
& event
);
49 void OnListboxSelect (wxCommandEvent
& event
);
50 void OnCheckboxToggle (wxCommandEvent
& event
);
51 void OnListboxDblClick (wxCommandEvent
& event
);
52 bool OnClose () { return true; }
59 wxCheckListBox
*m_pListBox
;
60 wxMenuItem
*pAboutItem
;
67 Menu_Test1
, Menu_Test2
, Menu_Test3
,
68 Menu_Bitmap
, Menu_Bitmap2
,
69 Menu_Submenu
, Menu_Sub1
, Menu_Sub2
, Menu_Sub3
,
70 Menu_Toggle
, Menu_About
,
71 Menu_Drawn1
, Menu_Drawn2
, Menu_Drawn3
, Menu_Drawn4
, Menu_Drawn5
,
72 Menu_Native1
, Menu_Native2
, Menu_Native3
, Menu_Native4
, Menu_Native5
,
74 Control_Listbox
, Control_Listbox2
77 BEGIN_EVENT_TABLE(OwnerDrawnFrame
, wxFrame
)
78 EVT_MENU(Menu_Toggle
, OwnerDrawnFrame::OnMenuToggle
)
79 EVT_MENU(Menu_About
, OwnerDrawnFrame::OnAbout
)
80 EVT_MENU(Menu_Quit
, OwnerDrawnFrame::OnQuit
)
81 EVT_LISTBOX(Control_Listbox
, OwnerDrawnFrame::OnListboxSelect
)
82 EVT_CHECKLISTBOX(Control_Listbox
, OwnerDrawnFrame::OnCheckboxToggle
)
83 EVT_COMMAND(Control_Listbox
, wxEVT_LISTBOX_DCLICK
,
84 OwnerDrawnFrame::OnListboxDblClick
)
87 IMPLEMENT_APP(OwnerDrawnApp
)
89 // init our app: create windows
90 bool OwnerDrawnApp::OnInit(void)
92 if ( !wxApp::OnInit() )
95 OwnerDrawnFrame
*pFrame
96 = new OwnerDrawnFrame(NULL
, wxT("wxWidgets Ownerdraw Sample"),
102 // create the menu bar for the main frame
103 void OwnerDrawnFrame::InitMenu()
107 wxMenu
*file_menu
= new wxMenu
;
109 #ifndef __WXUNIVERSAL__
110 wxMenu
*sub_menu
= new wxMenu
;
112 // vars used for menu construction
113 wxFont
fontLarge(18, wxROMAN
, wxNORMAL
, wxBOLD
, false),
114 fontUlined(12, wxDEFAULT
, wxNORMAL
, wxNORMAL
, true),
115 fontItalic(12, wxMODERN
, wxITALIC
, wxBOLD
, false),
116 // should be at least of the size of bitmaps
117 fontBmp(14, wxDEFAULT
, wxNORMAL
, wxNORMAL
, false);
119 // sorry for my artistic skills...
120 wxBitmap
bmpBell(wxT("bell")),
121 bmpSound(wxT("sound")),
122 bmpNoSound(wxT("nosound")),
123 bmpInfo(wxT("info")),
124 bmpInfo_mono(wxT("info_mono"));
127 pItem
= new wxMenuItem(sub_menu
, Menu_Sub1
, wxT("Submenu &first"), wxT("large"));
129 pItem
->SetFont(fontLarge
);
130 sub_menu
->Append(pItem
);
132 pItem
= new wxMenuItem(sub_menu
, Menu_Sub2
, wxT("Submenu &second"), wxT("italic"),
134 pItem
->SetFont(fontItalic
);
135 sub_menu
->Append(pItem
);
137 pItem
= new wxMenuItem(sub_menu
, Menu_Sub3
, wxT("Submenu &third"), wxT("underlined"),
139 pItem
->SetFont(fontUlined
);
140 sub_menu
->Append(pItem
);
143 pItem
= new wxMenuItem(file_menu
, Menu_Test1
, wxT("&Uncheckable"), wxT("red item"));
144 pItem
->SetFont(*wxITALIC_FONT
);
145 pItem
->SetTextColour(wxColor(255, 0, 0));
146 file_menu
->Append(pItem
);
148 pItem
= new wxMenuItem(file_menu
, Menu_Test2
, wxT("&Checkable"),
149 wxT("checkable item"), wxITEM_CHECK
);
150 pItem
->SetFont(*wxSMALL_FONT
);
151 file_menu
->Append(pItem
);
152 file_menu
->Check(Menu_Test2
, true);
154 pItem
= new wxMenuItem(file_menu
, Menu_Test3
, wxT("&Disabled"), wxT("disabled item"));
155 pItem
->SetFont(*wxNORMAL_FONT
);
156 file_menu
->Append(pItem
);
157 file_menu
->Enable(Menu_Test3
, false);
159 file_menu
->AppendSeparator();
161 pItem
= new wxMenuItem(file_menu
, Menu_Bitmap
, wxT("&Bell"),
162 wxT("check/uncheck me!"), wxITEM_CHECK
);
163 pItem
->SetFont(fontBmp
);
164 pItem
->SetBitmaps(bmpBell
);
165 file_menu
->Append(pItem
);
167 pItem
= new wxMenuItem(file_menu
, Menu_Bitmap2
, wxT("So&und"),
168 wxT("icon changes!"), wxITEM_CHECK
);
169 pItem
->SetFont(fontBmp
);
170 pItem
->SetBitmaps(bmpSound
, bmpNoSound
);
171 file_menu
->Append(pItem
);
173 file_menu
->AppendSeparator();
175 pItem
= new wxMenuItem(file_menu
, Menu_Submenu
, wxT("&Sub menu"), wxT(""),
176 wxITEM_CHECK
, sub_menu
);
177 pItem
->SetFont(*wxSWISS_FONT
);
178 file_menu
->Append(pItem
);
180 file_menu
->AppendSeparator();
181 pItem
= new wxMenuItem(file_menu
, Menu_Toggle
, wxT("&Disable/Enable\tCtrl+D"),
182 wxT("enables/disables the About-Item"), wxITEM_NORMAL
);
183 pItem
->SetFont(*wxNORMAL_FONT
);
184 file_menu
->Append(pItem
);
186 // Of course Ctrl+RatherLongAccel will not work in this example:
187 pAboutItem
= new wxMenuItem(file_menu
, Menu_About
, wxT("&About\tCtrl+RatherLongAccel"),
188 wxT("display program information"), wxITEM_NORMAL
);
189 pAboutItem
->SetBitmap(bmpInfo
);
190 pAboutItem
->SetDisabledBitmap(bmpInfo_mono
);
191 file_menu
->Append(pAboutItem
);
193 file_menu
->AppendSeparator();
196 pItem
= new wxMenuItem(file_menu
, Menu_Quit
, wxT("&Quit"), wxT("Normal item"),
198 file_menu
->Append(pItem
);
200 wxMenu
* drawn_menu
= new wxMenu
;
201 pItem
= new wxMenuItem(drawn_menu
, Menu_Drawn1
, wxT("&Menu item\tCtrl+K"));
202 drawn_menu
->Append(pItem
);
204 drawn_menu
->AppendSeparator();
206 pItem
= new wxMenuItem(drawn_menu
, Menu_Drawn2
, wxT("&Checked item"),
207 wxT("check/uncheck me!"), wxITEM_CHECK
);
208 drawn_menu
->Append(pItem
);
209 drawn_menu
->Check(Menu_Drawn2
, true);
211 pItem
= new wxMenuItem(drawn_menu
, Menu_Drawn3
, wxT("&Radio item"),
212 wxT("check/uncheck me!"), wxITEM_RADIO
);
213 drawn_menu
->Append(pItem
);
215 drawn_menu
->AppendSeparator();
217 pItem
= new wxMenuItem(drawn_menu
, Menu_Drawn4
, wxT("&Disabled item\tCtrl+RatherLongAccel"),
218 wxT("disabled item"));
219 pItem
->Enable(false);
220 drawn_menu
->Append(pItem
);
222 pItem
= new wxMenuItem(drawn_menu
, Menu_Drawn5
, wxT("&Other\tCtrl+O"), wxT("other item"));
223 pItem
->SetTextColour(*wxRED
);
224 drawn_menu
->Append(pItem
);
226 wxMenu
* native_menu
= new wxMenu
;
227 pItem
= new wxMenuItem(native_menu
, Menu_Native1
, wxT("&Menu item\tCtrl+K"));
228 native_menu
->Append(pItem
);
230 native_menu
->AppendSeparator();
232 pItem
= new wxMenuItem(native_menu
, Menu_Native2
, wxT("&Checked item"),
233 wxT("check/uncheck me!"), wxITEM_CHECK
);
234 native_menu
->Append(pItem
);
235 native_menu
->Check(Menu_Native2
, true);
237 pItem
= new wxMenuItem(native_menu
, Menu_Native3
, wxT("&Radio item"),
238 wxT("check/uncheck me!"), wxITEM_RADIO
);
239 native_menu
->Append(pItem
);
241 native_menu
->AppendSeparator();
243 pItem
= new wxMenuItem(native_menu
, Menu_Native4
, wxT("&Disabled item\tCtrl+RatherLongAccel"),
244 wxT("disabled item"));
245 pItem
->Enable(false);
246 native_menu
->Append(pItem
);
248 pItem
= new wxMenuItem(native_menu
, Menu_Native5
, wxT("&Other\tCtrl+O"), wxT("other item"));
249 native_menu
->Append(pItem
);
251 wxMenuBar
*menu_bar
= new wxMenuBar
;
253 menu_bar
->Append(file_menu
, wxT("&File"));
254 menu_bar
->Append(drawn_menu
, wxT("&Drawn"));
255 menu_bar
->Append(native_menu
, wxT("&Native"));
256 SetMenuBar(menu_bar
);
259 // main frame constructor
260 OwnerDrawnFrame::OwnerDrawnFrame(wxFrame
*frame
, const wxChar
*title
,
261 int x
, int y
, int w
, int h
)
262 : wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
265 SetIcon(wxICON(sample
));
271 // create the status line
272 const int widths
[] = { -1, 60 };
274 SetStatusWidths(2, widths
);
275 SetStatusText(wxT("no selection"), 0);
276 #endif // wxUSE_STATUSBAR
278 // make a panel with some controls
279 wxPanel
*pPanel
= new wxPanel(this);
282 static const wxChar
* aszChoices
[] = { wxT("Hello"), wxT("world"), wxT("and"),
283 wxT("goodbye"), wxT("cruel"), wxT("world"),
284 wxT("-------"), wxT("owner-drawn"), wxT("listbox") };
286 wxString
*astrChoices
= new wxString
[WXSIZEOF(aszChoices
)];
288 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
++ )
289 astrChoices
[ui
] = aszChoices
[ui
];
291 m_pListBox
= new wxCheckListBox
294 Control_Listbox
, // control id
295 wxPoint(10, 10), // listbox position
296 wxSize(200, 200), // listbox size
297 WXSIZEOF(aszChoices
), // number of strings
298 astrChoices
// array of strings
301 delete [] astrChoices
;
303 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
+= 2 )
305 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
306 m_pListBox
->GetItem(ui
)->SetBackgroundColour(wxColor(200, 200, 200));
310 m_pListBox
->Check(2);
312 // normal (but owner-drawn) listbox
313 static const wxChar
* aszColors
[] = { wxT("Red"), wxT("Blue"), wxT("Pink"),
314 wxT("Green"), wxT("Yellow"),
315 wxT("Black"), wxT("Violet") };
317 astrChoices
= new wxString
[WXSIZEOF(aszColors
)];
319 for ( ui
= 0; ui
< WXSIZEOF(aszColors
); ui
++ )
321 astrChoices
[ui
] = aszColors
[ui
];
324 wxListBox
*pListBox
= new wxListBox
327 Control_Listbox2
, // control id
328 wxPoint(220, 10), // listbox position
329 wxSize(200, 200), // listbox size
330 WXSIZEOF(aszColors
), // number of strings
331 astrChoices
, // array of strings
332 wxLB_OWNERDRAW
// owner-drawn
335 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
337 struct { unsigned char r
, g
, b
; } aColors
[] =
339 {255,0,0}, {0,0,255}, {255,128,192},
340 {0,255,0}, {255,255,128},
344 for ( ui
= 0; ui
< WXSIZEOF(aszColors
); ui
++ )
346 pListBox
->GetItem(ui
)->SetTextColour(wxColor(aColors
[ui
].r
,
349 // yellow on white is horrible...
352 pListBox
->GetItem(ui
)->SetBackgroundColour(wxColor(0, 0, 0));
357 wxUnusedVar( pListBox
);
360 delete[] astrChoices
;
365 void OwnerDrawnFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
370 void OwnerDrawnFrame::OnMenuToggle(wxCommandEvent
& WXUNUSED(event
))
372 // This example shows the use of bitmaps in ownerdrawn menuitems and is not a good
373 // example on how to enable and disable menuitems - this should be done with the help of
374 // EVT_UPDATE_UI and EVT_UPDATE_UI_RANGE !
375 pAboutItem
->Enable( pAboutItem
->IsEnabled() ? false : true );
378 void OwnerDrawnFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
380 wxMessageDialog
dialog(this,
381 wxT("Demo of owner-drawn controls\n"),
382 wxT("About wxOwnerDrawn"), wxYES_NO
| wxCANCEL
);
386 void OwnerDrawnFrame::OnListboxSelect(wxCommandEvent
& event
)
389 wxString strSelection
;
390 unsigned int nSel
= event
.GetSelection();
391 strSelection
.Printf(wxT("item %d selected (%schecked)"), nSel
,
392 m_pListBox
->IsChecked(nSel
) ? wxT("") : wxT("not "));
393 SetStatusText(strSelection
);
396 #endif // wxUSE_STATUSBAR
399 void OwnerDrawnFrame::OnListboxDblClick(wxCommandEvent
& WXUNUSED(event
))
401 wxString strSelection
;
402 strSelection
.Printf(wxT("item %d double clicked"),
403 m_pListBox
->GetSelection());
404 wxMessageDialog
dialog(this, strSelection
);
408 void OwnerDrawnFrame::OnCheckboxToggle(wxCommandEvent
& event
)
411 wxString strSelection
;
412 unsigned int nItem
= event
.GetInt();
413 strSelection
.Printf(wxT("item %d was %schecked"), nItem
,
414 m_pListBox
->IsChecked(nItem
) ? wxT("") : wxT("un"));
415 SetStatusText(strSelection
);
418 #endif // wxUSE_STATUSBAR