Files
anm2ed/src/dialog.cpp
T
shweetandClaude Opus 5.5 3f31cedeed Slim Document: move data-model code out, unify edit plumbing
- Region generate/scan move to anm2/regions.cpp; both generators number from 1
- file_merge moves to anm2/merge.cpp and is table-driven
- MaxRects packer moves to util/pack
- Uniform config apply/trim/save move to resource/shader
- Snapshots push from their owned state; Document snapshot wrappers removed
- DocumentData base replaces the hand-written move constructor
- toast_log replaces 55 duplicated toast + English log pairs
- Apply repo .clang-format to touched files

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 13:08:00 -04:00

97 lines
2.5 KiB
C++

#include "dialog.hpp"
#ifdef _WIN32
#include <windows.h>
#elif __unix__
#else
#include "log.hpp"
#include "strings.hpp"
#include "toast.hpp"
#endif
#include <cstdlib>
#include <cstring>
#include <format>
#include "path.hpp"
using namespace anm2ed::util;
namespace anm2ed
{
static void callback(void* userData, const char* const* filelist, int filter)
{
auto self = (Dialog*)(userData);
if (filelist && filelist[0] && strlen(filelist[0]) > 0)
{
self->paths.clear();
for (int i = 0; filelist[i] && strlen(filelist[i]) > 0; ++i)
self->paths.push_back(path::from_utf8(filelist[i]));
self->path = self->paths.empty() ? std::filesystem::path{} : self->paths.front();
self->selectedFilter = filter;
}
else
{
self->selectedFilter = -1;
self->path.clear();
self->paths.clear();
}
}
Dialog::Dialog(SDL_Window* window)
{
*this = Dialog();
this->window = window;
}
void Dialog::file_open(Type type, bool isMany)
{
if (type == Dialog::NONE) return;
path.clear();
paths.clear();
SDL_ShowOpenFileDialog(callback, this, window, FILTERS[TYPE_FILTERS[type]], std::size(FILTERS[TYPE_FILTERS[type]]),
nullptr, isMany);
this->type = type;
}
void Dialog::file_save(Type type, const std::filesystem::path& defaultPath)
{
if (type == Dialog::NONE) return;
path.clear();
paths.clear();
auto defaultPathString = path::to_utf8(defaultPath);
auto defaultPathCStr = defaultPathString.empty() ? nullptr : defaultPathString.c_str();
SDL_ShowSaveFileDialog(callback, this, window, FILTERS[TYPE_FILTERS[type]], std::size(FILTERS[TYPE_FILTERS[type]]),
defaultPathCStr);
this->type = type;
}
void Dialog::folder_open(Type type)
{
if (type == Dialog::NONE) return;
path.clear();
paths.clear();
SDL_ShowOpenFolderDialog(callback, this, window, nullptr, false);
this->type = type;
}
void Dialog::file_explorer_open(const std::filesystem::path& path)
{
if (path.empty()) return;
#ifdef _WIN32
ShellExecuteW(nullptr, L"open", path.native().c_str(), nullptr, nullptr, SW_SHOWNORMAL);
#elif __unix__
auto pathUtf8 = path::to_utf8(path);
system(std::format("xdg-open \"{}\" &", pathUtf8).c_str());
#else
toast_log(Level::WARNING, TOAST_NOT_SUPPORTED);
#endif
}
void Dialog::reset() { *this = Dialog(this->window); }
bool Dialog::is_selected(Type type) const { return this->type == type && !paths.empty(); }
};