Patch [ 584078 ] Misc samples fixes from Dimitri Schoolwerth
[wxWidgets.git] / samples / erase / erase.cpp
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
20 #ifdef __GNUG__
21 #pragma implementation "erase.cpp"
22 #pragma interface "erase.cpp"
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
42 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
43 #include "mondrian.xpm"
44 #endif
45
46 // ----------------------------------------------------------------------------
47 // private classes
48 // ----------------------------------------------------------------------------
49
50 class MyApp : public wxApp
51 {
52 public:
53 virtual bool OnInit();
54 };
55
56
57 class MyFrame : public wxFrame
58 {
59 public:
60 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
61
62 void OnQuit(wxCommandEvent& event);
63 void OnAbout(wxCommandEvent& event);
64
65 private:
66 DECLARE_EVENT_TABLE()
67 };
68
69
70 class MyCanvas : public wxScrolledWindow
71 {
72 public:
73 MyCanvas( MyFrame *parent );
74
75 void OnPaint( wxPaintEvent &event );
76 void OnEraseBackground( wxEraseEvent &event );
77
78 wxBitmap m_bitmap;
79
80 private:
81 DECLARE_EVENT_TABLE()
82 };
83
84 // ----------------------------------------------------------------------------
85 // constants
86 // ----------------------------------------------------------------------------
87
88 enum
89 {
90 // menu items
91 ID_MENU_QUIT = 1,
92 };
93
94
95 // ----------------------------------------------------------------------------
96 // the application class
97 // ----------------------------------------------------------------------------
98
99 IMPLEMENT_APP(MyApp)
100
101 bool MyApp::OnInit()
102 {
103 MyFrame *frame = new MyFrame(_T("Erase sample"),
104 wxPoint(50, 50), wxSize(450, 340));
105
106 frame->Show(TRUE);
107
108 return TRUE;
109 }
110
111 // ----------------------------------------------------------------------------
112 // main frame
113 // ----------------------------------------------------------------------------
114
115 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
116 EVT_MENU(ID_MENU_QUIT, MyFrame::OnQuit)
117 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
118 END_EVENT_TABLE()
119
120 // frame constructor
121 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
122 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
123 {
124 SetIcon(wxICON(mondrian));
125
126 wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF);
127
128 wxMenu *helpMenu = new wxMenu;
129 helpMenu->Append(wxID_ABOUT, _T("&About...\tCtrl-A"), _T("Show about dialog"));
130
131 menuFile->Append(ID_MENU_QUIT, _T("E&xit\tAlt-X"), _T("Quit this program"));
132
133 wxMenuBar *menuBar = new wxMenuBar();
134 menuBar->Append(menuFile, _T("&File"));
135 menuBar->Append(helpMenu, _T("&Help"));
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);
142 SetStatusText(_T("Welcome to wxWindows erase sample!"));
143 #endif // wxUSE_STATUSBAR
144
145 (void)new MyCanvas( this );
146 }
147
148
149 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
150 {
151 Close(TRUE);
152 }
153
154 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
155 {
156 wxMessageBox(_T("This sample shows how you can draw custom background."),
157 _T("About Erase Sample"), wxOK | wxICON_INFORMATION, this);
158 }
159
160
161 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
162 EVT_PAINT( MyCanvas::OnPaint)
163 EVT_ERASE_BACKGROUND( MyCanvas::OnEraseBackground)
164 END_EVENT_TABLE()
165
166 MyCanvas::MyCanvas( MyFrame *parent )
167 : wxScrolledWindow( parent, -1, wxDefaultPosition, wxDefaultSize,
168 wxScrolledWindowStyle |
169 wxNO_FULL_REPAINT_ON_RESIZE|
170 wxSUNKEN_BORDER )
171 {
172 SetScrollbars( 10, 10, 40, 100, 0, 0 );
173
174 m_bitmap = wxBitmap( wxICON(mondrian) );
175
176 new wxStaticBitmap( this, -1, m_bitmap, wxPoint(80,20) );
177 }
178
179 void MyCanvas::OnPaint( wxPaintEvent &event )
180 {
181 wxPaintDC dc(this);
182 PrepareDC( dc );
183
184 dc.SetBrush( *wxBLACK_BRUSH );
185 dc.DrawRectangle( 0,0,200,50 );
186
187 dc.DrawBitmap( m_bitmap, 10, 20, TRUE );
188
189 dc.SetTextForeground(*wxBLUE);
190 dc.DrawText(_T("This text is drawn from OnPaint"), 65, 65);
191
192 #if 0
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 }
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
206
207 #if 0
208 int i;
209 dc.SetPen( *wxWHITE_PEN );
210 for (i = 0; i < 20; i += 2)
211 dc.DrawLine( i,i, i+100,i );
212
213 dc.SetPen( *wxWHITE_PEN );
214 for (i = 200; i < 220; i += 2)
215 dc.DrawLine( i-200,i, i-100,i );
216
217 wxRegion region( 110, 110, 80, 80 );
218 wxRegion hole( 130, 130, 40, 1 );
219 region.Intersect( hole );
220 dc.SetClippingRegion( region );
221
222 dc.SetBrush( *wxRED_BRUSH );
223 dc.DrawRectangle( 100, 100, 200, 200 );
224
225 dc.DestroyClippingRegion();
226
227 dc.SetPen( *wxTRANSPARENT_PEN );
228
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 }
236 #endif // 0
237 }
238
239 void MyCanvas::OnEraseBackground( wxEraseEvent& event )
240 {
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);
260 }
261