]> git.saurik.com Git - wxWidgets.git/blame - samples/ownerdrw/ownerdrw.cpp
Added support for 'AutoComplete' attribute, automatically used by any wxTextCtrl...
[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
925e9792 5// Modified by:
bbf1f0e5
KB
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"
f0f574b0 29#include "wx/checklst.h"
bbf1f0e5
KB
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 42 // ctor & dtor
fbfb8bcc 43 OwnerDrawnFrame(wxFrame *frame, const wxChar *title, int x, int y, int w, int h);
925e9792 44 ~OwnerDrawnFrame(){};
bbf1f0e5 45
4ccf704a
VZ
46 // notifications
47 void OnQuit (wxCommandEvent& event);
1bf0f0af 48 void OnMenuToggle (wxCommandEvent& event);
4ccf704a
VZ
49 void OnAbout (wxCommandEvent& event);
50 void OnListboxSelect (wxCommandEvent& event);
51 void OnCheckboxToggle (wxCommandEvent& event);
52 void OnListboxDblClick (wxCommandEvent& event);
f0f574b0 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;
1bf0f0af 61 wxMenuItem *pAboutItem;
bbf1f0e5
KB
62};
63
925e9792 64enum
4ccf704a 65{
925e9792 66 Menu_Quit = 1,
4ccf704a 67 Menu_First = 100,
925e9792
WS
68 Menu_Test1, Menu_Test2, Menu_Test3,
69 Menu_Bitmap, Menu_Bitmap2,
4ccf704a 70 Menu_Submenu, Menu_Sub1, Menu_Sub2, Menu_Sub3,
1bf0f0af 71 Menu_Toggle, Menu_About,
4ccf704a 72 Control_First = 1000,
004f4002 73 Control_Listbox, Control_Listbox2
bbf1f0e5
KB
74};
75
76BEGIN_EVENT_TABLE(OwnerDrawnFrame, wxFrame)
1bf0f0af
VZ
77 EVT_MENU(Menu_Toggle, OwnerDrawnFrame::OnMenuToggle)
78 EVT_MENU(Menu_About, OwnerDrawnFrame::OnAbout)
4ccf704a
VZ
79 EVT_MENU(Menu_Quit, OwnerDrawnFrame::OnQuit)
80 EVT_LISTBOX(Control_Listbox, OwnerDrawnFrame::OnListboxSelect)
81 EVT_CHECKLISTBOX(Control_Listbox, OwnerDrawnFrame::OnCheckboxToggle)
925e9792 82 EVT_COMMAND(Control_Listbox, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED,
4ccf704a 83 OwnerDrawnFrame::OnListboxDblClick)
bbf1f0e5
KB
84END_EVENT_TABLE()
85
004f4002 86IMPLEMENT_APP(OwnerDrawnApp)
bbf1f0e5
KB
87
88// init our app: create windows
89bool OwnerDrawnApp::OnInit(void)
90{
45e6e6f8
VZ
91 if ( !wxApp::OnInit() )
92 return false;
93
4ccf704a 94 OwnerDrawnFrame *pFrame
be5a51fb 95 = new OwnerDrawnFrame(NULL, _T("wxWidgets Ownerdraw Sample"),
4ccf704a
VZ
96 50, 50, 450, 340);
97
98 SetTopWindow(pFrame);
bbf1f0e5 99
f0f574b0 100 return true;
bbf1f0e5
KB
101}
102
103// create the menu bar for the main frame
104void OwnerDrawnFrame::InitMenu()
105{
4ccf704a 106 // Make a menubar
f0f574b0
WS
107 wxMenuItem *pItem;
108 wxMenu *file_menu = new wxMenu;
109
110#ifndef __WXUNIVERSAL__
111 wxMenu *sub_menu = new wxMenu;
4ccf704a
VZ
112
113 // vars used for menu construction
f0f574b0
WS
114 wxFont fontLarge(18, wxROMAN, wxNORMAL, wxBOLD, false),
115 fontUlined(12, wxDEFAULT, wxNORMAL, wxNORMAL, true),
116 fontItalic(12, wxMODERN, wxITALIC, wxBOLD, false),
4ccf704a 117 // should be at least of the size of bitmaps
f0f574b0 118 fontBmp(14, wxDEFAULT, wxNORMAL, wxNORMAL, false);
4ccf704a
VZ
119
120 // sorry for my artistic skills...
2ef76992
VZ
121 wxBitmap bmpBell(_T("bell")),
122 bmpSound(_T("sound")),
123 bmpNoSound(_T("nosound")),
124 bmpInfo(_T("info")),
125 bmpInfo_mono(_T("info_mono"));
4ccf704a
VZ
126
127 // construct submenu
600683ca 128 pItem = new wxMenuItem(sub_menu, Menu_Sub1, _T("Submenu &first"), _T("large"));
4ccf704a
VZ
129
130 pItem->SetFont(fontLarge);
131 sub_menu->Append(pItem);
132
600683ca 133 pItem = new wxMenuItem(sub_menu, Menu_Sub2, _T("Submenu &second"), _T("italic"),
4ccf704a
VZ
134 wxITEM_CHECK);
135 pItem->SetFont(fontItalic);
136 sub_menu->Append(pItem);
137
600683ca 138 pItem = new wxMenuItem(sub_menu, Menu_Sub3, _T("Submenu &third"), _T("underlined"),
4ccf704a
VZ
139 wxITEM_CHECK);
140 pItem->SetFont(fontUlined);
141 sub_menu->Append(pItem);
142
143 // construct menu
600683ca 144 pItem = new wxMenuItem(file_menu, Menu_Test1, _T("&Uncheckable"), _T("red item"));
4ccf704a
VZ
145 pItem->SetFont(*wxITALIC_FONT);
146 pItem->SetTextColour(wxColor(255, 0, 0));
4ccf704a
VZ
147 file_menu->Append(pItem);
148
600683ca
MB
149 pItem = new wxMenuItem(file_menu, Menu_Test2, _T("&Checkable"),
150 _T("checkable item"), wxITEM_CHECK);
4ccf704a
VZ
151 pItem->SetFont(*wxSMALL_FONT);
152 file_menu->Append(pItem);
f0f574b0 153 file_menu->Check(Menu_Test2, true);
4ccf704a 154
600683ca 155 pItem = new wxMenuItem(file_menu, Menu_Test3, _T("&Disabled"), _T("disabled item"));
4ccf704a
VZ
156 pItem->SetFont(*wxNORMAL_FONT);
157 file_menu->Append(pItem);
f0f574b0 158 file_menu->Enable(Menu_Test3, false);
4ccf704a
VZ
159
160 file_menu->AppendSeparator();
161
600683ca
MB
162 pItem = new wxMenuItem(file_menu, Menu_Bitmap, _T("&Bell"),
163 _T("check/uncheck me!"), wxITEM_CHECK);
4ccf704a
VZ
164 pItem->SetFont(fontBmp);
165 pItem->SetBitmaps(bmpBell);
166 file_menu->Append(pItem);
167
600683ca
MB
168 pItem = new wxMenuItem(file_menu, Menu_Bitmap2, _T("So&und"),
169 _T("icon changes!"), wxITEM_CHECK);
4ccf704a
VZ
170 pItem->SetFont(fontBmp);
171 pItem->SetBitmaps(bmpSound, bmpNoSound);
172 file_menu->Append(pItem);
173
174 file_menu->AppendSeparator();
175
600683ca 176 pItem = new wxMenuItem(file_menu, Menu_Submenu, _T("&Sub menu"), _T(""),
4ccf704a
VZ
177 wxITEM_CHECK, sub_menu);
178 pItem->SetFont(*wxSWISS_FONT);
179 file_menu->Append(pItem);
180
1bf0f0af 181 file_menu->AppendSeparator();
925e9792 182 pItem = new wxMenuItem(file_menu, Menu_Toggle, _T("&Disable/Enable\tCtrl+D"),
1bf0f0af
VZ
183 _T("enables/disables the About-Item"), wxITEM_NORMAL);
184 pItem->SetFont(*wxNORMAL_FONT);
185 file_menu->Append(pItem);
186
187 // Of course Ctrl+RatherLongAccel will not work in this example:
925e9792 188 pAboutItem = new wxMenuItem(file_menu, Menu_About, _T("&About\tCtrl+RatherLongAccel"),
1bf0f0af
VZ
189 _T("display program information"), wxITEM_NORMAL);
190 pAboutItem->SetBitmap(bmpInfo);
191 pAboutItem->SetDisabledBitmap(bmpInfo_mono);
1bf0f0af
VZ
192 file_menu->Append(pAboutItem);
193
4ccf704a 194 file_menu->AppendSeparator();
f0f574b0
WS
195#endif
196
600683ca 197 pItem = new wxMenuItem(file_menu, Menu_Quit, _T("&Quit"), _T("Normal item"),
2a2a71e3 198 wxITEM_NORMAL);
2a2a71e3 199 file_menu->Append(pItem);
4ccf704a
VZ
200
201 wxMenuBar *menu_bar = new wxMenuBar;
202
600683ca 203 menu_bar->Append(file_menu, _T("&File"));
4ccf704a 204 SetMenuBar(menu_bar);
bbf1f0e5
KB
205}
206
207// main frame constructor
fbfb8bcc 208OwnerDrawnFrame::OwnerDrawnFrame(wxFrame *frame, const wxChar *title,
4ccf704a 209 int x, int y, int w, int h)
f0f574b0 210 : wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
bbf1f0e5 211{
4ccf704a 212 // set the icon
600683ca 213 SetIcon(wxIcon(_T("mondrian")));
bbf1f0e5 214
4ccf704a
VZ
215 // create the menu
216 InitMenu();
bbf1f0e5 217
8520f137 218#if wxUSE_STATUSBAR
4ccf704a
VZ
219 // create the status line
220 const int widths[] = { -1, 60 };
221 CreateStatusBar(2);
222 SetStatusWidths(2, widths);
600683ca 223 SetStatusText(_T("no selection"), 0);
8520f137 224#endif // wxUSE_STATUSBAR
88b0e1c8 225
4ccf704a 226 // make a panel with some controls
f8a0822b 227 wxPanel *pPanel = new wxPanel(this);
bbf1f0e5 228
4ccf704a 229 // check list box
925e9792 230 static const wxChar* aszChoices[] = { _T("Hello"), _T("world"), _T("and"),
600683ca
MB
231 _T("goodbye"), _T("cruel"), _T("world"),
232 _T("-------"), _T("owner-drawn"), _T("listbox") };
bbf1f0e5 233
4ccf704a
VZ
234 wxString *astrChoices = new wxString[WXSIZEOF(aszChoices)];
235 unsigned int ui;
236 for ( ui = 0; ui < WXSIZEOF(aszChoices); ui++ )
237 astrChoices[ui] = aszChoices[ui];
238
239 m_pListBox = new wxCheckListBox
240 (
241 pPanel, // parent
242 Control_Listbox, // control id
243 wxPoint(10, 10), // listbox position
244 wxSize(200, 200), // listbox size
245 WXSIZEOF(aszChoices), // number of strings
246 astrChoices // array of strings
247 );
248
249 delete [] astrChoices;
250
251 for ( ui = 0; ui < WXSIZEOF(aszChoices); ui += 2 )
252 {
f0f574b0 253#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
4ccf704a 254 m_pListBox->GetItem(ui)->SetBackgroundColour(wxColor(200, 200, 200));
f0f574b0 255#endif
4ccf704a
VZ
256 }
257
258 m_pListBox->Check(2);
259
260 // normal (but owner-drawn) listbox
600683ca 261 static const wxChar* aszColors[] = { _T("Red"), _T("Blue"), _T("Pink"),
925e9792 262 _T("Green"), _T("Yellow"),
600683ca 263 _T("Black"), _T("Violet") };
4ccf704a
VZ
264
265 astrChoices = new wxString[WXSIZEOF(aszColors)];
266
267 for ( ui = 0; ui < WXSIZEOF(aszColors); ui++ )
268 {
269 astrChoices[ui] = aszColors[ui];
270 }
271
272 wxListBox *pListBox = new wxListBox
273 (
274 pPanel, // parent
275 Control_Listbox2, // control id
276 wxPoint(220, 10), // listbox position
f8a0822b
VZ
277 wxSize(200, 200), // listbox size
278 WXSIZEOF(aszColors), // number of strings
4ccf704a 279 astrChoices, // array of strings
f8a0822b 280 wxLB_OWNERDRAW // owner-drawn
4ccf704a
VZ
281 );
282
f0f574b0
WS
283#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
284
925e9792 285 struct { unsigned char r, g, b; } aColors[] =
f0f574b0
WS
286 {
287 {255,0,0}, {0,0,255}, {255,128,192},
925e9792 288 {0,255,0}, {255,255,128},
f0f574b0
WS
289 {0,0,0}, {128,0,255}
290 };
291
4ccf704a
VZ
292 for ( ui = 0; ui < WXSIZEOF(aszColors); ui++ )
293 {
294 pListBox->GetItem(ui)->SetTextColour(wxColor(aColors[ui].r,
925e9792 295 aColors[ui].g,
4ccf704a
VZ
296 aColors[ui].b));
297 // yellow on white is horrible...
298 if ( ui == 4 )
299 {
300 pListBox->GetItem(ui)->SetBackgroundColour(wxColor(0, 0, 0));
301 }
4ccf704a
VZ
302 }
303
f0f574b0
WS
304#else
305 wxUnusedVar( pListBox );
306#endif
307
4ccf704a
VZ
308 delete[] astrChoices;
309
f0f574b0 310 Show(true);
bbf1f0e5
KB
311}
312
33c96451 313void OwnerDrawnFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
bbf1f0e5 314{
f0f574b0 315 Close(true);
bbf1f0e5
KB
316}
317
33c96451 318void OwnerDrawnFrame::OnMenuToggle(wxCommandEvent& WXUNUSED(event))
1bf0f0af
VZ
319{
320 // This example shows the use of bitmaps in ownerdrawn menuitems and is not a good
321 // example on how to enable and disable menuitems - this should be done with the help of
322 // EVT_UPDATE_UI and EVT_UPDATE_UI_RANGE !
f0f574b0 323 pAboutItem->Enable( pAboutItem->IsEnabled() ? false : true );
1bf0f0af
VZ
324}
325
33c96451 326void OwnerDrawnFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
bbf1f0e5 327{
4ccf704a 328 wxMessageDialog dialog(this,
600683ca
MB
329 _T("Demo of owner-drawn controls\n"),
330 _T("About wxOwnerDrawn"), wxYES_NO | wxCANCEL);
4ccf704a 331 dialog.ShowModal();
bbf1f0e5
KB
332}
333
334void OwnerDrawnFrame::OnListboxSelect(wxCommandEvent& event)
335{
8520f137 336#if wxUSE_STATUSBAR
4ccf704a
VZ
337 wxString strSelection;
338 unsigned int nSel = event.GetSelection();
600683ca
MB
339 strSelection.Printf(wxT("item %d selected (%schecked)"), nSel,
340 m_pListBox->IsChecked(nSel) ? wxT("") : wxT("not "));
4ccf704a 341 SetStatusText(strSelection);
8520f137
WS
342#else
343 wxUnusedVar(event);
344#endif // wxUSE_STATUSBAR
bbf1f0e5
KB
345}
346
33c96451 347void OwnerDrawnFrame::OnListboxDblClick(wxCommandEvent& WXUNUSED(event))
bbf1f0e5 348{
4ccf704a 349 wxString strSelection;
600683ca
MB
350 strSelection.Printf(wxT("item %d double clicked"),
351 m_pListBox->GetSelection());
4ccf704a
VZ
352 wxMessageDialog dialog(this, strSelection);
353 dialog.ShowModal();
bbf1f0e5
KB
354}
355
356void OwnerDrawnFrame::OnCheckboxToggle(wxCommandEvent& event)
357{
8520f137 358#if wxUSE_STATUSBAR
4ccf704a
VZ
359 wxString strSelection;
360 unsigned int nItem = event.GetInt();
600683ca
MB
361 strSelection.Printf(wxT("item %d was %schecked"), nItem,
362 m_pListBox->IsChecked(nItem) ? wxT("") : wxT("un"));
4ccf704a 363 SetStatusText(strSelection);
8520f137
WS
364#else
365 wxUnusedVar(event);
366#endif // wxUSE_STATUSBAR
88b0e1c8 367}