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