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 EraseBgInPaint(bool erase
) { m_eraseBgInPaint
= erase
; Refresh(); }
66 void OnPaint( wxPaintEvent
&event
);
67 void OnChar( wxKeyEvent
&event
);
68 void OnEraseBackground( wxEraseEvent
&event
);
70 void DoPaint(wxDC
& dc
);
76 // use wxMemoryDC in OnPaint()?
79 // erase background in OnPaint()?
80 bool m_eraseBgInPaint
;
86 class MyFrame
: public wxFrame
92 void OnUseBuffer(wxCommandEvent
& event
);
93 void OnEraseBgInPaint(wxCommandEvent
& event
);
94 void OnChangeBgStyle(wxCommandEvent
& event
);
95 void OnQuit(wxCommandEvent
& event
);
96 void OnAbout(wxCommandEvent
& event
);
98 // we can only use double-buffering with wxBG_STYLE_PAINT
99 void OnUpdateUIUseBuffer(wxUpdateUIEvent
& event
)
101 event
.Enable( m_canvas
->GetBackgroundStyle() == wxBG_STYLE_PAINT
);
104 void OnUpdateUIChangeBgStyle(wxUpdateUIEvent
& event
)
106 event
.Enable( !m_canvas
->UsesBuffer() );
111 DECLARE_EVENT_TABLE()
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
122 Erase_Menu_UseBuffer
= 100,
123 Erase_Menu_EraseBgInPaint
,
124 Erase_Menu_BgStyleErase
,
125 Erase_Menu_BgStyleSystem
,
126 Erase_Menu_BgStylePaint
,
127 Erase_Menu_Exit
= wxID_EXIT
,
128 Erase_Menu_About
= wxID_ABOUT
132 // ----------------------------------------------------------------------------
133 // the application class
134 // ----------------------------------------------------------------------------
140 if ( !wxApp::OnInit() )
143 MyFrame
*frame
= new MyFrame
;
150 // ----------------------------------------------------------------------------
152 // ----------------------------------------------------------------------------
154 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
155 EVT_MENU(Erase_Menu_UseBuffer
, MyFrame::OnUseBuffer
)
156 EVT_MENU(Erase_Menu_EraseBgInPaint
, MyFrame::OnEraseBgInPaint
)
157 EVT_MENU_RANGE(Erase_Menu_BgStyleErase
, Erase_Menu_BgStylePaint
,
158 MyFrame::OnChangeBgStyle
)
160 EVT_MENU(Erase_Menu_Exit
, MyFrame::OnQuit
)
161 EVT_MENU(Erase_Menu_About
, MyFrame::OnAbout
)
163 EVT_UPDATE_UI(Erase_Menu_UseBuffer
, MyFrame::OnUpdateUIUseBuffer
)
164 EVT_UPDATE_UI_RANGE(Erase_Menu_BgStyleErase
, Erase_Menu_BgStylePaint
,
165 MyFrame::OnUpdateUIChangeBgStyle
)
170 : wxFrame(NULL
, wxID_ANY
, "Erase sample",
171 wxPoint(50, 50), wxSize(450, 340))
173 SetIcon(wxICON(sample
));
175 wxMenu
*menuFile
= new wxMenu("", wxMENU_TEAROFF
);
176 menuFile
->AppendCheckItem(Erase_Menu_UseBuffer
, "&Use memory DC\tCtrl-M");
177 menuFile
->AppendCheckItem(Erase_Menu_EraseBgInPaint
,
178 "&Erase background in EVT_PAINT\tCtrl-R");
179 menuFile
->AppendSeparator();
180 menuFile
->AppendRadioItem(Erase_Menu_BgStyleErase
,
181 "Use wxBG_STYLE_&ERASE\tCtrl-E");
182 menuFile
->AppendRadioItem(Erase_Menu_BgStyleSystem
,
183 "Use wxBG_STYLE_&SYSTEM\tCtrl-S");
184 menuFile
->AppendRadioItem(Erase_Menu_BgStylePaint
,
185 "Use wxBG_STYLE_&PAINT\tCtrl-P");
186 menuFile
->AppendSeparator();
187 menuFile
->Append(Erase_Menu_Exit
, "E&xit\tAlt-X", "Quit this program");
190 wxMenu
*helpMenu
= new wxMenu
;
191 helpMenu
->Append(Erase_Menu_About
, "&About...\tCtrl-A", "Show about dialog");
193 wxMenuBar
*menuBar
= new wxMenuBar();
194 menuBar
->Append(menuFile
, "&File");
195 menuBar
->Append(helpMenu
, "&Help");
199 m_canvas
= new MyCanvas( this );
203 void MyFrame::OnUseBuffer(wxCommandEvent
& event
)
205 m_canvas
->UseBuffer(event
.IsChecked());
208 void MyFrame::OnEraseBgInPaint(wxCommandEvent
& event
)
210 m_canvas
->EraseBgInPaint(event
.IsChecked());
213 void MyFrame::OnChangeBgStyle(wxCommandEvent
& event
)
215 int style
= wxBG_STYLE_ERASE
+ event
.GetId() - Erase_Menu_BgStyleErase
;
216 m_canvas
->SetBackgroundStyle(static_cast<wxBackgroundStyle
>(style
));
221 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
226 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
230 "This sample shows differences between different background styles "
231 "and how you may draw custom background.\n"
233 "(c) 1998 Robert Roebling\n"
234 "(c) 2009 Vadim Zeitlin\n",
235 "About Erase Sample",
236 wxOK
| wxICON_INFORMATION
,
242 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
243 EVT_PAINT(MyCanvas::OnPaint
)
244 EVT_CHAR(MyCanvas::OnChar
)
245 EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground
)
248 MyCanvas::MyCanvas(wxFrame
*parent
)
249 : wxScrolledWindow(parent
, wxID_ANY
)
252 m_eraseBgInPaint
= false;
254 SetScrollbars( 10, 10, 40, 100, 0, 0 );
256 m_bitmap
= wxBitmap( wxICON(sample
) );
258 new wxStaticBitmap( this, wxID_ANY
, m_bitmap
, wxPoint(80,20) );
260 SetFocusIgnoringChildren();
261 SetBackgroundColour(*wxBLUE
);
264 void MyCanvas::OnChar( wxKeyEvent
&event
)
269 m_text
+= event
.m_uniChar
;
276 switch (event
.m_keyCode
)
278 case WXK_UP
: m_text
+= wxT( "<UP>" ); break;
279 case WXK_LEFT
: m_text
+= wxT( "<LEFT>" ); break;
280 case WXK_RIGHT
: m_text
+= wxT( "<RIGHT>" ); break;
281 case WXK_DOWN
: m_text
+= wxT( "<DOWN>" ); break;
282 case WXK_RETURN
: m_text
+= wxT( "<ENTER>" ); break;
283 default: m_text
+= (wxChar
)event
.m_keyCode
; break;
287 void MyCanvas::DoPaint(wxDC
& dc
)
289 if ( m_eraseBgInPaint
)
291 dc
.SetBackground(*wxLIGHT_GREY
);
294 dc
.DrawText("Background erased in OnPaint", 65, 110);
296 else if ( GetBackgroundStyle() == wxBG_STYLE_PAINT
)
298 dc
.SetTextForeground(*wxRED
);
299 dc
.DrawText("You must enable erasing background in OnPaint to avoid "
300 "display corruption", 65, 110);
303 dc
.SetBrush( *wxBLACK_BRUSH
);
304 dc
.DrawRectangle( 10,10,60,50 );
306 dc
.DrawBitmap( m_bitmap
, 20, 20, true );
308 dc
.SetTextForeground(*wxWHITE
);
309 dc
.DrawText("This text is drawn from OnPaint", 65, 65);
312 tmp
.Printf("Hit any key to display more text: %s", m_text
);
315 dc
.GetTextExtent( tmp
, &w
, &h
);
316 dc
.DrawRectangle( 65, 85, w
, h
);
317 dc
.DrawText( tmp
, 65, 85 );
320 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
324 wxAutoBufferedPaintDC
dc(this);
338 void MyCanvas::OnEraseBackground( wxEraseEvent
& event
)
342 GetBackgroundStyle() == wxBG_STYLE_ERASE
,
343 "shouldn't be called unless background style is \"erase\""
346 wxDC
& dc
= *event
.GetDC();
347 dc
.SetPen(*wxGREEN_PEN
);
351 // clear any junk currently displayed
354 const wxSize size
= GetClientSize();
355 for ( int x
= 0; x
< size
.x
; x
+= 15 )
357 dc
.DrawLine(x
, 0, x
, size
.y
);
360 for ( int y
= 0; y
< size
.y
; y
+= 15 )
362 dc
.DrawLine(0, y
, size
.x
, y
);
365 dc
.SetTextForeground(*wxRED
);
366 dc
.SetBackgroundMode(wxSOLID
);
367 dc
.DrawText("This text is drawn from OnEraseBackground", 60, 160);