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