browser-window.cpp 3.2 KB

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