1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
#include "PathEntry.h"
#include "imodule.h"
#include "i18n.h"
#include <wx/sizer.h>
#include <wx/bmpbuttn.h>
#include <wx/textctrl.h>
#include <wx/bitmap.h>
#include "FileChooser.h"
#include "DirChooser.h"
#include "os/path.h"
namespace wxutil
{
PathEntry::PathEntry(wxWindow* parent, const std::string& fileType, bool open) :
PathEntry(parent, false, open, fileType, std::string())
{}
PathEntry::PathEntry(wxWindow* parent, bool foldersOnly) :
PathEntry(parent, foldersOnly, true, std::string(), std::string())
{}
PathEntry::PathEntry(wxWindow* parent, const char* fileType, bool open) :
PathEntry(parent, std::string(fileType), open, std::string())
{}
PathEntry::PathEntry(wxWindow* parent, const std::string& fileType, bool open, const std::string& defaultExt) :
PathEntry(parent, false, open, fileType, defaultExt)
{}
PathEntry::PathEntry(wxWindow* parent, bool foldersOnly, bool open,
const std::string& fileType, const std::string& defaultExt) :
wxPanel(parent, wxID_ANY),
_fileType(fileType),
_defaultExt(defaultExt),
_open(open),
_askForOverwrite(true)
{
SetSizer(new wxBoxSizer(wxHORIZONTAL));
// path entry
_entry = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
_entry->Bind(wxEVT_TEXT_ENTER, [&](wxCommandEvent& ev)
{
// Fire the PathEntryChanged event on enter
wxQueueEvent(_entry->GetEventHandler(), new wxCommandEvent(EV_PATH_ENTRY_CHANGED, _entry->GetId()));
});
_entry->Bind(wxEVT_TEXT, [&](wxCommandEvent& ev)
{
// Fire the PathEntryChanged event on typing
wxQueueEvent(_entry->GetEventHandler(), new wxCommandEvent(EV_PATH_ENTRY_CHANGED, _entry->GetId()));
});
// Generate browse button image
const auto& appCtx = module::GlobalModuleRegistry().getApplicationContext();
std::string fullFileName = appCtx.getBitmapsPath() + "ellipsis.png";
wxImage image(fullFileName);
// browse button
_button = new wxBitmapButton(this, wxID_ANY, wxBitmap(image));
// Connect the button
if (foldersOnly)
{
_button->Connect(wxEVT_BUTTON, wxCommandEventHandler(PathEntry::onBrowseFolders), NULL, this);
}
else
{
_button->Connect(wxEVT_BUTTON, wxCommandEventHandler(PathEntry::onBrowseFiles), NULL, this);
}
GetSizer()->Add(_entry, 1, wxEXPAND | wxRIGHT, 6);
GetSizer()->Add(_button, 0, wxEXPAND);
}
void PathEntry::setValue(const std::string& val)
{
_entry->SetValue(val);
_entry->SetInsertionPointEnd();
}
std::string PathEntry::getValue() const
{
return _entry->GetValue().ToStdString();
}
wxTextCtrl* PathEntry::getEntryWidget()
{
return _entry;
}
void PathEntry::setDefaultExtension(const std::string& defaultExt)
{
_defaultExt = defaultExt;
}
void PathEntry::setAskForOverwrite(bool ask)
{
_askForOverwrite = ask;
}
void PathEntry::onBrowseFiles(wxCommandEvent& ev)
{
wxWindow* topLevel = wxGetTopLevelParent(this);
FileChooser fileChooser(topLevel, _("Choose File"), _open, _fileType, _defaultExt);
// Propagate the setting of this picker
fileChooser.askForOverwrite(_askForOverwrite);
auto curValue = getValue();
if (!curValue.empty())
{
// Set the filename to the one contained in the text box
fileChooser.setCurrentFile(os::getFilename(curValue));
// If there's a non-empty path, point it to the specified folder
auto curFolder = os::getDirectory(curValue);
if (!curFolder.empty())
{
fileChooser.setCurrentPath(curFolder);
}
}
auto filename = fileChooser.display();
topLevel->Show();
if (!filename.empty())
{
setValue(filename);
// Fire the PathEntryChanged event
wxQueueEvent(GetEventHandler(), new wxCommandEvent(EV_PATH_ENTRY_CHANGED, _entry->GetId()));
}
}
void PathEntry::onBrowseFolders(wxCommandEvent& ev)
{
wxWindow* topLevel = wxGetTopLevelParent(this);
DirChooser dirChooser(topLevel, _("Choose Directory"));
std::string curEntry = getValue();
if (!path_is_absolute(curEntry.c_str()))
{
curEntry.clear();
}
dirChooser.setCurrentPath(curEntry);
std::string filename = dirChooser.display();
topLevel->Show();
if (!filename.empty())
{
setValue(filename);
// Fire the PathEntryChanged event
wxQueueEvent(GetEventHandler(), new wxCommandEvent(EV_PATH_ENTRY_CHANGED, _entry->GetId()));
}
}
wxDEFINE_EVENT(EV_PATH_ENTRY_CHANGED, wxCommandEvent);
} // namespace wxutil
|