home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / wxos2240.zip / wxWindows-2.4.0 / samples / erase / erase.cpp next >
C/C++ Source or Header  |  2002-12-16  |  8KB  |  297 lines

  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: erase.cpp,v 1.13.2.3 2002/12/14 18:19:47 MBN Exp $
  8. // Copyright:   (c) Robert Roebling
  9. // Licence:     wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11.  
  12. // ============================================================================
  13. // declarations
  14. // ============================================================================
  15.  
  16. // ----------------------------------------------------------------------------
  17. // headers
  18. // ----------------------------------------------------------------------------
  19.  
  20. #if defined(__GNUG__) && !defined(__APPLE__)
  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 OnChar( wxKeyEvent &event );
  77.     void OnEraseBackground( wxEraseEvent &event );
  78.  
  79.     wxBitmap    m_bitmap;
  80.     wxString    m_text;
  81.  
  82. private:
  83.     DECLARE_EVENT_TABLE()
  84. };
  85.  
  86. // ----------------------------------------------------------------------------
  87. // constants
  88. // ----------------------------------------------------------------------------
  89.  
  90. enum
  91. {
  92.     // menu items
  93.     ID_MENU_QUIT = 1,
  94. };
  95.  
  96.  
  97. // ----------------------------------------------------------------------------
  98. // the application class
  99. // ----------------------------------------------------------------------------
  100.  
  101. IMPLEMENT_APP(MyApp)
  102.  
  103. bool MyApp::OnInit()
  104. {
  105.     MyFrame *frame = new MyFrame(_T("Erase sample"),
  106.                                  wxPoint(50, 50), wxSize(450, 340));
  107.  
  108.     frame->Show(TRUE);
  109.  
  110.     return TRUE;
  111. }
  112.  
  113. // ----------------------------------------------------------------------------
  114. // main frame
  115. // ----------------------------------------------------------------------------
  116.  
  117. BEGIN_EVENT_TABLE(MyFrame, wxFrame)
  118.     EVT_MENU(ID_MENU_QUIT,  MyFrame::OnQuit)
  119.     EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
  120. END_EVENT_TABLE()
  121.  
  122. // frame constructor
  123. MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
  124.        : wxFrame((wxFrame *)NULL, -1, title, pos, size)
  125. {
  126.     SetIcon(wxICON(mondrian));
  127.  
  128.     wxMenu *menuFile = new wxMenu(_T(""), wxMENU_TEAROFF);
  129.  
  130.     wxMenu *helpMenu = new wxMenu;
  131.     helpMenu->Append(wxID_ABOUT, _T("&About...\tCtrl-A"), _T("Show about dialog"));
  132.  
  133.     menuFile->Append(ID_MENU_QUIT, _T("E&xit\tAlt-X"), _T("Quit this program"));
  134.  
  135.     wxMenuBar *menuBar = new wxMenuBar();
  136.     menuBar->Append(menuFile, _T("&File"));
  137.     menuBar->Append(helpMenu, _T("&Help"));
  138.  
  139.     SetMenuBar(menuBar);
  140.  
  141. #if wxUSE_STATUSBAR
  142.     // create a status bar just for fun (by default with 1 pane only)
  143.     CreateStatusBar(2);
  144.     SetStatusText(_T("Welcome to wxWindows erase sample!"));
  145. #endif // wxUSE_STATUSBAR
  146.  
  147.     (void)new MyCanvas( this );
  148. }
  149.  
  150.  
  151. void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  152. {
  153.     Close(TRUE);
  154. }
  155.  
  156. void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
  157. {
  158.     wxMessageBox(_T("This sample shows how you can draw custom background."),
  159.                  _T("About Erase Sample"), wxOK | wxICON_INFORMATION, this);
  160. }
  161.  
  162.  
  163. BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
  164.     EVT_PAINT(  MyCanvas::OnPaint)
  165.     EVT_CHAR(  MyCanvas::OnChar)
  166.     EVT_ERASE_BACKGROUND(  MyCanvas::OnEraseBackground)
  167. END_EVENT_TABLE()
  168.  
  169. MyCanvas::MyCanvas( MyFrame *parent )
  170.         : wxScrolledWindow( parent, -1, wxDefaultPosition, wxDefaultSize,
  171.                             wxScrolledWindowStyle |
  172.                             wxNO_FULL_REPAINT_ON_RESIZE|
  173.                             wxSUNKEN_BORDER )
  174. {
  175.     SetScrollbars( 10, 10, 40, 100, 0, 0 );
  176.  
  177.     m_bitmap = wxBitmap( wxICON(mondrian) );
  178.  
  179.     new wxStaticBitmap( this, -1, m_bitmap, wxPoint(80,20) );
  180. }
  181.  
  182. void MyCanvas::OnChar( wxKeyEvent &event )
  183. {
  184. #if wxUSE_UNICODE
  185.     if (event.m_uniChar)
  186.     {
  187.         m_text += event.m_uniChar;
  188.         Refresh();
  189.         return;
  190.     }
  191. #endif
  192.  
  193.     // some test cases
  194.     switch (event.m_keyCode)
  195.     {
  196.         case WXK_UP: m_text += wxT( "<UP>" ); break;
  197.         case WXK_LEFT: m_text += wxT( "<LEFT>" ); break;
  198.         case WXK_RIGHT: m_text += wxT( "<RIGHT>" ); break;
  199.         case WXK_DOWN: m_text += wxT( "<DOWN>" ); break;
  200.         case WXK_RETURN: m_text += wxT( "<ENTER>" ); break;
  201.         default: m_text += event.m_keyCode; break;
  202.     }
  203.     
  204. }
  205.  
  206. void MyCanvas::OnPaint( wxPaintEvent &event )
  207. {
  208.     wxPaintDC dc(this);
  209.     PrepareDC( dc );
  210.  
  211.     dc.SetBrush( *wxBLACK_BRUSH );
  212.     dc.DrawRectangle( 0,0,200,50 );
  213.  
  214.     dc.DrawBitmap( m_bitmap, 10, 20, TRUE );
  215.  
  216.     dc.SetTextForeground(*wxBLUE);
  217.     dc.DrawText(_T("This text is drawn from OnPaint"), 65, 65);
  218.     
  219.     wxString tmp;
  220.     tmp.Printf( _T("Hit any key to display more text: %s"), m_text.c_str() );
  221.     int w,h;
  222.     dc.GetTextExtent( tmp, &w, &h );
  223.     dc.SetBrush( *wxWHITE_BRUSH );
  224.     dc.DrawRectangle( 65, 85, w, h );
  225.     dc.DrawText( tmp, 65, 85 );
  226.  
  227. #if 0
  228.     wxRegionIterator upd( GetUpdateRegion() );
  229.     while (upd)
  230.     {
  231.         wxLogDebug( _T("Paint: %d %d %d %d"), upd.GetX(), upd.GetY(), upd.GetWidth(), upd.GetHeight() );
  232.         upd ++;
  233.     }
  234. #endif
  235.  
  236. #if 0
  237.     wxSize size = GetSize();
  238.     wxSize client_size = GetClientSize();
  239.     wxLogDebug( _T("size %d %d client_size %d %d"), size.x, size.y, client_size.x, client_size.y );
  240. #endif
  241.  
  242. #if 0
  243.     int i;
  244.     dc.SetPen( *wxWHITE_PEN );
  245.     for (i = 0; i < 20; i += 2)
  246.        dc.DrawLine( i,i, i+100,i );
  247.  
  248.     dc.SetPen( *wxWHITE_PEN );
  249.     for (i = 200; i < 220; i += 2)
  250.        dc.DrawLine( i-200,i, i-100,i );
  251.  
  252.     wxRegion region( 110, 110, 80, 80 );
  253.     wxRegion hole( 130, 130, 40, 1 );
  254.     region.Intersect( hole );
  255.     dc.SetClippingRegion( region );
  256.  
  257.     dc.SetBrush( *wxRED_BRUSH );
  258.     dc.DrawRectangle( 100, 100, 200, 200 );
  259.  
  260.     dc.DestroyClippingRegion();
  261.  
  262.     dc.SetPen( *wxTRANSPARENT_PEN );
  263.  
  264.     wxRegion strip( 110, 200, 30, 1 );
  265.     wxRegionIterator it( strip );
  266.     while (it)
  267.     {
  268.         dc.DrawRectangle( it.GetX(), it.GetY(), it.GetWidth(), it.GetHeight() );
  269.         it ++;
  270.     }
  271. #endif // 0
  272. }
  273.  
  274. void MyCanvas::OnEraseBackground( wxEraseEvent& event )
  275. {
  276.     wxDC& dc = *event.GetDC();
  277.     dc.SetPen(*wxGREEN_PEN);
  278.  
  279.     // this line is needed, otherwise the junk is left on win the background
  280.     dc.Clear();
  281.  
  282.     wxSize size = GetClientSize();
  283.     for ( int x = 0; x < size.x; x+= 10 )
  284.     {
  285.         dc.DrawLine(x, 0, x, size.y);
  286.     }
  287.  
  288.     for ( int y = 0; y < size.y; y+= 10 )
  289.     {
  290.         dc.DrawLine(0, y, size.x, y);
  291.     }
  292.  
  293.     dc.SetTextForeground(*wxRED);
  294.     dc.DrawText(_T("This text is drawn from OnEraseBackground"), 60, 60);
  295. }
  296.  
  297.