]> git.saurik.com Git - wxWidgets.git/blame - samples/erase/erase.cpp
documented need to install ZIP handler
[wxWidgets.git] / samples / erase / erase.cpp
CommitLineData
33611ebb
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: erase.cpp
3// Purpose: Erase wxWindows sample
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
33// need because it includes almost all "standard" wxWindows headers)
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
RR
75 void OnPaint( wxPaintEvent &event );
76 void OnEraseBackground( wxEraseEvent &event );
98dd66cf 77
b5a49d4c 78 wxBitmap m_bitmap;
33611ebb
RR
79
80private:
81 DECLARE_EVENT_TABLE()
82};
83
84// ----------------------------------------------------------------------------
85// constants
86// ----------------------------------------------------------------------------
87
88enum
89{
90 // menu items
aec18ff7 91 ID_MENU_QUIT = 1,
33611ebb
RR
92};
93
94
95// ----------------------------------------------------------------------------
96// the application class
97// ----------------------------------------------------------------------------
98
99IMPLEMENT_APP(MyApp)
100
101bool MyApp::OnInit()
102{
aec18ff7 103 MyFrame *frame = new MyFrame(_T("Erase sample"),
33611ebb
RR
104 wxPoint(50, 50), wxSize(450, 340));
105
106 frame->Show(TRUE);
98dd66cf 107
33611ebb
RR
108 return TRUE;
109}
110
111// ----------------------------------------------------------------------------
112// main frame
113// ----------------------------------------------------------------------------
114
115BEGIN_EVENT_TABLE(MyFrame, wxFrame)
aec18ff7
MB
116 EVT_MENU(ID_MENU_QUIT, MyFrame::OnQuit)
117 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
33611ebb
RR
118END_EVENT_TABLE()
119
120// frame constructor
121MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
122 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
123{
33611ebb
RR
124 SetIcon(wxICON(mondrian));
125
126 wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
127
128 wxMenu *helpMenu = new wxMenu;
aec18ff7 129 helpMenu->Append(wxID_ABOUT, _T("&About...\tCtrl-A"), _T("Show about dialog"));
33611ebb 130
aec18ff7 131 menuFile->Append(ID_MENU_QUIT, _T("E&xit\tAlt-X"), _T("Quit this program"));
33611ebb
RR
132
133 wxMenuBar *menuBar = new wxMenuBar();
98dd66cf
VZ
134 menuBar->Append(menuFile, _T("&File"));
135 menuBar->Append(helpMenu, _T("&Help"));
33611ebb
RR
136
137 SetMenuBar(menuBar);
138
139#if wxUSE_STATUSBAR
140 // create a status bar just for fun (by default with 1 pane only)
141 CreateStatusBar(2);
98dd66cf 142 SetStatusText(_T("Welcome to wxWindows erase sample!"));
33611ebb
RR
143#endif // wxUSE_STATUSBAR
144
145 (void)new MyCanvas( this );
146}
147
148
149void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
150{
151 Close(TRUE);
152}
153
154void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
155{
98dd66cf
VZ
156 wxMessageBox(_T("This sample shows how you can draw custom background."),
157 _T("About Erase Sample"), wxOK | wxICON_INFORMATION, this);
33611ebb
RR
158}
159
160
161BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
162 EVT_PAINT( MyCanvas::OnPaint)
163 EVT_ERASE_BACKGROUND( MyCanvas::OnEraseBackground)
164END_EVENT_TABLE()
165
166MyCanvas::MyCanvas( MyFrame *parent )
98dd66cf
VZ
167 : wxScrolledWindow( parent, -1, wxDefaultPosition, wxDefaultSize,
168 wxScrolledWindowStyle |
169 wxNO_FULL_REPAINT_ON_RESIZE|
170 wxSUNKEN_BORDER )
33611ebb
RR
171{
172 SetScrollbars( 10, 10, 40, 100, 0, 0 );
98dd66cf
VZ
173
174 m_bitmap = wxBitmap( wxICON(mondrian) );
175
b5a49d4c 176 new wxStaticBitmap( this, -1, m_bitmap, wxPoint(80,20) );
33611ebb
RR
177}
178
179void MyCanvas::OnPaint( wxPaintEvent &event )
180{
181 wxPaintDC dc(this);
182 PrepareDC( dc );
98dd66cf 183
b5a49d4c
RR
184 dc.SetBrush( *wxBLACK_BRUSH );
185 dc.DrawRectangle( 0,0,200,50 );
98dd66cf 186
b5a49d4c 187 dc.DrawBitmap( m_bitmap, 10, 20, TRUE );
98dd66cf
VZ
188
189 dc.SetTextForeground(*wxBLUE);
190 dc.DrawText(_T("This text is drawn from OnPaint"), 65, 65);
191
192#if 0
e88be8c9
RR
193 wxRegionIterator upd( GetUpdateRegion() );
194 while (upd)
195 {
196 wxLogDebug( "Paint: %d %d %d %d", upd.GetX(), upd.GetY(), upd.GetWidth(), upd.GetHeight() );
197 upd ++;
198 }
f809133f
RR
199#endif
200
201#if 0
202 wxSize size = GetSize();
203 wxSize client_size = GetClientSize();
204 wxLogDebug( "size %d %d client_size %d %d", size.x, size.y, client_size.x, client_size.y );
205#endif
2f6c54eb 206
98dd66cf 207#if 0
2f6c54eb 208 int i;
e88be8c9 209 dc.SetPen( *wxWHITE_PEN );
2f6c54eb 210 for (i = 0; i < 20; i += 2)
e88be8c9 211 dc.DrawLine( i,i, i+100,i );
98dd66cf 212
f809133f 213 dc.SetPen( *wxWHITE_PEN );
2f6c54eb 214 for (i = 200; i < 220; i += 2)
f809133f 215 dc.DrawLine( i-200,i, i-100,i );
98dd66cf 216
f566b4fe
RR
217 wxRegion region( 110, 110, 80, 80 );
218 wxRegion hole( 130, 130, 40, 1 );
219 region.Intersect( hole );
220 dc.SetClippingRegion( region );
98dd66cf 221
33611ebb 222 dc.SetBrush( *wxRED_BRUSH );
f566b4fe 223 dc.DrawRectangle( 100, 100, 200, 200 );
98dd66cf 224
e88be8c9 225 dc.DestroyClippingRegion();
887dd52f
RR
226
227 dc.SetPen( *wxTRANSPARENT_PEN );
98dd66cf 228
887dd52f
RR
229 wxRegion strip( 110, 200, 30, 1 );
230 wxRegionIterator it( strip );
231 while (it)
232 {
233 dc.DrawRectangle( it.GetX(), it.GetY(), it.GetWidth(), it.GetHeight() );
234 it ++;
235 }
98dd66cf 236#endif // 0
33611ebb
RR
237}
238
98dd66cf 239void MyCanvas::OnEraseBackground( wxEraseEvent& event )
33611ebb 240{
98dd66cf
VZ
241 wxDC& dc = *event.GetDC();
242 dc.SetPen(*wxGREEN_PEN);
243
244 // this line is needed, otherwise the junk is left on win the background
245 dc.Clear();
246
247 wxSize size = GetClientSize();
248 for ( int x = 0; x < size.x; x+= 10 )
249 {
250 dc.DrawLine(x, 0, x, size.y);
251 }
252
253 for ( int y = 0; y < size.y; y+= 10 )
254 {
255 dc.DrawLine(0, y, size.x, y);
256 }
257
258 dc.SetTextForeground(*wxRED);
259 dc.DrawText(_T("This text is drawn from OnEraseBackground"), 60, 60);
33611ebb
RR
260}
261