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