X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/7a632f1056d7894daca7342394c1db9b61f6aecf..a111f2b216a90bacb254644d3cae526eddc9f9f1:/samples/rotate/rotate.cpp diff --git a/samples/rotate/rotate.cpp b/samples/rotate/rotate.cpp index 74b3f80551..2bdb6270e9 100644 --- a/samples/rotate/rotate.cpp +++ b/samples/rotate/rotate.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: test.cpp +// Name: rotate.cpp // Purpose: Image rotation test // Author: Carlos Moreno // Modified by: @@ -16,46 +16,93 @@ #pragma hdrstop #endif +#ifndef WX_PRECOMP +#include "wx/wx.h" +#endif + #include "wx/image.h" +/* GRG: This is not ANSI standard, define M_PI explicitly +#include // M_PI +*/ + +#ifndef M_PI +#define M_PI 3.1415926535897932384626433832795 +#endif + + class MyApp: public wxApp { +public: virtual bool OnInit(); + + const wxImage& GetImage() const { return m_image; } + +private: + wxImage m_image; }; -class MyFrame: public wxFrame +class MyCanvas: public wxScrolledWindow { public: - MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); + MyCanvas(wxWindow* parent); - void OnQuit (wxCommandEvent &); void OnMouseLeftUp (wxMouseEvent & event); void OnMouseRightUp (wxMouseEvent & event); private: + + DECLARE_EVENT_TABLE() +}; + +class MyFrame: public wxFrame +{ +public: + MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); + + void OnQuit (wxCommandEvent &); + void OnAngle(wxCommandEvent &); + + double m_angle; + DECLARE_EVENT_TABLE() }; enum { - ID_Quit = 1 + ID_Quit = 1, + ID_Angle }; +BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) + EVT_LEFT_UP (MyCanvas::OnMouseLeftUp) + EVT_RIGHT_UP (MyCanvas::OnMouseRightUp) +END_EVENT_TABLE() + BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU (ID_Quit, MyFrame::OnQuit) - EVT_LEFT_UP (MyFrame::OnMouseLeftUp) - EVT_RIGHT_UP (MyFrame::OnMouseRightUp) + EVT_MENU (ID_Angle, MyFrame::OnAngle) END_EVENT_TABLE() IMPLEMENT_APP(MyApp) - bool MyApp::OnInit() { - MyFrame *frame = new MyFrame ("wxWindows Skeleton", wxPoint(20,20), wxSize(600,450)); + m_image = wxImage("kclub.bmp", wxBITMAP_TYPE_BMP); + + // any unused colour will do + m_image.SetMaskColour( 0, 255, 255 ); - frame->SetBackgroundColour (wxColour (0,80,60)); + if ( !m_image.Ok() ) + { + wxLogError(wxT("Can't load the test image, please copy it to the ") + wxT("program directory")); + return FALSE; + } + + MyFrame *frame = new MyFrame ("wxWindows rotate sample", + wxPoint(20,20), wxSize(600,450)); frame->Show (TRUE); SetTopWindow (frame); @@ -65,8 +112,14 @@ bool MyApp::OnInit() MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) : wxFrame((wxFrame *)NULL, -1, title, pos, size) { + m_angle = 0.1; + + new MyCanvas(this); + wxMenu *menuFile = new wxMenu; - menuFile->Append (ID_Quit, "E&xit"); + menuFile->Append (ID_Angle, "Set &angle\tCtrl-A"); + menuFile->AppendSeparator(); + menuFile->Append (ID_Quit, "E&xit\tAlt-X"); wxMenuBar *menuBar = new wxMenuBar; menuBar->Append (menuFile, "&File"); @@ -74,47 +127,56 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) SetMenuBar (menuBar); } +void MyFrame::OnAngle (wxCommandEvent &) +{ + long degrees = (long)((180*m_angle)/M_PI); + degrees = wxGetNumberFromUser("Change the image rotation angle", + "Angle in degrees:", + "wxWindows rotate sample", + degrees, + -180, +180, + this); + if ( degrees != -1 ) + m_angle = (degrees * M_PI) / 180.0; +} + void MyFrame::OnQuit (wxCommandEvent &) { Close (TRUE); } +MyCanvas::MyCanvas(wxWindow* parent): + wxScrolledWindow(parent, -1) +{ + SetBackgroundColour (wxColour (0,80,60)); + Clear(); +} // Rotate with interpolation and with offset correction -void MyFrame::OnMouseLeftUp (wxMouseEvent & event) +void MyCanvas::OnMouseLeftUp (wxMouseEvent & event) { - static double angle = 0.1; - const double pi = 3.14159265359; - - wxImage img ("kclub.bmp", wxBITMAP_TYPE_BMP); + MyFrame* frame = (MyFrame*) GetParent(); wxPoint offset; - wxImage img2 = img.Rotate(angle, wxPoint(img.GetWidth()/2, img.GetHeight()/2), TRUE, &offset); - angle += 0.05; + const wxImage& img = wxGetApp().GetImage(); + wxImage img2 = img.Rotate(frame->m_angle, wxPoint(img.GetWidth()/2, img.GetHeight()/2), TRUE, &offset); wxBitmap bmp = img2.ConvertToBitmap (); wxClientDC dc (this); - dc.DrawBitmap (bmp, event.m_x + offset.x, event.m_y + offset.y); - - return; + dc.DrawBitmap (img2.ConvertToBitmap(), event.m_x + offset.x, event.m_y + offset.y, TRUE); } // without interpolation, and without offset correction -void MyFrame::OnMouseRightUp (wxMouseEvent & event) +void MyCanvas::OnMouseRightUp (wxMouseEvent & event) { - static double angle = 0.1; - const double pi = 3.14159265359; + MyFrame* frame = (MyFrame*) GetParent(); - wxImage img ("kclub.bmp", wxBITMAP_TYPE_BMP); - - wxImage img2 = img.Rotate(angle, wxPoint(img.GetWidth()/2, img.GetHeight()/2), FALSE); - angle += 0.05; + const wxImage& img = wxGetApp().GetImage(); + wxImage img2 = img.Rotate(frame->m_angle, wxPoint(img.GetWidth()/2, img.GetHeight()/2), FALSE); wxBitmap bmp = img2.ConvertToBitmap (); wxClientDC dc (this); - dc.DrawBitmap (bmp, event.m_x, event.m_y); - - return; + dc.DrawBitmap (bmp, event.m_x, event.m_y, TRUE); }