PathDialog.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. //////////////////////////////////////////////////////////////////////////
  2. //PathDialog.h file
  3. //
  4. //Written by Nguyen Tan Hung <[email protected]>
  5. //////////////////////////////////////////////////////////////////////////
  6. #include "stdafx.h"
  7. #include "PathDialog.h"
  8. #include <io.h>
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. #define IDC_FOLDERTREE 0x3741
  15. #define IDC_TITLE 0x3742
  16. #define IDC_STATUSTEXT 0x3743
  17. #define IDC_NEW_EDIT_PATH 0x3744
  18. // Class CDlgWnd
  19. BEGIN_MESSAGE_MAP(CPathDialogSub, CWnd)
  20. ON_BN_CLICKED(IDOK, OnOK)
  21. ON_EN_CHANGE(IDC_NEW_EDIT_PATH, OnChangeEditPath)
  22. END_MESSAGE_MAP()
  23. void CPathDialogSub::OnOK()
  24. {
  25. ::GetWindowText(::GetDlgItem(m_hWnd, IDC_NEW_EDIT_PATH),
  26. m_pPathDialog->m_szPathName, MAX_PATH);
  27. if(CPathDialog::MakeSurePathExists(m_pPathDialog->m_szPathName)==0)
  28. {
  29. m_pPathDialog->m_bGetSuccess=TRUE;
  30. ::EndDialog(m_pPathDialog->m_hWnd, IDOK);
  31. }
  32. else
  33. {
  34. ::SetFocus(::GetDlgItem(m_hWnd, IDC_NEW_EDIT_PATH));
  35. }
  36. }
  37. void CPathDialogSub::OnChangeEditPath()
  38. {
  39. ::GetWindowText(::GetDlgItem(m_hWnd, IDC_NEW_EDIT_PATH),
  40. m_pPathDialog->m_szPathName, MAX_PATH);
  41. BOOL bEnableOKButton = (_tcslen(m_pPathDialog->m_szPathName)>0);
  42. SendMessage(BFFM_ENABLEOK, 0, bEnableOKButton);
  43. }
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CPathDialog dialog
  46. CPathDialog::CPathDialog(LPCTSTR lpszCaption,
  47. LPCTSTR lpszTitle,
  48. LPCTSTR lpszInitialPath,
  49. CWnd* pParent)
  50. {
  51. m_hWnd=NULL;
  52. m_PathDialogSub.m_pPathDialog= this;
  53. m_bParentDisabled = FALSE;
  54. // Get the true parent of the dialog
  55. m_pParentWnd = CWnd::GetSafeOwner(pParent);
  56. m_lpszCaption = lpszCaption;
  57. m_lpszInitialPath = lpszInitialPath;
  58. memset(&m_bi, 0, sizeof(BROWSEINFO) );
  59. m_bi.hwndOwner = (m_pParentWnd==NULL)?NULL:m_pParentWnd->GetSafeHwnd();
  60. m_bi.pszDisplayName = 0;
  61. m_bi.pidlRoot = 0;
  62. m_bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
  63. m_bi.lpfn = BrowseCallbackProc;
  64. m_bi.lpszTitle = lpszTitle;
  65. }
  66. /////////////////////////////////////////////////////////////////////////////
  67. // CPathDialog message handlers
  68. CString CPathDialog::GetPathName()
  69. {
  70. return CString(m_szPathName);
  71. }
  72. int CALLBACK CPathDialog::BrowseCallbackProc(HWND hwnd,UINT uMsg,LPARAM lParam, LPARAM pData)
  73. {
  74. CPathDialog* pDlg = (CPathDialog*)pData;
  75. switch(uMsg)
  76. {
  77. case BFFM_INITIALIZED:
  78. {
  79. RECT rc;
  80. HWND hEdit;
  81. HFONT hFont;
  82. pDlg->m_hWnd = hwnd;
  83. if(pDlg->m_lpszCaption!=NULL)
  84. {
  85. ::SetWindowText(hwnd, pDlg->m_lpszCaption);
  86. }
  87. VERIFY(pDlg->m_PathDialogSub.SubclassWindow(hwnd));
  88. ::ShowWindow(::GetDlgItem(hwnd, IDC_STATUSTEXT), SW_HIDE);
  89. ::GetWindowRect(::GetDlgItem(hwnd, IDC_FOLDERTREE), &rc);
  90. rc.bottom = rc.top - 4;
  91. rc.top = rc.bottom - 23;
  92. ::ScreenToClient(hwnd, (LPPOINT)&rc);
  93. ::ScreenToClient(hwnd, ((LPPOINT)&rc)+1);
  94. hEdit = ::CreateWindowEx(WS_EX_CLIENTEDGE, _T("EDIT"), _T(""),
  95. WS_CHILD|WS_TABSTOP|WS_VISIBLE|ES_AUTOHSCROLL,
  96. rc.left, rc.top,
  97. rc.right-rc.left, rc.bottom-rc.top,
  98. hwnd, NULL, NULL, NULL);
  99. ::SetWindowLong(hEdit, GWL_ID, IDC_NEW_EDIT_PATH);
  100. ::ShowWindow(hEdit, SW_SHOW);
  101. hFont = (HFONT)::SendMessage(hwnd, WM_GETFONT, 0, 0);
  102. ::SendMessage(hEdit, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
  103. LPCTSTR lpszPath = pDlg->m_lpszInitialPath;
  104. TCHAR szTemp[MAX_PATH];
  105. if(lpszPath==NULL)
  106. {
  107. ::GetCurrentDirectory(MAX_PATH, szTemp );
  108. lpszPath = szTemp;
  109. }
  110. // WParam is TRUE since you are passing a path.
  111. // It would be FALSE if you were passing a pidl.
  112. ::SendMessage(hwnd,BFFM_SETSELECTION,TRUE,
  113. (LPARAM)lpszPath);
  114. break;
  115. }
  116. case BFFM_SELCHANGED:
  117. {
  118. char szSelection[MAX_PATH];
  119. if(!::SHGetPathFromIDList((LPITEMIDLIST)lParam, szSelection) ||
  120. (szSelection[1] !=':' && szSelection[1] != '\\'))
  121. {
  122. szSelection[0] = '\0';
  123. ::SendMessage(hwnd, BFFM_ENABLEOK, 0, FALSE);
  124. }
  125. else
  126. {
  127. ::SendMessage(hwnd, BFFM_ENABLEOK, 0, TRUE);
  128. }
  129. ::SendMessage(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)szSelection);
  130. ::SetWindowText(::GetDlgItem(hwnd, IDC_NEW_EDIT_PATH), szSelection);
  131. break;
  132. }
  133. default:
  134. break;
  135. }
  136. return 0;
  137. }
  138. int CPathDialog::DoModal()
  139. {
  140. /////////////////////////////////////////////////////////
  141. TCHAR szPathTemp[MAX_PATH];
  142. m_bi.lpfn = BrowseCallbackProc; // address of callback function
  143. m_bi.lParam = (LPARAM)this; // pass address of object to callback function
  144. m_bi.pszDisplayName = szPathTemp;
  145. LPITEMIDLIST pidl;
  146. LPMALLOC pMalloc;
  147. int iResult=-1;
  148. if(SUCCEEDED(SHGetMalloc(&pMalloc)))
  149. {
  150. m_bGetSuccess = FALSE;
  151. pidl = SHBrowseForFolder(&m_bi);
  152. if (pidl!=NULL)
  153. {
  154. //not need do this because OnOK function did
  155. //bSucceeded = SHGetPathFromIDList(pidl, m_szPathName);
  156. // In C++:
  157. pMalloc->Free(pidl);
  158. //In C:
  159. //pMalloc->lpVtbl->Free(pMalloc,pidl);
  160. //pMalloc->lpVtbl->Release(pMalloc);
  161. }
  162. if(m_bGetSuccess)
  163. {
  164. iResult = IDOK;
  165. }
  166. pMalloc->Release();
  167. }
  168. if(m_bParentDisabled && (m_pParentWnd!=NULL))
  169. {
  170. m_pParentWnd->EnableWindow(TRUE);
  171. }
  172. m_bParentDisabled=FALSE;
  173. return iResult;
  174. }
  175. BOOL CPathDialog::IsFileNameValid(LPCTSTR lpFileName)
  176. {
  177. return TRUE;
  178. }
  179. const TCHAR c_FolderDoesNotExist[] = _T(
  180. "The folder:\n\n"
  181. "%s\n\n"
  182. "does not exist. Do you want the folder to be created?");
  183. const TCHAR c_szErrInvalidPath[] = _T(
  184. "The folder:"
  185. "\n\n"
  186. "%s\n\n"
  187. "is invalid. Please reenter.");
  188. const TCHAR c_szErrCreatePath[] = _T(
  189. "The folder:"
  190. "\n\n"
  191. "%s"
  192. "\n\ncan not be created. Please double check.");
  193. //return -1: user break;
  194. //return 0: no error
  195. //return 1: lpPath is invalid
  196. //return 2: can not create lpPath
  197. int CPathDialog::MakeSurePathExists(LPCTSTR lpPath)
  198. {
  199. CString strMsg;
  200. int iRet;
  201. try
  202. {
  203. //validate path
  204. iRet=Touch(lpPath, TRUE);
  205. if(iRet!=0)
  206. {
  207. throw iRet;
  208. }
  209. if(_taccess(lpPath, 0)==0)
  210. {
  211. return (int)0;
  212. }
  213. strMsg.Format(c_FolderDoesNotExist, lpPath);
  214. if(AfxMessageBox(strMsg, MB_YESNO|MB_ICONQUESTION) != IDYES)
  215. {
  216. return (int)-1;
  217. }
  218. //create path
  219. iRet=Touch(lpPath, FALSE);
  220. if(iRet!=0)
  221. {
  222. throw iRet;
  223. }
  224. return 0;
  225. }
  226. catch(int nErrCode)
  227. {
  228. switch(nErrCode)
  229. {
  230. case 1:
  231. strMsg.Format(c_szErrInvalidPath, lpPath);
  232. break;
  233. case 2:
  234. default:
  235. strMsg.Format(c_szErrCreatePath, lpPath);
  236. break;
  237. }
  238. AfxMessageBox(strMsg, MB_OK|MB_ICONEXCLAMATION);
  239. }
  240. return iRet;
  241. }
  242. //return 0: no error
  243. //return 1: lpPath is invalid
  244. //return 2: lpPath can not be created(bValidate==FALSE)
  245. int CPathDialog::Touch(LPCTSTR lpPath, BOOL bValidate)
  246. {
  247. if(lpPath==NULL)
  248. {
  249. return 1;
  250. }
  251. TCHAR szPath[MAX_PATH];
  252. _tcscpy(szPath, lpPath);
  253. int nLen = _tcslen(szPath);
  254. int i;
  255. if(nLen==3)
  256. {
  257. if(!bValidate)
  258. {
  259. if(_access(szPath, 0)!=0)
  260. {
  261. return 2;
  262. }
  263. }
  264. return 0;
  265. }
  266. i = 3;
  267. BOOL bLastOne=TRUE;
  268. LPTSTR lpCurrentName;
  269. while(szPath[i]!=0)
  270. {
  271. lpCurrentName = &szPath[i];
  272. while( (szPath[i]!=0) && (szPath[i]!=_T('\\')) )
  273. {
  274. i++;
  275. }
  276. bLastOne =(szPath[i]==0);
  277. szPath[i] = 0;
  278. if(!bValidate)
  279. {
  280. CreateDirectory(szPath, NULL);
  281. if(_taccess(szPath, 0)!=0)
  282. {
  283. return 2;
  284. }
  285. }
  286. if(bLastOne)
  287. {
  288. break; //it's done
  289. }
  290. else
  291. {
  292. szPath[i] = _T('\\');
  293. }
  294. i++;
  295. }
  296. return (bLastOne?0:1);
  297. }
  298. //return 0: ok
  299. //return 1: error
  300. int CPathDialog::ConcatPath(LPTSTR lpRoot, LPCTSTR lpMorePath)
  301. {
  302. if(lpRoot==NULL)
  303. {
  304. return 1;
  305. }
  306. int nLen = _tcslen(lpRoot);
  307. if(nLen<3)
  308. {
  309. return 1;
  310. }
  311. if(lpMorePath==NULL)
  312. {
  313. return 0;
  314. }
  315. if(nLen==3)
  316. {
  317. _tcscat(lpRoot, lpMorePath);
  318. return 0;
  319. }
  320. _tcscat(lpRoot, _T("\\"));
  321. _tcscat(lpRoot, lpMorePath);
  322. return 0;
  323. }