]> git.saurik.com Git - wxWidgets.git/blame - samples/erase/erase.cpp
delete-->delete[]
[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
788233da 20#if defined(__GNUG__) && !defined(__APPLE__)
4d2c67a9
JS
21 #pragma implementation "erase.cpp"
22 #pragma interface "erase.cpp"
33611ebb
RR
23#endif
24
25// For compilers that support precompilation, includes "wx/wx.h".
26#include "wx/wxprec.h"
27
28#ifdef __BORLANDC__
29 #pragma hdrstop
30#endif
31
32// for all others, include the necessary headers (this file is usually all you
be5a51fb 33// need because it includes almost all "standard" wxWidgets headers)
33611ebb
RR
34#ifndef WX_PRECOMP
35 #include "wx/wx.h"
36#endif
37
38// ----------------------------------------------------------------------------
39// resources
40// ----------------------------------------------------------------------------
41// the application icon
f566b4fe 42#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
33611ebb
RR
43 #include "mondrian.xpm"
44#endif
45
46// ----------------------------------------------------------------------------
47// private classes
48// ----------------------------------------------------------------------------
49
50class MyApp : public wxApp
51{
52public:
53 virtual bool OnInit();
54};
55
56
57class MyFrame : public wxFrame
58{
59public:
60 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
61
62 void OnQuit(wxCommandEvent& event);
63 void OnAbout(wxCommandEvent& event);
64
65private:
66 DECLARE_EVENT_TABLE()
67};
68
69
70class MyCanvas : public wxScrolledWindow
71{
72public:
73 MyCanvas( MyFrame *parent );
98dd66cf 74
33611ebb 75 void OnPaint( wxPaintEvent &event );
2b5f62a0 76 void OnChar( wxKeyEvent &event );
33611ebb 77 void OnEraseBackground( wxEraseEvent &event );
98dd66cf 78
b5a49d4c 79 wxBitmap m_bitmap;
2b5f62a0 80 wxString m_text;
33611ebb
RR
81
82private:
83 DECLARE_EVENT_TABLE()
84};
85
86// ----------------------------------------------------------------------------
87// constants
88// ----------------------------------------------------------------------------
89
90enum
91{
92 // menu items
aec18ff7 93 ID_MENU_QUIT = 1,
33611ebb
RR
94};
95
96
97// ----------------------------------------------------------------------------
98// the application class
99// ----------------------------------------------------------------------------
100
101IMPLEMENT_APP(MyApp)
102
103bool MyApp::OnInit()
104{
aec18ff7 105 MyFrame *frame = new MyFrame(_T("Erase sample"),
33611ebb
RR
106 wxPoint(50, 50), wxSize(450, 340));
107
07850a49 108 frame->Show(true);
98dd66cf 109
07850a49 110 return true;
33611ebb
RR
111}
112
113// ----------------------------------------------------------------------------
114// main frame
115// ----------------------------------------------------------------------------
116
117BEGIN_EVENT_TABLE(MyFrame, wxFrame)
aec18ff7
MB
118 EVT_MENU(ID_MENU_QUIT, MyFrame::OnQuit)
119 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
33611ebb
RR
120END_EVENT_TABLE()
121
122// frame constructor
123MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
07850a49 124 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
33611ebb 125{
33611ebb
RR
126 SetIcon(wxICON(mondrian));
127
ab1ca7b3 128 wxMenu *menuFile = new wxMenu(_T(""), wxMENU_TEAROFF);
33611ebb
RR
129
130 wxMenu *helpMenu = new wxMenu;
aec18ff7 131 helpMenu->Append(wxID_ABOUT, _T("&About...\tCtrl-A"), _T("Show about dialog"));
33611ebb 132
aec18ff7 133 menuFile->Append(ID_MENU_QUIT, _T("E&xit\tAlt-X"), _T("Quit this program"));
33611ebb
RR
134
135 wxMenuBar *menuBar = new wxMenuBar();
98dd66cf
VZ
136 menuBar->Append(menuFile, _T("&File"));
137 menuBar->Append(helpMenu, _T("&Help"));
33611ebb
RR
138
139 SetMenuBar(menuBar);
140
141#if wxUSE_STATUSBAR
142 // create a status bar just for fun (by default with 1 pane only)
143 CreateStatusBar(2);
be5a51fb 144 SetStatusText(_T("Welcome to wxWidgets erase sample!"));
33611ebb
RR
145#endif // wxUSE_STATUSBAR
146
147 (void)new MyCanvas( this );
148}
149
150
151void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
152{
07850a49 153 Close(true);
33611ebb
RR
154}
155
156void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
157{
98dd66cf
VZ
158 wxMessageBox(_T("This sample shows how you can draw custom background."),
159 _T("About Erase Sample"), wxOK | wxICON_INFORMATION, this);
33611ebb
RR
160}
161
162
163BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
164 EVT_PAINT( MyCanvas::OnPaint)
2b5f62a0 165 EVT_CHAR( MyCanvas::OnChar)
33611ebb
RR
166 EVT_ERASE_BACKGROUND( MyCanvas::OnEraseBackground)
167END_EVENT_TABLE()
168
169MyCanvas::MyCanvas( MyFrame *parent )
07850a49 170 : wxScrolledWindow( parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
98dd66cf
VZ
171 wxScrolledWindowStyle |
172 wxNO_FULL_REPAINT_ON_RESIZE|
173 wxSUNKEN_BORDER )
33611ebb
RR
174{
175 SetScrollbars( 10, 10, 40, 100, 0, 0 );
98dd66cf
VZ
176
177 m_bitmap = wxBitmap( wxICON(mondrian) );
178
07850a49 179 new wxStaticBitmap( this, wxID_ANY, m_bitmap, wxPoint(80,20) );
33611ebb
RR
180}
181
2b5f62a0
VZ
182void MyCanvas::OnChar( wxKeyEvent &event )
183{
184#if wxUSE_UNICODE
185 if (event.m_uniChar)
186 {
187 m_text += event.m_uniChar;
188 Refresh();
189 return;
190 }
191#endif
192
193 // some test cases
194 switch (event.m_keyCode)
195 {
196 case WXK_UP: m_text += wxT( "<UP>" ); break;
197 case WXK_LEFT: m_text += wxT( "<LEFT>" ); break;
198 case WXK_RIGHT: m_text += wxT( "<RIGHT>" ); break;
199 case WXK_DOWN: m_text += wxT( "<DOWN>" ); break;
200 case WXK_RETURN: m_text += wxT( "<ENTER>" ); break;
925e9792 201 default: m_text += (wxChar)event.m_keyCode; break;
2b5f62a0 202 }
925e9792 203
2b5f62a0
VZ
204}
205
87728739 206void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
33611ebb
RR
207{
208 wxPaintDC dc(this);
209 PrepareDC( dc );
98dd66cf 210
b5a49d4c
RR
211 dc.SetBrush( *wxBLACK_BRUSH );
212 dc.DrawRectangle( 0,0,200,50 );
98dd66cf 213
07850a49 214 dc.DrawBitmap( m_bitmap, 10, 20, true );
98dd66cf
VZ
215
216 dc.SetTextForeground(*wxBLUE);
217 dc.DrawText(_T("This text is drawn from OnPaint"), 65, 65);
925e9792 218
2b5f62a0
VZ
219 wxString tmp;
220 tmp.Printf( _T("Hit any key to display more text: %s"), m_text.c_str() );
221 int w,h;
222 dc.GetTextExtent( tmp, &w, &h );
223 dc.SetBrush( *wxWHITE_BRUSH );
224 dc.DrawRectangle( 65, 85, w, h );
225 dc.DrawText( tmp, 65, 85 );
98dd66cf
VZ
226
227#if 0
e88be8c9
RR
228 wxRegionIterator upd( GetUpdateRegion() );
229 while (upd)
230 {
ab1ca7b3 231 wxLogDebug( _T("Paint: %d %d %d %d"), upd.GetX(), upd.GetY(), upd.GetWidth(), upd.GetHeight() );
e88be8c9
RR
232 upd ++;
233 }
f809133f
RR
234#endif
235
236#if 0
237 wxSize size = GetSize();
238 wxSize client_size = GetClientSize();
ab1ca7b3 239 wxLogDebug( _T("size %d %d client_size %d %d"), size.x, size.y, client_size.x, client_size.y );
f809133f 240#endif
2f6c54eb 241
98dd66cf 242#if 0
2f6c54eb 243 int i;
e88be8c9 244 dc.SetPen( *wxWHITE_PEN );
2f6c54eb 245 for (i = 0; i < 20; i += 2)
e88be8c9 246 dc.DrawLine( i,i, i+100,i );
98dd66cf 247
f809133f 248 dc.SetPen( *wxWHITE_PEN );
2f6c54eb 249 for (i = 200; i < 220; i += 2)
f809133f 250 dc.DrawLine( i-200,i, i-100,i );
98dd66cf 251
f566b4fe
RR
252 wxRegion region( 110, 110, 80, 80 );
253 wxRegion hole( 130, 130, 40, 1 );
254 region.Intersect( hole );
255 dc.SetClippingRegion( region );
98dd66cf 256
33611ebb 257 dc.SetBrush( *wxRED_BRUSH );
f566b4fe 258 dc.DrawRectangle( 100, 100, 200, 200 );
98dd66cf 259
e88be8c9 260 dc.DestroyClippingRegion();
887dd52f
RR
261
262 dc.SetPen( *wxTRANSPARENT_PEN );
98dd66cf 263
887dd52f
RR
264 wxRegion strip( 110, 200, 30, 1 );
265 wxRegionIterator it( strip );
266 while (it)
267 {
268 dc.DrawRectangle( it.GetX(), it.GetY(), it.GetWidth(), it.GetHeight() );
269 it ++;
270 }
98dd66cf 271#endif // 0
33611ebb
RR
272}
273
98dd66cf 274void MyCanvas::OnEraseBackground( wxEraseEvent& event )
33611ebb 275{
98dd66cf
VZ
276 wxDC& dc = *event.GetDC();
277 dc.SetPen(*wxGREEN_PEN);
278
279 // this line is needed, otherwise the junk is left on win the background
280 dc.Clear();
281
282 wxSize size = GetClientSize();
283 for ( int x = 0; x < size.x; x+= 10 )
284 {
285 dc.DrawLine(x, 0, x, size.y);
286 }
287
288 for ( int y = 0; y < size.y; y+= 10 )
289 {
290 dc.DrawLine(0, y, size.x, y);
291 }
292
293 dc.SetTextForeground(*wxRED);
294 dc.DrawText(_T("This text is drawn from OnEraseBackground"), 60, 60);
33611ebb
RR
295}
296