| 0 | 1 // Filename   : myframe.cpp | 
| 1 | 2 // Last Change: 29-Jan-2014. | 
| 0 | 3 // | 
|  | 4 | 
|  | 5 #include "myframe.h" | 
|  | 6 #include "dndfile.h" | 
|  | 7 #include "sample.xpm" | 
|  | 8 | 
|  | 9 MyFrame::MyFrame( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) | 
|  | 10     : wxFrame( parent, id, title, pos, size, style ) | 
|  | 11 { | 
|  | 12     this->SetIcon( wxIcon( wxT("sample") ) ); | 
|  | 13 	this->SetSizeHints( wxSize( 450, 200 ), wxSize( 450, 200 ) ); | 
|  | 14 	this->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW ) ); | 
|  | 15 	this->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNTEXT ) ); | 
|  | 16 | 
|  | 17 	wxBoxSizer* bSizerTop = new wxBoxSizer( wxVERTICAL ); | 
|  | 18 | 
|  | 19     // Source | 
|  | 20 	wxBoxSizer* bSizerSource = new wxBoxSizer( wxHORIZONTAL ); | 
|  | 21 | 
|  | 22 	m_staticTextSource = new wxStaticText( this, wxID_ANY, wxT("Source"), wxDefaultPosition, wxSize( 90,-1 ), wxALIGN_RIGHT ); | 
|  | 23 	m_staticTextSource->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DLIGHT ) ); | 
|  | 24 	m_staticTextSource->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DDKSHADOW ) ); | 
|  | 25 | 
|  | 26 	bSizerSource->Add( m_staticTextSource, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 ); | 
|  | 27 | 
|  | 28 	m_textCtrlSource = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize( 200,-1 ), wxTAB_TRAVERSAL ); | 
|  | 29 	bSizerSource->Add( m_textCtrlSource, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 ); | 
|  | 30 | 
|  | 31 	bSizerTop->Add( bSizerSource, 1, wxEXPAND|wxALIGN_CENTER_HORIZONTAL, 5 ); | 
|  | 32 | 
|  | 33     // Config | 
|  | 34     // Type | 
|  | 35 	wxBoxSizer* bSizerConfig = new wxBoxSizer( wxHORIZONTAL ); | 
|  | 36 | 
|  | 37 	m_staticTextConfig = new wxStaticText( this, wxID_ANY, wxT("Config"), wxDefaultPosition, wxSize( 90,-1 ), wxALIGN_RIGHT ); | 
|  | 38 	m_staticTextConfig->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DLIGHT ) ); | 
|  | 39 	m_staticTextConfig->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DDKSHADOW ) ); | 
|  | 40 	bSizerConfig->Add( m_staticTextConfig, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 ); | 
|  | 41 | 
|  | 42 	m_staticTextType = new wxStaticText( this, wxID_ANY, wxT("Type"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT ); | 
|  | 43 	m_staticTextType->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DLIGHT ) ); | 
|  | 44 	m_staticTextType->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DDKSHADOW ) ); | 
|  | 45 	bSizerConfig->Add( m_staticTextType, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 ); | 
|  | 46 | 
|  | 47 	m_comboBoxType = new wxComboBox( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, NULL, wxCB_READONLY|wxTAB_TRAVERSAL ); | 
|  | 48 	m_comboBoxType->Append( wxT("Not change") ); | 
|  | 49 	m_comboBoxType->Append( wxT("PNG")  ); | 
|  | 50 	m_comboBoxType->Append( wxT("XPM")  ); | 
|  | 51 	m_comboBoxType->Append( wxT("GIF")  ); | 
|  | 52 	m_comboBoxType->Append( wxT("ICON") ); | 
|  | 53 	m_comboBoxType->Append( wxT("BMP")  ); | 
|  | 54 	m_comboBoxType->Append( wxT("TIFF") ); | 
|  | 55 	m_comboBoxType->SetSelection( 0 ); | 
|  | 56 	bSizerConfig->Add( m_comboBoxType, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 ); | 
|  | 57 | 
|  | 58     // Size | 
|  | 59 	m_staticTextSize = new wxStaticText( this, wxID_ANY, wxT("Size"), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT ); | 
|  | 60 	m_staticTextSize->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DLIGHT ) ); | 
|  | 61 	m_staticTextSize->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DDKSHADOW ) ); | 
|  | 62 	bSizerConfig->Add( m_staticTextSize, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 ); | 
|  | 63 | 
|  | 64 	m_staticTextWidth = new wxStaticText( this, wxID_ANY, wxT("Width"), wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL ); | 
|  | 65 	m_staticTextWidth->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DLIGHT ) ); | 
|  | 66 	m_staticTextWidth->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DDKSHADOW ) ); | 
|  | 67 	bSizerConfig->Add( m_staticTextWidth, 0, wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT|wxALL, 5 ); | 
|  | 68 | 
|  | 69 	m_textCtrlWidth = new wxTextCtrl( this, wxID_ANY, wxT("0"), wxDefaultPosition, wxSize( 40, -1 ), wxTE_CENTRE ); | 
|  | 70 	bSizerConfig->Add( m_textCtrlWidth, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 ); | 
|  | 71 | 
|  | 72 	m_staticTextHeight = new wxStaticText( this, wxID_ANY, wxT("Height"), wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL ); | 
|  | 73 	m_staticTextHeight->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DLIGHT ) ); | 
|  | 74 	m_staticTextHeight->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DDKSHADOW ) ); | 
|  | 75 	bSizerConfig->Add( m_staticTextHeight, 0, wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT|wxALL, 5 ); | 
|  | 76 | 
|  | 77 	m_textCtrlHeight = new wxTextCtrl( this, wxID_ANY, wxT("0"), wxDefaultPosition, wxSize( 50, -1 ), wxTE_CENTRE ); | 
|  | 78 	bSizerConfig->Add( m_textCtrlHeight, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 ); | 
|  | 79 | 
|  | 80 	bSizerTop->Add( bSizerConfig, 1, wxEXPAND, 5 ); | 
|  | 81 | 
|  | 82     // Output | 
|  | 83 	wxBoxSizer* bSizerOutput = new wxBoxSizer( wxHORIZONTAL ); | 
|  | 84 | 
|  | 85 	m_staticTextOutput = new wxStaticText( this, wxID_ANY, wxT("Output folder"), wxDefaultPosition, wxSize( 90,-1 ), wxALIGN_RIGHT ); | 
|  | 86 	m_staticTextOutput->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DLIGHT ) ); | 
|  | 87 	m_staticTextOutput->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DDKSHADOW ) ); | 
|  | 88 	bSizerOutput->Add( m_staticTextOutput, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 ); | 
|  | 89 | 
|  | 90 	m_dirPicker = new wxDirPickerCtrl( this, wxID_ANY, wxGetCwd(), wxT("Select a folder"), wxDefaultPosition, wxSize( 200,-1 ), wxDIRP_DEFAULT_STYLE ); | 
|  | 91 	m_dirPicker->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DDKSHADOW ) ); | 
|  | 92 	bSizerOutput->Add( m_dirPicker, 1, wxALL|wxALIGN_CENTER_VERTICAL, 5 ); | 
|  | 93 | 
|  | 94 	bSizerTop->Add( bSizerOutput, 1, wxEXPAND, 5 ); | 
|  | 95 | 
|  | 96     // Button | 
|  | 97 	wxBoxSizer* bSizerButton = new wxBoxSizer( wxHORIZONTAL ); | 
|  | 98 | 
|  | 99 	m_buttonExec = new wxButton( this, ID_EXEC, wxT("Exec"), wxDefaultPosition, wxDefaultSize, 0 ); | 
|  | 100 	bSizerButton->Add( m_buttonExec, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 ); | 
|  | 101 | 
|  | 102 	m_buttonExit = new wxButton( this, ID_EXIT, wxT("Exit"), wxDefaultPosition, wxDefaultSize, 0 ); | 
|  | 103 	bSizerButton->Add( m_buttonExit, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 ); | 
|  | 104 | 
|  | 105 	bSizerTop->Add( bSizerButton, 1, wxALIGN_RIGHT|wxALL, 5 ); | 
|  | 106 | 
|  | 107     // | 
|  | 108 	this->SetSizer( bSizerTop ); | 
|  | 109 	this->Layout(); | 
|  | 110 | 
|  | 111     this->SetDropTarget( new DnDFile( m_textCtrlSource ) ); | 
|  | 112 | 
|  | 113 	this->Centre( wxBOTH ); | 
|  | 114 } | 
|  | 115 | 
|  | 116 MyFrame::~MyFrame() | 
|  | 117 { | 
|  | 118 } | 
|  | 119 | 
|  | 120 // Event Table | 
|  | 121 BEGIN_EVENT_TABLE( MyFrame, wxFrame ) | 
|  | 122     EVT_BUTTON( ID_EXEC, MyFrame::OnExec ) | 
|  | 123     EVT_BUTTON( ID_EXIT, MyFrame::OnExit ) | 
|  | 124 END_EVENT_TABLE() | 
|  | 125 | 
|  | 126 void MyFrame::OnExec( wxCommandEvent& WXUNUSED(event) ) | 
|  | 127 { | 
|  | 128     wxString buf; | 
|  | 129 | 
|  | 130     wxString file, path; | 
|  | 131     buf = m_textCtrlSource->GetValue(); | 
|  | 132     if ( wxFileName::FileExists( buf ) ) file = buf; | 
|  | 133     if ( wxFileName::DirExists( buf  ) ) path = buf; | 
|  | 134     if ( file.IsEmpty() && path.IsEmpty() ) { | 
|  | 135         WarnMessage( wxT("no input !") ); | 
|  | 136         return; | 
|  | 137     } | 
|  | 138 | 
|  | 139     choice_type = m_comboBoxType->GetSelection(); | 
|  | 140 | 
|  | 141     buf = m_textCtrlWidth->GetValue(); | 
|  | 142     buf.ToLong( &w, 10 ); | 
|  | 143     buf = m_textCtrlHeight->GetValue(); | 
|  | 144     buf.ToLong( &h, 10 ); | 
|  | 145 | 
|  | 146     if ( choice_type == 0 && w == 0 && h == 0 ) { | 
|  | 147         WarnMessage( wxT("no change !") ); | 
|  | 148         return; | 
|  | 149     } | 
|  | 150 | 
|  | 151     buf = m_dirPicker->GetPath(); | 
|  | 152     if ( buf.IsEmpty() ) { | 
|  | 153         WarnMessage( wxT("no output directory !") ); | 
|  | 154         return; | 
|  | 155     } | 
|  | 156     out_dir = buf; | 
|  | 157 | 
|  | 158     bool result; | 
|  | 159     if ( file.IsEmpty() ) { | 
|  | 160         result = ConvertFiles( path ); | 
|  | 161     } | 
|  | 162     else { | 
|  | 163         result = ConvertAFile( file ); | 
|  | 164     } | 
|  | 165     if ( !result ) return; | 
|  | 166 | 
|  | 167     wxMessageDialog *md = new wxMessageDialog( this, wxT("Done !"), wxT("Message"), wxICON_INFORMATION|wxOK ); | 
|  | 168     md->ShowModal(); | 
|  | 169     md->Destroy(); | 
|  | 170 } | 
|  | 171 | 
|  | 172 void MyFrame::OnExit( wxCommandEvent& WXUNUSED(event) ) | 
|  | 173 { | 
|  | 174     Close(); | 
|  | 175 } | 
|  | 176 | 
|  | 177 void MyFrame::WarnMessage( wxString msg ) | 
|  | 178 { | 
|  | 179     wxMessageDialog *md = new wxMessageDialog( this, msg, wxT("Warning"), wxICON_ERROR|wxOK ); | 
|  | 180     md->ShowModal(); | 
|  | 181     md->Destroy(); | 
|  | 182 } | 
|  | 183 | 
|  | 184 bool MyFrame::ConvertAFile( wxString file ) | 
|  | 185 { | 
|  | 186     wxFileName f( file ); | 
|  | 187     wxString ext = f.GetExt(); | 
|  | 188     wxString name = f.GetName(); | 
|  | 189     int in_type; | 
|  | 190 | 
|  | 191     if      ( ext.IsSameAs( wxT("jpg"),  false ) ) in_type = wxBITMAP_TYPE_JPEG; | 
|  | 192     else if ( ext.IsSameAs( wxT("jpeg"), false ) ) in_type = wxBITMAP_TYPE_JPEG; | 
|  | 193     else if ( ext.IsSameAs( wxT("png"),  false ) ) in_type = wxBITMAP_TYPE_PNG; | 
|  | 194     else if ( ext.IsSameAs( wxT("xpm"),  false ) ) in_type = wxBITMAP_TYPE_XPM; | 
|  | 195     else if ( ext.IsSameAs( wxT("gif"),  false ) ) in_type = wxBITMAP_TYPE_GIF; | 
|  | 196     else if ( ext.IsSameAs( wxT("ico"),  false ) ) in_type = wxBITMAP_TYPE_ICO; | 
|  | 197     else if ( ext.IsSameAs( wxT("bmp"),  false ) ) in_type = wxBITMAP_TYPE_BMP; | 
|  | 198     else if ( ext.IsSameAs( wxT("tif"),  false ) ) in_type = wxBITMAP_TYPE_TIFF; | 
|  | 199     else if ( ext.IsSameAs( wxT("tiff"), false ) ) in_type = wxBITMAP_TYPE_TIFF; | 
| 1 | 200     else { | 
|  | 201         WarnMessage( wxT("Input file type is not supported !") ); | 
|  | 202         return false; | 
|  | 203     } | 
| 0 | 204 | 
|  | 205     int out_type = in_type; | 
|  | 206     if ( choice_type == 1 ) { | 
|  | 207         out_type = wxBITMAP_TYPE_PNG; | 
|  | 208         ext = wxT("png"); | 
|  | 209     } | 
|  | 210     else if ( choice_type == 2 ) { | 
|  | 211         out_type = wxBITMAP_TYPE_XPM; | 
|  | 212         ext = wxT("xpm"); | 
|  | 213     } | 
|  | 214     else if ( choice_type == 3 ) { | 
|  | 215         out_type = wxBITMAP_TYPE_GIF; | 
|  | 216         ext = wxT("gif"); | 
|  | 217     } | 
|  | 218     else if ( choice_type == 4 ) { | 
|  | 219         out_type = wxBITMAP_TYPE_ICO; | 
|  | 220         ext = wxT("ico"); | 
|  | 221     } | 
|  | 222     else if ( choice_type == 5 ) { | 
|  | 223         out_type = wxBITMAP_TYPE_BMP; | 
|  | 224         ext = wxT("bmp"); | 
|  | 225     } | 
|  | 226     else if ( choice_type == 6 ) { | 
|  | 227         out_type = wxBITMAP_TYPE_TIFF; | 
|  | 228         ext = wxT("tiff"); | 
|  | 229     } | 
|  | 230 | 
|  | 231     wxImage image( file, in_type ); | 
|  | 232     if ( w == 0 || h == 0 ) { | 
|  | 233         w = (long)(image.GetWidth()); | 
|  | 234         h = (long)(image.GetHeight()); | 
|  | 235     } | 
|  | 236     wxImage output = image.Scale( (int)w, (int)h, wxIMAGE_QUALITY_HIGH ); | 
|  | 237     wxString outfile = out_dir + wxFILE_SEP_PATH + name + wxT(".") + ext; | 
|  | 238 | 
|  | 239     if ( file.IsSameAs( outfile ) ) { | 
|  | 240         WarnMessage( wxT("Output file is same as input file !") ); | 
|  | 241         return false; | 
|  | 242     } | 
|  | 243 | 
|  | 244     output.SaveFile( outfile, out_type ); | 
|  | 245     return true; | 
|  | 246 } | 
|  | 247 | 
|  | 248 bool MyFrame::ConvertFiles( wxString dir ) | 
|  | 249 { | 
|  | 250     wxFileName dn( dir ); | 
|  | 251 | 
|  | 252     if ( out_dir.IsSameAs( dn.GetPath() ) ) { | 
|  | 253         WarnMessage( wxT("Output directory is same as input directory !") ); | 
|  | 254         return false; | 
|  | 255     } | 
|  | 256 | 
|  | 257     int out_type = wxBITMAP_TYPE_INVALID; | 
|  | 258     wxString out_ext; | 
|  | 259     if ( choice_type == 1 ) { | 
|  | 260         out_type = wxBITMAP_TYPE_PNG; | 
|  | 261         out_ext = wxT("png"); | 
|  | 262     } | 
|  | 263     else if ( choice_type == 2 ) { | 
|  | 264         out_type = wxBITMAP_TYPE_XPM; | 
|  | 265         out_ext = wxT("xpm"); | 
|  | 266     } | 
|  | 267     else if ( choice_type == 3 ) { | 
|  | 268         out_type = wxBITMAP_TYPE_GIF; | 
|  | 269         out_ext = wxT("gif"); | 
|  | 270     } | 
|  | 271     else if ( choice_type == 4 ) { | 
|  | 272         out_type = wxBITMAP_TYPE_ICO; | 
|  | 273         out_ext = wxT("ico"); | 
|  | 274     } | 
|  | 275     else if ( choice_type == 5 ) { | 
|  | 276         out_type = wxBITMAP_TYPE_BMP; | 
|  | 277         out_ext = wxT("bmp"); | 
|  | 278     } | 
|  | 279     else if ( choice_type == 6 ) { | 
|  | 280         out_type = wxBITMAP_TYPE_TIFF; | 
|  | 281         out_ext = wxT("tiff"); | 
|  | 282     } | 
|  | 283 | 
|  | 284     wxArrayString files; | 
|  | 285     wxDir::GetAllFiles( dir, &files, wxT("*.*"), wxDIR_DEFAULT ); | 
|  | 286 | 
| 1 | 287     for ( long i = 0; i < files.GetCount(); i++ ) { | 
| 0 | 288 | 
|  | 289         wxFileName fn( files[i] ); | 
|  | 290         wxString in_ext = fn.GetExt(); | 
|  | 291         int in_type; | 
|  | 292 | 
|  | 293         if      ( in_ext.IsSameAs( wxT("jpg"),  false ) ) in_type = wxBITMAP_TYPE_JPEG; | 
|  | 294         else if ( in_ext.IsSameAs( wxT("jpeg"), false ) ) in_type = wxBITMAP_TYPE_JPEG; | 
|  | 295         else if ( in_ext.IsSameAs( wxT("png"),  false ) ) in_type = wxBITMAP_TYPE_PNG; | 
|  | 296         else if ( in_ext.IsSameAs( wxT("xpm"),  false ) ) in_type = wxBITMAP_TYPE_XPM; | 
|  | 297         else if ( in_ext.IsSameAs( wxT("gif"),  false ) ) in_type = wxBITMAP_TYPE_GIF; | 
|  | 298         else if ( in_ext.IsSameAs( wxT("ico"),  false ) ) in_type = wxBITMAP_TYPE_ICO; | 
|  | 299         else if ( in_ext.IsSameAs( wxT("bmp"),  false ) ) in_type = wxBITMAP_TYPE_BMP; | 
|  | 300         else if ( in_ext.IsSameAs( wxT("tif"),  false ) ) in_type = wxBITMAP_TYPE_TIFF; | 
|  | 301         else if ( in_ext.IsSameAs( wxT("tiff"), false ) ) in_type = wxBITMAP_TYPE_TIFF; | 
| 1 | 302         else continue; | 
| 0 | 303 | 
|  | 304         if ( out_type == wxBITMAP_TYPE_INVALID ) out_type = in_type; | 
|  | 305 | 
|  | 306         wxString fullpath = fn.GetPath() + wxFILE_SEP_PATH + fn.GetName(); | 
|  | 307 | 
|  | 308         fullpath.Replace( dn.GetPath(), wxEmptyString, false ); | 
|  | 309         fullpath = out_dir + fullpath + wxT(".") + out_ext; | 
|  | 310 | 
|  | 311         wxFileName tf( fullpath ); | 
|  | 312         if ( !tf.Exists() ) tf.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ); | 
|  | 313 | 
|  | 314         wxImage image( files[i], in_type ); | 
|  | 315         if ( w == 0 || h == 0 ) { | 
|  | 316             w = (long)(image.GetWidth()); | 
|  | 317             h = (long)(image.GetHeight()); | 
|  | 318         } | 
|  | 319         wxImage output = image.Scale( (int)w, (int)h, wxIMAGE_QUALITY_HIGH ); | 
|  | 320         output.SaveFile( fullpath, out_type ); | 
|  | 321     } | 
|  | 322 | 
|  | 323     return true; | 
|  | 324 } | 
|  | 325 |