]> git.saurik.com Git - wxWidgets.git/blob - samples/erase/erase.cpp
Update wxHtmlWindow documentation to refer to wxWebView.
[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/custombgwin.h"
34 #include "wx/dcbuffer.h"
35
36 // ----------------------------------------------------------------------------
37 // resources
38 // ----------------------------------------------------------------------------
39
40 // the application icon
41 #if !defined(__WXMSW__) && !defined(__WXPM__)
42 #include "../sample.xpm"
43 #endif
44
45 // ----------------------------------------------------------------------------
46 // private classes
47 // ----------------------------------------------------------------------------
48
49 class MyApp : public wxApp
50 {
51 public:
52 virtual bool OnInit();
53 };
54
55
56 class MyCanvas : public wxCustomBackgroundWindow<wxScrolledWindow>
57 {
58 public:
59 MyCanvas(wxFrame *parent);
60
61 void UseBuffer(bool useBuffer) { m_useBuffer = useBuffer; Refresh(); }
62 bool UsesBuffer() const { return m_useBuffer; }
63
64 void UseBgBitmap(bool useBgBmp)
65 {
66 m_useBgBmp = useBgBmp;
67 SetBackgroundBitmap(m_useBgBmp ? GetBgBitmap() : wxBitmap());
68
69 Refresh();
70 }
71
72 void EraseBgInPaint(bool erase) { m_eraseBgInPaint = erase; Refresh(); }
73
74 private:
75 void OnPaint( wxPaintEvent &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
101 // use wxMemoryDC in OnPaint()?
102 bool m_useBuffer;
103
104 // use background bitmap?
105 bool m_useBgBmp;
106
107 // erase background in OnPaint()?
108 bool m_eraseBgInPaint;
109
110
111 DECLARE_EVENT_TABLE()
112 };
113
114 class MyFrame : public wxFrame
115 {
116 public:
117 MyFrame();
118
119 private:
120 void OnUseBuffer(wxCommandEvent& event);
121 void OnUseBgBitmap(wxCommandEvent& event);
122 void OnEraseBgInPaint(wxCommandEvent& event);
123 void OnChangeBgStyle(wxCommandEvent& event);
124 void OnQuit(wxCommandEvent& event);
125 void OnAbout(wxCommandEvent& event);
126
127 // we can only use double-buffering with wxBG_STYLE_PAINT
128 void OnUpdateUIUseBuffer(wxUpdateUIEvent& event)
129 {
130 event.Enable( m_canvas->GetBackgroundStyle() == wxBG_STYLE_PAINT );
131 }
132
133 void OnUpdateUIChangeBgStyle(wxUpdateUIEvent& event)
134 {
135 event.Enable( !m_canvas->UsesBuffer() );
136 }
137
138 MyCanvas *m_canvas;
139
140 DECLARE_EVENT_TABLE()
141 };
142
143 class ControlWithTransparency : public wxWindow
144 {
145 public:
146 ControlWithTransparency(wxWindow *parent,
147 const wxPoint& pos,
148 const wxSize& size)
149 : wxWindow(parent, wxID_ANY, pos, size, wxBORDER_NONE)
150 {
151 Connect(wxEVT_PAINT,
152 wxPaintEventHandler(ControlWithTransparency::OnPaint));
153 }
154
155 virtual bool HasTransparentBackground() { return true; }
156
157 private:
158 void OnPaint( wxPaintEvent& WXUNUSED(event) )
159 {
160 wxPaintDC dc(this);
161
162 dc.SetPen(*wxRED_PEN);
163 dc.SetBrush(*wxTRANSPARENT_BRUSH);
164 dc.DrawRectangle(GetClientSize());
165
166 dc.SetTextForeground(*wxBLUE);
167 dc.SetBackgroundMode(wxTRANSPARENT);
168 dc.DrawText("This is custom control with transparency", 0, 2);
169 }
170 };
171
172 // ----------------------------------------------------------------------------
173 // constants
174 // ----------------------------------------------------------------------------
175
176 enum
177 {
178 // menu items
179 Erase_Menu_UseBuffer = 100,
180 Erase_Menu_UseBgBitmap,
181 Erase_Menu_EraseBgInPaint,
182 Erase_Menu_BgStyleErase,
183 Erase_Menu_BgStyleSystem,
184 Erase_Menu_BgStylePaint,
185 Erase_Menu_Exit = wxID_EXIT,
186 Erase_Menu_About = wxID_ABOUT
187 };
188
189
190 // ----------------------------------------------------------------------------
191 // the application class
192 // ----------------------------------------------------------------------------
193
194 IMPLEMENT_APP(MyApp)
195
196 bool MyApp::OnInit()
197 {
198 if ( !wxApp::OnInit() )
199 return false;
200
201 MyFrame *frame = new MyFrame;
202
203 frame->Show(true);
204
205 return true;
206 }
207
208 // ----------------------------------------------------------------------------
209 // main frame
210 // ----------------------------------------------------------------------------
211
212 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
213 EVT_MENU(Erase_Menu_UseBuffer, MyFrame::OnUseBuffer)
214 EVT_MENU(Erase_Menu_UseBgBitmap, MyFrame::OnUseBgBitmap)
215 EVT_MENU(Erase_Menu_EraseBgInPaint, MyFrame::OnEraseBgInPaint)
216 EVT_MENU_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint,
217 MyFrame::OnChangeBgStyle)
218
219 EVT_MENU(Erase_Menu_Exit, MyFrame::OnQuit)
220 EVT_MENU(Erase_Menu_About, MyFrame::OnAbout)
221
222 EVT_UPDATE_UI(Erase_Menu_UseBuffer, MyFrame::OnUpdateUIUseBuffer)
223 EVT_UPDATE_UI_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint,
224 MyFrame::OnUpdateUIChangeBgStyle)
225 END_EVENT_TABLE()
226
227 // frame constructor
228 MyFrame::MyFrame()
229 : wxFrame(NULL, wxID_ANY, "Erase sample",
230 wxPoint(50, 50), wxSize(450, 340))
231 {
232 SetIcon(wxICON(sample));
233
234 wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
235 menuFile->AppendCheckItem(Erase_Menu_UseBuffer, "&Use memory DC\tCtrl-M");
236 menuFile->AppendCheckItem(Erase_Menu_UseBgBitmap,
237 "Use background &bitmap\tCtrl-B");
238 menuFile->AppendCheckItem(Erase_Menu_EraseBgInPaint,
239 "&Erase background in EVT_PAINT\tCtrl-R");
240 menuFile->AppendSeparator();
241 menuFile->AppendRadioItem(Erase_Menu_BgStyleErase,
242 "Use wxBG_STYLE_&ERASE\tCtrl-E");
243 menuFile->AppendRadioItem(Erase_Menu_BgStyleSystem,
244 "Use wxBG_STYLE_&SYSTEM\tCtrl-S");
245 menuFile->AppendRadioItem(Erase_Menu_BgStylePaint,
246 "Use wxBG_STYLE_&PAINT\tCtrl-P");
247 menuFile->AppendSeparator();
248 menuFile->Append(Erase_Menu_Exit, "E&xit\tAlt-X", "Quit this program");
249
250
251 wxMenu *helpMenu = new wxMenu;
252 helpMenu->Append(Erase_Menu_About, "&About...\tCtrl-A", "Show about dialog");
253
254 wxMenuBar *menuBar = new wxMenuBar();
255 menuBar->Append(menuFile, "&File");
256 menuBar->Append(helpMenu, "&Help");
257
258 SetMenuBar(menuBar);
259
260 m_canvas = new MyCanvas( this );
261 }
262
263
264 void MyFrame::OnUseBuffer(wxCommandEvent& event)
265 {
266 m_canvas->UseBuffer(event.IsChecked());
267 }
268
269 void MyFrame::OnUseBgBitmap(wxCommandEvent& event)
270 {
271 m_canvas->UseBgBitmap(event.IsChecked());
272 }
273
274 void MyFrame::OnEraseBgInPaint(wxCommandEvent& event)
275 {
276 m_canvas->EraseBgInPaint(event.IsChecked());
277 }
278
279 void MyFrame::OnChangeBgStyle(wxCommandEvent& event)
280 {
281 int style = wxBG_STYLE_ERASE + event.GetId() - Erase_Menu_BgStyleErase;
282 m_canvas->SetBackgroundStyle(static_cast<wxBackgroundStyle>(style));
283
284 m_canvas->Refresh();
285 }
286
287 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
288 {
289 Close(true);
290 }
291
292 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
293 {
294 wxMessageBox
295 (
296 "This sample shows differences between different background styles "
297 "and how you may draw custom background.\n"
298 "\n"
299 "(c) 1998 Robert Roebling\n"
300 "(c) 2009 Vadim Zeitlin\n",
301 "About Erase Sample",
302 wxOK | wxICON_INFORMATION,
303 this
304 );
305 }
306
307
308 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
309 EVT_PAINT(MyCanvas::OnPaint)
310 EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground)
311 END_EVENT_TABLE()
312
313 MyCanvas::MyCanvas(wxFrame *parent)
314 {
315 Create(parent, wxID_ANY);
316
317 m_useBuffer = false;
318 m_useBgBmp = false;
319 m_eraseBgInPaint = false;
320
321 SetScrollbars( 10, 10, 40, 100, 0, 0 );
322
323 m_bitmap = wxBitmap( wxICON(sample) );
324
325 new wxStaticBitmap( this, wxID_ANY, m_bitmap, wxPoint(80,20) );
326
327 new wxStaticText(this, wxID_ANY,
328 "Left bitmap is a wxStaticBitmap,\n"
329 "right one drawn directly",
330 wxPoint(150, 20));
331
332 new ControlWithTransparency(this, wxPoint(65, 125), wxSize(300, 22));
333
334 SetFocusIgnoringChildren();
335 SetBackgroundColour(*wxCYAN);
336 }
337
338 void MyCanvas::DoPaint(wxDC& dc)
339 {
340 if ( m_eraseBgInPaint )
341 {
342 dc.SetBackground(*wxLIGHT_GREY);
343 dc.Clear();
344
345 dc.DrawText("Background erased in OnPaint", 65, 110);
346 }
347 else if ( GetBackgroundStyle() == wxBG_STYLE_PAINT )
348 {
349 dc.SetTextForeground(*wxRED);
350 dc.DrawText("You must enable erasing background in OnPaint to avoid "
351 "display corruption", 65, 110);
352 }
353
354 dc.DrawBitmap( m_bitmap, 20, 20, true );
355
356 dc.SetTextForeground(*wxRED);
357 dc.DrawText("This text is drawn from OnPaint", 65, 65);
358 }
359
360 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
361 {
362 if ( m_useBuffer )
363 {
364 wxAutoBufferedPaintDC dc(this);
365 PrepareDC(dc);
366
367 DoPaint(dc);
368 }
369 else
370 {
371 wxPaintDC dc(this);
372 PrepareDC(dc);
373
374 DoPaint(dc);
375 }
376 }
377
378 void MyCanvas::OnEraseBackground( wxEraseEvent& event )
379 {
380 // We must not erase the background ourselves if we asked wxPanel to erase
381 // it using a background bitmap.
382 if ( m_useBgBmp )
383 {
384 event.Skip();
385 return;
386 }
387
388 wxASSERT_MSG
389 (
390 GetBackgroundStyle() == wxBG_STYLE_ERASE,
391 "shouldn't be called unless background style is \"erase\""
392 );
393
394 wxDC& dc = *event.GetDC();
395 dc.SetPen(*wxGREEN_PEN);
396
397 PrepareDC( dc );
398
399 // clear any junk currently displayed
400 dc.Clear();
401
402 const wxSize size = GetClientSize();
403 for ( int x = 0; x < size.x; x += 15 )
404 {
405 dc.DrawLine(x, 0, x, size.y);
406 }
407
408 for ( int y = 0; y < size.y; y += 15 )
409 {
410 dc.DrawLine(0, y, size.x, y);
411 }
412
413 dc.SetTextForeground(*wxRED);
414 dc.SetBackgroundMode(wxSOLID);
415 dc.DrawText("This text is drawn from OnEraseBackground", 60, 160);
416 }
417