]>
git.saurik.com Git - wxWidgets.git/blob - samples/rotate/rotate.cpp
5927f6e7b2dde1cb793f0912fe63f3d05ea56ae9
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Image rotation test
4 // Author: Carlos Moreno
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
32 #include "wx/numdlg.h"
33 #include "wx/dynarray.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 class MyApp
: public wxApp
42 virtual bool OnInit();
43 const wxImage
& GetImage() const { return m_image
; }
49 // ----------------------------------------------------------------------------
50 // data class for images that need to be rendered
51 // ----------------------------------------------------------------------------
56 MyRenderedImage(const wxBitmap
& bmp
, int x
, int y
)
57 : m_bmp(bmp
), m_x(x
), m_y(y
) { }
62 // Declare a wxArray type to hold MyRenderedImages.
63 WX_DECLARE_OBJARRAY(MyRenderedImage
, ArrayOfImages
);
65 // ----------------------------------------------------------------------------
66 // custom canvas control that we can draw on
67 // ----------------------------------------------------------------------------
69 class MyCanvas
: public wxScrolledWindow
72 MyCanvas(wxWindow
* parent
);
76 void OnMouseLeftUp (wxMouseEvent
& event
);
77 void OnMouseRightUp (wxMouseEvent
& event
);
78 void OnPaint (wxPaintEvent
& event
);
81 ArrayOfImages m_images
;
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 class MyFrame
: public wxFrame
93 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
95 void OnQuit (wxCommandEvent
&);
96 void OnAngle(wxCommandEvent
&);
97 void OnClear(wxCommandEvent
&);
101 DECLARE_EVENT_TABLE()
107 // ----------------------------------------------------------------------------
108 // menu item identifiers
109 // ----------------------------------------------------------------------------
118 // ============================================================================
120 // ============================================================================
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
130 if ( !wxApp::OnInit() )
134 wxImage::AddHandler( new wxPNGHandler
);
137 m_image
= wxImage(_T("duck.png"), wxBITMAP_TYPE_PNG
);
141 wxLogError(wxT("Can't load the test image, please copy it to the ")
142 wxT("program directory"));
146 MyFrame
*frame
= new MyFrame (_T("wxWidgets rotate sample"),
147 wxPoint(20, 20), wxSize(600, 450));
150 SetTopWindow (frame
);
154 // ----------------------------------------------------------------------------
155 // data class for images that need to be rendered
156 // ----------------------------------------------------------------------------
158 #include "wx/arrimpl.cpp"
159 WX_DEFINE_OBJARRAY(ArrayOfImages
)
161 // ----------------------------------------------------------------------------
162 // custom canvas control that we can draw on
163 // ----------------------------------------------------------------------------
165 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
166 EVT_LEFT_UP (MyCanvas::OnMouseLeftUp
)
167 EVT_RIGHT_UP (MyCanvas::OnMouseRightUp
)
168 EVT_PAINT (MyCanvas::OnPaint
)
171 MyCanvas::MyCanvas(wxWindow
* parent
):
172 wxScrolledWindow(parent
, wxID_ANY
)
174 SetBackgroundColour (wxColour (0,80,60));
178 void MyCanvas::ClearImages ()
184 // Rotate with interpolation and with offset correction
185 void MyCanvas::OnMouseLeftUp (wxMouseEvent
& event
)
187 MyFrame
* frame
= (MyFrame
*) GetParent();
190 const wxImage
& img
= wxGetApp().GetImage();
191 wxImage img2
= img
.Rotate(frame
->m_angle
,
192 wxPoint(img
.GetWidth() / 2, img
.GetHeight() / 2), true, &offset
);
194 // Add the images to an array to be drawn later in OnPaint()
195 m_images
.Add(new MyRenderedImage(wxBitmap(img2
),
196 event
.m_x
+ offset
.x
, event
.m_y
+ offset
.y
));
200 // without interpolation, and without offset correction
201 void MyCanvas::OnMouseRightUp (wxMouseEvent
& event
)
203 MyFrame
* frame
= (MyFrame
*) GetParent();
205 const wxImage
& img
= wxGetApp().GetImage();
206 wxImage img2
= img
.Rotate(frame
->m_angle
,
207 wxPoint(img
.GetWidth() / 2, img
.GetHeight() / 2), false);
209 // Add the images to an array to be drawn later in OnPaint()
210 m_images
.Add(new MyRenderedImage(wxBitmap(img2
), event
.m_x
, event
.m_y
));
214 void MyCanvas::OnPaint (wxPaintEvent
&)
216 size_t numImages
= m_images
.GetCount();
220 dc
.SetTextForeground(wxColour(255, 255, 255));
221 dc
.DrawText(wxT("Click on the canvas to draw a duck."), 10, 10);
223 for (size_t i
= 0; i
< numImages
; i
++) {
224 MyRenderedImage
& image
= m_images
.Item(i
);
225 dc
.DrawBitmap(image
.m_bmp
, image
.m_x
, image
.m_y
, true);
229 // ----------------------------------------------------------------------------
231 // ----------------------------------------------------------------------------
233 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
234 EVT_MENU (ID_Quit
, MyFrame::OnQuit
)
235 EVT_MENU (ID_Angle
, MyFrame::OnAngle
)
236 EVT_MENU (ID_Clear
, MyFrame::OnClear
)
239 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
240 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
244 m_canvas
= new MyCanvas(this);
246 wxMenu
*menuFile
= new wxMenu
;
247 menuFile
->Append (ID_Angle
, _T("Set &angle...\tCtrl-A"));
248 menuFile
->Append (ID_Clear
, _T("&Clear all ducks\tCtrl-C"));
249 menuFile
->AppendSeparator();
250 menuFile
->Append (ID_Quit
, _T("E&xit\tAlt-X"));
252 wxMenuBar
*menuBar
= new wxMenuBar
;
253 menuBar
->Append (menuFile
, _T("&File"));
255 SetMenuBar (menuBar
);
258 void MyFrame::OnAngle (wxCommandEvent
&)
260 long degrees
= (long)((180*m_angle
)/M_PI
);
261 degrees
= wxGetNumberFromUser(_T("Change the image rotation angle"),
262 _T("Angle in degrees:"),
263 _T("wxWidgets rotate sample"),
268 m_angle
= (degrees
* M_PI
) / 180.0;
271 void MyFrame::OnQuit (wxCommandEvent
&)
276 void MyFrame::OnClear (wxCommandEvent
&)
278 m_canvas
->ClearImages ();