]> git.saurik.com Git - wxWidgets.git/blob - samples/erase/erase.cpp
use wx.ART_NEW
[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
22 #pragma interface
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 void EraseBg(bool eraseBg) { m_eraseBg = eraseBg; 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 // paint custom background in OnEraseBackground()?
80 bool m_eraseBg;
81
82
83 DECLARE_EVENT_TABLE()
84 };
85
86 class MyFrame : public wxFrame
87 {
88 public:
89 MyFrame();
90
91 void OnUseBuffer(wxCommandEvent& event);
92 void OnEraseBg(wxCommandEvent& event);
93 void OnQuit(wxCommandEvent& event);
94 void OnAbout(wxCommandEvent& event);
95
96 private:
97 MyCanvas *m_canvas;
98
99 DECLARE_EVENT_TABLE()
100 };
101
102
103 // ----------------------------------------------------------------------------
104 // constants
105 // ----------------------------------------------------------------------------
106
107 enum
108 {
109 // menu items
110 Erase_Menu_UseBuffer = 100,
111 Erase_Menu_EraseBg,
112 Erase_Menu_Exit = wxID_EXIT,
113 Erase_Menu_About = wxID_ABOUT
114 };
115
116
117 // ----------------------------------------------------------------------------
118 // the application class
119 // ----------------------------------------------------------------------------
120
121 IMPLEMENT_APP(MyApp)
122
123 bool MyApp::OnInit()
124 {
125 MyFrame *frame = new MyFrame;
126
127 frame->Show(true);
128
129 return true;
130 }
131
132 // ----------------------------------------------------------------------------
133 // main frame
134 // ----------------------------------------------------------------------------
135
136 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
137 EVT_MENU(Erase_Menu_UseBuffer, MyFrame::OnUseBuffer)
138 EVT_MENU(Erase_Menu_EraseBg, MyFrame::OnEraseBg)
139 EVT_MENU(Erase_Menu_Exit, MyFrame::OnQuit)
140 EVT_MENU(Erase_Menu_About, MyFrame::OnAbout)
141 END_EVENT_TABLE()
142
143 // frame constructor
144 MyFrame::MyFrame()
145 : wxFrame(NULL, wxID_ANY, _T("Erase sample"),
146 wxPoint(50, 50), wxSize(450, 340))
147 {
148 SetIcon(wxICON(mondrian));
149
150 wxMenu *menuFile = new wxMenu(_T(""), wxMENU_TEAROFF);
151 menuFile->AppendCheckItem(Erase_Menu_UseBuffer, _T("&Use memory DC\tCtrl-M"));
152 menuFile->AppendCheckItem(Erase_Menu_EraseBg, _T("Custom &background\tCtrl-B"));
153 menuFile->AppendSeparator();
154 menuFile->Append(Erase_Menu_Exit, _T("E&xit\tAlt-X"), _T("Quit this program"));
155
156
157 wxMenu *helpMenu = new wxMenu;
158 helpMenu->Append(Erase_Menu_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
159
160 wxMenuBar *menuBar = new wxMenuBar();
161 menuBar->Append(menuFile, _T("&File"));
162 menuBar->Append(helpMenu, _T("&Help"));
163
164 SetMenuBar(menuBar);
165
166 #if wxUSE_STATUSBAR
167 // create a status bar just for fun (by default with 1 pane only)
168 CreateStatusBar(2);
169 SetStatusText(_T("Welcome to wxWidgets erase sample!"));
170 #endif // wxUSE_STATUSBAR
171
172 m_canvas = new MyCanvas( this );
173 }
174
175
176 void MyFrame::OnUseBuffer(wxCommandEvent& event)
177 {
178 m_canvas->UseBuffer(event.IsChecked());
179 }
180
181 void MyFrame::OnEraseBg(wxCommandEvent& event)
182 {
183 m_canvas->EraseBg(event.IsChecked());
184 }
185
186 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
187 {
188 Close(true);
189 }
190
191 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
192 {
193 wxMessageBox(_T("This sample shows how you can draw custom background."),
194 _T("About Erase Sample"), wxOK | wxICON_INFORMATION, this);
195 }
196
197
198 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
199 EVT_PAINT( MyCanvas::OnPaint)
200 EVT_CHAR( MyCanvas::OnChar)
201 EVT_ERASE_BACKGROUND( MyCanvas::OnEraseBackground)
202 END_EVENT_TABLE()
203
204 MyCanvas::MyCanvas( wxFrame *parent )
205 : wxScrolledWindow( parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
206 wxScrolledWindowStyle | wxSUNKEN_BORDER )
207 {
208 m_eraseBg =
209 m_useBuffer = false;
210
211 SetScrollbars( 10, 10, 40, 100, 0, 0 );
212
213 m_bitmap = wxBitmap( wxICON(mondrian) );
214
215 new wxStaticBitmap( this, wxID_ANY, m_bitmap, wxPoint(80,20) );
216
217 SetFocusIgnoringChildren();
218 }
219
220 void MyCanvas::OnChar( wxKeyEvent &event )
221 {
222 #if wxUSE_UNICODE
223 if (event.m_uniChar)
224 {
225 m_text += event.m_uniChar;
226 Refresh();
227 return;
228 }
229 #endif
230
231 // some test cases
232 switch (event.m_keyCode)
233 {
234 case WXK_UP: m_text += wxT( "<UP>" ); break;
235 case WXK_LEFT: m_text += wxT( "<LEFT>" ); break;
236 case WXK_RIGHT: m_text += wxT( "<RIGHT>" ); break;
237 case WXK_DOWN: m_text += wxT( "<DOWN>" ); break;
238 case WXK_RETURN: m_text += wxT( "<ENTER>" ); break;
239 default: m_text += (wxChar)event.m_keyCode; break;
240 }
241 }
242
243 void MyCanvas::DoPaint(wxDC& dc)
244 {
245 dc.SetBrush( *wxBLACK_BRUSH );
246 dc.DrawRectangle( 10,10,200,50 );
247
248 dc.DrawBitmap( m_bitmap, 10, 20, true );
249
250 dc.SetTextForeground(*wxBLUE);
251 dc.DrawText(_T("This text is drawn from OnPaint"), 65, 65);
252
253 wxString tmp;
254 tmp.Printf( _T("Hit any key to display more text: %s"), m_text.c_str() );
255 int w,h;
256 dc.GetTextExtent( tmp, &w, &h );
257 dc.SetBrush( *wxWHITE_BRUSH );
258 dc.DrawRectangle( 65, 85, w, h );
259 dc.DrawText( tmp, 65, 85 );
260
261 #if 0
262 wxRegionIterator upd( GetUpdateRegion() );
263 while (upd)
264 {
265 wxLogDebug( _T("Paint: %d %d %d %d"), upd.GetX(), upd.GetY(), upd.GetWidth(), upd.GetHeight() );
266 upd ++;
267 }
268 #endif
269
270 #if 0
271 wxSize size = GetSize();
272 wxSize client_size = GetClientSize();
273 wxLogDebug( _T("size %d %d client_size %d %d"), size.x, size.y, client_size.x, client_size.y );
274 #endif
275
276 #if 0
277 int i;
278 dc.SetPen( *wxWHITE_PEN );
279 for (i = 0; i < 20; i += 2)
280 dc.DrawLine( i,i, i+100,i );
281
282 dc.SetPen( *wxWHITE_PEN );
283 for (i = 200; i < 220; i += 2)
284 dc.DrawLine( i-200,i, i-100,i );
285
286 wxRegion region( 110, 110, 80, 80 );
287 wxRegion hole( 130, 130, 40, 1 );
288 region.Intersect( hole );
289 dc.SetClippingRegion( region );
290
291 dc.SetBrush( *wxRED_BRUSH );
292 dc.DrawRectangle( 100, 100, 200, 200 );
293
294 dc.DestroyClippingRegion();
295
296 dc.SetPen( *wxTRANSPARENT_PEN );
297
298 wxRegion strip( 110, 200, 30, 1 );
299 wxRegionIterator it( strip );
300 while (it)
301 {
302 dc.DrawRectangle( it.GetX(), it.GetY(), it.GetWidth(), it.GetHeight() );
303 it ++;
304 }
305 #endif // 0
306 }
307
308 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
309 {
310 wxPaintDC dcWin(this);
311 PrepareDC( dcWin );
312
313 if ( m_useBuffer )
314 {
315 const wxSize size = GetClientSize();
316 wxMemoryDC dc;
317 wxBitmap bmp(size.x, size.y);
318 dc.SelectObject(bmp);
319 dc.Blit(0, 0, size.x, size.y, &dcWin, 0, 0);
320 dc.DrawText(_T("(copy of background)"), 5, 120 );
321
322 DoPaint(dc);
323
324 dcWin.Blit(0, 0, size.x, size.y, &dc, 0, 0);
325 }
326 else
327 {
328 DoPaint(dcWin);
329 }
330 }
331
332 void MyCanvas::OnEraseBackground( wxEraseEvent& event )
333 {
334 if ( !m_eraseBg )
335 {
336 event.Skip();
337 return;
338 }
339
340 wxDC& dc = *event.GetDC();
341 dc.SetPen(*wxGREEN_PEN);
342
343 PrepareDC( dc );
344
345 // clear any junk currently displayed
346 dc.Clear();
347
348 const wxSize size = GetClientSize();
349 for ( int x = 0; x < size.x; x += 15 )
350 {
351 dc.DrawLine(x, 0, x, size.y);
352 }
353
354 for ( int y = 0; y < size.y; y += 15 )
355 {
356 dc.DrawLine(0, y, size.x, y);
357 }
358
359 dc.SetTextForeground(*wxRED);
360 dc.DrawText(_T("This text is drawn from OnEraseBackground"), 60, 160);
361 }
362