| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: samples/erase/erase.cpp |
| 3 | // Purpose: Erase wxWidgets sample |
| 4 | // Author: Robert Roebling, Vadim Zeitlin |
| 5 | // Created: 04/01/98 |
| 6 | // RCS-ID: $Id$ |
| 7 | // Copyright: (c) 1998 Robert Roebling |
| 8 | // (c) 2009 Vadim Zeitlin |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 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 |
| 28 | // need because it includes almost all "standard" wxWidgets headers) |
| 29 | #ifndef WX_PRECOMP |
| 30 | #include "wx/wx.h" |
| 31 | #endif |
| 32 | |
| 33 | #include "wx/dcbuffer.h" |
| 34 | |
| 35 | // ---------------------------------------------------------------------------- |
| 36 | // resources |
| 37 | // ---------------------------------------------------------------------------- |
| 38 | |
| 39 | // the application icon |
| 40 | #if !defined(__WXMSW__) && !defined(__WXPM__) |
| 41 | #include "../sample.xpm" |
| 42 | #endif |
| 43 | |
| 44 | // ---------------------------------------------------------------------------- |
| 45 | // private classes |
| 46 | // ---------------------------------------------------------------------------- |
| 47 | |
| 48 | class MyApp : public wxApp |
| 49 | { |
| 50 | public: |
| 51 | virtual bool OnInit(); |
| 52 | }; |
| 53 | |
| 54 | |
| 55 | class MyCanvas : public wxScrolledWindow |
| 56 | { |
| 57 | public: |
| 58 | MyCanvas(wxFrame *parent); |
| 59 | |
| 60 | void UseBuffer(bool useBuffer) { m_useBuffer = useBuffer; Refresh(); } |
| 61 | bool UsesBuffer() const { return m_useBuffer; } |
| 62 | |
| 63 | void EraseBgInPaint(bool erase) { m_eraseBgInPaint = erase; Refresh(); } |
| 64 | |
| 65 | private: |
| 66 | void OnPaint( wxPaintEvent &event ); |
| 67 | void OnChar( wxKeyEvent &event ); |
| 68 | void OnEraseBackground( wxEraseEvent &event ); |
| 69 | |
| 70 | void DoPaint(wxDC& dc); |
| 71 | |
| 72 | |
| 73 | wxBitmap m_bitmap; |
| 74 | wxString m_text; |
| 75 | |
| 76 | // use wxMemoryDC in OnPaint()? |
| 77 | bool m_useBuffer; |
| 78 | |
| 79 | // erase background in OnPaint()? |
| 80 | bool m_eraseBgInPaint; |
| 81 | |
| 82 | |
| 83 | DECLARE_EVENT_TABLE() |
| 84 | }; |
| 85 | |
| 86 | class MyFrame : public wxFrame |
| 87 | { |
| 88 | public: |
| 89 | MyFrame(); |
| 90 | |
| 91 | private: |
| 92 | void OnUseBuffer(wxCommandEvent& event); |
| 93 | void OnEraseBgInPaint(wxCommandEvent& event); |
| 94 | void OnChangeBgStyle(wxCommandEvent& event); |
| 95 | void OnQuit(wxCommandEvent& event); |
| 96 | void OnAbout(wxCommandEvent& event); |
| 97 | |
| 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 | |
| 109 | MyCanvas *m_canvas; |
| 110 | |
| 111 | DECLARE_EVENT_TABLE() |
| 112 | }; |
| 113 | |
| 114 | |
| 115 | // ---------------------------------------------------------------------------- |
| 116 | // constants |
| 117 | // ---------------------------------------------------------------------------- |
| 118 | |
| 119 | enum |
| 120 | { |
| 121 | // menu items |
| 122 | Erase_Menu_UseBuffer = 100, |
| 123 | Erase_Menu_EraseBgInPaint, |
| 124 | Erase_Menu_BgStyleErase, |
| 125 | Erase_Menu_BgStyleSystem, |
| 126 | Erase_Menu_BgStylePaint, |
| 127 | Erase_Menu_Exit = wxID_EXIT, |
| 128 | Erase_Menu_About = wxID_ABOUT |
| 129 | }; |
| 130 | |
| 131 | |
| 132 | // ---------------------------------------------------------------------------- |
| 133 | // the application class |
| 134 | // ---------------------------------------------------------------------------- |
| 135 | |
| 136 | IMPLEMENT_APP(MyApp) |
| 137 | |
| 138 | bool MyApp::OnInit() |
| 139 | { |
| 140 | if ( !wxApp::OnInit() ) |
| 141 | return false; |
| 142 | |
| 143 | MyFrame *frame = new MyFrame; |
| 144 | |
| 145 | frame->Show(true); |
| 146 | |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | // ---------------------------------------------------------------------------- |
| 151 | // main frame |
| 152 | // ---------------------------------------------------------------------------- |
| 153 | |
| 154 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
| 155 | EVT_MENU(Erase_Menu_UseBuffer, MyFrame::OnUseBuffer) |
| 156 | EVT_MENU(Erase_Menu_EraseBgInPaint, MyFrame::OnEraseBgInPaint) |
| 157 | EVT_MENU_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint, |
| 158 | MyFrame::OnChangeBgStyle) |
| 159 | |
| 160 | EVT_MENU(Erase_Menu_Exit, MyFrame::OnQuit) |
| 161 | EVT_MENU(Erase_Menu_About, MyFrame::OnAbout) |
| 162 | |
| 163 | EVT_UPDATE_UI(Erase_Menu_UseBuffer, MyFrame::OnUpdateUIUseBuffer) |
| 164 | EVT_UPDATE_UI_RANGE(Erase_Menu_BgStyleErase, Erase_Menu_BgStylePaint, |
| 165 | MyFrame::OnUpdateUIChangeBgStyle) |
| 166 | END_EVENT_TABLE() |
| 167 | |
| 168 | // frame constructor |
| 169 | MyFrame::MyFrame() |
| 170 | : wxFrame(NULL, wxID_ANY, "Erase sample", |
| 171 | wxPoint(50, 50), wxSize(450, 340)) |
| 172 | { |
| 173 | SetIcon(wxICON(sample)); |
| 174 | |
| 175 | wxMenu *menuFile = new wxMenu("", wxMENU_TEAROFF); |
| 176 | menuFile->AppendCheckItem(Erase_Menu_UseBuffer, "&Use memory DC\tCtrl-M"); |
| 177 | menuFile->AppendCheckItem(Erase_Menu_EraseBgInPaint, |
| 178 | "&Erase background in EVT_PAINT\tCtrl-R"); |
| 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"); |
| 186 | menuFile->AppendSeparator(); |
| 187 | menuFile->Append(Erase_Menu_Exit, "E&xit\tAlt-X", "Quit this program"); |
| 188 | |
| 189 | |
| 190 | wxMenu *helpMenu = new wxMenu; |
| 191 | helpMenu->Append(Erase_Menu_About, "&About...\tCtrl-A", "Show about dialog"); |
| 192 | |
| 193 | wxMenuBar *menuBar = new wxMenuBar(); |
| 194 | menuBar->Append(menuFile, "&File"); |
| 195 | menuBar->Append(helpMenu, "&Help"); |
| 196 | |
| 197 | SetMenuBar(menuBar); |
| 198 | |
| 199 | m_canvas = new MyCanvas( this ); |
| 200 | } |
| 201 | |
| 202 | |
| 203 | void MyFrame::OnUseBuffer(wxCommandEvent& event) |
| 204 | { |
| 205 | m_canvas->UseBuffer(event.IsChecked()); |
| 206 | } |
| 207 | |
| 208 | void MyFrame::OnEraseBgInPaint(wxCommandEvent& event) |
| 209 | { |
| 210 | m_canvas->EraseBgInPaint(event.IsChecked()); |
| 211 | } |
| 212 | |
| 213 | void MyFrame::OnChangeBgStyle(wxCommandEvent& event) |
| 214 | { |
| 215 | int style = wxBG_STYLE_ERASE + event.GetId() - Erase_Menu_BgStyleErase; |
| 216 | m_canvas->SetBackgroundStyle(static_cast<wxBackgroundStyle>(style)); |
| 217 | |
| 218 | m_canvas->Refresh(); |
| 219 | } |
| 220 | |
| 221 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
| 222 | { |
| 223 | Close(true); |
| 224 | } |
| 225 | |
| 226 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) |
| 227 | { |
| 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 | ); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) |
| 243 | EVT_PAINT(MyCanvas::OnPaint) |
| 244 | EVT_CHAR(MyCanvas::OnChar) |
| 245 | EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground) |
| 246 | END_EVENT_TABLE() |
| 247 | |
| 248 | MyCanvas::MyCanvas(wxFrame *parent) |
| 249 | : wxScrolledWindow(parent, wxID_ANY) |
| 250 | { |
| 251 | m_useBuffer = false; |
| 252 | m_eraseBgInPaint = false; |
| 253 | |
| 254 | SetScrollbars( 10, 10, 40, 100, 0, 0 ); |
| 255 | |
| 256 | m_bitmap = wxBitmap( wxICON(sample) ); |
| 257 | |
| 258 | new wxStaticBitmap( this, wxID_ANY, m_bitmap, wxPoint(80,20) ); |
| 259 | |
| 260 | SetFocusIgnoringChildren(); |
| 261 | SetBackgroundColour(*wxBLUE); |
| 262 | } |
| 263 | |
| 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; |
| 283 | default: m_text += (wxChar)event.m_keyCode; break; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | void MyCanvas::DoPaint(wxDC& dc) |
| 288 | { |
| 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 | |
| 303 | dc.SetBrush( *wxBLACK_BRUSH ); |
| 304 | dc.DrawRectangle( 10,10,60,50 ); |
| 305 | |
| 306 | dc.DrawBitmap( m_bitmap, 20, 20, true ); |
| 307 | |
| 308 | dc.SetTextForeground(*wxWHITE); |
| 309 | dc.DrawText("This text is drawn from OnPaint", 65, 65); |
| 310 | |
| 311 | wxString tmp; |
| 312 | tmp.Printf("Hit any key to display more text: %s", m_text); |
| 313 | |
| 314 | int w,h; |
| 315 | dc.GetTextExtent( tmp, &w, &h ); |
| 316 | dc.DrawRectangle( 65, 85, w, h ); |
| 317 | dc.DrawText( tmp, 65, 85 ); |
| 318 | } |
| 319 | |
| 320 | void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) |
| 321 | { |
| 322 | if ( m_useBuffer ) |
| 323 | { |
| 324 | wxAutoBufferedPaintDC dc(this); |
| 325 | PrepareDC(dc); |
| 326 | |
| 327 | DoPaint(dc); |
| 328 | } |
| 329 | else |
| 330 | { |
| 331 | wxPaintDC dc(this); |
| 332 | PrepareDC(dc); |
| 333 | |
| 334 | DoPaint(dc); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | void MyCanvas::OnEraseBackground( wxEraseEvent& event ) |
| 339 | { |
| 340 | wxASSERT_MSG |
| 341 | ( |
| 342 | GetBackgroundStyle() == wxBG_STYLE_ERASE, |
| 343 | "shouldn't be called unless background style is \"erase\"" |
| 344 | ); |
| 345 | |
| 346 | wxDC& dc = *event.GetDC(); |
| 347 | dc.SetPen(*wxGREEN_PEN); |
| 348 | |
| 349 | PrepareDC( dc ); |
| 350 | |
| 351 | // clear any junk currently displayed |
| 352 | dc.Clear(); |
| 353 | |
| 354 | const wxSize size = GetClientSize(); |
| 355 | for ( int x = 0; x < size.x; x += 15 ) |
| 356 | { |
| 357 | dc.DrawLine(x, 0, x, size.y); |
| 358 | } |
| 359 | |
| 360 | for ( int y = 0; y < size.y; y += 15 ) |
| 361 | { |
| 362 | dc.DrawLine(0, y, size.x, y); |
| 363 | } |
| 364 | |
| 365 | dc.SetTextForeground(*wxRED); |
| 366 | dc.SetBackgroundMode(wxSOLID); |
| 367 | dc.DrawText("This text is drawn from OnEraseBackground", 60, 160); |
| 368 | } |
| 369 | |