view src/myframe.cpp @ 3:eea479fc2a28 default tip

redesign form.
author pyon@macmini
date Sun, 25 Nov 2012 12:59:11 +0900
parents deaf3f2d54b2
children
line wrap: on
line source

// Filename   : myframe.cpp
// Last Change: 25-Nov-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));

    // Menubar here
	m_menubar = new wxMenuBar( 0 );
	m_menuFile = new wxMenu();

	wxMenuItem* m_menuItemSetup = new wxMenuItem( m_menuFile, ID_MNSETUP, wxString( wxT("Setup") ) , wxEmptyString, wxITEM_NORMAL );
	m_menuFile->Append( m_menuItemSetup );
	
	wxMenuItem* m_menuItemSave = new wxMenuItem( m_menuFile, ID_MNSAVE, wxString( wxT("Save") ) , wxEmptyString, wxITEM_NORMAL );
	m_menuFile->Append( m_menuItemSave );
	
	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("File") ); 
	
	this->SetMenuBar( m_menubar );
	
    // Controls here
	wxBoxSizer* bSizerTop = new wxBoxSizer( wxVERTICAL );
	
	wxGridBagSizer* gbSizerWatchDir = new wxGridBagSizer( 0, 0 );
	gbSizerWatchDir->SetFlexibleDirection( wxBOTH );
	gbSizerWatchDir->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
	
	m_searchCtrlFile = new wxSearchCtrl( this, wxID_ANY, wxT("Input Filename"), wxDefaultPosition, wxDefaultSize, 0 );
	#ifndef __WXMAC__
	m_searchCtrlFile->ShowSearchButton( true );
	#endif
	m_searchCtrlFile->ShowCancelButton( false );
	gbSizerWatchDir->Add( m_searchCtrlFile, wxGBPosition( 0, 0 ), wxGBSpan( 1, 1 ), wxALL, 5 );
	
	m_dirPickerWatchDir = new wxDirPickerCtrl( this, wxID_ANY, wxEmptyString, wxT("Select a folder"), wxDefaultPosition, wxDefaultSize, wxDIRP_DEFAULT_STYLE );
	gbSizerWatchDir->Add( m_dirPickerWatchDir, wxGBPosition( 0, 1 ), wxGBSpan( 1, 1 ), wxALL, 5 );
	
	m_listCtrlWatchDir = new wxListCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT );
	gbSizerWatchDir->Add( m_listCtrlWatchDir, wxGBPosition( 1, 0 ), wxGBSpan( 1, 2 ), wxALL, 5 );
	
	bSizerTop->Add( gbSizerWatchDir, 0, wxEXPAND, 5 );
	
	wxStaticBoxSizer* sbSizerFound = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxT("If Find File") ), wxVERTICAL );
	
	wxStaticBoxSizer* sbSizerMove = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxT("Move/Copy") ), wxVERTICAL );
	
	wxBoxSizer* bSizerMove = new wxBoxSizer( wxHORIZONTAL );
	
	wxString m_choiceMoveChoices[] = { wxT("Nop"), wxT("Move"), wxT("Copy") };
	int m_choiceMoveNChoices = sizeof( m_choiceMoveChoices ) / sizeof( wxString );
	m_choiceMove = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, m_choiceMoveNChoices, m_choiceMoveChoices, 0 );
	m_choiceMove->SetSelection( 0 );
	bSizerMove->Add( m_choiceMove, 0, wxALL, 5 );
	
	m_staticText0 = new wxStaticText( this, wxID_ANY, wxT("Suffix"), wxDefaultPosition, wxDefaultSize, 0 );
	bSizerMove->Add( m_staticText0, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	wxString m_choiceSuffixChoices[] = { wxT("NoChange"), wxT("Date & Time"), wxT("Date"), wxT("Time") };
	int m_choiceSuffixNChoices = sizeof( m_choiceSuffixChoices ) / sizeof( wxString );
	m_choiceSuffix = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, m_choiceSuffixNChoices, m_choiceSuffixChoices, 0 );
	m_choiceSuffix->SetSelection( 0 );
	bSizerMove->Add( m_choiceSuffix, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	m_dirPickerMoveTo = new wxDirPickerCtrl( this, wxID_ANY, wxEmptyString, wxT("Select a folder"), wxDefaultPosition, wxDefaultSize, wxDIRP_DEFAULT_STYLE );
	bSizerMove->Add( m_dirPickerMoveTo, 0, wxALL|wxEXPAND, 5 );
	
	sbSizerMove->Add( bSizerMove, 1, wxEXPAND, 5 );
	
	m_listCtrlMove = new wxListCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLC_REPORT );
	sbSizerMove->Add( m_listCtrlMove, 0, wxALL|wxEXPAND, 5 );
	
	sbSizerFound->Add( sbSizerMove, 1, wxEXPAND, 5 );
	
	wxStaticBoxSizer* sbSizerCmd = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxT("Command") ), wxVERTICAL );
	
	wxFlexGridSizer* fgSizerCmd = new wxFlexGridSizer( 3, 3, 0, 0 );
	fgSizerCmd->SetFlexibleDirection( wxBOTH );
	fgSizerCmd->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
	
	m_staticTextCmd0 = new wxStaticText( this, wxID_ANY, wxT("Command 0"), wxDefaultPosition, wxDefaultSize, 0 );
	fgSizerCmd->Add( m_staticTextCmd0, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	m_textCtrlCmd0 = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
	fgSizerCmd->Add( m_textCtrlCmd0, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	wxString m_choiceCmd0Choices[] = { wxT("After"), wxT("Before") };
	int m_choiceCmd0NChoices = sizeof( m_choiceCmd0Choices ) / sizeof( wxString );
	m_choiceCmd0 = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, m_choiceCmd0NChoices, m_choiceCmd0Choices, 0 );
	m_choiceCmd0->SetSelection( 0 );
	fgSizerCmd->Add( m_choiceCmd0, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	m_staticTextCmd1 = new wxStaticText( this, wxID_ANY, wxT("Command 1"), wxDefaultPosition, wxDefaultSize, 0 );
	fgSizerCmd->Add( m_staticTextCmd1, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	m_textCtrlCmd1 = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
	fgSizerCmd->Add( m_textCtrlCmd1, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	wxString m_choiceCmd1Choices[] = { wxT("After"), wxT("Before") };
	int m_choiceCmd1NChoices = sizeof( m_choiceCmd1Choices ) / sizeof( wxString );
	m_choiceCmd1 = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, m_choiceCmd1NChoices, m_choiceCmd1Choices, 0 );
	m_choiceCmd1->SetSelection( 0 );
	fgSizerCmd->Add( m_choiceCmd1, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	m_staticTextCmd2 = new wxStaticText( this, wxID_ANY, wxT("Command 2"), wxDefaultPosition, wxDefaultSize, 0 );
	fgSizerCmd->Add( m_staticTextCmd2, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	m_textCtrlCmd2 = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
	fgSizerCmd->Add( m_textCtrlCmd2, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	wxString m_choiceCmd2Choices[] = { wxT("After"), wxT("Before") };
	int m_choiceCmd2NChoices = sizeof( m_choiceCmd2Choices ) / sizeof( wxString );
	m_choiceCmd2 = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, m_choiceCmd2NChoices, m_choiceCmd2Choices, 0 );
	m_choiceCmd2->SetSelection( 0 );
	fgSizerCmd->Add( m_choiceCmd2, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	sbSizerCmd->Add( fgSizerCmd, 1, wxEXPAND, 5 );
	
	sbSizerFound->Add( sbSizerCmd, 0, wxEXPAND, 5 );
	
	wxString m_choiceFoundChoices[] = { wxT("Continue"), wxT("Stop Watching"), wxT("Exit Program") };
	int m_choiceFoundNChoices = sizeof( m_choiceFoundChoices ) / sizeof( wxString );
	m_choiceFound = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, m_choiceFoundNChoices, m_choiceFoundChoices, 0 );
	m_choiceFound->SetSelection( 0 );
	sbSizerFound->Add( m_choiceFound, 0, wxALL|wxALIGN_RIGHT, 5 );
	
	bSizerTop->Add( sbSizerFound, 0, wxEXPAND, 5 );
	
	wxStaticBoxSizer* sbSizerNotFound = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxT("Else") ), wxHORIZONTAL );
	
	m_textCtrlWait = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
	sbSizerNotFound->Add( m_textCtrlWait, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	m_staticText1 = new wxStaticText( this, wxID_ANY, wxT("[sec] after"), wxDefaultPosition, wxDefaultSize, 0 );
	sbSizerNotFound->Add( m_staticText1, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	wxString m_choiceNotFoundChoices[] = { wxT("Stop Watching"), wxT("Exit Program") };
	int m_choiceNotFoundNChoices = sizeof( m_choiceNotFoundChoices ) / sizeof( wxString );
	m_choiceNotFound = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, m_choiceNotFoundNChoices, m_choiceNotFoundChoices, 0 );
	m_choiceNotFound->SetSelection( 0 );
	sbSizerNotFound->Add( m_choiceNotFound, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5 );
	
	bSizerTop->Add( sbSizerNotFound, 0, wxEXPAND, 5 );
	
	m_toggleBtn = new wxToggleButton( this, ID_TGLWATCH, wxT("Start Watching"), wxDefaultPosition, wxDefaultSize, 0 );
	m_toggleBtn->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW ) );
	
	bSizerTop->Add( m_toggleBtn, 0, wxALL|wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL, 5 );
	
	this->SetSizer( bSizerTop );
	this->Layout();

	this->Centre( wxBOTH );
}

MyFrame::~MyFrame()
{
}

// Event Table
BEGIN_EVENT_TABLE( MyFrame, wxFrame )
    EVT_MENU( ID_MNSETUP, MyFrame::OnSetup )
    EVT_MENU( ID_MNSAVE, MyFrame::OnSaveParams )
    EVT_MENU( ID_MNABOUT, MyFrame::OnAbout )
//    EVT_MENU( wxID_EXIT, MyFrame::OnQuit )
    EVT_TOGGLEBUTTON( ID_TGLWATCH, MyFrame::OnWatch )
    EVT_SIZE( MyFrame::OnWinSize )
    EVT_MOVE( MyFrame::OnWinMove )
    EVT_CLOSE( MyFrame::SaveConfig )
END_EVENT_TABLE()

// Event Handlers & Functions
/* $B%U%)%k%@4F;k3+;O(B/$BDd;_%9%$%C%A(B */
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_dirPickerWatchDir->GetPath(), wxFSW_EVENT_CREATE );
    }
    else {
        m_toggleBtn->SetLabelText( wxT("Start Watching") );
        wxDELETE( m_watcher );
    }
}

