view myframe.cpp @ 3:cc1f1fa544a4

for wxWidgets 3.1
author pyon@macmini
date Sun, 27 Nov 2016 20:15:37 +0900
parents 25a76c128804
children 3fadcf124bb4
line wrap: on
line source

// Filename   : myframe.cpp
// Last Change: 2016-11-27 Sun 20:08:02.
//

#include "myframe.h"
#include "dndfile.h"
#include "sample.xpm"

MyFrame::MyFrame( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) 
    : wxFrame( parent, id, title, pos, size, style )
{
    this->SetIcon( wxIcon( wxT("sample") ) );
	//this->SetSizeHints( wxSize( 450, 200 ), wxSize( 450, 200 ) );
	this->SetSizeHints( wxSize( 550, 200 ), wxSize( 550, 200 ) );
	this->SetBackgroundColour( *wxBLACK );
	
	wxBoxSizer* bSizerTop = new wxBoxSizer( wxVERTICAL );
	
    // Source
	wxBoxSizer* bSizerSource = new wxBoxSizer( wxHORIZONTAL );
	
	m_staticTextSource = new wxStaticText( this, wxID_ANY, wxT("Source"), wxDefaultPosition, wxSize( 90, -1 ), wxALIGN_RIGHT );
	m_staticTextSource->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DLIGHT ) );
	m_staticTextSource->SetBackgroundColour( *wxBLACK );
	
	bSizerSource->Add( m_staticTextSource, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	m_textCtrlSource = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize( 200, -1 ), wxTAB_TRAVERSAL );
	bSizerSource->Add( m_textCtrlSource, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	bSizerTop->Add( bSizerSource, 1, wxEXPAND, 5 );
	
    // Config
    // Type
	wxBoxSizer* bSizerConfig = new wxBoxSizer( wxHORIZONTAL );
	
	m_staticTextConfig = new wxStaticText( this, wxID_ANY, wxT("Config"), wxDefaultPosition, wxSize( 90, -1 ), wxALIGN_RIGHT );
	m_staticTextConfig->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DLIGHT ) );
	m_staticTextConfig->SetBackgroundColour( *wxBLACK );
	bSizerConfig->Add( m_staticTextConfig, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );

	m_staticTextType = new wxStaticText( this, wxID_ANY, wxT("Type"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT );
	m_staticTextType->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DLIGHT ) );
	m_staticTextType->SetBackgroundColour( *wxBLACK );
	bSizerConfig->Add( m_staticTextType, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	m_comboBoxType = new wxComboBox( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, NULL, wxCB_READONLY|wxTAB_TRAVERSAL );
	m_comboBoxType->Append( wxT("Not change") );
	m_comboBoxType->Append( wxT("PNG")  );
	m_comboBoxType->Append( wxT("XPM")  );
	m_comboBoxType->Append( wxT("GIF")  );
	m_comboBoxType->Append( wxT("ICON") );
	m_comboBoxType->Append( wxT("BMP")  );
	m_comboBoxType->Append( wxT("TIFF") );
	m_comboBoxType->Append( wxT("JPEG") );
	m_comboBoxType->Append( wxT("SVG")  );
	m_comboBoxType->SetSelection( 0 );
	bSizerConfig->Add( m_comboBoxType, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
    // Size
	m_staticTextSize = new wxStaticText( this, wxID_ANY, wxT("Size"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT );
	m_staticTextSize->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DLIGHT ) );
	m_staticTextSize->SetBackgroundColour( *wxBLACK );
	bSizerConfig->Add( m_staticTextSize, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	m_staticTextWidth = new wxStaticText( this, wxID_ANY, wxT("Width"), wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL );
	m_staticTextWidth->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DLIGHT ) );
	m_staticTextWidth->SetBackgroundColour( *wxBLACK );
	bSizerConfig->Add( m_staticTextWidth, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
	
	m_textCtrlWidth = new wxTextCtrl( this, wxID_ANY, wxT("0"), wxDefaultPosition, wxSize( 40, -1 ), wxTE_CENTRE );
	m_staticTextWidth->SetBackgroundColour( *wxBLACK );
	bSizerConfig->Add( m_textCtrlWidth, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	m_staticTextHeight = new wxStaticText( this, wxID_ANY, wxT("Height"), wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL );
	m_staticTextHeight->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DLIGHT ) );
	m_staticTextHeight->SetBackgroundColour( *wxBLACK );
	bSizerConfig->Add( m_staticTextHeight, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
	
	m_textCtrlHeight = new wxTextCtrl( this, wxID_ANY, wxT("0"), wxDefaultPosition, wxSize( 50, -1 ), wxTE_CENTRE );
	bSizerConfig->Add( m_textCtrlHeight, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	bSizerTop->Add( bSizerConfig, 1, wxEXPAND, 5 );
	
    // Output
	wxBoxSizer* bSizerOutput = new wxBoxSizer( wxHORIZONTAL );
	
	m_staticTextOutput = new wxStaticText( this, wxID_ANY, wxT("Output folder"), wxDefaultPosition, wxSize( 90, -1 ), wxALIGN_RIGHT );
	m_staticTextOutput->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DLIGHT ) );
	m_staticTextOutput->SetBackgroundColour( *wxBLACK );
	bSizerOutput->Add( m_staticTextOutput, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	m_dirPicker = new wxDirPickerCtrl( this, wxID_ANY, wxGetCwd(), wxT("Select a folder"), wxDefaultPosition, wxSize( 200, -1 ), wxDIRP_DEFAULT_STYLE );
	m_dirPicker->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DLIGHT ) );
	m_dirPicker->SetBackgroundColour( *wxBLACK );
	bSizerOutput->Add( m_dirPicker, 1, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	bSizerTop->Add( bSizerOutput, 1, wxEXPAND, 5 );
	
    // Button
	wxBoxSizer* bSizerButton = new wxBoxSizer( wxHORIZONTAL );
	
	m_buttonExec = new wxButton( this, ID_EXEC, wxT("Exec"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizerButton->Add( m_buttonExec, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	m_buttonExit = new wxButton( this, ID_EXIT, wxT("Exit"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizerButton->Add( m_buttonExit, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	bSizerTop->Add( bSizerButton, 1, wxALIGN_RIGHT|wxALL, 5 );
	
    //
	this->SetSizer( bSizerTop );
	this->Layout();
	
    this->SetDropTarget( new DnDFile( m_textCtrlSource ) );

	this->Centre( wxBOTH );
}

MyFrame::~MyFrame()
{
}

// Event Table
BEGIN_EVENT_TABLE( MyFrame, wxFrame )
    EVT_BUTTON( ID_EXEC, MyFrame::OnExec )
    EVT_BUTTON( ID_EXIT, MyFrame::OnExit )
END_EVENT_TABLE()

void MyFrame::OnExec( wxCommandEvent& WXUNUSED(event) )
{
    wxString buf;

    wxString file, path;
    buf = m_textCtrlSource->GetValue();
    if ( wxFileName::FileExists( buf ) ) file = buf;
    if ( wxFileName::DirExists( buf  ) ) path = buf;
    if ( file.IsEmpty() && path.IsEmpty() ) {
        WarnMessage( wxT("no input !") );
        return;
    }
    
    choice_type = m_comboBoxType->GetSelection();

    buf = m_textCtrlWidth->GetValue();
    buf.ToLong( &w, 10 );
    buf = m_textCtrlHeight->GetValue();
    buf.ToLong( &h, 10 );

    if ( choice_type == 0 && w == 0 && h == 0 ) {
        WarnMessage( wxT("no change !") );
        return;
    }

    buf = m_dirPicker->GetPath();
    if ( buf.IsEmpty() ) {
        WarnMessage( wxT("no output directory !") );
        return;
    }
    out_dir = buf;

    bool result;
    if ( file.IsEmpty() ) {
        result = ConvertFiles( path );
    }
    else {
        result = ConvertAFile( file );
    }
    if ( !result ) return;
    
    wxMessageDialog *md = new wxMessageDialog( this, wxT("Done !"), wxT("Message"), wxICON_INFORMATION|wxOK );
    md->ShowModal();
    md->Destroy();
}

void MyFrame::OnExit( wxCommandEvent& WXUNUSED(event) )
{
    Close();
}

void MyFrame::WarnMessage( wxString msg ) 
{
    wxMessageDialog *md = new wxMessageDialog( this, msg, wxT("Warning"), wxICON_ERROR|wxOK );
    md->ShowModal();
    md->Destroy();
}

bool MyFrame::ConvertAFile( wxString file )
{
    wxFileName f( file );
    wxString ext = f.GetExt();
    wxString name = f.GetName();
    wxBitmapType in_type;

    if      ( ext.IsSameAs( wxT("jpg"),  false ) ) in_type = wxBITMAP_TYPE_JPEG;
    else if ( ext.IsSameAs( wxT("jpeg"), false ) ) in_type = wxBITMAP_TYPE_JPEG;
    else if ( ext.IsSameAs( wxT("png"),  false ) ) in_type = wxBITMAP_TYPE_PNG;
    else if ( ext.IsSameAs( wxT("xpm"),  false ) ) in_type = wxBITMAP_TYPE_XPM;
    else if ( ext.IsSameAs( wxT("gif"),  false ) ) in_type = wxBITMAP_TYPE_GIF;
    else if ( ext.IsSameAs( wxT("ico"),  false ) ) in_type = wxBITMAP_TYPE_ICO;
    else if ( ext.IsSameAs( wxT("bmp"),  false ) ) in_type = wxBITMAP_TYPE_BMP;
    else if ( ext.IsSameAs( wxT("tif"),  false ) ) in_type = wxBITMAP_TYPE_TIFF;
    else if ( ext.IsSameAs( wxT("tiff"), false ) ) in_type = wxBITMAP_TYPE_TIFF;
    else {
        WarnMessage( wxT("Input file type is not supported !") );
        return false;
    }

    wxBitmapType out_type = in_type;
    if ( choice_type == 1 ) {
        out_type = wxBITMAP_TYPE_PNG;
        ext = wxT("png");
    }
    else if ( choice_type == 2 ) {
        out_type = wxBITMAP_TYPE_XPM;
        ext = wxT("xpm");
    }
    else if ( choice_type == 3 ) {
        out_type = wxBITMAP_TYPE_GIF;
        ext = wxT("gif");
    }
    else if ( choice_type == 4 ) {
        out_type = wxBITMAP_TYPE_ICO;
        ext = wxT("ico");
    }
    else if ( choice_type == 5 ) {
        out_type = wxBITMAP_TYPE_BMP;
        ext = wxT("bmp");
    }
    else if ( choice_type == 6 ) {
        out_type = wxBITMAP_TYPE_TIFF;
        ext = wxT("tiff");
    }
    else if ( choice_type == 7 ) {
        out_type = wxBITMAP_TYPE_JPEG;
        ext = wxT("jpg");
    }
    else if ( choice_type == 8 ) {
        // SVG
    }

    wxImage image( file, in_type );
    if ( w == 0 || h == 0 ) {
        w = (long)(image.GetWidth());
        h = (long)(image.GetHeight());
    }
    wxImage output = image.Scale( (int)w, (int)h, wxIMAGE_QUALITY_HIGH );
    wxString outfile = out_dir + wxFILE_SEP_PATH + name + wxT(".") + ext;

    if ( file.IsSameAs( outfile ) ) {
        WarnMessage( wxT("Output file is same as input file !") );
        return false;
    }

    output.SaveFile( outfile, out_type );
    return true;
}

bool MyFrame::ConvertFiles( wxString dir )
{
    wxFileName dn( dir );

    if ( out_dir.IsSameAs( dn.GetPath() ) ) {
        WarnMessage( wxT("Output directory is same as input directory !") );
        return false;
    }

    wxBitmapType out_type = wxBITMAP_TYPE_INVALID;
    wxString out_ext;
    if ( choice_type == 1 ) {
        out_type = wxBITMAP_TYPE_PNG;
        out_ext = wxT("png");
    }
    else if ( choice_type == 2 ) {
        out_type = wxBITMAP_TYPE_XPM;
        out_ext = wxT("xpm");
    }
    else if ( choice_type == 3 ) {
        out_type = wxBITMAP_TYPE_GIF;
        out_ext = wxT("gif");
    }
    else if ( choice_type == 4 ) {
        out_type = wxBITMAP_TYPE_ICO;
        out_ext = wxT("ico");
    }
    else if ( choice_type == 5 ) {
        out_type = wxBITMAP_TYPE_BMP;
        out_ext = wxT("bmp");
    }
    else if ( choice_type == 6 ) {
        out_type = wxBITMAP_TYPE_TIFF;
        out_ext = wxT("tiff");
    }
    else if ( choice_type == 7 ) {
        out_type = wxBITMAP_TYPE_JPEG;
        out_ext = wxT("jpg");
    }
    else if ( choice_type == 8 ) {
        // SVG
    }

    wxArrayString files;
    wxDir::GetAllFiles( dir, &files, wxT("*.*"), wxDIR_DEFAULT );

    for ( long i = 0; i < files.GetCount(); i++ ) {

        wxFileName fn( files[i] );
        wxString in_ext = fn.GetExt();
        wxBitmapType in_type;

        if      ( in_ext.IsSameAs( wxT("jpg"),  false ) ) in_type = wxBITMAP_TYPE_JPEG;
        else if ( in_ext.IsSameAs( wxT("jpeg"), false ) ) in_type = wxBITMAP_TYPE_JPEG;
        else if ( in_ext.IsSameAs( wxT("png"),  false ) ) in_type = wxBITMAP_TYPE_PNG;
        else if ( in_ext.IsSameAs( wxT("xpm"),  false ) ) in_type = wxBITMAP_TYPE_XPM;
        else if ( in_ext.IsSameAs( wxT("gif"),  false ) ) in_type = wxBITMAP_TYPE_GIF;
        else if ( in_ext.IsSameAs( wxT("ico"),  false ) ) in_type = wxBITMAP_TYPE_ICO;
        else if ( in_ext.IsSameAs( wxT("bmp"),  false ) ) in_type = wxBITMAP_TYPE_BMP;
        else if ( in_ext.IsSameAs( wxT("tif"),  false ) ) in_type = wxBITMAP_TYPE_TIFF;
        else if ( in_ext.IsSameAs( wxT("tiff"), false ) ) in_type = wxBITMAP_TYPE_TIFF;
        else continue;

        if ( out_type == wxBITMAP_TYPE_INVALID ) out_type = in_type;

        wxString fullpath = fn.GetPath() + wxFILE_SEP_PATH + fn.GetName();

        fullpath.Replace( dn.GetPath(), wxEmptyString, false );
        fullpath = out_dir + wxFILE_SEP_PATH + fullpath + wxT(".") + out_ext;

        wxFileName tf( fullpath );
        if ( !tf.Exists() ) tf.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );

        wxImage image( files[i], in_type );
        if ( w == 0 || h == 0 ) {
            w = (long)(image.GetWidth());
            h = (long)(image.GetHeight());
        }
        wxImage output = image.Scale( (int)w, (int)h, wxIMAGE_QUALITY_HIGH );
        output.SaveFile( fullpath, out_type );
    }

    return true;
}