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
; }
63 void OnPaint( wxPaintEvent
&event
);
64 void OnChar( wxKeyEvent
&event
);
65 void OnEraseBackground( wxEraseEvent
&event
);
67 void DoPaint(wxDC
& dc
);
73 // use wxMemoryDC in OnPaint()?
80 class MyFrame
: public wxFrame
86 void OnUseBuffer(wxCommandEvent
& event
);
87 void OnChangeBgStyle(wxCommandEvent
& event
);
88 void OnQuit(wxCommandEvent
& event
);
89 void OnAbout(wxCommandEvent
& event
);
91 // we can only use double-buffering with wxBG_STYLE_PAINT
92 void OnUpdateUIUseBuffer(wxUpdateUIEvent
& event
)
94 event
.Enable( m_canvas
->GetBackgroundStyle() == wxBG_STYLE_PAINT
);
97 void OnUpdateUIChangeBgStyle(wxUpdateUIEvent
& event
)
99 event
.Enable( !m_canvas
->UsesBuffer() );
104 DECLARE_EVENT_TABLE()
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
115 Erase_Menu_UseBuffer
= 100,
116 Erase_Menu_BgStyleErase
,
117 Erase_Menu_BgStyleSystem
,
118 Erase_Menu_BgStylePaint
,
119 Erase_Menu_Exit
= wxID_EXIT
,
120 Erase_Menu_About
= wxID_ABOUT
124 // ----------------------------------------------------------------------------
125 // the application class
126 // ----------------------------------------------------------------------------
132 if ( !wxApp
::OnInit() )
135 MyFrame
*frame
= new MyFrame
;
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
147 EVT_MENU(Erase_Menu_UseBuffer
, MyFrame
::OnUseBuffer
)
148 EVT_MENU_RANGE(Erase_Menu_BgStyleErase
, Erase_Menu_BgStylePaint
,
149 MyFrame
::OnChangeBgStyle
)
151 EVT_MENU(Erase_Menu_Exit
, MyFrame
::OnQuit
)
152 EVT_MENU(Erase_Menu_About
, MyFrame
::OnAbout
)
154 EVT_UPDATE_UI(Erase_Menu_UseBuffer
, MyFrame
::OnUpdateUIUseBuffer
)
155 EVT_UPDATE_UI_RANGE(Erase_Menu_BgStyleErase
, Erase_Menu_BgStylePaint
,
156 MyFrame
::OnUpdateUIChangeBgStyle
)
161 : wxFrame(NULL
, wxID_ANY
, "Erase sample",
162 wxPoint(50, 50), wxSize(450, 340))
164 SetIcon(wxICON(mondrian
));
166 wxMenu
*menuFile
= new wxMenu("", wxMENU_TEAROFF
);
167 menuFile
->AppendCheckItem(Erase_Menu_UseBuffer
, "&Use memory DC\tCtrl-M");
168 menuFile
->AppendSeparator();
169 menuFile
->AppendRadioItem(Erase_Menu_BgStyleErase
,
170 "Use wxBG_STYLE_&ERASE\tCtrl-E");
171 menuFile
->AppendRadioItem(Erase_Menu_BgStyleSystem
,
172 "Use wxBG_STYLE_&SYSTEM\tCtrl-S");
173 menuFile
->AppendRadioItem(Erase_Menu_BgStylePaint
,
174 "Use wxBG_STYLE_&PAINT\tCtrl-P");
175 menuFile
->AppendSeparator();
176 menuFile
->Append(Erase_Menu_Exit
, "E&xit\tAlt-X", "Quit this program");
179 wxMenu
*helpMenu
= new wxMenu
;
180 helpMenu
->Append(Erase_Menu_About
, "&About...\tCtrl-A", "Show about dialog");
182 wxMenuBar
*menuBar
= new wxMenuBar();
183 menuBar
->Append(menuFile
, "&File");
184 menuBar
->Append(helpMenu
, "&Help");
188 m_canvas
= new MyCanvas( this );
192 void MyFrame
::OnUseBuffer(wxCommandEvent
& event
)
194 m_canvas
->UseBuffer(event
.IsChecked());
197 void MyFrame
::OnChangeBgStyle(wxCommandEvent
& event
)
199 int style
= wxBG_STYLE_ERASE
+ event
.GetId() - Erase_Menu_BgStyleErase
;
200 m_canvas
->SetBackgroundStyle(static_cast<wxBackgroundStyle
>(style
));
205 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
210 void MyFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
))
214 "This sample shows differences between different background styles "
215 "and how you may draw custom background.\n"
217 "(c) 1998 Robert Roebling\n"
218 "(c) 2009 Vadim Zeitlin\n",
219 "About Erase Sample",
220 wxOK
| wxICON_INFORMATION
,
226 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
227 EVT_PAINT(MyCanvas
::OnPaint
)
228 EVT_CHAR(MyCanvas
::OnChar
)
229 EVT_ERASE_BACKGROUND(MyCanvas
::OnEraseBackground
)
232 MyCanvas
::MyCanvas(wxFrame
*parent
)
233 : wxScrolledWindow(parent
, wxID_ANY
)
237 SetScrollbars( 10, 10, 40, 100, 0, 0 );
239 m_bitmap
= wxBitmap( wxICON(mondrian
) );
241 new wxStaticBitmap( this, wxID_ANY
, m_bitmap
, wxPoint(80,20) );
243 SetFocusIgnoringChildren();
244 SetBackgroundColour(*wxBLUE
);
247 void MyCanvas
::OnChar( wxKeyEvent
&event
)
252 m_text
+= event
.m_uniChar
;
259 switch (event
.m_keyCode
)
261 case WXK_UP
: m_text
+= wxT( "<UP>" ); break;
262 case WXK_LEFT
: m_text
+= wxT( "<LEFT>" ); break;
263 case WXK_RIGHT
: m_text
+= wxT( "<RIGHT>" ); break;
264 case WXK_DOWN
: m_text
+= wxT( "<DOWN>" ); break;
265 case WXK_RETURN
: m_text
+= wxT( "<ENTER>" ); break;
266 default: m_text
+= (wxChar
)event
.m_keyCode
; break;
270 void MyCanvas
::DoPaint(wxDC
& dc
)
272 dc
.SetBrush( *wxBLACK_BRUSH
);
273 dc
.DrawRectangle( 10,10,60,50 );
275 dc
.DrawBitmap( m_bitmap
, 20, 20, true );
277 dc
.SetTextForeground(*wxWHITE
);
278 dc
.DrawText("This text is drawn from OnPaint", 65, 65);
281 tmp
.Printf("Hit any key to display more text: %s", m_text
);
284 dc
.GetTextExtent( tmp
, &w
, &h
);
285 dc
.DrawRectangle( 65, 85, w
, h
);
286 dc
.DrawText( tmp
, 65, 85 );
289 void MyCanvas
::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
293 wxAutoBufferedPaintDC
dc(this);
307 void MyCanvas
::OnEraseBackground( wxEraseEvent
& event
)
311 GetBackgroundStyle() == wxBG_STYLE_ERASE
,
312 "shouldn't be called unless background style is \"erase\""
315 wxDC
& dc
= *event
.GetDC();
316 dc
.SetPen(*wxGREEN_PEN
);
320 // clear any junk currently displayed
323 const wxSize size
= GetClientSize();
324 for ( int x
= 0; x
< size
.x
; x
+= 15 )
326 dc
.DrawLine(x
, 0, x
, size
.y
);
329 for ( int y
= 0; y
< size
.y
; y
+= 15 )
331 dc
.DrawLine(0, y
, size
.x
, y
);
334 dc
.SetTextForeground(*wxRED
);
335 dc
.SetBackgroundMode(wxSOLID
);
336 dc
.DrawText("This text is drawn from OnEraseBackground", 60, 160);