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"
35 #include "wx/artprov.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // the application icon
42 #if !defined(__WXMSW__) && !defined(__WXPM__)
43 #include "../sample.xpm"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 class MyApp
: public wxApp
53 virtual bool OnInit();
57 class MyCanvas
: public wxCustomBackgroundWindow
<wxScrolledWindow
>
60 MyCanvas(wxFrame
*parent
);
62 void UseBuffer(bool useBuffer
) { m_useBuffer
= useBuffer
; Refresh(); }
63 bool UsesBuffer() const { return m_useBuffer
; }
65 void UseBgBitmap(bool useBgBmp
)
67 m_useBgBmp
= useBgBmp
;
68 SetBackgroundBitmap(m_useBgBmp
? GetBgBitmap() : wxBitmap());
73 void EraseBgInPaint(bool erase
) { m_eraseBgInPaint
= erase
; Refresh(); }
76 void OnPaint( wxPaintEvent
&event
);
77 void OnEraseBackground( wxEraseEvent
&event
);
79 void DoPaint(wxDC
& dc
);
81 // Create an easily recognizable background bitmap.
82 static wxBitmap
GetBgBitmap()
84 static const int BMP_SIZE
= 40;
86 wxBitmap
bmp(BMP_SIZE
, BMP_SIZE
);
88 dc
.SetBackground(*wxCYAN
);
91 dc
.SetPen(*wxBLUE_PEN
);
92 dc
.DrawLine(0, BMP_SIZE
/2, BMP_SIZE
/2, 0);
93 dc
.DrawLine(BMP_SIZE
/2, 0, BMP_SIZE
, BMP_SIZE
/2);
94 dc
.DrawLine(BMP_SIZE
, BMP_SIZE
/2, BMP_SIZE
/2, BMP_SIZE
);
95 dc
.DrawLine(BMP_SIZE
/2, BMP_SIZE
, 0, BMP_SIZE
/2);
102 // use wxMemoryDC in OnPaint()?
105 // use background bitmap?
108 // erase background in OnPaint()?
109 bool m_eraseBgInPaint
;
112 DECLARE_EVENT_TABLE()
115 class MyFrame
: public wxFrame
121 void OnUseBuffer(wxCommandEvent
& event
);
122 void OnUseBgBitmap(wxCommandEvent
& event
);
123 void OnEraseBgInPaint(wxCommandEvent
& event
);
124 void OnChangeBgStyle(wxCommandEvent
& event
);
125 void OnQuit(wxCommandEvent
& event
);
126 void OnAbout(wxCommandEvent
& event
);
128 // we can only use double-buffering with wxBG_STYLE_PAINT
129 void OnUpdateUIUseBuffer(wxUpdateUIEvent
& event
)
131 event
.Enable( m_canvas
->GetBackgroundStyle() == wxBG_STYLE_PAINT
);
134 void OnUpdateUIChangeBgStyle(wxUpdateUIEvent
& event
)
136 event
.Enable( !m_canvas
->UsesBuffer() );
141 DECLARE_EVENT_TABLE()
144 class ControlWithTransparency
: public wxWindow
147 ControlWithTransparency(wxWindow
*parent
,
152 if ( parent
->IsTransparentBackgroundSupported(&reason
) )
154 SetBackgroundStyle (wxBG_STYLE_TRANSPARENT
);
155 m_message
= "This is custom control with transparency";
159 m_message
= "Transparency not supported, check tooltip.";
162 Create (parent
, wxID_ANY
, pos
, size
, wxBORDER_NONE
);
164 wxPaintEventHandler(ControlWithTransparency::OnPaint
));
166 if ( !reason
.empty() )
168 // This can be only done now, after creating the window.
174 void OnPaint( wxPaintEvent
& WXUNUSED(event
) )
178 dc
.SetPen(*wxRED_PEN
);
179 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
180 dc
.DrawRectangle(GetClientSize());
182 dc
.SetTextForeground(*wxBLUE
);
183 dc
.SetBackgroundMode(wxTRANSPARENT
);
184 dc
.DrawText(m_message
, 0, 2);
186 // Draw some bitmap/icon to ensure transparent bitmaps are indeed
187 // transparent on transparent windows
188 wxBitmap
bmp(wxArtProvider::GetBitmap(wxART_WARNING
, wxART_MENU
));
189 wxIcon
icon(wxArtProvider::GetIcon(wxART_GOTO_LAST
, wxART_MENU
));
190 dc
.DrawBitmap (bmp
, GetSize().x
- 1 - bmp
.GetWidth(), 2);
191 dc
.DrawIcon(icon
, GetSize().x
- 1 - bmp
.GetWidth()-icon
.GetWidth(), 2);
197 // ----------------------------------------------------------------------------
199 // ----------------------------------------------------------------------------
204 Erase_Menu_UseBuffer
= 100,
205 Erase_Menu_UseBgBitmap
,
206 Erase_Menu_EraseBgInPaint
,
207 Erase_Menu_BgStyleErase
,
208 Erase_Menu_BgStyleSystem
,
209 Erase_Menu_BgStylePaint
,
210 Erase_Menu_Exit
= wxID_EXIT
,
211 Erase_Menu_About
= wxID_ABOUT
215 // ----------------------------------------------------------------------------
216 // the application class
217 // ----------------------------------------------------------------------------
223 if ( !wxApp::OnInit() )
226 MyFrame
*frame
= new MyFrame
;
233 // ----------------------------------------------------------------------------
235 // ----------------------------------------------------------------------------
237 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
238 EVT_MENU(Erase_Menu_UseBuffer
, MyFrame::OnUseBuffer
)
239 EVT_MENU(Erase_Menu_UseBgBitmap
, MyFrame::OnUseBgBitmap
)
240 EVT_MENU(Erase_Menu_EraseBgInPaint
, MyFrame::OnEraseBgInPaint
)
241 EVT_MENU_RANGE(Erase_Menu_BgStyleErase
, Erase_Menu_BgStylePaint
,
242 MyFrame::OnChangeBgStyle
)
244 EVT_MENU(Erase_Menu_Exit
, MyFrame::OnQuit
)
245 EVT_MENU(Erase_Menu_About
, MyFrame::OnAbout
)
247 EVT_UPDATE_UI(Erase_Menu_UseBuffer
, MyFrame::OnUpdateUIUseBuffer
)
248 EVT_UPDATE_UI_RANGE(Erase_Menu_BgStyleErase
, Erase_Menu_BgStylePaint
,
249 MyFrame::OnUpdateUIChangeBgStyle
)
254 : wxFrame(NULL
, wxID_ANY
, "Erase sample",
255 wxPoint(50, 50), wxSize(450, 340))
257 SetIcon(wxICON(sample
));
259 wxMenu
*menuFile
= new wxMenu("", wxMENU_TEAROFF
);
260 menuFile
->AppendCheckItem(Erase_Menu_UseBuffer
, "&Use memory DC\tCtrl-M");
261 menuFile
->AppendCheckItem(Erase_Menu_UseBgBitmap
,
262 "Use background &bitmap\tCtrl-B");
263 menuFile
->AppendCheckItem(Erase_Menu_EraseBgInPaint
,
264 "&Erase background in EVT_PAINT\tCtrl-R");
265 menuFile
->AppendSeparator();
266 menuFile
->AppendRadioItem(Erase_Menu_BgStyleErase
,
267 "Use wxBG_STYLE_&ERASE\tCtrl-E");
268 menuFile
->AppendRadioItem(Erase_Menu_BgStyleSystem
,
269 "Use wxBG_STYLE_&SYSTEM\tCtrl-S");
270 menuFile
->AppendRadioItem(Erase_Menu_BgStylePaint
,
271 "Use wxBG_STYLE_&PAINT\tCtrl-P");
272 menuFile
->AppendSeparator();
273 menuFile
->Append(Erase_Menu_Exit
, "E&xit\tAlt-X", "Quit this program");
276 wxMenu
*helpMenu
= new wxMenu
;
277 helpMenu
->Append(Erase_Menu_About
, "&About\tCtrl-A", "Show about dialog");
279 wxMenuBar
*menuBar
= new wxMenuBar();
280 menuBar
->Append(menuFile
, "&File");
281 menuBar
->Append(helpMenu
, "&Help");
285 m_canvas
= new MyCanvas( this );
289 void MyFrame::OnUseBuffer(wxCommandEvent
& event
)
291 m_canvas
->UseBuffer(event
.IsChecked());
294 void MyFrame::OnUseBgBitmap(wxCommandEvent
& event
)
296 m_canvas
->UseBgBitmap(event
.IsChecked());
299 void MyFrame::OnEraseBgInPaint(wxCommandEvent
& event
)
301 m_canvas
->EraseBgInPaint(event
.IsChecked());
304 void MyFrame::OnChangeBgStyle(wxCommandEvent
& event
)
306 int style
= wxBG_STYLE_ERASE
+ event
.GetId() - Erase_Menu_BgStyleErase
;
307 m_canvas
->SetBackgroundStyle(static_cast<wxBackgroundStyle
>(style
));
312 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
317 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
321 "This sample shows differences between different background styles "
322 "and how you may draw custom background.\n"
324 "(c) 1998 Robert Roebling\n"
325 "(c) 2009 Vadim Zeitlin\n",
326 "About Erase Sample",
327 wxOK
| wxICON_INFORMATION
,
333 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
334 EVT_PAINT(MyCanvas::OnPaint
)
335 EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground
)
338 MyCanvas::MyCanvas(wxFrame
*parent
)
340 Create(parent
, wxID_ANY
);
344 m_eraseBgInPaint
= false;
346 SetScrollbars( 10, 10, 40, 100, 0, 0 );
348 m_bitmap
= wxBitmap( wxICON(sample
) );
350 new wxStaticBitmap( this, wxID_ANY
, m_bitmap
, wxPoint(80,20) );
352 new wxStaticText(this, wxID_ANY
,
353 "Left bitmap is a wxStaticBitmap,\n"
354 "right one drawn directly",
357 new ControlWithTransparency(this, wxPoint(65, 125), wxSize(350, 22));
359 SetFocusIgnoringChildren();
360 SetBackgroundColour(*wxCYAN
);
363 void MyCanvas::DoPaint(wxDC
& dc
)
365 if ( m_eraseBgInPaint
)
367 dc
.SetBackground(*wxLIGHT_GREY
);
370 dc
.DrawText("Background erased in OnPaint", 65, 110);
372 else if ( GetBackgroundStyle() == wxBG_STYLE_PAINT
)
374 dc
.SetTextForeground(*wxRED
);
375 dc
.DrawText("You must enable erasing background in OnPaint to avoid "
376 "display corruption", 65, 110);
379 dc
.DrawBitmap( m_bitmap
, 20, 20, true );
381 dc
.SetTextForeground(*wxRED
);
382 dc
.DrawText("This text is drawn from OnPaint", 65, 65);
385 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
389 wxAutoBufferedPaintDC
dc(this);
403 void MyCanvas::OnEraseBackground( wxEraseEvent
& event
)
405 // We must not erase the background ourselves if we asked wxPanel to erase
406 // it using a background bitmap.
415 GetBackgroundStyle() == wxBG_STYLE_ERASE
,
416 "shouldn't be called unless background style is \"erase\""
419 wxDC
& dc
= *event
.GetDC();
420 dc
.SetPen(*wxGREEN_PEN
);
424 // clear any junk currently displayed
427 const wxSize size
= GetClientSize();
428 for ( int x
= 0; x
< size
.x
; x
+= 15 )
430 dc
.DrawLine(x
, 0, x
, size
.y
);
433 for ( int y
= 0; y
< size
.y
; y
+= 15 )
435 dc
.DrawLine(0, y
, size
.x
, y
);
438 dc
.SetTextForeground(*wxRED
);
439 dc
.SetBackgroundMode(wxSOLID
);
440 dc
.DrawText("This text is drawn from OnEraseBackground", 60, 160);