Add wxPanel::SetBackgroundBitmap().
[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 OnChar( wxKeyEvent &event );
76 void OnEraseBackground( wxEraseEvent &event );
77
78 void DoPaint(wxDC& dc);
79
80 // Create an easily recognizable background bitmap.
81 static wxBitmap GetBgBitmap()
82 {
83 static const int BMP_SIZE = 40;
84
85 wxBitmap bmp(BMP_SIZE, BMP_SIZE);
86 wxMemoryDC dc(bmp);
87 dc.SetBackground(*wxCYAN);
88 dc.Clear();
89
90 dc.SetPen(*wxBLUE_PEN);
91 dc.DrawLine(0, BMP_SIZE/2, BMP_SIZE/2, 0);
92 dc.DrawLine(BMP_SIZE/2, 0, BMP_SIZE, BMP_SIZE/2);
93 dc.DrawLine(BMP_SIZE, BMP_SIZE/2, BMP_SIZE/2, BMP_SIZE);
94 dc.DrawLine(BMP_SIZE/2, BMP_SIZE, 0, BMP_SIZE/2);
95
96 return bmp;
97 }
98
99 wxBitmap m_bitmap;
100 wxString m_text;
101
102 // use wxMemoryDC in OnPaint()?
103 bool m_useBuffer;
104
105 // use background bitmap?
106 bool m_useBgBmp;
107
108 // erase background in OnPaint()?
109 bool m_eraseBgInPaint;
110
111
112 DECLARE_EVENT_TABLE()
113 };
114
115 class MyFrame : public wxFrame
116 {
117 public:
118 MyFrame();
119
120 private:
121 void OnUseBuffer(wxCommandEvent& event);
122 void OnUseBgBitmap(wxCommandEvent& event);
123 void OnEraseBgInPaint(wxCommandEvent& event);
124 void OnChangeBgStyle(wxCommandEvent& event);
125 void OnQuit(wxCommandEvent& event);
126 void OnAbout(wxCommandEvent& event);
127
128 // we can only use double-buffering with wxBG_STYLE_PAINT
129 void OnUpdateUIUseBuffer(wxUpdateUIEvent& event)
130 {
131 event.Enable( m_canvas->GetBackgroundStyle() == wxBG_STYLE_PAINT );
132 }
133
134 void OnUpdateUIChangeBgStyle(wxUpdateUIEvent& event)
135 {
136 event.Enable( !m_canvas->UsesBuffer() );
137 }
138
139 MyCanvas *m_canvas;
140
141 DECLARE_EVENT_TABLE()
142 };
143
144
145 // ----------------------------------------------------------------------------
146 // constants
147 // ----------------------------------------------------------------------------
148
149 enum
150 {
151 // menu items
152 Erase_Menu_UseBuffer = 100,
153 Erase_Menu_UseBgBitmap,
154 Erase_Menu_EraseBgInPaint,
155 Erase_Menu_BgStyleErase,
156 Erase_Menu_BgStyleSystem,
157 Erase_Menu_BgStylePaint,
158 Erase_Menu_Exit = wxID_EXIT,
159 Erase_Menu_About = wxID_ABOUT
160 };
161
162
163 // ----------------------------------------------------------------------------
164 // the application class
165 // ----------------------------------------------------------------------------
166
167 IMPLEMENT_APP(MyApp)
168
169 bool MyApp::OnInit()
170 {
171 if ( !wxApp::OnInit() )
172 return false;
173
174 MyFrame *frame = new MyFrame;
175
176 frame->Show(true);
177
178 return true;
179 }
180
181 // ----------------------------------------------------------------------------
182 // main frame
183 // ----------------------------------------------------------------------------
184
185 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
186 EVT_MENU(Erase_Menu_UseBuffer, MyFrame::OnUseBuffer)
187 EVT_MENU(Erase_Menu_UseBgBitmap, MyFrame::OnUseBgBitmap)
188 EVT_MENU(Erase_Menu_EraseBgInPaint, MyFrame::OnEraseBgInPaint)
189 EVT_MENU_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint,
190 MyFrame::OnChangeBgStyle)
191
192 EVT_MENU(Erase_Menu_Exit, MyFrame::OnQuit)
193 EVT_MENU(Erase_Menu_About, MyFrame::OnAbout)
194
195 EVT_UPDATE_UI(Erase_Menu_UseBuffer, MyFrame::OnUpdateUIUseBuffer)
196 EVT_UPDATE_UI_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint,
197 MyFrame::OnUpdateUIChangeBgStyle)
198 END_EVENT_TABLE()
199
200 // frame constructor
201 MyFrame::MyFrame()
202 : wxFrame(NULL, wxID_ANY, "Erase sample",
203 wxPoint(50, 50), wxSize(450, 340))
204 {
205 SetIcon(wxICON(sample));
206
207 wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
208 menuFile->AppendCheckItem(Erase_Menu_UseBuffer, "&Use memory DC\tCtrl-M");
209 menuFile->AppendCheckItem(Erase_Menu_UseBgBitmap,
210 "Use background &bitmap\tCtrl-B");
211 menuFile->AppendCheckItem(Erase_Menu_EraseBgInPaint,
212 "&Erase background in EVT_PAINT\tCtrl-R");
213 menuFile->AppendSeparator();
214 menuFile->AppendRadioItem(Erase_Menu_BgStyleErase,
215 "Use wxBG_STYLE_&ERASE\tCtrl-E");
216 menuFile->AppendRadioItem(Erase_Menu_BgStyleSystem,
217 "Use wxBG_STYLE_&SYSTEM\tCtrl-S");
218 menuFile->AppendRadioItem(Erase_Menu_BgStylePaint,
219 "Use wxBG_STYLE_&PAINT\tCtrl-P");
220 menuFile->AppendSeparator();
221 menuFile->Append(Erase_Menu_Exit, "E&xit\tAlt-X", "Quit this program");
222
223
224 wxMenu *helpMenu = new wxMenu;
225 helpMenu->Append(Erase_Menu_About, "&About...\tCtrl-A", "Show about dialog");
226
227 wxMenuBar *menuBar = new wxMenuBar();
228 menuBar->Append(menuFile, "&File");
229 menuBar->Append(helpMenu, "&Help");
230
231 SetMenuBar(menuBar);
232
233 m_canvas = new MyCanvas( this );
234 }
235
236
237 void MyFrame::OnUseBuffer(wxCommandEvent& event)
238 {
239 m_canvas->UseBuffer(event.IsChecked());
240 }
241
242 void MyFrame::OnUseBgBitmap(wxCommandEvent& event)
243 {
244 m_canvas->UseBgBitmap(event.IsChecked());
245 }
246
247 void MyFrame::OnEraseBgInPaint(wxCommandEvent& event)
248 {
249 m_canvas->EraseBgInPaint(event.IsChecked());
250 }
251
252 void MyFrame::OnChangeBgStyle(wxCommandEvent& event)
253 {
254 int style = wxBG_STYLE_ERASE + event.GetId() - Erase_Menu_BgStyleErase;
255 m_canvas->SetBackgroundStyle(static_cast<wxBackgroundStyle>(style));
256
257 m_canvas->Refresh();
258 }
259
260 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
261 {
262 Close(true);
263 }
264
265 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
266 {
267 wxMessageBox
268 (
269 "This sample shows differences between different background styles "
270 "and how you may draw custom background.\n"
271 "\n"
272 "(c) 1998 Robert Roebling\n"
273 "(c) 2009 Vadim Zeitlin\n",
274 "About Erase Sample",
275 wxOK | wxICON_INFORMATION,
276 this
277 );
278 }
279
280
281 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
282 EVT_PAINT(MyCanvas::OnPaint)
283 EVT_CHAR(MyCanvas::OnChar)
284 EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground)
285 END_EVENT_TABLE()
286
287 MyCanvas::MyCanvas(wxFrame *parent)
288 : wxScrolledWindow(parent, wxID_ANY)
289 {
290 m_useBuffer = false;
291 m_useBgBmp = false;
292 m_eraseBgInPaint = false;
293
294 SetScrollbars( 10, 10, 40, 100, 0, 0 );
295
296 m_bitmap = wxBitmap( wxICON(sample) );
297
298 new wxStaticBitmap( this, wxID_ANY, m_bitmap, wxPoint(80,20) );
299
300 new wxStaticText(this, wxID_ANY,
301 "Left bitmap is a wxStaticBitmap,\n"
302 "right one drawn directly",
303 wxPoint(150, 20));
304
305 SetFocusIgnoringChildren();
306 SetBackgroundColour(*wxCYAN);
307 }
308
309 void MyCanvas::OnChar( wxKeyEvent &event )
310 {
311 #if wxUSE_UNICODE
312 if (event.m_uniChar)
313 {
314 m_text += event.m_uniChar;
315 Refresh();
316 return;
317 }
318 #endif
319
320 // some test cases
321 switch (event.m_keyCode)
322 {
323 case WXK_UP: m_text += wxT( "<UP>" ); break;
324 case WXK_LEFT: m_text += wxT( "<LEFT>" ); break;
325 case WXK_RIGHT: m_text += wxT( "<RIGHT>" ); break;
326 case WXK_DOWN: m_text += wxT( "<DOWN>" ); break;
327 case WXK_RETURN: m_text += wxT( "<ENTER>" ); break;
328 default: m_text += (wxChar)event.m_keyCode; break;
329 }
330 }
331
332 void MyCanvas::DoPaint(wxDC& dc)
333 {
334 if ( m_eraseBgInPaint )
335 {
336 dc.SetBackground(*wxLIGHT_GREY);
337 dc.Clear();
338
339 dc.DrawText("Background erased in OnPaint", 65, 110);
340 }
341 else if ( GetBackgroundStyle() == wxBG_STYLE_PAINT )
342 {
343 dc.SetTextForeground(*wxRED);
344 dc.DrawText("You must enable erasing background in OnPaint to avoid "
345 "display corruption", 65, 110);
346 }
347
348 dc.SetBrush( *wxBLACK_BRUSH );
349 dc.DrawRectangle( 10,10,60,50 );
350
351 dc.DrawBitmap( m_bitmap, 20, 20, true );
352
353 dc.SetTextForeground(*wxRED);
354 dc.DrawText("This text is drawn from OnPaint", 65, 65);
355
356 wxString tmp;
357 tmp.Printf("Hit any key to display more text: %s", m_text);
358
359 int w,h;
360 dc.GetTextExtent( tmp, &w, &h );
361 dc.DrawRectangle( 65, 85, w, h );
362 dc.DrawText( tmp, 65, 85 );
363 }
364
365 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
366 {
367 if ( m_useBuffer )
368 {
369 wxAutoBufferedPaintDC dc(this);
370 PrepareDC(dc);
371
372 DoPaint(dc);
373 }
374 else
375 {
376 wxPaintDC dc(this);
377 PrepareDC(dc);
378
379 DoPaint(dc);
380 }
381 }
382
383 void MyCanvas::OnEraseBackground( wxEraseEvent& event )
384 {
385 // We must not erase the background ourselves if we asked wxPanel to erase
386 // it using a background bitmap.
387 if ( m_useBgBmp )
388 {
389 event.Skip();
390 return;
391 }
392
393 wxASSERT_MSG
394 (
395 GetBackgroundStyle() == wxBG_STYLE_ERASE,
396 "shouldn't be called unless background style is \"erase\""
397 );
398
399 wxDC& dc = *event.GetDC();
400 dc.SetPen(*wxGREEN_PEN);
401
402 PrepareDC( dc );
403
404 // clear any junk currently displayed
405 dc.Clear();
406
407 const wxSize size = GetClientSize();
408 for ( int x = 0; x < size.x; x += 15 )
409 {
410 dc.DrawLine(x, 0, x, size.y);
411 }
412
413 for ( int y = 0; y < size.y; y += 15 )
414 {
415 dc.DrawLine(0, y, size.x, y);
416 }
417
418 dc.SetTextForeground(*wxRED);
419 dc.SetBackgroundMode(wxSOLID);
420 dc.DrawText("This text is drawn from OnEraseBackground", 60, 160);
421 }
422