]> git.saurik.com Git - wxWidgets.git/blame - samples/erase/erase.cpp
Allow saving images under a different name in the image sample.
[wxWidgets.git] / samples / erase / erase.cpp
CommitLineData
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 34#include "wx/dcbuffer.h"
14421681 35#include "wx/artprov.h"
9c61c5b0 36
33611ebb
RR
37// ----------------------------------------------------------------------------
38// resources
39// ----------------------------------------------------------------------------
3cb332c1 40
33611ebb 41// the application icon
e7092398 42#ifndef wxHAS_IMAGES_IN_RESOURCES
3cb332c1 43 #include "../sample.xpm"
33611ebb
RR
44#endif
45
46// ----------------------------------------------------------------------------
47// private classes
48// ----------------------------------------------------------------------------
49
50class MyApp : public wxApp
51{
52public:
53 virtual bool OnInit();
54};
55
56
bbcf2821 57class MyCanvas : public wxCustomBackgroundWindow<wxScrolledWindow>
33611ebb
RR
58{
59public:
9c61c5b0 60 MyCanvas(wxFrame *parent);
33611ebb 61
79c5fe4b 62 void UseBuffer(bool useBuffer) { m_useBuffer = useBuffer; Refresh(); }
9c61c5b0 63 bool UsesBuffer() const { return m_useBuffer; }
33611ebb 64
cd95f7e6
VZ
65 void UseBgBitmap(bool useBgBmp)
66 {
67 m_useBgBmp = useBgBmp;
68 SetBackgroundBitmap(m_useBgBmp ? GetBgBitmap() : wxBitmap());
69
70 Refresh();
71 }
72
c753eb92
VZ
73 void EraseBgInPaint(bool erase) { m_eraseBgInPaint = erase; Refresh(); }
74
33611ebb 75private:
33611ebb
RR
76 void OnPaint( wxPaintEvent &event );
77 void OnEraseBackground( wxEraseEvent &event );
98dd66cf 78
79c5fe4b
VZ
79 void DoPaint(wxDC& dc);
80
cd95f7e6
VZ
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 }
79c5fe4b 99
b5a49d4c 100 wxBitmap m_bitmap;
33611ebb 101
79c5fe4b
VZ
102 // use wxMemoryDC in OnPaint()?
103 bool m_useBuffer;
104
cd95f7e6
VZ
105 // use background bitmap?
106 bool m_useBgBmp;
107
c753eb92
VZ
108 // erase background in OnPaint()?
109 bool m_eraseBgInPaint;
110
b18eb01c 111
79c5fe4b
VZ
112 DECLARE_EVENT_TABLE()
113};
114
115class MyFrame : public wxFrame
116{
117public:
118 MyFrame();
119
9c61c5b0 120private:
79c5fe4b 121 void OnUseBuffer(wxCommandEvent& event);
cd95f7e6 122 void OnUseBgBitmap(wxCommandEvent& event);
c753eb92 123 void OnEraseBgInPaint(wxCommandEvent& event);
9c61c5b0 124 void OnChangeBgStyle(wxCommandEvent& event);
79c5fe4b
VZ
125 void OnQuit(wxCommandEvent& event);
126 void OnAbout(wxCommandEvent& event);
127
9c61c5b0
VZ
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
79c5fe4b
VZ
139 MyCanvas *m_canvas;
140
33611ebb
RR
141 DECLARE_EVENT_TABLE()
142};
143
ab438739
VZ
144class ControlWithTransparency : public wxWindow
145{
146public:
147 ControlWithTransparency(wxWindow *parent,
148 const wxPoint& pos,
149 const wxSize& size)
ab438739 150 {
14421681
VZ
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);
ab438739
VZ
163 Connect(wxEVT_PAINT,
164 wxPaintEventHandler(ControlWithTransparency::OnPaint));
ab438739 165
14421681
VZ
166 if ( !reason.empty() )
167 {
168 // This can be only done now, after creating the window.
169 SetToolTip(reason);
170 }
171 }
ab438739
VZ
172
173private:
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);
14421681
VZ
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);
ab438739 192 }
14421681
VZ
193
194 wxString m_message;
ab438739 195};
79c5fe4b 196
33611ebb
RR
197// ----------------------------------------------------------------------------
198// constants
199// ----------------------------------------------------------------------------
200
201enum
202{
203 // menu items
79c5fe4b 204 Erase_Menu_UseBuffer = 100,
cd95f7e6 205 Erase_Menu_UseBgBitmap,
c753eb92 206 Erase_Menu_EraseBgInPaint,
9c61c5b0
VZ
207 Erase_Menu_BgStyleErase,
208 Erase_Menu_BgStyleSystem,
209 Erase_Menu_BgStylePaint,
79c5fe4b
VZ
210 Erase_Menu_Exit = wxID_EXIT,
211 Erase_Menu_About = wxID_ABOUT
33611ebb
RR
212};
213
214
215// ----------------------------------------------------------------------------
216// the application class
217// ----------------------------------------------------------------------------
218
219IMPLEMENT_APP(MyApp)
220
221bool MyApp::OnInit()
222{
45e6e6f8
VZ
223 if ( !wxApp::OnInit() )
224 return false;
225
79c5fe4b 226 MyFrame *frame = new MyFrame;
33611ebb 227
07850a49 228 frame->Show(true);
98dd66cf 229
07850a49 230 return true;
33611ebb
RR
231}
232
233// ----------------------------------------------------------------------------
234// main frame
235// ----------------------------------------------------------------------------
236
237BEGIN_EVENT_TABLE(MyFrame, wxFrame)
c753eb92 238 EVT_MENU(Erase_Menu_UseBuffer, MyFrame::OnUseBuffer)
cd95f7e6 239 EVT_MENU(Erase_Menu_UseBgBitmap, MyFrame::OnUseBgBitmap)
c753eb92 240 EVT_MENU(Erase_Menu_EraseBgInPaint, MyFrame::OnEraseBgInPaint)
9c61c5b0
VZ
241 EVT_MENU_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint,
242 MyFrame::OnChangeBgStyle)
243
79c5fe4b
VZ
244 EVT_MENU(Erase_Menu_Exit, MyFrame::OnQuit)
245 EVT_MENU(Erase_Menu_About, MyFrame::OnAbout)
9c61c5b0
VZ
246
247 EVT_UPDATE_UI(Erase_Menu_UseBuffer, MyFrame::OnUpdateUIUseBuffer)
248 EVT_UPDATE_UI_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint,
249 MyFrame::OnUpdateUIChangeBgStyle)
33611ebb
RR
250END_EVENT_TABLE()
251
252// frame constructor
79c5fe4b 253MyFrame::MyFrame()
9c61c5b0 254 : wxFrame(NULL, wxID_ANY, "Erase sample",
79c5fe4b 255 wxPoint(50, 50), wxSize(450, 340))
33611ebb 256{
3cb332c1 257 SetIcon(wxICON(sample));
33611ebb 258
9c61c5b0
VZ
259 wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
260 menuFile->AppendCheckItem(Erase_Menu_UseBuffer, "&Use memory DC\tCtrl-M");
cd95f7e6
VZ
261 menuFile->AppendCheckItem(Erase_Menu_UseBgBitmap,
262 "Use background &bitmap\tCtrl-B");
c753eb92
VZ
263 menuFile->AppendCheckItem(Erase_Menu_EraseBgInPaint,
264 "&Erase background in EVT_PAINT\tCtrl-R");
9c61c5b0
VZ
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");
79c5fe4b 272 menuFile->AppendSeparator();
9c61c5b0 273 menuFile->Append(Erase_Menu_Exit, "E&xit\tAlt-X", "Quit this program");
33611ebb 274
33611ebb 275
79c5fe4b 276 wxMenu *helpMenu = new wxMenu;
2d143b66 277 helpMenu->Append(Erase_Menu_About, "&About\tCtrl-A", "Show about dialog");
33611ebb
RR
278
279 wxMenuBar *menuBar = new wxMenuBar();
9c61c5b0
VZ
280 menuBar->Append(menuFile, "&File");
281 menuBar->Append(helpMenu, "&Help");
33611ebb
RR
282
283 SetMenuBar(menuBar);
284
79c5fe4b 285 m_canvas = new MyCanvas( this );
33611ebb
RR
286}
287
288
79c5fe4b
VZ
289void MyFrame::OnUseBuffer(wxCommandEvent& event)
290{
291 m_canvas->UseBuffer(event.IsChecked());
292}
293
cd95f7e6
VZ
294void MyFrame::OnUseBgBitmap(wxCommandEvent& event)
295{
296 m_canvas->UseBgBitmap(event.IsChecked());
297}
298
c753eb92
VZ
299void MyFrame::OnEraseBgInPaint(wxCommandEvent& event)
300{
301 m_canvas->EraseBgInPaint(event.IsChecked());
302}
303
9c61c5b0 304void MyFrame::OnChangeBgStyle(wxCommandEvent& event)
b18eb01c 305{
9c61c5b0
VZ
306 int style = wxBG_STYLE_ERASE + event.GetId() - Erase_Menu_BgStyleErase;
307 m_canvas->SetBackgroundStyle(static_cast<wxBackgroundStyle>(style));
308
309 m_canvas->Refresh();
b18eb01c
VZ
310}
311
33611ebb
RR
312void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
313{
07850a49 314 Close(true);
33611ebb
RR
315}
316
317void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
318{
9c61c5b0
VZ
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 );
33611ebb
RR
330}
331
332
333BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
9c61c5b0 334 EVT_PAINT(MyCanvas::OnPaint)
9c61c5b0 335 EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground)
33611ebb
RR
336END_EVENT_TABLE()
337
9c61c5b0 338MyCanvas::MyCanvas(wxFrame *parent)
33611ebb 339{
bbcf2821
VZ
340 Create(parent, wxID_ANY);
341
79c5fe4b 342 m_useBuffer = false;
cd95f7e6 343 m_useBgBmp = false;
c753eb92 344 m_eraseBgInPaint = false;
79c5fe4b 345
33611ebb 346 SetScrollbars( 10, 10, 40, 100, 0, 0 );
98dd66cf 347
3cb332c1 348 m_bitmap = wxBitmap( wxICON(sample) );
98dd66cf 349
07850a49 350 new wxStaticBitmap( this, wxID_ANY, m_bitmap, wxPoint(80,20) );
11fdee42 351
6fec48b7
VZ
352 new wxStaticText(this, wxID_ANY,
353 "Left bitmap is a wxStaticBitmap,\n"
354 "right one drawn directly",
355 wxPoint(150, 20));
356
14421681 357 new ControlWithTransparency(this, wxPoint(65, 125), wxSize(350, 22));
ab438739 358
ca37cfde 359 SetFocusIgnoringChildren();
6fec48b7 360 SetBackgroundColour(*wxCYAN);
33611ebb
RR
361}
362
79c5fe4b 363void MyCanvas::DoPaint(wxDC& dc)
33611ebb 364{
c753eb92
VZ
365 if ( m_eraseBgInPaint )
366 {
367 dc.SetBackground(*wxLIGHT_GREY);
368 dc.Clear();
369
370 dc.DrawText("Background erased in OnPaint", 65, 110);
371 }
372 else if ( GetBackgroundStyle() == wxBG_STYLE_PAINT )
373 {
374 dc.SetTextForeground(*wxRED);
375 dc.DrawText("You must enable erasing background in OnPaint to avoid "
376 "display corruption", 65, 110);
377 }
378
9c61c5b0 379 dc.DrawBitmap( m_bitmap, 20, 20, true );
98dd66cf 380
cd95f7e6 381 dc.SetTextForeground(*wxRED);
9c61c5b0 382 dc.DrawText("This text is drawn from OnPaint", 65, 65);
33611ebb
RR
383}
384
79c5fe4b
VZ
385void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
386{
79c5fe4b
VZ
387 if ( m_useBuffer )
388 {
9c61c5b0
VZ
389 wxAutoBufferedPaintDC dc(this);
390 PrepareDC(dc);
79c5fe4b
VZ
391
392 DoPaint(dc);
79c5fe4b
VZ
393 }
394 else
395 {
9c61c5b0
VZ
396 wxPaintDC dc(this);
397 PrepareDC(dc);
398
399 DoPaint(dc);
79c5fe4b
VZ
400 }
401}
402
98dd66cf 403void MyCanvas::OnEraseBackground( wxEraseEvent& event )
33611ebb 404{
cd95f7e6
VZ
405 // We must not erase the background ourselves if we asked wxPanel to erase
406 // it using a background bitmap.
407 if ( m_useBgBmp )
408 {
409 event.Skip();
410 return;
411 }
412
9c61c5b0
VZ
413 wxASSERT_MSG
414 (
415 GetBackgroundStyle() == wxBG_STYLE_ERASE,
416 "shouldn't be called unless background style is \"erase\""
417 );
b18eb01c 418
98dd66cf
VZ
419 wxDC& dc = *event.GetDC();
420 dc.SetPen(*wxGREEN_PEN);
421
ca37cfde 422 PrepareDC( dc );
11fdee42 423
79c5fe4b 424 // clear any junk currently displayed
98dd66cf
VZ
425 dc.Clear();
426
79c5fe4b 427 const wxSize size = GetClientSize();
ca37cfde 428 for ( int x = 0; x < size.x; x += 15 )
98dd66cf
VZ
429 {
430 dc.DrawLine(x, 0, x, size.y);
431 }
432
ca37cfde 433 for ( int y = 0; y < size.y; y += 15 )
98dd66cf
VZ
434 {
435 dc.DrawLine(0, y, size.x, y);
436 }
437
438 dc.SetTextForeground(*wxRED);
9c61c5b0
VZ
439 dc.SetBackgroundMode(wxSOLID);
440 dc.DrawText("This text is drawn from OnEraseBackground", 60, 160);
33611ebb
RR
441}
442