]> git.saurik.com Git - wxWidgets.git/blame - samples/ownerdrw/ownerdrw.cpp
better way of dealing with EXTRALIBS_xxx
[wxWidgets.git] / samples / ownerdrw / ownerdrw.cpp
CommitLineData
bbf1f0e5
KB
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__
4ccf704a 20 #pragma hdrstop
bbf1f0e5
KB
21#endif
22
23#ifndef WX_PRECOMP
4ccf704a 24 #include "wx/wx.h"
bbf1f0e5
KB
25#endif
26
27#include "wx/ownerdrw.h"
28#include "wx/menuitem.h"
0a4c16d6 29#include "wx/image.h"
bbf1f0e5
KB
30#include "wx/msw/checklst.h"
31
32// Define a new application type
33class OwnerDrawnApp: public wxApp
34{
35public:
4ccf704a 36 bool OnInit();
bbf1f0e5
KB
37};
38
39// Define a new frame type
40class OwnerDrawnFrame : public wxFrame
41{
42public:
4ccf704a 43 // ctor & dtor
600683ca 44 OwnerDrawnFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h);
4ccf704a 45 ~OwnerDrawnFrame();
bbf1f0e5 46
4ccf704a
VZ
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; }
bbf1f0e5 54
4ccf704a 55 DECLARE_EVENT_TABLE()
bbf1f0e5
KB
56
57private:
4ccf704a 58 void InitMenu();
bbf1f0e5 59
4ccf704a 60 wxCheckListBox *m_pListBox;
bbf1f0e5
KB
61};
62
63enum
4ccf704a
VZ
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,
bbf1f0e5
KB
72};
73
74BEGIN_EVENT_TABLE(OwnerDrawnFrame, wxFrame)
4ccf704a
VZ
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)
bbf1f0e5
KB
80END_EVENT_TABLE()
81
82IMPLEMENT_APP(OwnerDrawnApp);
83
84// init our app: create windows
85bool OwnerDrawnApp::OnInit(void)
86{
0a4c16d6 87 wxInitAllImageHandlers();
4ccf704a 88 OwnerDrawnFrame *pFrame
600683ca 89 = new OwnerDrawnFrame(NULL, _T("wxWindows Ownerdraw Sample"),
4ccf704a
VZ
90 50, 50, 450, 340);
91
92 SetTopWindow(pFrame);
bbf1f0e5 93
4ccf704a 94 return TRUE;
bbf1f0e5
KB
95}
96
97// create the menu bar for the main frame
98void OwnerDrawnFrame::InitMenu()
99{
4ccf704a
VZ
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...
0a4c16d6
JS
113 wxBitmap bmpBell(_T("bell"));
114 wxBitmap bmpSound(_T("sound.png"), wxBITMAP_TYPE_PNG);
115 wxBitmap bmpNoSound(_T("nosound.png"), wxBITMAP_TYPE_PNG);
4ccf704a
VZ
116
117 // construct submenu
600683ca 118 pItem = new wxMenuItem(sub_menu, Menu_Sub1, _T("Submenu &first"), _T("large"));
4ccf704a
VZ
119
120 pItem->SetFont(fontLarge);
121 sub_menu->Append(pItem);
122
600683ca 123 pItem = new wxMenuItem(sub_menu, Menu_Sub2, _T("Submenu &second"), _T("italic"),
4ccf704a
VZ
124 wxITEM_CHECK);
125 pItem->SetFont(fontItalic);
126 sub_menu->Append(pItem);
127
600683ca 128 pItem = new wxMenuItem(sub_menu, Menu_Sub3, _T("Submenu &third"), _T("underlined"),
4ccf704a
VZ
129 wxITEM_CHECK);
130 pItem->SetFont(fontUlined);
131 sub_menu->Append(pItem);
132
133 // construct menu
600683ca 134 pItem = new wxMenuItem(file_menu, Menu_Test1, _T("&Uncheckable"), _T("red item"));
4ccf704a
VZ
135 pItem->SetFont(*wxITALIC_FONT);
136 pItem->SetTextColour(wxColor(255, 0, 0));
137 pItem->SetMarginWidth(23);
138 file_menu->Append(pItem);
139
600683ca
MB
140 pItem = new wxMenuItem(file_menu, Menu_Test2, _T("&Checkable"),
141 _T("checkable item"), wxITEM_CHECK);
4ccf704a
VZ
142 pItem->SetFont(*wxSMALL_FONT);
143 file_menu->Append(pItem);
144 file_menu->Check(Menu_Test2, TRUE);
145
600683ca 146 pItem = new wxMenuItem(file_menu, Menu_Test3, _T("&Disabled"), _T("disabled item"));
4ccf704a
VZ
147 pItem->SetFont(*wxNORMAL_FONT);
148 file_menu->Append(pItem);
149 file_menu->Enable(Menu_Test3, FALSE);
150
151 file_menu->AppendSeparator();
152
600683ca
MB
153 pItem = new wxMenuItem(file_menu, Menu_Bitmap, _T("&Bell"),
154 _T("check/uncheck me!"), wxITEM_CHECK);
4ccf704a
VZ
155 pItem->SetFont(fontBmp);
156 pItem->SetBitmaps(bmpBell);
157 file_menu->Append(pItem);
158
600683ca
MB
159 pItem = new wxMenuItem(file_menu, Menu_Bitmap2, _T("So&und"),
160 _T("icon changes!"), wxITEM_CHECK);
4ccf704a
VZ
161 pItem->SetFont(fontBmp);
162 pItem->SetBitmaps(bmpSound, bmpNoSound);
163 file_menu->Append(pItem);
164
165 file_menu->AppendSeparator();
166
600683ca 167 pItem = new wxMenuItem(file_menu, Menu_Submenu, _T("&Sub menu"), _T(""),
4ccf704a
VZ
168 wxITEM_CHECK, sub_menu);
169 pItem->SetFont(*wxSWISS_FONT);
170 file_menu->Append(pItem);
171
172 file_menu->AppendSeparator();
600683ca 173 pItem = new wxMenuItem(file_menu, Menu_Quit, _T("&Quit"), _T("Normal item"),
2a2a71e3
JS
174 wxITEM_NORMAL);
175 pItem->SetFont(*wxNORMAL_FONT);
176 file_menu->Append(pItem);
4ccf704a
VZ
177
178 wxMenuBar *menu_bar = new wxMenuBar;
179
600683ca 180 menu_bar->Append(file_menu, _T("&File"));
4ccf704a 181 SetMenuBar(menu_bar);
bbf1f0e5
KB
182}
183
184// main frame constructor
600683ca 185OwnerDrawnFrame::OwnerDrawnFrame(wxFrame *frame, wxChar *title,
4ccf704a 186 int x, int y, int w, int h)
bbf1f0e5
KB
187 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h))
188{
4ccf704a 189 // set the icon
600683ca 190 SetIcon(wxIcon(_T("mondrian")));
bbf1f0e5 191
4ccf704a
VZ
192 // create the menu
193 InitMenu();
bbf1f0e5 194
4ccf704a
VZ
195 // create the status line
196 const int widths[] = { -1, 60 };
197 CreateStatusBar(2);
198 SetStatusWidths(2, widths);
600683ca 199 SetStatusText(_T("no selection"), 0);
88b0e1c8 200
4ccf704a
VZ
201 // make a panel with some controls
202 wxPanel *pPanel = new wxPanel(this, -1, wxPoint(0, 0),
203 wxSize(400, 200), wxTAB_TRAVERSAL);
bbf1f0e5 204
4ccf704a 205 // check list box
600683ca
MB
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") };
bbf1f0e5 209
4ccf704a
VZ
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
600683ca
MB
235 static const wxChar* aszColors[] = { _T("Red"), _T("Blue"), _T("Pink"),
236 _T("Green"), _T("Yellow"),
237 _T("Black"), _T("Violet") };
4ccf704a
VZ
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);
bbf1f0e5
KB
281}
282
283OwnerDrawnFrame::~OwnerDrawnFrame()
284{
285}
286
287void OwnerDrawnFrame::OnQuit(wxCommandEvent& event)
288{
4ccf704a 289 Close(TRUE);
bbf1f0e5
KB
290}
291
292void OwnerDrawnFrame::OnAbout(wxCommandEvent& event)
293{
4ccf704a 294 wxMessageDialog dialog(this,
600683ca
MB
295 _T("Demo of owner-drawn controls\n"),
296 _T("About wxOwnerDrawn"), wxYES_NO | wxCANCEL);
4ccf704a 297 dialog.ShowModal();
bbf1f0e5
KB
298}
299
300void OwnerDrawnFrame::OnListboxSelect(wxCommandEvent& event)
301{
4ccf704a
VZ
302 wxString strSelection;
303 unsigned int nSel = event.GetSelection();
600683ca
MB
304 strSelection.Printf(wxT("item %d selected (%schecked)"), nSel,
305 m_pListBox->IsChecked(nSel) ? wxT("") : wxT("not "));
4ccf704a 306 SetStatusText(strSelection);
bbf1f0e5
KB
307}
308
309void OwnerDrawnFrame::OnListboxDblClick(wxCommandEvent& event)
310{
4ccf704a 311 wxString strSelection;
600683ca
MB
312 strSelection.Printf(wxT("item %d double clicked"),
313 m_pListBox->GetSelection());
4ccf704a
VZ
314 wxMessageDialog dialog(this, strSelection);
315 dialog.ShowModal();
bbf1f0e5
KB
316}
317
318void OwnerDrawnFrame::OnCheckboxToggle(wxCommandEvent& event)
319{
4ccf704a
VZ
320 wxString strSelection;
321 unsigned int nItem = event.GetInt();
600683ca
MB
322 strSelection.Printf(wxT("item %d was %schecked"), nItem,
323 m_pListBox->IsChecked(nItem) ? wxT("") : wxT("un"));
4ccf704a 324 SetStatusText(strSelection);
88b0e1c8 325}