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