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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
#include "FileChooser.h"
#include "ifiletypes.h"
#include "i18n.h"
#include "imapformat.h"
#include "igame.h"
#include "os/path.h"
#include "os/file.h"
#include "MultiMonitor.h"
#include "dialog/MessageBox.h"
#include "string/predicate.h"
#include "string/case_conv.h"
#include "string/trim.h"
#include <wx/app.h>
namespace wxutil
{
namespace
{
const char* const WILDCARD_EXTENSION = "*";
}
FileChooser::FileChooser(const std::string& title,
bool open,
const std::string& fileType,
const std::string& defaultExt) :
FileChooser(GlobalMainFrame().getWxTopLevelWindow(), title, open, fileType, defaultExt)
{}
FileChooser::FileChooser(wxWindow* parentWindow,
const std::string& title,
bool open,
const std::string& fileType,
const std::string& defaultExt) :
_dialog(new wxFileDialog(parentWindow, title, wxEmptyString,
wxEmptyString, wxFileSelectorDefaultWildcardStr, getStyle(open))),
_title(title),
_fileType(fileType),
_defaultExt(defaultExt),
_open(open)
{
construct();
}
FileChooser::~FileChooser()
{
_dialog->Destroy();
}
void FileChooser::construct()
{
// Sanity-check the filetype
if (_fileType.empty())
{
_fileType = "*";
}
// Set a meaningful title if empty
if (_title.empty())
{
_title = _open ? _("Open File") : _("Save File");
}
// Make default extension lowercase, and cut off any dots at the beginning
string::to_lower(_defaultExt);
string::trim_left(_defaultExt, ".");
if (!_open && _fileType == filetype::TYPE_MAP_EXPORT)
{
assembleMapExportFileTypes();
}
else
{
assembleFileTypes();
}
// Add a final mask for All Files (*.*)
FileFilter wildCardFilter;
wildCardFilter.caption = _("All Files (*.*)");
wildCardFilter.filter = "*.*";
wildCardFilter.extension = WILDCARD_EXTENSION;
_fileFilters.push_back(wildCardFilter);
std::string wildcard = "";
for (const FileFilter& filter : _fileFilters)
{
wildcard += wildcard.empty() ? "" : "|";
wildcard += filter.caption + "|" + filter.filter;
}
_dialog->SetWildcard(wildcard);
for (std::size_t i = 0; i < _fileFilters.size(); ++i)
{
if (_fileFilters[i].isDefaultFilter)
{
_dialog->SetFilterIndex(i);
break;
}
}
}
void FileChooser::assembleMapExportFileTypes()
{
FileTypePatterns patterns = GlobalFiletypes().getPatternsForType(_fileType);
for (const auto& pattern : patterns)
{
auto formats = GlobalMapFormatManager().getMapFormatList(pattern.extension);
// Pre-select take the default map format for this game type
auto defaultFormat = GlobalMapFormatManager().getMapFormatForGameType(
GlobalGameManager().currentGame()->getKeyValue("type"), pattern.extension
);
for (const map::MapFormatPtr& format : formats)
{
FileFilter filter;
filter.caption = format->getMapFormatName() + " " + pattern.name + " (" + pattern.pattern + ")";
filter.filter = pattern.pattern;
filter.extension = pattern.extension;
filter.mapFormatName = format->getMapFormatName();
_fileFilters.push_back(filter);
if (format == defaultFormat)
{
filter.isDefaultFilter = true;
}
}
}
}
void FileChooser::assembleFileTypes()
{
FileTypePatterns patterns = GlobalFiletypes().getPatternsForType(_fileType);
for (const auto& pattern : patterns)
{
FileFilter filter;
filter.caption = pattern.name + " (" + pattern.pattern + ")";
filter.filter = pattern.pattern;
filter.extension = pattern.extension;
_fileFilters.push_back(filter);
// Pre-select the filter matching the default extension
if (pattern.extension == _defaultExt)
{
filter.isDefaultFilter = true;
}
}
}
long FileChooser::getStyle(bool open)
{
if (open)
{
return wxFD_OPEN;
}
else // !open
{
return wxFD_SAVE | wxFD_OVERWRITE_PROMPT;
}
}
void FileChooser::setCurrentPath(const std::string& path)
{
_path = os::standardPathWithSlash(path);
// Convert path to standard and set the folder in the dialog
_dialog->SetPath(_path);
// SetPath() overwrites the filename, so set it again
if (!_file.empty())
{
_dialog->SetFilename(_file);
selectFilterIndexFromFilename(_file);
}
}
void FileChooser::setCurrentFile(const std::string& file)
{
_file = os::getFilename(file);
if (!_open)
{
_dialog->SetFilename(_file);
selectFilterIndexFromFilename(_file);
}
}
void FileChooser::selectFilterIndexFromFilename(const std::string& filename)
{
if (filename.empty())
{
return;
}
auto ext = os::getExtension(filename);
std::size_t wildCardIndex = std::numeric_limits<std::size_t>::max();
for (std::size_t i = 0; i < _fileFilters.size(); ++i)
{
if (string::iequals(ext, _fileFilters[i].extension))
{
_dialog->SetFilterIndex(i);
return;
}
if (_fileFilters[i].extension == WILDCARD_EXTENSION)
{
wildCardIndex = i;
}
}
// Select the * extension if there's no better match
if (wildCardIndex < _fileFilters.size())
{
_dialog->SetFilterIndex(wildCardIndex);
}
}
std::string FileChooser::getSelectedFileName()
{
// Load the filename from the dialog
std::string fileName = os::standardPath(_dialog->GetPath().ToStdString());
// Append the default extension for save operations before checking overwrites
if (!_open // save operation
&& !fileName.empty() // valid filename
&& !_defaultExt.empty() // non-empty default extension
&& os::getExtension(fileName).empty()) // no extension selected by the user
{
fileName.append("." + _defaultExt);
}
return fileName;
}
std::string FileChooser::getSelectedMapFormat()
{
int index = _dialog->GetFilterIndex();
if (index >=0 && index < static_cast<int>(_fileFilters.size()))
{
return _fileFilters[index].mapFormatName;
}
return "";
}
void FileChooser::askForOverwrite(bool ask)
{
if (ask)
{
_dialog->SetWindowStyleFlag(_dialog->GetWindowStyleFlag() | wxFD_OVERWRITE_PROMPT);
}
else
{
_dialog->SetWindowStyleFlag(_dialog->GetWindowStyleFlag() & ~wxFD_OVERWRITE_PROMPT);
}
}
std::string FileChooser::display()
{
int curDisplayIdx = wxDisplay::GetFromWindow(wxTheApp->GetTopWindow());
wxDisplay curDisplay(curDisplayIdx);
wxRect rect = curDisplay.GetGeometry();
int newWidth = static_cast<int>(rect.GetWidth() * 0.5f);
int newHeight = static_cast<int>(rect.GetHeight() * 0.66f);
_dialog->SetSize(newWidth, newHeight);
_dialog->CenterOnScreen();
if (_dialog->ShowModal() == wxID_OK)
{
return getSelectedFileName();
}
return "";
}
} // namespace wxutil
|