Added a simple example of semi-transparent window to the erase sample.
[wxWidgets.git] / samples / erase / erase.cpp
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/dcbuffer.h"
34
35 // ----------------------------------------------------------------------------
36 // resources
37 // ----------------------------------------------------------------------------
38
39 // the application icon
40 #if !defined(__WXMSW__) && !defined(__WXPM__)
41 #include "../sample.xpm"
42 #endif
43
44 // ----------------------------------------------------------------------------
45 // private classes
46 // ----------------------------------------------------------------------------
47
48 class MyApp : public wxApp
49 {
50 public:
51 virtual bool OnInit();
52 };
53
54
55 class MyCanvas : public wxScrolledWindow
56 {
57 public:
58 MyCanvas(wxFrame *parent);
59
60 void UseBuffer(bool useBuffer) { m_useBuffer = useBuffer; Refresh(); }
61 bool UsesBuffer() const { return m_useBuffer; }
62
63 void UseBgBitmap(bool useBgBmp)
64 {
65 m_useBgBmp = useBgBmp;
66 SetBackgroundBitmap(m_useBgBmp ? GetBgBitmap() : wxBitmap());
67
68 Refresh();
69 }
70
71 void EraseBgInPaint(bool erase) { m_eraseBgInPaint = erase; Refresh(); }
72
73 private:
74 void OnPaint( wxPaintEvent &event );
75 void OnEraseBackground( wxEraseEvent &event );
76
77 void DoPaint(wxDC& dc);
78
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 }
97
98 wxBitmap m_bitmap;
99
100 // use wxMemoryDC in OnPaint()?
101 bool m_useBuffer;
102
103 // use background bitmap?
104 bool m_useBgBmp;
105
106 // erase background in OnPaint()?
107 bool m_eraseBgInPaint;
108
109
110 DECLARE_EVENT_TABLE()
111 };
112
113 class MyFrame : public wxFrame
114 {
115 public:
116 MyFrame();
117
118 private:
119 void OnUseBuffer(wxCommandEvent& event);
120 void OnUseBgBitmap(wxCommandEvent& event);
121 void OnEraseBgInPaint(wxCommandEvent& event);
122 void OnChangeBgStyle(wxCommandEvent& event);
123 void OnQuit(wxCommandEvent& event);
124 void OnAbout(wxCommandEvent& event);
125
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
137 MyCanvas *m_canvas;
138
139 DECLARE_EVENT_TABLE()
140 };
141
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 };
170
171 // ----------------------------------------------------------------------------
172 // constants
173 // ----------------------------------------------------------------------------
174
175 enum
176 {
177 // menu items
178 Erase_Menu_UseBuffer = 100,
179 Erase_Menu_UseBgBitmap,
180 Erase_Menu_EraseBgInPaint,
181 Erase_Menu_BgStyleErase,
182 Erase_Menu_BgStyleSystem,
183 Erase_Menu_BgStylePaint,
184 Erase_Menu_Exit = wxID_EXIT,
185 Erase_Menu_About = wxID_ABOUT
186 };
187
188
189 // ----------------------------------------------------------------------------
190 // the application class
191 // ----------------------------------------------------------------------------
192
193 IMPLEMENT_APP(MyApp)
194
195 bool MyApp::OnInit()
196 {
197 if ( !wxApp::OnInit() )
198 return false;
199
200 MyFrame *frame = new MyFrame;
201
202 frame->Show(true);
203
204 return true;
205 }
206
207 // ----------------------------------------------------------------------------
208 // main frame
209 // ----------------------------------------------------------------------------
210
211 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
212 EVT_MENU(Erase_Menu_UseBuffer, MyFrame::OnUseBuffer)
213 EVT_MENU(Erase_Menu_UseBgBitmap, MyFrame::OnUseBgBitmap)
214 EVT_MENU(Erase_Menu_EraseBgInPaint, MyFrame::OnEraseBgInPaint)
215 EVT_MENU_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint,
216 MyFrame::OnChangeBgStyle)
217
218 EVT_MENU(Erase_Menu_Exit, MyFrame::OnQuit)
219 EVT_MENU(Erase_Menu_About, MyFrame::OnAbout)
220
221 EVT_UPDATE_UI(Erase_Menu_UseBuffer, MyFrame::OnUpdateUIUseBuffer)
222 EVT_UPDATE_UI_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint,
223 MyFrame::OnUpdateUIChangeBgStyle)
224 END_EVENT_TABLE()
225
226 // frame constructor
227 MyFrame::MyFrame()
228 : wxFrame(NULL, wxID_ANY, "Erase sample",
229 wxPoint(50, 50), wxSize(450, 340))
230 {
231 SetIcon(wxICON(sample));
232
233 wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
234 menuFile->AppendCheckItem(Erase_Menu_UseBuffer, "&Use memory DC\tCtrl-M");
235 menuFile->AppendCheckItem(Erase_Menu_UseBgBitmap,
236 "Use background &bitmap\tCtrl-B");
237 menuFile->AppendCheckItem(Erase_Menu_EraseBgInPaint,
238 "&Erase background in EVT_PAINT\tCtrl-R");
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");
246 menuFile->AppendSeparator();
247 menuFile->Append(Erase_Menu_Exit, "E&xit\tAlt-X", "Quit this program");
248
249
250 wxMenu *helpMenu = new wxMenu;
251 helpMenu->Append(Erase_Menu_About, "&About...\tCtrl-A", "Show about dialog");
252
253 wxMenuBar *menuBar = new wxMenuBar();
254 menuBar->Append(menuFile, "&File");
255 menuBar->Append(helpMenu, "&Help");
256
257 SetMenuBar(menuBar);
258
259 m_canvas = new MyCanvas( this );
260 }
261
262
263 void MyFrame::OnUseBuffer(wxCommandEvent& event)
264 {
265 m_canvas->UseBuffer(event.IsChecked());
266 }
267
268 void MyFrame::OnUseBgBitmap(wxCommandEvent& event)
269 {
270 m_canvas->UseBgBitmap(event.IsChecked());
271 }
272
273 void MyFrame::OnEraseBgInPaint(wxCommandEvent& event)
274 {
275 m_canvas->EraseBgInPaint(event.IsChecked());
276 }
277
278 void MyFrame::OnChangeBgStyle(wxCommandEvent& event)
279 {
280 int style = wxBG_STYLE_ERASE + event.GetId() - Erase_Menu_BgStyleErase;
281 m_canvas->SetBackgroundStyle(static_cast<wxBackgroundStyle>(style));
282
283 m_canvas->Refresh();
284 }
285
286 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
287 {
288 Close(true);
289 }
290
291 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
292 {
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 );
304 }
305
306
307 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
308 EVT_PAINT(MyCanvas::OnPaint)
309 EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground)
310 END_EVENT_TABLE()
311
312 MyCanvas::MyCanvas(wxFrame *parent)
313 : wxScrolledWindow(parent, wxID_ANY)
314 {
315 m_useBuffer = false;
316 m_useBgBmp = false;
317 m_eraseBgInPaint = false;
318
319 SetScrollbars( 10, 10, 40, 100, 0, 0 );
320
321 m_bitmap = wxBitmap( wxICON(sample) );
322
323 new wxStaticBitmap( this, wxID_ANY, m_bitmap, wxPoint(80,20) );
324
325 new wxStaticText(this, wxID_ANY,
326 "Left bitmap is a wxStaticBitmap,\n"
327 "right one drawn directly",
328 wxPoint(150, 20));
329
330 new ControlWithTransparency(this, wxPoint(65, 125), wxSize(300, 22));
331
332 SetFocusIgnoringChildren();
333 SetBackgroundColour(*wxCYAN);
334 }
335
336 void MyCanvas::DoPaint(wxDC& dc)
337 {
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
352 dc.DrawBitmap( m_bitmap, 20, 20, true );
353
354 dc.SetTextForeground(*wxRED);
355 dc.DrawText("This text is drawn from OnPaint", 65, 65);
356 }
357
358 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
359 {
360 if ( m_useBuffer )
361 {
362 wxAutoBufferedPaintDC dc(this);
363 PrepareDC(dc);
364
365 DoPaint(dc);
366 }
367 else
368 {
369 wxPaintDC dc(this);
370 PrepareDC(dc);
371
372 DoPaint(dc);
373 }
374 }
375
376 void MyCanvas::OnEraseBackground( wxEraseEvent& event )
377 {
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
386 wxASSERT_MSG
387 (
388 GetBackgroundStyle() == wxBG_STYLE_ERASE,
389 "shouldn't be called unless background style is \"erase\""
390 );
391
392 wxDC& dc = *event.GetDC();
393 dc.SetPen(*wxGREEN_PEN);
394
395 PrepareDC( dc );
396
397 // clear any junk currently displayed
398 dc.Clear();
399
400 const wxSize size = GetClientSize();
401 for ( int x = 0; x < size.x; x += 15 )
402 {
403 dc.DrawLine(x, 0, x, size.y);
404 }
405
406 for ( int y = 0; y < size.y; y += 15 )
407 {
408 dc.DrawLine(0, y, size.x, y);
409 }
410
411 dc.SetTextForeground(*wxRED);
412 dc.SetBackgroundMode(wxSOLID);
413 dc.DrawText("This text is drawn from OnEraseBackground", 60, 160);
414 }
415