-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileRequester.cpp
170 lines (141 loc) · 4.16 KB
/
FileRequester.cpp
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
#include "stdafx.h"
#include "FileRequester.h"
int CALLBACK FileRequester::BrowseCallbackProc(HWND _hwnd, UINT _uMsg, LPARAM _lParam, LPARAM _lpData)
{
UNREFERENCED_PARAMETER(_lParam);
if (_uMsg == BFFM_INITIALIZED)
{
SendMessage(_hwnd, BFFM_SETSELECTION, TRUE, _lpData);
}
return 0;
}
bool FileRequester::BrowseFolder(const wstring& _init_path, const wstring& _title, wstring& _result)
{
BROWSEINFO browserInfo;
memset(&browserInfo, 0, sizeof(browserInfo));
browserInfo.lpszTitle = _title.c_str();
browserInfo.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
browserInfo.lpfn = FileRequester::BrowseCallbackProc;
browserInfo.lParam = (LPARAM)_init_path.c_str();
LPITEMIDLIST pidl = SHBrowseForFolder(&browserInfo);
if (pidl == 0)
return false;
TCHAR path[MAX_PATH];
if (!SHGetPathFromIDList(pidl, path))
return false;
IMalloc* imalloc = 0;
if (SUCCEEDED(SHGetMalloc(&imalloc))) {
imalloc->Free(pidl);
imalloc->Release();
}
_result = path;
return true;
}
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)
{
int mode = 0;
if (_open) mode++;
if (_save) mode++;
if (_directory) mode++;
if (mode == 0) {
mode++;
_open = true;
}
if (mode > 1) {
wcout << _T("Error - open, save or directory?") << endl;
return false;
}
if (_directory) {
FileRequester::BrowseFolder(_path, _title, _result);
return true;
}
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
if (FAILED(hr)) {
wcout << _T("Error - problem with COM library") << endl;
return false;
}
IFileDialog* pRequester = nullptr;
//IFileOpenDialog* pRequester;
//IFileSaveDialog* pRequester;
if (_open)
hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, reinterpret_cast<void**>(&pRequester));
else
hr = CoCreateInstance(CLSID_FileSaveDialog, NULL, CLSCTX_ALL, IID_IFileSaveDialog, reinterpret_cast<void**>(&pRequester));
if (FAILED(hr)) {
wcout << _T("Error - problem with creating object") << endl;
return false;
}
pRequester->SetTitle(_title.c_str());
pRequester->SetFileName(_filename.c_str());
if (_okLabel.empty() == false)
pRequester->SetOkButtonLabel(_okLabel.c_str());
IShellItem* folder;
if (_path.empty())
hr = SHCreateItemInKnownFolder(FOLDERID_Documents, 0, NULL, IID_PPV_ARGS(&folder));
else
hr = SHCreateItemFromParsingName(_path.c_str(), NULL, IID_PPV_ARGS(&folder));
if (FAILED(hr))
wcout << _T("Error - problem with shell item object") << endl;
else
pRequester->SetFolder(folder);
if (_drawersOnly) {
FILEOPENDIALOGOPTIONS ops = FOS_PICKFOLDERS;
pRequester->SetOptions(ops);
}
if (_filter.empty() == false) {
vector<wstring>vresult;
while (_filter.size()) {
int index = _filter.find(_T("|"));
if (index != string::npos) {
vresult.push_back(_filter.substr(0, index));
_filter = _filter.substr(index + 1);
if (_filter.size() == 0) vresult.push_back(_filter);
}
else {
vresult.push_back(_filter);
_filter = _T("");
}
}
if (vresult.size() % 2 == 0) {
COMDLG_FILTERSPEC* pFilter = new COMDLG_FILTERSPEC[vresult.size()];
int counter = 0;
for (unsigned int i = 0; i < vresult.size(); i++) {
pFilter[counter].pszName = &vresult[i][0];
i++;
if (i < vresult.size())
pFilter[counter].pszSpec = &vresult[i][0];
counter++;
}
pRequester->SetFileTypes(vresult.size() / 2, pFilter);
pRequester->SetFileTypeIndex(1);
}
else {
wcout << _T("Error - bad filter") << endl;
}
}
_result = _T("");
POINT mouse;
::GetCursorPos(&mouse);
hr = pRequester->Show(WindowFromPoint(mouse));
if (SUCCEEDED(hr)) {
IShellItem* pItem;
hr = pRequester->GetResult(&pItem);
if (SUCCEEDED(hr)) {
PWSTR pszFilePath;
hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
if (SUCCEEDED(hr)) {
_result = pszFilePath;
CoTaskMemFree(pszFilePath);
}
pItem->Release();
}
else {
wcout << _T("Error - problem with dialog") << endl;
return false;
}
}
pRequester->Release();
folder->Release();
CoUninitialize();
return true;
}