Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / MFCDialog / PathDialog.cpp
blobd7bd4279836b198feb989b17a5909be33db9db90
1 //////////////////////////////////////////////////////////////////////////
2 //PathDialog.h file
3 //
4 //Written by Nguyen Tan Hung <tanhung@yahoo.com>
5 //////////////////////////////////////////////////////////////////////////
7 #include "stdafx.h"
8 #include "PathDialog.h"
9 #include <io.h>
11 #ifdef _DEBUG
12 #define new DEBUG_NEW
13 #undef THIS_FILE
14 static char THIS_FILE[] = __FILE__;
15 #endif
17 #define IDC_FOLDERTREE 0x3741
18 #define IDC_TITLE 0x3742
19 #define IDC_STATUSTEXT 0x3743
21 #define IDC_NEW_EDIT_PATH 0x3744
23 // Class CDlgWnd
24 BEGIN_MESSAGE_MAP(CPathDialogSub, CWnd)
25 ON_BN_CLICKED(IDOK, OnOK)
26 ON_EN_CHANGE(IDC_NEW_EDIT_PATH, OnChangeEditPath)
27 END_MESSAGE_MAP()
29 void CPathDialogSub::OnOK()
31 ::GetWindowText(::GetDlgItem(m_hWnd, IDC_NEW_EDIT_PATH),
32 m_pPathDialog->m_szPathName, MAX_PATH);
34 if(CPathDialog::MakeSurePathExists(m_pPathDialog->m_szPathName)==0)
36 m_pPathDialog->m_bGetSuccess=TRUE;
37 ::EndDialog(m_pPathDialog->m_hWnd, IDOK);
39 else
41 ::SetFocus(::GetDlgItem(m_hWnd, IDC_NEW_EDIT_PATH));
45 void CPathDialogSub::OnChangeEditPath()
47 ::GetWindowText(::GetDlgItem(m_hWnd, IDC_NEW_EDIT_PATH),
48 m_pPathDialog->m_szPathName, MAX_PATH);
49 BOOL bEnableOKButton = (_tcslen(m_pPathDialog->m_szPathName)>0);
50 SendMessage(BFFM_ENABLEOK, 0, bEnableOKButton);
52 /////////////////////////////////////////////////////////////////////////////
53 // CPathDialog dialog
56 CPathDialog::CPathDialog(LPCTSTR lpszCaption,
57 LPCTSTR lpszTitle,
58 LPCTSTR lpszInitialPath,
59 CWnd* pParent)
61 m_hWnd=NULL;
62 m_PathDialogSub.m_pPathDialog= this;
63 m_bParentDisabled = FALSE;
65 // Get the true parent of the dialog
66 m_pParentWnd = CWnd::GetSafeOwner(pParent);
68 m_lpszCaption = lpszCaption;
69 m_lpszInitialPath = lpszInitialPath;
71 memset(&m_bi, 0, sizeof(BROWSEINFO) );
72 m_bi.hwndOwner = (m_pParentWnd==NULL)?NULL:m_pParentWnd->GetSafeHwnd();
73 m_bi.pszDisplayName = 0;
74 m_bi.pidlRoot = 0;
75 m_bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
76 m_bi.lpfn = BrowseCallbackProc;
77 m_bi.lpszTitle = lpszTitle;
81 /////////////////////////////////////////////////////////////////////////////
82 // CPathDialog message handlers
84 CString CPathDialog::GetPathName()
86 return CString(m_szPathName);
89 int CALLBACK CPathDialog::BrowseCallbackProc(HWND hwnd,UINT uMsg,LPARAM lParam, LPARAM pData)
91 CPathDialog* pDlg = (CPathDialog*)pData;
93 switch(uMsg)
95 case BFFM_INITIALIZED:
97 RECT rc;
98 HWND hEdit;
99 HFONT hFont;
101 pDlg->m_hWnd = hwnd;
103 if(pDlg->m_lpszCaption!=NULL)
105 ::SetWindowText(hwnd, pDlg->m_lpszCaption);
108 VERIFY(pDlg->m_PathDialogSub.SubclassWindow(hwnd));
109 ::ShowWindow(::GetDlgItem(hwnd, IDC_STATUSTEXT), SW_HIDE);
110 ::GetWindowRect(::GetDlgItem(hwnd, IDC_FOLDERTREE), &rc);
111 rc.bottom = rc.top - 4;
112 rc.top = rc.bottom - 23;
113 ::ScreenToClient(hwnd, (LPPOINT)&rc);
114 ::ScreenToClient(hwnd, ((LPPOINT)&rc)+1);
115 hEdit = ::CreateWindowEx(WS_EX_CLIENTEDGE, _T("EDIT"), _T(""),
116 WS_CHILD|WS_TABSTOP|WS_VISIBLE|ES_AUTOHSCROLL,
117 rc.left, rc.top,
118 rc.right-rc.left, rc.bottom-rc.top,
119 hwnd, NULL, NULL, NULL);
120 ::SetWindowLong(hEdit, GWL_ID, IDC_NEW_EDIT_PATH);
121 ::ShowWindow(hEdit, SW_SHOW);
123 hFont = (HFONT)::SendMessage(hwnd, WM_GETFONT, 0, 0);
124 ::SendMessage(hEdit, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
126 LPCTSTR lpszPath = pDlg->m_lpszInitialPath;
127 TCHAR szTemp[MAX_PATH];
128 if(lpszPath==NULL)
130 ::GetCurrentDirectory(MAX_PATH, szTemp );
131 lpszPath = szTemp;
133 // WParam is TRUE since you are passing a path.
134 // It would be FALSE if you were passing a pidl.
135 ::SendMessage(hwnd,BFFM_SETSELECTION,TRUE,
136 (LPARAM)lpszPath);
137 break;
139 case BFFM_SELCHANGED:
141 char szSelection[MAX_PATH];
142 if(!::SHGetPathFromIDList((LPITEMIDLIST)lParam, szSelection) ||
143 (szSelection[1] !=':' && szSelection[1] != '\\'))
145 szSelection[0] = '\0';
146 ::SendMessage(hwnd, BFFM_ENABLEOK, 0, FALSE);
148 else
150 ::SendMessage(hwnd, BFFM_ENABLEOK, 0, TRUE);
152 ::SendMessage(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)szSelection);
153 ::SetWindowText(::GetDlgItem(hwnd, IDC_NEW_EDIT_PATH), szSelection);
154 break;
156 default:
157 break;
159 return 0;
164 int CPathDialog::DoModal()
167 /////////////////////////////////////////////////////////
168 TCHAR szPathTemp[MAX_PATH];
169 m_bi.lpfn = BrowseCallbackProc; // address of callback function
170 m_bi.lParam = (LPARAM)this; // pass address of object to callback function
171 m_bi.pszDisplayName = szPathTemp;
173 LPITEMIDLIST pidl;
174 LPMALLOC pMalloc;
176 int iResult=-1;
177 if(SUCCEEDED(SHGetMalloc(&pMalloc)))
179 m_bGetSuccess = FALSE;
180 pidl = SHBrowseForFolder(&m_bi);
181 if (pidl!=NULL)
183 #if defined(_WIN64) && defined(__INTEL_COMPILER)
184 # pragma warning ( disable : 167)
185 #endif
186 // In C++:
187 pMalloc->Free(pidl);
188 //In C:
189 //pMalloc->lpVtbl->Free(pMalloc,pidl);
190 //pMalloc->lpVtbl->Release(pMalloc);
192 if(m_bGetSuccess)
194 iResult = IDOK;
196 pMalloc->Release();
199 if(m_bParentDisabled && (m_pParentWnd!=NULL))
201 m_pParentWnd->EnableWindow(TRUE);
203 m_bParentDisabled=FALSE;
205 return iResult;
208 BOOL CPathDialog::IsFileNameValid(LPCTSTR /* lpFileName */)
210 return TRUE;
213 const TCHAR c_FolderDoesNotExist[] = _T(
214 "The folder:\n\n"
215 "%s\n\n"
216 "does not exist. Do you want the folder to be created?");
217 const TCHAR c_szErrInvalidPath[] = _T(
218 "The folder:"
219 "\n\n"
220 "%s\n\n"
221 "is invalid. Please reenter.");
222 const TCHAR c_szErrCreatePath[] = _T(
223 "The folder:"
224 "\n\n"
225 "%s"
226 "\n\ncan not be created. Please double check.");
228 //return -1: user break;
229 //return 0: no error
230 //return 1: lpPath is invalid
231 //return 2: can not create lpPath
232 int CPathDialog::MakeSurePathExists(LPCTSTR lpPath)
234 CString strMsg;
235 int iRet = -1;
238 //validate path
239 iRet=Touch(lpPath, TRUE);
240 if(iRet!=0)
242 throw iRet;
245 if(_taccess(lpPath, 0)==0)
247 return (int)0;
250 strMsg.Format(c_FolderDoesNotExist, lpPath);
251 if(AfxMessageBox(strMsg, MB_YESNO|MB_ICONQUESTION) == IDYES)
253 //create path
254 iRet=Touch(lpPath, FALSE);
255 if(iRet!=0)
257 throw iRet;
260 return 0;
262 catch(int nErrCode)
264 switch(nErrCode)
266 case 1:
267 strMsg.Format(c_szErrInvalidPath, lpPath);
268 break;
269 case 2:
270 default:
271 strMsg.Format(c_szErrCreatePath, lpPath);
272 break;
275 AfxMessageBox(strMsg, MB_OK|MB_ICONEXCLAMATION);
278 return iRet;
281 //return 0: no error
282 //return 1: lpPath is invalid
283 //return 2: lpPath can not be created(bValidate==FALSE)
284 int CPathDialog::Touch(LPCTSTR lpPath, BOOL bValidate)
286 if(lpPath==NULL)
288 return 1;
291 TCHAR szPath[MAX_PATH];
292 _tcscpy(szPath, lpPath);
293 size_t nLen = _tcslen(szPath);
295 int i;
296 if(nLen==3)
298 if(!bValidate)
300 if(_access(szPath, 0)!=0)
302 return 2;
305 return 0;
308 i = 3;
309 BOOL bLastOne=TRUE;
310 LPTSTR lpCurrentName;
311 while(szPath[i]!=0)
313 lpCurrentName = &szPath[i];
314 while( (szPath[i]!=0) && (szPath[i]!=_T('\\')) )
316 i++;
319 bLastOne =(szPath[i]==0);
320 szPath[i] = 0;
322 if(!bValidate)
324 CreateDirectory(szPath, NULL);
325 if(_taccess(szPath, 0)!=0)
327 return 2;
331 if(bLastOne)
333 break; //it's done
335 else
337 szPath[i] = _T('\\');
340 i++;
343 return (bLastOne?0:1);
346 //return 0: ok
347 //return 1: error
348 int CPathDialog::ConcatPath(LPTSTR lpRoot, LPCTSTR lpMorePath)
350 if(lpRoot==NULL)
352 return 1;
355 size_t nLen = _tcslen(lpRoot);
357 if(nLen<3)
359 return 1;
362 if(lpMorePath==NULL)
364 return 0;
367 if(nLen==3)
369 _tcscat(lpRoot, lpMorePath);
370 return 0;
373 _tcscat(lpRoot, _T("\\"));
374 _tcscat(lpRoot, lpMorePath);
376 return 0;