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