]> git.saurik.com Git - wxWidgets.git/blame - samples/erase/erase.cpp
Exclude zlib 3rd party sources from our checks too.
[wxWidgets.git] / samples / erase / erase.cpp
CommitLineData
33611ebb 1/////////////////////////////////////////////////////////////////////////////
9c61c5b0 2// Name: samples/erase/erase.cpp
be5a51fb 3// Purpose: Erase wxWidgets sample
9c61c5b0 4// Author: Robert Roebling, Vadim Zeitlin
33611ebb 5// Created: 04/01/98
9c61c5b0
VZ
6// Copyright: (c) 1998 Robert Roebling
7// (c) 2009 Vadim Zeitlin
33611ebb
RR
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
33611ebb
RR
19// For compilers that support precompilation, includes "wx/wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26// for all others, include the necessary headers (this file is usually all you
be5a51fb 27// need because it includes almost all "standard" wxWidgets headers)
33611ebb
RR
28#ifndef WX_PRECOMP
29 #include "wx/wx.h"
30#endif
31
bbcf2821 32#include "wx/custombgwin.h"
9c61c5b0 33#include "wx/dcbuffer.h"
14421681 34#include "wx/artprov.h"
9c61c5b0 35
33611ebb
RR
36// ----------------------------------------------------------------------------
37// resources
38// ----------------------------------------------------------------------------
3cb332c1 39
33611ebb 40// the application icon
e7092398 41#ifndef wxHAS_IMAGES_IN_RESOURCES
3cb332c1 42 #include "../sample.xpm"
33611ebb
RR
43#endif
44
45// ----------------------------------------------------------------------------
46// private classes
47// ----------------------------------------------------------------------------
48
49class MyApp : public wxApp
50{
51public:
52 virtual bool OnInit();
53};
54
55
bbcf2821 56class MyCanvas : public wxCustomBackgroundWindow<wxScrolledWindow>
33611ebb
RR
57{
58public:
9c61c5b0 59 MyCanvas(wxFrame *parent);
33611ebb 60
79c5fe4b 61 void UseBuffer(bool useBuffer) { m_useBuffer = useBuffer; Refresh(); }
9c61c5b0 62 bool UsesBuffer() const { return m_useBuffer; }
33611ebb 63
cd95f7e6
VZ
64 void UseBgBitmap(bool useBgBmp)
65 {
66 m_useBgBmp = useBgBmp;
67 SetBackgroundBitmap(m_useBgBmp ? GetBgBitmap() : wxBitmap());
68
69 Refresh();
70 }
71
c753eb92
VZ
72 void EraseBgInPaint(bool erase) { m_eraseBgInPaint = erase; Refresh(); }
73
33611ebb 74private:
33611ebb
RR
75 void OnPaint( wxPaintEvent &event );
76 void OnEraseBackground( wxEraseEvent &event );
98dd66cf 77
79c5fe4b
VZ
78 void DoPaint(wxDC& dc);
79
cd95f7e6
VZ
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 }
79c5fe4b 98
b5a49d4c 99 wxBitmap m_bitmap;
33611ebb 100
79c5fe4b
VZ
101 // use wxMemoryDC in OnPaint()?
102 bool m_useBuffer;
103
cd95f7e6
VZ
104 // use background bitmap?
105 bool m_useBgBmp;
106
c753eb92
VZ
107 // erase background in OnPaint()?
108 bool m_eraseBgInPaint;
109
b18eb01c 110
79c5fe4b
VZ
111 DECLARE_EVENT_TABLE()
112};
113
114class MyFrame : public wxFrame
115{
116public:
117 MyFrame();
118
9c61c5b0 119private:
79c5fe4b 120 void OnUseBuffer(wxCommandEvent& event);
cd95f7e6 121 void OnUseBgBitmap(wxCommandEvent& event);
c753eb92 122 void OnEraseBgInPaint(wxCommandEvent& event);
9c61c5b0 123 void OnChangeBgStyle(wxCommandEvent& event);
79c5fe4b
VZ
124 void OnQuit(wxCommandEvent& event);
125 void OnAbout(wxCommandEvent& event);
126
9c61c5b0
VZ
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
79c5fe4b
VZ
138 MyCanvas *m_canvas;
139
33611ebb
RR
140 DECLARE_EVENT_TABLE()
141};
142
ab438739
VZ
143class ControlWithTransparency : public wxWindow
144{
145public:
146 ControlWithTransparency(wxWindow *parent,
147 const wxPoint& pos,
148 const wxSize& size)
ab438739 149 {
14421681
VZ
150 wxString reason;
151 if ( parent->IsTransparentBackgroundSupported(&reason) )
152 {
153 SetBackgroundStyle (wxBG_STYLE_TRANSPARENT);
154 m_message = "This is custom control with transparency";
155 }
156 else
157 {
158 m_message = "Transparency not supported, check tooltip.";
159 }
160
161 Create (parent, wxID_ANY, pos, size, wxBORDER_NONE);
ab438739
VZ
162 Connect(wxEVT_PAINT,
163 wxPaintEventHandler(ControlWithTransparency::OnPaint));
ab438739 164
14421681
VZ
165 if ( !reason.empty() )
166 {
167 // This can be only done now, after creating the window.
168 SetToolTip(reason);
169 }
170 }
ab438739
VZ
171
172private:
173 void OnPaint( wxPaintEvent& WXUNUSED(event) )
174 {
175 wxPaintDC dc(this);
176
177 dc.SetPen(*wxRED_PEN);
178 dc.SetBrush(*wxTRANSPARENT_BRUSH);
179 dc.DrawRectangle(GetClientSize());
180
181 dc.SetTextForeground(*wxBLUE);
182 dc.SetBackgroundMode(wxTRANSPARENT);
14421681
VZ
183 dc.DrawText(m_message, 0, 2);
184
185 // Draw some bitmap/icon to ensure transparent bitmaps are indeed
186 // transparent on transparent windows
187 wxBitmap bmp(wxArtProvider::GetBitmap(wxART_WARNING, wxART_MENU));
188 wxIcon icon(wxArtProvider::GetIcon(wxART_GOTO_LAST, wxART_MENU));
189 dc.DrawBitmap (bmp, GetSize().x - 1 - bmp.GetWidth(), 2);
190 dc.DrawIcon(icon, GetSize().x - 1 - bmp.GetWidth()-icon.GetWidth(), 2);
ab438739 191 }
14421681
VZ
192
193 wxString m_message;
ab438739 194};
79c5fe4b 195
33611ebb
RR
196// ----------------------------------------------------------------------------
197// constants
198// ----------------------------------------------------------------------------
199
200enum
201{
202 // menu items
79c5fe4b 203 Erase_Menu_UseBuffer = 100,
cd95f7e6 204 Erase_Menu_UseBgBitmap,
c753eb92 205 Erase_Menu_EraseBgInPaint,
9c61c5b0
VZ
206 Erase_Menu_BgStyleErase,
207 Erase_Menu_BgStyleSystem,
208 Erase_Menu_BgStylePaint,
79c5fe4b
VZ
209 Erase_Menu_Exit = wxID_EXIT,
210 Erase_Menu_About = wxID_ABOUT
33611ebb
RR
211};
212
213
214// ----------------------------------------------------------------------------
215// the application class
216// ----------------------------------------------------------------------------
217
218IMPLEMENT_APP(MyApp)
219
220bool MyApp::OnInit()
221{
45e6e6f8
VZ
222 if ( !wxApp::OnInit() )
223 return false;
224
79c5fe4b 225 MyFrame *frame = new MyFrame;
33611ebb 226
07850a49 227 frame->Show(true);
98dd66cf 228
07850a49 229 return true;
33611ebb
RR
230}
231
232// ----------------------------------------------------------------------------
233// main frame
234// ----------------------------------------------------------------------------
235
236BEGIN_EVENT_TABLE(MyFrame, wxFrame)
c753eb92 237 EVT_MENU(Erase_Menu_UseBuffer, MyFrame::OnUseBuffer)
cd95f7e6 238 EVT_MENU(Erase_Menu_UseBgBitmap, MyFrame::OnUseBgBitmap)
c753eb92 239 EVT_MENU(Erase_Menu_EraseBgInPaint, MyFrame::OnEraseBgInPaint)
9c61c5b0
VZ
240 EVT_MENU_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint,
241 MyFrame::OnChangeBgStyle)
242
79c5fe4b
VZ
243 EVT_MENU(Erase_Menu_Exit, MyFrame::OnQuit)
244 EVT_MENU(Erase_Menu_About, MyFrame::OnAbout)
9c61c5b0
VZ
245
246 EVT_UPDATE_UI(Erase_Menu_UseBuffer, MyFrame::OnUpdateUIUseBuffer)
247 EVT_UPDATE_UI_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint,
248 MyFrame::OnUpdateUIChangeBgStyle)
33611ebb
RR
249END_EVENT_TABLE()
250
251// frame constructor
79c5fe4b 252MyFrame::MyFrame()
9c61c5b0 253 : wxFrame(NULL, wxID_ANY, "Erase sample",
79c5fe4b 254 wxPoint(50, 50), wxSize(450, 340))
33611ebb 255{
3cb332c1 256 SetIcon(wxICON(sample));
33611ebb 257
9c61c5b0
VZ
258 wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
259 menuFile->AppendCheckItem(Erase_Menu_UseBuffer, "&Use memory DC\tCtrl-M");
cd95f7e6
VZ
260 menuFile->AppendCheckItem(Erase_Menu_UseBgBitmap,
261 "Use background &bitmap\tCtrl-B");
c753eb92
VZ
262 menuFile->AppendCheckItem(Erase_Menu_EraseBgInPaint,
263 "&Erase background in EVT_PAINT\tCtrl-R");
9c61c5b0
VZ
264 menuFile->AppendSeparator();
265 menuFile->AppendRadioItem(Erase_Menu_BgStyleErase,
266 "Use wxBG_STYLE_&ERASE\tCtrl-E");
267 menuFile->AppendRadioItem(Erase_Menu_BgStyleSystem,
268 "Use wxBG_STYLE_&SYSTEM\tCtrl-S");
269 menuFile->AppendRadioItem(Erase_Menu_BgStylePaint,
270 "Use wxBG_STYLE_&PAINT\tCtrl-P");
79c5fe4b 271 menuFile->AppendSeparator();
9c61c5b0 272 menuFile->Append(Erase_Menu_Exit, "E&xit\tAlt-X", "Quit this program");
33611ebb 273
33611ebb 274
79c5fe4b 275 wxMenu *helpMenu = new wxMenu;
2d143b66 276 helpMenu->Append(Erase_Menu_About, "&About\tCtrl-A", "Show about dialog");
33611ebb
RR
277
278 wxMenuBar *menuBar = new wxMenuBar();
9c61c5b0
VZ
279 menuBar->Append(menuFile, "&File");
280 menuBar->Append(helpMenu, "&Help");
33611ebb
RR
281
282 SetMenuBar(menuBar);
283
79c5fe4b 284 m_canvas = new MyCanvas( this );
33611ebb
RR
285}
286
287
79c5fe4b
VZ
288void MyFrame::OnUseBuffer(wxCommandEvent& event)
289{
290 m_canvas->UseBuffer(event.IsChecked());
291}
292
cd95f7e6
VZ
293void MyFrame::OnUseBgBitmap(wxCommandEvent& event)
294{
295 m_canvas->UseBgBitmap(event.IsChecked());
296}
297
c753eb92
VZ
298void MyFrame::OnEraseBgInPaint(wxCommandEvent& event)
299{
300 m_canvas->EraseBgInPaint(event.IsChecked());
301}
302
9c61c5b0 303void MyFrame::OnChangeBgStyle(wxCommandEvent& event)
b18eb01c 304{
9c61c5b0
VZ
305 int style = wxBG_STYLE_ERASE + event.GetId() - Erase_Menu_BgStyleErase;
306 m_canvas->SetBackgroundStyle(static_cast<wxBackgroundStyle>(style));
307
308 m_canvas->Refresh();
b18eb01c
VZ
309}
310
33611ebb
RR
311void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
312{
07850a49 313 Close(true);
33611ebb
RR
314}
315
316void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
317{
9c61c5b0
VZ
318 wxMessageBox
319 (
320 "This sample shows differences between different background styles "
321 "and how you may draw custom background.\n"
322 "\n"
323 "(c) 1998 Robert Roebling\n"
324 "(c) 2009 Vadim Zeitlin\n",
325 "About Erase Sample",
326 wxOK | wxICON_INFORMATION,
327 this
328 );
33611ebb
RR
329}
330
331
332BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
9c61c5b0 333 EVT_PAINT(MyCanvas::OnPaint)
9c61c5b0 334 EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground)
33611ebb
RR
335END_EVENT_TABLE()
336
9c61c5b0 337MyCanvas::MyCanvas(wxFrame *parent)
33611ebb 338{
bbcf2821
VZ
339 Create(parent, wxID_ANY);
340
79c5fe4b 341 m_useBuffer = false;
cd95f7e6 342 m_useBgBmp = false;
c753eb92 343 m_eraseBgInPaint = false;
79c5fe4b 344
33611ebb 345 SetScrollbars( 10, 10, 40, 100, 0, 0 );
98dd66cf 346
3cb332c1 347 m_bitmap = wxBitmap( wxICON(sample) );
98dd66cf 348
07850a49 349 new wxStaticBitmap( this, wxID_ANY, m_bitmap, wxPoint(80,20) );
11fdee42 350
6fec48b7
VZ
351 new wxStaticText(this, wxID_ANY,
352 "Left bitmap is a wxStaticBitmap,\n"
353 "right one drawn directly",
354 wxPoint(150, 20));
355
14421681 356 new ControlWithTransparency(this, wxPoint(65, 125), wxSize(350, 22));
ab438739 357
ca37cfde 358 SetFocusIgnoringChildren();
6fec48b7 359 SetBackgroundColour(*wxCYAN);
33611ebb
RR
360}
361
79c5fe4b 362void MyCanvas::DoPaint(wxDC& dc)
33611ebb 363{
2aee45f6
VZ
364 PrepareDC(dc);
365
c753eb92
VZ
366 if ( m_eraseBgInPaint )
367 {
368 dc.SetBackground(*wxLIGHT_GREY);
963f64fb
VZ
369
370 // Erase the entire virtual area, not just the client area.
371 dc.SetPen(*wxTRANSPARENT_PEN);
372 dc.SetBrush(GetBackgroundColour());
373 dc.DrawRectangle(GetVirtualSize());
c753eb92
VZ
374
375 dc.DrawText("Background erased in OnPaint", 65, 110);
376 }
377 else if ( GetBackgroundStyle() == wxBG_STYLE_PAINT )
378 {
379 dc.SetTextForeground(*wxRED);
380 dc.DrawText("You must enable erasing background in OnPaint to avoid "
381 "display corruption", 65, 110);
382 }
383
9c61c5b0 384 dc.DrawBitmap( m_bitmap, 20, 20, true );
98dd66cf 385
cd95f7e6 386 dc.SetTextForeground(*wxRED);
9c61c5b0 387 dc.DrawText("This text is drawn from OnPaint", 65, 65);
33611ebb
RR
388}
389
79c5fe4b
VZ
390void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
391{
79c5fe4b
VZ
392 if ( m_useBuffer )
393 {
9c61c5b0 394 wxAutoBufferedPaintDC dc(this);
79c5fe4b 395 DoPaint(dc);
79c5fe4b
VZ
396 }
397 else
398 {
9c61c5b0 399 wxPaintDC dc(this);
9c61c5b0 400 DoPaint(dc);
79c5fe4b
VZ
401 }
402}
403
98dd66cf 404void MyCanvas::OnEraseBackground( wxEraseEvent& event )
33611ebb 405{
cd95f7e6
VZ
406 // We must not erase the background ourselves if we asked wxPanel to erase
407 // it using a background bitmap.
408 if ( m_useBgBmp )
409 {
410 event.Skip();
411 return;
412 }
413
9c61c5b0
VZ
414 wxASSERT_MSG
415 (
416 GetBackgroundStyle() == wxBG_STYLE_ERASE,
417 "shouldn't be called unless background style is \"erase\""
418 );
b18eb01c 419
98dd66cf
VZ
420 wxDC& dc = *event.GetDC();
421 dc.SetPen(*wxGREEN_PEN);
422
79c5fe4b 423 // clear any junk currently displayed
98dd66cf
VZ
424 dc.Clear();
425
99f22294
VZ
426 PrepareDC( dc );
427
428 const wxSize size = GetVirtualSize();
ca37cfde 429 for ( int x = 0; x < size.x; x += 15 )
98dd66cf
VZ
430 {
431 dc.DrawLine(x, 0, x, size.y);
432 }
433
ca37cfde 434 for ( int y = 0; y < size.y; y += 15 )
98dd66cf
VZ
435 {
436 dc.DrawLine(0, y, size.x, y);
437 }
438
439 dc.SetTextForeground(*wxRED);
9c61c5b0
VZ
440 dc.SetBackgroundMode(wxSOLID);
441 dc.DrawText("This text is drawn from OnEraseBackground", 60, 160);
33611ebb
RR
442}
443