* @brief Helper function for selecting folder or file. * This function shows standard Windows file selection dialog for selecting * file or folder to open or file to save. The last parameter @p is_open selects * between open or save modes. Biggest difference is that in save-mode Windows * asks if user wants to override existing file. * @param [in] parent Handle to parent window. Can be `nullp
| 47 | * @param [in] defaultExtension Extension to append if user doesn't provide one |
| 48 | */ |
| 49 | bool SelectFile(HWND parent, String& path, bool is_open /*= true*/, |
| 50 | const tchar_t* initialPath /*= nullptr*/, const String& stitle /*=_T("")*/, |
| 51 | const String& sfilter /*=_T("")*/, const tchar_t* defaultExtension /*= nullptr*/) |
| 52 | { |
| 53 | path.clear(); // Clear output param |
| 54 | |
| 55 | // This will tell common file dialog what to show |
| 56 | // and also this will hold its return value |
| 57 | tchar_t sSelectedFile[MAX_PATH_FULL] = {0}; |
| 58 | String sInitialDir; |
| 59 | |
| 60 | // check if specified path is a file |
| 61 | if (initialPath && initialPath[0]) |
| 62 | { |
| 63 | // If initial path info includes a file |
| 64 | // we put the bare filename into sSelectedFile |
| 65 | // so the common file dialog will start up with that file selected |
| 66 | if (paths::DoesPathExist(initialPath) == paths::IS_EXISTING_DIR) |
| 67 | { |
| 68 | sInitialDir = initialPath; |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | String temp; |
| 73 | paths::SplitFilename(initialPath, &sInitialDir, &temp, 0); |
| 74 | lstrcpy(sSelectedFile, temp.c_str()); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | String filters = sfilter, title = stitle; |
| 79 | if (sfilter.empty()) |
| 80 | filters = _("All Files (*.*)|*.*||"); |
| 81 | if (stitle.empty()) |
| 82 | { |
| 83 | title = is_open ? _("Open") : _("Save As"); |
| 84 | } |
| 85 | |
| 86 | // Convert extension mask from MFC style separators ('|') |
| 87 | // to Win32 style separators ('\0') |
| 88 | tchar_t* filtersStr = &*filters.begin(); |
| 89 | ConvertFilter(filtersStr); |
| 90 | |
| 91 | OPENFILENAME_NT4 ofn = { sizeof OPENFILENAME_NT4 }; |
| 92 | ofn.hwndOwner = parent; |
| 93 | ofn.lpstrFilter = filtersStr; |
| 94 | ofn.lpstrCustomFilter = nullptr; |
| 95 | ofn.nFilterIndex = 1; |
| 96 | ofn.lpstrFile = sSelectedFile; |
| 97 | ofn.nMaxFile = MAX_PATH_FULL; |
| 98 | ofn.lpstrInitialDir = sInitialDir.empty() ? nullptr : sInitialDir.c_str(); |
| 99 | ofn.lpstrTitle = title.c_str(); |
| 100 | ofn.lpstrFileTitle = nullptr; |
| 101 | if (defaultExtension) |
| 102 | ofn.lpstrDefExt = defaultExtension; |
| 103 | ofn.Flags = OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR; |
| 104 | |
| 105 | bool bRetVal = false; |
| 106 | if (is_open) |
no test coverage detected