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