/* $B%U%)%k%@4F;k(B */
void MyFrame::OnFileSystemEvent( wxFileSystemWatcherEvent& event )
{
    wxMilliSleep( 1500 );

    wxString watchdir = m_dirPickerWatchDir->GetPath();
    wxString filename = m_searchCtrlFile->GetValue();
    wxString command0 = m_textCtrlCmd0->GetValue();
    wxString command1 = m_textCtrlCmd1->GetValue();
    wxString command2 = m_textCtrlCmd2->GetValue();
    wxString movedir  = m_dirPickerMoveTo->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 );
            wxSleep( 2 );
        }

        if ( !movedir.IsEmpty() ) {
            from = watchdir + wxFILE_SEP_PATH + filename;
            wxFileName f( from );

            // $B%U%!%$%kL>$r$I$&$9$k$+(B
            if ( m_comboSuffix->GetCurrentSelection() == 0 ) { 
                wxDateTime now = wxDateTime::Now();
                to = movedir + wxFILE_SEP_PATH + f.GetName() + wxT("_") + now.Format(wxT("%Y%m%d%H%M%S")) + wxT(".") + f.GetExt();
            }
            else {
                to = movedir + wxFILE_SEP_PATH + filename;
            }

            // $B%*%j%8%J%k$r;D$9$+(B
            if ( m_checkBoxCopy->IsChecked() )
                wxCopyFile( from, to, false );
            else
                wxRenameFile( from, to, false );
        }

        cont = dir.GetNext( &file );
    */
    }
    //if ( m_checkBoxExit->IsChecked() ) Close( true );
}

