yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
file_dialog.cc
Go to the documentation of this file.
1#include "file_dialog.h"
2
3#ifdef _WIN32
4// Include Windows-specific headers
5#include <shobjidl.h>
6#include <windows.h>
7#endif // _WIN32
8
9namespace yaze {
10namespace app {
11namespace core {
12
13#ifdef _WIN32
14
16 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
17
18 IFileDialog *pfd = NULL;
19 HRESULT hr =
20 CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileDialog,
21 reinterpret_cast<void **>(&pfd));
22 std::string file_path_windows;
23 if (SUCCEEDED(hr)) {
24 // Show the dialog
25 hr = pfd->Show(NULL);
26 if (SUCCEEDED(hr)) {
27 IShellItem *psiResult;
28 hr = pfd->GetResult(&psiResult);
29 if (SUCCEEDED(hr)) {
30 // Get the file path
31 PWSTR pszFilePath;
32 psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
33 char str[128];
34 wcstombs(str, pszFilePath, 128);
35 file_path_windows = str;
36 psiResult->Release();
37 CoTaskMemFree(pszFilePath);
38 }
39 }
40 pfd->Release();
41 }
42
43 CoUninitialize();
44 return file_path_windows;
45}
46
48 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
49
50 IFileDialog *pfd = NULL;
51 HRESULT hr =
52 CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileDialog,
53 reinterpret_cast<void **>(&pfd));
54 std::string folder_path_windows;
55 if (SUCCEEDED(hr)) {
56 // Show the dialog
57 DWORD dwOptions;
58 hr = pfd->GetOptions(&dwOptions);
59 if (SUCCEEDED(hr)) {
60 hr = pfd->SetOptions(dwOptions | FOS_PICKFOLDERS);
61 if (SUCCEEDED(hr)) {
62 hr = pfd->Show(NULL);
63 if (SUCCEEDED(hr)) {
64 IShellItem *psiResult;
65 hr = pfd->GetResult(&psiResult);
66 if (SUCCEEDED(hr)) {
67 // Get the folder path
68 PWSTR pszFolderPath;
69 psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFolderPath);
70 char str[128];
71 wcstombs(str, pszFolderPath, 128);
72 folder_path_windows = str;
73 psiResult->Release();
74 CoTaskMemFree(pszFolderPath);
75 }
76 }
77 }
78 }
79 pfd->Release();
80 }
81
82 CoUninitialize();
83 return folder_path_windows;
84}
85
87 const std::string &folder_path) {
88 std::vector<std::string> subdirectories;
89 WIN32_FIND_DATA findFileData;
90 HANDLE hFind = FindFirstFile((folder_path + "\\*").c_str(), &findFileData);
91 if (hFind != INVALID_HANDLE_VALUE) {
92 do {
93 if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
94 if (strcmp(findFileData.cFileName, ".") != 0 &&
95 strcmp(findFileData.cFileName, "..") != 0) {
96 subdirectories.push_back(findFileData.cFileName);
97 }
98 }
99 } while (FindNextFile(hFind, &findFileData) != 0);
100 FindClose(hFind);
101 }
102 return subdirectories;
103}
104
105std::vector<std::string> FileDialogWrapper::GetFilesInFolder(
106 const std::string &folder_path) {
107 std::vector<std::string> files;
108 WIN32_FIND_DATA findFileData;
109 HANDLE hFind = FindFirstFile((folder_path + "\\*").c_str(), &findFileData);
110 if (hFind != INVALID_HANDLE_VALUE) {
111 do {
112 if (!(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
113 files.push_back(findFileData.cFileName);
114 }
115 } while (FindNextFile(hFind, &findFileData) != 0);
116 FindClose(hFind);
117 }
118 return files;
119}
120
121#elif defined(__linux__)
122
124 return "Linux: Open file dialog";
125}
126
128 return "Linux: Open folder dialog";
129}
130
131std::vector<std::string> FileDialogWrapper::GetSubdirectoriesInFolder(
132 const std::string& folder_path) {
133 return {"Linux: Subdirectories in folder"};
134}
135
136std::vector<std::string> FileDialogWrapper::GetFilesInFolder(
137 const std::string& folder_path) {
138 return {"Linux: Files in folder"};
139}
140
141#endif
142
143} // namespace core
144} // namespace app
145} // namespace yaze
static std::vector< std::string > GetSubdirectoriesInFolder(const std::string &folder_path)
static std::string ShowOpenFileDialog()
ShowOpenFileDialog opens a file dialog and returns the selected filepath.
static std::string ShowOpenFolderDialog()
ShowOpenFolderDialog opens a file dialog and returns the selected folder path.
static std::vector< std::string > GetFilesInFolder(const std::string &folder_path)
Definition common.cc:22