]>
Commit | Line | Data |
---|---|---|
33611ebb | 1 | ///////////////////////////////////////////////////////////////////////////// |
9c61c5b0 | 2 | // Name: samples/erase/erase.cpp |
be5a51fb | 3 | // Purpose: Erase wxWidgets sample |
9c61c5b0 | 4 | // Author: Robert Roebling, Vadim Zeitlin |
33611ebb RR |
5 | // Created: 04/01/98 |
6 | // RCS-ID: $Id$ | |
9c61c5b0 VZ |
7 | // Copyright: (c) 1998 Robert Roebling |
8 | // (c) 2009 Vadim Zeitlin | |
33611ebb RR |
9 | // Licence: wxWindows licence |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
33611ebb RR |
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 | |
be5a51fb | 28 | // need because it includes almost all "standard" wxWidgets headers) |
33611ebb RR |
29 | #ifndef WX_PRECOMP |
30 | #include "wx/wx.h" | |
31 | #endif | |
32 | ||
bbcf2821 | 33 | #include "wx/custombgwin.h" |
9c61c5b0 VZ |
34 | #include "wx/dcbuffer.h" |
35 | ||
33611ebb RR |
36 | // ---------------------------------------------------------------------------- |
37 | // resources | |
38 | // ---------------------------------------------------------------------------- | |
3cb332c1 | 39 | |
33611ebb | 40 | // the application icon |
3cb332c1 VZ |
41 | #if !defined(__WXMSW__) && !defined(__WXPM__) |
42 | #include "../sample.xpm" | |
33611ebb RR |
43 | #endif |
44 | ||
45 | // ---------------------------------------------------------------------------- | |
46 | // private classes | |
47 | // ---------------------------------------------------------------------------- | |
48 | ||
49 | class MyApp : public wxApp | |
50 | { | |
51 | public: | |
52 | virtual bool OnInit(); | |
53 | }; | |
54 | ||
55 | ||
bbcf2821 | 56 | class MyCanvas : public wxCustomBackgroundWindow<wxScrolledWindow> |
33611ebb RR |
57 | { |
58 | public: | |
9c61c5b0 | 59 | MyCanvas(wxFrame *parent); |
33611ebb | 60 | |
79c5fe4b | 61 | void UseBuffer(bool useBuffer) { m_useBuffer = useBuffer; Refresh(); } |
9c61c5b0 | 62 | bool UsesBuffer() const { return m_useBuffer; } |
33611ebb | 63 | |
cd95f7e6 VZ |
64 | void UseBgBitmap(bool useBgBmp) |
65 | { | |
66 | m_useBgBmp = useBgBmp; | |
67 | SetBackgroundBitmap(m_useBgBmp ? GetBgBitmap() : wxBitmap()); | |
68 | ||
69 | Refresh(); | |
70 | } | |
71 | ||
c753eb92 VZ |
72 | void EraseBgInPaint(bool erase) { m_eraseBgInPaint = erase; Refresh(); } |
73 | ||
33611ebb | 74 | private: |
33611ebb RR |
75 | void OnPaint( wxPaintEvent &event ); |
76 | void OnEraseBackground( wxEraseEvent &event ); | |
98dd66cf | 77 | |
79c5fe4b VZ |
78 | void DoPaint(wxDC& dc); |
79 | ||
cd95f7e6 VZ |
80 | // Create an easily recognizable background bitmap. |
81 | static wxBitmap GetBgBitmap() | |
82 | { | |
83 | static const int BMP_SIZE = 40; | |
84 | ||
85 | wxBitmap bmp(BMP_SIZE, BMP_SIZE); | |
86 | wxMemoryDC dc(bmp); | |
87 | dc.SetBackground(*wxCYAN); | |
88 | dc.Clear(); | |
89 | ||
90 | dc.SetPen(*wxBLUE_PEN); | |
91 | dc.DrawLine(0, BMP_SIZE/2, BMP_SIZE/2, 0); | |
92 | dc.DrawLine(BMP_SIZE/2, 0, BMP_SIZE, BMP_SIZE/2); | |
93 | dc.DrawLine(BMP_SIZE, BMP_SIZE/2, BMP_SIZE/2, BMP_SIZE); | |
94 | dc.DrawLine(BMP_SIZE/2, BMP_SIZE, 0, BMP_SIZE/2); | |
95 | ||
96 | return bmp; | |
97 | } | |
79c5fe4b | 98 | |
b5a49d4c | 99 | wxBitmap m_bitmap; |
33611ebb | 100 | |
79c5fe4b VZ |
101 | // use wxMemoryDC in OnPaint()? |
102 | bool m_useBuffer; | |
103 | ||
cd95f7e6 VZ |
104 | // use background bitmap? |
105 | bool m_useBgBmp; | |
106 | ||
c753eb92 VZ |
107 | // erase background in OnPaint()? |
108 | bool m_eraseBgInPaint; | |
109 | ||
b18eb01c | 110 | |
79c5fe4b VZ |
111 | DECLARE_EVENT_TABLE() |
112 | }; | |
113 | ||
114 | class MyFrame : public wxFrame | |
115 | { | |
116 | public: | |
117 | MyFrame(); | |
118 | ||
9c61c5b0 | 119 | private: |
79c5fe4b | 120 | void OnUseBuffer(wxCommandEvent& event); |
cd95f7e6 | 121 | void OnUseBgBitmap(wxCommandEvent& event); |
c753eb92 | 122 | void OnEraseBgInPaint(wxCommandEvent& event); |
9c61c5b0 | 123 | void OnChangeBgStyle(wxCommandEvent& event); |
79c5fe4b VZ |
124 | void OnQuit(wxCommandEvent& event); |
125 | void OnAbout(wxCommandEvent& event); | |
126 | ||
9c61c5b0 VZ |
127 | // we can only use double-buffering with wxBG_STYLE_PAINT |
128 | void OnUpdateUIUseBuffer(wxUpdateUIEvent& event) | |
129 | { | |
130 | event.Enable( m_canvas->GetBackgroundStyle() == wxBG_STYLE_PAINT ); | |
131 | } | |
132 | ||
133 | void OnUpdateUIChangeBgStyle(wxUpdateUIEvent& event) | |
134 | { | |
135 | event.Enable( !m_canvas->UsesBuffer() ); | |
136 | } | |
137 | ||
79c5fe4b VZ |
138 | MyCanvas *m_canvas; |
139 | ||
33611ebb RR |
140 | DECLARE_EVENT_TABLE() |
141 | }; | |
142 | ||
ab438739 VZ |
143 | class ControlWithTransparency : public wxWindow |
144 | { | |
145 | public: | |
146 | ControlWithTransparency(wxWindow *parent, | |
147 | const wxPoint& pos, | |
148 | const wxSize& size) | |
149 | : wxWindow(parent, wxID_ANY, pos, size, wxBORDER_NONE) | |
150 | { | |
151 | Connect(wxEVT_PAINT, | |
152 | wxPaintEventHandler(ControlWithTransparency::OnPaint)); | |
153 | } | |
154 | ||
155 | virtual bool HasTransparentBackground() { return true; } | |
156 | ||
157 | private: | |
158 | void OnPaint( wxPaintEvent& WXUNUSED(event) ) | |
159 | { | |
160 | wxPaintDC dc(this); | |
161 | ||
162 | dc.SetPen(*wxRED_PEN); | |
163 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
164 | dc.DrawRectangle(GetClientSize()); | |
165 | ||
166 | dc.SetTextForeground(*wxBLUE); | |
167 | dc.SetBackgroundMode(wxTRANSPARENT); | |
168 | dc.DrawText("This is custom control with transparency", 0, 2); | |
169 | } | |
170 | }; | |
79c5fe4b | 171 | |
33611ebb RR |
172 | // ---------------------------------------------------------------------------- |
173 | // constants | |
174 | // ---------------------------------------------------------------------------- | |
175 | ||
176 | enum | |
177 | { | |
178 | // menu items | |
79c5fe4b | 179 | Erase_Menu_UseBuffer = 100, |
cd95f7e6 | 180 | Erase_Menu_UseBgBitmap, |
c753eb92 | 181 | Erase_Menu_EraseBgInPaint, |
9c61c5b0 VZ |
182 | Erase_Menu_BgStyleErase, |
183 | Erase_Menu_BgStyleSystem, | |
184 | Erase_Menu_BgStylePaint, | |
79c5fe4b VZ |
185 | Erase_Menu_Exit = wxID_EXIT, |
186 | Erase_Menu_About = wxID_ABOUT | |
33611ebb RR |
187 | }; |
188 | ||
189 | ||
190 | // ---------------------------------------------------------------------------- | |
191 | // the application class | |
192 | // ---------------------------------------------------------------------------- | |
193 | ||
194 | IMPLEMENT_APP(MyApp) | |
195 | ||
196 | bool MyApp::OnInit() | |
197 | { | |
45e6e6f8 VZ |
198 | if ( !wxApp::OnInit() ) |
199 | return false; | |
200 | ||
79c5fe4b | 201 | MyFrame *frame = new MyFrame; |
33611ebb | 202 | |
07850a49 | 203 | frame->Show(true); |
98dd66cf | 204 | |
07850a49 | 205 | return true; |
33611ebb RR |
206 | } |
207 | ||
208 | // ---------------------------------------------------------------------------- | |
209 | // main frame | |
210 | // ---------------------------------------------------------------------------- | |
211 | ||
212 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
c753eb92 | 213 | EVT_MENU(Erase_Menu_UseBuffer, MyFrame::OnUseBuffer) |
cd95f7e6 | 214 | EVT_MENU(Erase_Menu_UseBgBitmap, MyFrame::OnUseBgBitmap) |
c753eb92 | 215 | EVT_MENU(Erase_Menu_EraseBgInPaint, MyFrame::OnEraseBgInPaint) |
9c61c5b0 VZ |
216 | EVT_MENU_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint, |
217 | MyFrame::OnChangeBgStyle) | |
218 | ||
79c5fe4b VZ |
219 | EVT_MENU(Erase_Menu_Exit, MyFrame::OnQuit) |
220 | EVT_MENU(Erase_Menu_About, MyFrame::OnAbout) | |
9c61c5b0 VZ |
221 | |
222 | EVT_UPDATE_UI(Erase_Menu_UseBuffer, MyFrame::OnUpdateUIUseBuffer) | |
223 | EVT_UPDATE_UI_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint, | |
224 | MyFrame::OnUpdateUIChangeBgStyle) | |
33611ebb RR |
225 | END_EVENT_TABLE() |
226 | ||
227 | // frame constructor | |
79c5fe4b | 228 | MyFrame::MyFrame() |
9c61c5b0 | 229 | : wxFrame(NULL, wxID_ANY, "Erase sample", |
79c5fe4b | 230 | wxPoint(50, 50), wxSize(450, 340)) |
33611ebb | 231 | { |
3cb332c1 | 232 | SetIcon(wxICON(sample)); |
33611ebb | 233 | |
9c61c5b0 VZ |
234 | wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF); |
235 | menuFile->AppendCheckItem(Erase_Menu_UseBuffer, "&Use memory DC\tCtrl-M"); | |
cd95f7e6 VZ |
236 | menuFile->AppendCheckItem(Erase_Menu_UseBgBitmap, |
237 | "Use background &bitmap\tCtrl-B"); | |
c753eb92 VZ |
238 | menuFile->AppendCheckItem(Erase_Menu_EraseBgInPaint, |
239 | "&Erase background in EVT_PAINT\tCtrl-R"); | |
9c61c5b0 VZ |
240 | menuFile->AppendSeparator(); |
241 | menuFile->AppendRadioItem(Erase_Menu_BgStyleErase, | |
242 | "Use wxBG_STYLE_&ERASE\tCtrl-E"); | |
243 | menuFile->AppendRadioItem(Erase_Menu_BgStyleSystem, | |
244 | "Use wxBG_STYLE_&SYSTEM\tCtrl-S"); | |
245 | menuFile->AppendRadioItem(Erase_Menu_BgStylePaint, | |
246 | "Use wxBG_STYLE_&PAINT\tCtrl-P"); | |
79c5fe4b | 247 | menuFile->AppendSeparator(); |
9c61c5b0 | 248 | menuFile->Append(Erase_Menu_Exit, "E&xit\tAlt-X", "Quit this program"); |
33611ebb | 249 | |
33611ebb | 250 | |
79c5fe4b | 251 | wxMenu *helpMenu = new wxMenu; |
9c61c5b0 | 252 | helpMenu->Append(Erase_Menu_About, "&About...\tCtrl-A", "Show about dialog"); |
33611ebb RR |
253 | |
254 | wxMenuBar *menuBar = new wxMenuBar(); | |
9c61c5b0 VZ |
255 | menuBar->Append(menuFile, "&File"); |
256 | menuBar->Append(helpMenu, "&Help"); | |
33611ebb RR |
257 | |
258 | SetMenuBar(menuBar); | |
259 | ||
79c5fe4b | 260 | m_canvas = new MyCanvas( this ); |
33611ebb RR |
261 | } |
262 | ||
263 | ||
79c5fe4b VZ |
264 | void MyFrame::OnUseBuffer(wxCommandEvent& event) |
265 | { | |
266 | m_canvas->UseBuffer(event.IsChecked()); | |
267 | } | |
268 | ||
cd95f7e6 VZ |
269 | void MyFrame::OnUseBgBitmap(wxCommandEvent& event) |
270 | { | |
271 | m_canvas->UseBgBitmap(event.IsChecked()); | |
272 | } | |
273 | ||
c753eb92 VZ |
274 | void MyFrame::OnEraseBgInPaint(wxCommandEvent& event) |
275 | { | |
276 | m_canvas->EraseBgInPaint(event.IsChecked()); | |
277 | } | |
278 | ||
9c61c5b0 | 279 | void MyFrame::OnChangeBgStyle(wxCommandEvent& event) |
b18eb01c | 280 | { |
9c61c5b0 VZ |
281 | int style = wxBG_STYLE_ERASE + event.GetId() - Erase_Menu_BgStyleErase; |
282 | m_canvas->SetBackgroundStyle(static_cast<wxBackgroundStyle>(style)); | |
283 | ||
284 | m_canvas->Refresh(); | |
b18eb01c VZ |
285 | } |
286 | ||
33611ebb RR |
287 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
288 | { | |
07850a49 | 289 | Close(true); |
33611ebb RR |
290 | } |
291 | ||
292 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
293 | { | |
9c61c5b0 VZ |
294 | wxMessageBox |
295 | ( | |
296 | "This sample shows differences between different background styles " | |
297 | "and how you may draw custom background.\n" | |
298 | "\n" | |
299 | "(c) 1998 Robert Roebling\n" | |
300 | "(c) 2009 Vadim Zeitlin\n", | |
301 | "About Erase Sample", | |
302 | wxOK | wxICON_INFORMATION, | |
303 | this | |
304 | ); | |
33611ebb RR |
305 | } |
306 | ||
307 | ||
308 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) | |
9c61c5b0 | 309 | EVT_PAINT(MyCanvas::OnPaint) |
9c61c5b0 | 310 | EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground) |
33611ebb RR |
311 | END_EVENT_TABLE() |
312 | ||
9c61c5b0 | 313 | MyCanvas::MyCanvas(wxFrame *parent) |
33611ebb | 314 | { |
bbcf2821 VZ |
315 | Create(parent, wxID_ANY); |
316 | ||
79c5fe4b | 317 | m_useBuffer = false; |
cd95f7e6 | 318 | m_useBgBmp = false; |
c753eb92 | 319 | m_eraseBgInPaint = false; |
79c5fe4b | 320 | |
33611ebb | 321 | SetScrollbars( 10, 10, 40, 100, 0, 0 ); |
98dd66cf | 322 | |
3cb332c1 | 323 | m_bitmap = wxBitmap( wxICON(sample) ); |
98dd66cf | 324 | |
07850a49 | 325 | new wxStaticBitmap( this, wxID_ANY, m_bitmap, wxPoint(80,20) ); |
11fdee42 | 326 | |
6fec48b7 VZ |
327 | new wxStaticText(this, wxID_ANY, |
328 | "Left bitmap is a wxStaticBitmap,\n" | |
329 | "right one drawn directly", | |
330 | wxPoint(150, 20)); | |
331 | ||
ab438739 VZ |
332 | new ControlWithTransparency(this, wxPoint(65, 125), wxSize(300, 22)); |
333 | ||
ca37cfde | 334 | SetFocusIgnoringChildren(); |
6fec48b7 | 335 | SetBackgroundColour(*wxCYAN); |
33611ebb RR |
336 | } |
337 | ||
79c5fe4b | 338 | void MyCanvas::DoPaint(wxDC& dc) |
33611ebb | 339 | { |
c753eb92 VZ |
340 | if ( m_eraseBgInPaint ) |
341 | { | |
342 | dc.SetBackground(*wxLIGHT_GREY); | |
343 | dc.Clear(); | |
344 | ||
345 | dc.DrawText("Background erased in OnPaint", 65, 110); | |
346 | } | |
347 | else if ( GetBackgroundStyle() == wxBG_STYLE_PAINT ) | |
348 | { | |
349 | dc.SetTextForeground(*wxRED); | |
350 | dc.DrawText("You must enable erasing background in OnPaint to avoid " | |
351 | "display corruption", 65, 110); | |
352 | } | |
353 | ||
9c61c5b0 | 354 | dc.DrawBitmap( m_bitmap, 20, 20, true ); |
98dd66cf | 355 | |
cd95f7e6 | 356 | dc.SetTextForeground(*wxRED); |
9c61c5b0 | 357 | dc.DrawText("This text is drawn from OnPaint", 65, 65); |
33611ebb RR |
358 | } |
359 | ||
79c5fe4b VZ |
360 | void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) |
361 | { | |
79c5fe4b VZ |
362 | if ( m_useBuffer ) |
363 | { | |
9c61c5b0 VZ |
364 | wxAutoBufferedPaintDC dc(this); |
365 | PrepareDC(dc); | |
79c5fe4b VZ |
366 | |
367 | DoPaint(dc); | |
79c5fe4b VZ |
368 | } |
369 | else | |
370 | { | |
9c61c5b0 VZ |
371 | wxPaintDC dc(this); |
372 | PrepareDC(dc); | |
373 | ||
374 | DoPaint(dc); | |
79c5fe4b VZ |
375 | } |
376 | } | |
377 | ||
98dd66cf | 378 | void MyCanvas::OnEraseBackground( wxEraseEvent& event ) |
33611ebb | 379 | { |
cd95f7e6 VZ |
380 | // We must not erase the background ourselves if we asked wxPanel to erase |
381 | // it using a background bitmap. | |
382 | if ( m_useBgBmp ) | |
383 | { | |
384 | event.Skip(); | |
385 | return; | |
386 | } | |
387 | ||
9c61c5b0 VZ |
388 | wxASSERT_MSG |
389 | ( | |
390 | GetBackgroundStyle() == wxBG_STYLE_ERASE, | |
391 | "shouldn't be called unless background style is \"erase\"" | |
392 | ); | |
b18eb01c | 393 | |
98dd66cf VZ |
394 | wxDC& dc = *event.GetDC(); |
395 | dc.SetPen(*wxGREEN_PEN); | |
396 | ||
ca37cfde | 397 | PrepareDC( dc ); |
11fdee42 | 398 | |
79c5fe4b | 399 | // clear any junk currently displayed |
98dd66cf VZ |
400 | dc.Clear(); |
401 | ||
79c5fe4b | 402 | const wxSize size = GetClientSize(); |
ca37cfde | 403 | for ( int x = 0; x < size.x; x += 15 ) |
98dd66cf VZ |
404 | { |
405 | dc.DrawLine(x, 0, x, size.y); | |
406 | } | |
407 | ||
ca37cfde | 408 | for ( int y = 0; y < size.y; y += 15 ) |
98dd66cf VZ |
409 | { |
410 | dc.DrawLine(0, y, size.x, y); | |
411 | } | |
412 | ||
413 | dc.SetTextForeground(*wxRED); | |
9c61c5b0 VZ |
414 | dc.SetBackgroundMode(wxSOLID); |
415 | dc.DrawText("This text is drawn from OnEraseBackground", 60, 160); | |
33611ebb RR |
416 | } |
417 |