J’ai un peu honte de montrer ce code, car il est tout crade, mais le but était de faire un truc qui marche et de le faire vite.
[codebox] // For compilers that support precompilation, includes « wx/wx.h ».
#include « wx/wxprec.h »
#ifndef WX_PRECOMP
#include « wx/wx.h »
#endif
#include « wx/image.h »
#include « wx/file.h »
#include « wx/filename.h »
#include « wx/protocol/ftp.h »
#include <Magick++.h>
using namespace Magick;
#include « exivsimple.h »
#include « ftp.h »
// derived classes
class cGUApp: public wxApp
{
virtual bool OnInit();
};
class cGUPanelImage: public wxPanel
{
public:
void LoadImage( wxString sFilePath );
void OnPaint (wxPaintEvent & event);
wxImage m_Image;
wxSize m_Size;
bool m_bHori;
DECLARE_EVENT_TABLE()
};
class cGUDialog: public wxDialog
{
public:
cGUDialog(wxFrame parent, wxWindowID id, const wxString &title, const wxString &sFilePath );
void OnSize( wxSizeEvent & event );
void Manage();
private:
wxBoxSizer m_pTopSizer;
wxBoxSizer* m_pLogPwdSizer;
wxBoxSizer* m_pWidthHeightSizer;
wxBoxSizer* m_pButtonSizer;
cGUPanelImage m_PanelImage;
wxTextCtrl m_FTP;
wxTextCtrl m_Directory;
wxTextCtrl m_Login;
wxTextCtrl m_Password;
wxTextCtrl m_HeadLine;
wxTextCtrl m_Caption;
wxTextCtrl m_Width;
wxTextCtrl m_Height;
wxString m_sHeadLine;
wxString m_sCaption;
wxString m_sFilePath;
wxString m_sSmallFilePath;
wxString m_sSmallFileName;
ftp m_FtpConnection;
DECLARE_EVENT_TABLE()
};
BEGIN_EVENT_TABLE(cGUDialog, wxDialog)
EVT_SIZE (cGUDialog::OnSize)
END_EVENT_TABLE()
BEGIN_EVENT_TABLE(cGUPanelImage, wxPanel)
EVT_PAINT(cGUPanelImage::OnPaint)
END_EVENT_TABLE()
IMPLEMENT_APP(cGUApp)
bool cGUApp::OnInit()
{
InitializeMagick(NULL);
wxInitAllImageHandlers();
wxString sFilePath;
if ( argc == 2 )
{
sFilePath = argv[1];
cGUDialog dlg(NULL,-1,« mGallery », sFilePath );
int x = dlg.ShowModal();
if ( x == 5100 )
{
dlg.Manage();
}
else
{
}
}
return false;
}
cGUDialog::cGUDialog(wxFrame *parent, wxWindowID id, const wxString &title, const wxString& filePath )
: wxDialog(parent, id, title, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
{
m_pTopSizer = new wxBoxSizer( wxVERTICAL );
m_pLogPwdSizer = new wxBoxSizer( wxHORIZONTAL );
m_pWidthHeightSizer = new wxBoxSizer( wxHORIZONTAL );
m_pButtonSizer = new wxBoxSizer( wxHORIZONTAL );
m_sFilePath = filePath;
int lSlashPos = filePath.Last( '\\' );
m_sSmallFileName = "_" + filePath.Right( filePath.Length() - lSlashPos - 1 );
m_sSmallFilePath = filePath.Left( lSlashPos + 1 );
HIMAGE hImage = OpenFileImage(filePath);
if ( ReadMeta(hImage, "Iptc.Application2.Headline", m_sHeadLine.GetWriteBuf(256), 256 ) == 0 )
{
m_sHeadLine.UngetWriteBuf();
}
else
{
m_sHeadLine.UngetWriteBuf();
m_sHeadLine.Empty();
}
if ( ReadMeta(hImage, "Iptc.Application2.Caption", m_sCaption.GetWriteBuf(2000), 2000 ) == 0 )
{
m_sCaption.UngetWriteBuf();
}
else
{
m_sCaption.UngetWriteBuf();
m_sCaption.Empty();
}
FreeImage(hImage);
m_PanelImage.LoadImage( filePath );
m_PanelImage.Create( this, -1, wxDefaultPosition, m_PanelImage.m_Size );
m_pTopSizer->Add( &m_PanelImage, 1, wxEXPAND | wxALL | wxCENTER, 10 );
m_FTP.Create( this, -1, wxString(FTP) );
m_pTopSizer->Add( &m_FTP, 0, wxLEFT | wxRIGHT | wxEXPAND, 10 );
m_Directory.Create( this, -1, wxString(DIR) );
m_pTopSizer->Add( &m_Directory, 0, wxLEFT | wxRIGHT | wxEXPAND, 10 );
m_Login.Create( this, -1, LOGIN );
m_Password.Create( this, -1, PASSWORD, wxDefaultPosition, wxDefaultSize, wxTE_PASSWORD);
m_pLogPwdSizer->Add( &m_Login, 1, wxEXPAND, 0 );
m_pLogPwdSizer->Add( &m_Password, 1, wxEXPAND, 0 );
m_pTopSizer->Add( m_pLogPwdSizer, 0, wxLEFT | wxRIGHT | wxEXPAND, 10 );
m_HeadLine.Create( this, -1, m_sHeadLine, wxDefaultPosition, wxDefaultSize );
m_pTopSizer->Add( &m_HeadLine, 0, wxEXPAND | wxALL, 10 );
m_Caption.Create( this, -1, m_sCaption, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
m_pTopSizer->Add( &m_Caption, 0, wxEXPAND | wxLEFT | wxRIGHT , 10 );
m_Width.Create( this, -1, "800" );
m_Height.Create( this, -1, "600" );
m_pWidthHeightSizer->Add( &m_Width, 1, wxEXPAND, 0 );
m_pWidthHeightSizer->Add( &m_Height, 1, wxEXPAND, 0 );
m_pTopSizer->Add( m_pWidthHeightSizer, 0, wxLEFT | wxRIGHT | wxEXPAND, 10 );
m_pButtonSizer->Add( new wxButton( this, wxID_OK, "OK" ), 0, wxALL, 10 );
m_pButtonSizer->Add( new wxButton( this, wxID_CANCEL, "Cancel" ), 0, wxALL, 10 );
m_pTopSizer->Add( m_pButtonSizer, 0, wxCENTER, 0 );
SetSizer( m_pTopSizer ); // use the sizer for layout
m_pTopSizer->SetSizeHints( this ); // set size hints to honour minimum size
CenterOnScreen();
}
void cGUDialog::Manage()
{
m_sHeadLine = m_HeadLine.GetValue();
m_sCaption = m_Caption.GetValue();
//Update Title/Caption
HIMAGE hImage = OpenFileImage(m_sFilePath);
if ( m_sHeadLine.Length() != 0 )
ModifyMeta( hImage, "Iptc.Application2.Headline", m_sHeadLine, string );
else
RemoveMeta( hImage, "Iptc.Application2.Headline" );
if ( m_sCaption.Length() != 0 )
ModifyMeta( hImage, "Iptc.Application2.Caption", m_sCaption, string );
else
RemoveMeta( hImage, "Iptc.Application2.Caption" );
SaveImage( hImage );
FreeImage( hImage );
//Resize
long lWidth;
long lHeight;
m_Width.GetValue().ToLong( &lWidth );
m_Height.GetValue().ToLong( &lHeight );
Geometry geo( lWidth, lHeight );
std::string sFilePath = m_sFilePath;
std::string sSmallFilePath = m_sSmallFilePath + m_sSmallFileName;
Image Img;
Img.read( sFilePath );
Img.resize( geo );
Img.write( sSmallFilePath );
//Upload
wxString sTemp;
char* pBuffer = new char[512];
sTemp = m_FTP.GetValue();
memcpy( pBuffer, sTemp.data(), sTemp.length()+1 );
m_FtpConnection.DoOpen( pBuffer );
sTemp = "user " + m_Login.GetValue() + ":" + m_Password.GetValue();
memcpy( pBuffer, sTemp.data(), sTemp.length()+1 );
m_FtpConnection.DoLogin( pBuffer );
m_FtpConnection.DoBinary();
sTemp = "cd " + m_sSmallFilePath;
memcpy( pBuffer, sTemp.data(), sTemp.length()+1 );
m_FtpConnection.DoLCD( pBuffer );
sTemp = "cd " + m_Directory.GetValue();
memcpy( pBuffer, sTemp.data(), sTemp.length()+1 );
m_FtpConnection.DoCD( pBuffer );
memcpy( pBuffer, m_sSmallFileName.data(), m_sSmallFileName.length()+1 );
m_FtpConnection.DoPut( pBuffer );
delete pBuffer;
}
void cGUPanelImage::LoadImage( wxString sFilePath )
{
m_Image = wxImage(sFilePath);
m_bHori = ( m_Image.GetWidth() > m_Image.GetHeight() );
if ( m_bHori )
{
m_Size.SetWidth( 300 );
m_Size.SetHeight( 300 * m_Image.GetHeight() / m_Image.GetWidth() );
}
else
{
m_Size.SetHeight( 200 );
m_Size.SetWidth( 200 * m_Image.GetWidth() / m_Image.GetHeight() );
}
}
void cGUDialog::OnSize( wxSizeEvent & e)
{
wxDialog::OnSize(e);
m_PanelImage.Refresh();
}
void cGUPanelImage::OnPaint (wxPaintEvent &)
{
wxPaintDC dc(this);
wxSize size = GetSize();
wxSize sizeImage = size;
if ( m_bHori )
{
sizeImage.SetHeight( size.GetWidth() * m_Image.GetHeight() / m_Image.GetWidth() );
}
else
{
sizeImage.SetWidth( size.GetHeight() * m_Image.GetWidth() / m_Image.GetHeight() );
}
dc.DrawBitmap(m_Image.Scale( sizeImage.GetWidth(), sizeImage.GetHeight(), wxIMAGE_QUALITY_NORMAL ), (size.GetWidth()-sizeImage.GetWidth())/2, 0, true);
}[/codebox]