]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/rotate/rotate.cpp
Changed version number
[wxWidgets.git] / samples / rotate / rotate.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: rotate.cpp
3// Purpose: Image rotation test
4// Author: Carlos Moreno
5// Modified by:
6// Created: 6/2/2000
7// RCS-ID: $Id$
8// Copyright: (c) 2000
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24#pragma hdrstop
25#endif
26
27#ifndef WX_PRECOMP
28#include "wx/wx.h"
29#endif
30
31#include "wx/image.h"
32#include "wx/numdlg.h"
33#include "wx/dynarray.h"
34
35// ----------------------------------------------------------------------------
36// application class
37// ----------------------------------------------------------------------------
38
39class MyApp: public wxApp
40{
41public:
42 virtual bool OnInit();
43 const wxImage& GetImage() const { return m_image; }
44
45private:
46 wxImage m_image;
47};
48
49// ----------------------------------------------------------------------------
50// data class for cards that need to be rendered
51// ----------------------------------------------------------------------------
52
53class MyRenderedCard
54{
55public:
56 MyRenderedCard(const wxBitmap& bmp, int x, int y)
57 : m_bmp(bmp), m_x(x), m_y(y) { }
58 wxBitmap m_bmp;
59 int m_x, m_y;
60};
61
62// Declare a wxArray type to hold MyRenderedCards.
63WX_DECLARE_OBJARRAY(MyRenderedCard, ArrayOfCards);
64
65// ----------------------------------------------------------------------------
66// custom canvas control that we can draw on
67// ----------------------------------------------------------------------------
68
69class MyCanvas: public wxScrolledWindow
70{
71public:
72 MyCanvas(wxWindow* parent);
73
74 void ClearCards();
75
76 void OnMouseLeftUp (wxMouseEvent & event);
77 void OnMouseRightUp (wxMouseEvent & event);
78 void OnPaint (wxPaintEvent & event);
79
80private:
81 ArrayOfCards m_cards;
82
83 DECLARE_EVENT_TABLE()
84};
85
86// ----------------------------------------------------------------------------
87// main frame
88// ----------------------------------------------------------------------------
89
90class MyFrame: public wxFrame
91{
92public:
93 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
94
95 void OnQuit (wxCommandEvent &);
96 void OnAngle(wxCommandEvent &);
97 void OnClear(wxCommandEvent &);
98
99 double m_angle;
100
101 DECLARE_EVENT_TABLE()
102
103private:
104 MyCanvas *m_canvas;
105};
106
107// ----------------------------------------------------------------------------
108// menu item identifiers
109// ----------------------------------------------------------------------------
110
111enum
112{
113 ID_Quit = 1,
114 ID_Angle,
115 ID_Clear
116};
117
118// ============================================================================
119// implementation
120// ============================================================================
121
122// ----------------------------------------------------------------------------
123// application class
124// ----------------------------------------------------------------------------
125
126IMPLEMENT_APP(MyApp)
127
128bool MyApp::OnInit()
129{
130 m_image = wxImage(_T("kclub.bmp"), wxBITMAP_TYPE_BMP);
131
132 // any unused colour will do
133 m_image.SetMaskColour( 0, 255, 255 );
134
135 if ( !m_image.Ok() )
136 {
137 wxLogError(wxT("Can't load the test image, please copy it to the ")
138 wxT("program directory"));
139 return false;
140 }
141
142 MyFrame *frame = new MyFrame (_T("wxWidgets rotate sample"),
143 wxPoint(20, 20), wxSize(600, 450));
144
145 frame->Show (true);
146 SetTopWindow (frame);
147 return true;
148}
149
150// ----------------------------------------------------------------------------
151// data class for cards that need to be rendered
152// ----------------------------------------------------------------------------
153
154#include "wx/arrimpl.cpp"
155WX_DEFINE_OBJARRAY(ArrayOfCards);
156
157// ----------------------------------------------------------------------------
158// custom canvas control that we can draw on
159// ----------------------------------------------------------------------------
160
161BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
162 EVT_LEFT_UP (MyCanvas::OnMouseLeftUp)
163 EVT_RIGHT_UP (MyCanvas::OnMouseRightUp)
164 EVT_PAINT (MyCanvas::OnPaint)
165END_EVENT_TABLE()
166
167MyCanvas::MyCanvas(wxWindow* parent):
168 wxScrolledWindow(parent, wxID_ANY)
169{
170 SetBackgroundColour (wxColour (0,80,60));
171 ClearBackground();
172}
173
174void MyCanvas::ClearCards ()
175{
176 m_cards.Clear();
177 Refresh(true);
178}
179
180// Rotate with interpolation and with offset correction
181void MyCanvas::OnMouseLeftUp (wxMouseEvent & event)
182{
183 MyFrame* frame = (MyFrame*) GetParent();
184
185 wxPoint offset;
186 const wxImage& img = wxGetApp().GetImage();
187 wxImage img2 = img.Rotate(frame->m_angle,
188 wxPoint(img.GetWidth() / 2, img.GetHeight() / 2), true, &offset);
189
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));
193 Refresh(false);
194}
195
196// without interpolation, and without offset correction
197void MyCanvas::OnMouseRightUp (wxMouseEvent & event)
198{
199 MyFrame* frame = (MyFrame*) GetParent();
200
201 const wxImage& img = wxGetApp().GetImage();
202 wxImage img2 = img.Rotate(frame->m_angle,
203 wxPoint(img.GetWidth() / 2, img.GetHeight() / 2), false);
204
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));
207 Refresh(false);
208}
209
210void MyCanvas::OnPaint (wxPaintEvent &)
211{
212 size_t numCards = m_cards.GetCount();
213
214 wxPaintDC dc(this);
215 dc.BeginDrawing();
216
217 dc.SetTextForeground(wxColour(255, 255, 255));
218 dc.DrawText(wxT("Click on the canvas to draw a card."), 10, 10);
219
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);
223 }
224
225 dc.EndDrawing();
226}
227
228// ----------------------------------------------------------------------------
229// main frame
230// ----------------------------------------------------------------------------
231
232BEGIN_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)
236END_EVENT_TABLE()
237
238MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
239 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
240{
241 m_angle = 0.1;
242
243 m_canvas = new MyCanvas(this);
244
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"));
250
251 wxMenuBar *menuBar = new wxMenuBar;
252 menuBar->Append (menuFile, _T("&File"));
253
254 SetMenuBar (menuBar);
255}
256
257void MyFrame::OnAngle (wxCommandEvent &)
258{
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"),
263 degrees,
264 -180, +180,
265 this);
266 if ( degrees != -1 )
267 m_angle = (degrees * M_PI) / 180.0;
268}
269
270void MyFrame::OnQuit (wxCommandEvent &)
271{
272 Close (true);
273}
274
275void MyFrame::OnClear (wxCommandEvent &)
276{
277 m_canvas->ClearCards ();
278}