]> git.saurik.com Git - wxWidgets.git/blame - samples/erase/erase.cpp
test wxCANCEL_DEFAULT flag
[wxWidgets.git] / samples / erase / erase.cpp
CommitLineData
33611ebb
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: erase.cpp
be5a51fb 3// Purpose: Erase wxWidgets sample
33611ebb
RR
4// Author: Robert Roebling
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Robert Roebling
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
33// ----------------------------------------------------------------------------
34// resources
35// ----------------------------------------------------------------------------
36// the application icon
f566b4fe 37#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
33611ebb
RR
38 #include "mondrian.xpm"
39#endif
40
41// ----------------------------------------------------------------------------
42// private classes
43// ----------------------------------------------------------------------------
44
45class MyApp : public wxApp
46{
47public:
48 virtual bool OnInit();
49};
50
51
79c5fe4b 52class MyCanvas : public wxScrolledWindow
33611ebb
RR
53{
54public:
79c5fe4b 55 MyCanvas( wxFrame *parent );
33611ebb 56
79c5fe4b 57 void UseBuffer(bool useBuffer) { m_useBuffer = useBuffer; Refresh(); }
b18eb01c 58 void EraseBg(bool eraseBg) { m_eraseBg = eraseBg; Refresh(); }
33611ebb
RR
59
60private:
33611ebb 61 void OnPaint( wxPaintEvent &event );
2b5f62a0 62 void OnChar( wxKeyEvent &event );
33611ebb 63 void OnEraseBackground( wxEraseEvent &event );
98dd66cf 64
79c5fe4b
VZ
65 void DoPaint(wxDC& dc);
66
67
b5a49d4c 68 wxBitmap m_bitmap;
2b5f62a0 69 wxString m_text;
33611ebb 70
79c5fe4b
VZ
71 // use wxMemoryDC in OnPaint()?
72 bool m_useBuffer;
73
b18eb01c
VZ
74 // paint custom background in OnEraseBackground()?
75 bool m_eraseBg;
76
77
79c5fe4b
VZ
78 DECLARE_EVENT_TABLE()
79};
80
81class MyFrame : public wxFrame
82{
83public:
84 MyFrame();
85
86 void OnUseBuffer(wxCommandEvent& event);
b18eb01c 87 void OnEraseBg(wxCommandEvent& event);
79c5fe4b
VZ
88 void OnQuit(wxCommandEvent& event);
89 void OnAbout(wxCommandEvent& event);
90
33611ebb 91private:
79c5fe4b
VZ
92 MyCanvas *m_canvas;
93
33611ebb
RR
94 DECLARE_EVENT_TABLE()
95};
96
79c5fe4b 97
33611ebb
RR
98// ----------------------------------------------------------------------------
99// constants
100// ----------------------------------------------------------------------------
101
102enum
103{
104 // menu items
79c5fe4b 105 Erase_Menu_UseBuffer = 100,
b18eb01c 106 Erase_Menu_EraseBg,
79c5fe4b
VZ
107 Erase_Menu_Exit = wxID_EXIT,
108 Erase_Menu_About = wxID_ABOUT
33611ebb
RR
109};
110
111
112// ----------------------------------------------------------------------------
113// the application class
114// ----------------------------------------------------------------------------
115
116IMPLEMENT_APP(MyApp)
117
118bool MyApp::OnInit()
119{
45e6e6f8
VZ
120 if ( !wxApp::OnInit() )
121 return false;
122
79c5fe4b 123 MyFrame *frame = new MyFrame;
33611ebb 124
07850a49 125 frame->Show(true);
98dd66cf 126
07850a49 127 return true;
33611ebb
RR
128}
129
130// ----------------------------------------------------------------------------
131// main frame
132// ----------------------------------------------------------------------------
133
134BEGIN_EVENT_TABLE(MyFrame, wxFrame)
79c5fe4b 135 EVT_MENU(Erase_Menu_UseBuffer, MyFrame::OnUseBuffer)
b18eb01c 136 EVT_MENU(Erase_Menu_EraseBg, MyFrame::OnEraseBg)
79c5fe4b
VZ
137 EVT_MENU(Erase_Menu_Exit, MyFrame::OnQuit)
138 EVT_MENU(Erase_Menu_About, MyFrame::OnAbout)
33611ebb
RR
139END_EVENT_TABLE()
140
141// frame constructor
79c5fe4b
VZ
142MyFrame::MyFrame()
143 : wxFrame(NULL, wxID_ANY, _T("Erase sample"),
144 wxPoint(50, 50), wxSize(450, 340))
33611ebb 145{
33611ebb
RR
146 SetIcon(wxICON(mondrian));
147
ab1ca7b3 148 wxMenu *menuFile = new wxMenu(_T(""), wxMENU_TEAROFF);
79c5fe4b 149 menuFile->AppendCheckItem(Erase_Menu_UseBuffer, _T("&Use memory DC\tCtrl-M"));
b18eb01c 150 menuFile->AppendCheckItem(Erase_Menu_EraseBg, _T("Custom &background\tCtrl-B"));
79c5fe4b
VZ
151 menuFile->AppendSeparator();
152 menuFile->Append(Erase_Menu_Exit, _T("E&xit\tAlt-X"), _T("Quit this program"));
33611ebb 153
33611ebb 154
79c5fe4b
VZ
155 wxMenu *helpMenu = new wxMenu;
156 helpMenu->Append(Erase_Menu_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));
33611ebb
RR
157
158 wxMenuBar *menuBar = new wxMenuBar();
98dd66cf
VZ
159 menuBar->Append(menuFile, _T("&File"));
160 menuBar->Append(helpMenu, _T("&Help"));
33611ebb
RR
161
162 SetMenuBar(menuBar);
163
164#if wxUSE_STATUSBAR
165 // create a status bar just for fun (by default with 1 pane only)
166 CreateStatusBar(2);
be5a51fb 167 SetStatusText(_T("Welcome to wxWidgets erase sample!"));
33611ebb
RR
168#endif // wxUSE_STATUSBAR
169
79c5fe4b 170 m_canvas = new MyCanvas( this );
33611ebb
RR
171}
172
173
79c5fe4b
VZ
174void MyFrame::OnUseBuffer(wxCommandEvent& event)
175{
176 m_canvas->UseBuffer(event.IsChecked());
177}
178
b18eb01c
VZ
179void MyFrame::OnEraseBg(wxCommandEvent& event)
180{
181 m_canvas->EraseBg(event.IsChecked());
182}
183
33611ebb
RR
184void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
185{
07850a49 186 Close(true);
33611ebb
RR
187}
188
189void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
190{
98dd66cf
VZ
191 wxMessageBox(_T("This sample shows how you can draw custom background."),
192 _T("About Erase Sample"), wxOK | wxICON_INFORMATION, this);
33611ebb
RR
193}
194
195
196BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
197 EVT_PAINT( MyCanvas::OnPaint)
2b5f62a0 198 EVT_CHAR( MyCanvas::OnChar)
33611ebb
RR
199 EVT_ERASE_BACKGROUND( MyCanvas::OnEraseBackground)
200END_EVENT_TABLE()
201
79c5fe4b 202MyCanvas::MyCanvas( wxFrame *parent )
07850a49 203 : wxScrolledWindow( parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
79c5fe4b 204 wxScrolledWindowStyle | wxSUNKEN_BORDER )
33611ebb 205{
b18eb01c 206 m_eraseBg =
79c5fe4b
VZ
207 m_useBuffer = false;
208
33611ebb 209 SetScrollbars( 10, 10, 40, 100, 0, 0 );
98dd66cf
VZ
210
211 m_bitmap = wxBitmap( wxICON(mondrian) );
212
07850a49 213 new wxStaticBitmap( this, wxID_ANY, m_bitmap, wxPoint(80,20) );
11fdee42 214
ca37cfde 215 SetFocusIgnoringChildren();
33611ebb
RR
216}
217
2b5f62a0
VZ
218void MyCanvas::OnChar( wxKeyEvent &event )
219{
220#if wxUSE_UNICODE
221 if (event.m_uniChar)
222 {
223 m_text += event.m_uniChar;
224 Refresh();
225 return;
226 }
227#endif
228
229 // some test cases
230 switch (event.m_keyCode)
231 {
232 case WXK_UP: m_text += wxT( "<UP>" ); break;
233 case WXK_LEFT: m_text += wxT( "<LEFT>" ); break;
234 case WXK_RIGHT: m_text += wxT( "<RIGHT>" ); break;
235 case WXK_DOWN: m_text += wxT( "<DOWN>" ); break;
236 case WXK_RETURN: m_text += wxT( "<ENTER>" ); break;
925e9792 237 default: m_text += (wxChar)event.m_keyCode; break;
2b5f62a0 238 }
2b5f62a0
VZ
239}
240
79c5fe4b 241void MyCanvas::DoPaint(wxDC& dc)
33611ebb 242{
b5a49d4c 243 dc.SetBrush( *wxBLACK_BRUSH );
ca37cfde 244 dc.DrawRectangle( 10,10,200,50 );
98dd66cf 245
07850a49 246 dc.DrawBitmap( m_bitmap, 10, 20, true );
98dd66cf
VZ
247
248 dc.SetTextForeground(*wxBLUE);
249 dc.DrawText(_T("This text is drawn from OnPaint"), 65, 65);
925e9792 250
2b5f62a0
VZ
251 wxString tmp;
252 tmp.Printf( _T("Hit any key to display more text: %s"), m_text.c_str() );
253 int w,h;
254 dc.GetTextExtent( tmp, &w, &h );
255 dc.SetBrush( *wxWHITE_BRUSH );
256 dc.DrawRectangle( 65, 85, w, h );
257 dc.DrawText( tmp, 65, 85 );
98dd66cf
VZ
258
259#if 0
e88be8c9
RR
260 wxRegionIterator upd( GetUpdateRegion() );
261 while (upd)
262 {
ab1ca7b3 263 wxLogDebug( _T("Paint: %d %d %d %d"), upd.GetX(), upd.GetY(), upd.GetWidth(), upd.GetHeight() );
e88be8c9
RR
264 upd ++;
265 }
f809133f
RR
266#endif
267
268#if 0
269 wxSize size = GetSize();
270 wxSize client_size = GetClientSize();
ab1ca7b3 271 wxLogDebug( _T("size %d %d client_size %d %d"), size.x, size.y, client_size.x, client_size.y );
f809133f 272#endif
2f6c54eb 273
98dd66cf 274#if 0
2f6c54eb 275 int i;
e88be8c9 276 dc.SetPen( *wxWHITE_PEN );
2f6c54eb 277 for (i = 0; i < 20; i += 2)
e88be8c9 278 dc.DrawLine( i,i, i+100,i );
98dd66cf 279
f809133f 280 dc.SetPen( *wxWHITE_PEN );
2f6c54eb 281 for (i = 200; i < 220; i += 2)
f809133f 282 dc.DrawLine( i-200,i, i-100,i );
98dd66cf 283
f566b4fe
RR
284 wxRegion region( 110, 110, 80, 80 );
285 wxRegion hole( 130, 130, 40, 1 );
286 region.Intersect( hole );
287 dc.SetClippingRegion( region );
98dd66cf 288
33611ebb 289 dc.SetBrush( *wxRED_BRUSH );
f566b4fe 290 dc.DrawRectangle( 100, 100, 200, 200 );
98dd66cf 291
e88be8c9 292 dc.DestroyClippingRegion();
887dd52f
RR
293
294 dc.SetPen( *wxTRANSPARENT_PEN );
98dd66cf 295
887dd52f
RR
296 wxRegion strip( 110, 200, 30, 1 );
297 wxRegionIterator it( strip );
298 while (it)
299 {
300 dc.DrawRectangle( it.GetX(), it.GetY(), it.GetWidth(), it.GetHeight() );
301 it ++;
302 }
98dd66cf 303#endif // 0
33611ebb
RR
304}
305
79c5fe4b
VZ
306void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
307{
308 wxPaintDC dcWin(this);
309 PrepareDC( dcWin );
310
311 if ( m_useBuffer )
312 {
313 const wxSize size = GetClientSize();
314 wxMemoryDC dc;
315 wxBitmap bmp(size.x, size.y);
316 dc.SelectObject(bmp);
317 dc.Blit(0, 0, size.x, size.y, &dcWin, 0, 0);
318 dc.DrawText(_T("(copy of background)"), 5, 120 );
319
320 DoPaint(dc);
321
322 dcWin.Blit(0, 0, size.x, size.y, &dc, 0, 0);
323 }
324 else
325 {
326 DoPaint(dcWin);
327 }
328}
329
98dd66cf 330void MyCanvas::OnEraseBackground( wxEraseEvent& event )
33611ebb 331{
b18eb01c
VZ
332 if ( !m_eraseBg )
333 {
334 event.Skip();
335 return;
336 }
337
98dd66cf
VZ
338 wxDC& dc = *event.GetDC();
339 dc.SetPen(*wxGREEN_PEN);
340
ca37cfde 341 PrepareDC( dc );
11fdee42 342
79c5fe4b 343 // clear any junk currently displayed
98dd66cf
VZ
344 dc.Clear();
345
79c5fe4b 346 const wxSize size = GetClientSize();
ca37cfde 347 for ( int x = 0; x < size.x; x += 15 )
98dd66cf
VZ
348 {
349 dc.DrawLine(x, 0, x, size.y);
350 }
351
ca37cfde 352 for ( int y = 0; y < size.y; y += 15 )
98dd66cf
VZ
353 {
354 dc.DrawLine(0, y, size.x, y);
355 }
356
357 dc.SetTextForeground(*wxRED);
ca37cfde 358 dc.DrawText(_T("This text is drawn from OnEraseBackground"), 60, 160);
33611ebb
RR
359}
360