X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f5ac656c5a220c129347e059e1fc656b00ac55d4..b28b097d5955c5565eafb8c590347d22306aaeb3:/samples/rotate/rotate.cpp diff --git a/samples/rotate/rotate.cpp b/samples/rotate/rotate.cpp index 3319489b51..c7eacc7ef7 100644 --- a/samples/rotate/rotate.cpp +++ b/samples/rotate/rotate.cpp @@ -22,7 +22,14 @@ #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 { @@ -36,6 +43,19 @@ private: }; +class MyCanvas: public wxScrolledWindow +{ +public: + MyCanvas(wxWindow* parent); + + void OnMouseLeftUp (wxMouseEvent & event); + void OnMouseRightUp (wxMouseEvent & event); + +private: + + DECLARE_EVENT_TABLE() +}; + class MyFrame: public wxFrame { public: @@ -43,10 +63,7 @@ public: void OnQuit (wxCommandEvent &); void OnAngle(wxCommandEvent &); - void OnMouseLeftUp (wxMouseEvent & event); - void OnMouseRightUp (wxMouseEvent & event); -private: double m_angle; DECLARE_EVENT_TABLE() @@ -58,31 +75,35 @@ enum 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_MENU (ID_Angle, MyFrame::OnAngle) - EVT_LEFT_UP (MyFrame::OnMouseLeftUp) - EVT_RIGHT_UP (MyFrame::OnMouseRightUp) END_EVENT_TABLE() IMPLEMENT_APP(MyApp) - bool MyApp::OnInit() { - m_image = wxImage("kclub.bmp", wxBITMAP_TYPE_BMP); + m_image = wxImage(_T("kclub.bmp"), wxBITMAP_TYPE_BMP); + + // any unused colour will do + m_image.SetMaskColour( 0, 255, 255 ); + if ( !m_image.Ok() ) { - wxLogError("Can't load the test image, please copy it to the " - "program directory"); + 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", + MyFrame *frame = new MyFrame (_T("wxWindows rotate sample"), wxPoint(20,20), wxSize(600,450)); - frame->SetBackgroundColour (wxColour (0,80,60)); - frame->Show (TRUE); SetTopWindow (frame); return TRUE; @@ -93,13 +114,15 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) { m_angle = 0.1; + new MyCanvas(this); + wxMenu *menuFile = new wxMenu; - menuFile->Append (ID_Angle, "Set &angle\tCtrl-A"); + menuFile->Append (ID_Angle, _T("Set &angle\tCtrl-A")); menuFile->AppendSeparator(); - menuFile->Append (ID_Quit, "E&xit\tAlt-X"); + menuFile->Append (ID_Quit, _T("E&xit\tAlt-X")); wxMenuBar *menuBar = new wxMenuBar; - menuBar->Append (menuFile, "&File"); + menuBar->Append (menuFile, _T("&File")); SetMenuBar (menuBar); } @@ -107,13 +130,14 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) 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 = wxGetNumberFromUser(_T("Change the image rotation angle"), + _T("Angle in degrees:"), + _T("wxWindows rotate sample"), degrees, -180, +180, this); - m_angle = (degrees * M_PI) / 180.0; + if ( degrees != -1 ) + m_angle = (degrees * M_PI) / 180.0; } void MyFrame::OnQuit (wxCommandEvent &) @@ -121,28 +145,38 @@ 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) { + MyFrame* frame = (MyFrame*) GetParent(); + wxPoint offset; const wxImage& img = wxGetApp().GetImage(); - wxImage img2 = img.Rotate(m_angle, wxPoint(img.GetWidth()/2, img.GetHeight()/2), TRUE, &offset); + wxImage img2 = img.Rotate(frame->m_angle, wxPoint(img.GetWidth()/2, img.GetHeight()/2), TRUE, &offset); - wxBitmap bmp = img2.ConvertToBitmap (); + wxBitmap bmp(img2); wxClientDC dc (this); - dc.DrawBitmap (img2.ConvertToBitmap(), event.m_x + offset.x, event.m_y + offset.y); + dc.DrawBitmap (bmp, 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) { + MyFrame* frame = (MyFrame*) GetParent(); + const wxImage& img = wxGetApp().GetImage(); - wxImage img2 = img.Rotate(m_angle, wxPoint(img.GetWidth()/2, img.GetHeight()/2), FALSE); + wxImage img2 = img.Rotate(frame->m_angle, wxPoint(img.GetWidth()/2, img.GetHeight()/2), FALSE); - wxBitmap bmp = img2.ConvertToBitmap (); + wxBitmap bmp(img2); wxClientDC dc (this); - dc.DrawBitmap (bmp, event.m_x, event.m_y); + dc.DrawBitmap (bmp, event.m_x, event.m_y, TRUE); }