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/dcbuffer.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // the application icon
40 #if !defined(__WXMSW__) && !defined(__WXPM__)
41 #include "../sample.xpm"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 class MyApp
: public wxApp
51 virtual bool OnInit();
55 class MyCanvas
: public wxScrolledWindow
58 MyCanvas(wxFrame
*parent
);
60 void UseBuffer(bool useBuffer
) { m_useBuffer
= useBuffer
; Refresh(); }
61 bool UsesBuffer() const { return m_useBuffer
; }
63 void UseBgBitmap(bool useBgBmp
)
65 m_useBgBmp
= useBgBmp
;
66 SetBackgroundBitmap(m_useBgBmp
? GetBgBitmap() : wxBitmap());
71 void EraseBgInPaint(bool erase
) { m_eraseBgInPaint
= erase
; Refresh(); }
74 void OnPaint( wxPaintEvent
&event
);
75 void OnEraseBackground( wxEraseEvent
&event
);
77 void DoPaint(wxDC
& dc
);
79 // Create an easily recognizable background bitmap.
80 static wxBitmap
GetBgBitmap()
82 static const int BMP_SIZE
= 40;
84 wxBitmap
bmp(BMP_SIZE
, BMP_SIZE
);
86 dc
.SetBackground(*wxCYAN
);
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);
100 // use wxMemoryDC in OnPaint()?
103 // use background bitmap?
106 // erase background in OnPaint()?
107 bool m_eraseBgInPaint
;
110 DECLARE_EVENT_TABLE()
113 class MyFrame
: public wxFrame
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
);
126 // we can only use double-buffering with wxBG_STYLE_PAINT
127 void OnUpdateUIUseBuffer(wxUpdateUIEvent
& event
)
129 event
.Enable( m_canvas
->GetBackgroundStyle() == wxBG_STYLE_PAINT
);
132 void OnUpdateUIChangeBgStyle(wxUpdateUIEvent
& event
)
134 event
.Enable( !m_canvas
->UsesBuffer() );
139 DECLARE_EVENT_TABLE()
142 class ControlWithTransparency
: public wxWindow
145 ControlWithTransparency(wxWindow
*parent
,
148 : wxWindow(parent
, wxID_ANY
, pos
, size
, wxBORDER_NONE
)
151 wxPaintEventHandler(ControlWithTransparency::OnPaint
));
154 virtual bool HasTransparentBackground() { return true; }
157 void OnPaint( wxPaintEvent
& WXUNUSED(event
) )
161 dc
.SetPen(*wxRED_PEN
);
162 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
163 dc
.DrawRectangle(GetClientSize());
165 dc
.SetTextForeground(*wxBLUE
);
166 dc
.SetBackgroundMode(wxTRANSPARENT
);
167 dc
.DrawText("This is custom control with transparency", 0, 2);
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
178 Erase_Menu_UseBuffer
= 100,
179 Erase_Menu_UseBgBitmap
,
180 Erase_Menu_EraseBgInPaint
,
181 Erase_Menu_BgStyleErase
,
182 Erase_Menu_BgStyleSystem
,
183 Erase_Menu_BgStylePaint
,
184 Erase_Menu_Exit
= wxID_EXIT
,
185 Erase_Menu_About
= wxID_ABOUT
189 // ----------------------------------------------------------------------------
190 // the application class
191 // ----------------------------------------------------------------------------
197 if ( !wxApp::OnInit() )
200 MyFrame
*frame
= new MyFrame
;
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
211 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
212 EVT_MENU(Erase_Menu_UseBuffer
, MyFrame::OnUseBuffer
)
213 EVT_MENU(Erase_Menu_UseBgBitmap
, MyFrame::OnUseBgBitmap
)
214 EVT_MENU(Erase_Menu_EraseBgInPaint
, MyFrame::OnEraseBgInPaint
)
215 EVT_MENU_RANGE(Erase_Menu_BgStyleErase
, Erase_Menu_BgStylePaint
,
216 MyFrame::OnChangeBgStyle
)
218 EVT_MENU(Erase_Menu_Exit
, MyFrame::OnQuit
)
219 EVT_MENU(Erase_Menu_About
, MyFrame::OnAbout
)
221 EVT_UPDATE_UI(Erase_Menu_UseBuffer
, MyFrame::OnUpdateUIUseBuffer
)
222 EVT_UPDATE_UI_RANGE(Erase_Menu_BgStyleErase
, Erase_Menu_BgStylePaint
,
223 MyFrame::OnUpdateUIChangeBgStyle
)
228 : wxFrame(NULL
, wxID_ANY
, "Erase sample",
229 wxPoint(50, 50), wxSize(450, 340))
231 SetIcon(wxICON(sample
));
233 wxMenu
*menuFile
= new wxMenu("", wxMENU_TEAROFF
);
234 menuFile
->AppendCheckItem(Erase_Menu_UseBuffer
, "&Use memory DC\tCtrl-M");
235 menuFile
->AppendCheckItem(Erase_Menu_UseBgBitmap
,
236 "Use background &bitmap\tCtrl-B");
237 menuFile
->AppendCheckItem(Erase_Menu_EraseBgInPaint
,
238 "&Erase background in EVT_PAINT\tCtrl-R");
239 menuFile
->AppendSeparator();
240 menuFile
->AppendRadioItem(Erase_Menu_BgStyleErase
,
241 "Use wxBG_STYLE_&ERASE\tCtrl-E");
242 menuFile
->AppendRadioItem(Erase_Menu_BgStyleSystem
,
243 "Use wxBG_STYLE_&SYSTEM\tCtrl-S");
244 menuFile
->AppendRadioItem(Erase_Menu_BgStylePaint
,
245 "Use wxBG_STYLE_&PAINT\tCtrl-P");
246 menuFile
->AppendSeparator();
247 menuFile
->Append(Erase_Menu_Exit
, "E&xit\tAlt-X", "Quit this program");
250 wxMenu
*helpMenu
= new wxMenu
;
251 helpMenu
->Append(Erase_Menu_About
, "&About...\tCtrl-A", "Show about dialog");
253 wxMenuBar
*menuBar
= new wxMenuBar();
254 menuBar
->Append(menuFile
, "&File");
255 menuBar
->Append(helpMenu
, "&Help");
259 m_canvas
= new MyCanvas( this );
263 void MyFrame::OnUseBuffer(wxCommandEvent
& event
)
265 m_canvas
->UseBuffer(event
.IsChecked());
268 void MyFrame::OnUseBgBitmap(wxCommandEvent
& event
)
270 m_canvas
->UseBgBitmap(event
.IsChecked());
273 void MyFrame::OnEraseBgInPaint(wxCommandEvent
& event
)
275 m_canvas
->EraseBgInPaint(event
.IsChecked());
278 void MyFrame::OnChangeBgStyle(wxCommandEvent
& event
)
280 int style
= wxBG_STYLE_ERASE
+ event
.GetId() - Erase_Menu_BgStyleErase
;
281 m_canvas
->SetBackgroundStyle(static_cast<wxBackgroundStyle
>(style
));
286 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
291 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
295 "This sample shows differences between different background styles "
296 "and how you may draw custom background.\n"
298 "(c) 1998 Robert Roebling\n"
299 "(c) 2009 Vadim Zeitlin\n",
300 "About Erase Sample",
301 wxOK
| wxICON_INFORMATION
,
307 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
308 EVT_PAINT(MyCanvas::OnPaint
)
309 EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground
)
312 MyCanvas::MyCanvas(wxFrame
*parent
)
313 : wxScrolledWindow(parent
, wxID_ANY
)
317 m_eraseBgInPaint
= false;
319 SetScrollbars( 10, 10, 40, 100, 0, 0 );
321 m_bitmap
= wxBitmap( wxICON(sample
) );
323 new wxStaticBitmap( this, wxID_ANY
, m_bitmap
, wxPoint(80,20) );
325 new wxStaticText(this, wxID_ANY
,
326 "Left bitmap is a wxStaticBitmap,\n"
327 "right one drawn directly",
330 new ControlWithTransparency(this, wxPoint(65, 125), wxSize(300, 22));
332 SetFocusIgnoringChildren();
333 SetBackgroundColour(*wxCYAN
);
336 void MyCanvas::DoPaint(wxDC
& dc
)
338 if ( m_eraseBgInPaint
)
340 dc
.SetBackground(*wxLIGHT_GREY
);
343 dc
.DrawText("Background erased in OnPaint", 65, 110);
345 else if ( GetBackgroundStyle() == wxBG_STYLE_PAINT
)
347 dc
.SetTextForeground(*wxRED
);
348 dc
.DrawText("You must enable erasing background in OnPaint to avoid "
349 "display corruption", 65, 110);
352 dc
.DrawBitmap( m_bitmap
, 20, 20, true );
354 dc
.SetTextForeground(*wxRED
);
355 dc
.DrawText("This text is drawn from OnPaint", 65, 65);
358 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
362 wxAutoBufferedPaintDC
dc(this);
376 void MyCanvas::OnEraseBackground( wxEraseEvent
& event
)
378 // We must not erase the background ourselves if we asked wxPanel to erase
379 // it using a background bitmap.
388 GetBackgroundStyle() == wxBG_STYLE_ERASE
,
389 "shouldn't be called unless background style is \"erase\""
392 wxDC
& dc
= *event
.GetDC();
393 dc
.SetPen(*wxGREEN_PEN
);
397 // clear any junk currently displayed
400 const wxSize size
= GetClientSize();
401 for ( int x
= 0; x
< size
.x
; x
+= 15 )
403 dc
.DrawLine(x
, 0, x
, size
.y
);
406 for ( int y
= 0; y
< size
.y
; y
+= 15 )
408 dc
.DrawLine(0, y
, size
.x
, y
);
411 dc
.SetTextForeground(*wxRED
);
412 dc
.SetBackgroundMode(wxSOLID
);
413 dc
.DrawText("This text is drawn from OnEraseBackground", 60, 160);