Skip to content

Instantly share code, notes, and snippets.

@PBfordev
Created October 4, 2021 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PBfordev/7a4865bf9ec7be14e5c716c5d81c5385 to your computer and use it in GitHub Desktop.
Save PBfordev/7a4865bf9ec7be14e5c716c5d81c5385 to your computer and use it in GitHub Desktop.
Simple program to test wxDesktopEnv
#include <wx/wx.h>
#include <wx/desktopenv.h>
class MyFrame : public wxFrame
{
public:
MyFrame() : wxFrame(nullptr, wxID_ANY, "RecycleBin restore Test")
{
wxPanel* mainPanel = new wxPanel(this);
wxBoxSizer* mainPanelSizer = new wxBoxSizer(wxVERTICAL);
wxBoxSizer* buttonSizer = new wxBoxSizer(wxHORIZONTAL);
wxButton* button;
button = new wxButton(mainPanel, wxID_ANY, "Refresh");
button->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &MyFrame::OnRefresh, this);
buttonSizer->Add(button, wxSizerFlags().Expand().Border());
m_ctlSortPaths = new wxCheckBox(mainPanel, wxID_ANY, "Sort (press Refresh to reflect the change)");
buttonSizer->Add(m_ctlSortPaths, wxSizerFlags().Expand().Border());
buttonSizer->AddStretchSpacer(1);
button = new wxButton(mainPanel, wxID_ANY, "Restore");
button->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &MyFrame::OnRestore, this);
buttonSizer->Add(button, wxSizerFlags().Expand().Border());
mainPanelSizer->Add(buttonSizer, wxSizerFlags().Expand());
m_ctlPathList = new wxListBox(mainPanel, wxID_ANY);
RefreshFiles();
mainPanelSizer->Add(m_ctlPathList, wxSizerFlags().Proportion(1).Expand().Border());
mainPanel->SetSizer(mainPanelSizer);
SetClientSize(FromDIP(wxSize(800, 600)));
}
protected:
wxCheckBox* m_ctlSortPaths;
wxListBox* m_ctlPathList;
void RefreshFiles()
{
std::vector<wxString> paths;
if ( !wxDesktopEnv::GetFilesInRecycleBin(paths) )
{
m_ctlPathList->Clear();
return;
}
wxArrayString pathsAS;
for ( const auto& p : paths )
pathsAS.push_back(p);
if ( m_ctlSortPaths->GetValue() )
pathsAS.Sort(wxCmpNatural);
m_ctlPathList->Set(pathsAS);
}
void OnRefresh(wxCommandEvent&)
{
RefreshFiles();
}
void OnRestore(wxCommandEvent&)
{
const int selected = m_ctlPathList->GetSelection();
if ( selected == wxNOT_FOUND )
{
wxLogError("No path was selected to be restored.");
return;
}
const wxString pathToRestore = m_ctlPathList->GetString(selected);
if ( wxDesktopEnv::RestoreFromRecycleBin(pathToRestore) )
wxLogMessage("Restored '%s'.", pathToRestore);
RefreshFiles();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment