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 OnChar( wxKeyEvent
&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);
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()
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
152 Erase_Menu_UseBuffer
= 100,
153 Erase_Menu_UseBgBitmap
,
154 Erase_Menu_EraseBgInPaint
,
155 Erase_Menu_BgStyleErase
,
156 Erase_Menu_BgStyleSystem
,
157 Erase_Menu_BgStylePaint
,
158 Erase_Menu_Exit
= wxID_EXIT
,
159 Erase_Menu_About
= wxID_ABOUT
163 // ----------------------------------------------------------------------------
164 // the application class
165 // ----------------------------------------------------------------------------
171 if ( !wxApp
::OnInit() )
174 MyFrame
*frame
= new MyFrame
;
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
185 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
186 EVT_MENU(Erase_Menu_UseBuffer
, MyFrame
::OnUseBuffer
)
187 EVT_MENU(Erase_Menu_UseBgBitmap
, MyFrame
::OnUseBgBitmap
)
188 EVT_MENU(Erase_Menu_EraseBgInPaint
, MyFrame
::OnEraseBgInPaint
)
189 EVT_MENU_RANGE(Erase_Menu_BgStyleErase
, Erase_Menu_BgStylePaint
,
190 MyFrame
::OnChangeBgStyle
)
192 EVT_MENU(Erase_Menu_Exit
, MyFrame
::OnQuit
)
193 EVT_MENU(Erase_Menu_About
, MyFrame
::OnAbout
)
195 EVT_UPDATE_UI(Erase_Menu_UseBuffer
, MyFrame
::OnUpdateUIUseBuffer
)
196 EVT_UPDATE_UI_RANGE(Erase_Menu_BgStyleErase
, Erase_Menu_BgStylePaint
,
197 MyFrame
::OnUpdateUIChangeBgStyle
)
202 : wxFrame(NULL
, wxID_ANY
, "Erase sample",
203 wxPoint(50, 50), wxSize(450, 340))
205 SetIcon(wxICON(sample
));
207 wxMenu
*menuFile
= new wxMenu("", wxMENU_TEAROFF
);
208 menuFile
->AppendCheckItem(Erase_Menu_UseBuffer
, "&Use memory DC\tCtrl-M");
209 menuFile
->AppendCheckItem(Erase_Menu_UseBgBitmap
,
210 "Use background &bitmap\tCtrl-B");
211 menuFile
->AppendCheckItem(Erase_Menu_EraseBgInPaint
,
212 "&Erase background in EVT_PAINT\tCtrl-R");
213 menuFile
->AppendSeparator();
214 menuFile
->AppendRadioItem(Erase_Menu_BgStyleErase
,
215 "Use wxBG_STYLE_&ERASE\tCtrl-E");
216 menuFile
->AppendRadioItem(Erase_Menu_BgStyleSystem
,
217 "Use wxBG_STYLE_&SYSTEM\tCtrl-S");
218 menuFile
->AppendRadioItem(Erase_Menu_BgStylePaint
,
219 "Use wxBG_STYLE_&PAINT\tCtrl-P");
220 menuFile
->AppendSeparator();
221 menuFile
->Append(Erase_Menu_Exit
, "E&xit\tAlt-X", "Quit this program");
224 wxMenu
*helpMenu
= new wxMenu
;
225 helpMenu
->Append(Erase_Menu_About
, "&About...\tCtrl-A", "Show about dialog");
227 wxMenuBar
*menuBar
= new wxMenuBar();
228 menuBar
->Append(menuFile
, "&File");
229 menuBar
->Append(helpMenu
, "&Help");
233 m_canvas
= new MyCanvas( this );
237 void MyFrame
::OnUseBuffer(wxCommandEvent
& event
)
239 m_canvas
->UseBuffer(event
.IsChecked());
242 void MyFrame
::OnUseBgBitmap(wxCommandEvent
& event
)
244 m_canvas
->UseBgBitmap(event
.IsChecked());
247 void MyFrame
::OnEraseBgInPaint(wxCommandEvent
& event
)
249 m_canvas
->EraseBgInPaint(event
.IsChecked());
252 void MyFrame
::OnChangeBgStyle(wxCommandEvent
& event
)
254 int style
= wxBG_STYLE_ERASE
+ event
.GetId() - Erase_Menu_BgStyleErase
;
255 m_canvas
->SetBackgroundStyle(static_cast<wxBackgroundStyle
>(style
));
260 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
265 void MyFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
))
269 "This sample shows differences between different background styles "
270 "and how you may draw custom background.\n"
272 "(c) 1998 Robert Roebling\n"
273 "(c) 2009 Vadim Zeitlin\n",
274 "About Erase Sample",
275 wxOK
| wxICON_INFORMATION
,
281 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
282 EVT_PAINT(MyCanvas
::OnPaint
)
283 EVT_CHAR(MyCanvas
::OnChar
)
284 EVT_ERASE_BACKGROUND(MyCanvas
::OnEraseBackground
)
287 MyCanvas
::MyCanvas(wxFrame
*parent
)
288 : wxScrolledWindow(parent
, wxID_ANY
)
292 m_eraseBgInPaint
= false;
294 SetScrollbars( 10, 10, 40, 100, 0, 0 );
296 m_bitmap
= wxBitmap( wxICON(sample
) );
298 new wxStaticBitmap( this, wxID_ANY
, m_bitmap
, wxPoint(80,20) );
300 new wxStaticText(this, wxID_ANY
,
301 "Left bitmap is a wxStaticBitmap,\n"
302 "right one drawn directly",
305 SetFocusIgnoringChildren();
306 SetBackgroundColour(*wxCYAN
);
309 void MyCanvas
::OnChar( wxKeyEvent
&event
)
314 m_text
+= event
.m_uniChar
;
321 switch (event
.m_keyCode
)
323 case WXK_UP
: m_text
+= wxT( "<UP>" ); break;
324 case WXK_LEFT
: m_text
+= wxT( "<LEFT>" ); break;
325 case WXK_RIGHT
: m_text
+= wxT( "<RIGHT>" ); break;
326 case WXK_DOWN
: m_text
+= wxT( "<DOWN>" ); break;
327 case WXK_RETURN
: m_text
+= wxT( "<ENTER>" ); break;
328 default: m_text
+= (wxChar
)event
.m_keyCode
; break;
332 void MyCanvas
::DoPaint(wxDC
& dc
)
334 if ( m_eraseBgInPaint
)
336 dc
.SetBackground(*wxLIGHT_GREY
);
339 dc
.DrawText("Background erased in OnPaint", 65, 110);
341 else if ( GetBackgroundStyle() == wxBG_STYLE_PAINT
)
343 dc
.SetTextForeground(*wxRED
);
344 dc
.DrawText("You must enable erasing background in OnPaint to avoid "
345 "display corruption", 65, 110);
348 dc
.SetBrush( *wxBLACK_BRUSH
);
350 dc
.DrawBitmap( m_bitmap
, 20, 20, true );
352 dc
.SetTextForeground(*wxRED
);
353 dc
.DrawText("This text is drawn from OnPaint", 65, 65);
356 tmp
.Printf("Hit any key to display more text: %s", m_text
);
359 dc
.GetTextExtent( tmp
, &w
, &h
);
360 dc
.DrawRectangle( 65, 85, w
, h
);
361 dc
.DrawText( tmp
, 65, 85 );
364 void MyCanvas
::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
368 wxAutoBufferedPaintDC
dc(this);
382 void MyCanvas
::OnEraseBackground( wxEraseEvent
& event
)
384 // We must not erase the background ourselves if we asked wxPanel to erase
385 // it using a background bitmap.
394 GetBackgroundStyle() == wxBG_STYLE_ERASE
,
395 "shouldn't be called unless background style is \"erase\""
398 wxDC
& dc
= *event
.GetDC();
399 dc
.SetPen(*wxGREEN_PEN
);
403 // clear any junk currently displayed
406 const wxSize size
= GetClientSize();
407 for ( int x
= 0; x
< size
.x
; x
+= 15 )
409 dc
.DrawLine(x
, 0, x
, size
.y
);
412 for ( int y
= 0; y
< size
.y
; y
+= 15 )
414 dc
.DrawLine(0, y
, size
.x
, y
);
417 dc
.SetTextForeground(*wxRED
);
418 dc
.SetBackgroundMode(wxSOLID
);
419 dc
.DrawText("This text is drawn from OnEraseBackground", 60, 160);