Allow specifying the directory to watch on command line of fswatcher sample.
This makes it more convenient to run the sample repeatedly as the directory to watch can be specified only once instead of having to choose it interactively after the sample startup every time. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65907 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -23,6 +23,7 @@
|
|||||||
|
|
||||||
#include "wx/fswatcher.h"
|
#include "wx/fswatcher.h"
|
||||||
#include "wx/listctrl.h"
|
#include "wx/listctrl.h"
|
||||||
|
#include "wx/cmdline.h"
|
||||||
|
|
||||||
// Define a new frame type: this is going to be our main frame
|
// Define a new frame type: this is going to be our main frame
|
||||||
class MyFrame : public wxFrame
|
class MyFrame : public wxFrame
|
||||||
@@ -31,9 +32,12 @@ public:
|
|||||||
MyFrame(const wxString& title);
|
MyFrame(const wxString& title);
|
||||||
virtual ~MyFrame();
|
virtual ~MyFrame();
|
||||||
|
|
||||||
|
void AddDirectory(const wxString& dir);
|
||||||
|
|
||||||
|
bool CreateWatcherIfNecessary();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// file system watcher creation
|
// file system watcher creation
|
||||||
void OnEventLoopEnter();
|
|
||||||
void CreateWatcher();
|
void CreateWatcher();
|
||||||
|
|
||||||
// event handlers
|
// event handlers
|
||||||
@@ -52,8 +56,6 @@ private:
|
|||||||
wxListView *m_filesList; // list of watched paths
|
wxListView *m_filesList; // list of watched paths
|
||||||
wxFileSystemWatcher* m_watcher; // file system watcher
|
wxFileSystemWatcher* m_watcher; // file system watcher
|
||||||
|
|
||||||
friend class MyApp;
|
|
||||||
|
|
||||||
const static wxString LOG_FORMAT; // how to format events
|
const static wxString LOG_FORMAT; // how to format events
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -66,6 +68,9 @@ public:
|
|||||||
// 'Main program' equivalent: the program execution "starts" here
|
// 'Main program' equivalent: the program execution "starts" here
|
||||||
virtual bool OnInit()
|
virtual bool OnInit()
|
||||||
{
|
{
|
||||||
|
if ( !wxApp::OnInit() )
|
||||||
|
return false;
|
||||||
|
|
||||||
wxLog::AddTraceMask("EventSource");
|
wxLog::AddTraceMask("EventSource");
|
||||||
wxLog::AddTraceMask(wxTRACE_FSWATCHER);
|
wxLog::AddTraceMask(wxTRACE_FSWATCHER);
|
||||||
|
|
||||||
@@ -79,11 +84,37 @@ public:
|
|||||||
// create the file system watcher here, because it needs an active loop
|
// create the file system watcher here, because it needs an active loop
|
||||||
virtual void OnEventLoopEnter(wxEventLoopBase* WXUNUSED(loop))
|
virtual void OnEventLoopEnter(wxEventLoopBase* WXUNUSED(loop))
|
||||||
{
|
{
|
||||||
m_frame->OnEventLoopEnter();
|
if ( m_frame->CreateWatcherIfNecessary() )
|
||||||
|
{
|
||||||
|
if ( !m_dirToWatch.empty() )
|
||||||
|
m_frame->AddDirectory(m_dirToWatch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void OnInitCmdLine(wxCmdLineParser& parser)
|
||||||
|
{
|
||||||
|
wxApp::OnInitCmdLine(parser);
|
||||||
|
parser.AddParam("directory to watch",
|
||||||
|
wxCMD_LINE_VAL_STRING,
|
||||||
|
wxCMD_LINE_PARAM_OPTIONAL);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual bool OnCmdLineParsed(wxCmdLineParser& parser)
|
||||||
|
{
|
||||||
|
if ( !wxApp::OnCmdLineParsed(parser) )
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if ( parser.GetParamCount() )
|
||||||
|
m_dirToWatch = parser.GetParam();
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MyFrame *m_frame;
|
MyFrame *m_frame;
|
||||||
|
|
||||||
|
// The directory to watch if specified on the command line.
|
||||||
|
wxString m_dirToWatch;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create a new application object: this macro will allow wxWidgets to create
|
// Create a new application object: this macro will allow wxWidgets to create
|
||||||
@@ -235,14 +266,16 @@ MyFrame::~MyFrame()
|
|||||||
delete m_watcher;
|
delete m_watcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MyFrame::OnEventLoopEnter()
|
bool MyFrame::CreateWatcherIfNecessary()
|
||||||
{
|
{
|
||||||
if (m_watcher)
|
if (m_watcher)
|
||||||
return;
|
return false;
|
||||||
|
|
||||||
CreateWatcher();
|
CreateWatcher();
|
||||||
Connect(wxEVT_FSWATCHER,
|
Connect(wxEVT_FSWATCHER,
|
||||||
wxFileSystemWatcherEventHandler(MyFrame::OnFileSystemEvent));
|
wxFileSystemWatcherEventHandler(MyFrame::OnFileSystemEvent));
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MyFrame::CreateWatcher()
|
void MyFrame::CreateWatcher()
|
||||||
@@ -293,6 +326,11 @@ void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event))
|
|||||||
if ( dir.empty() )
|
if ( dir.empty() )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
AddDirectory(dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MyFrame::AddDirectory(const wxString& dir)
|
||||||
|
{
|
||||||
wxLogDebug("Adding directory: '%s'", dir);
|
wxLogDebug("Adding directory: '%s'", dir);
|
||||||
|
|
||||||
if (!m_watcher->Add(wxFileName::DirName(dir), wxFSW_EVENT_ALL))
|
if (!m_watcher->Add(wxFileName::DirName(dir), wxFSW_EVENT_ALL))
|
||||||
|
Reference in New Issue
Block a user