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