-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileRequester.cpp
More file actions
195 lines (156 loc) · 4.66 KB
/
Copy pathFileRequester.cpp
File metadata and controls
195 lines (156 loc) · 4.66 KB
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
#include "stdafx.h"
#include "FileRequester.h"
class ComScope
{
public:
ComScope() : m_hr(CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE))
{
}
~ComScope()
{
if (SUCCEEDED(m_hr)) {
CoUninitialize();
}
}
HRESULT Result() const { return m_hr; }
private:
HRESULT m_hr;
};
bool FileRequester::Requester(bool _save, bool _open, bool _directory, bool _drawersOnly, const wstring& _title, const wstring& _path, const wstring& _filename, const wstring& _okLabel, wstring _filter, wstring& _result)
{
if (!_open && !_save && !_directory) {
_open = true;
}
bool pickFolders = _directory || _drawersOnly;
if (_save && pickFolders) {
wcout << _T("Error - cannot use save mode with directory(drawersonly/directory) selection") << endl;
return false;
}
if (_open && _save) {
wcout << _T("Error - parameters(open/save) were mixed up") << endl;
return false;
}
ComScope com;
if (FAILED(com.Result())) {
wcout << _T("Error - problem with COM library") << endl;
return false;
}
IFileDialog* pRequester = nullptr;
HRESULT hr = E_FAIL;
if (_save)
hr = CoCreateInstance(CLSID_FileSaveDialog, NULL, CLSCTX_ALL, IID_IFileSaveDialog, reinterpret_cast<void**>(&pRequester));
else
hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, reinterpret_cast<void**>(&pRequester));
if (FAILED(hr) || !pRequester) {
wcout << _T("Error - problem with creating object") << endl;
if (pRequester) pRequester->Release();
return false;
}
pRequester->SetTitle(_title.c_str());
pRequester->SetFileName(_filename.c_str());
if (_okLabel.empty() == false)
pRequester->SetOkButtonLabel(_okLabel.c_str());
IShellItem* folder = nullptr;
hr = E_FAIL;
if (!_path.empty()) {
wchar_t fullPath[MAX_PATH];
DWORD len = GetFullPathNameW(_path.c_str(), MAX_PATH, fullPath, nullptr);
if (len > 0 && len < MAX_PATH) {
DWORD attr = GetFileAttributesW(fullPath);
if (attr != INVALID_FILE_ATTRIBUTES && !(attr & FILE_ATTRIBUTE_DIRECTORY)) {
PathRemoveFileSpecW(fullPath);
}
hr = SHCreateItemFromParsingName(fullPath, NULL, IID_PPV_ARGS(&folder));
}
}
if (FAILED(hr) || !folder)
hr = SHCreateItemInKnownFolder(FOLDERID_Documents, 0, NULL, IID_PPV_ARGS(&folder));
if (FAILED(hr))
wcout << _T("Error - problem with shell item object") << endl;
else
pRequester->SetFolder(folder);
if (pickFolders) {
FILEOPENDIALOGOPTIONS ops = 0;
pRequester->GetOptions(&ops);
ops |= FOS_PICKFOLDERS;
ops |= FOS_FORCEFILESYSTEM;
pRequester->SetOptions(ops);
}
vector<COMDLG_FILTERSPEC> filters;
if (!pickFolders && !_filter.empty()) {
if (_filter.empty() == false) {
vector<wstring> parts;
parts.reserve(12);
wstring_view view = _filter;
while (!view.empty()) {
size_t pos = view.find(L'|');
if (pos == wstring_view::npos) {
parts.emplace_back(view);
break;
}
wstring_view token = view.substr(0, pos);
parts.emplace_back(token);
view.remove_prefix(pos + 1);
}
parts.erase(
remove_if(parts.begin(), parts.end(),
[](const wstring& s) { return s.empty(); }),
parts.end()
);
if (parts.size() % 2 != 0) {
wcout << _T("Error - bad filter") << endl;
}
else {
filters.reserve(parts.size() / 2);
for (size_t i = 0; i < parts.size(); i += 2) {
COMDLG_FILTERSPEC spec;
spec.pszName = parts[i].c_str();
spec.pszSpec = parts[i + 1].c_str();
filters.push_back(spec);
}
pRequester->SetFileTypes(static_cast<UINT>(filters.size()), filters.data());
pRequester->SetFileTypeIndex(1);
}
}
}
_result = _T("");
HWND hwnd = GetActiveWindow();
if (!hwnd) hwnd = GetForegroundWindow();
if (!hwnd) hwnd = GetDesktopWindow();
hr = pRequester->Show(hwnd);
if (hr == HRESULT_FROM_WIN32(ERROR_CANCELLED)) {
if (folder) folder->Release();
if (pRequester) pRequester->Release();
return false;
}
if (FAILED(hr)) {
wcout << _T("Error - problem with dialog") << endl;
if (folder) folder->Release();
if (pRequester) pRequester->Release();
return false;
}
IShellItem* pItem = nullptr;
hr = pRequester->GetResult(&pItem);
if (FAILED(hr) || !pItem) {
wcout << _T("Error - problem with dialog result") << endl;
if (folder) folder->Release();
if (pRequester) pRequester->Release();
return false;
}
PWSTR pszFilePath = nullptr;
hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
if (SUCCEEDED(hr) && pszFilePath) {
_result = pszFilePath;
CoTaskMemFree(pszFilePath);
}
else {
pItem->Release();
if (folder) folder->Release();
if (pRequester) pRequester->Release();
return false;
}
pItem->Release();
if (folder) folder->Release();
if (pRequester) pRequester->Release();
return !_result.empty();
}