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

  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name:      rotate.cpp
  3. // Purpose:   Image rotation test
  4. // Author:    Carlos Moreno
  5. // Modified by:
  6. // Created:   6/2/2000
  7. // RCS-ID:    $Id: rotate.cpp,v 1.8.2.1 2002/12/15 17:25:32 MBN Exp $
  8. // Copyright: (c) 2000
  9. // Licence:   wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11.  
  12. // For compilers that support precompilation, includes "wx.h".
  13. #include "wx/wxprec.h"
  14.  
  15. #ifdef __BORLANDC__
  16. #pragma hdrstop
  17. #endif
  18.  
  19. #ifndef WX_PRECOMP
  20. #include "wx/wx.h"
  21. #endif
  22.  
  23. #include "wx/image.h"
  24.  
  25. /* GRG: This is not ANSI standard, define M_PI explicitly
  26. #include <math.h>       // M_PI
  27. */
  28.  
  29. #ifndef M_PI
  30. #define M_PI 3.1415926535897932384626433832795
  31. #endif
  32.  
  33.  
  34. class MyApp: public wxApp
  35. {
  36. public:
  37.     virtual bool OnInit();
  38.  
  39.     const wxImage& GetImage() const { return m_image; }
  40.  
  41. private:
  42.     wxImage m_image;
  43. };
  44.  
  45.  
  46. class MyCanvas: public wxScrolledWindow
  47. {
  48. public:
  49.     MyCanvas(wxWindow* parent);
  50.  
  51.     void OnMouseLeftUp (wxMouseEvent & event);
  52.     void OnMouseRightUp (wxMouseEvent & event);
  53.  
  54. private:
  55.  
  56.     DECLARE_EVENT_TABLE()
  57. };
  58.  
  59. class MyFrame: public wxFrame
  60. {
  61. public:
  62.     MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
  63.  
  64.     void OnQuit (wxCommandEvent &);
  65.     void OnAngle(wxCommandEvent &);
  66.  
  67.     double  m_angle;
  68.  
  69.     DECLARE_EVENT_TABLE()
  70. };
  71.  
  72. enum
  73. {
  74.     ID_Quit = 1,
  75.     ID_Angle
  76. };
  77.  
  78. BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
  79.     EVT_LEFT_UP (MyCanvas::OnMouseLeftUp)
  80.     EVT_RIGHT_UP (MyCanvas::OnMouseRightUp)
  81. END_EVENT_TABLE()
  82.  
  83. BEGIN_EVENT_TABLE(MyFrame, wxFrame)
  84.     EVT_MENU (ID_Quit, MyFrame::OnQuit)
  85.     EVT_MENU (ID_Angle, MyFrame::OnAngle)
  86. END_EVENT_TABLE()
  87.  
  88. IMPLEMENT_APP(MyApp)
  89.  
  90. bool MyApp::OnInit()
  91. {
  92.     m_image = wxImage(_T("kclub.bmp"), wxBITMAP_TYPE_BMP);
  93.  
  94.     // any unused colour will do
  95.     m_image.SetMaskColour( 0, 255, 255 );
  96.  
  97.     if ( !m_image.Ok() )
  98.     {
  99.         wxLogError(wxT("Can't load the test image, please copy it to the ")
  100.                    wxT("program directory"));
  101.         return FALSE;
  102.     }
  103.  
  104.     MyFrame *frame = new MyFrame (_T("wxWindows rotate sample"),
  105.                                   wxPoint(20,20), wxSize(600,450));
  106.  
  107.     frame->Show (TRUE);
  108.     SetTopWindow (frame);
  109.     return TRUE;
  110. }
  111.  
  112. MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
  113.     : wxFrame((wxFrame *)NULL, -1, title, pos, size)
  114. {
  115.     m_angle = 0.1;
  116.  
  117.     new MyCanvas(this);
  118.  
  119.     wxMenu *menuFile = new wxMenu;
  120.     menuFile->Append (ID_Angle, _T("Set &angle\tCtrl-A"));
  121.     menuFile->AppendSeparator();
  122.     menuFile->Append (ID_Quit, _T("E&xit\tAlt-X"));
  123.  
  124.     wxMenuBar *menuBar = new wxMenuBar;
  125.     menuBar->Append (menuFile, _T("&File"));
  126.  
  127.     SetMenuBar (menuBar);
  128. }
  129.  
  130. void MyFrame::OnAngle (wxCommandEvent &)
  131. {
  132.     long degrees = (long)((180*m_angle)/M_PI);
  133.     degrees = wxGetNumberFromUser(_T("Change the image rotation angle"),
  134.                                   _T("Angle in degrees:"),
  135.                                   _T("wxWindows rotate sample"),
  136.                                   degrees,
  137.                                   -180, +180,
  138.                                   this);
  139.     if ( degrees != -1 )
  140.         m_angle = (degrees * M_PI) / 180.0;
  141. }
  142.  
  143. void MyFrame::OnQuit (wxCommandEvent &)
  144. {
  145.     Close (TRUE);
  146. }
  147.  
  148. MyCanvas::MyCanvas(wxWindow* parent):
  149.   wxScrolledWindow(parent, -1)
  150. {
  151.     SetBackgroundColour (wxColour (0,80,60));
  152.     Clear();
  153. }
  154.  
  155. // Rotate with interpolation and with offset correction
  156. void MyCanvas::OnMouseLeftUp (wxMouseEvent & event)
  157. {
  158.     MyFrame* frame = (MyFrame*) GetParent();
  159.  
  160.     wxPoint offset;
  161.     const wxImage& img = wxGetApp().GetImage();
  162.     wxImage img2 = img.Rotate(frame->m_angle, wxPoint(img.GetWidth()/2, img.GetHeight()/2), TRUE, &offset);
  163.  
  164.     wxBitmap bmp(img2);
  165.  
  166.     wxClientDC dc (this);
  167.     dc.DrawBitmap (bmp, event.m_x + offset.x, event.m_y + offset.y, TRUE);
  168. }
  169.  
  170. // without interpolation, and without offset correction
  171. void MyCanvas::OnMouseRightUp (wxMouseEvent & event)
  172. {
  173.     MyFrame* frame = (MyFrame*) GetParent();
  174.  
  175.     const wxImage& img = wxGetApp().GetImage();
  176.     wxImage img2 = img.Rotate(frame->m_angle, wxPoint(img.GetWidth()/2, img.GetHeight()/2), FALSE);
  177.  
  178.     wxBitmap bmp(img2);
  179.  
  180.     wxClientDC dc (this);
  181.     dc.DrawBitmap (bmp, event.m_x, event.m_y, TRUE);
  182. }
  183.