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()
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
150 Erase_Menu_UseBuffer
= 100,
151 Erase_Menu_UseBgBitmap
,
152 Erase_Menu_EraseBgInPaint
,
153 Erase_Menu_BgStyleErase
,
154 Erase_Menu_BgStyleSystem
,
155 Erase_Menu_BgStylePaint
,
156 Erase_Menu_Exit
= wxID_EXIT
,
157 Erase_Menu_About
= wxID_ABOUT
161 // ----------------------------------------------------------------------------
162 // the application class
163 // ----------------------------------------------------------------------------
169 if ( !wxApp::OnInit() )
172 MyFrame
*frame
= new MyFrame
;
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
183 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
184 EVT_MENU(Erase_Menu_UseBuffer
, MyFrame::OnUseBuffer
)
185 EVT_MENU(Erase_Menu_UseBgBitmap
, MyFrame::OnUseBgBitmap
)
186 EVT_MENU(Erase_Menu_EraseBgInPaint
, MyFrame::OnEraseBgInPaint
)
187 EVT_MENU_RANGE(Erase_Menu_BgStyleErase
, Erase_Menu_BgStylePaint
,
188 MyFrame::OnChangeBgStyle
)
190 EVT_MENU(Erase_Menu_Exit
, MyFrame::OnQuit
)
191 EVT_MENU(Erase_Menu_About
, MyFrame::OnAbout
)
193 EVT_UPDATE_UI(Erase_Menu_UseBuffer
, MyFrame::OnUpdateUIUseBuffer
)
194 EVT_UPDATE_UI_RANGE(Erase_Menu_BgStyleErase
, Erase_Menu_BgStylePaint
,
195 MyFrame::OnUpdateUIChangeBgStyle
)
200 : wxFrame(NULL
, wxID_ANY
, "Erase sample",
201 wxPoint(50, 50), wxSize(450, 340))
203 SetIcon(wxICON(sample
));
205 wxMenu
*menuFile
= new wxMenu("", wxMENU_TEAROFF
);
206 menuFile
->AppendCheckItem(Erase_Menu_UseBuffer
, "&Use memory DC\tCtrl-M");
207 menuFile
->AppendCheckItem(Erase_Menu_UseBgBitmap
,
208 "Use background &bitmap\tCtrl-B");
209 menuFile
->AppendCheckItem(Erase_Menu_EraseBgInPaint
,
210 "&Erase background in EVT_PAINT\tCtrl-R");
211 menuFile
->AppendSeparator();
212 menuFile
->AppendRadioItem(Erase_Menu_BgStyleErase
,
213 "Use wxBG_STYLE_&ERASE\tCtrl-E");
214 menuFile
->AppendRadioItem(Erase_Menu_BgStyleSystem
,
215 "Use wxBG_STYLE_&SYSTEM\tCtrl-S");
216 menuFile
->AppendRadioItem(Erase_Menu_BgStylePaint
,
217 "Use wxBG_STYLE_&PAINT\tCtrl-P");
218 menuFile
->AppendSeparator();
219 menuFile
->Append(Erase_Menu_Exit
, "E&xit\tAlt-X", "Quit this program");
222 wxMenu
*helpMenu
= new wxMenu
;
223 helpMenu
->Append(Erase_Menu_About
, "&About...\tCtrl-A", "Show about dialog");
225 wxMenuBar
*menuBar
= new wxMenuBar();
226 menuBar
->Append(menuFile
, "&File");
227 menuBar
->Append(helpMenu
, "&Help");
231 m_canvas
= new MyCanvas( this );
235 void MyFrame::OnUseBuffer(wxCommandEvent
& event
)
237 m_canvas
->UseBuffer(event
.IsChecked());
240 void MyFrame::OnUseBgBitmap(wxCommandEvent
& event
)
242 m_canvas
->UseBgBitmap(event
.IsChecked());
245 void MyFrame::OnEraseBgInPaint(wxCommandEvent
& event
)
247 m_canvas
->EraseBgInPaint(event
.IsChecked());
250 void MyFrame::OnChangeBgStyle(wxCommandEvent
& event
)
252 int style
= wxBG_STYLE_ERASE
+ event
.GetId() - Erase_Menu_BgStyleErase
;
253 m_canvas
->SetBackgroundStyle(static_cast<wxBackgroundStyle
>(style
));
258 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
263 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
267 "This sample shows differences between different background styles "
268 "and how you may draw custom background.\n"
270 "(c) 1998 Robert Roebling\n"
271 "(c) 2009 Vadim Zeitlin\n",
272 "About Erase Sample",
273 wxOK
| wxICON_INFORMATION
,
279 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
280 EVT_PAINT(MyCanvas::OnPaint
)
281 EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground
)
284 MyCanvas::MyCanvas(wxFrame
*parent
)
285 : wxScrolledWindow(parent
, wxID_ANY
)
289 m_eraseBgInPaint
= false;
291 SetScrollbars( 10, 10, 40, 100, 0, 0 );
293 m_bitmap
= wxBitmap( wxICON(sample
) );
295 new wxStaticBitmap( this, wxID_ANY
, m_bitmap
, wxPoint(80,20) );
297 new wxStaticText(this, wxID_ANY
,
298 "Left bitmap is a wxStaticBitmap,\n"
299 "right one drawn directly",
302 SetFocusIgnoringChildren();
303 SetBackgroundColour(*wxCYAN
);
306 void MyCanvas::DoPaint(wxDC
& dc
)
308 if ( m_eraseBgInPaint
)
310 dc
.SetBackground(*wxLIGHT_GREY
);
313 dc
.DrawText("Background erased in OnPaint", 65, 110);
315 else if ( GetBackgroundStyle() == wxBG_STYLE_PAINT
)
317 dc
.SetTextForeground(*wxRED
);
318 dc
.DrawText("You must enable erasing background in OnPaint to avoid "
319 "display corruption", 65, 110);
322 dc
.DrawBitmap( m_bitmap
, 20, 20, true );
324 dc
.SetTextForeground(*wxRED
);
325 dc
.DrawText("This text is drawn from OnPaint", 65, 65);
328 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
332 wxAutoBufferedPaintDC
dc(this);
346 void MyCanvas::OnEraseBackground( wxEraseEvent
& event
)
348 // We must not erase the background ourselves if we asked wxPanel to erase
349 // it using a background bitmap.
358 GetBackgroundStyle() == wxBG_STYLE_ERASE
,
359 "shouldn't be called unless background style is \"erase\""
362 wxDC
& dc
= *event
.GetDC();
363 dc
.SetPen(*wxGREEN_PEN
);
367 // clear any junk currently displayed
370 const wxSize size
= GetClientSize();
371 for ( int x
= 0; x
< size
.x
; x
+= 15 )
373 dc
.DrawLine(x
, 0, x
, size
.y
);
376 for ( int y
= 0; y
< size
.y
; y
+= 15 )
378 dc
.DrawLine(0, y
, size
.x
, y
);
381 dc
.SetTextForeground(*wxRED
);
382 dc
.SetBackgroundMode(wxSOLID
);
383 dc
.DrawText("This text is drawn from OnEraseBackground", 60, 160);