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