]>
Commit | Line | Data |
---|---|---|
33611ebb | 1 | ///////////////////////////////////////////////////////////////////////////// |
9c61c5b0 | 2 | // Name: samples/erase/erase.cpp |
be5a51fb | 3 | // Purpose: Erase wxWidgets sample |
9c61c5b0 | 4 | // Author: Robert Roebling, Vadim Zeitlin |
33611ebb RR |
5 | // Created: 04/01/98 |
6 | // RCS-ID: $Id$ | |
9c61c5b0 VZ |
7 | // Copyright: (c) 1998 Robert Roebling |
8 | // (c) 2009 Vadim Zeitlin | |
33611ebb RR |
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 | ||
9c61c5b0 VZ |
33 | #include "wx/dcbuffer.h" |
34 | ||
33611ebb RR |
35 | // ---------------------------------------------------------------------------- |
36 | // resources | |
37 | // ---------------------------------------------------------------------------- | |
3cb332c1 | 38 | |
33611ebb | 39 | // the application icon |
3cb332c1 VZ |
40 | #if !defined(__WXMSW__) && !defined(__WXPM__) |
41 | #include "../sample.xpm" | |
33611ebb RR |
42 | #endif |
43 | ||
44 | // ---------------------------------------------------------------------------- | |
45 | // private classes | |
46 | // ---------------------------------------------------------------------------- | |
47 | ||
48 | class MyApp : public wxApp | |
49 | { | |
50 | public: | |
51 | virtual bool OnInit(); | |
52 | }; | |
53 | ||
54 | ||
79c5fe4b | 55 | class MyCanvas : public wxScrolledWindow |
33611ebb RR |
56 | { |
57 | public: | |
9c61c5b0 | 58 | MyCanvas(wxFrame *parent); |
33611ebb | 59 | |
79c5fe4b | 60 | void UseBuffer(bool useBuffer) { m_useBuffer = useBuffer; Refresh(); } |
9c61c5b0 | 61 | bool UsesBuffer() const { return m_useBuffer; } |
33611ebb | 62 | |
c753eb92 VZ |
63 | void EraseBgInPaint(bool erase) { m_eraseBgInPaint = erase; Refresh(); } |
64 | ||
33611ebb | 65 | private: |
33611ebb | 66 | void OnPaint( wxPaintEvent &event ); |
2b5f62a0 | 67 | void OnChar( wxKeyEvent &event ); |
33611ebb | 68 | void OnEraseBackground( wxEraseEvent &event ); |
98dd66cf | 69 | |
79c5fe4b VZ |
70 | void DoPaint(wxDC& dc); |
71 | ||
72 | ||
b5a49d4c | 73 | wxBitmap m_bitmap; |
2b5f62a0 | 74 | wxString m_text; |
33611ebb | 75 | |
79c5fe4b VZ |
76 | // use wxMemoryDC in OnPaint()? |
77 | bool m_useBuffer; | |
78 | ||
c753eb92 VZ |
79 | // erase background in OnPaint()? |
80 | bool m_eraseBgInPaint; | |
81 | ||
b18eb01c | 82 | |
79c5fe4b VZ |
83 | DECLARE_EVENT_TABLE() |
84 | }; | |
85 | ||
86 | class MyFrame : public wxFrame | |
87 | { | |
88 | public: | |
89 | MyFrame(); | |
90 | ||
9c61c5b0 | 91 | private: |
79c5fe4b | 92 | void OnUseBuffer(wxCommandEvent& event); |
c753eb92 | 93 | void OnEraseBgInPaint(wxCommandEvent& event); |
9c61c5b0 | 94 | void OnChangeBgStyle(wxCommandEvent& event); |
79c5fe4b VZ |
95 | void OnQuit(wxCommandEvent& event); |
96 | void OnAbout(wxCommandEvent& event); | |
97 | ||
9c61c5b0 VZ |
98 | // we can only use double-buffering with wxBG_STYLE_PAINT |
99 | void OnUpdateUIUseBuffer(wxUpdateUIEvent& event) | |
100 | { | |
101 | event.Enable( m_canvas->GetBackgroundStyle() == wxBG_STYLE_PAINT ); | |
102 | } | |
103 | ||
104 | void OnUpdateUIChangeBgStyle(wxUpdateUIEvent& event) | |
105 | { | |
106 | event.Enable( !m_canvas->UsesBuffer() ); | |
107 | } | |
108 | ||
79c5fe4b VZ |
109 | MyCanvas *m_canvas; |
110 | ||
33611ebb RR |
111 | DECLARE_EVENT_TABLE() |
112 | }; | |
113 | ||
79c5fe4b | 114 | |
33611ebb RR |
115 | // ---------------------------------------------------------------------------- |
116 | // constants | |
117 | // ---------------------------------------------------------------------------- | |
118 | ||
119 | enum | |
120 | { | |
121 | // menu items | |
79c5fe4b | 122 | Erase_Menu_UseBuffer = 100, |
c753eb92 | 123 | Erase_Menu_EraseBgInPaint, |
9c61c5b0 VZ |
124 | Erase_Menu_BgStyleErase, |
125 | Erase_Menu_BgStyleSystem, | |
126 | Erase_Menu_BgStylePaint, | |
79c5fe4b VZ |
127 | Erase_Menu_Exit = wxID_EXIT, |
128 | Erase_Menu_About = wxID_ABOUT | |
33611ebb RR |
129 | }; |
130 | ||
131 | ||
132 | // ---------------------------------------------------------------------------- | |
133 | // the application class | |
134 | // ---------------------------------------------------------------------------- | |
135 | ||
136 | IMPLEMENT_APP(MyApp) | |
137 | ||
138 | bool MyApp::OnInit() | |
139 | { | |
45e6e6f8 VZ |
140 | if ( !wxApp::OnInit() ) |
141 | return false; | |
142 | ||
79c5fe4b | 143 | MyFrame *frame = new MyFrame; |
33611ebb | 144 | |
07850a49 | 145 | frame->Show(true); |
98dd66cf | 146 | |
07850a49 | 147 | return true; |
33611ebb RR |
148 | } |
149 | ||
150 | // ---------------------------------------------------------------------------- | |
151 | // main frame | |
152 | // ---------------------------------------------------------------------------- | |
153 | ||
154 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
c753eb92 VZ |
155 | EVT_MENU(Erase_Menu_UseBuffer, MyFrame::OnUseBuffer) |
156 | EVT_MENU(Erase_Menu_EraseBgInPaint, MyFrame::OnEraseBgInPaint) | |
9c61c5b0 VZ |
157 | EVT_MENU_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint, |
158 | MyFrame::OnChangeBgStyle) | |
159 | ||
79c5fe4b VZ |
160 | EVT_MENU(Erase_Menu_Exit, MyFrame::OnQuit) |
161 | EVT_MENU(Erase_Menu_About, MyFrame::OnAbout) | |
9c61c5b0 VZ |
162 | |
163 | EVT_UPDATE_UI(Erase_Menu_UseBuffer, MyFrame::OnUpdateUIUseBuffer) | |
164 | EVT_UPDATE_UI_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint, | |
165 | MyFrame::OnUpdateUIChangeBgStyle) | |
33611ebb RR |
166 | END_EVENT_TABLE() |
167 | ||
168 | // frame constructor | |
79c5fe4b | 169 | MyFrame::MyFrame() |
9c61c5b0 | 170 | : wxFrame(NULL, wxID_ANY, "Erase sample", |
79c5fe4b | 171 | wxPoint(50, 50), wxSize(450, 340)) |
33611ebb | 172 | { |
3cb332c1 | 173 | SetIcon(wxICON(sample)); |
33611ebb | 174 | |
9c61c5b0 VZ |
175 | wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF); |
176 | menuFile->AppendCheckItem(Erase_Menu_UseBuffer, "&Use memory DC\tCtrl-M"); | |
c753eb92 VZ |
177 | menuFile->AppendCheckItem(Erase_Menu_EraseBgInPaint, |
178 | "&Erase background in EVT_PAINT\tCtrl-R"); | |
9c61c5b0 VZ |
179 | menuFile->AppendSeparator(); |
180 | menuFile->AppendRadioItem(Erase_Menu_BgStyleErase, | |
181 | "Use wxBG_STYLE_&ERASE\tCtrl-E"); | |
182 | menuFile->AppendRadioItem(Erase_Menu_BgStyleSystem, | |
183 | "Use wxBG_STYLE_&SYSTEM\tCtrl-S"); | |
184 | menuFile->AppendRadioItem(Erase_Menu_BgStylePaint, | |
185 | "Use wxBG_STYLE_&PAINT\tCtrl-P"); | |
79c5fe4b | 186 | menuFile->AppendSeparator(); |
9c61c5b0 | 187 | menuFile->Append(Erase_Menu_Exit, "E&xit\tAlt-X", "Quit this program"); |
33611ebb | 188 | |
33611ebb | 189 | |
79c5fe4b | 190 | wxMenu *helpMenu = new wxMenu; |
9c61c5b0 | 191 | helpMenu->Append(Erase_Menu_About, "&About...\tCtrl-A", "Show about dialog"); |
33611ebb RR |
192 | |
193 | wxMenuBar *menuBar = new wxMenuBar(); | |
9c61c5b0 VZ |
194 | menuBar->Append(menuFile, "&File"); |
195 | menuBar->Append(helpMenu, "&Help"); | |
33611ebb RR |
196 | |
197 | SetMenuBar(menuBar); | |
198 | ||
79c5fe4b | 199 | m_canvas = new MyCanvas( this ); |
33611ebb RR |
200 | } |
201 | ||
202 | ||
79c5fe4b VZ |
203 | void MyFrame::OnUseBuffer(wxCommandEvent& event) |
204 | { | |
205 | m_canvas->UseBuffer(event.IsChecked()); | |
206 | } | |
207 | ||
c753eb92 VZ |
208 | void MyFrame::OnEraseBgInPaint(wxCommandEvent& event) |
209 | { | |
210 | m_canvas->EraseBgInPaint(event.IsChecked()); | |
211 | } | |
212 | ||
9c61c5b0 | 213 | void MyFrame::OnChangeBgStyle(wxCommandEvent& event) |
b18eb01c | 214 | { |
9c61c5b0 VZ |
215 | int style = wxBG_STYLE_ERASE + event.GetId() - Erase_Menu_BgStyleErase; |
216 | m_canvas->SetBackgroundStyle(static_cast<wxBackgroundStyle>(style)); | |
217 | ||
218 | m_canvas->Refresh(); | |
b18eb01c VZ |
219 | } |
220 | ||
33611ebb RR |
221 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
222 | { | |
07850a49 | 223 | Close(true); |
33611ebb RR |
224 | } |
225 | ||
226 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
227 | { | |
9c61c5b0 VZ |
228 | wxMessageBox |
229 | ( | |
230 | "This sample shows differences between different background styles " | |
231 | "and how you may draw custom background.\n" | |
232 | "\n" | |
233 | "(c) 1998 Robert Roebling\n" | |
234 | "(c) 2009 Vadim Zeitlin\n", | |
235 | "About Erase Sample", | |
236 | wxOK | wxICON_INFORMATION, | |
237 | this | |
238 | ); | |
33611ebb RR |
239 | } |
240 | ||
241 | ||
242 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) | |
9c61c5b0 VZ |
243 | EVT_PAINT(MyCanvas::OnPaint) |
244 | EVT_CHAR(MyCanvas::OnChar) | |
245 | EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground) | |
33611ebb RR |
246 | END_EVENT_TABLE() |
247 | ||
9c61c5b0 VZ |
248 | MyCanvas::MyCanvas(wxFrame *parent) |
249 | : wxScrolledWindow(parent, wxID_ANY) | |
33611ebb | 250 | { |
79c5fe4b | 251 | m_useBuffer = false; |
c753eb92 | 252 | m_eraseBgInPaint = false; |
79c5fe4b | 253 | |
33611ebb | 254 | SetScrollbars( 10, 10, 40, 100, 0, 0 ); |
98dd66cf | 255 | |
3cb332c1 | 256 | m_bitmap = wxBitmap( wxICON(sample) ); |
98dd66cf | 257 | |
07850a49 | 258 | new wxStaticBitmap( this, wxID_ANY, m_bitmap, wxPoint(80,20) ); |
11fdee42 | 259 | |
ca37cfde | 260 | SetFocusIgnoringChildren(); |
9c61c5b0 | 261 | SetBackgroundColour(*wxBLUE); |
33611ebb RR |
262 | } |
263 | ||
2b5f62a0 VZ |
264 | void MyCanvas::OnChar( wxKeyEvent &event ) |
265 | { | |
266 | #if wxUSE_UNICODE | |
267 | if (event.m_uniChar) | |
268 | { | |
269 | m_text += event.m_uniChar; | |
270 | Refresh(); | |
271 | return; | |
272 | } | |
273 | #endif | |
274 | ||
275 | // some test cases | |
276 | switch (event.m_keyCode) | |
277 | { | |
278 | case WXK_UP: m_text += wxT( "<UP>" ); break; | |
279 | case WXK_LEFT: m_text += wxT( "<LEFT>" ); break; | |
280 | case WXK_RIGHT: m_text += wxT( "<RIGHT>" ); break; | |
281 | case WXK_DOWN: m_text += wxT( "<DOWN>" ); break; | |
282 | case WXK_RETURN: m_text += wxT( "<ENTER>" ); break; | |
925e9792 | 283 | default: m_text += (wxChar)event.m_keyCode; break; |
2b5f62a0 | 284 | } |
2b5f62a0 VZ |
285 | } |
286 | ||
79c5fe4b | 287 | void MyCanvas::DoPaint(wxDC& dc) |
33611ebb | 288 | { |
c753eb92 VZ |
289 | if ( m_eraseBgInPaint ) |
290 | { | |
291 | dc.SetBackground(*wxLIGHT_GREY); | |
292 | dc.Clear(); | |
293 | ||
294 | dc.DrawText("Background erased in OnPaint", 65, 110); | |
295 | } | |
296 | else if ( GetBackgroundStyle() == wxBG_STYLE_PAINT ) | |
297 | { | |
298 | dc.SetTextForeground(*wxRED); | |
299 | dc.DrawText("You must enable erasing background in OnPaint to avoid " | |
300 | "display corruption", 65, 110); | |
301 | } | |
302 | ||
b5a49d4c | 303 | dc.SetBrush( *wxBLACK_BRUSH ); |
9c61c5b0 | 304 | dc.DrawRectangle( 10,10,60,50 ); |
98dd66cf | 305 | |
9c61c5b0 | 306 | dc.DrawBitmap( m_bitmap, 20, 20, true ); |
98dd66cf | 307 | |
9c61c5b0 VZ |
308 | dc.SetTextForeground(*wxWHITE); |
309 | dc.DrawText("This text is drawn from OnPaint", 65, 65); | |
925e9792 | 310 | |
2b5f62a0 | 311 | wxString tmp; |
9c61c5b0 VZ |
312 | tmp.Printf("Hit any key to display more text: %s", m_text); |
313 | ||
2b5f62a0 VZ |
314 | int w,h; |
315 | dc.GetTextExtent( tmp, &w, &h ); | |
2b5f62a0 VZ |
316 | dc.DrawRectangle( 65, 85, w, h ); |
317 | dc.DrawText( tmp, 65, 85 ); | |
33611ebb RR |
318 | } |
319 | ||
79c5fe4b VZ |
320 | void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) |
321 | { | |
79c5fe4b VZ |
322 | if ( m_useBuffer ) |
323 | { | |
9c61c5b0 VZ |
324 | wxAutoBufferedPaintDC dc(this); |
325 | PrepareDC(dc); | |
79c5fe4b VZ |
326 | |
327 | DoPaint(dc); | |
79c5fe4b VZ |
328 | } |
329 | else | |
330 | { | |
9c61c5b0 VZ |
331 | wxPaintDC dc(this); |
332 | PrepareDC(dc); | |
333 | ||
334 | DoPaint(dc); | |
79c5fe4b VZ |
335 | } |
336 | } | |
337 | ||
98dd66cf | 338 | void MyCanvas::OnEraseBackground( wxEraseEvent& event ) |
33611ebb | 339 | { |
9c61c5b0 VZ |
340 | wxASSERT_MSG |
341 | ( | |
342 | GetBackgroundStyle() == wxBG_STYLE_ERASE, | |
343 | "shouldn't be called unless background style is \"erase\"" | |
344 | ); | |
b18eb01c | 345 | |
98dd66cf VZ |
346 | wxDC& dc = *event.GetDC(); |
347 | dc.SetPen(*wxGREEN_PEN); | |
348 | ||
ca37cfde | 349 | PrepareDC( dc ); |
11fdee42 | 350 | |
79c5fe4b | 351 | // clear any junk currently displayed |
98dd66cf VZ |
352 | dc.Clear(); |
353 | ||
79c5fe4b | 354 | const wxSize size = GetClientSize(); |
ca37cfde | 355 | for ( int x = 0; x < size.x; x += 15 ) |
98dd66cf VZ |
356 | { |
357 | dc.DrawLine(x, 0, x, size.y); | |
358 | } | |
359 | ||
ca37cfde | 360 | for ( int y = 0; y < size.y; y += 15 ) |
98dd66cf VZ |
361 | { |
362 | dc.DrawLine(0, y, size.x, y); | |
363 | } | |
364 | ||
365 | dc.SetTextForeground(*wxRED); | |
9c61c5b0 VZ |
366 | dc.SetBackgroundMode(wxSOLID); |
367 | dc.DrawText("This text is drawn from OnEraseBackground", 60, 160); | |
33611ebb RR |
368 | } |
369 |