]> git.saurik.com Git - wxWidgets.git/blob - samples/erase/erase.cpp
Add a wxStaticText to the erase simple.
[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
39 // the application icon
40 #if !defined(__WXMSW__) && !defined(__WXPM__)
41 #include "../sample.xpm"
42 #endif
43
44 // ----------------------------------------------------------------------------
45 // private classes
46 // ----------------------------------------------------------------------------
47
48 class MyApp : public wxApp
49 {
50 public:
51 virtual bool OnInit();
52 };
53
54
55 class MyCanvas : public wxScrolledWindow
56 {
57 public:
58 MyCanvas(wxFrame *parent);
59
60 void UseBuffer(bool useBuffer) { m_useBuffer = useBuffer; Refresh(); }
61 bool UsesBuffer() const { return m_useBuffer; }
62
63 void EraseBgInPaint(bool erase) { m_eraseBgInPaint = erase; Refresh(); }
64
65 private:
66 void OnPaint( wxPaintEvent &event );
67 void OnChar( wxKeyEvent &event );
68 void OnEraseBackground( wxEraseEvent &event );
69
70 void DoPaint(wxDC& dc);
71
72
73 wxBitmap m_bitmap;
74 wxString m_text;
75
76 // use wxMemoryDC in OnPaint()?
77 bool m_useBuffer;
78
79 // erase background in OnPaint()?
80 bool m_eraseBgInPaint;
81
82
83 DECLARE_EVENT_TABLE()
84 };
85
86 class MyFrame : public wxFrame
87 {
88 public:
89 MyFrame();
90
91 private:
92 void OnUseBuffer(wxCommandEvent& event);
93 void OnEraseBgInPaint(wxCommandEvent& event);
94 void OnChangeBgStyle(wxCommandEvent& event);
95 void OnQuit(wxCommandEvent& event);
96 void OnAbout(wxCommandEvent& event);
97
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
109 MyCanvas *m_canvas;
110
111 DECLARE_EVENT_TABLE()
112 };
113
114
115 // ----------------------------------------------------------------------------
116 // constants
117 // ----------------------------------------------------------------------------
118
119 enum
120 {
121 // menu items
122 Erase_Menu_UseBuffer = 100,
123 Erase_Menu_EraseBgInPaint,
124 Erase_Menu_BgStyleErase,
125 Erase_Menu_BgStyleSystem,
126 Erase_Menu_BgStylePaint,
127 Erase_Menu_Exit = wxID_EXIT,
128 Erase_Menu_About = wxID_ABOUT
129 };
130
131
132 // ----------------------------------------------------------------------------
133 // the application class
134 // ----------------------------------------------------------------------------
135
136 IMPLEMENT_APP(MyApp)
137
138 bool MyApp::OnInit()
139 {
140 if ( !wxApp::OnInit() )
141 return false;
142
143 MyFrame *frame = new MyFrame;
144
145 frame->Show(true);
146
147 return true;
148 }
149
150 // ----------------------------------------------------------------------------
151 // main frame
152 // ----------------------------------------------------------------------------
153
154 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
155 EVT_MENU(Erase_Menu_UseBuffer, MyFrame::OnUseBuffer)
156 EVT_MENU(Erase_Menu_EraseBgInPaint, MyFrame::OnEraseBgInPaint)
157 EVT_MENU_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint,
158 MyFrame::OnChangeBgStyle)
159
160 EVT_MENU(Erase_Menu_Exit, MyFrame::OnQuit)
161 EVT_MENU(Erase_Menu_About, MyFrame::OnAbout)
162
163 EVT_UPDATE_UI(Erase_Menu_UseBuffer, MyFrame::OnUpdateUIUseBuffer)
164 EVT_UPDATE_UI_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint,
165 MyFrame::OnUpdateUIChangeBgStyle)
166 END_EVENT_TABLE()
167
168 // frame constructor
169 MyFrame::MyFrame()
170 : wxFrame(NULL, wxID_ANY, "Erase sample",
171 wxPoint(50, 50), wxSize(450, 340))
172 {
173 SetIcon(wxICON(sample));
174
175 wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
176 menuFile->AppendCheckItem(Erase_Menu_UseBuffer, "&Use memory DC\tCtrl-M");
177 menuFile->AppendCheckItem(Erase_Menu_EraseBgInPaint,
178 "&Erase background in EVT_PAINT\tCtrl-R");
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");
186 menuFile->AppendSeparator();
187 menuFile->Append(Erase_Menu_Exit, "E&xit\tAlt-X", "Quit this program");
188
189
190 wxMenu *helpMenu = new wxMenu;
191 helpMenu->Append(Erase_Menu_About, "&About...\tCtrl-A", "Show about dialog");
192
193 wxMenuBar *menuBar = new wxMenuBar();
194 menuBar->Append(menuFile, "&File");
195 menuBar->Append(helpMenu, "&Help");
196
197 SetMenuBar(menuBar);
198
199 m_canvas = new MyCanvas( this );
200 }
201
202
203 void MyFrame::OnUseBuffer(wxCommandEvent& event)
204 {
205 m_canvas->UseBuffer(event.IsChecked());
206 }
207
208 void MyFrame::OnEraseBgInPaint(wxCommandEvent& event)
209 {
210 m_canvas->EraseBgInPaint(event.IsChecked());
211 }
212
213 void MyFrame::OnChangeBgStyle(wxCommandEvent& event)
214 {
215 int style = wxBG_STYLE_ERASE + event.GetId() - Erase_Menu_BgStyleErase;
216 m_canvas->SetBackgroundStyle(static_cast<wxBackgroundStyle>(style));
217
218 m_canvas->Refresh();
219 }
220
221 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
222 {
223 Close(true);
224 }
225
226 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
227 {
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 );
239 }
240
241
242 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
243 EVT_PAINT(MyCanvas::OnPaint)
244 EVT_CHAR(MyCanvas::OnChar)
245 EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground)
246 END_EVENT_TABLE()
247
248 MyCanvas::MyCanvas(wxFrame *parent)
249 : wxScrolledWindow(parent, wxID_ANY)
250 {
251 m_useBuffer = false;
252 m_eraseBgInPaint = false;
253
254 SetScrollbars( 10, 10, 40, 100, 0, 0 );
255
256 m_bitmap = wxBitmap( wxICON(sample) );
257
258 new wxStaticBitmap( this, wxID_ANY, m_bitmap, wxPoint(80,20) );
259
260 new wxStaticText(this, wxID_ANY,
261 "Left bitmap is a wxStaticBitmap,\n"
262 "right one drawn directly",
263 wxPoint(150, 20));
264
265 SetFocusIgnoringChildren();
266 SetBackgroundColour(*wxCYAN);
267 }
268
269 void 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;
288 default: m_text += (wxChar)event.m_keyCode; break;
289 }
290 }
291
292 void MyCanvas::DoPaint(wxDC& dc)
293 {
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
308 dc.SetBrush( *wxBLACK_BRUSH );
309 dc.DrawRectangle( 10,10,60,50 );
310
311 dc.DrawBitmap( m_bitmap, 20, 20, true );
312
313 dc.SetTextForeground(*wxWHITE);
314 dc.DrawText("This text is drawn from OnPaint", 65, 65);
315
316 wxString tmp;
317 tmp.Printf("Hit any key to display more text: %s", m_text);
318
319 int w,h;
320 dc.GetTextExtent( tmp, &w, &h );
321 dc.DrawRectangle( 65, 85, w, h );
322 dc.DrawText( tmp, 65, 85 );
323 }
324
325 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
326 {
327 if ( m_useBuffer )
328 {
329 wxAutoBufferedPaintDC dc(this);
330 PrepareDC(dc);
331
332 DoPaint(dc);
333 }
334 else
335 {
336 wxPaintDC dc(this);
337 PrepareDC(dc);
338
339 DoPaint(dc);
340 }
341 }
342
343 void MyCanvas::OnEraseBackground( wxEraseEvent& event )
344 {
345 wxASSERT_MSG
346 (
347 GetBackgroundStyle() == wxBG_STYLE_ERASE,
348 "shouldn't be called unless background style is \"erase\""
349 );
350
351 wxDC& dc = *event.GetDC();
352 dc.SetPen(*wxGREEN_PEN);
353
354 PrepareDC( dc );
355
356 // clear any junk currently displayed
357 dc.Clear();
358
359 const wxSize size = GetClientSize();
360 for ( int x = 0; x < size.x; x += 15 )
361 {
362 dc.DrawLine(x, 0, x, size.y);
363 }
364
365 for ( int y = 0; y < size.y; y += 15 )
366 {
367 dc.DrawLine(0, y, size.x, y);
368 }
369
370 dc.SetTextForeground(*wxRED);
371 dc.SetBackgroundMode(wxSOLID);
372 dc.DrawText("This text is drawn from OnEraseBackground", 60, 160);
373 }
374