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