| 1 |
namespace phoenix { |
| 2 |
|
| 3 |
static int CALLBACK BrowserWindowCallbackProc(HWND hwnd, UINT msg, LPARAM lparam, LPARAM lpdata) { |
| 4 |
if(msg == BFFM_INITIALIZED) { |
| 5 |
if(lpdata) { |
| 6 |
auto state = (BrowserWindow::State*)lpdata; |
| 7 |
utf16_t wpath(string{state->path}.transform("/", "\\")); |
| 8 |
if(state->title) SetWindowText(hwnd, utf16_t(state->title)); |
| 9 |
SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)(wchar_t*)wpath); |
| 10 |
} |
| 11 |
} |
| 12 |
return 0; |
| 13 |
} |
| 14 |
|
| 15 |
static string BrowserWindow_fileDialog(bool save, BrowserWindow::State& state) { |
| 16 |
string path = string{state.path}.replace("/", "\\"); |
| 17 |
|
| 18 |
string filters; |
| 19 |
for(auto& filter : state.filters) { |
| 20 |
lstring part = filter.split("("); |
| 21 |
if(part.size() != 2) continue; |
| 22 |
part[1].rtrim<1>(")"); |
| 23 |
part[1].replace(" ", ""); |
| 24 |
part[1].transform(",", ";"); |
| 25 |
filters.append(filter, "\t", part[1], "\t"); |
| 26 |
} |
| 27 |
|
| 28 |
utf16_t wfilters(filters); |
| 29 |
wchar_t wname[PATH_MAX + 1] = L""; |
| 30 |
utf16_t wpath(path); |
| 31 |
utf16_t wtitle(state.title); |
| 32 |
|
| 33 |
wchar_t* p = wfilters; |
| 34 |
while(*p != L'\0') { |
| 35 |
if(*p == L'\t') *p = L'\0'; |
| 36 |
p++; |
| 37 |
} |
| 38 |
|
| 39 |
if(path.empty() == false) { |
| 40 |
//clear COMDLG32 MRU (most recently used) file list |
| 41 |
//this is required in order for lpstrInitialDir to be honored in Windows 7 and above |
| 42 |
registry::remove("HKCU/Software/Microsoft/Windows/CurrentVersion/Explorer/ComDlg32/LastVisitedPidlMRU/"); |
| 43 |
registry::remove("HKCU/Software/Microsoft/Windows/CurrentVersion/Explorer/ComDlg32/OpenSavePidlMRU/"); |
| 44 |
} |
| 45 |
|
| 46 |
OPENFILENAME ofn; |
| 47 |
memset(&ofn, 0, sizeof(OPENFILENAME)); |
| 48 |
ofn.lStructSize = sizeof(OPENFILENAME); |
| 49 |
ofn.hwndOwner = state.parent ? state.parent->p.hwnd : 0; |
| 50 |
ofn.lpstrFilter = wfilters; |
| 51 |
ofn.lpstrInitialDir = wpath; |
| 52 |
ofn.lpstrFile = wname; |
| 53 |
ofn.lpstrTitle = wtitle; |
| 54 |
ofn.nMaxFile = PATH_MAX; |
| 55 |
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; |
| 56 |
ofn.lpstrDefExt = L""; |
| 57 |
|
| 58 |
bool result = (save == false ? GetOpenFileName(&ofn) : GetSaveFileName(&ofn)); |
| 59 |
if(result == false) return ""; |
| 60 |
string name = (const char*)utf8_t(wname); |
| 61 |
name.transform("\\", "/"); |
| 62 |
return name; |
| 63 |
} |
| 64 |
|
| 65 |
string pBrowserWindow::directory(BrowserWindow::State& state) { |
| 66 |
wchar_t wname[PATH_MAX + 1] = L""; |
| 67 |
|
| 68 |
BROWSEINFO bi; |
| 69 |
bi.hwndOwner = state.parent ? state.parent->p.hwnd : 0; |
| 70 |
bi.pidlRoot = NULL; |
| 71 |
bi.pszDisplayName = wname; |
| 72 |
bi.lpszTitle = L"\nChoose a directory:"; |
| 73 |
bi.ulFlags = BIF_NEWDIALOGSTYLE | BIF_RETURNONLYFSDIRS; |
| 74 |
bi.lpfn = BrowserWindowCallbackProc; |
| 75 |
bi.lParam = (LPARAM)&state; |
| 76 |
bi.iImage = 0; |
| 77 |
bool result = false; |
| 78 |
LPITEMIDLIST pidl = SHBrowseForFolder(&bi); |
| 79 |
if(pidl) { |
| 80 |
if(SHGetPathFromIDList(pidl, wname)) { |
| 81 |
result = true; |
| 82 |
IMalloc *imalloc = 0; |
| 83 |
if(SUCCEEDED(SHGetMalloc(&imalloc))) { |
| 84 |
imalloc->Free(pidl); |
| 85 |
imalloc->Release(); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
if(result == false) return ""; |
| 90 |
string name = (const char*)utf8_t(wname); |
| 91 |
if(!name) return ""; |
| 92 |
name.transform("\\", "/"); |
| 93 |
if(name.endsWith("/") == false) name.append("/"); |
| 94 |
return name; |
| 95 |
} |
| 96 |
|
| 97 |
string pBrowserWindow::open(BrowserWindow::State& state) { |
| 98 |
return BrowserWindow_fileDialog(0, state); |
| 99 |
} |
| 100 |
|
| 101 |
string pBrowserWindow::save(BrowserWindow::State& state) { |
| 102 |
return BrowserWindow_fileDialog(1, state); |
| 103 |
} |
| 104 |
|
| 105 |
} |