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