]>
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"
25 /* GRG: This is not ANSI standard, define M_PI explicitly
26 #include <math.h> // M_PI
30 #define M_PI 3.1415926535897932384626433832795
34 class MyApp
: public wxApp
37 virtual bool OnInit();
39 const wxImage
& GetImage() const { return m_image
; }
46 class MyCanvas
: public wxScrolledWindow
49 MyCanvas(wxWindow
* parent
);
51 void OnMouseLeftUp (wxMouseEvent
& event
);
52 void OnMouseRightUp (wxMouseEvent
& event
);
59 class MyFrame
: public wxFrame
62 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
64 void OnQuit (wxCommandEvent
&);
65 void OnAngle(wxCommandEvent
&);
78 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
79 EVT_LEFT_UP (MyCanvas::OnMouseLeftUp
)
80 EVT_RIGHT_UP (MyCanvas::OnMouseRightUp
)
83 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
84 EVT_MENU (ID_Quit
, MyFrame::OnQuit
)
85 EVT_MENU (ID_Angle
, MyFrame::OnAngle
)
92 m_image
= wxImage("kclub.bmp", wxBITMAP_TYPE_BMP
);
94 // any unused colour will do
95 m_image
.SetMaskColour( 0, 255, 255 );
99 wxLogError("Can't load the test image, please copy it to the "
100 "program directory");
104 MyFrame
*frame
= new MyFrame ("wxWindows rotate sample",
105 wxPoint(20,20), wxSize(600,450));
108 SetTopWindow (frame
);
112 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
113 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
119 wxMenu
*menuFile
= new wxMenu
;
120 menuFile
->Append (ID_Angle
, "Set &angle\tCtrl-A");
121 menuFile
->AppendSeparator();
122 menuFile
->Append (ID_Quit
, "E&xit\tAlt-X");
124 wxMenuBar
*menuBar
= new wxMenuBar
;
125 menuBar
->Append (menuFile
, "&File");
127 SetMenuBar (menuBar
);
130 void MyFrame::OnAngle (wxCommandEvent
&)
132 long degrees
= (long)((180*m_angle
)/M_PI
);
133 degrees
= wxGetNumberFromUser("Change the image rotation angle",
135 "wxWindows rotate sample",
140 m_angle
= (degrees
* M_PI
) / 180.0;
143 void MyFrame::OnQuit (wxCommandEvent
&)
148 MyCanvas::MyCanvas(wxWindow
* parent
):
149 wxScrolledWindow(parent
, -1)
151 SetBackgroundColour (wxColour (0,80,60));
155 // Rotate with interpolation and with offset correction
156 void MyCanvas::OnMouseLeftUp (wxMouseEvent
& event
)
158 MyFrame
* frame
= (MyFrame
*) GetParent();
161 const wxImage
& img
= wxGetApp().GetImage();
162 wxImage img2
= img
.Rotate(frame
->m_angle
, wxPoint(img
.GetWidth()/2, img
.GetHeight()/2), TRUE
, &offset
);
164 wxBitmap bmp
= img2
.ConvertToBitmap ();
166 wxClientDC
dc (this);
167 dc
.DrawBitmap (img2
.ConvertToBitmap(), event
.m_x
+ offset
.x
, event
.m_y
+ offset
.y
, TRUE
);
170 // without interpolation, and without offset correction
171 void MyCanvas::OnMouseRightUp (wxMouseEvent
& event
)
173 MyFrame
* frame
= (MyFrame
*) GetParent();
175 const wxImage
& img
= wxGetApp().GetImage();
176 wxImage img2
= img
.Rotate(frame
->m_angle
, wxPoint(img
.GetWidth()/2, img
.GetHeight()/2), FALSE
);
178 wxBitmap bmp
= img2
.ConvertToBitmap ();
180 wxClientDC
dc (this);
181 dc
.DrawBitmap (bmp
, event
.m_x
, event
.m_y
, TRUE
);