]> git.saurik.com Git - wxWidgets.git/blob - samples/rotate/rotate.cpp
moved Destroy() to the beginning
[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
82 // any unused colour will do
83 m_image.SetMaskColour( 0, 255, 255 );
84
85 if ( !m_image.Ok() )
86 {
87 wxLogError("Can't load the test image, please copy it to the "
88 "program directory");
89 return FALSE;
90 }
91
92 MyFrame *frame = new MyFrame ("wxWindows rotate sample",
93 wxPoint(20,20), wxSize(600,450));
94
95 frame->SetBackgroundColour (wxColour (0,80,60));
96 frame->Clear();
97 frame->Show (TRUE);
98 SetTopWindow (frame);
99 return TRUE;
100 }
101
102 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
103 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
104 {
105 m_angle = 0.1;
106
107 wxMenu *menuFile = new wxMenu;
108 menuFile->Append (ID_Angle, "Set &angle\tCtrl-A");
109 menuFile->AppendSeparator();
110 menuFile->Append (ID_Quit, "E&xit\tAlt-X");
111
112 wxMenuBar *menuBar = new wxMenuBar;
113 menuBar->Append (menuFile, "&File");
114
115 SetMenuBar (menuBar);
116 }
117
118 void MyFrame::OnAngle (wxCommandEvent &)
119 {
120 long degrees = (long)((180*m_angle)/M_PI);
121 degrees = wxGetNumberFromUser("Change the image rotation angle",
122 "Angle in degrees:",
123 "wxWindows rotate sample",
124 degrees,
125 -180, +180,
126 this);
127 m_angle = (degrees * M_PI) / 180.0;
128 }
129
130 void MyFrame::OnQuit (wxCommandEvent &)
131 {
132 Close (TRUE);
133 }
134
135
136 // Rotate with interpolation and with offset correction
137 void MyFrame::OnMouseLeftUp (wxMouseEvent & event)
138 {
139 wxPoint offset;
140 const wxImage& img = wxGetApp().GetImage();
141 wxImage img2 = img.Rotate(m_angle, wxPoint(img.GetWidth()/2, img.GetHeight()/2), TRUE, &offset);
142
143 wxBitmap bmp = img2.ConvertToBitmap ();
144
145 wxClientDC dc (this);
146 dc.DrawBitmap (img2.ConvertToBitmap(), event.m_x + offset.x, event.m_y + offset.y, TRUE);
147 }
148
149 // without interpolation, and without offset correction
150 void MyFrame::OnMouseRightUp (wxMouseEvent & event)
151 {
152 const wxImage& img = wxGetApp().GetImage();
153 wxImage img2 = img.Rotate(m_angle, wxPoint(img.GetWidth()/2, img.GetHeight()/2), FALSE);
154
155 wxBitmap bmp = img2.ConvertToBitmap ();
156
157 wxClientDC dc (this);
158 dc.DrawBitmap (bmp, event.m_x, event.m_y, TRUE);
159 }