]> git.saurik.com Git - wxWidgets.git/blob - samples/rotate/rotate.cpp
M_PI is not ANSI
[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
25 /* GRG: This is not ANSI standard, define M_PI explicitly
26 #include <math.h> // M_PI
27 */
28
29 #ifndef M_PI
30 #define M_PI 3.1415926535897932384626433832795
31 #endif
32
33
34 class MyApp: public wxApp
35 {
36 public:
37 virtual bool OnInit();
38
39 const wxImage& GetImage() const { return m_image; }
40
41 private:
42 wxImage m_image;
43 };
44
45
46 class MyFrame: public wxFrame
47 {
48 public:
49 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
50
51 void OnQuit (wxCommandEvent &);
52 void OnAngle(wxCommandEvent &);
53 void OnMouseLeftUp (wxMouseEvent & event);
54 void OnMouseRightUp (wxMouseEvent & event);
55
56 private:
57 double m_angle;
58
59 DECLARE_EVENT_TABLE()
60 };
61
62 enum
63 {
64 ID_Quit = 1,
65 ID_Angle
66 };
67
68 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
69 EVT_MENU (ID_Quit, MyFrame::OnQuit)
70 EVT_MENU (ID_Angle, MyFrame::OnAngle)
71 EVT_LEFT_UP (MyFrame::OnMouseLeftUp)
72 EVT_RIGHT_UP (MyFrame::OnMouseRightUp)
73 END_EVENT_TABLE()
74
75 IMPLEMENT_APP(MyApp)
76
77
78 bool MyApp::OnInit()
79 {
80 m_image = wxImage("kclub.bmp", wxBITMAP_TYPE_BMP);
81 if ( !m_image.Ok() )
82 {
83 wxLogError("Can't load the test image, please copy it to the "
84 "program directory");
85 return FALSE;
86 }
87
88 MyFrame *frame = new MyFrame ("wxWindows rotate sample",
89 wxPoint(20,20), wxSize(600,450));
90
91 frame->SetBackgroundColour (wxColour (0,80,60));
92
93 frame->Show (TRUE);
94 SetTopWindow (frame);
95 return TRUE;
96 }
97
98 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
99 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
100 {
101 m_angle = 0.1;
102
103 wxMenu *menuFile = new wxMenu;
104 menuFile->Append (ID_Angle, "Set &angle\tCtrl-A");
105 menuFile->AppendSeparator();
106 menuFile->Append (ID_Quit, "E&xit\tAlt-X");
107
108 wxMenuBar *menuBar = new wxMenuBar;
109 menuBar->Append (menuFile, "&File");
110
111 SetMenuBar (menuBar);
112 }
113
114 void MyFrame::OnAngle (wxCommandEvent &)
115 {
116 long degrees = (long)((180*m_angle)/M_PI);
117 degrees = wxGetNumberFromUser("Change the image rotation angle",
118 "Angle in degrees:",
119 "wxWindows rotate sample",
120 degrees,
121 -180, +180,
122 this);
123 m_angle = (degrees * M_PI) / 180.0;
124 }
125
126 void MyFrame::OnQuit (wxCommandEvent &)
127 {
128 Close (TRUE);
129 }
130
131
132 // Rotate with interpolation and with offset correction
133 void MyFrame::OnMouseLeftUp (wxMouseEvent & event)
134 {
135 wxPoint offset;
136 const wxImage& img = wxGetApp().GetImage();
137 wxImage img2 = img.Rotate(m_angle, wxPoint(img.GetWidth()/2, img.GetHeight()/2), TRUE, &offset);
138
139 wxBitmap bmp = img2.ConvertToBitmap ();
140
141 wxClientDC dc (this);
142 dc.DrawBitmap (img2.ConvertToBitmap(), event.m_x + offset.x, event.m_y + offset.y);
143 }
144
145 // without interpolation, and without offset correction
146 void MyFrame::OnMouseRightUp (wxMouseEvent & event)
147 {
148 const wxImage& img = wxGetApp().GetImage();
149 wxImage img2 = img.Rotate(m_angle, wxPoint(img.GetWidth()/2, img.GetHeight()/2), FALSE);
150
151 wxBitmap bmp = img2.ConvertToBitmap ();
152
153 wxClientDC dc (this);
154 dc.DrawBitmap (bmp, event.m_x, event.m_y);
155 }