0
|
1 // Filename : myframe.cpp
|
|
2 // Last Change: 20-Oct-2012.
|
|
3 //
|
|
4
|
|
5 #include "main.h"
|
|
6 #include "myframe.h"
|
|
7 #include "about.h"
|
|
8
|
|
9 // resources
|
|
10 #if !defined(__WXMSW__) && !defined(__WXPM__)
|
|
11 #include "sample.xpm"
|
|
12 #endif
|
|
13
|
|
14 MyFrame::MyFrame( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style )
|
|
15 : wxFrame( parent, id, title, pos, size, style )
|
|
16 {
|
|
17 this->SetSizeHints( wxDefaultSize, wxDefaultSize );
|
|
18 this->SetBackgroundColour( wxColour(wxT("WHEAT")) );
|
|
19 // set the frame icon
|
|
20 SetIcon(wxICON(sample));
|
|
21
|
|
22 // メニューバー Menu
|
|
23 m_menubar = new wxMenuBar();
|
|
24 m_menuFile = new wxMenu();
|
|
25
|
2
|
26 wxMenuItem* m_menuItemSetup = new wxMenuItem( m_menuFile, ID_MNSETUP, wxString( wxT("&Setup...") ) , wxT("Setup parameters"), wxITEM_NORMAL );
|
|
27 m_menuFile->Append( m_menuItemSetup );
|
|
28
|
|
29 wxMenuItem* m_menuItemSave = new wxMenuItem( m_menuFile, ID_MNSAVE, wxString( wxT("Save setting...") ) , wxT("Save parameters"), wxITEM_NORMAL );
|
|
30 m_menuFile->Append( m_menuItemSave );
|
|
31
|
0
|
32 wxMenuItem* m_menuItemAbout = new wxMenuItem( m_menuFile, ID_MNABOUT, wxString( wxT("&About...\tF1") ) , wxT("Show about dialog"), wxITEM_NORMAL );
|
|
33 m_menuFile->Append( m_menuItemAbout );
|
|
34
|
1
|
35 m_menubar->Append( m_menuFile, wxT("File(&F)") );
|
0
|
36
|
|
37 this->SetMenuBar( m_menubar );
|
|
38
|
|
39 //
|
1
|
40 wxFlexGridSizer* fgSizer = new wxFlexGridSizer( 0, 4, 0, 0 );
|
0
|
41 fgSizer->SetFlexibleDirection( wxBOTH );
|
|
42 fgSizer->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
|
|
43
|
|
44 m_staticText1 = new wxStaticText( this, wxID_ANY, wxT("Folder"), wxDefaultPosition, wxDefaultSize, 0 );
|
|
45 fgSizer->Add( m_staticText1, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
|
|
46
|
|
47 m_dirPickerWatch = new wxDirPickerCtrl( this, ID_WATCHDIR, wxEmptyString, wxT("Select a folder"), wxDefaultPosition, wxDefaultSize, wxDIRP_DEFAULT_STYLE );
|
|
48 fgSizer->Add( m_dirPickerWatch, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
|
|
49
|
|
50 m_staticText2 = new wxStaticText( this, wxID_ANY, wxT("Filename"), wxDefaultPosition, wxDefaultSize, 0 );
|
|
51 fgSizer->Add( m_staticText2, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
|
|
52
|
|
53 m_textCtrlFile = new wxTextCtrl( this, ID_FILENAME, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
|
|
54 fgSizer->Add( m_textCtrlFile, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 5 );
|
|
55
|
|
56 m_staticText3 = new wxStaticText( this, wxID_ANY, wxT("Command"), wxDefaultPosition, wxDefaultSize, 0 );
|
|
57 fgSizer->Add( m_staticText3, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
|
|
58
|
|
59 m_textCtrlCommand = new wxTextCtrl( this, ID_COMMAND, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
|
|
60 fgSizer->Add( m_textCtrlCommand, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 5 );
|
|
61
|
1
|
62 m_staticText4 = new wxStaticText( this, wxID_ANY, wxT("wait [sec]"), wxDefaultPosition, wxDefaultSize, 0 );
|
0
|
63 fgSizer->Add( m_staticText4, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
|
1
|
64
|
|
65 m_textCtrlWait = new wxTextCtrl( this, ID_WAIT, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
|
|
66 fgSizer->Add( m_textCtrlWait, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 5 );
|
|
67
|
|
68 m_staticText5 = new wxStaticText( this, wxID_ANY, wxT("MoveTo"), wxDefaultPosition, wxDefaultSize, 0 );
|
|
69 fgSizer->Add( m_staticText5, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
|
0
|
70
|
|
71 m_dirPickerMove = new wxDirPickerCtrl( this, ID_MOVEDIR, wxEmptyString, wxT("Select a folder"), wxDefaultPosition, wxDefaultSize, wxDIRP_DEFAULT_STYLE );
|
|
72 fgSizer->Add( m_dirPickerMove, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
|
|
73
|
1
|
74 m_staticText6 = new wxStaticText( this, wxID_ANY, wxT("copy"), wxDefaultPosition, wxDefaultSize, 0 );
|
|
75 fgSizer->Add( m_staticText6, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
|
|
76
|
|
77 m_checkBoxCopy = new wxCheckBox( this, ID_COPY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
|
|
78 fgSizer->Add( m_checkBoxCopy, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
|
|
79
|
2
|
80 m_staticText7 = new wxStaticText( this, wxID_ANY, wxT("suffix"), wxDefaultPosition, wxDefaultSize, 0 );
|
|
81 fgSizer->Add( m_staticText7, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
|
|
82
|
|
83 wxArrayString suffix;
|
|
84 suffix.Add( wxT("Date & Time") );
|
|
85 m_comboSuffix = new wxComboBox( this, ID_SUFFIX, wxEmptyString, wxDefaultPosition, wxDefaultSize, suffix, 0 );
|
|
86 fgSizer->Add( m_comboSuffix, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
|
|
87
|
1
|
88 m_staticTextDummy1 = new wxStaticText( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
|
|
89 fgSizer->Add( m_staticTextDummy1, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
|
|
90
|
|
91 m_staticTextDummy2 = new wxStaticText( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
|
|
92 fgSizer->Add( m_staticTextDummy2, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
|
|
93
|
2
|
94 m_staticText8 = new wxStaticText( this, wxID_ANY, wxT("Exit"), wxDefaultPosition, wxDefaultSize, 0 );
|
|
95 fgSizer->Add( m_staticText8, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
|
|
96
|
|
97 m_checkBoxExit = new wxCheckBox( this, ID_EXIT, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
|
|
98 fgSizer->Add( m_checkBoxExit, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
|
|
99
|
1
|
100 m_staticTextDummy3 = new wxStaticText( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
|
|
101 fgSizer->Add( m_staticTextDummy3, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
|
0
|
102
|
|
103 m_toggleBtn = new wxToggleButton( this, ID_TOGGLEWATCH, wxT("Start Watching"), wxDefaultPosition, wxDefaultSize, 0 );
|
|
104 fgSizer->Add( m_toggleBtn, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 5 );
|
|
105
|
|
106 this->SetSizer( fgSizer );
|
|
107 this->Layout();
|
|
108
|
|
109 this->Centre( wxBOTH );
|
|
110 }
|
|
111
|
|
112 MyFrame::~MyFrame()
|
|
113 {
|
|
114 delete m_watcher;
|
|
115 }
|
|
116
|
|
117 // Event Table
|
|
118 BEGIN_EVENT_TABLE( MyFrame, wxFrame )
|
|
119 EVT_SIZE( MyFrame::OnWinSize )
|
|
120 EVT_MOVE( MyFrame::OnWinMove )
|
2
|
121 EVT_MENU( ID_MNSETUP, MyFrame::OnSetup )
|
|
122 EVT_MENU( ID_MNSAVE, MyFrame::OnSaveParams )
|
0
|
123 EVT_MENU( ID_MNABOUT, MyFrame::OnAbout )
|
|
124 //EVT_MENU( wxID_EXIT, MyFrame::OnQuit )
|
|
125 EVT_TOGGLEBUTTON( ID_TOGGLEWATCH, MyFrame::OnWatch )
|
|
126 EVT_CLOSE( MyFrame::SaveConfig )
|
|
127 END_EVENT_TABLE()
|
|
128
|
|
129 // Event Handlers & Functions
|
|
130 /* フォルダ監視開始/停止スイッチ */
|
|
131 void MyFrame::OnWatch( wxCommandEvent& WXUNUSED(event) )
|
|
132 {
|
|
133 Connect( wxEVT_FSWATCHER, wxFileSystemWatcherEventHandler( MyFrame::OnFileSystemEvent ) );
|
|
134
|
|
135 static bool enabled = false;
|
|
136 enabled = !enabled;
|
|
137
|
|
138 if ( enabled ) {
|
|
139 m_toggleBtn->SetLabelText( wxT("Stop Watching") );
|
|
140 m_watcher = new wxFileSystemWatcher();
|
|
141 m_watcher->SetOwner( this );
|
|
142
|
|
143 m_watcher->Add( m_dirPickerWatch->GetPath(), wxFSW_EVENT_CREATE );
|
|
144 }
|
|
145 else {
|
|
146 m_toggleBtn->SetLabelText( wxT("Start Watching") );
|
|
147 wxDELETE( m_watcher );
|
|
148 }
|
|
149 }
|
|
150
|
|
151 /* フォルダ監視 */
|
|
152 void MyFrame::OnFileSystemEvent( wxFileSystemWatcherEvent& event )
|
|
153 {
|
1
|
154 wxMilliSleep( 1500 );
|
|
155
|
0
|
156 wxString watchdir = m_dirPickerWatch->GetPath();
|
|
157 wxString filename = m_textCtrlFile->GetValue();
|
|
158 wxString command = m_textCtrlCommand->GetValue();
|
|
159 wxString movedir = m_dirPickerMove->GetPath();
|
|
160
|
|
161 wxString from;
|
|
162 wxString to;
|
|
163
|
|
164 wxString file;
|
|
165 wxDir dir( watchdir );
|
|
166 bool cont = dir.GetFirst( &file, filename, wxDIR_FILES );
|
|
167 while ( cont ) {
|
|
168
|
|
169 if ( !command.IsEmpty() ) {
|
2
|
170 wxExecute( command );
|
1
|
171 wxSleep( 2 );
|
0
|
172 }
|
|
173
|
|
174 if ( !movedir.IsEmpty() ) {
|
|
175 from = watchdir + wxFILE_SEP_PATH + filename;
|
|
176 wxFileName f( from );
|
2
|
177
|
|
178 // ファイル名をどうするか
|
|
179 if ( m_comboSuffix->GetCurrentSelection() == 0 ) {
|
|
180 wxDateTime now = wxDateTime::Now();
|
|
181 to = movedir + wxFILE_SEP_PATH + f.GetName() + wxT("_") + now.Format(wxT("%Y%m%d%H%M%S")) + wxT(".") + f.GetExt();
|
1
|
182 }
|
|
183 else {
|
2
|
184 to = movedir + wxFILE_SEP_PATH + filename;
|
|
185 }
|
|
186
|
|
187 // オリジナルを残すか
|
|
188 if ( m_checkBoxCopy->IsChecked() )
|
|
189 wxCopyFile( from, to, false );
|
|
190 else
|
1
|
191 wxRenameFile( from, to, false );
|
0
|
192 }
|
|
193
|
|
194 cont = dir.GetNext( &file );
|
|
195 }
|
2
|
196 if ( m_checkBoxExit->IsChecked() ) Close( true );
|
|
197 }
|
|
198
|
|
199 /* セットアップ */
|
|
200 void MyFrame::OnSetup( wxCommandEvent& WXUNUSED(event) )
|
|
201 {
|
|
202 wxString dws_dir = wxGetCwd() + wxFILE_SEP_PATH + wxT("dws");
|
|
203 wxFileDialog fd( this, wxT("Open Setup file"), dws_dir , wxEmptyString, wxT("Setup files (*.dws)|*.dws"), wxFD_OPEN|wxFD_FILE_MUST_EXIST );
|
|
204 if ( fd.ShowModal() == wxID_CANCEL ) return; // the user changed idea...
|
|
205
|
|
206 wxFileConfig* config = new wxFileConfig( wxT("MyApp"), wxT("T.Mutoh"), fd.GetPath(), wxEmptyString, wxCONFIG_USE_LOCAL_FILE );
|
|
207
|
|
208 wxString p_folder, p_filename, p_command, p_wait, p_moveto;
|
|
209 bool p_copy, p_exit;
|
|
210 long p_suffix;
|
|
211 config->SetPath( wxT("/Parameter") );
|
|
212 config->Read( wxT("Folder"), &p_folder );
|
|
213 config->Read( wxT("Filename"), &p_filename );
|
|
214 config->Read( wxT("Command"), &p_command );
|
|
215 config->Read( wxT("Wait"), &p_wait );
|
|
216 config->Read( wxT("MoveTo"), &p_moveto );
|
|
217 config->Read( wxT("Copy"), p_copy );
|
|
218 config->Read( wxT("Suffix"), &p_suffix );
|
|
219 config->Read( wxT("Exit"), p_exit );
|
|
220
|
|
221 m_dirPickerWatch->SetPath( p_folder );
|
|
222 m_textCtrlFile->SetValue( p_filename );
|
|
223 m_textCtrlCommand->SetValue( p_command );
|
|
224 m_textCtrlWait->SetValue( p_wait );
|
|
225 m_dirPickerMove->SetPath( p_moveto );
|
|
226 m_checkBoxCopy->SetValue( p_copy );
|
|
227 m_comboSuffix->SetSelection( (int)p_suffix );
|
|
228 m_checkBoxExit->SetValue( p_exit );
|
|
229
|
|
230 delete config;
|
|
231 }
|
|
232 /* パラーメータを保存 */
|
|
233 void MyFrame::OnSaveParams( wxCommandEvent& WXUNUSED(event) )
|
|
234 {
|
|
235 wxString dws_dir = wxGetCwd() + wxFILE_SEP_PATH + wxT("dws");
|
|
236 wxFileDialog fd( this, wxT("Save Setup file"), dws_dir , wxEmptyString, wxT("Setup files (*.dws)|*.dws"), wxFD_SAVE|wxFD_OVERWRITE_PROMPT );
|
|
237 if ( fd.ShowModal() == wxID_CANCEL ) return; // the user changed idea...
|
|
238
|
|
239 wxFileConfig* config = new wxFileConfig( wxT("MyApp"), wxT("T.Mutoh"), fd.GetPath(), wxEmptyString, wxCONFIG_USE_LOCAL_FILE );
|
|
240
|
|
241 config->SetPath( wxT("/Parameter") );
|
|
242 config->Write( wxT("Folder"), m_dirPickerWatch->GetPath() );
|
|
243 config->Write( wxT("Filename"), m_textCtrlFile->GetValue() );
|
|
244 config->Write( wxT("Command"), m_textCtrlCommand->GetValue() );
|
|
245 config->Write( wxT("Wait"), m_textCtrlWait->GetValue() );
|
|
246 config->Write( wxT("MoveTo"), m_dirPickerMove->GetPath() );
|
|
247 config->Write( wxT("Copy"), m_checkBoxCopy->GetValue() );
|
|
248 config->Write( wxT("Suffix"), m_comboSuffix->GetSelection() );
|
|
249 config->Write( wxT("Exit"), m_checkBoxExit->GetValue() );
|
|
250
|
|
251 delete config;
|
0
|
252 }
|
|
253
|
|
254 /* サイズ変更 */
|
|
255 void MyFrame::OnWinSize( wxSizeEvent& event )
|
|
256 {
|
|
257 this->Refresh( true, NULL );
|
|
258 TellLocation();
|
|
259 event.Skip();
|
|
260 }
|
|
261 /* ウィンドウ移動 */
|
|
262 void MyFrame::OnWinMove( wxMoveEvent& WXUNUSED(event) )
|
|
263 {
|
|
264 TellLocation();
|
|
265 return;
|
|
266 }
|
|
267 /* ウィンドウ位置とサイズを表示 */
|
|
268 void MyFrame::TellLocation( void )
|
|
269 {
|
|
270 wxRect r = this->GetRect();
|
|
271 int x = r.GetX();
|
|
272 int y = r.GetY();
|
|
273 int w = r.GetWidth();
|
|
274 int h = r.GetHeight();
|
|
275 //SetStatusText( wxString::Format(wxT("(%d,%d) %dx%d"),x,y,w,h), 3 );
|
|
276 }
|
|
277 /* 終了 */
|
|
278 void MyFrame::OnQuit( wxCommandEvent& WXUNUSED(event) )
|
|
279 {
|
2
|
280 Close( true );
|
0
|
281 }
|
|
282 /* 設定を保存 */
|
|
283 void MyFrame::SaveConfig( wxCloseEvent& WXUNUSED(event) )
|
|
284 {
|
|
285 if ( !IsIconized() && !IsMaximized() ) {
|
|
286 wxGetApp().rect = this->GetRect();
|
|
287 }
|
|
288 Destroy();
|
|
289 }
|
|
290 /* アバウトダイアログ */
|
|
291 void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) )
|
|
292 {
|
|
293 AboutDialog* aboutDlg = new AboutDialog( this, wxID_ANY, wxT("About this Software"), wxDefaultPosition, wxSize(320,280), wxDEFAULT_DIALOG_STYLE|wxSTAY_ON_TOP );
|
|
294 aboutDlg->ShowModal();
|
|
295 }
|
|
296
|