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