]> git.saurik.com Git - wxWidgets.git/blame - samples/erase/erase.cpp
wxVariantData now inherits from wxObjectRefData
[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// ----------------------------------------------------------------------------
38// the application icon
f566b4fe 39#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
33611ebb
RR
40 #include "mondrian.xpm"
41#endif
42
43// ----------------------------------------------------------------------------
44// private classes
45// ----------------------------------------------------------------------------
46
47class MyApp : public wxApp
48{
49public:
50 virtual bool OnInit();
51};
52
53
79c5fe4b 54class MyCanvas : public wxScrolledWindow
33611ebb
RR
55{
56public:
9c61c5b0 57 MyCanvas(wxFrame *parent);
33611ebb 58
79c5fe4b 59 void UseBuffer(bool useBuffer) { m_useBuffer = useBuffer; Refresh(); }
9c61c5b0 60 bool UsesBuffer() const { return m_useBuffer; }
33611ebb
RR
61
62private:
33611ebb 63 void OnPaint( wxPaintEvent &event );
2b5f62a0 64 void OnChar( wxKeyEvent &event );
33611ebb 65 void OnEraseBackground( wxEraseEvent &event );
98dd66cf 66
79c5fe4b
VZ
67 void DoPaint(wxDC& dc);
68
69
b5a49d4c 70 wxBitmap m_bitmap;
2b5f62a0 71 wxString m_text;
33611ebb 72
79c5fe4b
VZ
73 // use wxMemoryDC in OnPaint()?
74 bool m_useBuffer;
75
b18eb01c 76
79c5fe4b
VZ
77 DECLARE_EVENT_TABLE()
78};
79
80class MyFrame : public wxFrame
81{
82public:
83 MyFrame();
84
9c61c5b0 85private:
79c5fe4b 86 void OnUseBuffer(wxCommandEvent& event);
9c61c5b0 87 void OnChangeBgStyle(wxCommandEvent& event);
79c5fe4b
VZ
88 void OnQuit(wxCommandEvent& event);
89 void OnAbout(wxCommandEvent& event);
90
9c61c5b0
VZ
91 // we can only use double-buffering with wxBG_STYLE_PAINT
92 void OnUpdateUIUseBuffer(wxUpdateUIEvent& event)
93 {
94 event.Enable( m_canvas->GetBackgroundStyle() == wxBG_STYLE_PAINT );
95 }
96
97 void OnUpdateUIChangeBgStyle(wxUpdateUIEvent& event)
98 {
99 event.Enable( !m_canvas->UsesBuffer() );
100 }
101
79c5fe4b
VZ
102 MyCanvas *m_canvas;
103
33611ebb
RR
104 DECLARE_EVENT_TABLE()
105};
106
79c5fe4b 107
33611ebb
RR
108// ----------------------------------------------------------------------------
109// constants
110// ----------------------------------------------------------------------------
111
112enum
113{
114 // menu items
79c5fe4b 115 Erase_Menu_UseBuffer = 100,
9c61c5b0
VZ
116 Erase_Menu_BgStyleErase,
117 Erase_Menu_BgStyleSystem,
118 Erase_Menu_BgStylePaint,
79c5fe4b
VZ
119 Erase_Menu_Exit = wxID_EXIT,
120 Erase_Menu_About = wxID_ABOUT
33611ebb
RR
121};
122
123
124// ----------------------------------------------------------------------------
125// the application class
126// ----------------------------------------------------------------------------
127
128IMPLEMENT_APP(MyApp)
129
130bool MyApp::OnInit()
131{
45e6e6f8
VZ
132 if ( !wxApp::OnInit() )
133 return false;
134
79c5fe4b 135 MyFrame *frame = new MyFrame;
33611ebb 136
07850a49 137 frame->Show(true);
98dd66cf 138
07850a49 139 return true;
33611ebb
RR
140}
141
142// ----------------------------------------------------------------------------
143// main frame
144// ----------------------------------------------------------------------------
145
146BEGIN_EVENT_TABLE(MyFrame, wxFrame)
79c5fe4b 147 EVT_MENU(Erase_Menu_UseBuffer, MyFrame::OnUseBuffer)
9c61c5b0
VZ
148 EVT_MENU_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint,
149 MyFrame::OnChangeBgStyle)
150
79c5fe4b
VZ
151 EVT_MENU(Erase_Menu_Exit, MyFrame::OnQuit)
152 EVT_MENU(Erase_Menu_About, MyFrame::OnAbout)
9c61c5b0
VZ
153
154 EVT_UPDATE_UI(Erase_Menu_UseBuffer, MyFrame::OnUpdateUIUseBuffer)
155 EVT_UPDATE_UI_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint,
156 MyFrame::OnUpdateUIChangeBgStyle)
33611ebb
RR
157END_EVENT_TABLE()
158
159// frame constructor
79c5fe4b 160MyFrame::MyFrame()
9c61c5b0 161 : wxFrame(NULL, wxID_ANY, "Erase sample",
79c5fe4b 162 wxPoint(50, 50), wxSize(450, 340))
33611ebb 163{
33611ebb
RR
164 SetIcon(wxICON(mondrian));
165
9c61c5b0
VZ
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");
79c5fe4b 175 menuFile->AppendSeparator();
9c61c5b0 176 menuFile->Append(Erase_Menu_Exit, "E&xit\tAlt-X", "Quit this program");
33611ebb 177
33611ebb 178
79c5fe4b 179 wxMenu *helpMenu = new wxMenu;
9c61c5b0 180 helpMenu->Append(Erase_Menu_About, "&About...\tCtrl-A", "Show about dialog");
33611ebb
RR
181
182 wxMenuBar *menuBar = new wxMenuBar();
9c61c5b0
VZ
183 menuBar->Append(menuFile, "&File");
184 menuBar->Append(helpMenu, "&Help");
33611ebb
RR
185
186 SetMenuBar(menuBar);
187
79c5fe4b 188 m_canvas = new MyCanvas( this );
33611ebb
RR
189}
190
191
79c5fe4b
VZ
192void MyFrame::OnUseBuffer(wxCommandEvent& event)
193{
194 m_canvas->UseBuffer(event.IsChecked());
195}
196
9c61c5b0 197void MyFrame::OnChangeBgStyle(wxCommandEvent& event)
b18eb01c 198{
9c61c5b0
VZ
199 int style = wxBG_STYLE_ERASE + event.GetId() - Erase_Menu_BgStyleErase;
200 m_canvas->SetBackgroundStyle(static_cast<wxBackgroundStyle>(style));
201
202 m_canvas->Refresh();
b18eb01c
VZ
203}
204
33611ebb
RR
205void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
206{
07850a49 207 Close(true);
33611ebb
RR
208}
209
210void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
211{
9c61c5b0
VZ
212 wxMessageBox
213 (
214 "This sample shows differences between different background styles "
215 "and how you may draw custom background.\n"
216 "\n"
217 "(c) 1998 Robert Roebling\n"
218 "(c) 2009 Vadim Zeitlin\n",
219 "About Erase Sample",
220 wxOK | wxICON_INFORMATION,
221 this
222 );
33611ebb
RR
223}
224
225
226BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
9c61c5b0
VZ
227 EVT_PAINT(MyCanvas::OnPaint)
228 EVT_CHAR(MyCanvas::OnChar)
229 EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground)
33611ebb
RR
230END_EVENT_TABLE()
231
9c61c5b0
VZ
232MyCanvas::MyCanvas(wxFrame *parent)
233 : wxScrolledWindow(parent, wxID_ANY)
33611ebb 234{
79c5fe4b
VZ
235 m_useBuffer = false;
236
33611ebb 237 SetScrollbars( 10, 10, 40, 100, 0, 0 );
98dd66cf
VZ
238
239 m_bitmap = wxBitmap( wxICON(mondrian) );
240
07850a49 241 new wxStaticBitmap( this, wxID_ANY, m_bitmap, wxPoint(80,20) );
11fdee42 242
ca37cfde 243 SetFocusIgnoringChildren();
9c61c5b0 244 SetBackgroundColour(*wxBLUE);
33611ebb
RR
245}
246
2b5f62a0
VZ
247void MyCanvas::OnChar( wxKeyEvent &event )
248{
249#if wxUSE_UNICODE
250 if (event.m_uniChar)
251 {
252 m_text += event.m_uniChar;
253 Refresh();
254 return;
255 }
256#endif
257
258 // some test cases
259 switch (event.m_keyCode)
260 {
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;
925e9792 266 default: m_text += (wxChar)event.m_keyCode; break;
2b5f62a0 267 }
2b5f62a0
VZ
268}
269
79c5fe4b 270void MyCanvas::DoPaint(wxDC& dc)
33611ebb 271{
b5a49d4c 272 dc.SetBrush( *wxBLACK_BRUSH );
9c61c5b0 273 dc.DrawRectangle( 10,10,60,50 );
98dd66cf 274
9c61c5b0 275 dc.DrawBitmap( m_bitmap, 20, 20, true );
98dd66cf 276
9c61c5b0
VZ
277 dc.SetTextForeground(*wxWHITE);
278 dc.DrawText("This text is drawn from OnPaint", 65, 65);
925e9792 279
2b5f62a0 280 wxString tmp;
9c61c5b0
VZ
281 tmp.Printf("Hit any key to display more text: %s", m_text);
282
2b5f62a0
VZ
283 int w,h;
284 dc.GetTextExtent( tmp, &w, &h );
2b5f62a0
VZ
285 dc.DrawRectangle( 65, 85, w, h );
286 dc.DrawText( tmp, 65, 85 );
33611ebb
RR
287}
288
79c5fe4b
VZ
289void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
290{
79c5fe4b
VZ
291 if ( m_useBuffer )
292 {
9c61c5b0
VZ
293 wxAutoBufferedPaintDC dc(this);
294 PrepareDC(dc);
79c5fe4b
VZ
295
296 DoPaint(dc);
79c5fe4b
VZ
297 }
298 else
299 {
9c61c5b0
VZ
300 wxPaintDC dc(this);
301 PrepareDC(dc);
302
303 DoPaint(dc);
79c5fe4b
VZ
304 }
305}
306
98dd66cf 307void MyCanvas::OnEraseBackground( wxEraseEvent& event )
33611ebb 308{
9c61c5b0
VZ
309 wxASSERT_MSG
310 (
311 GetBackgroundStyle() == wxBG_STYLE_ERASE,
312 "shouldn't be called unless background style is \"erase\""
313 );
b18eb01c 314
98dd66cf
VZ
315 wxDC& dc = *event.GetDC();
316 dc.SetPen(*wxGREEN_PEN);
317
ca37cfde 318 PrepareDC( dc );
11fdee42 319
79c5fe4b 320 // clear any junk currently displayed
98dd66cf
VZ
321 dc.Clear();
322
79c5fe4b 323 const wxSize size = GetClientSize();
ca37cfde 324 for ( int x = 0; x < size.x; x += 15 )
98dd66cf
VZ
325 {
326 dc.DrawLine(x, 0, x, size.y);
327 }
328
ca37cfde 329 for ( int y = 0; y < size.y; y += 15 )
98dd66cf
VZ
330 {
331 dc.DrawLine(0, y, size.x, y);
332 }
333
334 dc.SetTextForeground(*wxRED);
9c61c5b0
VZ
335 dc.SetBackgroundMode(wxSOLID);
336 dc.DrawText("This text is drawn from OnEraseBackground", 60, 160);
33611ebb
RR
337}
338