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