Small correction to erase sample, added PrepareDC()
[wxWidgets.git] / samples / erase / erase.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: erase.cpp
3 // Purpose: Erase wxWidgets sample
4 // Author: Robert Roebling
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(__APPLE__)
21 #pragma implementation "erase.cpp"
22 #pragma interface "erase.cpp"
23 #endif
24
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 // for all others, include the necessary headers (this file is usually all you
33 // need because it includes almost all "standard" wxWidgets headers)
34 #ifndef WX_PRECOMP
35 #include "wx/wx.h"
36 #endif
37
38 // ----------------------------------------------------------------------------
39 // resources
40 // ----------------------------------------------------------------------------
41 // the application icon
42 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
43 #include "mondrian.xpm"
44 #endif
45
46 // ----------------------------------------------------------------------------
47 // private classes
48 // ----------------------------------------------------------------------------
49
50 class MyApp : public wxApp
51 {
52 public:
53 virtual bool OnInit();
54 };
55
56
57 class MyCanvas : public wxScrolledWindow
58 {
59 public:
60 MyCanvas( wxFrame *parent );
61
62 void UseBuffer(bool useBuffer) { m_useBuffer = useBuffer; 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 DECLARE_EVENT_TABLE()
79 };
80
81 class MyFrame : public wxFrame
82 {
83 public:
84 MyFrame();
85
86 void OnUseBuffer(wxCommandEvent& event);
87 void OnQuit(wxCommandEvent& event);
88 void OnAbout(wxCommandEvent& event);
89
90 private:
91 MyCanvas *m_canvas;
92
93 DECLARE_EVENT_TABLE()
94 };
95
96
97 // ----------------------------------------------------------------------------
98 // constants
99 // ----------------------------------------------------------------------------
100
101 enum
102 {
103 // menu items
104 Erase_Menu_UseBuffer = 100,
105 Erase_Menu_Exit = wxID_EXIT,
106 Erase_Menu_About = wxID_ABOUT
107 };
108
109
110 // ----------------------------------------------------------------------------
111 // the application class
112 // ----------------------------------------------------------------------------
113
114 IMPLEMENT_APP(MyApp)
115
116 bool MyApp::OnInit()
117 {
118 MyFrame *frame = new MyFrame;
119
120 frame->Show(true);
121
122 return true;
123 }
124
125 // ----------------------------------------------------------------------------
126 // main frame
127 // ----------------------------------------------------------------------------
128
129 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
130 EVT_MENU(Erase_Menu_UseBuffer, MyFrame::OnUseBuffer)
131 EVT_MENU(Erase_Menu_Exit, MyFrame::OnQuit)
132 EVT_MENU(Erase_Menu_About, MyFrame::OnAbout)
133 END_EVENT_TABLE()
134
135 // frame constructor
136 MyFrame::MyFrame()
137 : wxFrame(NULL, wxID_ANY, _T("Erase sample"),
138 wxPoint(50, 50), wxSize(450, 340))
139 {
140 SetIcon(wxICON(mondrian));
141
142 wxMenu *menuFile = new wxMenu(_T(""), wxMENU_TEAROFF);
143 menuFile->AppendCheckItem(Erase_Menu_UseBuffer, _T("&Use memory DC\tCtrl-M"));
144 menuFile->AppendSeparator();
145 menuFile->Append(Erase_Menu_Exit, _T("E&xit\tAlt-X"), _T("Quit this program"));
146
147
148 wxMenu *helpMenu = new wxMenu;
149 helpMenu->Append(Erase_Menu_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
150
151 wxMenuBar *menuBar = new wxMenuBar();
152 menuBar->Append(menuFile, _T("&File"));
153 menuBar->Append(helpMenu, _T("&Help"));
154
155 SetMenuBar(menuBar);
156
157 #if wxUSE_STATUSBAR
158 // create a status bar just for fun (by default with 1 pane only)
159 CreateStatusBar(2);
160 SetStatusText(_T("Welcome to wxWidgets erase sample!"));
161 #endif // wxUSE_STATUSBAR
162
163 m_canvas = new MyCanvas( this );
164 }
165
166
167 void MyFrame::OnUseBuffer(wxCommandEvent& event)
168 {
169 m_canvas->UseBuffer(event.IsChecked());
170 }
171
172 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
173 {
174 Close(true);
175 }
176
177 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
178 {
179 wxMessageBox(_T("This sample shows how you can draw custom background."),
180 _T("About Erase Sample"), wxOK | wxICON_INFORMATION, this);
181 }
182
183
184 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
185 EVT_PAINT( MyCanvas::OnPaint)
186 EVT_CHAR( MyCanvas::OnChar)
187 EVT_ERASE_BACKGROUND( MyCanvas::OnEraseBackground)
188 END_EVENT_TABLE()
189
190 MyCanvas::MyCanvas( wxFrame *parent )
191 : wxScrolledWindow( parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
192 wxScrolledWindowStyle | wxSUNKEN_BORDER )
193 {
194 m_useBuffer = false;
195
196 SetScrollbars( 10, 10, 40, 100, 0, 0 );
197
198 m_bitmap = wxBitmap( wxICON(mondrian) );
199
200 new wxStaticBitmap( this, wxID_ANY, m_bitmap, wxPoint(80,20) );
201
202 SetFocusIgnoringChildren();
203 }
204
205 void MyCanvas::OnChar( wxKeyEvent &event )
206 {
207 #if wxUSE_UNICODE
208 if (event.m_uniChar)
209 {
210 m_text += event.m_uniChar;
211 Refresh();
212 return;
213 }
214 #endif
215
216 // some test cases
217 switch (event.m_keyCode)
218 {
219 case WXK_UP: m_text += wxT( "<UP>" ); break;
220 case WXK_LEFT: m_text += wxT( "<LEFT>" ); break;
221 case WXK_RIGHT: m_text += wxT( "<RIGHT>" ); break;
222 case WXK_DOWN: m_text += wxT( "<DOWN>" ); break;
223 case WXK_RETURN: m_text += wxT( "<ENTER>" ); break;
224 default: m_text += (wxChar)event.m_keyCode; break;
225 }
226 }
227
228 void MyCanvas::DoPaint(wxDC& dc)
229 {
230 dc.SetBrush( *wxBLACK_BRUSH );
231 dc.DrawRectangle( 10,10,200,50 );
232
233 dc.DrawBitmap( m_bitmap, 10, 20, true );
234
235 dc.SetTextForeground(*wxBLUE);
236 dc.DrawText(_T("This text is drawn from OnPaint"), 65, 65);
237
238 wxString tmp;
239 tmp.Printf( _T("Hit any key to display more text: %s"), m_text.c_str() );
240 int w,h;
241 dc.GetTextExtent( tmp, &w, &h );
242 dc.SetBrush( *wxWHITE_BRUSH );
243 dc.DrawRectangle( 65, 85, w, h );
244 dc.DrawText( tmp, 65, 85 );
245
246 #if 0
247 wxRegionIterator upd( GetUpdateRegion() );
248 while (upd)
249 {
250 wxLogDebug( _T("Paint: %d %d %d %d"), upd.GetX(), upd.GetY(), upd.GetWidth(), upd.GetHeight() );
251 upd ++;
252 }
253 #endif
254
255 #if 0
256 wxSize size = GetSize();
257 wxSize client_size = GetClientSize();
258 wxLogDebug( _T("size %d %d client_size %d %d"), size.x, size.y, client_size.x, client_size.y );
259 #endif
260
261 #if 0
262 int i;
263 dc.SetPen( *wxWHITE_PEN );
264 for (i = 0; i < 20; i += 2)
265 dc.DrawLine( i,i, i+100,i );
266
267 dc.SetPen( *wxWHITE_PEN );
268 for (i = 200; i < 220; i += 2)
269 dc.DrawLine( i-200,i, i-100,i );
270
271 wxRegion region( 110, 110, 80, 80 );
272 wxRegion hole( 130, 130, 40, 1 );
273 region.Intersect( hole );
274 dc.SetClippingRegion( region );
275
276 dc.SetBrush( *wxRED_BRUSH );
277 dc.DrawRectangle( 100, 100, 200, 200 );
278
279 dc.DestroyClippingRegion();
280
281 dc.SetPen( *wxTRANSPARENT_PEN );
282
283 wxRegion strip( 110, 200, 30, 1 );
284 wxRegionIterator it( strip );
285 while (it)
286 {
287 dc.DrawRectangle( it.GetX(), it.GetY(), it.GetWidth(), it.GetHeight() );
288 it ++;
289 }
290 #endif // 0
291 }
292
293 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
294 {
295 wxPaintDC dcWin(this);
296 PrepareDC( dcWin );
297
298 if ( m_useBuffer )
299 {
300 const wxSize size = GetClientSize();
301 wxMemoryDC dc;
302 wxBitmap bmp(size.x, size.y);
303 dc.SelectObject(bmp);
304 dc.Blit(0, 0, size.x, size.y, &dcWin, 0, 0);
305 dc.DrawText(_T("(copy of background)"), 5, 120 );
306
307 DoPaint(dc);
308
309 dcWin.Blit(0, 0, size.x, size.y, &dc, 0, 0);
310 }
311 else
312 {
313 DoPaint(dcWin);
314 }
315 }
316
317 void MyCanvas::OnEraseBackground( wxEraseEvent& event )
318 {
319 wxDC& dc = *event.GetDC();
320 dc.SetPen(*wxGREEN_PEN);
321
322 PrepareDC( dc );
323
324 // clear any junk currently displayed
325 dc.Clear();
326
327 const wxSize size = GetClientSize();
328 for ( int x = 0; x < size.x; x += 15 )
329 {
330 dc.DrawLine(x, 0, x, size.y);
331 }
332
333 for ( int y = 0; y < size.y; y += 15 )
334 {
335 dc.DrawLine(0, y, size.x, y);
336 }
337
338 dc.SetTextForeground(*wxRED);
339 dc.DrawText(_T("This text is drawn from OnEraseBackground"), 60, 160);
340 }
341