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