-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdirectory-chooser.cpp
More file actions
59 lines (45 loc) · 1.55 KB
/
directory-chooser.cpp
File metadata and controls
59 lines (45 loc) · 1.55 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
#pragma warning(push, 0)
#include <FL/filename.H>
#include <FL/fl_utf8.h>
#pragma warning(pop)
#include "directory-chooser.h"
#ifdef _WIN32
Directory_Chooser::Directory_Chooser(int) : _title(NULL), _filename(NULL), _fod_ptr(NULL) {}
Directory_Chooser::~Directory_Chooser() {
if (_fod_ptr) { _fod_ptr->Release(); }
delete [] _filename;
}
int Directory_Chooser::show() {
HRESULT hr;
if (!_fod_ptr) {
hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileOpenDialog, reinterpret_cast<void **>(&_fod_ptr));
if (!SUCCEEDED(hr)) { return -1; }
}
delete [] _filename;
_filename = NULL;
wchar_t wtitle[FL_PATH_MAX] = {};
fl_utf8towc(_title, (unsigned int) strlen(_title), wtitle, sizeof(wtitle));
_fod_ptr->SetTitle(wtitle);
FILEOPENDIALOGOPTIONS fod_opts;
_fod_ptr->GetOptions(&fod_opts);
fod_opts |= FOS_PICKFOLDERS;
_fod_ptr->SetOptions(fod_opts);
HWND hWnd = GetForegroundWindow();
hr = _fod_ptr->Show(hWnd);
if (hr == HRESULT_FROM_WIN32(ERROR_CANCELLED)) { return 1; }
if (!SUCCEEDED(hr)) { return -1; }
IShellItem *pItem;
hr = _fod_ptr->GetResult(&pItem);
if (!SUCCEEDED(hr)) { return -1; }
PWSTR pszFilePath;
hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
if (!SUCCEEDED(hr)) { pItem->Release(); return -1; }
size_t len = wcslen(pszFilePath);
char *filename = new char[len + 1];
fl_utf8fromwc(filename, (unsigned int)len + 1, pszFilePath, (unsigned int)len);
_filename = filename;
CoTaskMemFree(pszFilePath);
pItem->Release();
return 0;
}
#endif