1 /////////////////////////////////////////////////////////////////////////////
2 // WinMerge: an interactive diff/merge utility
3 // Copyright (C) 1997-2000 Thingamahoochie Software
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /////////////////////////////////////////////////////////////////////////////
22 * @file FileOrFolderSelect.cpp
24 * @brief Implementation of the file and folder selection routines.
28 #include "FileOrFolderSelect.h"
31 #include "Environment.h"
36 static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam,
38 static void ConvertFilter(LPTSTR filterStr);
40 /** @brief Last selected folder for folder selection dialog. */
41 static String LastSelectedFolder;
44 * @brief Helper function for selecting folder or file.
45 * This function shows standard Windows file selection dialog for selecting
46 * file or folder to open or file to save. The last parameter @p is_open selects
47 * between open or save modes. Biggest difference is that in save-mode Windows
48 * asks if user wants to override existing file.
49 * @param [in] parent Handle to parent window. Can be a NULL, but then
50 * CMainFrame is used which can cause modality problems.
51 * @param [out] path Selected path is returned in this string
52 * @param [in] initialPath Initial path (and file) shown when dialog is opened
53 * @param [in] titleid Resource string ID for dialog title.
54 * @param [in] filterid 0 or STRING ID for filter string
55 * - 0 means "All files (*.*)". Note the string formatting!
56 * @param [in] is_open Selects Open/Save -dialog (mode).
57 * @note Be careful when setting @p parent to NULL as there are potential
58 * modality problems with this. Dialog can be lost behind other windows!
59 * @param [in] defaultExtension Extension to append if user doesn't provide one
61 BOOL SelectFile(HWND parent, String& path, LPCTSTR initialPath /*=NULL*/,
62 const String& stitle /*=_T("")*/, const String& sfilter /*=_T("")*/,
63 BOOL is_open /*=TRUE*/, LPCTSTR defaultExtension /*=NULL*/)
65 path.clear(); // Clear output param
67 // This will tell common file dialog what to show
68 // and also this will hold its return value
69 TCHAR sSelectedFile[MAX_PATH] = {0};
71 // check if specified path is a file
72 if (initialPath && initialPath[0])
74 // If initial path info includes a file
75 // we put the bare filename into sSelectedFile
76 // so the common file dialog will start up with that file selected
77 if (paths_DoesPathExist(initialPath) == IS_EXISTING_FILE)
80 paths_SplitFilename(initialPath, 0, &temp, 0);
81 lstrcpy(sSelectedFile, temp.c_str());
85 String filters = sfilter, title = stitle;
87 filters = _("All Files (*.*)|*.*||");
91 // Convert extension mask from MFC style separators ('|')
92 // to Win32 style separators ('\0')
93 LPTSTR filtersStr = &*filters.begin();
94 ConvertFilter(filtersStr);
97 memset(&ofn, 0, sizeof(ofn));
98 ofn.lStructSize = sizeof(ofn);
99 ofn.hwndOwner = parent;
100 ofn.lpstrFilter = filtersStr;
101 ofn.lpstrCustomFilter = NULL;
102 ofn.nFilterIndex = 1;
103 ofn.lpstrFile = sSelectedFile;
104 ofn.nMaxFile = MAX_PATH;
105 ofn.lpstrInitialDir = initialPath;
106 ofn.lpstrTitle = title.c_str();
107 ofn.lpstrFileTitle = NULL;
108 if (defaultExtension)
109 ofn.lpstrDefExt = defaultExtension;
110 ofn.Flags = OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR;
112 BOOL bRetVal = FALSE;
114 bRetVal = GetOpenFileName((OPENFILENAME *)&ofn);
116 bRetVal = GetSaveFileName((OPENFILENAME *)&ofn);
117 // common file dialog populated sSelectedFile variable's buffer
120 path = sSelectedFile;
126 * @brief Helper function for selecting directory
127 * @param [out] path Selected path is returned in this string
128 * @param [in] root_path Initial path shown when dialog is opened
129 * @param [in] titleid Resource string ID for dialog title.
130 * @param [in] hwndOwner Handle to owner window or NULL
131 * @return TRUE if valid folder selected (not cancelled)
133 BOOL SelectFolder(String& path, LPCTSTR root_path /*=NULL*/,
134 const String& stitle /*=_T("")*/,
135 HWND hwndOwner /*=NULL*/)
140 TCHAR szPath[MAX_PATH] = {0};
142 String title = stitle;
143 if (root_path == NULL)
144 LastSelectedFolder.clear();
146 LastSelectedFolder = root_path;
148 bi.hwndOwner = hwndOwner;
149 bi.pidlRoot = NULL; // Start from desktop folder
150 bi.pszDisplayName = szPath;
151 bi.lpszTitle = title.c_str();
152 bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_USENEWUI | BIF_VALIDATE;
153 bi.lpfn = BrowseCallbackProc;
154 bi.lParam = (LPARAM)root_path;
156 pidl = SHBrowseForFolder(&bi);
160 if (SHGetPathFromIDList(pidl, szPath))
166 SHGetMalloc(&pMalloc);
174 * @brief Callback function for setting selected folder for folder dialog.
176 static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam,
179 // Look for BFFM_INITIALIZED
180 if (uMsg == BFFM_INITIALIZED)
183 SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
185 SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)LastSelectedFolder.c_str());
187 else if (uMsg == BFFM_VALIDATEFAILED)
189 String strMessage = (TCHAR *)lParam;
190 strMessage += _T("
\83t
\83H
\83\8b\83_
\82Í
\91¶
\8dÝ
\82µ
\82Ü
\82¹
\82ñ
\81B
\8dì
\90¬
\82µ
\82Ü
\82·
\82©?");
191 int answer = MessageBox(hwnd, strMessage.c_str(), _T("
\83t
\83H
\83\8b\83_
\82Ì
\8dì
\90¬"), MB_YESNO);
194 if (!paths_CreateIfNeeded((TCHAR*)lParam))
196 MessageBox(hwnd, _T("
\83t
\83H
\83\8b\83_
\82Ì
\8dì
\90¬
\82É
\8e¸
\94s
\82µ
\82Ü
\82µ
\82½"), _T("
\83t
\83H
\83\8b\83_
\82Ì
\8dì
\90¬"), MB_OK | MB_ICONWARNING);
205 * @brief Shows file/folder selection dialog.
207 * We need this custom function so we can select files and folders with the
209 * - If existing filename is selected return it
210 * - If filename in (CFileDialog) editbox and current folder doesn't form
211 * a valid path to file, return current folder.
212 * @param [in] parent Handle to parent window. Can be a NULL, but then
213 * CMainFrame is used which can cause modality problems.
214 * @param [out] path Selected folder/filename
215 * @param [in] initialPath Initial file or folder shown/selected.
216 * @return TRUE if user choosed a file/folder, FALSE if user canceled dialog.
218 BOOL SelectFileOrFolder(HWND parent, String& path, LPCTSTR initialPath /*=NULL*/)
220 String title = _("Open");
222 // This will tell common file dialog what to show
223 // and also this will hold its return value
224 TCHAR sSelectedFile[MAX_PATH];
226 // check if specified path is a file
227 if (initialPath && initialPath[0])
229 // If initial path info includes a file
230 // we put the bare filename into sSelectedFile
231 // so the common file dialog will start up with that file selected
232 if (paths_DoesPathExist(initialPath) == IS_EXISTING_FILE)
235 paths_SplitFilename(initialPath, 0, &temp, 0);
236 lstrcpy(sSelectedFile, temp.c_str());
240 String filters = _("All Files (*.*)|*.*||");
242 // Convert extension mask from MFC style separators ('|')
243 // to Win32 style separators ('\0')
244 LPTSTR filtersStr = &*filters.begin();
245 ConvertFilter(filtersStr);
247 String dirSelTag = _("Folder Selection");
249 // Set initial filename to folder selection tag
250 dirSelTag += _T("."); // Treat it as filename
251 lstrcpy(sSelectedFile, dirSelTag.c_str()); // What is assignment above good for?
253 OPENFILENAME_NT4 ofn;
254 memset(&ofn, 0, sizeof(ofn));
255 ofn.lStructSize = sizeof(ofn);
256 ofn.hwndOwner = parent;
257 ofn.lpstrFilter = filtersStr;
258 ofn.lpstrCustomFilter = NULL;
259 ofn.nFilterIndex = 1;
260 ofn.lpstrFile = sSelectedFile;
261 ofn.nMaxFile = MAX_PATH;
262 ofn.lpstrInitialDir = initialPath;
263 ofn.lpstrTitle = title.c_str();
264 ofn.lpstrFileTitle = NULL;
265 ofn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_NOTESTFILECREATE | OFN_NOCHANGEDIR;
267 BOOL bRetVal = GetOpenFileName((OPENFILENAME *)&ofn);
271 path = sSelectedFile;
272 struct _stati64 statBuffer;
273 int nRetVal = _tstati64(path.c_str(), &statBuffer);
276 // We have a valid folder name, but propably garbage as a filename.
277 // Return folder name
278 String folder = paths_GetPathOnly(sSelectedFile);
279 path = paths_AddTrailingSlash(folder);
287 * @brief Helper function for converting filter format.
289 * MFC functions separate filter strings with | char which is also
290 * good choice to safe into resource. But WinAPI32 functions we use
291 * needs '\0' as separator. This function replaces '|'s with '\0's.
293 * @param [in,out] filterStr
294 * - in Mask string to convert
295 * - out Converted string
297 static void ConvertFilter(LPTSTR filterStr)
299 while (TCHAR *ch = _tcschr(filterStr, '|'))