]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/fswatcher/fswatcher.cpp
Create a wrapper file for X11/XKBlib.h header.
[wxWidgets.git] / samples / fswatcher / fswatcher.cpp
index bafc0d2db4bf4b118f7f163c7d99b045e622a025..539a414b4b130004517b467ad50ec23dad643caa 100644 (file)
@@ -17,7 +17,7 @@
     #include "wx/wx.h"
 #endif
 
-#ifndef __WXMSW__
+#ifndef wxHAS_IMAGES_IN_RESOURCES
     #include "../sample.xpm"
 #endif
 
@@ -32,7 +32,9 @@ public:
     MyFrame(const wxString& title);
     virtual ~MyFrame();
 
-    void AddDirectory(const wxString& dir);
+    // Add an entry of the specified type asking the user for the filename if
+    // the one passed to this function is empty.
+    void AddEntry(wxFSWPathType type, wxString filename = wxString());
 
     bool CreateWatcherIfNecessary();
 
@@ -47,6 +49,7 @@ private:
     void OnAbout(wxCommandEvent& event);
 
     void OnAdd(wxCommandEvent& event);
+    void OnAddTree(wxCommandEvent& event);
     void OnRemove(wxCommandEvent& event);
 
     void OnFileSystemEvent(wxFileSystemWatcherEvent& event);
@@ -87,7 +90,7 @@ public:
         if ( m_frame->CreateWatcherIfNecessary() )
         {
             if ( !m_dirToWatch.empty() )
-                m_frame->AddDirectory(m_dirToWatch);
+                m_frame->AddEntry(wxFSWPath_Dir, m_dirToWatch);
         }
     }
 
@@ -144,7 +147,8 @@ MyFrame::MyFrame(const wxString& title)
         MENU_ID_WATCH = 101,
 
         BTN_ID_ADD = 200,
-        BTN_ID_REMOVE = 201,
+        BTN_ID_ADD_TREE,
+        BTN_ID_REMOVE
     };
 
     // ================================================================
@@ -164,7 +168,7 @@ MyFrame::MyFrame(const wxString& title)
 
     // the "About" item should be in the help menu
     wxMenu *menuHelp = new wxMenu;
-    menuHelp->Append(wxID_ABOUT, "&About...\tF1", "Show about dialog");
+    menuHelp->Append(wxID_ABOUT, "&About\tF1", "Show about dialog");
 
     // now append the freshly created menu to the menu bar...
     wxMenuBar *menuBar = new wxMenuBar();
@@ -194,9 +198,11 @@ MyFrame::MyFrame(const wxString& title)
 
     // buttons
     wxButton* buttonAdd = new wxButton(panel, BTN_ID_ADD, "&Add");
+    wxButton* buttonAddTree = new wxButton(panel, BTN_ID_ADD_TREE, "Add &tree");
     wxButton* buttonRemove = new wxButton(panel, BTN_ID_REMOVE, "&Remove");
     wxSizer *btnSizer = new wxGridSizer(2);
     btnSizer->Add(buttonAdd, wxSizerFlags().Center().Border(wxALL));
+    btnSizer->Add(buttonAddTree, wxSizerFlags().Center().Border(wxALL));
     btnSizer->Add(buttonRemove, wxSizerFlags().Center().Border(wxALL));
 
     // and put it all together
@@ -253,6 +259,8 @@ MyFrame::MyFrame(const wxString& title)
     // buttons
     Connect(BTN_ID_ADD, wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(MyFrame::OnAdd));
+    Connect(BTN_ID_ADD_TREE, wxEVT_COMMAND_BUTTON_CLICKED,
+            wxCommandEventHandler(MyFrame::OnAddTree));
     Connect(BTN_ID_REMOVE, wxEVT_COMMAND_BUTTON_CLICKED,
             wxCommandEventHandler(MyFrame::OnRemove));
 
@@ -318,29 +326,54 @@ void MyFrame::OnWatch(wxCommandEvent& event)
 
 void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event))
 {
-    wxCHECK_RET(m_watcher, "Watcher not initialized");
-
-    // TODO account for adding the files as well
-    const wxString& dir = wxDirSelector("Choose a folder to watch", "",
-                                    wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST);
-    if ( dir.empty() )
-        return;
+    AddEntry(wxFSWPath_Dir);
+}
 
-    AddDirectory(dir);
+void MyFrame::OnAddTree(wxCommandEvent& WXUNUSED(event))
+{
+    AddEntry(wxFSWPath_Tree);
 }
 
-void MyFrame::AddDirectory(const wxString& dir)
+void MyFrame::AddEntry(wxFSWPathType type, wxString filename)
 {
-    wxLogDebug("Adding directory: '%s'", dir);
+    if ( filename.empty() )
+    {
+        // TODO account for adding the files as well
+        filename = wxDirSelector("Choose a folder to watch", "",
+                                 wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST);
+        if ( filename.empty() )
+            return;
+    }
+
+    wxCHECK_RET(m_watcher, "Watcher not initialized");
+
+    wxLogDebug("Adding %s: '%s'",
+               filename,
+               type == wxFSWPath_Dir ? "directory" : "directory tree");
 
-    if (!m_watcher->Add(wxFileName::DirName(dir), wxFSW_EVENT_ALL))
+    bool ok = false;
+    switch ( type )
     {
-        wxLogError("Error adding '%s' to watched paths", dir);
+        case wxFSWPath_Dir:
+            ok = m_watcher->Add(wxFileName::DirName(filename));
+            break;
+
+        case wxFSWPath_Tree:
+            ok = m_watcher->AddTree(wxFileName::DirName(filename));
+            break;
+
+        case wxFSWPath_File:
+        case wxFSWPath_None:
+            wxFAIL_MSG( "Unexpected path type." );
     }
-    else
+
+    if (!ok)
     {
-        m_filesList->InsertItem(m_filesList->GetItemCount(), dir);
+        wxLogError("Error adding '%s' to watched paths", filename);
+        return;
     }
+
+    m_filesList->InsertItem(m_filesList->GetItemCount(), filename);
 }
 
 void MyFrame::OnRemove(wxCommandEvent& WXUNUSED(event))
@@ -366,7 +399,7 @@ void MyFrame::OnRemove(wxCommandEvent& WXUNUSED(event))
 void MyFrame::OnFileSystemEvent(wxFileSystemWatcherEvent& event)
 {
     // TODO remove when code is rock-solid
-    wxLogDebug(wxTRACE_FSWATCHER, "*** %s ***", event.ToString());
+    wxLogTrace(wxTRACE_FSWATCHER, "*** %s ***", event.ToString());
     LogEvent(event);
 }