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