1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/erase/erase.cpp
3 // Purpose: Erase wxWidgets sample
4 // Author: Robert Roebling, Vadim Zeitlin
7 // Copyright: (c) 1998 Robert Roebling
8 // (c) 2009 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
33 #include "wx/custombgwin.h"
34 #include "wx/dcbuffer.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // the application icon
41 #if !defined(__WXMSW__) && !defined(__WXPM__)
42 #include "../sample.xpm"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 class MyApp
: public wxApp
52 virtual bool OnInit();
56 class MyCanvas
: public wxCustomBackgroundWindow
<wxScrolledWindow
>
59 MyCanvas(wxFrame
*parent
);
61 void UseBuffer(bool useBuffer
) { m_useBuffer
= useBuffer
; Refresh(); }
62 bool UsesBuffer() const { return m_useBuffer
; }
64 void UseBgBitmap(bool useBgBmp
)
66 m_useBgBmp
= useBgBmp
;
67 SetBackgroundBitmap(m_useBgBmp
? GetBgBitmap() : wxBitmap());
72 void EraseBgInPaint(bool erase
) { m_eraseBgInPaint
= erase
; Refresh(); }
75 void OnPaint( wxPaintEvent
&event
);
76 void OnEraseBackground( wxEraseEvent
&event
);
78 void DoPaint(wxDC
& dc
);
80 // Create an easily recognizable background bitmap.
81 static wxBitmap
GetBgBitmap()
83 static const int BMP_SIZE
= 40;
85 wxBitmap
bmp(BMP_SIZE
, BMP_SIZE
);
87 dc
.SetBackground(*wxCYAN
);
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);
101 // use wxMemoryDC in OnPaint()?
104 // use background bitmap?
107 // erase background in OnPaint()?
108 bool m_eraseBgInPaint
;
111 DECLARE_EVENT_TABLE()
114 class MyFrame
: public wxFrame
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
);
127 // we can only use double-buffering with wxBG_STYLE_PAINT
128 void OnUpdateUIUseBuffer(wxUpdateUIEvent
& event
)
130 event
.Enable( m_canvas
->GetBackgroundStyle() == wxBG_STYLE_PAINT
);
133 void OnUpdateUIChangeBgStyle(wxUpdateUIEvent
& event
)
135 event
.Enable( !m_canvas
->UsesBuffer() );
140 DECLARE_EVENT_TABLE()
143 class ControlWithTransparency
: public wxWindow
146 ControlWithTransparency(wxWindow
*parent
,
149 : wxWindow(parent
, wxID_ANY
, pos
, size
, wxBORDER_NONE
)
152 wxPaintEventHandler(ControlWithTransparency::OnPaint
));
155 virtual bool HasTransparentBackground() { return true; }
158 void OnPaint( wxPaintEvent
& WXUNUSED(event
) )
162 dc
.SetPen(*wxRED_PEN
);
163 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
164 dc
.DrawRectangle(GetClientSize());
166 dc
.SetTextForeground(*wxBLUE
);
167 dc
.SetBackgroundMode(wxTRANSPARENT
);
168 dc
.DrawText("This is custom control with transparency", 0, 2);
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
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
190 // ----------------------------------------------------------------------------
191 // the application class
192 // ----------------------------------------------------------------------------
198 if ( !wxApp::OnInit() )
201 MyFrame
*frame
= new MyFrame
;
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
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
)
219 EVT_MENU(Erase_Menu_Exit
, MyFrame::OnQuit
)
220 EVT_MENU(Erase_Menu_About
, MyFrame::OnAbout
)
222 EVT_UPDATE_UI(Erase_Menu_UseBuffer
, MyFrame::OnUpdateUIUseBuffer
)
223 EVT_UPDATE_UI_RANGE(Erase_Menu_BgStyleErase
, Erase_Menu_BgStylePaint
,
224 MyFrame::OnUpdateUIChangeBgStyle
)
229 : wxFrame(NULL
, wxID_ANY
, "Erase sample",
230 wxPoint(50, 50), wxSize(450, 340))
232 SetIcon(wxICON(sample
));
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");
251 wxMenu
*helpMenu
= new wxMenu
;
252 helpMenu
->Append(Erase_Menu_About
, "&About...\tCtrl-A", "Show about dialog");
254 wxMenuBar
*menuBar
= new wxMenuBar();
255 menuBar
->Append(menuFile
, "&File");
256 menuBar
->Append(helpMenu
, "&Help");
260 m_canvas
= new MyCanvas( this );
264 void MyFrame::OnUseBuffer(wxCommandEvent
& event
)
266 m_canvas
->UseBuffer(event
.IsChecked());
269 void MyFrame::OnUseBgBitmap(wxCommandEvent
& event
)
271 m_canvas
->UseBgBitmap(event
.IsChecked());
274 void MyFrame::OnEraseBgInPaint(wxCommandEvent
& event
)
276 m_canvas
->EraseBgInPaint(event
.IsChecked());
279 void MyFrame::OnChangeBgStyle(wxCommandEvent
& event
)
281 int style
= wxBG_STYLE_ERASE
+ event
.GetId() - Erase_Menu_BgStyleErase
;
282 m_canvas
->SetBackgroundStyle(static_cast<wxBackgroundStyle
>(style
));
287 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
292 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
296 "This sample shows differences between different background styles "
297 "and how you may draw custom background.\n"
299 "(c) 1998 Robert Roebling\n"
300 "(c) 2009 Vadim Zeitlin\n",
301 "About Erase Sample",
302 wxOK
| wxICON_INFORMATION
,
308 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
309 EVT_PAINT(MyCanvas::OnPaint
)
310 EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground
)
313 MyCanvas::MyCanvas(wxFrame
*parent
)
315 Create(parent
, wxID_ANY
);
319 m_eraseBgInPaint
= false;
321 SetScrollbars( 10, 10, 40, 100, 0, 0 );
323 m_bitmap
= wxBitmap( wxICON(sample
) );
325 new wxStaticBitmap( this, wxID_ANY
, m_bitmap
, wxPoint(80,20) );
327 new wxStaticText(this, wxID_ANY
,
328 "Left bitmap is a wxStaticBitmap,\n"
329 "right one drawn directly",
332 new ControlWithTransparency(this, wxPoint(65, 125), wxSize(300, 22));
334 SetFocusIgnoringChildren();
335 SetBackgroundColour(*wxCYAN
);
338 void MyCanvas::DoPaint(wxDC
& dc
)
340 if ( m_eraseBgInPaint
)
342 dc
.SetBackground(*wxLIGHT_GREY
);
345 dc
.DrawText("Background erased in OnPaint", 65, 110);
347 else if ( GetBackgroundStyle() == wxBG_STYLE_PAINT
)
349 dc
.SetTextForeground(*wxRED
);
350 dc
.DrawText("You must enable erasing background in OnPaint to avoid "
351 "display corruption", 65, 110);
354 dc
.DrawBitmap( m_bitmap
, 20, 20, true );
356 dc
.SetTextForeground(*wxRED
);
357 dc
.DrawText("This text is drawn from OnPaint", 65, 65);
360 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
364 wxAutoBufferedPaintDC
dc(this);
378 void MyCanvas::OnEraseBackground( wxEraseEvent
& event
)
380 // We must not erase the background ourselves if we asked wxPanel to erase
381 // it using a background bitmap.
390 GetBackgroundStyle() == wxBG_STYLE_ERASE
,
391 "shouldn't be called unless background style is \"erase\""
394 wxDC
& dc
= *event
.GetDC();
395 dc
.SetPen(*wxGREEN_PEN
);
399 // clear any junk currently displayed
402 const wxSize size
= GetClientSize();
403 for ( int x
= 0; x
< size
.x
; x
+= 15 )
405 dc
.DrawLine(x
, 0, x
, size
.y
);
408 for ( int y
= 0; y
< size
.y
; y
+= 15 )
410 dc
.DrawLine(0, y
, size
.x
, y
);
413 dc
.SetTextForeground(*wxRED
);
414 dc
.SetBackgroundMode(wxSOLID
);
415 dc
.DrawText("This text is drawn from OnEraseBackground", 60, 160);