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