]> git.saurik.com Git - wxWidgets.git/blob - samples/rotate/rotate.cpp
final sweep over docs - replace & with \&
[wxWidgets.git] / samples / rotate / rotate.cpp
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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #include "wx/image.h"
24 #include "wx/numdlg.h"
25
26 /* GRG: This is not ANSI standard, define M_PI explicitly
27 #include <math.h> // M_PI
28 */
29
30 #ifndef M_PI
31 #define M_PI 3.1415926535897932384626433832795
32 #endif
33
34
35 class MyApp: public wxApp
36 {
37 public:
38 virtual bool OnInit();
39
40 const wxImage& GetImage() const { return m_image; }
41
42 private:
43 wxImage m_image;
44 };
45
46
47 class MyCanvas: public wxScrolledWindow
48 {
49 public:
50 MyCanvas(wxWindow* parent);
51
52 void OnMouseLeftUp (wxMouseEvent & event);
53 void OnMouseRightUp (wxMouseEvent & event);
54
55 private:
56
57 DECLARE_EVENT_TABLE()
58 };
59
60 class MyFrame: public wxFrame
61 {
62 public:
63 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
64
65 void OnQuit (wxCommandEvent &);
66 void OnAngle(wxCommandEvent &);
67
68 double m_angle;
69
70 DECLARE_EVENT_TABLE()
71 };
72
73 enum
74 {
75 ID_Quit = 1,
76 ID_Angle
77 };
78
79 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
80 EVT_LEFT_UP (MyCanvas::OnMouseLeftUp)
81 EVT_RIGHT_UP (MyCanvas::OnMouseRightUp)
82 END_EVENT_TABLE()
83
84 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
85 EVT_MENU (ID_Quit, MyFrame::OnQuit)
86 EVT_MENU (ID_Angle, MyFrame::OnAngle)
87 END_EVENT_TABLE()
88
89 IMPLEMENT_APP(MyApp)
90
91 bool MyApp::OnInit()
92 {
93 m_image = wxImage(_T("kclub.bmp"), wxBITMAP_TYPE_BMP);
94
95 // any unused colour will do
96 m_image.SetMaskColour( 0, 255, 255 );
97
98 if ( !m_image.Ok() )
99 {
100 wxLogError(wxT("Can't load the test image, please copy it to the ")
101 wxT("program directory"));
102 return false;
103 }
104
105 MyFrame *frame = new MyFrame (_T("wxWidgets rotate sample"),
106 wxPoint(20,20), wxSize(600,450));
107
108 frame->Show (true);
109 SetTopWindow (frame);
110 return true;
111 }
112
113 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
114 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
115 {
116 m_angle = 0.1;
117
118 new MyCanvas(this);
119
120 wxMenu *menuFile = new wxMenu;
121 menuFile->Append (ID_Angle, _T("Set &angle\tCtrl-A"));
122 menuFile->AppendSeparator();
123 menuFile->Append (ID_Quit, _T("E&xit\tAlt-X"));
124
125 wxMenuBar *menuBar = new wxMenuBar;
126 menuBar->Append (menuFile, _T("&File"));
127
128 SetMenuBar (menuBar);
129 }
130
131 void MyFrame::OnAngle (wxCommandEvent &)
132 {
133 long degrees = (long)((180*m_angle)/M_PI);
134 degrees = wxGetNumberFromUser(_T("Change the image rotation angle"),
135 _T("Angle in degrees:"),
136 _T("wxWidgets rotate sample"),
137 degrees,
138 -180, +180,
139 this);
140 if ( degrees != -1 )
141 m_angle = (degrees * M_PI) / 180.0;
142 }
143
144 void MyFrame::OnQuit (wxCommandEvent &)
145 {
146 Close (true);
147 }
148
149 MyCanvas::MyCanvas(wxWindow* parent):
150 wxScrolledWindow(parent, wxID_ANY)
151 {
152 SetBackgroundColour (wxColour (0,80,60));
153 ClearBackground();
154 }
155
156 // Rotate with interpolation and with offset correction
157 void MyCanvas::OnMouseLeftUp (wxMouseEvent & event)
158 {
159 MyFrame* frame = (MyFrame*) GetParent();
160
161 wxPoint offset;
162 const wxImage& img = wxGetApp().GetImage();
163 wxImage img2 = img.Rotate(frame->m_angle, wxPoint(img.GetWidth()/2, img.GetHeight()/2), true, &offset);
164
165 wxBitmap bmp(img2);
166
167 wxClientDC dc (this);
168 dc.DrawBitmap (bmp, event.m_x + offset.x, event.m_y + offset.y, true);
169 }
170
171 // without interpolation, and without offset correction
172 void MyCanvas::OnMouseRightUp (wxMouseEvent & event)
173 {
174 MyFrame* frame = (MyFrame*) GetParent();
175
176 const wxImage& img = wxGetApp().GetImage();
177 wxImage img2 = img.Rotate(frame->m_angle, wxPoint(img.GetWidth()/2, img.GetHeight()/2), false);
178
179 wxBitmap bmp(img2);
180
181 wxClientDC dc (this);
182 dc.DrawBitmap (bmp, event.m_x, event.m_y, true);
183 }