diff src/myframe.cpp @ 0:7c3921bf511e

Beta2
author pyon@macmini
date Sat, 20 Oct 2012 16:07:42 +0900
parents
children 20018a6f69a9
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/myframe.cpp	Sat Oct 20 16:07:42 2012 +0900
@@ -0,0 +1,190 @@
+// Filename   : myframe.cpp
+// Last Change: 20-Oct-2012.
+//
+
+#include "main.h"
+#include "myframe.h"
+#include "about.h"
+
+// resources
+#if !defined(__WXMSW__) && !defined(__WXPM__)
+    #include "sample.xpm"
+#endif
+
+MyFrame::MyFrame( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) 
+    : wxFrame( parent, id, title, pos, size, style )
+{
+	this->SetSizeHints( wxDefaultSize, wxDefaultSize );
+    this->SetBackgroundColour( wxColour(wxT("WHEAT")) );
+    // set the frame icon
+    SetIcon(wxICON(sample));
+	
+    // メニューバー Menu
+	m_menubar = new wxMenuBar();
+	m_menuFile = new wxMenu();
+
+	wxMenuItem* m_menuItemAbout = new wxMenuItem( m_menuFile, ID_MNABOUT, wxString( wxT("&About...\tF1") ) , wxT("Show about dialog"), wxITEM_NORMAL );
+	m_menuFile->Append( m_menuItemAbout );
+	
+	m_menubar->Append( m_menuFile, wxT("ファイル(&F)") ); 
+	
+	this->SetMenuBar( m_menubar );
+	
+    // 
+	wxFlexGridSizer* fgSizer = new wxFlexGridSizer( 0, 2, 0, 0 );
+	fgSizer->SetFlexibleDirection( wxBOTH );
+	fgSizer->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
+	
+	m_staticText1 = new wxStaticText( this, wxID_ANY, wxT("Folder"), wxDefaultPosition, wxDefaultSize, 0 );
+	fgSizer->Add( m_staticText1, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
+	
+	m_dirPickerWatch = new wxDirPickerCtrl( this, ID_WATCHDIR, wxEmptyString, wxT("Select a folder"), wxDefaultPosition, wxDefaultSize, wxDIRP_DEFAULT_STYLE );
+	fgSizer->Add( m_dirPickerWatch, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
+	
+	m_staticText2 = new wxStaticText( this, wxID_ANY, wxT("Filename"), wxDefaultPosition, wxDefaultSize, 0 );
+	fgSizer->Add( m_staticText2, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
+	
+	m_textCtrlFile = new wxTextCtrl( this, ID_FILENAME, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
+	fgSizer->Add( m_textCtrlFile, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 5 );
+	
+	m_staticText3 = new wxStaticText( this, wxID_ANY, wxT("Command"), wxDefaultPosition, wxDefaultSize, 0 );
+	fgSizer->Add( m_staticText3, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
+	
+	m_textCtrlCommand = new wxTextCtrl( this, ID_COMMAND, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 ); 
+	fgSizer->Add( m_textCtrlCommand, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 5 );
+	
+	m_staticText4 = new wxStaticText( this, wxID_ANY, wxT("MoveTo"), wxDefaultPosition, wxDefaultSize, 0 );
+	fgSizer->Add( m_staticText4, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT, 5 );
+	
+	m_dirPickerMove = new wxDirPickerCtrl( this, ID_MOVEDIR, wxEmptyString, wxT("Select a folder"), wxDefaultPosition, wxDefaultSize, wxDIRP_DEFAULT_STYLE );
+	fgSizer->Add( m_dirPickerMove, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
+	
+	m_staticText5 = new wxStaticText( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
+	fgSizer->Add( m_staticText5, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
+	
+	m_toggleBtn = new wxToggleButton( this, ID_TOGGLEWATCH, wxT("Start Watching"), wxDefaultPosition, wxDefaultSize, 0 );
+	fgSizer->Add( m_toggleBtn, 0, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 5 );
+	
+	this->SetSizer( fgSizer );
+	this->Layout();
+	
+	this->Centre( wxBOTH );
+
+    m_dirPickerWatch->SetPath( wxT("/Users/takayuki/wx/dw_test" ) );
+    m_dirPickerMove->SetPath( wxT("/Users/takayuki/wx/dw_test/log" ) );
+    m_textCtrlFile->SetValue( wxT("test.file") );
+}
+
+MyFrame::~MyFrame()
+{
+    delete m_watcher;
+}
+
+// Event Table
+BEGIN_EVENT_TABLE( MyFrame, wxFrame )
+    EVT_SIZE( MyFrame::OnWinSize )
+    EVT_MOVE( MyFrame::OnWinMove )
+    EVT_MENU( ID_MNABOUT, MyFrame::OnAbout )
+    //EVT_MENU( wxID_EXIT,          MyFrame::OnQuit )
+    EVT_TOGGLEBUTTON( ID_TOGGLEWATCH, MyFrame::OnWatch )
+    EVT_CLOSE( MyFrame::SaveConfig )
+END_EVENT_TABLE()
+
+// Event Handlers & Functions
+/* フォルダ監視開始/停止スイッチ */
+void MyFrame::OnWatch( wxCommandEvent& WXUNUSED(event) )
+{
+    Connect( wxEVT_FSWATCHER, wxFileSystemWatcherEventHandler( MyFrame::OnFileSystemEvent ) );
+
+    static bool enabled = false;
+    enabled = !enabled;
+
+    if ( enabled ) {
+        m_toggleBtn->SetLabelText( wxT("Stop Watching") );
+        m_watcher = new wxFileSystemWatcher();
+        m_watcher->SetOwner( this );
+
+        m_watcher->Add( m_dirPickerWatch->GetPath(), wxFSW_EVENT_CREATE );
+    }
+    else {
+        m_toggleBtn->SetLabelText( wxT("Start Watching") );
+        wxDELETE( m_watcher );
+    }
+}
+
+/* フォルダ監視 */
+void MyFrame::OnFileSystemEvent( wxFileSystemWatcherEvent& event )
+{
+    //wxMessageBox(wxT(""));
+    wxString watchdir = m_dirPickerWatch->GetPath();
+    wxString filename = m_textCtrlFile->GetValue();
+    wxString command  = m_textCtrlCommand->GetValue();
+    wxString movedir  = m_dirPickerMove->GetPath();
+
+    wxString from;
+    wxString to;
+
+    wxString file;
+    wxDir dir( watchdir );
+    bool cont = dir.GetFirst( &file, filename, wxDIR_FILES );
+    while ( cont ) {
+
+        if ( !command.IsEmpty() ) {
+            wxExecute( command );
+        }
+
+        if ( !movedir.IsEmpty() ) {
+            from = watchdir + wxFILE_SEP_PATH + filename;
+            wxFileName f( from );
+            wxDateTime now = wxDateTime::Now();
+            to = movedir + wxFILE_SEP_PATH + f.GetName() + wxT("_") + now.Format(wxT("%Y%m%d%H%M%S")) + wxT(".") + f.GetExt();
+            wxRenameFile( from, to, false );
+        }
+
+        cont = dir.GetNext( &file );
+    }
+}
+
+/* サイズ変更 */
+void MyFrame::OnWinSize( wxSizeEvent& event )
+{
+    this->Refresh( true, NULL );
+    TellLocation();
+    event.Skip();
+}
+/* ウィンドウ移動 */
+void MyFrame::OnWinMove( wxMoveEvent& WXUNUSED(event) )
+{
+    TellLocation();
+    return;
+}
+/* ウィンドウ位置とサイズを表示 */
+void MyFrame::TellLocation( void ) 
+{
+    wxRect r = this->GetRect();
+    int x = r.GetX();
+    int y = r.GetY();
+    int w = r.GetWidth();
+    int h = r.GetHeight();
+    //SetStatusText( wxString::Format(wxT("(%d,%d) %dx%d"),x,y,w,h), 3 );
+}
+/* 終了 */
+void MyFrame::OnQuit( wxCommandEvent& WXUNUSED(event) )
+{
+    Close(true);
+}
+/* 設定を保存 */
+void MyFrame::SaveConfig( wxCloseEvent& WXUNUSED(event) )
+{
+    if ( !IsIconized() && !IsMaximized() ) {
+        wxGetApp().rect = this->GetRect();
+    }
+    Destroy();
+}
+/* アバウトダイアログ */
+void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) )
+{
+    AboutDialog* aboutDlg = new AboutDialog( this, wxID_ANY, wxT("About this Software"), wxDefaultPosition, wxSize(320,280), wxDEFAULT_DIALOG_STYLE|wxSTAY_ON_TOP ); 
+    aboutDlg->ShowModal();
+}
+