/* $B%;%C%H%"%C%W(B */
void MyFrame::OnSetup( wxCommandEvent& WXUNUSED(event) )
{
    wxString dws_dir = wxGetCwd() + wxFILE_SEP_PATH + wxT("dws");
    wxFileDialog fd( this, wxT("Open Setup file"), dws_dir , wxEmptyString, wxT("Setup files (*.dws)|*.dws"), wxFD_OPEN|wxFD_FILE_MUST_EXIST );
    if ( fd.ShowModal() == wxID_CANCEL ) return;     // the user changed idea...     

    wxFileConfig* config = new wxFileConfig( wxT("MyApp"), wxT("T.Mutoh"), fd.GetPath(), wxEmptyString, wxCONFIG_USE_LOCAL_FILE );

    wxString p_watchdir, p_filename, p_moveto, p_command0, p_command1, p_command2, p_wait;
    long p_move, p_suffix, p_found, p_notfound;
    config->SetPath( wxT("/Parameter") );
    config->Read( wxT("Filename"), &p_filename );
    config->Read( wxT("Watchdir"), &p_watchdir );
    config->Read( wxT("Move"),     &p_move     );
    config->Read( wxT("Suffix"),   &p_suffix   );
    config->Read( wxT("MoveTo"),   &p_moveto   );
    config->Read( wxT("Command0"), &p_command0 );
    config->Read( wxT("Command1"), &p_command1 );
    config->Read( wxT("Command2"), &p_command2 );
    config->Read( wxT("Found"),    &p_found    );
    config->Read( wxT("Wait"),     &p_wait     );
    config->Read( wxT("Notfound"), &p_notfound );

    m_dirPickerWatchDir->SetPath( p_watchdir );
    m_searchCtrlFile->SetValue( p_filename );
    m_choiceMove->SetSelection( p_move );
    m_choiceSuffix->SetSelection( p_suffix );
    m_dirPickerMoveTo->SetPath( p_moveto );
    m_textCtrlCmd0->SetValue( p_command0 );
    m_textCtrlCmd1->SetValue( p_command1 );
    m_textCtrlCmd2->SetValue( p_command2 );
    m_choiceFound->SetSelection( p_found );
    m_textCtrlWait->SetValue( p_wait );
    m_choiceNotFound->SetSelection( p_notfound );

    delete config;
}
/* $B%Q%i!<%a!<%?$rJ]B8(B */
void MyFrame::OnSaveParams(  wxCommandEvent& WXUNUSED(event) )
{
    wxString dws_dir = wxGetCwd() + wxFILE_SEP_PATH + wxT("dws");
    wxFileDialog fd( this, wxT("Save Setup file"), dws_dir , wxEmptyString, wxT("Setup files (*.dws)|*.dws"), wxFD_SAVE|wxFD_OVERWRITE_PROMPT );
    if ( fd.ShowModal() == wxID_CANCEL ) return;     // the user changed idea...     

    wxFileConfig* config = new wxFileConfig( wxT("MyApp"), wxT("T.Mutoh"), fd.GetPath(), wxEmptyString, wxCONFIG_USE_LOCAL_FILE );

    config->SetPath( wxT("/Parameter") );
    config->Write( wxT("Filename"), m_searchCtrlFile->GetValue()     );
    config->Write( wxT("Watchdir"), m_dirPickerWatchDir->GetPath()   );
    config->Write( wxT("Move"),     m_choiceMove->GetSelection()     );
    config->Write( wxT("Suffix"),   m_choiceSuffix->GetSelection()   );
    config->Write( wxT("MoveTo"),   m_dirPickerMoveTo->GetPath()     );
    config->Write( wxT("Command0"), m_textCtrlCmd0->GetValue()       );
    config->Write( wxT("Command1"), m_textCtrlCmd1->GetValue()       );
    config->Write( wxT("Command2"), m_textCtrlCmd2->GetValue()       );
    config->Write( wxT("Found"),    m_choiceFound->GetSelection()    );
    config->Write( wxT("Wait"),     m_textCtrlWait->GetValue()       );
    config->Write( wxT("Notfound"), m_choiceNotFound->GetSelection() );

    delete config;
}

/* $B%5%$%:JQ99(B */
void MyFrame::OnWinSize( wxSizeEvent& event )
{
    this->Refresh( true, NULL );
    TellLocation();
    event.Skip();
}
/* $B%&%#%s%I%&0\F0(B */
void MyFrame::OnWinMove( wxMoveEvent& WXUNUSED(event) )
{
    TellLocation();
    return;
}
/* $B%&%#%s%I%&0LCV$H%5%$%:$rI=<((B */
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 );
}
/* $B=*N;(B */
void MyFrame::OnQuit( wxCommandEvent& WXUNUSED(event) )
{
    Close( true );
}
/* $B@_Dj$rJ]B8(B */
void MyFrame::SaveConfig( wxCloseEvent& WXUNUSED(event) )
{
    if ( !IsIconized() && !IsMaximized() ) {
        wxGetApp().rect = this->GetRect();
    }
    Destroy();
}
/* $B%"%P%&%H%@%$%"%m%0(B */
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();
}