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 // VC 6 headers don't define these constants for folder browse dialog
37 // so define them here. Copied from shlobj.h
39 #define BIF_EDITBOX 0x0010 // Add an editbox to the dialog
41 #ifndef BIF_NEWDIALOGSTYLE
42 #define BIF_NEWDIALOGSTYLE 0x0040 // Use the new dialog layout with the ability to resize
43 // Caller needs to call OleInitialize() before using this API
46 #define BIF_USENEWUI (BIF_NEWDIALOGSTYLE | BIF_EDITBOX)
49 static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam,
51 static void ConvertFilter(LPTSTR filterStr);
53 /** @brief Last selected folder for folder selection dialog. */
54 static String LastSelectedFolder;
57 * @brief Helper function for selecting folder or file.
58 * This function shows standard Windows file selection dialog for selecting
59 * file or folder to open or file to save. The last parameter @p is_open selects
60 * between open or save modes. Biggest difference is that in save-mode Windows
61 * asks if user wants to override existing file.
62 * @param [in] parent Handle to parent window. Can be a NULL, but then
63 * CMainFrame is used which can cause modality problems.
64 * @param [out] path Selected path is returned in this string
65 * @param [in] initialPath Initial path (and file) shown when dialog is opened
66 * @param [in] titleid Resource string ID for dialog title.
67 * @param [in] filterid 0 or STRING ID for filter string
68 * - 0 means "All files (*.*)". Note the string formatting!
69 * @param [in] is_open Selects Open/Save -dialog (mode).
70 * @note Be careful when setting @p parent to NULL as there are potential
71 * modality problems with this. Dialog can be lost behind other windows!
72 * @param [in] defaultExtension Extension to append if user doesn't provide one
74 BOOL SelectFile(HWND parent, String& path, LPCTSTR initialPath /*=NULL*/,
75 const String& stitle /*=_T("")*/, const String& sfilter /*=_T("")*/,
76 BOOL is_open /*=TRUE*/, LPCTSTR defaultExtension /*=NULL*/)
78 path.clear(); // Clear output param
80 // This will tell common file dialog what to show
81 // and also this will hold its return value
82 TCHAR sSelectedFile[MAX_PATH] = {0};
84 // check if specified path is a file
85 if (initialPath && initialPath[0])
87 // If initial path info includes a file
88 // we put the bare filename into sSelectedFile
89 // so the common file dialog will start up with that file selected
90 if (paths_DoesPathExist(initialPath) == IS_EXISTING_FILE)
93 paths_SplitFilename(initialPath, 0, &temp, 0);
94 lstrcpy(sSelectedFile, temp.c_str());
98 String filters = sfilter, title = stitle;
100 filters = _("All Files (*.*)|*.*||");
104 // Convert extension mask from MFC style separators ('|')
105 // to Win32 style separators ('\0')
106 LPTSTR filtersStr = &*filters.begin();
107 ConvertFilter(filtersStr);
109 OPENFILENAME_NT4 ofn;
110 memset(&ofn, 0, sizeof(ofn));
111 ofn.lStructSize = sizeof(ofn);
112 ofn.hwndOwner = parent;
113 ofn.lpstrFilter = filtersStr;
114 ofn.lpstrCustomFilter = NULL;
115 ofn.nFilterIndex = 1;
116 ofn.lpstrFile = sSelectedFile;
117 ofn.nMaxFile = MAX_PATH;
118 ofn.lpstrInitialDir = initialPath;
119 ofn.lpstrTitle = title.c_str();
120 ofn.lpstrFileTitle = NULL;
121 if (defaultExtension)
122 ofn.lpstrDefExt = defaultExtension;
123 ofn.Flags = OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR;
125 BOOL bRetVal = FALSE;
127 bRetVal = GetOpenFileName((OPENFILENAME *)&ofn);
129 bRetVal = GetSaveFileName((OPENFILENAME *)&ofn);
130 // common file dialog populated sSelectedFile variable's buffer
133 path = sSelectedFile;
139 * @brief Helper function for selecting directory
140 * @param [out] path Selected path is returned in this string
141 * @param [in] root_path Initial path shown when dialog is opened
142 * @param [in] titleid Resource string ID for dialog title.
143 * @param [in] hwndOwner Handle to owner window or NULL
144 * @return TRUE if valid folder selected (not cancelled)
146 BOOL SelectFolder(String& path, LPCTSTR root_path /*=NULL*/,
147 const String& stitle /*=_T("")*/,
148 HWND hwndOwner /*=NULL*/)
153 TCHAR szPath[MAX_PATH] = {0};
155 String title = stitle;
156 if (root_path == NULL)
157 LastSelectedFolder.clear();
159 LastSelectedFolder = root_path;
161 bi.hwndOwner = hwndOwner;
162 bi.pidlRoot = NULL; // Start from desktop folder
163 bi.pszDisplayName = szPath;
164 bi.lpszTitle = title.c_str();
165 bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_USENEWUI | BIF_VALIDATE;
166 bi.lpfn = BrowseCallbackProc;
167 bi.lParam = (LPARAM)root_path;
169 pidl = SHBrowseForFolder(&bi);
173 if (SHGetPathFromIDList(pidl, szPath))
179 SHGetMalloc(&pMalloc);
187 * @brief Callback function for setting selected folder for folder dialog.
189 static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam,
192 // Look for BFFM_INITIALIZED
193 if (uMsg == BFFM_INITIALIZED)
196 SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
198 SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)LastSelectedFolder.c_str());
200 else if (uMsg == BFFM_VALIDATEFAILED)
202 String strMessage = (TCHAR *)lParam;
203 strMessage += _T("
\83t
\83H
\83\8b\83_
\82Í
\91¶
\8dÝ
\82µ
\82Ü
\82¹
\82ñ
\81B
\8dì
\90¬
\82µ
\82Ü
\82·
\82©?");
204 int answer = MessageBox(hwnd, strMessage.c_str(), _T("
\83t
\83H
\83\8b\83_
\82Ì
\8dì
\90¬"), MB_YESNO);
207 if (!paths_CreateIfNeeded((TCHAR*)lParam))
209 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);
218 * @brief Shows file/folder selection dialog.
220 * We need this custom function so we can select files and folders with the
222 * - If existing filename is selected return it
223 * - If filename in (CFileDialog) editbox and current folder doesn't form
224 * a valid path to file, return current folder.
225 * @param [in] parent Handle to parent window. Can be a NULL, but then
226 * CMainFrame is used which can cause modality problems.
227 * @param [out] path Selected folder/filename
228 * @param [in] initialPath Initial file or folder shown/selected.
229 * @return TRUE if user choosed a file/folder, FALSE if user canceled dialog.
231 BOOL SelectFileOrFolder(HWND parent, String& path, LPCTSTR initialPath /*=NULL*/)
233 String title = _("Open");
235 // This will tell common file dialog what to show
236 // and also this will hold its return value
237 TCHAR sSelectedFile[MAX_PATH];
239 // check if specified path is a file
240 if (initialPath && initialPath[0])
242 // If initial path info includes a file
243 // we put the bare filename into sSelectedFile
244 // so the common file dialog will start up with that file selected
245 if (paths_DoesPathExist(initialPath) == IS_EXISTING_FILE)
248 paths_SplitFilename(initialPath, 0, &temp, 0);
249 lstrcpy(sSelectedFile, temp.c_str());
253 String filters = _("All Files (*.*)|*.*||");
255 // Convert extension mask from MFC style separators ('|')
256 // to Win32 style separators ('\0')
257 LPTSTR filtersStr = &*filters.begin();
258 ConvertFilter(filtersStr);
260 String dirSelTag = _("Folder Selection");
262 // Set initial filename to folder selection tag
263 dirSelTag += _T("."); // Treat it as filename
264 lstrcpy(sSelectedFile, dirSelTag.c_str()); // What is assignment above good for?
266 OPENFILENAME_NT4 ofn;
267 memset(&ofn, 0, sizeof(ofn));
268 ofn.lStructSize = sizeof(ofn);
269 ofn.hwndOwner = parent;
270 ofn.lpstrFilter = filtersStr;
271 ofn.lpstrCustomFilter = NULL;
272 ofn.nFilterIndex = 1;
273 ofn.lpstrFile = sSelectedFile;
274 ofn.nMaxFile = MAX_PATH;
275 ofn.lpstrInitialDir = initialPath;
276 ofn.lpstrTitle = title.c_str();
277 ofn.lpstrFileTitle = NULL;
278 ofn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_NOTESTFILECREATE | OFN_NOCHANGEDIR;
280 BOOL bRetVal = GetOpenFileName((OPENFILENAME *)&ofn);
284 path = sSelectedFile;
285 struct _stati64 statBuffer;
286 int nRetVal = _tstati64(path.c_str(), &statBuffer);
289 // We have a valid folder name, but propably garbage as a filename.
290 // Return folder name
291 String folder = paths_GetPathOnly(sSelectedFile);
292 path = paths_AddTrailingSlash(folder);
300 * @brief Helper function for converting filter format.
302 * MFC functions separate filter strings with | char which is also
303 * good choice to safe into resource. But WinAPI32 functions we use
304 * needs '\0' as separator. This function replaces '|'s with '\0's.
306 * @param [in,out] filterStr
307 * - in Mask string to convert
308 * - out Converted string
310 static void ConvertFilter(LPTSTR filterStr)
312 while (TCHAR *ch = _tcschr(filterStr, '|'))