]> git.saurik.com Git - wxWidgets.git/blob - samples/erase/erase.cpp
added test for Blit()ting OnEraseBackground() results in OnPaint()
[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
203 void MyCanvas::OnChar( wxKeyEvent &event )
204 {
205 #if wxUSE_UNICODE
206 if (event.m_uniChar)
207 {
208 m_text += event.m_uniChar;
209 Refresh();
210 return;
211 }
212 #endif
213
214 // some test cases
215 switch (event.m_keyCode)
216 {
217 case WXK_UP: m_text += wxT( "<UP>" ); break;
218 case WXK_LEFT: m_text += wxT( "<LEFT>" ); break;
219 case WXK_RIGHT: m_text += wxT( "<RIGHT>" ); break;
220 case WXK_DOWN: m_text += wxT( "<DOWN>" ); break;
221 case WXK_RETURN: m_text += wxT( "<ENTER>" ); break;
222 default: m_text += (wxChar)event.m_keyCode; break;
223 }
224 }
225
226 void MyCanvas::DoPaint(wxDC& dc)
227 {
228 dc.SetBrush( *wxBLACK_BRUSH );
229 dc.DrawRectangle( 0,0,200,50 );
230
231 dc.DrawBitmap( m_bitmap, 10, 20, true );
232
233 dc.SetTextForeground(*wxBLUE);
234 dc.DrawText(_T("This text is drawn from OnPaint"), 65, 65);
235
236 wxString tmp;
237 tmp.Printf( _T("Hit any key to display more text: %s"), m_text.c_str() );
238 int w,h;
239 dc.GetTextExtent( tmp, &w, &h );
240 dc.SetBrush( *wxWHITE_BRUSH );
241 dc.DrawRectangle( 65, 85, w, h );
242 dc.DrawText( tmp, 65, 85 );
243
244 #if 0
245 wxRegionIterator upd( GetUpdateRegion() );
246 while (upd)
247 {
248 wxLogDebug( _T("Paint: %d %d %d %d"), upd.GetX(), upd.GetY(), upd.GetWidth(), upd.GetHeight() );
249 upd ++;
250 }
251 #endif
252
253 #if 0
254 wxSize size = GetSize();
255 wxSize client_size = GetClientSize();
256 wxLogDebug( _T("size %d %d client_size %d %d"), size.x, size.y, client_size.x, client_size.y );
257 #endif
258
259 #if 0
260 int i;
261 dc.SetPen( *wxWHITE_PEN );
262 for (i = 0; i < 20; i += 2)
263 dc.DrawLine( i,i, i+100,i );
264
265 dc.SetPen( *wxWHITE_PEN );
266 for (i = 200; i < 220; i += 2)
267 dc.DrawLine( i-200,i, i-100,i );
268
269 wxRegion region( 110, 110, 80, 80 );
270 wxRegion hole( 130, 130, 40, 1 );
271 region.Intersect( hole );
272 dc.SetClippingRegion( region );
273
274 dc.SetBrush( *wxRED_BRUSH );
275 dc.DrawRectangle( 100, 100, 200, 200 );
276
277 dc.DestroyClippingRegion();
278
279 dc.SetPen( *wxTRANSPARENT_PEN );
280
281 wxRegion strip( 110, 200, 30, 1 );
282 wxRegionIterator it( strip );
283 while (it)
284 {
285 dc.DrawRectangle( it.GetX(), it.GetY(), it.GetWidth(), it.GetHeight() );
286 it ++;
287 }
288 #endif // 0
289 }
290
291 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
292 {
293 wxPaintDC dcWin(this);
294 PrepareDC( dcWin );
295
296 if ( m_useBuffer )
297 {
298 const wxSize size = GetClientSize();
299 wxMemoryDC dc;
300 wxBitmap bmp(size.x, size.y);
301 dc.SelectObject(bmp);
302 dc.Blit(0, 0, size.x, size.y, &dcWin, 0, 0);
303 dc.DrawText(_T("(copy of background)"), 5, 120 );
304
305 DoPaint(dc);
306
307 dcWin.Blit(0, 0, size.x, size.y, &dc, 0, 0);
308 }
309 else
310 {
311 DoPaint(dcWin);
312 }
313 }
314
315 void MyCanvas::OnEraseBackground( wxEraseEvent& event )
316 {
317 wxDC& dc = *event.GetDC();
318 dc.SetPen(*wxGREEN_PEN);
319
320 // clear any junk currently displayed
321 dc.Clear();
322
323 const wxSize size = GetClientSize();
324 for ( int x = 0; x < size.x; x += 10 )
325 {
326 dc.DrawLine(x, 0, x, size.y);
327 }
328
329 for ( int y = 0; y < size.y; y += 10 )
330 {
331 dc.DrawLine(0, y, size.x, y);
332 }
333
334 dc.SetTextForeground(*wxRED);
335 dc.DrawText(_T("This text is drawn from OnEraseBackground"), 60, 60);
336 }
337