]> git.saurik.com Git - wxWidgets.git/blame - samples/erase/erase.cpp
Deprecate old style wxPanel ctor taking separate coordinates.
[wxWidgets.git] / samples / erase / erase.cpp
CommitLineData
33611ebb 1/////////////////////////////////////////////////////////////////////////////
9c61c5b0 2// Name: samples/erase/erase.cpp
be5a51fb 3// Purpose: Erase wxWidgets sample
9c61c5b0 4// Author: Robert Roebling, Vadim Zeitlin
33611ebb
RR
5// Created: 04/01/98
6// RCS-ID: $Id$
9c61c5b0
VZ
7// Copyright: (c) 1998 Robert Roebling
8// (c) 2009 Vadim Zeitlin
33611ebb
RR
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
33611ebb
RR
20// For compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27// for all others, include the necessary headers (this file is usually all you
be5a51fb 28// need because it includes almost all "standard" wxWidgets headers)
33611ebb
RR
29#ifndef WX_PRECOMP
30 #include "wx/wx.h"
31#endif
32
9c61c5b0
VZ
33#include "wx/dcbuffer.h"
34
33611ebb
RR
35// ----------------------------------------------------------------------------
36// resources
37// ----------------------------------------------------------------------------
3cb332c1 38
33611ebb 39// the application icon
3cb332c1
VZ
40#if !defined(__WXMSW__) && !defined(__WXPM__)
41 #include "../sample.xpm"
33611ebb
RR
42#endif
43
44// ----------------------------------------------------------------------------
45// private classes
46// ----------------------------------------------------------------------------
47
48class MyApp : public wxApp
49{
50public:
51 virtual bool OnInit();
52};
53
54
79c5fe4b 55class MyCanvas : public wxScrolledWindow
33611ebb
RR
56{
57public:
9c61c5b0 58 MyCanvas(wxFrame *parent);
33611ebb 59
79c5fe4b 60 void UseBuffer(bool useBuffer) { m_useBuffer = useBuffer; Refresh(); }
9c61c5b0 61 bool UsesBuffer() const { return m_useBuffer; }
33611ebb 62
c753eb92
VZ
63 void EraseBgInPaint(bool erase) { m_eraseBgInPaint = erase; Refresh(); }
64
33611ebb 65private:
33611ebb 66 void OnPaint( wxPaintEvent &event );
2b5f62a0 67 void OnChar( wxKeyEvent &event );
33611ebb 68 void OnEraseBackground( wxEraseEvent &event );
98dd66cf 69
79c5fe4b
VZ
70 void DoPaint(wxDC& dc);
71
72
b5a49d4c 73 wxBitmap m_bitmap;
2b5f62a0 74 wxString m_text;
33611ebb 75
79c5fe4b
VZ
76 // use wxMemoryDC in OnPaint()?
77 bool m_useBuffer;
78
c753eb92
VZ
79 // erase background in OnPaint()?
80 bool m_eraseBgInPaint;
81
b18eb01c 82
79c5fe4b
VZ
83 DECLARE_EVENT_TABLE()
84};
85
86class MyFrame : public wxFrame
87{
88public:
89 MyFrame();
90
9c61c5b0 91private:
79c5fe4b 92 void OnUseBuffer(wxCommandEvent& event);
c753eb92 93 void OnEraseBgInPaint(wxCommandEvent& event);
9c61c5b0 94 void OnChangeBgStyle(wxCommandEvent& event);
79c5fe4b
VZ
95 void OnQuit(wxCommandEvent& event);
96 void OnAbout(wxCommandEvent& event);
97
9c61c5b0
VZ
98 // we can only use double-buffering with wxBG_STYLE_PAINT
99 void OnUpdateUIUseBuffer(wxUpdateUIEvent& event)
100 {
101 event.Enable( m_canvas->GetBackgroundStyle() == wxBG_STYLE_PAINT );
102 }
103
104 void OnUpdateUIChangeBgStyle(wxUpdateUIEvent& event)
105 {
106 event.Enable( !m_canvas->UsesBuffer() );
107 }
108
79c5fe4b
VZ
109 MyCanvas *m_canvas;
110
33611ebb
RR
111 DECLARE_EVENT_TABLE()
112};
113
79c5fe4b 114
33611ebb
RR
115// ----------------------------------------------------------------------------
116// constants
117// ----------------------------------------------------------------------------
118
119enum
120{
121 // menu items
79c5fe4b 122 Erase_Menu_UseBuffer = 100,
c753eb92 123 Erase_Menu_EraseBgInPaint,
9c61c5b0
VZ
124 Erase_Menu_BgStyleErase,
125 Erase_Menu_BgStyleSystem,
126 Erase_Menu_BgStylePaint,
79c5fe4b
VZ
127 Erase_Menu_Exit = wxID_EXIT,
128 Erase_Menu_About = wxID_ABOUT
33611ebb
RR
129};
130
131
132// ----------------------------------------------------------------------------
133// the application class
134// ----------------------------------------------------------------------------
135
136IMPLEMENT_APP(MyApp)
137
138bool MyApp::OnInit()
139{
45e6e6f8
VZ
140 if ( !wxApp::OnInit() )
141 return false;
142
79c5fe4b 143 MyFrame *frame = new MyFrame;
33611ebb 144
07850a49 145 frame->Show(true);
98dd66cf 146
07850a49 147 return true;
33611ebb
RR
148}
149
150// ----------------------------------------------------------------------------
151// main frame
152// ----------------------------------------------------------------------------
153
154BEGIN_EVENT_TABLE(MyFrame, wxFrame)
c753eb92
VZ
155 EVT_MENU(Erase_Menu_UseBuffer, MyFrame::OnUseBuffer)
156 EVT_MENU(Erase_Menu_EraseBgInPaint, MyFrame::OnEraseBgInPaint)
9c61c5b0
VZ
157 EVT_MENU_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint,
158 MyFrame::OnChangeBgStyle)
159
79c5fe4b
VZ
160 EVT_MENU(Erase_Menu_Exit, MyFrame::OnQuit)
161 EVT_MENU(Erase_Menu_About, MyFrame::OnAbout)
9c61c5b0
VZ
162
163 EVT_UPDATE_UI(Erase_Menu_UseBuffer, MyFrame::OnUpdateUIUseBuffer)
164 EVT_UPDATE_UI_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint,
165 MyFrame::OnUpdateUIChangeBgStyle)
33611ebb
RR
166END_EVENT_TABLE()
167
168// frame constructor
79c5fe4b 169MyFrame::MyFrame()
9c61c5b0 170 : wxFrame(NULL, wxID_ANY, "Erase sample",
79c5fe4b 171 wxPoint(50, 50), wxSize(450, 340))
33611ebb 172{
3cb332c1 173 SetIcon(wxICON(sample));
33611ebb 174
9c61c5b0
VZ
175 wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
176 menuFile->AppendCheckItem(Erase_Menu_UseBuffer, "&Use memory DC\tCtrl-M");
c753eb92
VZ
177 menuFile->AppendCheckItem(Erase_Menu_EraseBgInPaint,
178 "&Erase background in EVT_PAINT\tCtrl-R");
9c61c5b0
VZ
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");
79c5fe4b 186 menuFile->AppendSeparator();
9c61c5b0 187 menuFile->Append(Erase_Menu_Exit, "E&xit\tAlt-X", "Quit this program");
33611ebb 188
33611ebb 189
79c5fe4b 190 wxMenu *helpMenu = new wxMenu;
9c61c5b0 191 helpMenu->Append(Erase_Menu_About, "&About...\tCtrl-A", "Show about dialog");
33611ebb
RR
192
193 wxMenuBar *menuBar = new wxMenuBar();
9c61c5b0
VZ
194 menuBar->Append(menuFile, "&File");
195 menuBar->Append(helpMenu, "&Help");
33611ebb
RR
196
197 SetMenuBar(menuBar);
198
79c5fe4b 199 m_canvas = new MyCanvas( this );
33611ebb
RR
200}
201
202
79c5fe4b
VZ
203void MyFrame::OnUseBuffer(wxCommandEvent& event)
204{
205 m_canvas->UseBuffer(event.IsChecked());
206}
207
c753eb92
VZ
208void MyFrame::OnEraseBgInPaint(wxCommandEvent& event)
209{
210 m_canvas->EraseBgInPaint(event.IsChecked());
211}
212
9c61c5b0 213void MyFrame::OnChangeBgStyle(wxCommandEvent& event)
b18eb01c 214{
9c61c5b0
VZ
215 int style = wxBG_STYLE_ERASE + event.GetId() - Erase_Menu_BgStyleErase;
216 m_canvas->SetBackgroundStyle(static_cast<wxBackgroundStyle>(style));
217
218 m_canvas->Refresh();
b18eb01c
VZ
219}
220
33611ebb
RR
221void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
222{
07850a49 223 Close(true);
33611ebb
RR
224}
225
226void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
227{
9c61c5b0
VZ
228 wxMessageBox
229 (
230 "This sample shows differences between different background styles "
231 "and how you may draw custom background.\n"
232 "\n"
233 "(c) 1998 Robert Roebling\n"
234 "(c) 2009 Vadim Zeitlin\n",
235 "About Erase Sample",
236 wxOK | wxICON_INFORMATION,
237 this
238 );
33611ebb
RR
239}
240
241
242BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
9c61c5b0
VZ
243 EVT_PAINT(MyCanvas::OnPaint)
244 EVT_CHAR(MyCanvas::OnChar)
245 EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground)
33611ebb
RR
246END_EVENT_TABLE()
247
9c61c5b0
VZ
248MyCanvas::MyCanvas(wxFrame *parent)
249 : wxScrolledWindow(parent, wxID_ANY)
33611ebb 250{
79c5fe4b 251 m_useBuffer = false;
c753eb92 252 m_eraseBgInPaint = false;
79c5fe4b 253
33611ebb 254 SetScrollbars( 10, 10, 40, 100, 0, 0 );
98dd66cf 255
3cb332c1 256 m_bitmap = wxBitmap( wxICON(sample) );
98dd66cf 257
07850a49 258 new wxStaticBitmap( this, wxID_ANY, m_bitmap, wxPoint(80,20) );
11fdee42 259
6fec48b7
VZ
260 new wxStaticText(this, wxID_ANY,
261 "Left bitmap is a wxStaticBitmap,\n"
262 "right one drawn directly",
263 wxPoint(150, 20));
264
ca37cfde 265 SetFocusIgnoringChildren();
6fec48b7 266 SetBackgroundColour(*wxCYAN);
33611ebb
RR
267}
268
2b5f62a0
VZ
269void MyCanvas::OnChar( wxKeyEvent &event )
270{
271#if wxUSE_UNICODE
272 if (event.m_uniChar)
273 {
274 m_text += event.m_uniChar;
275 Refresh();
276 return;
277 }
278#endif
279
280 // some test cases
281 switch (event.m_keyCode)
282 {
283 case WXK_UP: m_text += wxT( "<UP>" ); break;
284 case WXK_LEFT: m_text += wxT( "<LEFT>" ); break;
285 case WXK_RIGHT: m_text += wxT( "<RIGHT>" ); break;
286 case WXK_DOWN: m_text += wxT( "<DOWN>" ); break;
287 case WXK_RETURN: m_text += wxT( "<ENTER>" ); break;
925e9792 288 default: m_text += (wxChar)event.m_keyCode; break;
2b5f62a0 289 }
2b5f62a0
VZ
290}
291
79c5fe4b 292void MyCanvas::DoPaint(wxDC& dc)
33611ebb 293{
c753eb92
VZ
294 if ( m_eraseBgInPaint )
295 {
296 dc.SetBackground(*wxLIGHT_GREY);
297 dc.Clear();
298
299 dc.DrawText("Background erased in OnPaint", 65, 110);
300 }
301 else if ( GetBackgroundStyle() == wxBG_STYLE_PAINT )
302 {
303 dc.SetTextForeground(*wxRED);
304 dc.DrawText("You must enable erasing background in OnPaint to avoid "
305 "display corruption", 65, 110);
306 }
307
b5a49d4c 308 dc.SetBrush( *wxBLACK_BRUSH );
9c61c5b0 309 dc.DrawRectangle( 10,10,60,50 );
98dd66cf 310
9c61c5b0 311 dc.DrawBitmap( m_bitmap, 20, 20, true );
98dd66cf 312
9c61c5b0
VZ
313 dc.SetTextForeground(*wxWHITE);
314 dc.DrawText("This text is drawn from OnPaint", 65, 65);
925e9792 315
2b5f62a0 316 wxString tmp;
9c61c5b0
VZ
317 tmp.Printf("Hit any key to display more text: %s", m_text);
318
2b5f62a0
VZ
319 int w,h;
320 dc.GetTextExtent( tmp, &w, &h );
2b5f62a0
VZ
321 dc.DrawRectangle( 65, 85, w, h );
322 dc.DrawText( tmp, 65, 85 );
33611ebb
RR
323}
324
79c5fe4b
VZ
325void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
326{
79c5fe4b
VZ
327 if ( m_useBuffer )
328 {
9c61c5b0
VZ
329 wxAutoBufferedPaintDC dc(this);
330 PrepareDC(dc);
79c5fe4b
VZ
331
332 DoPaint(dc);
79c5fe4b
VZ
333 }
334 else
335 {
9c61c5b0
VZ
336 wxPaintDC dc(this);
337 PrepareDC(dc);
338
339 DoPaint(dc);
79c5fe4b
VZ
340 }
341}
342
98dd66cf 343void MyCanvas::OnEraseBackground( wxEraseEvent& event )
33611ebb 344{
9c61c5b0
VZ
345 wxASSERT_MSG
346 (
347 GetBackgroundStyle() == wxBG_STYLE_ERASE,
348 "shouldn't be called unless background style is \"erase\""
349 );
b18eb01c 350
98dd66cf
VZ
351 wxDC& dc = *event.GetDC();
352 dc.SetPen(*wxGREEN_PEN);
353
ca37cfde 354 PrepareDC( dc );
11fdee42 355
79c5fe4b 356 // clear any junk currently displayed
98dd66cf
VZ
357 dc.Clear();
358
79c5fe4b 359 const wxSize size = GetClientSize();
ca37cfde 360 for ( int x = 0; x < size.x; x += 15 )
98dd66cf
VZ
361 {
362 dc.DrawLine(x, 0, x, size.y);
363 }
364
ca37cfde 365 for ( int y = 0; y < size.y; y += 15 )
98dd66cf
VZ
366 {
367 dc.DrawLine(0, y, size.x, y);
368 }
369
370 dc.SetTextForeground(*wxRED);
9c61c5b0
VZ
371 dc.SetBackgroundMode(wxSOLID);
372 dc.DrawText("This text is drawn from OnEraseBackground", 60, 160);
33611ebb
RR
373}
374