]>
Commit | Line | Data |
---|---|---|
33611ebb RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: erase.cpp | |
be5a51fb | 3 | // Purpose: Erase wxWidgets sample |
33611ebb RR |
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 | ||
788233da | 20 | #if defined(__GNUG__) && !defined(__APPLE__) |
4d2c67a9 JS |
21 | #pragma implementation "erase.cpp" |
22 | #pragma interface "erase.cpp" | |
33611ebb RR |
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 | |
be5a51fb | 33 | // need because it includes almost all "standard" wxWidgets headers) |
33611ebb RR |
34 | #ifndef WX_PRECOMP |
35 | #include "wx/wx.h" | |
36 | #endif | |
37 | ||
38 | // ---------------------------------------------------------------------------- | |
39 | // resources | |
40 | // ---------------------------------------------------------------------------- | |
41 | // the application icon | |
f566b4fe | 42 | #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__) |
33611ebb RR |
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 | ||
79c5fe4b | 57 | class MyCanvas : public wxScrolledWindow |
33611ebb RR |
58 | { |
59 | public: | |
79c5fe4b | 60 | MyCanvas( wxFrame *parent ); |
33611ebb | 61 | |
79c5fe4b | 62 | void UseBuffer(bool useBuffer) { m_useBuffer = useBuffer; Refresh(); } |
b18eb01c | 63 | void EraseBg(bool eraseBg) { m_eraseBg = eraseBg; Refresh(); } |
33611ebb RR |
64 | |
65 | private: | |
33611ebb | 66 | void OnPaint( wxPaintEvent &event ); |
2b5f62a0 | 67 | void OnChar( wxKeyEvent &event ); |
33611ebb | 68 | void OnEraseBackground( wxEraseEvent &event ); |
98dd66cf | 69 | |
79c5fe4b VZ |
70 | void DoPaint(wxDC& dc); |
71 | ||
72 | ||
b5a49d4c | 73 | wxBitmap m_bitmap; |
2b5f62a0 | 74 | wxString m_text; |
33611ebb | 75 | |
79c5fe4b VZ |
76 | // use wxMemoryDC in OnPaint()? |
77 | bool m_useBuffer; | |
78 | ||
b18eb01c VZ |
79 | // paint custom background in OnEraseBackground()? |
80 | bool m_eraseBg; | |
81 | ||
82 | ||
79c5fe4b VZ |
83 | DECLARE_EVENT_TABLE() |
84 | }; | |
85 | ||
86 | class MyFrame : public wxFrame | |
87 | { | |
88 | public: | |
89 | MyFrame(); | |
90 | ||
91 | void OnUseBuffer(wxCommandEvent& event); | |
b18eb01c | 92 | void OnEraseBg(wxCommandEvent& event); |
79c5fe4b VZ |
93 | void OnQuit(wxCommandEvent& event); |
94 | void OnAbout(wxCommandEvent& event); | |
95 | ||
33611ebb | 96 | private: |
79c5fe4b VZ |
97 | MyCanvas *m_canvas; |
98 | ||
33611ebb RR |
99 | DECLARE_EVENT_TABLE() |
100 | }; | |
101 | ||
79c5fe4b | 102 | |
33611ebb RR |
103 | // ---------------------------------------------------------------------------- |
104 | // constants | |
105 | // ---------------------------------------------------------------------------- | |
106 | ||
107 | enum | |
108 | { | |
109 | // menu items | |
79c5fe4b | 110 | Erase_Menu_UseBuffer = 100, |
b18eb01c | 111 | Erase_Menu_EraseBg, |
79c5fe4b VZ |
112 | Erase_Menu_Exit = wxID_EXIT, |
113 | Erase_Menu_About = wxID_ABOUT | |
33611ebb RR |
114 | }; |
115 | ||
116 | ||
117 | // ---------------------------------------------------------------------------- | |
118 | // the application class | |
119 | // ---------------------------------------------------------------------------- | |
120 | ||
121 | IMPLEMENT_APP(MyApp) | |
122 | ||
123 | bool MyApp::OnInit() | |
124 | { | |
79c5fe4b | 125 | MyFrame *frame = new MyFrame; |
33611ebb | 126 | |
07850a49 | 127 | frame->Show(true); |
98dd66cf | 128 | |
07850a49 | 129 | return true; |
33611ebb RR |
130 | } |
131 | ||
132 | // ---------------------------------------------------------------------------- | |
133 | // main frame | |
134 | // ---------------------------------------------------------------------------- | |
135 | ||
136 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
79c5fe4b | 137 | EVT_MENU(Erase_Menu_UseBuffer, MyFrame::OnUseBuffer) |
b18eb01c | 138 | EVT_MENU(Erase_Menu_EraseBg, MyFrame::OnEraseBg) |
79c5fe4b VZ |
139 | EVT_MENU(Erase_Menu_Exit, MyFrame::OnQuit) |
140 | EVT_MENU(Erase_Menu_About, MyFrame::OnAbout) | |
33611ebb RR |
141 | END_EVENT_TABLE() |
142 | ||
143 | // frame constructor | |
79c5fe4b VZ |
144 | MyFrame::MyFrame() |
145 | : wxFrame(NULL, wxID_ANY, _T("Erase sample"), | |
146 | wxPoint(50, 50), wxSize(450, 340)) | |
33611ebb | 147 | { |
33611ebb RR |
148 | SetIcon(wxICON(mondrian)); |
149 | ||
ab1ca7b3 | 150 | wxMenu *menuFile = new wxMenu(_T(""), wxMENU_TEAROFF); |
79c5fe4b | 151 | menuFile->AppendCheckItem(Erase_Menu_UseBuffer, _T("&Use memory DC\tCtrl-M")); |
b18eb01c | 152 | menuFile->AppendCheckItem(Erase_Menu_EraseBg, _T("Custom &background\tCtrl-B")); |
79c5fe4b VZ |
153 | menuFile->AppendSeparator(); |
154 | menuFile->Append(Erase_Menu_Exit, _T("E&xit\tAlt-X"), _T("Quit this program")); | |
33611ebb | 155 | |
33611ebb | 156 | |
79c5fe4b VZ |
157 | wxMenu *helpMenu = new wxMenu; |
158 | helpMenu->Append(Erase_Menu_About, _T("&About...\tCtrl-A"), _T("Show about dialog")); | |
33611ebb RR |
159 | |
160 | wxMenuBar *menuBar = new wxMenuBar(); | |
98dd66cf VZ |
161 | menuBar->Append(menuFile, _T("&File")); |
162 | menuBar->Append(helpMenu, _T("&Help")); | |
33611ebb RR |
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); | |
be5a51fb | 169 | SetStatusText(_T("Welcome to wxWidgets erase sample!")); |
33611ebb RR |
170 | #endif // wxUSE_STATUSBAR |
171 | ||
79c5fe4b | 172 | m_canvas = new MyCanvas( this ); |
33611ebb RR |
173 | } |
174 | ||
175 | ||
79c5fe4b VZ |
176 | void MyFrame::OnUseBuffer(wxCommandEvent& event) |
177 | { | |
178 | m_canvas->UseBuffer(event.IsChecked()); | |
179 | } | |
180 | ||
b18eb01c VZ |
181 | void MyFrame::OnEraseBg(wxCommandEvent& event) |
182 | { | |
183 | m_canvas->EraseBg(event.IsChecked()); | |
184 | } | |
185 | ||
33611ebb RR |
186 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
187 | { | |
07850a49 | 188 | Close(true); |
33611ebb RR |
189 | } |
190 | ||
191 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
192 | { | |
98dd66cf VZ |
193 | wxMessageBox(_T("This sample shows how you can draw custom background."), |
194 | _T("About Erase Sample"), wxOK | wxICON_INFORMATION, this); | |
33611ebb RR |
195 | } |
196 | ||
197 | ||
198 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) | |
199 | EVT_PAINT( MyCanvas::OnPaint) | |
2b5f62a0 | 200 | EVT_CHAR( MyCanvas::OnChar) |
33611ebb RR |
201 | EVT_ERASE_BACKGROUND( MyCanvas::OnEraseBackground) |
202 | END_EVENT_TABLE() | |
203 | ||
79c5fe4b | 204 | MyCanvas::MyCanvas( wxFrame *parent ) |
07850a49 | 205 | : wxScrolledWindow( parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, |
79c5fe4b | 206 | wxScrolledWindowStyle | wxSUNKEN_BORDER ) |
33611ebb | 207 | { |
b18eb01c | 208 | m_eraseBg = |
79c5fe4b VZ |
209 | m_useBuffer = false; |
210 | ||
33611ebb | 211 | SetScrollbars( 10, 10, 40, 100, 0, 0 ); |
98dd66cf VZ |
212 | |
213 | m_bitmap = wxBitmap( wxICON(mondrian) ); | |
214 | ||
07850a49 | 215 | new wxStaticBitmap( this, wxID_ANY, m_bitmap, wxPoint(80,20) ); |
ca37cfde RR |
216 | |
217 | SetFocusIgnoringChildren(); | |
33611ebb RR |
218 | } |
219 | ||
2b5f62a0 VZ |
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; | |
925e9792 | 239 | default: m_text += (wxChar)event.m_keyCode; break; |
2b5f62a0 | 240 | } |
2b5f62a0 VZ |
241 | } |
242 | ||
79c5fe4b | 243 | void MyCanvas::DoPaint(wxDC& dc) |
33611ebb | 244 | { |
b5a49d4c | 245 | dc.SetBrush( *wxBLACK_BRUSH ); |
ca37cfde | 246 | dc.DrawRectangle( 10,10,200,50 ); |
98dd66cf | 247 | |
07850a49 | 248 | dc.DrawBitmap( m_bitmap, 10, 20, true ); |
98dd66cf VZ |
249 | |
250 | dc.SetTextForeground(*wxBLUE); | |
251 | dc.DrawText(_T("This text is drawn from OnPaint"), 65, 65); | |
925e9792 | 252 | |
2b5f62a0 VZ |
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 ); | |
98dd66cf VZ |
260 | |
261 | #if 0 | |
e88be8c9 RR |
262 | wxRegionIterator upd( GetUpdateRegion() ); |
263 | while (upd) | |
264 | { | |
ab1ca7b3 | 265 | wxLogDebug( _T("Paint: %d %d %d %d"), upd.GetX(), upd.GetY(), upd.GetWidth(), upd.GetHeight() ); |
e88be8c9 RR |
266 | upd ++; |
267 | } | |
f809133f RR |
268 | #endif |
269 | ||
270 | #if 0 | |
271 | wxSize size = GetSize(); | |
272 | wxSize client_size = GetClientSize(); | |
ab1ca7b3 | 273 | wxLogDebug( _T("size %d %d client_size %d %d"), size.x, size.y, client_size.x, client_size.y ); |
f809133f | 274 | #endif |
2f6c54eb | 275 | |
98dd66cf | 276 | #if 0 |
2f6c54eb | 277 | int i; |
e88be8c9 | 278 | dc.SetPen( *wxWHITE_PEN ); |
2f6c54eb | 279 | for (i = 0; i < 20; i += 2) |
e88be8c9 | 280 | dc.DrawLine( i,i, i+100,i ); |
98dd66cf | 281 | |
f809133f | 282 | dc.SetPen( *wxWHITE_PEN ); |
2f6c54eb | 283 | for (i = 200; i < 220; i += 2) |
f809133f | 284 | dc.DrawLine( i-200,i, i-100,i ); |
98dd66cf | 285 | |
f566b4fe RR |
286 | wxRegion region( 110, 110, 80, 80 ); |
287 | wxRegion hole( 130, 130, 40, 1 ); | |
288 | region.Intersect( hole ); | |
289 | dc.SetClippingRegion( region ); | |
98dd66cf | 290 | |
33611ebb | 291 | dc.SetBrush( *wxRED_BRUSH ); |
f566b4fe | 292 | dc.DrawRectangle( 100, 100, 200, 200 ); |
98dd66cf | 293 | |
e88be8c9 | 294 | dc.DestroyClippingRegion(); |
887dd52f RR |
295 | |
296 | dc.SetPen( *wxTRANSPARENT_PEN ); | |
98dd66cf | 297 | |
887dd52f RR |
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 | } | |
98dd66cf | 305 | #endif // 0 |
33611ebb RR |
306 | } |
307 | ||
79c5fe4b VZ |
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 | ||
98dd66cf | 332 | void MyCanvas::OnEraseBackground( wxEraseEvent& event ) |
33611ebb | 333 | { |
b18eb01c VZ |
334 | if ( !m_eraseBg ) |
335 | { | |
336 | event.Skip(); | |
337 | return; | |
338 | } | |
339 | ||
98dd66cf VZ |
340 | wxDC& dc = *event.GetDC(); |
341 | dc.SetPen(*wxGREEN_PEN); | |
342 | ||
ca37cfde RR |
343 | PrepareDC( dc ); |
344 | ||
79c5fe4b | 345 | // clear any junk currently displayed |
98dd66cf VZ |
346 | dc.Clear(); |
347 | ||
79c5fe4b | 348 | const wxSize size = GetClientSize(); |
ca37cfde | 349 | for ( int x = 0; x < size.x; x += 15 ) |
98dd66cf VZ |
350 | { |
351 | dc.DrawLine(x, 0, x, size.y); | |
352 | } | |
353 | ||
ca37cfde | 354 | for ( int y = 0; y < size.y; y += 15 ) |
98dd66cf VZ |
355 | { |
356 | dc.DrawLine(0, y, size.x, y); | |
357 | } | |
358 | ||
359 | dc.SetTextForeground(*wxRED); | |
ca37cfde | 360 | dc.DrawText(_T("This text is drawn from OnEraseBackground"), 60, 160); |
33611ebb RR |
361 | } |
362 |