1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/erase/erase.cpp
3 // Purpose: Erase wxWidgets sample
4 // Author: Robert Roebling, Vadim Zeitlin
6 // Copyright: (c) 1998 Robert Roebling
7 // (c) 2009 Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
26 // for all others, include the necessary headers (this file is usually all you
27 // need because it includes almost all "standard" wxWidgets headers)
32 #include "wx/custombgwin.h"
33 #include "wx/dcbuffer.h"
34 #include "wx/artprov.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // the application icon
41 #ifndef wxHAS_IMAGES_IN_RESOURCES
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
,
151 if ( parent
->IsTransparentBackgroundSupported(&reason
) )
153 SetBackgroundStyle (wxBG_STYLE_TRANSPARENT
);
154 m_message
= "This is custom control with transparency";
158 m_message
= "Transparency not supported, check tooltip.";
161 Create (parent
, wxID_ANY
, pos
, size
, wxBORDER_NONE
);
163 wxPaintEventHandler(ControlWithTransparency::OnPaint
));
165 if ( !reason
.empty() )
167 // This can be only done now, after creating the window.
173 void OnPaint( wxPaintEvent
& WXUNUSED(event
) )
177 dc
.SetPen(*wxRED_PEN
);
178 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
179 dc
.DrawRectangle(GetClientSize());
181 dc
.SetTextForeground(*wxBLUE
);
182 dc
.SetBackgroundMode(wxTRANSPARENT
);
183 dc
.DrawText(m_message
, 0, 2);
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);
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
203 Erase_Menu_UseBuffer
= 100,
204 Erase_Menu_UseBgBitmap
,
205 Erase_Menu_EraseBgInPaint
,
206 Erase_Menu_BgStyleErase
,
207 Erase_Menu_BgStyleSystem
,
208 Erase_Menu_BgStylePaint
,
209 Erase_Menu_Exit
= wxID_EXIT
,
210 Erase_Menu_About
= wxID_ABOUT
214 // ----------------------------------------------------------------------------
215 // the application class
216 // ----------------------------------------------------------------------------
222 if ( !wxApp::OnInit() )
225 MyFrame
*frame
= new MyFrame
;
232 // ----------------------------------------------------------------------------
234 // ----------------------------------------------------------------------------
236 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
237 EVT_MENU(Erase_Menu_UseBuffer
, MyFrame::OnUseBuffer
)
238 EVT_MENU(Erase_Menu_UseBgBitmap
, MyFrame::OnUseBgBitmap
)
239 EVT_MENU(Erase_Menu_EraseBgInPaint
, MyFrame::OnEraseBgInPaint
)
240 EVT_MENU_RANGE(Erase_Menu_BgStyleErase
, Erase_Menu_BgStylePaint
,
241 MyFrame::OnChangeBgStyle
)
243 EVT_MENU(Erase_Menu_Exit
, MyFrame::OnQuit
)
244 EVT_MENU(Erase_Menu_About
, MyFrame::OnAbout
)
246 EVT_UPDATE_UI(Erase_Menu_UseBuffer
, MyFrame::OnUpdateUIUseBuffer
)
247 EVT_UPDATE_UI_RANGE(Erase_Menu_BgStyleErase
, Erase_Menu_BgStylePaint
,
248 MyFrame::OnUpdateUIChangeBgStyle
)
253 : wxFrame(NULL
, wxID_ANY
, "Erase sample",
254 wxPoint(50, 50), wxSize(450, 340))
256 SetIcon(wxICON(sample
));
258 wxMenu
*menuFile
= new wxMenu("", wxMENU_TEAROFF
);
259 menuFile
->AppendCheckItem(Erase_Menu_UseBuffer
, "&Use memory DC\tCtrl-M");
260 menuFile
->AppendCheckItem(Erase_Menu_UseBgBitmap
,
261 "Use background &bitmap\tCtrl-B");
262 menuFile
->AppendCheckItem(Erase_Menu_EraseBgInPaint
,
263 "&Erase background in EVT_PAINT\tCtrl-R");
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");
271 menuFile
->AppendSeparator();
272 menuFile
->Append(Erase_Menu_Exit
, "E&xit\tAlt-X", "Quit this program");
275 wxMenu
*helpMenu
= new wxMenu
;
276 helpMenu
->Append(Erase_Menu_About
, "&About\tCtrl-A", "Show about dialog");
278 wxMenuBar
*menuBar
= new wxMenuBar();
279 menuBar
->Append(menuFile
, "&File");
280 menuBar
->Append(helpMenu
, "&Help");
284 m_canvas
= new MyCanvas( this );
288 void MyFrame::OnUseBuffer(wxCommandEvent
& event
)
290 m_canvas
->UseBuffer(event
.IsChecked());
293 void MyFrame::OnUseBgBitmap(wxCommandEvent
& event
)
295 m_canvas
->UseBgBitmap(event
.IsChecked());
298 void MyFrame::OnEraseBgInPaint(wxCommandEvent
& event
)
300 m_canvas
->EraseBgInPaint(event
.IsChecked());
303 void MyFrame::OnChangeBgStyle(wxCommandEvent
& event
)
305 int style
= wxBG_STYLE_ERASE
+ event
.GetId() - Erase_Menu_BgStyleErase
;
306 m_canvas
->SetBackgroundStyle(static_cast<wxBackgroundStyle
>(style
));
311 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
316 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
320 "This sample shows differences between different background styles "
321 "and how you may draw custom background.\n"
323 "(c) 1998 Robert Roebling\n"
324 "(c) 2009 Vadim Zeitlin\n",
325 "About Erase Sample",
326 wxOK
| wxICON_INFORMATION
,
332 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
333 EVT_PAINT(MyCanvas::OnPaint
)
334 EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground
)
337 MyCanvas::MyCanvas(wxFrame
*parent
)
339 Create(parent
, wxID_ANY
);
343 m_eraseBgInPaint
= false;
345 SetScrollbars( 10, 10, 40, 100, 0, 0 );
347 m_bitmap
= wxBitmap( wxICON(sample
) );
349 new wxStaticBitmap( this, wxID_ANY
, m_bitmap
, wxPoint(80,20) );
351 new wxStaticText(this, wxID_ANY
,
352 "Left bitmap is a wxStaticBitmap,\n"
353 "right one drawn directly",
356 new ControlWithTransparency(this, wxPoint(65, 125), wxSize(350, 22));
358 SetFocusIgnoringChildren();
359 SetBackgroundColour(*wxCYAN
);
362 void MyCanvas::DoPaint(wxDC
& dc
)
366 if ( m_eraseBgInPaint
)
368 dc
.SetBackground(*wxLIGHT_GREY
);
370 // Erase the entire virtual area, not just the client area.
371 dc
.SetPen(*wxTRANSPARENT_PEN
);
372 dc
.SetBrush(GetBackgroundColour());
373 dc
.DrawRectangle(GetVirtualSize());
375 dc
.DrawText("Background erased in OnPaint", 65, 110);
377 else if ( GetBackgroundStyle() == wxBG_STYLE_PAINT
)
379 dc
.SetTextForeground(*wxRED
);
380 dc
.DrawText("You must enable erasing background in OnPaint to avoid "
381 "display corruption", 65, 110);
384 dc
.DrawBitmap( m_bitmap
, 20, 20, true );
386 dc
.SetTextForeground(*wxRED
);
387 dc
.DrawText("This text is drawn from OnPaint", 65, 65);
390 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
394 wxAutoBufferedPaintDC
dc(this);
404 void MyCanvas::OnEraseBackground( wxEraseEvent
& event
)
406 // We must not erase the background ourselves if we asked wxPanel to erase
407 // it using a background bitmap.
416 GetBackgroundStyle() == wxBG_STYLE_ERASE
,
417 "shouldn't be called unless background style is \"erase\""
420 wxDC
& dc
= *event
.GetDC();
421 dc
.SetPen(*wxGREEN_PEN
);
423 // clear any junk currently displayed
428 const wxSize size
= GetVirtualSize();
429 for ( int x
= 0; x
< size
.x
; x
+= 15 )
431 dc
.DrawLine(x
, 0, x
, size
.y
);
434 for ( int y
= 0; y
< size
.y
; y
+= 15 )
436 dc
.DrawLine(0, y
, size
.x
, y
);
439 dc
.SetTextForeground(*wxRED
);
440 dc
.SetBackgroundMode(wxSOLID
);
441 dc
.DrawText("This text is drawn from OnEraseBackground", 60, 160);