2000-02-06 14:51:36 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2000-02-07 16:37:59 +00:00
|
|
|
// Name: rotate.cpp
|
2000-02-06 14:51:36 +00:00
|
|
|
// Purpose: Image rotation test
|
|
|
|
// Author: Carlos Moreno
|
|
|
|
// Modified by:
|
|
|
|
// Created: 6/2/2000
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 2000
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
2000-02-06 17:13:14 +00:00
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
|
|
|
|
2000-02-06 14:51:36 +00:00
|
|
|
#include "wx/image.h"
|
|
|
|
|
2000-02-08 02:09:54 +00:00
|
|
|
/* GRG: This is not ANSI standard, define M_PI explicitly
|
2000-02-07 16:37:59 +00:00
|
|
|
#include <math.h> // M_PI
|
2000-02-08 02:09:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef M_PI
|
|
|
|
#define M_PI 3.1415926535897932384626433832795
|
|
|
|
#endif
|
|
|
|
|
2000-02-07 16:37:59 +00:00
|
|
|
|
2000-02-06 14:51:36 +00:00
|
|
|
class MyApp: public wxApp
|
|
|
|
{
|
2000-02-07 16:37:59 +00:00
|
|
|
public:
|
2000-02-06 14:51:36 +00:00
|
|
|
virtual bool OnInit();
|
2000-02-07 16:37:59 +00:00
|
|
|
|
|
|
|
const wxImage& GetImage() const { return m_image; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxImage m_image;
|
2000-02-06 14:51:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-07-15 19:51:35 +00:00
|
|
|
class MyCanvas: public wxScrolledWindow
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyCanvas(wxWindow* parent);
|
|
|
|
|
|
|
|
void OnMouseLeftUp (wxMouseEvent & event);
|
|
|
|
void OnMouseRightUp (wxMouseEvent & event);
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
DECLARE_EVENT_TABLE()
|
|
|
|
};
|
|
|
|
|
2000-02-06 14:51:36 +00:00
|
|
|
class MyFrame: public wxFrame
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
|
|
|
|
|
|
|
|
void OnQuit (wxCommandEvent &);
|
2000-02-07 16:37:59 +00:00
|
|
|
void OnAngle(wxCommandEvent &);
|
2000-02-06 14:51:36 +00:00
|
|
|
|
2000-02-07 16:37:59 +00:00
|
|
|
double m_angle;
|
|
|
|
|
2000-02-06 14:51:36 +00:00
|
|
|
DECLARE_EVENT_TABLE()
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2000-02-07 16:37:59 +00:00
|
|
|
ID_Quit = 1,
|
|
|
|
ID_Angle
|
2000-02-06 14:51:36 +00:00
|
|
|
};
|
|
|
|
|
2000-07-15 19:51:35 +00:00
|
|
|
BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
|
|
|
|
EVT_LEFT_UP (MyCanvas::OnMouseLeftUp)
|
|
|
|
EVT_RIGHT_UP (MyCanvas::OnMouseRightUp)
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
2000-02-06 14:51:36 +00:00
|
|
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
|
|
|
EVT_MENU (ID_Quit, MyFrame::OnQuit)
|
2000-02-07 16:37:59 +00:00
|
|
|
EVT_MENU (ID_Angle, MyFrame::OnAngle)
|
2000-02-06 14:51:36 +00:00
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
IMPLEMENT_APP(MyApp)
|
|
|
|
|
|
|
|
bool MyApp::OnInit()
|
|
|
|
{
|
2000-02-07 16:37:59 +00:00
|
|
|
m_image = wxImage("kclub.bmp", wxBITMAP_TYPE_BMP);
|
2000-02-10 13:26:30 +00:00
|
|
|
|
|
|
|
// any unused colour will do
|
|
|
|
m_image.SetMaskColour( 0, 255, 255 );
|
|
|
|
|
2000-02-07 16:37:59 +00:00
|
|
|
if ( !m_image.Ok() )
|
|
|
|
{
|
2001-09-21 20:21:44 +00:00
|
|
|
wxLogError(wxT("Can't load the test image, please copy it to the ")
|
|
|
|
wxT("program directory"));
|
2000-02-07 16:37:59 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
MyFrame *frame = new MyFrame ("wxWindows rotate sample",
|
|
|
|
wxPoint(20,20), wxSize(600,450));
|
2000-02-06 14:51:36 +00:00
|
|
|
|
|
|
|
frame->Show (TRUE);
|
|
|
|
SetTopWindow (frame);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
|
|
|
|
: wxFrame((wxFrame *)NULL, -1, title, pos, size)
|
|
|
|
{
|
2000-02-07 16:37:59 +00:00
|
|
|
m_angle = 0.1;
|
|
|
|
|
2000-07-15 19:51:35 +00:00
|
|
|
new MyCanvas(this);
|
|
|
|
|
2000-02-06 14:51:36 +00:00
|
|
|
wxMenu *menuFile = new wxMenu;
|
2000-02-07 16:37:59 +00:00
|
|
|
menuFile->Append (ID_Angle, "Set &angle\tCtrl-A");
|
|
|
|
menuFile->AppendSeparator();
|
|
|
|
menuFile->Append (ID_Quit, "E&xit\tAlt-X");
|
2000-02-06 14:51:36 +00:00
|
|
|
|
|
|
|
wxMenuBar *menuBar = new wxMenuBar;
|
|
|
|
menuBar->Append (menuFile, "&File");
|
|
|
|
|
|
|
|
SetMenuBar (menuBar);
|
|
|
|
}
|
|
|
|
|
2000-02-07 16:37:59 +00:00
|
|
|
void MyFrame::OnAngle (wxCommandEvent &)
|
|
|
|
{
|
|
|
|
long degrees = (long)((180*m_angle)/M_PI);
|
|
|
|
degrees = wxGetNumberFromUser("Change the image rotation angle",
|
|
|
|
"Angle in degrees:",
|
|
|
|
"wxWindows rotate sample",
|
|
|
|
degrees,
|
|
|
|
-180, +180,
|
|
|
|
this);
|
2000-07-15 19:51:35 +00:00
|
|
|
if ( degrees != -1 )
|
|
|
|
m_angle = (degrees * M_PI) / 180.0;
|
2000-02-07 16:37:59 +00:00
|
|
|
}
|
|
|
|
|
2000-02-06 14:51:36 +00:00
|
|
|
void MyFrame::OnQuit (wxCommandEvent &)
|
|
|
|
{
|
|
|
|
Close (TRUE);
|
|
|
|
}
|
|
|
|
|
2000-07-15 19:51:35 +00:00
|
|
|
MyCanvas::MyCanvas(wxWindow* parent):
|
|
|
|
wxScrolledWindow(parent, -1)
|
|
|
|
{
|
|
|
|
SetBackgroundColour (wxColour (0,80,60));
|
|
|
|
Clear();
|
|
|
|
}
|
2000-02-06 14:51:36 +00:00
|
|
|
|
|
|
|
// Rotate with interpolation and with offset correction
|
2000-07-15 19:51:35 +00:00
|
|
|
void MyCanvas::OnMouseLeftUp (wxMouseEvent & event)
|
2000-02-06 14:51:36 +00:00
|
|
|
{
|
2000-07-15 19:51:35 +00:00
|
|
|
MyFrame* frame = (MyFrame*) GetParent();
|
|
|
|
|
2000-02-06 14:51:36 +00:00
|
|
|
wxPoint offset;
|
2000-02-07 16:37:59 +00:00
|
|
|
const wxImage& img = wxGetApp().GetImage();
|
2000-07-15 19:51:35 +00:00
|
|
|
wxImage img2 = img.Rotate(frame->m_angle, wxPoint(img.GetWidth()/2, img.GetHeight()/2), TRUE, &offset);
|
2000-02-06 14:51:36 +00:00
|
|
|
|
|
|
|
wxBitmap bmp = img2.ConvertToBitmap ();
|
|
|
|
|
|
|
|
wxClientDC dc (this);
|
2000-02-10 13:26:30 +00:00
|
|
|
dc.DrawBitmap (img2.ConvertToBitmap(), event.m_x + offset.x, event.m_y + offset.y, TRUE);
|
2000-02-06 14:51:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// without interpolation, and without offset correction
|
2000-07-15 19:51:35 +00:00
|
|
|
void MyCanvas::OnMouseRightUp (wxMouseEvent & event)
|
2000-02-06 14:51:36 +00:00
|
|
|
{
|
2000-07-15 19:51:35 +00:00
|
|
|
MyFrame* frame = (MyFrame*) GetParent();
|
|
|
|
|
2000-02-07 16:37:59 +00:00
|
|
|
const wxImage& img = wxGetApp().GetImage();
|
2000-07-15 19:51:35 +00:00
|
|
|
wxImage img2 = img.Rotate(frame->m_angle, wxPoint(img.GetWidth()/2, img.GetHeight()/2), FALSE);
|
2000-02-06 14:51:36 +00:00
|
|
|
|
|
|
|
wxBitmap bmp = img2.ConvertToBitmap ();
|
|
|
|
|
|
|
|
wxClientDC dc (this);
|
2000-02-10 13:26:30 +00:00
|
|
|
dc.DrawBitmap (bmp, event.m_x, event.m_y, TRUE);
|
2000-02-06 14:51:36 +00:00
|
|
|
}
|