]>
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 // ============================================================================
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 cards that need to be rendered
51 // ----------------------------------------------------------------------------
56 MyRenderedCard(const wxBitmap
& bmp
, int x
, int y
)
57 : m_bmp(bmp
), m_x(x
), m_y(y
) { }
62 // Declare a wxArray type to hold MyRenderedCards.
63 WX_DECLARE_OBJARRAY(MyRenderedCard
, ArrayOfCards
);
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
);
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 m_image
= wxImage(_T("kclub.bmp"), wxBITMAP_TYPE_BMP
);
132 // any unused colour will do
133 m_image
.SetMaskColour( 0, 255, 255 );
137 wxLogError(wxT("Can't load the test image, please copy it to the ")
138 wxT("program directory"));
142 MyFrame
*frame
= new MyFrame (_T("wxWidgets rotate sample"),
143 wxPoint(20, 20), wxSize(600, 450));
146 SetTopWindow (frame
);
150 // ----------------------------------------------------------------------------
151 // data class for cards that need to be rendered
152 // ----------------------------------------------------------------------------
154 #include "wx/arrimpl.cpp"
155 WX_DEFINE_OBJARRAY(ArrayOfCards
);
157 // ----------------------------------------------------------------------------
158 // custom canvas control that we can draw on
159 // ----------------------------------------------------------------------------
161 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
162 EVT_LEFT_UP (MyCanvas::OnMouseLeftUp
)
163 EVT_RIGHT_UP (MyCanvas::OnMouseRightUp
)
164 EVT_PAINT (MyCanvas::OnPaint
)
167 MyCanvas::MyCanvas(wxWindow
* parent
):
168 wxScrolledWindow(parent
, wxID_ANY
)
170 SetBackgroundColour (wxColour (0,80,60));
174 void MyCanvas::ClearCards ()
180 // Rotate with interpolation and with offset correction
181 void MyCanvas::OnMouseLeftUp (wxMouseEvent
& event
)
183 MyFrame
* frame
= (MyFrame
*) GetParent();
186 const wxImage
& img
= wxGetApp().GetImage();
187 wxImage img2
= img
.Rotate(frame
->m_angle
,
188 wxPoint(img
.GetWidth() / 2, img
.GetHeight() / 2), true, &offset
);
190 // Add the cards to an array to be drawn later in OnPaint()
191 m_cards
.Add(new MyRenderedCard(wxBitmap(img2
),
192 event
.m_x
+ offset
.x
, event
.m_y
+ offset
.y
));
196 // without interpolation, and without offset correction
197 void MyCanvas::OnMouseRightUp (wxMouseEvent
& event
)
199 MyFrame
* frame
= (MyFrame
*) GetParent();
201 const wxImage
& img
= wxGetApp().GetImage();
202 wxImage img2
= img
.Rotate(frame
->m_angle
,
203 wxPoint(img
.GetWidth() / 2, img
.GetHeight() / 2), false);
205 // Add the cards to an array to be drawn later in OnPaint()
206 m_cards
.Add(new MyRenderedCard(wxBitmap(img2
), event
.m_x
, event
.m_y
));
210 void MyCanvas::OnPaint (wxPaintEvent
&)
212 size_t numCards
= m_cards
.GetCount();
217 dc
.SetTextForeground(wxColour(255, 255, 255));
218 dc
.DrawText(wxT("Click on the canvas to draw a card."), 10, 10);
220 for (size_t i
= 0; i
< numCards
; i
++) {
221 MyRenderedCard
& card
= m_cards
.Item(i
);
222 dc
.DrawBitmap(card
.m_bmp
, card
.m_x
, card
.m_y
, true);
228 // ----------------------------------------------------------------------------
230 // ----------------------------------------------------------------------------
232 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
233 EVT_MENU (ID_Quit
, MyFrame::OnQuit
)
234 EVT_MENU (ID_Angle
, MyFrame::OnAngle
)
235 EVT_MENU (ID_Clear
, MyFrame::OnClear
)
238 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
239 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
243 m_canvas
= new MyCanvas(this);
245 wxMenu
*menuFile
= new wxMenu
;
246 menuFile
->Append (ID_Angle
, _T("Set &angle...\tCtrl-A"));
247 menuFile
->Append (ID_Clear
, _T("&Clear all cards\tCtrl-C"));
248 menuFile
->AppendSeparator();
249 menuFile
->Append (ID_Quit
, _T("E&xit\tAlt-X"));
251 wxMenuBar
*menuBar
= new wxMenuBar
;
252 menuBar
->Append (menuFile
, _T("&File"));
254 SetMenuBar (menuBar
);
257 void MyFrame::OnAngle (wxCommandEvent
&)
259 long degrees
= (long)((180*m_angle
)/M_PI
);
260 degrees
= wxGetNumberFromUser(_T("Change the image rotation angle"),
261 _T("Angle in degrees:"),
262 _T("wxWidgets rotate sample"),
267 m_angle
= (degrees
* M_PI
) / 180.0;
270 void MyFrame::OnQuit (wxCommandEvent
&)
275 void MyFrame::OnClear (wxCommandEvent
&)
277 m_canvas
->ClearCards ();