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