MCPcopy Create free account
hub / github.com/WinMerge/winmerge / SelectFileOrFolder

Function SelectFileOrFolder

Src/FileOrFolderSelect.cpp:208–274  ·  view source on GitHub ↗

* @brief Shows file/folder selection dialog. * * We need this custom function so we can select files and folders with the * same dialog. * - If existing filename is selected return it * - If filename in (CFileDialog) editbox and current folder doesn't form * a valid path to file, return current folder. * @param [in] parent Handle to parent window. Can be `nullptr`, but then * CMainFram

Source from the content-addressed store, hash-verified

206 * @return `true` if user choosed a file/folder, `false` if user canceled dialog.
207 */
208bool SelectFileOrFolder(HWND parent, String& path, const tchar_t* initialPath /*= nullptr*/)
209{
210 String title = _("Open");
211
212 // This will tell common file dialog what to show
213 // and also this will hold its return value
214 tchar_t sSelectedFile[MAX_PATH_FULL];
215 String sInitialDir;
216
217 // check if specified path is a file
218 if (initialPath!=nullptr && initialPath[0] != '\0')
219 {
220 // If initial path info includes a file
221 // we put the bare filename into sSelectedFile
222 // so the common file dialog will start up with that file selected
223 if (paths::DoesPathExist(initialPath) == paths::IS_EXISTING_DIR)
224 {
225 sInitialDir = initialPath;
226 }
227 else
228 {
229 String temp;
230 paths::SplitFilename(initialPath, &sInitialDir, &temp, 0);
231 lstrcpy(sSelectedFile, temp.c_str());
232 }
233 }
234
235 String filters = _("All Files (*.*)|*.*||");
236
237 // Convert extension mask from MFC style separators ('|')
238 // to Win32 style separators ('\0')
239 tchar_t* filtersStr = &*filters.begin();
240 ConvertFilter(filtersStr);
241
242 String dirSelTag = _("Folder Selection");
243
244 // Set initial filename to folder selection tag
245 dirSelTag += _T("."); // Treat it as filename
246 lstrcpy(sSelectedFile, dirSelTag.c_str()); // What is assignment above good for?
247
248 OPENFILENAME_NT4 ofn = { sizeof OPENFILENAME_NT4 };
249 ofn.hwndOwner = parent;
250 ofn.lpstrFilter = filtersStr;
251 ofn.lpstrCustomFilter = nullptr;
252 ofn.nFilterIndex = 1;
253 ofn.lpstrFile = sSelectedFile;
254 ofn.nMaxFile = MAX_PATH_FULL;
255 ofn.lpstrInitialDir = sInitialDir.empty() ? nullptr : sInitialDir.c_str();
256 ofn.lpstrTitle = title.c_str();
257 ofn.lpstrFileTitle = nullptr;
258 ofn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_NOTESTFILECREATE | OFN_NOCHANGEDIR;
259
260 bool bRetVal = !!GetOpenFileName((OPENFILENAME *)&ofn);
261
262 if (bRetVal)
263 {
264 path = sSelectedFile;
265 if (paths::DoesPathExist(path) == paths::DOES_NOT_EXIST)

Callers 3

OnPathButtonMethod · 0.85
OnDiffBrowseFile1Method · 0.85
OnDiffBrowseFile2Method · 0.85

Calls 7

DoesPathExistFunction · 0.85
SplitFilenameFunction · 0.85
ConvertFilterFunction · 0.85
GetPathOnlyFunction · 0.85
AddTrailingSlashFunction · 0.85
beginMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected