]> git.saurik.com Git - wxWidgets.git/blame - samples/rotate/rotate.cpp
gravity for splitter window (patch 1046105)
[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 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
35class MyApp: public wxApp
36{
f5ac656c 37public:
7a632f10 38 virtual bool OnInit();
f5ac656c
VZ
39
40 const wxImage& GetImage() const { return m_image; }
41
42private:
43 wxImage m_image;
7a632f10
JS
44};
45
46
f6bcfd97
BP
47class MyCanvas: public wxScrolledWindow
48{
49public:
50 MyCanvas(wxWindow* parent);
51
52 void OnMouseLeftUp (wxMouseEvent & event);
53 void OnMouseRightUp (wxMouseEvent & event);
54
55private:
56
57 DECLARE_EVENT_TABLE()
58};
59
7a632f10
JS
60class MyFrame: public wxFrame
61{
62public:
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
73enum
74{
f5ac656c
VZ
75 ID_Quit = 1,
76 ID_Angle
7a632f10
JS
77};
78
f6bcfd97
BP
79BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
80 EVT_LEFT_UP (MyCanvas::OnMouseLeftUp)
81 EVT_RIGHT_UP (MyCanvas::OnMouseRightUp)
82END_EVENT_TABLE()
83
7a632f10
JS
84BEGIN_EVENT_TABLE(MyFrame, wxFrame)
85 EVT_MENU (ID_Quit, MyFrame::OnQuit)
f5ac656c 86 EVT_MENU (ID_Angle, MyFrame::OnAngle)
7a632f10
JS
87END_EVENT_TABLE()
88
89IMPLEMENT_APP(MyApp)
90
7a632f10
JS
91bool 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"));
b62ca03d 102 return false;
f5ac656c
VZ
103 }
104
be5a51fb 105 MyFrame *frame = new MyFrame (_T("wxWidgets rotate sample"),
f5ac656c 106 wxPoint(20,20), wxSize(600,450));
7a632f10 107
b62ca03d 108 frame->Show (true);
7a632f10 109 SetTopWindow (frame);
b62ca03d 110 return true;
7a632f10
JS
111}
112
113MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
b62ca03d 114 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
7a632f10 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
131void 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:"),
be5a51fb 136 _T("wxWidgets 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
144void MyFrame::OnQuit (wxCommandEvent &)
145{
b62ca03d 146 Close (true);
7a632f10
JS
147}
148
f6bcfd97 149MyCanvas::MyCanvas(wxWindow* parent):
b62ca03d 150 wxScrolledWindow(parent, wxID_ANY)
f6bcfd97
BP
151{
152 SetBackgroundColour (wxColour (0,80,60));
14112c7b 153 ClearBackground();
f6bcfd97 154}
7a632f10
JS
155
156// Rotate with interpolation and with offset correction
f6bcfd97 157void 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();
b62ca03d 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);
b62ca03d 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 172void MyCanvas::OnMouseRightUp (wxMouseEvent & event)
7a632f10 173{
f6bcfd97
BP
174 MyFrame* frame = (MyFrame*) GetParent();
175
f5ac656c 176 const wxImage& img = wxGetApp().GetImage();
b62ca03d 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);
b62ca03d 182 dc.DrawBitmap (bmp, event.m_x, event.m_y, true);
7a632f10 183}