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 // ----------------------------------------------------------------------------
38 // the application icon
39 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
40 #include "mondrian.xpm"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 class MyApp
: public wxApp
50 virtual bool OnInit();
54 class MyCanvas
: public wxScrolledWindow
57 MyCanvas(wxFrame
*parent
);
59 void UseBuffer(bool useBuffer
) { m_useBuffer
= useBuffer
; Refresh(); }
60 bool UsesBuffer() const { return m_useBuffer
; }
62 void EraseBgInPaint(bool erase
) { m_eraseBgInPaint
= erase
; Refresh(); }
65 void OnPaint( wxPaintEvent
&event
);
66 void OnChar( wxKeyEvent
&event
);
67 void OnEraseBackground( wxEraseEvent
&event
);
69 void DoPaint(wxDC
& dc
);
75 // use wxMemoryDC in OnPaint()?
78 // erase background in OnPaint()?
79 bool m_eraseBgInPaint
;
85 class MyFrame
: public wxFrame
91 void OnUseBuffer(wxCommandEvent
& event
);
92 void OnEraseBgInPaint(wxCommandEvent
& event
);
93 void OnChangeBgStyle(wxCommandEvent
& event
);
94 void OnQuit(wxCommandEvent
& event
);
95 void OnAbout(wxCommandEvent
& event
);
97 // we can only use double-buffering with wxBG_STYLE_PAINT
98 void OnUpdateUIUseBuffer(wxUpdateUIEvent
& event
)
100 event
.Enable( m_canvas
->GetBackgroundStyle() == wxBG_STYLE_PAINT
);
103 void OnUpdateUIChangeBgStyle(wxUpdateUIEvent
& event
)
105 event
.Enable( !m_canvas
->UsesBuffer() );
110 DECLARE_EVENT_TABLE()
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
121 Erase_Menu_UseBuffer
= 100,
122 Erase_Menu_EraseBgInPaint
,
123 Erase_Menu_BgStyleErase
,
124 Erase_Menu_BgStyleSystem
,
125 Erase_Menu_BgStylePaint
,
126 Erase_Menu_Exit
= wxID_EXIT
,
127 Erase_Menu_About
= wxID_ABOUT
131 // ----------------------------------------------------------------------------
132 // the application class
133 // ----------------------------------------------------------------------------
139 if ( !wxApp::OnInit() )
142 MyFrame
*frame
= new MyFrame
;
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
154 EVT_MENU(Erase_Menu_UseBuffer
, MyFrame::OnUseBuffer
)
155 EVT_MENU(Erase_Menu_EraseBgInPaint
, MyFrame::OnEraseBgInPaint
)
156 EVT_MENU_RANGE(Erase_Menu_BgStyleErase
, Erase_Menu_BgStylePaint
,
157 MyFrame::OnChangeBgStyle
)
159 EVT_MENU(Erase_Menu_Exit
, MyFrame::OnQuit
)
160 EVT_MENU(Erase_Menu_About
, MyFrame::OnAbout
)
162 EVT_UPDATE_UI(Erase_Menu_UseBuffer
, MyFrame::OnUpdateUIUseBuffer
)
163 EVT_UPDATE_UI_RANGE(Erase_Menu_BgStyleErase
, Erase_Menu_BgStylePaint
,
164 MyFrame::OnUpdateUIChangeBgStyle
)
169 : wxFrame(NULL
, wxID_ANY
, "Erase sample",
170 wxPoint(50, 50), wxSize(450, 340))
172 SetIcon(wxICON(mondrian
));
174 wxMenu
*menuFile
= new wxMenu("", wxMENU_TEAROFF
);
175 menuFile
->AppendCheckItem(Erase_Menu_UseBuffer
, "&Use memory DC\tCtrl-M");
176 menuFile
->AppendCheckItem(Erase_Menu_EraseBgInPaint
,
177 "&Erase background in EVT_PAINT\tCtrl-R");
178 menuFile
->AppendSeparator();
179 menuFile
->AppendRadioItem(Erase_Menu_BgStyleErase
,
180 "Use wxBG_STYLE_&ERASE\tCtrl-E");
181 menuFile
->AppendRadioItem(Erase_Menu_BgStyleSystem
,
182 "Use wxBG_STYLE_&SYSTEM\tCtrl-S");
183 menuFile
->AppendRadioItem(Erase_Menu_BgStylePaint
,
184 "Use wxBG_STYLE_&PAINT\tCtrl-P");
185 menuFile
->AppendSeparator();
186 menuFile
->Append(Erase_Menu_Exit
, "E&xit\tAlt-X", "Quit this program");
189 wxMenu
*helpMenu
= new wxMenu
;
190 helpMenu
->Append(Erase_Menu_About
, "&About...\tCtrl-A", "Show about dialog");
192 wxMenuBar
*menuBar
= new wxMenuBar();
193 menuBar
->Append(menuFile
, "&File");
194 menuBar
->Append(helpMenu
, "&Help");
198 m_canvas
= new MyCanvas( this );
202 void MyFrame::OnUseBuffer(wxCommandEvent
& event
)
204 m_canvas
->UseBuffer(event
.IsChecked());
207 void MyFrame::OnEraseBgInPaint(wxCommandEvent
& event
)
209 m_canvas
->EraseBgInPaint(event
.IsChecked());
212 void MyFrame::OnChangeBgStyle(wxCommandEvent
& event
)
214 int style
= wxBG_STYLE_ERASE
+ event
.GetId() - Erase_Menu_BgStyleErase
;
215 m_canvas
->SetBackgroundStyle(static_cast<wxBackgroundStyle
>(style
));
220 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
225 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
229 "This sample shows differences between different background styles "
230 "and how you may draw custom background.\n"
232 "(c) 1998 Robert Roebling\n"
233 "(c) 2009 Vadim Zeitlin\n",
234 "About Erase Sample",
235 wxOK
| wxICON_INFORMATION
,
241 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
242 EVT_PAINT(MyCanvas::OnPaint
)
243 EVT_CHAR(MyCanvas::OnChar
)
244 EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground
)
247 MyCanvas::MyCanvas(wxFrame
*parent
)
248 : wxScrolledWindow(parent
, wxID_ANY
)
251 m_eraseBgInPaint
= false;
253 SetScrollbars( 10, 10, 40, 100, 0, 0 );
255 m_bitmap
= wxBitmap( wxICON(mondrian
) );
257 new wxStaticBitmap( this, wxID_ANY
, m_bitmap
, wxPoint(80,20) );
259 SetFocusIgnoringChildren();
260 SetBackgroundColour(*wxBLUE
);
263 void MyCanvas::OnChar( wxKeyEvent
&event
)
268 m_text
+= event
.m_uniChar
;
275 switch (event
.m_keyCode
)
277 case WXK_UP
: m_text
+= wxT( "<UP>" ); break;
278 case WXK_LEFT
: m_text
+= wxT( "<LEFT>" ); break;
279 case WXK_RIGHT
: m_text
+= wxT( "<RIGHT>" ); break;
280 case WXK_DOWN
: m_text
+= wxT( "<DOWN>" ); break;
281 case WXK_RETURN
: m_text
+= wxT( "<ENTER>" ); break;
282 default: m_text
+= (wxChar
)event
.m_keyCode
; break;
286 void MyCanvas::DoPaint(wxDC
& dc
)
288 if ( m_eraseBgInPaint
)
290 dc
.SetBackground(*wxLIGHT_GREY
);
293 dc
.DrawText("Background erased in OnPaint", 65, 110);
295 else if ( GetBackgroundStyle() == wxBG_STYLE_PAINT
)
297 dc
.SetTextForeground(*wxRED
);
298 dc
.DrawText("You must enable erasing background in OnPaint to avoid "
299 "display corruption", 65, 110);
302 dc
.SetBrush( *wxBLACK_BRUSH
);
303 dc
.DrawRectangle( 10,10,60,50 );
305 dc
.DrawBitmap( m_bitmap
, 20, 20, true );
307 dc
.SetTextForeground(*wxWHITE
);
308 dc
.DrawText("This text is drawn from OnPaint", 65, 65);
311 tmp
.Printf("Hit any key to display more text: %s", m_text
);
314 dc
.GetTextExtent( tmp
, &w
, &h
);
315 dc
.DrawRectangle( 65, 85, w
, h
);
316 dc
.DrawText( tmp
, 65, 85 );
319 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
323 wxAutoBufferedPaintDC
dc(this);
337 void MyCanvas::OnEraseBackground( wxEraseEvent
& event
)
341 GetBackgroundStyle() == wxBG_STYLE_ERASE
,
342 "shouldn't be called unless background style is \"erase\""
345 wxDC
& dc
= *event
.GetDC();
346 dc
.SetPen(*wxGREEN_PEN
);
350 // clear any junk currently displayed
353 const wxSize size
= GetClientSize();
354 for ( int x
= 0; x
< size
.x
; x
+= 15 )
356 dc
.DrawLine(x
, 0, x
, size
.y
);
359 for ( int y
= 0; y
< size
.y
; y
+= 15 )
361 dc
.DrawLine(0, y
, size
.x
, y
);
364 dc
.SetTextForeground(*wxRED
);
365 dc
.SetBackgroundMode(wxSOLID
);
366 dc
.DrawText("This text is drawn from OnEraseBackground", 60, 160);