Remove keyboard-related code from the erase sample.
[wxWidgets.git] / samples / erase / erase.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/erase/erase.cpp
3 // Purpose: Erase wxWidgets sample
4 // Author: Robert Roebling, Vadim Zeitlin
5 // Created: 04/01/98
6 // RCS-ID: $Id$
7 // Copyright: (c) 1998 Robert Roebling
8 // (c) 2009 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
29 #ifndef WX_PRECOMP
30 #include "wx/wx.h"
31 #endif
32
33 #include "wx/dcbuffer.h"
34
35 // ----------------------------------------------------------------------------
36 // resources
37 // ----------------------------------------------------------------------------
38
39 // the application icon
40 #if !defined(__WXMSW__) && !defined(__WXPM__)
41 #include "../sample.xpm"
42 #endif
43
44 // ----------------------------------------------------------------------------
45 // private classes
46 // ----------------------------------------------------------------------------
47
48 class MyApp : public wxApp
49 {
50 public:
51 virtual bool OnInit();
52 };
53
54
55 class MyCanvas : public wxScrolledWindow
56 {
57 public:
58 MyCanvas(wxFrame *parent);
59
60 void UseBuffer(bool useBuffer) { m_useBuffer = useBuffer; Refresh(); }
61 bool UsesBuffer() const { return m_useBuffer; }
62
63 void UseBgBitmap(bool useBgBmp)
64 {
65 m_useBgBmp = useBgBmp;
66 SetBackgroundBitmap(m_useBgBmp ? GetBgBitmap() : wxBitmap());
67
68 Refresh();
69 }
70
71 void EraseBgInPaint(bool erase) { m_eraseBgInPaint = erase; Refresh(); }
72
73 private:
74 void OnPaint( wxPaintEvent &event );
75 void OnEraseBackground( wxEraseEvent &event );
76
77 void DoPaint(wxDC& dc);
78
79 // Create an easily recognizable background bitmap.
80 static wxBitmap GetBgBitmap()
81 {
82 static const int BMP_SIZE = 40;
83
84 wxBitmap bmp(BMP_SIZE, BMP_SIZE);
85 wxMemoryDC dc(bmp);
86 dc.SetBackground(*wxCYAN);
87 dc.Clear();
88
89 dc.SetPen(*wxBLUE_PEN);
90 dc.DrawLine(0, BMP_SIZE/2, BMP_SIZE/2, 0);
91 dc.DrawLine(BMP_SIZE/2, 0, BMP_SIZE, BMP_SIZE/2);
92 dc.DrawLine(BMP_SIZE, BMP_SIZE/2, BMP_SIZE/2, BMP_SIZE);
93 dc.DrawLine(BMP_SIZE/2, BMP_SIZE, 0, BMP_SIZE/2);
94
95 return bmp;
96 }
97
98 wxBitmap m_bitmap;
99
100 // use wxMemoryDC in OnPaint()?
101 bool m_useBuffer;
102
103 // use background bitmap?
104 bool m_useBgBmp;
105
106 // erase background in OnPaint()?
107 bool m_eraseBgInPaint;
108
109
110 DECLARE_EVENT_TABLE()
111 };
112
113 class MyFrame : public wxFrame
114 {
115 public:
116 MyFrame();
117
118 private:
119 void OnUseBuffer(wxCommandEvent& event);
120 void OnUseBgBitmap(wxCommandEvent& event);
121 void OnEraseBgInPaint(wxCommandEvent& event);
122 void OnChangeBgStyle(wxCommandEvent& event);
123 void OnQuit(wxCommandEvent& event);
124 void OnAbout(wxCommandEvent& event);
125
126 // we can only use double-buffering with wxBG_STYLE_PAINT
127 void OnUpdateUIUseBuffer(wxUpdateUIEvent& event)
128 {
129 event.Enable( m_canvas->GetBackgroundStyle() == wxBG_STYLE_PAINT );
130 }
131
132 void OnUpdateUIChangeBgStyle(wxUpdateUIEvent& event)
133 {
134 event.Enable( !m_canvas->UsesBuffer() );
135 }
136
137 MyCanvas *m_canvas;
138
139 DECLARE_EVENT_TABLE()
140 };
141
142
143 // ----------------------------------------------------------------------------
144 // constants
145 // ----------------------------------------------------------------------------
146
147 enum
148 {
149 // menu items
150 Erase_Menu_UseBuffer = 100,
151 Erase_Menu_UseBgBitmap,
152 Erase_Menu_EraseBgInPaint,
153 Erase_Menu_BgStyleErase,
154 Erase_Menu_BgStyleSystem,
155 Erase_Menu_BgStylePaint,
156 Erase_Menu_Exit = wxID_EXIT,
157 Erase_Menu_About = wxID_ABOUT
158 };
159
160
161 // ----------------------------------------------------------------------------
162 // the application class
163 // ----------------------------------------------------------------------------
164
165 IMPLEMENT_APP(MyApp)
166
167 bool MyApp::OnInit()
168 {
169 if ( !wxApp::OnInit() )
170 return false;
171
172 MyFrame *frame = new MyFrame;
173
174 frame->Show(true);
175
176 return true;
177 }
178
179 // ----------------------------------------------------------------------------
180 // main frame
181 // ----------------------------------------------------------------------------
182
183 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
184 EVT_MENU(Erase_Menu_UseBuffer, MyFrame::OnUseBuffer)
185 EVT_MENU(Erase_Menu_UseBgBitmap, MyFrame::OnUseBgBitmap)
186 EVT_MENU(Erase_Menu_EraseBgInPaint, MyFrame::OnEraseBgInPaint)
187 EVT_MENU_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint,
188 MyFrame::OnChangeBgStyle)
189
190 EVT_MENU(Erase_Menu_Exit, MyFrame::OnQuit)
191 EVT_MENU(Erase_Menu_About, MyFrame::OnAbout)
192
193 EVT_UPDATE_UI(Erase_Menu_UseBuffer, MyFrame::OnUpdateUIUseBuffer)
194 EVT_UPDATE_UI_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint,
195 MyFrame::OnUpdateUIChangeBgStyle)
196 END_EVENT_TABLE()
197
198 // frame constructor
199 MyFrame::MyFrame()
200 : wxFrame(NULL, wxID_ANY, "Erase sample",
201 wxPoint(50, 50), wxSize(450, 340))
202 {
203 SetIcon(wxICON(sample));
204
205 wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
206 menuFile->AppendCheckItem(Erase_Menu_UseBuffer, "&Use memory DC\tCtrl-M");
207 menuFile->AppendCheckItem(Erase_Menu_UseBgBitmap,
208 "Use background &bitmap\tCtrl-B");
209 menuFile->AppendCheckItem(Erase_Menu_EraseBgInPaint,
210 "&Erase background in EVT_PAINT\tCtrl-R");
211 menuFile->AppendSeparator();
212 menuFile->AppendRadioItem(Erase_Menu_BgStyleErase,
213 "Use wxBG_STYLE_&ERASE\tCtrl-E");
214 menuFile->AppendRadioItem(Erase_Menu_BgStyleSystem,
215 "Use wxBG_STYLE_&SYSTEM\tCtrl-S");
216 menuFile->AppendRadioItem(Erase_Menu_BgStylePaint,
217 "Use wxBG_STYLE_&PAINT\tCtrl-P");
218 menuFile->AppendSeparator();
219 menuFile->Append(Erase_Menu_Exit, "E&xit\tAlt-X", "Quit this program");
220
221
222 wxMenu *helpMenu = new wxMenu;
223 helpMenu->Append(Erase_Menu_About, "&About...\tCtrl-A", "Show about dialog");
224
225 wxMenuBar *menuBar = new wxMenuBar();
226 menuBar->Append(menuFile, "&File");
227 menuBar->Append(helpMenu, "&Help");
228
229 SetMenuBar(menuBar);
230
231 m_canvas = new MyCanvas( this );
232 }
233
234
235 void MyFrame::OnUseBuffer(wxCommandEvent& event)
236 {
237 m_canvas->UseBuffer(event.IsChecked());
238 }
239
240 void MyFrame::OnUseBgBitmap(wxCommandEvent& event)
241 {
242 m_canvas->UseBgBitmap(event.IsChecked());
243 }
244
245 void MyFrame::OnEraseBgInPaint(wxCommandEvent& event)
246 {
247 m_canvas->EraseBgInPaint(event.IsChecked());
248 }
249
250 void MyFrame::OnChangeBgStyle(wxCommandEvent& event)
251 {
252 int style = wxBG_STYLE_ERASE + event.GetId() - Erase_Menu_BgStyleErase;
253 m_canvas->SetBackgroundStyle(static_cast<wxBackgroundStyle>(style));
254
255 m_canvas->Refresh();
256 }
257
258 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
259 {
260 Close(true);
261 }
262
263 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
264 {
265 wxMessageBox
266 (
267 "This sample shows differences between different background styles "
268 "and how you may draw custom background.\n"
269 "\n"
270 "(c) 1998 Robert Roebling\n"
271 "(c) 2009 Vadim Zeitlin\n",
272 "About Erase Sample",
273 wxOK | wxICON_INFORMATION,
274 this
275 );
276 }
277
278
279 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
280 EVT_PAINT(MyCanvas::OnPaint)
281 EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground)
282 END_EVENT_TABLE()
283
284 MyCanvas::MyCanvas(wxFrame *parent)
285 : wxScrolledWindow(parent, wxID_ANY)
286 {
287 m_useBuffer = false;
288 m_useBgBmp = false;
289 m_eraseBgInPaint = false;
290
291 SetScrollbars( 10, 10, 40, 100, 0, 0 );
292
293 m_bitmap = wxBitmap( wxICON(sample) );
294
295 new wxStaticBitmap( this, wxID_ANY, m_bitmap, wxPoint(80,20) );
296
297 new wxStaticText(this, wxID_ANY,
298 "Left bitmap is a wxStaticBitmap,\n"
299 "right one drawn directly",
300 wxPoint(150, 20));
301
302 SetFocusIgnoringChildren();
303 SetBackgroundColour(*wxCYAN);
304 }
305
306 void MyCanvas::DoPaint(wxDC& dc)
307 {
308 if ( m_eraseBgInPaint )
309 {
310 dc.SetBackground(*wxLIGHT_GREY);
311 dc.Clear();
312
313 dc.DrawText("Background erased in OnPaint", 65, 110);
314 }
315 else if ( GetBackgroundStyle() == wxBG_STYLE_PAINT )
316 {
317 dc.SetTextForeground(*wxRED);
318 dc.DrawText("You must enable erasing background in OnPaint to avoid "
319 "display corruption", 65, 110);
320 }
321
322 dc.DrawBitmap( m_bitmap, 20, 20, true );
323
324 dc.SetTextForeground(*wxRED);
325 dc.DrawText("This text is drawn from OnPaint", 65, 65);
326 }
327
328 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
329 {
330 if ( m_useBuffer )
331 {
332 wxAutoBufferedPaintDC dc(this);
333 PrepareDC(dc);
334
335 DoPaint(dc);
336 }
337 else
338 {
339 wxPaintDC dc(this);
340 PrepareDC(dc);
341
342 DoPaint(dc);
343 }
344 }
345
346 void MyCanvas::OnEraseBackground( wxEraseEvent& event )
347 {
348 // We must not erase the background ourselves if we asked wxPanel to erase
349 // it using a background bitmap.
350 if ( m_useBgBmp )
351 {
352 event.Skip();
353 return;
354 }
355
356 wxASSERT_MSG
357 (
358 GetBackgroundStyle() == wxBG_STYLE_ERASE,
359 "shouldn't be called unless background style is \"erase\""
360 );
361
362 wxDC& dc = *event.GetDC();
363 dc.SetPen(*wxGREEN_PEN);
364
365 PrepareDC( dc );
366
367 // clear any junk currently displayed
368 dc.Clear();
369
370 const wxSize size = GetClientSize();
371 for ( int x = 0; x < size.x; x += 15 )
372 {
373 dc.DrawLine(x, 0, x, size.y);
374 }
375
376 for ( int y = 0; y < size.y; y += 15 )
377 {
378 dc.DrawLine(0, y, size.x, y);
379 }
380
381 dc.SetTextForeground(*wxRED);
382 dc.SetBackgroundMode(wxSOLID);
383 dc.DrawText("This text is drawn from OnEraseBackground", 60, 160);
384 }
385