comparison myframe.cpp @ 0:ebc42d7a0010

First Release.
author pyon@macmini
date Fri, 24 Jan 2014 20:35:59 +0900
parents
children 593c93e904b5
comparison
equal deleted inserted replaced
-1:000000000000 0:ebc42d7a0010
1 // Filename : myframe.cpp
2 // Last Change: 24-Jan-2014.
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;
200
201 int out_type = in_type;
202 if ( choice_type == 1 ) {
203 out_type = wxBITMAP_TYPE_PNG;
204 ext = wxT("png");
205 }
206 else if ( choice_type == 2 ) {
207 out_type = wxBITMAP_TYPE_XPM;
208 ext = wxT("xpm");
209 }
210 else if ( choice_type == 3 ) {
211 out_type = wxBITMAP_TYPE_GIF;
212 ext = wxT("gif");
213 }
214 else if ( choice_type == 4 ) {
215 out_type = wxBITMAP_TYPE_ICO;
216 ext = wxT("ico");
217 }
218 else if ( choice_type == 5 ) {
219 out_type = wxBITMAP_TYPE_BMP;
220 ext = wxT("bmp");
221 }
222 else if ( choice_type == 6 ) {
223 out_type = wxBITMAP_TYPE_TIFF;
224 ext = wxT("tiff");
225 }
226
227 wxImage image( file, in_type );
228 if ( w == 0 || h == 0 ) {
229 w = (long)(image.GetWidth());
230 h = (long)(image.GetHeight());
231 }
232 wxImage output = image.Scale( (int)w, (int)h, wxIMAGE_QUALITY_HIGH );
233 wxString outfile = out_dir + wxFILE_SEP_PATH + name + wxT(".") + ext;
234
235 if ( file.IsSameAs( outfile ) ) {
236 WarnMessage( wxT("Output file is same as input file !") );
237 return false;
238 }
239
240 output.SaveFile( outfile, out_type );
241 return true;
242 }
243
244 bool MyFrame::ConvertFiles( wxString dir )
245 {
246 wxFileName dn( dir );
247
248 if ( out_dir.IsSameAs( dn.GetPath() ) ) {
249 WarnMessage( wxT("Output directory is same as input directory !") );
250 return false;
251 }
252
253 int out_type = wxBITMAP_TYPE_INVALID;
254 wxString out_ext;
255 if ( choice_type == 1 ) {
256 out_type = wxBITMAP_TYPE_PNG;
257 out_ext = wxT("png");
258 }
259 else if ( choice_type == 2 ) {
260 out_type = wxBITMAP_TYPE_XPM;
261 out_ext = wxT("xpm");
262 }
263 else if ( choice_type == 3 ) {
264 out_type = wxBITMAP_TYPE_GIF;
265 out_ext = wxT("gif");
266 }
267 else if ( choice_type == 4 ) {
268 out_type = wxBITMAP_TYPE_ICO;
269 out_ext = wxT("ico");
270 }
271 else if ( choice_type == 5 ) {
272 out_type = wxBITMAP_TYPE_BMP;
273 out_ext = wxT("bmp");
274 }
275 else if ( choice_type == 6 ) {
276 out_type = wxBITMAP_TYPE_TIFF;
277 out_ext = wxT("tiff");
278 }
279
280 wxArrayString files;
281 wxDir::GetAllFiles( dir, &files, wxT("*.*"), wxDIR_DEFAULT );
282
283 for ( int i = 0; i < files.GetCount(); i++ ) {
284
285 wxFileName fn( files[i] );
286 wxString in_ext = fn.GetExt();
287 int in_type;
288
289 if ( in_ext.IsSameAs( wxT("jpg"), false ) ) in_type = wxBITMAP_TYPE_JPEG;
290 else if ( in_ext.IsSameAs( wxT("jpeg"), false ) ) in_type = wxBITMAP_TYPE_JPEG;
291 else if ( in_ext.IsSameAs( wxT("png"), false ) ) in_type = wxBITMAP_TYPE_PNG;
292 else if ( in_ext.IsSameAs( wxT("xpm"), false ) ) in_type = wxBITMAP_TYPE_XPM;
293 else if ( in_ext.IsSameAs( wxT("gif"), false ) ) in_type = wxBITMAP_TYPE_GIF;
294 else if ( in_ext.IsSameAs( wxT("ico"), false ) ) in_type = wxBITMAP_TYPE_ICO;
295 else if ( in_ext.IsSameAs( wxT("bmp"), false ) ) in_type = wxBITMAP_TYPE_BMP;
296 else if ( in_ext.IsSameAs( wxT("tif"), false ) ) in_type = wxBITMAP_TYPE_TIFF;
297 else if ( in_ext.IsSameAs( wxT("tiff"), false ) ) in_type = wxBITMAP_TYPE_TIFF;
298
299 if ( out_type == wxBITMAP_TYPE_INVALID ) out_type = in_type;
300
301 wxString fullpath = fn.GetPath() + wxFILE_SEP_PATH + fn.GetName();
302
303 fullpath.Replace( dn.GetPath(), wxEmptyString, false );
304 fullpath = out_dir + fullpath + wxT(".") + out_ext;
305
306 wxFileName tf( fullpath );
307 if ( !tf.Exists() ) tf.Mkdir( wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL );
308
309 wxImage image( files[i], in_type );
310 if ( w == 0 || h == 0 ) {
311 w = (long)(image.GetWidth());
312 h = (long)(image.GetHeight());
313 }
314 wxImage output = image.Scale( (int)w, (int)h, wxIMAGE_QUALITY_HIGH );
315 output.SaveFile( fullpath, out_type );
316 }
317
318 return true;
319 }
320