]> git.saurik.com Git - wxWidgets.git/blame - samples/rotate/rotate.cpp
set labels for new controls
[wxWidgets.git] / samples / rotate / rotate.cpp
CommitLineData
7a632f10 1/////////////////////////////////////////////////////////////////////////////
f5ac656c 2// Name: rotate.cpp
7a632f10
JS
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
31f1a50e
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
7a632f10
JS
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24#pragma hdrstop
25#endif
26
46132182
RR
27#ifndef WX_PRECOMP
28#include "wx/wx.h"
29#endif
30
7a632f10 31#include "wx/image.h"
e4f3eb42 32#include "wx/numdlg.h"
31f1a50e
VZ
33#include "wx/dynarray.h"
34
35// ----------------------------------------------------------------------------
36// application class
37// ----------------------------------------------------------------------------
7a632f10
JS
38
39class MyApp: public wxApp
40{
f5ac656c 41public:
7a632f10 42 virtual bool OnInit();
f5ac656c
VZ
43 const wxImage& GetImage() const { return m_image; }
44
45private:
46 wxImage m_image;
7a632f10
JS
47};
48
31f1a50e 49// ----------------------------------------------------------------------------
4fea5f26 50// data class for images that need to be rendered
31f1a50e
VZ
51// ----------------------------------------------------------------------------
52
4fea5f26 53class MyRenderedImage
31f1a50e
VZ
54{
55public:
4fea5f26 56 MyRenderedImage(const wxBitmap& bmp, int x, int y)
31f1a50e
VZ
57 : m_bmp(bmp), m_x(x), m_y(y) { }
58 wxBitmap m_bmp;
59 int m_x, m_y;
60};
61
4fea5f26
JS
62// Declare a wxArray type to hold MyRenderedImages.
63WX_DECLARE_OBJARRAY(MyRenderedImage, ArrayOfImages);
31f1a50e
VZ
64
65// ----------------------------------------------------------------------------
66// custom canvas control that we can draw on
67// ----------------------------------------------------------------------------
7a632f10 68
f6bcfd97
BP
69class MyCanvas: public wxScrolledWindow
70{
71public:
72 MyCanvas(wxWindow* parent);
73
4fea5f26 74 void ClearImages();
31f1a50e 75
f6bcfd97
BP
76 void OnMouseLeftUp (wxMouseEvent & event);
77 void OnMouseRightUp (wxMouseEvent & event);
31f1a50e 78 void OnPaint (wxPaintEvent & event);
f6bcfd97
BP
79
80private:
4fea5f26 81 ArrayOfImages m_images;
f6bcfd97
BP
82
83 DECLARE_EVENT_TABLE()
84};
85
31f1a50e
VZ
86// ----------------------------------------------------------------------------
87// main frame
88// ----------------------------------------------------------------------------
89
7a632f10
JS
90class MyFrame: public wxFrame
91{
92public:
93 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
94
95 void OnQuit (wxCommandEvent &);
f5ac656c 96 void OnAngle(wxCommandEvent &);
31f1a50e 97 void OnClear(wxCommandEvent &);
7a632f10 98
f5ac656c
VZ
99 double m_angle;
100
7a632f10 101 DECLARE_EVENT_TABLE()
31f1a50e
VZ
102
103private:
104 MyCanvas *m_canvas;
7a632f10
JS
105};
106
31f1a50e
VZ
107// ----------------------------------------------------------------------------
108// menu item identifiers
109// ----------------------------------------------------------------------------
110
7a632f10
JS
111enum
112{
f5ac656c 113 ID_Quit = 1,
31f1a50e
VZ
114 ID_Angle,
115 ID_Clear
7a632f10
JS
116};
117
31f1a50e
VZ
118// ============================================================================
119// implementation
120// ============================================================================
f6bcfd97 121
31f1a50e
VZ
122// ----------------------------------------------------------------------------
123// application class
124// ----------------------------------------------------------------------------
7a632f10
JS
125
126IMPLEMENT_APP(MyApp)
127
7a632f10
JS
128bool MyApp::OnInit()
129{
45e6e6f8
VZ
130 if ( !wxApp::OnInit() )
131 return false;
132
4fea5f26
JS
133#if wxUSE_LIBPNG
134 wxImage::AddHandler( new wxPNGHandler );
135#endif
b2177839 136
4fea5f26 137 m_image = wxImage(_T("duck.png"), wxBITMAP_TYPE_PNG);
b2177839 138
f5ac656c
VZ
139 if ( !m_image.Ok() )
140 {
4693b20c
MB
141 wxLogError(wxT("Can't load the test image, please copy it to the ")
142 wxT("program directory"));
b62ca03d 143 return false;
f5ac656c
VZ
144 }
145
be5a51fb 146 MyFrame *frame = new MyFrame (_T("wxWidgets rotate sample"),
31f1a50e 147 wxPoint(20, 20), wxSize(600, 450));
7a632f10 148
b62ca03d 149 frame->Show (true);
7a632f10 150 SetTopWindow (frame);
b62ca03d 151 return true;
7a632f10
JS
152}
153
31f1a50e 154// ----------------------------------------------------------------------------
4fea5f26 155// data class for images that need to be rendered
31f1a50e
VZ
156// ----------------------------------------------------------------------------
157
158#include "wx/arrimpl.cpp"
004f4002 159WX_DEFINE_OBJARRAY(ArrayOfImages)
31f1a50e
VZ
160
161// ----------------------------------------------------------------------------
162// custom canvas control that we can draw on
163// ----------------------------------------------------------------------------
164
165BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
166 EVT_LEFT_UP (MyCanvas::OnMouseLeftUp)
167 EVT_RIGHT_UP (MyCanvas::OnMouseRightUp)
168 EVT_PAINT (MyCanvas::OnPaint)
169END_EVENT_TABLE()
170
171MyCanvas::MyCanvas(wxWindow* parent):
172 wxScrolledWindow(parent, wxID_ANY)
173{
174 SetBackgroundColour (wxColour (0,80,60));
175 ClearBackground();
176}
177
4fea5f26 178void MyCanvas::ClearImages ()
31f1a50e 179{
4fea5f26 180 m_images.Clear();
31f1a50e
VZ
181 Refresh(true);
182}
183
184// Rotate with interpolation and with offset correction
185void MyCanvas::OnMouseLeftUp (wxMouseEvent & event)
186{
187 MyFrame* frame = (MyFrame*) GetParent();
188
189 wxPoint offset;
190 const wxImage& img = wxGetApp().GetImage();
191 wxImage img2 = img.Rotate(frame->m_angle,
192 wxPoint(img.GetWidth() / 2, img.GetHeight() / 2), true, &offset);
193
4fea5f26
JS
194 // Add the images to an array to be drawn later in OnPaint()
195 m_images.Add(new MyRenderedImage(wxBitmap(img2),
31f1a50e
VZ
196 event.m_x + offset.x, event.m_y + offset.y));
197 Refresh(false);
198}
199
200// without interpolation, and without offset correction
201void MyCanvas::OnMouseRightUp (wxMouseEvent & event)
202{
203 MyFrame* frame = (MyFrame*) GetParent();
204
205 const wxImage& img = wxGetApp().GetImage();
206 wxImage img2 = img.Rotate(frame->m_angle,
207 wxPoint(img.GetWidth() / 2, img.GetHeight() / 2), false);
208
4fea5f26
JS
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));
31f1a50e
VZ
211 Refresh(false);
212}
213
214void MyCanvas::OnPaint (wxPaintEvent &)
215{
4fea5f26 216 size_t numImages = m_images.GetCount();
31f1a50e
VZ
217
218 wxPaintDC dc(this);
31f1a50e
VZ
219
220 dc.SetTextForeground(wxColour(255, 255, 255));
4fea5f26 221 dc.DrawText(wxT("Click on the canvas to draw a duck."), 10, 10);
31f1a50e 222
4fea5f26
JS
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);
31f1a50e 226 }
31f1a50e
VZ
227}
228
229// ----------------------------------------------------------------------------
230// main frame
231// ----------------------------------------------------------------------------
232
233BEGIN_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)
237END_EVENT_TABLE()
238
7a632f10 239MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
b62ca03d 240 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
7a632f10 241{
f5ac656c
VZ
242 m_angle = 0.1;
243
31f1a50e 244 m_canvas = new MyCanvas(this);
f6bcfd97 245
7a632f10 246 wxMenu *menuFile = new wxMenu;
31f1a50e 247 menuFile->Append (ID_Angle, _T("Set &angle...\tCtrl-A"));
4fea5f26 248 menuFile->Append (ID_Clear, _T("&Clear all ducks\tCtrl-C"));
f5ac656c 249 menuFile->AppendSeparator();
600683ca 250 menuFile->Append (ID_Quit, _T("E&xit\tAlt-X"));
7a632f10
JS
251
252 wxMenuBar *menuBar = new wxMenuBar;
600683ca 253 menuBar->Append (menuFile, _T("&File"));
7a632f10
JS
254
255 SetMenuBar (menuBar);
256}
257
f5ac656c
VZ
258void MyFrame::OnAngle (wxCommandEvent &)
259{
260 long degrees = (long)((180*m_angle)/M_PI);
600683ca
MB
261 degrees = wxGetNumberFromUser(_T("Change the image rotation angle"),
262 _T("Angle in degrees:"),
be5a51fb 263 _T("wxWidgets rotate sample"),
f5ac656c
VZ
264 degrees,
265 -180, +180,
266 this);
f6bcfd97
BP
267 if ( degrees != -1 )
268 m_angle = (degrees * M_PI) / 180.0;
f5ac656c
VZ
269}
270
7a632f10
JS
271void MyFrame::OnQuit (wxCommandEvent &)
272{
b62ca03d 273 Close (true);
7a632f10
JS
274}
275
31f1a50e 276void MyFrame::OnClear (wxCommandEvent &)
7a632f10 277{
4fea5f26 278 m_canvas->ClearImages ();
7a632f10 279}
4fea5f26 280