]>
git.saurik.com Git - wxWidgets.git/blob - samples/rotate/rotate.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Image rotation test
4 // Author: Carlos Moreno
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
24 #include "wx/numdlg.h"
26 /* GRG: This is not ANSI standard, define M_PI explicitly
27 #include <math.h> // M_PI
31 #define M_PI 3.1415926535897932384626433832795
35 class MyApp
: public wxApp
38 virtual bool OnInit();
40 const wxImage
& GetImage() const { return m_image
; }
47 class MyCanvas
: public wxScrolledWindow
50 MyCanvas(wxWindow
* parent
);
52 void OnMouseLeftUp (wxMouseEvent
& event
);
53 void OnMouseRightUp (wxMouseEvent
& event
);
60 class MyFrame
: public wxFrame
63 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
65 void OnQuit (wxCommandEvent
&);
66 void OnAngle(wxCommandEvent
&);
79 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
80 EVT_LEFT_UP (MyCanvas::OnMouseLeftUp
)
81 EVT_RIGHT_UP (MyCanvas::OnMouseRightUp
)
84 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
85 EVT_MENU (ID_Quit
, MyFrame::OnQuit
)
86 EVT_MENU (ID_Angle
, MyFrame::OnAngle
)
93 m_image
= wxImage(_T("kclub.bmp"), wxBITMAP_TYPE_BMP
);
95 // any unused colour will do
96 m_image
.SetMaskColour( 0, 255, 255 );
100 wxLogError(wxT("Can't load the test image, please copy it to the ")
101 wxT("program directory"));
105 MyFrame
*frame
= new MyFrame (_T("wxWidgets rotate sample"),
106 wxPoint(20,20), wxSize(600,450));
109 SetTopWindow (frame
);
113 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
114 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
120 wxMenu
*menuFile
= new wxMenu
;
121 menuFile
->Append (ID_Angle
, _T("Set &angle\tCtrl-A"));
122 menuFile
->AppendSeparator();
123 menuFile
->Append (ID_Quit
, _T("E&xit\tAlt-X"));
125 wxMenuBar
*menuBar
= new wxMenuBar
;
126 menuBar
->Append (menuFile
, _T("&File"));
128 SetMenuBar (menuBar
);
131 void MyFrame::OnAngle (wxCommandEvent
&)
133 long degrees
= (long)((180*m_angle
)/M_PI
);
134 degrees
= wxGetNumberFromUser(_T("Change the image rotation angle"),
135 _T("Angle in degrees:"),
136 _T("wxWidgets rotate sample"),
141 m_angle
= (degrees
* M_PI
) / 180.0;
144 void MyFrame::OnQuit (wxCommandEvent
&)
149 MyCanvas::MyCanvas(wxWindow
* parent
):
150 wxScrolledWindow(parent
, -1)
152 SetBackgroundColour (wxColour (0,80,60));
156 // Rotate with interpolation and with offset correction
157 void MyCanvas::OnMouseLeftUp (wxMouseEvent
& event
)
159 MyFrame
* frame
= (MyFrame
*) GetParent();
162 const wxImage
& img
= wxGetApp().GetImage();
163 wxImage img2
= img
.Rotate(frame
->m_angle
, wxPoint(img
.GetWidth()/2, img
.GetHeight()/2), TRUE
, &offset
);
167 wxClientDC
dc (this);
168 dc
.DrawBitmap (bmp
, event
.m_x
+ offset
.x
, event
.m_y
+ offset
.y
, TRUE
);
171 // without interpolation, and without offset correction
172 void MyCanvas::OnMouseRightUp (wxMouseEvent
& event
)
174 MyFrame
* frame
= (MyFrame
*) GetParent();
176 const wxImage
& img
= wxGetApp().GetImage();
177 wxImage img2
= img
.Rotate(frame
->m_angle
, wxPoint(img
.GetWidth()/2, img
.GetHeight()/2), FALSE
);
181 wxClientDC
dc (this);
182 dc
.DrawBitmap (bmp
, event
.m_x
, event
.m_y
, TRUE
);