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